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