88
99import net .zetetic .database .sqlcipher .SQLiteDatabase ;
1010import net .zetetic .database .sqlcipher .SQLiteDatabaseConfiguration ;
11+ import net .zetetic .database .sqlcipher .SQLiteDatabaseCorruptException ;
1112import net .zetetic .database .sqlcipher .SQLiteException ;
1213
1314import org .junit .Test ;
1415
16+ import java .io .File ;
17+ import java .nio .charset .StandardCharsets ;
18+
1519public class SQLCipherDatabaseTest extends AndroidSQLCipherTestCase {
1620
1721 @ Test
18- public void testConnectionWithPassword () {
22+ public void testCreateDatabaseConnectionWithStringPassword () {
1923 try {
2024 int a = 0 , b = 0 ;
2125 closeAndDelete (database );
2226 database = SQLiteDatabase .openOrCreateDatabase (context .getDatabasePath ("foo.db" ), "foo" , null , null );
2327 database .execSQL ("create table t1(a,b);" );
2428 database .execSQL ("insert into t1(a,b) values(?,?)" , new Object []{1 , 2 });
29+ database .close ();
30+ database = SQLiteDatabase .openOrCreateDatabase (context .getDatabasePath ("foo.db" ), "foo" , null , null );
2531 Cursor cursor = database .rawQuery ("select * from t1;" , new String []{});
2632 if (cursor != null && cursor .moveToFirst ()) {
2733 a = cursor .getInt (0 );
@@ -35,6 +41,125 @@ public void testConnectionWithPassword() {
3541 }
3642 }
3743
44+ @ Test
45+ public void testCreateDatabaseConnectionWithStringByteArray () {
46+ try {
47+ int a = 0 , b = 0 ;
48+ closeAndDelete (database );
49+ byte [] generatedPassword = generateRandomBytes (64 );
50+ database = SQLiteDatabase .openOrCreateDatabase (context .getDatabasePath ("foo.db" ), generatedPassword , null , null );
51+ database .execSQL ("create table t1(a,b);" );
52+ database .execSQL ("insert into t1(a,b) values(?,?)" , new Object []{1 , 2 });
53+ database .close ();
54+ database = SQLiteDatabase .openOrCreateDatabase (context .getDatabasePath ("foo.db" ), generatedPassword , null , null );
55+ Cursor cursor = database .rawQuery ("select * from t1;" , new String []{});
56+ if (cursor != null && cursor .moveToFirst ()) {
57+ a = cursor .getInt (0 );
58+ b = cursor .getInt (1 );
59+ cursor .close ();
60+ }
61+ assertThat (a , is (1 ));
62+ assertThat (b , is (2 ));
63+ } finally {
64+ delete (context .getDatabasePath ("foo.db" ));
65+ }
66+ }
67+
68+ @ Test (expected = SQLiteDatabaseCorruptException .class )
69+ public void testOpenDatabaseConnectionWithInvalidStringPassword () {
70+ try {
71+ int a = 0 , b = 0 ;
72+ closeAndDelete (database );
73+ database = SQLiteDatabase .openOrCreateDatabase (context .getDatabasePath ("foo.db" ), "foo" , null , null );
74+ database .execSQL ("create table t1(a,b);" );
75+ database .execSQL ("insert into t1(a,b) values(?,?)" , new Object []{1 , 2 });
76+ database .close ();
77+ database = SQLiteDatabase .openDatabase (context .getDatabasePath ("foo.db" ).getPath (), "bar" , null , SQLiteDatabase .OPEN_READWRITE , null );
78+ } finally {
79+ delete (context .getDatabasePath ("foo.db" ));
80+ }
81+ }
82+
83+ @ Test (expected = SQLiteDatabaseCorruptException .class )
84+ public void testOpenDatabaseConnectionWithInvalidByteArrayPassword () {
85+ try {
86+ int a = 0 , b = 0 ;
87+ closeAndDelete (database );
88+ byte [] initialPassword = generateRandomBytes (64 );
89+ byte [] otherPassword = generateRandomBytes (64 );
90+ database = SQLiteDatabase .openOrCreateDatabase (context .getDatabasePath ("foo.db" ), initialPassword , null , null );
91+ database .execSQL ("create table t1(a,b);" );
92+ database .execSQL ("insert into t1(a,b) values(?,?)" , new Object []{1 , 2 });
93+ database .close ();
94+ database = SQLiteDatabase .openDatabase (context .getDatabasePath ("foo.db" ).getPath (), otherPassword , null , SQLiteDatabase .OPEN_READWRITE , null );
95+ } finally {
96+ delete (context .getDatabasePath ("foo.db" ));
97+ }
98+ }
99+
100+ @ Test
101+ public void openExistingSQLCipherDatabaseWithStringPassword (){
102+ File databasePath = null ;
103+ int a = 0 , b = 0 ;
104+ try {
105+ closeAndDelete (database );
106+ databasePath = extractAssetToDatabaseDirectory ("sqlcipher-4.x-testkey.db" );
107+ database = SQLiteDatabase .openDatabase (databasePath .getPath (), "testkey" , null , SQLiteDatabase .OPEN_READWRITE , null );
108+ Cursor cursor = database .rawQuery ("SELECT * FROM t1;" );
109+ if (cursor != null && cursor .moveToFirst ()){
110+ a = cursor .getInt (0 );
111+ b = cursor .getInt (1 );
112+ cursor .close ();
113+ }
114+ assertThat (a , is (1 ));
115+ assertThat (b , is (2 ));
116+ } finally {
117+ delete (databasePath );
118+ }
119+ }
120+
121+ @ Test
122+ public void openExistingSQLCipherDatabaseWithByteArrayPassword (){
123+ File databasePath = null ;
124+ int a = 0 , b = 0 ;
125+ try {
126+ closeAndDelete (database );
127+ databasePath = extractAssetToDatabaseDirectory ("sqlcipher-4.x-testkey.db" );
128+ database = SQLiteDatabase .openDatabase (databasePath .getPath (), "testkey" .getBytes (StandardCharsets .UTF_8 ), null , SQLiteDatabase .OPEN_READWRITE , null );
129+ Cursor cursor = database .rawQuery ("SELECT * FROM t1;" );
130+ if (cursor != null && cursor .moveToFirst ()){
131+ a = cursor .getInt (0 );
132+ b = cursor .getInt (1 );
133+ cursor .close ();
134+ }
135+ assertThat (a , is (1 ));
136+ assertThat (b , is (2 ));
137+ } finally {
138+ delete (databasePath );
139+ }
140+ }
141+
142+ @ Test
143+ public void openExistingSQLitePlaintextDatabase (){
144+ File databasePath = null ;
145+ int a = 0 , b = 0 ;
146+ try {
147+ closeAndDelete (database );
148+ databasePath = extractAssetToDatabaseDirectory ("sqlite-plaintext.db" );
149+ database = SQLiteDatabase .openDatabase (databasePath .getPath (), "" , null , SQLiteDatabase .OPEN_READWRITE , null );
150+ Cursor cursor = database .rawQuery ("SELECT * FROM t1;" );
151+ if (cursor != null && cursor .moveToFirst ()){
152+ a = cursor .getInt (0 );
153+ b = cursor .getInt (1 );
154+ cursor .close ();
155+ }
156+ assertThat (a , is (1 ));
157+ assertThat (b , is (2 ));
158+ } finally {
159+ delete (databasePath );
160+ }
161+ }
162+
38163 @ Test
39164 public void insertDataQueryByObjectParams () {
40165 float a = 1.25f , a1 = 0.0f ;
0 commit comments