Merge changes Ib2d54e53,Ieb606d2b
* changes: Test if underlying networks are the same as what VpnService sets Convert VpnTest to JUnit4
This commit is contained in:
@@ -30,9 +30,18 @@ import static android.system.OsConstants.POLLIN;
|
|||||||
import static android.system.OsConstants.SOCK_DGRAM;
|
import static android.system.OsConstants.SOCK_DGRAM;
|
||||||
import static android.test.MoreAsserts.assertNotEqual;
|
import static android.test.MoreAsserts.assertNotEqual;
|
||||||
|
|
||||||
|
import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
|
||||||
|
|
||||||
import static com.android.compatibility.common.util.SystemUtil.runWithShellPermissionIdentity;
|
import static com.android.compatibility.common.util.SystemUtil.runWithShellPermissionIdentity;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
import android.annotation.Nullable;
|
import android.annotation.Nullable;
|
||||||
|
import android.app.Activity;
|
||||||
import android.app.DownloadManager;
|
import android.app.DownloadManager;
|
||||||
import android.app.DownloadManager.Query;
|
import android.app.DownloadManager.Query;
|
||||||
import android.app.DownloadManager.Request;
|
import android.app.DownloadManager.Request;
|
||||||
@@ -71,15 +80,21 @@ import android.system.ErrnoException;
|
|||||||
import android.system.Os;
|
import android.system.Os;
|
||||||
import android.system.OsConstants;
|
import android.system.OsConstants;
|
||||||
import android.system.StructPollfd;
|
import android.system.StructPollfd;
|
||||||
import android.test.InstrumentationTestCase;
|
|
||||||
import android.test.MoreAsserts;
|
import android.test.MoreAsserts;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||||
|
|
||||||
import com.android.compatibility.common.util.BlockingBroadcastReceiver;
|
import com.android.compatibility.common.util.BlockingBroadcastReceiver;
|
||||||
import com.android.modules.utils.build.SdkLevel;
|
import com.android.modules.utils.build.SdkLevel;
|
||||||
import com.android.testutils.TestableNetworkCallback;
|
import com.android.testutils.TestableNetworkCallback;
|
||||||
|
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
import java.io.Closeable;
|
import java.io.Closeable;
|
||||||
import java.io.FileDescriptor;
|
import java.io.FileDescriptor;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -127,7 +142,8 @@ import java.util.concurrent.TimeUnit;
|
|||||||
* https://source.android.com/devices/tech/config/kernel_network_tests.html
|
* https://source.android.com/devices/tech/config/kernel_network_tests.html
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class VpnTest extends InstrumentationTestCase {
|
@RunWith(AndroidJUnit4.class)
|
||||||
|
public class VpnTest {
|
||||||
|
|
||||||
// These are neither public nor @TestApi.
|
// These are neither public nor @TestApi.
|
||||||
// TODO: add them to @TestApi.
|
// TODO: add them to @TestApi.
|
||||||
@@ -161,17 +177,24 @@ public class VpnTest extends InstrumentationTestCase {
|
|||||||
return !pm.hasSystemFeature("android.hardware.type.watch");
|
return !pm.hasSystemFeature("android.hardware.type.watch");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public final <T extends Activity> T launchActivity(String packageName, Class<T> activityClass) {
|
||||||
public void setUp() throws Exception {
|
final Intent intent = new Intent(Intent.ACTION_MAIN);
|
||||||
super.setUp();
|
intent.setClassName(packageName, activityClass.getName());
|
||||||
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||||
|
final T activity = (T) getInstrumentation().startActivitySync(intent);
|
||||||
|
getInstrumentation().waitForIdleSync();
|
||||||
|
return activity;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() throws Exception {
|
||||||
mNetwork = null;
|
mNetwork = null;
|
||||||
mCallback = null;
|
mCallback = null;
|
||||||
storePrivateDnsSetting();
|
storePrivateDnsSetting();
|
||||||
|
|
||||||
mDevice = UiDevice.getInstance(getInstrumentation());
|
mDevice = UiDevice.getInstance(getInstrumentation());
|
||||||
mActivity = launchActivity(getInstrumentation().getTargetContext().getPackageName(),
|
mActivity = launchActivity(getInstrumentation().getTargetContext().getPackageName(),
|
||||||
MyActivity.class, null);
|
MyActivity.class);
|
||||||
mPackageName = mActivity.getPackageName();
|
mPackageName = mActivity.getPackageName();
|
||||||
mCM = (ConnectivityManager) mActivity.getSystemService(Context.CONNECTIVITY_SERVICE);
|
mCM = (ConnectivityManager) mActivity.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||||
mWifiManager = (WifiManager) mActivity.getSystemService(Context.WIFI_SERVICE);
|
mWifiManager = (WifiManager) mActivity.getSystemService(Context.WIFI_SERVICE);
|
||||||
@@ -180,7 +203,7 @@ public class VpnTest extends InstrumentationTestCase {
|
|||||||
mDevice.waitForIdle();
|
mDevice.waitForIdle();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@After
|
||||||
public void tearDown() throws Exception {
|
public void tearDown() throws Exception {
|
||||||
restorePrivateDnsSetting();
|
restorePrivateDnsSetting();
|
||||||
mRemoteSocketFactoryClient.unbind();
|
mRemoteSocketFactoryClient.unbind();
|
||||||
@@ -190,7 +213,6 @@ public class VpnTest extends InstrumentationTestCase {
|
|||||||
Log.i(TAG, "Stopping VPN");
|
Log.i(TAG, "Stopping VPN");
|
||||||
stopVpn();
|
stopVpn();
|
||||||
mActivity.finish();
|
mActivity.finish();
|
||||||
super.tearDown();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void prepareVpn() throws Exception {
|
private void prepareVpn() throws Exception {
|
||||||
@@ -702,6 +724,7 @@ public class VpnTest extends InstrumentationTestCase {
|
|||||||
setAndVerifyPrivateDns(initialMode);
|
setAndVerifyPrivateDns(initialMode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testDefault() throws Exception {
|
public void testDefault() throws Exception {
|
||||||
if (!supportedHardware()) return;
|
if (!supportedHardware()) return;
|
||||||
// If adb TCP port opened, this test may running by adb over network.
|
// If adb TCP port opened, this test may running by adb over network.
|
||||||
@@ -762,6 +785,15 @@ public class VpnTest extends InstrumentationTestCase {
|
|||||||
maybeExpectVpnTransportInfo(vpnNetwork);
|
maybeExpectVpnTransportInfo(vpnNetwork);
|
||||||
assertEquals(TYPE_VPN, mCM.getNetworkInfo(vpnNetwork).getType());
|
assertEquals(TYPE_VPN, mCM.getNetworkInfo(vpnNetwork).getType());
|
||||||
|
|
||||||
|
if (SdkLevel.isAtLeastT()) {
|
||||||
|
runWithShellPermissionIdentity(() -> {
|
||||||
|
final NetworkCapabilities nc = mCM.getNetworkCapabilities(vpnNetwork);
|
||||||
|
assertNotNull(nc);
|
||||||
|
assertNotNull(nc.getUnderlyingNetworks());
|
||||||
|
assertEquals(defaultNetwork, new ArrayList<>(nc.getUnderlyingNetworks()).get(0));
|
||||||
|
}, NETWORK_SETTINGS);
|
||||||
|
}
|
||||||
|
|
||||||
if (SdkLevel.isAtLeastS()) {
|
if (SdkLevel.isAtLeastS()) {
|
||||||
// Check that system default network callback has not seen any network changes, even
|
// Check that system default network callback has not seen any network changes, even
|
||||||
// though the app's default network changed. Also check that otherUidCallback saw no
|
// though the app's default network changed. Also check that otherUidCallback saw no
|
||||||
@@ -781,6 +813,7 @@ public class VpnTest extends InstrumentationTestCase {
|
|||||||
receiver.unregisterQuietly();
|
receiver.unregisterQuietly();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testAppAllowed() throws Exception {
|
public void testAppAllowed() throws Exception {
|
||||||
if (!supportedHardware()) return;
|
if (!supportedHardware()) return;
|
||||||
|
|
||||||
@@ -801,6 +834,7 @@ public class VpnTest extends InstrumentationTestCase {
|
|||||||
checkStrictModePrivateDns();
|
checkStrictModePrivateDns();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testAppDisallowed() throws Exception {
|
public void testAppDisallowed() throws Exception {
|
||||||
if (!supportedHardware()) return;
|
if (!supportedHardware()) return;
|
||||||
|
|
||||||
@@ -829,6 +863,7 @@ public class VpnTest extends InstrumentationTestCase {
|
|||||||
assertFalse(nc.hasTransport(TRANSPORT_VPN));
|
assertFalse(nc.hasTransport(TRANSPORT_VPN));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testGetConnectionOwnerUidSecurity() throws Exception {
|
public void testGetConnectionOwnerUidSecurity() throws Exception {
|
||||||
if (!supportedHardware()) return;
|
if (!supportedHardware()) return;
|
||||||
|
|
||||||
@@ -850,6 +885,7 @@ public class VpnTest extends InstrumentationTestCase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testSetProxy() throws Exception {
|
public void testSetProxy() throws Exception {
|
||||||
if (!supportedHardware()) return;
|
if (!supportedHardware()) return;
|
||||||
ProxyInfo initialProxy = mCM.getDefaultProxy();
|
ProxyInfo initialProxy = mCM.getDefaultProxy();
|
||||||
@@ -889,6 +925,7 @@ public class VpnTest extends InstrumentationTestCase {
|
|||||||
assertDefaultProxy(initialProxy);
|
assertDefaultProxy(initialProxy);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testSetProxyDisallowedApps() throws Exception {
|
public void testSetProxyDisallowedApps() throws Exception {
|
||||||
if (!supportedHardware()) return;
|
if (!supportedHardware()) return;
|
||||||
ProxyInfo initialProxy = mCM.getDefaultProxy();
|
ProxyInfo initialProxy = mCM.getDefaultProxy();
|
||||||
@@ -908,6 +945,7 @@ public class VpnTest extends InstrumentationTestCase {
|
|||||||
assertDefaultProxy(initialProxy);
|
assertDefaultProxy(initialProxy);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testNoProxy() throws Exception {
|
public void testNoProxy() throws Exception {
|
||||||
if (!supportedHardware()) return;
|
if (!supportedHardware()) return;
|
||||||
ProxyInfo initialProxy = mCM.getDefaultProxy();
|
ProxyInfo initialProxy = mCM.getDefaultProxy();
|
||||||
@@ -942,6 +980,7 @@ public class VpnTest extends InstrumentationTestCase {
|
|||||||
assertNetworkHasExpectedProxy(initialProxy, mCM.getActiveNetwork());
|
assertNetworkHasExpectedProxy(initialProxy, mCM.getActiveNetwork());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testBindToNetworkWithProxy() throws Exception {
|
public void testBindToNetworkWithProxy() throws Exception {
|
||||||
if (!supportedHardware()) return;
|
if (!supportedHardware()) return;
|
||||||
String allowedApps = mPackageName;
|
String allowedApps = mPackageName;
|
||||||
@@ -966,6 +1005,7 @@ public class VpnTest extends InstrumentationTestCase {
|
|||||||
assertDefaultProxy(initialProxy);
|
assertDefaultProxy(initialProxy);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testVpnMeterednessWithNoUnderlyingNetwork() throws Exception {
|
public void testVpnMeterednessWithNoUnderlyingNetwork() throws Exception {
|
||||||
if (!supportedHardware()) {
|
if (!supportedHardware()) {
|
||||||
return;
|
return;
|
||||||
@@ -986,8 +1026,18 @@ public class VpnTest extends InstrumentationTestCase {
|
|||||||
assertTrue(mCM.isActiveNetworkMetered());
|
assertTrue(mCM.isActiveNetworkMetered());
|
||||||
|
|
||||||
maybeExpectVpnTransportInfo(mCM.getActiveNetwork());
|
maybeExpectVpnTransportInfo(mCM.getActiveNetwork());
|
||||||
|
|
||||||
|
if (SdkLevel.isAtLeastT()) {
|
||||||
|
runWithShellPermissionIdentity(() -> {
|
||||||
|
final NetworkCapabilities nc = mCM.getNetworkCapabilities(mNetwork);
|
||||||
|
assertNotNull(nc);
|
||||||
|
assertNotNull(nc.getUnderlyingNetworks());
|
||||||
|
assertEquals(underlyingNetworks, new ArrayList<>(nc.getUnderlyingNetworks()));
|
||||||
|
}, NETWORK_SETTINGS);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testVpnMeterednessWithNullUnderlyingNetwork() throws Exception {
|
public void testVpnMeterednessWithNullUnderlyingNetwork() throws Exception {
|
||||||
if (!supportedHardware()) {
|
if (!supportedHardware()) {
|
||||||
return;
|
return;
|
||||||
@@ -1016,6 +1066,7 @@ public class VpnTest extends InstrumentationTestCase {
|
|||||||
maybeExpectVpnTransportInfo(mCM.getActiveNetwork());
|
maybeExpectVpnTransportInfo(mCM.getActiveNetwork());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testVpnMeterednessWithNonNullUnderlyingNetwork() throws Exception {
|
public void testVpnMeterednessWithNonNullUnderlyingNetwork() throws Exception {
|
||||||
if (!supportedHardware()) {
|
if (!supportedHardware()) {
|
||||||
return;
|
return;
|
||||||
@@ -1043,8 +1094,21 @@ public class VpnTest extends InstrumentationTestCase {
|
|||||||
assertEquals(isNetworkMetered(mNetwork), mCM.isActiveNetworkMetered());
|
assertEquals(isNetworkMetered(mNetwork), mCM.isActiveNetworkMetered());
|
||||||
|
|
||||||
maybeExpectVpnTransportInfo(mCM.getActiveNetwork());
|
maybeExpectVpnTransportInfo(mCM.getActiveNetwork());
|
||||||
|
|
||||||
|
if (SdkLevel.isAtLeastT()) {
|
||||||
|
final Network vpnNetwork = mCM.getActiveNetwork();
|
||||||
|
assertNotEqual(underlyingNetwork, vpnNetwork);
|
||||||
|
runWithShellPermissionIdentity(() -> {
|
||||||
|
final NetworkCapabilities nc = mCM.getNetworkCapabilities(vpnNetwork);
|
||||||
|
assertNotNull(nc);
|
||||||
|
assertNotNull(nc.getUnderlyingNetworks());
|
||||||
|
final List<Network> underlying = nc.getUnderlyingNetworks();
|
||||||
|
assertEquals(underlyingNetwork, underlying.get(0));
|
||||||
|
}, NETWORK_SETTINGS);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testAlwaysMeteredVpnWithNullUnderlyingNetwork() throws Exception {
|
public void testAlwaysMeteredVpnWithNullUnderlyingNetwork() throws Exception {
|
||||||
if (!supportedHardware()) {
|
if (!supportedHardware()) {
|
||||||
return;
|
return;
|
||||||
@@ -1071,6 +1135,7 @@ public class VpnTest extends InstrumentationTestCase {
|
|||||||
maybeExpectVpnTransportInfo(mCM.getActiveNetwork());
|
maybeExpectVpnTransportInfo(mCM.getActiveNetwork());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testAlwaysMeteredVpnWithNonNullUnderlyingNetwork() throws Exception {
|
public void testAlwaysMeteredVpnWithNonNullUnderlyingNetwork() throws Exception {
|
||||||
if (!supportedHardware()) {
|
if (!supportedHardware()) {
|
||||||
return;
|
return;
|
||||||
@@ -1096,8 +1161,21 @@ public class VpnTest extends InstrumentationTestCase {
|
|||||||
assertTrue(mCM.isActiveNetworkMetered());
|
assertTrue(mCM.isActiveNetworkMetered());
|
||||||
|
|
||||||
maybeExpectVpnTransportInfo(mCM.getActiveNetwork());
|
maybeExpectVpnTransportInfo(mCM.getActiveNetwork());
|
||||||
|
|
||||||
|
if (SdkLevel.isAtLeastT()) {
|
||||||
|
final Network vpnNetwork = mCM.getActiveNetwork();
|
||||||
|
assertNotEqual(underlyingNetwork, vpnNetwork);
|
||||||
|
runWithShellPermissionIdentity(() -> {
|
||||||
|
final NetworkCapabilities nc = mCM.getNetworkCapabilities(vpnNetwork);
|
||||||
|
assertNotNull(nc);
|
||||||
|
assertNotNull(nc.getUnderlyingNetworks());
|
||||||
|
final List<Network> underlying = nc.getUnderlyingNetworks();
|
||||||
|
assertEquals(underlyingNetwork, underlying.get(0));
|
||||||
|
}, NETWORK_SETTINGS);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testB141603906() throws Exception {
|
public void testB141603906() throws Exception {
|
||||||
if (!supportedHardware()) {
|
if (!supportedHardware()) {
|
||||||
return;
|
return;
|
||||||
@@ -1176,7 +1254,7 @@ public class VpnTest extends InstrumentationTestCase {
|
|||||||
private boolean received;
|
private boolean received;
|
||||||
|
|
||||||
public ProxyChangeBroadcastReceiver() {
|
public ProxyChangeBroadcastReceiver() {
|
||||||
super(VpnTest.this.getInstrumentation().getContext(), Proxy.PROXY_CHANGE_ACTION);
|
super(getInstrumentation().getContext(), Proxy.PROXY_CHANGE_ACTION);
|
||||||
received = false;
|
received = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1196,6 +1274,7 @@ public class VpnTest extends InstrumentationTestCase {
|
|||||||
* allowed list.
|
* allowed list.
|
||||||
* See b/165774987.
|
* See b/165774987.
|
||||||
*/
|
*/
|
||||||
|
@Test
|
||||||
public void testDownloadWithDownloadManagerDisallowed() throws Exception {
|
public void testDownloadWithDownloadManagerDisallowed() throws Exception {
|
||||||
if (!supportedHardware()) return;
|
if (!supportedHardware()) return;
|
||||||
|
|
||||||
@@ -1205,7 +1284,7 @@ public class VpnTest extends InstrumentationTestCase {
|
|||||||
"" /* allowedApps */, "com.android.providers.downloads", null /* proxyInfo */,
|
"" /* allowedApps */, "com.android.providers.downloads", null /* proxyInfo */,
|
||||||
null /* underlyingNetworks */, false /* isAlwaysMetered */);
|
null /* underlyingNetworks */, false /* isAlwaysMetered */);
|
||||||
|
|
||||||
final Context context = VpnTest.this.getInstrumentation().getContext();
|
final Context context = getInstrumentation().getContext();
|
||||||
final DownloadManager dm = context.getSystemService(DownloadManager.class);
|
final DownloadManager dm = context.getSystemService(DownloadManager.class);
|
||||||
final DownloadCompleteReceiver receiver = new DownloadCompleteReceiver();
|
final DownloadCompleteReceiver receiver = new DownloadCompleteReceiver();
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user