diff --git a/cryptography/src/commonJvmAndroid/kotlin/com.wire.kalium.cryptography/MLSClientImpl.kt b/cryptography/src/commonJvmAndroid/kotlin/com.wire.kalium.cryptography/MLSClientImpl.kt index 52eab3a59df..5a2ffe35eaf 100644 --- a/cryptography/src/commonJvmAndroid/kotlin/com.wire.kalium.cryptography/MLSClientImpl.kt +++ b/cryptography/src/commonJvmAndroid/kotlin/com.wire.kalium.cryptography/MLSClientImpl.kt @@ -331,7 +331,8 @@ class MLSClientImpl( value.domain, value.certificate, toDeviceStatus(value.status), - value.thumbprint + value.thumbprint, + value.serialNumber ) } } diff --git a/cryptography/src/commonMain/kotlin/com/wire/kalium/cryptography/IDs.kt b/cryptography/src/commonMain/kotlin/com/wire/kalium/cryptography/IDs.kt index e1eb0309de6..9b28596dcda 100644 --- a/cryptography/src/commonMain/kotlin/com/wire/kalium/cryptography/IDs.kt +++ b/cryptography/src/commonMain/kotlin/com/wire/kalium/cryptography/IDs.kt @@ -77,7 +77,8 @@ data class WireIdentity( val domain: String, val certificate: String, val status: CryptoCertificateStatus, - val thumbprint: String + val thumbprint: String, + val serialNumber: String ) enum class CryptoCertificateStatus { diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index b7623238bd0..d41bff00a1b 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -57,7 +57,6 @@ faker = "1.15.0" robolectric = "4.9" stately = "2.0.6" micrometer = "1.11.3" -bouncy-castle = "1.70" konsist = "0.13.0" postgres = "1.0.2.RELEASE" r2dbc = "1.0.0.RELEASE" @@ -213,6 +212,3 @@ jna = { module = "net.java.dev.jna:jna", version.ref = "jna" } kermit = { module = "co.touchlab:kermit", version.ref = "kermit" } micrometer = { module = "io.micrometer:micrometer-registry-prometheus", version.ref = "micrometer" } slf4js = { module = "org.slf4j:slf4j-simple", version.ref = "slf4js" } - -# bouncy castle -bouncy-castle = { module = "org.bouncycastle:bcprov-jdk15on", version.ref = "bouncy-castle" } diff --git a/logic/build.gradle.kts b/logic/build.gradle.kts index b3893bf03c1..f966511c7ea 100644 --- a/logic/build.gradle.kts +++ b/logic/build.gradle.kts @@ -96,7 +96,6 @@ kotlin { dependencies { implementation(libs.jna) implementation(libs.coreCryptoJvm) - implementation(libs.bouncy.castle) } } val jvmTest by getting { diff --git a/logic/src/appleMain/kotlin/com/wire/kalium/logic/feature/e2ei/PemCertificateDecoder.kt b/logic/src/appleMain/kotlin/com/wire/kalium/logic/feature/e2ei/PemCertificateDecoder.kt deleted file mode 100644 index bf63ba3a4fd..00000000000 --- a/logic/src/appleMain/kotlin/com/wire/kalium/logic/feature/e2ei/PemCertificateDecoder.kt +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Wire - * Copyright (C) 2024 Wire Swiss GmbH - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see http://www.gnu.org/licenses/. - */ -package com.wire.kalium.logic.feature.e2ei - -import com.wire.kalium.cryptography.CryptoCertificateStatus - -actual interface PemCertificateDecoder { - actual fun decode(certificate: String, status: CryptoCertificateStatus): E2eiCertificate -} - -actual class PemCertificateDecoderImpl actual constructor( - private val x509CertificateGenerator: X509CertificateGenerator, - private val certificateStatusChecker: CertificateStatusChecker -) : PemCertificateDecoder { - override fun decode(certificate: String, status: CryptoCertificateStatus): E2eiCertificate { - TODO("Not yet implemented") - } -} diff --git a/logic/src/appleMain/kotlin/com/wire/kalium/logic/feature/e2ei/PlatformX509Certificate.kt b/logic/src/appleMain/kotlin/com/wire/kalium/logic/feature/e2ei/PlatformX509Certificate.kt deleted file mode 100644 index 7adbc6b8621..00000000000 --- a/logic/src/appleMain/kotlin/com/wire/kalium/logic/feature/e2ei/PlatformX509Certificate.kt +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Wire - * Copyright (C) 2024 Wire Swiss GmbH - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see http://www.gnu.org/licenses/. - */ -package com.wire.kalium.logic.feature.e2ei - -actual class PlatformX509Certificate diff --git a/logic/src/appleMain/kotlin/com/wire/kalium/logic/feature/e2ei/X509CertificateGenerator.kt b/logic/src/appleMain/kotlin/com/wire/kalium/logic/feature/e2ei/X509CertificateGenerator.kt deleted file mode 100644 index 41e6ac35a6e..00000000000 --- a/logic/src/appleMain/kotlin/com/wire/kalium/logic/feature/e2ei/X509CertificateGenerator.kt +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Wire - * Copyright (C) 2024 Wire Swiss GmbH - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see http://www.gnu.org/licenses/. - */ -package com.wire.kalium.logic.feature.e2ei - -actual interface X509CertificateGenerator { - actual fun generate(certificateByteArray: ByteArray): PlatformX509Certificate -} - -actual class X509CertificateGeneratorImpl : X509CertificateGenerator { - override fun generate(certificateByteArray: ByteArray): PlatformX509Certificate { - TODO("Not yet implemented") - } -} diff --git a/logic/src/commonJvmAndroid/kotlin/com/wire/kalium/logic/feature/e2ei/CertificateStatusChecker.kt b/logic/src/commonJvmAndroid/kotlin/com/wire/kalium/logic/feature/e2ei/CertificateStatusChecker.kt deleted file mode 100644 index f2727947076..00000000000 --- a/logic/src/commonJvmAndroid/kotlin/com/wire/kalium/logic/feature/e2ei/CertificateStatusChecker.kt +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Wire - * Copyright (C) 2024 Wire Swiss GmbH - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see http://www.gnu.org/licenses/. - */ -package com.wire.kalium.logic.feature.e2ei - -import com.wire.kalium.cryptography.CryptoCertificateStatus -import java.util.Date - -actual interface CertificateStatusChecker { - actual fun status(notAfterTimestamp: Long, certificateStatus: CryptoCertificateStatus): CertificateStatus -} - -actual class CertificateStatusCheckerImpl : CertificateStatusChecker { - override fun status(notAfterTimestamp: Long, certificateStatus: CryptoCertificateStatus): CertificateStatus { - val current = Date() - - return when { - (certificateStatus == CryptoCertificateStatus.REVOKED) -> CertificateStatus.REVOKED - (current.time >= notAfterTimestamp || certificateStatus == CryptoCertificateStatus.EXPIRED) -> CertificateStatus.EXPIRED - else -> CertificateStatus.VALID - } - } -} diff --git a/logic/src/commonJvmAndroid/kotlin/com/wire/kalium/logic/feature/e2ei/PemCertificateDecoder.kt b/logic/src/commonJvmAndroid/kotlin/com/wire/kalium/logic/feature/e2ei/PemCertificateDecoder.kt deleted file mode 100644 index 4755766bc59..00000000000 --- a/logic/src/commonJvmAndroid/kotlin/com/wire/kalium/logic/feature/e2ei/PemCertificateDecoder.kt +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Wire - * Copyright (C) 2024 Wire Swiss GmbH - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see http://www.gnu.org/licenses/. - */ -package com.wire.kalium.logic.feature.e2ei - -import com.wire.kalium.cryptography.CryptoCertificateStatus -import com.wire.kalium.logic.util.serialNumber - -actual interface PemCertificateDecoder { - actual fun decode(certificate: String, status: CryptoCertificateStatus): E2eiCertificate -} - -actual class PemCertificateDecoderImpl actual constructor( - private val x509CertificateGenerator: X509CertificateGenerator, - private val certificateStatusChecker: CertificateStatusChecker -) : PemCertificateDecoder { - override fun decode(certificate: String, status: CryptoCertificateStatus): E2eiCertificate { - x509CertificateGenerator.generate(certificate.toByteArray()).also { - return E2eiCertificate( - issuer = it.value.issuerX500Principal.name, - status = certificateStatusChecker.status(it.value.notAfter.time, status), - serialNumber = it.value.serialNumber.toString(BASE_16).serialNumber(), - certificateDetail = certificate - ) - } - } -} - -private const val BASE_16 = 16 diff --git a/logic/src/commonJvmAndroid/kotlin/com/wire/kalium/logic/feature/e2ei/PlatformX509Certificate.kt b/logic/src/commonJvmAndroid/kotlin/com/wire/kalium/logic/feature/e2ei/PlatformX509Certificate.kt deleted file mode 100644 index 6cd1c3570aa..00000000000 --- a/logic/src/commonJvmAndroid/kotlin/com/wire/kalium/logic/feature/e2ei/PlatformX509Certificate.kt +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Wire - * Copyright (C) 2024 Wire Swiss GmbH - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see http://www.gnu.org/licenses/. - */ -package com.wire.kalium.logic.feature.e2ei - -import java.security.cert.X509Certificate - -actual class PlatformX509Certificate( - val value: X509Certificate -) diff --git a/logic/src/commonJvmAndroid/kotlin/com/wire/kalium/logic/feature/e2ei/X509CertificateGenerator.kt b/logic/src/commonJvmAndroid/kotlin/com/wire/kalium/logic/feature/e2ei/X509CertificateGenerator.kt deleted file mode 100644 index 9477f587830..00000000000 --- a/logic/src/commonJvmAndroid/kotlin/com/wire/kalium/logic/feature/e2ei/X509CertificateGenerator.kt +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Wire - * Copyright (C) 2024 Wire Swiss GmbH - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see http://www.gnu.org/licenses/. - */ -package com.wire.kalium.logic.feature.e2ei - -import java.io.ByteArrayInputStream -import java.security.cert.CertificateFactory -import java.security.cert.X509Certificate - -actual interface X509CertificateGenerator { - actual fun generate(certificateByteArray: ByteArray): PlatformX509Certificate -} - -actual class X509CertificateGeneratorImpl : X509CertificateGenerator { - override fun generate(certificateByteArray: ByteArray): PlatformX509Certificate { - return PlatformX509Certificate( - CertificateFactory.getInstance(TYPE) - .generateCertificate(ByteArrayInputStream(certificateByteArray)) as X509Certificate - ) - } -} - -private const val TYPE = "X.509" diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/e2ei/CertificateStatusChecker.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/e2ei/CertificateStatusChecker.kt deleted file mode 100644 index 5ed64e5a70c..00000000000 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/e2ei/CertificateStatusChecker.kt +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Wire - * Copyright (C) 2024 Wire Swiss GmbH - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see http://www.gnu.org/licenses/. - */ -package com.wire.kalium.logic.feature.e2ei - -import com.wire.kalium.cryptography.CryptoCertificateStatus - -expect interface CertificateStatusChecker { - fun status(notAfterTimestamp: Long, certificateStatus: CryptoCertificateStatus): CertificateStatus -} - -expect class CertificateStatusCheckerImpl() : CertificateStatusChecker diff --git a/logic/src/appleMain/kotlin/com/wire/kalium/logic/feature/e2ei/CertificateStatusChecker.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/e2ei/CertificateStatusMapper.kt similarity index 59% rename from logic/src/appleMain/kotlin/com/wire/kalium/logic/feature/e2ei/CertificateStatusChecker.kt rename to logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/e2ei/CertificateStatusMapper.kt index 9af3516ebbc..252810561bc 100644 --- a/logic/src/appleMain/kotlin/com/wire/kalium/logic/feature/e2ei/CertificateStatusChecker.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/e2ei/CertificateStatusMapper.kt @@ -15,16 +15,20 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/. */ + package com.wire.kalium.logic.feature.e2ei import com.wire.kalium.cryptography.CryptoCertificateStatus -actual interface CertificateStatusChecker { - actual fun status(notAfterTimestamp: Long, certificateStatus: CryptoCertificateStatus): CertificateStatus +interface CertificateStatusMapper { + fun toCertificateStatus(certificateStatus: CryptoCertificateStatus): CertificateStatus } -actual class CertificateStatusCheckerImpl : CertificateStatusChecker { - override fun status(notAfterTimestamp: Long, certificateStatus: CryptoCertificateStatus): CertificateStatus { - TODO("Not yet implemented") - } +class CertificateStatusMapperImpl : CertificateStatusMapper { + override fun toCertificateStatus(certificateStatus: CryptoCertificateStatus): CertificateStatus = + when (certificateStatus) { + CryptoCertificateStatus.EXPIRED -> CertificateStatus.EXPIRED + CryptoCertificateStatus.REVOKED -> CertificateStatus.REVOKED + CryptoCertificateStatus.VALID -> CertificateStatus.VALID + } } diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/e2ei/E2eiCertificate.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/e2ei/E2eiCertificate.kt index e54d2d0afeb..4c07c93a080 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/e2ei/E2eiCertificate.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/e2ei/E2eiCertificate.kt @@ -18,8 +18,7 @@ package com.wire.kalium.logic.feature.e2ei data class E2eiCertificate( - val issuer: String = "", - val status: CertificateStatus = CertificateStatus.EXPIRED, - val serialNumber: String = "", - val certificateDetail: String = "" + val status: CertificateStatus, + val serialNumber: String, + val certificateDetail: String ) diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/e2ei/PemCertificateDecoder.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/e2ei/PemCertificateDecoder.kt deleted file mode 100644 index 22bdbc68560..00000000000 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/e2ei/PemCertificateDecoder.kt +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Wire - * Copyright (C) 2024 Wire Swiss GmbH - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see http://www.gnu.org/licenses/. - */ -package com.wire.kalium.logic.feature.e2ei - -import com.wire.kalium.cryptography.CryptoCertificateStatus - -expect interface PemCertificateDecoder { - fun decode(certificate: String, status: CryptoCertificateStatus): E2eiCertificate -} - -expect class PemCertificateDecoderImpl( - x509CertificateGenerator: X509CertificateGenerator = X509CertificateGeneratorImpl(), - certificateStatusChecker: CertificateStatusChecker = CertificateStatusCheckerImpl() -) : PemCertificateDecoder diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/e2ei/PlatformX509Certificate.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/e2ei/PlatformX509Certificate.kt deleted file mode 100644 index 72d41b71746..00000000000 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/e2ei/PlatformX509Certificate.kt +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Wire - * Copyright (C) 2024 Wire Swiss GmbH - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see http://www.gnu.org/licenses/. - */ -package com.wire.kalium.logic.feature.e2ei - -expect class PlatformX509Certificate diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/e2ei/X509CertificateGenerator.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/e2ei/X509CertificateGenerator.kt deleted file mode 100644 index 6c40785d836..00000000000 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/e2ei/X509CertificateGenerator.kt +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Wire - * Copyright (C) 2024 Wire Swiss GmbH - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see http://www.gnu.org/licenses/. - */ -package com.wire.kalium.logic.feature.e2ei - -expect interface X509CertificateGenerator { - fun generate(certificateByteArray: ByteArray): PlatformX509Certificate -} - -expect class X509CertificateGeneratorImpl : X509CertificateGenerator diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/e2ei/usecase/GetE2EICertificateUseCase.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/e2ei/usecase/GetE2EICertificateUseCase.kt index 0fc537b0a62..c20a520f937 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/e2ei/usecase/GetE2EICertificateUseCase.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/e2ei/usecase/GetE2EICertificateUseCase.kt @@ -19,8 +19,8 @@ package com.wire.kalium.logic.feature.e2ei.usecase import com.wire.kalium.logic.data.conversation.ClientId import com.wire.kalium.logic.data.conversation.MLSConversationRepository +import com.wire.kalium.logic.feature.e2ei.CertificateStatusMapper import com.wire.kalium.logic.feature.e2ei.E2eiCertificate -import com.wire.kalium.logic.feature.e2ei.PemCertificateDecoder import com.wire.kalium.logic.functional.fold /** @@ -32,7 +32,7 @@ interface GetE2eiCertificateUseCase { class GetE2eiCertificateUseCaseImpl internal constructor( private val mlsConversationRepository: MLSConversationRepository, - private val pemCertificateDecoder: PemCertificateDecoder + private val certificateStatusMapper: CertificateStatusMapper ) : GetE2eiCertificateUseCase { override suspend operator fun invoke(clientId: ClientId): GetE2EICertificateUseCaseResult = mlsConversationRepository.getClientIdentity(clientId).fold( @@ -41,7 +41,11 @@ class GetE2eiCertificateUseCaseImpl internal constructor( }, { it?.let { - val certificate = pemCertificateDecoder.decode(it.certificate, it.status) + val certificate = E2eiCertificate( + status = certificateStatusMapper.toCertificateStatus(it.status), + serialNumber = it.serialNumber, + certificateDetail = it.certificate + ) GetE2EICertificateUseCaseResult.Success(certificate) } ?: GetE2EICertificateUseCaseResult.NotActivated } diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/e2ei/usecase/GetMembersE2EICertificateStatusesUseCase.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/e2ei/usecase/GetMembersE2EICertificateStatusesUseCase.kt index 55ee1e34d94..6b52bae7fe5 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/e2ei/usecase/GetMembersE2EICertificateStatusesUseCase.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/e2ei/usecase/GetMembersE2EICertificateStatusesUseCase.kt @@ -22,7 +22,8 @@ import com.wire.kalium.logic.data.conversation.MLSConversationRepository import com.wire.kalium.logic.data.id.ConversationId import com.wire.kalium.logic.data.user.UserId import com.wire.kalium.logic.feature.e2ei.CertificateStatus -import com.wire.kalium.logic.feature.e2ei.PemCertificateDecoder +import com.wire.kalium.logic.feature.e2ei.CertificateStatusMapper +import com.wire.kalium.logic.feature.e2ei.E2eiCertificate import com.wire.kalium.logic.functional.fold /** @@ -35,14 +36,14 @@ interface GetMembersE2EICertificateStatusesUseCase { class GetMembersE2EICertificateStatusesUseCaseImpl internal constructor( private val mlsConversationRepository: MLSConversationRepository, - private val pemCertificateDecoder: PemCertificateDecoder + private val certificateStatusMapper: CertificateStatusMapper ) : GetMembersE2EICertificateStatusesUseCase { override suspend operator fun invoke(conversationId: ConversationId, userIds: List): Map = mlsConversationRepository.getMembersIdentities(conversationId, userIds).fold( { mapOf() }, { it.mapValues { (_, identities) -> - identities.getUserCertificateStatus(pemCertificateDecoder) + identities.getUserCertificateStatus(certificateStatusMapper) } } ) @@ -54,8 +55,14 @@ class GetMembersE2EICertificateStatusesUseCaseImpl internal constructor( * [CertificateStatus.EXPIRED] if any certificate is expired; * [CertificateStatus.VALID] otherwise. */ -fun List.getUserCertificateStatus(pemCertificateDecoder: PemCertificateDecoder): CertificateStatus? { - val certificates = this.map { pemCertificateDecoder.decode(it.certificate, it.status) } +fun List.getUserCertificateStatus(certificateStatusMapper: CertificateStatusMapper): CertificateStatus? { + val certificates = this.map { + E2eiCertificate( + status = certificateStatusMapper.toCertificateStatus(it.status), + serialNumber = it.serialNumber, + certificateDetail = it.certificate + ) + } return if (certificates.isEmpty()) { null } else if (certificates.any { it.status == CertificateStatus.REVOKED }) { diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/e2ei/usecase/GetUserE2EIAllCertificatesUseCase.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/e2ei/usecase/GetUserE2EIAllCertificatesUseCase.kt index af0e99108f1..bbfd86c84b5 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/e2ei/usecase/GetUserE2EIAllCertificatesUseCase.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/e2ei/usecase/GetUserE2EIAllCertificatesUseCase.kt @@ -20,8 +20,8 @@ package com.wire.kalium.logic.feature.e2ei.usecase import com.wire.kalium.logic.data.conversation.ClientId import com.wire.kalium.logic.data.conversation.MLSConversationRepository import com.wire.kalium.logic.data.user.UserId +import com.wire.kalium.logic.feature.e2ei.CertificateStatusMapper import com.wire.kalium.logic.feature.e2ei.E2eiCertificate -import com.wire.kalium.logic.feature.e2ei.PemCertificateDecoder import com.wire.kalium.logic.feature.user.IsE2EIEnabledUseCase import com.wire.kalium.logic.functional.getOrElse import com.wire.kalium.logic.functional.map @@ -36,15 +36,20 @@ interface GetUserE2eiCertificatesUseCase { class GetUserE2eiCertificatesUseCaseImpl internal constructor( private val mlsConversationRepository: MLSConversationRepository, - private val pemCertificateDecoder: PemCertificateDecoder, - private val isE2EIEnabledUseCase: IsE2EIEnabledUseCase + private val isE2EIEnabledUseCase: IsE2EIEnabledUseCase, + private val certificateStatusMapper: CertificateStatusMapper ) : GetUserE2eiCertificatesUseCase { override suspend operator fun invoke(userId: UserId): Map = if (isE2EIEnabledUseCase()) { mlsConversationRepository.getUserIdentity(userId).map { identities -> val result = mutableMapOf() identities.forEach { - result[it.clientId.value] = pemCertificateDecoder.decode(it.certificate, it.status) + val certificate = E2eiCertificate( + status = certificateStatusMapper.toCertificateStatus(it.status), + serialNumber = it.serialNumber, + certificateDetail = it.certificate + ) + result[it.clientId.value] = certificate } result }.getOrElse(mapOf()) diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/e2ei/usecase/GetUserE2EICertificateUseCase.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/e2ei/usecase/GetUserE2EICertificateUseCase.kt index d9689f62f69..b54378f128a 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/e2ei/usecase/GetUserE2EICertificateUseCase.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/e2ei/usecase/GetUserE2EICertificateUseCase.kt @@ -20,7 +20,7 @@ package com.wire.kalium.logic.feature.e2ei.usecase import com.wire.kalium.logic.data.conversation.MLSConversationRepository import com.wire.kalium.logic.data.user.UserId import com.wire.kalium.logic.feature.e2ei.CertificateStatus -import com.wire.kalium.logic.feature.e2ei.PemCertificateDecoder +import com.wire.kalium.logic.feature.e2ei.CertificateStatusMapper import com.wire.kalium.logic.feature.user.IsE2EIEnabledUseCase import com.wire.kalium.logic.functional.fold @@ -33,7 +33,7 @@ interface GetUserE2eiCertificateStatusUseCase { class GetUserE2eiCertificateStatusUseCaseImpl internal constructor( private val mlsConversationRepository: MLSConversationRepository, - private val pemCertificateDecoder: PemCertificateDecoder, + private val certificateStatusMapper: CertificateStatusMapper, private val isE2EIEnabledUseCase: IsE2EIEnabledUseCase ) : GetUserE2eiCertificateStatusUseCase { override suspend operator fun invoke(userId: UserId): GetUserE2eiCertificateStatusResult = @@ -43,7 +43,7 @@ class GetUserE2eiCertificateStatusUseCaseImpl internal constructor( GetUserE2eiCertificateStatusResult.Failure.NotActivated }, { identities -> - identities.getUserCertificateStatus(pemCertificateDecoder)?.let { + identities.getUserCertificateStatus(certificateStatusMapper)?.let { GetUserE2eiCertificateStatusResult.Success(it) } ?: GetUserE2eiCertificateStatusResult.Failure.NotActivated } diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/user/UserScope.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/user/UserScope.kt index 54b6db46659..7df37952928 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/user/UserScope.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/user/UserScope.kt @@ -49,7 +49,7 @@ import com.wire.kalium.logic.feature.auth.ValidateUserHandleUseCaseImpl import com.wire.kalium.logic.feature.client.FinalizeMLSClientAfterE2EIEnrollment import com.wire.kalium.logic.feature.client.FinalizeMLSClientAfterE2EIEnrollmentImpl import com.wire.kalium.logic.feature.conversation.GetAllContactsNotInConversationUseCase -import com.wire.kalium.logic.feature.e2ei.PemCertificateDecoderImpl +import com.wire.kalium.logic.feature.e2ei.CertificateStatusMapperImpl import com.wire.kalium.logic.feature.e2ei.usecase.EnrollE2EIUseCase import com.wire.kalium.logic.feature.e2ei.usecase.EnrollE2EIUseCaseImpl import com.wire.kalium.logic.feature.e2ei.usecase.GetE2eiCertificateUseCase @@ -113,7 +113,7 @@ class UserScope internal constructor( val observeUserInfo: ObserveUserInfoUseCase get() = ObserveUserInfoUseCaseImpl(userRepository, teamRepository) val uploadUserAvatar: UploadUserAvatarUseCase get() = UploadUserAvatarUseCaseImpl(userRepository, assetRepository) - private val pemCertificateDecoderImpl by lazy { PemCertificateDecoderImpl() } + private val certificateStatusMapper by lazy { CertificateStatusMapperImpl() } val getPublicAsset: GetAvatarAssetUseCase get() = GetAvatarAssetUseCaseImpl(assetRepository, userRepository) val enrollE2EI: EnrollE2EIUseCase get() = EnrollE2EIUseCaseImpl(e2EIRepository) @@ -125,24 +125,24 @@ class UserScope internal constructor( val getE2EICertificate: GetE2eiCertificateUseCase get() = GetE2eiCertificateUseCaseImpl( mlsConversationRepository = mlsConversationRepository, - pemCertificateDecoder = pemCertificateDecoderImpl + certificateStatusMapper = certificateStatusMapper ) val getUserE2eiCertificateStatus: GetUserE2eiCertificateStatusUseCase get() = GetUserE2eiCertificateStatusUseCaseImpl( mlsConversationRepository = mlsConversationRepository, - pemCertificateDecoder = pemCertificateDecoderImpl, + certificateStatusMapper = certificateStatusMapper, isE2EIEnabledUseCase = isE2EIEnabledUseCase ) val getUserE2eiCertificates: GetUserE2eiCertificatesUseCase get() = GetUserE2eiCertificatesUseCaseImpl( mlsConversationRepository = mlsConversationRepository, - pemCertificateDecoder = pemCertificateDecoderImpl, + certificateStatusMapper = certificateStatusMapper, isE2EIEnabledUseCase = isE2EIEnabledUseCase ) val getMembersE2EICertificateStatuses: GetMembersE2EICertificateStatusesUseCase get() = GetMembersE2EICertificateStatusesUseCaseImpl( mlsConversationRepository = mlsConversationRepository, - pemCertificateDecoder = pemCertificateDecoderImpl + certificateStatusMapper = certificateStatusMapper ) val deleteAsset: DeleteAssetUseCase get() = DeleteAssetUseCaseImpl(assetRepository) val setUserHandle: SetUserHandleUseCase get() = SetUserHandleUseCase(accountRepository, validateUserHandleUseCase, syncManager) diff --git a/logic/src/commonTest/kotlin/com/wire/kalium/logic/data/conversation/MLSConversationRepositoryTest.kt b/logic/src/commonTest/kotlin/com/wire/kalium/logic/data/conversation/MLSConversationRepositoryTest.kt index 0380e283e2a..044f272f5e0 100644 --- a/logic/src/commonTest/kotlin/com/wire/kalium/logic/data/conversation/MLSConversationRepositoryTest.kt +++ b/logic/src/commonTest/kotlin/com/wire/kalium/logic/data/conversation/MLSConversationRepositoryTest.kt @@ -1752,15 +1752,17 @@ class MLSConversationRepositoryTest { val COMMIT_BUNDLE = CommitBundle(COMMIT, WELCOME, PUBLIC_GROUP_STATE_BUNDLE, null) val ROTATE_BUNDLE = RotateBundle(mapOf(RAW_GROUP_ID to COMMIT_BUNDLE), emptyList(), emptyList(), null) val CRYPTO_CLIENT_ID = CryptoQualifiedClientId("clientId", TestConversation.USER_1.toCrypto()) - val WIRE_IDENTITY = WireIdentity( - CRYPTO_CLIENT_ID, - "user_handle", - "User Test", - "domain.com", - "certificate", - CryptoCertificateStatus.VALID, - thumbprint = "thumbprint" - ) + val WIRE_IDENTITY = + WireIdentity( + CRYPTO_CLIENT_ID, + "user_handle", + "User Test", + "domain.com", + "certificate", + CryptoCertificateStatus.VALID, + thumbprint = "thumbprint", + serialNumber = "serialNumber" + ) val E2EI_CONVERSATION_CLIENT_INFO_ENTITY = E2EIConversationClientInfoEntity(UserIDEntity(uuid4().toString(), "domain.com"), "clientId", "groupId") val DECRYPTED_MESSAGE_BUNDLE = com.wire.kalium.cryptography.DecryptedMessageBundle( diff --git a/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/client/ObserveE2EIRequiredUseCaseTest.kt b/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/client/ObserveE2EIRequiredUseCaseTest.kt index 46b6a307a41..584a6a91ec7 100644 --- a/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/client/ObserveE2EIRequiredUseCaseTest.kt +++ b/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/client/ObserveE2EIRequiredUseCaseTest.kt @@ -295,6 +295,10 @@ class ObserveE2EIRequiredUseCaseTest { companion object { private val MLS_E2EI_SETTING = E2EISettings(true, "some_url", null) - private val VALID_CERTIFICATE = E2eiCertificate(status = CertificateStatus.VALID) + private val VALID_CERTIFICATE = E2eiCertificate( + serialNumber = "serialNumber", + certificateDetail = "certificateDetail", + status = CertificateStatus.VALID + ) } } diff --git a/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/e2ei/GetE2eiCertificateUseCaseTest.kt b/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/e2ei/GetE2eiCertificateUseCaseTest.kt index 5ed224ddf80..3642561fea2 100644 --- a/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/e2ei/GetE2eiCertificateUseCaseTest.kt +++ b/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/e2ei/GetE2eiCertificateUseCaseTest.kt @@ -58,48 +58,49 @@ class GetE2eiCertificateUseCaseTest { } @Test - fun givenRepositoryReturnsValidCertificateString_whenRunningUseCase_thenReturnCertificate() = runTest { - val (arrangement, getE2eiCertificateUseCase) = Arrangement() - .withRepositoryValidCertificate(IDENTITY) - .withDecodeSuccess() - .arrange() - - val result = getE2eiCertificateUseCase.invoke(CLIENT_ID) - - verify(arrangement.mlsConversationRepository) - .suspendFunction(arrangement.mlsConversationRepository::getClientIdentity) - .with(any()) - .wasInvoked(once) - - verify(arrangement.pemCertificateDecoder) - .function(arrangement.pemCertificateDecoder::decode) - .with(any()) - .wasInvoked(once) - - assertEquals(true, result is GetE2EICertificateUseCaseResult.Success) - } + fun givenRepositoryReturnsValidCertificateString_whenRunningUseCase_thenReturnCertificate() = + runTest { + val (arrangement, getE2eiCertificateUseCase) = Arrangement() + .withRepositoryValidCertificate(IDENTITY) + .withMapperReturning(CertificateStatus.EXPIRED) + .arrange() + + val result = getE2eiCertificateUseCase.invoke(CLIENT_ID) + + verify(arrangement.mlsConversationRepository) + .suspendFunction(arrangement.mlsConversationRepository::getClientIdentity) + .with(any()) + .wasInvoked(once) + + verify(arrangement.certificateStatusMapper) + .function(arrangement.certificateStatusMapper::toCertificateStatus) + .with(any()) + .wasInvoked(once) + + assertEquals(true, result is GetE2EICertificateUseCaseResult.Success) + } @Test - fun givenRepositoryReturnsNullCertificate_whenRunningUseCase_thenReturnNotActivated() = runTest { - val (arrangement, getE2eiCertificateUseCase) = Arrangement() - .withRepositoryValidCertificate(null) - .withDecodeSuccess() - .arrange() + fun givenRepositoryReturnsNullCertificate_whenRunningUseCase_thenReturnNotActivated() = + runTest { + val (arrangement, getE2eiCertificateUseCase) = Arrangement() + .withRepositoryValidCertificate(null) + .arrange() - val result = getE2eiCertificateUseCase.invoke(CLIENT_ID) + val result = getE2eiCertificateUseCase.invoke(CLIENT_ID) - verify(arrangement.mlsConversationRepository) - .suspendFunction(arrangement.mlsConversationRepository::getClientIdentity) - .with(any()) - .wasInvoked(once) + verify(arrangement.mlsConversationRepository) + .suspendFunction(arrangement.mlsConversationRepository::getClientIdentity) + .with(any()) + .wasInvoked(once) - verify(arrangement.pemCertificateDecoder) - .function(arrangement.pemCertificateDecoder::decode) - .with(any()) - .wasNotInvoked() + verify(arrangement.certificateStatusMapper) + .function(arrangement.certificateStatusMapper::toCertificateStatus) + .with(any()) + .wasNotInvoked() - assertEquals(true, result is GetE2EICertificateUseCaseResult.NotActivated) - } + assertEquals(true, result is GetE2EICertificateUseCaseResult.NotActivated) + } class Arrangement { @@ -107,11 +108,11 @@ class GetE2eiCertificateUseCaseTest { val mlsConversationRepository = mock(classOf()) @Mock - val pemCertificateDecoder = mock(classOf()) + val certificateStatusMapper = mock(classOf()) fun arrange() = this to GetE2eiCertificateUseCaseImpl( mlsConversationRepository = mlsConversationRepository, - pemCertificateDecoder = pemCertificateDecoder + certificateStatusMapper = certificateStatusMapper ) fun withRepositoryFailure() = apply { @@ -128,19 +129,22 @@ class GetE2eiCertificateUseCaseTest { .thenReturn(Either.Right(identity)) } - fun withDecodeSuccess() = apply { - given(pemCertificateDecoder) - .function(pemCertificateDecoder::decode) + fun withMapperReturning(status: CertificateStatus) = apply { + given(certificateStatusMapper) + .function(certificateStatusMapper::toCertificateStatus) .whenInvokedWith(any()) - .thenReturn(e2eiCertificate) + .thenReturn(status) } } companion object { val CLIENT_ID = ClientId("client-id") private val USER_ID = UserId("value", "domain") - private val CRYPTO_QUALIFIED_CLIENT_ID = CryptoQualifiedClientId("clientId", USER_ID.toCrypto()) - val e2eiCertificate = E2eiCertificate("certificate") + private val CRYPTO_QUALIFIED_CLIENT_ID = + CryptoQualifiedClientId("clientId", USER_ID.toCrypto()) + + val e2eiCertificate = + E2eiCertificate(CertificateStatus.EXPIRED, "serialNumber", "certificateDetail") val IDENTITY = WireIdentity( CRYPTO_QUALIFIED_CLIENT_ID, handle = "alic_test", @@ -148,7 +152,8 @@ class GetE2eiCertificateUseCaseTest { domain = "test.com", certificate = "certificate", status = CryptoCertificateStatus.EXPIRED, - thumbprint = "thumbprint" + thumbprint = "thumbprint", + serialNumber = "serialNumber" ) } } diff --git a/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/e2ei/GetMembersE2EICertificateStatusesUseCaseTest.kt b/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/e2ei/GetMembersE2EICertificateStatusesUseCaseTest.kt index bcdc62a1f88..b930a7cabe1 100644 --- a/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/e2ei/GetMembersE2EICertificateStatusesUseCaseTest.kt +++ b/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/e2ei/GetMembersE2EICertificateStatusesUseCaseTest.kt @@ -26,11 +26,10 @@ import com.wire.kalium.logic.data.id.toCrypto import com.wire.kalium.logic.data.user.UserId import com.wire.kalium.logic.feature.e2ei.usecase.GetMembersE2EICertificateStatusesUseCaseImpl import com.wire.kalium.logic.functional.Either +import com.wire.kalium.logic.util.arrangement.mls.CertificateStatusMapperArrangement +import com.wire.kalium.logic.util.arrangement.mls.CertificateStatusMapperArrangementImpl import com.wire.kalium.logic.util.arrangement.mls.MLSConversationRepositoryArrangement import com.wire.kalium.logic.util.arrangement.mls.MLSConversationRepositoryArrangementImpl -import com.wire.kalium.logic.util.arrangement.mls.PemCertificateDecoderArrangement -import com.wire.kalium.logic.util.arrangement.mls.PemCertificateDecoderArrangementImpl -import io.mockative.any import io.mockative.eq import kotlinx.coroutines.test.runTest import kotlin.test.Test @@ -39,15 +38,16 @@ import kotlin.test.assertEquals class GetMembersE2EICertificateStatusesUseCaseTest { @Test - fun givenErrorOnGettingMembersIdentities_whenRequestMembersStatuses_thenEmptyMapResult() = runTest { - val (_, getMembersE2EICertificateStatuses) = arrange { - withMembersIdentities(Either.Left(MLSFailure.WrongEpoch)) - } + fun givenErrorOnGettingMembersIdentities_whenRequestMembersStatuses_thenEmptyMapResult() = + runTest { + val (_, getMembersE2EICertificateStatuses) = arrange { + withMembersIdentities(Either.Left(MLSFailure.WrongEpoch)) + } - val result = getMembersE2EICertificateStatuses(CONVERSATION_ID, listOf()) + val result = getMembersE2EICertificateStatuses(CONVERSATION_ID, listOf()) - assertEquals(mapOf(), result) - } + assertEquals(mapOf(), result) + } @Test fun givenEmptyWireIdentityMap_whenRequestMembersStatuses_thenNotActivatedResult() = runTest { @@ -55,67 +55,79 @@ class GetMembersE2EICertificateStatusesUseCaseTest { withMembersIdentities(Either.Right(mapOf())) } - val result = getMembersE2EICertificateStatuses(conversationId, listOf()) + val result = getMembersE2EICertificateStatuses(CONVERSATION_ID, listOf()) assertEquals(mapOf(), result) } @Test - fun givenOneWireIdentityExpiredForSomeUser_whenRequestMembersStatuses_thenResultUsersStatusIsExpired() = runTest { - val (_, getMembersE2EICertificateStatuses) = arrange { - withMembersIdentities( - Either.Right( - mapOf( - USER_ID to listOf( - WIRE_IDENTITY, - WIRE_IDENTITY.copy(status = CryptoCertificateStatus.EXPIRED) + fun givenOneWireIdentityExpiredForSomeUser_whenRequestMembersStatuses_thenResultUsersStatusIsExpired() = + runTest { + val (_, getMembersE2EICertificateStatuses) = arrange { + withMembersIdentities( + Either.Right( + mapOf( + USER_ID to listOf( + WIRE_IDENTITY, + WIRE_IDENTITY.copy(status = CryptoCertificateStatus.EXPIRED) + ) ) ) ) - ) - } + } - val result = getMembersE2EICertificateStatuses(conversationId, listOf(USER_ID)) + val result = getMembersE2EICertificateStatuses(CONVERSATION_ID, listOf(USER_ID)) - assertEquals(CertificateStatus.EXPIRED, result[USER_ID]) - } + assertEquals(CertificateStatus.EXPIRED, result[USER_ID]) + } @Test - fun givenOneWireIdentityRevokedForSomeUser_whenRequestMembersStatuses_thenResultUsersStatusIsRevoked() = runTest { - val userId2 = USER_ID.copy(value = "value_2") - val (_, getMembersE2EICertificateStatuses) = arrange { - withMembersIdentities( - Either.Right( - mapOf( - USER_ID to listOf( - WIRE_IDENTITY, - WIRE_IDENTITY.copy(status = CryptoCertificateStatus.REVOKED) - ), - userId2 to listOf(WIRE_IDENTITY) + fun givenOneWireIdentityRevokedForSomeUser_whenRequestMembersStatuses_thenResultUsersStatusIsRevoked() = + runTest { + val userId2 = USER_ID.copy(value = "value_2") + val (_, getMembersE2EICertificateStatuses) = arrange { + withMembersIdentities( + Either.Right( + mapOf( + USER_ID to listOf( + WIRE_IDENTITY, + WIRE_IDENTITY.copy(status = CryptoCertificateStatus.REVOKED) + ), + userId2 to listOf(WIRE_IDENTITY) + ) ) ) - ) - } + } - val result = getMembersE2EICertificateStatuses(CONVERSATION_ID, listOf(USER_ID, userId2)) + val result = + getMembersE2EICertificateStatuses(CONVERSATION_ID, listOf(USER_ID, userId2)) - assertEquals(CertificateStatus.REVOKED, result[USER_ID]) - assertEquals(CertificateStatus.VALID, result[userId2]) - } + assertEquals(CertificateStatus.REVOKED, result[USER_ID]) + assertEquals(CertificateStatus.VALID, result[userId2]) + } private class Arrangement(private val block: Arrangement.() -> Unit) : MLSConversationRepositoryArrangement by MLSConversationRepositoryArrangementImpl(), - PemCertificateDecoderArrangement by PemCertificateDecoderArrangementImpl() { + CertificateStatusMapperArrangement by CertificateStatusMapperArrangementImpl() { fun arrange() = run { - withPemCertificateDecode(E2EI_CERTIFICATE, any(), eq(CryptoCertificateStatus.VALID)) - withPemCertificateDecode(E2EI_CERTIFICATE.copy(status = CertificateStatus.EXPIRED), any(), eq(CryptoCertificateStatus.EXPIRED)) - withPemCertificateDecode(E2EI_CERTIFICATE.copy(status = CertificateStatus.REVOKED), any(), eq(CryptoCertificateStatus.REVOKED)) + withCertificateStatusMapperReturning( + CertificateStatus.VALID, + eq(CryptoCertificateStatus.VALID) + ) + withCertificateStatusMapperReturning( + CertificateStatus.EXPIRED, + eq(CryptoCertificateStatus.EXPIRED) + ) + withCertificateStatusMapperReturning( + CertificateStatus.REVOKED, + eq(CryptoCertificateStatus.REVOKED) + ) block() this@Arrangement to GetMembersE2EICertificateStatusesUseCaseImpl( mlsConversationRepository = mlsConversationRepository, - pemCertificateDecoder = pemCertificateDecoder + certificateStatusMapper = certificateStatusMapper ) } } @@ -124,19 +136,26 @@ class GetMembersE2EICertificateStatusesUseCaseTest { fun arrange(configuration: Arrangement.() -> Unit) = Arrangement(configuration).arrange() private val USER_ID = UserId("value", "domain") - private val CRYPTO_QUALIFIED_CLIENT_ID = CryptoQualifiedClientId("clientId", USER_ID.toCrypto()) + private val CRYPTO_QUALIFIED_CLIENT_ID = + CryptoQualifiedClientId("clientId", USER_ID.toCrypto()) + private val CONVERSATION_ID = ConversationId("conversation_value", "domain") - private val conversationId = ConversationId("conversation_value", "domain") - private val WIRE_IDENTITY = WireIdentity( - CRYPTO_QUALIFIED_CLIENT_ID, - "user_handle", - "User Test", - "domain.com", - "certificate", - CryptoCertificateStatus.VALID, - "thumbprint" - ) + private val WIRE_IDENTITY = + WireIdentity( + CRYPTO_QUALIFIED_CLIENT_ID, + "user_handle", + "User Test", + "domain.com", + "certificate", + CryptoCertificateStatus.VALID, + "thumbprint", + "serialNumber" + ) private val E2EI_CERTIFICATE = - E2eiCertificate(issuer = "issue", status = CertificateStatus.VALID, serialNumber = "number", certificateDetail = "details") + E2eiCertificate( + status = CertificateStatus.VALID, + serialNumber = "number", + certificateDetail = "details" + ) } } diff --git a/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/e2ei/GetUserE2eiAllCertificateStatusesUseCaseTest.kt b/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/e2ei/GetUserE2eiAllCertificateStatusesUseCaseTest.kt index 726f362439c..20c468b19a1 100644 --- a/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/e2ei/GetUserE2eiAllCertificateStatusesUseCaseTest.kt +++ b/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/e2ei/GetUserE2eiAllCertificateStatusesUseCaseTest.kt @@ -25,12 +25,12 @@ import com.wire.kalium.logic.data.id.toCrypto import com.wire.kalium.logic.data.user.UserId import com.wire.kalium.logic.feature.e2ei.usecase.GetUserE2eiCertificatesUseCaseImpl import com.wire.kalium.logic.functional.Either +import com.wire.kalium.logic.util.arrangement.mls.CertificateStatusMapperArrangement +import com.wire.kalium.logic.util.arrangement.mls.CertificateStatusMapperArrangementImpl import com.wire.kalium.logic.util.arrangement.mls.IsE2EIEnabledUseCaseArrangement import com.wire.kalium.logic.util.arrangement.mls.IsE2EIEnabledUseCaseArrangementImpl import com.wire.kalium.logic.util.arrangement.mls.MLSConversationRepositoryArrangement import com.wire.kalium.logic.util.arrangement.mls.MLSConversationRepositoryArrangementImpl -import com.wire.kalium.logic.util.arrangement.mls.PemCertificateDecoderArrangement -import com.wire.kalium.logic.util.arrangement.mls.PemCertificateDecoderArrangementImpl import io.mockative.any import io.mockative.eq import io.mockative.verify @@ -42,82 +42,101 @@ import kotlin.test.assertTrue class GetUserE2eiAllCertificateStatusesUseCaseTest { @Test - fun givenErrorOnGettingUserIdentity_whenGetUserE2eiAllCertificateStatuses_thenEmptyMapResult() = runTest { - val (_, getUserE2eiAllCertificateStatuses) = arrange { - withE2EIEnabledAndMLSEnabled(true) - withUserIdentity(Either.Left(MLSFailure.WrongEpoch)) - } + fun givenErrorOnGettingUserIdentity_whenGetUserE2eiAllCertificateStatuses_thenEmptyMapResult() = + runTest { + val (_, getUserE2eiAllCertificateStatuses) = arrange { + withE2EIEnabledAndMLSEnabled(true) + withUserIdentity(Either.Left(MLSFailure.WrongEpoch)) + } - val result = getUserE2eiAllCertificateStatuses(USER_ID) + val result = getUserE2eiAllCertificateStatuses(USER_ID) - assertTrue(result.isEmpty()) - } + assertTrue(result.isEmpty()) + } @Test - fun givenEmptyWireIdentityList_whenGetUserE2eiAllCertificateStatuses_thenEmptyMapResult() = runTest { - val (_, getUserE2eiAllCertificateStatuses) = arrange { - withE2EIEnabledAndMLSEnabled(true) - withUserIdentity(Either.Right(listOf())) - } + fun givenEmptyWireIdentityList_whenGetUserE2eiAllCertificateStatuses_thenEmptyMapResult() = + runTest { + val (_, getUserE2eiAllCertificateStatuses) = arrange { + withE2EIEnabledAndMLSEnabled(true) + withUserIdentity(Either.Right(listOf())) + } - val result = getUserE2eiAllCertificateStatuses(USER_ID) + val result = getUserE2eiAllCertificateStatuses(USER_ID) - assertTrue(result.isEmpty()) - } + assertTrue(result.isEmpty()) + } @Test - fun givenOneWireIdentityExpired_whenGetUserE2eiAllCertificateStatuses_thenResultCorrectMap() = runTest { - val identity1 = WIRE_IDENTITY - val identity2 = WIRE_IDENTITY.copy(clientId = CRYPTO_QUALIFIED_CLIENT_ID.copy("id_2"), status = CryptoCertificateStatus.EXPIRED) - val identity3 = WIRE_IDENTITY.copy(clientId = CRYPTO_QUALIFIED_CLIENT_ID.copy("id_3"), status = CryptoCertificateStatus.REVOKED) - val (_, getUserE2eiAllCertificateStatuses) = arrange { - withE2EIEnabledAndMLSEnabled(true) - withUserIdentity(Either.Right(listOf(identity1, identity2, identity3))) - } + fun givenOneWireIdentityExpired_whenGetUserE2eiAllCertificateStatuses_thenResultCorrectMap() = + runTest { + val identity1 = WIRE_IDENTITY + val identity2 = WIRE_IDENTITY.copy( + clientId = CRYPTO_QUALIFIED_CLIENT_ID.copy("id_2"), + status = CryptoCertificateStatus.EXPIRED + ) + val identity3 = WIRE_IDENTITY.copy( + clientId = CRYPTO_QUALIFIED_CLIENT_ID.copy("id_3"), + status = CryptoCertificateStatus.REVOKED + ) + val (_, getUserE2eiAllCertificateStatuses) = arrange { + withE2EIEnabledAndMLSEnabled(true) + withUserIdentity(Either.Right(listOf(identity1, identity2, identity3))) + } - val result = getUserE2eiAllCertificateStatuses(USER_ID) + val result = getUserE2eiAllCertificateStatuses(USER_ID) - assertEquals(3, result.size) - assertEquals(CertificateStatus.VALID, result[identity1.clientId.value]?.status) - assertEquals(CertificateStatus.EXPIRED, result[identity2.clientId.value]?.status) - assertEquals(CertificateStatus.REVOKED, result[identity3.clientId.value]?.status) - } + assertEquals(3, result.size) + assertEquals(CertificateStatus.VALID, result[identity1.clientId.value]?.status) + assertEquals(CertificateStatus.EXPIRED, result[identity2.clientId.value]?.status) + assertEquals(CertificateStatus.REVOKED, result[identity3.clientId.value]?.status) + } @Test - fun givenE2EIAndMLSIsDisabled_whenGettingUserE2EICertificate_thenEmptyMapIsReturned() = runTest { - // given - val (arrangement, getUserE2eiAllCertificateStatuses) = arrange { - withE2EIEnabledAndMLSEnabled(false) + fun givenE2EIAndMLSIsDisabled_whenGettingUserE2EICertificate_thenEmptyMapIsReturned() = + runTest { + // given + val (arrangement, getUserE2eiAllCertificateStatuses) = arrange { + withE2EIEnabledAndMLSEnabled(false) + } + + // when + val result = getUserE2eiAllCertificateStatuses(USER_ID) + + // then + assertEquals( + mapOf(), + result + ) + verify(arrangement.mlsConversationRepository) + .suspendFunction(arrangement.mlsConversationRepository::getUserIdentity) + .with(any()) + .wasNotInvoked() } - // when - val result = getUserE2eiAllCertificateStatuses(USER_ID) - - // then - assertEquals( - mapOf(), - result - ) - verify(arrangement.mlsConversationRepository) - .suspendFunction(arrangement.mlsConversationRepository::getUserIdentity) - .with(any()) - .wasNotInvoked() - } - private class Arrangement(private val block: Arrangement.() -> Unit) : MLSConversationRepositoryArrangement by MLSConversationRepositoryArrangementImpl(), - PemCertificateDecoderArrangement by PemCertificateDecoderArrangementImpl(), + CertificateStatusMapperArrangement by CertificateStatusMapperArrangementImpl(), IsE2EIEnabledUseCaseArrangement by IsE2EIEnabledUseCaseArrangementImpl() { fun arrange() = run { - withPemCertificateDecode(E2EI_CERTIFICATE, any(), eq(CryptoCertificateStatus.VALID)) - withPemCertificateDecode(E2EI_CERTIFICATE.copy(status = CertificateStatus.EXPIRED), any(), eq(CryptoCertificateStatus.EXPIRED)) - withPemCertificateDecode(E2EI_CERTIFICATE.copy(status = CertificateStatus.REVOKED), any(), eq(CryptoCertificateStatus.REVOKED)) + withCertificateStatusMapperReturning( + CertificateStatus.VALID, + eq(CryptoCertificateStatus.VALID) + ) + withCertificateStatusMapperReturning( + CertificateStatus.EXPIRED, + eq(CryptoCertificateStatus.EXPIRED) + ) + withCertificateStatusMapperReturning( + CertificateStatus.REVOKED, + eq(CryptoCertificateStatus.REVOKED) + ) block() this@Arrangement to GetUserE2eiCertificatesUseCaseImpl( mlsConversationRepository = mlsConversationRepository, - pemCertificateDecoder = pemCertificateDecoder, + certificateStatusMapper = certificateStatusMapper, isE2EIEnabledUseCase = isE2EIEnabledUseCase ) } @@ -127,17 +146,24 @@ class GetUserE2eiAllCertificateStatusesUseCaseTest { fun arrange(configuration: Arrangement.() -> Unit) = Arrangement(configuration).arrange() private val USER_ID = UserId("value", "domain") - private val CRYPTO_QUALIFIED_CLIENT_ID = CryptoQualifiedClientId("clientId", USER_ID.toCrypto()) - private val WIRE_IDENTITY = WireIdentity( - CRYPTO_QUALIFIED_CLIENT_ID, - "user_handle", - "User Test", - "domain.com", - "certificate", - CryptoCertificateStatus.VALID, - "thumbprint" - ) + private val CRYPTO_QUALIFIED_CLIENT_ID = + CryptoQualifiedClientId("clientId", USER_ID.toCrypto()) + private val WIRE_IDENTITY = + WireIdentity( + CRYPTO_QUALIFIED_CLIENT_ID, + "user_handle", + "User Test", + "domain.com", + "certificate", + CryptoCertificateStatus.VALID, + "thumbprint", + "serialNumber" + ) private val E2EI_CERTIFICATE = - E2eiCertificate(issuer = "issue", status = CertificateStatus.VALID, serialNumber = "number", certificateDetail = "details") + E2eiCertificate( + status = CertificateStatus.VALID, + serialNumber = "number", + certificateDetail = "details" + ) } } diff --git a/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/e2ei/GetUserE2eiCertificateStatusUseCaseTest.kt b/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/e2ei/GetUserE2eiCertificateStatusUseCaseTest.kt index ebab173705f..0cc287ee937 100644 --- a/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/e2ei/GetUserE2eiCertificateStatusUseCaseTest.kt +++ b/logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/e2ei/GetUserE2eiCertificateStatusUseCaseTest.kt @@ -26,12 +26,12 @@ import com.wire.kalium.logic.data.user.UserId import com.wire.kalium.logic.feature.e2ei.usecase.GetUserE2eiCertificateStatusResult import com.wire.kalium.logic.feature.e2ei.usecase.GetUserE2eiCertificateStatusUseCaseImpl import com.wire.kalium.logic.functional.Either +import com.wire.kalium.logic.util.arrangement.mls.CertificateStatusMapperArrangement +import com.wire.kalium.logic.util.arrangement.mls.CertificateStatusMapperArrangementImpl import com.wire.kalium.logic.util.arrangement.mls.IsE2EIEnabledUseCaseArrangement import com.wire.kalium.logic.util.arrangement.mls.IsE2EIEnabledUseCaseArrangementImpl import com.wire.kalium.logic.util.arrangement.mls.MLSConversationRepositoryArrangement import com.wire.kalium.logic.util.arrangement.mls.MLSConversationRepositoryArrangementImpl -import com.wire.kalium.logic.util.arrangement.mls.PemCertificateDecoderArrangement -import com.wire.kalium.logic.util.arrangement.mls.PemCertificateDecoderArrangementImpl import io.mockative.any import io.mockative.eq import io.mockative.verify @@ -43,110 +43,148 @@ import kotlin.test.assertTrue class GetUserE2eiCertificateStatusUseCaseTest { @Test - fun givenErrorOnGettingUserIdentity_whenGetUserE2eiCertificateStatus_thenNotActivatedResult() = runTest { - val (_, getUserE2eiCertificateStatus) = arrange { - withE2EIEnabledAndMLSEnabled(true) - withUserIdentity(Either.Left(MLSFailure.WrongEpoch)) - } + fun givenErrorOnGettingUserIdentity_whenGetUserE2eiCertificateStatus_thenNotActivatedResult() = + runTest { + val (_, getUserE2eiCertificateStatus) = arrange { + withE2EIEnabledAndMLSEnabled(true) + withUserIdentity(Either.Left(MLSFailure.WrongEpoch)) + } - val result = getUserE2eiCertificateStatus(USER_ID) + val result = getUserE2eiCertificateStatus(USER_ID) - assertEquals(GetUserE2eiCertificateStatusResult.Failure.NotActivated, result) - } + assertEquals(GetUserE2eiCertificateStatusResult.Failure.NotActivated, result) + } @Test - fun givenEmptyWireIdentityList_whenGetUserE2eiCertificateStatus_thenNotActivatedResult() = runTest { - val (_, getUserE2eiCertificateStatus) = arrange { - withE2EIEnabledAndMLSEnabled(true) - withUserIdentity(Either.Right(listOf())) - } + fun givenEmptyWireIdentityList_whenGetUserE2eiCertificateStatus_thenNotActivatedResult() = + runTest { + val (_, getUserE2eiCertificateStatus) = arrange { + withE2EIEnabledAndMLSEnabled(true) + withUserIdentity(Either.Right(listOf())) + } - val result = getUserE2eiCertificateStatus(USER_ID) + val result = getUserE2eiCertificateStatus(USER_ID) - assertEquals(GetUserE2eiCertificateStatusResult.Failure.NotActivated, result) - } + assertEquals(GetUserE2eiCertificateStatusResult.Failure.NotActivated, result) + } @Test - fun givenOneWireIdentityExpired_whenGetUserE2eiCertificateStatus_thenResultIsExpired() = runTest { - val (_, getUserE2eiCertificateStatus) = arrange { - withE2EIEnabledAndMLSEnabled(true) - withUserIdentity(Either.Right(listOf(WIRE_IDENTITY, WIRE_IDENTITY.copy(status = CryptoCertificateStatus.EXPIRED)))) - } + fun givenOneWireIdentityExpired_whenGetUserE2eiCertificateStatus_thenResultIsExpired() = + runTest { + val (_, getUserE2eiCertificateStatus) = arrange { + withE2EIEnabledAndMLSEnabled(true) + withUserIdentity( + Either.Right( + listOf( + WIRE_IDENTITY, + WIRE_IDENTITY.copy(status = CryptoCertificateStatus.EXPIRED) + ) + ) + ) + } - val result = getUserE2eiCertificateStatus(USER_ID) + val result = getUserE2eiCertificateStatus(USER_ID) - assertTrue { result is GetUserE2eiCertificateStatusResult.Success } - assertEquals(CertificateStatus.EXPIRED, (result as GetUserE2eiCertificateStatusResult.Success).status) - } + assertTrue { result is GetUserE2eiCertificateStatusResult.Success } + assertEquals( + CertificateStatus.EXPIRED, + (result as GetUserE2eiCertificateStatusResult.Success).status + ) + } @Test - fun givenOneWireIdentityRevoked_whenGetUserE2eiCertificateStatus_thenResultIsRevoked() = runTest { - val (_, getUserE2eiCertificateStatus) = arrange { - withE2EIEnabledAndMLSEnabled(true) - withUserIdentity(Either.Right(listOf(WIRE_IDENTITY, WIRE_IDENTITY.copy(status = CryptoCertificateStatus.REVOKED)))) - } + fun givenOneWireIdentityRevoked_whenGetUserE2eiCertificateStatus_thenResultIsRevoked() = + runTest { + val (_, getUserE2eiCertificateStatus) = arrange { + withE2EIEnabledAndMLSEnabled(true) + withUserIdentity( + Either.Right( + listOf( + WIRE_IDENTITY, + WIRE_IDENTITY.copy(status = CryptoCertificateStatus.REVOKED) + ) + ) + ) + } - val result = getUserE2eiCertificateStatus(USER_ID) + val result = getUserE2eiCertificateStatus(USER_ID) - assertTrue { result is GetUserE2eiCertificateStatusResult.Success } - assertEquals(CertificateStatus.REVOKED, (result as GetUserE2eiCertificateStatusResult.Success).status) - } + assertTrue { result is GetUserE2eiCertificateStatusResult.Success } + assertEquals( + CertificateStatus.REVOKED, + (result as GetUserE2eiCertificateStatusResult.Success).status + ) + } @Test - fun givenOneWireIdentityRevoked_whenGetUserE2eiCertificateStatus_thenResultIsRevoked2() = runTest { - val (_, getUserE2eiCertificateStatus) = arrange { - withE2EIEnabledAndMLSEnabled(true) - withUserIdentity( - Either.Right( - listOf( - WIRE_IDENTITY.copy(status = CryptoCertificateStatus.EXPIRED), - WIRE_IDENTITY.copy(status = CryptoCertificateStatus.REVOKED) + fun givenOneWireIdentityRevoked_whenGetUserE2eiCertificateStatus_thenResultIsRevoked2() = + runTest { + val (_, getUserE2eiCertificateStatus) = arrange { + withE2EIEnabledAndMLSEnabled(true) + withUserIdentity( + Either.Right( + listOf( + WIRE_IDENTITY.copy(status = CryptoCertificateStatus.EXPIRED), + WIRE_IDENTITY.copy(status = CryptoCertificateStatus.REVOKED) + ) ) ) - ) - } + } - val result = getUserE2eiCertificateStatus(USER_ID) + val result = getUserE2eiCertificateStatus(USER_ID) - assertTrue { result is GetUserE2eiCertificateStatusResult.Success } - assertEquals(CertificateStatus.REVOKED, (result as GetUserE2eiCertificateStatusResult.Success).status) - } + assertTrue { result is GetUserE2eiCertificateStatusResult.Success } + assertEquals( + CertificateStatus.REVOKED, + (result as GetUserE2eiCertificateStatusResult.Success).status + ) + } @Test - fun givenE2EIAndMLSIsDisabled_whenGettingUserE2EICertificateStatus_thenFailureNotActivatedIsReturned() = runTest { - // given - val (arrangement, getUserE2eiCertificateStatus) = arrange { - withE2EIEnabledAndMLSEnabled(false) + fun givenE2EIAndMLSIsDisabled_whenGettingUserE2EICertificateStatus_thenFailureNotActivatedIsReturned() = + runTest { + // given + val (arrangement, getUserE2eiCertificateStatus) = arrange { + withE2EIEnabledAndMLSEnabled(false) + } + + // when + val result = getUserE2eiCertificateStatus(USER_ID) + + // then + assertEquals( + GetUserE2eiCertificateStatusResult.Failure.NotActivated, + result + ) + verify(arrangement.mlsConversationRepository) + .suspendFunction(arrangement.mlsConversationRepository::getUserIdentity) + .with(any()) + .wasNotInvoked() } - // when - val result = getUserE2eiCertificateStatus(USER_ID) - - // then - assertEquals( - GetUserE2eiCertificateStatusResult.Failure.NotActivated, - result - ) - verify(arrangement.mlsConversationRepository) - .suspendFunction(arrangement.mlsConversationRepository::getUserIdentity) - .with(any()) - .wasNotInvoked() - } - private class Arrangement(private val block: Arrangement.() -> Unit) : MLSConversationRepositoryArrangement by MLSConversationRepositoryArrangementImpl(), - PemCertificateDecoderArrangement by PemCertificateDecoderArrangementImpl(), + CertificateStatusMapperArrangement by CertificateStatusMapperArrangementImpl(), IsE2EIEnabledUseCaseArrangement by IsE2EIEnabledUseCaseArrangementImpl() { fun arrange() = run { - withPemCertificateDecode(E2EI_CERTIFICATE, any(), eq(CryptoCertificateStatus.VALID)) - withPemCertificateDecode(E2EI_CERTIFICATE.copy(status = CertificateStatus.EXPIRED), any(), eq(CryptoCertificateStatus.EXPIRED)) - withPemCertificateDecode(E2EI_CERTIFICATE.copy(status = CertificateStatus.REVOKED), any(), eq(CryptoCertificateStatus.REVOKED)) + withCertificateStatusMapperReturning( + CertificateStatus.VALID, + eq(CryptoCertificateStatus.VALID) + ) + withCertificateStatusMapperReturning( + CertificateStatus.EXPIRED, + eq(CryptoCertificateStatus.EXPIRED) + ) + withCertificateStatusMapperReturning( + CertificateStatus.REVOKED, + eq(CryptoCertificateStatus.REVOKED) + ) block() this@Arrangement to GetUserE2eiCertificateStatusUseCaseImpl( mlsConversationRepository = mlsConversationRepository, - pemCertificateDecoder = pemCertificateDecoder, + certificateStatusMapper = certificateStatusMapper, isE2EIEnabledUseCase = isE2EIEnabledUseCase ) } @@ -156,17 +194,24 @@ class GetUserE2eiCertificateStatusUseCaseTest { fun arrange(configuration: Arrangement.() -> Unit) = Arrangement(configuration).arrange() private val USER_ID = UserId("value", "domain") - private val CRYPTO_QUALIFIED_CLIENT_ID = CryptoQualifiedClientId("clientId", USER_ID.toCrypto()) - private val WIRE_IDENTITY = WireIdentity( - CRYPTO_QUALIFIED_CLIENT_ID, - "user_handle", - "User Test", - "domain.com", - "certificate", - CryptoCertificateStatus.VALID, - "thumbprint" - ) + private val CRYPTO_QUALIFIED_CLIENT_ID = + CryptoQualifiedClientId("clientId", USER_ID.toCrypto()) + private val WIRE_IDENTITY = + WireIdentity( + CRYPTO_QUALIFIED_CLIENT_ID, + "user_handle", + "User Test", + "domain.com", + "certificate", + CryptoCertificateStatus.VALID, + "thumbprint", + "serialNumber" + ) private val E2EI_CERTIFICATE = - E2eiCertificate(issuer = "issue", status = CertificateStatus.VALID, serialNumber = "number", certificateDetail = "details") + E2eiCertificate( + status = CertificateStatus.VALID, + serialNumber = "number", + certificateDetail = "details" + ) } } diff --git a/logic/src/commonTest/kotlin/com/wire/kalium/logic/util/arrangement/mls/PemCertificateDecoderArrangement.kt b/logic/src/commonTest/kotlin/com/wire/kalium/logic/util/arrangement/mls/CertificateStatusMapperArrangement.kt similarity index 50% rename from logic/src/commonTest/kotlin/com/wire/kalium/logic/util/arrangement/mls/PemCertificateDecoderArrangement.kt rename to logic/src/commonTest/kotlin/com/wire/kalium/logic/util/arrangement/mls/CertificateStatusMapperArrangement.kt index 4509f4906f8..2c7704874a1 100644 --- a/logic/src/commonTest/kotlin/com/wire/kalium/logic/util/arrangement/mls/PemCertificateDecoderArrangement.kt +++ b/logic/src/commonTest/kotlin/com/wire/kalium/logic/util/arrangement/mls/CertificateStatusMapperArrangement.kt @@ -18,34 +18,33 @@ package com.wire.kalium.logic.util.arrangement.mls import com.wire.kalium.cryptography.CryptoCertificateStatus -import com.wire.kalium.logic.feature.e2ei.E2eiCertificate -import com.wire.kalium.logic.feature.e2ei.PemCertificateDecoder +import com.wire.kalium.logic.feature.e2ei.CertificateStatus +import com.wire.kalium.logic.feature.e2ei.CertificateStatusMapper import io.mockative.any import io.mockative.given import io.mockative.matchers.Matcher import io.mockative.mock -interface PemCertificateDecoderArrangement { - val pemCertificateDecoder: PemCertificateDecoder +interface CertificateStatusMapperArrangement { + val certificateStatusMapper: CertificateStatusMapper - fun withPemCertificateDecode( - result: E2eiCertificate, - certificateMatcher: Matcher = any(), - statusMatcher: Matcher = any() + fun withCertificateStatusMapperReturning( + result: CertificateStatus, + certificateMatcher: Matcher = any() ) } -class PemCertificateDecoderArrangementImpl : PemCertificateDecoderArrangement { - override val pemCertificateDecoder: PemCertificateDecoder = mock(PemCertificateDecoder::class) +class CertificateStatusMapperArrangementImpl : CertificateStatusMapperArrangement { + override val certificateStatusMapper: CertificateStatusMapper = + mock(CertificateStatusMapper::class) - override fun withPemCertificateDecode( - result: E2eiCertificate, - certificateMatcher: Matcher, - statusMatcher: Matcher + override fun withCertificateStatusMapperReturning( + result: CertificateStatus, + certificateMatcher: Matcher ) { - given(pemCertificateDecoder) - .function(pemCertificateDecoder::decode) - .whenInvokedWith(certificateMatcher, statusMatcher) + given(certificateStatusMapper) + .function(certificateStatusMapper::toCertificateStatus) + .whenInvokedWith(certificateMatcher) .thenReturn(result) } } diff --git a/logic/src/jvmTest/kotlin/com/wire/kalium/logic/feature/e2ei/CertificateStatusCheckerTest.kt b/logic/src/jvmTest/kotlin/com/wire/kalium/logic/feature/e2ei/CertificateStatusCheckerTest.kt deleted file mode 100644 index 76c7b64cb4c..00000000000 --- a/logic/src/jvmTest/kotlin/com/wire/kalium/logic/feature/e2ei/CertificateStatusCheckerTest.kt +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Wire - * Copyright (C) 2024 Wire Swiss GmbH - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see http://www.gnu.org/licenses/. - */ -package com.wire.kalium.logic.feature.e2ei - -import com.wire.kalium.cryptography.CryptoCertificateStatus -import org.junit.Test -import kotlin.test.assertEquals - -class CertificateStatusCheckerTest { - - @Test - fun givenOldTimestamp_whenCheckingTheStatus_thenReturnExpired() { - val timestamp: Long = 1666681915000 // Tuesday, 25 October 2022 07:11:55 - val (_, certificateStatusChecker) = Arrangement() - .arrange() - - val result = certificateStatusChecker.status(timestamp, CryptoCertificateStatus.VALID) - - assertEquals(CertificateStatus.EXPIRED, result) - } - - @Test - fun givenFutureTimestampAndExpiredCertificateStatus_whenCheckingTheStatus_thenReturnExpired() { - val timestamp = 4822355515000 // Sunday, 25 October 2122 07:11:55 - val (_, certificateStatusChecker) = Arrangement() - .arrange() - - val result = certificateStatusChecker.status(timestamp, CryptoCertificateStatus.EXPIRED) - - assertEquals(CertificateStatus.EXPIRED, result) - } - - @Test - fun givenFutureTimestampAndRevokedCertificateStatus_whenCheckingTheStatus_thenReturnExpired() { - val timestamp = 4822355515000 // Sunday, 25 October 2122 07:11:55 - val (_, certificateStatusChecker) = Arrangement() - .arrange() - - val result = certificateStatusChecker.status(timestamp, CryptoCertificateStatus.REVOKED) - - assertEquals(CertificateStatus.REVOKED, result) - } - - @Test - fun givenFutureTimestamp_whenCheckingTheStatus_thenReturnValid() { - val timestamp = 4822355515000 // Sunday, 25 October 2122 07:11:55 - - val (_, certificateStatusChecker) = Arrangement() - .arrange() - - val result = certificateStatusChecker.status(timestamp, CryptoCertificateStatus.VALID) - - assertEquals(CertificateStatus.VALID, result) - } - - class Arrangement { - - fun arrange() = this to CertificateStatusCheckerImpl() - } -} diff --git a/logic/src/jvmTest/kotlin/com/wire/kalium/logic/feature/e2ei/PemCertificateDecoderTest.kt b/logic/src/jvmTest/kotlin/com/wire/kalium/logic/feature/e2ei/PemCertificateDecoderTest.kt deleted file mode 100644 index 298c24705c7..00000000000 --- a/logic/src/jvmTest/kotlin/com/wire/kalium/logic/feature/e2ei/PemCertificateDecoderTest.kt +++ /dev/null @@ -1,132 +0,0 @@ -/* - * Wire - * Copyright (C) 2024 Wire Swiss GmbH - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see http://www.gnu.org/licenses/. - */ -package com.wire.kalium.logic.feature.e2ei - -import com.wire.kalium.cryptography.CryptoCertificateStatus -import io.mockative.Mock -import io.mockative.any -import io.mockative.classOf -import io.mockative.given -import io.mockative.mock -import io.mockative.once -import io.mockative.verify -import org.junit.Test -import java.security.cert.CertificateException -import kotlin.test.assertEquals - -class PemCertificateDecoderTest { - - @Test - fun givenAValidCertificate_whenDecodingIt_thenReturnCertificateObject() { - val expectedCertificate = E2eiCertificate( - issuer = "CN=wire Intermediate CA,O=wire", - status = CertificateStatus.VALID, - serialNumber = "60:88:F6:3E:97:4F:2E:AB:50:5C:C9:B1:D1:39:97:BA", - certificateDetail = validPemCertificateString - ) - val (arrangement, pemCertificateDecoder) = Arrangement() - .withCertificate(validPemCertificateString) - .withValidStatus() - .arrange() - - val result = pemCertificateDecoder.decode(validPemCertificateString, CryptoCertificateStatus.VALID) - - verify(arrangement.x509CertificateGeneratorMock) - .function(arrangement.x509CertificateGeneratorMock::generate) - .with(any()) - .wasInvoked(once) - - assertEquals(expectedCertificate, result) - } - - @Test(expected = CertificateException::class) - fun givenAnInValidCertificate_whenDecodingIt_thenThrowCertificateException() { - val (_, pemCertificateDecoder) = Arrangement() - .withCertificate(invalidPemCertificateString) - .withValidStatus() - .arrange() - - pemCertificateDecoder.decode(invalidPemCertificateString, CryptoCertificateStatus.VALID) - } - - class Arrangement { - - @Mock - val x509CertificateGeneratorMock = mock(classOf()) - - @Mock - val certificateStatusChecker = mock(classOf()) - - fun arrange() = this to PemCertificateDecoderImpl( - x509CertificateGeneratorMock, - certificateStatusChecker - ) - - fun withCertificate(certificateString: String) = apply { - val platformCertificate = createPlatformX509Certificate(certificateString) - given(x509CertificateGeneratorMock) - .function(x509CertificateGeneratorMock::generate) - .whenInvokedWith(any()) - .thenReturn(platformCertificate) - } - - fun withValidStatus() = apply { - given(certificateStatusChecker) - .function(certificateStatusChecker::status) - .whenInvokedWith(any()) - .thenReturn(CertificateStatus.VALID) - } - - private fun createPlatformX509Certificate(certificateString: String): PlatformX509Certificate { - val x509CertificateGenerator = X509CertificateGeneratorImpl() - - return x509CertificateGenerator.generate(certificateString.toByteArray()) - } - } - - companion object { - const val validPemCertificateString = "-----BEGIN CERTIFICATE-----\n" + - "MIICNDCCAdqgAwIBAgIQYIj2PpdPLqtQXMmx0TmXujAKBggqhkjOPQQDAjAuMQ0w\n" + - "CwYDVQQKEwR3aXJlMR0wGwYDVQQDExR3aXJlIEludGVybWVkaWF0ZSBDQTAeFw0y\n" + - "MzEwMDIxNTIyMjJaFw0yMzEyMzExNTIyMjJaMDMxFzAVBgNVBAoTDmVsbmEud2ly\n" + - "ZS5saW5rMRgwFgYDVQQDEw9Nb2p0YWJhIENoZW5hbmkwKjAFBgMrZXADIQAonK3u\n" + - "cLIUnWP+8iG2GdabCWmzfiHTgXMncNx/r064LKOCAQIwgf8wDgYDVR0PAQH/BAQD\n" + - "AgeAMB0GA1UdJQQWMBQGCCsGAQUFBwMBBggrBgEFBQcDAjAdBgNVHQ4EFgQUhVb7\n" + - "GEXzaKxm3yiNEV0DOd78LBcwHwYDVR0jBBgwFoAUADMwoCFunlLkYO2SQbOAUOIL\n" + - "VCowZQYDVR0RBF4wXIZBaW06d2lyZWFwcD1JRzlZdnp1V1FJS1VhUmsxMkY1Q0lR\n" + - "Lzk1MzIxOGU2OGE2MzY0MWZAZWxuYS53aXJlLmxpbmuGF2ltOndpcmVhcHA9bW9q\n" + - "dGFiYV93aXJlMCcGDCsGAQQBgqRkxihAAQQXMBUCAQYEDmdvb2dsZS1hbmRyb2lk\n" + - "BAAwCgYIKoZIzj0EAwIDSAAwRQIhAJORy8WUjP8spjxlCCNOCAQrPIUbl6BTQGtv\n" + - "FhJqP3UrAiAC4mbuQ6BlVmiovCzqP1YbiaGimvBEm/XTwtWJE6wM0A==\n" + - "-----END CERTIFICATE-----\n" + - "-----BEGIN CERTIFICATE-----\n" + - "MIIBuDCCAV6gAwIBAgIQUJ8AHZqe79OeFVEPkdtQrDAKBggqhkjOPQQDAjAmMQ0w\n" + - "CwYDVQQKEwR3aXJlMRUwEwYDVQQDEwx3aXJlIFJvb3QgQ0EwHhcNMjMwNDE3MDkw\n" + - "ODQxWhcNMzMwNDE0MDkwODQxWjAuMQ0wCwYDVQQKEwR3aXJlMR0wGwYDVQQDExR3\n" + - "aXJlIEludGVybWVkaWF0ZSBDQTBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABB9p\n" + - "iYVv5ik10pwkOGdwVI6F6a8YKk9Ro/CqahPcTfefhOhL/M5RxzWmi2oW75mW6WKr\n" + - "tG94D45Ur6yfNclLspmjZjBkMA4GA1UdDwEB/wQEAwIBBjASBgNVHRMBAf8ECDAG\n" + - "AQH/AgEAMB0GA1UdDgQWBBQAMzCgIW6eUuRg7ZJBs4BQ4gtUKjAfBgNVHSMEGDAW\n" + - "gBR40ZJlSIKIjEI/4ZMwgV3X5CB7tDAKBggqhkjOPQQDAgNIADBFAiEA5VT2B38E\n" + - "9EunvJiLRCG9baeeMq4Yn1LwOT10cXdUIIICIEnDUrd2XW69YnUIPF3bEHln3oKt\n" + - "wje0yUIA61GMpqNz\n" + - "-----END CERTIFICATE-----" - - const val invalidPemCertificateString = "dsverlkerkvekvkadxjwencwejjk" - } -}