blob: aadf92448a52c84b5e38f5cc76367b1cb02185a2 [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;
Lenka Trochtova66c492a2018-12-06 11:29:21 +010091 protected boolean mHasSecureLockScreen;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000092
93 @Override
94 protected void setUp() throws Exception {
95 super.setUp();
96
Rubin Xu16c823e2017-06-27 14:44:58 +010097 mGateKeeperService = new FakeGateKeeperService();
Rubin Xu0cbc19e2016-12-09 14:00:21 +000098 mNotificationManager = mock(NotificationManager.class);
99 mUserManager = mock(UserManager.class);
Rubin Xub31be1b2017-06-16 17:08:21 +0100100 mStorageManager = new FakeStorageManager();
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000101 mActivityManager = mock(IActivityManager.class);
Rubin Xu8b30ec32017-03-05 00:47:09 +0000102 mDevicePolicyManager = mock(DevicePolicyManager.class);
Andrew Scull1416bd02018-01-05 18:33:58 +0000103 mDevicePolicyManagerInternal = mock(DevicePolicyManagerInternal.class);
Lenka Trochtova66c492a2018-12-06 11:29:21 +0100104 mMockWindowManager = mock(WindowManagerInternal.class);
David Anderson6ebc25b2019-02-12 16:25:56 -0800105 mGsiService = new FakeGsiService();
Andrew Scull1416bd02018-01-05 18:33:58 +0000106
Rubin Xufcd49f92017-08-24 18:21:52 +0100107 LocalServices.removeServiceForTest(LockSettingsInternal.class);
Andrew Scull1416bd02018-01-05 18:33:58 +0000108 LocalServices.removeServiceForTest(DevicePolicyManagerInternal.class);
Lenka Trochtova66c492a2018-12-06 11:29:21 +0100109 LocalServices.removeServiceForTest(WindowManagerInternal.class);
Andrew Scull1416bd02018-01-05 18:33:58 +0000110 LocalServices.addService(DevicePolicyManagerInternal.class, mDevicePolicyManagerInternal);
Lenka Trochtova66c492a2018-12-06 11:29:21 +0100111 LocalServices.addService(WindowManagerInternal.class, mMockWindowManager);
Rubin Xu7b7424b2017-03-31 18:03:20 +0100112
Rubin Xu8b30ec32017-03-05 00:47:09 +0000113 mContext = new MockLockSettingsContext(getContext(), mUserManager, mNotificationManager,
Andrew Scullf49794b2018-04-13 12:01:25 +0100114 mDevicePolicyManager, mock(StorageManager.class), mock(TrustManager.class),
115 mock(KeyguardManager.class));
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000116 mStorage = new LockSettingsStorageTestable(mContext,
117 new File(getContext().getFilesDir(), "locksettings"));
118 File storageDir = mStorage.mStorageDir;
119 if (storageDir.exists()) {
120 FileUtils.deleteContents(storageDir);
121 } else {
122 storageDir.mkdirs();
123 }
124
Lenka Trochtova66c492a2018-12-06 11:29:21 +0100125 mHasSecureLockScreen = true;
Rubin Xu16c823e2017-06-27 14:44:58 +0100126 mLockPatternUtils = new LockPatternUtils(mContext) {
127 @Override
128 public ILockSettings getLockSettings() {
129 return mService;
130 }
Lenka Trochtova66c492a2018-12-06 11:29:21 +0100131
132 @Override
133 public boolean hasSecureLockScreen() {
134 return mHasSecureLockScreen;
135 }
Rubin Xu16c823e2017-06-27 14:44:58 +0100136 };
Adrian Roos2adc2632017-09-05 17:01:42 +0200137 mSpManager = new MockSyntheticPasswordManager(mContext, mStorage, mGateKeeperService,
138 mUserManager);
Andrew Sculle6527c12018-01-05 18:33:58 +0000139 mAuthSecretService = mock(IAuthSecret.class);
Rubin Xub31be1b2017-06-16 17:08:21 +0100140 mService = new LockSettingsServiceTestable(mContext, mLockPatternUtils, mStorage,
141 mGateKeeperService, mKeyStore, setUpStorageManagerMock(), mActivityManager,
David Anderson6ebc25b2019-02-12 16:25:56 -0800142 mSpManager, mAuthSecretService, mGsiService);
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000143 when(mUserManager.getUserInfo(eq(PRIMARY_USER_ID))).thenReturn(PRIMARY_USER_INFO);
Andrew Scull8e87af52017-03-03 15:38:48 +0000144 mPrimaryUserProfiles.add(PRIMARY_USER_INFO);
145 installChildProfile(MANAGED_PROFILE_USER_ID);
Charles Hedec05402017-04-21 13:45:34 +0100146 installAndTurnOffChildProfile(TURNED_OFF_PROFILE_USER_ID);
Andrew Scull8e87af52017-03-03 15:38:48 +0000147 when(mUserManager.getProfiles(eq(PRIMARY_USER_ID))).thenReturn(mPrimaryUserProfiles);
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000148 when(mUserManager.getUserInfo(eq(SECONDARY_USER_ID))).thenReturn(SECONDARY_USER_INFO);
149
Andrew Sculle6527c12018-01-05 18:33:58 +0000150 final ArrayList<UserInfo> allUsers = new ArrayList<>(mPrimaryUserProfiles);
151 allUsers.add(SECONDARY_USER_INFO);
152 when(mUserManager.getUsers(anyBoolean())).thenReturn(allUsers);
153
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000154 when(mActivityManager.unlockUser(anyInt(), any(), any(), any())).thenAnswer(
155 new Answer<Boolean>() {
156 @Override
157 public Boolean answer(InvocationOnMock invocation) throws Throwable {
158 Object[] args = invocation.getArguments();
159 mStorageManager.unlockUser((int)args[0], (byte[])args[2],
160 (IProgressListener) args[3]);
161 return true;
162 }
163 });
164
Rubin Xu8b30ec32017-03-05 00:47:09 +0000165 // Adding a fake Device Owner app which will enable escrow token support in LSS.
166 when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(
167 new ComponentName("com.dummy.package", ".FakeDeviceOwner"));
Rubin Xufcd49f92017-08-24 18:21:52 +0100168 mLocalService = LocalServices.getService(LockSettingsInternal.class);
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000169 }
170
Andrew Scull8e87af52017-03-03 15:38:48 +0000171 private UserInfo installChildProfile(int profileId) {
172 final UserInfo userInfo = new UserInfo(
173 profileId, null, null, UserInfo.FLAG_INITIALIZED | UserInfo.FLAG_MANAGED_PROFILE);
174 mPrimaryUserProfiles.add(userInfo);
175 when(mUserManager.getUserInfo(eq(profileId))).thenReturn(userInfo);
176 when(mUserManager.getProfileParent(eq(profileId))).thenReturn(PRIMARY_USER_INFO);
Charles Hedec05402017-04-21 13:45:34 +0100177 when(mUserManager.isUserRunning(eq(profileId))).thenReturn(true);
178 when(mUserManager.isUserUnlocked(eq(profileId))).thenReturn(true);
Andrew Scull8e87af52017-03-03 15:38:48 +0000179 return userInfo;
180 }
181
Charles Hedec05402017-04-21 13:45:34 +0100182 private UserInfo installAndTurnOffChildProfile(int profileId) {
Andrew Scull8e87af52017-03-03 15:38:48 +0000183 final UserInfo userInfo = installChildProfile(profileId);
184 userInfo.flags |= UserInfo.FLAG_QUIET_MODE;
Charles Hedec05402017-04-21 13:45:34 +0100185 when(mUserManager.isUserRunning(eq(profileId))).thenReturn(false);
186 when(mUserManager.isUserUnlocked(eq(profileId))).thenReturn(false);
Andrew Scull8e87af52017-03-03 15:38:48 +0000187 return userInfo;
188 }
189
Rubin Xub31be1b2017-06-16 17:08:21 +0100190 private IStorageManager setUpStorageManagerMock() throws RemoteException {
191 final IStorageManager sm = mock(IStorageManager.class);
192
193 doAnswer(new Answer<Void>() {
194 @Override
195 public Void answer(InvocationOnMock invocation) throws Throwable {
196 Object[] args = invocation.getArguments();
197 mStorageManager.addUserKeyAuth((int) args[0] /* userId */,
198 (int) args[1] /* serialNumber */,
199 (byte[]) args[2] /* token */,
200 (byte[]) args[3] /* secret */);
201 return null;
202 }
203 }).when(sm).addUserKeyAuth(anyInt(), anyInt(), any(), any());
204
205 doAnswer(
206 new Answer<Void>() {
207 @Override
208 public Void answer(InvocationOnMock invocation) throws Throwable {
209 Object[] args = invocation.getArguments();
210 mStorageManager.fixateNewestUserKeyAuth((int) args[0] /* userId */);
211 return null;
212 }
213 }).when(sm).fixateNewestUserKeyAuth(anyInt());
214 return sm;
215 }
216
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000217 @Override
218 protected void tearDown() throws Exception {
219 super.tearDown();
220 mStorage.closeDatabase();
221 File db = getContext().getDatabasePath("locksettings.db");
222 assertTrue(!db.exists() || db.delete());
223
224 File storageDir = mStorage.mStorageDir;
225 assertTrue(FileUtils.deleteContents(storageDir));
226 }
Rubin Xu3bf722a2016-12-15 16:07:38 +0000227
Andrew Scull7f4ff4c2018-01-05 18:33:58 +0000228 protected void assertNotEquals(long expected, long actual) {
229 assertTrue(expected != actual);
230 }
231
Rubin Xu3bf722a2016-12-15 16:07:38 +0000232 protected static void assertArrayEquals(byte[] expected, byte[] actual) {
233 assertTrue(Arrays.equals(expected, actual));
234 }
235
Andrew Scull7f4ff4c2018-01-05 18:33:58 +0000236 protected static void assertArrayNotEquals(byte[] expected, byte[] actual) {
Rubin Xu3bf722a2016-12-15 16:07:38 +0000237 assertFalse(Arrays.equals(expected, actual));
238 }
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000239}