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;
18 
19 import android.content.pm.IPackageMoveObserver;
20 import android.os.IBinder;
21 import android.os.IProgressListener;
22 import android.os.ParcelFileDescriptor;
23 import android.os.RemoteException;
24 import android.os.storage.DiskInfo;
25 import android.os.storage.IObbActionListener;
26 import android.os.storage.IStorageEventListener;
27 import android.os.storage.IStorageManager;
28 import android.os.storage.IStorageShutdownObserver;
29 import android.os.storage.StorageVolume;
30 import android.os.storage.VolumeInfo;
31 import android.os.storage.VolumeRecord;
32 import android.util.ArrayMap;
33 import android.util.Pair;
34 
35 import com.android.internal.os.AppFuseMount;
36 
37 import junit.framework.AssertionFailedError;
38 
39 import java.util.ArrayList;
40 import java.util.Arrays;
41 
42 public class MockStorageManager implements IStorageManager {
43 
44     private ArrayMap<Integer, ArrayList<Pair<byte[], byte[]>>> mAuth = new ArrayMap<>();
45     private boolean mIgnoreBadUnlock;
46 
47     @Override
addUserKeyAuth(int userId, int serialNumber, byte[] token, byte[] secret)48     public void addUserKeyAuth(int userId, int serialNumber, byte[] token, byte[] secret)
49             throws RemoteException {
50         getUserAuth(userId).add(new Pair<>(token, secret));
51     }
52 
53     @Override
fixateNewestUserKeyAuth(int userId)54     public void fixateNewestUserKeyAuth(int userId) throws RemoteException {
55         ArrayList<Pair<byte[], byte[]>> auths = mAuth.get(userId);
56         Pair<byte[], byte[]> latest = auths.get(auths.size() - 1);
57         auths.clear();
58         auths.add(latest);
59     }
60 
getUserAuth(int userId)61     private ArrayList<Pair<byte[], byte[]>> getUserAuth(int userId) {
62         if (!mAuth.containsKey(userId)) {
63             ArrayList<Pair<byte[], byte[]>> auths = new ArrayList<Pair<byte[], byte[]>>();
64             auths.add(new Pair(null, null));
65             mAuth.put(userId,  auths);
66         }
67         return mAuth.get(userId);
68     }
69 
getUserUnlockToken(int userId)70     public byte[] getUserUnlockToken(int userId) {
71         ArrayList<Pair<byte[], byte[]>> auths = getUserAuth(userId);
72         if (auths.size() != 1) {
73             throw new AssertionFailedError("More than one secret exists");
74         }
75         return auths.get(0).second;
76     }
77 
unlockUser(int userId, byte[] secret, IProgressListener listener)78     public void unlockUser(int userId, byte[] secret, IProgressListener listener)
79             throws RemoteException {
80         listener.onStarted(userId, null);
81         listener.onFinished(userId, null);
82         ArrayList<Pair<byte[], byte[]>> auths = getUserAuth(userId);
83         if (secret != null) {
84             if (auths.size() > 1) {
85                 throw new AssertionFailedError("More than one secret exists");
86             }
87             Pair<byte[], byte[]> auth = auths.get(0);
88             if ((!mIgnoreBadUnlock) && auth.second != null && !Arrays.equals(secret, auth.second)) {
89                 throw new AssertionFailedError("Invalid secret to unlock user");
90             }
91         } else {
92             if (auths != null && auths.size() > 0) {
93                 throw new AssertionFailedError("Cannot unlock encrypted user with empty token");
94             }
95         }
96     }
97 
setIgnoreBadUnlock(boolean ignore)98     public void setIgnoreBadUnlock(boolean ignore) {
99         mIgnoreBadUnlock = ignore;
100     }
101 
102     @Override
asBinder()103     public IBinder asBinder() {
104         throw new UnsupportedOperationException();
105     }
106 
107     @Override
registerListener(IStorageEventListener listener)108     public void registerListener(IStorageEventListener listener) throws RemoteException {
109         throw new UnsupportedOperationException();
110     }
111 
112     @Override
unregisterListener(IStorageEventListener listener)113     public void unregisterListener(IStorageEventListener listener) throws RemoteException {
114         throw new UnsupportedOperationException();
115     }
116 
117     @Override
isUsbMassStorageConnected()118     public boolean isUsbMassStorageConnected() throws RemoteException {
119         throw new UnsupportedOperationException();
120     }
121 
122     @Override
setUsbMassStorageEnabled(boolean enable)123     public void setUsbMassStorageEnabled(boolean enable) throws RemoteException {
124         throw new UnsupportedOperationException();
125     }
126 
127     @Override
isUsbMassStorageEnabled()128     public boolean isUsbMassStorageEnabled() throws RemoteException {
129         throw new UnsupportedOperationException();
130     }
131 
132     @Override
mountVolume(String mountPoint)133     public int mountVolume(String mountPoint) throws RemoteException {
134         throw new UnsupportedOperationException();
135     }
136 
137     @Override
unmountVolume(String mountPoint, boolean force, boolean removeEncryption)138     public void unmountVolume(String mountPoint, boolean force, boolean removeEncryption)
139             throws RemoteException {
140         throw new UnsupportedOperationException();
141 
142     }
143 
144     @Override
formatVolume(String mountPoint)145     public int formatVolume(String mountPoint) throws RemoteException {
146         throw new UnsupportedOperationException();
147     }
148 
149     @Override
getStorageUsers(String path)150     public int[] getStorageUsers(String path) throws RemoteException {
151         throw new UnsupportedOperationException();
152     }
153 
154     @Override
getVolumeState(String mountPoint)155     public String getVolumeState(String mountPoint) throws RemoteException {
156         throw new UnsupportedOperationException();
157     }
158 
159     @Override
createSecureContainer(String id, int sizeMb, String fstype, String key, int ownerUid, boolean external)160     public int createSecureContainer(String id, int sizeMb, String fstype, String key, int ownerUid,
161             boolean external) throws RemoteException {
162         throw new UnsupportedOperationException();
163     }
164 
165     @Override
finalizeSecureContainer(String id)166     public int finalizeSecureContainer(String id) throws RemoteException {
167         throw new UnsupportedOperationException();
168     }
169 
170     @Override
destroySecureContainer(String id, boolean force)171     public int destroySecureContainer(String id, boolean force) throws RemoteException {
172         throw new UnsupportedOperationException();
173     }
174 
175     @Override
mountSecureContainer(String id, String key, int ownerUid, boolean readOnly)176     public int mountSecureContainer(String id, String key, int ownerUid, boolean readOnly)
177             throws RemoteException {
178         throw new UnsupportedOperationException();
179     }
180 
181     @Override
unmountSecureContainer(String id, boolean force)182     public int unmountSecureContainer(String id, boolean force) throws RemoteException {
183         throw new UnsupportedOperationException();
184     }
185 
186     @Override
isSecureContainerMounted(String id)187     public boolean isSecureContainerMounted(String id) throws RemoteException {
188         throw new UnsupportedOperationException();
189     }
190 
191     @Override
renameSecureContainer(String oldId, String newId)192     public int renameSecureContainer(String oldId, String newId) throws RemoteException {
193         throw new UnsupportedOperationException();
194     }
195 
196     @Override
getSecureContainerPath(String id)197     public String getSecureContainerPath(String id) throws RemoteException {
198         throw new UnsupportedOperationException();
199     }
200 
201     @Override
getSecureContainerList()202     public String[] getSecureContainerList() throws RemoteException {
203         throw new UnsupportedOperationException();
204     }
205 
206     @Override
shutdown(IStorageShutdownObserver observer)207     public void shutdown(IStorageShutdownObserver observer) throws RemoteException {
208         throw new UnsupportedOperationException();
209     }
210 
211     @Override
finishMediaUpdate()212     public void finishMediaUpdate() throws RemoteException {
213         throw new UnsupportedOperationException();
214     }
215 
216     @Override
mountObb(String rawPath, String canonicalPath, String key, IObbActionListener token, int nonce)217     public void mountObb(String rawPath, String canonicalPath, String key, IObbActionListener token,
218             int nonce) throws RemoteException {
219         throw new UnsupportedOperationException();
220     }
221 
222     @Override
unmountObb(String rawPath, boolean force, IObbActionListener token, int nonce)223     public void unmountObb(String rawPath, boolean force, IObbActionListener token, int nonce)
224             throws RemoteException {
225         throw new UnsupportedOperationException();
226     }
227 
228     @Override
isObbMounted(String rawPath)229     public boolean isObbMounted(String rawPath) throws RemoteException {
230         throw new UnsupportedOperationException();
231     }
232 
233     @Override
getMountedObbPath(String rawPath)234     public String getMountedObbPath(String rawPath) throws RemoteException {
235         throw new UnsupportedOperationException();
236     }
237 
238     @Override
isExternalStorageEmulated()239     public boolean isExternalStorageEmulated() throws RemoteException {
240         throw new UnsupportedOperationException();
241     }
242 
243     @Override
decryptStorage(String password)244     public int decryptStorage(String password) throws RemoteException {
245         throw new UnsupportedOperationException();
246     }
247 
248     @Override
encryptStorage(int type, String password)249     public int encryptStorage(int type, String password) throws RemoteException {
250         throw new UnsupportedOperationException();
251     }
252 
253     @Override
changeEncryptionPassword(int type, String password)254     public int changeEncryptionPassword(int type, String password) throws RemoteException {
255         throw new UnsupportedOperationException();
256     }
257 
258     @Override
getVolumeList(int uid, String packageName, int flags)259     public StorageVolume[] getVolumeList(int uid, String packageName, int flags)
260             throws RemoteException {
261         throw new UnsupportedOperationException();
262     }
263 
264     @Override
getSecureContainerFilesystemPath(String cid)265     public String getSecureContainerFilesystemPath(String cid) throws RemoteException {
266         throw new UnsupportedOperationException();
267     }
268 
269     @Override
getEncryptionState()270     public int getEncryptionState() throws RemoteException {
271         throw new UnsupportedOperationException();
272     }
273 
274     @Override
verifyEncryptionPassword(String password)275     public int verifyEncryptionPassword(String password) throws RemoteException {
276         throw new UnsupportedOperationException();
277     }
278 
279     @Override
fixPermissionsSecureContainer(String id, int gid, String filename)280     public int fixPermissionsSecureContainer(String id, int gid, String filename)
281             throws RemoteException {
282         throw new UnsupportedOperationException();
283     }
284 
285     @Override
mkdirs(String callingPkg, String path)286     public int mkdirs(String callingPkg, String path) throws RemoteException {
287         throw new UnsupportedOperationException();
288     }
289 
290     @Override
getPasswordType()291     public int getPasswordType() throws RemoteException {
292         throw new UnsupportedOperationException();
293     }
294 
295     @Override
getPassword()296     public String getPassword() throws RemoteException {
297         throw new UnsupportedOperationException();
298     }
299 
300     @Override
clearPassword()301     public void clearPassword() throws RemoteException {
302         throw new UnsupportedOperationException();
303 
304     }
305 
306     @Override
setField(String field, String contents)307     public void setField(String field, String contents) throws RemoteException {
308         throw new UnsupportedOperationException();
309 
310     }
311 
312     @Override
getField(String field)313     public String getField(String field) throws RemoteException {
314         throw new UnsupportedOperationException();
315     }
316 
317     @Override
resizeSecureContainer(String id, int sizeMb, String key)318     public int resizeSecureContainer(String id, int sizeMb, String key) throws RemoteException {
319         throw new UnsupportedOperationException();
320     }
321 
322     @Override
lastMaintenance()323     public long lastMaintenance() throws RemoteException {
324         throw new UnsupportedOperationException();
325     }
326 
327     @Override
runMaintenance()328     public void runMaintenance() throws RemoteException {
329         throw new UnsupportedOperationException();
330     }
331 
332     @Override
waitForAsecScan()333     public void waitForAsecScan() throws RemoteException {
334         throw new UnsupportedOperationException();
335     }
336 
337     @Override
getDisks()338     public DiskInfo[] getDisks() throws RemoteException {
339         throw new UnsupportedOperationException();
340     }
341 
342     @Override
getVolumes(int flags)343     public VolumeInfo[] getVolumes(int flags) throws RemoteException {
344         throw new UnsupportedOperationException();
345     }
346 
347     @Override
getVolumeRecords(int flags)348     public VolumeRecord[] getVolumeRecords(int flags) throws RemoteException {
349         throw new UnsupportedOperationException();
350     }
351 
352     @Override
mount(String volId)353     public void mount(String volId) throws RemoteException {
354         throw new UnsupportedOperationException();
355     }
356 
357     @Override
unmount(String volId)358     public void unmount(String volId) throws RemoteException {
359         throw new UnsupportedOperationException();
360     }
361 
362     @Override
format(String volId)363     public void format(String volId) throws RemoteException {
364         throw new UnsupportedOperationException();
365     }
366 
367     @Override
partitionPublic(String diskId)368     public void partitionPublic(String diskId) throws RemoteException {
369         throw new UnsupportedOperationException();
370     }
371 
372     @Override
partitionPrivate(String diskId)373     public void partitionPrivate(String diskId) throws RemoteException {
374         throw new UnsupportedOperationException();
375     }
376 
377     @Override
partitionMixed(String diskId, int ratio)378     public void partitionMixed(String diskId, int ratio) throws RemoteException {
379         throw new UnsupportedOperationException();
380     }
381 
382     @Override
setVolumeNickname(String fsUuid, String nickname)383     public void setVolumeNickname(String fsUuid, String nickname) throws RemoteException {
384         throw new UnsupportedOperationException();
385     }
386 
387     @Override
setVolumeUserFlags(String fsUuid, int flags, int mask)388     public void setVolumeUserFlags(String fsUuid, int flags, int mask) throws RemoteException {
389         throw new UnsupportedOperationException();
390     }
391 
392     @Override
forgetVolume(String fsUuid)393     public void forgetVolume(String fsUuid) throws RemoteException {
394         throw new UnsupportedOperationException();
395     }
396 
397     @Override
forgetAllVolumes()398     public void forgetAllVolumes() throws RemoteException {
399         throw new UnsupportedOperationException();
400     }
401 
402     @Override
getPrimaryStorageUuid()403     public String getPrimaryStorageUuid() throws RemoteException {
404         throw new UnsupportedOperationException();
405     }
406 
407     @Override
setPrimaryStorageUuid(String volumeUuid, IPackageMoveObserver callback)408     public void setPrimaryStorageUuid(String volumeUuid, IPackageMoveObserver callback)
409             throws RemoteException {
410         throw new UnsupportedOperationException();
411     }
412 
413     @Override
benchmark(String volId)414     public long benchmark(String volId) throws RemoteException {
415         throw new UnsupportedOperationException();
416     }
417 
418     @Override
setDebugFlags(int flags, int mask)419     public void setDebugFlags(int flags, int mask) throws RemoteException {
420         throw new UnsupportedOperationException();
421     }
422 
423     @Override
createUserKey(int userId, int serialNumber, boolean ephemeral)424     public void createUserKey(int userId, int serialNumber, boolean ephemeral)
425             throws RemoteException {
426         throw new UnsupportedOperationException();
427     }
428 
429     @Override
destroyUserKey(int userId)430     public void destroyUserKey(int userId) throws RemoteException {
431         throw new UnsupportedOperationException();
432     }
433 
434     @Override
unlockUserKey(int userId, int serialNumber, byte[] token, byte[] secret)435     public void unlockUserKey(int userId, int serialNumber, byte[] token, byte[] secret)
436             throws RemoteException {
437         throw new UnsupportedOperationException();
438     }
439 
440     @Override
lockUserKey(int userId)441     public void lockUserKey(int userId) throws RemoteException {
442         throw new UnsupportedOperationException();
443     }
444 
445     @Override
isUserKeyUnlocked(int userId)446     public boolean isUserKeyUnlocked(int userId) throws RemoteException {
447         throw new UnsupportedOperationException();
448     }
449 
450     @Override
prepareUserStorage(String volumeUuid, int userId, int serialNumber, int flags)451     public void prepareUserStorage(String volumeUuid, int userId, int serialNumber, int flags)
452             throws RemoteException {
453         throw new UnsupportedOperationException();
454     }
455 
456     @Override
destroyUserStorage(String volumeUuid, int userId, int flags)457     public void destroyUserStorage(String volumeUuid, int userId, int flags)
458             throws RemoteException {
459         throw new UnsupportedOperationException();
460     }
461 
462     @Override
isConvertibleToFBE()463     public boolean isConvertibleToFBE() throws RemoteException {
464         throw new UnsupportedOperationException();
465     }
466 
467     @Override
fstrim(int flags)468     public void fstrim(int flags) throws RemoteException {
469         throw new UnsupportedOperationException();
470     }
471 
472     @Override
mountProxyFileDescriptorBridge()473     public AppFuseMount mountProxyFileDescriptorBridge() throws RemoteException {
474         throw new UnsupportedOperationException();
475     }
476 
477     @Override
openProxyFileDescriptor(int mountPointId, int fileId, int mode)478     public ParcelFileDescriptor openProxyFileDescriptor(int mountPointId, int fileId, int mode)
479             throws RemoteException {
480         throw new UnsupportedOperationException();
481     }
482 
483     @Override
getCacheQuotaBytes(String volumeUuid, int uid)484     public long getCacheQuotaBytes(String volumeUuid, int uid) throws RemoteException {
485         throw new UnsupportedOperationException();
486     }
487 
488     @Override
getCacheSizeBytes(String volumeUuid, int uid)489     public long getCacheSizeBytes(String volumeUuid, int uid) throws RemoteException {
490         throw new UnsupportedOperationException();
491     }
492 
493     @Override
getAllocatableBytes(String path, int flags)494     public long getAllocatableBytes(String path, int flags) {
495         throw new UnsupportedOperationException();
496     }
497 
498     @Override
allocateBytes(String path, long bytes, int flags)499     public void allocateBytes(String path, long bytes, int flags) {
500         throw new UnsupportedOperationException();
501     }
502 
503     @Override
secdiscard(String path)504     public void secdiscard(String path) throws RemoteException {
505         throw new UnsupportedOperationException();
506     }
507 
508 }
509