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 android.appsecurity.cts;
18 
19 
20 import static org.junit.Assert.assertTrue;
21 
22 import com.android.tradefed.device.DeviceNotAvailableException;
23 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
24 
25 import org.junit.After;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 
30 import java.io.FileNotFoundException;
31 
32 /**
33  * Test that:
34  * 1) all the public fields annotated with @Readable in Settings.Secure, Settings.System,
35  * Settings.Global classes are readable.
36  * 2) hidden fields added before S are also readable, via their raw Settings key String values.
37  * 3) public fields without the @Readable annotation will not be readable.
38  *
39  * Run with:
40  * atest android.appsecurity.cts.ReadableSettingsFieldsTest
41  */
42 @RunWith(DeviceJUnit4ClassRunner.class)
43 public class ReadableSettingsFieldsTest extends BaseAppSecurityTest {
44     private static final String TEST_PACKAGE = "com.android.cts.readsettingsfieldsapp";
45     private static final String TEST_CLASS = TEST_PACKAGE + ".ReadSettingsFieldsTest";
46     private static final String TEST_APK = "CtsReadSettingsFieldsApp.apk";
47     private static final String TEST_APK_TEST_ONLY = "CtsReadSettingsFieldsAppTestOnly.apk";
48     private static final String TEST_APK_TARGET_Q = "CtsReadSettingsFieldsAppTargetQ.apk";
49     private static final String TEST_APK_TARGET_R = "CtsReadSettingsFieldsAppTargetR.apk";
50     private static final String TEST_APK_TARGET_S = "CtsReadSettingsFieldsAppTargetS.apk";
51 
52     @Before
setUp()53     public void setUp() throws Exception {
54         new InstallMultiple().addFile(TEST_APK).run();
55         assertTrue(getDevice().isPackageInstalled(TEST_PACKAGE));
56     }
57 
58     @After
tearDown()59     public void tearDown() throws Exception {
60         getDevice().uninstallPackage(TEST_PACKAGE);
61     }
62 
63     @Test
testSecureNonHiddenSettingsKeysAreReadable()64     public void testSecureNonHiddenSettingsKeysAreReadable() throws DeviceNotAvailableException {
65         runDeviceTests(TEST_PACKAGE, TEST_CLASS, "testSecureNonHiddenSettingsKeysAreReadable");
66     }
67 
68     @Test
testSystemNonHiddenSettingsKeysAreReadable()69     public void testSystemNonHiddenSettingsKeysAreReadable() throws DeviceNotAvailableException {
70         runDeviceTests(TEST_PACKAGE, TEST_CLASS, "testSystemNonHiddenSettingsKeysAreReadable");
71     }
72 
73     @Test
testGlobalNonHiddenSettingsKeysAreReadable()74     public void testGlobalNonHiddenSettingsKeysAreReadable() throws DeviceNotAvailableException {
75         runDeviceTests(TEST_PACKAGE, TEST_CLASS, "testGlobalNonHiddenSettingsKeysAreReadable");
76     }
77 
78     @Test
testSecureSomeHiddenSettingsKeysAreReadable()79     public void testSecureSomeHiddenSettingsKeysAreReadable() throws DeviceNotAvailableException {
80         runDeviceTests(TEST_PACKAGE, TEST_CLASS, "testSecureSomeHiddenSettingsKeysAreReadable");
81     }
82 
83     @Test
testSystemSomeHiddenSettingsKeysAreReadable()84     public void testSystemSomeHiddenSettingsKeysAreReadable() throws DeviceNotAvailableException {
85         runDeviceTests(TEST_PACKAGE, TEST_CLASS, "testSystemSomeHiddenSettingsKeysAreReadable");
86     }
87 
88     @Test
testGlobalSomeHiddenSettingsKeysAreReadable()89     public void testGlobalSomeHiddenSettingsKeysAreReadable() throws DeviceNotAvailableException {
90         runDeviceTests(TEST_PACKAGE, TEST_CLASS, "testGlobalSomeHiddenSettingsKeysAreReadable");
91     }
92 
93     @Test
testGlobalHiddenSettingsKeyNotReadableWithoutPermissions()94     public void testGlobalHiddenSettingsKeyNotReadableWithoutPermissions() throws
95             DeviceNotAvailableException {
96         runDeviceTests(TEST_PACKAGE, TEST_CLASS,
97                 "testGlobalHiddenSettingsKeyNotReadableWithoutPermissions");
98     }
99 
100     @Test
testSecureHiddenSettingsKeysNotReadableWithoutAnnotation()101     public void testSecureHiddenSettingsKeysNotReadableWithoutAnnotation()
102             throws DeviceNotAvailableException {
103         runDeviceTests(TEST_PACKAGE, TEST_CLASS,
104                 "testSecureHiddenSettingsKeysNotReadableWithoutAnnotation");
105     }
106 
107     @Test
testSystemHiddenSettingsKeysNotReadableWithoutAnnotation()108     public void testSystemHiddenSettingsKeysNotReadableWithoutAnnotation()
109             throws DeviceNotAvailableException {
110         runDeviceTests(TEST_PACKAGE, TEST_CLASS,
111                 "testSystemHiddenSettingsKeysNotReadableWithoutAnnotation");
112     }
113 
114     @Test
testGlobalHiddenSettingsKeysNotReadableWithoutAnnotation()115     public void testGlobalHiddenSettingsKeysNotReadableWithoutAnnotation()
116             throws DeviceNotAvailableException {
117         runDeviceTests(TEST_PACKAGE, TEST_CLASS,
118                 "testGlobalHiddenSettingsKeysNotReadableWithoutAnnotation");
119     }
120 
121     @Test
testSecureHiddenSettingsKeysReadableWhenTestOnly()122     public void testSecureHiddenSettingsKeysReadableWhenTestOnly()
123             throws DeviceNotAvailableException, FileNotFoundException {
124         new InstallMultiple().addFile(TEST_APK_TEST_ONLY).addArg("-t").run();
125         runDeviceTests(TEST_PACKAGE, TEST_CLASS,
126                 "testSecureHiddenSettingsKeysReadableWithoutAnnotation");
127     }
128 
129     @Test
testSystemHiddenSettingsKeysReadableWhenTestOnly()130     public void testSystemHiddenSettingsKeysReadableWhenTestOnly()
131             throws DeviceNotAvailableException, FileNotFoundException {
132         new InstallMultiple().addFile(TEST_APK_TEST_ONLY).addArg("-t").run();
133         runDeviceTests(TEST_PACKAGE, TEST_CLASS,
134                 "testSystemHiddenSettingsKeysReadableWithoutAnnotation");
135     }
136 
137     @Test
testGlobalHiddenSettingsKeysReadableWhenTestOnly()138     public void testGlobalHiddenSettingsKeysReadableWhenTestOnly()
139             throws DeviceNotAvailableException, FileNotFoundException {
140         new InstallMultiple().addFile(TEST_APK_TEST_ONLY).addArg("-t").run();
141         runDeviceTests(TEST_PACKAGE, TEST_CLASS,
142                 "testGlobalHiddenSettingsKeysReadableWithoutAnnotation");
143     }
144 
145     @Test
testSettingsKeysNotReadableForAfterR()146     public void testSettingsKeysNotReadableForAfterR()
147             throws DeviceNotAvailableException, FileNotFoundException {
148         new InstallMultiple().addFile(TEST_APK_TARGET_S).run();
149         runDeviceTests(TEST_PACKAGE, TEST_CLASS,
150                 "testSettingsKeysNotReadableForAfterR");
151     }
152 
153     @Test
testSettingsKeysReadableForRMinus()154     public void testSettingsKeysReadableForRMinus()
155             throws DeviceNotAvailableException, FileNotFoundException {
156         new InstallMultiple().addFile(TEST_APK_TARGET_R).run();
157         runDeviceTests(TEST_PACKAGE, TEST_CLASS,
158                 "testSettingsKeysReadableForRMinus");
159         new InstallMultiple().addFile(TEST_APK_TARGET_Q).run();
160         runDeviceTests(TEST_PACKAGE, TEST_CLASS,
161                 "testSettingsKeysReadableForRMinus");
162     }
163 
164     @Test
testQueryGlobalSettingsNoHiddenKeysWithoutAnnotation()165     public void testQueryGlobalSettingsNoHiddenKeysWithoutAnnotation()
166             throws DeviceNotAvailableException {
167         runDeviceTests(TEST_PACKAGE, TEST_CLASS,
168                 "testQueryGlobalSettingsNoHiddenKeysWithoutAnnotation");
169     }
170 
171     @Test
testQuerySystemSettingsNoHiddenKeysWithoutAnnotation()172     public void testQuerySystemSettingsNoHiddenKeysWithoutAnnotation()
173             throws DeviceNotAvailableException {
174         runDeviceTests(TEST_PACKAGE, TEST_CLASS,
175                 "testQuerySystemSettingsNoHiddenKeysWithoutAnnotation");
176     }
177 
178     @Test
testQuerySecureSettingsNoHiddenKeysWithoutAnnotation()179     public void testQuerySecureSettingsNoHiddenKeysWithoutAnnotation()
180             throws DeviceNotAvailableException {
181         runDeviceTests(TEST_PACKAGE, TEST_CLASS,
182                 "testQuerySecureSettingsNoHiddenKeysWithoutAnnotation");
183     }
184 
185     @Test
testListGlobalSettingsNoHiddenKeysWithoutAnnotation()186     public void testListGlobalSettingsNoHiddenKeysWithoutAnnotation()
187             throws DeviceNotAvailableException {
188         runDeviceTests(TEST_PACKAGE, TEST_CLASS,
189                 "testListGlobalSettingsNoHiddenKeysWithoutAnnotation");
190     }
191 
192     @Test
testListSystemSettingsNoHiddenKeysWithoutAnnotation()193     public void testListSystemSettingsNoHiddenKeysWithoutAnnotation()
194             throws DeviceNotAvailableException {
195         runDeviceTests(TEST_PACKAGE, TEST_CLASS,
196                 "testListSystemSettingsNoHiddenKeysWithoutAnnotation");
197     }
198 
199     @Test
testListSecureSettingsNoHiddenKeysWithoutAnnotation()200     public void testListSecureSettingsNoHiddenKeysWithoutAnnotation()
201             throws DeviceNotAvailableException {
202         runDeviceTests(TEST_PACKAGE, TEST_CLASS,
203                 "testListSecureSettingsNoHiddenKeysWithoutAnnotation");
204     }
205 }
206