-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add null checks to generated GRPC API (#2455)
Motivation: At the moment the generated GRPC code does not perform input validation in all necessary methods and might pass invalid/null inputs down into the GRPC layer where it is harder to track down what went wrong in the first place. Modifications: This changeset adds explicit requireNonNull to all generated methods which accept user input and adds a test case to verify that the requireNonNull is performed at the earliest point in time and not deeper in the call stack. Result: Early argument null checks to improve debugability in case of invalid argument values.
- Loading branch information
Showing
2 changed files
with
66 additions
and
1 deletion.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
servicetalk-grpc-netty/src/test/java/io/servicetalk/grpc/netty/GrpcInputValidationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright © 2022 Apple Inc. and the ServiceTalk project authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.servicetalk.grpc.netty; | ||
|
||
import io.servicetalk.grpc.api.GrpcClientMetadata; | ||
|
||
import io.grpc.examples.helloworld.Greeter; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.function.Executable; | ||
|
||
import static io.servicetalk.transport.netty.internal.AddressUtils.localAddress; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
class GrpcInputValidationTest { | ||
|
||
@Test | ||
void verifyClientEarlyNonNullArgumentsCheck() throws Exception { | ||
try (Greeter.BlockingGreeterClient client = GrpcClients | ||
.forAddress("localhost", 0) | ||
.buildBlocking(new Greeter.ClientFactory())) { | ||
|
||
assertEarlyRequireNonNull(() -> client.sayHello(null)); | ||
assertEarlyRequireNonNull(() -> client.sayHello((GrpcClientMetadata) null, null)); | ||
} | ||
} | ||
|
||
@Test | ||
void verifyServiceEarlyNonNullArgumentsCheck() { | ||
assertEarlyRequireNonNull(() -> GrpcServers | ||
.forAddress(localAddress(0)) | ||
.listenAndAwait(new Greeter.ServiceFactory.Builder().sayHello(null).build())); | ||
|
||
assertEarlyRequireNonNull(() -> GrpcServers | ||
.forAddress(localAddress(0)) | ||
.listenAndAwait(new Greeter.ServiceFactory.Builder().sayHello(null, null).build())); | ||
} | ||
|
||
private static void assertEarlyRequireNonNull(final Executable executable) { | ||
NullPointerException ex = assertThrows(NullPointerException.class, executable); | ||
assertEquals("requireNonNull", ex.getStackTrace()[0].getMethodName()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters