Skip to content

Commit 66389c3

Browse files
Adding an additional test around attaching an existing database
1 parent 635f81d commit 66389c3

File tree

6 files changed

+116
-91
lines changed

6 files changed

+116
-91
lines changed

net.zetetic.sqlcipher.test.iws

Lines changed: 39 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -75,30 +75,19 @@
7575
</component>
7676
<component name="FileEditorManager">
7777
<leaf>
78-
<file leaf-file-name="ZeteticApplication.java" pinned="false" current="false" current-in-tab="false">
79-
<entry file="file://$PROJECT_DIR$/src/main/java/net/zetetic/ZeteticApplication.java">
78+
<file leaf-file-name="AttachExistingDatabaseTest.java" pinned="false" current="true" current-in-tab="true">
79+
<entry file="file://$PROJECT_DIR$/src/main/java/net/zetetic/tests/AttachExistingDatabaseTest.java">
8080
<provider selected="true" editor-type-id="text-editor">
81-
<state line="34" column="0" selection-start="865" selection-end="865" vertical-scroll-proportion="0.0">
81+
<state line="4" column="38" selection-start="144" selection-end="144" vertical-scroll-proportion="0.0794702">
8282
<folding />
8383
</state>
8484
</provider>
8585
</entry>
8686
</file>
87-
<file leaf-file-name="MigrationFromDatabaseFormat1To2.java" pinned="false" current="true" current-in-tab="true">
87+
<file leaf-file-name="MigrationFromDatabaseFormat1To2.java" pinned="false" current="false" current-in-tab="false">
8888
<entry file="file://$PROJECT_DIR$/src/main/java/net/zetetic/tests/MigrationFromDatabaseFormat1To2.java">
8989
<provider selected="true" editor-type-id="text-editor">
90-
<state line="18" column="0" selection-start="466" selection-end="466" vertical-scroll-proportion="0.0794702">
91-
<folding>
92-
<element signature="imports" expanded="true" />
93-
</folding>
94-
</state>
95-
</provider>
96-
</entry>
97-
</file>
98-
<file leaf-file-name="AES128CipherTest.java" pinned="false" current="false" current-in-tab="false">
99-
<entry file="file://$PROJECT_DIR$/src/main/java/net/zetetic/tests/AES128CipherTest.java">
100-
<provider selected="true" editor-type-id="text-editor">
101-
<state line="34" column="61" selection-start="1025" selection-end="1025" vertical-scroll-proportion="0.0">
90+
<state line="19" column="88" selection-start="774" selection-end="774" vertical-scroll-proportion="0.0">
10291
<folding />
10392
</state>
10493
</provider>
@@ -116,7 +105,7 @@
116105
<file leaf-file-name="TestSuiteRunner.java" pinned="false" current="false" current-in-tab="false">
117106
<entry file="file://$PROJECT_DIR$/src/main/java/net/zetetic/tests/TestSuiteRunner.java">
118107
<provider selected="true" editor-type-id="text-editor">
119-
<state line="42" column="8" selection-start="1139" selection-end="1139" vertical-scroll-proportion="0.0">
108+
<state line="26" column="13" selection-start="647" selection-end="647" vertical-scroll-proportion="0.0">
120109
<folding />
121110
</state>
122111
</provider>
@@ -136,9 +125,6 @@
136125
<component name="IdeDocumentHistory">
137126
<option name="changedFiles">
138127
<list>
139-
<option value="$PROJECT_DIR$/src/main/java/net/zetetic/tests/NullQueryResultTest.java" />
140-
<option value="$PROJECT_DIR$/src/main/java/net/zetetic/tests/AttachDatabaseTest.java" />
141-
<option value="$PROJECT_DIR$/src/main/java/net/zetetic/tests/CrossProcessCursorQueryTest.java" />
142128
<option value="$PROJECT_DIR$/src/main/java/net/zetetic/tests/LoopingCountQueryTest.java" />
143129
<option value="$PROJECT_DIR$/src/main/java/net/zetetic/tests/LoopingQueryTest.java" />
144130
<option value="$PROJECT_DIR$/src/main/java/net/zetetic/tests/CanThrowSQLiteExceptionTest.java" />
@@ -149,9 +135,12 @@
149135
<option value="$PROJECT_DIR$/src/main/java/net/zetetic/tests/SQLCipherTest.java" />
150136
<option value="$PROJECT_DIR$/src/main/java/net/zetetic/ZeteticContentProvider.java" />
151137
<option value="$PROJECT_DIR$/src/main/java/net/zetetic/tests/AES128CipherTest.java" />
152-
<option value="$PROJECT_DIR$/src/main/java/net/zetetic/ZeteticApplication.java" />
153138
<option value="$PROJECT_DIR$/src/main/java/net/zetetic/tests/MigrationFromDatabaseFormat1To2.java" />
139+
<option value="$PROJECT_DIR$/src/main/java/net/zetetic/tests/AttachDatabaseTest.java" />
140+
<option value="$PROJECT_DIR$/src/main/java/net/zetetic/ZeteticApplication.java" />
141+
<option value="$PROJECT_DIR$/src/main/java/net/zetetic/tests/AttachExistingDatabaseTest.java" />
154142
<option value="$PROJECT_DIR$/src/main/java/net/zetetic/tests/TestSuiteRunner.java" />
143+
<option value="$PROJECT_DIR$/src/main/java/net/zetetic/tests/AttachNewDatabaseTest.java" />
155144
</list>
156145
</option>
157146
</component>
@@ -394,7 +383,7 @@
394383
<recent name="net.zetetic.tests" />
395384
</key>
396385
</component>
397-
<component name="RunManager" selected="Android Application.emulator-3.1">
386+
<component name="RunManager" selected="Android Application.emulator-2.1">
398387
<configuration default="true" type="AndroidTestRunConfigurationType" factoryName="Android Tests">
399388
<module name="" />
400389
<option name="TESTING_TYPE" value="0" />
@@ -732,16 +721,16 @@
732721
<window_info id="Changes" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="7" side_tool="false" content_ui="tabs" />
733722
<window_info id="Palette" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
734723
<window_info id="Ant Build" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.25" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
735-
<window_info id="Debug" active="true" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" weight="0.32800853" sideWeight="0.0" order="3" side_tool="false" content_ui="tabs" />
724+
<window_info id="Debug" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" weight="0.32800853" sideWeight="0.0" order="3" side_tool="false" content_ui="tabs" />
736725
<window_info id="Event Log" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="12" side_tool="true" content_ui="tabs" />
737726
<window_info id="Favorites" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="2" side_tool="true" content_ui="tabs" />
738727
<window_info id="Version Control" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="8" side_tool="false" content_ui="tabs" />
739728
<window_info id="Messages" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.32374868" sideWeight="0.46650276" order="9" side_tool="false" content_ui="tabs" />
740-
<window_info id="TODO" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.32893288" sideWeight="0.5" order="6" side_tool="false" content_ui="tabs" />
729+
<window_info id="TODO" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.32800853" sideWeight="0.5" order="6" side_tool="false" content_ui="tabs" />
741730
<window_info id="Structure" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.25" sideWeight="0.5" order="1" side_tool="true" content_ui="tabs" />
742731
<window_info id="Maven Projects" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
743732
<window_info id="Commander" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.4" sideWeight="0.5" order="0" side_tool="false" content_ui="tabs" />
744-
<window_info id="Project" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" weight="0.20299146" sideWeight="0.67199147" order="0" side_tool="false" content_ui="tabs" />
733+
<window_info id="Project" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" weight="0.22115384" sideWeight="0.67199147" order="0" side_tool="false" content_ui="tabs" />
745734
<window_info id="Run" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.32800853" sideWeight="0.0" order="2" side_tool="false" content_ui="tabs" />
746735
<window_info id="Android Logcat" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.66560173" sideWeight="0.5184275" order="11" side_tool="false" content_ui="tabs" />
747736
<window_info id="Cvs" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.25" sideWeight="0.5" order="4" side_tool="false" content_ui="tabs" />
@@ -808,11 +797,6 @@
808797
<option name="FILTER_TARGETS" value="false" />
809798
</component>
810799
<component name="editorHistoryManager">
811-
<entry file="file://$PROJECT_DIR$/src/main/java/net/zetetic/tests/CrossProcessCursorQueryTest.java">
812-
<provider selected="true" editor-type-id="text-editor">
813-
<state line="11" column="35" selection-start="329" selection-end="329" vertical-scroll-proportion="0.0" />
814-
</provider>
815-
</entry>
816800
<entry file="file://$PROJECT_DIR$/src/main/java/net/zetetic/tests/LoopingCountQueryTest.java">
817801
<provider selected="true" editor-type-id="text-editor">
818802
<state line="8" column="34" selection-start="219" selection-end="219" vertical-scroll-proportion="0.0" />
@@ -828,89 +812,80 @@
828812
<state line="5" column="40" selection-start="135" selection-end="162" vertical-scroll-proportion="0.0" />
829813
</provider>
830814
</entry>
831-
<entry file="file://$PROJECT_DIR$/src/main/java/net/zetetic/tests/AttachDatabaseTest.java">
832-
<provider selected="true" editor-type-id="text-editor">
833-
<state line="9" column="35" selection-start="241" selection-end="241" vertical-scroll-proportion="0.121348314">
834-
<folding />
835-
</state>
836-
</provider>
837-
</entry>
838815
<entry file="file://$PROJECT_DIR$/src/main/java/net/zetetic/tests/NullQueryResultTest.java">
839816
<provider selected="true" editor-type-id="text-editor">
840-
<state line="0" column="26" selection-start="0" selection-end="26" vertical-scroll-proportion="0.0">
841-
<folding />
842-
</state>
817+
<state line="0" column="26" selection-start="0" selection-end="26" vertical-scroll-proportion="0.0" />
843818
</provider>
844819
</entry>
845820
<entry file="file://$PROJECT_DIR$/src/main/java/net/zetetic/tests/ResultNotifier.java">
846821
<provider selected="true" editor-type-id="text-editor">
847-
<state line="4" column="20" selection-start="116" selection-end="116" vertical-scroll-proportion="0.078947365">
848-
<folding />
849-
</state>
822+
<state line="4" column="20" selection-start="116" selection-end="116" vertical-scroll-proportion="0.078947365" />
850823
</provider>
851824
</entry>
852825
<entry file="file://$PROJECT_DIR$/AndroidManifest.xml">
853826
<provider selected="true" editor-type-id="text-editor">
854-
<state line="16" column="0" selection-start="814" selection-end="814" vertical-scroll-proportion="0.31578946">
855-
<folding />
856-
</state>
827+
<state line="16" column="0" selection-start="814" selection-end="814" vertical-scroll-proportion="0.31578946" />
857828
</provider>
858829
</entry>
859830
<entry file="file://$PROJECT_DIR$/src/main/java/net/zetetic/tests/LoopingQueryTest.java">
860831
<provider selected="true" editor-type-id="text-editor">
861-
<state line="11" column="45" selection-start="312" selection-end="331" vertical-scroll-proportion="0.19736843">
862-
<folding />
863-
</state>
832+
<state line="11" column="45" selection-start="312" selection-end="331" vertical-scroll-proportion="0.19736843" />
864833
</provider>
865834
</entry>
866835
<entry file="file://$PROJECT_DIR$/src/main/java/net/zetetic/ZeteticContentProvider.java">
867836
<provider selected="true" editor-type-id="text-editor">
868-
<state line="29" column="0" selection-start="743" selection-end="743" vertical-scroll-proportion="0.43421054">
837+
<state line="29" column="0" selection-start="743" selection-end="743" vertical-scroll-proportion="0.43421054" />
838+
</provider>
839+
</entry>
840+
<entry file="file://$PROJECT_DIR$/src/main/java/net/zetetic/tests/SQLCipherTest.java">
841+
<provider selected="true" editor-type-id="text-editor">
842+
<state line="27" column="0" selection-start="768" selection-end="768" vertical-scroll-proportion="0.45394737" />
843+
</provider>
844+
</entry>
845+
<entry file="file://$PROJECT_DIR$/src/main/java/net/zetetic/tests/AES128CipherTest.java">
846+
<provider selected="true" editor-type-id="text-editor">
847+
<state line="34" column="61" selection-start="1025" selection-end="1025" vertical-scroll-proportion="0.58651686">
869848
<folding />
870849
</state>
871850
</provider>
872851
</entry>
873-
<entry file="file://$PROJECT_DIR$/src/main/java/net/zetetic/tests/SQLCipherTest.java">
852+
<entry file="file://$PROJECT_DIR$/src/main/java/net/zetetic/tests/AttachNewDatabaseTest.java">
874853
<provider selected="true" editor-type-id="text-editor">
875-
<state line="27" column="0" selection-start="768" selection-end="768" vertical-scroll-proportion="0.45394737">
854+
<state line="32" column="5" selection-start="1294" selection-end="1294" vertical-scroll-proportion="0.57236844">
876855
<folding />
877856
</state>
878857
</provider>
879858
</entry>
880859
<entry file="file://$PROJECT_DIR$/src/main/java/net/zetetic/ZeteticApplication.java">
881860
<provider selected="true" editor-type-id="text-editor">
882-
<state line="34" column="0" selection-start="865" selection-end="865" vertical-scroll-proportion="0.0">
861+
<state line="37" column="5" selection-start="986" selection-end="986" vertical-scroll-proportion="0.40789473">
883862
<folding />
884863
</state>
885864
</provider>
886865
</entry>
887-
<entry file="file://$PROJECT_DIR$/src/main/java/net/zetetic/tests/AES128CipherTest.java">
866+
<entry file="file://$PROJECT_DIR$/src/main/java/net/zetetic/tests/MigrationFromDatabaseFormat1To2.java">
888867
<provider selected="true" editor-type-id="text-editor">
889-
<state line="34" column="61" selection-start="1025" selection-end="1025" vertical-scroll-proportion="0.0">
868+
<state line="19" column="88" selection-start="774" selection-end="774" vertical-scroll-proportion="0.0">
890869
<folding />
891870
</state>
892871
</provider>
893872
</entry>
894873
<entry file="file://$PROJECT_DIR$/src/main/java/net/zetetic/tests/RawExecSQLTest.java">
895874
<provider selected="true" editor-type-id="text-editor">
896-
<state line="19" column="3" selection-start="640" selection-end="673" vertical-scroll-proportion="0.0">
897-
<folding />
898-
</state>
875+
<state line="19" column="3" selection-start="640" selection-end="673" vertical-scroll-proportion="0.0" />
899876
</provider>
900877
</entry>
901878
<entry file="file://$PROJECT_DIR$/src/main/java/net/zetetic/tests/TestSuiteRunner.java">
902879
<provider selected="true" editor-type-id="text-editor">
903-
<state line="42" column="8" selection-start="1139" selection-end="1139" vertical-scroll-proportion="0.0">
880+
<state line="26" column="13" selection-start="647" selection-end="647" vertical-scroll-proportion="0.0">
904881
<folding />
905882
</state>
906883
</provider>
907884
</entry>
908-
<entry file="file://$PROJECT_DIR$/src/main/java/net/zetetic/tests/MigrationFromDatabaseFormat1To2.java">
885+
<entry file="file://$PROJECT_DIR$/src/main/java/net/zetetic/tests/AttachExistingDatabaseTest.java">
909886
<provider selected="true" editor-type-id="text-editor">
910-
<state line="18" column="0" selection-start="466" selection-end="466" vertical-scroll-proportion="0.0794702">
911-
<folding>
912-
<element signature="imports" expanded="true" />
913-
</folding>
887+
<state line="4" column="38" selection-start="144" selection-end="144" vertical-scroll-proportion="0.0794702">
888+
<folding />
914889
</state>
915890
</provider>
916891
</entry>

src/main/java/net/zetetic/ZeteticApplication.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import android.app.Application;
55
import net.sqlcipher.database.SQLiteDatabase;
66

7-
import java.io.File;
7+
import java.io.*;
88

99
public class ZeteticApplication extends Application {
1010

@@ -13,6 +13,7 @@ public class ZeteticApplication extends Application {
1313
private static ZeteticApplication instance;
1414
private Activity activity;
1515
public static final String TAG = "Zetetic";
16+
public static final String ONE_X_DATABASE = "1x.db";
1617

1718
public ZeteticApplication(){
1819
instance = this;
@@ -39,4 +40,26 @@ public void prepareDatabaseEnvironment(){
3940
public SQLiteDatabase createDatabase(File databaseFile){
4041
return SQLiteDatabase.openOrCreateDatabase(databaseFile, DATABASE_PASSWORD, null);
4142
}
43+
44+
public void extract1xDatabaseToDatabaseDirectory() throws IOException {
45+
46+
int length;
47+
InputStream sourceDatabase = ZeteticApplication.getInstance().getAssets().open(ONE_X_DATABASE);
48+
File destinationPath = ZeteticApplication.getInstance().getDatabasePath(ONE_X_DATABASE);
49+
OutputStream destination = new FileOutputStream(destinationPath);
50+
51+
byte[] buffer = new byte[4096];
52+
while((length = sourceDatabase.read(buffer)) > 0){
53+
destination.write(buffer, 0, length);
54+
}
55+
sourceDatabase.close();
56+
destination.flush();
57+
destination.close();
58+
}
59+
60+
public void delete1xDatabase() {
61+
62+
File databaseFile = getInstance().getDatabasePath(ONE_X_DATABASE);
63+
databaseFile.delete();
64+
}
4265
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package net.zetetic.tests;
2+
3+
import android.database.Cursor;
4+
import net.sqlcipher.database.SQLiteDatabase;
5+
import net.zetetic.ZeteticApplication;
6+
7+
import java.io.File;
8+
import java.io.IOException;
9+
10+
public class AttachExistingDatabaseTest extends SQLCipherTest {
11+
12+
@Override
13+
public boolean execute(SQLiteDatabase database) {
14+
15+
try {
16+
ZeteticApplication.getInstance().extract1xDatabaseToDatabaseDirectory();
17+
File other = ZeteticApplication.getInstance().getDatabasePath(ZeteticApplication.ONE_X_DATABASE);
18+
String otherPath = other.getAbsolutePath();
19+
String attach = String.format("attach database ? as other key ?");
20+
database.rawExecSQL("pragma cipher_default_use_hmac = off");
21+
database.rawExecSQL(String.format("pragma key='%s'", ZeteticApplication.DATABASE_PASSWORD));
22+
database.execSQL(attach, new Object[]{otherPath, ZeteticApplication.DATABASE_PASSWORD});
23+
Cursor result = database.rawQuery("select * from other.t1", new String[]{});
24+
String a = "";
25+
String b = "";
26+
if(result != null){
27+
result.moveToFirst();
28+
a = result.getString(0);
29+
b = result.getString(1);
30+
result.close();
31+
}
32+
database.execSQL("detach database other");
33+
return a.length() > 0 && b.length() > 0;
34+
} catch (IOException e) {
35+
return false;
36+
}
37+
finally {
38+
ZeteticApplication.getInstance().delete1xDatabase();
39+
}
40+
}
41+
42+
@Override
43+
public String getName() {
44+
return "Attach Existing Database Test";
45+
}
46+
}

0 commit comments

Comments
 (0)