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 17package com.android.server.pm; 18 19import static com.android.server.devicepolicy.DpmTestUtils.assertRestrictions; 20import static com.android.server.devicepolicy.DpmTestUtils.newRestrictions; 21 22import android.os.Bundle; 23import android.os.UserHandle; 24import android.os.UserManager; 25import android.os.UserManagerInternal; 26import android.test.AndroidTestCase; 27import android.test.suitebuilder.annotation.SmallTest; 28import android.util.SparseArray; 29 30/** 31 * Tests for {@link com.android.server.pm.UserRestrictionsUtils}. 32 * 33 * <p>Run with:<pre> 34 m FrameworksServicesTests && 35 adb install \ 36 -r out/target/product/hammerhead/data/app/FrameworksServicesTests/FrameworksServicesTests.apk && 37 adb shell am instrument -e class com.android.server.pm.UserRestrictionsUtilsTest \ 38 -w com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner 39 * </pre> 40 */ 41@SmallTest 42public class UserRestrictionsUtilsTest extends AndroidTestCase { 43 public void testNonNull() { 44 Bundle out = UserRestrictionsUtils.nonNull(null); 45 assertNotNull(out); 46 out.putBoolean("a", true); // Should not be Bundle.EMPTY. 47 48 Bundle in = new Bundle(); 49 assertSame(in, UserRestrictionsUtils.nonNull(in)); 50 } 51 52 public void testIsEmpty() { 53 assertTrue(UserRestrictionsUtils.isEmpty(null)); 54 assertTrue(UserRestrictionsUtils.isEmpty(new Bundle())); 55 assertFalse(UserRestrictionsUtils.isEmpty(newRestrictions("a"))); 56 } 57 58 public void testClone() { 59 Bundle in = new Bundle(); 60 Bundle out = UserRestrictionsUtils.clone(in); 61 assertNotSame(in, out); 62 assertRestrictions(out, new Bundle()); 63 64 out = UserRestrictionsUtils.clone(null); 65 assertNotNull(out); 66 out.putBoolean("a", true); // Should not be Bundle.EMPTY. 67 } 68 69 public void testMerge() { 70 Bundle a = newRestrictions("a", "d"); 71 Bundle b = newRestrictions("b", "d", "e"); 72 73 UserRestrictionsUtils.merge(a, b); 74 75 assertRestrictions(newRestrictions("a", "b", "d", "e"), a); 76 77 UserRestrictionsUtils.merge(a, null); 78 79 assertRestrictions(newRestrictions("a", "b", "d", "e"), a); 80 81 try { 82 UserRestrictionsUtils.merge(a, a); 83 fail(); 84 } catch (IllegalArgumentException expected) { 85 } 86 } 87 88 public void testCanDeviceOwnerChange() { 89 assertFalse(UserRestrictionsUtils.canDeviceOwnerChange(UserManager.DISALLOW_RECORD_AUDIO)); 90 assertFalse(UserRestrictionsUtils.canDeviceOwnerChange(UserManager.DISALLOW_WALLPAPER)); 91 assertTrue(UserRestrictionsUtils.canDeviceOwnerChange(UserManager.DISALLOW_ADD_USER)); 92 } 93 94 public void testCanProfileOwnerChange() { 95 int user = UserHandle.USER_SYSTEM; 96 assertFalse(UserRestrictionsUtils.canProfileOwnerChange( 97 UserManager.DISALLOW_RECORD_AUDIO, user)); 98 assertFalse(UserRestrictionsUtils.canProfileOwnerChange( 99 UserManager.DISALLOW_WALLPAPER, user)); 100 assertTrue(UserRestrictionsUtils.canProfileOwnerChange( 101 UserManager.DISALLOW_ADD_USER, user)); 102 assertTrue(UserRestrictionsUtils.canProfileOwnerChange( 103 UserManager.DISALLOW_ADJUST_VOLUME, user)); 104 105 user = 10; 106 assertFalse(UserRestrictionsUtils.canProfileOwnerChange( 107 UserManager.DISALLOW_RECORD_AUDIO, user)); 108 assertFalse(UserRestrictionsUtils.canProfileOwnerChange( 109 UserManager.DISALLOW_WALLPAPER, user)); 110 assertFalse(UserRestrictionsUtils.canProfileOwnerChange( 111 UserManager.DISALLOW_ADD_USER, user)); 112 assertTrue(UserRestrictionsUtils.canProfileOwnerChange( 113 UserManager.DISALLOW_ADJUST_VOLUME, user)); 114 } 115 116 public void testSortToGlobalAndLocal() { 117 final Bundle local = new Bundle(); 118 final Bundle global = new Bundle(); 119 120 UserRestrictionsUtils.sortToGlobalAndLocal(null, false /* isDeviceOwner */, 121 UserManagerInternal.CAMERA_NOT_DISABLED, global, local); 122 assertEquals(0, global.size()); 123 assertEquals(0, local.size()); 124 125 UserRestrictionsUtils.sortToGlobalAndLocal(Bundle.EMPTY, false /* isDeviceOwner */, 126 UserManagerInternal.CAMERA_NOT_DISABLED, global, local); 127 assertEquals(0, global.size()); 128 assertEquals(0, local.size()); 129 130 // Restrictions set by DO. 131 UserRestrictionsUtils.sortToGlobalAndLocal(newRestrictions( 132 UserManager.DISALLOW_ADJUST_VOLUME, 133 UserManager.DISALLOW_UNMUTE_MICROPHONE, 134 UserManager.DISALLOW_USB_FILE_TRANSFER, 135 UserManager.DISALLOW_CONFIG_TETHERING, 136 UserManager.DISALLOW_OUTGOING_BEAM, 137 UserManager.DISALLOW_APPS_CONTROL, 138 UserManager.ENSURE_VERIFY_APPS 139 ), true /* isDeviceOwner */, UserManagerInternal.CAMERA_NOT_DISABLED, global, local); 140 141 142 assertRestrictions(newRestrictions( 143 // This one is global no matter who sets it. 144 UserManager.ENSURE_VERIFY_APPS, 145 146 // These can be set by PO too, but when DO sets them, they're global. 147 UserManager.DISALLOW_ADJUST_VOLUME, 148 UserManager.DISALLOW_UNMUTE_MICROPHONE, 149 150 // These can only be set by DO. 151 UserManager.DISALLOW_USB_FILE_TRANSFER, 152 UserManager.DISALLOW_CONFIG_TETHERING 153 ), global); 154 155 assertRestrictions(newRestrictions( 156 // They can be set by both DO/PO. 157 UserManager.DISALLOW_OUTGOING_BEAM, 158 UserManager.DISALLOW_APPS_CONTROL 159 ), local); 160 161 local.clear(); 162 global.clear(); 163 164 // Restrictions set by PO. 165 UserRestrictionsUtils.sortToGlobalAndLocal(newRestrictions( 166 UserManager.DISALLOW_ADJUST_VOLUME, 167 UserManager.DISALLOW_UNMUTE_MICROPHONE, 168 UserManager.DISALLOW_USB_FILE_TRANSFER, 169 UserManager.DISALLOW_CONFIG_TETHERING, 170 UserManager.DISALLOW_OUTGOING_BEAM, 171 UserManager.DISALLOW_APPS_CONTROL, 172 UserManager.ENSURE_VERIFY_APPS 173 ), false /* isDeviceOwner */, UserManagerInternal.CAMERA_NOT_DISABLED, global, local); 174 175 assertRestrictions(newRestrictions( 176 // This one is global no matter who sets it. 177 UserManager.ENSURE_VERIFY_APPS 178 ), global); 179 180 assertRestrictions(newRestrictions( 181 // These can be set by PO too, but when PO sets them, they're local. 182 UserManager.DISALLOW_ADJUST_VOLUME, 183 UserManager.DISALLOW_UNMUTE_MICROPHONE, 184 185 // They can be set by both DO/PO. 186 UserManager.DISALLOW_OUTGOING_BEAM, 187 UserManager.DISALLOW_APPS_CONTROL, 188 189 // These can only be set by DO. 190 UserManager.DISALLOW_USB_FILE_TRANSFER, 191 UserManager.DISALLOW_CONFIG_TETHERING 192 ), local); 193 194 } 195 196 public void testSortToLocalAndGlobalWithCameraDisabled() { 197 final Bundle local = new Bundle(); 198 final Bundle global = new Bundle(); 199 200 UserRestrictionsUtils.sortToGlobalAndLocal(Bundle.EMPTY, false, 201 UserManagerInternal.CAMERA_DISABLED_GLOBALLY, global, local); 202 assertRestrictions(newRestrictions(UserManager.DISALLOW_CAMERA), global); 203 assertEquals(0, local.size()); 204 global.clear(); 205 206 UserRestrictionsUtils.sortToGlobalAndLocal(Bundle.EMPTY, false, 207 UserManagerInternal.CAMERA_DISABLED_LOCALLY, global, local); 208 assertEquals(0, global.size()); 209 assertRestrictions(newRestrictions(UserManager.DISALLOW_CAMERA), local); 210 } 211 212 public void testMergeAll() { 213 SparseArray<Bundle> restrictions = new SparseArray<>(); 214 assertNull(UserRestrictionsUtils.mergeAll(restrictions)); 215 216 restrictions.put(0, newRestrictions(UserManager.DISALLOW_ADJUST_VOLUME)); 217 restrictions.put(1, newRestrictions(UserManager.DISALLOW_USB_FILE_TRANSFER)); 218 restrictions.put(2, newRestrictions(UserManager.DISALLOW_APPS_CONTROL)); 219 220 Bundle result = UserRestrictionsUtils.mergeAll(restrictions); 221 assertRestrictions( 222 newRestrictions( 223 UserManager.DISALLOW_ADJUST_VOLUME, 224 UserManager.DISALLOW_USB_FILE_TRANSFER, 225 UserManager.DISALLOW_APPS_CONTROL), 226 result); 227 } 228 229 public void testMoveRestriction() { 230 SparseArray<Bundle> localRestrictions = new SparseArray<>(); 231 SparseArray<Bundle> globalRestrictions = new SparseArray<>(); 232 233 // User 0 has only local restrictions, nothing should change. 234 localRestrictions.put(0, newRestrictions(UserManager.DISALLOW_ADJUST_VOLUME)); 235 // User 1 has a local restriction to be moved to global and some global already. Local 236 // restrictions should be removed for this user. 237 localRestrictions.put(1, newRestrictions(UserManager.ENSURE_VERIFY_APPS)); 238 globalRestrictions.put(1, newRestrictions(UserManager.DISALLOW_ADD_USER)); 239 // User 2 has a local restriction to be moved and one to leave local. 240 localRestrictions.put(2, newRestrictions( 241 UserManager.ENSURE_VERIFY_APPS, 242 UserManager.DISALLOW_CONFIG_VPN)); 243 244 UserRestrictionsUtils.moveRestriction( 245 UserManager.ENSURE_VERIFY_APPS, localRestrictions, globalRestrictions); 246 247 // Check user 0. 248 assertRestrictions( 249 newRestrictions(UserManager.DISALLOW_ADJUST_VOLUME), 250 localRestrictions.get(0)); 251 assertNull(globalRestrictions.get(0)); 252 253 // Check user 1. 254 assertNull(localRestrictions.get(1)); 255 assertRestrictions( 256 newRestrictions(UserManager.ENSURE_VERIFY_APPS, UserManager.DISALLOW_ADD_USER), 257 globalRestrictions.get(1)); 258 259 // Check user 2. 260 assertRestrictions( 261 newRestrictions(UserManager.DISALLOW_CONFIG_VPN), 262 localRestrictions.get(2)); 263 assertRestrictions( 264 newRestrictions(UserManager.ENSURE_VERIFY_APPS), 265 globalRestrictions.get(2)); 266 } 267 268 public void testAreEqual() { 269 assertTrue(UserRestrictionsUtils.areEqual( 270 null, 271 null)); 272 273 assertTrue(UserRestrictionsUtils.areEqual( 274 null, 275 Bundle.EMPTY)); 276 277 assertTrue(UserRestrictionsUtils.areEqual( 278 Bundle.EMPTY, 279 null)); 280 281 assertTrue(UserRestrictionsUtils.areEqual( 282 Bundle.EMPTY, 283 Bundle.EMPTY)); 284 285 assertTrue(UserRestrictionsUtils.areEqual( 286 new Bundle(), 287 Bundle.EMPTY)); 288 289 assertFalse(UserRestrictionsUtils.areEqual( 290 null, 291 newRestrictions("a"))); 292 293 assertFalse(UserRestrictionsUtils.areEqual( 294 newRestrictions("a"), 295 null)); 296 297 assertTrue(UserRestrictionsUtils.areEqual( 298 newRestrictions("a"), 299 newRestrictions("a"))); 300 301 assertFalse(UserRestrictionsUtils.areEqual( 302 newRestrictions("a"), 303 newRestrictions("a", "b"))); 304 305 assertFalse(UserRestrictionsUtils.areEqual( 306 newRestrictions("a", "b"), 307 newRestrictions("a"))); 308 309 assertFalse(UserRestrictionsUtils.areEqual( 310 newRestrictions("b", "a"), 311 newRestrictions("a", "a"))); 312 313 // Make sure false restrictions are handled correctly. 314 final Bundle a = newRestrictions("a"); 315 a.putBoolean("b", true); 316 317 final Bundle b = newRestrictions("a"); 318 b.putBoolean("b", false); 319 320 assertFalse(UserRestrictionsUtils.areEqual(a, b)); 321 assertFalse(UserRestrictionsUtils.areEqual(b, a)); 322 } 323} 324