1 /*
2 * Copyright (C) 2021 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.net.ConnectivityManager.TYPE_NONE
20 import android.net.ConnectivityManager.TYPE_WIFI
21 import android.net.InetAddresses.parseNumericAddress
22 import android.net.NetworkCapabilities.TRANSPORT_WIFI
23 import android.os.Build
24 import androidx.test.filters.SmallTest
25 import com.android.testutils.ConnectivityModuleTest
26 import com.android.testutils.DevSdkIgnoreRule
27 import com.android.testutils.DevSdkIgnoreRunner
28 import com.android.testutils.assertParcelingIsLossless
29 import org.junit.Test
30 import org.junit.runner.RunWith
31 import java.net.Inet4Address
32 import java.net.Inet6Address
33
34 private const val TEST_IMSI = "imsi1"
35 private const val TEST_SSID = "SSID1"
36 private const val TEST_NETID = 123
37
38 private val TEST_IPV4_GATEWAY = parseNumericAddress("192.168.222.3") as Inet4Address
39 private val TEST_IPV6_GATEWAY = parseNumericAddress("2001:db8::1") as Inet6Address
40 private val TEST_IPV4_LINKADDR = LinkAddress("192.168.222.123/24")
41 private val TEST_IPV6_LINKADDR = LinkAddress("2001:db8::123/64")
42 private val TEST_IFACE = "fake0"
<lambda>null43 private val TEST_LINK_PROPERTIES = LinkProperties().apply {
44 interfaceName = TEST_IFACE
45 addLinkAddress(TEST_IPV4_LINKADDR)
46 addLinkAddress(TEST_IPV6_LINKADDR)
47
48 // Add default routes
49 addRoute(RouteInfo(IpPrefix(parseNumericAddress("0.0.0.0"), 0), TEST_IPV4_GATEWAY))
50 addRoute(RouteInfo(IpPrefix(parseNumericAddress("::"), 0), TEST_IPV6_GATEWAY))
51 }
52
<lambda>null53 private val TEST_CAPABILITIES = NetworkCapabilities().apply {
54 addTransportType(TRANSPORT_WIFI)
55 setCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED, false)
56 setCapability(NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING, true)
57 setSSID(TEST_SSID)
58 }
59
60 @SmallTest
61 @RunWith(DevSdkIgnoreRunner::class)
62 @DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.R)
63 @ConnectivityModuleTest
64 class NetworkStateSnapshotTest {
65
66 @Test
testParcelUnparcelnull67 fun testParcelUnparcel() {
68 val emptySnapshot = NetworkStateSnapshot(Network(TEST_NETID), NetworkCapabilities(),
69 LinkProperties(), null, TYPE_NONE)
70 val snapshot = NetworkStateSnapshot(
71 Network(TEST_NETID), TEST_CAPABILITIES, TEST_LINK_PROPERTIES, TEST_IMSI, TYPE_WIFI)
72 assertParcelingIsLossless(emptySnapshot)
73 assertParcelingIsLossless(snapshot)
74 }
75 }
76