|
| 1 | +package io.vertx.grpc.it; |
| 2 | + |
| 3 | +import io.grpc.examples.helloworld.GreeterGrpcClient; |
| 4 | +import io.grpc.examples.helloworld.GreeterGrpcService; |
| 5 | +import io.grpc.examples.helloworld.HelloReply; |
| 6 | +import io.grpc.examples.helloworld.HelloRequest; |
| 7 | +import io.netty.util.internal.PlatformDependent; |
| 8 | +import io.vertx.core.http.HttpConnection; |
| 9 | +import io.vertx.core.http.HttpServerOptions; |
| 10 | +import io.vertx.core.net.ClientSSLOptions; |
| 11 | +import io.vertx.core.net.SocketAddress; |
| 12 | +import io.vertx.ext.unit.TestContext; |
| 13 | +import io.vertx.grpc.client.GrpcClient; |
| 14 | +import io.vertx.grpc.server.GrpcServer; |
| 15 | +import io.vertx.test.core.TestUtils; |
| 16 | +import io.vertx.test.tls.Cert; |
| 17 | +import io.vertx.tests.common.GrpcTestBase; |
| 18 | +import org.junit.Assume; |
| 19 | +import org.junit.Test; |
| 20 | + |
| 21 | +import java.io.File; |
| 22 | + |
| 23 | +import static org.junit.Assert.assertEquals; |
| 24 | + |
| 25 | +public class DomainSocketTest extends GrpcTestBase { |
| 26 | + |
| 27 | + @Test |
| 28 | + public void testPlainDomainSocket(TestContext ctx) throws Exception { |
| 29 | + testDomainSocket(ctx, false); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void testTlsDomainSocket(TestContext ctx) throws Exception { |
| 34 | + testDomainSocket(ctx, true); |
| 35 | + } |
| 36 | + |
| 37 | + private void testDomainSocket(TestContext ctx, boolean ssl) throws Exception { |
| 38 | + Assume.assumeTrue(PlatformDependent.javaVersion() >= 16); |
| 39 | + File sockFile = TestUtils.tmpFile(".sock"); |
| 40 | + SocketAddress sockAddress = SocketAddress.domainSocketAddress(sockFile.getAbsolutePath()); |
| 41 | + HttpServerOptions serverOptions = new HttpServerOptions() |
| 42 | + .setUseAlpn(true) |
| 43 | + .setSsl(ssl) |
| 44 | + .setKeyCertOptions(Cert.SERVER_JKS.get()); |
| 45 | + vertx |
| 46 | + .createHttpServer(serverOptions) |
| 47 | + .requestHandler(GrpcServer |
| 48 | + .server(vertx) |
| 49 | + .callHandler(GreeterGrpcService.SayHello, request -> { |
| 50 | + HttpConnection connection = request.connection(); |
| 51 | + ctx.assertEquals(ssl, connection.isSsl()); |
| 52 | + ctx.assertTrue(connection.remoteAddress().isDomainSocket()); |
| 53 | + if (ssl) { |
| 54 | + ctx.assertNull(connection.sslSession().getPeerHost()); |
| 55 | + } |
| 56 | + request.handler(hello -> { |
| 57 | + HelloReply reply = HelloReply.newBuilder().setMessage("Hello " + hello.getName()).build(); |
| 58 | + request.response().end(reply); |
| 59 | + }); |
| 60 | + })) |
| 61 | + .listen(sockAddress) |
| 62 | + .await(); |
| 63 | + GrpcClient client; |
| 64 | + if (ssl) { |
| 65 | + // Disable hostname verification because there is no host:port to verify the server validity against |
| 66 | + client = GrpcClient.client(vertx, new ClientSSLOptions() |
| 67 | + .setTrustAll(true) |
| 68 | + .setHostnameVerificationAlgorithm("")); |
| 69 | + } else { |
| 70 | + client = GrpcClient.client(vertx); |
| 71 | + } |
| 72 | + GreeterGrpcClient greeterClient = GreeterGrpcClient.create(client, sockAddress); |
| 73 | + HelloReply reply = greeterClient.sayHello(HelloRequest.newBuilder().setName("Julien").build()).await(); |
| 74 | + assertEquals("Hello Julien", reply.getMessage()); |
| 75 | + } |
| 76 | +} |
0 commit comments