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
17package android.net;
18
19import static android.net.NetworkCapabilities.NET_CAPABILITY_CBS;
20import static android.net.NetworkCapabilities.NET_CAPABILITY_DUN;
21import static android.net.NetworkCapabilities.NET_CAPABILITY_EIMS;
22import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET;
23import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_METERED;
24import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED;
25import static android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED;
26import static android.net.NetworkCapabilities.RESTRICTED_CAPABILITIES;
27import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR;
28import static android.net.NetworkCapabilities.TRANSPORT_WIFI;
29import static android.net.NetworkCapabilities.UNRESTRICTED_CAPABILITIES;
30import static org.junit.Assert.assertEquals;
31import static org.junit.Assert.assertFalse;
32import static org.junit.Assert.assertNotEquals;
33import static org.junit.Assert.assertTrue;
34
35
36import android.net.NetworkCapabilities;
37import android.support.test.runner.AndroidJUnit4;
38import android.test.suitebuilder.annotation.SmallTest;
39
40import org.junit.Test;
41import org.junit.runner.RunWith;
42
43
44@RunWith(AndroidJUnit4.class)
45@SmallTest
46public class NetworkCapabilitiesTest {
47    @Test
48    public void testMaybeMarkCapabilitiesRestricted() {
49        // verify EIMS is restricted
50        assertEquals((1 << NET_CAPABILITY_EIMS) & RESTRICTED_CAPABILITIES,
51                (1 << NET_CAPABILITY_EIMS));
52
53        // verify CBS is also restricted
54        assertEquals((1 << NET_CAPABILITY_CBS) & RESTRICTED_CAPABILITIES,
55                (1 << NET_CAPABILITY_CBS));
56
57        // verify default is not restricted
58        assertEquals((1 << NET_CAPABILITY_INTERNET) & RESTRICTED_CAPABILITIES, 0);
59
60        // just to see
61        assertEquals(RESTRICTED_CAPABILITIES & UNRESTRICTED_CAPABILITIES, 0);
62
63        // check that internet does not get restricted
64        NetworkCapabilities netCap = new NetworkCapabilities();
65        netCap.addCapability(NET_CAPABILITY_INTERNET);
66        netCap.maybeMarkCapabilitiesRestricted();
67        assertTrue(netCap.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
68
69        // metered-ness shouldn't matter
70        netCap = new NetworkCapabilities();
71        netCap.addCapability(NET_CAPABILITY_INTERNET);
72        netCap.addCapability(NET_CAPABILITY_NOT_METERED);
73        netCap.maybeMarkCapabilitiesRestricted();
74        assertTrue(netCap.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
75        netCap = new NetworkCapabilities();
76        netCap.addCapability(NET_CAPABILITY_INTERNET);
77        netCap.removeCapability(NET_CAPABILITY_NOT_METERED);
78        netCap.maybeMarkCapabilitiesRestricted();
79        assertTrue(netCap.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
80
81        // add EIMS - bundled with unrestricted means it's unrestricted
82        netCap = new NetworkCapabilities();
83        netCap.addCapability(NET_CAPABILITY_INTERNET);
84        netCap.addCapability(NET_CAPABILITY_EIMS);
85        netCap.addCapability(NET_CAPABILITY_NOT_METERED);
86        netCap.maybeMarkCapabilitiesRestricted();
87        assertTrue(netCap.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
88        netCap = new NetworkCapabilities();
89        netCap.addCapability(NET_CAPABILITY_INTERNET);
90        netCap.addCapability(NET_CAPABILITY_EIMS);
91        netCap.removeCapability(NET_CAPABILITY_NOT_METERED);
92        netCap.maybeMarkCapabilitiesRestricted();
93        assertTrue(netCap.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
94
95        // just a restricted cap should be restricted regardless of meteredness
96        netCap = new NetworkCapabilities();
97        netCap.addCapability(NET_CAPABILITY_EIMS);
98        netCap.addCapability(NET_CAPABILITY_NOT_METERED);
99        netCap.maybeMarkCapabilitiesRestricted();
100        assertFalse(netCap.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
101        netCap = new NetworkCapabilities();
102        netCap.addCapability(NET_CAPABILITY_EIMS);
103        netCap.removeCapability(NET_CAPABILITY_NOT_METERED);
104        netCap.maybeMarkCapabilitiesRestricted();
105        assertFalse(netCap.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
106
107        // try 2 restricted caps
108        netCap = new NetworkCapabilities();
109        netCap.addCapability(NET_CAPABILITY_CBS);
110        netCap.addCapability(NET_CAPABILITY_EIMS);
111        netCap.addCapability(NET_CAPABILITY_NOT_METERED);
112        netCap.maybeMarkCapabilitiesRestricted();
113        assertFalse(netCap.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
114        netCap = new NetworkCapabilities();
115        netCap.addCapability(NET_CAPABILITY_CBS);
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
122    @Test
123    public void testDescribeImmutableDifferences() {
124        NetworkCapabilities nc1;
125        NetworkCapabilities nc2;
126
127        // Transports changing
128        nc1 = new NetworkCapabilities().addTransportType(TRANSPORT_CELLULAR);
129        nc2 = new NetworkCapabilities().addTransportType(TRANSPORT_WIFI);
130        assertNotEquals("", nc1.describeImmutableDifferences(nc2));
131        assertEquals("", nc1.describeImmutableDifferences(nc1));
132
133        // Mutable capability changing
134        nc1 = new NetworkCapabilities().addCapability(NET_CAPABILITY_VALIDATED);
135        nc2 = new NetworkCapabilities();
136        assertEquals("", nc1.describeImmutableDifferences(nc2));
137        assertEquals("", nc1.describeImmutableDifferences(nc1));
138
139        // NOT_METERED changing (http://b/63326103)
140        nc1 = new NetworkCapabilities()
141                .addCapability(NET_CAPABILITY_NOT_METERED)
142                .addCapability(NET_CAPABILITY_INTERNET);
143        nc2 = new NetworkCapabilities().addCapability(NET_CAPABILITY_INTERNET);
144        assertEquals("", nc1.describeImmutableDifferences(nc2));
145        assertEquals("", nc1.describeImmutableDifferences(nc1));
146
147        // DUN changing (http://b/65257223)
148        nc1 = new NetworkCapabilities()
149                .addCapability(NET_CAPABILITY_DUN)
150                .addCapability(NET_CAPABILITY_INTERNET);
151        nc2 = new NetworkCapabilities().addCapability(NET_CAPABILITY_INTERNET);
152        assertEquals("", nc1.describeImmutableDifferences(nc2));
153        assertEquals("", nc1.describeImmutableDifferences(nc1));
154
155        // Immutable capability changing
156        nc1 = new NetworkCapabilities()
157                .addCapability(NET_CAPABILITY_INTERNET)
158                .removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
159        nc2 = new NetworkCapabilities().addCapability(NET_CAPABILITY_INTERNET);
160        assertNotEquals("", nc1.describeImmutableDifferences(nc2));
161        assertEquals("", nc1.describeImmutableDifferences(nc1));
162
163        // Specifier changing
164        nc1 = new NetworkCapabilities().addTransportType(TRANSPORT_WIFI);
165        nc2 = new NetworkCapabilities()
166                .addTransportType(TRANSPORT_WIFI)
167                .setNetworkSpecifier(new StringNetworkSpecifier("specs"));
168        assertNotEquals("", nc1.describeImmutableDifferences(nc2));
169        assertEquals("", nc1.describeImmutableDifferences(nc1));
170    }
171}
172