1 /* 2 * Copyright (C) 2017 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; 18 19 import static org.mockito.Mockito.mock; 20 21 import android.app.IActivityManager; 22 import android.content.Context; 23 import android.os.Handler; 24 import android.os.Process; 25 import android.os.RemoteException; 26 import android.os.storage.IStorageManager; 27 import android.security.KeyStore; 28 import android.security.keystore.KeyPermanentlyInvalidatedException; 29 30 import com.android.internal.widget.LockPatternUtils; 31 32 import java.io.FileNotFoundException; 33 34 public class LockSettingsServiceTestable extends LockSettingsService { 35 36 private static class MockInjector extends LockSettingsService.Injector { 37 38 private LockSettingsStorage mLockSettingsStorage; 39 private KeyStore mKeyStore; 40 private IActivityManager mActivityManager; 41 private LockPatternUtils mLockPatternUtils; 42 private IStorageManager mStorageManager; 43 private MockGateKeeperService mGatekeeper; 44 MockInjector(Context context, LockSettingsStorage storage, KeyStore keyStore, IActivityManager activityManager, LockPatternUtils lockPatternUtils, IStorageManager storageManager, MockGateKeeperService gatekeeper)45 public MockInjector(Context context, LockSettingsStorage storage, KeyStore keyStore, 46 IActivityManager activityManager, LockPatternUtils lockPatternUtils, 47 IStorageManager storageManager, MockGateKeeperService gatekeeper) { 48 super(context); 49 mLockSettingsStorage = storage; 50 mKeyStore = keyStore; 51 mActivityManager = activityManager; 52 mLockPatternUtils = lockPatternUtils; 53 mStorageManager = storageManager; 54 mGatekeeper = gatekeeper; 55 } 56 57 @Override getHandler()58 public Handler getHandler() { 59 return mock(Handler.class); 60 } 61 62 @Override getStorage()63 public LockSettingsStorage getStorage() { 64 return mLockSettingsStorage; 65 } 66 67 @Override getStrongAuth()68 public LockSettingsStrongAuth getStrongAuth() { 69 return mock(LockSettingsStrongAuth.class); 70 } 71 72 @Override getStrongAuthTracker()73 public SynchronizedStrongAuthTracker getStrongAuthTracker() { 74 return mock(SynchronizedStrongAuthTracker.class); 75 } 76 77 @Override getActivityManager()78 public IActivityManager getActivityManager() { 79 return mActivityManager; 80 } 81 82 @Override getLockPatternUtils()83 public LockPatternUtils getLockPatternUtils() { 84 return mLockPatternUtils; 85 } 86 87 @Override getKeyStore()88 public KeyStore getKeyStore() { 89 return mKeyStore; 90 } 91 92 @Override getStorageManager()93 public IStorageManager getStorageManager() { 94 return mStorageManager; 95 } 96 97 @Override getSyntheticPasswordManager(LockSettingsStorage storage)98 public SyntheticPasswordManager getSyntheticPasswordManager(LockSettingsStorage storage) { 99 return new MockSyntheticPasswordManager(storage, mGatekeeper); 100 } 101 102 @Override binderGetCallingUid()103 public int binderGetCallingUid() { 104 return Process.SYSTEM_UID; 105 } 106 107 108 } 109 LockSettingsServiceTestable(Context context, LockPatternUtils lockPatternUtils, LockSettingsStorage storage, MockGateKeeperService gatekeeper, KeyStore keystore, IStorageManager storageManager, IActivityManager mActivityManager)110 protected LockSettingsServiceTestable(Context context, LockPatternUtils lockPatternUtils, 111 LockSettingsStorage storage, MockGateKeeperService gatekeeper, KeyStore keystore, 112 IStorageManager storageManager, IActivityManager mActivityManager) { 113 super(new MockInjector(context, storage, keystore, mActivityManager, lockPatternUtils, 114 storageManager, gatekeeper)); 115 mGateKeeperService = gatekeeper; 116 } 117 118 @Override tieProfileLockToParent(int userId, String password)119 protected void tieProfileLockToParent(int userId, String password) { 120 mStorage.writeChildProfileLock(userId, password.getBytes()); 121 } 122 123 @Override getDecryptedPasswordForTiedProfile(int userId)124 protected String getDecryptedPasswordForTiedProfile(int userId) throws FileNotFoundException, KeyPermanentlyInvalidatedException { 125 byte[] storedData = mStorage.readChildProfileLock(userId); 126 if (storedData == null) { 127 throw new FileNotFoundException("Child profile lock file not found"); 128 } 129 try { 130 if (mGateKeeperService.getSecureUserId(userId) == 0) { 131 throw new KeyPermanentlyInvalidatedException(); 132 } 133 } catch (RemoteException e) { 134 // shouldn't happen. 135 } 136 return new String(storedData); 137 } 138 } 139