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 17 package com.android.net.module.util 18 19 import android.annotation.TargetApi 20 import android.net.NetworkCapabilities 21 import android.net.NetworkCapabilities.NET_CAPABILITY_BIP 22 import android.net.NetworkCapabilities.NET_CAPABILITY_CBS 23 import android.net.NetworkCapabilities.NET_CAPABILITY_EIMS 24 import android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET 25 import android.net.NetworkCapabilities.NET_CAPABILITY_OEM_PAID 26 import android.net.NetworkCapabilities.TRANSPORT_BLUETOOTH 27 import android.net.NetworkCapabilities.TRANSPORT_CELLULAR 28 import android.net.NetworkCapabilities.TRANSPORT_ETHERNET 29 import android.net.NetworkCapabilities.TRANSPORT_TEST 30 import android.net.NetworkCapabilities.TRANSPORT_VPN 31 import android.net.NetworkCapabilities.TRANSPORT_WIFI 32 import android.net.NetworkCapabilities.TRANSPORT_WIFI_AWARE 33 import android.os.Build 34 import androidx.test.filters.SmallTest 35 import androidx.test.runner.AndroidJUnit4 36 import com.android.modules.utils.build.SdkLevel 37 import com.android.net.module.util.NetworkCapabilitiesUtils.RESTRICTED_CAPABILITIES 38 import com.android.net.module.util.NetworkCapabilitiesUtils.UNRESTRICTED_CAPABILITIES 39 import com.android.net.module.util.NetworkCapabilitiesUtils.getDisplayTransport 40 import org.junit.Test 41 import org.junit.runner.RunWith 42 import java.lang.IllegalArgumentException 43 import kotlin.test.assertEquals 44 import kotlin.test.assertFailsWith 45 import kotlin.test.assertFalse 46 import kotlin.test.assertTrue 47 48 @RunWith(AndroidJUnit4::class) 49 @SmallTest 50 class NetworkCapabilitiesUtilsTest { 51 52 @Test testGetAccountingTransportnull53 fun testGetAccountingTransport() { 54 assertEquals(TRANSPORT_WIFI, getDisplayTransport(intArrayOf(TRANSPORT_WIFI))) 55 assertEquals(TRANSPORT_CELLULAR, getDisplayTransport(intArrayOf(TRANSPORT_CELLULAR))) 56 assertEquals(TRANSPORT_BLUETOOTH, getDisplayTransport(intArrayOf(TRANSPORT_BLUETOOTH))) 57 assertEquals(TRANSPORT_ETHERNET, getDisplayTransport(intArrayOf(TRANSPORT_ETHERNET))) 58 assertEquals(TRANSPORT_WIFI_AWARE, getDisplayTransport(intArrayOf(TRANSPORT_WIFI_AWARE))) 59 60 assertEquals(TRANSPORT_VPN, getDisplayTransport( 61 intArrayOf(TRANSPORT_VPN, TRANSPORT_WIFI))) 62 assertEquals(TRANSPORT_VPN, getDisplayTransport( 63 intArrayOf(TRANSPORT_CELLULAR, TRANSPORT_VPN))) 64 65 assertEquals(TRANSPORT_WIFI, getDisplayTransport( 66 intArrayOf(TRANSPORT_ETHERNET, TRANSPORT_WIFI))) 67 assertEquals(TRANSPORT_ETHERNET, getDisplayTransport( 68 intArrayOf(TRANSPORT_ETHERNET, TRANSPORT_TEST))) 69 70 assertFailsWith(IllegalArgumentException::class) { 71 getDisplayTransport(intArrayOf()) 72 } 73 } 74 75 // NetworkCapabilities constructor and Builder are not available until R. Mark TargetApi to 76 // ignore the linter error since it's used in only unit test. 77 @Test @TargetApi(Build.VERSION_CODES.R) testInferRestrictedCapabilitynull78 fun testInferRestrictedCapability() { 79 val nc = NetworkCapabilities() 80 // Default capabilities don't have restricted capability. 81 assertFalse(NetworkCapabilitiesUtils.inferRestrictedCapability(nc)) 82 // If there is a force restricted capability, then the network capabilities is restricted. 83 nc.addCapability(NET_CAPABILITY_OEM_PAID) 84 nc.addCapability(NET_CAPABILITY_INTERNET) 85 assertTrue(NetworkCapabilitiesUtils.inferRestrictedCapability(nc)) 86 // Except for the force restricted capability, if there is any unrestricted capability in 87 // capabilities, then the network capabilities is not restricted. 88 nc.removeCapability(NET_CAPABILITY_OEM_PAID) 89 nc.addCapability(NET_CAPABILITY_CBS) 90 assertFalse(NetworkCapabilitiesUtils.inferRestrictedCapability(nc)) 91 // Except for the force restricted capability, the network capabilities will only be treated 92 // as restricted when there is no any unrestricted capability. 93 nc.removeCapability(NET_CAPABILITY_INTERNET) 94 assertTrue(NetworkCapabilitiesUtils.inferRestrictedCapability(nc)) 95 if (!SdkLevel.isAtLeastS()) return 96 // BIP deserves its specific test because it's the first capability over 30, meaning the 97 // shift will overflow 98 nc.removeCapability(NET_CAPABILITY_CBS) 99 nc.addCapability(NET_CAPABILITY_BIP) 100 assertTrue(NetworkCapabilitiesUtils.inferRestrictedCapability(nc)) 101 } 102 103 @Test testRestrictedUnrestrictedCapabilitiesnull104 fun testRestrictedUnrestrictedCapabilities() { 105 // verify EIMS is restricted 106 assertEquals((1 shl NET_CAPABILITY_EIMS).toLong() and RESTRICTED_CAPABILITIES, 107 (1 shl NET_CAPABILITY_EIMS).toLong()) 108 109 // verify CBS is also restricted 110 assertEquals((1 shl NET_CAPABILITY_CBS).toLong() and RESTRICTED_CAPABILITIES, 111 (1 shl NET_CAPABILITY_CBS).toLong()) 112 113 // verify BIP is also restricted 114 // BIP is not available in R and before, but the BIP constant is inlined so 115 // this test can still run on R. 116 assertEquals((1L shl NET_CAPABILITY_BIP) and RESTRICTED_CAPABILITIES, 117 (1L shl NET_CAPABILITY_BIP)) 118 119 // verify default is not restricted 120 assertEquals((1 shl NET_CAPABILITY_INTERNET).toLong() and RESTRICTED_CAPABILITIES, 0) 121 122 assertTrue(RESTRICTED_CAPABILITIES > 0) 123 124 // just to see 125 assertEquals(RESTRICTED_CAPABILITIES and UNRESTRICTED_CAPABILITIES, 0) 126 } 127 } 128