Skip to content

Commit a5c0a6b

Browse files
committed
keystore: port test_keystore_create_and_unlock_twice to Rust
1 parent c023f4f commit a5c0a6b

File tree

2 files changed

+20
-30
lines changed

2 files changed

+20
-30
lines changed

src/rust/bitbox02/src/keystore.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,26 @@ mod tests {
509509
assert!(bip39_mnemonic_from_seed(b"foo").is_err());
510510
}
511511

512+
// This tests that you can create a keystore, unlock it, and then do this again. This is an
513+
// expected workflow for when the wallet setup process is restarted after seeding and unlocking,
514+
// but before creating a backup, in which case a new seed is created.
515+
#[test]
516+
fn test_create_and_unlock_twice() {
517+
mock_memory();
518+
lock();
519+
520+
let seed = hex::decode("cb33c20cea62a5c277527e2002da82e6e2b37450a755143a540a54cea8da9044")
521+
.unwrap();
522+
let seed2 = hex::decode("c28135734876aff9ccf4f1d60df8d19a0a38fd02085883f65fc608eb769a635d")
523+
.unwrap();
524+
assert!(encrypt_and_store_seed(&seed, "password").is_ok());
525+
assert!(unlock("password").is_ok());
526+
// Create new (different) seed.
527+
assert!(encrypt_and_store_seed(&seed2, "password").is_ok());
528+
assert!(unlock("password").is_ok());
529+
assert_eq!(copy_seed().unwrap().as_slice(), &seed2);
530+
}
531+
512532
// Functional test to store seeds, unlock, retrieve seed.
513533
#[test]
514534
fn test_seeds() {

test/unit-test/test_keystore.c

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,6 @@ static uint8_t _mock_seed[32] = {
4545
0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
4646
};
4747

48-
static uint8_t _mock_seed_2[32] = {
49-
0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
50-
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
51-
};
52-
5348
static uint8_t _mock_bip39_seed[64] = {
5449
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
5550
0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
@@ -305,30 +300,6 @@ static void _test_keystore_encrypt_and_store_seed(void** state)
305300
assert_int_equal(keystore_encrypt_and_store_seed(_mock_seed, 32, PASSWORD), KEYSTORE_OK);
306301
}
307302

308-
// this tests that you can create a keystore, unlock it, and then do this again. This is an expected
309-
// workflow for when the wallet setup process is restarted after seeding and unlocking, but before
310-
// creating a backup, in which case a new seed is created.
311-
static void _test_keystore_create_and_unlock_twice(void** state)
312-
{
313-
_expect_encrypt_and_store_seed();
314-
assert_int_equal(keystore_encrypt_and_store_seed(_mock_seed, 32, PASSWORD), KEYSTORE_OK);
315-
316-
uint8_t remaining_attempts;
317-
_smarteeprom_reset();
318-
319-
will_return(__wrap_memory_is_seeded, true);
320-
_expect_retain_seed();
321-
assert_int_equal(KEYSTORE_OK, keystore_unlock(PASSWORD, &remaining_attempts, NULL));
322-
323-
// Create new (different) seed.
324-
_expect_encrypt_and_store_seed();
325-
assert_int_equal(keystore_encrypt_and_store_seed(_mock_seed_2, 32, PASSWORD), KEYSTORE_OK);
326-
327-
will_return(__wrap_memory_is_seeded, true);
328-
_expect_retain_seed();
329-
assert_int_equal(KEYSTORE_OK, keystore_unlock(PASSWORD, &remaining_attempts, NULL));
330-
}
331-
332303
static void _expect_seeded(bool seeded)
333304
{
334305
uint8_t seed[KEYSTORE_MAX_SEED_LENGTH];
@@ -636,7 +607,6 @@ int main(void)
636607
cmocka_unit_test(_test_keystore_secp256k1_nonce_commit),
637608
cmocka_unit_test(_test_keystore_secp256k1_sign),
638609
cmocka_unit_test(_test_keystore_encrypt_and_store_seed),
639-
cmocka_unit_test(_test_keystore_create_and_unlock_twice),
640610
cmocka_unit_test(_test_keystore_unlock),
641611
cmocka_unit_test(_test_keystore_unlock_bip39),
642612
cmocka_unit_test(_test_keystore_lock),

0 commit comments

Comments
 (0)