Skip to content

Commit 5fcef62

Browse files
author
Chris Brody
committed
Testing of (almost all) public SQLiteDatabase functions on a database that was never opened in custom DatabaseErrorHandler, near the beginning of the test suite
1 parent 3dd4ad3 commit 5fcef62

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

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

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import java.util.Locale;
99

10+
import net.sqlcipher.DatabaseErrorHandler;
1011
import net.sqlcipher.database.SQLiteDatabase;
1112
import net.sqlcipher.database.SQLiteException;
1213
import net.zetetic.ZeteticApplication;
@@ -29,12 +30,12 @@ public TestResult run() {
2930
@Override
3031
public boolean execute(SQLiteDatabase null_database_ignored) {
3132

32-
File testDatabasePath = ZeteticApplication.getInstance().getDatabasePath("closed-db-test.db");
33+
File closedDatabasePath = ZeteticApplication.getInstance().getDatabasePath("closed-db-test.db");
3334

3435
boolean status = false;
3536

3637
try {
37-
SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(testDatabasePath, "", null);
38+
SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(closedDatabasePath, "", null);
3839

3940
database.close();
4041

@@ -45,7 +46,58 @@ public boolean execute(SQLiteDatabase null_database_ignored) {
4546
return false;
4647
}
4748
finally {
48-
testDatabasePath.delete();
49+
closedDatabasePath.delete();
50+
}
51+
52+
if (!status) return false;
53+
54+
status = false;
55+
56+
final File corruptDatabase = ZeteticApplication.getInstance().getDatabasePath("corrupt.db");
57+
58+
// run closed database tests again in custom DatabaseErrorHandler:
59+
try {
60+
ZeteticApplication.getInstance().extractAssetToDatabaseDirectory("corrupt.db");
61+
62+
// ugly trick from: http://stackoverflow.com/questions/5977735/setting-outer-variable-from-anonymous-inner-class
63+
final boolean[] inner_status_slot = new boolean[1];
64+
65+
// on a database object that was NEVER actually opened (due to a corrupt database file):
66+
SQLiteDatabase database = SQLiteDatabase.openDatabase(corruptDatabase.getPath(), "", null, SQLiteDatabase.CREATE_IF_NECESSARY, null, new DatabaseErrorHandler() {
67+
@Override
68+
public void onCorruption(SQLiteDatabase db) {
69+
try {
70+
inner_status_slot[0] = execute_closed_database_tests(db);
71+
} catch (Exception ex) {
72+
// Uncaught exception (not expected):
73+
Log.e(TAG, "UNEXPECTED EXCEPTION", ex);
74+
}
75+
76+
try {
77+
corruptDatabase.delete();
78+
} catch (Exception ex) {
79+
// Uncaught exception (not expected):
80+
Log.e(TAG, "UNEXPECTED EXCEPTION", ex);
81+
}
82+
}
83+
});
84+
85+
status = inner_status_slot[0];
86+
87+
// NOTE: database not expected to be null, but double-check:
88+
if (database == null) {
89+
Log.e(TAG, "ERROR: got null database object");
90+
return false;
91+
}
92+
93+
database.close();
94+
} catch (Exception ex) {
95+
// Uncaught exception (not expected):
96+
Log.e(TAG, "UNEXPECTED EXCEPTION", ex);
97+
return false;
98+
}
99+
finally {
100+
corruptDatabase.delete();
49101
}
50102

51103
return status;
@@ -257,9 +309,8 @@ boolean execute_closed_database_tests(SQLiteDatabase database) {
257309
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.markTableSyncable(String, String) did throw exception on closed database OK", e);
258310
}
259311

260-
// TBD SQLiteDatabase.markTableSyncable(String, String, String) does NOT throw exception on closed database:
261312
try {
262-
// NOTE: does not (yet) throw an exception on a closed database:
313+
// [should] throw an exception on a closed database:
263314
database.markTableSyncable("aa", "bb", "cc");
264315

265316
// ...

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ private List<SQLCipherTest> getTestsToRun(){
4949
List<SQLCipherTest> tests = new ArrayList<SQLCipherTest>();
5050
tests.add(new UnicodeTest());
5151
tests.add(new FIPSTest());
52+
tests.add(new ClosedDatabaseTest());
5253
tests.add(new AttachDatabaseTest());
5354
tests.add(new CipherMigrateTest());
5455
tests.add(new GetTypeFromCrossProcessCursorWrapperTest());
@@ -86,7 +87,6 @@ private List<SQLCipherTest> getTestsToRun(){
8687
tests.add(new RawQueryTest());
8788
tests.add(new OpenReadOnlyDatabaseTest());
8889
tests.add(new RawRekeyTest());
89-
tests.add(new ClosedDatabaseTest());
9090
tests.add(new CorruptDatabaseTest());
9191
tests.add(new CustomCorruptionHandlerTest());
9292
//tests.add(new MultiThreadReadWriteTest());

0 commit comments

Comments
 (0)