NetworkCapabilities: fix describeImmutableDifferences
This patch fixes the mask used in describeImmutableDifferences which did
not correctly turn NET_CAPABILITY_NOT_METERED into bit flag.
Bug: 63326103
Test: added unit tests, runtest frameworks-net
Merged-In: Ib6b390b1daef5912859302692af7dcd6cfd3e39a
Merged-In: If38efacdeec8476880835657938e435f9b598525
Merged-In: Ieccad46fcffcaf748f5644b04617e9a82527000e
Merged-In: I533ef8fe369cec19d283ff2950314fce6e28cffd
Merged-In: I12636c6699ff60487a28570208e819ea0b66fa2e
Merged-In: Ie5df14e0ea1c12e0cfabe87978ac6c9b744353b2
(cherry picked from commit 2ecb9408f4102687f20f9ca19c13071ac6098cc6)
Change-Id: I74ecf34a2c079c74152d00caea2c220e9c6d1fa5
diff --git a/core/java/android/net/NetworkCapabilities.java b/core/java/android/net/NetworkCapabilities.java
index 6ce9642..0a5b9c1 100644
--- a/core/java/android/net/NetworkCapabilities.java
+++ b/core/java/android/net/NetworkCapabilities.java
@@ -775,7 +775,7 @@
// TODO: consider only enforcing that capabilities are not removed, allowing addition.
// Ignore NOT_METERED being added or removed as it is effectively dynamic. http://b/63326103
// TODO: properly support NOT_METERED as a mutable and requestable capability.
- final long mask = ~MUTABLE_CAPABILITIES & ~NET_CAPABILITY_NOT_METERED;
+ final long mask = ~MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_NOT_METERED);
long oldImmutableCapabilities = this.mNetworkCapabilities & mask;
long newImmutableCapabilities = that.mNetworkCapabilities & mask;
if (oldImmutableCapabilities != newImmutableCapabilities) {
diff --git a/tests/net/java/android/net/NetworkCapabilitiesTest.java b/tests/net/java/android/net/NetworkCapabilitiesTest.java
index e3b06c8..7346f9f 100644
--- a/tests/net/java/android/net/NetworkCapabilitiesTest.java
+++ b/tests/net/java/android/net/NetworkCapabilitiesTest.java
@@ -21,10 +21,14 @@
import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET;
import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_METERED;
import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED;
+import static android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED;
import static android.net.NetworkCapabilities.RESTRICTED_CAPABILITIES;
+import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR;
+import static android.net.NetworkCapabilities.TRANSPORT_WIFI;
import static android.net.NetworkCapabilities.UNRESTRICTED_CAPABILITIES;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
@@ -114,4 +118,45 @@
assertFalse(netCap.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
}
+ @Test
+ public void testDescribeImmutableDifferences() {
+ NetworkCapabilities nc1;
+ NetworkCapabilities nc2;
+
+ // Transports changing
+ nc1 = new NetworkCapabilities().addTransportType(TRANSPORT_CELLULAR);
+ nc2 = new NetworkCapabilities().addTransportType(TRANSPORT_WIFI);
+ assertNotEquals("", nc1.describeImmutableDifferences(nc2));
+ assertEquals("", nc1.describeImmutableDifferences(nc1));
+
+ // Mutable capability changing
+ nc1 = new NetworkCapabilities().addCapability(NET_CAPABILITY_VALIDATED);
+ nc2 = new NetworkCapabilities();
+ assertEquals("", nc1.describeImmutableDifferences(nc2));
+ assertEquals("", nc1.describeImmutableDifferences(nc1));
+
+ // NOT_METERED changing (http://b/63326103)
+ nc1 = new NetworkCapabilities()
+ .addCapability(NET_CAPABILITY_NOT_METERED)
+ .addCapability(NET_CAPABILITY_INTERNET);
+ nc2 = new NetworkCapabilities().addCapability(NET_CAPABILITY_INTERNET);
+ assertEquals("", nc1.describeImmutableDifferences(nc2));
+ assertEquals("", nc1.describeImmutableDifferences(nc1));
+
+ // Immutable capability changing
+ nc1 = new NetworkCapabilities()
+ .addCapability(NET_CAPABILITY_INTERNET)
+ .removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
+ nc2 = new NetworkCapabilities().addCapability(NET_CAPABILITY_INTERNET);
+ assertNotEquals("", nc1.describeImmutableDifferences(nc2));
+ assertEquals("", nc1.describeImmutableDifferences(nc1));
+
+ // Specifier changing
+ nc1 = new NetworkCapabilities().addTransportType(TRANSPORT_WIFI);
+ nc2 = new NetworkCapabilities()
+ .addTransportType(TRANSPORT_WIFI)
+ .setNetworkSpecifier(new StringNetworkSpecifier("specs"));
+ assertNotEquals("", nc1.describeImmutableDifferences(nc2));
+ assertEquals("", nc1.describeImmutableDifferences(nc1));
+ }
}