1 /*
2  * Copyright (C) 2015 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.devicepolicy;
18 
19 import com.android.ddmlib.Log.LogLevel;
20 import com.android.tradefed.device.DeviceNotAvailableException;
21 import com.android.tradefed.log.LogUtil.CLog;
22 
23 import java.io.File;
24 
25 /**
26  * Set of tests for usecases that apply to profile and device owner.
27  * This class is the base class of MixedProfileOwnerTest and MixedDeviceOwnerTest and is abstract
28  * to avoid running spurious tests.
29  */
30 public abstract class DeviceAndProfileOwnerTest extends BaseDevicePolicyTest {
31 
32     protected static final String DEVICE_ADMIN_PKG = "com.android.cts.deviceandprofileowner";
33     protected static final String DEVICE_ADMIN_APK = "CtsDeviceAndProfileOwnerApp.apk";
34     protected static final String ADMIN_RECEIVER_TEST_CLASS
35             = ".BaseDeviceAdminTest$BasicAdminReceiver";
36 
37     private static final String PERMISSIONS_APP_PKG = "com.android.cts.permissionapp";
38     private static final String PERMISSIONS_APP_APK = "CtsPermissionApp.apk";
39 
40     private static final String SIMPLE_PRE_M_APP_PKG = "com.android.cts.launcherapps.simplepremapp";
41     private static final String SIMPLE_PRE_M_APP_APK = "CtsSimplePreMApp.apk";
42 
43     private static final String CERT_INSTALLER_PKG = "com.android.cts.certinstaller";
44     private static final String CERT_INSTALLER_APK = "CtsCertInstallerApp.apk";
45 
46     private static final String TEST_APP_APK = "CtsSimpleApp.apk";
47     private static final String TEST_APP_PKG = "com.android.cts.launcherapps.simpleapp";
48     private static final String TEST_APP_LOCATION = "/data/local/tmp/";
49 
50     private static final String PACKAGE_INSTALLER_PKG = "com.android.cts.packageinstaller";
51     private static final String PACKAGE_INSTALLER_APK = "CtsPackageInstallerApp.apk";
52 
53     protected static final int USER_OWNER = 0;
54 
55     private static final String ADD_RESTRICTION_COMMAND = "add-restriction";
56     private static final String CLEAR_RESTRICTION_COMMAND = "clear-restriction";
57 
58     // ID of the user all tests are run as. For device owner this will be 0, for profile owner it
59     // is the user id of the created profile.
60     protected int mUserId;
61 
62     @Override
tearDown()63     protected void tearDown() throws Exception {
64         if (mHasFeature) {
65             getDevice().uninstallPackage(DEVICE_ADMIN_PKG);
66             getDevice().uninstallPackage(PERMISSIONS_APP_PKG);
67             getDevice().uninstallPackage(SIMPLE_PRE_M_APP_PKG);
68             getDevice().uninstallPackage(CERT_INSTALLER_PKG);
69         }
70         super.tearDown();
71     }
72 
testApplicationRestrictions()73     public void testApplicationRestrictions() throws Exception {
74         if (!mHasFeature) {
75             return;
76         }
77         executeDeviceTestClass(".ApplicationRestrictionsTest");
78     }
79 
testPermissionGrant()80     public void testPermissionGrant() throws Exception {
81         if (!mHasFeature) {
82             return;
83         }
84         installAppAsUser(PERMISSIONS_APP_APK, mUserId);
85         executeDeviceTestMethod(".PermissionsTest", "testPermissionGrantState");
86     }
87 
testPermissionPolicy()88     public void testPermissionPolicy() throws Exception {
89         if (!mHasFeature) {
90             return;
91         }
92         installAppAsUser(PERMISSIONS_APP_APK, mUserId);
93         executeDeviceTestMethod(".PermissionsTest", "testPermissionPolicy");
94     }
95 
testPermissionMixedPolicies()96     public void testPermissionMixedPolicies() throws Exception {
97         if (!mHasFeature) {
98             return;
99         }
100         installAppAsUser(PERMISSIONS_APP_APK, mUserId);
101         executeDeviceTestMethod(".PermissionsTest", "testPermissionMixedPolicies");
102     }
103 
testPermissionPrompts()104     public void testPermissionPrompts() throws Exception {
105         if (!mHasFeature) {
106             return;
107         }
108         installAppAsUser(PERMISSIONS_APP_APK, mUserId);
109         executeDeviceTestMethod(".PermissionsTest", "testPermissionPrompts");
110     }
111 
testPermissionAppUpdate()112     public void testPermissionAppUpdate() throws Exception {
113         if (!mHasFeature) {
114             return;
115         }
116         installAppAsUser(PERMISSIONS_APP_APK, mUserId);
117         executeDeviceTestMethod(".PermissionsTest", "testPermissionUpdate_setDeniedState");
118         executeDeviceTestMethod(".PermissionsTest", "testPermissionUpdate_checkDenied");
119         installAppAsUser(PERMISSIONS_APP_APK, mUserId);
120         executeDeviceTestMethod(".PermissionsTest", "testPermissionUpdate_checkDenied");
121 
122         assertNull(getDevice().uninstallPackage(PERMISSIONS_APP_PKG));
123         installAppAsUser(PERMISSIONS_APP_APK, mUserId);
124         executeDeviceTestMethod(".PermissionsTest", "testPermissionUpdate_setGrantedState");
125         executeDeviceTestMethod(".PermissionsTest", "testPermissionUpdate_checkGranted");
126         installAppAsUser(PERMISSIONS_APP_APK, mUserId);
127         executeDeviceTestMethod(".PermissionsTest", "testPermissionUpdate_checkGranted");
128 
129         assertNull(getDevice().uninstallPackage(PERMISSIONS_APP_PKG));
130         installAppAsUser(PERMISSIONS_APP_APK, mUserId);
131         executeDeviceTestMethod(".PermissionsTest", "testPermissionUpdate_setAutoDeniedPolicy");
132         executeDeviceTestMethod(".PermissionsTest", "testPermissionUpdate_checkDenied");
133         installAppAsUser(PERMISSIONS_APP_APK, mUserId);
134         executeDeviceTestMethod(".PermissionsTest", "testPermissionUpdate_checkDenied");
135 
136         assertNull(getDevice().uninstallPackage(PERMISSIONS_APP_PKG));
137         installAppAsUser(PERMISSIONS_APP_APK, mUserId);
138         executeDeviceTestMethod(".PermissionsTest", "testPermissionUpdate_setAutoGrantedPolicy");
139         executeDeviceTestMethod(".PermissionsTest", "testPermissionUpdate_checkGranted");
140         installAppAsUser(PERMISSIONS_APP_APK, mUserId);
141         executeDeviceTestMethod(".PermissionsTest", "testPermissionUpdate_checkGranted");
142     }
143 
testPermissionGrantPreMApp()144     public void testPermissionGrantPreMApp() throws Exception {
145         if (!mHasFeature) {
146             return;
147         }
148         installAppAsUser(SIMPLE_PRE_M_APP_APK, mUserId);
149         executeDeviceTestMethod(".PermissionsTest", "testPermissionGrantStatePreMApp");
150     }
151 
testPersistentIntentResolving()152     public void testPersistentIntentResolving() throws Exception {
153         if (!mHasFeature) {
154             return;
155         }
156         executeDeviceTestClass(".PersistentIntentResolvingTest");
157     }
158 
testScreenCaptureDisabled()159     public void testScreenCaptureDisabled() throws Exception {
160         if (!mHasFeature) {
161             return;
162         }
163         // We need to ensure that the policy is deactivated for the device owner case, so making
164         // sure the second test is run even if the first one fails
165         try {
166             executeDeviceTestMethod(".ScreenCaptureDisabledTest",
167                     "testSetScreenCaptureDisabled_true");
168         } finally {
169             executeDeviceTestMethod(".ScreenCaptureDisabledTest",
170                     "testSetScreenCaptureDisabled_false");
171         }
172     }
173 
testApplicationHidden()174     public void testApplicationHidden() throws Exception {
175         if (!mHasFeature) {
176             return;
177         }
178         installAppAsUser(PERMISSIONS_APP_APK, mUserId);
179         executeDeviceTestClass(".ApplicationHiddenTest");
180     }
181 
testAccountManagement()182     public void testAccountManagement() throws Exception {
183         if (!mHasFeature) {
184             return;
185         }
186 
187         executeDeviceTestClass(".AccountManagementTest");
188 
189         // Send a home intent to dismiss an error dialog.
190         String command = "am start -a android.intent.action.MAIN -c android.intent.category.HOME";
191         CLog.logAndDisplay(LogLevel.INFO, "Output for command " + command + ": "
192                 + getDevice().executeShellCommand(command));
193     }
194 
testDelegatedCertInstaller()195     public void testDelegatedCertInstaller() throws Exception {
196         if (!mHasFeature) {
197             return;
198         }
199         installAppAsUser(CERT_INSTALLER_APK, mUserId);
200         installAppAsUser(DEVICE_ADMIN_APK, USER_OWNER);
201         setDeviceAdmin(DEVICE_ADMIN_PKG + "/.PrimaryUserDeviceAdmin");
202 
203         final String adminHelperClass = ".PrimaryUserAdminHelper";
204         try {
205             // Set a non-empty device lockscreen password, which is a precondition for installing
206             // private key pairs.
207             assertTrue("Set lockscreen password failed", runDeviceTestsAsUser(DEVICE_ADMIN_PKG,
208                     adminHelperClass, "testSetPassword", 0 /* user 0 */));
209             assertTrue("DelegatedCertInstaller failed", runDeviceTestsAsUser(DEVICE_ADMIN_PKG,
210                     ".DelegatedCertInstallerTest", mUserId));
211         } finally {
212             // Reset lockscreen password and remove device admin.
213             assertTrue("Clear lockscreen password failed", runDeviceTestsAsUser(DEVICE_ADMIN_PKG,
214                     adminHelperClass, "testClearPassword", 0 /* user 0 */));
215             assertTrue("Clear device admin failed", runDeviceTestsAsUser(DEVICE_ADMIN_PKG,
216                     adminHelperClass, "testClearDeviceAdmin", 0 /* user 0 */));
217         }
218     }
219 
testPackageInstallUserRestrictions()220     public void testPackageInstallUserRestrictions() throws Exception {
221         // UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES
222         final String DISALLOW_INSTALL_UNKNOWN_SOURCES = "no_install_unknown_sources";
223         final String UNKNOWN_SOURCES_SETTING = "install_non_market_apps";
224         final String SECURE_SETTING_CATEGORY = "secure";
225         final File apk = mCtsBuild.getTestApp(TEST_APP_APK);
226         String unknownSourceSetting = null;
227         try {
228             // Install the test and prepare the test apk.
229             installApp(PACKAGE_INSTALLER_APK);
230             assertTrue(getDevice().pushFile(apk, TEST_APP_LOCATION + apk.getName()));
231 
232             // Add restrictions and test if we can install the apk.
233             getDevice().uninstallPackage(TEST_APP_PKG);
234             changeUserRestrictionForUser(DISALLOW_INSTALL_UNKNOWN_SOURCES,
235                     ADD_RESTRICTION_COMMAND, mUserId);
236             assertTrue(runDeviceTestsAsUser(PACKAGE_INSTALLER_PKG, ".ManualPackageInstallTest",
237                     "testManualInstallBlocked", mUserId));
238 
239             // Clear restrictions and test if we can install the apk.
240             changeUserRestrictionForUser(DISALLOW_INSTALL_UNKNOWN_SOURCES,
241                     CLEAR_RESTRICTION_COMMAND, mUserId);
242 
243             // Enable Unknown sources in Settings.
244             unknownSourceSetting =
245                     getSettings(SECURE_SETTING_CATEGORY, UNKNOWN_SOURCES_SETTING, mUserId);
246             putSettings(SECURE_SETTING_CATEGORY, UNKNOWN_SOURCES_SETTING, "1", mUserId);
247             assertEquals("1",
248                     getSettings(SECURE_SETTING_CATEGORY, UNKNOWN_SOURCES_SETTING, mUserId));
249             assertTrue(runDeviceTestsAsUser(PACKAGE_INSTALLER_PKG, ".ManualPackageInstallTest",
250                     "testManualInstallSucceeded", mUserId));
251         } finally {
252             String command = "rm " + TEST_APP_LOCATION + apk.getName();
253             getDevice().executeShellCommand(command);
254             getDevice().uninstallPackage(TEST_APP_PKG);
255             getDevice().uninstallPackage(PACKAGE_INSTALLER_APK);
256             if (unknownSourceSetting != null) {
257                 putSettings(SECURE_SETTING_CATEGORY, UNKNOWN_SOURCES_SETTING, unknownSourceSetting,
258                         mUserId);
259             }
260         }
261     }
262 
executeDeviceTestClass(String className)263     protected void executeDeviceTestClass(String className) throws Exception {
264         assertTrue(runDeviceTestsAsUser(DEVICE_ADMIN_PKG, className, mUserId));
265     }
266 
executeDeviceTestMethod(String className, String testName)267     protected void executeDeviceTestMethod(String className, String testName) throws Exception {
268         assertTrue(runDeviceTestsAsUser(DEVICE_ADMIN_PKG, className, testName, mUserId));
269     }
270 
changeUserRestrictionForUser(String key, String command, int userId)271     private void changeUserRestrictionForUser(String key, String command, int userId)
272             throws DeviceNotAvailableException {
273         String adbCommand = "am start -W --user " + userId
274                 + " -c android.intent.category.DEFAULT "
275                 + " --es extra-command " + command
276                 + " --es extra-restriction-key " + key
277                 + " " + DEVICE_ADMIN_PKG + "/.UserRestrictionActivity";
278         String commandOutput = getDevice().executeShellCommand(adbCommand);
279         CLog.logAndDisplay(LogLevel.INFO,
280                 "Output for command " + adbCommand + ": " + commandOutput);
281         assertTrue("Command was expected to succeed " + commandOutput,
282                 commandOutput.contains("Status: ok"));
283     }
284 }
285