8000 Adjust existing test cases for SQLCipher · ETS-Android4/sqlcipher-android@cec6d4e · GitHub
[go: up one dir, main page]

Skip to content

Commit cec6d4e

Browse files
Adjust existing test cases for SQLCipher
1 parent f921f0f commit cec6d4e

File tree

3 files changed

+129
-70
lines changed

3 files changed

+129
-70
lines changed

sqlcipher/src/androidTest/java/net/zetetic/database/sqlcipher_cts/AndroidSQLCipherTestCase.java

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,43 @@
33
import android.content.Context;
44
import android.util.Log;
55

6-
import androidx.test.platform.app.InstrumentationRegistry;
76
import androidx.test.ext.junit.runners.AndroidJUnit4;
7+
import androidx.test.platform.app.InstrumentationRegistry;
88

99
import net.zetetic.database.sqlcipher.SQLiteDatabase;
1010

1111
import org.junit.After;
1212
import org.junit.Before;
13+
import org.junit.Test;
1314
import org.junit.runner.RunWith;
1415

1516
import java.io.File;
17+
import java.io.FileOutputStream;
18+
import java.io.IOException;
19+
import java.io.InputStream;
20+
import java.io.OutputStream;
1621
import java.util.Locale;
1722

18-
import static org.junit.Assert.assertNotNull;
19-
2023
@RunWith(AndroidJUnit4.class)
2124
public abstract class AndroidSQLCipherTestCase {
2225

2326
protected SQLiteDatabase database;
24-
protected String DATABASE_NAME = "database_test.db";
27+
protected static String DATABASE_NAME = "database_test.db";
2528
protected String TAG = getClass().getSimpleName();
26-
protected File databasePath = null;
29+
protected File databaseFilePath = null;
2730
protected Context context = null;
2831

32+
@SuppressWarnings("ResultOfMethodCallIgnored")
2933
@Before
3034
public void setUp() {
31-
context = InstrumentationRegistry.getInstrumentation().getTargetContext();
35+
context = InstrumentationRegistry.getInstrumentation().getContext();
3236
System.loadLibrary("sqlcipher");
33-
databasePath = context.getDatabasePath(DATABASE_NAME);
34-
databasePath.mkdirs();
35-
if (databasePath.exists()) {
36-
databasePath.delete();
37+
databaseFilePath = context.getDatabasePath(DATABASE_NAME);
38+
databaseFilePath.mkdirs();
39+
if (databaseFilePath.exists()) {
40+
databaseFilePath.delete();
3741
}
42+
database = SQLiteDatabase.openOrCreateDatabase(databaseFilePath, "foo", null, null, null);
3843
}
3944

4045
@After
@@ -47,7 +52,49 @@ public void tearDown() {
4752
}
4853
}
4954

55+
public File extractAssetToDatabaseDirectory(String fileName) {
56+
File destinationPath = null;
57+
try {
58+
int length;
59+
InputStream sourceDatabase = context.getAssets().open(fileName);
60+
destinationPath = context.getDatabasePath(fileName);
61+
OutputStream destination = new FileOutputStream(destinationPath);
62+
byte[] buffer = new byte[4096];
63+
while ((length = sourceDatabase.read(buffer)) > 0) {
64+
destination.write(buffer, 0, length);
65+
}
66+
sourceDatabase.close();
67+
destination.flush();
68+
destination.close();
69+
} catch (Exception ex){
70+
throw new RuntimeException(ex);
71+
}
72+
return destinationPath;
73+
}
74+
75+
public void delete(File filePath){
76+
if(filePath == null) return;
77+
if(filePath.exists()){
78+
boolean result = filePath.delete();
79+
log("Deleted file:%s result:%s", filePath.getAbsolutePath(), result);
80+
}
81+
}
82+
83+
public void closeAndDelete(SQLiteDatabase database){
84+
if(database == null) return;
85+
File path = new File(database.getPath());
86+
database.close();
87+
if(path.exists()){
88+
boolean result = path.delete();
89+
log("Deleted database:%s result:%s", path.getAbsolutePath(), result);
90+
}
91+
}
92+
5093
protected void log(String message, Object...args){
5194
Log.i(TAG, String.format(Locale.getDefault(), message, args));
5295
}
96+
97+
protected void loge(Exception ex, String message, Object...args){
98+
Log.e(TAG, String.format(Locale.getDefault(), message, args), ex);
99+
}
53100
}

sqlcipher/src/androidTest/java/net/zetetic/database/sqlcipher_cts/SQLCipherDatabaseTest.java

Lines changed: 68 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package 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+
37
import android.database.Cursor;
48

59
import net.zetetic.database.sqlcipher.SQLiteDatabase;
@@ -8,37 +12,37 @@
812

913
import 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-
1515
public class SQLCipherDatabaseTest extends AndroidSQLCipherTestCase {
1616

1717
@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"));
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
}

sqlcipher/src/androidTest/java/net/zetetic/database/sqlcipher_cts/SQLCipherVersionTest.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
package net.zetetic.database.sqlcipher_cts;
22

3-
import android.database.Cursor;
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.core.StringContains.containsString;
45

5-
import net.zetetic.database.sqlcipher.SQLiteDatabase;
6+
import android.database.Cursor;
67

78
import org.junit.Test;
89

9-
import static org.hamcrest.MatcherAssert.assertThat;
10-
import static org.hamcrest.core.IsEqual.equalTo;
11-
import static org.hamcrest.core.StringContains.containsString;
12-
1310
public class SQLCipherVersionTest extends AndroidSQLCipherTestCase {
1411

1512
@Test
16-
public void testCipherVersionReported(){
13+
public void shouldExtractLibraryCipherVersion() {
1714
String cipherVersion = "";
18-
database = SQLiteDatabase.openOrCreateDatabase(databasePath, "foo", null, null);
1915
Cursor cursor = database.rawQuery("PRAGMA cipher_version;");
2016
if(cursor != null && cursor.moveToFirst()){
2117
cipherVersion = cursor.getString(0);

0 commit comments

Comments
 (0)
0