OSDN Git Service

Integrate weaver into authentication flow
[android-x86/frameworks-base.git] / services / tests / servicestests / src / com / android / server / BaseLockSettingsServiceTests.java
1 /*
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
17 package com.android.server;
18
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;
26
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;
44
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;
51
52 import org.mockito.invocation.InvocationOnMock;
53 import org.mockito.stubbing.Answer;
54
55 import java.io.File;
56 import java.util.Arrays;
57 import java.util.ArrayList;
58
59
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;
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);
68     private static final UserInfo SECONDARY_USER_INFO = new UserInfo(SECONDARY_USER_ID, null, null,
69             UserInfo.FLAG_INITIALIZED);
70
71     private ArrayList<UserInfo> mPrimaryUserProfiles = new ArrayList<>();
72
73     LockSettingsService mService;
74
75     MockLockSettingsContext mContext;
76     LockSettingsStorageTestable mStorage;
77
78     LockPatternUtils mLockPatternUtils;
79     MockGateKeeperService mGateKeeperService;
80     NotificationManager mNotificationManager;
81     UserManager mUserManager;
82     MockStorageManager mStorageManager;
83     IActivityManager mActivityManager;
84     DevicePolicyManager mDevicePolicyManager;
85     KeyStore mKeyStore;
86     MockSyntheticPasswordManager mSpManager;
87
88     @Override
89     protected void setUp() throws Exception {
90         super.setUp();
91
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);
99
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);
107         } else {
108             storageDir.mkdirs();
109         }
110
111         mSpManager = new MockSyntheticPasswordManager(mStorage, mGateKeeperService);
112         mService = new LockSettingsServiceTestable(mContext, mLockPatternUtils,
113                 mStorage, mGateKeeperService, mKeyStore, mStorageManager, mActivityManager,
114                 mSpManager);
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);
123
124         when(mActivityManager.unlockUser(anyInt(), any(), any(), any())).thenAnswer(
125                 new Answer<Boolean>() {
126             @Override
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]);
131                 return true;
132             }
133         });
134
135         when(mLockPatternUtils.getLockSettings()).thenReturn(mService);
136
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"));
140     }
141
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);
148         return userInfo;
149     }
150
151     private UserInfo installQuietModeChildProfile(int profileId) {
152         final UserInfo userInfo = installChildProfile(profileId);
153         userInfo.flags |= UserInfo.FLAG_QUIET_MODE;
154         return userInfo;
155     }
156
157     @Override
158     protected void tearDown() throws Exception {
159         super.tearDown();
160         mStorage.closeDatabase();
161         File db = getContext().getDatabasePath("locksettings.db");
162         assertTrue(!db.exists() || db.delete());
163
164         File storageDir = mStorage.mStorageDir;
165         assertTrue(FileUtils.deleteContents(storageDir));
166     }
167
168     protected static void assertArrayEquals(byte[] expected, byte[] actual) {
169         assertTrue(Arrays.equals(expected, actual));
170     }
171
172     protected static void assertArrayNotSame(byte[] expected, byte[] actual) {
173         assertFalse(Arrays.equals(expected, actual));
174     }
175 }
176