Skip to content

Commit 1e8afaf

Browse files
Adding test to show migration of user_version when using the SQLiteDatabase.upgradeDatabaseFormatFromVersion1To2.
1 parent 8bf7514 commit 1e8afaf

File tree

7 files changed

+204
-184
lines changed

7 files changed

+204
-184
lines changed

assets/1x-user-version.db

2 KB
Binary file not shown.

net.zetetic.sqlcipher.test.iml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<option name="PROGUARD_CFG_PATH" value="/proguard.cfg" />
2626
<resOverlayFolders />
2727
<includeSystemProguardFile>false</includeSystemProguardFile>
28+
<additionalNativeLibs />
2829
</configuration>
2930
</facet>
3031
</component>
@@ -33,12 +34,13 @@
3334
<output-test url="file://$MODULE_DIR$/target/test-classes" />
3435
<content url="file://$MODULE_DIR$">
3536
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
36-
<sourceFolder url="file://$MODULE_DIR$/target/generated-sources/aidl" isTestSource="false" />
3737
<sourceFolder url="file://$MODULE_DIR$/target/generated-sources/r" isTestSource="false" />
3838
<sourceFolder url="file://$MODULE_DIR$/gen" isTestSource="false" />
39+
<excludeFolder url="file://$MODULE_DIR$/target/classes" />
3940
<excludeFolder url="file://$MODULE_DIR$/target/generated-sources/combined-assets" />
4041
<excludeFolder url="file://$MODULE_DIR$/target/generated-sources/combined-resources" />
4142
<excludeFolder url="file://$MODULE_DIR$/target/generated-sources/extracted-dependencies" />
43+
<excludeFolder url="file://$MODULE_DIR$/target/test-classes" />
4244
</content>
4345
<orderEntry type="sourceFolder" forTests="false" />
4446
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.android:android:2.1.2" level="project" />

net.zetetic.sqlcipher.test.iws

Lines changed: 140 additions & 182 deletions
Large diffs are not rendered by default.

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
<androidManifestFile>${project.basedir}/AndroidManifest.xml</androidManifestFile>
5151
<assetsDirectory>${project.basedir}/assets</assetsDirectory>
5252
<resourceDirectory>${project.basedir}/res</resourceDirectory>
53-
<nativeLibrariesDirectory>${project.basedir}/src/main/native</nativeLibrariesDirectory>
53+
<nativeLibrariesDirectory>${project.basedir}/libs</nativeLibrariesDirectory>
5454
<sdk>
5555
<platform>7</platform>
5656
</sdk>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class ZeteticApplication extends Application {
1414
private Activity activity;
1515
public static final String TAG = "Zetetic";
1616
public static final String ONE_X_DATABASE = "1x.db";
17+
public static final String ONE_X_USER_VERSION_DATABASE = "1x-user-version.db";
1718
public static final String UNENCRYPTED_DATABASE = "unencrypted.db";
1819

1920
public ZeteticApplication(){
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package net.zetetic.tests;
2+
3+
import android.content.Context;
4+
import net.sqlcipher.database.SQLiteDatabase;
5+
import net.sqlcipher.database.SQLiteDatabaseHook;
6+
import net.sqlcipher.database.SQLiteOpenHelper;
7+
import net.zetetic.ZeteticApplication;
8+
9+
import java.io.File;
10+
11+
public class MigrationUserVersion extends SQLCipherTest {
12+
13+
@Override
14+
public boolean execute(SQLiteDatabase database) {
15+
16+
try {
17+
String password = ZeteticApplication.DATABASE_PASSWORD;
18+
ZeteticApplication.getInstance().extractAssetToDatabaseDirectory(ZeteticApplication.ONE_X_USER_VERSION_DATABASE);
19+
File sourceDatabase = ZeteticApplication.getInstance().getDatabasePath(ZeteticApplication.ONE_X_USER_VERSION_DATABASE);
20+
SQLiteDatabase originalDatabase = SQLiteDatabase.openOrCreateDatabase(sourceDatabase, password, null, new SQLiteDatabaseHook() {
21+
public void preKey(SQLiteDatabase sqLiteDatabase) {
22+
sqLiteDatabase.rawExecSQL("PRAGMA cipher_default_use_hmac=off;");
23+
}
24+
public void postKey(SQLiteDatabase sqLiteDatabase) {}
25+
});
26+
int userVersion = originalDatabase.getVersion();
27+
originalDatabase.close();
28+
SQLiteDatabase.upgradeDatabaseFormatFromVersion1To2(sourceDatabase, password);
29+
SQLiteDatabase migratedDatabase = SQLiteDatabase.openOrCreateDatabase(sourceDatabase, password, null, null);
30+
migratedDatabase.setVersion(userVersion);
31+
return migratedDatabase.getVersion() == userVersion;
32+
} catch (Exception e) {
33+
return false;
34+
}
35+
}
36+
37+
@Override
38+
public String getName() {
39+
return "Database 1.x - 2 Migration with user_version";
40+
}
41+
42+
class SQLCipherOpenHelper extends SQLiteOpenHelper {
43+
44+
public SQLCipherOpenHelper(Context context, String name) {
45+
super(context, name, null, 2);
46+
}
47+
48+
@Override
49+
public void onCreate(SQLiteDatabase sqLiteDatabase) {
50+
51+
}
52+
53+
@Override
54+
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
55+
56+
}
57+
}
58+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ private List<SQLCipherTest> getTestsToRun(){
5959
tests.add(new AutoVacuumOverReadTest());
6060
tests.add(new ReadableWritableAccessTest());
6161
tests.add(new VerifyOnUpgradeIsCalledTest());
62+
tests.add(new MigrationUserVersion());
6263
return tests;
6364
}
6465
}

0 commit comments

Comments
 (0)