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.managedprovisioning.task;
18 
19 import static org.mockito.Mockito.any;
20 import static org.mockito.Mockito.anyBoolean;
21 import static org.mockito.Mockito.anyString;
22 import static org.mockito.Mockito.never;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.when;
25 
26 import android.content.Context;
27 import android.content.pm.UserInfo;
28 import android.os.UserHandle;
29 import android.os.UserManager;
30 import android.test.AndroidTestCase;
31 
32 import androidx.test.filters.SmallTest;
33 
34 import com.android.managedprovisioning.analytics.ProvisioningAnalyticsTracker;
35 
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 
39 import java.util.Arrays;
40 import java.util.Collections;
41 
42 /**
43  * Unit-tests for {@link DisallowAddUserTask}.
44  *
45  * <p>Run as {@code atest ManagedProvisioningTests:DisallowAddUserTaskTest}.
46  */
47 public class DisallowAddUserTaskTest extends AndroidTestCase {
48     @Mock private Context mMockContext;
49     @Mock private UserManager mMockUserManager;
50     @Mock private AbstractProvisioningTask.Callback mCallback;
51     @Mock private ProvisioningAnalyticsTracker mProvisioningAnalyticsTracker;
52 
53     // Normal cases.
54     private final UserInfo mSystemUser = new UserInfo(UserHandle.USER_SYSTEM, "System",
55             UserInfo.FLAG_PRIMARY | UserInfo.FLAG_ADMIN);
56 
57     // Headless-system-user cases.
58     private final UserInfo mFullUser = new UserInfo(10, "FullUser", UserInfo.FLAG_ADMIN);
59 
60     @Override
setUp()61     public void setUp() {
62         // this is necessary for mockito to work
63         System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString());
64 
65         MockitoAnnotations.initMocks(this);
66 
67         when(mMockContext.getSystemService(Context.USER_SERVICE)).thenReturn(mMockUserManager);
68         // Setup sensible default responses.
69         when(mMockUserManager.hasUserRestriction(anyString(), any(UserHandle.class)))
70                 .thenReturn(false);
71     }
72 
73     @SmallTest
testMaybeDisallowAddUsers_normalSystem()74     public void testMaybeDisallowAddUsers_normalSystem() {
75         // GIVEN that only one user exists on the device and the system doesn't have a headless
76         // system user
77         when(mMockUserManager.getUsers()).thenReturn(Collections.singletonList(mSystemUser));
78         final DisallowAddUserTask task =
79                 new DisallowAddUserTask(false, mMockContext, null, mCallback,
80                         mProvisioningAnalyticsTracker);
81 
82         // WHEN running the DisallowAddUserTask on the single user
83         task.run(mSystemUser.id);
84 
85         // THEN the user restriction should be set
86         verify(mMockUserManager).setUserRestriction(UserManager.DISALLOW_ADD_USER, true,
87                 mSystemUser.getUserHandle());
88         verify(mCallback).onSuccess(task);
89     }
90 
91     @SmallTest
testMaybeDisallowAddUsers_normalSystem_restrictionAlreadySetupForOneUser()92     public void testMaybeDisallowAddUsers_normalSystem_restrictionAlreadySetupForOneUser() {
93         // GIVEN that only one user exists on the device and the system doesn't have a headless
94         // system user
95         when(mMockUserManager.getUsers()).thenReturn(Collections.singletonList(mSystemUser));
96         final DisallowAddUserTask task =
97                 new DisallowAddUserTask(false, mMockContext, null, mCallback,
98                         mProvisioningAnalyticsTracker);
99 
100         // GIVEN that the user restriction has already been set
101         when(mMockUserManager.hasUserRestriction(UserManager.DISALLOW_ADD_USER,
102                 mSystemUser.getUserHandle()))
103                 .thenReturn(true);
104 
105         // WHEN running the DisallowAddUserTask on the single user
106         task.run(mSystemUser.id);
107 
108         // THEN the user restriction should not be set
109         verify(mMockUserManager, never()).setUserRestriction(anyString(), anyBoolean(),
110                 any(UserHandle.class));
111         verify(mCallback).onSuccess(task);
112     }
113 
114     @SmallTest
testNeverDisallowAddUsers_headlessSystemUserMode_currentUserIsSystemUser()115     public void testNeverDisallowAddUsers_headlessSystemUserMode_currentUserIsSystemUser() {
116         testNeverDisallowAddUsers_headlessSystemUserMode(/* currentUser= */ mSystemUser);
117     }
118 
119     @SmallTest
testNeverDisallowAddUsers_headlessSystemUserMode_currentUserIsFullUser()120     public void testNeverDisallowAddUsers_headlessSystemUserMode_currentUserIsFullUser() {
121         // This scenario should not happen on real life, but the end result would be the same - the
122         // restriction is not set on any user
123         testNeverDisallowAddUsers_headlessSystemUserMode(/* currentUser= */ mFullUser);
124     }
125 
126     // Not a @Test per se, but a helpers that contains the logic of the 2
127     // testNeverDisallowAddUsers_headlessSystemUserMode() - the behavior and logic is identical, the
128     // only difference is the current user
testNeverDisallowAddUsers_headlessSystemUserMode(UserInfo currentUser)129     private void testNeverDisallowAddUsers_headlessSystemUserMode(UserInfo currentUser) {
130         // GIVEN that we have a headless system user, it has a full user as well
131         when(mMockUserManager.getUsers()).thenReturn(Arrays.asList(mSystemUser, mFullUser));
132         final DisallowAddUserTask task =
133                 new DisallowAddUserTask(true, mMockContext, null, mCallback,
134                         mProvisioningAnalyticsTracker);
135 
136         // WHEN running the DisallowAddUserTask as the given user
137         task.run(currentUser.id);
138 
139         // THEN the user restriction should not be set
140         verify(mMockUserManager, never()).setUserRestriction(anyString(), anyBoolean(),
141                 any(UserHandle.class));
142         verify(mCallback).onSuccess(task);
143     }
144 }
145