feat: show the demo passphrase and say when an answer was wrong

This commit is contained in:
Willem Vanhulle committed 2026-10-21 12:49:34 +02:00
1 parent d0063c2371
commit 6b055260eb
5 files changed
+128 -7

No files matched your search

+44
View File
@@ -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();
+17 -7
View File
@@ -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<Line> = 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))
+2
View File
@@ -211,8 +211,10 @@ impl PasswordEntry {
};
PasswordPrompt {
message: &self.request.message,
hint: None,
input,
sent: self.sent,
rejected: false,
}
}
+63
View File
@@ -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<PasswordPrompt<'static>> {
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());
}
}
+2
View File
@@ -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,
}
}