From 2694138aa15869ecbb3615e95f7158fda504737f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Bispo?= Date: Fri, 26 Sep 2025 15:47:21 +0100 Subject: [PATCH] [PM-20977] Handle new sdk exception type. (#5937) --- .../datasource/sdk/VaultSdkSourceImpl.kt | 14 +++-- .../datasource/sdk/VaultSdkSourceTest.kt | 55 +++++++++++++++++-- 2 files changed, 60 insertions(+), 9 deletions(-) diff --git a/app/src/main/kotlin/com/x8bit/bitwarden/data/vault/datasource/sdk/VaultSdkSourceImpl.kt b/app/src/main/kotlin/com/x8bit/bitwarden/data/vault/datasource/sdk/VaultSdkSourceImpl.kt index eeff413d98..0d3e459c49 100644 --- a/app/src/main/kotlin/com/x8bit/bitwarden/data/vault/datasource/sdk/VaultSdkSourceImpl.kt +++ b/app/src/main/kotlin/com/x8bit/bitwarden/data/vault/datasource/sdk/VaultSdkSourceImpl.kt @@ -2,6 +2,7 @@ package com.x8bit.bitwarden.data.vault.datasource.sdk import com.bitwarden.collections.Collection import com.bitwarden.collections.CollectionView +import com.bitwarden.core.DeriveKeyConnectorException import com.bitwarden.core.DeriveKeyConnectorRequest import com.bitwarden.core.DerivePinKeyResponse import com.bitwarden.core.InitOrgCryptoRequest @@ -93,14 +94,17 @@ class VaultSdkSourceImpl( ), ) DeriveKeyConnectorResult.Success(key) - } catch (exception: BitwardenException) { - when { - exception.message == "Wrong password" -> { + } catch (ex: BitwardenException.DeriveKeyConnector) { + when (ex.v1) { + is DeriveKeyConnectorException.WrongPassword -> { DeriveKeyConnectorResult.WrongPasswordError } - - else -> DeriveKeyConnectorResult.Error(exception) + is DeriveKeyConnectorException.Crypto -> { + DeriveKeyConnectorResult.Error(error = ex) + } } + } catch (exception: BitwardenException) { + DeriveKeyConnectorResult.Error(error = exception) } } diff --git a/app/src/test/kotlin/com/x8bit/bitwarden/data/vault/datasource/sdk/VaultSdkSourceTest.kt b/app/src/test/kotlin/com/x8bit/bitwarden/data/vault/datasource/sdk/VaultSdkSourceTest.kt index c37716a415..4155b81379 100644 --- a/app/src/test/kotlin/com/x8bit/bitwarden/data/vault/datasource/sdk/VaultSdkSourceTest.kt +++ b/app/src/test/kotlin/com/x8bit/bitwarden/data/vault/datasource/sdk/VaultSdkSourceTest.kt @@ -2,6 +2,7 @@ package com.x8bit.bitwarden.data.vault.datasource.sdk import com.bitwarden.collections.Collection import com.bitwarden.collections.CollectionView +import com.bitwarden.core.DeriveKeyConnectorException import com.bitwarden.core.DeriveKeyConnectorRequest import com.bitwarden.core.DerivePinKeyResponse import com.bitwarden.core.InitOrgCryptoRequest @@ -199,15 +200,61 @@ class VaultSdkSourceTest { } @Test - fun `deriveKeyConnector should call SDK and return a Result with wrong password`() = + @Suppress("MaxLineLength") + fun `deriveKeyConnector should call SDK with WrongPassword exception and return a Result with wrong password`() = runBlocking { val userId = "userId" val userKeyEncrypted = "userKeyEncrypted" val email = "email" val password = "password" - val error = mockk { - every { message } returns "Wrong password" + val kdf = mockk() + coEvery { + clientCrypto.deriveKeyConnector( + request = DeriveKeyConnectorRequest( + userKeyEncrypted = userKeyEncrypted, + email = email, + password = password, + kdf = kdf, + ), + ) + } throws BitwardenException.DeriveKeyConnector( + v1 = DeriveKeyConnectorException.WrongPassword(message = "mock message"), + ) + val result = vaultSdkSource.deriveKeyConnector( + userId = userId, + userKeyEncrypted = userKeyEncrypted, + email = email, + password = password, + kdf = kdf, + ) + assertEquals( + DeriveKeyConnectorResult.WrongPasswordError, + result.getOrNull(), + ) + coVerify(exactly = 1) { + sdkClientManager.getOrCreateClient(userId = userId) + clientCrypto.deriveKeyConnector( + request = DeriveKeyConnectorRequest( + userKeyEncrypted = userKeyEncrypted, + email = email, + password = password, + kdf = kdf, + ), + ) } + } + + @Test + @Suppress("MaxLineLength") + fun `deriveKeyConnector should call SDK with Crypto exception return and return a Result with error`() = + runBlocking { + val userId = "userId" + val userKeyEncrypted = "userKeyEncrypted" + val email = "email" + val password = "password" + val error = BitwardenException.DeriveKeyConnector( + v1 = DeriveKeyConnectorException.Crypto(message = "mock message"), + ) val kdf = mockk() coEvery { clientCrypto.deriveKeyConnector( @@ -227,7 +274,7 @@ class VaultSdkSourceTest { kdf = kdf, ) assertEquals( - DeriveKeyConnectorResult.WrongPasswordError, + DeriveKeyConnectorResult.Error(error = error), result.getOrNull(), ) coVerify(exactly = 1) {