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