Skip to content

Commit 8a6e87f

Browse files
author
Christopher J. Brody
committed
Null File/path tests; more blank/null/invalid password tests
These tests demonstrate some cases where null parameter values trigger NPEs.
1 parent 6dfe7df commit 8a6e87f

8 files changed

+582
-11
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
41+
Log.e(ZeteticApplication.TAG, "BEHAVIOR CHANGED please update this test");
42+
return false;
43+
} catch (NullPointerException e) {
44+
Log.v(ZeteticApplication.TAG, "IGNORED: null pointer exception when calling SQLiteDatabase.changePassword with null password String", e);
45+
} catch (Exception e) {
46+
Log.e(ZeteticApplication.TAG, "NOT EXPECTED: exception", e);
47+
return false;
48+
}
49+
database.close();
50+
51+
return true;
52+
}
53+
54+
@Override
55+
public String getName() {
56+
return "Change Password Test";
57+
}
58+
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,17 @@ 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+
try {
77+
database = SQLiteDatabase.openOrCreateDatabase(unencryptedDatabase, "", null, null, null);
78+
79+
Log.e(TAG, "BEHAVIOR CHANGED: please update this test and attempt a rawQuery");
80+
return false;
81+
} catch (NullPointerException ex) {
82+
Log.v(TAG, "IGNORED: NullPointerException due to null DatabaseErrorHandler", ex);
83+
}
84+
7485
return true;
7586
} catch (Exception ex) {
7687
// Uncaught exception (not expected):
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
Log.e(ZeteticApplication.TAG, "BEHAVIOR CHANGED please update this test");
91+
return false;
92+
} catch (NullPointerException e) {
93+
Log.v(ZeteticApplication.TAG, "IGNORED: null pointer exception when opening database with null String password", e);
94+
} catch (Exception e) {
95+
Log.e(ZeteticApplication.TAG, "NOT EXPECTED: exception", e);
96+
return false;
97+
}
98+
99+
return true;
100+
}
101+
102+
@Override
103+
public String getName() {
104+
return "Create Non-Encrypted Database Test";
105+
}
106+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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 (NullPointerException e){
54+
Log.v(ZeteticApplication.TAG,
55+
"IGNORED: SQLiteDatabase.openOrCreateDatabase() with null file did throw a NullPointerException", e);
56+
} catch (IllegalArgumentException e){
57+
Log.e(ZeteticApplication.TAG,
58+
"BEHAVIOR CHANGED: SQLiteDatabase.openOrCreateDatabase() with null file did throw an IllegalArgumentException - please update this test", e);
59+
return false;
60+
} catch (Exception e){
61+
Log.e(ZeteticApplication.TAG,
62+
"NOT EXPECTED: SQLiteDatabase.openOrCreateDatabase() with no password did throw an unexpected exception type", e);
63+
return false;
64+
}
65+
66+
try {
67+
File nullFile = null;
68+
database = SQLiteDatabase.openOrCreateDatabase(nullFile, ZeteticApplication.DATABASE_PASSWORD, null, null, null);
69+
Log.e(ZeteticApplication.TAG,
70+
"NOT EXPECTED: SQLiteDatabase.openOrCreateDatabase() with null file did not fail");
71+
return false;
72+
} catch (NullPointerException e){
73+
Log.v(ZeteticApplication.TAG,
74+
"IGNORED: SQLiteDatabase.openOrCreateDatabase() with null file did throw a NullPointerException", e);
75+
} catch (IllegalArgumentException e){
76+
Log.e(ZeteticApplication.TAG,
77+
"BEHAVIOR CHANGED: SQLiteDatabase.openOrCreateDatabase() with null file did throw an IllegalArgumentException - please update this test", e);
78+
return false;
79+
} catch (Exception e){
80+
Log.e(ZeteticApplication.TAG,
81+
"NOT EXPECTED: SQLiteDatabase.openOrCreateDatabase() with null file did throw an unexpected exception type", e);
82+
return false;
83+
}
84+
85+
return true;
86+
}
87+
88+
@Override
89+
public String getName() {
90+
return "Invalid Open Argument Test";
91+
}
92+
}

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)