Skip to content

Commit 93771f6

Browse files
committed
api/backup: add unit test for creating a new backup when initialzed
1 parent c5253cd commit 93771f6

File tree

3 files changed

+77
-3
lines changed

3 files changed

+77
-3
lines changed

src/rust/bitbox02-rust/src/hww/api/backup.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ mod tests {
186186

187187
let mut mock_hal = TestingHal::new();
188188
mock_hal.sd.inserted = Some(true);
189+
bitbox02::securechip::fake_event_counter_reset();
189190
assert_eq!(
190191
block_on(create(
191192
&mut mock_hal,
@@ -196,6 +197,7 @@ mod tests {
196197
)),
197198
Ok(Response::Success(pb::Success {}))
198199
);
200+
assert_eq!(bitbox02::securechip::fake_event_counter(), 1);
199201
assert_eq!(EXPECTED_TIMESTMAP, bitbox02::memory::get_seed_birthdate());
200202
assert_eq!(
201203
mock_hal.ui.screens,
@@ -223,6 +225,68 @@ mod tests {
223225
);
224226
}
225227

228+
/// Test backup creation on a initialized keystore. The sdcard does not contain the backup yet.
229+
#[test]
230+
pub fn test_create_initialized_new() {
231+
const TIMESTMAP: u32 = 1601281809;
232+
233+
mock_memory();
234+
235+
let seed = hex::decode("cb33c20cea62a5c277527e2002da82e6e2b37450a755143a540a54cea8da9044")
236+
.unwrap();
237+
bitbox02::keystore::encrypt_and_store_seed(&seed, "password").unwrap();
238+
bitbox02::memory::set_initialized().unwrap();
239+
240+
let mut password_entered: bool = false;
241+
242+
let mut mock_hal = TestingHal::new();
243+
mock_hal.sd.inserted = Some(true);
244+
mock_hal.ui.set_enter_string(Box::new(|_params| {
245+
password_entered = true;
246+
Ok("password".into())
247+
}));
248+
bitbox02::securechip::fake_event_counter_reset();
249+
assert_eq!(
250+
block_on(create(
251+
&mut mock_hal,
252+
&pb::CreateBackupRequest {
253+
timestamp: TIMESTMAP,
254+
timezone_offset: 18000,
255+
}
256+
)),
257+
Ok(Response::Success(pb::Success {}))
258+
);
259+
assert_eq!(bitbox02::securechip::fake_event_counter(), 6);
260+
assert_eq!(
261+
mock_hal.ui.screens,
262+
vec![
263+
Screen::Confirm {
264+
title: "Is today?".into(),
265+
body: "Mon 2020-09-28".into(),
266+
longtouch: false
267+
},
268+
Screen::Status {
269+
title: "Backup created".into(),
270+
success: true
271+
}
272+
]
273+
);
274+
275+
mock_hal.ui.remove_enter_string(); // no more password entry needed
276+
assert_eq!(
277+
block_on(check(
278+
&mut mock_hal,
279+
&pb::CheckBackupRequest { silent: true }
280+
)),
281+
Ok(Response::CheckBackup(pb::CheckBackupResponse {
282+
id: backup::id(&seed),
283+
}))
284+
);
285+
286+
drop(mock_hal); // to remove mutable borrow of `password_entered`
287+
assert!(password_entered);
288+
}
289+
226290
/// Use backup file fixtures generated using firmware v9.12.0 and perform tests on it. This
227291
/// should catch regressions when changing backup loading/verification in the firmware code.
228292
#[test]

src/rust/bitbox02-rust/src/hww/api/show_mnemonic.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,13 @@ mod tests {
139139

140140
bitbox02::memory::set_initialized().unwrap();
141141

142+
let mut password_entered: bool = false;
143+
142144
let mut mock_hal = TestingHal::new();
143-
mock_hal
144-
.ui
145-
.set_enter_string(Box::new(|_params| Ok("password".into())));
145+
mock_hal.ui.set_enter_string(Box::new(|_params| {
146+
password_entered = true;
147+
Ok("password".into())
148+
}));
146149

147150
bitbox02::securechip::fake_event_counter_reset();
148151
assert_eq!(
@@ -173,6 +176,9 @@ mod tests {
173176
},
174177
]
175178
);
179+
180+
drop(mock_hal); // to remove mutable borrow of `password_entered`
181+
assert!(password_entered);
176182
}
177183

178184
/// When initialized, a password check is prompted before displaying the mnemonic.

src/rust/bitbox02-rust/src/workflow/testing.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,4 +206,8 @@ impl<'a> TestingWorkflows<'a> {
206206
pub fn set_enter_string(&mut self, cb: EnterStringCb<'a>) {
207207
self._enter_string = Some(cb);
208208
}
209+
210+
pub fn remove_enter_string(&mut self) {
211+
self._enter_string = None;
212+
}
209213
}

0 commit comments

Comments
 (0)