Unit tests for computing trust level of device

This commit is contained in:
Maxime NATUREL 2022-09-01 16:11:38 +02:00
parent af985d9b1f
commit 384c118b8d
8 changed files with 272 additions and 7 deletions

View File

@ -19,7 +19,6 @@ package im.vector.app.features.settings.devices
import org.matrix.android.sdk.api.session.crypto.model.RoomEncryptionTrustLevel
import javax.inject.Inject
// TODO add unit tests
class GetEncryptionTrustLevelForCurrentDeviceUseCase @Inject constructor() {
fun execute(trustMSK: Boolean, legacyMode: Boolean): RoomEncryptionTrustLevel {

View File

@ -20,7 +20,6 @@ import org.matrix.android.sdk.api.session.crypto.model.CryptoDeviceInfo
import org.matrix.android.sdk.api.session.crypto.model.RoomEncryptionTrustLevel
import javax.inject.Inject
// TODO add unit tests
class GetEncryptionTrustLevelForDeviceUseCase @Inject constructor(
private val getEncryptionTrustLevelForCurrentDeviceUseCase: GetEncryptionTrustLevelForCurrentDeviceUseCase,
private val getEncryptionTrustLevelForOtherDeviceUseCase: GetEncryptionTrustLevelForOtherDeviceUseCase,

View File

@ -20,7 +20,6 @@ import org.matrix.android.sdk.api.session.crypto.crosssigning.DeviceTrustLevel
import org.matrix.android.sdk.api.session.crypto.model.RoomEncryptionTrustLevel
import javax.inject.Inject
// TODO add unit tests
class GetEncryptionTrustLevelForOtherDeviceUseCase @Inject constructor() {
fun execute(trustMSK: Boolean, legacyMode: Boolean, deviceTrustLevel: DeviceTrustLevel?): RoomEncryptionTrustLevel {

View File

@ -19,7 +19,6 @@ package im.vector.app.features.settings.devices
import im.vector.app.test.fakes.FakeActiveSessionHolder
import io.mockk.every
import io.mockk.mockk
import kotlinx.coroutines.test.runTest
import org.amshove.kluent.shouldBeEqualTo
import org.junit.Test
import org.matrix.android.sdk.api.auth.data.SessionParams
@ -35,7 +34,7 @@ class GetCurrentSessionCrossSigningInfoUseCaseTest {
)
@Test
fun `given the active session when getting cross signing info then the result is correct`() = runTest {
fun `given the active session when getting cross signing info then the result is correct`() {
val sessionParams = mockk<SessionParams>()
every { sessionParams.deviceId } returns A_DEVICE_ID
fakeActiveSessionHolder.fakeSession.givenSessionParams(sessionParams)

View File

@ -0,0 +1,56 @@
/*
* Copyright (c) 2022 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.app.features.settings.devices
import org.amshove.kluent.shouldBeEqualTo
import org.junit.Test
import org.matrix.android.sdk.api.session.crypto.model.RoomEncryptionTrustLevel
class GetEncryptionTrustLevelForCurrentDeviceUseCaseTest {
private val getEncryptionTrustLevelForCurrentDeviceUseCase = GetEncryptionTrustLevelForCurrentDeviceUseCase()
@Test
fun `given in legacy mode when computing trust level then device is trusted`() {
val trustMSK = false
val legacyMode = true
val result = getEncryptionTrustLevelForCurrentDeviceUseCase.execute(trustMSK = trustMSK, legacyMode = legacyMode)
result shouldBeEqualTo RoomEncryptionTrustLevel.Trusted
}
@Test
fun `given trustMSK is true and not in legacy mode when computing trust level then device is trusted`() {
val trustMSK = true
val legacyMode = false
val result = getEncryptionTrustLevelForCurrentDeviceUseCase.execute(trustMSK = trustMSK, legacyMode = legacyMode)
result shouldBeEqualTo RoomEncryptionTrustLevel.Trusted
}
@Test
fun `given trustMSK is false and not in legacy mode when computing trust level then device is unverified`() {
val trustMSK = false
val legacyMode = false
val result = getEncryptionTrustLevelForCurrentDeviceUseCase.execute(trustMSK = trustMSK, legacyMode = legacyMode)
result shouldBeEqualTo RoomEncryptionTrustLevel.Warning
}
}

View File

@ -0,0 +1,114 @@
/*
* Copyright (c) 2022 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.app.features.settings.devices
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import org.amshove.kluent.shouldBeEqualTo
import org.junit.Test
import org.matrix.android.sdk.api.session.crypto.crosssigning.DeviceTrustLevel
import org.matrix.android.sdk.api.session.crypto.model.CryptoDeviceInfo
import org.matrix.android.sdk.api.session.crypto.model.RoomEncryptionTrustLevel
private const val A_DEVICE_ID = "device-id"
private const val A_DEVICE_ID_2 = "device-id-2"
class GetEncryptionTrustLevelForDeviceUseCaseTest {
private val getEncryptionTrustLevelForCurrentDeviceUseCase = mockk<GetEncryptionTrustLevelForCurrentDeviceUseCase>()
private val getEncryptionTrustLevelForOtherDeviceUseCase = mockk<GetEncryptionTrustLevelForOtherDeviceUseCase>()
private val getEncryptionTrustLevelForDeviceUseCase = GetEncryptionTrustLevelForDeviceUseCase(
getEncryptionTrustLevelForCurrentDeviceUseCase = getEncryptionTrustLevelForCurrentDeviceUseCase,
getEncryptionTrustLevelForOtherDeviceUseCase = getEncryptionTrustLevelForOtherDeviceUseCase,
)
@Test
fun `given is current device when computing trust level then the correct sub use case result is returned`() {
val currentSessionCrossSigningInfo = givenCurrentSessionCrossSigningInfo(
deviceId = A_DEVICE_ID,
isCrossSigningInitialized = true,
isCrossSigningVerified = false
)
val cryptoDeviceInfo = givenCryptoDeviceInfo(
deviceId = A_DEVICE_ID,
trustLevel = null
)
val trustLevel = RoomEncryptionTrustLevel.Trusted
every { getEncryptionTrustLevelForCurrentDeviceUseCase.execute(any(), any()) } returns trustLevel
val result = getEncryptionTrustLevelForDeviceUseCase.execute(currentSessionCrossSigningInfo, cryptoDeviceInfo)
result shouldBeEqualTo trustLevel
verify {
getEncryptionTrustLevelForCurrentDeviceUseCase.execute(
trustMSK = currentSessionCrossSigningInfo.isCrossSigningVerified,
legacyMode = !currentSessionCrossSigningInfo.isCrossSigningInitialized
)
}
}
@Test
fun `given is not current device when computing trust level then the correct sub use case result is returned`() {
val currentSessionCrossSigningInfo = givenCurrentSessionCrossSigningInfo(
deviceId = A_DEVICE_ID,
isCrossSigningInitialized = true,
isCrossSigningVerified = false
)
val cryptoDeviceInfo = givenCryptoDeviceInfo(
deviceId = A_DEVICE_ID_2,
trustLevel = null
)
val trustLevel = RoomEncryptionTrustLevel.Trusted
every { getEncryptionTrustLevelForOtherDeviceUseCase.execute(any(), any(), any()) } returns trustLevel
val result = getEncryptionTrustLevelForDeviceUseCase.execute(currentSessionCrossSigningInfo, cryptoDeviceInfo)
result shouldBeEqualTo trustLevel
verify {
getEncryptionTrustLevelForOtherDeviceUseCase.execute(
trustMSK = currentSessionCrossSigningInfo.isCrossSigningVerified,
legacyMode = !currentSessionCrossSigningInfo.isCrossSigningInitialized,
deviceTrustLevel = cryptoDeviceInfo.trustLevel
)
}
}
private fun givenCurrentSessionCrossSigningInfo(
deviceId: String?,
isCrossSigningInitialized: Boolean,
isCrossSigningVerified: Boolean
): CurrentSessionCrossSigningInfo {
return CurrentSessionCrossSigningInfo(
deviceId = deviceId,
isCrossSigningInitialized = isCrossSigningInitialized,
isCrossSigningVerified = isCrossSigningVerified
)
}
private fun givenCryptoDeviceInfo(
deviceId: String,
trustLevel: DeviceTrustLevel?
): CryptoDeviceInfo {
return CryptoDeviceInfo(
userId = "",
deviceId = deviceId,
trustLevel = trustLevel
)
}
}

View File

@ -0,0 +1,100 @@
/*
* Copyright (c) 2022 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.app.features.settings.devices
import org.amshove.kluent.shouldBeEqualTo
import org.junit.Test
import org.matrix.android.sdk.api.session.crypto.crosssigning.DeviceTrustLevel
import org.matrix.android.sdk.api.session.crypto.model.RoomEncryptionTrustLevel
class GetEncryptionTrustLevelForOtherDeviceUseCaseTest {
private val getEncryptionTrustLevelForOtherDeviceUseCase = GetEncryptionTrustLevelForOtherDeviceUseCase()
@Test
fun `given in legacy mode and device locally verified when computing trust level then device is trusted`() {
val trustMSK = false
val legacyMode = true
val deviceTrustLevel = givenDeviceTrustLevel(locallyVerified = true, crossSigningVerified = false)
val result = getEncryptionTrustLevelForOtherDeviceUseCase.execute(trustMSK = trustMSK, legacyMode = legacyMode, deviceTrustLevel = deviceTrustLevel)
result shouldBeEqualTo RoomEncryptionTrustLevel.Trusted
}
@Test
fun `given in legacy mode and device not locally verified when computing trust level then device is unverified`() {
val trustMSK = false
val legacyMode = true
val deviceTrustLevel = givenDeviceTrustLevel(locallyVerified = false, crossSigningVerified = false)
val result = getEncryptionTrustLevelForOtherDeviceUseCase.execute(trustMSK = trustMSK, legacyMode = legacyMode, deviceTrustLevel = deviceTrustLevel)
result shouldBeEqualTo RoomEncryptionTrustLevel.Warning
}
@Test
fun `given trustMSK is true and not in legacy mode and device cross signing verified when computing trust level then device is trusted`() {
val trustMSK = true
val legacyMode = false
val deviceTrustLevel = givenDeviceTrustLevel(locallyVerified = false, crossSigningVerified = true)
val result = getEncryptionTrustLevelForOtherDeviceUseCase.execute(trustMSK = trustMSK, legacyMode = legacyMode, deviceTrustLevel = deviceTrustLevel)
result shouldBeEqualTo RoomEncryptionTrustLevel.Trusted
}
@Test
fun `given trustMSK is true and not in legacy mode and device locally verified when computing trust level then device has default trust level`() {
val trustMSK = true
val legacyMode = false
val deviceTrustLevel = givenDeviceTrustLevel(locallyVerified = true, crossSigningVerified = false)
val result = getEncryptionTrustLevelForOtherDeviceUseCase.execute(trustMSK = trustMSK, legacyMode = legacyMode, deviceTrustLevel = deviceTrustLevel)
result shouldBeEqualTo RoomEncryptionTrustLevel.Default
}
@Test
fun `given trustMSK is true and not in legacy mode and device not verified when computing trust level then device is unverified`() {
val trustMSK = true
val legacyMode = false
val deviceTrustLevel = givenDeviceTrustLevel(locallyVerified = false, crossSigningVerified = false)
val result = getEncryptionTrustLevelForOtherDeviceUseCase.execute(trustMSK = trustMSK, legacyMode = legacyMode, deviceTrustLevel = deviceTrustLevel)
result shouldBeEqualTo RoomEncryptionTrustLevel.Warning
}
@Test
fun `given trustMSK is false and not in legacy mode when computing trust level then device has default trust level`() {
val trustMSK = false
val legacyMode = false
val deviceTrustLevel = givenDeviceTrustLevel(locallyVerified = false, crossSigningVerified = false)
val result = getEncryptionTrustLevelForOtherDeviceUseCase.execute(trustMSK = trustMSK, legacyMode = legacyMode, deviceTrustLevel = deviceTrustLevel)
result shouldBeEqualTo RoomEncryptionTrustLevel.Default
}
private fun givenDeviceTrustLevel(locallyVerified: Boolean?, crossSigningVerified: Boolean): DeviceTrustLevel {
return DeviceTrustLevel(
crossSigningVerified = crossSigningVerified,
locallyVerified = locallyVerified
)
}
}

View File

@ -26,7 +26,6 @@ import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import org.junit.Rule
import org.junit.Test
import org.matrix.android.sdk.api.auth.data.SessionParams
@ -52,7 +51,7 @@ class SessionOverviewViewModelTest {
)
@Test
fun `given the viewModel has been initialized then viewState is updated with session info`() = runTest {
fun `given the viewModel has been initialized then viewState is updated with session info`() {
val sessionParams = givenIdForSession(A_SESSION_ID)
val deviceFullInfo = mockk<DeviceFullInfo>()
every { getDeviceFullInfoUseCase.execute(A_SESSION_ID) } returns flowOf(Optional(deviceFullInfo))