Skip to content

Commit 61706be

Browse files
Merge branch 'npe-fix-tests' of https://github.com/brodybits/sqlcipher-android-tests into brodybits-npe-fix-tests
2 parents a70c56f + bf44c65 commit 61706be

8 files changed

+533
-11
lines changed
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 android.util.Log;
4+
5+
import net.sqlcipher.database.SQLiteDatabase;
6+
7+
import net.zetetic.QueryHelper;
8+
import net.zetetic.ZeteticApplication;
9+
10+
import java.io.File;
11+
12+
public class ChangePasswordTest extends SQLCipherTest {
13+
14+
File databaseFile = ZeteticApplication.getInstance().getDatabasePath(ZeteticApplication.DATABASE_NAME);
15+
String secondPassword = "second password";
16+
17+
@Override
18+
public boolean execute(SQLiteDatabase database) {
19+
database.execSQL("CREATE TABLE t1(a,b);");
20+
database.execSQL("INSERT INTO t1(a,b) VALUES(?,?)", new Object[]{"one for the money", "two for the show"});
21+
database.changePassword(secondPassword);
22+
database.close();
23+
24+
database = SQLiteDatabase.openOrCreateDatabase(databaseFile, secondPassword, null);
25+
int count = QueryHelper.singleIntegerValueFromQuery(database, "SELECT COUNT(*) FROM t1;");
26+
database.close();
27+
28+
if (count != 1) {
29+
Log.e(ZeteticApplication.TAG, "NOT EXPECTED: incorrect record count");
30+
return false;
31+
}
32+
33+
// Verify that these will not cause a crash:
34+
database = SQLiteDatabase.openOrCreateDatabase(databaseFile, secondPassword, null);
35+
String nullPasswordString = null;
36+
database.changePassword(nullPasswordString);
37+
try {
38+
char[] nullPassword = null;
39+
database.changePassword(nullPassword);
40+
} catch (Exception e) {
41+
Log.e(ZeteticApplication.TAG, "NOT EXPECTED: exception", e);
42+
return false;
43+
}
44+
database.close();
45+
46+
return true;
47+
}
48+
49+
@Override
50+
public String getName() {
51+
return "Change Password Test";
52+
}
53+
}

src/main/java/net/zetetic/tests/CorruptDatabaseTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ public boolean execute(SQLiteDatabase null_database_ignored) {
7171
return false;
7272
}
7373

74+
// Try with a null DatabaseErrorHandler:
75+
ZeteticApplication.getInstance().extractAssetToDatabaseDirectory("corrupt.db");
76+
SQLiteDatabase.openOrCreateDatabase(unencryptedDatabase, "", null, null, null);
77+
7478
return true;
7579
} catch (Exception ex) {
7680
// Uncaught exception (not expected):
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package net.zetetic.tests;
2+
3+
import android.database.Cursor;
4+
5+
import net.sqlcipher.database.SQLiteDatabase;
6+
import net.sqlcipher.database.SQLiteException;
7+
8+
import net.zetetic.ZeteticApplication;
9+
10+
import android.util.Log;
11+
12+
import java.io.File;
13+
import java.io.IOException;
14+
15+
import java.util.UUID;
16+
17+
public class CreateNonEncryptedDatabaseTest extends SQLCipherTest {
18+
19+
@Override
20+
public boolean execute(SQLiteDatabase database) {
21+
22+
database.close();
23+
24+
String unencryptedDatabaseName = "unencrypted-" + UUID.randomUUID().toString() + ".db";
25+
File unencryptedDatabase = ZeteticApplication.getInstance().getDatabasePath(unencryptedDatabaseName);
26+
27+
try {
28+
String noPasswordString = "";
29+
database = SQLiteDatabase.openOrCreateDatabase(unencryptedDatabase, noPasswordString, null);
30+
database.execSQL("CREATE TABLE test_table(test_column);");
31+
database.execSQL("INSERT INTO test_table VALUES(?);", new Object[]{"test value"});
32+
database.close();
33+
} catch (Exception e) {
34+
Log.e(ZeteticApplication.TAG, "NOT EXPECTED: exception", e);
35+
return false;
36+
}
37+
38+
try {
39+
char[] nullPassword = null;
40+
database = SQLiteDatabase.openOrCreateDatabase(unencryptedDatabase.getPath(), nullPassword, null, null);
41+
Cursor cursor = database.rawQuery("SELECT * FROM test_table;", new String[]{});
42+
cursor.moveToFirst();
43+
String a = cursor.getString(0);
44+
cursor.close();
45+
database.close();
46+
if (a == null || !a.equals("test value")) {
47+
Log.e(ZeteticApplication.TAG, "NOT EXPECTED: INCORRECT or MISSING test value");
48+
return false;
49+
}
50+
} catch (Exception e) {
51+
Log.e(ZeteticApplication.TAG, "NOT EXPECTED: exception when reading database with zero-length password", e);
52+
return false;
53+
}
54+
55+
try {
56+
char[] noPassword = new char[0];
57+
database = SQLiteDatabase.openOrCreateDatabase(unencryptedDatabase.getPath(), noPassword, null, null);
58+
Cursor cursor = database.rawQuery("SELECT * FROM test_table;", new String[]{});
59+
cursor.moveToFirst();
60+
String a = cursor.getString(0);
61+
cursor.close();
62+
database.close();
63+
if (a == null || !a.equals("test value")) {
64+
Log.e(ZeteticApplication.TAG, "NOT EXPECTED: INCORRECT or MISSING test value");
65+
return false;
66+
}
67+
} catch (Exception e) {
68+
Log.e(ZeteticApplication.TAG, "NOT EXPECTED: exception when reading database with zero-length password", e);
69+
return false;
70+
}
71+
72+
try {
73+
SQLiteDatabase.openOrCreateDatabase(unencryptedDatabase, ZeteticApplication.DATABASE_PASSWORD, null);
74+
75+
Log.e(ZeteticApplication.TAG,
76+
"NOT EXPECTED: SQLiteDatabase.openOrCreateDatabase() of unencrypted database with a password did not fail");
77+
return false;
78+
} catch (SQLiteException e){
79+
Log.v(ZeteticApplication.TAG,
80+
"SQLiteDatabase.openOrCreateDatabase() of unencrypted database with a password did throw a SQLiteException as expected OK", e);
81+
} catch (Exception e){
82+
Log.e(ZeteticApplication.TAG,
83+
"NOT EXPECTED: SQLiteDatabase.openOrCreateDatabase() with invalid password did throw an unexpected exception", e);
84+
return false;
85+
}
86+
87+
try {
88+
String nullPasswordString = null;
89+
SQLiteDatabase.create(null, nullPasswordString)
90+
.close();
91+
} catch (Exception e) {
92+
Log.e(ZeteticApplication.TAG, "NOT EXPECTED: exception", e);
93+
return false;
94+
}
95+
96+
return true;
97+
}
98+
99+
@Override
100+
public String getName() {
101+
return "Create Non-Encrypted Database Test";
102+
}
103+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package net.zetetic.tests;
2+
3+
import android.util.Log;
4+
5+
import net.sqlcipher.database.SQLiteDatabase;
6+
import net.sqlcipher.database.SQLiteException;
7+
8+
import net.zetetic.ZeteticApplication;
9+
10+
import java.io.File;
11+
12+
public class InvalidOpenArgumentTest extends SQLCipherTest {
13+
@Override
14+
public boolean execute(SQLiteDatabase database) {
15+
database.close();
16+
17+
try {
18+
String nullPathString = null;
19+
database = SQLiteDatabase.openOrCreateDatabase(nullPathString, ZeteticApplication.DATABASE_PASSWORD, null);
20+
Log.e(ZeteticApplication.TAG,
21+
"NOT EXPECTED: SQLiteDatabase.openOrCreateDatabase() with null path string did not fail");
22+
return false;
23+
} catch (IllegalArgumentException e){
24+
Log.v(ZeteticApplication.TAG,
25+
"SQLiteDatabase.openOrCreateDatabase() with null path string did throw an IllegalArgumentException as expected OK", e);
26+
} catch (Exception e){
27+
Log.e(ZeteticApplication.TAG,
28+
"NOT EXPECTED: SQLiteDatabase.openOrCreateDatabase() with null path string did throw an unexpected exception type", e);
29+
return false;
30+
}
31+
32+
try {
33+
String nullPathString = null;
34+
database = SQLiteDatabase.openOrCreateDatabase(nullPathString, ZeteticApplication.DATABASE_PASSWORD, null, null, null);
35+
Log.e(ZeteticApplication.TAG,
36+
"NOT EXPECTED: SQLiteDatabase.openOrCreateDatabase() with null path string did not fail");
37+
return false;
38+
} catch (IllegalArgumentException e){
39+
Log.v(ZeteticApplication.TAG,
40+
"SQLiteDatabase.openOrCreateDatabase() with null path string did throw an IllegalArgumentException as expected OK", e);
41+
} catch (Exception e){
42+
Log.e(ZeteticApplication.TAG,
43+
"NOT EXPECTED: SQLiteDatabase.openOrCreateDatabase() with null path string did throw an unexpected exception type", e);
44+
return false;
45+
}
46+
47+
try {
48+
File nullFile = null;
49+
database = SQLiteDatabase.openOrCreateDatabase(nullFile, ZeteticApplication.DATABASE_PASSWORD, null);
50+
Log.e(ZeteticApplication.TAG,
51+
"NOT EXPECTED: SQLiteDatabase.openOrCreateDatabase() with null file did not fail");
52+
return false;
53+
} catch (IllegalArgumentException e){
54+
Log.v(ZeteticApplication.TAG,
55+
"EXPECTED RESULT: SQLiteDatabase.openOrCreateDatabase() with null file did throw an IllegalArgumentException OK", e);
56+
} catch (Exception e){
57+
Log.e(ZeteticApplication.TAG,
58+
"NOT EXPECTED: SQLiteDatabase.openOrCreateDatabase() with no password did throw an unexpected exception type", e);
59+
return false;
60+
}
61+
62+
try {
63+
File nullFile = null;
64+
database = SQLiteDatabase.openOrCreateDatabase(nullFile, ZeteticApplication.DATABASE_PASSWORD, null, null, null);
65+
Log.e(ZeteticApplication.TAG,
66+
"NOT EXPECTED: SQLiteDatabase.openOrCreateDatabase() with null file did not fail");
67+
return false;
68+
} catch (IllegalArgumentException e){
69+
Log.v(ZeteticApplication.TAG,
70+
"EXPECTED RESULT: SQLiteDatabase.openOrCreateDatabase() with null file did throw an IllegalArgumentException OK", e);
71+
} catch (Exception e){
72+
Log.e(ZeteticApplication.TAG,
73+
"NOT EXPECTED: SQLiteDatabase.openOrCreateDatabase() with null file did throw an unexpected exception type", e);
74+
return false;
75+
}
76+
77+
return true;
78+
}
79+
80+
@Override
81+
public String getName() {
82+
return "Invalid Open Argument Test";
83+
}
84+
}

src/main/java/net/zetetic/tests/InvalidPasswordTest.java

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package net.zetetic.tests;
22

3+
import android.util.Log;
4+
35
import net.sqlcipher.Cursor;
46
import net.sqlcipher.database.SQLiteDatabase;
57
import net.sqlcipher.database.SQLiteDatabaseHook;
8+
import net.sqlcipher.database.SQLiteException;
9+
610
import net.zetetic.ZeteticApplication;
711

812
import java.io.File;
@@ -11,22 +15,51 @@
1115
public class InvalidPasswordTest extends SQLCipherTest {
1216
@Override
1317
public boolean execute(SQLiteDatabase database) {
14-
15-
boolean status = false;
1618
database.rawExecSQL("create table t1(a,b);");
1719
database.close();
20+
1821
File databaseFile = ZeteticApplication.getInstance().getDatabasePath(ZeteticApplication.DATABASE_NAME);
19-
String password = ZeteticApplication.DATABASE_PASSWORD;
20-
try{
22+
23+
try {
2124
SQLiteDatabase.openOrCreateDatabase(databaseFile, UUID.randomUUID().toString(), null);
25+
26+
Log.e(ZeteticApplication.TAG,
27+
"NOT EXPECTED: SQLiteDatabase.openOrCreateDatabase() with invalid password did not fail");
28+
return false;
29+
} catch (SQLiteException e){
30+
Log.v(ZeteticApplication.TAG,
31+
"SQLiteDatabase.openOrCreateDatabase() with invalid password did throw a SQLiteException as expected OK", e);
32+
} catch (Exception e){
33+
Log.e(ZeteticApplication.TAG,
34+
"NOT EXPECTED: SQLiteDatabase.openOrCreateDatabase() with invalid password did throw an unexpected exception", e);
35+
return false;
36+
}
37+
38+
try {
39+
String noPassword = "";
40+
SQLiteDatabase.openOrCreateDatabase(databaseFile, noPassword, null);
41+
42+
Log.e(ZeteticApplication.TAG,
43+
"NOT EXPECTED: SQLiteDatabase.openOrCreateDatabase() with no password did NOT fail on an encrypted database");
44+
return false;
45+
} catch (SQLiteException e){
46+
Log.v(ZeteticApplication.TAG,
47+
"SQLiteDatabase.openOrCreateDatabase() with no password did throw a SQLiteException as expected OK", e);
2248
} catch (Exception e){
23-
try {
24-
database = SQLiteDatabase.openOrCreateDatabase(databaseFile, password, null);
25-
database.execSQL("insert into t1(a,b) values(?, ?)", new Object[]{"testing", "123"});
26-
status = true;
27-
} catch (Exception ex){}
49+
Log.e(ZeteticApplication.TAG,
50+
"NOT EXPECTED: SQLiteDatabase.openOrCreateDatabase() with no password did throw an unexpected exception type", e);
51+
return false;
2852
}
29-
return status;
53+
54+
try {
55+
database = SQLiteDatabase.openOrCreateDatabase(databaseFile, ZeteticApplication.DATABASE_PASSWORD, null);
56+
database.execSQL("insert into t1(a,b) values(?, ?)", new Object[]{"testing", "123"});
57+
} catch (Exception e){
58+
Log.e(ZeteticApplication.TAG, "NOT EXPECTED: attempt to access database with correct password did throw an unexpected exception", e);
59+
return false;
60+
}
61+
62+
return true;
3063
}
3164

3265
@Override

0 commit comments

Comments
 (0)