Skip to content

Commit d96c98c

Browse files
committed
workflow/unlock: add unit test
1 parent 4976c71 commit d96c98c

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,55 @@ pub async fn unlock(hal: &mut impl crate::hal::Hal) -> Result<(), ()> {
165165
unlock_bip39(hal, &bitbox02::keystore::copy_seed()?).await;
166166
Ok(())
167167
}
168+
169+
#[cfg(test)]
170+
mod tests {
171+
use super::*;
172+
173+
use crate::hal::testing::TestingHal;
174+
use crate::workflow::testing::Screen;
175+
use alloc::boxed::Box;
176+
use bitbox02::testing::{mock_memory, mock_unlocked, mock_unlocked_using_mnemonic};
177+
use util::bb02_async::block_on;
178+
179+
#[test]
180+
fn test_unlock_success() {
181+
mock_memory();
182+
183+
// Set up an initialized wallet with password
184+
bitbox02::keystore::encrypt_and_store_seed(
185+
hex::decode("c7940c13479b8d9a6498f4e50d5a42e0d617bc8e8ac9f2b8cecf97e94c2b035c")
186+
.unwrap()
187+
.as_slice(),
188+
"password",
189+
)
190+
.unwrap();
191+
192+
bitbox02::memory::set_initialized().unwrap();
193+
194+
// Lock the keystore to simulate the normal locked state
195+
bitbox02::keystore::lock();
196+
197+
let mut password_entered = false;
198+
199+
let mut mock_hal = TestingHal::new();
200+
mock_hal.ui.set_enter_string(Box::new(|_params| {
201+
password_entered = true;
202+
Ok("password".into())
203+
}));
204+
bitbox02::securechip::fake_event_counter_reset();
205+
assert_eq!(block_on(unlock(&mut mock_hal)), Ok(()));
206+
assert_eq!(bitbox02::securechip::fake_event_counter(), 8);
207+
208+
assert!(!bitbox02::keystore::is_locked());
209+
210+
assert_eq!(
211+
bitbox02::keystore::copy_bip39_seed().unwrap().as_slice(),
212+
hex::decode("cff4b263e5b0eb299e5fd35fcd09988f6b14e5b464f8d18fb84b152f889dd2a30550f4c2b346cae825ffedd4a87fc63fc12a9433de5125b6c7fdbc5eab0c590b")
213+
.unwrap(),
214+
);
215+
216+
drop(mock_hal); // to remove mutable borrow of `password_entered`
217+
assert!(password_entered);
218+
}
219+
}

0 commit comments

Comments
 (0)