Java Code Examples for androidx.sqlite.db.SupportSQLiteOpenHelper#getReadableDatabase()

The following examples show how to use androidx.sqlite.db.SupportSQLiteOpenHelper#getReadableDatabase() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PostHookSqlTest.java    From cwac-saferoom with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example 2
Source File: PostHookSqlTest.java    From cwac-saferoom with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example 3
Source File: PostHookSqlTest.java    From cwac-saferoom with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 4
Source File: EncryptTest.java    From cwac-saferoom with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 5
Source File: DecryptTest.java    From cwac-saferoom with Apache License 2.0 5 votes vote down vote up
private void dekey(Callable<?> decrypter) throws Exception {
  final Context ctxt=InstrumentationRegistry.getTargetContext();

  assertEquals(SQLCipherUtils.State.DOES_NOT_EXIST, 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.getWritableDatabase();

  assertOriginalContent(db);
  db.close();

  assertEquals(SQLCipherUtils.State.ENCRYPTED, SQLCipherUtils.getDatabaseState(ctxt, DB_NAME));

  decrypter.call();

  SQLiteDatabase plainDb=
    SQLiteDatabase.openDatabase(ctxt.getDatabasePath(DB_NAME).getAbsolutePath(),
      null, SQLiteDatabase.OPEN_READWRITE);

  assertOriginalContent(plainDb);
  plainDb.close();

  assertEquals(SQLCipherUtils.State.UNENCRYPTED, SQLCipherUtils.getDatabaseState(ctxt, DB_NAME));

  factory = new SafeHelperFactory("".toCharArray());
  helper = factory.create(InstrumentationRegistry.getTargetContext(), DB_NAME, new Callback(1));
  db = helper.getReadableDatabase();

  assertOriginalContent(db);

  db.close();
}
 
Example 6
Source File: DbDefaultConnectionTest.java    From sqlitemagic with Apache License 2.0 5 votes vote down vote up
@Test
public void reInitClosesPrev() {
  final DbConnectionImpl dbConnection = SqliteMagic.getDefaultDbConnection();
  final SupportSQLiteOpenHelper dbHelper = dbConnection.dbHelper;
  final SupportSQLiteDatabase readableDatabase = dbHelper.getReadableDatabase();
  final SupportSQLiteDatabase writableDatabase = dbHelper.getWritableDatabase();

  initDbWithNewConnection();

  assertThat(readableDatabase.isOpen()).isFalse();
  assertThat(writableDatabase.isOpen()).isFalse();
  assertThat(dbConnection.triggers.hasObservers()).isFalse();
  assertThat(dbConnection.triggers.hasComplete()).isTrue();
}