Skip to content

Commit d9466fc

Browse files
Updated SQLCipher for Android libraries to 2.0.0-RC7 and added
unencrypted import test.
1 parent 389935b commit d9466fc

File tree

9 files changed

+175
-67
lines changed

9 files changed

+175
-67
lines changed

assets/unencrypted.db

12 KB
Binary file not shown.
64 Bytes
Binary file not shown.

libs/sqlcipher.jar

0 Bytes
Binary file not shown.

net.zetetic.sqlcipher.test.iws

Lines changed: 114 additions & 62 deletions
Large diffs are not rendered by default.

src/main/java/net/zetetic/ZeteticApplication.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class ZeteticApplication extends Application {
1414
private Activity activity;
1515
public static final String TAG = "Zetetic";
1616
public static final String ONE_X_DATABASE = "1x.db";
17+
public static final String UNENCRYPTED_DATABASE = "unencrypted.db";
1718

1819
public ZeteticApplication(){
1920
instance = this;
@@ -41,11 +42,11 @@ public SQLiteDatabase createDatabase(File databaseFile){
4142
return SQLiteDatabase.openOrCreateDatabase(databaseFile, DATABASE_PASSWORD, null);
4243
}
4344

44-
public void extract1xDatabaseToDatabaseDirectory() throws IOException {
45+
public void extractAssetToDatabaseDirectory(String fileName) throws IOException {
4546

4647
int length;
47-
InputStream sourceDatabase = ZeteticApplication.getInstance().getAssets().open(ONE_X_DATABASE);
48-
File destinationPath = ZeteticApplication.getInstance().getDatabasePath(ONE_X_DATABASE);
48+
InputStream sourceDatabase = ZeteticApplication.getInstance().getAssets().open(fileName);
49+
File destinationPath = ZeteticApplication.getInstance().getDatabasePath(fileName);
4950
OutputStream destination = new FileOutputStream(destinationPath);
5051

5152
byte[] buffer = new byte[4096];

src/main/java/net/zetetic/tests/AttachExistingDatabaseTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class AttachExistingDatabaseTest extends SQLCipherTest {
1313
public boolean execute(SQLiteDatabase database) {
1414

1515
try {
16-
ZeteticApplication.getInstance().extract1xDatabaseToDatabaseDirectory();
16+
ZeteticApplication.getInstance().extractAssetToDatabaseDirectory(ZeteticApplication.ONE_X_DATABASE);
1717
File other = ZeteticApplication.getInstance().getDatabasePath(ZeteticApplication.ONE_X_DATABASE);
1818
String otherPath = other.getAbsolutePath();
1919
String attach = String.format("attach database ? as other key ?");
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package net.zetetic.tests;
2+
3+
import android.database.Cursor;
4+
import net.sqlcipher.database.SQLiteDatabase;
5+
import net.zetetic.ZeteticApplication;
6+
7+
import java.io.File;
8+
import java.io.IOException;
9+
10+
public class ImportUnencryptedDatabaseTest extends SQLCipherTest {
11+
12+
@Override
13+
public boolean execute(SQLiteDatabase database) {
14+
15+
File unencryptedDatabase = ZeteticApplication.getInstance().getDatabasePath("unencrypted.db");
16+
File encryptedDatabase = ZeteticApplication.getInstance().getDatabasePath("encrypted.db");
17+
18+
try {
19+
ZeteticApplication.getInstance().extractAssetToDatabaseDirectory("unencrypted.db");
20+
21+
database.close();
22+
database = SQLiteDatabase.openOrCreateDatabase(unencryptedDatabase, "", null);
23+
database.rawExecSQL(String.format("ATTACH DATABASE '%s' AS encrypted KEY '%s'",
24+
encryptedDatabase.getAbsolutePath(), ZeteticApplication.DATABASE_PASSWORD));
25+
database.rawExecSQL("select sqlcipher_export('encrypted')");
26+
database.rawExecSQL("DETACH DATABASE encrypted");
27+
database.close();
28+
29+
database = SQLiteDatabase.openOrCreateDatabase(encryptedDatabase, ZeteticApplication.DATABASE_PASSWORD,
30+
null);
31+
Cursor cursor = database.rawQuery("select * from t1", new String[]{});
32+
cursor.moveToFirst();
33+
String a = cursor.getString(0);
34+
String b = cursor.getString(1);
35+
cursor.close();
36+
database.close();
37+
38+
return a.equals("one for the money") &&
39+
b.equals("two for the show");
40+
41+
} catch (IOException e) {
42+
return false;
43+
}
44+
finally {
45+
unencryptedDatabase.delete();
46+
encryptedDatabase.delete();
47+
}
48+
}
49+
50+
@Override
51+
public String getName() {
52+
return "Import Unencrypted Database Test";
53+
}
54+
}

src/main/java/net/zetetic/tests/MigrationFromDatabaseFormat1To2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public boolean execute(SQLiteDatabase database) {
1313

1414
try {
1515
String password = ZeteticApplication.DATABASE_PASSWORD;
16-
ZeteticApplication.getInstance().extract1xDatabaseToDatabaseDirectory();
16+
ZeteticApplication.getInstance().extractAssetToDatabaseDirectory(ZeteticApplication.ONE_X_DATABASE);
1717
File sourceDatabase = ZeteticApplication.getInstance().getDatabasePath(ZeteticApplication.ONE_X_DATABASE);
1818
SQLiteDatabase.upgradeDatabaseFormatFromVersion1To2(sourceDatabase, password);
1919

src/main/java/net/zetetic/tests/TestSuiteRunner.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ private List<SQLCipherTest> getTestsToRun(){
5353
tests.add(new MigrationFromDatabaseFormat1To2());
5454
tests.add(new StatusMemoryUsedTest());
5555
tests.add(new PragmaCipherVersionTest());
56+
tests.add(new ImportUnencryptedDatabaseTest());
5657
return tests;
5758
}
5859
}

0 commit comments

Comments
 (0)