Skip to content

Commit

Permalink
Merge branch 'crash-when-custom-port-input-exceeds-a-certain-length-o…
Browse files Browse the repository at this point in the history
…r-droid-294'
  • Loading branch information
Pururun committed Sep 1, 2023
2 parents 2bbf2d6 + 781bff2 commit 81b09e4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<String>().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 " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -75,7 +77,7 @@ fun CustomPortDialog(
),
isEnabled =
port.value.isNotEmpty() &&
allowedPortRanges.isPortInValidRanges(port.value.toInt())
allowedPortRanges.isPortInValidRanges(port.value.toIntOrNull() ?: 0)
)
if (showReset) {
ActionButton(
Expand Down Expand Up @@ -108,15 +110,17 @@ fun CustomPortDialog(
onSubmit = { input ->
if (
input.isNotEmpty() &&
allowedPortRanges.isPortInValidRanges(input.toInt())
allowedPortRanges.isPortInValidRanges(input.toIntOrNull() ?: 0)
) {
onSave(input)
}
},
onValueChanged = { input -> port.value = input },
isValidValue =
port.value.isNotEmpty() &&
allowedPortRanges.isPortInValidRanges(port.value.toInt())
allowedPortRanges.isPortInValidRanges(port.value.toIntOrNull() ?: 0),
maxCharLength = 5,
modifier = Modifier.testTag(CUSTOM_PORT_DIALOG_INPUT_TEST_TAG)
)
Spacer(modifier = Modifier.height(Dimens.smallPadding))
Text(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ fun CustomPortTextField(
modifier: Modifier = Modifier,
onSubmit: (String) -> Unit,
onValueChanged: (String) -> Unit,
isValidValue: Boolean
isValidValue: Boolean,
maxCharLength: Int
) {
CustomTextField(
value = value,
Expand All @@ -24,6 +25,7 @@ fun CustomPortTextField(
onSubmit = onSubmit,
isDigitsOnlyAllowed = true,
isEnabled = true,
isValidValue = isValidValue
isValidValue = isValidValue,
maxCharLength = maxCharLength
)
}

0 comments on commit 81b09e4

Please sign in to comment.