Java Code Examples for androidx.sqlite.db.SupportSQLiteDatabase#close()

The following examples show how to use androidx.sqlite.db.SupportSQLiteDatabase#close() . 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: 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 2
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 3
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 4
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 5
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 6
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 7
Source File: SQLiteTestUtils.java    From kripton with Apache License 2.0 6 votes vote down vote up
public static <H extends AbstractDataSource> void showSchema(H dataSource) {
	boolean needToBeOpened = !dataSource.isOpen();
	SupportSQLiteDatabase database=null;

	if (needToBeOpened) {
		database = dataSource.openReadOnlyDatabase();
	}

	List<String> actualSql = new ArrayList<String>();
	actualSql.addAll(SQLiteTestUtils.getAllTables(database).values());
	actualSql.addAll(SQLiteTestUtils.getAllIndexes(database).values());

	Logger.info("Database schema version %s", database.getVersion());
	for (String item : actualSql) {
		Logger.info("DDL: %s", item);
	}
	
	if (database!=null && needToBeOpened) {
		try {
			database.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
 
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: 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 10
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 11
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 12
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 13
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 14
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();
}