Skip to content

Fix MultiThreadReadWriteTest, testing with closed db & corrupt db, and other minor fixes #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package="net.zetetic" android:versionCode="1" android:versionName="1.0-SNAPSHOT">
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="21"/>
<application android:icon="@drawable/icon" android:label="@string/app_name" android:name="ZeteticApplication">
<activity android:name=".TestSuiteActivity">
<activity android:name=".TestSuiteActivity" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
Expand Down
Binary file added assets/corrupt.db
Binary file not shown.
3 changes: 1 addition & 2 deletions src/main/java/net/zetetic/tests/AttachDatabaseTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package net.zetetic.tests;


import net.sqlcipher.database.SQLiteDatabase;
import net.zetetic.QueryHelper;

import net.zetetic.ZeteticApplication;

import javax.management.Query;
import java.io.File;

public class AttachDatabaseTest extends SQLCipherTest {
Expand Down
335 changes: 335 additions & 0 deletions src/main/java/net/zetetic/tests/ClosedDatabaseTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,335 @@
package net.zetetic.tests;

import android.util.Log;

import java.io.File;
import java.io.IOException;

import java.util.Locale;

import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteException;
import net.zetetic.ZeteticApplication;

public class ClosedDatabaseTest extends SQLCipherTest {

@Override
public TestResult run() {

TestResult result = new TestResult(getName(), false);
try {
result.setResult(execute(null));
SQLiteDatabase.releaseMemory();
} catch (Exception e) {
Log.v(ZeteticApplication.TAG, e.toString());
}
return result;
}

@Override
public boolean execute(SQLiteDatabase null_database_ignored) {

File testDatabasePath = ZeteticApplication.getInstance().getDatabasePath("closed-db-test.db");

boolean status = false;

try {
SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(testDatabasePath, "", null);

database.close();

status = execute_closed_database_tests(database);
} catch (Exception e) {
// Uncaught [unexpected] exception:
Log.e(ZeteticApplication.TAG, "Unexpected exception", e);
return false;
}
finally {
testDatabasePath.delete();
}

return status;
}

boolean execute_closed_database_tests(SQLiteDatabase database) {
try {
/* operations that check if db is closed (and throw IllegalStateException): */
try {
// should throw IllegalStateException:
database.beginTransaction();

// should not get here:
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.beginTransaction() did NOT throw exception on closed database");
return false;
} catch (IllegalStateException e) {
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.beginTransaction() did throw exception on closed database OK", e);
}

try {
// should throw IllegalStateException:
database.endTransaction();

// should not get here:
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.endTransaction() did NOT throw exception on closed database");
return false;
} catch (IllegalStateException e) {
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.endTransaction() did throw exception on closed database OK", e);
}

try {
// should throw IllegalStateException:
database.setTransactionSuccessful();

// should not get here:
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.setTransactionSuccessful() did NOT throw throw exception on closed database");
return false;
} catch (IllegalStateException e) {
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.setTransactionSuccessful() did throw exception on closed database OK", e);
}

try {
// should throw IllegalStateException:
database.getVersion();

// should not get here:
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.getVersion() did NOT throw exception on closed database");
return false;
} catch (IllegalStateException e) {
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.getVersion() did throw exception on closed database OK", e);
}

try {
// should throw IllegalStateException:
database.setVersion(111);

// should not get here:
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.setVersion() did NOT throw exception on closed database");
return false;
} catch (IllegalStateException e) {
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.setVersion() did throw exception on closed database OK", e);
}

try {
// should throw IllegalStateException:
database.getMaximumSize();

// should not get here:
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.getMaximumSize() did NOT throw exception on closed database");
return false;
} catch (IllegalStateException e) {
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.getMaximumSize() did throw exception on closed database OK", e);
}

try {
// should throw IllegalStateException:
database.setMaximumSize(111);

// should not get here:
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.setMaximumSize() did NOT throw exception on closed database");
return false;
} catch (IllegalStateException e) {
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.setMaximumSize() did throw exception on closed database OK", e);
}

try {
// should throw IllegalStateException:
database.getPageSize();

// should not get here:
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.getPageSize() did NOT throw exception on closed database");
return false;
} catch (IllegalStateException e) {
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.getPageSize() did throw exception on closed database OK", e);
}

try {
// should throw IllegalStateException:
database.setPageSize(111);

// should not get here:
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.setPageSize() did NOT throw exception on closed database");
return false;
} catch (IllegalStateException e) {
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.setPageSize() did throw exception on closed database OK", e);
}

try {
// should throw IllegalStateException:
database.compileStatement("SELECT 1;");

// should not get here:
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.compileStatement() did NOT throw exception on closed database");
return false;
} catch (IllegalStateException e) {
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.compileStatement() did throw exception on closed database OK", e);
}

try {
// should throw IllegalStateException:
database.query("t1", new String[]{"a", "b"}, null, null, null, null, null);

// should not get here:
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.query() did NOT throw exception on closed database");
return false;
} catch (IllegalStateException e) {
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.query() did throw exception on closed database OK", e);
}

// TODO: cover more query functions

try {
// should throw IllegalStateException:
database.execSQL("SELECT 1;");

// should not get here:
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.execSQL(String) did NOT throw exception on closed database");
return false;
} catch (IllegalStateException e) {
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.execSQL(String) did throw exception on closed database OK", e);
}

try {
// should throw IllegalStateException:
database.execSQL("SELECT 1;", new Object[1]);

// should not get here:
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.execSQL(String, Object[]) did NOT throw exception on closed database");
return false;
} catch (IllegalStateException e) {
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.execSQL(String, Object[]) did throw exception on closed database OK", e);
}

try {
// should throw IllegalStateException:
database.rawExecSQL("SELECT 1;");

// should not get here:
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.rawExecSQL() did NOT throw exception on closed database");
return false;
} catch (IllegalStateException e) {
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.rawExecSQL() did throw exception on closed database OK", e);
}

/* operations that do not explicitly check if db is closed
* ([should] throw SQLiteException on a closed database): */

try {
// should throw IllegalStateException:
database.setLocale(Locale.getDefault());

// should not get here:
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.setLocale() did NOT throw exception on closed database");
return false;
} catch (SQLiteException e) {
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.setLocale() did throw exception on closed database OK", e);
}

try {
// [should] throw an exception on a closed database:
database.changePassword("new-password");

// should not get here:
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.changePassword(String) did NOT throw exception on closed database");
return false;
} catch (SQLiteException e) {
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.changePassword(String) did throw exception on closed database OK", e);
}

try {
// [should] throw an exception on a closed database:
database.changePassword("new-password".toCharArray());

// should not get here:
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.changePassword(char []) did NOT throw exception on closed database");
return false;
} catch (SQLiteException e) {
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.changePassword(char []) did throw exception on closed database OK", e);
}

try {
// [should] throw an exception on a closed database:
database.markTableSyncable("aa", "bb");

// should not get here:
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.markTableSyncable(String, String) did NOT throw exception on closed database");
return false;
} catch (SQLiteException e) {
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.markTableSyncable(String, String) did throw exception on closed database OK", e);
}

// TBD SQLiteDatabase.markTableSyncable(String, String, String) does NOT throw exception on closed database:
try {
// NOTE: does not (yet) throw an exception on a closed database:
database.markTableSyncable("aa", "bb", "cc");

// ...
// should not get here:
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.markTableSyncable(String, String, String) did NOT throw exception on closed database");
return false;
} catch (SQLiteException e) {
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.markTableSyncable(String, String, String) did throw exception on closed database OK", e);

// SIGNAL that this test must be updated:
//Log.e(ZeteticApplication.TAG, "BEHAVIOR CHANGED - please update the test");
//return false;
}

try {
// should throw IllegalStateException [since it calls getVersion()]:
database.needUpgrade(111);

// should not get here:
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.needUpgrade() did NOT throw exception on closed database");
return false;
} catch (IllegalStateException e) {
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.needUpgrade() did throw exception on closed database OK", e);
}

/* operations that are NOT expected to throw an exception if the database is closed ([should] not crash) */


/* XXX TODO: these functions should check the db state,
* TBD either throw or simply return false if the db is closed */
database.yieldIfContended();
database.yieldIfContendedSafely();
database.yieldIfContendedSafely(100);

database.setLockingEnabled(false);
database.setLockingEnabled(true);

database.close();

database.isReadOnly();
database.isOpen();

database.isInCompiledSqlCache("SELECT 1;");
database.purgeFromCompiledSqlCache("SELECT 1;");
database.resetCompiledSqlCache();

database.getMaxSqlCacheSize();

try {
// should throw IllegalStateException:
database.setMaxSqlCacheSize(111);

// should not get here:
Log.e(ZeteticApplication.TAG, "SQLiteDatabase.setMaxSqlCacheSize() did NOT throw exception on closed database");
return false;
} catch (IllegalStateException e) {
Log.v(ZeteticApplication.TAG, "SQLiteDatabase.setMaxSqlCacheSize() did throw exception on closed database OK", e);
}

} catch (Exception e) {
// Uncaught [unexpected] exception:
Log.e(ZeteticApplication.TAG, "Unexpected exception", e);
return false;
}

return true;
}

@Override
public String getName() {
return "Closed Database Test";
}
}
Loading