api/backup: add unit test for creating a new backup when initialzed
What changed, and why it matters
This commit only adds and updates unit tests for the BitBox02 firmware. It does not change any production code, cryptographic operations, or user-facing behavior. There is no security issue here.
No action required; this is a test-only change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit adds a new test test_create_initialized_new in backup.rs to verify creating a backup on an already-initialized keystore, and updates an existing test in show_mnemonic.rs to assert that a password prompt was actually exercised. It also adds a remove_enter_string helper to the test mock. No runtime firmware code is modified.
Changed components
src/rust/bitbox02-rust/src/hww/api/backup.rssrc/rust/bitbox02-rust/src/hww/api/show_mnemonic.rssrc/rust/bitbox02-rust/src/workflow/testing.rsInspect captured patch +77 / −3
diff --git a/src/rust/bitbox02-rust/src/hww/api/backup.rs b/src/rust/bitbox02-rust/src/hww/api/backup.rs
index 2ba9859..1e4f7c0 100644
--- a/src/rust/bitbox02-rust/src/hww/api/backup.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/backup.rs
@@ -186,6 +186,7 @@ mod tests {
let mut mock_hal = TestingHal::new();
mock_hal.sd.inserted = Some(true);
+ bitbox02::securechip::fake_event_counter_reset();
assert_eq!(
block_on(create(
&mut mock_hal,
@@ -196,6 +197,7 @@ mod tests {
)),
Ok(Response::Success(pb::Success {}))
);
+ assert_eq!(bitbox02::securechip::fake_event_counter(), 1);
assert_eq!(EXPECTED_TIMESTMAP, bitbox02::memory::get_seed_birthdate());
assert_eq!(
mock_hal.ui.screens,
@@ -223,6 +225,68 @@ mod tests {
);
}
+ /// Test backup creation on a initialized keystore. The sdcard does not contain the backup yet.
+ #[test]
+ pub fn test_create_initialized_new() {
+ const TIMESTMAP: u32 = 1601281809;
+
+ mock_memory();
+
+ let seed = hex::decode("cb33c20cea62a5c277527e2002da82e6e2b37450a755143a540a54cea8da9044")
+ .unwrap();
+ bitbox02::keystore::encrypt_and_store_seed(&seed, "password").unwrap();
+ bitbox02::memory::set_initialized().unwrap();
+
+ let mut password_entered: bool = false;
+
+ let mut mock_hal = TestingHal::new();
+ mock_hal.sd.inserted = Some(true);
+ mock_hal.ui.set_enter_string(Box::new(|_params| {
+ password_entered = true;
+ Ok("password".into())
+ }));
+ bitbox02::securechip::fake_event_counter_reset();
+ assert_eq!(
+ block_on(create(
+ &mut mock_hal,
+ &pb::CreateBackupRequest {
+ timestamp: TIMESTMAP,
+ timezone_offset: 18000,
+ }
+ )),
+ Ok(Response::Success(pb::Success {}))
+ );
+ assert_eq!(bitbox02::securechip::fake_event_counter(), 6);
+ assert_eq!(
+ mock_hal.ui.screens,
+ vec![
+ Screen::Confirm {
+ title: "Is today?".into(),
+ body: "Mon 2020-09-28".into(),
+ longtouch: false
+ },
+ Screen::Status {
+ title: "Backup created".into(),
+ success: true
+ }
+ ]
+ );
+
+ mock_hal.ui.remove_enter_string(); // no more password entry needed
+ assert_eq!(
+ block_on(check(
+ &mut mock_hal,
+ &pb::CheckBackupRequest { silent: true }
+ )),
+ Ok(Response::CheckBackup(pb::CheckBackupResponse {
+ id: backup::id(&seed),
+ }))
+ );
+
+ drop(mock_hal); // to remove mutable borrow of `password_entered`
+ assert!(password_entered);
+ }
+
/// Use backup file fixtures generated using firmware v9.12.0 and perform tests on it. This
/// should catch regressions when changing backup loading/verification in the firmware code.
#[test]
diff --git a/src/rust/bitbox02-rust/src/hww/api/show_mnemonic.rs b/src/rust/bitbox02-rust/src/hww/api/show_mnemonic.rs
index b44e112..f13538d 100644
--- a/src/rust/bitbox02-rust/src/hww/api/show_mnemonic.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/show_mnemonic.rs
@@ -139,10 +139,13 @@ mod tests {
bitbox02::memory::set_initialized().unwrap();
+ let mut password_entered: bool = false;
+
let mut mock_hal = TestingHal::new();
- mock_hal
- .ui
- .set_enter_string(Box::new(|_params| Ok("password".into())));
+ mock_hal.ui.set_enter_string(Box::new(|_params| {
+ password_entered = true;
+ Ok("password".into())
+ }));
bitbox02::securechip::fake_event_counter_reset();
assert_eq!(
@@ -173,6 +176,9 @@ mod tests {
},
]
);
+
+ drop(mock_hal); // to remove mutable borrow of `password_entered`
+ assert!(password_entered);
}
/// When initialized, a password check is prompted before displaying the mnemonic.
diff --git a/src/rust/bitbox02-rust/src/workflow/testing.rs b/src/rust/bitbox02-rust/src/workflow/testing.rs
index 1e998b7..d3014de 100644
--- a/src/rust/bitbox02-rust/src/workflow/testing.rs
+++ b/src/rust/bitbox02-rust/src/workflow/testing.rs
@@ -206,4 +206,8 @@ impl<'a> TestingWorkflows<'a> {
pub fn set_enter_string(&mut self, cb: EnterStringCb<'a>) {
self._enter_string = Some(cb);
}
+
+ pub fn remove_enter_string(&mut self) {
+ self._enter_string = None;
+ }
}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.