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.anyInt;
21 import static org.mockito.Matchers.eq;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.when;
24 
25 import android.app.IActivityManager;
26 import android.app.NotificationManager;
27 import android.app.admin.DevicePolicyManager;
28 import android.content.ComponentName;
29 import android.content.pm.UserInfo;
30 import android.os.FileUtils;
31 import android.os.IProgressListener;
32 import android.os.UserManager;
33 import android.os.storage.StorageManager;
34 import android.security.KeyStore;
35 import android.test.AndroidTestCase;
36 
37 import com.android.internal.widget.LockPatternUtils;
38 
39 import org.mockito.invocation.InvocationOnMock;
40 import org.mockito.stubbing.Answer;
41 
42 import java.io.File;
43 import java.util.ArrayList;
44 import java.util.Arrays;
45 
46 
47 public 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
setUp()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 
installChildProfile(int profileId)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 
installAndTurnOffChildProfile(int profileId)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
tearDown()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 
assertArrayEquals(byte[] expected, byte[] actual)153     protected static void assertArrayEquals(byte[] expected, byte[] actual) {
154         assertTrue(Arrays.equals(expected, actual));
155     }
156 
assertArrayNotSame(byte[] expected, byte[] actual)157     protected static void assertArrayNotSame(byte[] expected, byte[] actual) {
158         assertFalse(Arrays.equals(expected, actual));
159     }
160 }
161 
162