Skip to content

Commit bc06747

Browse files
Additional CursorWindowAllocation and data query size tests
1 parent fe6aaf1 commit bc06747

10 files changed

+443
-398
lines changed

.idea/workspace.xml

Lines changed: 209 additions & 391 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
18.6 KB
Binary file not shown.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package net.zetetic.tests;
2+
3+
import net.sqlcipher.Cursor;
4+
import net.sqlcipher.database.SQLiteDatabase;
5+
import net.zetetic.ZeteticApplication;
6+
7+
import java.io.File;
8+
9+
public class AccessDatabaseTest extends SQLCipherTest {
10+
@Override
11+
public boolean execute(SQLiteDatabase database) {
12+
database.close();
13+
try {
14+
int rows = 0;
15+
String filename = "encrypted.db";
16+
ZeteticApplication.getInstance().extractAssetToDatabaseDirectory(filename);
17+
File databasePath = ZeteticApplication.getInstance().getDatabasePath(filename);
18+
database = SQLiteDatabase.openDatabase(databasePath.getAbsolutePath(), "test", null, SQLiteDatabase.OPEN_READWRITE);
19+
if(database != null){
20+
Cursor cursor = database.rawQuery("SELECT COUNT(*) FROM sqlite_master;", new Object[]{});
21+
if(cursor != null){
22+
cursor.moveToFirst();
23+
rows = cursor.getInt(0);
24+
cursor.close();
25+
}
26+
}
27+
return rows > 0;
28+
} catch (Exception e) {
29+
return false;
30+
}
31+
}
32+
33+
@Override
34+
public String getName() {
35+
return "Access Database File Test";
36+
}
37+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package net.zetetic.tests;
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+
9+
public class FixedCursorWindowAllocationTest extends SQLCipherTest {
10+
11+
@Override
12+
public boolean execute(SQLiteDatabase database) {
13+
try {
14+
int rowCount = 0;
15+
int rows = 100;
16+
long allocationSize = 1024;
17+
final int dataSize = 491;
18+
CursorWindowAllocation fixedAllocation = new CustomCursorWindowAllocation(allocationSize, 0, allocationSize);
19+
CursorWindow.setCursorWindowAllocation(fixedAllocation);
20+
buildDatabase(database, rows, 1, new RowColumnValueBuilder() {
21+
@Override
22+
public Object buildRowColumnValue(String[] columns, int row, int column) {
23+
return generateRandomByteArray(dataSize);
24+
}
25+
});
26+
27+
Cursor cursor = database.rawQuery("SELECT * FROM t1;", new Object[]{});
28+
if(cursor == null) return false;
29+
while(cursor.moveToNext()){
30+
byte[] data = cursor.getBlob(0);
31+
if(data.length != dataSize) {
32+
cursor.close();
33+
return false;
34+
}
35+
rowCount++;
36+
}
37+
cursor.close();
38+
return rowCount == rows;
39+
} catch (Exception e){
40+
String message = String.format("Error:%s", e.getMessage());
41+
log(message);
42+
setMessage(message);
43+
return false;
44+
}
45+
}
46+
47+
@Override
48+
public String getName() {
49+
return "Small Cursor Window Allocation Test";
50+
}
51+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package net.zetetic.tests;
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+
9+
public class GrowingCursorWindowAllocationTest extends SQLCipherTest {
10+
@Override
11+
public boolean execute(SQLiteDatabase database) {
12+
try {
13+
int rowCount = 0;
14+
int rows = 20000;
15+
long intialAllocationSize = 128 * 1024;
16+
long growthAllocationSize = 1024 * 1024;
17+
long maxAllocationSize = 4 * 1024 * 1024;
18+
final int dataSize = 2000;
19+
CursorWindowAllocation fixedAllocation =
20+
new CustomCursorWindowAllocation(intialAllocationSize, growthAllocationSize, maxAllocationSize);
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 "Growing Cursor Window Allocation Test";
52+
}
53+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package net.zetetic.tests;
2+
3+
import net.sqlcipher.database.SQLiteDatabase;
4+
import net.sqlcipher.database.SQLiteQueryStats;
5+
6+
public class QueryDataSizeTest extends SQLCipherTest {
7+
@Override
8+
public boolean execute(SQLiteDatabase database) {
9+
database.execSQL("CREATE TABLE t1(a,b);");
10+
database.execSQL("INSERT INTO t1(a,b) VALUES(?, ?);",
11+
new Object[]{generateRandomByteArray(256), generateRandomByteArray(256)});
12+
database.execSQL("INSERT INTO t1(a,b) VALUES(?, ?);",
13+
new Object[]{generateRandomByteArray(1024), generateRandomByteArray(64)});
14+
SQLiteQueryStats result = database.getQueryStats("SELECT * FROM t1;", new Object[]{});
15+
return result.getTotalQueryResultSize() > 0 && result.getLargestIndividualRowSize() > 0;
16+
}
17+
18+
@Override
19+
public String getName() {
20+
return "Query Data Size Test";
21+
}
22+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package net.zetetic.tests;
2+
3+
import net.sqlcipher.Cursor;
4+
import net.sqlcipher.CursorWindow;
5+
import net.sqlcipher.database.SQLiteDatabase;
6+
7+
public class ReadWriteWriteAheadLoggingTest extends SQLCipherTest {
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+
}

app/src/main/java/net/zetetic/tests/SQLCipherTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ protected void buildDatabase(SQLiteDatabase database, int rows, int columns, Row
125125
Log.i(TAG, String.format("Database built with %d columns, %d rows", columns, rows));
126126
}
127127

128+
protected byte[] generateRandomByteArray(int size) {
129+
byte[] data = new byte[size];
130+
random.nextBytes(data);
131+
return data;
132+
}
133+
128134
protected void setUp(){};
129135
protected void tearDown(SQLiteDatabase database){};
130136
protected void createDatabasePreKey(SQLiteDatabase database){};

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
import android.util.Log;
77
import android.view.WindowManager;
88

9+
import net.sqlcipher.CursorWindow;
10+
import net.sqlcipher.CursorWindowAllocation;
911
import net.sqlcipher.database.SQLiteDatabase;
10-
import net.sqlcipher.database.SQLiteDatabaseHook;
1112
import net.zetetic.ZeteticApplication;
1213

1314
import java.util.ArrayList;
@@ -50,8 +51,10 @@ protected void onPostExecute(Void aVoid) {
5051
private void runSuite() {
5152

5253
SQLiteDatabase.loadLibs(ZeteticApplication.getInstance());
54+
CursorWindowAllocation defaultAllocation = CursorWindow.getCursorWindowAllocation();
5355
for (SQLCipherTest test : getTestsToRun()) {
5456
try {
57+
CursorWindow.setCursorWindowAllocation(defaultAllocation);
5558
Log.i(ZeteticApplication.TAG, "Running test:" + test.getName());
5659
TestResult result = test.run();
5760
publishProgress(result);
@@ -60,11 +63,18 @@ private void runSuite() {
6063
Log.i(ZeteticApplication.TAG, e.toString());
6164
publishProgress(new TestResult(test.getName(), false, e.toString()));
6265
}
66+
finally {
67+
CursorWindow.setCursorWindowAllocation(defaultAllocation);
68+
}
6369
}
6470
}
6571

6672
private List<SQLCipherTest> getTestsToRun() {
6773
List<SQLCipherTest> tests = new ArrayList<>();
74+
tests.add(new QueryDataSizeTest());
75+
tests.add(new FixedCursorWindowAllocationTest());
76+
tests.add(new GrowingCursorWindowAllocationTest());
77+
tests.add(new ReadWriteWriteAheadLoggingTest());
6878
tests.add(new SQLiteOpenHelperEnableWriteAheadLogBeforeGetDatabaseTest());
6979
tests.add(new SQLiteOpenHelperEnableWriteAheadLogAfterGetDatabaseTest());
7080
tests.add(new SQLiteOpenHelperGetNameTest());
@@ -83,7 +93,9 @@ private List<SQLCipherTest> getTestsToRun() {
8393
tests.add(new TransactionNonExclusiveTest());
8494
tests.add(new TransactionWithListenerTest());
8595
tests.add(new LargeDatabaseCursorAccessTest());
96+
8697
// tests.add(new TimeLargeByteArrayQueryTest());
98+
8799
tests.add(new QueryLimitTest());
88100
tests.add(new RTreeTest());
89101
tests.add(new ReadWriteDatabaseToExternalStorageTest());
@@ -171,6 +183,7 @@ private List<SQLCipherTest> getTestsToRun() {
171183
tests.add(new BindByteArrayRawQueryTest());
172184
tests.add(new NullRawQueryTest());
173185
tests.add(new ReadWriteDatabaseToExternalStorageTest());
186+
174187
return tests;
175188
}
176189
}

app/src/main/java/net/zetetic/tests/TimeLargeByteArrayQueryTest.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,6 @@ private Stats timeQuery(SQLiteDatabase database, String query, CursorScenario sc
130130
return stats;
131131
}
132132

133-
private byte[] generateRandomByteArray(int size) {
134-
byte[] data = new byte[size];
135-
random.nextBytes(data);
136-
return data;
137-
}
138-
139133
private String bytesToString(long bytes) {
140134
int unit = 1024;
141135
if (bytes < unit) return bytes + " B";

0 commit comments

Comments
 (0)