From fa6cacdf9e25bce1efab9b5987bafeb3d7a3ba76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20G=C3=B6ransson?= Date: Thu, 7 Nov 2024 15:09:21 +0100 Subject: [PATCH] Fix incorrect parsing of auth failed error --- .../mullvadvpn/usecase/OutOfTimeUseCaseTest.kt | 3 ++- .../lib/daemon/grpc/mapper/ToDomain.kt | 16 +++++++++++++++- .../mullvadvpn/lib/model/ErrorStateCause.kt | 18 ++++++++++++------ 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/android/app/src/test/kotlin/net/mullvad/mullvadvpn/usecase/OutOfTimeUseCaseTest.kt b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/usecase/OutOfTimeUseCaseTest.kt index e12c2a1d88bd..061255541221 100644 --- a/android/app/src/test/kotlin/net/mullvad/mullvadvpn/usecase/OutOfTimeUseCaseTest.kt +++ b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/usecase/OutOfTimeUseCaseTest.kt @@ -20,6 +20,7 @@ import kotlinx.coroutines.test.resetMain import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.setMain import net.mullvad.mullvadvpn.lib.model.AccountData +import net.mullvad.mullvadvpn.lib.model.AuthFailedError import net.mullvad.mullvadvpn.lib.model.ErrorState import net.mullvad.mullvadvpn.lib.model.ErrorStateCause import net.mullvad.mullvadvpn.lib.model.TunnelState @@ -74,7 +75,7 @@ class OutOfTimeUseCaseTest { fun `tunnel is blocking because out of time should emit true`() = scope.runTest { // Arrange - val errorStateCause = ErrorStateCause.AuthFailed("[EXPIRED_ACCOUNT]") + val errorStateCause = ErrorStateCause.AuthFailed(AuthFailedError.ExpiredAccount) val tunnelStateError = TunnelState.Error(ErrorState(errorStateCause, true)) // Act, Assert diff --git a/android/lib/daemon-grpc/src/main/kotlin/net/mullvad/mullvadvpn/lib/daemon/grpc/mapper/ToDomain.kt b/android/lib/daemon-grpc/src/main/kotlin/net/mullvad/mullvadvpn/lib/daemon/grpc/mapper/ToDomain.kt index fc4c64942f25..9c54bc5bc5c4 100644 --- a/android/lib/daemon-grpc/src/main/kotlin/net/mullvad/mullvadvpn/lib/daemon/grpc/mapper/ToDomain.kt +++ b/android/lib/daemon-grpc/src/main/kotlin/net/mullvad/mullvadvpn/lib/daemon/grpc/mapper/ToDomain.kt @@ -19,6 +19,7 @@ import net.mullvad.mullvadvpn.lib.model.ApiAccessMethodName import net.mullvad.mullvadvpn.lib.model.ApiAccessMethodSetting import net.mullvad.mullvadvpn.lib.model.AppId import net.mullvad.mullvadvpn.lib.model.AppVersionInfo +import net.mullvad.mullvadvpn.lib.model.AuthFailedError import net.mullvad.mullvadvpn.lib.model.Cipher import net.mullvad.mullvadvpn.lib.model.Constraint import net.mullvad.mullvadvpn.lib.model.CustomDnsOptions @@ -202,7 +203,7 @@ internal fun ManagementInterface.ErrorState.toDomain(): ErrorState = cause = when (cause!!) { ManagementInterface.ErrorState.Cause.AUTH_FAILED -> - ErrorStateCause.AuthFailed(authFailedError.name) + ErrorStateCause.AuthFailed(authFailedError.toDomain()) ManagementInterface.ErrorState.Cause.IPV6_UNAVAILABLE -> ErrorStateCause.Ipv6Unavailable ManagementInterface.ErrorState.Cause.SET_FIREWALL_POLICY_ERROR -> @@ -225,6 +226,19 @@ internal fun ManagementInterface.ErrorState.toDomain(): ErrorState = isBlocking = !hasBlockingError(), ) +private fun ManagementInterface.ErrorState.AuthFailedError.toDomain(): AuthFailedError = + when (this) { + ManagementInterface.ErrorState.AuthFailedError.UNKNOWN -> AuthFailedError.Unknown + ManagementInterface.ErrorState.AuthFailedError.INVALID_ACCOUNT -> + AuthFailedError.InvalidAccount + ManagementInterface.ErrorState.AuthFailedError.EXPIRED_ACCOUNT -> + AuthFailedError.ExpiredAccount + ManagementInterface.ErrorState.AuthFailedError.TOO_MANY_CONNECTIONS -> + AuthFailedError.TooManyConnections + ManagementInterface.ErrorState.AuthFailedError.UNRECOGNIZED -> + throw IllegalArgumentException("Unrecognized auth failed error") + } + internal fun ManagementInterface.ErrorState.FirewallPolicyError.toDomain(): ErrorStateCause.FirewallPolicyError = when (type!!) { diff --git a/android/lib/model/src/main/kotlin/net/mullvad/mullvadvpn/lib/model/ErrorStateCause.kt b/android/lib/model/src/main/kotlin/net/mullvad/mullvadvpn/lib/model/ErrorStateCause.kt index 0ba63a4b0856..3af72d663be4 100644 --- a/android/lib/model/src/main/kotlin/net/mullvad/mullvadvpn/lib/model/ErrorStateCause.kt +++ b/android/lib/model/src/main/kotlin/net/mullvad/mullvadvpn/lib/model/ErrorStateCause.kt @@ -3,13 +3,9 @@ package net.mullvad.mullvadvpn.lib.model import java.net.InetAddress sealed class ErrorStateCause { - class AuthFailed(private val reason: String?) : ErrorStateCause() { + class AuthFailed(private val error: AuthFailedError) : ErrorStateCause() { fun isCausedByExpiredAccount(): Boolean { - return reason == AUTH_FAILED_REASON_EXPIRED_ACCOUNT - } - - companion object { - private const val AUTH_FAILED_REASON_EXPIRED_ACCOUNT = "[EXPIRED_ACCOUNT]" + return error is AuthFailedError.ExpiredAccount } } @@ -32,3 +28,13 @@ sealed class ErrorStateCause { data object VpnPermissionDenied : ErrorStateCause() } + +sealed interface AuthFailedError { + data object ExpiredAccount : AuthFailedError + + data object InvalidAccount : AuthFailedError + + data object TooManyConnections : AuthFailedError + + data object Unknown : AuthFailedError +}