1 /*
2  * Copyright (C) 2020 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 package android.net
17 
18 import androidx.test.filters.SmallTest
19 import com.android.testutils.ConnectivityModuleTest
20 import com.android.testutils.DevSdkIgnoreRunner
21 import kotlin.test.assertEquals
22 import kotlin.test.assertFalse
23 import kotlin.test.assertNotEquals
24 import kotlin.test.assertTrue
25 import org.junit.Test
26 import org.junit.runner.RunWith
27 
28 @SmallTest
29 @RunWith(DevSdkIgnoreRunner::class)
30 @ConnectivityModuleTest
31 class NetworkSpecifierTest {
32     private class TestNetworkSpecifier(
33         val intData: Int = 123,
34         val stringData: String = "init"
35     ) : NetworkSpecifier() {
canBeSatisfiedBynull36         override fun canBeSatisfiedBy(other: NetworkSpecifier?): Boolean =
37                 other != null &&
38                 other is TestNetworkSpecifier &&
39                 other.intData >= intData &&
40                 stringData.equals(other.stringData)
41 
42         override fun redact(): NetworkSpecifier = TestNetworkSpecifier(intData, "redact")
43     }
44 
45     @Test
46     fun testRedact() {
47         val ns: TestNetworkSpecifier = TestNetworkSpecifier()
48         val redactNs = ns.redact()
49         assertTrue(redactNs is TestNetworkSpecifier)
50         assertEquals(ns.intData, redactNs.intData)
51         assertNotEquals(ns.stringData, redactNs.stringData)
52         assertTrue("redact".equals(redactNs.stringData))
53     }
54 
55     @Test
testcanBeSatisfiedBynull56     fun testcanBeSatisfiedBy() {
57         val target: TestNetworkSpecifier = TestNetworkSpecifier()
58         assertFalse(target.canBeSatisfiedBy(null))
59         assertTrue(target.canBeSatisfiedBy(TestNetworkSpecifier()))
60         val otherNs = TelephonyNetworkSpecifier.Builder().setSubscriptionId(123).build()
61         assertFalse(target.canBeSatisfiedBy(otherNs))
62         assertTrue(target.canBeSatisfiedBy(TestNetworkSpecifier(intData = 999)))
63         assertFalse(target.canBeSatisfiedBy(TestNetworkSpecifier(intData = 1)))
64         assertFalse(target.canBeSatisfiedBy(TestNetworkSpecifier(stringData = "diff")))
65     }
66 }