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