summaryrefslogtreecommitdiff
path: root/tests/unit/java/android/net/NetworkIdentityTest.kt
blob: bf5568d4e17fede92d504d715b0440ba3bd0f10d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.net

import android.content.Context
import android.net.ConnectivityManager.MAX_NETWORK_TYPE
import android.net.ConnectivityManager.TYPE_ETHERNET
import android.net.ConnectivityManager.TYPE_MOBILE
import android.net.ConnectivityManager.TYPE_NONE
import android.net.ConnectivityManager.TYPE_WIFI
import android.net.NetworkCapabilities.TRANSPORT_CELLULAR
import android.net.NetworkIdentity.OEM_NONE
import android.net.NetworkIdentity.OEM_PAID
import android.net.NetworkIdentity.OEM_PRIVATE
import android.net.NetworkIdentity.getOemBitfield
import android.app.usage.NetworkStatsManager
import android.telephony.TelephonyManager
import android.os.Build
import com.android.testutils.DevSdkIgnoreRule
import com.android.testutils.DevSdkIgnoreRunner
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.mock
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertFalse
import kotlin.test.assertTrue

private const val TEST_WIFI_KEY = "testwifikey"
private const val TEST_IMSI1 = "testimsi1"
private const val TEST_IMSI2 = "testimsi2"
private const val TEST_SUBID1 = 1
private const val TEST_SUBID2 = 2

@RunWith(DevSdkIgnoreRunner::class)
@DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.R)
class NetworkIdentityTest {
    private val mockContext = mock(Context::class.java)

    private fun buildMobileNetworkStateSnapshot(
        caps: NetworkCapabilities,
        subscriberId: String
    ): NetworkStateSnapshot {
        return NetworkStateSnapshot(mock(Network::class.java), caps,
                LinkProperties(), subscriberId, TYPE_MOBILE)
    }

    @Test
    fun testGetOemBitfield() {
        val oemNone = NetworkCapabilities().apply {
            setCapability(NetworkCapabilities.NET_CAPABILITY_OEM_PAID, false)
            setCapability(NetworkCapabilities.NET_CAPABILITY_OEM_PRIVATE, false)
        }
        val oemPaid = NetworkCapabilities().apply {
            setCapability(NetworkCapabilities.NET_CAPABILITY_OEM_PAID, true)
            setCapability(NetworkCapabilities.NET_CAPABILITY_OEM_PRIVATE, false)
        }
        val oemPrivate = NetworkCapabilities().apply {
            setCapability(NetworkCapabilities.NET_CAPABILITY_OEM_PAID, false)
            setCapability(NetworkCapabilities.NET_CAPABILITY_OEM_PRIVATE, true)
        }
        val oemAll = NetworkCapabilities().apply {
            setCapability(NetworkCapabilities.NET_CAPABILITY_OEM_PAID, true)
            setCapability(NetworkCapabilities.NET_CAPABILITY_OEM_PRIVATE, true)
        }

        assertEquals(getOemBitfield(oemNone), OEM_NONE)
        assertEquals(getOemBitfield(oemPaid), OEM_PAID)
        assertEquals(getOemBitfield(oemPrivate), OEM_PRIVATE)
        assertEquals(getOemBitfield(oemAll), OEM_PAID or OEM_PRIVATE)
    }

    @Test
    fun testIsMetered() {
        // Verify network is metered.
        val netIdent1 = NetworkIdentity.buildNetworkIdentity(mockContext,
                buildMobileNetworkStateSnapshot(NetworkCapabilities(), TEST_IMSI1),
                false /* defaultNetwork */, TelephonyManager.NETWORK_TYPE_UMTS)
        assertTrue(netIdent1.isMetered())

        // Verify network is not metered because it has NET_CAPABILITY_NOT_METERED capability.
        val capsNotMetered = NetworkCapabilities.Builder().apply {
            addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED)
        }.build()
        val netIdent2 = NetworkIdentity.buildNetworkIdentity(mockContext,
                buildMobileNetworkStateSnapshot(capsNotMetered, TEST_IMSI1),
                false /* defaultNetwork */, TelephonyManager.NETWORK_TYPE_UMTS)
        assertFalse(netIdent2.isMetered())

        // Verify network is not metered because it has NET_CAPABILITY_TEMPORARILY_NOT_METERED
        // capability .
        val capsTempNotMetered = NetworkCapabilities().apply {
            setCapability(NetworkCapabilities.NET_CAPABILITY_TEMPORARILY_NOT_METERED, true)
        }
        val netIdent3 = NetworkIdentity.buildNetworkIdentity(mockContext,
                buildMobileNetworkStateSnapshot(capsTempNotMetered, TEST_IMSI1),
                false /* defaultNetwork */, TelephonyManager.NETWORK_TYPE_UMTS)
        assertFalse(netIdent3.isMetered())
    }

    @Test
    fun testBuilder() {
        val specifier1 = TelephonyNetworkSpecifier(TEST_SUBID1)
        val oemPrivateRoamingNotMeteredCap = NetworkCapabilities().apply {
            addCapability(NetworkCapabilities.NET_CAPABILITY_OEM_PRIVATE)
            addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED)
            addTransportType(TRANSPORT_CELLULAR)
            setNetworkSpecifier(specifier1)
        }
        val identFromSnapshot = NetworkIdentity.Builder().setNetworkStateSnapshot(
                buildMobileNetworkStateSnapshot(oemPrivateRoamingNotMeteredCap, TEST_IMSI1))
                .setDefaultNetwork(true)
                .setRatType(TelephonyManager.NETWORK_TYPE_UMTS)
                .setSubId(TEST_SUBID1)
                .build()
        val identFromLegacyBuild = NetworkIdentity.buildNetworkIdentity(mockContext,
                buildMobileNetworkStateSnapshot(oemPrivateRoamingNotMeteredCap, TEST_IMSI1),
                true /* defaultNetwork */, TelephonyManager.NETWORK_TYPE_UMTS)
        val identFromConstructor = NetworkIdentity(TYPE_MOBILE,
                TelephonyManager.NETWORK_TYPE_UMTS,
                TEST_IMSI1,
                null /* wifiNetworkKey */,
                true /* roaming */,
                false /* metered */,
                true /* defaultNetwork */,
                NetworkTemplate.OEM_MANAGED_PRIVATE,
                TEST_SUBID1)
        assertEquals(identFromLegacyBuild, identFromSnapshot)
        assertEquals(identFromConstructor, identFromSnapshot)

        // Assert non-wifi can't have wifi network key.
        assertFailsWith<IllegalArgumentException> {
            NetworkIdentity.Builder()
                    .setType(TYPE_ETHERNET)
                    .setWifiNetworkKey(TEST_WIFI_KEY)
                    .build()
        }

        // Assert non-mobile can't have ratType.
        assertFailsWith<IllegalArgumentException> {
            NetworkIdentity.Builder()
                    .setType(TYPE_WIFI)
                    .setRatType(TelephonyManager.NETWORK_TYPE_LTE)
                    .build()
        }
    }

    @Test
    fun testBuilder_type() {
        // Assert illegal type values cannot make an identity.
        listOf(Integer.MIN_VALUE, TYPE_NONE - 1, MAX_NETWORK_TYPE + 1, Integer.MAX_VALUE)
                .forEach { type ->
                    assertFailsWith<IllegalArgumentException> {
                        NetworkIdentity.Builder().setType(type).build()
                    }
                }

        // Verify legitimate type values can make an identity.
        for (type in TYPE_NONE..MAX_NETWORK_TYPE) {
            NetworkIdentity.Builder().setType(type).build().also {
                assertEquals(it.type, type)
            }
        }
    }

    @Test
    fun testBuilder_ratType() {
        // Assert illegal ratTypes cannot make an identity.
        listOf(Integer.MIN_VALUE, NetworkTemplate.NETWORK_TYPE_ALL,
                NetworkStatsManager.NETWORK_TYPE_5G_NSA - 1, Integer.MAX_VALUE)
                .forEach {
                    assertFailsWith<IllegalArgumentException> {
                        NetworkIdentity.Builder()
                                .setType(TYPE_MOBILE)
                                .setRatType(it)
                                .build()
                    }
                }

        // Verify legitimate ratTypes can make an identity.
        TelephonyManager.getAllNetworkTypes().toMutableList().also {
            it.add(TelephonyManager.NETWORK_TYPE_UNKNOWN)
            it.add(NetworkStatsManager.NETWORK_TYPE_5G_NSA)
        }.forEach { rat ->
            NetworkIdentity.Builder()
                    .setType(TYPE_MOBILE)
                    .setRatType(rat)
                    .build().also {
                        assertEquals(it.ratType, rat)
                    }
        }
    }

    @Test
    fun testBuilder_oemManaged() {
        // Assert illegal oemManage values cannot make an identity.
        listOf(Integer.MIN_VALUE, NetworkTemplate.OEM_MANAGED_ALL, NetworkTemplate.OEM_MANAGED_YES,
                Integer.MAX_VALUE)
                .forEach { oemManaged ->
                    assertFailsWith<IllegalArgumentException> {
                        NetworkIdentity.Builder()
                                .setType(TYPE_MOBILE)
                                .setOemManaged(oemManaged)
                                .build()
                    }
                }

        // Verify legitimate oem managed values can make an identity.
        listOf(NetworkTemplate.OEM_MANAGED_NO, NetworkTemplate.OEM_MANAGED_PAID,
                NetworkTemplate.OEM_MANAGED_PRIVATE, NetworkTemplate.OEM_MANAGED_PAID or
                NetworkTemplate.OEM_MANAGED_PRIVATE)
                .forEach { oemManaged ->
                    NetworkIdentity.Builder()
                            .setOemManaged(oemManaged)
                            .build().also {
                                assertEquals(it.oemManaged, oemManaged)
                            }
                }
    }

    @Test
    fun testGetSubId() {
        val specifier1 = TelephonyNetworkSpecifier(TEST_SUBID1)
        val specifier2 = TelephonyNetworkSpecifier(TEST_SUBID2)
        val capSUBID1 = NetworkCapabilities().apply {
            addTransportType(TRANSPORT_CELLULAR)
            setNetworkSpecifier(specifier1)
        }
        val capSUBID2 = NetworkCapabilities().apply {
            addTransportType(TRANSPORT_CELLULAR)
            setNetworkSpecifier(specifier2)
        }

        val netIdent1 = NetworkIdentity.buildNetworkIdentity(mockContext,
                buildMobileNetworkStateSnapshot(capSUBID1, TEST_IMSI1),
                false /* defaultNetwork */, TelephonyManager.NETWORK_TYPE_UMTS)
        assertEquals(TEST_SUBID1, netIdent1.getSubId())

        val netIdent2 = NetworkIdentity.buildNetworkIdentity(mockContext,
                buildMobileNetworkStateSnapshot(capSUBID2, TEST_IMSI2),
                false /* defaultNetwork */, TelephonyManager.NETWORK_TYPE_UMTS)
        assertEquals(TEST_SUBID2, netIdent2.getSubId())
    }
}