• 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.android.integrationtest;
18 
19 import static org.junit.Assert.assertEquals;
20 
21 import android.util.Log;
22 import androidx.test.core.app.ApplicationProvider;
23 import androidx.test.ext.junit.runners.AndroidJUnit4;
24 import androidx.test.platform.app.InstrumentationRegistry;
25 import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
26 import com.google.android.gms.common.GooglePlayServicesRepairableException;
27 import com.google.android.gms.security.ProviderInstaller;
28 import com.google.common.util.concurrent.SettableFuture;
29 import io.grpc.android.integrationtest.InteropTask.Listener;
30 import java.io.InputStream;
31 import java.util.concurrent.TimeUnit;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 
36 @RunWith(AndroidJUnit4.class)
37 public class InteropInstrumentationTest {
38   private static final int TIMEOUT_SECONDS = 60;
39   private static final String LOG_TAG = "GrpcInteropInstrumentationTest";
40 
41   private String host;
42   private int port;
43   private boolean useTls;
44   private String serverHostOverride;
45   private boolean useTestCa;
46   private String testCase;
47 
48   @Before
setUp()49   public void setUp() throws Exception {
50     host = InstrumentationRegistry.getArguments().getString("server_host", "10.0.2.2");
51     port =
52         Integer.parseInt(InstrumentationRegistry.getArguments().getString("server_port", "8080"));
53     useTls =
54         Boolean.parseBoolean(InstrumentationRegistry.getArguments().getString("use_tls", "true"));
55     serverHostOverride = InstrumentationRegistry.getArguments().getString("server_host_override");
56     useTestCa =
57         Boolean.parseBoolean(
58             InstrumentationRegistry.getArguments().getString("use_test_ca", "false"));
59     testCase = InstrumentationRegistry.getArguments().getString("test_case", "all");
60 
61     if (useTls) {
62       try {
63         ProviderInstaller.installIfNeeded(ApplicationProvider.getApplicationContext());
64       } catch (GooglePlayServicesRepairableException e) {
65         // The provider is helpful, but it is possible to succeed without it.
66         // Hope that the system-provided libraries are new enough.
67         Log.i(LOG_TAG, "Failed installing security provider", e);
68       } catch (GooglePlayServicesNotAvailableException e) {
69         // The provider is helpful, but it is possible to succeed without it.
70         // Hope that the system-provided libraries are new enough.
71         Log.i(LOG_TAG, "Failed installing security provider", e);
72       }
73     }
74   }
75 
76   @Test
interopTests()77   public void interopTests() throws Exception {
78     if (testCase.equals("all")) {
79       runTest("empty_unary");
80       runTest("large_unary");
81       runTest("client_streaming");
82       runTest("server_streaming");
83       runTest("ping_pong");
84       runTest("empty_stream");
85       runTest("cancel_after_begin");
86       runTest("cancel_after_first_response");
87       runTest("full_duplex_call_should_succeed");
88       runTest("half_duplex_call_should_succeed");
89       runTest("server_streaming_should_be_flow_controlled");
90       runTest("very_large_request");
91       runTest("very_large_response");
92       runTest("deadline_not_exceeded");
93       runTest("deadline_exceeded");
94       runTest("deadline_exceeded_server_streaming");
95       runTest("unimplemented_method");
96       runTest("timeout_on_sleeping_server");
97       runTest("graceful_shutdown");
98     } else {
99       runTest(testCase);
100     }
101   }
102 
runTest(String testCase)103   private void runTest(String testCase) throws Exception {
104     final SettableFuture<String> resultFuture = SettableFuture.create();
105     InteropTask.Listener listener =
106         new Listener() {
107           @Override
108           public void onComplete(String result) {
109             resultFuture.set(result);
110           }
111         };
112     InputStream testCa;
113     if (useTestCa) {
114       testCa = ApplicationProvider.getApplicationContext().getResources().openRawResource(R.raw.ca);
115     } else {
116       testCa = null;
117     }
118     new InteropTask(
119             listener,
120             TesterOkHttpChannelBuilder.build(host, port, serverHostOverride, useTls, testCa),
121             testCase)
122         .execute();
123     String result = resultFuture.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
124     assertEquals(testCase + " failed", InteropTask.SUCCESS_MESSAGE, result);
125   }
126 }
127