From 3a97abeb62111869e865adb4206091ba0d55184a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20G=C3=B6ransson?= Date: Fri, 29 Sep 2023 20:09:39 +0200 Subject: [PATCH] Add class test and fix issues --- .../mullvad/mullvadvpn/model/TunnelState.kt | 42 +++++++++--------- .../mullvadvpn/service/MullvadVpnService.kt | 15 ++++--- .../service/endpoint/AccountCache.kt | 19 ++++---- .../service/endpoint/AuthTokenCache.kt | 12 ++--- .../service/endpoint/LocationInfoCache.kt | 13 +++--- .../service/endpoint/RelayListListener.kt | 13 +++--- .../service/endpoint/ServiceEndpoint.kt | 15 ++++--- .../AccountExpiryNotification.kt | 11 ++--- .../notifications/TunnelStateNotification.kt | 8 ++-- .../TunnelStateNotificationAction.kt | 44 +++++++++---------- .../test/arch/classes/ClassTests.kt | 21 +++++++++ 11 files changed, 120 insertions(+), 93 deletions(-) create mode 100644 android/test/arch/src/test/kotlin/net/mullvad/mullvadvpn/test/arch/classes/ClassTests.kt diff --git a/android/lib/model/src/main/kotlin/net/mullvad/mullvadvpn/model/TunnelState.kt b/android/lib/model/src/main/kotlin/net/mullvad/mullvadvpn/model/TunnelState.kt index c3d58d2ca7e9..364c8861ceda 100644 --- a/android/lib/model/src/main/kotlin/net/mullvad/mullvadvpn/model/TunnelState.kt +++ b/android/lib/model/src/main/kotlin/net/mullvad/mullvadvpn/model/TunnelState.kt @@ -34,6 +34,27 @@ sealed class TunnelState : Parcelable { } } + override fun toString(): String = + when (this) { + is Disconnected -> DISCONNECTED + is Connecting -> CONNECTING + is Connected -> CONNECTED + is Disconnecting -> { + if (actionAfterDisconnect == ActionAfterDisconnect.Reconnect) { + RECONNECTING + } else { + DISCONNECTING + } + } + is Error -> { + if (errorState.isBlocking) { + BLOCKING + } else { + ERROR + } + } + } + companion object { const val DISCONNECTED = "disconnected" const val CONNECTING = "connecting" @@ -58,25 +79,4 @@ sealed class TunnelState : Parcelable { } } } - - override fun toString(): String = - when (this) { - is Disconnected -> DISCONNECTED - is Connecting -> CONNECTING - is Connected -> CONNECTED - is Disconnecting -> { - if (actionAfterDisconnect == ActionAfterDisconnect.Reconnect) { - RECONNECTING - } else { - DISCONNECTING - } - } - is Error -> { - if (errorState.isBlocking) { - BLOCKING - } else { - ERROR - } - } - } } diff --git a/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt b/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt index 3f7149b282a4..cd05d5cc7139 100644 --- a/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt +++ b/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt @@ -28,13 +28,6 @@ import net.mullvad.talpid.TalpidVpnService import org.koin.core.context.loadKoinModules class MullvadVpnService : TalpidVpnService() { - companion object { - private val TAG = "mullvad" - - init { - System.loadLibrary("mullvad_jni") - } - } private enum class PendingAction { Connect, @@ -273,4 +266,12 @@ class MullvadVpnService : TalpidVpnService() { startActivity(intent) } + + companion object { + private val TAG = "mullvad" + + init { + System.loadLibrary("mullvad_jni") + } + } } diff --git a/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/AccountCache.kt b/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/AccountCache.kt index d6ba237f97e0..ba470a6b2428 100644 --- a/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/AccountCache.kt +++ b/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/AccountCache.kt @@ -17,15 +17,6 @@ import net.mullvad.mullvadvpn.model.GetAccountDataResult import net.mullvad.talpid.util.EventNotifier class AccountCache(private val endpoint: ServiceEndpoint) { - companion object { - private sealed class Command { - object CreateAccount : Command() - - data class Login(val account: String) : Command() - - object Logout : Command() - } - } private val commandChannel = spawnActor() @@ -179,4 +170,14 @@ class AccountCache(private val endpoint: ServiceEndpoint) { private suspend fun fetchAccountData(accountToken: String): GetAccountDataResult { return daemon.await().getAccountData(accountToken) } + + companion object { + private sealed class Command { + object CreateAccount : Command() + + data class Login(val account: String) : Command() + + object Logout : Command() + } + } } diff --git a/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/AuthTokenCache.kt b/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/AuthTokenCache.kt index 6506c0469d6c..08b0943c4d6f 100644 --- a/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/AuthTokenCache.kt +++ b/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/AuthTokenCache.kt @@ -11,12 +11,6 @@ import net.mullvad.mullvadvpn.lib.ipc.Event import net.mullvad.mullvadvpn.lib.ipc.Request class AuthTokenCache(endpoint: ServiceEndpoint) { - companion object { - private enum class Command { - Fetch - } - } - private val daemon = endpoint.intermittentDaemon private val requestQueue = spawnActor() @@ -46,4 +40,10 @@ class AuthTokenCache(endpoint: ServiceEndpoint) { // Closed sender, so stop the actor } } + + companion object { + private enum class Command { + Fetch + } + } } diff --git a/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/LocationInfoCache.kt b/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/LocationInfoCache.kt index e050421699b1..2d06cc109f73 100644 --- a/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/LocationInfoCache.kt +++ b/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/LocationInfoCache.kt @@ -22,12 +22,6 @@ import net.mullvad.mullvadvpn.service.util.ExponentialBackoff import net.mullvad.talpid.tunnel.ActionAfterDisconnect class LocationInfoCache(private val endpoint: ServiceEndpoint) { - companion object { - private enum class RequestFetch { - ForRealLocation, - ForRelayLocation, - } - } private val fetchRetryDelays = ExponentialBackoff().apply { @@ -135,4 +129,11 @@ class LocationInfoCache(private val endpoint: ServiceEndpoint) { selectedRelayLocation = constraint?.value?.toGeographicLocationConstraint()?.location } + + companion object { + private enum class RequestFetch { + ForRealLocation, + ForRelayLocation, + } + } } diff --git a/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/RelayListListener.kt b/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/RelayListListener.kt index 1abf64907c89..09f90a44d5da 100644 --- a/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/RelayListListener.kt +++ b/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/RelayListListener.kt @@ -19,12 +19,6 @@ import net.mullvad.mullvadvpn.model.WireguardConstraints import net.mullvad.mullvadvpn.service.MullvadDaemon class RelayListListener(endpoint: ServiceEndpoint) { - companion object { - private enum class Command { - SetRelayLocation, - SetWireguardConstraints - } - } private val commandChannel = spawnActor() private val daemon = endpoint.intermittentDaemon @@ -105,4 +99,11 @@ class RelayListListener(endpoint: ServiceEndpoint) { daemon.await().updateRelaySettings(update) } + + companion object { + private enum class Command { + SetRelayLocation, + SetWireguardConstraints + } + } } diff --git a/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/ServiceEndpoint.kt b/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/ServiceEndpoint.kt index e9aa8d432860..fe7ddb75e9cc 100644 --- a/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/ServiceEndpoint.kt +++ b/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/ServiceEndpoint.kt @@ -27,13 +27,6 @@ class ServiceEndpoint( val connectivityListener: ConnectivityListener, context: Context ) { - companion object { - sealed class Command { - data class RegisterListener(val listener: Messenger) : Command() - - data class UnregisterListener(val listenerId: Int) : Command() - } - } private val listeners = mutableMapOf() private val commands: SendChannel = startRegistrator() @@ -165,4 +158,12 @@ class ServiceEndpoint( return listenerId } + + companion object { + sealed class Command { + data class RegisterListener(val listener: Messenger) : Command() + + data class UnregisterListener(val listenerId: Int) : Command() + } + } } diff --git a/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/AccountExpiryNotification.kt b/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/AccountExpiryNotification.kt index b6085221f176..6ed6a5b72ec0 100644 --- a/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/AccountExpiryNotification.kt +++ b/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/AccountExpiryNotification.kt @@ -28,11 +28,6 @@ class AccountExpiryNotification( val daemon: Intermittent, val accountCache: AccountCache ) { - companion object { - val NOTIFICATION_ID: Int = 2 - val REMAINING_TIME_FOR_REMINDERS = Duration.standardDays(2) - val TIME_BETWEEN_CHECKS: Long = 12 /* h */ * 60 /* min */ * 60 /* s */ * 1000 /* ms */ - } private val jobTracker = JobTracker() private val resources = context.resources @@ -143,4 +138,10 @@ class AccountExpiryNotification( private fun getRemainingText(pluralId: Int, quantity: Int): String { return resources.getQuantityString(pluralId, quantity, quantity) } + + companion object { + val NOTIFICATION_ID: Int = 2 + val REMAINING_TIME_FOR_REMINDERS = Duration.standardDays(2) + val TIME_BETWEEN_CHECKS: Long = 12 /* h */ * 60 /* min */ * 60 /* s */ * 1000 /* ms */ + } } diff --git a/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/TunnelStateNotification.kt b/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/TunnelStateNotification.kt index a19358f95eba..97bddc860805 100644 --- a/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/TunnelStateNotification.kt +++ b/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/TunnelStateNotification.kt @@ -18,10 +18,6 @@ import net.mullvad.talpid.tunnel.ActionAfterDisconnect import net.mullvad.talpid.tunnel.ErrorStateCause class TunnelStateNotification(val context: Context) { - companion object { - val NOTIFICATION_ID: Int = 1 - } - private val channel = NotificationChannel( context, @@ -149,4 +145,8 @@ class TunnelStateNotification(val context: Context) { return NotificationCompat.Action(action.icon, label, pendingIntent) } + + companion object { + val NOTIFICATION_ID: Int = 1 + } } diff --git a/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/TunnelStateNotificationAction.kt b/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/TunnelStateNotificationAction.kt index c415940ea87c..c836c765f6bb 100644 --- a/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/TunnelStateNotificationAction.kt +++ b/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/notifications/TunnelStateNotificationAction.kt @@ -12,28 +12,6 @@ enum class TunnelStateNotificationAction { Cancel, Dismiss; - companion object { - fun from(tunnelState: TunnelState) = - when (tunnelState) { - is TunnelState.Disconnected -> Connect - is TunnelState.Connecting -> Cancel - is TunnelState.Connected -> Disconnect - is TunnelState.Disconnecting -> { - when (tunnelState.actionAfterDisconnect) { - ActionAfterDisconnect.Reconnect -> Cancel - else -> Connect - } - } - is TunnelState.Error -> { - if (tunnelState.errorState.isBlocking) { - Disconnect - } else { - Dismiss - } - } - } - } - val text get() = when (this) { @@ -56,4 +34,26 @@ enum class TunnelStateNotificationAction { Connect -> R.drawable.icon_notification_connect else -> R.drawable.icon_notification_disconnect } + + companion object { + fun from(tunnelState: TunnelState) = + when (tunnelState) { + is TunnelState.Disconnected -> Connect + is TunnelState.Connecting -> Cancel + is TunnelState.Connected -> Disconnect + is TunnelState.Disconnecting -> { + when (tunnelState.actionAfterDisconnect) { + ActionAfterDisconnect.Reconnect -> Cancel + else -> Connect + } + } + is TunnelState.Error -> { + if (tunnelState.errorState.isBlocking) { + Disconnect + } else { + Dismiss + } + } + } + } } diff --git a/android/test/arch/src/test/kotlin/net/mullvad/mullvadvpn/test/arch/classes/ClassTests.kt b/android/test/arch/src/test/kotlin/net/mullvad/mullvadvpn/test/arch/classes/ClassTests.kt new file mode 100644 index 000000000000..cc737250f65a --- /dev/null +++ b/android/test/arch/src/test/kotlin/net/mullvad/mullvadvpn/test/arch/classes/ClassTests.kt @@ -0,0 +1,21 @@ +package net.mullvad.mullvadvpn.test.arch.classes + +import com.lemonappdev.konsist.api.verify.assert +import net.mullvad.mullvadvpn.test.arch.extensions.projectScope +import org.junit.Test + +class ClassTests { + @Test + fun `companion object is last declaration in the class`() { + projectScope().classes(includeNested = true).assert { + val companionObject = + it.objects(includeNested = false).lastOrNull { obj -> obj.hasCompanionModifier } + if (companionObject != null) { + it.declarations(includeNested = false, includeLocal = false).last() == + companionObject + } else { + true + } + } + } +}