Skip to content

Commit 45dd05a

Browse files
committed
crypto migration tests
1 parent 9349b1a commit 45dd05a

File tree

5 files changed

+114
-0
lines changed

5 files changed

+114
-0
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
**/snapshots/**/*.png filter=lfs diff=lfs merge=lfs -text
2+
**/src/androidTest/assets/*.realm filter=lfs diff=lfs merge=lfs -text

changelog.d/7645.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Crypto database migration tests

docs/database_migration_test.md

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
## Testing database migration
2+
3+
### Creating a reference database
4+
5+
Databases are encrypted, the key to decrypt is needed to setup the test.
6+
A special build property must be enabled to extract it.
7+
8+
Set `vector.debugPrivateData=true` in `gradle.properties`
9+
10+
Launch the app in your emulator, login and use the app to feel up the database.
11+
12+
Save the key for the tested database
13+
```
14+
RealmKeysUtils W Database key for alias `session_db_fe9f212a611ccf6dea1141777065ed0a`: 935a6dfa0b0fc5cce1414194ed190....
15+
RealmKeysUtils W Database key for alias `crypto_module_fe9f212a611ccf6dea1141777065ed0a`: 7b9a21a8a311e85d75b069a343.....
16+
```
17+
18+
19+
Use the [Device File Explorer](https://developer.android.com/studio/debug/device-file-explorer) to extrat the database file from the emulator.
20+
21+
Go to `data/data/im.vector.app.debug/files/<hash>/`
22+
Pick the database you want to test (name can be found in SessionRealmConfigurationFactory):
23+
- crypto_store.real for crypto
24+
- disk_store for session
25+
- etc...
26+
27+
Download the file on your disk
28+
29+
### Testing
30+
31+
Copy the file in `src/AndroidTest/assets`
32+
33+
see `CryptoSanityMigrationTest` or `RealmSessionStoreMigration43Test` for sample tests.
34+
35+
There are already some databases in the assets folder.
36+
The existing test will properly detect schema changes, and fail with such errors:
37+
38+
```
39+
io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors:
40+
- Property 'CryptoMetadataEntity.foo' has been added.
41+
```
42+
43+
If you want to test properly more complex database migration (dynamic transforms) ensure that the database contains
44+
the entity you want to migrate.
45+
46+
You can explore the database with [realm studio](https://www.mongodb.com/docs/realm/studio/) if needed.
47+
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2022 The Matrix.org Foundation C.I.C.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.matrix.android.sdk.internal.database
18+
19+
import android.content.Context
20+
import androidx.test.platform.app.InstrumentationRegistry
21+
import io.realm.Realm
22+
import org.junit.After
23+
import org.junit.Before
24+
import org.junit.Rule
25+
import org.junit.Test
26+
import org.matrix.android.sdk.internal.crypto.store.db.RealmCryptoStoreMigration
27+
import org.matrix.android.sdk.internal.crypto.store.db.RealmCryptoStoreModule
28+
import org.matrix.android.sdk.internal.util.time.Clock
29+
30+
class CryptoSanityMigrationTest {
31+
@get:Rule val configurationFactory = TestRealmConfigurationFactory()
32+
33+
lateinit var context: Context
34+
var realm: Realm? = null
35+
36+
@Before
37+
fun setUp() {
38+
context = InstrumentationRegistry.getInstrumentation().context
39+
}
40+
41+
@After
42+
fun tearDown() {
43+
realm?.close()
44+
}
45+
46+
@Test
47+
fun cryptoDatabaseShouldMigrateGracefully() {
48+
val realmName = "crypto_store_20.realm"
49+
val migration = RealmCryptoStoreMigration(object : Clock {
50+
override fun epochMillis(): Long {
51+
return System.currentTimeMillis()
52+
}
53+
})
54+
val realmConfiguration = configurationFactory.createConfiguration(
55+
realmName,
56+
"7b9a21a8a311e85d75b069a343c23fc952fc3fec5e0c83ecfa13f24b787479c487c3ed587db3dd1f5805d52041fc0ac246516e94b27ffa699ff928622e621aca",
57+
RealmCryptoStoreModule(),
58+
migration.schemaVersion,
59+
migration
60+
)
61+
configurationFactory.copyRealmFromAssets(context, realmName, realmName)
62+
63+
realm = Realm.getInstance(realmConfiguration)
64+
}
65+
}

0 commit comments

Comments
 (0)