1 /* 2 * Copyright (C) 2016 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.netpolicy; 18 19 import static org.junit.Assert.fail; 20 21 import android.platform.test.annotations.FlakyTest; 22 import android.platform.test.annotations.SecurityTest; 23 24 import com.android.ddmlib.Log; 25 import com.android.tradefed.device.DeviceNotAvailableException; 26 import com.android.tradefed.util.RunUtil; 27 28 import org.junit.After; 29 import org.junit.Before; 30 import org.junit.Test; 31 32 @FlakyTest(bugId = 288324467) 33 public class HostsideRestrictBackgroundNetworkTests extends HostsideNetworkPolicyTestCase { 34 35 @Before setUp()36 public void setUp() throws Exception { 37 uninstallPackage(TEST_APP2_PKG, false); 38 installPackage(TEST_APP2_APK); 39 } 40 41 @After tearDown()42 public void tearDown() throws Exception { 43 uninstallPackage(TEST_APP2_PKG, true); 44 } 45 46 @SecurityTest 47 @Test testDataWarningReceiver()48 public void testDataWarningReceiver() throws Exception { 49 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".DataWarningReceiverTest", 50 "testSnoozeWarningNotReceived"); 51 } 52 53 /************************** 54 * Data Saver Mode tests. * 55 **************************/ 56 57 @Test testDataSaverMode_disabled()58 public void testDataSaverMode_disabled() throws Exception { 59 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".DataSaverModeTest", 60 "testGetRestrictBackgroundStatus_disabled"); 61 } 62 63 @Test testDataSaverMode_whitelisted()64 public void testDataSaverMode_whitelisted() throws Exception { 65 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".DataSaverModeTest", 66 "testGetRestrictBackgroundStatus_whitelisted"); 67 } 68 69 @Test testDataSaverMode_enabled()70 public void testDataSaverMode_enabled() throws Exception { 71 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".DataSaverModeTest", 72 "testGetRestrictBackgroundStatus_enabled"); 73 } 74 75 @Test testDataSaverMode_blacklisted()76 public void testDataSaverMode_blacklisted() throws Exception { 77 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".DataSaverModeTest", 78 "testGetRestrictBackgroundStatus_blacklisted"); 79 } 80 81 @Test testDataSaverMode_reinstall()82 public void testDataSaverMode_reinstall() throws Exception { 83 final int oldUid = getUid(TEST_APP2_PKG); 84 85 // Make sure whitelist is revoked when package is removed 86 addRestrictBackgroundWhitelist(oldUid); 87 88 uninstallPackage(TEST_APP2_PKG, true); 89 assertPackageUninstalled(TEST_APP2_PKG); 90 assertRestrictBackgroundWhitelist(oldUid, false); 91 92 installPackage(TEST_APP2_APK); 93 final int newUid = getUid(TEST_APP2_PKG); 94 assertRestrictBackgroundWhitelist(oldUid, false); 95 assertRestrictBackgroundWhitelist(newUid, false); 96 } 97 98 @Test testDataSaverMode_requiredWhitelistedPackages()99 public void testDataSaverMode_requiredWhitelistedPackages() throws Exception { 100 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".DataSaverModeTest", 101 "testGetRestrictBackgroundStatus_requiredWhitelistedPackages"); 102 } 103 104 @Test testDataSaverMode_broadcastNotSentOnUnsupportedDevices()105 public void testDataSaverMode_broadcastNotSentOnUnsupportedDevices() throws Exception { 106 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".DataSaverModeTest", 107 "testBroadcastNotSentOnUnsupportedDevices"); 108 } 109 110 /***************************** 111 * Battery Saver Mode tests. * 112 *****************************/ 113 114 @Test testBatterySaverModeMetered_disabled()115 public void testBatterySaverModeMetered_disabled() throws Exception { 116 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".BatterySaverModeMeteredTest", 117 "testBackgroundNetworkAccess_disabled"); 118 } 119 120 @Test testBatterySaverModeMetered_whitelisted()121 public void testBatterySaverModeMetered_whitelisted() throws Exception { 122 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".BatterySaverModeMeteredTest", 123 "testBackgroundNetworkAccess_whitelisted"); 124 } 125 126 @Test testBatterySaverModeMetered_enabled()127 public void testBatterySaverModeMetered_enabled() throws Exception { 128 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".BatterySaverModeMeteredTest", 129 "testBackgroundNetworkAccess_enabled"); 130 } 131 132 @Test testBatterySaverMode_reinstall()133 public void testBatterySaverMode_reinstall() throws Exception { 134 if (!isDozeModeEnabled()) { 135 Log.w(TAG, "testBatterySaverMode_reinstall() skipped because device does not support " 136 + "Doze Mode"); 137 return; 138 } 139 140 addPowerSaveModeWhitelist(TEST_APP2_PKG); 141 142 uninstallPackage(TEST_APP2_PKG, true); 143 assertPackageUninstalled(TEST_APP2_PKG); 144 assertPowerSaveModeWhitelist(TEST_APP2_PKG, false); 145 146 installPackage(TEST_APP2_APK); 147 assertPowerSaveModeWhitelist(TEST_APP2_PKG, false); 148 } 149 150 @Test testBatterySaverModeNonMetered_disabled()151 public void testBatterySaverModeNonMetered_disabled() throws Exception { 152 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".BatterySaverModeNonMeteredTest", 153 "testBackgroundNetworkAccess_disabled"); 154 } 155 156 @Test testBatterySaverModeNonMetered_whitelisted()157 public void testBatterySaverModeNonMetered_whitelisted() throws Exception { 158 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".BatterySaverModeNonMeteredTest", 159 "testBackgroundNetworkAccess_whitelisted"); 160 } 161 162 @Test testBatterySaverModeNonMetered_enabled()163 public void testBatterySaverModeNonMetered_enabled() throws Exception { 164 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".BatterySaverModeNonMeteredTest", 165 "testBackgroundNetworkAccess_enabled"); 166 } 167 168 /******************* 169 * App idle tests. * 170 *******************/ 171 172 @Test testAppIdleMetered_disabled()173 public void testAppIdleMetered_disabled() throws Exception { 174 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".AppIdleMeteredTest", 175 "testBackgroundNetworkAccess_disabled"); 176 } 177 178 @Test testAppIdleMetered_whitelisted()179 public void testAppIdleMetered_whitelisted() throws Exception { 180 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".AppIdleMeteredTest", 181 "testBackgroundNetworkAccess_whitelisted"); 182 } 183 184 @Test testAppIdleMetered_tempWhitelisted()185 public void testAppIdleMetered_tempWhitelisted() throws Exception { 186 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".AppIdleMeteredTest", 187 "testBackgroundNetworkAccess_tempWhitelisted"); 188 } 189 190 @Test testAppIdleMetered_enabled()191 public void testAppIdleMetered_enabled() throws Exception { 192 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".AppIdleMeteredTest", 193 "testBackgroundNetworkAccess_enabled"); 194 } 195 196 @Test testAppIdleMetered_idleWhitelisted()197 public void testAppIdleMetered_idleWhitelisted() throws Exception { 198 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".AppIdleMeteredTest", 199 "testAppIdleNetworkAccess_idleWhitelisted"); 200 } 201 202 // TODO: currently power-save mode and idle uses the same whitelist, so this test would be 203 // redundant (as it would be testing the same as testBatterySaverMode_reinstall()) 204 // public void testAppIdle_reinstall() throws Exception { 205 // } 206 207 @Test testAppIdleNonMetered_disabled()208 public void testAppIdleNonMetered_disabled() throws Exception { 209 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".AppIdleNonMeteredTest", 210 "testBackgroundNetworkAccess_disabled"); 211 } 212 213 214 @Test testAppIdleNonMetered_whitelisted()215 public void testAppIdleNonMetered_whitelisted() throws Exception { 216 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".AppIdleNonMeteredTest", 217 "testBackgroundNetworkAccess_whitelisted"); 218 } 219 220 @Test testAppIdleNonMetered_tempWhitelisted()221 public void testAppIdleNonMetered_tempWhitelisted() throws Exception { 222 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".AppIdleNonMeteredTest", 223 "testBackgroundNetworkAccess_tempWhitelisted"); 224 } 225 226 @Test testAppIdleNonMetered_enabled()227 public void testAppIdleNonMetered_enabled() throws Exception { 228 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".AppIdleNonMeteredTest", 229 "testBackgroundNetworkAccess_enabled"); 230 } 231 232 @Test testAppIdleNonMetered_idleWhitelisted()233 public void testAppIdleNonMetered_idleWhitelisted() throws Exception { 234 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".AppIdleNonMeteredTest", 235 "testAppIdleNetworkAccess_idleWhitelisted"); 236 } 237 238 @Test testAppIdleNonMetered_whenCharging()239 public void testAppIdleNonMetered_whenCharging() throws Exception { 240 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".AppIdleNonMeteredTest", 241 "testAppIdleNetworkAccess_whenCharging"); 242 } 243 244 @Test testAppIdleMetered_whenCharging()245 public void testAppIdleMetered_whenCharging() throws Exception { 246 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".AppIdleMeteredTest", 247 "testAppIdleNetworkAccess_whenCharging"); 248 } 249 250 @Test testAppIdle_toast()251 public void testAppIdle_toast() throws Exception { 252 // Check that showing a toast doesn't bring an app out of standby 253 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".AppIdleNonMeteredTest", 254 "testAppIdle_toast"); 255 } 256 257 /******************** 258 * Doze Mode tests. * 259 ********************/ 260 261 @Test testDozeModeMetered_disabled()262 public void testDozeModeMetered_disabled() throws Exception { 263 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".DozeModeMeteredTest", 264 "testBackgroundNetworkAccess_disabled"); 265 } 266 267 @Test testDozeModeMetered_whitelisted()268 public void testDozeModeMetered_whitelisted() throws Exception { 269 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".DozeModeMeteredTest", 270 "testBackgroundNetworkAccess_whitelisted"); 271 } 272 273 @Test testDozeModeMetered_enabled()274 public void testDozeModeMetered_enabled() throws Exception { 275 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".DozeModeMeteredTest", 276 "testBackgroundNetworkAccess_enabled"); 277 } 278 279 @Test testDozeModeMetered_enabledButWhitelistedOnNotificationAction()280 public void testDozeModeMetered_enabledButWhitelistedOnNotificationAction() throws Exception { 281 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".DozeModeMeteredTest", 282 "testBackgroundNetworkAccess_enabledButWhitelistedOnNotificationAction"); 283 } 284 285 // TODO: currently power-save mode and idle uses the same whitelist, so this test would be 286 // redundant (as it would be testing the same as testBatterySaverMode_reinstall()) 287 // public void testDozeMode_reinstall() throws Exception { 288 // } 289 290 @Test testDozeModeNonMetered_disabled()291 public void testDozeModeNonMetered_disabled() throws Exception { 292 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".DozeModeNonMeteredTest", 293 "testBackgroundNetworkAccess_disabled"); 294 } 295 296 @Test testDozeModeNonMetered_whitelisted()297 public void testDozeModeNonMetered_whitelisted() throws Exception { 298 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".DozeModeNonMeteredTest", 299 "testBackgroundNetworkAccess_whitelisted"); 300 } 301 302 @Test testDozeModeNonMetered_enabled()303 public void testDozeModeNonMetered_enabled() throws Exception { 304 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".DozeModeNonMeteredTest", 305 "testBackgroundNetworkAccess_enabled"); 306 } 307 308 @Test testDozeModeNonMetered_enabledButWhitelistedOnNotificationAction()309 public void testDozeModeNonMetered_enabledButWhitelistedOnNotificationAction() 310 throws Exception { 311 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".DozeModeNonMeteredTest", 312 "testBackgroundNetworkAccess_enabledButWhitelistedOnNotificationAction"); 313 } 314 315 /********************** 316 * Mixed modes tests. * 317 **********************/ 318 319 @Test testDataAndBatterySaverModes_meteredNetwork()320 public void testDataAndBatterySaverModes_meteredNetwork() throws Exception { 321 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".MixedModesTest", 322 "testDataAndBatterySaverModes_meteredNetwork"); 323 } 324 325 @Test testDataAndBatterySaverModes_nonMeteredNetwork()326 public void testDataAndBatterySaverModes_nonMeteredNetwork() throws Exception { 327 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".MixedModesTest", 328 "testDataAndBatterySaverModes_nonMeteredNetwork"); 329 } 330 331 @Test testDozeAndBatterySaverMode_powerSaveWhitelists()332 public void testDozeAndBatterySaverMode_powerSaveWhitelists() throws Exception { 333 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".MixedModesTest", 334 "testDozeAndBatterySaverMode_powerSaveWhitelists"); 335 } 336 337 @Test testDozeAndAppIdle_powerSaveWhitelists()338 public void testDozeAndAppIdle_powerSaveWhitelists() throws Exception { 339 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".MixedModesTest", 340 "testDozeAndAppIdle_powerSaveWhitelists"); 341 } 342 343 @Test testAppIdleAndDoze_tempPowerSaveWhitelists()344 public void testAppIdleAndDoze_tempPowerSaveWhitelists() throws Exception { 345 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".MixedModesTest", 346 "testAppIdleAndDoze_tempPowerSaveWhitelists"); 347 } 348 349 @Test testAppIdleAndBatterySaver_tempPowerSaveWhitelists()350 public void testAppIdleAndBatterySaver_tempPowerSaveWhitelists() throws Exception { 351 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".MixedModesTest", 352 "testAppIdleAndBatterySaver_tempPowerSaveWhitelists"); 353 } 354 355 @Test testDozeAndAppIdle_appIdleWhitelist()356 public void testDozeAndAppIdle_appIdleWhitelist() throws Exception { 357 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".MixedModesTest", 358 "testDozeAndAppIdle_appIdleWhitelist"); 359 } 360 361 @Test testAppIdleAndDoze_tempPowerSaveAndAppIdleWhitelists()362 public void testAppIdleAndDoze_tempPowerSaveAndAppIdleWhitelists() throws Exception { 363 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".MixedModesTest", 364 "testAppIdleAndDoze_tempPowerSaveAndAppIdleWhitelists"); 365 } 366 367 @Test testAppIdleAndBatterySaver_tempPowerSaveAndAppIdleWhitelists()368 public void testAppIdleAndBatterySaver_tempPowerSaveAndAppIdleWhitelists() throws Exception { 369 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".MixedModesTest", 370 "testAppIdleAndBatterySaver_tempPowerSaveAndAppIdleWhitelists"); 371 } 372 373 /************************** 374 * Restricted mode tests. * 375 **************************/ 376 377 @Test testNetworkAccess_restrictedMode()378 public void testNetworkAccess_restrictedMode() throws Exception { 379 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".RestrictedModeTest", 380 "testNetworkAccess"); 381 } 382 383 @Test testNetworkAccess_restrictedMode_withBatterySaver()384 public void testNetworkAccess_restrictedMode_withBatterySaver() throws Exception { 385 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".RestrictedModeTest", 386 "testNetworkAccess_withBatterySaver"); 387 } 388 389 /************************ 390 * Expedited job tests. * 391 ************************/ 392 393 @Test testMeteredNetworkAccess_expeditedJob()394 public void testMeteredNetworkAccess_expeditedJob() throws Exception { 395 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".ExpeditedJobMeteredTest"); 396 } 397 398 @Test testNonMeteredNetworkAccess_expeditedJob()399 public void testNonMeteredNetworkAccess_expeditedJob() throws Exception { 400 runDeviceTestsWithCustomOptions(TEST_PKG, TEST_PKG + ".ExpeditedJobNonMeteredTest"); 401 } 402 403 /******************* 404 * Helper methods. * 405 *******************/ 406 assertRestrictBackgroundWhitelist(int uid, boolean expected)407 private void assertRestrictBackgroundWhitelist(int uid, boolean expected) throws Exception { 408 final int max_tries = 5; 409 boolean actual = false; 410 for (int i = 1; i <= max_tries; i++) { 411 final String output = runCommand("cmd netpolicy list restrict-background-whitelist "); 412 actual = output.contains(Integer.toString(uid)); 413 if (expected == actual) { 414 return; 415 } 416 Log.v(TAG, "whitelist check for uid " + uid + " doesn't match yet (expected " 417 + expected + ", got " + actual + "); sleeping 1s before polling again"); 418 RunUtil.getDefault().sleep(1000); 419 } 420 fail("whitelist check for uid " + uid + " failed: expected " 421 + expected + ", got " + actual); 422 } 423 assertPowerSaveModeWhitelist(String packageName, boolean expected)424 private void assertPowerSaveModeWhitelist(String packageName, boolean expected) 425 throws Exception { 426 // TODO: currently the power-save mode is behaving like idle, but once it changes, we'll 427 // need to use netpolicy for whitelisting 428 assertDelayedCommand("dumpsys deviceidle whitelist =" + packageName, 429 Boolean.toString(expected)); 430 } 431 432 /** 433 * Asserts the result of a command, wait and re-running it a couple times if necessary. 434 */ assertDelayedCommand(String command, String expectedResult)435 private void assertDelayedCommand(String command, String expectedResult) 436 throws InterruptedException, DeviceNotAvailableException { 437 final int maxTries = 5; 438 for (int i = 1; i <= maxTries; i++) { 439 final String result = runCommand(command).trim(); 440 if (result.equals(expectedResult)) return; 441 Log.v(TAG, "Command '" + command + "' returned '" + result + " instead of '" 442 + expectedResult + "' on attempt #; sleeping 1s before polling again"); 443 RunUtil.getDefault().sleep(1000); 444 } 445 fail("Command '" + command + "' did not return '" + expectedResult + "' after " + maxTries 446 + " attempts"); 447 } 448 addRestrictBackgroundWhitelist(int uid)449 protected void addRestrictBackgroundWhitelist(int uid) throws Exception { 450 runCommand("cmd netpolicy add restrict-background-whitelist " + uid); 451 assertRestrictBackgroundWhitelist(uid, true); 452 } 453 addPowerSaveModeWhitelist(String packageName)454 private void addPowerSaveModeWhitelist(String packageName) throws Exception { 455 Log.i(TAG, "Adding package " + packageName + " to power-save-mode whitelist"); 456 // TODO: currently the power-save mode is behaving like idle, but once it changes, we'll 457 // need to use netpolicy for whitelisting 458 runCommand("dumpsys deviceidle whitelist +" + packageName); 459 assertPowerSaveModeWhitelist(packageName, true); 460 } 461 isDozeModeEnabled()462 protected boolean isDozeModeEnabled() throws Exception { 463 final String result = runCommand("cmd deviceidle enabled deep").trim(); 464 return result.equals("1"); 465 } 466 } 467