FileDocCategorySizeDatePackage
AsecTests.javaAPI DocAndroid 5.1 API26152Thu Mar 12 22:22:12 GMT 2015android.os.storage

AsecTests

public class AsecTests extends android.test.AndroidTestCase

Fields Summary
private static final String
SECURE_CONTAINER_PREFIX
private static final boolean
localLOGV
public static final String
TAG
private static final String
FS_FAT
private static final String
FS_EXT4
public final long
MAX_WAIT_TIME
public final long
WAIT_TIME_INCR
Constructors Summary
Methods Summary
private voidcleanupContainers()

        IMountService ms = getMs();
        String[] containers = ms.getSecureContainerList();

        for (int i = 0; i < containers.length; i++) {
            if (containers[i].startsWith(SECURE_CONTAINER_PREFIX)) {
                if (localLOGV)
                    Log.i(TAG, "Cleaning: " + containers[i]);
                ms.destroySecureContainer(containers[i], true);
            }
        }
    
private booleancontainerExists(java.lang.String localId)

        IMountService ms = getMs();
        String[] containers = ms.getSecureContainerList();
        String fullId = SECURE_CONTAINER_PREFIX + localId;

        for (int i = 0; i < containers.length; i++) {
            if (containers[i].equals(fullId)) {
                return true;
            }
        }
        return false;
    
private intcreateContainer(java.lang.String localId, int size, java.lang.String key, java.lang.String filesystem, boolean isExternal)

        assertTrue("Media should be mounted", isMediaMounted());
        String fullId = SECURE_CONTAINER_PREFIX + localId;

        IMountService ms = getMs();
        return ms.createSecureContainer(fullId, size, filesystem, key, android.os.Process.myUid(),
                isExternal);
    
private intdestroyContainer(java.lang.String localId, boolean force)

        assertTrue("Media should be mounted", isMediaMounted());
        String fullId = SECURE_CONTAINER_PREFIX + localId;

        IMountService ms = getMs();
        return ms.destroySecureContainer(fullId, force);
    
booleangetMediaState()


        
        String mPath = Environment.getExternalStorageDirectory().toString();
        String state = getMs().getVolumeState(mPath);
        return Environment.MEDIA_MOUNTED.equals(state);
    
private IMountServicegetMs()

        IBinder service = ServiceManager.getService("mount");
        if (service != null) {
            return IMountService.Stub.asInterface(service);
        } else {
            Log.e(TAG, "Can't get mount service");
        }
        return null;
    
voidinvokeShutdown()

        IMountService ms = getMs();
        ShutdownObserver observer = new ShutdownObserver();
        synchronized (observer) {
            ms.shutdown(observer);
        }
    
private booleanisContainerMounted(java.lang.String localId)

        assertTrue("Media should be mounted", isMediaMounted());
        String fullId = SECURE_CONTAINER_PREFIX + localId;

        IMountService ms = getMs();
        return ms.isSecureContainerMounted(fullId);
    
private booleanisMediaMounted()

        String mPath = Environment.getExternalStorageDirectory().toString();
        String state = getMs().getVolumeState(mPath);
        return Environment.MEDIA_MOUNTED.equals(state);
    
private intmountContainer(java.lang.String localId, java.lang.String key)

        assertTrue("Media should be mounted", isMediaMounted());
        String fullId = SECURE_CONTAINER_PREFIX + localId;

        IMountService ms = getMs();
        return ms.mountSecureContainer(fullId, key, android.os.Process.myUid(), true);
    
booleanmountMedia()

        if (Environment.isExternalStorageEmulated()) {
            return true;
        }

        if (getMediaState()) {
            return true;
        }

        String mPath = Environment.getExternalStorageDirectory().toString();
        int ret = getMs().mountVolume(mPath);
        return ret == StorageResultCode.OperationSucceeded;
    
private intrenameContainer(java.lang.String localId1, java.lang.String localId2)

        assertTrue("Media should be mounted", isMediaMounted());
        String fullId1 = SECURE_CONTAINER_PREFIX + localId1;
        String fullId2 = SECURE_CONTAINER_PREFIX + localId2;

        IMountService ms = getMs();
        return ms.renameSecureContainer(fullId1, fullId2);
    
protected voidsetUp()


    
         
        super.setUp();
        if (localLOGV) Log.i(TAG, "Cleaning out old test containers");
        cleanupContainers();
    
protected voidtearDown()

        super.tearDown();
        if (localLOGV) Log.i(TAG, "Cleaning out old test containers");
        cleanupContainers();
    
public voidtestGetSecureContainerPath_NonExistPath_Failure()

        IMountService ms = getMs();
        assertNull("Getting the path for an invalid container should return null",
                ms.getSecureContainerPath("jparks.broke.it"));
    
public voidtestShutdown()

        if (Environment.isExternalStorageEmulated()) {
            return;
        }

        boolean oldStatus = getMediaState();
        try {
            // Mount media firsts
            if (!getMediaState()) {
                mountMedia();
            }
            invokeShutdown();
        } finally {
            // Restore old status
            boolean currStatus = getMediaState();
            if (oldStatus != currStatus) {
                if (oldStatus) {
                    // Mount media
                    mountMedia();
                } else {
                    unmountMedia();
                }
            }
        }
    
public voidtestShutdownMultiple()

        if (Environment.isExternalStorageEmulated()) {
            return;
        }

        boolean oldStatus = getMediaState();
        try {
            // Mount media firsts
            if (!getMediaState()) {
                mountMedia();
            }
            IMountService ms = getMs();
            ShutdownObserver observer = new ShutdownObserver();
            synchronized (observer) {
                ms.shutdown(observer);
                for (int i = 0; i < 4; i++) {
                    ms.shutdown(null);
                }
            }
        } finally {
            // Restore old status
            boolean currStatus = getMediaState();
            if (oldStatus != currStatus) {
                if (oldStatus) {
                    // Mount media
                    mountMedia();
                } else {
                    unmountMedia();
                }
            }
        }
    
public voidtestUnmount()

        boolean oldStatus = getMediaState();
        Log.i(TAG, "oldStatus="+oldStatus);
        try {
            // Mount media firsts
            if (!getMediaState()) {
                mountMedia();
            }
            unmountMedia();
        } finally {
            // Restore old status
            boolean currStatus = getMediaState();
            if (oldStatus != currStatus) {
                if (oldStatus) {
                    // Mount media
                    mountMedia();
                } else {
                    unmountMedia();
                }
            }
        }
    
public voidtestUnmountMultiple()

        if (Environment.isExternalStorageEmulated()) {
            return;
        }

        boolean oldStatus = getMediaState();
        StorageManager sm = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE);
        MultipleStorageLis observer = new MultipleStorageLis();
        try {
            // Mount media firsts
            if (!getMediaState()) {
                mountMedia();
            }
            String path = Environment.getExternalStorageDirectory().toString();
            sm.registerListener(observer);
            // Wait on observer
            synchronized(observer) {
                for (int i = 0; i < 5; i++) {
                    getMs().unmountVolume(path, false, false);
                }
                long waitTime = 0;
                while((!observer.isDone()) && (waitTime < MAX_WAIT_TIME) ) {
                    observer.wait(WAIT_TIME_INCR);
                    waitTime += WAIT_TIME_INCR;
                }
                if(!observer.isDone()) {
                    fail("Timed out waiting for packageInstalled callback");
                }
            }
            assertEquals(observer.count, 1);
        } finally {
            sm.unregisterListener(observer);
            // Restore old status
            boolean currStatus = getMediaState();
            if (oldStatus != currStatus) {
                if (oldStatus) {
                    // Mount media
                    mountMedia();
                } else {
                    unmountMedia();
                }
            }
        }
    
public voidtest_Ext4_External_CreateDuplicate_Failure()

        if (Environment.isExternalStorageEmulated()) {
            return;
        }

        assertEquals(StorageResultCode.OperationSucceeded,
                createContainer("testCreateDupContainer", 4, "none", FS_EXT4, true));

        assertEquals(StorageResultCode.OperationFailedInternalError,
                createContainer("testCreateDupContainer", 4, "none", FS_EXT4, true));
    
public voidtest_Ext4_External_CreateMinSize_Success()

        if (Environment.isExternalStorageEmulated()) {
            return;
        }

        assertEquals(StorageResultCode.OperationSucceeded,
                createContainer("testCreateContainer", 1, "none", FS_EXT4, true));
        assertTrue(containerExists("testCreateContainer"));
    
public voidtest_Ext4_External_CreateZeroSize_Failure()

        if (Environment.isExternalStorageEmulated()) {
            return;
        }

        assertEquals(StorageResultCode.OperationFailedInternalError,
                createContainer("testCreateZeroContainer", 0, "none", FS_EXT4, true));
    
public voidtest_Ext4_External_Create_Success()

        if (Environment.isExternalStorageEmulated()) {
            return;
        }

        assertEquals(StorageResultCode.OperationSucceeded,
                createContainer("testCreateContainer", 4, "none", FS_EXT4, true));
        assertTrue(containerExists("testCreateContainer"));
    
public voidtest_Ext4_External_Destroy_Success()

        if (Environment.isExternalStorageEmulated()) {
            return;
        }

        assertEquals(StorageResultCode.OperationSucceeded,
                createContainer("testDestroyContainer", 4, "none", FS_EXT4, true));
        assertEquals(StorageResultCode.OperationSucceeded,
                destroyContainer("testDestroyContainer", false));
    
public voidtest_Ext4_Internal_CreateDuplicate_Failure()

        assertEquals(StorageResultCode.OperationSucceeded,
                createContainer("testCreateDupContainer", 4, "none", FS_EXT4, false));

        assertEquals(StorageResultCode.OperationFailedInternalError,
                createContainer("testCreateDupContainer", 4, "none", FS_EXT4, false));
    
public voidtest_Ext4_Internal_CreateMinSize_Success()

        assertEquals(StorageResultCode.OperationSucceeded,
                createContainer("testCreateContainer", 1, "none", FS_EXT4, false));
        assertTrue(containerExists("testCreateContainer"));
    
public voidtest_Ext4_Internal_CreateZeroSize_Failure()

        assertEquals(StorageResultCode.OperationFailedInternalError,
                createContainer("testCreateZeroContainer", 0, "none", FS_EXT4, false));
    
public voidtest_Ext4_Internal_Create_Success()

        assertEquals(StorageResultCode.OperationSucceeded,
                createContainer("testCreateContainer", 4, "none", FS_EXT4, false));
        assertTrue(containerExists("testCreateContainer"));
    
public voidtest_Ext4_Internal_Destroy_Success()

        assertEquals(StorageResultCode.OperationSucceeded,
                createContainer("testDestroyContainer", 4, "none", FS_EXT4, false));
        assertEquals(StorageResultCode.OperationSucceeded,
                destroyContainer("testDestroyContainer", false));
    
public voidtest_Fat_External_CreateDuplicate_Failure()

        if (Environment.isExternalStorageEmulated()) {
            return;
        }

        assertEquals(StorageResultCode.OperationSucceeded,
                createContainer("testCreateDupContainer", 4, "none", FS_FAT, true));

        assertEquals(StorageResultCode.OperationFailedInternalError,
                createContainer("testCreateDupContainer", 4, "none", FS_FAT, true));
    
public voidtest_Fat_External_CreateMinSize_Success()

        if (Environment.isExternalStorageEmulated()) {
            return;
        }

        assertEquals(StorageResultCode.OperationSucceeded,
                createContainer("testCreateContainer", 1, "none", FS_FAT, true));
        assertTrue(containerExists("testCreateContainer"));
    
public voidtest_Fat_External_CreateZeroSize_Failure()

        if (Environment.isExternalStorageEmulated()) {
            return;
        }

        assertEquals(StorageResultCode.OperationFailedInternalError,
                createContainer("testCreateZeroContainer", 0, "none", FS_FAT, true));
    
public voidtest_Fat_External_Create_Success()

        if (Environment.isExternalStorageEmulated()) {
            return;
        }

        assertEquals(StorageResultCode.OperationSucceeded,
                createContainer("testCreateContainer", 4, "none", FS_FAT, true));
        assertTrue(containerExists("testCreateContainer"));
    
public voidtest_Fat_External_DestroyBusy()

        if (Environment.isExternalStorageEmulated()) {
            return;
        }

        IMountService ms = getMs();

        assertEquals(StorageResultCode.OperationSucceeded,
                createContainer("testDestroyBusyContainer", 4, "none", FS_FAT, true));

        String path = ms.getSecureContainerPath(SECURE_CONTAINER_PREFIX
                + "testDestroyBusyContainer");

        File f = new File(path, "reference");
        FileOutputStream fos = new FileOutputStream(f);

        assertEquals(StorageResultCode.OperationFailedStorageBusy,
                destroyContainer("testDestroyBusyContainer", false));

        fos.close();
        assertEquals(StorageResultCode.OperationSucceeded,
                destroyContainer("testDestroyBusyContainer", false));
    
public voidtest_Fat_External_Destroy_Success()

        if (Environment.isExternalStorageEmulated()) {
            return;
        }

        assertEquals(StorageResultCode.OperationSucceeded,
                createContainer("testDestroyContainer", 4, "none", FS_FAT, true));
        assertEquals(StorageResultCode.OperationSucceeded,
                destroyContainer("testDestroyContainer", false));
    
public voidtest_Fat_External_Mount()

        if (Environment.isExternalStorageEmulated()) {
            return;
        }

        assertEquals(StorageResultCode.OperationSucceeded,
                createContainer("testMountContainer", 4, "none", FS_FAT, true));

        assertEquals(StorageResultCode.OperationSucceeded,
                unmountContainer("testMountContainer", false));

        assertEquals(StorageResultCode.OperationSucceeded,
                mountContainer("testMountContainer", "none"));
    
public voidtest_Fat_External_MountBadKey_Failure()

        if (Environment.isExternalStorageEmulated()) {
            return;
        }

        assertEquals(StorageResultCode.OperationSucceeded,
                createContainer("testMountBadKey", 4, "00000000000000000000000000000000", FS_FAT,
                        true));

        assertEquals(StorageResultCode.OperationSucceeded,
                unmountContainer("testMountBadKey", false));

        assertEquals(StorageResultCode.OperationFailedInternalError,
                mountContainer("testMountContainer", "000000000000000000000000000000001"));

        assertEquals(StorageResultCode.OperationFailedInternalError,
                mountContainer("testMountContainer", "none"));
    
public voidtest_Fat_External_RenameDstMounted_Failure()

        if (Environment.isExternalStorageEmulated()) {
            return;
        }

        assertEquals(StorageResultCode.OperationSucceeded,
                createContainer("testRenameContainer.1", 4, "none", FS_FAT, true));

        assertEquals(StorageResultCode.OperationSucceeded,
                unmountContainer("testRenameContainer.1", false));

        assertEquals(StorageResultCode.OperationSucceeded,
                createContainer("testRenameContainer.2", 4, "none", FS_FAT, true));

        assertEquals(StorageResultCode.OperationFailedStorageMounted,
                renameContainer("testRenameContainer.1", "testRenameContainer.2"));
    
public voidtest_Fat_External_RenameSrcMounted_Failure()

        if (Environment.isExternalStorageEmulated()) {
            return;
        }

        assertEquals(StorageResultCode.OperationSucceeded,
                createContainer("testRenameContainer.1", 4, "none", FS_FAT, true));

        assertEquals(StorageResultCode.OperationFailedStorageMounted,
                renameContainer("testRenameContainer.1", "testRenameContainer.2"));
    
public voidtest_Fat_External_Rename_Success()

        if (Environment.isExternalStorageEmulated()) {
            return;
        }

        assertEquals(StorageResultCode.OperationSucceeded,
                createContainer("testRenameContainer.1", 4, "none", FS_FAT, true));

        assertEquals(StorageResultCode.OperationSucceeded,
                unmountContainer("testRenameContainer.1", false));

        assertEquals(StorageResultCode.OperationSucceeded,
                renameContainer("testRenameContainer.1", "testRenameContainer.2"));

        assertFalse(containerExists("testRenameContainer.1"));
        assertTrue(containerExists("testRenameContainer.2"));
    
public voidtest_Fat_External_Size_Success()

        if (Environment.isExternalStorageEmulated()) {
            return;
        }

        IMountService ms = getMs();
        assertEquals(StorageResultCode.OperationSucceeded,
                createContainer("testContainerSize", 1, "none", FS_FAT, true));
        String path = ms.getSecureContainerPath(SECURE_CONTAINER_PREFIX + "testContainerSize");

        byte[] buf = new byte[4096];
        File f = new File(path, "reference");
        FileOutputStream fos = new FileOutputStream(f);
        for (int i = 0; i < (1024 * 1024); i += buf.length) {
            fos.write(buf);
        }
        fos.close();
    
public voidtest_Fat_External_UnmountBusy_Success()

        if (Environment.isExternalStorageEmulated()) {
            return;
        }

        IMountService ms = getMs();
        assertEquals(StorageResultCode.OperationSucceeded,
                createContainer("testUnmountBusyContainer", 4, "none", FS_FAT, true));

        String path = ms.getSecureContainerPath(SECURE_CONTAINER_PREFIX
                + "testUnmountBusyContainer");

        File f = new File(path, "reference");
        FileOutputStream fos = new FileOutputStream(f);

        assertEquals(StorageResultCode.OperationFailedStorageBusy,
                unmountContainer("testUnmountBusyContainer", false));

        fos.close();
        assertEquals(StorageResultCode.OperationSucceeded,
                unmountContainer("testUnmountBusyContainer", false));
    
public voidtest_Fat_Internal_CreateDuplicate_Failure()

        assertEquals(StorageResultCode.OperationSucceeded,
                createContainer("testCreateDupContainer", 4, "none", FS_FAT, false));

        assertEquals(StorageResultCode.OperationFailedInternalError,
                createContainer("testCreateDupContainer", 4, "none", FS_FAT, false));
    
public voidtest_Fat_Internal_CreateMinSize_Success()

        assertEquals(StorageResultCode.OperationSucceeded,
                createContainer("testCreateContainer", 1, "none", FS_FAT, false));
        assertTrue(containerExists("testCreateContainer"));
    
public voidtest_Fat_Internal_CreateZeroSize_Failure()

        assertEquals(StorageResultCode.OperationFailedInternalError,
                createContainer("testCreateZeroContainer", 0, "none", FS_FAT, false));
    
public voidtest_Fat_Internal_Create_Success()

        assertEquals(StorageResultCode.OperationSucceeded,
                createContainer("testCreateContainer", 4, "none", FS_FAT, false));
        assertTrue(containerExists("testCreateContainer"));
    
public voidtest_Fat_Internal_Destroy_Success()

        assertEquals(StorageResultCode.OperationSucceeded,
                createContainer("testDestroyContainer", 4, "none", FS_FAT, false));
        assertEquals(StorageResultCode.OperationSucceeded,
                destroyContainer("testDestroyContainer", false));
    
private intunmountContainer(java.lang.String localId, boolean force)

        assertTrue("Media should be mounted", isMediaMounted());
        String fullId = SECURE_CONTAINER_PREFIX + localId;

        IMountService ms = getMs();
        return ms.unmountSecureContainer(fullId, force);
    
private voidunmountMedia()

        if (Environment.isExternalStorageEmulated()) {
            return;
        }

        if (!getMediaState()) {
            return;
        }

        String path = Environment.getExternalStorageDirectory().toString();
        StorageListener observer = new StorageListener();
        StorageManager sm = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE);
        sm.registerListener(observer);
        try {
            // Wait on observer
            synchronized(observer) {
                getMs().unmountVolume(path, false, false);
                long waitTime = 0;
                while((!observer.isDone()) && (waitTime < MAX_WAIT_TIME) ) {
                    observer.wait(WAIT_TIME_INCR);
                    waitTime += WAIT_TIME_INCR;
                }
                if(!observer.isDone()) {
                    fail("Timed out waiting for packageInstalled callback");
                }
            }
        } finally {
            sm.unregisterListener(observer);
        }