Skip to content

Commit 0ea11ee

Browse files
committed
api/backup: add unit test for creating a new backup when initialzed
1 parent 4132672 commit 0ea11ee

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
@@ -179,6 +179,7 @@ mod tests {
179179

180180
let mut mock_hal = TestingHal::new();
181181
mock_hal.sd.inserted = Some(true);
182+
bitbox02::securechip::fake_event_counter_reset();
182183
assert_eq!(
183184
block_on(create(
184185
&mut mock_hal,
@@ -189,6 +190,7 @@ mod tests {
189190
)),
190191
Ok(Response::Success(pb::Success {}))
191192
);
193+
assert_eq!(bitbox02::securechip::fake_event_counter(), 1);
192194
assert_eq!(EXPECTED_TIMESTMAP, bitbox02::memory::get_seed_birthdate());
193195
assert_eq!(
194196
mock_hal.ui.screens,
@@ -216,6 +218,68 @@ mod tests {
216218
);
217219
}
218220

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