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 com.android.cts.net.hostside; 18 19 import static android.os.Process.SYSTEM_UID; 20 21 import static com.android.cts.net.hostside.NetworkPolicyTestUtils.assertNetworkingBlockedStatusForUid; 22 import static com.android.cts.net.hostside.NetworkPolicyTestUtils.canChangeActiveNetworkMeteredness; 23 import static com.android.cts.net.hostside.NetworkPolicyTestUtils.isUidNetworkingBlocked; 24 import static com.android.cts.net.hostside.NetworkPolicyTestUtils.isUidRestrictedOnMeteredNetworks; 25 import static com.android.cts.net.hostside.NetworkPolicyTestUtils.setRestrictBackground; 26 import static com.android.cts.net.hostside.Property.BATTERY_SAVER_MODE; 27 import static com.android.cts.net.hostside.Property.DATA_SAVER_MODE; 28 29 import static org.junit.Assert.assertFalse; 30 import static org.junit.Assert.assertTrue; 31 import static org.junit.Assume.assumeTrue; 32 33 import org.junit.After; 34 import org.junit.Before; 35 import org.junit.Rule; 36 import org.junit.Test; 37 38 public class NetworkPolicyManagerTest extends AbstractRestrictBackgroundNetworkTestCase { 39 private static final boolean METERED = true; 40 private static final boolean NON_METERED = false; 41 42 @Rule 43 public final MeterednessConfigurationRule mMeterednessConfiguration = 44 new MeterednessConfigurationRule(); 45 46 @Before setUp()47 public void setUp() throws Exception { 48 super.setUp(); 49 50 assumeTrue(canChangeActiveNetworkMeteredness()); 51 52 registerBroadcastReceiver(); 53 54 removeRestrictBackgroundWhitelist(mUid); 55 removeRestrictBackgroundBlacklist(mUid); 56 assertRestrictBackgroundChangedReceived(0); 57 58 // Initial state 59 setBatterySaverMode(false); 60 setRestrictBackground(false); 61 setRestrictedNetworkingMode(false); 62 } 63 64 @After tearDown()65 public void tearDown() throws Exception { 66 super.tearDown(); 67 68 setBatterySaverMode(false); 69 setRestrictBackground(false); 70 setRestrictedNetworkingMode(false); 71 unregisterNetworkCallback(); 72 } 73 74 @Test testIsUidNetworkingBlocked_withUidNotBlocked()75 public void testIsUidNetworkingBlocked_withUidNotBlocked() throws Exception { 76 // Refer to NetworkPolicyManagerService#isUidNetworkingBlockedInternal(), this test is to 77 // test the cases of non-metered network and uid not matched by any rule. 78 // If mUid is not blocked by data saver mode or power saver mode, no matter the network is 79 // metered or non-metered, mUid shouldn't be blocked. 80 assertFalse(isUidNetworkingBlocked(mUid, METERED)); // Match NTWK_ALLOWED_DEFAULT 81 assertFalse(isUidNetworkingBlocked(mUid, NON_METERED)); // Match NTWK_ALLOWED_NON_METERED 82 } 83 84 @RequiredProperties({DATA_SAVER_MODE, BATTERY_SAVER_MODE}) 85 @Test testIsUidNetworkingBlocked_withSystemUid()86 public void testIsUidNetworkingBlocked_withSystemUid() throws Exception { 87 // Refer to NetworkPolicyManagerService#isUidNetworkingBlockedInternal(), this test is to 88 // test the case of uid is system uid. 89 // SYSTEM_UID will never be blocked. 90 assertFalse(isUidNetworkingBlocked(SYSTEM_UID, METERED)); // Match NTWK_ALLOWED_SYSTEM 91 assertFalse(isUidNetworkingBlocked(SYSTEM_UID, NON_METERED)); // Match NTWK_ALLOWED_SYSTEM 92 try { 93 setRestrictBackground(true); 94 setBatterySaverMode(true); 95 setRestrictedNetworkingMode(true); 96 assertNetworkingBlockedStatusForUid(SYSTEM_UID, METERED, 97 false /* expectedResult */); // Match NTWK_ALLOWED_SYSTEM 98 assertFalse( 99 isUidNetworkingBlocked(SYSTEM_UID, NON_METERED)); // Match NTWK_ALLOWED_SYSTEM 100 } finally { 101 setRestrictBackground(false); 102 setBatterySaverMode(false); 103 setRestrictedNetworkingMode(false); 104 assertNetworkingBlockedStatusForUid(mUid, METERED, 105 false /* expectedResult */); // Match NTWK_ALLOWED_DEFAULT 106 } 107 } 108 109 @RequiredProperties({DATA_SAVER_MODE}) 110 @Test testIsUidNetworkingBlocked_withDataSaverMode()111 public void testIsUidNetworkingBlocked_withDataSaverMode() throws Exception { 112 // Refer to NetworkPolicyManagerService#isUidNetworkingBlockedInternal(), this test is to 113 // test the cases of non-metered network, uid is matched by restrict background blacklist, 114 // uid is matched by restrict background whitelist, app is in the foreground with restrict 115 // background enabled and the app is in the background with restrict background enabled. 116 try { 117 // Enable restrict background and mUid will be blocked because it's not in the 118 // foreground. 119 setRestrictBackground(true); 120 assertNetworkingBlockedStatusForUid(mUid, METERED, 121 true /* expectedResult */); // Match NTWK_BLOCKED_BG_RESTRICT 122 123 // Although restrict background is enabled and mUid is in the background, but mUid will 124 // not be blocked if network is non-metered. 125 assertFalse( 126 isUidNetworkingBlocked(mUid, NON_METERED)); // Match NTWK_ALLOWED_NON_METERED 127 128 // Add mUid into the restrict background blacklist. 129 addRestrictBackgroundBlacklist(mUid); 130 assertNetworkingBlockedStatusForUid(mUid, METERED, 131 true /* expectedResult */); // Match NTWK_BLOCKED_DENYLIST 132 133 // Although mUid is in the restrict background blacklist, but mUid won't be blocked if 134 // the network is non-metered. 135 assertFalse( 136 isUidNetworkingBlocked(mUid, NON_METERED)); // Match NTWK_ALLOWED_NON_METERED 137 removeRestrictBackgroundBlacklist(mUid); 138 139 // Add mUid into the restrict background whitelist. 140 addRestrictBackgroundWhitelist(mUid); 141 assertNetworkingBlockedStatusForUid(mUid, METERED, 142 false /* expectedResult */); // Match NTWK_ALLOWED_ALLOWLIST 143 assertFalse( 144 isUidNetworkingBlocked(mUid, NON_METERED)); // Match NTWK_ALLOWED_NON_METERED 145 removeRestrictBackgroundWhitelist(mUid); 146 147 // Make TEST_APP2_PKG go to foreground and mUid will be allowed temporarily. 148 launchComponentAndAssertNetworkAccess(TYPE_COMPONENT_ACTIVTIY); 149 assertForegroundState(); 150 assertNetworkingBlockedStatusForUid(mUid, METERED, 151 false /* expectedResult */); // Match NTWK_ALLOWED_TMP_ALLOWLIST 152 153 // Back to background. 154 finishActivity(); 155 assertNetworkingBlockedStatusForUid(mUid, METERED, 156 true /* expectedResult */); // Match NTWK_BLOCKED_BG_RESTRICT 157 } finally { 158 setRestrictBackground(false); 159 assertNetworkingBlockedStatusForUid(mUid, METERED, 160 false /* expectedResult */); // Match NTWK_ALLOWED_DEFAULT 161 } 162 } 163 164 @Test testIsUidNetworkingBlocked_withRestrictedNetworkingMode()165 public void testIsUidNetworkingBlocked_withRestrictedNetworkingMode() throws Exception { 166 // Refer to NetworkPolicyManagerService#isUidNetworkingBlockedInternal(), this test is to 167 // test the cases of restricted networking mode enabled. 168 try { 169 // All apps should be blocked if restricted networking mode is enabled except for those 170 // apps who have CONNECTIVITY_USE_RESTRICTED_NETWORKS permission. 171 // This test won't test if an app who has CONNECTIVITY_USE_RESTRICTED_NETWORKS will not 172 // be blocked because CONNECTIVITY_USE_RESTRICTED_NETWORKS is a signature/privileged 173 // permission that CTS cannot acquire. Also it's not good for this test to use those 174 // privileged apps which have CONNECTIVITY_USE_RESTRICTED_NETWORKS to test because there 175 // is no guarantee that those apps won't remove this permission someday, and if it 176 // happens, then this test will fail. 177 setRestrictedNetworkingMode(true); 178 assertNetworkingBlockedStatusForUid(mUid, METERED, 179 true /* expectedResult */); // Match NTWK_BLOCKED_RESTRICTED_MODE 180 assertTrue(isUidNetworkingBlocked(mUid, 181 NON_METERED)); // Match NTWK_BLOCKED_RESTRICTED_MODE 182 } finally { 183 setRestrictedNetworkingMode(false); 184 assertNetworkingBlockedStatusForUid(mUid, METERED, 185 false /* expectedResult */); // Match NTWK_ALLOWED_DEFAULT 186 } 187 } 188 189 @RequiredProperties({BATTERY_SAVER_MODE}) 190 @Test testIsUidNetworkingBlocked_withPowerSaverMode()191 public void testIsUidNetworkingBlocked_withPowerSaverMode() throws Exception { 192 // Refer to NetworkPolicyManagerService#isUidNetworkingBlockedInternal(), this test is to 193 // test the cases of power saver mode enabled, uid in the power saver mode whitelist and 194 // uid in the power saver mode whitelist with non-metered network. 195 try { 196 // mUid should be blocked if power saver mode is enabled. 197 setBatterySaverMode(true); 198 assertNetworkingBlockedStatusForUid(mUid, METERED, 199 true /* expectedResult */); // Match NTWK_BLOCKED_POWER 200 assertTrue(isUidNetworkingBlocked(mUid, NON_METERED)); // Match NTWK_BLOCKED_POWER 201 202 // Add TEST_APP2_PKG into power saver mode whitelist, its uid rule is RULE_ALLOW_ALL and 203 // it shouldn't be blocked. 204 addPowerSaveModeWhitelist(TEST_APP2_PKG); 205 assertNetworkingBlockedStatusForUid(mUid, METERED, 206 false /* expectedResult */); // Match NTWK_ALLOWED_DEFAULT 207 assertFalse( 208 isUidNetworkingBlocked(mUid, NON_METERED)); // Match NTWK_ALLOWED_NON_METERED 209 removePowerSaveModeWhitelist(TEST_APP2_PKG); 210 } finally { 211 setBatterySaverMode(false); 212 assertNetworkingBlockedStatusForUid(mUid, METERED, 213 false /* expectedResult */); // Match NTWK_ALLOWED_DEFAULT 214 } 215 } 216 217 @RequiredProperties({DATA_SAVER_MODE}) 218 @Test testIsUidRestrictedOnMeteredNetworks()219 public void testIsUidRestrictedOnMeteredNetworks() throws Exception { 220 try { 221 // isUidRestrictedOnMeteredNetworks() will only return true when restrict background is 222 // enabled and mUid is not in the restrict background whitelist and TEST_APP2_PKG is not 223 // in the foreground. For other cases, it will return false. 224 setRestrictBackground(true); 225 assertTrue(isUidRestrictedOnMeteredNetworks(mUid)); 226 227 // Make TEST_APP2_PKG go to foreground and isUidRestrictedOnMeteredNetworks() will 228 // return false. 229 launchComponentAndAssertNetworkAccess(TYPE_COMPONENT_ACTIVTIY); 230 assertForegroundState(); 231 assertFalse(isUidRestrictedOnMeteredNetworks(mUid)); 232 // Back to background. 233 finishActivity(); 234 235 // Add mUid into restrict background whitelist and isUidRestrictedOnMeteredNetworks() 236 // will return false. 237 addRestrictBackgroundWhitelist(mUid); 238 assertFalse(isUidRestrictedOnMeteredNetworks(mUid)); 239 removeRestrictBackgroundWhitelist(mUid); 240 } finally { 241 // Restrict background is disabled and isUidRestrictedOnMeteredNetworks() will return 242 // false. 243 setRestrictBackground(false); 244 assertFalse(isUidRestrictedOnMeteredNetworks(mUid)); 245 } 246 } 247 } 248