From a7a5fdbf63129b01de8ce4094954742db14fac33 Mon Sep 17 00:00:00 2001 From: Jonatan Rhodin Date: Thu, 31 Aug 2023 15:14:17 +0200 Subject: [PATCH 1/3] Fix number format crash in custom port dialog --- .../mullvadvpn/compose/dialog/CustomPortDialog.kt | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/dialog/CustomPortDialog.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/dialog/CustomPortDialog.kt index c2298783d86a..1d42503d1ccc 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/dialog/CustomPortDialog.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/dialog/CustomPortDialog.kt @@ -12,10 +12,12 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.testTag import androidx.compose.ui.res.stringResource import androidx.compose.ui.tooling.preview.Preview import net.mullvad.mullvadvpn.R import net.mullvad.mullvadvpn.compose.button.ActionButton +import net.mullvad.mullvadvpn.compose.test.CUSTOM_PORT_DIALOG_INPUT_TEST_TAG import net.mullvad.mullvadvpn.compose.textfield.CustomPortTextField import net.mullvad.mullvadvpn.lib.theme.AlphaDescription import net.mullvad.mullvadvpn.lib.theme.AlphaDisabled @@ -75,7 +77,7 @@ fun CustomPortDialog( ), isEnabled = port.value.isNotEmpty() && - allowedPortRanges.isPortInValidRanges(port.value.toInt()) + allowedPortRanges.isPortInValidRanges(port.value.toIntOrNull() ?: 0) ) if (showReset) { ActionButton( @@ -108,7 +110,7 @@ fun CustomPortDialog( onSubmit = { input -> if ( input.isNotEmpty() && - allowedPortRanges.isPortInValidRanges(input.toInt()) + allowedPortRanges.isPortInValidRanges(input.toIntOrNull() ?: 0) ) { onSave(input) } @@ -116,7 +118,8 @@ fun CustomPortDialog( onValueChanged = { input -> port.value = input }, isValidValue = port.value.isNotEmpty() && - allowedPortRanges.isPortInValidRanges(port.value.toInt()) + allowedPortRanges.isPortInValidRanges(port.value.toIntOrNull() ?: 0), + modifier = Modifier.testTag(CUSTOM_PORT_DIALOG_INPUT_TEST_TAG) ) Spacer(modifier = Modifier.height(Dimens.smallPadding)) Text( From ff74284daee2b4b5f9e32f53af0bc17db4bf010e Mon Sep 17 00:00:00 2001 From: Jonatan Rhodin Date: Thu, 31 Aug 2023 15:16:46 +0200 Subject: [PATCH 2/3] Limit length of custom port input to 5 --- .../mullvad/mullvadvpn/compose/dialog/CustomPortDialog.kt | 3 +-- .../mullvadvpn/compose/textfield/CustomPortTextField.kt | 6 ++++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/dialog/CustomPortDialog.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/dialog/CustomPortDialog.kt index 1d42503d1ccc..b5487ed43609 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/dialog/CustomPortDialog.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/dialog/CustomPortDialog.kt @@ -17,7 +17,6 @@ import androidx.compose.ui.res.stringResource import androidx.compose.ui.tooling.preview.Preview import net.mullvad.mullvadvpn.R import net.mullvad.mullvadvpn.compose.button.ActionButton -import net.mullvad.mullvadvpn.compose.test.CUSTOM_PORT_DIALOG_INPUT_TEST_TAG import net.mullvad.mullvadvpn.compose.textfield.CustomPortTextField import net.mullvad.mullvadvpn.lib.theme.AlphaDescription import net.mullvad.mullvadvpn.lib.theme.AlphaDisabled @@ -119,7 +118,7 @@ fun CustomPortDialog( isValidValue = port.value.isNotEmpty() && allowedPortRanges.isPortInValidRanges(port.value.toIntOrNull() ?: 0), - modifier = Modifier.testTag(CUSTOM_PORT_DIALOG_INPUT_TEST_TAG) + maxCharLength = 5 ) Spacer(modifier = Modifier.height(Dimens.smallPadding)) Text( diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/textfield/CustomPortTextField.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/textfield/CustomPortTextField.kt index 956c65eedfb4..e1478e198716 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/textfield/CustomPortTextField.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/textfield/CustomPortTextField.kt @@ -12,7 +12,8 @@ fun CustomPortTextField( modifier: Modifier = Modifier, onSubmit: (String) -> Unit, onValueChanged: (String) -> Unit, - isValidValue: Boolean + isValidValue: Boolean, + maxCharLength: Int ) { CustomTextField( value = value, @@ -24,6 +25,7 @@ fun CustomPortTextField( onSubmit = onSubmit, isDigitsOnlyAllowed = true, isEnabled = true, - isValidValue = isValidValue + isValidValue = isValidValue, + maxCharLength = maxCharLength ) } From 781bff221837ee2be12f94df6fbd151f57e095ce Mon Sep 17 00:00:00 2001 From: Jonatan Rhodin Date: Thu, 31 Aug 2023 15:19:15 +0200 Subject: [PATCH 3/3] Add test for invalid int input to custom port dialog --- .../compose/screen/VpnSettingsScreenTest.kt | 28 +++++++++++++++++++ .../compose/dialog/CustomPortDialog.kt | 4 ++- .../compose/test/ComposeTestTagConstants.kt | 1 + 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/android/app/src/androidTest/kotlin/net/mullvad/mullvadvpn/compose/screen/VpnSettingsScreenTest.kt b/android/app/src/androidTest/kotlin/net/mullvad/mullvadvpn/compose/screen/VpnSettingsScreenTest.kt index 47f7d8280c51..dc190c6bdf36 100644 --- a/android/app/src/androidTest/kotlin/net/mullvad/mullvadvpn/compose/screen/VpnSettingsScreenTest.kt +++ b/android/app/src/androidTest/kotlin/net/mullvad/mullvadvpn/compose/screen/VpnSettingsScreenTest.kt @@ -17,6 +17,7 @@ import io.mockk.verifyAll import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.asSharedFlow import net.mullvad.mullvadvpn.compose.state.VpnSettingsUiState +import net.mullvad.mullvadvpn.compose.test.CUSTOM_PORT_DIALOG_INPUT_TEST_TAG import net.mullvad.mullvadvpn.compose.test.LAZY_LIST_LAST_ITEM_TEST_TAG import net.mullvad.mullvadvpn.compose.test.LAZY_LIST_QUANTUM_ITEM_OFF_TEST_TAG import net.mullvad.mullvadvpn.compose.test.LAZY_LIST_QUANTUM_ITEM_ON_TEST_TAG @@ -813,6 +814,33 @@ class VpnSettingsScreenTest { verify { onWireguardPortSelected.invoke(Constraint.Only(Port(4000))) } } + @Test + fun testShowWireguardCustomPortDialogInvalidInt() { + // Input a number to make sure that a too long number does not show and it does not crash + // the app + + // Arrange + composeTestRule.setContent { + VpnSettingsScreen( + uiState = + VpnSettingsUiState.CustomPortDialogUiState( + availablePortRanges = listOf(PortRange(53, 53), PortRange(120, 121)) + ), + toastMessagesSharedFlow = MutableSharedFlow().asSharedFlow() + ) + } + + // Act + composeTestRule + .onNodeWithTag(CUSTOM_PORT_DIALOG_INPUT_TEST_TAG) + .performTextInput("21474836471") + + // Assert + composeTestRule + .onNodeWithTagAndText(CUSTOM_PORT_DIALOG_INPUT_TEST_TAG, "21474836471") + .assertDoesNotExist() + } + companion object { private const val LOCAL_DNS_SERVER_WARNING = "The local DNS server will not work unless you enable " + diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/dialog/CustomPortDialog.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/dialog/CustomPortDialog.kt index b5487ed43609..3c36ca8f8885 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/dialog/CustomPortDialog.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/dialog/CustomPortDialog.kt @@ -17,6 +17,7 @@ import androidx.compose.ui.res.stringResource import androidx.compose.ui.tooling.preview.Preview import net.mullvad.mullvadvpn.R import net.mullvad.mullvadvpn.compose.button.ActionButton +import net.mullvad.mullvadvpn.compose.test.CUSTOM_PORT_DIALOG_INPUT_TEST_TAG import net.mullvad.mullvadvpn.compose.textfield.CustomPortTextField import net.mullvad.mullvadvpn.lib.theme.AlphaDescription import net.mullvad.mullvadvpn.lib.theme.AlphaDisabled @@ -118,7 +119,8 @@ fun CustomPortDialog( isValidValue = port.value.isNotEmpty() && allowedPortRanges.isPortInValidRanges(port.value.toIntOrNull() ?: 0), - maxCharLength = 5 + maxCharLength = 5, + modifier = Modifier.testTag(CUSTOM_PORT_DIALOG_INPUT_TEST_TAG) ) Spacer(modifier = Modifier.height(Dimens.smallPadding)) Text( diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/test/ComposeTestTagConstants.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/test/ComposeTestTagConstants.kt index f896183bcb2e..7e1664119130 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/test/ComposeTestTagConstants.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/test/ComposeTestTagConstants.kt @@ -10,6 +10,7 @@ const val LAZY_LIST_WIREGUARD_CUSTOM_PORT_TEXT_TEST_TAG = "lazy_list_wireguard_custom_port_text_test_tag" const val LAZY_LIST_WIREGUARD_CUSTOM_PORT_NUMBER_TEST_TAG = "lazy_list_wireguard_custom_port_number_test_tag" +const val CUSTOM_PORT_DIALOG_INPUT_TEST_TAG = "custom_port_dialog_input_test_tag" // SelectLocationScreen, ConnectScreen const val CIRCULAR_PROGRESS_INDICATOR = "circular_progress_indicator"