• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 hidden match rules cannot construct templates.
66         assertFailsWith<IllegalArgumentException> {
67             NetworkTemplate.Builder(MATCH_PROXY).build()
68         }
69 
70         // Verify template which matches metered cellular and carrier networks with
71         // the given IMSI. See buildTemplateMobileAll and buildTemplateCarrierMetered.
72         listOf(MATCH_MOBILE, MATCH_CARRIER).forEach { matchRule ->
73             NetworkTemplate.Builder(matchRule).setSubscriberIds(setOf(TEST_IMSI1))
74                     .setMeteredness(METERED_YES).build().let {
75                         val expectedTemplate = NetworkTemplate(matchRule, arrayOf(TEST_IMSI1),
76                                 emptyArray<String>(), METERED_YES, ROAMING_ALL, DEFAULT_NETWORK_ALL,
77                                 NETWORK_TYPE_ALL, OEM_MANAGED_ALL)
78                         assertEquals(expectedTemplate, it)
79                     }
80         }
81 
82         // Verify template which matches roaming cellular and carrier networks with
83         // the given IMSI.
84         listOf(MATCH_MOBILE, MATCH_CARRIER).forEach { matchRule ->
85             NetworkTemplate.Builder(matchRule).setSubscriberIds(setOf(TEST_IMSI1))
86                     .setRoaming(ROAMING_YES).setMeteredness(METERED_YES).build().let {
87                         val expectedTemplate = NetworkTemplate(matchRule, arrayOf(TEST_IMSI1),
88                                 emptyArray<String>(), METERED_YES, ROAMING_YES, DEFAULT_NETWORK_ALL,
89                                 NETWORK_TYPE_ALL, OEM_MANAGED_ALL)
90                         assertEquals(expectedTemplate, it)
91                     }
92         }
93 
94         // Verify carrier template cannot be created without IMSI.
95         assertFailsWith<IllegalArgumentException> {
96             NetworkTemplate.Builder(MATCH_CARRIER).build()
97         }
98 
99         // Verify carrier and mobile template cannot contain one of subscriber Id is null.
100         assertFailsWith<IllegalArgumentException> {
101             NetworkTemplate.Builder(MATCH_CARRIER).setSubscriberIds(setOf(null)).build()
102         }
103         val firstSdk = Build.VERSION.DEVICE_INITIAL_SDK_INT
104         if (firstSdk > Build.VERSION_CODES.TIRAMISU) {
105             assertFailsWith<IllegalArgumentException> {
106                 NetworkTemplate.Builder(MATCH_MOBILE).setSubscriberIds(setOf(null)).build()
107             }
108         } else {
109             NetworkTemplate.Builder(MATCH_MOBILE).setSubscriberIds(setOf(null)).build().let {
110                 val expectedTemplate = NetworkTemplate(
111                     MATCH_MOBILE,
112                     arrayOfNulls<String>(1) /*subscriberIds*/,
113                     emptyArray<String>() /*wifiNetworkKey*/,
114                     METERED_ALL,
115                     ROAMING_ALL,
116                     DEFAULT_NETWORK_ALL,
117                     NETWORK_TYPE_ALL,
118                     OEM_MANAGED_ALL
119                 )
120                 assertEquals(expectedTemplate, it)
121             }
122         }
123 
124         // Verify template which matches metered cellular networks,
125         // regardless of IMSI. See buildTemplateMobileWildcard.
126         NetworkTemplate.Builder(MATCH_MOBILE).setMeteredness(METERED_YES).build().let {
127             val expectedTemplate = NetworkTemplate(MATCH_MOBILE,
128                     emptyArray<String>() /*subscriberIds*/, emptyArray<String>() /*wifiNetworkKey*/,
129                     METERED_YES, ROAMING_ALL, DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL,
130                     OEM_MANAGED_ALL)
131             assertEquals(expectedTemplate, it)
132         }
133 
134         // Verify template which matches metered cellular networks and ratType.
135         NetworkTemplate.Builder(MATCH_MOBILE).setSubscriberIds(setOf(TEST_IMSI1))
136                 .setMeteredness(METERED_YES).setRatType(TelephonyManager.NETWORK_TYPE_UMTS)
137                 .build().let {
138                     val expectedTemplate = NetworkTemplate(MATCH_MOBILE, arrayOf(TEST_IMSI1),
139                             emptyArray<String>(), METERED_YES, ROAMING_ALL, DEFAULT_NETWORK_ALL,
140                             TelephonyManager.NETWORK_TYPE_UMTS, OEM_MANAGED_ALL)
141                     assertEquals(expectedTemplate, it)
142                 }
143 
144         // Verify template which matches all wifi networks,
145         // regardless of Wifi Network Key. See buildTemplateWifiWildcard and buildTemplateWifi.
146         NetworkTemplate.Builder(MATCH_WIFI).build().let {
147             val expectedTemplate = NetworkTemplate(MATCH_WIFI,
148                     emptyArray<String>() /*subscriberIds*/, emptyArray<String>(), METERED_ALL,
149                     ROAMING_ALL, DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL, OEM_MANAGED_ALL)
150             assertEquals(expectedTemplate, it)
151         }
152 
153         // Verify template which matches wifi networks with the given Wifi Network Key.
154         // See buildTemplateWifi(wifiNetworkKey).
155         NetworkTemplate.Builder(MATCH_WIFI).setWifiNetworkKeys(setOf(TEST_WIFI_KEY1)).build().let {
156             val expectedTemplate =
157                     NetworkTemplate(MATCH_WIFI, emptyArray<String>() /*subscriberIds*/,
158                     arrayOf(TEST_WIFI_KEY1), METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL,
159                     NETWORK_TYPE_ALL, OEM_MANAGED_ALL)
160             assertEquals(expectedTemplate, it)
161         }
162 
163         // Verify template which matches all wifi networks with the
164         // given Wifi Network Key, and IMSI. See buildTemplateWifi(wifiNetworkKey, subscriberId).
165         NetworkTemplate.Builder(MATCH_WIFI).setSubscriberIds(setOf(TEST_IMSI1))
166                 .setWifiNetworkKeys(setOf(TEST_WIFI_KEY1)).build().let {
167                     val expectedTemplate = NetworkTemplate(MATCH_WIFI, arrayOf(TEST_IMSI1),
168                             arrayOf(TEST_WIFI_KEY1), METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL,
169                             NETWORK_TYPE_ALL, OEM_MANAGED_ALL)
170                     assertEquals(expectedTemplate, it)
171                 }
172 
173         // Verify template which matches ethernet and bluetooth networks.
174         // See buildTemplateEthernet and buildTemplateBluetooth.
175         listOf(MATCH_ETHERNET, MATCH_BLUETOOTH).forEach { matchRule ->
176             NetworkTemplate.Builder(matchRule).build().let {
177                 val expectedTemplate = NetworkTemplate(matchRule,
178                         emptyArray<String>() /*subscriberIds*/, emptyArray<String>(),
179                         METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL,
180                         OEM_MANAGED_ALL)
181                 assertEquals(expectedTemplate, it)
182             }
183         }
184     }
185 
186     @Test
187     fun testBuilderWifiNetworkKeys() {
188         // Verify template builder which generates same template with the given different
189         // sequence keys.
190         NetworkTemplate.Builder(MATCH_WIFI).setWifiNetworkKeys(
191                 setOf(TEST_WIFI_KEY1, TEST_WIFI_KEY2)).build().let {
192             val expectedTemplate = NetworkTemplate.Builder(MATCH_WIFI).setWifiNetworkKeys(
193                     setOf(TEST_WIFI_KEY2, TEST_WIFI_KEY1)).build()
194             assertEquals(expectedTemplate, it)
195         }
196 
197         // Verify template which matches non-wifi networks with the given key is invalid.
198         listOf(MATCH_MOBILE, MATCH_CARRIER, MATCH_ETHERNET, MATCH_BLUETOOTH, -1,
199                 Integer.MAX_VALUE).forEach { matchRule ->
200             assertFailsWith<IllegalArgumentException> {
201                 NetworkTemplate.Builder(matchRule).setWifiNetworkKeys(setOf(TEST_WIFI_KEY1)).build()
202             }
203         }
204 
205         // Verify template which matches wifi networks with the given null key is invalid.
206         assertFailsWith<IllegalArgumentException> {
207             NetworkTemplate.Builder(MATCH_WIFI).setWifiNetworkKeys(setOf(null)).build()
208         }
209 
210         // Verify template which matches wifi wildcard with the given empty key set.
211         NetworkTemplate.Builder(MATCH_WIFI).setWifiNetworkKeys(setOf<String>()).build().let {
212             val expectedTemplate = NetworkTemplate(MATCH_WIFI,
213                     emptyArray<String>() /*subscriberIds*/, emptyArray<String>(),
214                     METERED_ALL, ROAMING_ALL, DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL,
215                     OEM_MANAGED_ALL)
216             assertEquals(expectedTemplate, it)
217         }
218     }
219 }
220