Merge changes I71a37663,Ib2010b38

* changes:
  Improve testing of registerSystemDefaultNetworkCallback.
  Remove buggy ConnectivityManagerTest#ensureWifiConnected.
This commit is contained in:
Lorenzo Colitti
2021-02-12 02:45:02 +00:00
committed by Gerrit Code Review
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 volatile Network mLastNetwork;
private CountDownLatch mLatch = new CountDownLatch(1);
private volatile Network mFirstNetwork;
private volatile Network mOtherNetwork;
public void onAvailable(Network n) {
assertNull("Callback got onAvailable more than once: " + mLastNetwork + ", " + n,
mLastNetwork);
mLastNetwork = n;
// Don't assert here, as it crashes the test with a hard to debug message.
if (mFirstNetwork == null) {
mFirstNetwork = n;
mLatch.countDown();
} else if (mOtherNetwork == null) {
mOtherNetwork = n;
}
}
public Network getLastNetwork() {
return mLastNetwork;
public Network getFirstNetwork() throws Exception {
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());
// Check that system default network callback has not seen any network changes, but the app
// default network callback has. This needs to be done before testing private DNS because
// checkStrictModePrivateDns will set the private DNS server to a nonexistent name, which
// will cause validation to fail could cause the default network to switch (e.g., from wifi
// to cellular).
assertEquals(defaultNetwork, neverChangeCallback.getLastNetwork());
// Check that system default network callback has not seen any network changes, even though
// the app's default network changed. This needs to be done before testing private
// DNS because checkStrictModePrivateDns will set the private DNS server to a nonexistent
// name, which will cause validation to fail and cause the default network to switch (e.g.,
// from wifi to cellular).
assertEquals(defaultNetwork, neverChangeCallback.getFirstNetwork());
assertNotEqual(defaultNetwork, mCM.getActiveNetwork());
neverChangeCallback.assertNeverChanged();
runWithShellPermissionIdentity(
() -> mCM.unregisterNetworkCallback(neverChangeCallback),
NETWORK_SETTINGS);

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,17 +527,20 @@ 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
// is registered.
wifiNetwork = callback.waitForAvailable();
assertNotNull("Did not receive NetworkCallback.onAvailable for TRANSPORT_WIFI",
assertNotNull("Did not receive onAvailable for TRANSPORT_WIFI request",
wifiNetwork);
assertNotNull("Did not receive NetworkCallback.onAvailable for any default network",
assertNotNull("Did not receive onAvailable on default network callback",
defaultTrackingCallback.waitForAvailable());
assertNotNull("Did not receive onAvailable on system default network callback",
systemDefaultTrackingCallback.waitForAvailable());
} catch (InterruptedException e) {
fail("Broadcast receiver or NetworkCallback wait was interrupted.");
} finally {
@@ -596,7 +583,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 +664,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 +802,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 +816,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 +1030,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 +1050,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 +1261,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 +1358,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 +1406,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;
}