Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 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 | |
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.Matchers.any; |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 20 | import static org.mockito.Matchers.anyBoolean; |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 21 | import static org.mockito.Matchers.anyInt; |
| 22 | import static org.mockito.Matchers.eq; |
Rubin Xu | b31be1b | 2017-06-16 17:08:21 +0100 | [diff] [blame] | 23 | import static org.mockito.Mockito.doAnswer; |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 24 | import static org.mockito.Mockito.mock; |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 25 | import static org.mockito.Mockito.when; |
| 26 | |
| 27 | import android.app.IActivityManager; |
Andrew Scull | f49794b | 2018-04-13 12:01:25 +0100 | [diff] [blame] | 28 | import android.app.KeyguardManager; |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 29 | import android.app.NotificationManager; |
Rubin Xu | 8b30ec3 | 2017-03-05 00:47:09 +0000 | [diff] [blame] | 30 | import android.app.admin.DevicePolicyManager; |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 31 | import android.app.admin.DevicePolicyManagerInternal; |
Rubin Xu | 0f1e56d | 2019-08-23 13:34:25 +0100 | [diff] [blame] | 32 | import android.app.admin.DeviceStateCache; |
Rubin Xu | 16c823e | 2017-06-27 14:44:58 +0100 | [diff] [blame] | 33 | import android.app.trust.TrustManager; |
Rubin Xu | 8b30ec3 | 2017-03-05 00:47:09 +0000 | [diff] [blame] | 34 | import android.content.ComponentName; |
Alex Johnston | 6183cf9 | 2019-10-03 15:59:03 +0100 | [diff] [blame] | 35 | import android.content.pm.PackageManager; |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 36 | import android.content.pm.UserInfo; |
Andrew Scull | e6527c1 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 37 | import android.hardware.authsecret.V1_0.IAuthSecret; |
Rubin Xu | f526a69 | 2019-10-14 11:42:41 +0100 | [diff] [blame^] | 38 | import android.hardware.face.Face; |
Alex Johnston | 6183cf9 | 2019-10-03 15:59:03 +0100 | [diff] [blame] | 39 | import android.hardware.face.FaceManager; |
Rubin Xu | f526a69 | 2019-10-14 11:42:41 +0100 | [diff] [blame^] | 40 | import android.hardware.fingerprint.Fingerprint; |
Alex Johnston | 6183cf9 | 2019-10-03 15:59:03 +0100 | [diff] [blame] | 41 | import android.hardware.fingerprint.FingerprintManager; |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 42 | import android.os.FileUtils; |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 43 | import android.os.IProgressListener; |
Rubin Xu | b31be1b | 2017-06-16 17:08:21 +0100 | [diff] [blame] | 44 | import android.os.RemoteException; |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 45 | import android.os.UserManager; |
Rubin Xu | 0f1e56d | 2019-08-23 13:34:25 +0100 | [diff] [blame] | 46 | import android.os.UserManagerInternal; |
Rubin Xu | b31be1b | 2017-06-16 17:08:21 +0100 | [diff] [blame] | 47 | import android.os.storage.IStorageManager; |
Lenka Trochtova | 66c492a | 2018-12-06 11:29:21 +0100 | [diff] [blame] | 48 | import android.os.storage.StorageManager; |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 49 | import android.security.KeyStore; |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 50 | import android.test.AndroidTestCase; |
| 51 | |
Rubin Xu | 16c823e | 2017-06-27 14:44:58 +0100 | [diff] [blame] | 52 | import com.android.internal.widget.ILockSettings; |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 53 | import com.android.internal.widget.LockPatternUtils; |
Rubin Xu | fcd49f9 | 2017-08-24 18:21:52 +0100 | [diff] [blame] | 54 | import com.android.internal.widget.LockSettingsInternal; |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 55 | import com.android.server.LocalServices; |
Annie Meng | 086ddc8 | 2019-03-29 17:43:35 +0000 | [diff] [blame] | 56 | import com.android.server.locksettings.recoverablekeystore.RecoverableKeyStoreManager; |
Lenka Trochtova | 66c492a | 2018-12-06 11:29:21 +0100 | [diff] [blame] | 57 | import com.android.server.wm.WindowManagerInternal; |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 58 | |
| 59 | import org.mockito.invocation.InvocationOnMock; |
| 60 | import org.mockito.stubbing.Answer; |
| 61 | |
| 62 | import java.io.File; |
Andrew Scull | 8e87af5 | 2017-03-03 15:38:48 +0000 | [diff] [blame] | 63 | import java.util.ArrayList; |
Charles He | dec0540 | 2017-04-21 13:45:34 +0100 | [diff] [blame] | 64 | import java.util.Arrays; |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 65 | |
| 66 | |
Pavel Grafov | 57f1b66 | 2019-03-27 14:55:38 +0000 | [diff] [blame] | 67 | public abstract class BaseLockSettingsServiceTests extends AndroidTestCase { |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 68 | protected static final int PRIMARY_USER_ID = 0; |
| 69 | protected static final int MANAGED_PROFILE_USER_ID = 12; |
Andrew Scull | 8e87af5 | 2017-03-03 15:38:48 +0000 | [diff] [blame] | 70 | protected static final int TURNED_OFF_PROFILE_USER_ID = 17; |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 71 | protected static final int SECONDARY_USER_ID = 20; |
| 72 | |
| 73 | private static final UserInfo PRIMARY_USER_INFO = new UserInfo(PRIMARY_USER_ID, null, null, |
| 74 | UserInfo.FLAG_INITIALIZED | UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY); |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 75 | private static final UserInfo SECONDARY_USER_INFO = new UserInfo(SECONDARY_USER_ID, null, null, |
| 76 | UserInfo.FLAG_INITIALIZED); |
| 77 | |
Andrew Scull | 8e87af5 | 2017-03-03 15:38:48 +0000 | [diff] [blame] | 78 | private ArrayList<UserInfo> mPrimaryUserProfiles = new ArrayList<>(); |
| 79 | |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 80 | LockSettingsService mService; |
Rubin Xu | fcd49f9 | 2017-08-24 18:21:52 +0100 | [diff] [blame] | 81 | LockSettingsInternal mLocalService; |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 82 | |
| 83 | MockLockSettingsContext mContext; |
| 84 | LockSettingsStorageTestable mStorage; |
| 85 | |
| 86 | LockPatternUtils mLockPatternUtils; |
Rubin Xu | 16c823e | 2017-06-27 14:44:58 +0100 | [diff] [blame] | 87 | FakeGateKeeperService mGateKeeperService; |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 88 | NotificationManager mNotificationManager; |
| 89 | UserManager mUserManager; |
Rubin Xu | b31be1b | 2017-06-16 17:08:21 +0100 | [diff] [blame] | 90 | FakeStorageManager mStorageManager; |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 91 | IActivityManager mActivityManager; |
Rubin Xu | 8b30ec3 | 2017-03-05 00:47:09 +0000 | [diff] [blame] | 92 | DevicePolicyManager mDevicePolicyManager; |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 93 | DevicePolicyManagerInternal mDevicePolicyManagerInternal; |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 94 | KeyStore mKeyStore; |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 95 | MockSyntheticPasswordManager mSpManager; |
Andrew Scull | e6527c1 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 96 | IAuthSecret mAuthSecretService; |
Lenka Trochtova | 66c492a | 2018-12-06 11:29:21 +0100 | [diff] [blame] | 97 | WindowManagerInternal mMockWindowManager; |
David Anderson | 6ebc25b | 2019-02-12 16:25:56 -0800 | [diff] [blame] | 98 | FakeGsiService mGsiService; |
David Anderson | 28dea68 | 2019-02-20 13:37:51 -0800 | [diff] [blame] | 99 | PasswordSlotManagerTestable mPasswordSlotManager; |
Annie Meng | 086ddc8 | 2019-03-29 17:43:35 +0000 | [diff] [blame] | 100 | RecoverableKeyStoreManager mRecoverableKeyStoreManager; |
Rubin Xu | 0f1e56d | 2019-08-23 13:34:25 +0100 | [diff] [blame] | 101 | UserManagerInternal mUserManagerInternal; |
| 102 | DeviceStateCache mDeviceStateCache; |
Alex Johnston | 6183cf9 | 2019-10-03 15:59:03 +0100 | [diff] [blame] | 103 | FingerprintManager mFingerprintManager; |
| 104 | FaceManager mFaceManager; |
| 105 | PackageManager mPackageManager; |
Lenka Trochtova | 66c492a | 2018-12-06 11:29:21 +0100 | [diff] [blame] | 106 | protected boolean mHasSecureLockScreen; |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 107 | |
| 108 | @Override |
| 109 | protected void setUp() throws Exception { |
| 110 | super.setUp(); |
| 111 | |
Rubin Xu | 16c823e | 2017-06-27 14:44:58 +0100 | [diff] [blame] | 112 | mGateKeeperService = new FakeGateKeeperService(); |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 113 | mNotificationManager = mock(NotificationManager.class); |
| 114 | mUserManager = mock(UserManager.class); |
Rubin Xu | b31be1b | 2017-06-16 17:08:21 +0100 | [diff] [blame] | 115 | mStorageManager = new FakeStorageManager(); |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 116 | mActivityManager = mock(IActivityManager.class); |
Rubin Xu | 8b30ec3 | 2017-03-05 00:47:09 +0000 | [diff] [blame] | 117 | mDevicePolicyManager = mock(DevicePolicyManager.class); |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 118 | mDevicePolicyManagerInternal = mock(DevicePolicyManagerInternal.class); |
Lenka Trochtova | 66c492a | 2018-12-06 11:29:21 +0100 | [diff] [blame] | 119 | mMockWindowManager = mock(WindowManagerInternal.class); |
David Anderson | 6ebc25b | 2019-02-12 16:25:56 -0800 | [diff] [blame] | 120 | mGsiService = new FakeGsiService(); |
David Anderson | 28dea68 | 2019-02-20 13:37:51 -0800 | [diff] [blame] | 121 | mPasswordSlotManager = new PasswordSlotManagerTestable(); |
Annie Meng | 086ddc8 | 2019-03-29 17:43:35 +0000 | [diff] [blame] | 122 | mRecoverableKeyStoreManager = mock(RecoverableKeyStoreManager.class); |
Rubin Xu | 0f1e56d | 2019-08-23 13:34:25 +0100 | [diff] [blame] | 123 | mUserManagerInternal = mock(UserManagerInternal.class); |
| 124 | mDeviceStateCache = mock(DeviceStateCache.class); |
Alex Johnston | 6183cf9 | 2019-10-03 15:59:03 +0100 | [diff] [blame] | 125 | mFingerprintManager = mock(FingerprintManager.class); |
| 126 | mFaceManager = mock(FaceManager.class); |
| 127 | mPackageManager = mock(PackageManager.class); |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 128 | |
Rubin Xu | fcd49f9 | 2017-08-24 18:21:52 +0100 | [diff] [blame] | 129 | LocalServices.removeServiceForTest(LockSettingsInternal.class); |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 130 | LocalServices.removeServiceForTest(DevicePolicyManagerInternal.class); |
Lenka Trochtova | 66c492a | 2018-12-06 11:29:21 +0100 | [diff] [blame] | 131 | LocalServices.removeServiceForTest(WindowManagerInternal.class); |
Andrew Scull | 1416bd0 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 132 | LocalServices.addService(DevicePolicyManagerInternal.class, mDevicePolicyManagerInternal); |
Lenka Trochtova | 66c492a | 2018-12-06 11:29:21 +0100 | [diff] [blame] | 133 | LocalServices.addService(WindowManagerInternal.class, mMockWindowManager); |
Rubin Xu | 7b7424b | 2017-03-31 18:03:20 +0100 | [diff] [blame] | 134 | |
Rubin Xu | 8b30ec3 | 2017-03-05 00:47:09 +0000 | [diff] [blame] | 135 | mContext = new MockLockSettingsContext(getContext(), mUserManager, mNotificationManager, |
Andrew Scull | f49794b | 2018-04-13 12:01:25 +0100 | [diff] [blame] | 136 | mDevicePolicyManager, mock(StorageManager.class), mock(TrustManager.class), |
Alex Johnston | 6183cf9 | 2019-10-03 15:59:03 +0100 | [diff] [blame] | 137 | mock(KeyguardManager.class), mFingerprintManager, mFaceManager, mPackageManager); |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 138 | mStorage = new LockSettingsStorageTestable(mContext, |
| 139 | new File(getContext().getFilesDir(), "locksettings")); |
| 140 | File storageDir = mStorage.mStorageDir; |
| 141 | if (storageDir.exists()) { |
| 142 | FileUtils.deleteContents(storageDir); |
| 143 | } else { |
| 144 | storageDir.mkdirs(); |
| 145 | } |
| 146 | |
Lenka Trochtova | 66c492a | 2018-12-06 11:29:21 +0100 | [diff] [blame] | 147 | mHasSecureLockScreen = true; |
Rubin Xu | 16c823e | 2017-06-27 14:44:58 +0100 | [diff] [blame] | 148 | mLockPatternUtils = new LockPatternUtils(mContext) { |
| 149 | @Override |
| 150 | public ILockSettings getLockSettings() { |
| 151 | return mService; |
| 152 | } |
Lenka Trochtova | 66c492a | 2018-12-06 11:29:21 +0100 | [diff] [blame] | 153 | |
| 154 | @Override |
| 155 | public boolean hasSecureLockScreen() { |
| 156 | return mHasSecureLockScreen; |
| 157 | } |
Rubin Xu | 16c823e | 2017-06-27 14:44:58 +0100 | [diff] [blame] | 158 | }; |
Adrian Roos | 2adc263 | 2017-09-05 17:01:42 +0200 | [diff] [blame] | 159 | mSpManager = new MockSyntheticPasswordManager(mContext, mStorage, mGateKeeperService, |
David Anderson | 28dea68 | 2019-02-20 13:37:51 -0800 | [diff] [blame] | 160 | mUserManager, mPasswordSlotManager); |
Andrew Scull | e6527c1 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 161 | mAuthSecretService = mock(IAuthSecret.class); |
Rubin Xu | b31be1b | 2017-06-16 17:08:21 +0100 | [diff] [blame] | 162 | mService = new LockSettingsServiceTestable(mContext, mLockPatternUtils, mStorage, |
| 163 | mGateKeeperService, mKeyStore, setUpStorageManagerMock(), mActivityManager, |
Rubin Xu | 0f1e56d | 2019-08-23 13:34:25 +0100 | [diff] [blame] | 164 | mSpManager, mAuthSecretService, mGsiService, mRecoverableKeyStoreManager, |
| 165 | mUserManagerInternal, mDeviceStateCache); |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 166 | when(mUserManager.getUserInfo(eq(PRIMARY_USER_ID))).thenReturn(PRIMARY_USER_INFO); |
Andrew Scull | 8e87af5 | 2017-03-03 15:38:48 +0000 | [diff] [blame] | 167 | mPrimaryUserProfiles.add(PRIMARY_USER_INFO); |
| 168 | installChildProfile(MANAGED_PROFILE_USER_ID); |
Charles He | dec0540 | 2017-04-21 13:45:34 +0100 | [diff] [blame] | 169 | installAndTurnOffChildProfile(TURNED_OFF_PROFILE_USER_ID); |
Annie Meng | 086ddc8 | 2019-03-29 17:43:35 +0000 | [diff] [blame] | 170 | for (UserInfo profile : mPrimaryUserProfiles) { |
| 171 | when(mUserManager.getProfiles(eq(profile.id))).thenReturn(mPrimaryUserProfiles); |
| 172 | } |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 173 | when(mUserManager.getUserInfo(eq(SECONDARY_USER_ID))).thenReturn(SECONDARY_USER_INFO); |
| 174 | |
Andrew Scull | e6527c1 | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 175 | final ArrayList<UserInfo> allUsers = new ArrayList<>(mPrimaryUserProfiles); |
| 176 | allUsers.add(SECONDARY_USER_INFO); |
| 177 | when(mUserManager.getUsers(anyBoolean())).thenReturn(allUsers); |
| 178 | |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 179 | when(mActivityManager.unlockUser(anyInt(), any(), any(), any())).thenAnswer( |
| 180 | new Answer<Boolean>() { |
| 181 | @Override |
| 182 | public Boolean answer(InvocationOnMock invocation) throws Throwable { |
| 183 | Object[] args = invocation.getArguments(); |
| 184 | mStorageManager.unlockUser((int)args[0], (byte[])args[2], |
| 185 | (IProgressListener) args[3]); |
| 186 | return true; |
| 187 | } |
| 188 | }); |
| 189 | |
Rubin Xu | 8b30ec3 | 2017-03-05 00:47:09 +0000 | [diff] [blame] | 190 | // Adding a fake Device Owner app which will enable escrow token support in LSS. |
| 191 | when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn( |
| 192 | new ComponentName("com.dummy.package", ".FakeDeviceOwner")); |
Rubin Xu | 0f1e56d | 2019-08-23 13:34:25 +0100 | [diff] [blame] | 193 | when(mUserManagerInternal.isDeviceManaged()).thenReturn(true); |
| 194 | when(mDeviceStateCache.isDeviceProvisioned()).thenReturn(true); |
Alex Johnston | 6183cf9 | 2019-10-03 15:59:03 +0100 | [diff] [blame] | 195 | mockBiometricsHardwareFingerprintsAndTemplates(PRIMARY_USER_ID); |
| 196 | mockBiometricsHardwareFingerprintsAndTemplates(MANAGED_PROFILE_USER_ID); |
Rubin Xu | 0f1e56d | 2019-08-23 13:34:25 +0100 | [diff] [blame] | 197 | |
Rubin Xu | fcd49f9 | 2017-08-24 18:21:52 +0100 | [diff] [blame] | 198 | mLocalService = LocalServices.getService(LockSettingsInternal.class); |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Andrew Scull | 8e87af5 | 2017-03-03 15:38:48 +0000 | [diff] [blame] | 201 | private UserInfo installChildProfile(int profileId) { |
| 202 | final UserInfo userInfo = new UserInfo( |
| 203 | profileId, null, null, UserInfo.FLAG_INITIALIZED | UserInfo.FLAG_MANAGED_PROFILE); |
Annie Meng | 086ddc8 | 2019-03-29 17:43:35 +0000 | [diff] [blame] | 204 | userInfo.profileGroupId = PRIMARY_USER_ID; |
Andrew Scull | 8e87af5 | 2017-03-03 15:38:48 +0000 | [diff] [blame] | 205 | mPrimaryUserProfiles.add(userInfo); |
| 206 | when(mUserManager.getUserInfo(eq(profileId))).thenReturn(userInfo); |
| 207 | when(mUserManager.getProfileParent(eq(profileId))).thenReturn(PRIMARY_USER_INFO); |
Charles He | dec0540 | 2017-04-21 13:45:34 +0100 | [diff] [blame] | 208 | when(mUserManager.isUserRunning(eq(profileId))).thenReturn(true); |
| 209 | when(mUserManager.isUserUnlocked(eq(profileId))).thenReturn(true); |
Rubin Xu | 0f1e56d | 2019-08-23 13:34:25 +0100 | [diff] [blame] | 210 | when(mUserManagerInternal.isUserManaged(eq(profileId))).thenReturn(true); |
Andrew Scull | 8e87af5 | 2017-03-03 15:38:48 +0000 | [diff] [blame] | 211 | return userInfo; |
| 212 | } |
| 213 | |
Charles He | dec0540 | 2017-04-21 13:45:34 +0100 | [diff] [blame] | 214 | private UserInfo installAndTurnOffChildProfile(int profileId) { |
Andrew Scull | 8e87af5 | 2017-03-03 15:38:48 +0000 | [diff] [blame] | 215 | final UserInfo userInfo = installChildProfile(profileId); |
| 216 | userInfo.flags |= UserInfo.FLAG_QUIET_MODE; |
Charles He | dec0540 | 2017-04-21 13:45:34 +0100 | [diff] [blame] | 217 | when(mUserManager.isUserRunning(eq(profileId))).thenReturn(false); |
| 218 | when(mUserManager.isUserUnlocked(eq(profileId))).thenReturn(false); |
Andrew Scull | 8e87af5 | 2017-03-03 15:38:48 +0000 | [diff] [blame] | 219 | return userInfo; |
| 220 | } |
| 221 | |
Rubin Xu | b31be1b | 2017-06-16 17:08:21 +0100 | [diff] [blame] | 222 | private IStorageManager setUpStorageManagerMock() throws RemoteException { |
| 223 | final IStorageManager sm = mock(IStorageManager.class); |
| 224 | |
| 225 | doAnswer(new Answer<Void>() { |
| 226 | @Override |
| 227 | public Void answer(InvocationOnMock invocation) throws Throwable { |
| 228 | Object[] args = invocation.getArguments(); |
| 229 | mStorageManager.addUserKeyAuth((int) args[0] /* userId */, |
| 230 | (int) args[1] /* serialNumber */, |
| 231 | (byte[]) args[2] /* token */, |
| 232 | (byte[]) args[3] /* secret */); |
| 233 | return null; |
| 234 | } |
| 235 | }).when(sm).addUserKeyAuth(anyInt(), anyInt(), any(), any()); |
| 236 | |
| 237 | doAnswer( |
| 238 | new Answer<Void>() { |
| 239 | @Override |
| 240 | public Void answer(InvocationOnMock invocation) throws Throwable { |
| 241 | Object[] args = invocation.getArguments(); |
| 242 | mStorageManager.fixateNewestUserKeyAuth((int) args[0] /* userId */); |
| 243 | return null; |
| 244 | } |
| 245 | }).when(sm).fixateNewestUserKeyAuth(anyInt()); |
| 246 | return sm; |
| 247 | } |
| 248 | |
Alex Johnston | 6183cf9 | 2019-10-03 15:59:03 +0100 | [diff] [blame] | 249 | private void mockBiometricsHardwareFingerprintsAndTemplates(int userId) { |
| 250 | // Hardware must be detected and fingerprints must be enrolled |
| 251 | when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)).thenReturn(true); |
| 252 | when(mFingerprintManager.isHardwareDetected()).thenReturn(true); |
| 253 | when(mFingerprintManager.hasEnrolledFingerprints(userId)).thenReturn(true); |
Rubin Xu | f526a69 | 2019-10-14 11:42:41 +0100 | [diff] [blame^] | 254 | doAnswer(new Answer<Void>() { |
| 255 | @Override |
| 256 | public Void answer(InvocationOnMock invocation) throws Throwable { |
| 257 | Fingerprint fp = (Fingerprint) invocation.getArguments()[0]; |
| 258 | FingerprintManager.RemovalCallback callback = |
| 259 | (FingerprintManager.RemovalCallback) invocation.getArguments()[2]; |
| 260 | callback.onRemovalSucceeded(fp, 0); |
| 261 | return null; |
| 262 | } |
| 263 | }).when(mFingerprintManager).remove(any(), eq(userId), any()); |
| 264 | |
Alex Johnston | 6183cf9 | 2019-10-03 15:59:03 +0100 | [diff] [blame] | 265 | |
| 266 | // Hardware must be detected and templates must be enrolled |
| 267 | when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_FACE)).thenReturn(true); |
| 268 | when(mFaceManager.isHardwareDetected()).thenReturn(true); |
| 269 | when(mFaceManager.hasEnrolledTemplates(userId)).thenReturn(true); |
Rubin Xu | f526a69 | 2019-10-14 11:42:41 +0100 | [diff] [blame^] | 270 | doAnswer(new Answer<Void>() { |
| 271 | @Override |
| 272 | public Void answer(InvocationOnMock invocation) throws Throwable { |
| 273 | Face face = (Face) invocation.getArguments()[0]; |
| 274 | FaceManager.RemovalCallback callback = |
| 275 | (FaceManager.RemovalCallback) invocation.getArguments()[2]; |
| 276 | callback.onRemovalSucceeded(face, 0); |
| 277 | return null; |
| 278 | } |
| 279 | }).when(mFaceManager).remove(any(), eq(userId), any()); |
Alex Johnston | 6183cf9 | 2019-10-03 15:59:03 +0100 | [diff] [blame] | 280 | } |
| 281 | |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 282 | @Override |
| 283 | protected void tearDown() throws Exception { |
| 284 | super.tearDown(); |
| 285 | mStorage.closeDatabase(); |
| 286 | File db = getContext().getDatabasePath("locksettings.db"); |
| 287 | assertTrue(!db.exists() || db.delete()); |
| 288 | |
| 289 | File storageDir = mStorage.mStorageDir; |
| 290 | assertTrue(FileUtils.deleteContents(storageDir)); |
David Anderson | 28dea68 | 2019-02-20 13:37:51 -0800 | [diff] [blame] | 291 | |
| 292 | mPasswordSlotManager.cleanup(); |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 293 | } |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 294 | |
Rubin Xu | 340e5ba | 2019-05-14 16:10:03 +0100 | [diff] [blame] | 295 | protected void flushHandlerTasks() { |
| 296 | mService.mHandler.runWithScissors(() -> { }, 0 /*now*/); // Flush runnables on handler |
| 297 | } |
| 298 | |
Andrew Scull | 7f4ff4c | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 299 | protected void assertNotEquals(long expected, long actual) { |
| 300 | assertTrue(expected != actual); |
| 301 | } |
| 302 | |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 303 | protected static void assertArrayEquals(byte[] expected, byte[] actual) { |
| 304 | assertTrue(Arrays.equals(expected, actual)); |
| 305 | } |
| 306 | |
Andrew Scull | 7f4ff4c | 2018-01-05 18:33:58 +0000 | [diff] [blame] | 307 | protected static void assertArrayNotEquals(byte[] expected, byte[] actual) { |
Rubin Xu | 3bf722a | 2016-12-15 16:07:38 +0000 | [diff] [blame] | 308 | assertFalse(Arrays.equals(expected, actual)); |
| 309 | } |
Rubin Xu | 0cbc19e | 2016-12-09 14:00:21 +0000 | [diff] [blame] | 310 | } |