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.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 
LockSettingsStorageTestable(Context context, File storageDir)39     public LockSettingsStorageTestable(Context context, File storageDir) {
40         super(context);
41         mStorageDir = storageDir;
42         mPersistentDataBlockManager = mock(PersistentDataBlockManagerInternal.class);
43         doAnswer(new Answer<Void>() {
44             @Override
45             public Void answer(InvocationOnMock invocation) throws Throwable {
46                 byte[] handle = (byte[]) invocation.getArguments()[0];
47                 if (handle != null) {
48                     mPersistentData = Arrays.copyOf(handle, handle.length);
49                 } else {
50                     mPersistentData = null;
51                 }
52                 return null;
53             }
54         }).when(mPersistentDataBlockManager).setFrpCredentialHandle(any());
55         // For some reasons, simply mocking getFrpCredentialHandle() with
56         // when(mPersistentDataBlockManager.getFrpCredentialHandle()).thenReturn(mPersistentData)
57         // does not work, I had to use the long-winded way below.
58         doAnswer(new Answer<byte[]>() {
59             @Override
60             public byte[] answer(InvocationOnMock invocation) throws Throwable {
61                 return mPersistentData;
62             }
63         }).when(mPersistentDataBlockManager).getFrpCredentialHandle();
64     }
65 
66     @Override
getChildProfileLockFile(int userId)67     File getChildProfileLockFile(int userId) {
68         return remapToStorageDir(super.getChildProfileLockFile(userId));
69     }
70 
71     @Override
getRebootEscrowServerBlobFile()72     File getRebootEscrowServerBlobFile() {
73         return remapToStorageDir(super.getRebootEscrowServerBlobFile());
74     }
75 
76     @Override
getRebootEscrowFile(int userId)77     File getRebootEscrowFile(int userId) {
78         return remapToStorageDir(super.getRebootEscrowFile(userId));
79     }
80 
81     @Override
getSyntheticPasswordDirectoryForUser(int userId)82     protected File getSyntheticPasswordDirectoryForUser(int userId) {
83         return remapToStorageDir(super.getSyntheticPasswordDirectoryForUser(userId));
84     }
85 
86     @Override
getRepairModePersistentDataFile()87     File getRepairModePersistentDataFile() {
88         return remapToStorageDir(super.getRepairModePersistentDataFile());
89     }
90 
91     @Override
getPersistentDataBlockManager()92     PersistentDataBlockManagerInternal getPersistentDataBlockManager() {
93         return mPersistentDataBlockManager;
94     }
95     @Override
isAutoPinConfirmSettingEnabled(int userId)96     public boolean isAutoPinConfirmSettingEnabled(int userId) {
97         return true;
98     }
remapToStorageDir(File origPath)99     private File remapToStorageDir(File origPath) {
100         File mappedPath = new File(mStorageDir, origPath.toString());
101         mappedPath.getParentFile().mkdirs();
102         return mappedPath;
103     }
104 }
105