Permalink
Browse files

Using the SQLiteDatabaseHook to allow for a test that changes the cip…

…her used on the database
  • Loading branch information...
1 parent 59c04b6 commit c35213dcd961f056599cd7ca4a86bafd37d5a93a @developernotes developernotes committed Mar 22, 2012
View
Binary file not shown.
@@ -30,10 +30,13 @@ public Activity getCurrentActivity(){
return activity;
}
- public SQLiteDatabase createDatabase(){
+ public void prepareDatabaseEnvironment(){
File databaseFile = getDatabasePath(DATABASE_NAME);
databaseFile.mkdirs();
databaseFile.delete();
+ }
+
+ public SQLiteDatabase createDatabase(File databaseFile){
return SQLiteDatabase.openOrCreateDatabase(databaseFile, DATABASE_PASSWORD, null);
}
}
@@ -7,6 +7,8 @@
import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteQueryBuilder;
+import java.io.File;
+
public class ZeteticContentProvider extends ContentProvider {
public static final Uri CONTENT_URI =
@@ -27,7 +29,8 @@ public boolean onCreate() {
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteDatabase.loadLibs(ZeteticApplication.getInstance());
- database = ZeteticApplication.getInstance().createDatabase();
+ File databasePath = ZeteticApplication.getInstance().getDatabasePath(ZeteticApplication.DATABASE_NAME);
+ database = ZeteticApplication.getInstance().createDatabase(databasePath);
createDatabaseWithData(database);
SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
@@ -0,0 +1,47 @@
+package net.zetetic.tests;
+
+import android.database.Cursor;
+import net.sqlcipher.database.SQLiteDatabase;
+import net.sqlcipher.database.SQLiteDatabaseHook;
+import net.zetetic.ZeteticApplication;
+
+import java.io.File;
+
+public class AES128CipherTest extends SQLCipherTest {
+ @Override
+ public boolean execute(SQLiteDatabase database) {
+
+ String actual = "";
+ String value = "hey";
+ database.execSQL("create table t1(a)");
+ database.execSQL("insert into t1(a) values (?)", new Object[]{value});
+ Cursor result = database.rawQuery("select * from t1", new String[]{});
+ if(result != null){
+ result.moveToFirst();
+ actual = result.getString(0);
+ result.close();
+ }
+ return actual.equals(value);
+ }
+
+ @Override
+ public String getName() {
+ return "AES-128 Bit Cipher Test";
+ }
+
+ @Override
+ protected SQLiteDatabase createDatabase(File databasePath) {
+
+ String password = ZeteticApplication.DATABASE_PASSWORD;
+ return SQLiteDatabase.openOrCreateDatabase(databasePath, password, null, new SQLiteDatabaseHook() {
+
+ @Override
+ public void preKey(SQLiteDatabase database) {}
+
+ @Override
+ public void postKey(SQLiteDatabase database) {
+ database.execSQL("PRAGMA cipher = 'aes-128-cbc'");
+ }
+ });
+ }
+}
@@ -4,6 +4,8 @@
import net.sqlcipher.database.SQLiteDatabase;
import net.zetetic.ZeteticApplication;
+import java.io.File;
+
public abstract class SQLCipherTest {
public abstract boolean execute(SQLiteDatabase database);
@@ -12,7 +14,9 @@
private SQLiteDatabase database;
protected void internalSetUp() {
- database = ZeteticApplication.getInstance().createDatabase();
+ ZeteticApplication.getInstance().prepareDatabaseEnvironment();
+ File databasePath = ZeteticApplication.getInstance().getDatabasePath(ZeteticApplication.DATABASE_NAME);
+ database = createDatabase(databasePath);
setUp();
}
@@ -34,6 +38,10 @@ private void internalTearDown(){
SQLiteDatabase.releaseMemory();
tearDown();
}
+
+ protected SQLiteDatabase createDatabase(File databasePath){
+ return ZeteticApplication.getInstance().createDatabase(databasePath);
+ }
protected void setUp(){};
protected void tearDown(){};
@@ -48,6 +48,7 @@ private void runSuite(){
tests.add(new CanThrowSQLiteExceptionTest());
tests.add(new RawExecSQLTest());
tests.add(new RawExecSQLExceptionTest());
+ tests.add(new AES128CipherTest());
return tests;
}
}

0 comments on commit c35213d

Please sign in to comment.