1/*
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
17package com.android.server;
18
19import static org.mockito.Mockito.mock;
20
21import android.app.IActivityManager;
22import android.content.Context;
23import android.os.Handler;
24import android.os.Process;
25import android.os.RemoteException;
26import android.os.storage.IStorageManager;
27import android.security.KeyStore;
28import android.security.keystore.KeyPermanentlyInvalidatedException;
29
30import com.android.internal.widget.LockPatternUtils;
31
32import java.io.FileNotFoundException;
33
34public class LockSettingsServiceTestable extends LockSettingsService {
35
36    private static class MockInjector extends LockSettingsService.Injector {
37
38        private LockSettingsStorage mLockSettingsStorage;
39        private KeyStore mKeyStore;
40        private IActivityManager mActivityManager;
41        private LockPatternUtils mLockPatternUtils;
42        private IStorageManager mStorageManager;
43        private MockGateKeeperService mGatekeeper;
44
45        public MockInjector(Context context, LockSettingsStorage storage, KeyStore keyStore,
46                IActivityManager activityManager, LockPatternUtils lockPatternUtils,
47                IStorageManager storageManager, MockGateKeeperService gatekeeper) {
48            super(context);
49            mLockSettingsStorage = storage;
50            mKeyStore = keyStore;
51            mActivityManager = activityManager;
52            mLockPatternUtils = lockPatternUtils;
53            mStorageManager = storageManager;
54            mGatekeeper = gatekeeper;
55        }
56
57        @Override
58        public Handler getHandler() {
59            return mock(Handler.class);
60        }
61
62        @Override
63        public LockSettingsStorage getStorage() {
64            return mLockSettingsStorage;
65        }
66
67        @Override
68        public LockSettingsStrongAuth getStrongAuth() {
69            return mock(LockSettingsStrongAuth.class);
70        }
71
72        @Override
73        public SynchronizedStrongAuthTracker getStrongAuthTracker() {
74            return mock(SynchronizedStrongAuthTracker.class);
75        }
76
77        @Override
78        public IActivityManager getActivityManager() {
79            return mActivityManager;
80        }
81
82        @Override
83        public LockPatternUtils getLockPatternUtils() {
84            return mLockPatternUtils;
85        }
86
87        @Override
88        public KeyStore getKeyStore() {
89            return mKeyStore;
90        }
91
92        @Override
93        public IStorageManager getStorageManager() {
94            return mStorageManager;
95        }
96
97        @Override
98        public SyntheticPasswordManager getSyntheticPasswordManager(LockSettingsStorage storage) {
99            return new MockSyntheticPasswordManager(storage, mGatekeeper);
100        }
101
102        @Override
103        public int binderGetCallingUid() {
104            return Process.SYSTEM_UID;
105        }
106
107
108    }
109
110    protected LockSettingsServiceTestable(Context context, LockPatternUtils lockPatternUtils,
111            LockSettingsStorage storage, MockGateKeeperService gatekeeper, KeyStore keystore,
112            IStorageManager storageManager, IActivityManager mActivityManager) {
113        super(new MockInjector(context, storage, keystore, mActivityManager, lockPatternUtils,
114                storageManager, gatekeeper));
115        mGateKeeperService = gatekeeper;
116    }
117
118    @Override
119    protected void tieProfileLockToParent(int userId, String password) {
120        mStorage.writeChildProfileLock(userId, password.getBytes());
121    }
122
123    @Override
124    protected String getDecryptedPasswordForTiedProfile(int userId) throws FileNotFoundException, KeyPermanentlyInvalidatedException {
125        byte[] storedData = mStorage.readChildProfileLock(userId);
126        if (storedData == null) {
127            throw new FileNotFoundException("Child profile lock file not found");
128        }
129        try {
130            if (mGateKeeperService.getSecureUserId(userId) == 0) {
131                throw new KeyPermanentlyInvalidatedException();
132            }
133        } catch (RemoteException e) {
134            // shouldn't happen.
135        }
136        return new String(storedData);
137    }
138}
139