1 /* 2 * Copyright (C) 2019 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.os.Build 20 import androidx.test.filters.SmallTest 21 import androidx.test.runner.AndroidJUnit4 22 import com.android.modules.utils.build.SdkLevel.isAtLeastS 23 import com.android.testutils.DevSdkIgnoreRule 24 import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo 25 import com.android.testutils.assertParcelSane 26 import org.junit.Assert.assertEquals 27 import org.junit.Assert.assertFalse 28 import org.junit.Assert.assertTrue 29 import org.junit.Rule 30 import org.junit.Test 31 import org.junit.runner.RunWith 32 33 @RunWith(AndroidJUnit4::class) 34 @SmallTest 35 class NetworkAgentConfigTest { 36 @Rule @JvmField 37 val ignoreRule = DevSdkIgnoreRule() 38 39 @Test @IgnoreUpTo(Build.VERSION_CODES.Q) testParcelNetworkAgentConfignull40 fun testParcelNetworkAgentConfig() { 41 val config = NetworkAgentConfig.Builder().apply { 42 setExplicitlySelected(true) 43 setLegacyType(ConnectivityManager.TYPE_ETHERNET) 44 setSubscriberId("MySubId") 45 setPartialConnectivityAcceptable(false) 46 setUnvalidatedConnectivityAcceptable(true) 47 if (isAtLeastS()) { 48 setBypassableVpn(true) 49 } 50 }.build() 51 if (isAtLeastS()) { 52 // From S, the config will have 12 items 53 assertParcelSane(config, 12) 54 } else { 55 // For R or below, the config will have 10 items 56 assertParcelSane(config, 10) 57 } 58 } 59 60 @Test @IgnoreUpTo(Build.VERSION_CODES.Q) testBuildernull61 fun testBuilder() { 62 val testExtraInfo = "mylegacyExtraInfo" 63 val config = NetworkAgentConfig.Builder().apply { 64 setExplicitlySelected(true) 65 setLegacyType(ConnectivityManager.TYPE_ETHERNET) 66 setSubscriberId("MySubId") 67 setPartialConnectivityAcceptable(false) 68 setUnvalidatedConnectivityAcceptable(true) 69 setLegacyTypeName("TEST_NETWORK") 70 if (isAtLeastS()) { 71 setLegacyExtraInfo(testExtraInfo) 72 setNat64DetectionEnabled(false) 73 setProvisioningNotificationEnabled(false) 74 setBypassableVpn(true) 75 } 76 }.build() 77 78 assertTrue(config.isExplicitlySelected()) 79 assertEquals(ConnectivityManager.TYPE_ETHERNET, config.getLegacyType()) 80 assertEquals("MySubId", config.getSubscriberId()) 81 assertFalse(config.isPartialConnectivityAcceptable()) 82 assertTrue(config.isUnvalidatedConnectivityAcceptable()) 83 assertEquals("TEST_NETWORK", config.getLegacyTypeName()) 84 if (isAtLeastS()) { 85 assertEquals(testExtraInfo, config.getLegacyExtraInfo()) 86 assertFalse(config.isNat64DetectionEnabled()) 87 assertFalse(config.isProvisioningNotificationEnabled()) 88 assertTrue(config.isBypassableVpn()) 89 } else { 90 assertTrue(config.isNat64DetectionEnabled()) 91 assertTrue(config.isProvisioningNotificationEnabled()) 92 } 93 } 94 } 95