blob: 0916a335b51e21e2c5fdfcf2e5a3410ce07518ab [file] [log] [blame]
Rubin Xu0cbc19e2016-12-09 14:00:21 +00001/*
2 * Copyright (C) 2017 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
Andrew Scull507d11c2017-05-03 17:19:01 +010017package com.android.server.locksettings;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000018
19import static org.mockito.Mockito.mock;
20
21import android.app.IActivityManager;
22import android.content.Context;
23import android.os.Handler;
Rubin Xu7cf45092017-08-28 11:47:35 +010024import android.os.Looper;
Rubin Xu3bf722a2016-12-15 16:07:38 +000025import android.os.Process;
26import android.os.RemoteException;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000027import android.os.storage.IStorageManager;
28import android.security.KeyStore;
Rubin Xu3bf722a2016-12-15 16:07:38 +000029import android.security.keystore.KeyPermanentlyInvalidatedException;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000030
31import com.android.internal.widget.LockPatternUtils;
32
33import java.io.FileNotFoundException;
34
35public class LockSettingsServiceTestable extends LockSettingsService {
36
37 private static class MockInjector extends LockSettingsService.Injector {
38
39 private LockSettingsStorage mLockSettingsStorage;
40 private KeyStore mKeyStore;
41 private IActivityManager mActivityManager;
42 private LockPatternUtils mLockPatternUtils;
43 private IStorageManager mStorageManager;
Rubin Xu7b7424b2017-03-31 18:03:20 +010044 private SyntheticPasswordManager mSpManager;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000045
46 public MockInjector(Context context, LockSettingsStorage storage, KeyStore keyStore,
47 IActivityManager activityManager, LockPatternUtils lockPatternUtils,
Rubin Xu7b7424b2017-03-31 18:03:20 +010048 IStorageManager storageManager, SyntheticPasswordManager spManager) {
Rubin Xu0cbc19e2016-12-09 14:00:21 +000049 super(context);
50 mLockSettingsStorage = storage;
51 mKeyStore = keyStore;
52 mActivityManager = activityManager;
53 mLockPatternUtils = lockPatternUtils;
54 mStorageManager = storageManager;
Rubin Xu7b7424b2017-03-31 18:03:20 +010055 mSpManager = spManager;
Rubin Xu0cbc19e2016-12-09 14:00:21 +000056 }
57
58 @Override
59 public Handler getHandler() {
Rubin Xu7cf45092017-08-28 11:47:35 +010060 return new Handler(Looper.getMainLooper());
Rubin Xu0cbc19e2016-12-09 14:00:21 +000061 }
62
63 @Override
64 public LockSettingsStorage getStorage() {
65 return mLockSettingsStorage;
66 }
67
68 @Override
69 public LockSettingsStrongAuth getStrongAuth() {
70 return mock(LockSettingsStrongAuth.class);
71 }
72
73 @Override
74 public SynchronizedStrongAuthTracker getStrongAuthTracker() {
75 return mock(SynchronizedStrongAuthTracker.class);
76 }
77
78 @Override
79 public IActivityManager getActivityManager() {
80 return mActivityManager;
81 }
82
83 @Override
84 public LockPatternUtils getLockPatternUtils() {
85 return mLockPatternUtils;
86 }
87
88 @Override
89 public KeyStore getKeyStore() {
90 return mKeyStore;
91 }
92
93 @Override
94 public IStorageManager getStorageManager() {
95 return mStorageManager;
96 }
Rubin Xu3bf722a2016-12-15 16:07:38 +000097
98 @Override
99 public SyntheticPasswordManager getSyntheticPasswordManager(LockSettingsStorage storage) {
Rubin Xu7b7424b2017-03-31 18:03:20 +0100100 return mSpManager;
Rubin Xu3bf722a2016-12-15 16:07:38 +0000101 }
102
103 @Override
104 public int binderGetCallingUid() {
105 return Process.SYSTEM_UID;
106 }
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000107 }
108
109 protected LockSettingsServiceTestable(Context context, LockPatternUtils lockPatternUtils,
Rubin Xu16c823e2017-06-27 14:44:58 +0100110 LockSettingsStorage storage, FakeGateKeeperService gatekeeper, KeyStore keystore,
Rubin Xu7b7424b2017-03-31 18:03:20 +0100111 IStorageManager storageManager, IActivityManager mActivityManager,
112 SyntheticPasswordManager spManager) {
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000113 super(new MockInjector(context, storage, keystore, mActivityManager, lockPatternUtils,
Rubin Xu7b7424b2017-03-31 18:03:20 +0100114 storageManager, spManager));
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000115 mGateKeeperService = gatekeeper;
116 }
117
118 @Override
119 protected void tieProfileLockToParent(int userId, String password) {
120 mStorage.writeChildProfileLock(userId, password.getBytes());
121 }
122
123 @Override
Rubin Xu7b7424b2017-03-31 18:03:20 +0100124 protected String getDecryptedPasswordForTiedProfile(int userId) throws FileNotFoundException,
125 KeyPermanentlyInvalidatedException {
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000126 byte[] storedData = mStorage.readChildProfileLock(userId);
127 if (storedData == null) {
128 throw new FileNotFoundException("Child profile lock file not found");
129 }
Rubin Xu3bf722a2016-12-15 16:07:38 +0000130 try {
131 if (mGateKeeperService.getSecureUserId(userId) == 0) {
132 throw new KeyPermanentlyInvalidatedException();
133 }
134 } catch (RemoteException e) {
135 // shouldn't happen.
136 }
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000137 return new String(storedData);
138 }
Rubin Xu16c823e2017-06-27 14:44:58 +0100139
Rubin Xu0cbc19e2016-12-09 14:00:21 +0000140}