1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.net;
18 
19 import static android.net.NetworkCapabilities.LINK_BANDWIDTH_UNSPECIFIED;
20 import static android.net.NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL;
21 import static android.net.NetworkCapabilities.NET_CAPABILITY_CBS;
22 import static android.net.NetworkCapabilities.NET_CAPABILITY_EIMS;
23 import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET;
24 import static android.net.NetworkCapabilities.NET_CAPABILITY_MMS;
25 import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_METERED;
26 import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED;
27 import static android.net.NetworkCapabilities.NET_CAPABILITY_OEM_PAID;
28 import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING;
29 import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_VPN;
30 import static android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED;
31 import static android.net.NetworkCapabilities.NET_CAPABILITY_WIFI_P2P;
32 import static android.net.NetworkCapabilities.RESTRICTED_CAPABILITIES;
33 import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR;
34 import static android.net.NetworkCapabilities.TRANSPORT_WIFI;
35 import static android.net.NetworkCapabilities.UNRESTRICTED_CAPABILITIES;
36 
37 import static org.junit.Assert.assertArrayEquals;
38 import static org.junit.Assert.assertEquals;
39 import static org.junit.Assert.assertFalse;
40 import static org.junit.Assert.assertNotEquals;
41 import static org.junit.Assert.assertTrue;
42 import static org.junit.Assert.fail;
43 
44 import android.os.Parcel;
45 import android.support.test.runner.AndroidJUnit4;
46 import android.test.suitebuilder.annotation.SmallTest;
47 import android.util.ArraySet;
48 
49 
50 import org.junit.Test;
51 import org.junit.runner.RunWith;
52 
53 import java.util.Set;
54 
55 @RunWith(AndroidJUnit4.class)
56 @SmallTest
57 public class NetworkCapabilitiesTest {
58     private static final String TEST_SSID = "TEST_SSID";
59     private static final String DIFFERENT_TEST_SSID = "DIFFERENT_TEST_SSID";
60 
61     @Test
testMaybeMarkCapabilitiesRestricted()62     public void testMaybeMarkCapabilitiesRestricted() {
63         // verify EIMS is restricted
64         assertEquals((1 << NET_CAPABILITY_EIMS) & RESTRICTED_CAPABILITIES,
65                 (1 << NET_CAPABILITY_EIMS));
66 
67         // verify CBS is also restricted
68         assertEquals((1 << NET_CAPABILITY_CBS) & RESTRICTED_CAPABILITIES,
69                 (1 << NET_CAPABILITY_CBS));
70 
71         // verify default is not restricted
72         assertEquals((1 << NET_CAPABILITY_INTERNET) & RESTRICTED_CAPABILITIES, 0);
73 
74         // just to see
75         assertEquals(RESTRICTED_CAPABILITIES & UNRESTRICTED_CAPABILITIES, 0);
76 
77         // check that internet does not get restricted
78         NetworkCapabilities netCap = new NetworkCapabilities();
79         netCap.addCapability(NET_CAPABILITY_INTERNET);
80         netCap.maybeMarkCapabilitiesRestricted();
81         assertTrue(netCap.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
82 
83         // metered-ness shouldn't matter
84         netCap = new NetworkCapabilities();
85         netCap.addCapability(NET_CAPABILITY_INTERNET);
86         netCap.addCapability(NET_CAPABILITY_NOT_METERED);
87         netCap.maybeMarkCapabilitiesRestricted();
88         assertTrue(netCap.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
89         netCap = new NetworkCapabilities();
90         netCap.addCapability(NET_CAPABILITY_INTERNET);
91         netCap.removeCapability(NET_CAPABILITY_NOT_METERED);
92         netCap.maybeMarkCapabilitiesRestricted();
93         assertTrue(netCap.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
94 
95         // add EIMS - bundled with unrestricted means it's unrestricted
96         netCap = new NetworkCapabilities();
97         netCap.addCapability(NET_CAPABILITY_INTERNET);
98         netCap.addCapability(NET_CAPABILITY_EIMS);
99         netCap.addCapability(NET_CAPABILITY_NOT_METERED);
100         netCap.maybeMarkCapabilitiesRestricted();
101         assertTrue(netCap.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
102         netCap = new NetworkCapabilities();
103         netCap.addCapability(NET_CAPABILITY_INTERNET);
104         netCap.addCapability(NET_CAPABILITY_EIMS);
105         netCap.removeCapability(NET_CAPABILITY_NOT_METERED);
106         netCap.maybeMarkCapabilitiesRestricted();
107         assertTrue(netCap.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
108 
109         // just a restricted cap should be restricted regardless of meteredness
110         netCap = new NetworkCapabilities();
111         netCap.addCapability(NET_CAPABILITY_EIMS);
112         netCap.addCapability(NET_CAPABILITY_NOT_METERED);
113         netCap.maybeMarkCapabilitiesRestricted();
114         assertFalse(netCap.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
115         netCap = new NetworkCapabilities();
116         netCap.addCapability(NET_CAPABILITY_EIMS);
117         netCap.removeCapability(NET_CAPABILITY_NOT_METERED);
118         netCap.maybeMarkCapabilitiesRestricted();
119         assertFalse(netCap.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
120 
121         // try 2 restricted caps
122         netCap = new NetworkCapabilities();
123         netCap.addCapability(NET_CAPABILITY_CBS);
124         netCap.addCapability(NET_CAPABILITY_EIMS);
125         netCap.addCapability(NET_CAPABILITY_NOT_METERED);
126         netCap.maybeMarkCapabilitiesRestricted();
127         assertFalse(netCap.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
128         netCap = new NetworkCapabilities();
129         netCap.addCapability(NET_CAPABILITY_CBS);
130         netCap.addCapability(NET_CAPABILITY_EIMS);
131         netCap.removeCapability(NET_CAPABILITY_NOT_METERED);
132         netCap.maybeMarkCapabilitiesRestricted();
133         assertFalse(netCap.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
134     }
135 
136     @Test
testDescribeImmutableDifferences()137     public void testDescribeImmutableDifferences() {
138         NetworkCapabilities nc1;
139         NetworkCapabilities nc2;
140 
141         // Transports changing
142         nc1 = new NetworkCapabilities().addTransportType(TRANSPORT_CELLULAR);
143         nc2 = new NetworkCapabilities().addTransportType(TRANSPORT_WIFI);
144         assertNotEquals("", nc1.describeImmutableDifferences(nc2));
145         assertEquals("", nc1.describeImmutableDifferences(nc1));
146 
147         // Mutable capability changing
148         nc1 = new NetworkCapabilities().addCapability(NET_CAPABILITY_VALIDATED);
149         nc2 = new NetworkCapabilities();
150         assertEquals("", nc1.describeImmutableDifferences(nc2));
151         assertEquals("", nc1.describeImmutableDifferences(nc1));
152 
153         // NOT_METERED changing (http://b/63326103)
154         nc1 = new NetworkCapabilities()
155                 .addCapability(NET_CAPABILITY_NOT_METERED)
156                 .addCapability(NET_CAPABILITY_INTERNET);
157         nc2 = new NetworkCapabilities().addCapability(NET_CAPABILITY_INTERNET);
158         assertEquals("", nc1.describeImmutableDifferences(nc2));
159         assertEquals("", nc1.describeImmutableDifferences(nc1));
160 
161         // Immutable capability changing
162         nc1 = new NetworkCapabilities()
163                 .addCapability(NET_CAPABILITY_INTERNET)
164                 .removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
165         nc2 = new NetworkCapabilities().addCapability(NET_CAPABILITY_INTERNET);
166         assertNotEquals("", nc1.describeImmutableDifferences(nc2));
167         assertEquals("", nc1.describeImmutableDifferences(nc1));
168 
169         // Specifier changing
170         nc1 = new NetworkCapabilities().addTransportType(TRANSPORT_WIFI);
171         nc2 = new NetworkCapabilities()
172                 .addTransportType(TRANSPORT_WIFI)
173                 .setNetworkSpecifier(new StringNetworkSpecifier("specs"));
174         assertNotEquals("", nc1.describeImmutableDifferences(nc2));
175         assertEquals("", nc1.describeImmutableDifferences(nc1));
176     }
177 
178     @Test
testLinkBandwidthUtils()179     public void testLinkBandwidthUtils() {
180         assertEquals(LINK_BANDWIDTH_UNSPECIFIED, NetworkCapabilities
181                 .minBandwidth(LINK_BANDWIDTH_UNSPECIFIED, LINK_BANDWIDTH_UNSPECIFIED));
182         assertEquals(10, NetworkCapabilities
183                 .minBandwidth(LINK_BANDWIDTH_UNSPECIFIED, 10));
184         assertEquals(10, NetworkCapabilities
185                 .minBandwidth(10, LINK_BANDWIDTH_UNSPECIFIED));
186         assertEquals(10, NetworkCapabilities
187                 .minBandwidth(10, 20));
188 
189         assertEquals(LINK_BANDWIDTH_UNSPECIFIED, NetworkCapabilities
190                 .maxBandwidth(LINK_BANDWIDTH_UNSPECIFIED, LINK_BANDWIDTH_UNSPECIFIED));
191         assertEquals(10, NetworkCapabilities
192                 .maxBandwidth(LINK_BANDWIDTH_UNSPECIFIED, 10));
193         assertEquals(10, NetworkCapabilities
194                 .maxBandwidth(10, LINK_BANDWIDTH_UNSPECIFIED));
195         assertEquals(20, NetworkCapabilities
196                 .maxBandwidth(10, 20));
197     }
198 
199     @Test
testSetUids()200     public void testSetUids() {
201         final NetworkCapabilities netCap = new NetworkCapabilities();
202         final Set<UidRange> uids = new ArraySet<>();
203         uids.add(new UidRange(50, 100));
204         uids.add(new UidRange(3000, 4000));
205         netCap.setUids(uids);
206         assertTrue(netCap.appliesToUid(50));
207         assertTrue(netCap.appliesToUid(80));
208         assertTrue(netCap.appliesToUid(100));
209         assertTrue(netCap.appliesToUid(3000));
210         assertTrue(netCap.appliesToUid(3001));
211         assertFalse(netCap.appliesToUid(10));
212         assertFalse(netCap.appliesToUid(25));
213         assertFalse(netCap.appliesToUid(49));
214         assertFalse(netCap.appliesToUid(101));
215         assertFalse(netCap.appliesToUid(2000));
216         assertFalse(netCap.appliesToUid(100000));
217 
218         assertTrue(netCap.appliesToUidRange(new UidRange(50, 100)));
219         assertTrue(netCap.appliesToUidRange(new UidRange(70, 72)));
220         assertTrue(netCap.appliesToUidRange(new UidRange(3500, 3912)));
221         assertFalse(netCap.appliesToUidRange(new UidRange(1, 100)));
222         assertFalse(netCap.appliesToUidRange(new UidRange(49, 100)));
223         assertFalse(netCap.appliesToUidRange(new UidRange(1, 10)));
224         assertFalse(netCap.appliesToUidRange(new UidRange(60, 101)));
225         assertFalse(netCap.appliesToUidRange(new UidRange(60, 3400)));
226 
227         NetworkCapabilities netCap2 = new NetworkCapabilities();
228         // A new netcap object has null UIDs, so anything will satisfy it.
229         assertTrue(netCap2.satisfiedByUids(netCap));
230         // Still not equal though.
231         assertFalse(netCap2.equalsUids(netCap));
232         netCap2.setUids(uids);
233         assertTrue(netCap2.satisfiedByUids(netCap));
234         assertTrue(netCap.equalsUids(netCap2));
235         assertTrue(netCap2.equalsUids(netCap));
236 
237         uids.add(new UidRange(600, 700));
238         netCap2.setUids(uids);
239         assertFalse(netCap2.satisfiedByUids(netCap));
240         assertFalse(netCap.appliesToUid(650));
241         assertTrue(netCap2.appliesToUid(650));
242         netCap.combineCapabilities(netCap2);
243         assertTrue(netCap2.satisfiedByUids(netCap));
244         assertTrue(netCap.appliesToUid(650));
245         assertFalse(netCap.appliesToUid(500));
246 
247         assertTrue(new NetworkCapabilities().satisfiedByUids(netCap));
248         netCap.combineCapabilities(new NetworkCapabilities());
249         assertTrue(netCap.appliesToUid(500));
250         assertTrue(netCap.appliesToUidRange(new UidRange(1, 100000)));
251         assertFalse(netCap2.appliesToUid(500));
252         assertFalse(netCap2.appliesToUidRange(new UidRange(1, 100000)));
253         assertTrue(new NetworkCapabilities().satisfiedByUids(netCap));
254     }
255 
256     @Test
testParcelNetworkCapabilities()257     public void testParcelNetworkCapabilities() {
258         final Set<UidRange> uids = new ArraySet<>();
259         uids.add(new UidRange(50, 100));
260         uids.add(new UidRange(3000, 4000));
261         final NetworkCapabilities netCap = new NetworkCapabilities()
262             .addCapability(NET_CAPABILITY_INTERNET)
263             .setUids(uids)
264             .addCapability(NET_CAPABILITY_EIMS)
265             .addCapability(NET_CAPABILITY_NOT_METERED);
266         assertEqualsThroughMarshalling(netCap);
267         netCap.setSSID(TEST_SSID);
268         assertEqualsThroughMarshalling(netCap);
269     }
270 
271     @Test
testOemPaid()272     public void testOemPaid() {
273         NetworkCapabilities nc = new NetworkCapabilities();
274         // By default OEM_PAID is neither in the unwanted or required lists and the network is not
275         // restricted.
276         assertFalse(nc.hasUnwantedCapability(NET_CAPABILITY_OEM_PAID));
277         assertFalse(nc.hasCapability(NET_CAPABILITY_OEM_PAID));
278         nc.maybeMarkCapabilitiesRestricted();
279         assertTrue(nc.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
280 
281         // Adding OEM_PAID to capability list should make network restricted.
282         nc.addCapability(NET_CAPABILITY_OEM_PAID);
283         nc.addCapability(NET_CAPABILITY_INTERNET);  // Combine with unrestricted capability.
284         nc.maybeMarkCapabilitiesRestricted();
285         assertTrue(nc.hasCapability(NET_CAPABILITY_OEM_PAID));
286         assertFalse(nc.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
287 
288         // Now let's make request for OEM_PAID network.
289         NetworkCapabilities nr = new NetworkCapabilities();
290         nr.addCapability(NET_CAPABILITY_OEM_PAID);
291         nr.maybeMarkCapabilitiesRestricted();
292         assertTrue(nr.satisfiedByNetworkCapabilities(nc));
293 
294         // Request fails for network with the default capabilities.
295         assertFalse(nr.satisfiedByNetworkCapabilities(new NetworkCapabilities()));
296     }
297 
298     @Test
testUnwantedCapabilities()299     public void testUnwantedCapabilities() {
300         NetworkCapabilities network = new NetworkCapabilities();
301 
302         NetworkCapabilities request = new NetworkCapabilities();
303         assertTrue("Request: " + request + ", Network:" + network,
304                 request.satisfiedByNetworkCapabilities(network));
305 
306         // Adding capabilities that doesn't exist in the network anyway
307         request.addUnwantedCapability(NET_CAPABILITY_WIFI_P2P);
308         request.addUnwantedCapability(NET_CAPABILITY_NOT_METERED);
309         assertTrue(request.satisfiedByNetworkCapabilities(network));
310         assertArrayEquals(new int[] {NET_CAPABILITY_WIFI_P2P,
311                         NET_CAPABILITY_NOT_METERED},
312                 request.getUnwantedCapabilities());
313 
314         // This is a default capability, just want to make sure its there because we use it below.
315         assertTrue(network.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
316 
317         // Verify that adding unwanted capability will effectively remove it from capability list.
318         request.addUnwantedCapability(NET_CAPABILITY_NOT_RESTRICTED);
319         assertTrue(request.hasUnwantedCapability(NET_CAPABILITY_NOT_RESTRICTED));
320         assertFalse(request.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
321 
322 
323         // Now this request won't be satisfied because network contains NOT_RESTRICTED.
324         assertFalse(request.satisfiedByNetworkCapabilities(network));
325         network.removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
326         assertTrue(request.satisfiedByNetworkCapabilities(network));
327 
328         // Verify that adding capability will effectively remove it from unwanted list
329         request.addCapability(NET_CAPABILITY_NOT_RESTRICTED);
330         assertTrue(request.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
331         assertFalse(request.hasUnwantedCapability(NET_CAPABILITY_NOT_RESTRICTED));
332 
333         assertFalse(request.satisfiedByNetworkCapabilities(network));
334         network.addCapability(NET_CAPABILITY_NOT_RESTRICTED);
335         assertTrue(request.satisfiedByNetworkCapabilities(network));
336     }
337 
338     @Test
testEqualsNetCapabilities()339     public void testEqualsNetCapabilities() {
340         NetworkCapabilities nc1 = new NetworkCapabilities();
341         NetworkCapabilities nc2 = new NetworkCapabilities();
342         assertTrue(nc1.equalsNetCapabilities(nc2));
343         assertEquals(nc1, nc2);
344 
345         nc1.addCapability(NET_CAPABILITY_MMS);
346         assertFalse(nc1.equalsNetCapabilities(nc2));
347         assertNotEquals(nc1, nc2);
348         nc2.addCapability(NET_CAPABILITY_MMS);
349         assertTrue(nc1.equalsNetCapabilities(nc2));
350         assertEquals(nc1, nc2);
351 
352         nc1.addUnwantedCapability(NET_CAPABILITY_INTERNET);
353         assertFalse(nc1.equalsNetCapabilities(nc2));
354         nc2.addUnwantedCapability(NET_CAPABILITY_INTERNET);
355         assertTrue(nc1.equalsNetCapabilities(nc2));
356 
357         nc1.removeCapability(NET_CAPABILITY_INTERNET);
358         assertFalse(nc1.equalsNetCapabilities(nc2));
359         nc2.removeCapability(NET_CAPABILITY_INTERNET);
360         assertTrue(nc1.equalsNetCapabilities(nc2));
361     }
362 
363     @Test
testSSID()364     public void testSSID() {
365         NetworkCapabilities nc1 = new NetworkCapabilities();
366         NetworkCapabilities nc2 = new NetworkCapabilities();
367         assertTrue(nc2.satisfiedBySSID(nc1));
368 
369         nc1.setSSID(TEST_SSID);
370         assertTrue(nc2.satisfiedBySSID(nc1));
371         nc2.setSSID("different " + TEST_SSID);
372         assertFalse(nc2.satisfiedBySSID(nc1));
373 
374         assertTrue(nc1.satisfiedByImmutableNetworkCapabilities(nc2));
375         assertFalse(nc1.satisfiedByNetworkCapabilities(nc2));
376     }
377 
uidRange(int from, int to)378     private ArraySet<UidRange> uidRange(int from, int to) {
379         final ArraySet<UidRange> range = new ArraySet<>(1);
380         range.add(new UidRange(from, to));
381         return range;
382     }
383 
384     @Test
testCombineCapabilities()385     public void testCombineCapabilities() {
386         NetworkCapabilities nc1 = new NetworkCapabilities();
387         NetworkCapabilities nc2 = new NetworkCapabilities();
388 
389         nc1.addUnwantedCapability(NET_CAPABILITY_CAPTIVE_PORTAL);
390         nc1.addCapability(NET_CAPABILITY_NOT_ROAMING);
391         assertNotEquals(nc1, nc2);
392         nc2.combineCapabilities(nc1);
393         assertEquals(nc1, nc2);
394         assertTrue(nc2.hasCapability(NET_CAPABILITY_NOT_ROAMING));
395         assertTrue(nc2.hasUnwantedCapability(NET_CAPABILITY_CAPTIVE_PORTAL));
396 
397         // This will effectively move NOT_ROAMING capability from required to unwanted for nc1.
398         nc1.addUnwantedCapability(NET_CAPABILITY_NOT_ROAMING);
399 
400         nc2.combineCapabilities(nc1);
401         // We will get this capability in both requested and unwanted lists thus this request
402         // will never be satisfied.
403         assertTrue(nc2.hasCapability(NET_CAPABILITY_NOT_ROAMING));
404         assertTrue(nc2.hasUnwantedCapability(NET_CAPABILITY_NOT_ROAMING));
405 
406         nc1.setSSID(TEST_SSID);
407         nc2.combineCapabilities(nc1);
408         assertTrue(TEST_SSID.equals(nc2.getSSID()));
409 
410         // Because they now have the same SSID, the following call should not throw
411         nc2.combineCapabilities(nc1);
412 
413         nc1.setSSID(DIFFERENT_TEST_SSID);
414         try {
415             nc2.combineCapabilities(nc1);
416             fail("Expected IllegalStateException: can't combine different SSIDs");
417         } catch (IllegalStateException expected) {}
418         nc1.setSSID(TEST_SSID);
419 
420         nc1.setUids(uidRange(10, 13));
421         assertNotEquals(nc1, nc2);
422         nc2.combineCapabilities(nc1);  // Everything + 10~13 is still everything.
423         assertNotEquals(nc1, nc2);
424         nc1.combineCapabilities(nc2);  // 10~13 + everything is everything.
425         assertEquals(nc1, nc2);
426         nc1.setUids(uidRange(10, 13));
427         nc2.setUids(uidRange(20, 23));
428         assertNotEquals(nc1, nc2);
429         nc1.combineCapabilities(nc2);
430         assertTrue(nc1.appliesToUid(12));
431         assertFalse(nc2.appliesToUid(12));
432         assertTrue(nc1.appliesToUid(22));
433         assertTrue(nc2.appliesToUid(22));
434     }
435 
436     @Test
testSetCapabilities()437     public void testSetCapabilities() {
438         final int[] REQUIRED_CAPABILITIES = new int[] {
439                 NET_CAPABILITY_INTERNET, NET_CAPABILITY_NOT_VPN };
440         final int[] UNWANTED_CAPABILITIES = new int[] {
441                 NET_CAPABILITY_NOT_RESTRICTED, NET_CAPABILITY_NOT_METERED
442         };
443 
444         NetworkCapabilities nc1 = new NetworkCapabilities();
445         NetworkCapabilities nc2 = new NetworkCapabilities();
446 
447         nc1.setCapabilities(REQUIRED_CAPABILITIES, UNWANTED_CAPABILITIES);
448         assertArrayEquals(REQUIRED_CAPABILITIES, nc1.getCapabilities());
449 
450         // Verify that setting and adding capabilities leads to the same object state.
451         nc2.clearAll();
452         for (int cap : REQUIRED_CAPABILITIES) {
453             nc2.addCapability(cap);
454         }
455         for (int cap : UNWANTED_CAPABILITIES) {
456             nc2.addUnwantedCapability(cap);
457         }
458         assertEquals(nc1, nc2);
459     }
460 
assertEqualsThroughMarshalling(NetworkCapabilities netCap)461     private void assertEqualsThroughMarshalling(NetworkCapabilities netCap) {
462         Parcel p = Parcel.obtain();
463         netCap.writeToParcel(p, /* flags */ 0);
464         p.setDataPosition(0);
465         byte[] marshalledData = p.marshall();
466 
467         p = Parcel.obtain();
468         p.unmarshall(marshalledData, 0, marshalledData.length);
469         p.setDataPosition(0);
470         assertEquals(NetworkCapabilities.CREATOR.createFromParcel(p), netCap);
471     }
472 
473     @Test
testSet()474     public void testSet() {
475         NetworkCapabilities nc1 = new NetworkCapabilities();
476         NetworkCapabilities nc2 = new NetworkCapabilities();
477 
478         nc1.addUnwantedCapability(NET_CAPABILITY_CAPTIVE_PORTAL);
479         nc1.addCapability(NET_CAPABILITY_NOT_ROAMING);
480         assertNotEquals(nc1, nc2);
481         nc2.set(nc1);
482         assertEquals(nc1, nc2);
483         assertTrue(nc2.hasCapability(NET_CAPABILITY_NOT_ROAMING));
484         assertTrue(nc2.hasUnwantedCapability(NET_CAPABILITY_CAPTIVE_PORTAL));
485 
486         // This will effectively move NOT_ROAMING capability from required to unwanted for nc1.
487         nc1.addUnwantedCapability(NET_CAPABILITY_NOT_ROAMING);
488         nc1.setSSID(TEST_SSID);
489         nc2.set(nc1);
490         assertEquals(nc1, nc2);
491         // Contrary to combineCapabilities, set() will have removed the NOT_ROAMING capability
492         // from nc2.
493         assertFalse(nc2.hasCapability(NET_CAPABILITY_NOT_ROAMING));
494         assertTrue(nc2.hasUnwantedCapability(NET_CAPABILITY_NOT_ROAMING));
495         assertTrue(TEST_SSID.equals(nc2.getSSID()));
496 
497         nc1.setSSID(DIFFERENT_TEST_SSID);
498         nc2.set(nc1);
499         assertEquals(nc1, nc2);
500         assertTrue(DIFFERENT_TEST_SSID.equals(nc2.getSSID()));
501 
502         nc1.setUids(uidRange(10, 13));
503         nc2.set(nc1);  // Overwrites, as opposed to combineCapabilities
504         assertEquals(nc1, nc2);
505     }
506 }
507