Skip to content

Commit 6e52f70

Browse files
Update tests to run against commercial builds of SQLCipher
1 parent 4c84550 commit 6e52f70

16 files changed

+410
-18
lines changed

app/build.gradle

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,12 @@ dependencies {
2525
//implementation files('libs/sqlcipher.jar')
2626

2727
// For testing local AAR packages:
28-
implementation (name: 'android-database-sqlcipher-4.3.0-release', ext: 'aar')
28+
//implementation (name: 'android-database-sqlcipher-4.3.0-release', ext: 'aar')
2929

3030

3131
// For testing on remote AAR references:
32-
//implementation files('libs/android-database-sqlcipher-4.3.0-release.aar')
33-
//implementation 'net.zetetic:android-database-sqlcipher:4.3.0@aar'
34-
32+
implementation 'net.zetetic:android-database-sqlcipher:4.3.0@aar'
3533
implementation "androidx.sqlite:sqlite:2.0.1"
3634
implementation "androidx.room:room-runtime:2.1.0"
3735
annotationProcessor "androidx.room:room-compiler:2.1.0"
38-
39-
40-
// For testing on remote AAR references:
41-
//implementation 'net.zetetic:android-database-sqlcipher:4.2.0@aar'
4236
}

app/src/main/java/net/zetetic/ZeteticApplication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public void deleteDatabaseFileAndSiblings(String databaseName) {
9292
}
9393
}
9494

95-
private SQLiteDatabaseHook wrapHook(final SQLiteDatabaseHook hook) {
95+
public SQLiteDatabaseHook wrapHook(final SQLiteDatabaseHook hook) {
9696
if (hook == null)
9797
{
9898
return keyHook;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package net.zetetic.tests;
2+
3+
import net.sqlcipher.Cursor;
4+
import net.sqlcipher.database.SQLiteDatabase;
5+
6+
public class DefaultCursorWindowAllocationTest extends SQLCipherTest {
7+
@Override
8+
public boolean execute(SQLiteDatabase database) {
9+
try {
10+
int rowCount = 0;
11+
int rows = 10000;
12+
//final int dataSize = 2048;
13+
final int dataSize = 16384;
14+
buildDatabase(database, rows, 1, new RowColumnValueBuilder() {
15+
@Override
16+
public Object buildRowColumnValue(String[] columns, int row, int column) {
17+
return generateRandomByteArray(dataSize);
18+
}
19+
});
20+
rows = 1;
21+
Cursor cursor = database.rawQuery("SELECT count(length(a)) FROM t1;", new Object[]{});
22+
if(cursor == null) return false;
23+
while(cursor.moveToNext()){
24+
//byte[] data = cursor.getBlob(0);
25+
int size = cursor.getInt(0);
26+
cursor.close();
27+
// if(data.length != dataSize) {
28+
// cursor.close();
29+
// return false;
30+
// }
31+
rowCount++;
32+
}
33+
cursor.close();
34+
return rowCount == rows;
35+
} catch (Exception e){
36+
String message = String.format("Error:%s", e.getMessage());
37+
log(message);
38+
setMessage(message);
39+
return false;
40+
}
41+
}
42+
43+
@Override
44+
public String getName() {
45+
return null;
46+
}
47+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package net.zetetic.tests;
2+
3+
import net.sqlcipher.database.SQLiteDatabase;
4+
5+
public class DeleteTableWithNullWhereArgsTest extends SQLCipherTest {
6+
@Override
7+
public boolean execute(SQLiteDatabase database) {
8+
String[] args = null;
9+
int rowsDeleted = 0;
10+
database.rawExecSQL("create table t1(a,b);");
11+
database.execSQL("insert into t1(a,b) values(?, ?);", new Object[]{1, 2});
12+
rowsDeleted = database.delete("t1", "a = 1", args);
13+
return rowsDeleted == 1;
14+
}
15+
16+
@Override
17+
public String getName() {
18+
return "Delete Table Test";
19+
}
20+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
public class JavaClientLibraryVersionTest extends SQLCipherTest {
66

7-
String expectedClientLibraryVersion = "4.2.0";
7+
String expectedClientLibraryVersion = "4.3.0";
88

99
@Override
1010
public boolean execute(SQLiteDatabase database) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
public class PragmaCipherVersionTest extends SQLCipherTest {
88

9-
private final String CURRENT_CIPHER_VERSION = "4.2.0";
9+
private final String CURRENT_CIPHER_VERSION = "4.3.0";
1010

1111
@Override
1212
public boolean execute(SQLiteDatabase database) {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ protected void buildDatabase(SQLiteDatabase database, int rows, int columns, Row
115115
String insert = String.format(insertTemplate, insertBuilder.toString());
116116
database.execSQL("DROP TABLE IF EXISTS t1;");
117117
database.execSQL(create);
118+
database.execSQL("BEGIN;");
118119
String[] names = columnNames.toArray(new String[0]);
119120
for (int row = 0; row < rows; row++) {
120121
Object[] insertArgs = new Object[columns];
@@ -123,6 +124,7 @@ protected void buildDatabase(SQLiteDatabase database, int rows, int columns, Row
123124
}
124125
database.execSQL(insert, insertArgs);
125126
}
127+
database.execSQL("COMMIT;");
126128
Log.i(TAG, String.format("Database built with %d columns, %d rows", columns, rows));
127129
}
128130

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package net.zetetic.tests;
2+
3+
import net.sqlcipher.Cursor;
4+
import net.sqlcipher.database.SQLiteDatabase;
5+
import net.sqlcipher.database.SQLiteStatement;
6+
7+
import java.util.UUID;
8+
9+
public class SimpleQueryTest extends SQLCipherTest {
10+
@Override
11+
public boolean execute(SQLiteDatabase database) {
12+
database.execSQL("create table t1(a,b);");
13+
database.execSQL("insert into t1(a, b) values(?, ?);",
14+
new Object[]{UUID.randomUUID().toString(), "foo"});
15+
database.execSQL("insert into t1(a, b) values(?, ?);",
16+
new Object[]{UUID.randomUUID().toString(), "bar"});
17+
int count = 0;
18+
Cursor cursor = database.rawQuery("select * from t1 where a = ?1 or ?1 = -1;",
19+
new Object[]{-1});
20+
if(cursor != null){
21+
while (cursor.moveToNext()){
22+
count++;
23+
}
24+
cursor.close();
25+
}
26+
27+
// Works
28+
// SQLiteStatement statement = database.compileStatement("select count(*) from t1 where a = ?1 or ?1 = -1;");
29+
// statement.
30+
// statement.bindLong(1, -1);
31+
// long count = statement.simpleQueryForLong();
32+
// statement.close();
33+
return count == 2;
34+
}
35+
36+
@Override
37+
public String getName() {
38+
return "Simple Query Test";
39+
}
40+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ private void runSuite() {
7171

7272
private List<SQLCipherTest> getTestsToRun() {
7373
List<SQLCipherTest> tests = new ArrayList<>();
74+
tests.add(new SimpleQueryTest());
75+
//tests.add(new DefaultCursorWindowAllocationTest());
76+
77+
//tests.add(new DeleteTableWithNullWhereArgsTest());
7478
tests.add(new LoopingInsertTest());
7579
tests.add(new FIPSTest());
7680
tests.add(new PragmaCipherVersionTest());
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package net.zetetic.tests.support;
2+
3+
import android.annotation.SuppressLint;
4+
import android.app.Activity;
5+
6+
import androidx.annotation.NonNull;
7+
import androidx.room.Dao;
8+
import androidx.room.Database;
9+
import androidx.room.Entity;
10+
import androidx.room.ForeignKey;
11+
import androidx.room.Index;
12+
import androidx.room.Insert;
13+
import androidx.room.PrimaryKey;
14+
import androidx.room.Query;
15+
import androidx.room.Room;
16+
import androidx.room.RoomDatabase;
17+
18+
import net.zetetic.ZeteticApplication;
19+
import net.zetetic.tests.TestResult;
20+
21+
import java.io.File;
22+
import java.util.ArrayList;
23+
import java.util.List;
24+
import java.util.UUID;
25+
26+
public class DecryptedRoomTest implements ISupportTest {
27+
private static final String DB_NAME = "room.db";
28+
29+
private final Activity activity;
30+
31+
@Entity()
32+
public static class ParentEntity {
33+
@PrimaryKey(autoGenerate = true) long id;
34+
int intValue;
35+
float floatValue;
36+
double doubleValue;
37+
char charValue;
38+
boolean boolValue;
39+
}
40+
41+
@Entity(foreignKeys = @ForeignKey(entity = ParentEntity.class,
42+
parentColumns = "id", childColumns = "parentId", onDelete = ForeignKey.CASCADE),
43+
indices = @Index("parentId"))
44+
public static class ChildEntity {
45+
@PrimaryKey @NonNull String uuid;
46+
String stringValue;
47+
long parentId;
48+
}
49+
50+
@Dao
51+
public static abstract class TestDao {
52+
@Query("SELECT * FROM ChildEntity WHERE parentId = :parentId or :parentId = -1")
53+
abstract List<ChildEntity> getAllChildrenForParent(long parentId);
54+
55+
@Query("SELECT * FROM ChildEntity")
56+
abstract List<ChildEntity> getAllChildren();
57+
58+
@Insert
59+
abstract void insert(List<ChildEntity> entities);
60+
61+
@Insert
62+
abstract long insert(ParentEntity entity);
63+
}
64+
65+
@Database(entities = {ParentEntity.class, ChildEntity.class}, version = 1)
66+
public static abstract class TestDatabase extends RoomDatabase {
67+
abstract TestDao testDao();
68+
}
69+
70+
public DecryptedRoomTest(Activity activity) {
71+
this.activity = activity;
72+
}
73+
74+
@SuppressLint("DefaultLocale")
75+
public TestResult run() {
76+
File dbFile = ZeteticApplication.getInstance().getDatabasePath(DB_NAME);
77+
78+
if (dbFile.exists()){
79+
dbFile.delete();
80+
}
81+
82+
final TestResult result = new TestResult(getName(), false);
83+
final TestDatabase room = Room.databaseBuilder(activity, TestDatabase.class, DB_NAME)
84+
.build();
85+
ParentEntity parent = new ParentEntity();
86+
87+
parent.boolValue = true;
88+
parent.charValue = 'x';
89+
parent.intValue = 1337;
90+
parent.doubleValue = 3.14159;
91+
parent.floatValue = 2.71828f;
92+
parent.id = room.testDao().insert(parent);
93+
94+
result.setResult(true);
95+
96+
ChildEntity firstChild = new ChildEntity();
97+
98+
firstChild.uuid = UUID.randomUUID().toString();
99+
firstChild.stringValue = "Um, hi!";
100+
firstChild.parentId = parent.id;
101+
102+
ChildEntity secondChild = new ChildEntity();
103+
104+
secondChild.uuid = UUID.randomUUID().toString();
105+
secondChild.stringValue = "And now for something completely different";
106+
secondChild.parentId = parent.id;
107+
108+
List<ChildEntity> children = new ArrayList<>();
109+
110+
children.add(firstChild);
111+
children.add(secondChild);
112+
113+
room.testDao().insert(children);
114+
115+
List<ChildEntity> allChildren = room.testDao().getAllChildren();
116+
List<ChildEntity> allChildrenNegativeOne = room.testDao().getAllChildrenForParent(-1);
117+
List<ChildEntity> other = room.testDao().getAllChildrenForParent( parent.id);
118+
119+
//the query should have returned both the list with same sizes here
120+
if (allChildren.size() != allChildrenNegativeOne.size()) {
121+
result.setResult(false);
122+
result.setMessage(String.format("expected all children, found %d from entity and %d from main query", allChildrenNegativeOne.size(), allChildren.size()));
123+
return result;
124+
}
125+
126+
return result;
127+
}
128+
129+
@Override
130+
public String getName() {
131+
return "Decrypted Room Test";
132+
}
133+
}

0 commit comments

Comments
 (0)