diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/GetEncryptionTrustLevelForCurrentDeviceUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/GetEncryptionTrustLevelForCurrentDeviceUseCase.kt index eaa72b424a..0d30aba318 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/GetEncryptionTrustLevelForCurrentDeviceUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/GetEncryptionTrustLevelForCurrentDeviceUseCase.kt @@ -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 { diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/GetEncryptionTrustLevelForDeviceUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/GetEncryptionTrustLevelForDeviceUseCase.kt index d988f728ae..e5ef4b446b 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/GetEncryptionTrustLevelForDeviceUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/GetEncryptionTrustLevelForDeviceUseCase.kt @@ -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, diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/GetEncryptionTrustLevelForOtherDeviceUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/GetEncryptionTrustLevelForOtherDeviceUseCase.kt index 41cdae23a4..11bc3a8ede 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/GetEncryptionTrustLevelForOtherDeviceUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/GetEncryptionTrustLevelForOtherDeviceUseCase.kt @@ -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 { diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/GetCurrentSessionCrossSigningInfoUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/GetCurrentSessionCrossSigningInfoUseCaseTest.kt index f3684fd8cf..7c8ee008eb 100644 --- a/vector/src/test/java/im/vector/app/features/settings/devices/GetCurrentSessionCrossSigningInfoUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/devices/GetCurrentSessionCrossSigningInfoUseCaseTest.kt @@ -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() every { sessionParams.deviceId } returns A_DEVICE_ID fakeActiveSessionHolder.fakeSession.givenSessionParams(sessionParams) diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/GetEncryptionTrustLevelForCurrentDeviceUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/GetEncryptionTrustLevelForCurrentDeviceUseCaseTest.kt new file mode 100644 index 0000000000..830eab5dcb --- /dev/null +++ b/vector/src/test/java/im/vector/app/features/settings/devices/GetEncryptionTrustLevelForCurrentDeviceUseCaseTest.kt @@ -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 + } +} diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/GetEncryptionTrustLevelForDeviceUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/GetEncryptionTrustLevelForDeviceUseCaseTest.kt new file mode 100644 index 0000000000..8d54b31ab4 --- /dev/null +++ b/vector/src/test/java/im/vector/app/features/settings/devices/GetEncryptionTrustLevelForDeviceUseCaseTest.kt @@ -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() + private val getEncryptionTrustLevelForOtherDeviceUseCase = mockk() + + 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 + ) + } +} diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/GetEncryptionTrustLevelForOtherDeviceUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/GetEncryptionTrustLevelForOtherDeviceUseCaseTest.kt new file mode 100644 index 0000000000..9dc87c2a16 --- /dev/null +++ b/vector/src/test/java/im/vector/app/features/settings/devices/GetEncryptionTrustLevelForOtherDeviceUseCaseTest.kt @@ -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 + ) + } +} diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModelTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModelTest.kt index f15bc0860c..10b1c0fdb1 100644 --- a/vector/src/test/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModelTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/overview/SessionOverviewViewModelTest.kt @@ -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() every { getDeviceFullInfoUseCase.execute(A_SESSION_ID) } returns flowOf(Optional(deviceFullInfo))