blob: 842cdd5715f90d31c6048b9cb963dd781f733bcd [file] [log] [blame]
Rubin Xu0cbc19e2016-12-09 14:00:21 +00001/*
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 Scull507d11c2017-05-03 17:19:01 +010017package com.android.server.locksettings;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000018
Kenny Rootd01bb412019-11-22 09:34:03 -080019import static org.junit.Assert.assertFalse;
20import static org.junit.Assert.assertTrue;
21import static org.mockito.ArgumentMatchers.any;
22import static org.mockito.ArgumentMatchers.anyBoolean;
23import static org.mockito.ArgumentMatchers.anyInt;
24import static org.mockito.ArgumentMatchers.eq;
Rubin Xub31be1b2017-06-16 17:08:21 +010025import static org.mockito.Mockito.doAnswer;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000026import static org.mockito.Mockito.mock;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000027import static org.mockito.Mockito.when;
28
29import android.app.IActivityManager;
Andrew Scullf49794b2018-04-13 12:01:25 +010030import android.app.KeyguardManager;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000031import android.app.NotificationManager;
Rubin Xu8b30ec32017-03-05 00:47:09 +000032import android.app.admin.DevicePolicyManager;
Andrew Scull1416bd02018-01-05 18:33:58 +000033import android.app.admin.DevicePolicyManagerInternal;
Rubin Xu16c823e2017-06-27 14:44:58 +010034import android.app.trust.TrustManager;
Rubin Xu8b30ec32017-03-05 00:47:09 +000035import android.content.ComponentName;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000036import android.content.pm.UserInfo;
Andrew Sculle6527c12018-01-05 18:33:58 +000037import android.hardware.authsecret.V1_0.IAuthSecret;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000038import android.os.FileUtils;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000039import android.os.IProgressListener;
Rubin Xub31be1b2017-06-16 17:08:21 +010040import android.os.RemoteException;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000041import android.os.UserManager;
Rubin Xub31be1b2017-06-16 17:08:21 +010042import android.os.storage.IStorageManager;
Lenka Trochtova66c492a2018-12-06 11:29:21 +010043import android.os.storage.StorageManager;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000044import android.security.KeyStore;
Kenny Rootd01bb412019-11-22 09:34:03 -080045
46import androidx.test.InstrumentationRegistry;
47import androidx.test.runner.AndroidJUnit4;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000048
Rubin Xu16c823e2017-06-27 14:44:58 +010049import com.android.internal.widget.ILockSettings;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000050import com.android.internal.widget.LockPatternUtils;
Rubin Xufcd49f92017-08-24 18:21:52 +010051import com.android.internal.widget.LockSettingsInternal;
Andrew Scull1416bd02018-01-05 18:33:58 +000052import com.android.server.LocalServices;
Annie Meng086ddc82019-03-29 17:43:35 +000053import com.android.server.locksettings.recoverablekeystore.RecoverableKeyStoreManager;
Lenka Trochtova66c492a2018-12-06 11:29:21 +010054import com.android.server.wm.WindowManagerInternal;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000055
Kenny Rootd01bb412019-11-22 09:34:03 -080056import org.junit.After;
57import org.junit.Before;
58import org.junit.runner.RunWith;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000059import org.mockito.invocation.InvocationOnMock;
60import org.mockito.stubbing.Answer;
61
62import java.io.File;
Andrew Scull8e87af52017-03-03 15:38:48 +000063import java.util.ArrayList;
Charles Hedec05402017-04-21 13:45:34 +010064import java.util.Arrays;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000065
Kenny Rootd01bb412019-11-22 09:34:03 -080066@RunWith(AndroidJUnit4.class)
67public abstract class BaseLockSettingsServiceTests {
Rubin Xu0cbc19e2016-12-09 14:00:21 +000068 protected static final int PRIMARY_USER_ID = 0;
69 protected static final int MANAGED_PROFILE_USER_ID = 12;
Andrew Scull8e87af52017-03-03 15:38:48 +000070 protected static final int TURNED_OFF_PROFILE_USER_ID = 17;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000071 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 Xu0cbc19e2016-12-09 14:00:21 +000075 private static final UserInfo SECONDARY_USER_INFO = new UserInfo(SECONDARY_USER_ID, null, null,
76 UserInfo.FLAG_INITIALIZED);
77
Andrew Scull8e87af52017-03-03 15:38:48 +000078 private ArrayList<UserInfo> mPrimaryUserProfiles = new ArrayList<>();
79
Rubin Xu0cbc19e2016-12-09 14:00:21 +000080 LockSettingsService mService;
Rubin Xufcd49f92017-08-24 18:21:52 +010081 LockSettingsInternal mLocalService;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000082
83 MockLockSettingsContext mContext;
84 LockSettingsStorageTestable mStorage;
85
86 LockPatternUtils mLockPatternUtils;
Rubin Xu16c823e2017-06-27 14:44:58 +010087 FakeGateKeeperService mGateKeeperService;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000088 NotificationManager mNotificationManager;
89 UserManager mUserManager;
Rubin Xub31be1b2017-06-16 17:08:21 +010090 FakeStorageManager mStorageManager;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000091 IActivityManager mActivityManager;
Rubin Xu8b30ec32017-03-05 00:47:09 +000092 DevicePolicyManager mDevicePolicyManager;
Andrew Scull1416bd02018-01-05 18:33:58 +000093 DevicePolicyManagerInternal mDevicePolicyManagerInternal;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000094 KeyStore mKeyStore;
Rubin Xu7b7424b2017-03-31 18:03:20 +010095 MockSyntheticPasswordManager mSpManager;
Andrew Sculle6527c12018-01-05 18:33:58 +000096 IAuthSecret mAuthSecretService;
Lenka Trochtova66c492a2018-12-06 11:29:21 +010097 WindowManagerInternal mMockWindowManager;
David Anderson6ebc25b2019-02-12 16:25:56 -080098 FakeGsiService mGsiService;
David Anderson28dea682019-02-20 13:37:51 -080099 PasswordSlotManagerTestable mPasswordSlotManager;
Annie Meng086ddc82019-03-29 17:43:35 +0000100 RecoverableKeyStoreManager mRecoverableKeyStoreManager;
Lenka Trochtova66c492a2018-12-06 11:29:21 +0100101 protected boolean mHasSecureLockScreen;
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000102
Kenny Rootd01bb412019-11-22 09:34:03 -0800103 @Before
104 public void setUp_baseServices() throws Exception {
Rubin Xu16c823e2017-06-27 14:44:58 +0100105 mGateKeeperService = new FakeGateKeeperService();
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000106 mNotificationManager = mock(NotificationManager.class);
107 mUserManager = mock(UserManager.class);
Rubin Xub31be1b2017-06-16 17:08:21 +0100108 mStorageManager = new FakeStorageManager();
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000109 mActivityManager = mock(IActivityManager.class);
Rubin Xu8b30ec32017-03-05 00:47:09 +0000110 mDevicePolicyManager = mock(DevicePolicyManager.class);
Andrew Scull1416bd02018-01-05 18:33:58 +0000111 mDevicePolicyManagerInternal = mock(DevicePolicyManagerInternal.class);
Lenka Trochtova66c492a2018-12-06 11:29:21 +0100112 mMockWindowManager = mock(WindowManagerInternal.class);
David Anderson6ebc25b2019-02-12 16:25:56 -0800113 mGsiService = new FakeGsiService();
David Anderson28dea682019-02-20 13:37:51 -0800114 mPasswordSlotManager = new PasswordSlotManagerTestable();
Annie Meng086ddc82019-03-29 17:43:35 +0000115 mRecoverableKeyStoreManager = mock(RecoverableKeyStoreManager.class);
Andrew Scull1416bd02018-01-05 18:33:58 +0000116
Rubin Xufcd49f92017-08-24 18:21:52 +0100117 LocalServices.removeServiceForTest(LockSettingsInternal.class);
Andrew Scull1416bd02018-01-05 18:33:58 +0000118 LocalServices.removeServiceForTest(DevicePolicyManagerInternal.class);
Lenka Trochtova66c492a2018-12-06 11:29:21 +0100119 LocalServices.removeServiceForTest(WindowManagerInternal.class);
Andrew Scull1416bd02018-01-05 18:33:58 +0000120 LocalServices.addService(DevicePolicyManagerInternal.class, mDevicePolicyManagerInternal);
Lenka Trochtova66c492a2018-12-06 11:29:21 +0100121 LocalServices.addService(WindowManagerInternal.class, mMockWindowManager);
Rubin Xu7b7424b2017-03-31 18:03:20 +0100122
Kenny Rootd01bb412019-11-22 09:34:03 -0800123 mContext = new MockLockSettingsContext(InstrumentationRegistry.getContext(), mUserManager,
124 mNotificationManager, mDevicePolicyManager, mock(StorageManager.class),
125 mock(TrustManager.class), mock(KeyguardManager.class));
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000126 mStorage = new LockSettingsStorageTestable(mContext,
Kenny Rootd01bb412019-11-22 09:34:03 -0800127 new File(InstrumentationRegistry.getContext().getFilesDir(), "locksettings"));
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000128 File storageDir = mStorage.mStorageDir;
129 if (storageDir.exists()) {
130 FileUtils.deleteContents(storageDir);
131 } else {
132 storageDir.mkdirs();
133 }
134
Lenka Trochtova66c492a2018-12-06 11:29:21 +0100135 mHasSecureLockScreen = true;
Rubin Xu16c823e2017-06-27 14:44:58 +0100136 mLockPatternUtils = new LockPatternUtils(mContext) {
137 @Override
138 public ILockSettings getLockSettings() {
139 return mService;
140 }
Lenka Trochtova66c492a2018-12-06 11:29:21 +0100141
142 @Override
143 public boolean hasSecureLockScreen() {
144 return mHasSecureLockScreen;
145 }
Rubin Xu16c823e2017-06-27 14:44:58 +0100146 };
Adrian Roos2adc2632017-09-05 17:01:42 +0200147 mSpManager = new MockSyntheticPasswordManager(mContext, mStorage, mGateKeeperService,
David Anderson28dea682019-02-20 13:37:51 -0800148 mUserManager, mPasswordSlotManager);
Andrew Sculle6527c12018-01-05 18:33:58 +0000149 mAuthSecretService = mock(IAuthSecret.class);
Rubin Xub31be1b2017-06-16 17:08:21 +0100150 mService = new LockSettingsServiceTestable(mContext, mLockPatternUtils, mStorage,
151 mGateKeeperService, mKeyStore, setUpStorageManagerMock(), mActivityManager,
Annie Meng086ddc82019-03-29 17:43:35 +0000152 mSpManager, mAuthSecretService, mGsiService, mRecoverableKeyStoreManager);
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000153 when(mUserManager.getUserInfo(eq(PRIMARY_USER_ID))).thenReturn(PRIMARY_USER_INFO);
Andrew Scull8e87af52017-03-03 15:38:48 +0000154 mPrimaryUserProfiles.add(PRIMARY_USER_INFO);
155 installChildProfile(MANAGED_PROFILE_USER_ID);
Charles Hedec05402017-04-21 13:45:34 +0100156 installAndTurnOffChildProfile(TURNED_OFF_PROFILE_USER_ID);
Annie Meng086ddc82019-03-29 17:43:35 +0000157 for (UserInfo profile : mPrimaryUserProfiles) {
158 when(mUserManager.getProfiles(eq(profile.id))).thenReturn(mPrimaryUserProfiles);
159 }
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000160 when(mUserManager.getUserInfo(eq(SECONDARY_USER_ID))).thenReturn(SECONDARY_USER_INFO);
161
Andrew Sculle6527c12018-01-05 18:33:58 +0000162 final ArrayList<UserInfo> allUsers = new ArrayList<>(mPrimaryUserProfiles);
163 allUsers.add(SECONDARY_USER_INFO);
164 when(mUserManager.getUsers(anyBoolean())).thenReturn(allUsers);
165
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000166 when(mActivityManager.unlockUser(anyInt(), any(), any(), any())).thenAnswer(
167 new Answer<Boolean>() {
168 @Override
169 public Boolean answer(InvocationOnMock invocation) throws Throwable {
170 Object[] args = invocation.getArguments();
171 mStorageManager.unlockUser((int)args[0], (byte[])args[2],
172 (IProgressListener) args[3]);
173 return true;
174 }
175 });
176
Rubin Xu8b30ec32017-03-05 00:47:09 +0000177 // Adding a fake Device Owner app which will enable escrow token support in LSS.
178 when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(
179 new ComponentName("com.dummy.package", ".FakeDeviceOwner"));
Rubin Xufcd49f92017-08-24 18:21:52 +0100180 mLocalService = LocalServices.getService(LockSettingsInternal.class);
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000181 }
182
Andrew Scull8e87af52017-03-03 15:38:48 +0000183 private UserInfo installChildProfile(int profileId) {
184 final UserInfo userInfo = new UserInfo(
185 profileId, null, null, UserInfo.FLAG_INITIALIZED | UserInfo.FLAG_MANAGED_PROFILE);
Annie Meng086ddc82019-03-29 17:43:35 +0000186 userInfo.profileGroupId = PRIMARY_USER_ID;
Andrew Scull8e87af52017-03-03 15:38:48 +0000187 mPrimaryUserProfiles.add(userInfo);
188 when(mUserManager.getUserInfo(eq(profileId))).thenReturn(userInfo);
189 when(mUserManager.getProfileParent(eq(profileId))).thenReturn(PRIMARY_USER_INFO);
Charles Hedec05402017-04-21 13:45:34 +0100190 when(mUserManager.isUserRunning(eq(profileId))).thenReturn(true);
191 when(mUserManager.isUserUnlocked(eq(profileId))).thenReturn(true);
Andrew Scull8e87af52017-03-03 15:38:48 +0000192 return userInfo;
193 }
194
Charles Hedec05402017-04-21 13:45:34 +0100195 private UserInfo installAndTurnOffChildProfile(int profileId) {
Andrew Scull8e87af52017-03-03 15:38:48 +0000196 final UserInfo userInfo = installChildProfile(profileId);
197 userInfo.flags |= UserInfo.FLAG_QUIET_MODE;
Charles Hedec05402017-04-21 13:45:34 +0100198 when(mUserManager.isUserRunning(eq(profileId))).thenReturn(false);
199 when(mUserManager.isUserUnlocked(eq(profileId))).thenReturn(false);
Andrew Scull8e87af52017-03-03 15:38:48 +0000200 return userInfo;
201 }
202
Rubin Xub31be1b2017-06-16 17:08:21 +0100203 private IStorageManager setUpStorageManagerMock() throws RemoteException {
204 final IStorageManager sm = mock(IStorageManager.class);
205
206 doAnswer(new Answer<Void>() {
207 @Override
208 public Void answer(InvocationOnMock invocation) throws Throwable {
209 Object[] args = invocation.getArguments();
210 mStorageManager.addUserKeyAuth((int) args[0] /* userId */,
211 (int) args[1] /* serialNumber */,
212 (byte[]) args[2] /* token */,
213 (byte[]) args[3] /* secret */);
214 return null;
215 }
216 }).when(sm).addUserKeyAuth(anyInt(), anyInt(), any(), any());
217
218 doAnswer(
219 new Answer<Void>() {
220 @Override
221 public Void answer(InvocationOnMock invocation) throws Throwable {
222 Object[] args = invocation.getArguments();
223 mStorageManager.fixateNewestUserKeyAuth((int) args[0] /* userId */);
224 return null;
225 }
226 }).when(sm).fixateNewestUserKeyAuth(anyInt());
227 return sm;
228 }
229
Kenny Rootd01bb412019-11-22 09:34:03 -0800230 @After
231 public void tearDown_baseServices() throws Exception {
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000232 mStorage.closeDatabase();
Kenny Rootd01bb412019-11-22 09:34:03 -0800233 File db = InstrumentationRegistry.getContext().getDatabasePath("locksettings.db");
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000234 assertTrue(!db.exists() || db.delete());
235
236 File storageDir = mStorage.mStorageDir;
237 assertTrue(FileUtils.deleteContents(storageDir));
David Anderson28dea682019-02-20 13:37:51 -0800238
239 mPasswordSlotManager.cleanup();
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000240 }
Rubin Xu3bf722a2016-12-15 16:07:38 +0000241
Rubin Xu340e5ba2019-05-14 16:10:03 +0100242 protected void flushHandlerTasks() {
243 mService.mHandler.runWithScissors(() -> { }, 0 /*now*/); // Flush runnables on handler
244 }
245
Andrew Scull7f4ff4c2018-01-05 18:33:58 +0000246 protected void assertNotEquals(long expected, long actual) {
247 assertTrue(expected != actual);
248 }
249
Rubin Xu3bf722a2016-12-15 16:07:38 +0000250 protected static void assertArrayEquals(byte[] expected, byte[] actual) {
251 assertTrue(Arrays.equals(expected, actual));
252 }
253
Andrew Scull7f4ff4c2018-01-05 18:33:58 +0000254 protected static void assertArrayNotEquals(byte[] expected, byte[] actual) {
Rubin Xu3bf722a2016-12-15 16:07:38 +0000255 assertFalse(Arrays.equals(expected, actual));
256 }
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000257}