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