1 /*
2  * Copyright (C) 2017 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.deviceidle;
18 
19 import com.android.tradefed.util.RunUtil;
20 import static org.junit.Assert.*;
21 
22 import com.android.tradefed.device.DeviceNotAvailableException;
23 import com.android.tradefed.log.LogUtil;
24 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
25 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
26 
27 import org.junit.After;
28 import org.junit.Assume;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 
33 import java.util.ArrayList;
34 import java.util.List;
35 
36 /**
37  * Tests that it is possible to remove apps from the system allowlist
38  */
39 @RunWith(DeviceJUnit4ClassRunner.class)
40 public class DeviceIdleWhitelistTest extends BaseHostJUnit4Test {
41 
42     private static final String DEVICE_IDLE_COMMAND_PREFIX = "cmd deviceidle sys-whitelist ";
43     private static final String RESET_SYS_ALLOWLIST_COMMAND = "cmd deviceidle sys-whitelist reset";
44     private static final String SHOW_SYS_ALLOWLIST_COMMAND = DEVICE_IDLE_COMMAND_PREFIX;
45 
46     private List<String> mOriginalSystemWhitelist;
47 
48     @Before
setUp()49     public void setUp() throws Exception {
50         getDevice().executeShellCommand(RESET_SYS_ALLOWLIST_COMMAND);
51         mOriginalSystemWhitelist = getSystemAllowlist();
52         if (mOriginalSystemWhitelist.size() < 1) {
53             LogUtil.CLog.w("No packages found in system whitelist");
54             Assume.assumeTrue(false);
55         }
56     }
57 
58     @After
tearDown()59     public void tearDown() throws Exception {
60         getDevice().executeShellCommand(RESET_SYS_ALLOWLIST_COMMAND);
61     }
62 
63     @Test
testRemoveFromSysWhitelist()64     public void testRemoveFromSysWhitelist() throws Exception {
65         final String packageToRemove = mOriginalSystemWhitelist.get(0);
66         getDevice().executeShellCommand(DEVICE_IDLE_COMMAND_PREFIX + "-" + packageToRemove);
67         final List<String> newWhitelist = getSystemAllowlist();
68         assertFalse("Package " + packageToRemove + " not removed from whitelist",
69                 newWhitelist.contains(packageToRemove));
70     }
71 
72     @Test
testRemovesPersistedAcrossReboots()73     public void testRemovesPersistedAcrossReboots() throws Exception {
74         for (int i = 0; i < mOriginalSystemWhitelist.size(); i+=2) {
75             // remove odd indexed packages from the allowlist
76             getDevice().executeShellCommand(
77                     DEVICE_IDLE_COMMAND_PREFIX + "-" + mOriginalSystemWhitelist.get(i));
78         }
79         final List<String> whitelistBeforeReboot = getSystemAllowlist();
80         RunUtil.getDefault().sleep(10_000); // write to disk happens after 5 seconds
81         getDevice().reboot();
82         RunUtil.getDefault().sleep(5_000); // to make sure service is initialized
83         final List<String> whitelistAfterReboot = getSystemAllowlist();
84         assertEquals(whitelistBeforeReboot.size(), whitelistAfterReboot.size());
85         for (int i = 0; i < whitelistBeforeReboot.size(); i++) {
86             assertTrue(whitelistAfterReboot.contains(whitelistBeforeReboot.get(i)));
87         }
88     }
89 
getSystemAllowlist()90     private List<String> getSystemAllowlist() throws DeviceNotAvailableException {
91         final String output = getDevice().executeShellCommand(SHOW_SYS_ALLOWLIST_COMMAND).trim();
92         final List<String> packages = new ArrayList<>();
93         for (String line : output.split("\n")) {
94             final int i = line.indexOf(',');
95             if (i > 0) {
96                 packages.add(line.substring(0, i));
97             }
98         }
99         return packages;
100     }
101 }
102