• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.os.Build
19 import androidx.test.filters.SmallTest
20 import com.android.testutils.ConnectivityModuleTest
21 import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo
22 import com.android.testutils.DevSdkIgnoreRunner
23 import org.junit.Test
24 import org.junit.runner.RunWith
25 import kotlin.test.assertEquals
26 import kotlin.test.assertFalse
27 import kotlin.test.assertNotEquals
28 import kotlin.test.assertTrue
29 
30 @SmallTest
31 @RunWith(DevSdkIgnoreRunner::class)
32 @IgnoreUpTo(Build.VERSION_CODES.Q)
33 @ConnectivityModuleTest
34 class NetworkSpecifierTest {
35     private class TestNetworkSpecifier(
36         val intData: Int = 123,
37         val stringData: String = "init"
38     ) : NetworkSpecifier() {
canBeSatisfiedBynull39         override fun canBeSatisfiedBy(other: NetworkSpecifier?): Boolean =
40                 other != null &&
41                 other is TestNetworkSpecifier &&
42                 other.intData >= intData &&
43                 stringData.equals(other.stringData)
44 
45         override fun redact(): NetworkSpecifier = TestNetworkSpecifier(intData, "redact")
46     }
47 
48     @Test
49     fun testRedact() {
50         val ns: TestNetworkSpecifier = TestNetworkSpecifier()
51         val redactNs = ns.redact()
52         assertTrue(redactNs is TestNetworkSpecifier)
53         assertEquals(ns.intData, redactNs.intData)
54         assertNotEquals(ns.stringData, redactNs.stringData)
55         assertTrue("redact".equals(redactNs.stringData))
56     }
57 
58     @Test
testcanBeSatisfiedBynull59     fun testcanBeSatisfiedBy() {
60         val target: TestNetworkSpecifier = TestNetworkSpecifier()
61         assertFalse(target.canBeSatisfiedBy(null))
62         assertTrue(target.canBeSatisfiedBy(TestNetworkSpecifier()))
63         val otherNs = TelephonyNetworkSpecifier.Builder().setSubscriptionId(123).build()
64         assertFalse(target.canBeSatisfiedBy(otherNs))
65         assertTrue(target.canBeSatisfiedBy(TestNetworkSpecifier(intData = 999)))
66         assertFalse(target.canBeSatisfiedBy(TestNetworkSpecifier(intData = 1)))
67         assertFalse(target.canBeSatisfiedBy(TestNetworkSpecifier(stringData = "diff")))
68     }
69 }