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.assertParcelingIsLossless
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)
40     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         assertParcelingIsLossless(config)
52     }
53 
54     @Test @IgnoreUpTo(Build.VERSION_CODES.Q)
55     fun testBuilder() {
56         val testExtraInfo = "mylegacyExtraInfo"
57         val config = NetworkAgentConfig.Builder().apply {
58             setExplicitlySelected(true)
59             setLegacyType(ConnectivityManager.TYPE_ETHERNET)
60             setSubscriberId("MySubId")
61             setPartialConnectivityAcceptable(false)
62             setUnvalidatedConnectivityAcceptable(true)
63             setLegacyTypeName("TEST_NETWORK")
64             if (isAtLeastS()) {
65                 setLegacyExtraInfo(testExtraInfo)
66                 setNat64DetectionEnabled(false)
67                 setProvisioningNotificationEnabled(false)
68                 setBypassableVpn(true)
69             }
70         }.build()
71 
72         assertTrue(config.isExplicitlySelected())
73         assertEquals(ConnectivityManager.TYPE_ETHERNET, config.getLegacyType())
74         assertEquals("MySubId", config.getSubscriberId())
75         assertFalse(config.isPartialConnectivityAcceptable())
76         assertTrue(config.isUnvalidatedConnectivityAcceptable())
77         assertEquals("TEST_NETWORK", config.getLegacyTypeName())
78         if (isAtLeastS()) {
79             assertEquals(testExtraInfo, config.getLegacyExtraInfo())
80             assertFalse(config.isNat64DetectionEnabled())
81             assertFalse(config.isProvisioningNotificationEnabled())
82             assertTrue(config.isBypassableVpn())
83         } else {
84             assertTrue(config.isNat64DetectionEnabled())
85             assertTrue(config.isProvisioningNotificationEnabled())
86         }
87     }
88 }
89