blob: 3d02576c8e7f00b4f89dce28e77eac81a913eb00 [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;
Lenka Trochtova66c492a2018-12-06 11:29:21 +010049import com.android.server.wm.WindowManagerInternal;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000050
51import org.mockito.invocation.InvocationOnMock;
52import org.mockito.stubbing.Answer;
53
54import java.io.File;
Andrew Scull8e87af52017-03-03 15:38:48 +000055import java.util.ArrayList;
Charles Hedec05402017-04-21 13:45:34 +010056import java.util.Arrays;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000057
58
59public class BaseLockSettingsServiceTests extends AndroidTestCase {
60 protected static final int PRIMARY_USER_ID = 0;
61 protected static final int MANAGED_PROFILE_USER_ID = 12;
Andrew Scull8e87af52017-03-03 15:38:48 +000062 protected static final int TURNED_OFF_PROFILE_USER_ID = 17;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000063 protected static final int SECONDARY_USER_ID = 20;
64
65 private static final UserInfo PRIMARY_USER_INFO = new UserInfo(PRIMARY_USER_ID, null, null,
66 UserInfo.FLAG_INITIALIZED | UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY);
Rubin Xu0cbc19e2016-12-09 14:00:21 +000067 private static final UserInfo SECONDARY_USER_INFO = new UserInfo(SECONDARY_USER_ID, null, null,
68 UserInfo.FLAG_INITIALIZED);
69
Andrew Scull8e87af52017-03-03 15:38:48 +000070 private ArrayList<UserInfo> mPrimaryUserProfiles = new ArrayList<>();
71
Rubin Xu0cbc19e2016-12-09 14:00:21 +000072 LockSettingsService mService;
Rubin Xufcd49f92017-08-24 18:21:52 +010073 LockSettingsInternal mLocalService;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000074
75 MockLockSettingsContext mContext;
76 LockSettingsStorageTestable mStorage;
77
78 LockPatternUtils mLockPatternUtils;
Rubin Xu16c823e2017-06-27 14:44:58 +010079 FakeGateKeeperService mGateKeeperService;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000080 NotificationManager mNotificationManager;
81 UserManager mUserManager;
Rubin Xub31be1b2017-06-16 17:08:21 +010082 FakeStorageManager mStorageManager;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000083 IActivityManager mActivityManager;
Rubin Xu8b30ec32017-03-05 00:47:09 +000084 DevicePolicyManager mDevicePolicyManager;
Andrew Scull1416bd02018-01-05 18:33:58 +000085 DevicePolicyManagerInternal mDevicePolicyManagerInternal;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000086 KeyStore mKeyStore;
Rubin Xu7b7424b2017-03-31 18:03:20 +010087 MockSyntheticPasswordManager mSpManager;
Andrew Sculle6527c12018-01-05 18:33:58 +000088 IAuthSecret mAuthSecretService;
Lenka Trochtova66c492a2018-12-06 11:29:21 +010089 WindowManagerInternal mMockWindowManager;
David Anderson6ebc25b2019-02-12 16:25:56 -080090 FakeGsiService mGsiService;
David Anderson28dea682019-02-20 13:37:51 -080091 PasswordSlotManagerTestable mPasswordSlotManager;
Lenka Trochtova66c492a2018-12-06 11:29:21 +010092 protected boolean mHasSecureLockScreen;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000093
94 @Override
95 protected void setUp() throws Exception {
96 super.setUp();
97
Rubin Xu16c823e2017-06-27 14:44:58 +010098 mGateKeeperService = new FakeGateKeeperService();
Rubin Xu0cbc19e2016-12-09 14:00:21 +000099 mNotificationManager = mock(NotificationManager.class);
100 mUserManager = mock(UserManager.class);
Rubin Xub31be1b2017-06-16 17:08:21 +0100101 mStorageManager = new FakeStorageManager();
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000102 mActivityManager = mock(IActivityManager.class);
Rubin Xu8b30ec32017-03-05 00:47:09 +0000103 mDevicePolicyManager = mock(DevicePolicyManager.class);
Andrew Scull1416bd02018-01-05 18:33:58 +0000104 mDevicePolicyManagerInternal = mock(DevicePolicyManagerInternal.class);
Lenka Trochtova66c492a2018-12-06 11:29:21 +0100105 mMockWindowManager = mock(WindowManagerInternal.class);
David Anderson6ebc25b2019-02-12 16:25:56 -0800106 mGsiService = new FakeGsiService();
David Anderson28dea682019-02-20 13:37:51 -0800107 mPasswordSlotManager = new PasswordSlotManagerTestable();
Andrew Scull1416bd02018-01-05 18:33:58 +0000108
Rubin Xufcd49f92017-08-24 18:21:52 +0100109 LocalServices.removeServiceForTest(LockSettingsInternal.class);
Andrew Scull1416bd02018-01-05 18:33:58 +0000110 LocalServices.removeServiceForTest(DevicePolicyManagerInternal.class);
Lenka Trochtova66c492a2018-12-06 11:29:21 +0100111 LocalServices.removeServiceForTest(WindowManagerInternal.class);
Andrew Scull1416bd02018-01-05 18:33:58 +0000112 LocalServices.addService(DevicePolicyManagerInternal.class, mDevicePolicyManagerInternal);
Lenka Trochtova66c492a2018-12-06 11:29:21 +0100113 LocalServices.addService(WindowManagerInternal.class, mMockWindowManager);
Rubin Xu7b7424b2017-03-31 18:03:20 +0100114
Rubin Xu8b30ec32017-03-05 00:47:09 +0000115 mContext = new MockLockSettingsContext(getContext(), mUserManager, mNotificationManager,
Andrew Scullf49794b2018-04-13 12:01:25 +0100116 mDevicePolicyManager, mock(StorageManager.class), mock(TrustManager.class),
117 mock(KeyguardManager.class));
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000118 mStorage = new LockSettingsStorageTestable(mContext,
119 new File(getContext().getFilesDir(), "locksettings"));
120 File storageDir = mStorage.mStorageDir;
121 if (storageDir.exists()) {
122 FileUtils.deleteContents(storageDir);
123 } else {
124 storageDir.mkdirs();
125 }
126
Lenka Trochtova66c492a2018-12-06 11:29:21 +0100127 mHasSecureLockScreen = true;
Rubin Xu16c823e2017-06-27 14:44:58 +0100128 mLockPatternUtils = new LockPatternUtils(mContext) {
129 @Override
130 public ILockSettings getLockSettings() {
131 return mService;
132 }
Lenka Trochtova66c492a2018-12-06 11:29:21 +0100133
134 @Override
135 public boolean hasSecureLockScreen() {
136 return mHasSecureLockScreen;
137 }
Rubin Xu16c823e2017-06-27 14:44:58 +0100138 };
Adrian Roos2adc2632017-09-05 17:01:42 +0200139 mSpManager = new MockSyntheticPasswordManager(mContext, mStorage, mGateKeeperService,
David Anderson28dea682019-02-20 13:37:51 -0800140 mUserManager, mPasswordSlotManager);
Andrew Sculle6527c12018-01-05 18:33:58 +0000141 mAuthSecretService = mock(IAuthSecret.class);
Rubin Xub31be1b2017-06-16 17:08:21 +0100142 mService = new LockSettingsServiceTestable(mContext, mLockPatternUtils, mStorage,
143 mGateKeeperService, mKeyStore, setUpStorageManagerMock(), mActivityManager,
David Anderson6ebc25b2019-02-12 16:25:56 -0800144 mSpManager, mAuthSecretService, mGsiService);
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000145 when(mUserManager.getUserInfo(eq(PRIMARY_USER_ID))).thenReturn(PRIMARY_USER_INFO);
Andrew Scull8e87af52017-03-03 15:38:48 +0000146 mPrimaryUserProfiles.add(PRIMARY_USER_INFO);
147 installChildProfile(MANAGED_PROFILE_USER_ID);
Charles Hedec05402017-04-21 13:45:34 +0100148 installAndTurnOffChildProfile(TURNED_OFF_PROFILE_USER_ID);
Andrew Scull8e87af52017-03-03 15:38:48 +0000149 when(mUserManager.getProfiles(eq(PRIMARY_USER_ID))).thenReturn(mPrimaryUserProfiles);
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000150 when(mUserManager.getUserInfo(eq(SECONDARY_USER_ID))).thenReturn(SECONDARY_USER_INFO);
151
Andrew Sculle6527c12018-01-05 18:33:58 +0000152 final ArrayList<UserInfo> allUsers = new ArrayList<>(mPrimaryUserProfiles);
153 allUsers.add(SECONDARY_USER_INFO);
154 when(mUserManager.getUsers(anyBoolean())).thenReturn(allUsers);
155
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000156 when(mActivityManager.unlockUser(anyInt(), any(), any(), any())).thenAnswer(
157 new Answer<Boolean>() {
158 @Override
159 public Boolean answer(InvocationOnMock invocation) throws Throwable {
160 Object[] args = invocation.getArguments();
161 mStorageManager.unlockUser((int)args[0], (byte[])args[2],
162 (IProgressListener) args[3]);
163 return true;
164 }
165 });
166
Rubin Xu8b30ec32017-03-05 00:47:09 +0000167 // Adding a fake Device Owner app which will enable escrow token support in LSS.
168 when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(
169 new ComponentName("com.dummy.package", ".FakeDeviceOwner"));
Rubin Xufcd49f92017-08-24 18:21:52 +0100170 mLocalService = LocalServices.getService(LockSettingsInternal.class);
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000171 }
172
Andrew Scull8e87af52017-03-03 15:38:48 +0000173 private UserInfo installChildProfile(int profileId) {
174 final UserInfo userInfo = new UserInfo(
175 profileId, null, null, UserInfo.FLAG_INITIALIZED | UserInfo.FLAG_MANAGED_PROFILE);
176 mPrimaryUserProfiles.add(userInfo);
177 when(mUserManager.getUserInfo(eq(profileId))).thenReturn(userInfo);
178 when(mUserManager.getProfileParent(eq(profileId))).thenReturn(PRIMARY_USER_INFO);
Charles Hedec05402017-04-21 13:45:34 +0100179 when(mUserManager.isUserRunning(eq(profileId))).thenReturn(true);
180 when(mUserManager.isUserUnlocked(eq(profileId))).thenReturn(true);
Andrew Scull8e87af52017-03-03 15:38:48 +0000181 return userInfo;
182 }
183
Charles Hedec05402017-04-21 13:45:34 +0100184 private UserInfo installAndTurnOffChildProfile(int profileId) {
Andrew Scull8e87af52017-03-03 15:38:48 +0000185 final UserInfo userInfo = installChildProfile(profileId);
186 userInfo.flags |= UserInfo.FLAG_QUIET_MODE;
Charles Hedec05402017-04-21 13:45:34 +0100187 when(mUserManager.isUserRunning(eq(profileId))).thenReturn(false);
188 when(mUserManager.isUserUnlocked(eq(profileId))).thenReturn(false);
Andrew Scull8e87af52017-03-03 15:38:48 +0000189 return userInfo;
190 }
191
Rubin Xub31be1b2017-06-16 17:08:21 +0100192 private IStorageManager setUpStorageManagerMock() throws RemoteException {
193 final IStorageManager sm = mock(IStorageManager.class);
194
195 doAnswer(new Answer<Void>() {
196 @Override
197 public Void answer(InvocationOnMock invocation) throws Throwable {
198 Object[] args = invocation.getArguments();
199 mStorageManager.addUserKeyAuth((int) args[0] /* userId */,
200 (int) args[1] /* serialNumber */,
201 (byte[]) args[2] /* token */,
202 (byte[]) args[3] /* secret */);
203 return null;
204 }
205 }).when(sm).addUserKeyAuth(anyInt(), anyInt(), any(), any());
206
207 doAnswer(
208 new Answer<Void>() {
209 @Override
210 public Void answer(InvocationOnMock invocation) throws Throwable {
211 Object[] args = invocation.getArguments();
212 mStorageManager.fixateNewestUserKeyAuth((int) args[0] /* userId */);
213 return null;
214 }
215 }).when(sm).fixateNewestUserKeyAuth(anyInt());
216 return sm;
217 }
218
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000219 @Override
220 protected void tearDown() throws Exception {
221 super.tearDown();
222 mStorage.closeDatabase();
223 File db = getContext().getDatabasePath("locksettings.db");
224 assertTrue(!db.exists() || db.delete());
225
226 File storageDir = mStorage.mStorageDir;
227 assertTrue(FileUtils.deleteContents(storageDir));
David Anderson28dea682019-02-20 13:37:51 -0800228
229 mPasswordSlotManager.cleanup();
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000230 }
Rubin Xu3bf722a2016-12-15 16:07:38 +0000231
Andrew Scull7f4ff4c2018-01-05 18:33:58 +0000232 protected void assertNotEquals(long expected, long actual) {
233 assertTrue(expected != actual);
234 }
235
Rubin Xu3bf722a2016-12-15 16:07:38 +0000236 protected static void assertArrayEquals(byte[] expected, byte[] actual) {
237 assertTrue(Arrays.equals(expected, actual));
238 }
239
Andrew Scull7f4ff4c2018-01-05 18:33:58 +0000240 protected static void assertArrayNotEquals(byte[] expected, byte[] actual) {
Rubin Xu3bf722a2016-12-15 16:07:38 +0000241 assertFalse(Arrays.equals(expected, actual));
242 }
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000243}