Skip to content

Commit 718cfda

Browse files
committed
keystore: port _test_keystore_lock to Rust
1 parent 0e535e6 commit 718cfda

File tree

2 files changed

+16
-26
lines changed

2 files changed

+16
-26
lines changed

src/rust/bitbox02/src/keystore.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,22 @@ mod tests {
568568
assert!(bip39_mnemonic_from_seed(b"foo").is_err());
569569
}
570570

571+
#[test]
572+
fn test_lock() {
573+
lock();
574+
assert!(is_locked());
575+
576+
let seed = hex::decode("cb33c20cea62a5c277527e2002da82e6e2b37450a755143a540a54cea8da9044")
577+
.unwrap();
578+
assert!(encrypt_and_store_seed(&seed, "password").is_ok());
579+
assert!(unlock("password").is_ok());
580+
assert!(is_locked()); // still locked, it is only unlocked after unlock_bip39.
581+
assert!(unlock_bip39("foo").is_ok());
582+
assert!(!is_locked());
583+
lock();
584+
assert!(is_locked());
585+
}
586+
571587
#[test]
572588
fn test_unlock() {
573589
mock_memory();
@@ -642,7 +658,6 @@ mod tests {
642658

643659
assert!(encrypt_and_store_seed(&seed, "password").is_ok());
644660
assert!(unlock("password").is_ok());
645-
assert!(is_locked()); // still locked, it is only unlocked after unlock_bip39.
646661
assert!(unlock_bip39("foo").is_ok());
647662

648663
// Check that the retained bip39 seed was encrypted with the expected encryption key.

test/unit-test/test_keystore.c

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,6 @@ static uint8_t _salt_root[KEYSTORE_MAX_SEED_LENGTH] = {
4040
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
4141
};
4242

43-
static uint8_t _mock_seed[32] = {
44-
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
45-
0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
46-
};
47-
48-
static uint8_t _mock_bip39_seed[64] = {
49-
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
50-
0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
51-
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
52-
0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
53-
};
54-
5543
// Same as Python:
5644
// import hmac, hashlib; hmac.digest(b"unit-test", b"password", hashlib.sha256).hex()
5745
// See also: securechip_mock.c
@@ -109,18 +97,6 @@ static void _expect_encrypt_and_store_seed(void)
10997
will_return(__wrap_memory_is_initialized, false);
11098
}
11199

112-
static void _test_keystore_lock(void** state)
113-
{
114-
_mock_unlocked(NULL, 0, NULL);
115-
assert_true(keystore_is_locked());
116-
_mock_unlocked(_mock_seed, sizeof(_mock_seed), NULL);
117-
assert_true(keystore_is_locked());
118-
_mock_unlocked(_mock_seed, sizeof(_mock_seed), _mock_bip39_seed);
119-
assert_false(keystore_is_locked());
120-
keystore_lock();
121-
assert_true(keystore_is_locked());
122-
}
123-
124100
static void _test_keystore_create_and_store_seed(void** state)
125101
{
126102
const uint8_t seed_random[32] =
@@ -282,7 +258,6 @@ int main(void)
282258
mock_memory_set_salt_root(_salt_root);
283259

284260
const struct CMUnitTest tests[] = {
285-
cmocka_unit_test(_test_keystore_lock),
286261
cmocka_unit_test(_test_keystore_create_and_store_seed),
287262
cmocka_unit_test(_test_secp256k1_schnorr_sign),
288263
cmocka_unit_test(_test_keystore_secp256k1_schnorr_sign),

0 commit comments

Comments
 (0)