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