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