Skip to content

Commit dbc37e6

Browse files
author
Chris Brody
committed
Attempt to write to a database file that is corrupted *after* it was opened, and check state after (expected to be closed)
1 parent f8a0de9 commit dbc37e6

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.database.Cursor;
66

77
import net.sqlcipher.database.SQLiteDatabase;
8+
import net.sqlcipher.database.SQLiteDatabaseCorruptException;
89

910
import net.zetetic.ZeteticApplication;
1011

@@ -42,6 +43,27 @@ public boolean execute(SQLiteDatabase null_database_ignored) {
4243
return false;
4344
}
4445

46+
Cursor cursor = database.rawQuery("select * from sqlite_master;", null);
47+
48+
if (cursor == null) {
49+
Log.e(TAG, "NOT EXPECTED: database.rawQuery() returned null cursor");
50+
return false;
51+
}
52+
53+
// *Should* corrupt the database file that is already open:
54+
ZeteticApplication.getInstance().extractAssetToDatabaseDirectory("corrupt.db");
55+
56+
try {
57+
// Attempt to write to corrupt database file *should* fail:
58+
database.execSQL("CREATE TABLE t1(a,b);");
59+
60+
// NOT EXPECTED to get here:
61+
Log.e(TAG, "NOT EXPECTED: CREATE TABLE succeeded ");
62+
return false;
63+
} catch (SQLiteDatabaseCorruptException ex) {
64+
Log.v(TAG, "Caught SQLiteDatabaseCorruptException as expected OK");
65+
}
66+
4567
database.close();
4668

4769
return true;

0 commit comments

Comments
 (0)