From 6b055260ebdb630ff02cf17a841499422d635d79 Mon Sep 17 00:00:00 2001 From: Willem Vanhulle Date: Wed, 21 Oct 2026 12:37:25 +0200 Subject: [PATCH] feat: show the demo passphrase and say when an answer was wrong --- src/boot_view/panel_render_tests.rs | 44 ++++++++++++++++++++ src/boot_view/screen_chrome.rs | 24 +++++++---- src/console_session/ask_password.rs | 2 + src/demo_script/scripted_boot.rs | 63 +++++++++++++++++++++++++++++ src/screen_settings/color_scheme.rs | 2 + 5 files changed, 128 insertions(+), 7 deletions(-) diff --git a/src/boot_view/panel_render_tests.rs b/src/boot_view/panel_render_tests.rs index b3aa749..69032a1 100644 --- a/src/boot_view/panel_render_tests.rs +++ b/src/boot_view/panel_render_tests.rs @@ -78,8 +78,10 @@ fn password_prompt_floats_over_a_dimmed_view() { 24, Some(PasswordPrompt { message: CRYPTSETUP_MESSAGE, + hint: None, input: "***".to_owned(), sent: false, + rejected: false, }), ); let lines = screen_lines(&buffer); @@ -121,8 +123,10 @@ fn answered_password_prompt_shows_it_is_checking() { 24, Some(PasswordPrompt { message: CRYPTSETUP_MESSAGE, + hint: None, input: String::new(), sent: true, + rejected: false, }), )); assert!( @@ -141,8 +145,10 @@ fn long_password_messages_wrap_inside_narrow_screens() { 24, Some(PasswordPrompt { message: CRYPTSETUP_MESSAGE, + hint: None, input: String::new(), sent: false, + rejected: false, }), )); let screen = lines.join("\n"); @@ -153,6 +159,44 @@ fn long_password_messages_wrap_inside_narrow_screens() { assert!(lines.iter().any(|line| line.contains("> _")), "{screen}"); } +#[test] +fn a_rejected_passphrase_is_called_out_under_the_hint() { + let buffer = render_prompted( + &boot_in_progress(), + 12_400 * MS, + 110, + 24, + Some(PasswordPrompt { + message: CRYPTSETUP_MESSAGE, + hint: Some("This demo unlocks with the passphrase \"demo\""), + input: String::new(), + sent: false, + rejected: true, + }), + ); + let lines = screen_lines(&buffer); + let screen = lines.join("\n"); + let message_row = lines + .iter() + .position(|line| line.contains(CRYPTSETUP_MESSAGE)) + .unwrap_or_else(|| panic!("{screen}")); + assert!( + lines[message_row + 1].contains("โ”‚ This demo unlocks with the passphrase \"demo\""), + "{screen}" + ); + assert!( + lines[message_row + 3].contains("โ”‚ Wrong passphrase, try again"), + "{screen}" + ); + assert!(lines[message_row + 4].contains("โ”‚ > _"), "{screen}"); + let rejection_row = u16::try_from(message_row + 3).expect("row fits the screen"); + let warning = (0..buffer.area.width) + .map(|column| &buffer[(column, rejection_row)]) + .find(|cell| cell.symbol() == "W") + .unwrap_or_else(|| panic!("{screen}")); + assert_eq!(warning.fg, Colors::default().password.rejected, "{screen}"); +} + #[test] fn sub_second_runs_stay_off_the_timeline() { let mut state = boot_in_progress(); diff --git a/src/boot_view/screen_chrome.rs b/src/boot_view/screen_chrome.rs index a9bcfa5..f70e7a1 100644 --- a/src/boot_view/screen_chrome.rs +++ b/src/boot_view/screen_chrome.rs @@ -19,6 +19,7 @@ use crate::{ pub const FOOTER_HEIGHT: u16 = 2; const SEPARATOR: &str = " ยท "; +const REJECTED_PASSPHRASE: &str = "Wrong passphrase, try again"; pub struct Header<'a> { pub origin: &'a BootOrigin, @@ -94,8 +95,10 @@ impl Widget for Footer<'_> { pub struct PasswordPrompt<'a> { pub message: &'a str, + pub hint: Option<&'a str>, pub input: String, pub sent: bool, + pub rejected: bool, } pub struct PasswordPopup<'a> { @@ -119,13 +122,20 @@ impl PasswordPopup<'_> { } fn paragraph(&self) -> Paragraph<'_> { - Paragraph::new(vec![ - Line::from(self.asking.message.fg(self.colors.message).bold()), - Line::default(), - self.input(), - ]) - .wrap(Wrap { trim: false }) - .block( + let message = Line::from(self.asking.message.fg(self.colors.message).bold()); + let hint = self + .asking + .hint + .map(|hint| Line::from(hint.fg(self.colors.hint))); + let rejection = (self.asking.rejected && !self.asking.sent) + .then(|| Line::from(REJECTED_PASSPHRASE.fg(self.colors.rejected).bold())); + let lines: Vec = once(message) + .chain(hint) + .chain(once(Line::default())) + .chain(rejection) + .chain(once(self.input())) + .collect(); + Paragraph::new(lines).wrap(Wrap { trim: false }).block( Block::bordered() .border_style(self.colors.frame) .padding(Padding::horizontal(2)) diff --git a/src/console_session/ask_password.rs b/src/console_session/ask_password.rs index 640716e..b5e8ff6 100644 --- a/src/console_session/ask_password.rs +++ b/src/console_session/ask_password.rs @@ -211,8 +211,10 @@ impl PasswordEntry { }; PasswordPrompt { message: &self.request.message, + hint: None, input, sent: self.sent, + rejected: false, } } diff --git a/src/demo_script/scripted_boot.rs b/src/demo_script/scripted_boot.rs index 2ba35be..071f696 100644 --- a/src/demo_script/scripted_boot.rs +++ b/src/demo_script/scripted_boot.rs @@ -28,6 +28,7 @@ use crate::{ const FINAL_FRAME: Duration = Duration::from_secs(3); pub const DEMO_PASSPHRASE: &str = "demo"; +const DEMO_PASSPHRASE_HINT: &str = "This demo unlocks with the passphrase \"demo\""; pub struct Step { unit: &'static str, @@ -211,11 +212,18 @@ impl Script { self.answer.is_some_and(|answer| now < answer.checked_at) } + fn was_rejected(&self, now: Duration) -> bool { + self.answer + .is_some_and(|answer| !answer.unlocks && now >= answer.checked_at) + } + fn prompt(&self, typed: &str, now: Duration) -> Option> { Some(PasswordPrompt { message: self.asking()?.asks?, + hint: Some(DEMO_PASSPHRASE_HINT), input: passphrase_input::masked(typed), sent: self.is_checking(now), + rejected: self.was_rejected(now), }) } @@ -392,3 +400,58 @@ pub fn play(terminal: &mut DefaultTerminal, scenario: &Scenario) -> io::Result<( .build()? .block_on(play_scenario(terminal, scenario)) } + +#[cfg(test)] +mod tests { + use super::*; + + const KEY_DERIVATION: Duration = Duration::from_millis(500); + const UNLOCK_ONLY: &[Step] = &[unlocking( + "systemd-cryptsetup@root.service", + &[], + "Please enter passphrase for disk root:", + KEY_DERIVATION, + )]; + + struct AskingDemo { + script: Script, + state: BootState, + } + + fn asking_demo() -> AskingDemo { + let mut state = BootState::new(Phase::Initrd); + let mut script = Script::begin(UNLOCK_ONLY, |_| "", 1, &mut state, Duration::ZERO); + script.advance(&mut state, Duration::ZERO); + AskingDemo { script, state } + } + + #[test] + fn the_hint_names_the_demo_passphrase() { + assert!(DEMO_PASSPHRASE_HINT.contains(&format!("\"{DEMO_PASSPHRASE}\""))); + } + + #[test] + fn a_wrong_answer_is_checked_then_called_out() { + let AskingDemo { mut script, .. } = asking_demo(); + let asked = script.prompt("", Duration::ZERO).expect("asking"); + assert_eq!(asked.hint, Some(DEMO_PASSPHRASE_HINT)); + assert!(!asked.rejected); + script.answer("wrong", Duration::ZERO); + let checking = script.prompt("", KEY_DERIVATION / 2).expect("asking"); + assert!(checking.sent && !checking.rejected); + let rejected = script.prompt("", KEY_DERIVATION).expect("asking again"); + assert!(rejected.rejected && !rejected.sent); + } + + #[test] + fn the_right_answer_unlocks_and_closes_the_prompt() { + let AskingDemo { + mut script, + mut state, + } = asking_demo(); + script.answer(DEMO_PASSPHRASE, Duration::ZERO); + script.advance(&mut state, KEY_DERIVATION); + assert!(script.is_done()); + assert!(script.prompt("", KEY_DERIVATION).is_none()); + } +} diff --git a/src/screen_settings/color_scheme.rs b/src/screen_settings/color_scheme.rs index 226d102..0fdbcfe 100644 --- a/src/screen_settings/color_scheme.rs +++ b/src/screen_settings/color_scheme.rs @@ -153,6 +153,7 @@ pub struct PasswordColors { pub message: Color, pub hint: Color, pub checking: Color, + pub rejected: Color, pub backdrop: Color, } @@ -163,6 +164,7 @@ impl Default for PasswordColors { message: Color::White, hint: Color::DarkGray, checking: Color::Gray, + rejected: Color::LightRed, backdrop: Color::DarkGray, } }