1 /*
<lambda>null2  * Copyright (C) 2021 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 android.content.Context
20 import android.net.ConnectivityManager.MAX_NETWORK_TYPE
21 import android.net.ConnectivityManager.TYPE_ETHERNET
22 import android.net.ConnectivityManager.TYPE_MOBILE
23 import android.net.ConnectivityManager.TYPE_NONE
24 import android.net.ConnectivityManager.TYPE_WIFI
25 import android.net.NetworkCapabilities.TRANSPORT_CELLULAR
26 import android.net.NetworkIdentity.OEM_NONE
27 import android.net.NetworkIdentity.OEM_PAID
28 import android.net.NetworkIdentity.OEM_PRIVATE
29 import android.net.NetworkIdentity.getOemBitfield
30 import android.app.usage.NetworkStatsManager
31 import android.telephony.TelephonyManager
32 import android.os.Build
33 import com.android.testutils.DevSdkIgnoreRule
34 import com.android.testutils.DevSdkIgnoreRunner
35 import org.junit.Test
36 import org.junit.runner.RunWith
37 import org.mockito.Mockito.mock
38 import kotlin.test.assertEquals
39 import kotlin.test.assertFailsWith
40 import kotlin.test.assertFalse
41 import kotlin.test.assertTrue
42 
43 private const val TEST_WIFI_KEY = "testwifikey"
44 private const val TEST_IMSI1 = "testimsi1"
45 private const val TEST_IMSI2 = "testimsi2"
46 private const val TEST_SUBID1 = 1
47 private const val TEST_SUBID2 = 2
48 
49 @RunWith(DevSdkIgnoreRunner::class)
50 @DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.S_V2)
51 class NetworkIdentityTest {
52     private val mockContext = mock(Context::class.java)
53 
54     private fun buildMobileNetworkStateSnapshot(
55         caps: NetworkCapabilities,
56         subscriberId: String
57     ): NetworkStateSnapshot {
58         return NetworkStateSnapshot(mock(Network::class.java), caps,
59                 LinkProperties(), subscriberId, TYPE_MOBILE)
60     }
61 
62     @Test
63     fun testGetOemBitfield() {
64         val oemNone = NetworkCapabilities().apply {
65             setCapability(NetworkCapabilities.NET_CAPABILITY_OEM_PAID, false)
66             setCapability(NetworkCapabilities.NET_CAPABILITY_OEM_PRIVATE, false)
67         }
68         val oemPaid = NetworkCapabilities().apply {
69             setCapability(NetworkCapabilities.NET_CAPABILITY_OEM_PAID, true)
70             setCapability(NetworkCapabilities.NET_CAPABILITY_OEM_PRIVATE, false)
71         }
72         val oemPrivate = NetworkCapabilities().apply {
73             setCapability(NetworkCapabilities.NET_CAPABILITY_OEM_PAID, false)
74             setCapability(NetworkCapabilities.NET_CAPABILITY_OEM_PRIVATE, true)
75         }
76         val oemAll = NetworkCapabilities().apply {
77             setCapability(NetworkCapabilities.NET_CAPABILITY_OEM_PAID, true)
78             setCapability(NetworkCapabilities.NET_CAPABILITY_OEM_PRIVATE, true)
79         }
80 
81         assertEquals(getOemBitfield(oemNone), OEM_NONE)
82         assertEquals(getOemBitfield(oemPaid), OEM_PAID)
83         assertEquals(getOemBitfield(oemPrivate), OEM_PRIVATE)
84         assertEquals(getOemBitfield(oemAll), OEM_PAID or OEM_PRIVATE)
85     }
86 
87     @Test
88     fun testIsMetered() {
89         // Verify network is metered.
90         val netIdent1 = NetworkIdentity.buildNetworkIdentity(mockContext,
91                 buildMobileNetworkStateSnapshot(NetworkCapabilities(), TEST_IMSI1),
92                 false /* defaultNetwork */, TelephonyManager.NETWORK_TYPE_UMTS)
93         assertTrue(netIdent1.isMetered())
94 
95         // Verify network is not metered because it has NET_CAPABILITY_NOT_METERED capability.
96         val capsNotMetered = NetworkCapabilities.Builder().apply {
97             addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED)
98         }.build()
99         val netIdent2 = NetworkIdentity.buildNetworkIdentity(mockContext,
100                 buildMobileNetworkStateSnapshot(capsNotMetered, TEST_IMSI1),
101                 false /* defaultNetwork */, TelephonyManager.NETWORK_TYPE_UMTS)
102         assertFalse(netIdent2.isMetered())
103 
104         // In current design, a network that has NET_CAPABILITY_TEMPORARILY_NOT_METERED
105         // capability will be treated as metered.
106         val capsTempNotMetered = NetworkCapabilities().apply {
107             setCapability(NetworkCapabilities.NET_CAPABILITY_TEMPORARILY_NOT_METERED, true)
108         }
109         val netIdent3 = NetworkIdentity.buildNetworkIdentity(mockContext,
110                 buildMobileNetworkStateSnapshot(capsTempNotMetered, TEST_IMSI1),
111                 false /* defaultNetwork */, TelephonyManager.NETWORK_TYPE_UMTS)
112         assertTrue(netIdent3.isMetered())
113     }
114 
115     @Test
116     fun testBuilder() {
117         val specifier1 = TelephonyNetworkSpecifier(TEST_SUBID1)
118         val oemPrivateRoamingNotMeteredCap = NetworkCapabilities().apply {
119             addCapability(NetworkCapabilities.NET_CAPABILITY_OEM_PRIVATE)
120             addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED)
121             addTransportType(TRANSPORT_CELLULAR)
122             setNetworkSpecifier(specifier1)
123         }
124         val identFromSnapshot = NetworkIdentity.Builder().setNetworkStateSnapshot(
125                 buildMobileNetworkStateSnapshot(oemPrivateRoamingNotMeteredCap, TEST_IMSI1))
126                 .setDefaultNetwork(true)
127                 .setRatType(TelephonyManager.NETWORK_TYPE_UMTS)
128                 .setSubId(TEST_SUBID1)
129                 .build()
130         val identFromLegacyBuild = NetworkIdentity.buildNetworkIdentity(mockContext,
131                 buildMobileNetworkStateSnapshot(oemPrivateRoamingNotMeteredCap, TEST_IMSI1),
132                 true /* defaultNetwork */, TelephonyManager.NETWORK_TYPE_UMTS)
133         val identFromConstructor = NetworkIdentity(TYPE_MOBILE,
134                 TelephonyManager.NETWORK_TYPE_UMTS,
135                 TEST_IMSI1,
136                 null /* wifiNetworkKey */,
137                 true /* roaming */,
138                 false /* metered */,
139                 true /* defaultNetwork */,
140                 NetworkTemplate.OEM_MANAGED_PRIVATE,
141                 TEST_SUBID1)
142         assertEquals(identFromLegacyBuild, identFromSnapshot)
143         assertEquals(identFromConstructor, identFromSnapshot)
144 
145         // Assert non-wifi can't have wifi network key.
146         assertFailsWith<IllegalArgumentException> {
147             NetworkIdentity.Builder()
148                     .setType(TYPE_ETHERNET)
149                     .setWifiNetworkKey(TEST_WIFI_KEY)
150                     .build()
151         }
152 
153         // Assert non-mobile can't have ratType.
154         assertFailsWith<IllegalArgumentException> {
155             NetworkIdentity.Builder()
156                     .setType(TYPE_WIFI)
157                     .setRatType(TelephonyManager.NETWORK_TYPE_LTE)
158                     .build()
159         }
160     }
161 
162     @Test
163     fun testBuilder_type() {
164         // Assert illegal type values cannot make an identity.
165         listOf(Integer.MIN_VALUE, TYPE_NONE - 1, MAX_NETWORK_TYPE + 1, Integer.MAX_VALUE)
166                 .forEach { type ->
167                     assertFailsWith<IllegalArgumentException> {
168                         NetworkIdentity.Builder().setType(type).build()
169                     }
170                 }
171 
172         // Verify legitimate type values can make an identity.
173         for (type in TYPE_NONE..MAX_NETWORK_TYPE) {
174             NetworkIdentity.Builder().setType(type).build().also {
175                 assertEquals(it.type, type)
176             }
177         }
178     }
179 
180     @Test
181     fun testBuilder_ratType() {
182         // Assert illegal ratTypes cannot make an identity.
183         listOf(Integer.MIN_VALUE, NetworkTemplate.NETWORK_TYPE_ALL,
184                 NetworkStatsManager.NETWORK_TYPE_5G_NSA - 1, Integer.MAX_VALUE)
185                 .forEach {
186                     assertFailsWith<IllegalArgumentException> {
187                         NetworkIdentity.Builder()
188                                 .setType(TYPE_MOBILE)
189                                 .setRatType(it)
190                                 .build()
191                     }
192                 }
193 
194         // Verify legitimate ratTypes can make an identity.
195         TelephonyManager.getAllNetworkTypes().toMutableList().also {
196             it.add(TelephonyManager.NETWORK_TYPE_UNKNOWN)
197             it.add(NetworkStatsManager.NETWORK_TYPE_5G_NSA)
198         }.forEach { rat ->
199             NetworkIdentity.Builder()
200                     .setType(TYPE_MOBILE)
201                     .setRatType(rat)
202                     .build().also {
203                         assertEquals(it.ratType, rat)
204                     }
205         }
206     }
207 
208     @Test
209     fun testBuilder_oemManaged() {
210         // Assert illegal oemManage values cannot make an identity.
211         listOf(Integer.MIN_VALUE, NetworkTemplate.OEM_MANAGED_ALL, NetworkTemplate.OEM_MANAGED_YES,
212                 Integer.MAX_VALUE)
213                 .forEach { oemManaged ->
214                     assertFailsWith<IllegalArgumentException> {
215                         NetworkIdentity.Builder()
216                                 .setType(TYPE_MOBILE)
217                                 .setOemManaged(oemManaged)
218                                 .build()
219                     }
220                 }
221 
222         // Verify legitimate oem managed values can make an identity.
223         listOf(NetworkTemplate.OEM_MANAGED_NO, NetworkTemplate.OEM_MANAGED_PAID,
224                 NetworkTemplate.OEM_MANAGED_PRIVATE, NetworkTemplate.OEM_MANAGED_PAID or
225                 NetworkTemplate.OEM_MANAGED_PRIVATE)
226                 .forEach { oemManaged ->
227                     NetworkIdentity.Builder()
228                             .setOemManaged(oemManaged)
229                             .build().also {
230                                 assertEquals(it.oemManaged, oemManaged)
231                             }
232                 }
233     }
234 
235     @Test
236     fun testGetSubId() {
237         val specifier1 = TelephonyNetworkSpecifier(TEST_SUBID1)
238         val specifier2 = TelephonyNetworkSpecifier(TEST_SUBID2)
239         val capSUBID1 = NetworkCapabilities().apply {
240             addTransportType(TRANSPORT_CELLULAR)
241             setNetworkSpecifier(specifier1)
242         }
243         val capSUBID2 = NetworkCapabilities().apply {
244             addTransportType(TRANSPORT_CELLULAR)
245             setNetworkSpecifier(specifier2)
246         }
247 
248         val netIdent1 = NetworkIdentity.buildNetworkIdentity(mockContext,
249                 buildMobileNetworkStateSnapshot(capSUBID1, TEST_IMSI1),
250                 false /* defaultNetwork */, TelephonyManager.NETWORK_TYPE_UMTS)
251         assertEquals(TEST_SUBID1, netIdent1.getSubId())
252 
253         val netIdent2 = NetworkIdentity.buildNetworkIdentity(mockContext,
254                 buildMobileNetworkStateSnapshot(capSUBID2, TEST_IMSI2),
255                 false /* defaultNetwork */, TelephonyManager.NETWORK_TYPE_UMTS)
256         assertEquals(TEST_SUBID2, netIdent2.getSubId())
257     }
258 }
259