Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 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 | |
Andrew Scull | 507d11c | 2017-05-03 17:19:01 +0100 | [diff] [blame] | 17 | package com.android.server.locksettings; |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 18 | |
| 19 | import static org.mockito.Mockito.mock; |
| 20 | |
| 21 | import android.app.IActivityManager; |
| 22 | import android.content.Context; |
| 23 | import android.os.Handler; |
Rubin Xu | 7cf4509 | 2017-08-28 11:47:35 +0100 | [diff] [blame] | 24 | import android.os.Looper; |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 25 | import android.os.Process; |
| 26 | import android.os.RemoteException; |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 27 | import android.os.storage.IStorageManager; |
| 28 | import android.security.KeyStore; |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 29 | import android.security.keystore.KeyPermanentlyInvalidatedException; |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 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; |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 44 | private SyntheticPasswordManager mSpManager; |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 45 | |
| 46 | public MockInjector(Context context, LockSettingsStorage storage, KeyStore keyStore, |
| 47 | IActivityManager activityManager, LockPatternUtils lockPatternUtils, |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 48 | IStorageManager storageManager, SyntheticPasswordManager spManager) { |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 49 | super(context); |
| 50 | mLockSettingsStorage = storage; |
| 51 | mKeyStore = keyStore; |
| 52 | mActivityManager = activityManager; |
| 53 | mLockPatternUtils = lockPatternUtils; |
| 54 | mStorageManager = storageManager; |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 55 | mSpManager = spManager; |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public Handler getHandler() { |
Rubin Xu | 7cf4509 | 2017-08-28 11:47:35 +0100 | [diff] [blame] | 60 | return new Handler(Looper.getMainLooper()); |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 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 | } |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 97 | |
| 98 | @Override |
| 99 | public SyntheticPasswordManager getSyntheticPasswordManager(LockSettingsStorage storage) { |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 100 | return mSpManager; |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public int binderGetCallingUid() { |
| 105 | return Process.SYSTEM_UID; |
| 106 | } |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | protected LockSettingsServiceTestable(Context context, LockPatternUtils lockPatternUtils, |
Rubin Xu | 16c823e | 2017-06-27 14:44:58 +0100 | [diff] [blame] | 110 | LockSettingsStorage storage, FakeGateKeeperService gatekeeper, KeyStore keystore, |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 111 | IStorageManager storageManager, IActivityManager mActivityManager, |
| 112 | SyntheticPasswordManager spManager) { |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 113 | super(new MockInjector(context, storage, keystore, mActivityManager, lockPatternUtils, |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 114 | storageManager, spManager)); |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 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 |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 124 | protected String getDecryptedPasswordForTiedProfile(int userId) throws FileNotFoundException, |
| 125 | KeyPermanentlyInvalidatedException { |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 126 | byte[] storedData = mStorage.readChildProfileLock(userId); |
| 127 | if (storedData == null) { |
| 128 | throw new FileNotFoundException("Child profile lock file not found"); |
| 129 | } |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 130 | try { |
| 131 | if (mGateKeeperService.getSecureUserId(userId) == 0) { |
| 132 | throw new KeyPermanentlyInvalidatedException(); |
| 133 | } |
| 134 | } catch (RemoteException e) { |
| 135 | // shouldn't happen. |
| 136 | } |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 137 | return new String(storedData); |
| 138 | } |
Rubin Xu | 16c823e | 2017-06-27 14:44:58 +0100 | [diff] [blame] | 139 | |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 140 | } |