1 /* 2 * Copyright (C) 2016 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 package com.android.cts.devicepolicy; 17 18 import static com.android.cts.devicepolicy.DeviceAdminFeaturesCheckerRule.FEATURE_MANAGED_USERS; 19 20 import static org.junit.Assert.assertTrue; 21 22 import com.android.cts.devicepolicy.DeviceAdminFeaturesCheckerRule.RequiresAdditionalFeatures; 23 import com.android.tradefed.device.DeviceNotAvailableException; 24 import com.android.tradefed.log.LogUtil.CLog; 25 26 import org.junit.Test; 27 28 import javax.annotation.Nonnull; 29 import javax.annotation.Nullable; 30 31 public final class UserRestrictionsTest extends BaseDevicePolicyTest { 32 private static final String DEVICE_ADMIN_PKG = "com.android.cts.deviceandprofileowner"; 33 private static final String DEVICE_ADMIN_APK = "CtsDeviceAndProfileOwnerApp.apk"; 34 private static final String ADMIN_RECEIVER_TEST_CLASS 35 = ".BaseDeviceAdminTest$BasicAdminReceiver"; 36 37 private static final String GLOBAL_RESTRICTIONS_TEST_CLASS = 38 "userrestrictions.ProfileGlobalRestrictionsTest"; 39 private static final String SET_GLOBAL_RESTRICTIONS_TEST = 40 "testSetProfileGlobalRestrictions"; 41 private static final String CLEAR_GLOBAL_RESTRICTIONS_TEST = 42 "testClearProfileGlobalRestrictions"; 43 private static final String ENSURE_GLOBAL_RESTRICTIONS_TEST = 44 "testProfileGlobalRestrictionsEnforced"; 45 private static final String ENSURE_NO_GLOBAL_RESTRICTIONS_TEST = 46 "testProfileGlobalRestrictionsNotEnforced"; 47 48 private boolean mRemoveOwnerInTearDown; 49 50 @Override setUp()51 public void setUp() throws Exception { 52 super.setUp(); 53 54 mRemoveOwnerInTearDown = false; 55 } 56 57 @Override tearDown()58 public void tearDown() throws Exception { 59 if (mRemoveOwnerInTearDown) { 60 String componentName = DEVICE_ADMIN_PKG + "/" + ADMIN_RECEIVER_TEST_CLASS; 61 assertTrue("Failed to clear owner", removeAdmin(componentName, mDeviceOwnerUserId)); 62 if (isHeadlessSystemUserMode()) { 63 boolean removed = removeAdmin(componentName, mPrimaryUserId); 64 if (!removed) { 65 CLog.e("Failed to remove %s on user %d", componentName, mPrimaryUserId); 66 } 67 } 68 } 69 70 // DO/PO might have set DISALLOW_REMOVE_USER, so it needs to be done after removing 71 // them. 72 removeTestUsers(); 73 getDevice().uninstallPackage(DEVICE_ADMIN_PKG); 74 75 super.tearDown(); 76 } 77 runTests(@onnull String className, @Nullable String method, int userId)78 private void runTests(@Nonnull String className, 79 @Nullable String method, int userId) throws DeviceNotAvailableException { 80 runDeviceTestsAsUser(DEVICE_ADMIN_PKG, "." + className, method, userId); 81 } 82 runTests(@onnull String className, int userId)83 private void runTests(@Nonnull String className, int userId) 84 throws DeviceNotAvailableException { 85 runTests(className, null, userId); 86 } 87 88 @Test testUserRestrictions_deviceOwnerOnly()89 public void testUserRestrictions_deviceOwnerOnly() throws Exception { 90 setDo(); 91 92 try { 93 runTests("userrestrictions.DeviceOwnerUserRestrictionsTest", 94 "testDefaultRestrictions", mDeviceOwnerUserId); 95 runTests("userrestrictions.DeviceOwnerUserRestrictionsTest", 96 "testSetAllRestrictions", mDeviceOwnerUserId); 97 runTests("userrestrictions.DeviceOwnerUserRestrictionsTest", 98 "testBroadcast", mDeviceOwnerUserId); 99 } finally { 100 // Clear all restrictions restrictions on user 0. 101 runTests("userrestrictions.DeviceOwnerUserRestrictionsTest", 102 "testClearAllRestrictions", mDeviceOwnerUserId); 103 } 104 } 105 106 @Test testUserRestrictions_primaryProfileOwnerOnly()107 public void testUserRestrictions_primaryProfileOwnerOnly() throws Exception { 108 assumeHasMainUser(); 109 setPoAsUser(mPrimaryUserId); 110 111 try { 112 runTests("userrestrictions.PrimaryProfileOwnerUserRestrictionsTest", 113 "testDefaultRestrictions", mPrimaryUserId); 114 runTests("userrestrictions.PrimaryProfileOwnerUserRestrictionsTest", 115 "testSetAllRestrictions", mPrimaryUserId); 116 runTests("userrestrictions.PrimaryProfileOwnerUserRestrictionsTest", 117 "testBroadcast", mPrimaryUserId); 118 } finally { 119 // Clear all restrictions restrictions on the main user. 120 runTests("userrestrictions.PrimaryProfileOwnerUserRestrictionsTest", 121 "testClearAllRestrictions", mPrimaryUserId); 122 } 123 } 124 125 // Checks restrictions for managed user (NOT managed profile). 126 @Test testUserRestrictions_secondaryProfileOwnerOnly()127 public void testUserRestrictions_secondaryProfileOwnerOnly() throws Exception { 128 assumeSupportsMultiUser(); 129 130 final int secondaryUserId = createUser(); 131 setPoAsUser(secondaryUserId); 132 133 try { 134 runTests("userrestrictions.SecondaryProfileOwnerUserRestrictionsTest", 135 "testDefaultRestrictions", secondaryUserId); 136 runTests("userrestrictions.SecondaryProfileOwnerUserRestrictionsTest", 137 "testSetAllRestrictions", secondaryUserId); 138 runTests("userrestrictions.SecondaryProfileOwnerUserRestrictionsTest", 139 "testBroadcast", secondaryUserId); 140 } finally { 141 // Clear all restrictions restrictions on secondary user. 142 runTests("userrestrictions.SecondaryProfileOwnerUserRestrictionsTest", 143 "testClearAllRestrictions", secondaryUserId); 144 } 145 } 146 147 // Checks restrictions for managed profile. 148 @RequiresAdditionalFeatures({FEATURE_MANAGED_USERS}) 149 @Test testUserRestrictions_managedProfileOwnerOnly()150 public void testUserRestrictions_managedProfileOwnerOnly() throws Exception { 151 assumeCanCreateOneManagedUser(); 152 153 // Create managed profile. 154 final int profileUserId = createManagedProfile(mPrimaryUserId /* parentUserId */); 155 // createManagedProfile doesn't start the user automatically. 156 startUser(profileUserId); 157 setPoAsUser(profileUserId); 158 159 try { 160 runTests("userrestrictions.ManagedProfileOwnerUserRestrictionsTest", 161 "testDefaultRestrictions", profileUserId); 162 runTests("userrestrictions.ManagedProfileOwnerUserRestrictionsTest", 163 "testSetAllRestrictions", profileUserId); 164 runTests("userrestrictions.ManagedProfileOwnerUserRestrictionsTest", 165 "testBroadcast", profileUserId); 166 } finally { 167 // Clear all restrictions restrictions on profile. 168 runTests("userrestrictions.ManagedProfileOwnerUserRestrictionsTest", 169 "testClearAllRestrictions", profileUserId); 170 } 171 } 172 173 /** 174 * DO + PO combination. Make sure global DO restrictions are visible on secondary users. 175 */ 176 @Test testUserRestrictions_layering()177 public void testUserRestrictions_layering() throws Exception { 178 assumeSupportsMultiUser(); 179 setDo(); 180 181 final int secondaryUserId = createUserAndWaitStart(); 182 if (!isHeadlessSystemUserMode()) { 183 // In headless a PO is set by default on any new additional users so we only need to 184 // set it on non-headless. 185 setPoAsUser(secondaryUserId); 186 } 187 188 try { 189 // Ensure that UserManager differentiates its own restrictions from DO restrictions. 190 runTests("userrestrictions.DeviceOwnerUserRestrictionsTest", 191 "testHasBaseUserRestrictions", mDeviceOwnerUserId); 192 193 // Let DO set all restrictions. 194 runTests("userrestrictions.DeviceOwnerUserRestrictionsTest", 195 "testSetAllRestrictions", mDeviceOwnerUserId); 196 197 // Make sure the global restrictions are visible to secondary users. 198 runTests("userrestrictions.SecondaryProfileOwnerUserRestrictionsTest", 199 "testHasGlobalRestrictions", secondaryUserId); 200 201 // Then let PO set all restrictions. 202 runTests("userrestrictions.SecondaryProfileOwnerUserRestrictionsTest", 203 "testSetAllRestrictions", secondaryUserId); 204 205 // Make sure both local and global restrictions are visible on secondary users. 206 runTests("userrestrictions.SecondaryProfileOwnerUserRestrictionsTest", 207 "testHasBothGlobalAndLocalRestrictions", secondaryUserId); 208 209 // Let DO clear all restrictions. 210 runTests("userrestrictions.DeviceOwnerUserRestrictionsTest", 211 "testClearAllRestrictions", mDeviceOwnerUserId); 212 213 // Now only PO restrictions should be set on the secondary user. 214 runTests("userrestrictions.SecondaryProfileOwnerUserRestrictionsTest", 215 "testLocalRestrictionsOnly", secondaryUserId); 216 } finally { 217 // Clear all restrictions restrictions on user 0. 218 runTests("userrestrictions.DeviceOwnerUserRestrictionsTest", 219 "testClearAllRestrictions", mDeviceOwnerUserId); 220 // Clear all restrictions restrictions on secondary user. 221 runTests("userrestrictions.SecondaryProfileOwnerUserRestrictionsTest", 222 "testClearAllRestrictions", secondaryUserId); 223 } 224 } 225 226 /** 227 * PO on main-user. It can set DO restrictions too, but they shouldn't leak to other users. 228 */ 229 @Test testUserRestrictions_layering_profileOwnerNoLeaking()230 public void testUserRestrictions_layering_profileOwnerNoLeaking() throws Exception { 231 assumeSupportsMultiUser(); 232 assumeHasMainUser(); 233 234 // Set PO on the main user. 235 setPoAsUser(mPrimaryUserId); 236 237 // Create another user and set PO. 238 final int secondaryUserId = createUserAndWaitStart(); 239 setPoAsUser(secondaryUserId); 240 241 try { 242 // Let main-user PO set all restrictions. 243 runTests("userrestrictions.PrimaryProfileOwnerUserRestrictionsTest", 244 "testSetAllRestrictions", mPrimaryUserId); 245 246 // Secondary users shouldn't see any of them. Leaky user restrictions are excluded. 247 runTests("userrestrictions.SecondaryProfileOwnerUserRestrictionsTest", 248 "testDefaultAndLeakyRestrictions", secondaryUserId); 249 } finally { 250 // Clear all restrictions restrictions on the main user. 251 runTests("userrestrictions.PrimaryProfileOwnerUserRestrictionsTest", 252 "testClearAllRestrictions", mPrimaryUserId); 253 // Clear all restrictions restrictions on secondary user. 254 runTests("userrestrictions.SecondaryProfileOwnerUserRestrictionsTest", 255 "testClearAllRestrictions", secondaryUserId); 256 } 257 } 258 259 /** 260 * DO sets profile global restrictions (only ENSURE_VERIFY_APPS), should affect all 261 * users (not a particularly special case but to be sure). 262 */ 263 @Test testUserRestrictions_profileGlobalRestrictionsAsDo()264 public void testUserRestrictions_profileGlobalRestrictionsAsDo() throws Exception { 265 assumeSupportsMultiUser(); 266 setDo(); 267 final int secondaryUserId; 268 if (!isHeadlessSystemUserMode()) { 269 // Create another user and set PO. 270 secondaryUserId = createUserAndWaitStart(); 271 setPoAsUser(secondaryUserId); 272 } else { 273 // In headless system user mode, PO is set on primary user when DO is set. 274 secondaryUserId = mPrimaryUserId; 275 } 276 277 final int[] usersToCheck = {mDeviceOwnerUserId, secondaryUserId}; 278 279 // Do sets the restriction. 280 setAndCheckProfileGlobalRestriction(mDeviceOwnerUserId, usersToCheck); 281 } 282 283 /** 284 * Managed profile owner sets profile global restrictions (only ENSURE_VERIFY_APPS), should 285 * affect all users. 286 */ 287 @RequiresAdditionalFeatures({FEATURE_MANAGED_USERS}) 288 @Test testUserRestrictions_ProfileGlobalRestrictionsAsPo()289 public void testUserRestrictions_ProfileGlobalRestrictionsAsPo() throws Exception { 290 assumeCanCreateOneManagedUser(); 291 292 // Set PO on the main user. 293 setPoAsUser(mPrimaryUserId); 294 295 // Create another user with PO. 296 final int secondaryUserId = createManagedProfile(mPrimaryUserId /* parentUserId */); 297 setPoAsUser(secondaryUserId); 298 299 final int[] usersToCheck = {mPrimaryUserId, secondaryUserId}; 300 301 // Check the case when primary user's PO sets the restriction. 302 setAndCheckProfileGlobalRestriction(mPrimaryUserId, usersToCheck); 303 304 // Check the case when managed profile owner sets the restriction. 305 setAndCheckProfileGlobalRestriction(secondaryUserId, usersToCheck); 306 } 307 308 /** Installs admin package and makes it a profile owner for a given user. */ setPoAsUser(int userId)309 private void setPoAsUser(int userId) throws Exception { 310 installAppAsUser(DEVICE_ADMIN_APK, /* grantPermssions= */true, 311 /* dontKillApp= */ true, userId); 312 assertTrue("Failed to set profile owner", 313 setProfileOwner(DEVICE_ADMIN_PKG + "/" + ADMIN_RECEIVER_TEST_CLASS, 314 userId, /* expectFailure */ false)); 315 // If PO is not in primary user, it will be removed with the user. 316 if (userId == mDeviceOwnerUserId) { 317 mRemoveOwnerInTearDown = true; 318 } 319 } 320 321 /** Installs admin package and makes it a device owner. */ setDo()322 private void setDo() throws Exception { 323 installDeviceOwnerApp(DEVICE_ADMIN_APK); 324 325 assertTrue("Failed to set device owner", 326 setDeviceOwner(DEVICE_ADMIN_PKG + "/" + ADMIN_RECEIVER_TEST_CLASS, 327 mDeviceOwnerUserId, /*expectFailure*/ false)); 328 mRemoveOwnerInTearDown = true; 329 } 330 331 /** 332 * Sets user restriction and checks that it applies to all users. 333 * @param enforcingUserId user who should set/clear the restriction, should be either 334 * primary or secondary user id and should have device or profile owner active. 335 * @param usersToCheck users that should have this restriction enforced. 336 */ setAndCheckProfileGlobalRestriction(int enforcingUserId, int usersToCheck[])337 private void setAndCheckProfileGlobalRestriction(int enforcingUserId, int usersToCheck[]) 338 throws Exception { 339 // Always try to clear the restriction to avoid undesirable side effects. 340 try { 341 // Set the restriction. 342 runGlobalRestrictionsTest(SET_GLOBAL_RESTRICTIONS_TEST, enforcingUserId); 343 // Check that the restriction is in power. 344 for (int userId : usersToCheck) { 345 runGlobalRestrictionsTest(ENSURE_GLOBAL_RESTRICTIONS_TEST, userId); 346 } 347 } finally { 348 // Clear the restriction. 349 runGlobalRestrictionsTest(CLEAR_GLOBAL_RESTRICTIONS_TEST, enforcingUserId); 350 // Check that the restriction is not in power anymore. 351 for (int userId : usersToCheck) { 352 runGlobalRestrictionsTest(ENSURE_NO_GLOBAL_RESTRICTIONS_TEST, userId); 353 } 354 } 355 } 356 357 /** Convenience method to run global user restrictions tests. */ runGlobalRestrictionsTest(String testMethodName, int userId)358 private void runGlobalRestrictionsTest(String testMethodName, int userId) throws Exception { 359 runTests(GLOBAL_RESTRICTIONS_TEST_CLASS, testMethodName, userId); 360 } 361 } 362