blob: 1f5ebe4536d8c8885804918c04e924ee847016a6 [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
19import static org.mockito.Matchers.any;
Rubin Xu7b7424b2017-03-31 18:03:20 +010020import static org.mockito.Matchers.anyBoolean;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000021import static org.mockito.Matchers.anyInt;
22import static org.mockito.Matchers.eq;
Rubin Xub31be1b2017-06-16 17:08:21 +010023import static org.mockito.Mockito.doAnswer;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000024import static org.mockito.Mockito.mock;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000025import static org.mockito.Mockito.when;
26
27import android.app.IActivityManager;
Andrew Scullf49794b2018-04-13 12:01:25 +010028import android.app.KeyguardManager;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000029import android.app.NotificationManager;
Rubin Xu8b30ec32017-03-05 00:47:09 +000030import android.app.admin.DevicePolicyManager;
Andrew Scull1416bd02018-01-05 18:33:58 +000031import android.app.admin.DevicePolicyManagerInternal;
Rubin Xu16c823e2017-06-27 14:44:58 +010032import android.app.trust.TrustManager;
Rubin Xu8b30ec32017-03-05 00:47:09 +000033import android.content.ComponentName;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000034import android.content.pm.UserInfo;
Andrew Sculle6527c12018-01-05 18:33:58 +000035import android.hardware.authsecret.V1_0.IAuthSecret;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000036import android.os.FileUtils;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000037import android.os.IProgressListener;
Rubin Xub31be1b2017-06-16 17:08:21 +010038import android.os.RemoteException;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000039import android.os.UserManager;
Rubin Xub31be1b2017-06-16 17:08:21 +010040import android.os.storage.IStorageManager;
Lenka Trochtova66c492a2018-12-06 11:29:21 +010041import android.os.storage.StorageManager;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000042import android.security.KeyStore;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000043import android.test.AndroidTestCase;
44
Rubin Xu16c823e2017-06-27 14:44:58 +010045import com.android.internal.widget.ILockSettings;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000046import com.android.internal.widget.LockPatternUtils;
Rubin Xufcd49f92017-08-24 18:21:52 +010047import com.android.internal.widget.LockSettingsInternal;
Andrew Scull1416bd02018-01-05 18:33:58 +000048import com.android.server.LocalServices;
Annie Meng086ddc82019-03-29 17:43:35 +000049import com.android.server.locksettings.recoverablekeystore.RecoverableKeyStoreManager;
Lenka Trochtova66c492a2018-12-06 11:29:21 +010050import com.android.server.wm.WindowManagerInternal;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000051
52import org.mockito.invocation.InvocationOnMock;
53import org.mockito.stubbing.Answer;
54
55import java.io.File;
Andrew Scull8e87af52017-03-03 15:38:48 +000056import java.util.ArrayList;
Charles Hedec05402017-04-21 13:45:34 +010057import java.util.Arrays;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000058
59
Pavel Grafov57f1b662019-03-27 14:55:38 +000060public abstract class BaseLockSettingsServiceTests extends AndroidTestCase {
Rubin Xu0cbc19e2016-12-09 14:00:21 +000061 protected static final int PRIMARY_USER_ID = 0;
62 protected static final int MANAGED_PROFILE_USER_ID = 12;
Andrew Scull8e87af52017-03-03 15:38:48 +000063 protected static final int TURNED_OFF_PROFILE_USER_ID = 17;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000064 protected static final int SECONDARY_USER_ID = 20;
65
66 private static final UserInfo PRIMARY_USER_INFO = new UserInfo(PRIMARY_USER_ID, null, null,
67 UserInfo.FLAG_INITIALIZED | UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY);
Rubin Xu0cbc19e2016-12-09 14:00:21 +000068 private static final UserInfo SECONDARY_USER_INFO = new UserInfo(SECONDARY_USER_ID, null, null,
69 UserInfo.FLAG_INITIALIZED);
70
Andrew Scull8e87af52017-03-03 15:38:48 +000071 private ArrayList<UserInfo> mPrimaryUserProfiles = new ArrayList<>();
72
Rubin Xu0cbc19e2016-12-09 14:00:21 +000073 LockSettingsService mService;
Rubin Xufcd49f92017-08-24 18:21:52 +010074 LockSettingsInternal mLocalService;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000075
76 MockLockSettingsContext mContext;
77 LockSettingsStorageTestable mStorage;
78
79 LockPatternUtils mLockPatternUtils;
Rubin Xu16c823e2017-06-27 14:44:58 +010080 FakeGateKeeperService mGateKeeperService;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000081 NotificationManager mNotificationManager;
82 UserManager mUserManager;
Rubin Xub31be1b2017-06-16 17:08:21 +010083 FakeStorageManager mStorageManager;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000084 IActivityManager mActivityManager;
Rubin Xu8b30ec32017-03-05 00:47:09 +000085 DevicePolicyManager mDevicePolicyManager;
Andrew Scull1416bd02018-01-05 18:33:58 +000086 DevicePolicyManagerInternal mDevicePolicyManagerInternal;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000087 KeyStore mKeyStore;
Rubin Xu7b7424b2017-03-31 18:03:20 +010088 MockSyntheticPasswordManager mSpManager;
Andrew Sculle6527c12018-01-05 18:33:58 +000089 IAuthSecret mAuthSecretService;
Lenka Trochtova66c492a2018-12-06 11:29:21 +010090 WindowManagerInternal mMockWindowManager;
David Anderson6ebc25b2019-02-12 16:25:56 -080091 FakeGsiService mGsiService;
David Anderson28dea682019-02-20 13:37:51 -080092 PasswordSlotManagerTestable mPasswordSlotManager;
Annie Meng086ddc82019-03-29 17:43:35 +000093 RecoverableKeyStoreManager mRecoverableKeyStoreManager;
Lenka Trochtova66c492a2018-12-06 11:29:21 +010094 protected boolean mHasSecureLockScreen;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000095
96 @Override
97 protected void setUp() throws Exception {
98 super.setUp();
99
Rubin Xu16c823e2017-06-27 14:44:58 +0100100 mGateKeeperService = new FakeGateKeeperService();
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000101 mNotificationManager = mock(NotificationManager.class);
102 mUserManager = mock(UserManager.class);
Rubin Xub31be1b2017-06-16 17:08:21 +0100103 mStorageManager = new FakeStorageManager();
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000104 mActivityManager = mock(IActivityManager.class);
Rubin Xu8b30ec32017-03-05 00:47:09 +0000105 mDevicePolicyManager = mock(DevicePolicyManager.class);
Andrew Scull1416bd02018-01-05 18:33:58 +0000106 mDevicePolicyManagerInternal = mock(DevicePolicyManagerInternal.class);
Lenka Trochtova66c492a2018-12-06 11:29:21 +0100107 mMockWindowManager = mock(WindowManagerInternal.class);
David Anderson6ebc25b2019-02-12 16:25:56 -0800108 mGsiService = new FakeGsiService();
David Anderson28dea682019-02-20 13:37:51 -0800109 mPasswordSlotManager = new PasswordSlotManagerTestable();
Annie Meng086ddc82019-03-29 17:43:35 +0000110 mRecoverableKeyStoreManager = mock(RecoverableKeyStoreManager.class);
Andrew Scull1416bd02018-01-05 18:33:58 +0000111
Rubin Xufcd49f92017-08-24 18:21:52 +0100112 LocalServices.removeServiceForTest(LockSettingsInternal.class);
Andrew Scull1416bd02018-01-05 18:33:58 +0000113 LocalServices.removeServiceForTest(DevicePolicyManagerInternal.class);
Lenka Trochtova66c492a2018-12-06 11:29:21 +0100114 LocalServices.removeServiceForTest(WindowManagerInternal.class);
Andrew Scull1416bd02018-01-05 18:33:58 +0000115 LocalServices.addService(DevicePolicyManagerInternal.class, mDevicePolicyManagerInternal);
Lenka Trochtova66c492a2018-12-06 11:29:21 +0100116 LocalServices.addService(WindowManagerInternal.class, mMockWindowManager);
Rubin Xu7b7424b2017-03-31 18:03:20 +0100117
Rubin Xu8b30ec32017-03-05 00:47:09 +0000118 mContext = new MockLockSettingsContext(getContext(), mUserManager, mNotificationManager,
Andrew Scullf49794b2018-04-13 12:01:25 +0100119 mDevicePolicyManager, mock(StorageManager.class), mock(TrustManager.class),
120 mock(KeyguardManager.class));
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000121 mStorage = new LockSettingsStorageTestable(mContext,
122 new File(getContext().getFilesDir(), "locksettings"));
123 File storageDir = mStorage.mStorageDir;
124 if (storageDir.exists()) {
125 FileUtils.deleteContents(storageDir);
126 } else {
127 storageDir.mkdirs();
128 }
129
Lenka Trochtova66c492a2018-12-06 11:29:21 +0100130 mHasSecureLockScreen = true;
Rubin Xu16c823e2017-06-27 14:44:58 +0100131 mLockPatternUtils = new LockPatternUtils(mContext) {
132 @Override
133 public ILockSettings getLockSettings() {
134 return mService;
135 }
Lenka Trochtova66c492a2018-12-06 11:29:21 +0100136
137 @Override
138 public boolean hasSecureLockScreen() {
139 return mHasSecureLockScreen;
140 }
Rubin Xu16c823e2017-06-27 14:44:58 +0100141 };
Adrian Roos2adc2632017-09-05 17:01:42 +0200142 mSpManager = new MockSyntheticPasswordManager(mContext, mStorage, mGateKeeperService,
David Anderson28dea682019-02-20 13:37:51 -0800143 mUserManager, mPasswordSlotManager);
Andrew Sculle6527c12018-01-05 18:33:58 +0000144 mAuthSecretService = mock(IAuthSecret.class);
Rubin Xub31be1b2017-06-16 17:08:21 +0100145 mService = new LockSettingsServiceTestable(mContext, mLockPatternUtils, mStorage,
146 mGateKeeperService, mKeyStore, setUpStorageManagerMock(), mActivityManager,
Annie Meng086ddc82019-03-29 17:43:35 +0000147 mSpManager, mAuthSecretService, mGsiService, mRecoverableKeyStoreManager);
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000148 when(mUserManager.getUserInfo(eq(PRIMARY_USER_ID))).thenReturn(PRIMARY_USER_INFO);
Andrew Scull8e87af52017-03-03 15:38:48 +0000149 mPrimaryUserProfiles.add(PRIMARY_USER_INFO);
150 installChildProfile(MANAGED_PROFILE_USER_ID);
Charles Hedec05402017-04-21 13:45:34 +0100151 installAndTurnOffChildProfile(TURNED_OFF_PROFILE_USER_ID);
Annie Meng086ddc82019-03-29 17:43:35 +0000152 for (UserInfo profile : mPrimaryUserProfiles) {
153 when(mUserManager.getProfiles(eq(profile.id))).thenReturn(mPrimaryUserProfiles);
154 }
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000155 when(mUserManager.getUserInfo(eq(SECONDARY_USER_ID))).thenReturn(SECONDARY_USER_INFO);
156
Andrew Sculle6527c12018-01-05 18:33:58 +0000157 final ArrayList<UserInfo> allUsers = new ArrayList<>(mPrimaryUserProfiles);
158 allUsers.add(SECONDARY_USER_INFO);
159 when(mUserManager.getUsers(anyBoolean())).thenReturn(allUsers);
160
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000161 when(mActivityManager.unlockUser(anyInt(), any(), any(), any())).thenAnswer(
162 new Answer<Boolean>() {
163 @Override
164 public Boolean answer(InvocationOnMock invocation) throws Throwable {
165 Object[] args = invocation.getArguments();
166 mStorageManager.unlockUser((int)args[0], (byte[])args[2],
167 (IProgressListener) args[3]);
168 return true;
169 }
170 });
171
Rubin Xu8b30ec32017-03-05 00:47:09 +0000172 // Adding a fake Device Owner app which will enable escrow token support in LSS.
173 when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(
174 new ComponentName("com.dummy.package", ".FakeDeviceOwner"));
Rubin Xufcd49f92017-08-24 18:21:52 +0100175 mLocalService = LocalServices.getService(LockSettingsInternal.class);
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000176 }
177
Andrew Scull8e87af52017-03-03 15:38:48 +0000178 private UserInfo installChildProfile(int profileId) {
179 final UserInfo userInfo = new UserInfo(
180 profileId, null, null, UserInfo.FLAG_INITIALIZED | UserInfo.FLAG_MANAGED_PROFILE);
Annie Meng086ddc82019-03-29 17:43:35 +0000181 userInfo.profileGroupId = PRIMARY_USER_ID;
Andrew Scull8e87af52017-03-03 15:38:48 +0000182 mPrimaryUserProfiles.add(userInfo);
183 when(mUserManager.getUserInfo(eq(profileId))).thenReturn(userInfo);
184 when(mUserManager.getProfileParent(eq(profileId))).thenReturn(PRIMARY_USER_INFO);
Charles Hedec05402017-04-21 13:45:34 +0100185 when(mUserManager.isUserRunning(eq(profileId))).thenReturn(true);
186 when(mUserManager.isUserUnlocked(eq(profileId))).thenReturn(true);
Andrew Scull8e87af52017-03-03 15:38:48 +0000187 return userInfo;
188 }
189
Charles Hedec05402017-04-21 13:45:34 +0100190 private UserInfo installAndTurnOffChildProfile(int profileId) {
Andrew Scull8e87af52017-03-03 15:38:48 +0000191 final UserInfo userInfo = installChildProfile(profileId);
192 userInfo.flags |= UserInfo.FLAG_QUIET_MODE;
Charles Hedec05402017-04-21 13:45:34 +0100193 when(mUserManager.isUserRunning(eq(profileId))).thenReturn(false);
194 when(mUserManager.isUserUnlocked(eq(profileId))).thenReturn(false);
Andrew Scull8e87af52017-03-03 15:38:48 +0000195 return userInfo;
196 }
197
Rubin Xub31be1b2017-06-16 17:08:21 +0100198 private IStorageManager setUpStorageManagerMock() throws RemoteException {
199 final IStorageManager sm = mock(IStorageManager.class);
200
201 doAnswer(new Answer<Void>() {
202 @Override
203 public Void answer(InvocationOnMock invocation) throws Throwable {
204 Object[] args = invocation.getArguments();
205 mStorageManager.addUserKeyAuth((int) args[0] /* userId */,
206 (int) args[1] /* serialNumber */,
207 (byte[]) args[2] /* token */,
208 (byte[]) args[3] /* secret */);
209 return null;
210 }
211 }).when(sm).addUserKeyAuth(anyInt(), anyInt(), any(), any());
212
213 doAnswer(
214 new Answer<Void>() {
215 @Override
216 public Void answer(InvocationOnMock invocation) throws Throwable {
217 Object[] args = invocation.getArguments();
218 mStorageManager.fixateNewestUserKeyAuth((int) args[0] /* userId */);
219 return null;
220 }
221 }).when(sm).fixateNewestUserKeyAuth(anyInt());
222 return sm;
223 }
224
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000225 @Override
226 protected void tearDown() throws Exception {
227 super.tearDown();
228 mStorage.closeDatabase();
229 File db = getContext().getDatabasePath("locksettings.db");
230 assertTrue(!db.exists() || db.delete());
231
232 File storageDir = mStorage.mStorageDir;
233 assertTrue(FileUtils.deleteContents(storageDir));
David Anderson28dea682019-02-20 13:37:51 -0800234
235 mPasswordSlotManager.cleanup();
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000236 }
Rubin Xu3bf722a2016-12-15 16:07:38 +0000237
Rubin Xu340e5ba2019-05-14 16:10:03 +0100238 protected void flushHandlerTasks() {
239 mService.mHandler.runWithScissors(() -> { }, 0 /*now*/); // Flush runnables on handler
240 }
241
Andrew Scull7f4ff4c2018-01-05 18:33:58 +0000242 protected void assertNotEquals(long expected, long actual) {
243 assertTrue(expected != actual);
244 }
245
Rubin Xu3bf722a2016-12-15 16:07:38 +0000246 protected static void assertArrayEquals(byte[] expected, byte[] actual) {
247 assertTrue(Arrays.equals(expected, actual));
248 }
249
Andrew Scull7f4ff4c2018-01-05 18:33:58 +0000250 protected static void assertArrayNotEquals(byte[] expected, byte[] actual) {
Rubin Xu3bf722a2016-12-15 16:07:38 +0000251 assertFalse(Arrays.equals(expected, actual));
252 }
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000253}