• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.cts.migration.MigrationHelper;
20 import com.android.ddmlib.Log.LogLevel;
21 import com.android.tradefed.device.DeviceNotAvailableException;
22 import com.android.tradefed.log.LogUtil.CLog;
23 
24 import java.io.File;
25 import java.io.FileNotFoundException;
26 
27 /**
28  * Set of tests for use cases that apply to profile and device owner.
29  * This class is the base class of MixedProfileOwnerTest, MixedDeviceOwnerTest and
30  * MixedManagedProfileOwnerTest and is abstract to avoid running spurious tests.
31  *
32  * NOTE: Not all tests are executed in the subclasses.  Sometimes, if a test is not applicable to
33  * a subclass, they override it with an empty method.
34  */
35 public abstract class DeviceAndProfileOwnerTest extends BaseDevicePolicyTest {
36 
37     protected static final String DEVICE_ADMIN_PKG = "com.android.cts.deviceandprofileowner";
38     protected static final String DEVICE_ADMIN_APK = "CtsDeviceAndProfileOwnerApp.apk";
39     protected static final String ADMIN_RECEIVER_TEST_CLASS
40             = ".BaseDeviceAdminTest$BasicAdminReceiver";
41 
42     private static final String INTENT_RECEIVER_PKG = "com.android.cts.intent.receiver";
43     private static final String INTENT_RECEIVER_APK = "CtsIntentReceiverApp.apk";
44 
45     private static final String INTENT_SENDER_PKG = "com.android.cts.intent.sender";
46     private static final String INTENT_SENDER_APK = "CtsIntentSenderApp.apk";
47 
48     private static final String PERMISSIONS_APP_PKG = "com.android.cts.permissionapp";
49     private static final String PERMISSIONS_APP_APK = "CtsPermissionApp.apk";
50 
51     private static final String SIMPLE_PRE_M_APP_PKG = "com.android.cts.launcherapps.simplepremapp";
52     private static final String SIMPLE_PRE_M_APP_APK = "CtsSimplePreMApp.apk";
53 
54     private static final String APP_RESTRICTIONS_MANAGING_APP_PKG
55             = "com.android.cts.apprestrictions.managingapp";
56     private static final String APP_RESTRICTIONS_MANAGING_APP_APK
57             = "CtsAppRestrictionsManagingApp.apk";
58     private static final String APP_RESTRICTIONS_TARGET_APP_PKG
59             = "com.android.cts.apprestrictions.targetapp";
60     private static final String APP_RESTRICTIONS_TARGET_APP_APK = "CtsAppRestrictionsTargetApp.apk";
61 
62     private static final String CERT_INSTALLER_PKG = "com.android.cts.certinstaller";
63     private static final String CERT_INSTALLER_APK = "CtsCertInstallerApp.apk";
64 
65     private static final String TEST_APP_APK = "CtsSimpleApp.apk";
66     private static final String TEST_APP_PKG = "com.android.cts.launcherapps.simpleapp";
67     private static final String TEST_APP_LOCATION = "/data/local/tmp/";
68 
69     private static final String PACKAGE_INSTALLER_PKG = "com.android.cts.packageinstaller";
70     private static final String PACKAGE_INSTALLER_APK = "CtsPackageInstallerApp.apk";
71 
72     private static final String ACCOUNT_MANAGEMENT_PKG
73             = "com.android.cts.devicepolicy.accountmanagement";
74     private static final String ACCOUNT_MANAGEMENT_APK = "CtsAccountManagementDevicePolicyApp.apk";
75 
76     private static final String VPN_APP_PKG = "com.android.cts.vpnfirewall";
77     private static final String VPN_APP_APK = "CtsVpnFirewallApp.apk";
78 
79     private static final String COMMAND_ADD_USER_RESTRICTION = "add-restriction";
80     private static final String COMMAND_CLEAR_USER_RESTRICTION = "clear-restriction";
81     private static final String COMMAND_BLOCK_ACCOUNT_TYPE = "block-accounttype";
82     private static final String COMMAND_UNBLOCK_ACCOUNT_TYPE = "unblock-accounttype";
83 
84     private static final String DISALLOW_MODIFY_ACCOUNTS = "no_modify_accounts";
85     private static final String ACCOUNT_TYPE
86             = "com.android.cts.devicepolicy.accountmanagement.account.type";
87 
88     private static final String CUSTOMIZATION_APP_PKG = "com.android.cts.customizationapp";
89     private static final String CUSTOMIZATION_APP_APK = "CtsCustomizationApp.apk";
90 
91     // ID of the user all tests are run as. For device owner this will be the primary user, for
92     // profile owner it is the user id of the created profile.
93     protected int mUserId;
94 
95     @Override
tearDown()96     protected void tearDown() throws Exception {
97         if (mHasFeature) {
98             getDevice().uninstallPackage(DEVICE_ADMIN_PKG);
99             getDevice().uninstallPackage(PERMISSIONS_APP_PKG);
100             getDevice().uninstallPackage(SIMPLE_PRE_M_APP_PKG);
101             getDevice().uninstallPackage(APP_RESTRICTIONS_MANAGING_APP_PKG);
102             getDevice().uninstallPackage(APP_RESTRICTIONS_TARGET_APP_PKG);
103             getDevice().uninstallPackage(CERT_INSTALLER_PKG);
104             getDevice().uninstallPackage(ACCOUNT_MANAGEMENT_PKG);
105             getDevice().uninstallPackage(VPN_APP_PKG);
106             getDevice().uninstallPackage(INTENT_RECEIVER_PKG);
107             getDevice().uninstallPackage(INTENT_SENDER_PKG);
108             getDevice().uninstallPackage(CUSTOMIZATION_APP_PKG);
109 
110             // Press the HOME key to close any alart dialog that may be shown.
111             getDevice().executeShellCommand("input keyevent 3");
112         }
113         super.tearDown();
114     }
115 
testResetPassword()116     public void testResetPassword() throws Exception {
117         if (!mHasFeature) {
118             return;
119         }
120         executeDeviceTestClass(".ResetPasswordTest");
121     }
122 
testApplicationRestrictions()123     public void testApplicationRestrictions() throws Exception {
124         if (!mHasFeature) {
125             return;
126         }
127 
128         installAppAsUser(APP_RESTRICTIONS_MANAGING_APP_APK, mUserId);
129         installAppAsUser(APP_RESTRICTIONS_TARGET_APP_APK, mUserId);
130 
131         try {
132             // Only the DPC can manage app restrictions by default.
133             executeDeviceTestClass(".ApplicationRestrictionsTest");
134             executeAppRestrictionsManagingPackageTest("testCannotManageAppRestrictions");
135 
136             // Letting the APP_RESTRICTIONS_MANAGING_APP_PKG manage app restrictions too.
137             changeApplicationRestrictionsManagingPackage(APP_RESTRICTIONS_MANAGING_APP_PKG);
138             executeAppRestrictionsManagingPackageTest("testCanManageAppRestrictions");
139             executeAppRestrictionsManagingPackageTest("testSettingComponentNameThrowsException");
140 
141             // The DPC should still be able to manage app restrictions normally.
142             executeDeviceTestClass(".ApplicationRestrictionsTest");
143 
144             // The app shouldn't be able to manage app restrictions for other users.
145             int parentUserId = getPrimaryUser();
146             if (parentUserId != mUserId) {
147                 installAppAsUser(APP_RESTRICTIONS_MANAGING_APP_APK, parentUserId);
148                 installAppAsUser(APP_RESTRICTIONS_TARGET_APP_APK, parentUserId);
149                 assertTrue(runDeviceTestsAsUser(
150                         APP_RESTRICTIONS_MANAGING_APP_PKG, ".ApplicationRestrictionsManagerTest",
151                         "testCannotManageAppRestrictions", parentUserId));
152             }
153 
154             // Revoking the permission for APP_RESTRICTIONS_MANAGING_APP_PKG to manage restrictions.
155             changeApplicationRestrictionsManagingPackage(null);
156             executeAppRestrictionsManagingPackageTest("testCannotManageAppRestrictions");
157 
158             // The DPC should still be able to manage app restrictions normally.
159             executeDeviceTestClass(".ApplicationRestrictionsTest");
160         } finally {
161             changeApplicationRestrictionsManagingPackage(null);
162         }
163     }
164 
testPermissionGrant()165     public void testPermissionGrant() throws Exception {
166         if (!mHasFeature) {
167             return;
168         }
169         installAppPermissionAppAsUser();
170         executeDeviceTestMethod(".PermissionsTest", "testPermissionGrantState");
171     }
172 
testAlwaysOnVpn()173     public void testAlwaysOnVpn() throws Exception {
174         if (!mHasFeature) {
175             return;
176         }
177         installAppAsUser(VPN_APP_APK, mUserId);
178         executeDeviceTestClass(".AlwaysOnVpnTest");
179     }
180 
testAlwaysOnVpnLockDown()181     public void testAlwaysOnVpnLockDown() throws Exception {
182         if (!mHasFeature) {
183             return;
184         }
185 
186         installAppAsUser(VPN_APP_APK, mUserId);
187         try {
188             executeDeviceTestMethod(".AlwaysOnVpnMultiStageTest", "testAlwaysOnSet");
189             forceStopPackageForUser(VPN_APP_PKG, mUserId);
190             executeDeviceTestMethod(".AlwaysOnVpnMultiStageTest", "testNetworkBlocked");
191         } finally {
192             executeDeviceTestMethod(".AlwaysOnVpnMultiStageTest", "testCleanup");
193         }
194     }
195 
testAlwaysOnVpnPackageUninstalled()196     public void testAlwaysOnVpnPackageUninstalled() throws Exception {
197         if (!mHasFeature) {
198             return;
199         }
200 
201         installAppAsUser(VPN_APP_APK, mUserId);
202         try {
203             executeDeviceTestMethod(".AlwaysOnVpnMultiStageTest", "testAlwaysOnSet");
204             getDevice().uninstallPackage(VPN_APP_PKG);
205             executeDeviceTestMethod(".AlwaysOnVpnMultiStageTest", "testAlwaysOnVpnDisabled");
206             executeDeviceTestMethod(".AlwaysOnVpnMultiStageTest", "testSetNonExistingPackage");
207         } finally {
208             executeDeviceTestMethod(".AlwaysOnVpnMultiStageTest", "testCleanup");
209         }
210     }
211 
testPermissionPolicy()212     public void testPermissionPolicy() throws Exception {
213         if (!mHasFeature) {
214             return;
215         }
216         installAppPermissionAppAsUser();
217         executeDeviceTestMethod(".PermissionsTest", "testPermissionPolicy");
218     }
219 
testPermissionMixedPolicies()220     public void testPermissionMixedPolicies() throws Exception {
221         if (!mHasFeature) {
222             return;
223         }
224         installAppPermissionAppAsUser();
225         executeDeviceTestMethod(".PermissionsTest", "testPermissionMixedPolicies");
226     }
227 
228     // Test flakey; suppressed.
229 //    public void testPermissionPrompts() throws Exception {
230 //        if (!mHasFeature) {
231 //            return;
232 //        }
233 //        installAppPermissionAppAsUser();
234 //        executeDeviceTestMethod(".PermissionsTest", "testPermissionPrompts");
235 //    }
236 
testPermissionAppUpdate()237     public void testPermissionAppUpdate() throws Exception {
238         if (!mHasFeature) {
239             return;
240         }
241         installAppPermissionAppAsUser();
242         executeDeviceTestMethod(".PermissionsTest", "testPermissionUpdate_setDeniedState");
243         executeDeviceTestMethod(".PermissionsTest", "testPermissionUpdate_checkDenied");
244         installAppPermissionAppAsUser();
245         executeDeviceTestMethod(".PermissionsTest", "testPermissionUpdate_checkDenied");
246 
247         assertNull(getDevice().uninstallPackage(PERMISSIONS_APP_PKG));
248         installAppPermissionAppAsUser();
249         executeDeviceTestMethod(".PermissionsTest", "testPermissionUpdate_setGrantedState");
250         executeDeviceTestMethod(".PermissionsTest", "testPermissionUpdate_checkGranted");
251         installAppPermissionAppAsUser();
252         executeDeviceTestMethod(".PermissionsTest", "testPermissionUpdate_checkGranted");
253 
254         assertNull(getDevice().uninstallPackage(PERMISSIONS_APP_PKG));
255         installAppPermissionAppAsUser();
256         executeDeviceTestMethod(".PermissionsTest", "testPermissionUpdate_setAutoDeniedPolicy");
257         executeDeviceTestMethod(".PermissionsTest", "testPermissionUpdate_checkDenied");
258         installAppPermissionAppAsUser();
259         executeDeviceTestMethod(".PermissionsTest", "testPermissionUpdate_checkDenied");
260 
261         assertNull(getDevice().uninstallPackage(PERMISSIONS_APP_PKG));
262         installAppPermissionAppAsUser();
263         executeDeviceTestMethod(".PermissionsTest", "testPermissionUpdate_setAutoGrantedPolicy");
264         executeDeviceTestMethod(".PermissionsTest", "testPermissionUpdate_checkGranted");
265         installAppPermissionAppAsUser();
266         executeDeviceTestMethod(".PermissionsTest", "testPermissionUpdate_checkGranted");
267     }
268 
testPermissionGrantPreMApp()269     public void testPermissionGrantPreMApp() throws Exception {
270         if (!mHasFeature) {
271             return;
272         }
273         installAppAsUser(SIMPLE_PRE_M_APP_APK, mUserId);
274         executeDeviceTestMethod(".PermissionsTest", "testPermissionGrantStatePreMApp");
275     }
276 
testPersistentIntentResolving()277     public void testPersistentIntentResolving() throws Exception {
278         if (!mHasFeature) {
279             return;
280         }
281         executeDeviceTestClass(".PersistentIntentResolvingTest");
282     }
283 
testScreenCaptureDisabled()284     public void testScreenCaptureDisabled() throws Exception {
285         if (!mHasFeature) {
286             return;
287         }
288         // We need to ensure that the policy is deactivated for the device owner case, so making
289         // sure the second test is run even if the first one fails
290         try {
291             executeDeviceTestMethod(".ScreenCaptureDisabledTest",
292                     "testSetScreenCaptureDisabled_true");
293         } finally {
294             executeDeviceTestMethod(".ScreenCaptureDisabledTest",
295                     "testSetScreenCaptureDisabled_false");
296         }
297     }
298 
testSupportMessage()299     public void testSupportMessage() throws Exception {
300         if (!mHasFeature) {
301             return;
302         }
303         installAppAsUser(APP_RESTRICTIONS_MANAGING_APP_APK, mUserId);
304         executeDeviceTestClass(".SupportMessageTest");
305     }
306 
testApplicationHidden()307     public void testApplicationHidden() throws Exception {
308         if (!mHasFeature) {
309             return;
310         }
311         installAppPermissionAppAsUser();
312         executeDeviceTestClass(".ApplicationHiddenTest");
313     }
314 
testAccountManagement_deviceAndProfileOwnerAlwaysAllowed()315     public void testAccountManagement_deviceAndProfileOwnerAlwaysAllowed() throws Exception {
316         if (!mHasFeature) {
317             return;
318         }
319 
320         installAppAsUser(ACCOUNT_MANAGEMENT_APK, mUserId);
321         executeDeviceTestClass(".DpcAllowedAccountManagementTest");
322     }
323 
testAccountManagement_userRestrictionAddAccount()324     public void testAccountManagement_userRestrictionAddAccount() throws Exception {
325         if (!mHasFeature) {
326             return;
327         }
328 
329         installAppAsUser(ACCOUNT_MANAGEMENT_APK, mUserId);
330         try {
331             changeUserRestrictionForUser(DISALLOW_MODIFY_ACCOUNTS, COMMAND_ADD_USER_RESTRICTION,
332                     mUserId);
333             executeAccountTest("testAddAccount_blocked");
334         } finally {
335             // Ensure we clear the user restriction
336             changeUserRestrictionForUser(DISALLOW_MODIFY_ACCOUNTS, COMMAND_CLEAR_USER_RESTRICTION,
337                     mUserId);
338         }
339         executeAccountTest("testAddAccount_allowed");
340     }
341 
testAccountManagement_userRestrictionRemoveAccount()342     public void testAccountManagement_userRestrictionRemoveAccount() throws Exception {
343         if (!mHasFeature) {
344             return;
345         }
346 
347         installAppAsUser(ACCOUNT_MANAGEMENT_APK, mUserId);
348         try {
349             changeUserRestrictionForUser(DISALLOW_MODIFY_ACCOUNTS, COMMAND_ADD_USER_RESTRICTION,
350                     mUserId);
351             executeAccountTest("testRemoveAccount_blocked");
352         } finally {
353             // Ensure we clear the user restriction
354             changeUserRestrictionForUser(DISALLOW_MODIFY_ACCOUNTS, COMMAND_CLEAR_USER_RESTRICTION,
355                     mUserId);
356         }
357         executeAccountTest("testRemoveAccount_allowed");
358     }
359 
testAccountManagement_disabledAddAccount()360     public void testAccountManagement_disabledAddAccount() throws Exception {
361         if (!mHasFeature) {
362             return;
363         }
364 
365         installAppAsUser(ACCOUNT_MANAGEMENT_APK, mUserId);
366         try {
367             changeAccountManagement(COMMAND_BLOCK_ACCOUNT_TYPE, ACCOUNT_TYPE, mUserId);
368             executeAccountTest("testAddAccount_blocked");
369         } finally {
370             // Ensure we remove account management policies
371             changeAccountManagement(COMMAND_UNBLOCK_ACCOUNT_TYPE, ACCOUNT_TYPE, mUserId);
372         }
373         executeAccountTest("testAddAccount_allowed");
374     }
375 
testAccountManagement_disabledRemoveAccount()376     public void testAccountManagement_disabledRemoveAccount() throws Exception {
377         if (!mHasFeature) {
378             return;
379         }
380 
381         installAppAsUser(ACCOUNT_MANAGEMENT_APK, mUserId);
382         try {
383             changeAccountManagement(COMMAND_BLOCK_ACCOUNT_TYPE, ACCOUNT_TYPE, mUserId);
384             executeAccountTest("testRemoveAccount_blocked");
385         } finally {
386             // Ensure we remove account management policies
387             changeAccountManagement(COMMAND_UNBLOCK_ACCOUNT_TYPE, ACCOUNT_TYPE, mUserId);
388         }
389         executeAccountTest("testRemoveAccount_allowed");
390     }
391 
testDelegatedCertInstaller()392     public void testDelegatedCertInstaller() throws Exception {
393         if (!mHasFeature) {
394             return;
395         }
396 
397         installAppAsUser(CERT_INSTALLER_APK, mUserId);
398 
399         boolean isManagedProfile = (mPrimaryUserId != mUserId);
400 
401         try {
402             // Set a non-empty device lockscreen password, which is a precondition for installing
403             // private key pairs.
404             assertTrue("Set lockscreen password failed", runDeviceTestsAsUser(DEVICE_ADMIN_PKG,
405                     ".ResetPasswordHelper", "testSetPassword", mUserId));
406             assertTrue("DelegatedCertInstaller failed", runDeviceTestsAsUser(DEVICE_ADMIN_PKG,
407                     ".DelegatedCertInstallerTest", mUserId));
408         } finally {
409             if (!isManagedProfile) {
410                 // Skip managed profile as dpm doesn't allow clear password
411                 assertTrue("Clear lockscreen password failed", runDeviceTestsAsUser(DEVICE_ADMIN_PKG,
412                         ".ResetPasswordHelper", "testClearPassword", mUserId));
413             }
414         }
415     }
416 
417     // Sets restrictions and launches non-admin app, that tries to set wallpaper.
418     // Non-admin apps must not violate any user restriction.
testSetWallpaper_disallowed()419     public void testSetWallpaper_disallowed() throws Exception {
420         // UserManager.DISALLOW_SET_WALLPAPER
421         final String DISALLOW_SET_WALLPAPER = "no_set_wallpaper";
422         if (!mHasFeature) {
423             return;
424         }
425 
426         installAppAsUser(CUSTOMIZATION_APP_APK, mUserId);
427         try {
428             changeUserRestrictionForUser(DISALLOW_SET_WALLPAPER, COMMAND_ADD_USER_RESTRICTION,
429                     mUserId);
430             assertTrue(runDeviceTestsAsUser(CUSTOMIZATION_APP_PKG, ".CustomizationTest",
431                 "testSetWallpaper_disallowed", mUserId));
432         } finally {
433             changeUserRestrictionForUser(DISALLOW_SET_WALLPAPER, COMMAND_CLEAR_USER_RESTRICTION,
434                     mUserId);
435         }
436     }
437 
438     // Runs test with admin privileges. The test methods set all the tested restrictions
439     // inside. But these restrictions must have no effect on the device/profile owner behavior.
testDisallowSetWallpaper_allowed()440     public void testDisallowSetWallpaper_allowed() throws Exception {
441         if (!mHasFeature) {
442             return;
443         }
444         executeDeviceTestMethod(".CustomizationRestrictionsTest",
445                 "testDisallowSetWallpaper_allowed");
446     }
447 
testDisallowSetUserIcon_allowed()448     public void testDisallowSetUserIcon_allowed() throws Exception {
449         if (!mHasFeature) {
450             return;
451         }
452         executeDeviceTestMethod(".CustomizationRestrictionsTest",
453                 "testDisallowSetUserIcon_allowed");
454     }
455 
testPackageInstallUserRestrictions()456     public void testPackageInstallUserRestrictions() throws Exception {
457         if (!mHasFeature) {
458             return;
459         }
460         // UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES
461         final String DISALLOW_INSTALL_UNKNOWN_SOURCES = "no_install_unknown_sources";
462         final String UNKNOWN_SOURCES_SETTING = "install_non_market_apps";
463         final String PACKAGE_VERIFIER_USER_CONSENT_SETTING = "package_verifier_user_consent";
464         final String PACKAGE_VERIFIER_ENABLE_SETTING = "package_verifier_enable";
465         final String SECURE_SETTING_CATEGORY = "secure";
466         final String GLOBAL_SETTING_CATEGORY = "global";
467         final File apk = MigrationHelper.getTestFile(mCtsBuild, TEST_APP_APK);
468         String unknownSourceSetting = null;
469         String packageVerifierEnableSetting = null;
470         String packageVerifierUserConsentSetting = null;
471         try {
472             // Install the test and prepare the test apk.
473             installAppAsUser(PACKAGE_INSTALLER_APK, mUserId);
474             assertTrue(getDevice().pushFile(apk, TEST_APP_LOCATION + apk.getName()));
475 
476             // Add restrictions and test if we can install the apk.
477             getDevice().uninstallPackage(TEST_APP_PKG);
478             changeUserRestrictionForUser(DISALLOW_INSTALL_UNKNOWN_SOURCES,
479                     COMMAND_ADD_USER_RESTRICTION, mUserId);
480             assertTrue(runDeviceTestsAsUser(PACKAGE_INSTALLER_PKG, ".ManualPackageInstallTest",
481                     "testManualInstallBlocked", mUserId));
482 
483             // Clear restrictions and test if we can install the apk.
484             changeUserRestrictionForUser(DISALLOW_INSTALL_UNKNOWN_SOURCES,
485                     COMMAND_CLEAR_USER_RESTRICTION, mUserId);
486 
487             // Enable Unknown sources in Settings.
488             unknownSourceSetting =
489                     getSettings(SECURE_SETTING_CATEGORY, UNKNOWN_SOURCES_SETTING, mUserId);
490             packageVerifierUserConsentSetting = getSettings(SECURE_SETTING_CATEGORY,
491                     PACKAGE_VERIFIER_USER_CONSENT_SETTING, mUserId);
492             packageVerifierEnableSetting = getSettings(GLOBAL_SETTING_CATEGORY,
493                     PACKAGE_VERIFIER_ENABLE_SETTING, mUserId);
494 
495             putSettings(SECURE_SETTING_CATEGORY, UNKNOWN_SOURCES_SETTING, "1", mUserId);
496             putSettings(SECURE_SETTING_CATEGORY, PACKAGE_VERIFIER_USER_CONSENT_SETTING, "-1",
497                     mUserId);
498             putSettings(GLOBAL_SETTING_CATEGORY, PACKAGE_VERIFIER_ENABLE_SETTING, "0", mUserId);
499             assertEquals("1",
500                     getSettings(SECURE_SETTING_CATEGORY, UNKNOWN_SOURCES_SETTING, mUserId));
501             assertEquals("-1", getSettings(SECURE_SETTING_CATEGORY,
502                     PACKAGE_VERIFIER_USER_CONSENT_SETTING, mUserId));
503             assertEquals("0", getSettings(GLOBAL_SETTING_CATEGORY,
504                     PACKAGE_VERIFIER_ENABLE_SETTING, mUserId));
505             assertTrue(runDeviceTestsAsUser(PACKAGE_INSTALLER_PKG, ".ManualPackageInstallTest",
506                     "testManualInstallSucceeded", mUserId));
507         } finally {
508             String command = "rm " + TEST_APP_LOCATION + apk.getName();
509             getDevice().executeShellCommand(command);
510             getDevice().uninstallPackage(TEST_APP_PKG);
511             getDevice().uninstallPackage(PACKAGE_INSTALLER_APK);
512             if (unknownSourceSetting != null) {
513                 putSettings(SECURE_SETTING_CATEGORY, UNKNOWN_SOURCES_SETTING, unknownSourceSetting,
514                         mUserId);
515             }
516             if (packageVerifierEnableSetting != null) {
517                 putSettings(GLOBAL_SETTING_CATEGORY, PACKAGE_VERIFIER_ENABLE_SETTING,
518                         packageVerifierEnableSetting, mUserId);
519             }
520             if (packageVerifierUserConsentSetting != null) {
521                 putSettings(SECURE_SETTING_CATEGORY, PACKAGE_VERIFIER_USER_CONSENT_SETTING,
522                         packageVerifierUserConsentSetting, mUserId);
523             }
524         }
525     }
526 
testAudioRestriction()527     public void testAudioRestriction() throws Exception {
528         if (!mHasFeature) {
529             return;
530         }
531         executeDeviceTestClass(".AudioRestrictionTest");
532     }
533 
testSuspendPackage()534     public void testSuspendPackage() throws Exception {
535         if (!mHasFeature) {
536             return;
537         }
538         installAppAsUser(INTENT_SENDER_APK, mUserId);
539         installAppAsUser(INTENT_RECEIVER_APK, mUserId);
540         // Suspend a testing package.
541         executeDeviceTestMethod(".SuspendPackageTest", "testSetPackagesSuspended");
542         // Verify that the package is suspended.
543         executeSuspendPackageTestMethod("testPackageSuspended");
544         // Undo the suspend.
545         executeDeviceTestMethod(".SuspendPackageTest", "testSetPackagesNotSuspended");
546         // Verify that the package is not suspended.
547         executeSuspendPackageTestMethod("testPackageNotSuspended");
548         // Verify we cannot suspend not suspendable packages.
549         executeDeviceTestMethod(".SuspendPackageTest", "testSuspendNotSuspendablePackages");
550     }
551 
testTrustAgentInfo()552     public void testTrustAgentInfo() throws Exception {
553         if (!mHasFeature) {
554             return;
555         }
556         executeDeviceTestClass(".TrustAgentInfoTest");
557     }
558 
executeDeviceTestClass(String className)559     protected void executeDeviceTestClass(String className) throws Exception {
560         assertTrue(runDeviceTestsAsUser(DEVICE_ADMIN_PKG, className, mUserId));
561     }
562 
executeDeviceTestMethod(String className, String testName)563     protected void executeDeviceTestMethod(String className, String testName) throws Exception {
564         assertTrue(runDeviceTestsAsUser(DEVICE_ADMIN_PKG, className, testName, mUserId));
565     }
566 
installAppPermissionAppAsUser()567     private void installAppPermissionAppAsUser()
568             throws FileNotFoundException, DeviceNotAvailableException {
569         installAppAsUser(PERMISSIONS_APP_APK, false, mUserId);
570     }
571 
executeSuspendPackageTestMethod(String testName)572     private void executeSuspendPackageTestMethod(String testName) throws Exception {
573         assertTrue(runDeviceTestsAsUser(INTENT_SENDER_PKG, ".SuspendPackageTest",
574                 testName, mUserId));
575     }
576 
executeAccountTest(String testName)577     private void executeAccountTest(String testName) throws DeviceNotAvailableException {
578         assertTrue(runDeviceTestsAsUser(ACCOUNT_MANAGEMENT_PKG, ".AccountManagementTest",
579                 testName, mUserId));
580         // Send a home intent to dismiss an error dialog.
581         String command = "am start -a android.intent.action.MAIN"
582                 + " -c android.intent.category.HOME";
583         CLog.i("Output for command " + command + ": " + getDevice().executeShellCommand(command));
584     }
585 
executeAppRestrictionsManagingPackageTest(String testName)586     private void executeAppRestrictionsManagingPackageTest(String testName) throws Exception {
587         assertTrue(runDeviceTestsAsUser(APP_RESTRICTIONS_MANAGING_APP_PKG,
588                 ".ApplicationRestrictionsManagerTest", testName, mUserId));
589     }
590 
changeUserRestrictionForUser(String key, String command, int userId)591     private void changeUserRestrictionForUser(String key, String command, int userId)
592             throws DeviceNotAvailableException {
593         changePolicy(command, "--es extra-restriction-key " + key, userId);
594     }
595 
changeAccountManagement(String command, String accountType, int userId)596     private void changeAccountManagement(String command, String accountType, int userId)
597             throws DeviceNotAvailableException {
598         changePolicy(command, "--es extra-account-type " + accountType, userId);
599     }
600 
changeApplicationRestrictionsManagingPackage(String packageName)601     private void changeApplicationRestrictionsManagingPackage(String packageName)
602             throws DeviceNotAvailableException {
603         String packageNameExtra = (packageName != null)
604                 ? "--es extra-package-name " + packageName : "";
605         changePolicy("set-app-restrictions-manager", packageNameExtra, mUserId);
606     }
607 
changePolicy(String command, String extras, int userId)608     private void changePolicy(String command, String extras, int userId)
609             throws DeviceNotAvailableException {
610         String adbCommand = "am start -W --user " + userId
611                 + " -c android.intent.category.DEFAULT "
612                 + " --es extra-command " + command
613                 + " " + extras
614                 + " " + DEVICE_ADMIN_PKG + "/.SetPolicyActivity";
615         String commandOutput = getDevice().executeShellCommand(adbCommand);
616         CLog.d("Output for command " + adbCommand + ": " + commandOutput);
617         assertTrue("Command was expected to succeed " + commandOutput,
618                 commandOutput.contains("Status: ok"));
619     }
620 }
621