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