LockSettingsServiceTestable.java revision 0cbc19e4a66f7db51596b57ca91afc6f5b27f3b4
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.storage.IStorageManager;
25import android.security.KeyStore;
26import android.service.gatekeeper.IGateKeeperService;
27
28import com.android.internal.widget.LockPatternUtils;
29
30import java.io.FileNotFoundException;
31
32public class LockSettingsServiceTestable extends LockSettingsService {
33
34    private static class MockInjector extends LockSettingsService.Injector {
35
36        private LockSettingsStorage mLockSettingsStorage;
37        private KeyStore mKeyStore;
38        private IActivityManager mActivityManager;
39        private LockPatternUtils mLockPatternUtils;
40        private IStorageManager mStorageManager;
41
42        public MockInjector(Context context, LockSettingsStorage storage, KeyStore keyStore,
43                IActivityManager activityManager, LockPatternUtils lockPatternUtils,
44                IStorageManager storageManager) {
45            super(context);
46            mLockSettingsStorage = storage;
47            mKeyStore = keyStore;
48            mActivityManager = activityManager;
49            mLockPatternUtils = lockPatternUtils;
50            mStorageManager = storageManager;
51        }
52
53        @Override
54        public Handler getHandler() {
55            return mock(Handler.class);
56        }
57
58        @Override
59        public LockSettingsStorage getStorage() {
60            return mLockSettingsStorage;
61        }
62
63        @Override
64        public LockSettingsStrongAuth getStrongAuth() {
65            return mock(LockSettingsStrongAuth.class);
66        }
67
68        @Override
69        public SynchronizedStrongAuthTracker getStrongAuthTracker() {
70            return mock(SynchronizedStrongAuthTracker.class);
71        }
72
73        @Override
74        public IActivityManager getActivityManager() {
75            return mActivityManager;
76        }
77
78        @Override
79        public LockPatternUtils getLockPatternUtils() {
80            return mLockPatternUtils;
81        }
82
83        @Override
84        public KeyStore getKeyStore() {
85            return mKeyStore;
86        }
87
88        @Override
89        public IStorageManager getStorageManager() {
90            return mStorageManager;
91        }
92    }
93
94    protected LockSettingsServiceTestable(Context context, LockPatternUtils lockPatternUtils,
95            LockSettingsStorage storage, IGateKeeperService gatekeeper, KeyStore keystore,
96            IStorageManager storageManager, IActivityManager mActivityManager) {
97        super(new MockInjector(context, storage, keystore, mActivityManager, lockPatternUtils,
98                storageManager));
99        mGateKeeperService = gatekeeper;
100    }
101
102    @Override
103    protected void tieProfileLockToParent(int userId, String password) {
104        mStorage.writeChildProfileLock(userId, password.getBytes());
105    }
106
107    @Override
108    protected String getDecryptedPasswordForTiedProfile(int userId) throws FileNotFoundException {
109        byte[] storedData = mStorage.readChildProfileLock(userId);
110        if (storedData == null) {
111            throw new FileNotFoundException("Child profile lock file not found");
112        }
113        return new String(storedData);
114    }
115
116}
117