Home | History | Annotate | Download | only in pm
      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.server.pm;
     18 
     19 import static com.android.server.devicepolicy.DpmTestUtils.assertRestrictions;
     20 import static com.android.server.devicepolicy.DpmTestUtils.newRestrictions;
     21 
     22 import android.os.Bundle;
     23 import android.os.UserHandle;
     24 import android.os.UserManager;
     25 import android.os.UserManagerInternal;
     26 import android.test.AndroidTestCase;
     27 import android.test.suitebuilder.annotation.SmallTest;
     28 import 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
     42 public 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         assertTrue(UserRestrictionsUtils.canDeviceOwnerChange(UserManager.DISALLOW_USER_SWITCH));
     93     }
     94 
     95     public void testCanProfileOwnerChange() {
     96         int user = UserHandle.USER_SYSTEM;
     97         assertFalse(UserRestrictionsUtils.canProfileOwnerChange(
     98                 UserManager.DISALLOW_RECORD_AUDIO, user));
     99         assertFalse(UserRestrictionsUtils.canProfileOwnerChange(
    100                 UserManager.DISALLOW_WALLPAPER, user));
    101         assertFalse(UserRestrictionsUtils.canProfileOwnerChange(
    102                 UserManager.DISALLOW_USER_SWITCH, user));
    103         assertTrue(UserRestrictionsUtils.canProfileOwnerChange(
    104                 UserManager.DISALLOW_ADD_USER, user));
    105         assertTrue(UserRestrictionsUtils.canProfileOwnerChange(
    106                 UserManager.DISALLOW_ADJUST_VOLUME, user));
    107 
    108         user = 10;
    109         assertFalse(UserRestrictionsUtils.canProfileOwnerChange(
    110                 UserManager.DISALLOW_RECORD_AUDIO, user));
    111         assertFalse(UserRestrictionsUtils.canProfileOwnerChange(
    112                 UserManager.DISALLOW_WALLPAPER, user));
    113         assertFalse(UserRestrictionsUtils.canProfileOwnerChange(
    114                 UserManager.DISALLOW_ADD_USER, user));
    115         assertFalse(UserRestrictionsUtils.canProfileOwnerChange(
    116                 UserManager.DISALLOW_USER_SWITCH, user));
    117         assertTrue(UserRestrictionsUtils.canProfileOwnerChange(
    118                 UserManager.DISALLOW_ADJUST_VOLUME, user));
    119     }
    120 
    121     public void testSortToGlobalAndLocal() {
    122         final Bundle local = new Bundle();
    123         final Bundle global = new Bundle();
    124 
    125         UserRestrictionsUtils.sortToGlobalAndLocal(null, false /* isDeviceOwner */,
    126                 UserManagerInternal.CAMERA_NOT_DISABLED, global, local);
    127         assertEquals(0, global.size());
    128         assertEquals(0, local.size());
    129 
    130         UserRestrictionsUtils.sortToGlobalAndLocal(Bundle.EMPTY, false /* isDeviceOwner */,
    131                 UserManagerInternal.CAMERA_NOT_DISABLED, global, local);
    132         assertEquals(0, global.size());
    133         assertEquals(0, local.size());
    134 
    135         // Restrictions set by DO.
    136         UserRestrictionsUtils.sortToGlobalAndLocal(newRestrictions(
    137                 UserManager.DISALLOW_ADJUST_VOLUME,
    138                 UserManager.DISALLOW_UNMUTE_MICROPHONE,
    139                 UserManager.DISALLOW_USB_FILE_TRANSFER,
    140                 UserManager.DISALLOW_CONFIG_TETHERING,
    141                 UserManager.DISALLOW_OUTGOING_BEAM,
    142                 UserManager.DISALLOW_APPS_CONTROL,
    143                 UserManager.ENSURE_VERIFY_APPS
    144         ), true /* isDeviceOwner */, UserManagerInternal.CAMERA_NOT_DISABLED, global, local);
    145 
    146 
    147         assertRestrictions(newRestrictions(
    148                 // This one is global no matter who sets it.
    149                 UserManager.ENSURE_VERIFY_APPS,
    150 
    151                 // These can be set by PO too, but when DO sets them, they're global.
    152                 UserManager.DISALLOW_ADJUST_VOLUME,
    153                 UserManager.DISALLOW_UNMUTE_MICROPHONE,
    154 
    155                 // These can only be set by DO.
    156                 UserManager.DISALLOW_USB_FILE_TRANSFER,
    157                 UserManager.DISALLOW_CONFIG_TETHERING
    158         ), global);
    159 
    160         assertRestrictions(newRestrictions(
    161                 // They can be set by both DO/PO.
    162                 UserManager.DISALLOW_OUTGOING_BEAM,
    163                 UserManager.DISALLOW_APPS_CONTROL
    164         ), local);
    165 
    166         local.clear();
    167         global.clear();
    168 
    169         // Restrictions set by PO.
    170         UserRestrictionsUtils.sortToGlobalAndLocal(newRestrictions(
    171                 UserManager.DISALLOW_ADJUST_VOLUME,
    172                 UserManager.DISALLOW_UNMUTE_MICROPHONE,
    173                 UserManager.DISALLOW_USB_FILE_TRANSFER,
    174                 UserManager.DISALLOW_CONFIG_TETHERING,
    175                 UserManager.DISALLOW_OUTGOING_BEAM,
    176                 UserManager.DISALLOW_APPS_CONTROL,
    177                 UserManager.ENSURE_VERIFY_APPS
    178         ), false /* isDeviceOwner */, UserManagerInternal.CAMERA_NOT_DISABLED, global, local);
    179 
    180         assertRestrictions(newRestrictions(
    181                 // This one is global no matter who sets it.
    182                 UserManager.ENSURE_VERIFY_APPS
    183         ), global);
    184 
    185         assertRestrictions(newRestrictions(
    186                 // These can be set by PO too, but when PO sets them, they're local.
    187                 UserManager.DISALLOW_ADJUST_VOLUME,
    188                 UserManager.DISALLOW_UNMUTE_MICROPHONE,
    189 
    190                 // They can be set by both DO/PO.
    191                 UserManager.DISALLOW_OUTGOING_BEAM,
    192                 UserManager.DISALLOW_APPS_CONTROL,
    193 
    194                 // These can only be set by DO.
    195                 UserManager.DISALLOW_USB_FILE_TRANSFER,
    196                 UserManager.DISALLOW_CONFIG_TETHERING
    197         ), local);
    198 
    199     }
    200 
    201     public void testSortToLocalAndGlobalWithCameraDisabled() {
    202         final Bundle local = new Bundle();
    203         final Bundle global = new Bundle();
    204 
    205         UserRestrictionsUtils.sortToGlobalAndLocal(Bundle.EMPTY, false,
    206                 UserManagerInternal.CAMERA_DISABLED_GLOBALLY, global, local);
    207         assertRestrictions(newRestrictions(UserManager.DISALLOW_CAMERA), global);
    208         assertEquals(0, local.size());
    209         global.clear();
    210 
    211         UserRestrictionsUtils.sortToGlobalAndLocal(Bundle.EMPTY, false,
    212                 UserManagerInternal.CAMERA_DISABLED_LOCALLY, global, local);
    213         assertEquals(0, global.size());
    214         assertRestrictions(newRestrictions(UserManager.DISALLOW_CAMERA), local);
    215     }
    216 
    217     public void testMergeAll() {
    218         SparseArray<Bundle> restrictions = new SparseArray<>();
    219         assertNull(UserRestrictionsUtils.mergeAll(restrictions));
    220 
    221         restrictions.put(0, newRestrictions(UserManager.DISALLOW_ADJUST_VOLUME));
    222         restrictions.put(1, newRestrictions(UserManager.DISALLOW_USB_FILE_TRANSFER));
    223         restrictions.put(2, newRestrictions(UserManager.DISALLOW_APPS_CONTROL));
    224 
    225         Bundle result = UserRestrictionsUtils.mergeAll(restrictions);
    226         assertRestrictions(
    227                 newRestrictions(
    228                         UserManager.DISALLOW_ADJUST_VOLUME,
    229                         UserManager.DISALLOW_USB_FILE_TRANSFER,
    230                         UserManager.DISALLOW_APPS_CONTROL),
    231                 result);
    232     }
    233 
    234     public void testMoveRestriction() {
    235         SparseArray<Bundle> localRestrictions = new SparseArray<>();
    236         SparseArray<Bundle> globalRestrictions = new SparseArray<>();
    237 
    238         // User 0 has only local restrictions, nothing should change.
    239         localRestrictions.put(0, newRestrictions(UserManager.DISALLOW_ADJUST_VOLUME));
    240         // User 1 has a local restriction to be moved to global and some global already. Local
    241         // restrictions should be removed for this user.
    242         localRestrictions.put(1, newRestrictions(UserManager.ENSURE_VERIFY_APPS));
    243         globalRestrictions.put(1, newRestrictions(UserManager.DISALLOW_ADD_USER));
    244         // User 2 has a local restriction to be moved and one to leave local.
    245         localRestrictions.put(2, newRestrictions(
    246                 UserManager.ENSURE_VERIFY_APPS,
    247                 UserManager.DISALLOW_CONFIG_VPN));
    248 
    249         UserRestrictionsUtils.moveRestriction(
    250                 UserManager.ENSURE_VERIFY_APPS, localRestrictions, globalRestrictions);
    251 
    252         // Check user 0.
    253         assertRestrictions(
    254                 newRestrictions(UserManager.DISALLOW_ADJUST_VOLUME),
    255                 localRestrictions.get(0));
    256         assertNull(globalRestrictions.get(0));
    257 
    258         // Check user 1.
    259         assertNull(localRestrictions.get(1));
    260         assertRestrictions(
    261                 newRestrictions(UserManager.ENSURE_VERIFY_APPS, UserManager.DISALLOW_ADD_USER),
    262                 globalRestrictions.get(1));
    263 
    264         // Check user 2.
    265         assertRestrictions(
    266                 newRestrictions(UserManager.DISALLOW_CONFIG_VPN),
    267                 localRestrictions.get(2));
    268         assertRestrictions(
    269                 newRestrictions(UserManager.ENSURE_VERIFY_APPS),
    270                 globalRestrictions.get(2));
    271     }
    272 
    273     public void testAreEqual() {
    274         assertTrue(UserRestrictionsUtils.areEqual(
    275                 null,
    276                 null));
    277 
    278         assertTrue(UserRestrictionsUtils.areEqual(
    279                 null,
    280                 Bundle.EMPTY));
    281 
    282         assertTrue(UserRestrictionsUtils.areEqual(
    283                 Bundle.EMPTY,
    284                 null));
    285 
    286         assertTrue(UserRestrictionsUtils.areEqual(
    287                 Bundle.EMPTY,
    288                 Bundle.EMPTY));
    289 
    290         assertTrue(UserRestrictionsUtils.areEqual(
    291                 new Bundle(),
    292                 Bundle.EMPTY));
    293 
    294         assertFalse(UserRestrictionsUtils.areEqual(
    295                 null,
    296                 newRestrictions("a")));
    297 
    298         assertFalse(UserRestrictionsUtils.areEqual(
    299                 newRestrictions("a"),
    300                 null));
    301 
    302         assertTrue(UserRestrictionsUtils.areEqual(
    303                 newRestrictions("a"),
    304                 newRestrictions("a")));
    305 
    306         assertFalse(UserRestrictionsUtils.areEqual(
    307                 newRestrictions("a"),
    308                 newRestrictions("a", "b")));
    309 
    310         assertFalse(UserRestrictionsUtils.areEqual(
    311                 newRestrictions("a", "b"),
    312                 newRestrictions("a")));
    313 
    314         assertFalse(UserRestrictionsUtils.areEqual(
    315                 newRestrictions("b", "a"),
    316                 newRestrictions("a", "a")));
    317 
    318         // Make sure false restrictions are handled correctly.
    319         final Bundle a = newRestrictions("a");
    320         a.putBoolean("b", true);
    321 
    322         final Bundle b = newRestrictions("a");
    323         b.putBoolean("b", false);
    324 
    325         assertFalse(UserRestrictionsUtils.areEqual(a, b));
    326         assertFalse(UserRestrictionsUtils.areEqual(b, a));
    327     }
    328 }
    329