Fix setCapabilities.
P introduced setSSID, UIDs and unwanted capabilities. None of these exhibit commutative behavior through combineCapabilities because their semantics don't allow it. Therefore NetworkRequest.setCapabilities() is badly broken around any of these. Look at the comments in the new tests to realize the extent of the damage. Bug: 79748782 Test: new tests written, old tests pass Change-Id: Ie46581bdaf9ecc2f14aab44788bbdb27a3fec8c1
This commit is contained in:
@@ -56,6 +56,7 @@ import java.util.Set;
|
||||
@SmallTest
|
||||
public class NetworkCapabilitiesTest {
|
||||
private static final String TEST_SSID = "TEST_SSID";
|
||||
private static final String DIFFERENT_TEST_SSID = "DIFFERENT_TEST_SSID";
|
||||
|
||||
@Test
|
||||
public void testMaybeMarkCapabilitiesRestricted() {
|
||||
@@ -374,6 +375,12 @@ public class NetworkCapabilitiesTest {
|
||||
assertFalse(nc1.satisfiedByNetworkCapabilities(nc2));
|
||||
}
|
||||
|
||||
private ArraySet<UidRange> uidRange(int from, int to) {
|
||||
final ArraySet<UidRange> range = new ArraySet<>(1);
|
||||
range.add(new UidRange(from, to));
|
||||
return range;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCombineCapabilities() {
|
||||
NetworkCapabilities nc1 = new NetworkCapabilities();
|
||||
@@ -400,14 +407,30 @@ public class NetworkCapabilitiesTest {
|
||||
nc2.combineCapabilities(nc1);
|
||||
assertTrue(TEST_SSID.equals(nc2.getSSID()));
|
||||
|
||||
// Because they now have the same SSID, the folllowing call should not throw
|
||||
// Because they now have the same SSID, the following call should not throw
|
||||
nc2.combineCapabilities(nc1);
|
||||
|
||||
nc1.setSSID("different " + TEST_SSID);
|
||||
nc1.setSSID(DIFFERENT_TEST_SSID);
|
||||
try {
|
||||
nc2.combineCapabilities(nc1);
|
||||
fail("Expected IllegalStateException: can't combine different SSIDs");
|
||||
} catch (IllegalStateException expected) {}
|
||||
nc1.setSSID(TEST_SSID);
|
||||
|
||||
nc1.setUids(uidRange(10, 13));
|
||||
assertNotEquals(nc1, nc2);
|
||||
nc2.combineCapabilities(nc1); // Everything + 10~13 is still everything.
|
||||
assertNotEquals(nc1, nc2);
|
||||
nc1.combineCapabilities(nc2); // 10~13 + everything is everything.
|
||||
assertEquals(nc1, nc2);
|
||||
nc1.setUids(uidRange(10, 13));
|
||||
nc2.setUids(uidRange(20, 23));
|
||||
assertNotEquals(nc1, nc2);
|
||||
nc1.combineCapabilities(nc2);
|
||||
assertTrue(nc1.appliesToUid(12));
|
||||
assertFalse(nc2.appliesToUid(12));
|
||||
assertTrue(nc1.appliesToUid(22));
|
||||
assertTrue(nc2.appliesToUid(22));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -446,4 +469,38 @@ public class NetworkCapabilitiesTest {
|
||||
p.setDataPosition(0);
|
||||
assertEquals(NetworkCapabilities.CREATOR.createFromParcel(p), netCap);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSet() {
|
||||
NetworkCapabilities nc1 = new NetworkCapabilities();
|
||||
NetworkCapabilities nc2 = new NetworkCapabilities();
|
||||
|
||||
nc1.addUnwantedCapability(NET_CAPABILITY_CAPTIVE_PORTAL);
|
||||
nc1.addCapability(NET_CAPABILITY_NOT_ROAMING);
|
||||
assertNotEquals(nc1, nc2);
|
||||
nc2.set(nc1);
|
||||
assertEquals(nc1, nc2);
|
||||
assertTrue(nc2.hasCapability(NET_CAPABILITY_NOT_ROAMING));
|
||||
assertTrue(nc2.hasUnwantedCapability(NET_CAPABILITY_CAPTIVE_PORTAL));
|
||||
|
||||
// This will effectively move NOT_ROAMING capability from required to unwanted for nc1.
|
||||
nc1.addUnwantedCapability(NET_CAPABILITY_NOT_ROAMING);
|
||||
nc1.setSSID(TEST_SSID);
|
||||
nc2.set(nc1);
|
||||
assertEquals(nc1, nc2);
|
||||
// Contrary to combineCapabilities, set() will have removed the NOT_ROAMING capability
|
||||
// from nc2.
|
||||
assertFalse(nc2.hasCapability(NET_CAPABILITY_NOT_ROAMING));
|
||||
assertTrue(nc2.hasUnwantedCapability(NET_CAPABILITY_NOT_ROAMING));
|
||||
assertTrue(TEST_SSID.equals(nc2.getSSID()));
|
||||
|
||||
nc1.setSSID(DIFFERENT_TEST_SSID);
|
||||
nc2.set(nc1);
|
||||
assertEquals(nc1, nc2);
|
||||
assertTrue(DIFFERENT_TEST_SSID.equals(nc2.getSSID()));
|
||||
|
||||
nc1.setUids(uidRange(10, 13));
|
||||
nc2.set(nc1); // Overwrites, as opposed to combineCapabilities
|
||||
assertEquals(nc1, nc2);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user