@Test(expected = net.sqlcipher.database.SQLiteException.class) public void defaultBehavior() throws IOException { assertTrue(getDbFile().exists()); SafeHelperFactory factory= SafeHelperFactory.fromUser(new SpannableStringBuilder(PASSPHRASE)); SupportSQLiteOpenHelper helper= factory.create(InstrumentationRegistry.getTargetContext(), DB_NAME, new Callback(1)); SupportSQLiteDatabase db=helper.getReadableDatabase(); db.close(); }
private void enkey(Callable<?> encrypter) throws Exception { final Context ctxt=InstrumentationRegistry.getTargetContext(); assertEquals(SQLCipherUtils.State.DOES_NOT_EXIST, SQLCipherUtils.getDatabaseState(ctxt, DB_NAME)); SQLiteDatabase plainDb= SQLiteDatabase.openOrCreateDatabase(ctxt.getDatabasePath(DB_NAME).getAbsolutePath(), null); plainDb.execSQL("CREATE TABLE foo (bar, goo);"); plainDb.execSQL("INSERT INTO foo (bar, goo) VALUES (?, ?)", new Object[] {1, "two"}); assertOriginalContent(plainDb); plainDb.close(); assertEquals(SQLCipherUtils.State.UNENCRYPTED, SQLCipherUtils.getDatabaseState(ctxt, DB_NAME)); encrypter.call(); assertEquals(SQLCipherUtils.State.ENCRYPTED, SQLCipherUtils.getDatabaseState(ctxt, DB_NAME)); SafeHelperFactory factory= SafeHelperFactory.fromUser(new SpannableStringBuilder(PASSPHRASE)); SupportSQLiteOpenHelper helper= factory.create(InstrumentationRegistry.getTargetContext(), DB_NAME, new Callback(1)); SupportSQLiteDatabase db=helper.getReadableDatabase(); assertOriginalContent(db); db.close(); }
@Test public void migrate() throws IOException { assertTrue(getDbFile().exists()); SafeHelperFactory factory= SafeHelperFactory.fromUser(new SpannableStringBuilder(PASSPHRASE), SafeHelperFactory.POST_KEY_SQL_MIGRATE); SupportSQLiteOpenHelper helper= factory.create(InstrumentationRegistry.getTargetContext(), DB_NAME, new Callback(1)); SupportSQLiteDatabase db=helper.getReadableDatabase(); assertOriginalContent(db); db.close(); // with migrate, the change should be permanent factory=SafeHelperFactory.fromUser(new SpannableStringBuilder(PASSPHRASE)); helper=factory.create(InstrumentationRegistry.getTargetContext(), DB_NAME, new Callback(1)); db=helper.getReadableDatabase(); assertOriginalContent(db); db.close(); }
@Test public void v3() throws IOException { assertTrue(getDbFile().exists()); SafeHelperFactory factory= SafeHelperFactory.fromUser(new SpannableStringBuilder(PASSPHRASE), SafeHelperFactory.POST_KEY_SQL_V3); SupportSQLiteOpenHelper helper= factory.create(InstrumentationRegistry.getTargetContext(), DB_NAME, new Callback(1)); SupportSQLiteDatabase db=helper.getReadableDatabase(); assertOriginalContent(db); db.close(); // with v3, the change should be temporary factory=SafeHelperFactory.fromUser(new SpannableStringBuilder(PASSPHRASE)); helper=factory.create(InstrumentationRegistry.getTargetContext(), DB_NAME, new Callback(1)); boolean didWeGoBoom = false; try { db = helper.getReadableDatabase(); } catch (net.sqlcipher.database.SQLiteException ex) { didWeGoBoom = true; } assertTrue(didWeGoBoom); }