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

The following examples show how to use androidx.sqlite.db.SupportSQLiteOpenHelper#getWritableDatabase() . 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: PassphraseClearTest.java    From cwac-saferoom with Apache License 2.0 6 votes vote down vote up
@Test
public void charArrayCleared() throws IOException {
  char[] charArray = PASSPHRASE.toCharArray();
  SafeHelperFactory factory=new SafeHelperFactory(charArray);
  SupportSQLiteOpenHelper helper=
    factory.create(InstrumentationRegistry.getTargetContext(), DB_NAME,
      new Callback(1));
  SupportSQLiteDatabase db=helper.getWritableDatabase();

  helper.close();

  char[] expected = new char[charArray.length];

  for (int i=0;i<expected.length;i++) {
    expected[i]=(char)0;
  }

  assertArrayEquals(expected, charArray);
}
 
Example 2
Source File: PassphraseClearTest.java    From cwac-saferoom with Apache License 2.0 6 votes vote down vote up
@Test
public void byteArrayCleared() throws IOException {
  char[] charArray = PASSPHRASE.toCharArray();
  byte[] byteArray = SQLiteDatabase.getBytes(charArray);
  SafeHelperFactory factory=new SafeHelperFactory(byteArray);
  SupportSQLiteOpenHelper helper=
      factory.create(InstrumentationRegistry.getTargetContext(), DB_NAME,
          new Callback(1));
  SupportSQLiteDatabase db=helper.getWritableDatabase();

  helper.close();

  byte[] expected = new byte[byteArray.length];

  for (int i=0;i<expected.length;i++) {
    expected[i]=(byte)0;
  }

  assertArrayEquals(expected, byteArray);
}
 
Example 3
Source File: CloseThenOpenTest.java    From cwac-saferoom with Apache License 2.0 6 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void defaultReopenClosedHelper() throws IOException {
  SafeHelperFactory factory=
    SafeHelperFactory.fromUser(new SpannableStringBuilder(PASSPHRASE));
  SupportSQLiteOpenHelper helper=
    factory.create(InstrumentationRegistry.getTargetContext(), DB_NAME,
      new Callback(1));
  SupportSQLiteDatabase db=helper.getWritableDatabase();

  helper.close();

  helper=
      factory.create(InstrumentationRegistry.getTargetContext(), DB_NAME,
          new Callback(1));
  SupportSQLiteDatabase db2=helper.getWritableDatabase();

  db2.close();
}
 
Example 4
Source File: CloseThenOpenTest.java    From cwac-saferoom with Apache License 2.0 6 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void explicitClearReopenClosedHelper() throws IOException {
  SafeHelperFactory factory=
      SafeHelperFactory.fromUser(new SpannableStringBuilder(PASSPHRASE),
          SafeHelperFactory.Options.builder().setClearPassphrase(true).build());
  SupportSQLiteOpenHelper helper=
      factory.create(InstrumentationRegistry.getTargetContext(), DB_NAME,
          new Callback(1));
  SupportSQLiteDatabase db=helper.getWritableDatabase();

  helper.close();

  helper=
      factory.create(InstrumentationRegistry.getTargetContext(), DB_NAME,
          new Callback(1));
  SupportSQLiteDatabase db2=helper.getWritableDatabase();

  db2.close();
}
 
Example 5
Source File: CloseThenOpenTest.java    From cwac-saferoom with Apache License 2.0 6 votes vote down vote up
@Test
public void clearOutOutReopenClosedHelper() throws IOException {
  SafeHelperFactory factory=
      SafeHelperFactory.fromUser(new SpannableStringBuilder(PASSPHRASE),
          SafeHelperFactory.Options.builder().setClearPassphrase(false).build());
  SupportSQLiteOpenHelper helper=
      factory.create(InstrumentationRegistry.getTargetContext(), DB_NAME,
          new Callback(1));
  SupportSQLiteDatabase db=helper.getWritableDatabase();

  helper.close();

  helper=
      factory.create(InstrumentationRegistry.getTargetContext(), DB_NAME,
          new Callback(1));
  SupportSQLiteDatabase db2=helper.getWritableDatabase();

  db2.close();
}
 
Example 6
Source File: RekeyTest.java    From cwac-saferoom with Apache License 2.0 6 votes vote down vote up
@Test
public void rekeyEditable() throws IOException {
  SafeHelperFactory factory=
    SafeHelperFactory.fromUser(new SpannableStringBuilder("sekrit"));
  SupportSQLiteOpenHelper helper=
    factory.create(InstrumentationRegistry.getTargetContext(), DB_NAME,
      new Callback(1));
  SupportSQLiteDatabase db=helper.getWritableDatabase();

  assertOriginalContent(db);
  SafeHelperFactory.rekey(db, new SpannableStringBuilder(PASSPHRASE));
  assertOriginalContent(db);
  db.execSQL("UPDATE foo SET bar=?, goo=?", new Object[] {3, "four"});
  assertUpdatedContent(db);
  db.close();

  factory=SafeHelperFactory.fromUser(new SpannableStringBuilder(PASSPHRASE));
  helper=factory.create(InstrumentationRegistry.getTargetContext(), DB_NAME,
    new Callback(1));
  db=helper.getWritableDatabase();
  assertUpdatedContent(db);
}
 
Example 7
Source File: RekeyTest.java    From cwac-saferoom with Apache License 2.0 6 votes vote down vote up
@Test
public void rekeyCharArray() throws IOException {
  SafeHelperFactory factory=
          SafeHelperFactory.fromUser(new SpannableStringBuilder("sekrit"));
  SupportSQLiteOpenHelper helper=
          factory.create(InstrumentationRegistry.getTargetContext(), DB_NAME,
                  new Callback(1));
  SupportSQLiteDatabase db=helper.getWritableDatabase();

  assertOriginalContent(db);
  SafeHelperFactory.rekey(db, PASSPHRASE.toCharArray());
  assertOriginalContent(db);
  db.execSQL("UPDATE foo SET bar=?, goo=?", new Object[] {3, "four"});
  assertUpdatedContent(db);
  db.close();

  factory=SafeHelperFactory.fromUser(new SpannableStringBuilder(PASSPHRASE));
  helper=factory.create(InstrumentationRegistry.getTargetContext(), DB_NAME,
          new Callback(1));
  db=helper.getWritableDatabase();
  assertUpdatedContent(db);
}
 
Example 8
Source File: PreHookSqlTest.java    From cwac-saferoom with Apache License 2.0 5 votes vote down vote up
@Test
public void testPreKeySql() throws IOException {
  SafeHelperFactory.Options options = SafeHelperFactory.Options.builder().setPreKeySql(PREKEY_SQL).build();
  SafeHelperFactory factory=
    SafeHelperFactory.fromUser(new SpannableStringBuilder(PASSPHRASE), options);
  SupportSQLiteOpenHelper helper=
    factory.create(InstrumentationRegistry.getTargetContext(), DB_NAME,
      new Callback(1));
  SupportSQLiteDatabase db=helper.getWritableDatabase();

  assertEquals(1, db.getVersion());

  db.close();
}
 
Example 9
Source File: WALTest.java    From cwac-saferoom with Apache License 2.0 5 votes vote down vote up
@Test
public void wal() throws IOException {
  SafeHelperFactory factory=
    SafeHelperFactory.fromUser(new SpannableStringBuilder(PASSPHRASE));
  SupportSQLiteOpenHelper helper=
    factory.create(InstrumentationRegistry.getTargetContext(), DB_NAME,
      new Callback(1));
  SupportSQLiteDatabase db=helper.getWritableDatabase();

  assertFalse(db.isWriteAheadLoggingEnabled());

  assertTrue(db.enableWriteAheadLogging());
  assertTrue(db.isWriteAheadLoggingEnabled());

  db.close();

  factory=SafeHelperFactory.fromUser(new SpannableStringBuilder(PASSPHRASE));
  helper=
    factory.create(InstrumentationRegistry.getTargetContext(), DB_NAME,
      new Callback(1));
  db=helper.getWritableDatabase();

  assertTrue(db.isWriteAheadLoggingEnabled());
  db.disableWriteAheadLogging();
  assertFalse(db.isWriteAheadLoggingEnabled());

  db.close();
}
 
Example 10
Source File: RekeyTest.java    From cwac-saferoom with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void rekeyEditableFramework() throws IOException {
  FrameworkSQLiteOpenHelperFactory factory=new FrameworkSQLiteOpenHelperFactory();
  SupportSQLiteOpenHelper.Configuration cfg=
      SupportSQLiteOpenHelper.Configuration.builder(InstrumentationRegistry.getTargetContext())
          .callback(new Callback(1))
          .name(DB_NAME)
          .build();
  SupportSQLiteOpenHelper helper=factory.create(cfg);
  SupportSQLiteDatabase db=helper.getWritableDatabase();

  SafeHelperFactory.rekey(db, new SpannableStringBuilder(PASSPHRASE));
  db.close();
}
 
Example 11
Source File: RekeyTest.java    From cwac-saferoom with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void rekeyCharArrayFramework() throws IOException {
  FrameworkSQLiteOpenHelperFactory factory=new FrameworkSQLiteOpenHelperFactory();
  SupportSQLiteOpenHelper.Configuration cfg=
      SupportSQLiteOpenHelper.Configuration.builder(InstrumentationRegistry.getTargetContext())
          .callback(new Callback(1))
          .name(DB_NAME)
          .build();
  SupportSQLiteOpenHelper helper=factory.create(cfg);
  SupportSQLiteDatabase db=helper.getWritableDatabase();

  SafeHelperFactory.rekey(db, PASSPHRASE.toCharArray());
}
 
Example 12
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 13
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();
}