BaseLockSettingsServiceTests.java revision 8e87af55337a19cf7242a1bf2f516bc26ae65f9e
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
17package com.android.server;
18
19import static org.mockito.Matchers.any;
20import static org.mockito.Matchers.anyInt;
21import static org.mockito.Matchers.eq;
22import static org.mockito.Mockito.mock;
23import static org.mockito.Mockito.verify;
24import static org.mockito.Mockito.when;
25
26import android.app.IActivityManager;
27import android.app.NotificationManager;
28import android.app.admin.DevicePolicyManager;
29import android.content.ComponentName;
30import android.content.Context;
31import android.content.pm.UserInfo;
32import android.database.sqlite.SQLiteDatabase;
33import android.os.FileUtils;
34import android.os.Handler;
35import android.os.IProgressListener;
36import android.os.RemoteException;
37import android.os.UserManager;
38import android.os.storage.IStorageManager;
39import android.security.KeyStore;
40import android.service.gatekeeper.GateKeeperResponse;
41import android.service.gatekeeper.IGateKeeperService;
42import android.test.AndroidTestCase;
43
44import com.android.internal.widget.LockPatternUtils;
45import com.android.internal.widget.VerifyCredentialResponse;
46import com.android.server.LockSettingsService.SynchronizedStrongAuthTracker;
47import com.android.server.LockSettingsStorage.CredentialHash;
48import com.android.server.MockGateKeeperService.AuthToken;
49import com.android.server.MockGateKeeperService.VerifyHandle;
50
51import org.mockito.invocation.InvocationOnMock;
52import org.mockito.stubbing.Answer;
53
54import java.io.File;
55import java.util.Arrays;
56import java.util.ArrayList;
57
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;
62    protected static final int TURNED_OFF_PROFILE_USER_ID = 17;
63    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);
67    private static final UserInfo SECONDARY_USER_INFO = new UserInfo(SECONDARY_USER_ID, null, null,
68            UserInfo.FLAG_INITIALIZED);
69
70    private ArrayList<UserInfo> mPrimaryUserProfiles = new ArrayList<>();
71
72    LockSettingsService mService;
73
74    MockLockSettingsContext mContext;
75    LockSettingsStorageTestable mStorage;
76
77    LockPatternUtils mLockPatternUtils;
78    MockGateKeeperService mGateKeeperService;
79    NotificationManager mNotificationManager;
80    UserManager mUserManager;
81    MockStorageManager mStorageManager;
82    IActivityManager mActivityManager;
83    DevicePolicyManager mDevicePolicyManager;
84    KeyStore mKeyStore;
85
86    @Override
87    protected void setUp() throws Exception {
88        super.setUp();
89
90        mLockPatternUtils = mock(LockPatternUtils.class);
91        mGateKeeperService = new MockGateKeeperService();
92        mNotificationManager = mock(NotificationManager.class);
93        mUserManager = mock(UserManager.class);
94        mStorageManager = new MockStorageManager();
95        mActivityManager = mock(IActivityManager.class);
96        mDevicePolicyManager = mock(DevicePolicyManager.class);
97        mContext = new MockLockSettingsContext(getContext(), mUserManager, mNotificationManager,
98                mDevicePolicyManager);
99        mStorage = new LockSettingsStorageTestable(mContext,
100                new File(getContext().getFilesDir(), "locksettings"));
101        File storageDir = mStorage.mStorageDir;
102        if (storageDir.exists()) {
103            FileUtils.deleteContents(storageDir);
104        } else {
105            storageDir.mkdirs();
106        }
107
108        mService = new LockSettingsServiceTestable(mContext, mLockPatternUtils,
109                mStorage, mGateKeeperService, mKeyStore, mStorageManager, mActivityManager);
110        when(mUserManager.getUserInfo(eq(PRIMARY_USER_ID))).thenReturn(PRIMARY_USER_INFO);
111        mPrimaryUserProfiles.add(PRIMARY_USER_INFO);
112        installChildProfile(MANAGED_PROFILE_USER_ID);
113        installQuietModeChildProfile(TURNED_OFF_PROFILE_USER_ID);
114        when(mUserManager.getProfiles(eq(PRIMARY_USER_ID))).thenReturn(mPrimaryUserProfiles);
115        when(mUserManager.getUserInfo(eq(SECONDARY_USER_ID))).thenReturn(SECONDARY_USER_INFO);
116        when(mUserManager.isUserRunning(eq(MANAGED_PROFILE_USER_ID))).thenReturn(true);
117
118        when(mActivityManager.unlockUser(anyInt(), any(), any(), any())).thenAnswer(
119                new Answer<Boolean>() {
120            @Override
121            public Boolean answer(InvocationOnMock invocation) throws Throwable {
122                Object[] args = invocation.getArguments();
123                mStorageManager.unlockUser((int)args[0], (byte[])args[2],
124                        (IProgressListener) args[3]);
125                return true;
126            }
127        });
128
129        when(mLockPatternUtils.getLockSettings()).thenReturn(mService);
130
131        // Adding a fake Device Owner app which will enable escrow token support in LSS.
132        when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(
133                new ComponentName("com.dummy.package", ".FakeDeviceOwner"));
134    }
135
136    private UserInfo installChildProfile(int profileId) {
137        final UserInfo userInfo = new UserInfo(
138            profileId, null, null, UserInfo.FLAG_INITIALIZED | UserInfo.FLAG_MANAGED_PROFILE);
139        mPrimaryUserProfiles.add(userInfo);
140        when(mUserManager.getUserInfo(eq(profileId))).thenReturn(userInfo);
141        when(mUserManager.getProfileParent(eq(profileId))).thenReturn(PRIMARY_USER_INFO);
142        return userInfo;
143    }
144
145    private UserInfo installQuietModeChildProfile(int profileId) {
146        final UserInfo userInfo = installChildProfile(profileId);
147        userInfo.flags |= UserInfo.FLAG_QUIET_MODE;
148        return userInfo;
149    }
150
151    @Override
152    protected void tearDown() throws Exception {
153        super.tearDown();
154        mStorage.closeDatabase();
155        File db = getContext().getDatabasePath("locksettings.db");
156        assertTrue(!db.exists() || db.delete());
157
158        File storageDir = mStorage.mStorageDir;
159        assertTrue(FileUtils.deleteContents(storageDir));
160    }
161
162    protected static void assertArrayEquals(byte[] expected, byte[] actual) {
163        assertTrue(Arrays.equals(expected, actual));
164    }
165
166    protected static void assertArrayNotSame(byte[] expected, byte[] actual) {
167        assertFalse(Arrays.equals(expected, actual));
168    }
169}
170
171