• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 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.android.integrationtest;
18 
19 import androidx.annotation.Nullable;
20 import io.grpc.ChannelCredentials;
21 import io.grpc.Grpc;
22 import io.grpc.InsecureChannelCredentials;
23 import io.grpc.ManagedChannel;
24 import io.grpc.ManagedChannelBuilder;
25 import io.grpc.TlsChannelCredentials;
26 import io.grpc.okhttp.OkHttpChannelBuilder;
27 import java.io.InputStream;
28 
29 /**
30  * A helper class to create a OkHttp based channel.
31  */
32 class TesterOkHttpChannelBuilder {
build( String host, int port, @Nullable String serverHostOverride, boolean useTls, @Nullable InputStream testCa)33   public static ManagedChannel build(
34       String host,
35       int port,
36       @Nullable String serverHostOverride,
37       boolean useTls,
38       @Nullable InputStream testCa) {
39     ChannelCredentials credentials;
40     if (useTls) {
41       if (testCa == null) {
42         credentials = TlsChannelCredentials.create();
43       } else {
44         try {
45           credentials = TlsChannelCredentials.newBuilder().trustManager(testCa).build();
46         } catch (Exception e) {
47           throw new RuntimeException(e);
48         }
49       }
50     } else {
51       credentials = InsecureChannelCredentials.create();
52     }
53 
54     ManagedChannelBuilder<?> channelBuilder = Grpc.newChannelBuilderForAddress(
55           host, port, credentials)
56         .maxInboundMessageSize(16 * 1024 * 1024);
57     if (!(channelBuilder instanceof OkHttpChannelBuilder)) {
58       throw new RuntimeException("Did not receive an OkHttpChannelBuilder");
59     }
60     if (serverHostOverride != null) {
61       // Force the hostname to match the cert the server uses.
62       channelBuilder.overrideAuthority(serverHostOverride);
63     }
64     return channelBuilder.build();
65   }
66 }
67