• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.os.IProgressListener;
20 import android.os.RemoteException;
21 import android.util.ArrayMap;
22 
23 
24 import junit.framework.AssertionFailedError;
25 
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 
29 public class FakeStorageManager {
30 
31     private ArrayMap<Integer, ArrayList<byte[]>> mAuth = new ArrayMap<>();
32     private boolean mIgnoreBadUnlock;
33 
addUserKeyAuth(int userId, int serialNumber, byte[] secret)34     public void addUserKeyAuth(int userId, int serialNumber, byte[] secret) {
35         getUserAuth(userId).add(secret);
36     }
37 
clearUserKeyAuth(int userId, int serialNumber, byte[] secret)38     public void clearUserKeyAuth(int userId, int serialNumber, byte[] secret) {
39         ArrayList<byte[]> auths = getUserAuth(userId);
40         if (secret == null) {
41             return;
42         }
43         auths.remove(secret);
44         auths.add(null);
45     }
46 
fixateNewestUserKeyAuth(int userId)47     public void fixateNewestUserKeyAuth(int userId) {
48         ArrayList<byte[]> auths = mAuth.get(userId);
49         byte[] latest = auths.get(auths.size() - 1);
50         auths.clear();
51         auths.add(latest);
52     }
53 
getUserAuth(int userId)54     private ArrayList<byte[]> getUserAuth(int userId) {
55         if (!mAuth.containsKey(userId)) {
56             ArrayList<byte[]> auths = new ArrayList<>();
57             auths.add(null);
58             mAuth.put(userId, auths);
59         }
60         return mAuth.get(userId);
61     }
62 
getUserUnlockToken(int userId)63     public byte[] getUserUnlockToken(int userId) {
64         ArrayList<byte[]> auths = getUserAuth(userId);
65         if (auths.size() != 1) {
66             throw new AssertionFailedError("More than one secret exists");
67         }
68         return auths.get(0);
69     }
70 
unlockUser(int userId, byte[] secret, IProgressListener listener)71     public void unlockUser(int userId, byte[] secret, IProgressListener listener)
72             throws RemoteException {
73         listener.onStarted(userId, null);
74         listener.onFinished(userId, null);
75         ArrayList<byte[]> auths = getUserAuth(userId);
76         if (auths.size() > 1) {
77             throw new AssertionFailedError("More than one secret exists");
78         }
79         byte[] auth = auths.get(0);
80         if (!Arrays.equals(secret, auth)) {
81             if (!mIgnoreBadUnlock) {
82                 throw new AssertionFailedError("Invalid secret to unlock user " + userId);
83             }
84         }
85     }
86 
setIgnoreBadUnlock(boolean ignore)87     public void setIgnoreBadUnlock(boolean ignore) {
88         mIgnoreBadUnlock = ignore;
89     }
90 }
91