Merge changes I71a37663,Ib2010b38 am: fd710547b2 am: c241c0f4f3

Original change: https://android-review.googlesource.com/c/platform/packages/modules/Connectivity/+/1583023

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: Idfc4f1cc18e5e535c35e6738e6a4322866c03a17
This commit is contained in:
Lorenzo Colitti
2021-02-12 05:47:56 +00:00
committed by Automerger Merge Worker
3 changed files with 56 additions and 43 deletions

View File

@@ -698,16 +698,30 @@ public class VpnTest extends InstrumentationTestCase {
} }
private class NeverChangeNetworkCallback extends NetworkCallback { private class NeverChangeNetworkCallback extends NetworkCallback {
private volatile Network mLastNetwork; private CountDownLatch mLatch = new CountDownLatch(1);
private volatile Network mFirstNetwork;
private volatile Network mOtherNetwork;
public void onAvailable(Network n) { public void onAvailable(Network n) {
assertNull("Callback got onAvailable more than once: " + mLastNetwork + ", " + n, // Don't assert here, as it crashes the test with a hard to debug message.
mLastNetwork); if (mFirstNetwork == null) {
mLastNetwork = n; mFirstNetwork = n;
mLatch.countDown();
} else if (mOtherNetwork == null) {
mOtherNetwork = n;
}
} }
public Network getLastNetwork() { public Network getFirstNetwork() throws Exception {
return mLastNetwork; assertTrue(
"System default callback got no network after " + TIMEOUT_MS + "ms. "
+ "Please ensure the device has a working Internet connection.",
mLatch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS));
return mFirstNetwork;
}
public void assertNeverChanged() {
assertNull(mOtherNetwork);
} }
} }
@@ -753,13 +767,14 @@ public class VpnTest extends InstrumentationTestCase {
expectVpnTransportInfo(mCM.getActiveNetwork()); expectVpnTransportInfo(mCM.getActiveNetwork());
// Check that system default network callback has not seen any network changes, but the app // Check that system default network callback has not seen any network changes, even though
// default network callback has. This needs to be done before testing private DNS because // the app's default network changed. This needs to be done before testing private
// checkStrictModePrivateDns will set the private DNS server to a nonexistent name, which // DNS because checkStrictModePrivateDns will set the private DNS server to a nonexistent
// will cause validation to fail could cause the default network to switch (e.g., from wifi // name, which will cause validation to fail and cause the default network to switch (e.g.,
// to cellular). // from wifi to cellular).
assertEquals(defaultNetwork, neverChangeCallback.getLastNetwork()); assertEquals(defaultNetwork, neverChangeCallback.getFirstNetwork());
assertNotEqual(defaultNetwork, mCM.getActiveNetwork()); assertNotEqual(defaultNetwork, mCM.getActiveNetwork());
neverChangeCallback.assertNeverChanged();
runWithShellPermissionIdentity( runWithShellPermissionIdentity(
() -> mCM.unregisterNetworkCallback(neverChangeCallback), () -> mCM.unregisterNetworkCallback(neverChangeCallback),
NETWORK_SETTINGS); NETWORK_SETTINGS);

View File

@@ -195,7 +195,6 @@ public class ConnectivityManagerTest {
private PackageManager mPackageManager; private PackageManager mPackageManager;
private final HashMap<Integer, NetworkConfig> mNetworks = private final HashMap<Integer, NetworkConfig> mNetworks =
new HashMap<Integer, NetworkConfig>(); new HashMap<Integer, NetworkConfig>();
boolean mWifiWasDisabled;
private UiAutomation mUiAutomation; private UiAutomation mUiAutomation;
private CtsNetUtils mCtsNetUtils; private CtsNetUtils mCtsNetUtils;
@@ -207,7 +206,6 @@ public class ConnectivityManagerTest {
mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE); mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
mPackageManager = mContext.getPackageManager(); mPackageManager = mContext.getPackageManager();
mCtsNetUtils = new CtsNetUtils(mContext); mCtsNetUtils = new CtsNetUtils(mContext);
mWifiWasDisabled = false;
// Get com.android.internal.R.array.networkAttributes // Get com.android.internal.R.array.networkAttributes
int resId = mContext.getResources().getIdentifier("networkAttributes", "array", "android"); int resId = mContext.getResources().getIdentifier("networkAttributes", "array", "android");
@@ -230,10 +228,7 @@ public class ConnectivityManagerTest {
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
// Return WiFi to its original disabled state after tests that explicitly connect. // Release any NetworkRequests filed to connect mobile data.
if (mWifiWasDisabled) {
mCtsNetUtils.disconnectFromWifi(null);
}
if (mCtsNetUtils.cellConnectAttempted()) { if (mCtsNetUtils.cellConnectAttempted()) {
mCtsNetUtils.disconnectFromCell(); mCtsNetUtils.disconnectFromCell();
} }
@@ -249,17 +244,6 @@ public class ConnectivityManagerTest {
} }
} }
/**
* Make sure WiFi is connected to an access point if it is not already. If
* WiFi is enabled as a result of this function, it will be disabled
* automatically in tearDown().
*/
private Network ensureWifiConnected() {
mWifiWasDisabled = !mWifiManager.isWifiEnabled();
// Even if wifi is enabled, the network may not be connected or ready yet
return mCtsNetUtils.connectToWifi();
}
@Test @Test
public void testIsNetworkTypeValid() { public void testIsNetworkTypeValid() {
assertTrue(ConnectivityManager.isNetworkTypeValid(ConnectivityManager.TYPE_MOBILE)); assertTrue(ConnectivityManager.isNetworkTypeValid(ConnectivityManager.TYPE_MOBILE));
@@ -543,17 +527,20 @@ public class ConnectivityManagerTest {
Network wifiNetwork = null; Network wifiNetwork = null;
try { try {
ensureWifiConnected(); mCtsNetUtils.ensureWifiConnected();
// Now we should expect to get a network callback about availability of the wifi // Now we should expect to get a network callback about availability of the wifi
// network even if it was already connected as a state-based action when the callback // network even if it was already connected as a state-based action when the callback
// is registered. // is registered.
wifiNetwork = callback.waitForAvailable(); wifiNetwork = callback.waitForAvailable();
assertNotNull("Did not receive NetworkCallback.onAvailable for TRANSPORT_WIFI", assertNotNull("Did not receive onAvailable for TRANSPORT_WIFI request",
wifiNetwork); wifiNetwork);
assertNotNull("Did not receive NetworkCallback.onAvailable for any default network", assertNotNull("Did not receive onAvailable on default network callback",
defaultTrackingCallback.waitForAvailable()); defaultTrackingCallback.waitForAvailable());
assertNotNull("Did not receive onAvailable on system default network callback",
systemDefaultTrackingCallback.waitForAvailable());
} catch (InterruptedException e) { } catch (InterruptedException e) {
fail("Broadcast receiver or NetworkCallback wait was interrupted."); fail("Broadcast receiver or NetworkCallback wait was interrupted.");
} finally { } finally {
@@ -596,7 +583,7 @@ public class ConnectivityManagerTest {
mCm.registerNetworkCallback(makeWifiNetworkRequest(), pendingIntent); mCm.registerNetworkCallback(makeWifiNetworkRequest(), pendingIntent);
try { try {
ensureWifiConnected(); mCtsNetUtils.ensureWifiConnected();
// Now we expect to get the Intent delivered notifying of the availability of the wifi // Now we expect to get the Intent delivered notifying of the availability of the wifi
// network even if it was already connected as a state-based action when the callback // network even if it was already connected as a state-based action when the callback
@@ -677,6 +664,17 @@ public class ConnectivityManagerTest {
return null; return null;
} }
/**
* Checks that enabling/disabling wifi causes CONNECTIVITY_ACTION broadcasts.
*/
@AppModeFull(reason = "Cannot get WifiManager in instant app mode")
@Test
public void testToggleWifiConnectivityAction() {
// toggleWifi calls connectToWifi and disconnectFromWifi, which both wait for
// CONNECTIVITY_ACTION broadcasts.
mCtsNetUtils.toggleWifi();
}
/** Verify restricted networks cannot be requested. */ /** Verify restricted networks cannot be requested. */
@AppModeFull(reason = "CHANGE_NETWORK_STATE permission can't be granted to instant apps") @AppModeFull(reason = "CHANGE_NETWORK_STATE permission can't be granted to instant apps")
@Test @Test
@@ -804,7 +802,7 @@ public class ConnectivityManagerTest {
@Test @Test
public void testGetMultipathPreference() throws Exception { public void testGetMultipathPreference() throws Exception {
final ContentResolver resolver = mContext.getContentResolver(); final ContentResolver resolver = mContext.getContentResolver();
ensureWifiConnected(); mCtsNetUtils.ensureWifiConnected();
final String ssid = unquoteSSID(mWifiManager.getConnectionInfo().getSSID()); final String ssid = unquoteSSID(mWifiManager.getConnectionInfo().getSSID());
final String oldMeteredSetting = getWifiMeteredStatus(ssid); final String oldMeteredSetting = getWifiMeteredStatus(ssid);
final String oldMeteredMultipathPreference = Settings.Global.getString( final String oldMeteredMultipathPreference = Settings.Global.getString(
@@ -818,7 +816,7 @@ public class ConnectivityManagerTest {
waitForActiveNetworkMetered(TRANSPORT_WIFI, true); waitForActiveNetworkMetered(TRANSPORT_WIFI, true);
// Wifi meterness changes from unmetered to metered will disconnect and reconnect since // Wifi meterness changes from unmetered to metered will disconnect and reconnect since
// R. // R.
final Network network = ensureWifiConnected(); final Network network = mCtsNetUtils.ensureWifiConnected();
assertEquals(ssid, unquoteSSID(mWifiManager.getConnectionInfo().getSSID())); assertEquals(ssid, unquoteSSID(mWifiManager.getConnectionInfo().getSSID()));
assertEquals(mCm.getNetworkCapabilities(network).hasCapability( assertEquals(mCm.getNetworkCapabilities(network).hasCapability(
NET_CAPABILITY_NOT_METERED), false); NET_CAPABILITY_NOT_METERED), false);
@@ -1032,7 +1030,7 @@ public class ConnectivityManagerTest {
return; return;
} }
final Network network = ensureWifiConnected(); final Network network = mCtsNetUtils.ensureWifiConnected();
if (getSupportedKeepalivesForNet(network) != 0) return; if (getSupportedKeepalivesForNet(network) != 0) return;
final InetAddress srcAddr = getFirstV4Address(network); final InetAddress srcAddr = getFirstV4Address(network);
assumeTrue("This test requires native IPv4", srcAddr != null); assumeTrue("This test requires native IPv4", srcAddr != null);
@@ -1052,7 +1050,7 @@ public class ConnectivityManagerTest {
return; return;
} }
final Network network = ensureWifiConnected(); final Network network = mCtsNetUtils.ensureWifiConnected();
if (getSupportedKeepalivesForNet(network) == 0) return; if (getSupportedKeepalivesForNet(network) == 0) return;
final InetAddress srcAddr = getFirstV4Address(network); final InetAddress srcAddr = getFirstV4Address(network);
assumeTrue("This test requires native IPv4", srcAddr != null); assumeTrue("This test requires native IPv4", srcAddr != null);
@@ -1263,7 +1261,7 @@ public class ConnectivityManagerTest {
return; return;
} }
final Network network = ensureWifiConnected(); final Network network = mCtsNetUtils.ensureWifiConnected();
final int supported = getSupportedKeepalivesForNet(network); final int supported = getSupportedKeepalivesForNet(network);
if (supported == 0) { if (supported == 0) {
return; return;
@@ -1360,7 +1358,7 @@ public class ConnectivityManagerTest {
return; return;
} }
final Network network = ensureWifiConnected(); final Network network = mCtsNetUtils.ensureWifiConnected();
final int supported = getSupportedKeepalivesForNet(network); final int supported = getSupportedKeepalivesForNet(network);
if (supported == 0) { if (supported == 0) {
return; return;
@@ -1408,7 +1406,7 @@ public class ConnectivityManagerTest {
// Ensure that NetworkUtils.queryUserAccess always returns false since this package should // Ensure that NetworkUtils.queryUserAccess always returns false since this package should
// not have netd system permission to call this function. // not have netd system permission to call this function.
final Network wifiNetwork = ensureWifiConnected(); final Network wifiNetwork = mCtsNetUtils.ensureWifiConnected();
assertFalse(NetworkUtils.queryUserAccess(Binder.getCallingUid(), wifiNetwork.netId)); assertFalse(NetworkUtils.queryUserAccess(Binder.getCallingUid(), wifiNetwork.netId));
// Ensure that this package cannot bind to any restricted network that's currently // Ensure that this package cannot bind to any restricted network that's currently

View File

@@ -212,7 +212,7 @@ public final class CtsNetUtils {
mContext.registerReceiver(receiver, filter); mContext.registerReceiver(receiver, filter);
boolean connected = false; boolean connected = false;
final String err = "Wifi must be configured to connect to an access point for this test."; final String err = "Wifi must be configured to connect to an access point for this test";
try { try {
clearWifiBlacklist(); clearWifiBlacklist();
SystemUtil.runShellCommand("svc wifi enable"); SystemUtil.runShellCommand("svc wifi enable");
@@ -235,7 +235,7 @@ public final class CtsNetUtils {
} }
// Ensure we get an onAvailable callback and possibly a CONNECTIVITY_ACTION. // Ensure we get an onAvailable callback and possibly a CONNECTIVITY_ACTION.
wifiNetwork = callback.waitForAvailable(); wifiNetwork = callback.waitForAvailable();
assertNotNull(err, wifiNetwork); assertNotNull(err + ": onAvailable callback not received", wifiNetwork);
connected = !expectLegacyBroadcast || receiver.waitForState(); connected = !expectLegacyBroadcast || receiver.waitForState();
} catch (InterruptedException ex) { } catch (InterruptedException ex) {
fail("connectToWifi was interrupted"); fail("connectToWifi was interrupted");
@@ -244,7 +244,7 @@ public final class CtsNetUtils {
mContext.unregisterReceiver(receiver); mContext.unregisterReceiver(receiver);
} }
assertTrue(err, connected); assertTrue(err + ": CONNECTIVITY_ACTION not received", connected);
return wifiNetwork; return wifiNetwork;
} }