• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 The gRPC Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.grpc.netty.shaded;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import io.grpc.ChannelCredentials;
22 import io.grpc.Grpc;
23 import io.grpc.InsecureChannelCredentials;
24 import io.grpc.InsecureServerCredentials;
25 import io.grpc.ManagedChannel;
26 import io.grpc.Server;
27 import io.grpc.ServerCredentials;
28 import io.grpc.TlsServerCredentials;
29 import io.grpc.netty.shaded.io.grpc.netty.GrpcSslContexts;
30 import io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder;
31 import io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder;
32 import io.grpc.netty.shaded.io.grpc.netty.NettySslContextChannelCredentials;
33 import io.grpc.netty.shaded.io.netty.handler.ssl.SslContextBuilder;
34 import io.grpc.netty.shaded.io.netty.handler.ssl.SslProvider;
35 import io.grpc.stub.StreamObserver;
36 import io.grpc.testing.TlsTesting;
37 import io.grpc.testing.protobuf.SimpleRequest;
38 import io.grpc.testing.protobuf.SimpleResponse;
39 import io.grpc.testing.protobuf.SimpleServiceGrpc;
40 import io.grpc.testing.protobuf.SimpleServiceGrpc.SimpleServiceBlockingStub;
41 import io.grpc.testing.protobuf.SimpleServiceGrpc.SimpleServiceImplBase;
42 import java.io.IOException;
43 import java.io.InputStream;
44 import java.nio.charset.StandardCharsets;
45 import java.util.Scanner;
46 import java.util.concurrent.TimeUnit;
47 import org.junit.After;
48 import org.junit.Test;
49 import org.junit.runner.RunWith;
50 import org.junit.runners.JUnit4;
51 
52 /** Unit tests for shaded gRPC Netty. */
53 @RunWith(JUnit4.class)
54 public final class ShadingTest {
55   private ManagedChannel channel;
56   private Server server;
57 
58   @After
tearDown()59   public void tearDown() throws Exception {
60     if (channel != null) {
61       channel.shutdownNow();
62       channel.awaitTermination(1, TimeUnit.SECONDS);
63     }
64     if (server != null) {
65       server.shutdownNow();
66       server.awaitTermination(1, TimeUnit.SECONDS);
67     }
68   }
69 
70   /** Verify that normal Netty didn't leak into the test runtime. */
71   @Test(expected = ClassNotFoundException.class)
noNormalNetty()72   public void noNormalNetty() throws Exception {
73     Class.forName("io.grpc.netty.NettyServerBuilder");
74   }
75 
76   /** Verify that resources under META-INF/native-image reference shaded class names. */
77   @Test
nettyResourcesUpdated()78   public void nettyResourcesUpdated() throws IOException {
79     InputStream inputStream = NettyChannelBuilder.class.getClassLoader()
80         .getResourceAsStream(
81             "META-INF/native-image/io.grpc.netty.shaded.io.netty/netty-transport/"
82                 + "reflection-config.json");
83     assertThat(inputStream).isNotNull();
84 
85     Scanner s = new Scanner(inputStream, StandardCharsets.UTF_8.name()).useDelimiter("\\A");
86     String reflectionConfig = s.hasNext() ? s.next() : "";
87 
88     assertThat(reflectionConfig).contains("io.grpc.netty.shaded.io.netty");
89   }
90 
91   @Test
serviceLoaderFindsNetty()92   public void serviceLoaderFindsNetty() throws Exception {
93     assertThat(Grpc.newServerBuilderForPort(0, InsecureServerCredentials.create()))
94         .isInstanceOf(NettyServerBuilder.class);
95     assertThat(Grpc.newChannelBuilder("localhost:1234", InsecureChannelCredentials.create()))
96         .isInstanceOf(NettyChannelBuilder.class);
97   }
98 
99   @Test
basic()100   public void basic() throws Exception {
101     server = Grpc.newServerBuilderForPort(0, InsecureServerCredentials.create())
102         .addService(new SimpleServiceImpl())
103         .build().start();
104     channel = Grpc.newChannelBuilder(
105           "localhost:" + server.getPort(), InsecureChannelCredentials.create())
106         .build();
107     SimpleServiceBlockingStub stub = SimpleServiceGrpc.newBlockingStub(channel);
108     assertThat(SimpleResponse.getDefaultInstance())
109         .isEqualTo(stub.unaryRpc(SimpleRequest.getDefaultInstance()));
110   }
111 
112   @Test
tcnative()113   public void tcnative() throws Exception {
114     ServerCredentials serverCreds = TlsServerCredentials.create(
115         TlsTesting.loadCert("server1.pem"), TlsTesting.loadCert("server1.key"));
116     server = Grpc.newServerBuilderForPort(0, serverCreds)
117         .addService(new SimpleServiceImpl())
118         .build().start();
119     ChannelCredentials creds = NettySslContextChannelCredentials.create(
120         GrpcSslContexts.configure(SslContextBuilder.forClient(), SslProvider.OPENSSL)
121             .trustManager(TlsTesting.loadCert("ca.pem")).build());
122     channel = Grpc.newChannelBuilder("localhost:" + server.getPort(), creds)
123         .overrideAuthority("foo.test.google.fr")
124         .build();
125     SimpleServiceBlockingStub stub = SimpleServiceGrpc.newBlockingStub(channel);
126     assertThat(SimpleResponse.getDefaultInstance())
127         .isEqualTo(stub.unaryRpc(SimpleRequest.getDefaultInstance()));
128   }
129 
130   private static class SimpleServiceImpl extends SimpleServiceImplBase {
unaryRpc(SimpleRequest req, StreamObserver<SimpleResponse> obs)131     @Override public void unaryRpc(SimpleRequest req, StreamObserver<SimpleResponse> obs) {
132       obs.onNext(SimpleResponse.getDefaultInstance());
133       obs.onCompleted();
134     }
135   }
136 }
137