2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
17 package com.android.server;
19 import static org.mockito.Matchers.any;
20 import static org.mockito.Matchers.anyBoolean;
21 import static org.mockito.Matchers.anyInt;
22 import static org.mockito.Matchers.eq;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
27 import android.app.IActivityManager;
28 import android.app.NotificationManager;
29 import android.app.admin.DevicePolicyManager;
30 import android.content.ComponentName;
31 import android.content.Context;
32 import android.content.pm.UserInfo;
33 import android.database.sqlite.SQLiteDatabase;
34 import android.os.FileUtils;
35 import android.os.Handler;
36 import android.os.IProgressListener;
37 import android.os.RemoteException;
38 import android.os.UserManager;
39 import android.os.storage.IStorageManager;
40 import android.security.KeyStore;
41 import android.service.gatekeeper.GateKeeperResponse;
42 import android.service.gatekeeper.IGateKeeperService;
43 import android.test.AndroidTestCase;
45 import com.android.internal.widget.LockPatternUtils;
46 import com.android.internal.widget.VerifyCredentialResponse;
47 import com.android.server.LockSettingsService.SynchronizedStrongAuthTracker;
48 import com.android.server.LockSettingsStorage.CredentialHash;
49 import com.android.server.MockGateKeeperService.AuthToken;
50 import com.android.server.MockGateKeeperService.VerifyHandle;
52 import org.mockito.invocation.InvocationOnMock;
53 import org.mockito.stubbing.Answer;
56 import java.util.Arrays;
57 import java.util.ArrayList;
60 public class BaseLockSettingsServiceTests extends AndroidTestCase {
61 protected static final int PRIMARY_USER_ID = 0;
62 protected static final int MANAGED_PROFILE_USER_ID = 12;
63 protected static final int TURNED_OFF_PROFILE_USER_ID = 17;
64 protected static final int SECONDARY_USER_ID = 20;
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);
68 private static final UserInfo SECONDARY_USER_INFO = new UserInfo(SECONDARY_USER_ID, null, null,
69 UserInfo.FLAG_INITIALIZED);
71 private ArrayList<UserInfo> mPrimaryUserProfiles = new ArrayList<>();
73 LockSettingsService mService;
75 MockLockSettingsContext mContext;
76 LockSettingsStorageTestable mStorage;
78 LockPatternUtils mLockPatternUtils;
79 MockGateKeeperService mGateKeeperService;
80 NotificationManager mNotificationManager;
81 UserManager mUserManager;
82 MockStorageManager mStorageManager;
83 IActivityManager mActivityManager;
84 DevicePolicyManager mDevicePolicyManager;
86 MockSyntheticPasswordManager mSpManager;
89 protected void setUp() throws Exception {
92 mLockPatternUtils = mock(LockPatternUtils.class);
93 mGateKeeperService = new MockGateKeeperService();
94 mNotificationManager = mock(NotificationManager.class);
95 mUserManager = mock(UserManager.class);
96 mStorageManager = new MockStorageManager();
97 mActivityManager = mock(IActivityManager.class);
98 mDevicePolicyManager = mock(DevicePolicyManager.class);
100 mContext = new MockLockSettingsContext(getContext(), mUserManager, mNotificationManager,
101 mDevicePolicyManager);
102 mStorage = new LockSettingsStorageTestable(mContext,
103 new File(getContext().getFilesDir(), "locksettings"));
104 File storageDir = mStorage.mStorageDir;
105 if (storageDir.exists()) {
106 FileUtils.deleteContents(storageDir);
111 mSpManager = new MockSyntheticPasswordManager(mStorage, mGateKeeperService);
112 mService = new LockSettingsServiceTestable(mContext, mLockPatternUtils,
113 mStorage, mGateKeeperService, mKeyStore, mStorageManager, mActivityManager,
115 when(mUserManager.getUserInfo(eq(PRIMARY_USER_ID))).thenReturn(PRIMARY_USER_INFO);
116 mPrimaryUserProfiles.add(PRIMARY_USER_INFO);
117 installChildProfile(MANAGED_PROFILE_USER_ID);
118 installQuietModeChildProfile(TURNED_OFF_PROFILE_USER_ID);
119 when(mUserManager.getUsers(anyBoolean())).thenReturn(mPrimaryUserProfiles);
120 when(mUserManager.getProfiles(eq(PRIMARY_USER_ID))).thenReturn(mPrimaryUserProfiles);
121 when(mUserManager.getUserInfo(eq(SECONDARY_USER_ID))).thenReturn(SECONDARY_USER_INFO);
122 when(mUserManager.isUserRunning(eq(MANAGED_PROFILE_USER_ID))).thenReturn(true);
124 when(mActivityManager.unlockUser(anyInt(), any(), any(), any())).thenAnswer(
125 new Answer<Boolean>() {
127 public Boolean answer(InvocationOnMock invocation) throws Throwable {
128 Object[] args = invocation.getArguments();
129 mStorageManager.unlockUser((int)args[0], (byte[])args[2],
130 (IProgressListener) args[3]);
135 when(mLockPatternUtils.getLockSettings()).thenReturn(mService);
137 // Adding a fake Device Owner app which will enable escrow token support in LSS.
138 when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(
139 new ComponentName("com.dummy.package", ".FakeDeviceOwner"));
142 private UserInfo installChildProfile(int profileId) {
143 final UserInfo userInfo = new UserInfo(
144 profileId, null, null, UserInfo.FLAG_INITIALIZED | UserInfo.FLAG_MANAGED_PROFILE);
145 mPrimaryUserProfiles.add(userInfo);
146 when(mUserManager.getUserInfo(eq(profileId))).thenReturn(userInfo);
147 when(mUserManager.getProfileParent(eq(profileId))).thenReturn(PRIMARY_USER_INFO);
151 private UserInfo installQuietModeChildProfile(int profileId) {
152 final UserInfo userInfo = installChildProfile(profileId);
153 userInfo.flags |= UserInfo.FLAG_QUIET_MODE;
158 protected void tearDown() throws Exception {
160 mStorage.closeDatabase();
161 File db = getContext().getDatabasePath("locksettings.db");
162 assertTrue(!db.exists() || db.delete());
164 File storageDir = mStorage.mStorageDir;
165 assertTrue(FileUtils.deleteContents(storageDir));
168 protected static void assertArrayEquals(byte[] expected, byte[] actual) {
169 assertTrue(Arrays.equals(expected, actual));
172 protected static void assertArrayNotSame(byte[] expected, byte[] actual) {
173 assertFalse(Arrays.equals(expected, actual));