11package net .zetetic .database .sqlcipher_cts ;
22
3+ import static org .hamcrest .MatcherAssert .assertThat ;
4+ import static org .hamcrest .core .Is .is ;
5+ import static org .hamcrest .core .IsNull .nullValue ;
6+
37import android .database .Cursor ;
48
59import net .zetetic .database .sqlcipher .SQLiteDatabase ;
812
913import org .junit .Test ;
1014
11- import static org .hamcrest .MatcherAssert .assertThat ;
12- import static org .hamcrest .core .Is .is ;
13- import static org .hamcrest .core .IsNull .nullValue ;
14-
1515public class SQLCipherDatabaseTest extends AndroidSQLCipherTestCase {
1616
1717 @ Test
18- public void testConnectionWithPassword (){
19- int a = 0 , b = 0 ;
20- database = 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" ));
2835 }
29- assertThat (a , is (1 ));
30- assertThat (b , is (2 ));
3136 }
3237
3338 @ Test
34- public void insertDataQueryByObjectParams (){
39+ public void insertDataQueryByObjectParams () {
3540 float a = 1.25f , a1 = 0.0f ;
3641 double b = 2.00123d , b1 = 0.0d ;
37- database = SQLiteDatabase .openOrCreateDatabase (databasePath , "foo" , null , null );
3842 database .execSQL ("create table t1(a,b);" );
3943 database .execSQL ("insert into t1(a,b) values(?,?)" , new Object []{a , b });
4044 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 ()) {
4246 a1 = cursor .getFloat (0 );
4347 b1 = cursor .getDouble (1 );
4448 cursor .close ();
@@ -48,51 +52,63 @@ public void insertDataQueryByObjectParams(){
4852 }
4953
5054 @ 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 () {
6156 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 ));
7179 }
72- assertThat ( a1 , is ( a ));
73- assertThat ( b1 , is ( b ));
80+ } finally {
81+ delete ( context . getDatabasePath ( "foo.db" ));
7482 }
7583 }
7684
7785 @ 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+ }
8294 }
8395
8496 @ 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+ }
92108 }
93109
94110 @ Test (expected = IllegalStateException .class )
95- public void shouldThrowExceptionOnChangePasswordWithInMemoryDatabase (){
111+ public void shouldThrowExceptionOnChangePasswordWithInMemoryDatabase () {
96112 database = SQLiteDatabase .openOrCreateDatabase (SQLiteDatabaseConfiguration .MEMORY_DB_PATH , "foo" , null , null , null );
97113 database .changePassword ("bar" );
98114 }
0 commit comments