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.ArgumentMatchers.any;
20 import static org.mockito.Mockito.doAnswer;
21 import static org.mockito.Mockito.mock;
22 
23 import android.content.Context;
24 
25 import com.android.server.pdb.PersistentDataBlockManagerInternal;
26 
27 import org.mockito.invocation.InvocationOnMock;
28 import org.mockito.stubbing.Answer;
29 
30 import java.io.File;
31 import java.util.Arrays;
32 
33 public class LockSettingsStorageTestable extends LockSettingsStorage {
34 
35     public final File mStorageDir;
36     public PersistentDataBlockManagerInternal mPersistentDataBlockManager;
37     private byte[] mPersistentData;
38     private boolean mIsFactoryResetProtectionActive = false;
39 
LockSettingsStorageTestable(Context context, File storageDir)40     public LockSettingsStorageTestable(Context context, File storageDir) {
41         super(context);
42         mStorageDir = storageDir;
43         mPersistentDataBlockManager = mock(PersistentDataBlockManagerInternal.class);
44         doAnswer(new Answer<Void>() {
45             @Override
46             public Void answer(InvocationOnMock invocation) throws Throwable {
47                 byte[] handle = (byte[]) invocation.getArguments()[0];
48                 if (handle != null) {
49                     mPersistentData = Arrays.copyOf(handle, handle.length);
50                 } else {
51                     mPersistentData = null;
52                 }
53                 return null;
54             }
55         }).when(mPersistentDataBlockManager).setFrpCredentialHandle(any());
56         // For some reasons, simply mocking getFrpCredentialHandle() with
57         // when(mPersistentDataBlockManager.getFrpCredentialHandle()).thenReturn(mPersistentData)
58         // does not work, I had to use the long-winded way below.
59         doAnswer(new Answer<byte[]>() {
60             @Override
61             public byte[] answer(InvocationOnMock invocation) throws Throwable {
62                 return mPersistentData;
63             }
64         }).when(mPersistentDataBlockManager).getFrpCredentialHandle();
65     }
66 
setTestFactoryResetProtectionState(boolean active)67     void setTestFactoryResetProtectionState(boolean active) {
68         mIsFactoryResetProtectionActive = active;
69     }
70 
71     @Override
getChildProfileLockFile(int userId)72     File getChildProfileLockFile(int userId) {
73         return remapToStorageDir(super.getChildProfileLockFile(userId));
74     }
75 
76     @Override
getRebootEscrowServerBlobFile()77     File getRebootEscrowServerBlobFile() {
78         return remapToStorageDir(super.getRebootEscrowServerBlobFile());
79     }
80 
81     @Override
getRebootEscrowFile(int userId)82     File getRebootEscrowFile(int userId) {
83         return remapToStorageDir(super.getRebootEscrowFile(userId));
84     }
85 
86     @Override
getSyntheticPasswordDirectoryForUser(int userId)87     protected File getSyntheticPasswordDirectoryForUser(int userId) {
88         return remapToStorageDir(super.getSyntheticPasswordDirectoryForUser(userId));
89     }
90 
91     @Override
getRepairModePersistentDataFile()92     File getRepairModePersistentDataFile() {
93         return remapToStorageDir(super.getRepairModePersistentDataFile());
94     }
95 
96     @Override
getPersistentDataBlockManager()97     PersistentDataBlockManagerInternal getPersistentDataBlockManager() {
98         return mPersistentDataBlockManager;
99     }
100     @Override
isAutoPinConfirmSettingEnabled(int userId)101     public boolean isAutoPinConfirmSettingEnabled(int userId) {
102         return true;
103     }
remapToStorageDir(File origPath)104     private File remapToStorageDir(File origPath) {
105         File mappedPath = new File(mStorageDir, origPath.toString());
106         mappedPath.getParentFile().mkdirs();
107         return mappedPath;
108     }
109 
110     @Override
isFactoryResetProtectionActive()111     public boolean isFactoryResetProtectionActive() {
112         return mIsFactoryResetProtectionActive;
113     }
114 }
115