Realm crypto: update test

This commit is contained in:
ganfra 2022-08-05 19:29:10 +02:00
parent 2cdd05072b
commit f25764d09a
4 changed files with 23 additions and 18 deletions

View File

@ -152,6 +152,7 @@ dependencies {
// Lifecycle
implementation libs.androidx.lifecycleCommon
implementation libs.androidx.lifecycleProcess
implementation libs.androidx.lifecycleLivedata
// Network
implementation libs.squareup.retrofit
@ -226,6 +227,7 @@ dependencies {
androidTestImplementation libs.mockk.mockkAndroid
androidTestImplementation libs.androidx.coreTesting
androidTestImplementation libs.jetbrains.coroutinesAndroid
androidTestImplementation libs.jetbrains.coroutinesTest
// Plant Timber tree for test
androidTestImplementation libs.tests.timberJunitRule

View File

@ -16,23 +16,31 @@
package org.matrix.android.sdk.internal.crypto
import io.realm.RealmConfiguration
import io.realm.kotlin.RealmConfiguration
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import org.matrix.android.sdk.internal.crypto.store.IMXCryptoStore
import org.matrix.android.sdk.internal.crypto.store.db.CRYPTO_REALM_SCHEMA
import org.matrix.android.sdk.internal.crypto.store.db.RealmCryptoStore
import org.matrix.android.sdk.internal.crypto.store.db.RealmCryptoStoreModule
import org.matrix.android.sdk.internal.crypto.store.db.mapper.CrossSigningKeysMapper
import org.matrix.android.sdk.internal.database.RealmInstance
import org.matrix.android.sdk.internal.di.MoshiProvider
import org.matrix.android.sdk.internal.util.time.DefaultClock
import kotlin.random.Random
internal class CryptoStoreHelper {
fun createStore(): IMXCryptoStore {
@OptIn(ExperimentalCoroutinesApi::class)
fun createStore(coroutineScope: CoroutineScope): IMXCryptoStore {
val realmConfiguration = RealmConfiguration.Builder(CRYPTO_REALM_SCHEMA)
.name("test.realm")
.deleteRealmIfMigrationNeeded()
.build()
val realmInstance = RealmInstance(coroutineScope, realmConfiguration, UnconfinedTestDispatcher())
return RealmCryptoStore(
realmConfiguration = RealmConfiguration.Builder()
.name("test.realm")
.modules(RealmCryptoStoreModule())
.build(),
realmInstance = realmInstance,
crossSigningKeysMapper = CrossSigningKeysMapper(MoshiProvider.providesMoshi()),
userId = "userId_" + Random.nextInt(),
deviceId = "deviceId_sample",

View File

@ -17,12 +17,11 @@
package org.matrix.android.sdk.internal.crypto
import androidx.test.ext.junit.runners.AndroidJUnit4
import io.realm.Realm
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals
import org.junit.Assert.assertNull
import org.junit.Before
import org.junit.Ignore
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
@ -37,8 +36,8 @@ import org.matrix.olm.OlmSession
private const val DUMMY_DEVICE_KEY = "DeviceKey"
@OptIn(ExperimentalCoroutinesApi::class)
@RunWith(AndroidJUnit4::class)
@Ignore
class CryptoStoreTest : InstrumentedTest {
@get:Rule val rule = RetryTestRule(3)
@ -46,11 +45,6 @@ class CryptoStoreTest : InstrumentedTest {
private val cryptoStoreHelper = CryptoStoreHelper()
private val clock = DefaultClock()
@Before
fun setup() {
Realm.init(context())
}
// @Test
// fun test_metadata_realm_ok() {
// val cryptoStore: IMXCryptoStore = cryptoStoreHelper.createStore()
@ -69,11 +63,11 @@ class CryptoStoreTest : InstrumentedTest {
// }
@Test
fun test_lastSessionUsed() {
fun test_lastSessionUsed() = runTest {
// Ensure Olm is initialized
OlmManager()
val cryptoStore: IMXCryptoStore = cryptoStoreHelper.createStore()
val cryptoStore: IMXCryptoStore = cryptoStoreHelper.createStore(this)
assertNull(cryptoStore.getLastUsedSessionId(DUMMY_DEVICE_KEY))

View File

@ -32,6 +32,7 @@ import org.matrix.android.sdk.internal.database.helper.addTimelineEvent
import org.matrix.android.sdk.internal.database.mapper.toEntity
import org.matrix.android.sdk.internal.database.model.ChunkEntity
import org.matrix.android.sdk.internal.database.model.SessionRealmModule
import org.matrix.android.sdk.internal.extensions.realm
import org.matrix.android.sdk.internal.session.room.timeline.PaginationDirection
import org.matrix.android.sdk.internal.util.time.DefaultClock
import org.matrix.android.sdk.session.room.timeline.RoomDataHelper.createFakeMessageEvent