From 1981466747b7e3b45343bf52b8dc96c0578621ea Mon Sep 17 00:00:00 2001 From: Chiachang Wang Date: Wed, 16 Jun 2021 15:29:47 +0000 Subject: [PATCH] Add test for CM#setAcceptUnvalidated The always bit is verified in NetworkAgentTest. There is no easy configuration to set an explicitly selected network to accept an unvalidated network. Verify the accepted case also in NetworkAgentTest. Bug: 186061922 Test: atest CtsNetTestCases:android.net.cts.ConnectivityManagerTest Test: atest CtsNetTestCasesLatestSdk Original-Change: https://android-review.googlesource.com/1731234 Merged-In: I803eb975fb0b6c52d55b2310293bcad407d21f42 Change-Id: I803eb975fb0b6c52d55b2310293bcad407d21f42 --- .../net/cts/ConnectivityManagerTest.java | 62 ++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java b/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java index fc1c743dd7..2cca1e6b58 100644 --- a/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java +++ b/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java @@ -686,12 +686,14 @@ public class ConnectivityManagerTest { private NetworkRequest makeWifiNetworkRequest() { return new NetworkRequest.Builder() .addTransportType(NetworkCapabilities.TRANSPORT_WIFI) + .addCapability(NET_CAPABILITY_INTERNET) .build(); } private NetworkRequest makeCellNetworkRequest() { return new NetworkRequest.Builder() .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR) + .addCapability(NET_CAPABILITY_INTERNET) .build(); } @@ -2411,6 +2413,47 @@ public class ConnectivityManagerTest { } } + @Test + public void testSetAcceptUnvalidated_NoPermission_GetException() { + assumeTrue(TestUtils.shouldTestSApis()); + assertThrows(SecurityException.class, () -> mCm.setAcceptUnvalidated( + mCm.getActiveNetwork(), false /* accept */ , false /* always */)); + } + + @AppModeFull(reason = "WRITE_DEVICE_CONFIG permission can't be granted to instant apps") + @Test + public void testRejectUnvalidated_TearDownNetwork() throws Exception { + assumeTrue(TestUtils.shouldTestSApis()); + final boolean canRunTest = mPackageManager.hasSystemFeature(FEATURE_WIFI) + && mPackageManager.hasSystemFeature(FEATURE_TELEPHONY); + assumeTrue("testAcceptPartialConnectivity_validatedNetwork cannot execute" + + " unless device supports WiFi and telephony", canRunTest); + + final TestableNetworkCallback wifiCb = new TestableNetworkCallback(); + try { + // Ensure at least one default network candidate connected. + mCtsNetUtils.connectToCell(); + + final Network wifiNetwork = prepareUnvalidatedNetwork(); + // Default network should not be wifi ,but checking that wifi is not the default doesn't + // guarantee that it won't become the default in the future. + assertNotEquals(wifiNetwork, mCm.getActiveNetwork()); + + mCm.registerNetworkCallback(makeWifiNetworkRequest(), wifiCb); + runAsShell(NETWORK_SETTINGS, () -> { + mCm.setAcceptUnvalidated(wifiNetwork, false /* accept */, false /* always */); + }); + waitForLost(wifiCb); + } finally { + mCm.unregisterNetworkCallback(wifiCb); + resetValidationConfig(); + /// Wifi will not automatically reconnect to the network. ensureWifiDisconnected cannot + // apply here. Thus, turn off wifi first and restart to restore. + runShellCommand("svc wifi disable"); + mCtsNetUtils.ensureWifiConnected(); + } + } + private Network expectNetworkHasCapability(Network network, int expectedNetCap, long timeout) throws Exception { final CompletableFuture future = new CompletableFuture(); @@ -2436,7 +2479,7 @@ public class ConnectivityManagerTest { mHttpServer.stop(); } - private Network preparePartialConnectivity() throws Exception { + private void prepareHttpServer() throws Exception { runAsShell(READ_DEVICE_CONFIG, () -> { // Verify that the test URLs are not normally set on the device, but do not fail if the // test URLs are set to what this test uses (URLs on localhost), in case the test was @@ -2448,6 +2491,10 @@ public class ConnectivityManagerTest { NetworkValidationTestUtil.clearValidationTestUrlsDeviceConfig(); mHttpServer.start(); + } + + private Network preparePartialConnectivity() throws Exception { + prepareHttpServer(); // Configure response code for partial connectivity configTestServer(Status.INTERNAL_ERROR /* httpsStatusCode */, Status.NO_CONTENT /* httpStatusCode */); @@ -2459,6 +2506,19 @@ public class ConnectivityManagerTest { WIFI_CONNECT_TIMEOUT_MS); } + private Network prepareUnvalidatedNetwork() throws Exception { + prepareHttpServer(); + // Configure response code for unvalidated network + configTestServer(Status.INTERNAL_ERROR /* httpsStatusCode */, + Status.INTERNAL_ERROR /* httpStatusCode */); + + // Disconnect wifi first then start wifi network with configuration. + mCtsNetUtils.ensureWifiDisconnected(null /* wifiNetworkToCheck */); + final Network wifiNetwork = mCtsNetUtils.ensureWifiConnected(); + return expectNetworkHasCapability(wifiNetwork, NET_CAPABILITY_INTERNET, + WIFI_CONNECT_TIMEOUT_MS); + } + private String makeUrl(String path) { return "http://localhost:" + mHttpServer.getListeningPort() + path; }