From 65b08aefdfd4c94a428c0bc34554e7b09a899307 Mon Sep 17 00:00:00 2001 From: Oussama Hassine Date: Fri, 26 Jan 2024 14:23:22 +0100 Subject: [PATCH] fix: add crl url when there is no stored list (#2417) --- .../CertificateRevocationListRepository.kt | 20 +++++++++------- ...CertificateRevocationListRepositoryTest.kt | 24 +++++++++++++++++++ 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/e2ei/CertificateRevocationListRepository.kt b/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/e2ei/CertificateRevocationListRepository.kt index bce8b8e4058..10588351510 100644 --- a/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/e2ei/CertificateRevocationListRepository.kt +++ b/logic/src/commonMain/kotlin/com/wire/kalium/logic/data/e2ei/CertificateRevocationListRepository.kt @@ -51,13 +51,12 @@ internal class CertificateRevocationListRepositoryDataSource( metadataDAO.getSerializable(CRL_LIST_KEY, CRLUrlExpirationList.serializer()) override suspend fun addOrUpdateCRL(url: String, timestamp: ULong) { - - metadataDAO.getSerializable(CRL_LIST_KEY, CRLUrlExpirationList.serializer()) + val newCRLUrls = metadataDAO.getSerializable(CRL_LIST_KEY, CRLUrlExpirationList.serializer()) ?.let { crlExpirationList -> val crlWithExpiration = crlExpirationList.cRLWithExpirationList.find { it.url == url } - val newCRLs = crlWithExpiration?.let { item -> + crlWithExpiration?.let { item -> crlExpirationList.cRLWithExpirationList.map { current -> if (current.url == url) { return@map item.copy(expiration = timestamp) @@ -72,12 +71,15 @@ internal class CertificateRevocationListRepositoryDataSource( ) } - metadataDAO.putSerializable( - CRL_LIST_KEY, - CRLUrlExpirationList(newCRLs), - CRLUrlExpirationList.serializer() - ) - } + } ?: run { + // add new CRL + listOf(CRLWithExpiration(url, timestamp)) + } + metadataDAO.putSerializable( + CRL_LIST_KEY, + CRLUrlExpirationList(newCRLUrls), + CRLUrlExpirationList.serializer() + ) } override suspend fun getCurrentClientCrlUrl(): Either = diff --git a/logic/src/commonTest/kotlin/com/wire/kalium/logic/data/e2ei/CertificateRevocationListRepositoryTest.kt b/logic/src/commonTest/kotlin/com/wire/kalium/logic/data/e2ei/CertificateRevocationListRepositoryTest.kt index de488a7c2c8..701e98c83e4 100644 --- a/logic/src/commonTest/kotlin/com/wire/kalium/logic/data/e2ei/CertificateRevocationListRepositoryTest.kt +++ b/logic/src/commonTest/kotlin/com/wire/kalium/logic/data/e2ei/CertificateRevocationListRepositoryTest.kt @@ -50,6 +50,22 @@ class CertificateRevocationListRepositoryTest { ) }.wasInvoked(once) } + @Test + fun givenNoStoredList_whenUpdatingCRLs_thenAddNewCRL() = runTest { + val (arrangement, crlRepository) = Arrangement() + .withNullCRLResult() + .arrange() + + crlRepository.addOrUpdateCRL(DUMMY_URL, TIMESTAMP) + + verify(arrangement.metadataDAO).coroutine { + putSerializable( + CRL_LIST_KEY, + CRLUrlExpirationList(listOf(CRLWithExpiration(DUMMY_URL, TIMESTAMP))), + CRLUrlExpirationList.serializer() + ) + }.wasInvoked(once) + } @Test fun givenPassedCRLExistsInStoredList_whenUpdatingCRLs_thenUpdateCurrentCRL() = runTest { @@ -111,6 +127,14 @@ class CertificateRevocationListRepositoryTest { ) }.thenReturn(CRLUrlExpirationList(listOf())) } + suspend fun withNullCRLResult() = apply { + given(metadataDAO).coroutine { + metadataDAO.getSerializable( + CRL_LIST_KEY, + CRLUrlExpirationList.serializer() + ) + }.thenReturn(null) + } suspend fun withCRLs() = apply { given(metadataDAO).coroutine {