Skip to content

Commit 8237188

Browse files
committed
add more tests; beep when tests complete
1 parent d3e0670 commit 8237188

9 files changed

+368
-17
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package net.zetetic.tests.support;
2+
3+
import net.sqlcipher.database.SQLiteDatabase;
4+
import net.zetetic.tests.SQLCipherTest;
5+
6+
public class CheckIsDatabaseIntegrityOkTest extends SupportTest {
7+
@Override
8+
public boolean execute(SQLiteDatabase database) {
9+
return database.isDatabaseIntegrityOk();
10+
}
11+
12+
@Override
13+
public String getName() {
14+
return "Check Database Integrity";
15+
}
16+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package net.zetetic.tests.support;
2+
3+
import net.sqlcipher.Cursor;
4+
import net.sqlcipher.database.SQLiteDatabase;
5+
import net.zetetic.ZeteticApplication;
6+
import net.zetetic.tests.SQLCipherTest;
7+
import java.io.File;
8+
9+
public class CreateOpenDatabaseWithByteArrayTest extends SupportTest {
10+
11+
private String databaseName = "foo.db";
12+
13+
@Override
14+
public boolean execute(SQLiteDatabase database) {
15+
boolean status = false;
16+
database.close();
17+
byte[] key = generateRandomByteArray(32);
18+
File newDatabasePath = ZeteticApplication.getInstance().getDatabasePath(databaseName);
19+
newDatabasePath.delete();
20+
database = SQLiteDatabase.openOrCreateDatabase(newDatabasePath.getPath(), key, null);
21+
database.execSQL("create table t1(a,b);");
22+
database.execSQL("insert into t1(a,b) values(?, ?);", new Object[]{1, 2});
23+
database.close();
24+
database = SQLiteDatabase.openOrCreateDatabase(newDatabasePath.getPath(), key, null);
25+
Cursor cursor = database.rawQuery("select * from t1;", null);
26+
if (cursor != null) {
27+
cursor.moveToNext();
28+
int a = cursor.getInt(0);
29+
int b = cursor.getInt(1);
30+
cursor.close();
31+
status = a == 1 && b == 2;
32+
}
33+
return status;
34+
}
35+
36+
@Override
37+
public String getName() {
38+
return "Create/Open with Byte Array Test";
39+
}
40+
41+
@Override
42+
protected void tearDown(SQLiteDatabase database) {
43+
super.tearDown(database);
44+
File newDatabasePath = ZeteticApplication.getInstance().getDatabasePath(databaseName);
45+
newDatabasePath.delete();
46+
}
47+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package net.zetetic.tests.support;
2+
3+
import net.sqlcipher.Cursor;
4+
import net.sqlcipher.CursorWindow;
5+
import net.sqlcipher.CursorWindowAllocation;
6+
import net.sqlcipher.CustomCursorWindowAllocation;
7+
import net.sqlcipher.database.SQLiteDatabase;
8+
import net.zetetic.tests.RowColumnValueBuilder;
9+
import net.zetetic.tests.SQLCipherTest;
10+
11+
public class FixedCursorWindowAllocationTest extends SupportTest {
12+
13+
@Override
14+
public boolean execute(SQLiteDatabase database) {
15+
try {
16+
int rowCount = 0;
17+
int rows = 100;
18+
long allocationSize = 1024;
19+
final int dataSize = 491;
20+
CursorWindowAllocation fixedAllocation = new CustomCursorWindowAllocation(allocationSize, 0, allocationSize);
21+
CursorWindow.setCursorWindowAllocation(fixedAllocation);
22+
buildDatabase(database, rows, 1, new RowColumnValueBuilder() {
23+
@Override
24+
public Object buildRowColumnValue(String[] columns, int row, int column) {
25+
return generateRandomByteArray(dataSize);
26+
}
27+
});
28+
29+
Cursor cursor = database.rawQuery("SELECT * FROM t1;", new Object[]{});
30+
if(cursor == null) return false;
31+
while(cursor.moveToNext()){
32+
byte[] data = cursor.getBlob(0);
33+
if(data.length != dataSize) {
34+
cursor.close();
35+
return false;
36+
}
37+
rowCount++;
38+
}
39+
cursor.close();
40+
return rowCount == rows;
41+
} catch (Exception e){
42+
String message = String.format("Error:%s", e.getMessage());
43+
log(message);
44+
setMessage(message);
45+
return false;
46+
}
47+
}
48+
49+
@Override
50+
public String getName() {
51+
return "Small Cursor Window Allocation Test";
52+
}
53+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package net.zetetic.tests.support;
2+
3+
import android.util.Pair;
4+
import net.sqlcipher.database.SQLiteDatabase;
5+
import net.zetetic.ZeteticApplication;
6+
import net.zetetic.tests.SQLCipherTest;
7+
import java.io.File;
8+
import java.util.List;
9+
import java.util.UUID;
10+
11+
public class GetAttachedDatabasesTest extends SupportTest {
12+
@Override
13+
public boolean execute(SQLiteDatabase database) {
14+
UUID id = UUID.randomUUID();
15+
File databasePath = ZeteticApplication.getInstance().getDatabasePath(id.toString());
16+
List<Pair<String, String>> attached = database.getAttachedDbs();
17+
boolean initialAttach = attached.size() == 1 && attached.get(0).first.equals("main");
18+
database.execSQL("ATTACH database ? as foo;",
19+
new Object[]{databasePath.getAbsolutePath()});
20+
attached = database.getAttachedDbs();
21+
return initialAttach && attached.size() == 2;
22+
}
23+
24+
@Override
25+
public String getName() {
26+
return "Get Attached Databases Test";
27+
}
28+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package net.zetetic.tests.support;
2+
3+
import net.sqlcipher.Cursor;
4+
import net.sqlcipher.CursorWindow;
5+
import net.sqlcipher.CursorWindowAllocation;
6+
import net.sqlcipher.CustomCursorWindowAllocation;
7+
import net.sqlcipher.database.SQLiteDatabase;
8+
import net.zetetic.tests.RowColumnValueBuilder;
9+
import net.zetetic.tests.SQLCipherTest;
10+
11+
public class GrowingCursorWindowAllocationTest extends SupportTest {
12+
@Override
13+
public boolean execute(SQLiteDatabase database) {
14+
try {
15+
int rowCount = 0;
16+
int rows = 20000;
17+
long intialAllocationSize = 128 * 1024;
18+
long growthAllocationSize = 1024 * 1024;
19+
long maxAllocationSize = 4 * 1024 * 1024;
20+
final int dataSize = 2000;
21+
CursorWindowAllocation fixedAllocation =
22+
new CustomCursorWindowAllocation(intialAllocationSize, growthAllocationSize, maxAllocationSize);
23+
CursorWindow.setCursorWindowAllocation(fixedAllocation);
24+
buildDatabase(database, rows, 1, new RowColumnValueBuilder() {
25+
@Override
26+
public Object buildRowColumnValue(String[] columns, int row, int column) {
27+
return generateRandomByteArray(dataSize);
28+
}
29+
});
30+
31+
Cursor cursor = database.rawQuery("SELECT * FROM t1;", new Object[]{});
32+
if(cursor == null) return false;
33+
while(cursor.moveToNext()){
34+
byte[] data = cursor.getBlob(0);
35+
if(data.length != dataSize) {
36+
cursor.close();
37+
return false;
38+
}
39+
rowCount++;
40+
}
41+
cursor.close();
42+
return rowCount == rows;
43+
} catch (Exception e){
44+
String message = String.format("Error:%s", e.getMessage());
45+
log(message);
46+
setMessage(message);
47+
return false;
48+
}
49+
}
50+
51+
@Override
52+
public String getName() {
53+
return "Growing Cursor Window Allocation Test";
54+
}
55+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package net.zetetic.tests.support;
2+
3+
import net.sqlcipher.Cursor;
4+
import net.sqlcipher.database.SQLiteDatabase;
5+
import net.zetetic.tests.SQLCipherTest;
6+
7+
public class ReadWriteWriteAheadLoggingTest extends SupportTest {
8+
@Override
9+
public boolean execute(final SQLiteDatabase database) {
10+
11+
try {
12+
final int[] a = new int[1];
13+
final int[] b = new int[1];
14+
database.setLockingEnabled(false);
15+
boolean walEnabled = database.enableWriteAheadLogging();
16+
if (!walEnabled) return false;
17+
18+
//database.execSQL("PRAGMA read_uncommitted = 1;");
19+
20+
database.execSQL("CREATE TABLE t1(a,b)");
21+
database.rawQuery("INSERT INTO t1(a,b) VALUES(?,?);", new Object[]{1, 2});
22+
database.beginTransaction();
23+
//database.beginTransactionNonExclusive();
24+
database.rawQuery("DELETE FROM t1 WHERE a = ?;", new Object[]{1});
25+
Thread t = new Thread(new Runnable() {
26+
@Override
27+
public void run() {
28+
Cursor cursor = database.rawQuery("SELECT COUNT(*) FROM t1 WHERE a = ?;", new Object[]{1});
29+
if (cursor != null && cursor.moveToFirst()) {
30+
a[0] = cursor.getInt(0);
31+
b[0] = cursor.getInt(0);
32+
log(String.format("Retrieved %d rows back", a[0]));
33+
}
34+
}
35+
});
36+
t.start();
37+
t.join();
38+
database.setTransactionSuccessful();
39+
database.endTransaction();
40+
//return a[0] == 1 && b[0] == 2;
41+
return a[0] == 0 && b[0] == 0;
42+
} catch (InterruptedException ex){
43+
return false;
44+
}
45+
}
46+
47+
@Override
48+
public String getName() {
49+
return "Read/Write WAL Test";
50+
}
51+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package net.zetetic.tests.support;
2+
3+
import android.content.Context;
4+
import net.sqlcipher.database.SQLiteDatabase;
5+
import net.sqlcipher.database.SQLiteOpenHelper;
6+
import net.zetetic.ZeteticApplication;
7+
import net.zetetic.tests.SQLCipherTest;
8+
import java.io.File;
9+
import java.util.UUID;
10+
11+
public class SQLiteOpenHelperGetNameTest extends SupportTest {
12+
@Override
13+
public boolean execute(SQLiteDatabase database) {
14+
UUID name = UUID.randomUUID();
15+
File databasePath = ZeteticApplication.getInstance().getDatabasePath(name.toString());
16+
DatabaseHelper helper = new DatabaseHelper(ZeteticApplication.getInstance(),
17+
databasePath.getAbsolutePath());
18+
return databasePath.getAbsolutePath().equals(helper.getDatabaseName());
19+
}
20+
21+
@Override
22+
public String getName() {
23+
return "SQLiteOpenHelper GetName Test";
24+
}
25+
26+
class DatabaseHelper extends SQLiteOpenHelper {
27+
28+
public DatabaseHelper(Context context, String databasePath) {
29+
super(context, databasePath, null, 1);
30+
}
31+
32+
@Override
33+
public void onCreate(SQLiteDatabase database) { }
34+
35+
@Override
36+
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion){}
37+
}
38+
39+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package net.zetetic.tests.support;
2+
3+
import android.content.Context;
4+
import net.sqlcipher.Cursor;
5+
import net.sqlcipher.database.SQLiteDatabase;
6+
import net.sqlcipher.database.SQLiteOpenHelper;
7+
import net.zetetic.ZeteticApplication;
8+
import net.zetetic.tests.SQLCipherTest;
9+
import java.io.File;
10+
11+
public class SQLiteOpenHelperWithByteArrayKeyTest extends SupportTest {
12+
13+
14+
private String databaseName = "foo.db";
15+
16+
@Override
17+
public boolean execute(SQLiteDatabase database) {
18+
boolean status = false;
19+
database.close();
20+
byte[] key = generateRandomByteArray(32);
21+
File databasePath = ZeteticApplication.getInstance().getDatabasePath(databaseName);
22+
SQLiteOpenHelper helper = new TestHelper(ZeteticApplication.getInstance(), databasePath.getPath());
23+
database = helper.getWritableDatabase(key);
24+
database.execSQL("insert into t1(a,b) values(?, ?);", new Object[]{1, 2});
25+
database.close();
26+
helper.close();
27+
28+
helper = new TestHelper(ZeteticApplication.getInstance(), databasePath.getPath());
29+
database = helper.getWritableDatabase(key);
30+
Cursor cursor = database.rawQuery("select * from t1;", null);
31+
if (cursor != null) {
32+
cursor.moveToNext();
33+
int a = cursor.getInt(0);
34+
int b = cursor.getInt(1);
35+
cursor.close();
36+
status = a == 1 && b == 2;
37+
}
38+
return status;
39+
}
40+
41+
@Override
42+
public String getName() {
43+
return "SQLiteOpenHelper with Byte Array Key";
44+
}
45+
46+
private class TestHelper extends SQLiteOpenHelper {
47+
48+
private static final int version = 1;
49+
50+
public TestHelper(Context context, String databasePath){
51+
super(context, databaseName, null, version, null);
52+
}
53+
54+
public TestHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
55+
super(context, databaseName, factory, version);
56+
}
57+
58+
@Override
59+
public void onCreate(SQLiteDatabase database) {
60+
database.execSQL("create table t1(a,b);");
61+
}
62+
63+
@Override
64+
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
65+
}
66+
}
67+
68+
}

0 commit comments

Comments
 (0)