BaseLockSettingsServiceTests.java revision 3bf722a8d54ca7192dfe07ee7b73eac7d25ccac5
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.content.Context;
29import android.content.pm.UserInfo;
30import android.database.sqlite.SQLiteDatabase;
31import android.os.FileUtils;
32import android.os.Handler;
33import android.os.IProgressListener;
34import android.os.RemoteException;
35import android.os.UserManager;
36import android.os.storage.IStorageManager;
37import android.security.KeyStore;
38import android.service.gatekeeper.GateKeeperResponse;
39import android.service.gatekeeper.IGateKeeperService;
40import android.test.AndroidTestCase;
41
42import com.android.internal.widget.LockPatternUtils;
43import com.android.internal.widget.VerifyCredentialResponse;
44import com.android.server.LockSettingsService.SynchronizedStrongAuthTracker;
45import com.android.server.LockSettingsStorage.CredentialHash;
46import com.android.server.MockGateKeeperService.AuthToken;
47import com.android.server.MockGateKeeperService.VerifyHandle;
48
49import org.mockito.invocation.InvocationOnMock;
50import org.mockito.stubbing.Answer;
51
52import java.io.File;
53import java.util.Arrays;
54
55
56public class BaseLockSettingsServiceTests extends AndroidTestCase {
57    protected static final int PRIMARY_USER_ID = 0;
58    protected static final int MANAGED_PROFILE_USER_ID = 12;
59    protected static final int SECONDARY_USER_ID = 20;
60
61    private static final UserInfo PRIMARY_USER_INFO = new UserInfo(PRIMARY_USER_ID, null, null,
62            UserInfo.FLAG_INITIALIZED | UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY);
63    private static final UserInfo MANAGED_PROFILE_INFO = new UserInfo(MANAGED_PROFILE_USER_ID, null,
64            null, UserInfo.FLAG_INITIALIZED | UserInfo.FLAG_MANAGED_PROFILE);
65    private static final UserInfo SECONDARY_USER_INFO = new UserInfo(SECONDARY_USER_ID, null, null,
66            UserInfo.FLAG_INITIALIZED);
67
68    LockSettingsService mService;
69
70    MockLockSettingsContext mContext;
71    LockSettingsStorageTestable mStorage;
72
73    LockPatternUtils mLockPatternUtils;
74    MockGateKeeperService mGateKeeperService;
75    NotificationManager mNotificationManager;
76    UserManager mUserManager;
77    MockStorageManager mStorageManager;
78    IActivityManager mActivityManager;
79
80    KeyStore mKeyStore;
81
82    @Override
83    protected void setUp() throws Exception {
84        super.setUp();
85
86        mLockPatternUtils = mock(LockPatternUtils.class);
87        mGateKeeperService = new MockGateKeeperService();
88        mNotificationManager = mock(NotificationManager.class);
89        mUserManager = mock(UserManager.class);
90        mStorageManager = new MockStorageManager();
91        mActivityManager = mock(IActivityManager.class);
92        mContext = new MockLockSettingsContext(getContext(), mUserManager, mNotificationManager);
93        mStorage = new LockSettingsStorageTestable(mContext,
94                new File(getContext().getFilesDir(), "locksettings"));
95        File storageDir = mStorage.mStorageDir;
96        if (storageDir.exists()) {
97            FileUtils.deleteContents(storageDir);
98        } else {
99            storageDir.mkdirs();
100        }
101
102        mService = new LockSettingsServiceTestable(mContext, mLockPatternUtils,
103                mStorage, mGateKeeperService, mKeyStore, mStorageManager, mActivityManager);
104        when(mUserManager.getUserInfo(eq(PRIMARY_USER_ID))).thenReturn(PRIMARY_USER_INFO);
105        when(mUserManager.getProfiles(eq(PRIMARY_USER_ID))).thenReturn(Arrays.asList(
106                new UserInfo[] {PRIMARY_USER_INFO, MANAGED_PROFILE_INFO}));
107        when(mUserManager.getUserInfo(eq(MANAGED_PROFILE_USER_ID))).thenReturn(
108                MANAGED_PROFILE_INFO);
109        when(mUserManager.getProfileParent(eq(MANAGED_PROFILE_USER_ID))).thenReturn(
110                PRIMARY_USER_INFO);
111        when(mUserManager.getUserInfo(eq(SECONDARY_USER_ID))).thenReturn(SECONDARY_USER_INFO);
112
113        when(mActivityManager.unlockUser(anyInt(), any(), any(), any())).thenAnswer(
114                new Answer<Boolean>() {
115            @Override
116            public Boolean answer(InvocationOnMock invocation) throws Throwable {
117                Object[] args = invocation.getArguments();
118                mStorageManager.unlockUser((int)args[0], (byte[])args[2],
119                        (IProgressListener) args[3]);
120                return true;
121            }
122        });
123
124        when(mLockPatternUtils.getLockSettings()).thenReturn(mService);
125    }
126
127    @Override
128    protected void tearDown() throws Exception {
129        super.tearDown();
130        mStorage.closeDatabase();
131        File db = getContext().getDatabasePath("locksettings.db");
132        assertTrue(!db.exists() || db.delete());
133
134        File storageDir = mStorage.mStorageDir;
135        assertTrue(FileUtils.deleteContents(storageDir));
136    }
137
138    protected static void assertArrayEquals(byte[] expected, byte[] actual) {
139        assertTrue(Arrays.equals(expected, actual));
140    }
141
142    protected static void assertArrayNotSame(byte[] expected, byte[] actual) {
143        assertFalse(Arrays.equals(expected, actual));
144    }
145}
146
147