Test passed

This commit is contained in:
Benoit Marty 2019-05-17 15:21:44 +02:00
parent de4662b9d5
commit 340830d45f
2 changed files with 161 additions and 0 deletions

View File

@ -0,0 +1,44 @@
/*
* Copyright 2018 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package im.vector.matrix.android.internal.crypto

import im.vector.matrix.android.api.auth.data.Credentials
import im.vector.matrix.android.internal.crypto.store.IMXCryptoStore
import im.vector.matrix.android.internal.crypto.store.db.RealmCryptoStore
import im.vector.matrix.android.internal.crypto.store.db.RealmCryptoStoreModule
import io.realm.RealmConfiguration
import java.util.*

internal class CryptoStoreHelper {

fun createStore(): IMXCryptoStore {
return RealmCryptoStore(
realmConfiguration = RealmConfiguration.Builder()
.name("test.realm")
.modules(RealmCryptoStoreModule())
.build(),
credentials = createCredential())
}

fun createCredential() = Credentials(
userId = "userId_" + Random().nextInt(),
homeServer = "http://matrix.org",
accessToken = "access_token",
refreshToken = null,
deviceId = "deviceId_sample"
)
}

View File

@ -0,0 +1,117 @@
/*
* Copyright 2018 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package im.vector.matrix.android.internal.crypto

import im.vector.matrix.android.internal.crypto.model.MXOlmSession
import im.vector.matrix.android.internal.crypto.store.IMXCryptoStore
import org.junit.Assert.*
import org.junit.Test
import org.matrix.olm.OlmAccount
import org.matrix.olm.OlmManager
import org.matrix.olm.OlmSession

private const val DUMMY_DEVICE_KEY = "DeviceKey"

class CryptoStoreTest {

private val cryptoStoreHelper = CryptoStoreHelper()

@Test
fun test_metadata_realm_ok() {
val cryptoStore: IMXCryptoStore = cryptoStoreHelper.createStore()

assertFalse(cryptoStore.hasData())

cryptoStore.open()

assertEquals("deviceId_sample", cryptoStore.getDeviceId())

assertTrue(cryptoStore.hasData())

// Cleanup
cryptoStore.close()
cryptoStore.deleteStore()
}

@Test
fun test_lastSessionUsed() {
// Ensure Olm is initialized
OlmManager()

val cryptoStore: IMXCryptoStore = cryptoStoreHelper.createStore()

assertNull(cryptoStore.getLastUsedSessionId(DUMMY_DEVICE_KEY))

val olmAccount1 = OlmAccount().apply {
generateOneTimeKeys(1)
}

val olmSession1 = OlmSession().apply {
initOutboundSession(olmAccount1,
olmAccount1.identityKeys()[OlmAccount.JSON_KEY_IDENTITY_KEY],
olmAccount1.oneTimeKeys()[OlmAccount.JSON_KEY_ONE_TIME_KEY]?.values?.first())
}

val sessionId1 = olmSession1.sessionIdentifier()
val mxOlmSession1 = MXOlmSession(olmSession1)

cryptoStore.storeSession(mxOlmSession1, DUMMY_DEVICE_KEY)

assertEquals(sessionId1, cryptoStore.getLastUsedSessionId(DUMMY_DEVICE_KEY))

val olmAccount2 = OlmAccount().apply {
generateOneTimeKeys(1)
}

val olmSession2 = OlmSession().apply {
initOutboundSession(olmAccount2,
olmAccount2.identityKeys()[OlmAccount.JSON_KEY_IDENTITY_KEY],
olmAccount2.oneTimeKeys()[OlmAccount.JSON_KEY_ONE_TIME_KEY]?.values?.first())
}

val sessionId2 = olmSession2.sessionIdentifier()
val mxOlmSession2 = MXOlmSession(olmSession2)

cryptoStore.storeSession(mxOlmSession2, DUMMY_DEVICE_KEY)

// Ensure sessionIds are distinct
assertNotEquals(sessionId1, sessionId2)

// Note: we cannot be sure what will be the result of getLastUsedSessionId() here

mxOlmSession2.onMessageReceived()
cryptoStore.storeSession(mxOlmSession2, DUMMY_DEVICE_KEY)

// sessionId2 is returned now
assertEquals(sessionId2, cryptoStore.getLastUsedSessionId(DUMMY_DEVICE_KEY))

Thread.sleep(2)

mxOlmSession1.onMessageReceived()
cryptoStore.storeSession(mxOlmSession1, DUMMY_DEVICE_KEY)

// sessionId1 is returned now
assertEquals(sessionId1, cryptoStore.getLastUsedSessionId(DUMMY_DEVICE_KEY))

// Cleanup
olmSession1.releaseSession()
olmSession2.releaseSession()

olmAccount1.releaseAccount()
olmAccount2.releaseAccount()
}
}