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.UserManager;
24 import android.platform.test.annotations.Presubmit;
25 import android.test.AndroidTestCase;
26 import android.util.SparseArray;
27 
28 import androidx.test.filters.SmallTest;
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/androidx.test.runner.AndroidJUnitRunner
39  * </pre>
40  */
41 @Presubmit
42 @SmallTest
43 public class UserRestrictionsUtilsTest extends AndroidTestCase {
testNonNull()44     public void testNonNull() {
45         Bundle out = UserRestrictionsUtils.nonNull(null);
46         assertNotNull(out);
47         out.putBoolean("a", true); // Should not be Bundle.EMPTY.
48 
49         Bundle in = new Bundle();
50         assertSame(in, UserRestrictionsUtils.nonNull(in));
51     }
52 
testMerge()53     public void testMerge() {
54         Bundle a = newRestrictions("a", "d");
55         Bundle b = newRestrictions("b", "d", "e");
56 
57         UserRestrictionsUtils.merge(a, b);
58 
59         assertRestrictions(newRestrictions("a", "b", "d", "e"), a);
60 
61         UserRestrictionsUtils.merge(a, null);
62 
63         assertRestrictions(newRestrictions("a", "b", "d", "e"), a);
64 
65         try {
66             UserRestrictionsUtils.merge(a, a);
67             fail();
68         } catch (IllegalArgumentException expected) {
69         }
70     }
71 
testCanDeviceOwnerChange()72     public void testCanDeviceOwnerChange() {
73         assertFalse(UserRestrictionsUtils.canDeviceOwnerChange(UserManager.DISALLOW_RECORD_AUDIO));
74         assertFalse(UserRestrictionsUtils.canDeviceOwnerChange(UserManager.DISALLOW_WALLPAPER));
75         assertTrue(UserRestrictionsUtils.canDeviceOwnerChange(UserManager.DISALLOW_ADD_USER));
76         assertTrue(UserRestrictionsUtils.canDeviceOwnerChange(UserManager.DISALLOW_USER_SWITCH));
77     }
78 
testCanProfileOwnerChange_mainUser()79     public void testCanProfileOwnerChange_mainUser() {
80         assertFalse(UserRestrictionsUtils.canProfileOwnerChange(
81                 UserManager.DISALLOW_RECORD_AUDIO, true));
82         assertFalse(UserRestrictionsUtils.canProfileOwnerChange(
83                 UserManager.DISALLOW_WALLPAPER, true));
84         assertFalse(UserRestrictionsUtils.canProfileOwnerChange(
85                 UserManager.DISALLOW_USER_SWITCH, true));
86         assertTrue(UserRestrictionsUtils.canProfileOwnerChange(
87                 UserManager.DISALLOW_ADD_USER, true));
88         assertTrue(UserRestrictionsUtils.canProfileOwnerChange(
89                 UserManager.DISALLOW_ADJUST_VOLUME, true));
90     }
91 
testCanProfileOwnerChange_notMainUser()92     public void testCanProfileOwnerChange_notMainUser() {
93         assertFalse(UserRestrictionsUtils.canProfileOwnerChange(
94                 UserManager.DISALLOW_RECORD_AUDIO, false));
95         assertFalse(UserRestrictionsUtils.canProfileOwnerChange(
96                 UserManager.DISALLOW_WALLPAPER, false));
97         assertFalse(UserRestrictionsUtils.canProfileOwnerChange(
98                 UserManager.DISALLOW_ADD_USER, false));
99         assertFalse(UserRestrictionsUtils.canProfileOwnerChange(
100                 UserManager.DISALLOW_USER_SWITCH, false));
101         assertTrue(UserRestrictionsUtils.canProfileOwnerChange(
102                 UserManager.DISALLOW_ADJUST_VOLUME, false));
103     }
104 
testMoveRestriction()105     public void testMoveRestriction() {
106         SparseArray<RestrictionsSet> localRestrictions = new SparseArray<>();
107         RestrictionsSet globalRestrictions = new RestrictionsSet();
108 
109         // User 0 has only local restrictions, nothing should change.
110         localRestrictions.put(0, newRestrictions(0, UserManager.DISALLOW_ADJUST_VOLUME));
111         // User 1 has a local restriction to be moved to global and some global already. Local
112         // restrictions should be removed for this user.
113         localRestrictions.put(1, newRestrictions(1, UserManager.ENSURE_VERIFY_APPS));
114         globalRestrictions.updateRestrictions(1,
115                 newRestrictions(UserManager.DISALLOW_ADD_USER));
116         // User 2 has a local restriction to be moved and one to leave local.
117         localRestrictions.put(2, newRestrictions(2,
118                 UserManager.ENSURE_VERIFY_APPS, UserManager.DISALLOW_CONFIG_VPN));
119 
120         UserRestrictionsUtils.moveRestriction(
121                 UserManager.ENSURE_VERIFY_APPS, localRestrictions, globalRestrictions);
122 
123         // Check user 0.
124         assertRestrictions(
125                 newRestrictions(0, UserManager.DISALLOW_ADJUST_VOLUME),
126                 localRestrictions.get(0));
127         assertNull(globalRestrictions.getRestrictions(0));
128 
129         // Check user 1.
130         assertTrue(localRestrictions.get(1).isEmpty());
131         assertRestrictions(
132                 newRestrictions(UserManager.ENSURE_VERIFY_APPS, UserManager.DISALLOW_ADD_USER),
133                 globalRestrictions.getRestrictions(1));
134 
135         // Check user 2.
136         assertRestrictions(
137                 newRestrictions(2, UserManager.DISALLOW_CONFIG_VPN),
138                 localRestrictions.get(2));
139         assertRestrictions(
140                 newRestrictions(UserManager.ENSURE_VERIFY_APPS),
141                 globalRestrictions.getRestrictions(2));
142     }
143 
testAreEqual()144     public void testAreEqual() {
145         assertTrue(UserRestrictionsUtils.areEqual(
146                 null,
147                 null));
148 
149         assertTrue(UserRestrictionsUtils.areEqual(
150                 null,
151                 Bundle.EMPTY));
152 
153         assertTrue(UserRestrictionsUtils.areEqual(
154                 Bundle.EMPTY,
155                 null));
156 
157         assertTrue(UserRestrictionsUtils.areEqual(
158                 Bundle.EMPTY,
159                 Bundle.EMPTY));
160 
161         assertTrue(UserRestrictionsUtils.areEqual(
162                 new Bundle(),
163                 Bundle.EMPTY));
164 
165         assertFalse(UserRestrictionsUtils.areEqual(
166                 null,
167                 newRestrictions("a")));
168 
169         assertFalse(UserRestrictionsUtils.areEqual(
170                 newRestrictions("a"),
171                 null));
172 
173         assertTrue(UserRestrictionsUtils.areEqual(
174                 newRestrictions("a"),
175                 newRestrictions("a")));
176 
177         assertFalse(UserRestrictionsUtils.areEqual(
178                 newRestrictions("a"),
179                 newRestrictions("a", "b")));
180 
181         assertFalse(UserRestrictionsUtils.areEqual(
182                 newRestrictions("a", "b"),
183                 newRestrictions("a")));
184 
185         assertFalse(UserRestrictionsUtils.areEqual(
186                 newRestrictions("b", "a"),
187                 newRestrictions("a", "a")));
188 
189         // Make sure false restrictions are handled correctly.
190         final Bundle a = newRestrictions("a");
191         a.putBoolean("b", true);
192 
193         final Bundle b = newRestrictions("a");
194         b.putBoolean("b", false);
195 
196         assertFalse(UserRestrictionsUtils.areEqual(a, b));
197         assertFalse(UserRestrictionsUtils.areEqual(b, a));
198     }
199 }
200