Skip to content

Commit 53bf09d

Browse files
Merge branch 'vitorhugods-master'
2 parents 454149c + c12cef9 commit 53bf09d

File tree

3 files changed

+135
-3
lines changed

3 files changed

+135
-3
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package net.zetetic.database.sqlcipher_cts;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import android.content.Context;
6+
7+
import androidx.annotation.CallSuper;
8+
import androidx.annotation.NonNull;
9+
import androidx.sqlite.db.SupportSQLiteDatabase;
10+
import androidx.sqlite.db.SupportSQLiteOpenHelper;
11+
import androidx.test.ext.junit.runners.AndroidJUnit4;
12+
import androidx.test.platform.app.InstrumentationRegistry;
13+
14+
import net.zetetic.database.sqlcipher.SupportHelper;
15+
16+
import org.junit.Before;
17+
import org.junit.Test;
18+
import org.junit.runner.RunWith;
19+
20+
@RunWith(AndroidJUnit4.class)
21+
public class SupportHelperTest {
22+
23+
private static final String DATABASE_NAME = "DB-Test.db";
24+
private static final int CREATION_INDEX = 0;
25+
private static final int UPGRADE_INDEX = 1;
26+
27+
@Before
28+
public void setup() {
29+
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
30+
System.loadLibrary("sqlcipher");
31+
for (String databaseName : context.databaseList()) {
32+
context.deleteDatabase(databaseName);
33+
}
34+
}
35+
36+
@Test
37+
public void shouldCreateDatabaseNormallyWithInitialVersion() {
38+
FakeCallback callbackWrapper = new FakeCallback(1);
39+
40+
SupportSQLiteOpenHelper.Configuration configuration = createConfiguration(callbackWrapper);
41+
SupportHelper helper = new SupportHelper(configuration, null, null, true);
42+
43+
helper.getWritableDatabase();
44+
helper.close();
45+
46+
assertEquals(1, callbackWrapper.callbackCount[CREATION_INDEX]);
47+
assertEquals(0, callbackWrapper.callbackCount[UPGRADE_INDEX]);
48+
}
49+
50+
@Test
51+
public void shouldRunUpgradeFromVersion1ToVersion2() {
52+
FakeCallback initialCallback = new FakeCallback(1);
53+
54+
SupportHelper initialHelper = new SupportHelper(createConfiguration(initialCallback), null, null, true);
55+
56+
initialHelper.getWritableDatabase();
57+
initialHelper.close();
58+
59+
assertEquals(1, initialCallback.callbackCount[CREATION_INDEX]);
60+
assertEquals(0, initialCallback.callbackCount[UPGRADE_INDEX]);
61+
62+
FakeCallback callbackWrapper = new FakeCallback(2);
63+
64+
// minSupportedVersion = 1
65+
SupportHelper helper = new SupportHelper(createConfiguration(callbackWrapper), null, null, true, 1);
66+
67+
helper.getWritableDatabase();
68+
helper.close();
69+
70+
assertEquals(0, callbackWrapper.callbackCount[CREATION_INDEX]);
71+
assertEquals(1, callbackWrapper.callbackCount[UPGRADE_INDEX]);
72+
}
73+
74+
private SupportSQLiteOpenHelper.Configuration createConfiguration(SupportSQLiteOpenHelper.Callback callback) {
75+
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
76+
return SupportSQLiteOpenHelper.Configuration.builder(context)
77+
.name(DATABASE_NAME)
78+
.callback(callback)
79+
.build();
80+
}
81+
82+
private static class FakeCallback extends SupportSQLiteOpenHelper.Callback {
83+
public final int[] callbackCount = {0, 0};
84+
85+
public FakeCallback(int version) {
86+
super(version);
87+
}
88+
89+
SupportSQLiteOpenHelper.Callback callback = new SupportSQLiteOpenHelper.Callback(version) {
90+
@Override
91+
public void onCreate(@NonNull SupportSQLiteDatabase db) {
92+
callbackCount[CREATION_INDEX] += 1;
93+
}
94+
95+
@Override
96+
public void onUpgrade(@NonNull SupportSQLiteDatabase db, int oldVersion, int newVersion) {
97+
callbackCount[UPGRADE_INDEX] += 1;
98+
}
99+
};
100+
101+
@Override
102+
@CallSuper
103+
public void onCreate(@NonNull SupportSQLiteDatabase db) {
104+
callback.onCreate(db);
105+
}
106+
107+
@Override
108+
@CallSuper
109+
public void onUpgrade(@NonNull SupportSQLiteDatabase db, int oldVersion, int newVersion) {
110+
callback.onUpgrade(db, oldVersion, newVersion);
111+
}
112+
}
113+
}

sqlcipher/src/main/java/net/zetetic/database/sqlcipher/SupportHelper.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ public class SupportHelper implements SupportSQLiteOpenHelper {
1010

1111
public SupportHelper(final Configuration configuration, byte[] password, SQLiteDatabaseHook hook,
1212
boolean enableWriteAheadLogging) {
13+
this(configuration, password, hook, enableWriteAheadLogging, configuration.callback.version);
14+
}
15+
16+
public SupportHelper(final Configuration configuration, byte[] password, SQLiteDatabaseHook hook,
17+
boolean enableWriteAheadLogging, int minimumSupportedVersion) {
1318
openHelper = new SQLiteOpenHelper(configuration.context, configuration.name, password,
14-
null, configuration.callback.version, configuration.callback.version,
15-
null, hook, enableWriteAheadLogging) {
19+
null, configuration.callback.version, minimumSupportedVersion, null, hook, enableWriteAheadLogging) {
1620
@Override
1721
public void onCreate(SQLiteDatabase db) {
1822
configuration.callback.onCreate(db);

sqlcipher/src/main/java/net/zetetic/database/sqlcipher/SupportOpenHelperFactory.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,38 @@
55

66
public class SupportOpenHelperFactory implements SupportSQLiteOpenHelper.Factory {
77

8+
private static final int UNCHANGED = -1;
9+
810
private final byte[] password;
911
private final SQLiteDatabaseHook hook;
1012
private final boolean enableWriteAheadLogging;
1113

14+
private final int minimumSupportedVersion;
15+
1216
public SupportOpenHelperFactory(byte[] password){
1317
this(password, null, false);
1418
}
1519

1620
public SupportOpenHelperFactory(byte[] password, SQLiteDatabaseHook hook, boolean enableWriteAheadLogging) {
21+
this(password, hook, enableWriteAheadLogging, UNCHANGED);
22+
}
23+
24+
public SupportOpenHelperFactory(byte[] password, SQLiteDatabaseHook hook,
25+
boolean enableWriteAheadLogging, int minimumSupportedVersion) {
1726
this.password = password;
1827
this.hook = hook;
1928
this.enableWriteAheadLogging = enableWriteAheadLogging;
29+
this.minimumSupportedVersion = minimumSupportedVersion;
2030
}
2131

2232
@NonNull
2333
@Override
2434
public SupportSQLiteOpenHelper create(@NonNull SupportSQLiteOpenHelper.Configuration configuration) {
25-
return new SupportHelper(configuration, this.password, this.hook, enableWriteAheadLogging);
35+
if (minimumSupportedVersion == UNCHANGED) {
36+
return new SupportHelper(configuration, this.password, this.hook, enableWriteAheadLogging);
37+
} else {
38+
return new SupportHelper(configuration, this.password, this.hook,
39+
enableWriteAheadLogging, minimumSupportedVersion);
40+
}
2641
}
2742
}

0 commit comments

Comments
 (0)