Skip to content

Commit bc3e052

Browse files
Adding test for getting a readable database
1 parent d74db4c commit bc3e052

File tree

8 files changed

+178
-85
lines changed

8 files changed

+178
-85
lines changed

libs/sqlcipher.jar

107 Bytes
Binary file not shown.

net.zetetic.sqlcipher.test.iml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<option name="MANIFEST_FILE_RELATIVE_PATH" value="/AndroidManifest.xml" />
99
<option name="RES_FOLDER_RELATIVE_PATH" value="/res" />
1010
<option name="ASSETS_FOLDER_RELATIVE_PATH" value="/assets" />
11-
<option name="LIBS_FOLDER_RELATIVE_PATH" value="/src/main/native" />
11+
<option name="LIBS_FOLDER_RELATIVE_PATH" value="/libs" />
1212
<option name="REGENERATE_R_JAVA" value="true" />
1313
<option name="REGENERATE_JAVA_BY_AIDL" value="true" />
1414
<option name="USE_CUSTOM_APK_RESOURCE_FOLDER" value="false" />

net.zetetic.sqlcipher.test.iws

Lines changed: 96 additions & 68 deletions
Large diffs are not rendered by default.

res/layout/main.xml

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3-
android:orientation="vertical"
4-
android:layout_width="fill_parent"
5-
android:layout_height="fill_parent"
6-
>
7-
<Button
8-
android:id="@+id/executeSuite"
9-
android:layout_width="fill_parent"
10-
android:layout_height="wrap_content"
11-
android:text="@string/test_suite_button"
12-
android:onClick="onButtonClick"/>
13-
<TextView
14-
android:id="@+id/test_suite_results"
15-
android:layout_width="fill_parent"
16-
android:layout_height="wrap_content" />
17-
</LinearLayout>
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:orientation="vertical"
4+
android:layout_width="fill_parent"
5+
android:layout_height="fill_parent">
6+
<LinearLayout android:id="@+id/container"
7+
android:layout_height="wrap_content"
8+
android:layout_width="fill_parent">
9+
<Button
10+
android:id="@+id/executeSuite"
11+
android:layout_width="fill_parent"
12+
android:layout_height="wrap_content"
13+
android:text="@string/test_suite_button"
14+
android:onClick="onButtonClick"/>
15+
</LinearLayout>
16+
<TextView
17+
android:id="@+id/test_suite_results"
18+
android:layout_width="fill_parent"
19+
android:layout_height="fill_parent"
20+
android:gravity="top|left"
21+
android:scrollbars="vertical"
22+
android:singleLine="false"
23+
android:layout_below="@+id/container"/>
24+
</RelativeLayout>

src/main/java/net/zetetic/TestSuiteActivity.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.app.Activity;
44
import android.os.Bundle;
5+
import android.text.method.ScrollingMovementMethod;
56
import android.util.Log;
67
import android.view.View;
78
import android.widget.TextView;
@@ -25,6 +26,7 @@ public void onButtonClick(View view) {
2526

2627
findViewById(R.id.executeSuite).setEnabled(false);
2728
resultsView = (TextView) findViewById(R.id.test_suite_results);
29+
resultsView.setMovementMethod(ScrollingMovementMethod.getInstance());
2830
ZeteticApplication.getInstance().setCurrentActivity(this);
2931
new TestSuiteRunner().execute(this);
3032
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package net.zetetic.tests;
2+
3+
import android.content.Context;
4+
import android.database.Cursor;
5+
import net.sqlcipher.database.SQLiteDatabase;
6+
import net.sqlcipher.database.SQLiteOpenHelper;
7+
import net.zetetic.ZeteticApplication;
8+
9+
import java.io.File;
10+
11+
public class ReadableDatabaseTest extends SQLCipherTest {
12+
13+
@Override
14+
public boolean execute(SQLiteDatabase database) {
15+
16+
File databaseFile = ZeteticApplication.getInstance().getDatabasePath(ZeteticApplication.DATABASE_NAME);
17+
File databasesDirectory = new File(databaseFile.getParent());
18+
for(File file : databasesDirectory.listFiles()){
19+
file.delete();
20+
}
21+
ReadableDatabaseHelper helper = new ReadableDatabaseHelper(ZeteticApplication.getInstance());
22+
SQLiteDatabase readableDatabase = helper.getReadableDatabase(ZeteticApplication.DATABASE_PASSWORD);
23+
Cursor results = readableDatabase.rawQuery("select * from t1", new String[]{});
24+
int resultCount = 0;
25+
while (results.moveToNext()){
26+
resultCount++;
27+
}
28+
readableDatabase.close();
29+
return resultCount > 0;
30+
}
31+
32+
@Override
33+
public String getName() {
34+
return "Readable Database Test";
35+
}
36+
37+
class ReadableDatabaseHelper extends SQLiteOpenHelper {
38+
39+
public ReadableDatabaseHelper(Context context) {
40+
super(context, ZeteticApplication.DATABASE_NAME, null, 1);
41+
}
42+
43+
@Override
44+
public void onCreate(SQLiteDatabase database) {
45+
database.execSQL("create table t1(a,b)");
46+
database.execSQL("insert into t1(a,b) values(?, ?)", new Object[]{"one for the money",
47+
"two for the show"});
48+
}
49+
50+
@Override
51+
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {}
52+
}
53+
54+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public abstract class SQLCipherTest {
1010

1111
public abstract boolean execute(SQLiteDatabase database);
1212
public abstract String getName();
13+
public String TAG = getClass().getSimpleName();
1314

1415
private SQLiteDatabase database;
1516

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ private List<SQLCipherTest> getTestsToRun(){
5555
tests.add(new PragmaCipherVersionTest());
5656
tests.add(new ImportUnencryptedDatabaseTest());
5757
tests.add(new FullTextSearchTest());
58+
tests.add(new ReadableDatabaseTest());
5859
return tests;
5960
}
6061
}

0 commit comments

Comments
 (0)