|
| 1 | +package net.zetetic.tests; |
| 2 | + |
| 3 | +import android.util.Log; |
| 4 | + |
| 5 | +import android.database.Cursor; |
| 6 | + |
| 7 | +import net.sqlcipher.database.SQLiteDatabase; |
| 8 | + |
| 9 | +import net.zetetic.ZeteticApplication; |
| 10 | + |
| 11 | +import java.io.File; |
| 12 | +import java.io.IOException; |
| 13 | + |
| 14 | +public class CorruptDatabaseTest extends SQLCipherTest { |
| 15 | + |
| 16 | + @Override |
| 17 | + public TestResult run() { |
| 18 | + |
| 19 | + TestResult result = new TestResult(getName(), false); |
| 20 | + try { |
| 21 | + result.setResult(execute(null)); |
| 22 | + SQLiteDatabase.releaseMemory(); |
| 23 | + } catch (Exception e) { |
| 24 | + Log.v(ZeteticApplication.TAG, e.toString()); |
| 25 | + } |
| 26 | + return result; |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public boolean execute(SQLiteDatabase null_database_ignored) { |
| 31 | + |
| 32 | + File unencryptedDatabase = ZeteticApplication.getInstance().getDatabasePath("corrupt.db"); |
| 33 | + |
| 34 | + try { |
| 35 | + ZeteticApplication.getInstance().extractAssetToDatabaseDirectory("corrupt.db"); |
| 36 | + |
| 37 | + SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(unencryptedDatabase, "", null); |
| 38 | + |
| 39 | + // NOTE: database not expected to be null, but check: |
| 40 | + if (database == null) { |
| 41 | + Log.e(TAG, "ERROR: got null database object"); |
| 42 | + return false; |
| 43 | + } |
| 44 | + |
| 45 | + database.close(); |
| 46 | + |
| 47 | + return true; |
| 48 | + } catch (Exception ex) { |
| 49 | + // Uncaught exception (not expected): |
| 50 | + Log.e(TAG, "UNEXPECTED EXCEPTION", ex); |
| 51 | + return false; |
| 52 | + } |
| 53 | + finally { |
| 54 | + unencryptedDatabase.delete(); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public String getName() { |
| 60 | + return "Corrupt Database Test"; |
| 61 | + } |
| 62 | +} |
0 commit comments