Skip to content

Commit 9f146f9

Browse files
committed
api/restore: add unit test and count security chip events
1 parent a92c32a commit 9f146f9

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,50 @@ pub async fn from_mnemonic(
168168
unlock::unlock_bip39(hal).await;
169169
Ok(Response::Success(pb::Success {}))
170170
}
171+
172+
#[cfg(test)]
173+
mod tests {
174+
use super::*;
175+
176+
use crate::bb02_async::block_on;
177+
use crate::hal::testing::TestingHal;
178+
use bitbox02::testing::mock_memory;
179+
use bitbox02::{keystore, memory};
180+
181+
use alloc::boxed::Box;
182+
183+
#[test]
184+
fn test_from_mnemonic() {
185+
mock_memory();
186+
keystore::lock();
187+
let mut counter = 0u32;
188+
let mut mock_hal = TestingHal::new();
189+
mock_hal.ui.set_enter_string(Box::new(|params| {
190+
counter += 1;
191+
match counter {
192+
1 => assert_eq!(params.title, "Set password"),
193+
2 => assert_eq!(params.title, "Repeat password"),
194+
_ => panic!("too many user inputs"),
195+
}
196+
Ok("password".into())
197+
}));
198+
199+
bitbox02::securechip::fake_event_counter_reset();
200+
assert_eq!(
201+
block_on(from_mnemonic(
202+
&mut mock_hal,
203+
&pb::RestoreFromMnemonicRequest {
204+
timestamp: 0,
205+
timezone_offset: 0,
206+
}
207+
)),
208+
Ok(Response::Success(pb::Success {}))
209+
);
210+
assert_eq!(bitbox02::securechip::fake_event_counter(), 19);
211+
drop(mock_hal); // to remove mutable borrow of counter
212+
assert_eq!(counter, 2);
213+
assert!(!keystore::is_locked());
214+
assert!(memory::is_initialized());
215+
assert!(keystore::copy_seed().unwrap().len() == 32);
216+
}
217+
}

0 commit comments

Comments
 (0)