Skip to content

Commit

Permalink
Fix incorrect parsing of auth failed error
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawa committed Nov 12, 2024
1 parent e1567cf commit fa6cacd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ->
Expand All @@ -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!!) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -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
}

0 comments on commit fa6cacd

Please sign in to comment.