1 /*
<lambda>null2  * Copyright (C) 2022 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.netstats
18 
19 import android.net.NetworkStats.DEFAULT_NETWORK_ALL
20 import android.net.NetworkStats.METERED_ALL
21 import android.net.NetworkStats.METERED_YES
22 import android.net.NetworkStats.ROAMING_ALL
23 import android.net.NetworkStats.ROAMING_YES
24 import android.net.NetworkTemplate
25 import android.net.NetworkTemplate.MATCH_BLUETOOTH
26 import android.net.NetworkTemplate.MATCH_CARRIER
27 import android.net.NetworkTemplate.MATCH_ETHERNET
28 import android.net.NetworkTemplate.MATCH_MOBILE
29 import android.net.NetworkTemplate.MATCH_PROXY
30 import android.net.NetworkTemplate.MATCH_WIFI
31 import android.net.NetworkTemplate.NETWORK_TYPE_ALL
32 import android.net.NetworkTemplate.OEM_MANAGED_ALL
33 import android.os.Build
34 import android.telephony.TelephonyManager
35 import com.android.testutils.ConnectivityModuleTest
36 import com.android.testutils.DevSdkIgnoreRule
37 import com.android.testutils.SC_V2
38 import kotlin.test.assertEquals
39 import kotlin.test.assertFailsWith
40 import org.junit.Rule
41 import org.junit.Test
42 import org.junit.runner.RunWith
43 import org.junit.runners.JUnit4
44 
45 private const val TEST_IMSI1 = "imsi"
46 private const val TEST_WIFI_KEY1 = "wifiKey1"
47 private const val TEST_WIFI_KEY2 = "wifiKey2"
48 
49 @RunWith(JUnit4::class)
50 @ConnectivityModuleTest
51 class NetworkTemplateTest {
52     @Rule
53     @JvmField
54     val ignoreRule = DevSdkIgnoreRule(ignoreClassUpTo = SC_V2)
55 
56     @Test
57     fun testBuilderMatchRules() {
58         // Verify unknown match rules cannot construct templates.
59         listOf(Integer.MIN_VALUE, -1, Integer.MAX_VALUE).forEach {
60             assertFailsWith<IllegalArgumentException> {
61                 NetworkTemplate.Builder(it).build()
62             }
63         }
64 
65         // Verify template which matches metered cellular and carrier networks with
66         // the given IMSI. See buildTemplateMobileAll and buildTemplateCarrierMetered.
67         listOf(MATCH_MOBILE, MATCH_CARRIER).forEach { matchRule ->
68             NetworkTemplate.Builder(matchRule).setSubscriberIds(setOf(TEST_IMSI1))
69                     .setMeteredness(METERED_YES).build().let {
70                         val expectedTemplate = NetworkTemplate(matchRule, arrayOf(TEST_IMSI1),
71                                 emptyArray<String>(), METERED_YES, ROAMING_ALL, DEFAULT_NETWORK_ALL,
72                                 NETWORK_TYPE_ALL, OEM_MANAGED_ALL)
73                         assertEquals(expectedTemplate, it)
74                     }
75         }
76 
77         // Verify template which matches roaming cellular and carrier networks with
78         // the given IMSI.
79         listOf(MATCH_MOBILE, MATCH_CARRIER).forEach { matchRule ->
80             NetworkTemplate.Builder(matchRule).setSubscriberIds(setOf(TEST_IMSI1))
81                     .setRoaming(ROAMING_YES).setMeteredness(METERED_YES).build().let {
82                         val expectedTemplate = NetworkTemplate(matchRule, arrayOf(TEST_IMSI1),
83                                 emptyArray<String>(), METERED_YES, ROAMING_YES, DEFAULT_NETWORK_ALL,
84                                 NETWORK_TYPE_ALL, OEM_MANAGED_ALL)
85                         assertEquals(expectedTemplate, it)
86                     }
87         }
88 
89         // Verify carrier template cannot be created without IMSI.
90         assertFailsWith<IllegalArgumentException> {
91             NetworkTemplate.Builder(MATCH_CARRIER).build()
92         }
93 
94         // Verify carrier and mobile template cannot contain one of subscriber Id is null.
95         assertFailsWith<IllegalArgumentException> {
96             NetworkTemplate.Builder(MATCH_CARRIER).setSubscriberIds(setOf(null)).build()
97         }
98         val firstSdk = Build.VERSION.DEVICE_INITIAL_SDK_INT
99         if (firstSdk > Build.VERSION_CODES.TIRAMISU) {
100             assertFailsWith<IllegalArgumentException> {
101                 NetworkTemplate.Builder(MATCH_MOBILE).setSubscriberIds(setOf(null)).build()
102             }
103         } else {
104             NetworkTemplate.Builder(MATCH_MOBILE).setSubscriberIds(setOf(null)).build().let {
105                 val expectedTemplate = NetworkTemplate(
106                     MATCH_MOBILE,
107                     arrayOfNulls<String>(1) /*subscriberIds*/,
108                     emptyArray<String>() /*wifiNetworkKey*/,
109                     METERED_ALL,
110                     ROAMING_ALL,
111                     DEFAULT_NETWORK_ALL,
112                     NETWORK_TYPE_ALL,
113                     OEM_MANAGED_ALL
114                 )
115                 assertEquals(expectedTemplate, it)
116             }
117         }
118 
119         // Verify template which matches metered cellular networks,
120         // regardless of IMSI. See buildTemplateMobileWildcard.
121         NetworkTemplate.Builder(MATCH_MOBILE).setMeteredness(METERED_YES).build().let {
122             val expectedTemplate = NetworkTemplate(MATCH_MOBILE,
123                     emptyArray<String>() /*subscriberIds*/, emptyArray<String>() /*wifiNetworkKey*/,
124                     METERED_YES, ROAMING_ALL, DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL,
125                     OEM_MANAGED_ALL)
126             assertEquals(expectedTemplate, it)
127         }
128 
129         // Verify template which matches metered cellular networks and ratType.
130         NetworkTemplate.Builder(MATCH_MOBILE).setSubscriberIds(setOf(TEST_IMSI1))
131                 .setMeteredness(METERED_YES).setRatType(TelephonyManager.NETWORK_TYPE_UMTS)
132                 .build().let {
133                     val expectedTemplate = NetworkTemplate(MATCH_MOBILE, arrayOf(TEST_IMSI1),
134                             emptyArray<String>(), METERED_YES, ROAMING_ALL, DEFAULT_NETWORK_ALL,
135                             TelephonyManager.NETWORK_TYPE_UMTS, OEM_MANAGED_ALL)
136                     assertEquals(expectedTemplate, it)
137                 }
138 
139         // Verify template which matches all wifi networks,
140         // regardless of Wifi Network Key. See buildTemplateWifiWildcard and buildTemplateWifi.
141         NetworkTemplate.Builder(MATCH_WIFI).build().let {
142             val expectedTemplate = NetworkTemplate(MATCH_WIFI,
143                     emptyArray<String>() /*subscriberIds*/, emptyArray<String>(), METERED_ALL,
144                     ROAMING_ALL, DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL, OEM_MANAGED_ALL)
145             assertEquals(expectedTemplate, it)
146         }
147 
148         // Verify template which matches wifi networks with the given Wifi Network Key.
149         // See buildTemplateWifi(wifiNetworkKey).
150         NetworkTemplate.Builder(MATCH_WIFI).setWifiNetworkKeys(setOf(TEST_WIFI_KEY1)).build().let {
151             val expectedTemplate =
152                     NetworkTemplate(MATCH_WIFI, emptyArray<String>() /*subscriberIds*/,
153                     arrayOf(TEST_WIFI_KEY1), METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL,
154                     NETWORK_TYPE_ALL, OEM_MANAGED_ALL)
155             assertEquals(expectedTemplate, it)
156         }
157 
158         // Verify template which matches all wifi networks with the
159         // given Wifi Network Key, and IMSI. See buildTemplateWifi(wifiNetworkKey, subscriberId).
160         NetworkTemplate.Builder(MATCH_WIFI).setSubscriberIds(setOf(TEST_IMSI1))
161                 .setWifiNetworkKeys(setOf(TEST_WIFI_KEY1)).build().let {
162                     val expectedTemplate = NetworkTemplate(MATCH_WIFI, arrayOf(TEST_IMSI1),
163                             arrayOf(TEST_WIFI_KEY1), METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL,
164                             NETWORK_TYPE_ALL, OEM_MANAGED_ALL)
165                     assertEquals(expectedTemplate, it)
166                 }
167 
168         // Verify template which matches ethernet, bluetooth and proxy networks.
169         // See buildTemplateEthernet and buildTemplateBluetooth.
170         listOf(MATCH_ETHERNET, MATCH_BLUETOOTH, MATCH_PROXY).forEach { matchRule ->
171             NetworkTemplate.Builder(matchRule).build().let {
172                 val expectedTemplate = NetworkTemplate(matchRule,
173                         emptyArray<String>() /*subscriberIds*/, emptyArray<String>(),
174                         METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL,
175                         OEM_MANAGED_ALL)
176                 assertEquals(expectedTemplate, it)
177             }
178         }
179     }
180 
181     @Test
182     fun testBuilderWifiNetworkKeys() {
183         // Verify template builder which generates same template with the given different
184         // sequence keys.
185         NetworkTemplate.Builder(MATCH_WIFI).setWifiNetworkKeys(
186                 setOf(TEST_WIFI_KEY1, TEST_WIFI_KEY2)).build().let {
187             val expectedTemplate = NetworkTemplate.Builder(MATCH_WIFI).setWifiNetworkKeys(
188                     setOf(TEST_WIFI_KEY2, TEST_WIFI_KEY1)).build()
189             assertEquals(expectedTemplate, it)
190         }
191 
192         // Verify template which matches non-wifi networks with the given key is invalid.
193         listOf(MATCH_MOBILE, MATCH_CARRIER, MATCH_ETHERNET, MATCH_BLUETOOTH, -1,
194                 Integer.MAX_VALUE).forEach { matchRule ->
195             assertFailsWith<IllegalArgumentException> {
196                 NetworkTemplate.Builder(matchRule).setWifiNetworkKeys(setOf(TEST_WIFI_KEY1)).build()
197             }
198         }
199 
200         // Verify template which matches wifi networks with the given null key is invalid.
201         assertFailsWith<IllegalArgumentException> {
202             NetworkTemplate.Builder(MATCH_WIFI).setWifiNetworkKeys(setOf(null)).build()
203         }
204 
205         // Verify template which matches wifi wildcard with the given empty key set.
206         NetworkTemplate.Builder(MATCH_WIFI).setWifiNetworkKeys(setOf<String>()).build().let {
207             val expectedTemplate = NetworkTemplate(MATCH_WIFI,
208                     emptyArray<String>() /*subscriberIds*/, emptyArray<String>(),
209                     METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL,
210                     OEM_MANAGED_ALL)
211             assertEquals(expectedTemplate, it)
212         }
213     }
214 }
215