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