• 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.ManagedChannel;
22 import io.grpc.ManagedChannelBuilder;
23 import io.grpc.Server;
24 import io.grpc.ServerBuilder;
25 import io.grpc.internal.testing.TestUtils;
26 import io.grpc.netty.shaded.io.grpc.netty.GrpcSslContexts;
27 import io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder;
28 import io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder;
29 import io.grpc.netty.shaded.io.netty.handler.ssl.SslContextBuilder;
30 import io.grpc.netty.shaded.io.netty.handler.ssl.SslProvider;
31 import io.grpc.stub.StreamObserver;
32 import io.grpc.testing.protobuf.SimpleRequest;
33 import io.grpc.testing.protobuf.SimpleResponse;
34 import io.grpc.testing.protobuf.SimpleServiceGrpc;
35 import io.grpc.testing.protobuf.SimpleServiceGrpc.SimpleServiceBlockingStub;
36 import io.grpc.testing.protobuf.SimpleServiceGrpc.SimpleServiceImplBase;
37 import java.util.concurrent.TimeUnit;
38 import org.junit.After;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.junit.runners.JUnit4;
42 
43 /** Unit tests for {@link Shading}. */
44 @RunWith(JUnit4.class)
45 public final class ShadingTest {
46   private ManagedChannel channel;
47   private Server server;
48 
49   @After
tearDown()50   public void tearDown() throws Exception {
51     if (channel != null) {
52       channel.shutdownNow();
53       channel.awaitTermination(1, TimeUnit.SECONDS);
54     }
55     if (server != null) {
56       server.shutdownNow();
57       server.awaitTermination(1, TimeUnit.SECONDS);
58     }
59   }
60 
61   /** Verify that normal Netty didn't leak into the test runtime. */
62   @Test(expected = ClassNotFoundException.class)
noNormalNetty()63   public void noNormalNetty() throws Exception {
64     Class.forName("io.grpc.netty.NettyServerBuilder");
65   }
66 
67   @Test
serviceLoaderFindsNetty()68   public void serviceLoaderFindsNetty() throws Exception {
69     assertThat(ServerBuilder.forPort(0)).isInstanceOf(NettyServerBuilder.class);
70     assertThat(ManagedChannelBuilder.forAddress("localhost", 1234))
71         .isInstanceOf(NettyChannelBuilder.class);
72   }
73 
74   @Test
basic()75   public void basic() throws Exception {
76     server = ServerBuilder.forPort(0)
77         .addService(new SimpleServiceImpl())
78         .build().start();
79     channel = ManagedChannelBuilder
80         .forAddress("localhost", server.getPort())
81         .usePlaintext()
82         .build();
83     SimpleServiceBlockingStub stub = SimpleServiceGrpc.newBlockingStub(channel);
84     assertThat(SimpleResponse.getDefaultInstance())
85         .isEqualTo(stub.unaryRpc(SimpleRequest.getDefaultInstance()));
86   }
87 
88   @Test
tcnative()89   public void tcnative() throws Exception {
90     server = NettyServerBuilder.forPort(0)
91         .useTransportSecurity(TestUtils.loadCert("server1.pem"), TestUtils.loadCert("server1.key"))
92         .addService(new SimpleServiceImpl())
93         .build().start();
94     channel = NettyChannelBuilder
95         .forAddress("localhost", server.getPort())
96         .sslContext(
97             GrpcSslContexts.configure(SslContextBuilder.forClient(), SslProvider.OPENSSL)
98                 .trustManager(TestUtils.loadCert("ca.pem")).build())
99         .overrideAuthority("foo.test.google.fr")
100         .build();
101     SimpleServiceBlockingStub stub = SimpleServiceGrpc.newBlockingStub(channel);
102     assertThat(SimpleResponse.getDefaultInstance())
103         .isEqualTo(stub.unaryRpc(SimpleRequest.getDefaultInstance()));
104   }
105 
106   private static class SimpleServiceImpl extends SimpleServiceImplBase {
unaryRpc(SimpleRequest req, StreamObserver<SimpleResponse> obs)107     @Override public void unaryRpc(SimpleRequest req, StreamObserver<SimpleResponse> obs) {
108       obs.onNext(SimpleResponse.getDefaultInstance());
109       obs.onCompleted();
110     }
111   }
112 }
113