• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 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.testing.integration;
18 
19 import static org.junit.Assert.assertEquals;
20 
21 import com.google.protobuf.ByteString;
22 import io.grpc.ChannelCredentials;
23 import io.grpc.Grpc;
24 import io.grpc.ManagedChannel;
25 import io.grpc.Server;
26 import io.grpc.ServerCredentials;
27 import io.grpc.alts.AltsChannelCredentials;
28 import io.grpc.alts.AltsServerCredentials;
29 import io.grpc.netty.NettyServerBuilder;
30 import io.grpc.stub.StreamObserver;
31 import io.grpc.testing.GrpcCleanupRule;
32 import io.grpc.testing.integration.Messages.Payload;
33 import io.grpc.testing.integration.Messages.SimpleRequest;
34 import io.grpc.testing.integration.Messages.SimpleResponse;
35 import io.netty.channel.nio.NioEventLoopGroup;
36 import io.netty.channel.socket.nio.NioServerSocketChannel;
37 import io.netty.util.concurrent.DefaultThreadFactory;
38 import org.junit.Before;
39 import org.junit.Rule;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.junit.runners.JUnit4;
43 
44 @RunWith(JUnit4.class)
45 public class AltsHandshakerTest {
46   @Rule
47   public final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule();
48   private Server handshakerServer;
49   private Server testServer;
50   private ManagedChannel channel;
51 
startAltsServer()52   private void startAltsServer() throws Exception {
53     ServerCredentials serverCredentials = AltsServerCredentials.newBuilder()
54         .enableUntrustedAltsForTesting()
55         .setHandshakerAddressForTesting("localhost:" + handshakerServer.getPort())
56         .build();
57     testServer = grpcCleanup.register(
58         Grpc.newServerBuilderForPort(0, serverCredentials)
59             .addService(new TestServiceGrpc.TestServiceImplBase() {
60               @Override
61               public void unaryCall(SimpleRequest request, StreamObserver<SimpleResponse> so) {
62                 so.onNext(SimpleResponse.getDefaultInstance());
63                 so.onCompleted();
64               }
65             })
66             .build())
67         .start();
68   }
69 
70   @Before
setup()71   public void setup() throws Exception {
72     // create new EventLoopGroups to avoid deadlock at server side handshake negotiation, e.g.
73     // happens when handshakerServer and testServer child channels are on the same eventloop.
74     handshakerServer = grpcCleanup.register(NettyServerBuilder.forPort(0)
75         .bossEventLoopGroup(
76             new NioEventLoopGroup(0, new DefaultThreadFactory("test-alts-boss")))
77         .workerEventLoopGroup(
78             new NioEventLoopGroup(0, new DefaultThreadFactory("test-alts-worker")))
79         .channelType(NioServerSocketChannel.class)
80         .addService(new AltsHandshakerTestService())
81         .build()).start();
82     startAltsServer();
83 
84     ChannelCredentials channelCredentials = AltsChannelCredentials.newBuilder()
85         .enableUntrustedAltsForTesting()
86         .setHandshakerAddressForTesting("localhost:" + handshakerServer.getPort()).build();
87     channel = grpcCleanup.register(
88         Grpc.newChannelBuilderForAddress("localhost", testServer.getPort(), channelCredentials)
89             .build());
90   }
91 
92   @Test
testAlts()93   public void testAlts() {
94     TestServiceGrpc.TestServiceBlockingStub blockingStub = TestServiceGrpc.newBlockingStub(channel);
95     final SimpleRequest request = SimpleRequest.newBuilder()
96             .setPayload(Payload.newBuilder().setBody(ByteString.copyFrom(new byte[10])))
97             .build();
98     assertEquals(SimpleResponse.getDefaultInstance(), blockingStub.unaryCall(request));
99   }
100 }
101