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