1 /*
2  * Copyright (C) 2019 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 android.appsecurity.cts;
18 
19 import android.platform.test.annotations.AppModeFull;
20 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
21 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 
25 /**
26  * Test for app ops behaviors.
27  */
28 @RunWith(DeviceJUnit4ClassRunner.class)
29 @AppModeFull(reason = "No instant specific behaviors")
30 public final class AppOpsTest extends BaseHostJUnit4Test {
31 
32     @Test
testBadConfigCannotCauseBootLoopEnabled()33     public void testBadConfigCannotCauseBootLoopEnabled() throws Exception {
34         try {
35             createBadHistoricalFile();
36             setAppOpHistoryParameters("mode=HISTORICAL_MODE_ENABLED_ACTIVE,baseIntervalMillis=1000"
37                     + ",intervalMultiplier=10");
38             getDevice().reboot();
39         } finally {
40             setAppOpHistoryParameters(null);
41             deleteHistoricalFiles();
42         }
43     }
44 
45     @Test
testBadConfigCannotCauseBootLoopDisabled()46     public void testBadConfigCannotCauseBootLoopDisabled() throws Exception {
47         try {
48             createBadHistoricalFile();
49             setAppOpHistoryParameters("mode=HISTORICAL_MODE_DISABLED,baseIntervalMillis=1000"
50                     + ",intervalMultiplier=10");
51             getDevice().reboot();
52         } finally {
53             setAppOpHistoryParameters(null);
54             deleteHistoricalFiles();
55         }
56     }
57 
setAppOpHistoryParameters(String value)58     private void setAppOpHistoryParameters(String value) throws Exception {
59         getDevice().executeShellCommand("settings put global appop_history_parameters " + value);
60     }
61 
createBadHistoricalFile()62     private void createBadHistoricalFile() throws Exception {
63         getDevice().executeShellCommand("touch data/system/appops/history/1000.xml");
64     }
65 
deleteHistoricalFiles()66     private void deleteHistoricalFiles() throws Exception {
67         getDevice().executeShellCommand("rm -rf data/system/appops/history");
68     }
69 }
70