Skip to content

Commit d05e10e

Browse files
BillCarsonFrbmarty
andauthored
crypto migration tests (#7645)
Crypto migration tests Co-authored-by: Benoit Marty <[email protected]>
1 parent 250bd9c commit d05e10e

File tree

6 files changed

+125
-0
lines changed

6 files changed

+125
-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

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<!--- TOC -->
2+
3+
* [Testing database migration](#testing-database-migration)
4+
* [Creating a reference database](#creating-a-reference-database)
5+
* [Testing](#testing)
6+
7+
<!--- END -->
8+
9+
## Testing database migration
10+
11+
### Creating a reference database
12+
13+
Databases are encrypted, the key to decrypt is needed to setup the test.
14+
A special build property must be enabled to extract it.
15+
16+
Set `vector.debugPrivateData=true` in `~/.gradle/gradle.properties` (to avoid committing by mistake)
17+
18+
Launch the app in your emulator, login and use the app to fill up the database.
19+
20+
Save the key for the tested database
21+
```
22+
RealmKeysUtils W Database key for alias `session_db_fe9f212a611ccf6dea1141777065ed0a`: 935a6dfa0b0fc5cce1414194ed190....
23+
RealmKeysUtils W Database key for alias `crypto_module_fe9f212a611ccf6dea1141777065ed0a`: 7b9a21a8a311e85d75b069a343.....
24+
```
25+
26+
27+
Use the [Device File Explorer](https://developer.android.com/studio/debug/device-file-explorer) to extrat the database file from the emulator.
28+
29+
Go to `data/data/im.vector.app.debug/files/<hash>/`
30+
Pick the database you want to test (name can be found in SessionRealmConfigurationFactory):
31+
- crypto_store.realm for crypto
32+
- disk_store.realm for session
33+
- etc...
34+
35+
Download the file on your disk
36+
37+
### Testing
38+
39+
Copy the file in `src/AndroidTest/assets`
40+
41+
see `CryptoSanityMigrationTest` or `RealmSessionStoreMigration43Test` for sample tests.
42+
43+
There are already some databases in the assets folder.
44+
The existing test will properly detect schema changes, and fail with such errors if a migration is missing:
45+
46+
```
47+
io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors:
48+
- Property 'CryptoMetadataEntity.foo' has been added.
49+
```
50+
51+
If you want to test properly more complex database migration (dynamic transforms) ensure that the database contains
52+
the entity you want to migrate.
53+
54+
You can explore the database with [realm studio](https://www.mongodb.com/docs/realm/studio/) if needed.
55+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com./spec/v1
2+
oid sha256:a7acd69f37612bab0a1ab7f456656712d7ba19dbb679f81b97b58ef44e239f42
3+
size 8523776
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 0L
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)