Harden the setup wizard after the branch review

Written-by: Gemini 4 Hydrogen (Antigravity)
Written-by: Claude Fable 5.2
This commit is contained in:
Rsclub2_2 committed 2026-11-18 21:32:12 +01:00
1 parent f871f551f7
commit a8cfe94853
13 files changed
+248 -31

No files matched your search

@@ -517,9 +517,11 @@ wizard.source.stationKeep=Station unverändert lassen (Locator {0})
wizard.personal.title=Persönliches
wizard.personal.lead=Gehört zum Operator.
wizard.personal.name=Anzeigename im Chat
wizard.personal.nameInvalid=Der Name darf kein Zeichen | enthalten.
wizard.login.title=Anmeldung bei ON4KST
wizard.login.lead=Die Zugangsdaten deines ON4KST-Kontos.
wizard.login.callsignInvalid=Das sieht nicht nach einem Rufzeichen aus.
wizard.login.passwordInvalid=Das Passwort darf nicht mit einem Leerzeichen beginnen oder enden und kein Zeichen | enthalten.
wizard.site.title=Standort und Bänder
wizard.site.lead=Gehört zur Station, nicht zum Operator.
wizard.site.locatorInvalid=Sechsstelliger Maidenhead-Locator, zum Beispiel JO50AA.
@@ -526,9 +526,11 @@ wizard.source.stationKeep=Leave the station unchanged (locator {0})
wizard.personal.title=Personal
wizard.personal.lead=Belongs to the operator.
wizard.personal.name=Name shown in the chat
wizard.personal.nameInvalid=The name must not contain a | character.
wizard.login.title=ON4KST login
wizard.login.lead=The credentials of your ON4KST account.
wizard.login.callsignInvalid=This does not look like a callsign.
wizard.login.passwordInvalid=The password must not begin or end with a blank and must not contain a | character.
wizard.site.title=Location and bands
wizard.site.lead=Belongs to the station, not to the operator.
wizard.site.locatorInvalid=Six-character Maidenhead locator, for example JO50AA.
@@ -32,6 +32,13 @@ public final class ApplicationRuntimeLauncher {
private static final java.util.concurrent.atomic.AtomicBoolean SETUP_WIZARD_REQUESTED =
new java.util.concurrent.atomic.AtomicBoolean(false);
/**
* Guards {@link #switchProfile(OperatorProfile)} against re-entry while window teardown
* inside {@link Kst4ContestApplication#shutdownRuntime()} is still pumping events.
*/
private static final java.util.concurrent.atomic.AtomicBoolean SWITCH_IN_PROGRESS =
new java.util.concurrent.atomic.AtomicBoolean(false);
private ApplicationRuntimeLauncher() {
// Utility class.
}
@@ -83,6 +90,20 @@ public final class ApplicationRuntimeLauncher {
return SETUP_WIZARD_REQUESTED.getAndSet(false);
}
/**
* Marks a profile switch as in progress if none is currently running.
*
* @return true if this call obtained the mark
*/
static boolean beginSwitch() {
return SWITCH_IN_PROGRESS.compareAndSet(false, true);
}
/** Clears the in-progress mark of a profile switch. */
static void endSwitch() {
SWITCH_IN_PROGRESS.set(false);
}
/**
* Rebuilds the runtime of the active profile with the setup wizard in front of it.
*
@@ -100,7 +121,16 @@ public final class ApplicationRuntimeLauncher {
}
requestSetupWizard();
return switchProfile(active.getProfile());
try {
boolean switched = switchProfile(active.getProfile());
if (!switched) {
consumeSetupWizardRequest();
}
return switched;
} catch (RuntimeException e) {
consumeSetupWizardRequest();
throw e;
}
}
/**
@@ -111,33 +141,41 @@ public final class ApplicationRuntimeLauncher {
*/
public static boolean switchProfile(final OperatorProfile targetProfile) {
if (targetProfile == null) {
if (!beginSwitch()) {
return false;
}
new OperatorProfileStore().recordLastUsed(targetProfile.getProfileId());
if (currentRuntime != null) {
currentRuntime.shutdownRuntime();
}
ActiveOperatorProfile.set(OperatorProfilePaths.resolve(targetProfile));
Kst4ContestApplication nextRuntime = new Kst4ContestApplication();
try {
nextRuntime.startRuntime();
} catch (Exception e) {
// The previous runtime is already gone, so there is nothing left to return to.
LOGGER.log(Level.SEVERE, "Could not start the selected operator profile", e);
Kst4ContestApplication.alertWindowEvent(
"The operator profile could not be started: " + e.getMessage()
+ "\n\nKST4Contest has to be closed.");
System.exit(1);
return false;
}
if (targetProfile == null) {
return false;
}
setCurrent(nextRuntime);
return true;
new OperatorProfileStore().recordLastUsed(targetProfile.getProfileId());
if (currentRuntime != null) {
currentRuntime.shutdownRuntime();
}
ActiveOperatorProfile.set(OperatorProfilePaths.resolve(targetProfile));
Kst4ContestApplication nextRuntime = new Kst4ContestApplication();
try {
nextRuntime.startRuntime();
} catch (Exception e) {
// The previous runtime is already gone, so there is nothing left to return to.
LOGGER.log(Level.SEVERE, "Could not start the selected operator profile", e);
Kst4ContestApplication.alertWindowEvent(
"The operator profile could not be started: " + e.getMessage()
+ "\n\nKST4Contest has to be closed.");
System.exit(1);
return false;
}
setCurrent(nextRuntime);
return true;
} finally {
endSwitch();
}
}
}
@@ -44,7 +44,7 @@ private fun categoryName(number: Int): String = ChatCategory(number).getChatCate
* The wizard's pages and its button row.
*
* All decisions are in [state]; this draws them. The texts are read from [languageStore] in
* the composition, so the language picker on the first page switches the wizard itself.
* the composition, so the language picker on the personal page switches the wizard itself.
*
* @param saveFailed whether the last Save could not write the file
* @param onLater the operator does not want to set up now; nothing is written
@@ -183,6 +183,7 @@ private fun PersonalPage(state: SetupWizardState, strings: Strings, languageStor
onSelect = { languageStore.use(it) },
)
Form.text(strings.wizardPersonalName, state.name) { state.name = it }
if (state.nameRefused) Refusal(strings.wizardPersonalNameInvalid)
}
@Composable
@@ -192,6 +193,7 @@ private fun LoginPage(state: SetupWizardState, strings: Strings) {
Form.text(strings.stationCallsign, state.callsign) { state.callsign = it }
if (state.callsignRefused) Refusal(strings.wizardLoginCallsignInvalid)
Form.password(strings.stationPassword, state.password) { state.password = it }
if (state.passwordRefused) Refusal(strings.wizardLoginPasswordInvalid)
}
@Composable
@@ -249,6 +251,10 @@ private fun SummaryPage(state: SetupWizardState, strings: Strings, saveFailed: B
if (state.leavesIntegrationsUntouched) Text(strings.wizardSummaryIntegrationsUnchanged)
Form.section(strings.wizardSummaryPersonal) {
SummaryRow(
strings.guiLanguage,
if (state.language == SYSTEM_LANGUAGE) strings.guiLanguageSystem else state.language.uppercase(),
)
SummaryRow(strings.wizardPersonalName, state.name.ifBlank { strings.wizardSummaryNone })
Form.button(strings.wizardChange) { state.edit(WizardPage.PERSONAL) }
}
@@ -204,10 +204,10 @@ class SetupWizardState(prefs: ChatPreferences) {
}
private val personalComplete: Boolean
get() = name.isNotBlank()
get() = isStorableName(name)
private val loginComplete: Boolean
get() = isPlausibleCallsign(callsign) && password.isNotEmpty()
get() = isPlausibleCallsign(callsign) && isStorablePassword(password)
private val stationComplete: Boolean
get() = isLocator(locator) && bands.isNotEmpty() && category in CATEGORY_NUMBERS &&
@@ -217,10 +217,18 @@ class SetupWizardState(prefs: ChatPreferences) {
val mandatoryComplete: Boolean
get() = personalComplete && loginComplete && stationComplete
/** A chat name that was typed and cannot be sent. An empty or blank field is not an error yet. */
val nameRefused: Boolean
get() = name.isNotBlank() && !isStorableName(name)
/** A callsign that was typed and does not look like one. An empty field is not an error yet. */
val callsignRefused: Boolean
get() = callsign.isNotBlank() && !isPlausibleCallsign(callsign)
/** A password that was typed and cannot be stored or sent. An empty field is not an error yet. */
val passwordRefused: Boolean
get() = password.isNotEmpty() && !isStorablePassword(password)
/** A locator that was typed and is not one. An empty field is not an error yet. */
val locatorRefused: Boolean
get() = locator.isNotBlank() && !isLocator(locator)
@@ -342,6 +350,21 @@ class SetupWizardState(prefs: ChatPreferences) {
/** Whether a text is a six-character Maidenhead locator, in either case. */
fun isLocator(text: String): Boolean = LOCATOR.matches(text.trim())
/**
* Whether a password can be stored in preferences.xml and sent in an ON4KST login frame:
* not blank, free of surrounding whitespace (which the XML reader trims), and free of
* frame delimiters.
*/
fun isStorablePassword(text: String): Boolean =
text.isNotBlank() && text == text.trim() && '|' !in text && '\r' !in text && '\n' !in text
/**
* Whether a chat display name can be sent in an ON4KST frame: not blank and free of
* frame delimiters.
*/
fun isStorableName(text: String): Boolean =
text.isNotBlank() && '|' !in text && '\r' !in text && '\n' !in text
/**
* The chat category the lowest band belongs to: 1 for 50/70 MHz, 2 for 144/432 MHz,
* 3 above. Null without a band. A suggestion only -- the operator confirms it.
@@ -145,9 +145,11 @@ object SetupWizardLauncher {
/*
* A file that cannot be read is not a profile to set up: the wizard would start from
* compiled-in defaults and Save would write them over whatever the file holds. The
* runtime deals with such a file as it always did.
* runtime deals with such a file as it always did. A throwing read counts as failed too.
*/
if (!prefs.readPreferencesFromXmlFile()) return SetupWizardOutcome.NOT_SHOWN
if (!runCatching { prefs.readPreferencesFromXmlFile() }.getOrDefault(false)) {
return SetupWizardOutcome.NOT_SHOWN
}
if (!forced && !LoginConfiguration.isUnconfigured(prefs)) return SetupWizardOutcome.NOT_SHOWN
@@ -1,5 +1,8 @@
package kst4contest.view;
import kst4contest.controller.ActiveOperatorProfile;
import kst4contest.model.OperatorProfile;
import kst4contest.model.OperatorProfileSelection;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -20,4 +23,39 @@ class ApplicationRuntimeLauncherSetupWizardTest {
assertTrue(ApplicationRuntimeLauncher.consumeSetupWizardRequest());
assertFalse(ApplicationRuntimeLauncher.consumeSetupWizardRequest(), "the request must not stick");
}
@Test
void switchGuardAllowsOnlyOneSwitchAtATime() {
try {
assertTrue(ApplicationRuntimeLauncher.beginSwitch());
assertFalse(ApplicationRuntimeLauncher.beginSwitch(),
"a second switch must be refused while one is in progress");
} finally {
ApplicationRuntimeLauncher.endSwitch();
}
try {
assertTrue(ApplicationRuntimeLauncher.beginSwitch(),
"ending the first switch must release the guard");
} finally {
ApplicationRuntimeLauncher.endSwitch();
}
}
@Test
void rerunSetupWizardWhileSwitchInProgressReturnsFalseAndClearsRequest() {
OperatorProfileSelection previous = ActiveOperatorProfile.get();
OperatorProfile profile = new OperatorProfile("wizard-guard-test", "Wizard Guard Test", false, false);
ActiveOperatorProfile.set(new OperatorProfileSelection(profile, "preferences.xml", "worked.db", false));
assertTrue(ApplicationRuntimeLauncher.beginSwitch());
try {
assertFalse(ApplicationRuntimeLauncher.rerunSetupWizard());
assertFalse(ApplicationRuntimeLauncher.consumeSetupWizardRequest(),
"a refused rerun must not leave a wizard request behind");
} finally {
ApplicationRuntimeLauncher.endSwitch();
ApplicationRuntimeLauncher.consumeSetupWizardRequest();
ActiveOperatorProfile.set(previous);
}
}
}
@@ -112,4 +112,16 @@ class SetupWizardContentScreenTest {
onNodeWithText("The settings could not be written. Nothing was changed.").assertExists()
}
@Test
fun theSummaryShowsTheChosenLanguage() = runComposeUiTest {
val state = SetupWizardState(configuredProfile().apply { setGuiOptions_language("de") })
state.chooseLoginSource(WizardSource.KEEP)
state.chooseStationSource(WizardSource.KEEP)
state.next()
state.next()
show(state)
onNodeWithText("DE").assertExists()
}
}
@@ -12,7 +12,7 @@ import java.nio.file.Path
import java.nio.file.StandardCopyOption
/** When the wizard opens at all, decided without a window. */
class SetupWizardLauncherTest(@TempDir private val directory: Path) {
class SetupWizardLauncherTest(@param:TempDir private val directory: Path) {
private fun templateFile(): Path {
val file = directory.resolve("preferences.xml")
@@ -81,6 +81,21 @@ class SetupWizardLauncherTest(@TempDir private val directory: Path) {
assertArrayEquals(before, Files.readAllBytes(garbage), "the unreadable file was touched")
}
@Test
fun aFileTheReaderThrowsOnNeverOpensTheWizard() {
val file = templateFile()
val original = Files.readString(file)
assertTrue(original.contains("<ChatCategory>2</ChatCategory>"))
Files.writeString(file, original.replace("<ChatCategory>2</ChatCategory>", "<ChatCategory>2.0</ChatCategory>"))
val before = Files.readAllBytes(file)
assertEquals(
SetupWizardOutcome.NOT_SHOWN,
SetupWizardLauncher.run(ChatPreferences(file), forced = true, neverShown),
)
assertArrayEquals(before, Files.readAllBytes(file), "the unreadable file was touched")
}
@Test
fun aFailedSaveKeepsTheWizardOpen() {
val source = ChatPreferences(templateFile()).apply { assertTrue(readPreferencesFromXmlFile()) }
@@ -351,4 +351,67 @@ class SetupWizardStateTest {
assertTrue(state.mandatoryComplete)
assertFalse(state.pages.contains(WizardPage.SITE))
}
@Test
fun storablePasswordsAndNamesRejectDelimitersAndOuterWhitespace() {
listOf("secret", "se cret").forEach {
assertTrue(SetupWizardState.isStorablePassword(it), "'$it' must be accepted")
}
listOf("", " ", " secret", "secret ", "se|cret", "se\ncret", "se\rcret").forEach {
assertFalse(SetupWizardState.isStorablePassword(it), "'$it' must be refused")
}
listOf("Philipp", "Philipp W").forEach {
assertTrue(SetupWizardState.isStorableName(it), "'$it' must be accepted")
}
listOf("", " ", "Phil|ipp", "Phil\nipp", "Phil\ripp").forEach {
assertFalse(SetupWizardState.isStorableName(it), "'$it' must be refused")
}
}
@Test
fun aRefusedPasswordOrNameIsReportedAndBlocksCompletion() {
val state = SetupWizardState(freshInstallation())
assertFalse(state.nameRefused)
state.name = " "
assertFalse(state.nameRefused)
assertFalse(state.canGoNext)
state.name = "Phil|ipp"
assertTrue(state.nameRefused)
assertFalse(state.canGoNext)
state.fillFirstInstallation()
state.back()
assertEquals(WizardPage.LOGIN, state.page)
assertFalse(state.passwordRefused)
listOf(" secret", "secret ", " ", "se|cret", "se\ncret").forEach { refused ->
state.password = refused
assertTrue(state.passwordRefused, "'$refused' must set passwordRefused")
assertFalse(state.canGoNext, "'$refused' must block Next on login")
assertFalse(state.mandatoryComplete, "'$refused' must block mandatoryComplete")
}
state.password = ""
assertFalse(state.passwordRefused)
assertFalse(state.canGoNext)
state.password = "se cret"
assertFalse(state.passwordRefused)
assertTrue(state.canGoNext)
assertTrue(state.mandatoryComplete)
}
@Test
fun keepingALoginWithAStorablePasswordSkipsTheLoginPage() {
val state = SetupWizardState(configuredProfile())
state.chooseLoginSource(WizardSource.KEEP)
state.chooseStationSource(WizardSource.KEEP)
state.next()
assertFalse(state.pages.contains(WizardPage.LOGIN))
assertEquals(WizardPage.PERSONAL, state.page)
state.next()
assertEquals(WizardPage.SUMMARY, state.page)
}
}
@@ -16,7 +16,7 @@ import java.nio.file.StandardCopyOption
* What the wizard does to a real preferences file: nothing until Save, exactly the answers
* afterwards, and every setting it never asked about left as it was.
*/
class SetupWizardWriteTest(@TempDir private val directory: Path) {
class SetupWizardWriteTest(@param:TempDir private val directory: Path) {
/** A copy of the bundled template, read the way the runtime reads a profile. */
private fun templateFile(): Path {