Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions android-interop-testing/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ android {
disable 'InvalidPackage', 'HardcodedText',
'MissingClass' // https://github.com/grpc/grpc-java/issues/8799
}
packagingOptions {
exclude 'META-INF/INDEX.LIST'
exclude 'META-INF/io.netty.versions.properties'
}
}

dependencies {
Expand All @@ -59,7 +63,8 @@ dependencies {
implementation libraries.androidx.annotation
implementation 'com.google.android.gms:play-services-base:18.0.1'

implementation project(':grpc-auth'),
implementation project(':grpc-android'),
project(':grpc-auth'),
project(':grpc-census'),
project(':grpc-okhttp'),
project(':grpc-protobuf-lite'),
Expand All @@ -68,6 +73,7 @@ dependencies {
libraries.hdrhistogram,
libraries.junit,
libraries.truth,
libraries.androidx.test.rules,
libraries.opencensus.contrib.grpc.metrics

implementation (libraries.google.auth.oauth2Http) {
Expand All @@ -80,7 +86,8 @@ dependencies {

compileOnly libraries.javax.annotation

androidTestImplementation 'androidx.test.ext:junit:1.1.3',
androidTestImplementation project(':grpc-netty'),
'androidx.test.ext:junit:1.1.3',
'androidx.test:runner:1.4.0'
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* Copyright 2021 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.grpc.android.integrationtest;

import static java.util.concurrent.TimeUnit.SECONDS;
import static org.junit.Assert.assertEquals;

import android.net.LocalSocketAddress.Namespace;
import androidx.test.InstrumentationRegistry;
import androidx.test.rule.ActivityTestRule;
import androidx.test.runner.AndroidJUnit4;
import com.google.common.util.concurrent.SettableFuture;
import io.grpc.Server;
import io.grpc.android.UdsChannelBuilder;
import io.grpc.android.integrationtest.InteropTask.Listener;
import io.grpc.netty.NettyServerBuilder;
import io.grpc.testing.integration.TestServiceImpl;
import java.io.IOException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
* Tests for channels created with {@link UdsChannelBuilder}. The UDS Channel is only meant to talk
* to Unix Domain Socket endpoints on servers that are on-device, so a {@link LocalTestServer} is
* set up to expose a UDS endpoint.
*/
@RunWith(AndroidJUnit4.class)
public class UdsChannelInteropTest {
private static final int TIMEOUT_SECONDS = 150;

private static final String UDS_PATH = "udspath";
private String testCase;

private Server server;
private UdsTcpEndpointConnector endpointConnector;

private ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);

// Ensures Looper is initialized for tests running on API level 15. Otherwise instantiating an
// AsyncTask throws an exception.
@Rule
public ActivityTestRule<TesterActivity> activityRule =
new ActivityTestRule<TesterActivity>(TesterActivity.class);

@Before
public void setUp() throws IOException {
testCase = InstrumentationRegistry.getArguments().getString("test_case", "all");

// Start local server.
server =
NettyServerBuilder.forPort(0)
.maxInboundMessageSize(16 * 1024 * 1024)
.addService(new TestServiceImpl(executor))
.build();
server.start();

// Connect uds endpoint to server's endpoint.
endpointConnector = new UdsTcpEndpointConnector(UDS_PATH, "0.0.0.0", server.getPort());
endpointConnector.start();
}

@After
public void teardown() {
server.shutdownNow();
endpointConnector.shutDown();
}

@Test
public void interopTests() throws Exception {
if (testCase.equals("all")) {
runTest("empty_unary");
runTest("large_unary");
runTest("client_streaming");
runTest("server_streaming");
runTest("ping_pong");
runTest("empty_stream");
runTest("cancel_after_begin");
runTest("cancel_after_first_response");
runTest("full_duplex_call_should_succeed");
runTest("half_duplex_call_should_succeed");
runTest("server_streaming_should_be_flow_controlled");
runTest("very_large_request");
runTest("very_large_response");
runTest("deadline_not_exceeded");
runTest("deadline_exceeded");
runTest("deadline_exceeded_server_streaming");
runTest("unimplemented_method");
runTest("timeout_on_sleeping_server");
runTest("graceful_shutdown");
} else {
runTest(testCase);
}
}

private void runTest(String testCase) throws Exception {
final SettableFuture<String> resultFuture = SettableFuture.create();
InteropTask.Listener listener =
new Listener() {
@Override
public void onComplete(String result) {
resultFuture.set(result);
}
};

new InteropTask(
listener,
UdsChannelBuilder.forPath(UDS_PATH, Namespace.ABSTRACT)
.maxInboundMessageSize(16 * 1024 * 1024)
.build(),
testCase)
.execute();
String result = resultFuture.get(TIMEOUT_SECONDS, SECONDS);
assertEquals(testCase + " failed", InteropTask.SUCCESS_MESSAGE, result);
}
}
5 changes: 4 additions & 1 deletion android-interop-testing/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
package="io.grpc.android.integrationtest" >

<uses-permission android:name="android.permission.INTERNET" />
<!-- For UDS -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Base.V7.Theme.AppCompat.Light"
android:name="androidx.multidex.MultiDexApplication" >
android:name="androidx.multidex.MultiDexApplication">
<activity
android:name=".TesterActivity"
android:label="@string/app_name" >
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import android.content.Context;
import android.content.Intent;
import android.net.LocalSocketAddress.Namespace;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
Expand All @@ -30,6 +31,8 @@
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.security.ProviderInstaller;
import io.grpc.ManagedChannel;
import io.grpc.android.UdsChannelBuilder;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -41,9 +44,13 @@ public class TesterActivity extends AppCompatActivity
private List<Button> buttons;
private EditText hostEdit;
private EditText portEdit;
private CheckBox useUdsCheckBox;
private EditText udsEdit;
private TextView resultText;
private CheckBox testCertCheckBox;

private UdsTcpEndpointConnector endpointConnector;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -57,6 +64,8 @@ protected void onCreate(Bundle savedInstanceState) {

hostEdit = (EditText) findViewById(R.id.host_edit_text);
portEdit = (EditText) findViewById(R.id.port_edit_text);
useUdsCheckBox = (CheckBox) findViewById(R.id.use_uds_checkbox);
udsEdit = (EditText) findViewById(R.id.uds_proxy_edit_text);
resultText = (TextView) findViewById(R.id.grpc_response_text);
testCertCheckBox = (CheckBox) findViewById(R.id.test_cert_checkbox);

Expand All @@ -65,6 +74,16 @@ protected void onCreate(Bundle savedInstanceState) {
enableButtons(false);
}

/** Click handler for unix domain socket. */
public void enableUds(View view) {
boolean enabled = ((CheckBox) view).isChecked();
udsEdit.setEnabled(enabled);
testCertCheckBox.setEnabled(!enabled);
if (enabled) {
testCertCheckBox.setChecked(false);
}
}

public void startEmptyUnary(View view) {
startTest("empty_unary");
}
Expand Down Expand Up @@ -93,6 +112,10 @@ private void enableButtons(boolean enable) {

@Override
public void onComplete(String result) {
if (endpointConnector != null) {
endpointConnector.shutDown();
endpointConnector = null;
}
resultText.setText(result);
enableButtons(true);
}
Expand All @@ -106,6 +129,9 @@ private void startTest(String testCase) {
String host = hostEdit.getText().toString();
String portStr = portEdit.getText().toString();
int port = TextUtils.isEmpty(portStr) ? 8080 : Integer.valueOf(portStr);
boolean udsEnabled = useUdsCheckBox.isChecked();
String udsPath =
TextUtils.isEmpty(udsEdit.getText()) ? "default" : udsEdit.getText().toString();

String serverHostOverride;
InputStream testCert;
Expand All @@ -116,10 +142,27 @@ private void startTest(String testCase) {
serverHostOverride = null;
testCert = null;
}
ManagedChannel channel =
TesterOkHttpChannelBuilder.build(host, port, serverHostOverride, true, testCert);

new InteropTask(this, channel, testCase).execute();
// Create Channel
ManagedChannel channel;
if (udsEnabled) {
channel = UdsChannelBuilder.forPath(udsPath, Namespace.ABSTRACT).build();
} else {
channel = TesterOkHttpChannelBuilder.build(host, port, serverHostOverride, true, testCert);
}

// Port-forward uds local port to server exposing tcp endpoint.
if (udsEnabled) {
endpointConnector = new UdsTcpEndpointConnector(udsPath, host, port);
try {
endpointConnector.start();
} catch (IOException e) {
Log.e(LOG_TAG, "Failed to start UDS-TCP Endpoint Connector.");
}
}

// Start Test.
new InteropTask(TesterActivity.this, channel, testCase).execute();
}

@Override
Expand Down
Loading