Merge "Release test network in a test"
This commit is contained in:
@@ -1578,11 +1578,12 @@ public class ConnectivityManagerTest {
|
|||||||
public void testRequestBackgroundNetwork() throws Exception {
|
public void testRequestBackgroundNetwork() throws Exception {
|
||||||
// Create a tun interface. Use the returned interface name as the specifier to create
|
// Create a tun interface. Use the returned interface name as the specifier to create
|
||||||
// a test network request.
|
// a test network request.
|
||||||
final TestNetworkInterface testNetworkInterface = runWithShellPermissionIdentity(() -> {
|
final TestNetworkManager tnm = runWithShellPermissionIdentity(() ->
|
||||||
final TestNetworkManager tnm =
|
mContext.getSystemService(TestNetworkManager.class),
|
||||||
mContext.getSystemService(TestNetworkManager.class);
|
android.Manifest.permission.MANAGE_TEST_NETWORKS);
|
||||||
return tnm.createTunInterface(new LinkAddress[]{TEST_LINKADDR});
|
final TestNetworkInterface testNetworkInterface = runWithShellPermissionIdentity(() ->
|
||||||
}, android.Manifest.permission.MANAGE_TEST_NETWORKS,
|
tnm.createTunInterface(new LinkAddress[]{TEST_LINKADDR}),
|
||||||
|
android.Manifest.permission.MANAGE_TEST_NETWORKS,
|
||||||
android.Manifest.permission.NETWORK_SETTINGS);
|
android.Manifest.permission.NETWORK_SETTINGS);
|
||||||
assertNotNull(testNetworkInterface);
|
assertNotNull(testNetworkInterface);
|
||||||
|
|
||||||
@@ -1600,24 +1601,23 @@ public class ConnectivityManagerTest {
|
|||||||
assertThrows(SecurityException.class,
|
assertThrows(SecurityException.class,
|
||||||
() -> mCm.requestBackgroundNetwork(testRequest, null, callback));
|
() -> mCm.requestBackgroundNetwork(testRequest, null, callback));
|
||||||
|
|
||||||
|
Network testNetwork = null;
|
||||||
try {
|
try {
|
||||||
// Request background test network via Shell identity which has NETWORK_SETTINGS
|
// Request background test network via Shell identity which has NETWORK_SETTINGS
|
||||||
// permission granted.
|
// permission granted.
|
||||||
runWithShellPermissionIdentity(
|
runWithShellPermissionIdentity(
|
||||||
() -> mCm.requestBackgroundNetwork(testRequest, null, callback),
|
() -> mCm.requestBackgroundNetwork(testRequest, null, callback),
|
||||||
android.Manifest.permission.NETWORK_SETTINGS);
|
new String[] { android.Manifest.permission.NETWORK_SETTINGS });
|
||||||
|
|
||||||
// Register the test network agent which has no foreground request associated to it.
|
// Register the test network agent which has no foreground request associated to it.
|
||||||
// And verify it can satisfy the background network request just fired.
|
// And verify it can satisfy the background network request just fired.
|
||||||
final Binder binder = new Binder();
|
final Binder binder = new Binder();
|
||||||
runWithShellPermissionIdentity(() -> {
|
runWithShellPermissionIdentity(() ->
|
||||||
final TestNetworkManager tnm =
|
tnm.setupTestNetwork(testNetworkInterface.getInterfaceName(), binder),
|
||||||
mContext.getSystemService(TestNetworkManager.class);
|
new String[] { android.Manifest.permission.MANAGE_TEST_NETWORKS,
|
||||||
tnm.setupTestNetwork(testNetworkInterface.getInterfaceName(), binder);
|
android.Manifest.permission.NETWORK_SETTINGS });
|
||||||
}, android.Manifest.permission.MANAGE_TEST_NETWORKS,
|
|
||||||
android.Manifest.permission.NETWORK_SETTINGS);
|
|
||||||
waitForAvailable(callback);
|
waitForAvailable(callback);
|
||||||
final Network testNetwork = callback.getLastAvailableNetwork();
|
testNetwork = callback.getLastAvailableNetwork();
|
||||||
assertNotNull(testNetwork);
|
assertNotNull(testNetwork);
|
||||||
|
|
||||||
// The test network that has just connected is a foreground network,
|
// The test network that has just connected is a foreground network,
|
||||||
@@ -1633,6 +1633,16 @@ public class ConnectivityManagerTest {
|
|||||||
assertFalse("expected background network, but got " + nc,
|
assertFalse("expected background network, but got " + nc,
|
||||||
nc.hasCapability(NET_CAPABILITY_FOREGROUND));
|
nc.hasCapability(NET_CAPABILITY_FOREGROUND));
|
||||||
} finally {
|
} finally {
|
||||||
|
final Network n = testNetwork;
|
||||||
|
runWithShellPermissionIdentity(() -> {
|
||||||
|
if (null != n) {
|
||||||
|
tnm.teardownTestNetwork(n);
|
||||||
|
callback.eventuallyExpect(CallbackEntry.LOST,
|
||||||
|
NETWORK_CALLBACK_TIMEOUT_MS,
|
||||||
|
lost -> n.equals(lost.getNetwork()));
|
||||||
|
}
|
||||||
|
testNetworkInterface.getFileDescriptor().close();
|
||||||
|
}, new String[] { android.Manifest.permission.MANAGE_TEST_NETWORKS });
|
||||||
mCm.unregisterNetworkCallback(callback);
|
mCm.unregisterNetworkCallback(callback);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user