1
1
package net .zetetic .database .sqlcipher_cts ;
2
2
3
+ import static org .hamcrest .MatcherAssert .assertThat ;
4
+ import static org .hamcrest .core .Is .is ;
5
+ import static org .hamcrest .core .IsNull .nullValue ;
6
+
3
7
import android .database .Cursor ;
4
8
5
9
import net .zetetic .database .sqlcipher .SQLiteDatabase ;
8
12
9
13
import org .junit .Test ;
10
14
11
- import static org .hamcrest .MatcherAssert .assertThat ;
12
- import static org .hamcrest .core .Is .is ;
13
- import static org .hamcrest .core .IsNull .nullValue ;
14
-
15
15
public class SQLCipherDatabaseTest extends AndroidSQLCipherTestCase {
16
16
17
17
@ Test
18
- public void testConnectionWithPassword (){
19
- int a = 0 , b = 0 ;
20
- database
10000
= SQLiteDatabase .openOrCreateDatabase (databasePath , "foo" , null , null );
21
- database .execSQL ("create table t1(a,b);" );
22
- database .execSQL ("insert into t1(a,b) values(?,?)" , new Object []{1 , 2 });
23
- Cursor cursor = database .rawQuery ("select * from t1;" , new String []{});
24
- if (cursor != null && cursor .moveToFirst ()){
25
- a = cursor .getInt (0 );
26
- b = cursor .getInt (1 );
27
- cursor .close ();
18
+ public void testConnectionWithPassword () {
19
+ try {
20
+ int a = 0 , b = 0 ;
21
+ closeAndDelete (database );
22
+ database = SQLiteDatabase .openOrCreateDatabase (context .getDatabasePath ("foo.db" ), "foo" , null , null );
23
+ database .execSQL ("create table t1(a,b);" );
24
+ database .execSQL ("insert into t1(a,b) values(?,?)" , new Object []{1 , 2 });
25
+ Cursor cursor = database .rawQuery ("select * from t1;" , new String []{});
26
+ if (cursor != null && cursor .moveToFirst ()) {
27
+ a = cursor .getInt (0 );
28
+ b = cursor .getInt (1 );
29
+ cursor .close ();
30
+ }
31
+ assertThat (a , is (1 ));
32
+ assertThat (b , is (2 ));
33
+ } finally {
34
+ delete (context .getDatabasePath ("foo.db" ));
28
35
}
29
- assertThat (a , is (1 ));
30
- assertThat (b , is (2 ));
31
36
}
32
37
33
38
@ Test
34
- public void insertDataQueryByObjectParams (){
39
+ public void insertDataQueryByObjectParams () {
35
40
float a = 1.25f , a1 = 0.0f ;
36
41
double b = 2.00123d , b1 = 0.0d ;
37
- database = SQLiteDatabase .openOrCreateDatabase (databasePath , "foo" , null , null );
38
42
database .execSQL ("create table t1(a,b);" );
39
43
database .execSQL ("insert into t1(a,b) values(?,?)" , new Object []{a , b });
40
44
Cursor cursor = database .rawQuery ("select * from t1 where a = ? and b = ?;" , a , b );
41
- if (cursor != null && cursor .moveToFirst ()){
45
+ if (cursor != null && cursor .moveToFirst ()) {
42
46
a1 = cursor .getFloat (0 );
43
47
b1 = cursor .getDouble (1 );
44
48
cursor .close ();
@@ -48,51 +52,63 @@ public void insertDataQueryByObjectParams(){
48
52
}
49
53
50
54
@ Test
51
- public void shouldChangeDatabasePasswordSuccessfully (){
52
- int a = 3 , b = 4 ;
53
- int a1 = 0 , b1 = 0 ;
54
- String originalPassword = "foo" , newPassword = "bar" ;
55
- database = SQLiteDatabase .openOrCreateDatabase (databasePath , originalPassword , null , null );
56
- database .execSQL ("create table t1(a,b);" );
57
- database .execSQL ("insert into t1(a,b) values(?,?)" , new Object []{a , b });
58
- database .changePassword (newPassword );
59
- database .close ();
60
- database = null ;
55
+ public void shouldChangeDatabasePasswordSuccessfully () {
61
56
try {
62
- database = SQLiteDatabase .openOrCreateDatabase (databasePath , originalPassword , null , null );
63
- assertThat (database , is (nullValue ()));
64
- } catch (SQLiteException ex ){
65
- database = SQLiteDatabase .openOrCreateDatabase (databasePath , newPassword , null , null );
66
- Cursor cursor = database .rawQuery ("select * from t1;" );
67
- if (cursor != null && cursor .moveToFirst ()){
68
- a1 = cursor .getInt (0 );
69
- b1 = cursor .getInt (1 );
70
- cursor .close ();
57
+ int a = 3 , b = 4 ;
58
+ int a1 = 0 , b1 = 0 ;
59
+ String originalPassword = "foo" , newPassword = "bar" ;
60
+ database = SQLiteDatabase .openOrCreateDatabase (context .getDatabasePath ("foo.db" ), originalPassword , null , null );
61
+ database .execSQL ("create table t1(a,b);" );
62
+ database .execSQL ("insert into t1(a,b) values(?,?)" , new Object []{a , b });
63
+ database .changePassword (newPassword );
64
+ database .close ();
65
+ database = null ;
66
+ try {
67
+ database = SQLiteDatabase .openOrCreateDatabase (context .getDatabasePath ("foo.db" ), originalPassword , null , null );
68
+ assertThat (database , is (nullValue ()));
69
+ } catch (SQLiteException ex ) {
70
+ database = SQLiteDatabase .openOrCreateDatabase (context .getDatabasePath ("foo.db" ), newPassword , null , null );
71
+ Cursor cursor = database .rawQuery ("select * from t1;" );
72
+ if (cursor != null && cursor .moveToFirst ()) {
73
+ a1 = cursor .getInt (0 );
74
+ b1 = cursor .getInt (1 );
75
+ cursor .close ();
76
+ }
77
+ assertThat (a1 , is (a ));
78
+ assertThat (b1 , is (b ));
71
79
}
72
- assertThat ( a1 , is ( a ));
73
- assertThat ( b1 , is ( b ));
80
+ } finally {
81
+ delete ( context . getDatabasePath ( "foo.db" ));
74
82
}
75
83
}
76
84
77
85
@ Test (expected = IllegalStateException .class )
78
- public void shouldThrowExceptionWhenChangingPasswordOnClosedDatabase (){
79
- database = SQLiteDatabase .openOrCreateDatabase (databasePath , "foo" , null , null );
80
- database .close ();
81
- database .changePassword ("bar" );
86
+ public void shouldThrowExceptionWhenChangingPasswordOnClosedDatabase () {
87
+ try {
88
+ database = SQLiteDatabase .openOrCreateDatabase (context .getDatabasePath ("foo.db" ), "foo" , null , null );
89
+ database .close ();
90
+ database .changePassword ("bar" );
91
+ } finally {
92
+ delete (context .getDatabasePath ("foo.db" ));
93
+ }
82
94
}
83
95
84
96
@ Test (expected = IllegalStateException .class )
85
- public void shouldThrowExceptionOnChangePasswordWithReadOnlyDatabase (){
86
- database = SQLiteDatabase .openOrCreateDatabase (databasePath .getAbsolutePath (), "foo" , null , null );
87
- database .execSQL ("create table t1(a,b);" );
88
- database .close ();
89
- database = null ;
90
- database = SQLiteDatabase .openDatabase (databasePath .getAbsolutePath (), "foo" , null , SQLiteDatabase .OPEN_READONLY , null , null );
91
- database .changePassword ("bar" );
97
+ public void shouldThrowExceptionOnChangePasswordWithReadOnlyDatabase () {
98
+ try {
99
+ database = SQLiteDatabase .openOrCreateDatabase (context .getDatabasePath ("foo.db" ).getAbsolutePath (), "foo" , null , null );
100
+ database .execSQL ("create table t1(a,b);" );
101
+ database .close ();
102
+ database = null ;
103
+ database = SQLiteDatabase .openDatabase (context .getDatabasePath ("foo.db" ).getAbsolutePath (), "foo" , null , SQLiteDatabase .OPEN_READONLY , null , null );
104
+ database .changePassword ("bar" );
105
+ } finally {
106
+ delete (context .getDatabasePath ("foo.db" ));
107
+ }
92
108
}
93
109
94
110
@ Test (expected = IllegalStateException .class )
95
- public void shouldThrowExceptionOnChangePasswordWithInMemoryDatabase (){
111
+ public void shouldThrowExceptionOnChangePasswordWithInMemoryDatabase () {
96
112
database = SQLiteDatabase .openOrCreateDatabase (SQLiteDatabaseConfiguration .MEMORY_DB_PATH , "foo" , null , null , null );
97
113
database .changePassword ("bar" );
98
114
}
0 commit comments