1 /* 2 * Copyright 2016, 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.ota; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.fail; 21 import static org.mockito.Mockito.mock; 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.app.admin.DevicePolicyManager; 27 import android.content.ComponentName; 28 import android.content.Context; 29 import android.content.pm.PackageManager; 30 import android.content.pm.UserInfo; 31 import android.os.UserHandle; 32 import android.os.UserManager; 33 import android.util.ArraySet; 34 import android.util.Pair; 35 36 import androidx.test.InstrumentationRegistry; 37 import androidx.test.filters.SmallTest; 38 39 import com.android.managedprovisioning.analytics.ProvisioningAnalyticsTracker; 40 import com.android.managedprovisioning.task.AbstractProvisioningTask; 41 import com.android.managedprovisioning.task.DeleteNonRequiredAppsTask; 42 import com.android.managedprovisioning.task.DisallowAddUserTask; 43 import com.android.managedprovisioning.task.InstallExistingPackageTask; 44 import com.android.managedprovisioning.task.MigrateSystemAppsSnapshotTask; 45 import com.android.managedprovisioning.task.UpdateInteractAcrossProfilesAppOpTask; 46 47 import org.junit.Before; 48 import org.junit.Test; 49 import org.mockito.Mock; 50 import org.mockito.MockitoAnnotations; 51 52 import java.util.ArrayList; 53 import java.util.Collections; 54 import java.util.List; 55 import java.util.function.IntFunction; 56 57 /** 58 * Unit tests for {@link OtaController}. 59 */ 60 @SmallTest 61 public class OtaControllerTest { 62 private static final int MANAGED_PROFILE_USER_ID = 15; 63 private static final int MANAGED_USER_USER_ID = 18; 64 65 private static final String DUMMY_SYSTEM_IME_PACKAGE_NAME = 66 "com.android.inputmethod.dummysystemime"; 67 68 private static final ComponentName ADMIN_COMPONENT = new ComponentName("com.test.admin", 69 ".AdminReceiver"); 70 71 private static final IntFunction<ArraySet<String>> NO_MISSING_SYSTEM_IME_PROVIDER 72 = userHandle -> new ArraySet<>(); 73 74 @Mock private Context mContext; 75 @Mock private DevicePolicyManager mDevicePolicyManager; 76 @Mock private PackageManager mPackageManager; 77 @Mock private UserManager mUserManager; 78 @Mock private ProvisioningAnalyticsTracker mProvisioningAnalyticsTracker; 79 80 private TaskExecutor mTaskExecutor; 81 82 private List<Pair<Integer, AbstractProvisioningTask>> mTasks = new ArrayList<>(); 83 private List<UserInfo> mUsers = new ArrayList<>(); 84 private List<UserInfo> mProfiles = new ArrayList<>(); 85 86 @Before setUp()87 public void setUp() { 88 MockitoAnnotations.initMocks(this); 89 90 when(mContext.getSystemService(Context.DEVICE_POLICY_SERVICE)) 91 .thenReturn(mDevicePolicyManager); 92 when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); 93 when(mContext.getPackageManager()).thenReturn(mPackageManager); 94 when(mContext.getResources()) 95 .thenReturn(InstrumentationRegistry.getTargetContext().getResources()); 96 97 when(mUserManager.getUsers()).thenReturn(mUsers); 98 when(mUserManager.getProfiles(UserHandle.USER_SYSTEM)).thenReturn(mProfiles); 99 100 mTaskExecutor = new FakeTaskExecutor(); 101 102 addSystemUser(); 103 } 104 105 @Test testDeviceOwnerSystemUser()106 public void testDeviceOwnerSystemUser() { 107 OtaController controller = new OtaController(mContext, mTaskExecutor, 108 NO_MISSING_SYSTEM_IME_PROVIDER, 109 mProvisioningAnalyticsTracker); 110 111 // GIVEN that there is a device owner on the system user 112 setDeviceOwner(UserHandle.USER_SYSTEM, ADMIN_COMPONENT); 113 114 // WHEN running the OtaController 115 controller.run(); 116 117 // THEN the task list should contain these tasks. 118 assertTaskList( 119 Pair.create(UserHandle.USER_SYSTEM, MigrateSystemAppsSnapshotTask.class), 120 Pair.create(UserHandle.USER_SYSTEM, DeleteNonRequiredAppsTask.class), 121 Pair.create(UserHandle.USER_SYSTEM, DisallowAddUserTask.class), 122 Pair.create(UserHandle.USER_SYSTEM, UpdateInteractAcrossProfilesAppOpTask.class)); 123 124 // THEN cross profile intent filters setter should be invoked for system user 125 verify(mDevicePolicyManager).resetDefaultCrossProfileIntentFilters(UserHandle.USER_SYSTEM); 126 } 127 128 @Test testManagedProfileWithoutMissingSystemIme()129 public void testManagedProfileWithoutMissingSystemIme() { 130 OtaController controller = new OtaController(mContext, mTaskExecutor, 131 NO_MISSING_SYSTEM_IME_PROVIDER, 132 mProvisioningAnalyticsTracker); 133 134 // GIVEN that there is a managed profile 135 addManagedProfile(MANAGED_PROFILE_USER_ID, ADMIN_COMPONENT); 136 137 // WHEN running the OtaController 138 controller.run(); 139 140 // THEN the task list should contain these tasks. 141 assertTaskList( 142 Pair.create(UserHandle.USER_SYSTEM, MigrateSystemAppsSnapshotTask.class), 143 Pair.create(MANAGED_PROFILE_USER_ID, InstallExistingPackageTask.class), 144 Pair.create(MANAGED_PROFILE_USER_ID, DeleteNonRequiredAppsTask.class), 145 Pair.create(UserHandle.USER_SYSTEM, UpdateInteractAcrossProfilesAppOpTask.class)); 146 147 // THEN the cross profile intent filters should be reset 148 verify(mDevicePolicyManager).resetDefaultCrossProfileIntentFilters(UserHandle.USER_SYSTEM); 149 verify(mDevicePolicyManager, never()).resetDefaultCrossProfileIntentFilters( 150 MANAGED_PROFILE_USER_ID); 151 152 // THEN the DISALLOW_WALLPAPER restriction should be set 153 verify(mUserManager).setUserRestriction(UserManager.DISALLOW_WALLPAPER, true, 154 UserHandle.of(MANAGED_PROFILE_USER_ID)); 155 } 156 157 @Test testManagedProfileWithMissingSystemIme()158 public void testManagedProfileWithMissingSystemIme() { 159 IntFunction<ArraySet<String>> missingSystemImeProvider = 160 (IntFunction<ArraySet<String>>) mock(IntFunction.class); 161 162 OtaController controller = new OtaController(mContext, mTaskExecutor, 163 missingSystemImeProvider, 164 mProvisioningAnalyticsTracker); 165 166 // GIVEN that there is a managed profile 167 addManagedProfile(MANAGED_PROFILE_USER_ID, ADMIN_COMPONENT); 168 169 // GIVEN that the managed profile does not have DUMMY_SYSTEM_IME_PACKAGE_NAME. 170 ArraySet<String> missingImes = new ArraySet<>(); 171 missingImes.add(DUMMY_SYSTEM_IME_PACKAGE_NAME); 172 when(missingSystemImeProvider.apply(MANAGED_PROFILE_USER_ID)).thenReturn(missingImes); 173 174 // WHEN running the OtaController 175 controller.run(); 176 177 // THEN the task list should contain these tasks. 178 assertTaskList( 179 Pair.create(UserHandle.USER_SYSTEM, MigrateSystemAppsSnapshotTask.class), 180 Pair.create(MANAGED_PROFILE_USER_ID, InstallExistingPackageTask.class), 181 Pair.create(MANAGED_PROFILE_USER_ID, InstallExistingPackageTask.class), 182 183 Pair.create(MANAGED_PROFILE_USER_ID, DeleteNonRequiredAppsTask.class), 184 Pair.create(UserHandle.USER_SYSTEM, UpdateInteractAcrossProfilesAppOpTask.class)); 185 186 // THEN the cross profile intent filters should be reset 187 verify(mDevicePolicyManager).resetDefaultCrossProfileIntentFilters(UserHandle.USER_SYSTEM); 188 verify(mDevicePolicyManager, never()).resetDefaultCrossProfileIntentFilters( 189 MANAGED_PROFILE_USER_ID); 190 191 // THEN the DISALLOW_WALLPAPER restriction should be set 192 verify(mUserManager).setUserRestriction(UserManager.DISALLOW_WALLPAPER, true, 193 UserHandle.of(MANAGED_PROFILE_USER_ID)); 194 195 // THEN the DUMMY_SYSTEM_IME_PACKAGE_NAME should be installed. 196 assertInstallExistingPackageTask(MANAGED_PROFILE_USER_ID, DUMMY_SYSTEM_IME_PACKAGE_NAME); 197 } 198 199 @Test testManagedUser()200 public void testManagedUser() { 201 OtaController controller = new OtaController(mContext, mTaskExecutor, 202 NO_MISSING_SYSTEM_IME_PROVIDER, mProvisioningAnalyticsTracker); 203 204 // GIVEN that there is a managed profile 205 addManagedUser(MANAGED_USER_USER_ID, ADMIN_COMPONENT); 206 207 // WHEN running the OtaController 208 controller.run(); 209 210 // THEN the task list should contain these tasks. 211 assertTaskList( 212 Pair.create(UserHandle.USER_SYSTEM, MigrateSystemAppsSnapshotTask.class), 213 Pair.create(MANAGED_USER_USER_ID, DeleteNonRequiredAppsTask.class), 214 Pair.create(UserHandle.USER_SYSTEM, UpdateInteractAcrossProfilesAppOpTask.class)); 215 } 216 217 private class FakeTaskExecutor extends TaskExecutor { 218 FakeTaskExecutor()219 public FakeTaskExecutor() { 220 super(); 221 } 222 223 @Override execute(int userId, AbstractProvisioningTask task)224 public synchronized void execute(int userId, AbstractProvisioningTask task) { 225 mTasks.add(Pair.create(userId, task)); 226 } 227 } 228 setDeviceOwner(int userId, ComponentName admin)229 private void setDeviceOwner(int userId, ComponentName admin) { 230 when(mDevicePolicyManager.getDeviceOwnerUserId()).thenReturn(userId); 231 when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(admin); 232 } 233 addManagedProfile(int userId, ComponentName admin)234 private void addManagedProfile(int userId, ComponentName admin) { 235 UserInfo ui = new UserInfo(userId, null, UserInfo.FLAG_MANAGED_PROFILE); 236 mUsers.add(ui); 237 when(mDevicePolicyManager.getProfileOwnerAsUser(userId)).thenReturn(admin); 238 when(mUserManager.getProfiles(userId)).thenReturn(Collections.singletonList(ui)); 239 mProfiles.add(ui); 240 } 241 addManagedUser(int userId, ComponentName admin)242 private void addManagedUser(int userId, ComponentName admin) { 243 UserInfo ui = new UserInfo(userId, null, 0); 244 mUsers.add(ui); 245 when(mDevicePolicyManager.getProfileOwnerAsUser(userId)).thenReturn(admin); 246 } 247 addSystemUser()248 private void addSystemUser() { 249 UserInfo ui = new UserInfo(UserHandle.USER_SYSTEM, null, UserInfo.FLAG_PRIMARY); 250 mUsers.add(ui); 251 mProfiles.add(ui); 252 } 253 assertTaskList(Pair<Integer, Class>.... tasks)254 private void assertTaskList(Pair<Integer, Class>... tasks) { 255 assertEquals(tasks.length, mTasks.size()); 256 257 for (Pair<Integer, Class> task : tasks) { 258 assertTaskListContains(task.first, task.second); 259 } 260 } 261 assertTaskListContains(Integer userId, Class taskClass)262 private void assertTaskListContains(Integer userId, Class taskClass) { 263 for (Pair<Integer, AbstractProvisioningTask> task : mTasks) { 264 if (userId == task.first && taskClass.isInstance(task.second)) { 265 return; 266 } 267 } 268 fail("Task for class " + taskClass + " and userId " + userId + " not executed"); 269 } 270 assertInstallExistingPackageTask(int userId, String packageName)271 private void assertInstallExistingPackageTask(int userId, String packageName) { 272 for (Pair<Integer, AbstractProvisioningTask> task : mTasks) { 273 if (userId != task.first || !InstallExistingPackageTask.class.isInstance(task.second)) { 274 continue; 275 } 276 InstallExistingPackageTask installExistingPackageTask = 277 (InstallExistingPackageTask) task.second; 278 if (packageName.equals(installExistingPackageTask.getPackageName())) { 279 return; 280 } 281 } 282 fail("InstallExistingPackageTask for " + packageName + " and userId " + userId 283 + " not executed"); 284 } 285 } 286