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