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