FileDocCategorySizeDatePackage
LockSettingsStorageTests.javaAPI DocAndroid 5.1 API13481Thu Mar 12 22:22:42 GMT 2015com.android.server

LockSettingsStorageTests

public class LockSettingsStorageTests extends android.test.AndroidTestCase

Fields Summary
LockSettingsStorage
mStorage
File
mStorageDir
private File
mDb
Constructors Summary
Methods Summary
private static voidassertArrayEquals(byte[] expected, byte[] actual)

        if (!Arrays.equals(expected, actual)) {
            fail("expected:<" + Arrays.toString(expected) +
                    "> but was:<" + Arrays.toString(actual) + ">");
        }
    
protected voidsetUp()

        super.setUp();
        mStorageDir = new File(getContext().getFilesDir(), "locksettings");
        mDb = getContext().getDatabasePath("locksettings.db");

        assertTrue(mStorageDir.exists() || mStorageDir.mkdirs());
        assertTrue(FileUtils.deleteContents(mStorageDir));
        assertTrue(!mDb.exists() || mDb.delete());

        final Context ctx = getContext();
        setContext(new ContextWrapper(ctx) {
            @Override
            public Object getSystemService(String name) {
                if (USER_SERVICE.equals(name)) {
                    return new UserManager(ctx, null) {
                        @Override
                        public UserInfo getProfileParent(int userHandle) {
                            if (userHandle == 2) {
                                // User 2 is a profile of user 1.
                                return new UserInfo(1, "name", 0);
                            }
                            if (userHandle == 3) {
                                // User 3 is a profile of user 0.
                                return new UserInfo(0, "name", 0);
                            }
                            return null;
                        }
                    };
                }
                return super.getSystemService(name);
            }
        });

        mStorage = new LockSettingsStorage(getContext(), new LockSettingsStorage.Callback() {
            @Override
            public void initialize(SQLiteDatabase db) {
                mStorage.writeKeyValue(db, "initializedKey", "initialValue", 0);
            }
        }) {
            @Override
            String getLockPatternFilename(int userId) {
                return new File(mStorageDir,
                        super.getLockPatternFilename(userId).replace('/", '-")).getAbsolutePath();
            }

            @Override
            String getLockPasswordFilename(int userId) {
                return new File(mStorageDir,
                        super.getLockPasswordFilename(userId).replace('/", '-")).getAbsolutePath();
            }
        };
    
protected voidtearDown()

        super.tearDown();
        mStorage.closeDatabase();
    
public voidtestFileLocation_Owner()

        LockSettingsStorage storage = new LockSettingsStorage(getContext(), null);

        assertEquals("/data/system/gesture.key", storage.getLockPatternFilename(0));
        assertEquals("/data/system/password.key", storage.getLockPasswordFilename(0));
    
public voidtestFileLocation_ProfileToOwner()

        LockSettingsStorage storage = new LockSettingsStorage(getContext(), null);

        assertEquals("/data/system/gesture.key", storage.getLockPatternFilename(3));
        assertEquals("/data/system/password.key", storage.getLockPasswordFilename(3));
    
public voidtestFileLocation_ProfileToSecondary()

        LockSettingsStorage storage = new LockSettingsStorage(getContext(), null);

        assertEquals("/data/system/users/1/gesture.key", storage.getLockPatternFilename(2));
        assertEquals("/data/system/users/1/password.key", storage.getLockPasswordFilename(2));
    
public voidtestFileLocation_SecondaryUser()

        LockSettingsStorage storage = new LockSettingsStorage(getContext(), null);

        assertEquals("/data/system/users/1/gesture.key", storage.getLockPatternFilename(1));
        assertEquals("/data/system/users/1/password.key", storage.getLockPasswordFilename(1));
    
public voidtestKeyValue_CacheStarvedWriter()

        final CountDownLatch latch = new CountDownLatch(1);
        List<Thread> threads = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            final int threadId = i;
            threads.add(new Thread() {
                @Override
                public void run() {
                    try {
                        latch.await();
                    } catch (InterruptedException e) {
                        return;
                    }
                    if (threadId == 50) {
                        mStorage.writeKeyValue("starvedWriterKey", "value", 0);
                    } else {
                        mStorage.readKeyValue("starvedWriterKey", "default", 0);
                    }
                }
            });
            threads.get(i).start();
        }
        latch.countDown();
        for (int i = 0; i < threads.size(); i++) {
            try {
                threads.get(i).join();
            } catch (InterruptedException e) {
            }
        }
        String cached = mStorage.readKeyValue("key", "default", 0);
        mStorage.clearCache();
        String storage = mStorage.readKeyValue("key", "default", 0);
        assertEquals("Cached value didn't match stored value", storage, cached);
    
public voidtestKeyValue_Concurrency()

        final Object monitor = new Object();
        List<Thread> threads = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            final int threadId = i;
            threads.add(new Thread() {
                @Override
                public void run() {
                    synchronized (monitor) {
                        try {
                            monitor.wait();
                        } catch (InterruptedException e) {
                            return;
                        }
                        mStorage.writeKeyValue("key", "1 from thread " + threadId, 0);
                        mStorage.readKeyValue("key", "default", 0);
                        mStorage.writeKeyValue("key", "2 from thread " + threadId, 0);
                        mStorage.readKeyValue("key", "default", 0);
                        mStorage.writeKeyValue("key", "3 from thread " + threadId, 0);
                        mStorage.readKeyValue("key", "default", 0);
                        mStorage.writeKeyValue("key", "4 from thread " + threadId, 0);
                        mStorage.readKeyValue("key", "default", 0);
                        mStorage.writeKeyValue("key", "5 from thread " + threadId, 0);
                        mStorage.readKeyValue("key", "default", 0);
                    }
                }
            });
            threads.get(i).start();
        }
        mStorage.writeKeyValue("key", "initalValue", 0);
        synchronized (monitor) {
            monitor.notifyAll();
        }
        for (int i = 0; i < threads.size(); i++) {
            try {
                threads.get(i).join();
            } catch (InterruptedException e) {
            }
        }
        assertEquals('5", mStorage.readKeyValue("key", "default", 0).charAt(0));
        mStorage.clearCache();
        assertEquals('5", mStorage.readKeyValue("key", "default", 0).charAt(0));
    
public voidtestKeyValue_DefaultValue()

        assertEquals("default", mStorage.readKeyValue("unititialized key", "default", 0));
        assertEquals("default2", mStorage.readKeyValue("unititialized key", "default2", 0));
    
public voidtestKeyValue_InitializeWorked()

        assertEquals("initialValue", mStorage.readKeyValue("initializedKey", "default", 0));
        mStorage.clearCache();
        assertEquals("initialValue", mStorage.readKeyValue("initializedKey", "default", 0));
    
public voidtestKeyValue_WriteThenRead()

        mStorage.writeKeyValue("key", "value", 0);
        assertEquals("value", mStorage.readKeyValue("key", "default", 0));
        mStorage.clearCache();
        assertEquals("value", mStorage.readKeyValue("key", "default", 0));
    
public voidtestPassword_Default()

        assertNull(mStorage.readPasswordHash(0));
    
public voidtestPassword_Write()

        mStorage.writePasswordHash("thepassword".getBytes(), 0);

        assertArrayEquals("thepassword".getBytes(), mStorage.readPasswordHash(0));
        mStorage.clearCache();
        assertArrayEquals("thepassword".getBytes(), mStorage.readPasswordHash(0));
    
public voidtestPassword_WriteParentWritesProfile()

        mStorage.writePasswordHash("profilepassword".getBytes(), 2);
        mStorage.writePasswordHash("parentpasswordd".getBytes(), 1);

        assertArrayEquals("parentpasswordd".getBytes(), mStorage.readPasswordHash(1));
        assertArrayEquals("parentpasswordd".getBytes(), mStorage.readPasswordHash(2));
        mStorage.clearCache();
        assertArrayEquals("parentpasswordd".getBytes(), mStorage.readPasswordHash(1));
        assertArrayEquals("parentpasswordd".getBytes(), mStorage.readPasswordHash(2));
    
public voidtestPassword_WriteProfileWritesParent()

        mStorage.writePasswordHash("parentpasswordd".getBytes(), 1);
        mStorage.writePasswordHash("profilepassword".getBytes(), 2);

        assertArrayEquals("profilepassword".getBytes(), mStorage.readPasswordHash(1));
        assertArrayEquals("profilepassword".getBytes(), mStorage.readPasswordHash(2));
        mStorage.clearCache();
        assertArrayEquals("profilepassword".getBytes(), mStorage.readPasswordHash(1));
        assertArrayEquals("profilepassword".getBytes(), mStorage.readPasswordHash(2));
    
public voidtestPattern_Default()

        assertNull(mStorage.readPasswordHash(0));
    
public voidtestPattern_Write()

        mStorage.writePatternHash("thepattern".getBytes(), 0);

        assertArrayEquals("thepattern".getBytes(), mStorage.readPatternHash(0));
        mStorage.clearCache();
        assertArrayEquals("thepattern".getBytes(), mStorage.readPatternHash(0));
    
public voidtestPattern_WriteParentWritesProfile()

        mStorage.writePatternHash("profilepattern".getBytes(), 2);
        mStorage.writePatternHash("parentpatternn".getBytes(), 1);

        assertArrayEquals("parentpatternn".getBytes(), mStorage.readPatternHash(1));
        assertArrayEquals("parentpatternn".getBytes(), mStorage.readPatternHash(2));
        mStorage.clearCache();
        assertArrayEquals("parentpatternn".getBytes(), mStorage.readPatternHash(1));
        assertArrayEquals("parentpatternn".getBytes(), mStorage.readPatternHash(2));
    
public voidtestPattern_WriteProfileWritesParent()

        mStorage.writePatternHash("parentpatternn".getBytes(), 1);
        mStorage.writePatternHash("profilepattern".getBytes(), 2);

        assertArrayEquals("profilepattern".getBytes(), mStorage.readPatternHash(1));
        assertArrayEquals("profilepattern".getBytes(), mStorage.readPatternHash(2));
        mStorage.clearCache();
        assertArrayEquals("profilepattern".getBytes(), mStorage.readPatternHash(1));
        assertArrayEquals("profilepattern".getBytes(), mStorage.readPatternHash(2));
    
public voidtestPrefetch()

        mStorage.writeKeyValue("key", "toBeFetched", 0);
        mStorage.writePatternHash("pattern".getBytes(), 0);
        mStorage.writePasswordHash("password".getBytes(), 0);

        mStorage.clearCache();
        mStorage.prefetchUser(0);

        assertEquals("toBeFetched", mStorage.readKeyValue("key", "default", 0));
        assertArrayEquals("pattern".getBytes(), mStorage.readPatternHash(0));
        assertArrayEquals("password".getBytes(), mStorage.readPasswordHash(0));
    
public voidtestRemoveUser()

        mStorage.writeKeyValue("key", "value", 0);
        mStorage.writePasswordHash(new byte[]{1}, 0);
        mStorage.writePatternHash(new byte[]{2}, 0);

        mStorage.writeKeyValue("key", "value", 1);
        mStorage.writePasswordHash(new byte[]{1}, 1);
        mStorage.writePatternHash(new byte[]{2}, 1);

        mStorage.removeUser(0);

        assertEquals("value", mStorage.readKeyValue("key", "default", 1));
        assertEquals("default", mStorage.readKeyValue("key", "default", 0));
        assertNotNull(mStorage.readPasswordHash(1));
        assertNull(mStorage.readPasswordHash(0));
        assertNotNull(mStorage.readPatternHash(1));
        assertNull(mStorage.readPatternHash(0));