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 package com.android.server.devicepolicy; 17 18 import android.app.ActivityManagerInternal; 19 import android.app.AlarmManager; 20 import android.app.IActivityManager; 21 import android.app.IActivityTaskManager; 22 import android.app.NotificationManager; 23 import android.app.PendingIntent; 24 import android.app.backup.IBackupManager; 25 import android.app.usage.UsageStatsManagerInternal; 26 import android.content.Context; 27 import android.content.Intent; 28 import android.content.pm.IPackageManager; 29 import android.content.pm.PackageManagerInternal; 30 import android.database.ContentObserver; 31 import android.hardware.usb.UsbManager; 32 import android.media.IAudioService; 33 import android.net.IIpConnectivityMetrics; 34 import android.net.Uri; 35 import android.os.Bundle; 36 import android.os.Looper; 37 import android.os.PowerManagerInternal; 38 import android.os.UserHandle; 39 import android.os.UserManager; 40 import android.permission.IPermissionManager; 41 import android.security.KeyChain; 42 import android.telephony.TelephonyManager; 43 import android.util.ArrayMap; 44 import android.util.Pair; 45 import android.view.IWindowManager; 46 47 import androidx.annotation.NonNull; 48 49 import com.android.internal.util.FunctionalUtils.ThrowingRunnable; 50 import com.android.internal.widget.LockPatternUtils; 51 import com.android.internal.widget.LockSettingsInternal; 52 import com.android.server.PersistentDataBlockManagerInternal; 53 import com.android.server.net.NetworkPolicyManagerInternal; 54 import com.android.server.pm.UserManagerInternal; 55 56 import java.io.File; 57 import java.io.IOException; 58 import java.util.Map; 59 60 /** 61 * Overrides {@link #DevicePolicyManagerService} for dependency injection. 62 */ 63 public class DevicePolicyManagerServiceTestable extends DevicePolicyManagerService { 64 /** 65 * Overrides {@link #Owners} for dependency injection. 66 */ 67 public static class OwnersTestable extends Owners { 68 OwnersTestable(MockSystemServices services)69 public OwnersTestable(MockSystemServices services) { 70 super(services.userManager, services.userManagerInternal, 71 services.packageManagerInternal, services.activityTaskManagerInternal, 72 services.activityManagerInternal, new MockInjector(services)); 73 } 74 75 static class MockInjector extends Injector { 76 private final MockSystemServices mServices; 77 MockInjector(MockSystemServices services)78 private MockInjector(MockSystemServices services) { 79 mServices = services; 80 } 81 82 @Override environmentGetDataSystemDirectory()83 File environmentGetDataSystemDirectory() { 84 return mServices.dataDir; 85 } 86 87 @Override environmentGetUserSystemDirectory(int userId)88 File environmentGetUserSystemDirectory(int userId) { 89 return mServices.environment.getUserSystemDirectory(userId); 90 } 91 } 92 } 93 94 public final DpmMockContext context; 95 protected final MockInjector mMockInjector; 96 DevicePolicyManagerServiceTestable(MockSystemServices services, DpmMockContext context)97 public DevicePolicyManagerServiceTestable(MockSystemServices services, DpmMockContext context) { 98 this(new MockInjector(services, context)); 99 } 100 DevicePolicyManagerServiceTestable(MockInjector injector)101 private DevicePolicyManagerServiceTestable(MockInjector injector) { 102 super(injector); 103 mMockInjector = injector; 104 this.context = injector.context; 105 } 106 notifyChangeToContentObserver(Uri uri, int userHandle)107 public void notifyChangeToContentObserver(Uri uri, int userHandle) { 108 ContentObserver co = mMockInjector.mContentObservers.get(new Pair<>(uri, userHandle)); 109 if (co != null) { 110 co.onChange(false, uri, userHandle); // notify synchronously 111 } 112 113 // Notify USER_ALL observer too. 114 co = mMockInjector.mContentObservers.get(new Pair<>(uri, UserHandle.USER_ALL)); 115 if (co != null) { 116 co.onChange(false, uri, userHandle); // notify synchronously 117 } 118 } 119 120 static class MockInjector extends Injector { 121 122 public final DpmMockContext context; 123 private final MockSystemServices services; 124 125 // Key is a pair of uri and userId 126 private final Map<Pair<Uri, Integer>, ContentObserver> mContentObservers = new ArrayMap<>(); 127 128 // Used as an override when set to nonzero. 129 private long mCurrentTimeMillis = 0; 130 131 private final Map<Long, Pair<String, Integer>> mEnabledChanges = new ArrayMap<>(); 132 MockInjector(MockSystemServices services, DpmMockContext context)133 public MockInjector(MockSystemServices services, DpmMockContext context) { 134 super(context); 135 this.services = services; 136 this.context = context; 137 } 138 139 @Override newOwners()140 Owners newOwners() { 141 return new OwnersTestable(services); 142 } 143 144 @Override getUserManager()145 UserManager getUserManager() { 146 return services.userManager; 147 } 148 149 @Override getUserManagerInternal()150 UserManagerInternal getUserManagerInternal() { 151 return services.userManagerInternal; 152 } 153 154 @Override getUsageStatsManagerInternal()155 UsageStatsManagerInternal getUsageStatsManagerInternal() { 156 return services.usageStatsManagerInternal; 157 } 158 159 @Override getNetworkPolicyManagerInternal()160 NetworkPolicyManagerInternal getNetworkPolicyManagerInternal() { 161 return services.networkPolicyManagerInternal; 162 } 163 164 @Override getPackageManagerInternal()165 PackageManagerInternal getPackageManagerInternal() { 166 return services.packageManagerInternal; 167 } 168 169 @Override getPowerManagerInternal()170 PowerManagerInternal getPowerManagerInternal() { 171 return services.powerManagerInternal; 172 } 173 174 @Override getNotificationManager()175 NotificationManager getNotificationManager() { 176 return services.notificationManager; 177 } 178 179 @Override getIIpConnectivityMetrics()180 IIpConnectivityMetrics getIIpConnectivityMetrics() { 181 return services.iipConnectivityMetrics; 182 } 183 184 @Override getIWindowManager()185 IWindowManager getIWindowManager() { 186 return services.iwindowManager; 187 } 188 189 @Override getIActivityManager()190 IActivityManager getIActivityManager() { 191 return services.iactivityManager; 192 } 193 194 @Override getIActivityTaskManager()195 IActivityTaskManager getIActivityTaskManager() { 196 return services.iactivityTaskManager; 197 } 198 199 @Override getActivityManagerInternal()200 ActivityManagerInternal getActivityManagerInternal() { 201 return services.activityManagerInternal; 202 } 203 204 @Override getIPackageManager()205 IPackageManager getIPackageManager() { 206 return services.ipackageManager; 207 } 208 209 @Override getIPermissionManager()210 IPermissionManager getIPermissionManager() { 211 return services.ipermissionManager; 212 } 213 214 @Override getIBackupManager()215 IBackupManager getIBackupManager() { 216 return services.ibackupManager; 217 } 218 219 @Override getLockSettingsInternal()220 LockSettingsInternal getLockSettingsInternal() { 221 return services.lockSettingsInternal; 222 } 223 224 @Override getIAudioService()225 IAudioService getIAudioService() { 226 return services.iaudioService; 227 } 228 229 @Override getPersistentDataBlockManagerInternal()230 PersistentDataBlockManagerInternal getPersistentDataBlockManagerInternal() { 231 return services.persistentDataBlockManagerInternal; 232 } 233 234 @Override getMyLooper()235 Looper getMyLooper() { 236 return Looper.getMainLooper(); 237 } 238 239 @Override getAlarmManager()240 AlarmManager getAlarmManager() {return services.alarmManager;} 241 242 @Override newLockPatternUtils()243 LockPatternUtils newLockPatternUtils() { 244 return services.lockPatternUtils; 245 } 246 247 @Override getUsbManager()248 UsbManager getUsbManager() { 249 return services.usbManager; 250 } 251 252 @Override storageManagerIsFileBasedEncryptionEnabled()253 boolean storageManagerIsFileBasedEncryptionEnabled() { 254 return services.storageManager.isFileBasedEncryptionEnabled(); 255 } 256 257 @Override storageManagerIsNonDefaultBlockEncrypted()258 boolean storageManagerIsNonDefaultBlockEncrypted() { 259 return services.storageManager.isNonDefaultBlockEncrypted(); 260 } 261 262 @Override storageManagerIsEncrypted()263 boolean storageManagerIsEncrypted() { 264 return services.storageManager.isEncrypted(); 265 } 266 267 @Override storageManagerIsEncryptable()268 boolean storageManagerIsEncryptable() { 269 return services.storageManager.isEncryptable(); 270 } 271 272 @Override getDevicePolicyFilePathForSystemUser()273 String getDevicePolicyFilePathForSystemUser() { 274 return services.systemUserDataDir.getAbsolutePath() + "/"; 275 } 276 277 @Override binderClearCallingIdentity()278 long binderClearCallingIdentity() { 279 return context.binder.clearCallingIdentity(); 280 } 281 282 @Override binderRestoreCallingIdentity(long token)283 void binderRestoreCallingIdentity(long token) { 284 context.binder.restoreCallingIdentity(token); 285 } 286 287 @Override binderWithCleanCallingIdentity(@onNull ThrowingRunnable action)288 void binderWithCleanCallingIdentity(@NonNull ThrowingRunnable action) { 289 context.binder.withCleanCallingIdentity(action); 290 } 291 292 @Override binderGetCallingUid()293 int binderGetCallingUid() { 294 return context.binder.getCallingUid(); 295 } 296 297 @Override binderGetCallingPid()298 int binderGetCallingPid() { 299 return context.binder.getCallingPid(); 300 } 301 302 @Override binderGetCallingUserHandle()303 UserHandle binderGetCallingUserHandle() { 304 return context.binder.getCallingUserHandle(); 305 } 306 307 @Override binderIsCallingUidMyUid()308 boolean binderIsCallingUidMyUid() { 309 return context.binder.isCallerUidMyUid(); 310 } 311 312 @Override environmentGetUserSystemDirectory(int userId)313 File environmentGetUserSystemDirectory(int userId) { 314 return services.environment.getUserSystemDirectory(userId); 315 } 316 317 @Override powerManagerGoToSleep(long time, int reason, int flags)318 void powerManagerGoToSleep(long time, int reason, int flags) { 319 services.powerManager.goToSleep(time, reason, flags); 320 } 321 322 @Override powerManagerReboot(String reason)323 void powerManagerReboot(String reason) { 324 services.powerManager.reboot(reason); 325 } 326 327 @Override recoverySystemRebootWipeUserData(boolean shutdown, String reason, boolean force, boolean wipeEuicc, boolean wipeExtRequested, boolean wipeResetProtectionData)328 boolean recoverySystemRebootWipeUserData(boolean shutdown, String reason, boolean force, 329 boolean wipeEuicc, boolean wipeExtRequested, boolean wipeResetProtectionData) 330 throws IOException { 331 return services.recoverySystem.rebootWipeUserData(shutdown, reason, force, wipeEuicc, 332 wipeExtRequested, wipeResetProtectionData); 333 } 334 335 @Override systemPropertiesGetBoolean(String key, boolean def)336 boolean systemPropertiesGetBoolean(String key, boolean def) { 337 return services.systemProperties.getBoolean(key, def); 338 } 339 340 @Override systemPropertiesGetLong(String key, long def)341 long systemPropertiesGetLong(String key, long def) { 342 return services.systemProperties.getLong(key, def); 343 } 344 345 @Override systemPropertiesGet(String key, String def)346 String systemPropertiesGet(String key, String def) { 347 return services.systemProperties.get(key, def); 348 } 349 350 @Override systemPropertiesGet(String key)351 String systemPropertiesGet(String key) { 352 return services.systemProperties.get(key); 353 } 354 355 @Override systemPropertiesSet(String key, String value)356 void systemPropertiesSet(String key, String value) { 357 services.systemProperties.set(key, value); 358 } 359 360 @Override userManagerIsHeadlessSystemUserMode()361 boolean userManagerIsHeadlessSystemUserMode() { 362 return services.userManagerForMock.isHeadlessSystemUserMode(); 363 } 364 365 @Override pendingIntentGetActivityAsUser(Context context, int requestCode, Intent intent, int flags, Bundle options, UserHandle user)366 PendingIntent pendingIntentGetActivityAsUser(Context context, int requestCode, 367 Intent intent, int flags, Bundle options, UserHandle user) { 368 return null; 369 } 370 371 @Override pendingIntentGetBroadcast(Context context, int requestCode, Intent intent, int flags)372 PendingIntent pendingIntentGetBroadcast(Context context, int requestCode, 373 Intent intent, int flags) { 374 return null; 375 } 376 377 @Override registerContentObserver(Uri uri, boolean notifyForDescendents, ContentObserver observer, int userHandle)378 void registerContentObserver(Uri uri, boolean notifyForDescendents, 379 ContentObserver observer, int userHandle) { 380 mContentObservers.put(new Pair<Uri, Integer>(uri, userHandle), observer); 381 } 382 383 @Override settingsSecureGetIntForUser(String name, int def, int userHandle)384 int settingsSecureGetIntForUser(String name, int def, int userHandle) { 385 return services.settings.settingsSecureGetIntForUser(name, def, userHandle); 386 } 387 388 @Override settingsSecureGetStringForUser(String name, int userHandle)389 String settingsSecureGetStringForUser(String name, int userHandle) { 390 return services.settings.settingsSecureGetStringForUser(name, userHandle); 391 } 392 393 @Override settingsSecurePutIntForUser(String name, int value, int userHandle)394 void settingsSecurePutIntForUser(String name, int value, int userHandle) { 395 services.settings.settingsSecurePutIntForUser(name, value, userHandle); 396 } 397 398 @Override settingsSecurePutStringForUser(String name, String value, int userHandle)399 void settingsSecurePutStringForUser(String name, String value, int userHandle) { 400 services.settings.settingsSecurePutStringForUser(name, value, userHandle); 401 } 402 403 @Override settingsGlobalPutStringForUser(String name, String value, int userHandle)404 void settingsGlobalPutStringForUser(String name, String value, int userHandle) { 405 services.settings.settingsGlobalPutStringForUser(name, value, userHandle); 406 } 407 408 @Override settingsSecurePutInt(String name, int value)409 void settingsSecurePutInt(String name, int value) { 410 services.settings.settingsSecurePutInt(name, value); 411 } 412 413 @Override settingsGlobalPutInt(String name, int value)414 void settingsGlobalPutInt(String name, int value) { 415 services.settings.settingsGlobalPutInt(name, value); 416 } 417 418 @Override settingsSecurePutString(String name, String value)419 void settingsSecurePutString(String name, String value) { 420 services.settings.settingsSecurePutString(name, value); 421 } 422 423 @Override settingsGlobalPutString(String name, String value)424 void settingsGlobalPutString(String name, String value) { 425 services.settings.settingsGlobalPutString(name, value); 426 } 427 428 @Override settingsSystemPutStringForUser(String name, String value, int userId)429 void settingsSystemPutStringForUser(String name, String value, int userId) { 430 services.settings.settingsSystemPutStringForUser(name, value, userId); 431 } 432 433 @Override settingsGlobalGetInt(String name, int def)434 int settingsGlobalGetInt(String name, int def) { 435 return services.settings.settingsGlobalGetInt(name, def); 436 } 437 438 @Override settingsGlobalGetString(String name)439 String settingsGlobalGetString(String name) { 440 return services.settings.settingsGlobalGetString(name); 441 } 442 443 @Override securityLogSetLoggingEnabledProperty(boolean enabled)444 void securityLogSetLoggingEnabledProperty(boolean enabled) { 445 services.settings.securityLogSetLoggingEnabledProperty(enabled); 446 } 447 448 @Override securityLogGetLoggingEnabledProperty()449 boolean securityLogGetLoggingEnabledProperty() { 450 return services.settings.securityLogGetLoggingEnabledProperty(); 451 } 452 453 @Override securityLogIsLoggingEnabled()454 boolean securityLogIsLoggingEnabled() { 455 return services.settings.securityLogIsLoggingEnabled(); 456 } 457 458 @Override getTelephonyManager()459 TelephonyManager getTelephonyManager() { 460 return services.telephonyManager; 461 } 462 463 @Override isBuildDebuggable()464 boolean isBuildDebuggable() { 465 return services.buildMock.isDebuggable; 466 } 467 468 @Override keyChainBind()469 KeyChain.KeyChainConnection keyChainBind() { 470 return services.keyChainConnection; 471 } 472 473 @Override keyChainBindAsUser(UserHandle user)474 KeyChain.KeyChainConnection keyChainBindAsUser(UserHandle user) { 475 return services.keyChainConnection; 476 } 477 478 @Override postOnSystemServerInitThreadPool(Runnable runnable)479 void postOnSystemServerInitThreadPool(Runnable runnable) { 480 runnable.run(); 481 } 482 483 @Override newTransferOwnershipMetadataManager()484 public TransferOwnershipMetadataManager newTransferOwnershipMetadataManager() { 485 return new TransferOwnershipMetadataManager( 486 new TransferOwnershipMetadataManagerTest.MockInjector()); 487 } 488 489 @Override runCryptoSelfTest()490 public void runCryptoSelfTest() {} 491 492 @Override getPersonalAppsForSuspension(int userId)493 public String[] getPersonalAppsForSuspension(int userId) { 494 return new String[]{}; 495 } 496 setSystemCurrentTimeMillis(long value)497 public void setSystemCurrentTimeMillis(long value) { 498 mCurrentTimeMillis = value; 499 } 500 501 @Override systemCurrentTimeMillis()502 public long systemCurrentTimeMillis() { 503 return mCurrentTimeMillis != 0 ? mCurrentTimeMillis : System.currentTimeMillis(); 504 } 505 setChangeEnabledForPackage( long changeId, boolean enabled, String packageName, int userId)506 public void setChangeEnabledForPackage( 507 long changeId, boolean enabled, String packageName, int userId) { 508 if (enabled) { 509 mEnabledChanges.put(changeId, Pair.create(packageName, userId)); 510 } else { 511 mEnabledChanges.remove(changeId); 512 } 513 } 514 clearEnabledChanges()515 public void clearEnabledChanges() { 516 mEnabledChanges.clear(); 517 } 518 519 @Override isChangeEnabled(long changeId, String packageName, int userId)520 public boolean isChangeEnabled(long changeId, String packageName, int userId) { 521 Pair<String, Integer> packageAndUser = mEnabledChanges.get(changeId); 522 if (packageAndUser == null) { 523 return false; 524 } 525 526 if (!packageAndUser.first.equals(packageName) 527 || !packageAndUser.second.equals(userId)) { 528 return false; 529 } 530 531 return true; 532 } 533 } 534 } 535