Remove buggy ConnectivityManagerTest#ensureWifiConnected.

This method does not behave correctly when wifi is connected but
the last CONNECTIVITY_ACTION broadcast was not for wifi. This
could happen due to another network connecting or disconnecting,
such as VPN.

Bug: 179774433
Test: test-only change
Change-Id: Ib2010b3871133c38b6d508bf508134dd9b814ce2
This commit is contained in:
Lorenzo Colitti 2021-02-09 22:45:25 +09:00
commit bf5c2979dd
2 changed files with 24 additions and 29 deletions

View file

@ -195,7 +195,6 @@ public class ConnectivityManagerTest {
private PackageManager mPackageManager;
private final HashMap<Integer, NetworkConfig> mNetworks =
new HashMap<Integer, NetworkConfig>();
boolean mWifiWasDisabled;
private UiAutomation mUiAutomation;
private CtsNetUtils mCtsNetUtils;
@ -207,7 +206,6 @@ public class ConnectivityManagerTest {
mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
mPackageManager = mContext.getPackageManager();
mCtsNetUtils = new CtsNetUtils(mContext);
mWifiWasDisabled = false;
// Get com.android.internal.R.array.networkAttributes
int resId = mContext.getResources().getIdentifier("networkAttributes", "array", "android");
@ -230,10 +228,7 @@ public class ConnectivityManagerTest {
@After
public void tearDown() throws Exception {
// Return WiFi to its original disabled state after tests that explicitly connect.
if (mWifiWasDisabled) {
mCtsNetUtils.disconnectFromWifi(null);
}
// Release any NetworkRequests filed to connect mobile data.
if (mCtsNetUtils.cellConnectAttempted()) {
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
public void testIsNetworkTypeValid() {
assertTrue(ConnectivityManager.isNetworkTypeValid(ConnectivityManager.TYPE_MOBILE));
@ -543,7 +527,7 @@ public class ConnectivityManagerTest {
Network wifiNetwork = null;
try {
ensureWifiConnected();
mCtsNetUtils.ensureWifiConnected();
// 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
@ -596,7 +580,7 @@ public class ConnectivityManagerTest {
mCm.registerNetworkCallback(makeWifiNetworkRequest(), pendingIntent);
try {
ensureWifiConnected();
mCtsNetUtils.ensureWifiConnected();
// 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
@ -677,6 +661,17 @@ public class ConnectivityManagerTest {
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. */
@AppModeFull(reason = "CHANGE_NETWORK_STATE permission can't be granted to instant apps")
@Test
@ -804,7 +799,7 @@ public class ConnectivityManagerTest {
@Test
public void testGetMultipathPreference() throws Exception {
final ContentResolver resolver = mContext.getContentResolver();
ensureWifiConnected();
mCtsNetUtils.ensureWifiConnected();
final String ssid = unquoteSSID(mWifiManager.getConnectionInfo().getSSID());
final String oldMeteredSetting = getWifiMeteredStatus(ssid);
final String oldMeteredMultipathPreference = Settings.Global.getString(
@ -818,7 +813,7 @@ public class ConnectivityManagerTest {
waitForActiveNetworkMetered(TRANSPORT_WIFI, true);
// Wifi meterness changes from unmetered to metered will disconnect and reconnect since
// R.
final Network network = ensureWifiConnected();
final Network network = mCtsNetUtils.ensureWifiConnected();
assertEquals(ssid, unquoteSSID(mWifiManager.getConnectionInfo().getSSID()));
assertEquals(mCm.getNetworkCapabilities(network).hasCapability(
NET_CAPABILITY_NOT_METERED), false);
@ -1032,7 +1027,7 @@ public class ConnectivityManagerTest {
return;
}
final Network network = ensureWifiConnected();
final Network network = mCtsNetUtils.ensureWifiConnected();
if (getSupportedKeepalivesForNet(network) != 0) return;
final InetAddress srcAddr = getFirstV4Address(network);
assumeTrue("This test requires native IPv4", srcAddr != null);
@ -1052,7 +1047,7 @@ public class ConnectivityManagerTest {
return;
}
final Network network = ensureWifiConnected();
final Network network = mCtsNetUtils.ensureWifiConnected();
if (getSupportedKeepalivesForNet(network) == 0) return;
final InetAddress srcAddr = getFirstV4Address(network);
assumeTrue("This test requires native IPv4", srcAddr != null);
@ -1263,7 +1258,7 @@ public class ConnectivityManagerTest {
return;
}
final Network network = ensureWifiConnected();
final Network network = mCtsNetUtils.ensureWifiConnected();
final int supported = getSupportedKeepalivesForNet(network);
if (supported == 0) {
return;
@ -1360,7 +1355,7 @@ public class ConnectivityManagerTest {
return;
}
final Network network = ensureWifiConnected();
final Network network = mCtsNetUtils.ensureWifiConnected();
final int supported = getSupportedKeepalivesForNet(network);
if (supported == 0) {
return;
@ -1408,7 +1403,7 @@ public class ConnectivityManagerTest {
// Ensure that NetworkUtils.queryUserAccess always returns false since this package should
// 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));
// 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);
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 {
clearWifiBlacklist();
SystemUtil.runShellCommand("svc wifi enable");
@ -235,7 +235,7 @@ public final class CtsNetUtils {
}
// Ensure we get an onAvailable callback and possibly a CONNECTIVITY_ACTION.
wifiNetwork = callback.waitForAvailable();
assertNotNull(err, wifiNetwork);
assertNotNull(err + ": onAvailable callback not received", wifiNetwork);
connected = !expectLegacyBroadcast || receiver.waitForState();
} catch (InterruptedException ex) {
fail("connectToWifi was interrupted");
@ -244,7 +244,7 @@ public final class CtsNetUtils {
mContext.unregisterReceiver(receiver);
}
assertTrue(err, connected);
assertTrue(err + ": CONNECTIVITY_ACTION not received", connected);
return wifiNetwork;
}