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