1 /*
2  * 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
18 
19 import android.content.Context
20 import android.net.ConnectivityManager.TYPE_MOBILE
21 import android.os.Build
22 import android.telephony.TelephonyManager
23 import com.android.testutils.DevSdkIgnoreRule
24 import com.android.testutils.DevSdkIgnoreRunner
25 import org.junit.Test
26 import org.junit.runner.RunWith
27 import org.mockito.Mockito.mock
28 import kotlin.test.assertEquals
29 
30 private const val TEST_IMSI1 = "testimsi1"
31 
32 @DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.S_V2)
33 @RunWith(DevSdkIgnoreRunner::class)
34 class NetworkIdentitySetTest {
35     private val mockContext = mock(Context::class.java)
36 
buildMobileNetworkStateSnapshotnull37     private fun buildMobileNetworkStateSnapshot(
38         caps: NetworkCapabilities,
39         subscriberId: String
40     ): NetworkStateSnapshot {
41         return NetworkStateSnapshot(mock(Network::class.java), caps,
42                 LinkProperties(), subscriberId, TYPE_MOBILE)
43     }
44 
45     @Test
testComparenull46     fun testCompare() {
47         val ident1 = NetworkIdentity.buildNetworkIdentity(mockContext,
48             buildMobileNetworkStateSnapshot(NetworkCapabilities(), TEST_IMSI1),
49             false /* defaultNetwork */, TelephonyManager.NETWORK_TYPE_UMTS)
50         val ident2 = NetworkIdentity.buildNetworkIdentity(mockContext,
51             buildMobileNetworkStateSnapshot(NetworkCapabilities(), TEST_IMSI1),
52             true /* defaultNetwork */, TelephonyManager.NETWORK_TYPE_UMTS)
53 
54         // Verify that the results of comparing two empty sets are equal
55         assertEquals(0, NetworkIdentitySet.compare(NetworkIdentitySet(), NetworkIdentitySet()))
56 
57         val identSet1 = NetworkIdentitySet()
58         val identSet2 = NetworkIdentitySet()
59         identSet1.add(ident1)
60         identSet2.add(ident2)
61         assertEquals(-1, NetworkIdentitySet.compare(NetworkIdentitySet(), identSet1))
62         assertEquals(1, NetworkIdentitySet.compare(identSet1, NetworkIdentitySet()))
63         assertEquals(0, NetworkIdentitySet.compare(identSet1, identSet1))
64         assertEquals(-1, NetworkIdentitySet.compare(identSet1, identSet2))
65     }
66 }
67