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 import android.test.suitebuilder.annotation.SmallTest;
32 
33 import com.android.managedprovisioning.analytics.ProvisioningAnalyticsTracker;
34 
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 
38 import java.util.Arrays;
39 import java.util.Collections;
40 
41 /**
42  * Unit-tests for {@link DisallowAddUserTask}.
43  */
44 public class DisallowAddUserTaskTest extends AndroidTestCase {
45     @Mock private Context mockContext;
46     @Mock private UserManager mockUserManager;
47     @Mock private AbstractProvisioningTask.Callback mCallback;
48     @Mock private ProvisioningAnalyticsTracker mProvisioningAnalyticsTracker;
49 
50     // Normal cases.
51     private UserInfo primaryUser = new UserInfo(0, "Primary",
52             UserInfo.FLAG_PRIMARY | UserInfo.FLAG_ADMIN);
53 
54     // Split-system-user cases.
55     private UserInfo systemUser = new UserInfo(UserHandle.USER_SYSTEM, "System", 0 /* flags */);
56     private UserInfo meatUser = new UserInfo(10, "Primary",
57             UserInfo.FLAG_PRIMARY | UserInfo.FLAG_ADMIN);
58 
59     @Override
setUp()60     public void setUp() {
61         // this is necessary for mockito to work
62         System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString());
63 
64         MockitoAnnotations.initMocks(this);
65 
66         when(mockContext.getSystemService(Context.USER_SERVICE)).thenReturn(mockUserManager);
67         // Setup sensible default responses.
68         when(mockUserManager.hasUserRestriction(anyString(), any(UserHandle.class)))
69                 .thenReturn(false);
70     }
71 
72     @SmallTest
testMaybeDisallowAddUsers_normalSystem()73     public void testMaybeDisallowAddUsers_normalSystem() {
74         // GIVEN that only one user exists on the device and the system doesn't have a split system
75         // user
76         when(mockUserManager.getUsers()).thenReturn(Collections.singletonList(primaryUser));
77         final DisallowAddUserTask task =
78                 new DisallowAddUserTask(false, mockContext, null, mCallback,
79                         mProvisioningAnalyticsTracker);
80 
81         // WHEN running the DisallowAddUserTask on the single user
82         task.run(primaryUser.id);
83 
84         // THEN the user restriction should be set
85         verify(mockUserManager).setUserRestriction(UserManager.DISALLOW_ADD_USER, true,
86                 primaryUser.getUserHandle());
87         verify(mCallback).onSuccess(task);
88     }
89 
90     @SmallTest
testMaybeDisallowAddUsers_normalSystem_restrictionAlreadySetupForOneUser()91     public void testMaybeDisallowAddUsers_normalSystem_restrictionAlreadySetupForOneUser() {
92         // GIVEN that only one user exists on the device and the system doesn't have a split system
93         // user
94         when(mockUserManager.getUsers()).thenReturn(Collections.singletonList(primaryUser));
95         final DisallowAddUserTask task =
96                 new DisallowAddUserTask(false, mockContext, null, mCallback,
97                         mProvisioningAnalyticsTracker);
98 
99         // GIVEN that the user restriction has already been set
100         when(mockUserManager.hasUserRestriction(UserManager.DISALLOW_ADD_USER,
101                 primaryUser.getUserHandle()))
102                 .thenReturn(true);
103 
104         // WHEN running the DisallowAddUserTask on the single user
105         task.run(primaryUser.id);
106 
107         // THEN the user restriction should not be set
108         verify(mockUserManager, never()).setUserRestriction(anyString(), anyBoolean(),
109                 any(UserHandle.class));
110         verify(mCallback).onSuccess(task);
111     }
112 
113     @SmallTest
testMaybeDisallowAddUsers_splitUserSystem_meatUserDeviceOwner()114     public void testMaybeDisallowAddUsers_splitUserSystem_meatUserDeviceOwner() {
115         // GIVEN that we have a split system user and a single meat user on the device
116         when(mockUserManager.getUsers()).thenReturn(Arrays.asList(new UserInfo[]{
117                 systemUser, meatUser}));
118         final DisallowAddUserTask task =
119                 new DisallowAddUserTask(true, mockContext, null, mCallback,
120                         mProvisioningAnalyticsTracker);
121 
122         // WHEN running the DisallowAddUserTask on the meat user
123         task.run(meatUser.id);
124 
125         // THEN the user restriction should be added on both users
126         verify(mockUserManager).setUserRestriction(UserManager.DISALLOW_ADD_USER, true,
127                 systemUser.getUserHandle());
128         verify(mockUserManager).setUserRestriction(UserManager.DISALLOW_ADD_USER, true,
129                 meatUser.getUserHandle());
130         verify(mCallback).onSuccess(task);
131     }
132 
133     @SmallTest
testMaybeDisallowAddUsers_splitUserSystem_systemDeviceOwner()134     public void testMaybeDisallowAddUsers_splitUserSystem_systemDeviceOwner() {
135         // GIVEN that we have a split system user and only the system user on the device
136         when(mockUserManager.getUsers()).thenReturn(Collections.singletonList(systemUser));
137         final DisallowAddUserTask task =
138                 new DisallowAddUserTask(true, mockContext, null, mCallback,
139                         mProvisioningAnalyticsTracker);
140 
141         // WHEN running the DisallowAddUserTask on the system user
142         task.run(systemUser.id);
143 
144         // THEN the user restriction should not be set
145         verify(mockUserManager, never()).setUserRestriction(anyString(), anyBoolean(),
146                 any(UserHandle.class));
147         verify(mCallback).onSuccess(task);
148     }
149 }
150