Skip to content

Commit 31d899c

Browse files
committed
keystore: port functional seed test to Rust
Maintaining C tests is hard.
1 parent bccecb5 commit 31d899c

File tree

2 files changed

+42
-39
lines changed

2 files changed

+42
-39
lines changed

src/rust/bitbox02/src/keystore.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ pub fn secp256k1_schnorr_sign(
346346
#[cfg(test)]
347347
mod tests {
348348
use super::*;
349-
use crate::testing::{mock_unlocked, mock_unlocked_using_mnemonic};
349+
use crate::testing::{mock_memory, mock_unlocked, mock_unlocked_using_mnemonic};
350350
use util::bip32::HARDENED;
351351

352352
#[test]
@@ -479,4 +479,44 @@ mod tests {
479479
// Invalid seed side
480480
assert!(bip39_mnemonic_from_seed(b"foo").is_err());
481481
}
482+
483+
// Functional test to store seeds, unlock, retrieve seed.
484+
#[test]
485+
fn test_seeds() {
486+
let seed = hex::decode("cb33c20cea62a5c277527e2002da82e6e2b37450a755143a540a54cea8da9044")
487+
.unwrap();
488+
489+
for seed_size in [16, 24, 32] {
490+
mock_memory();
491+
lock();
492+
493+
// Can repeat until initialized - initialized means backup has been created.
494+
for _ in 0..2 {
495+
assert!(encrypt_and_store_seed(&seed[..seed_size], "foo").is_ok());
496+
}
497+
498+
// Wrong password.
499+
assert!(matches!(
500+
unlock("bar"),
501+
Err(Error::IncorrectPassword {
502+
remaining_attempts: 9
503+
})
504+
));
505+
506+
// Can't get seed before unlock.
507+
assert!(copy_seed().is_err());
508+
// Correct password. First time: unlock. After unlock, it becomes a password check.
509+
for _ in 0..3 {
510+
assert!(unlock("foo").is_ok());
511+
}
512+
assert_eq!(copy_seed().unwrap().as_slice(), &seed[..seed_size]);
513+
514+
// Can't store new seed once initialized.
515+
crate::memory::set_initialized().unwrap();
516+
assert!(matches!(
517+
encrypt_and_store_seed(&seed[..seed_size], "foo"),
518+
Err(Error::Memory)
519+
));
520+
}
521+
}
482522
}

test/unit-test/test_keystore_functional.c

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
that seeding/unlocking/derivations work as expected. */
3838

3939
static const char* _some_password = "foo";
40-
static const char* _some_other_password = "bar";
4140

4241
static uint8_t _seed[KEYSTORE_MAX_SEED_LENGTH] =
4342
"\xcb\x33\xc2\x0c\xea\x62\xa5\xc2\x77\x52\x7e\x20\x02\xda\x82\xe6\xe2\xb3\x74\x50\xa7\x55\x14"
@@ -58,42 +57,6 @@ static void _smarteeprom_reset(void)
5857
bitbox02_smarteeprom_init();
5958
}
6059

61-
static void _test_seeds(void** state)
62-
{
63-
_smarteeprom_reset();
64-
assert_true(keystore_is_locked());
65-
uint8_t read_seed[KEYSTORE_MAX_SEED_LENGTH];
66-
size_t read_seed_len;
67-
assert_false(keystore_copy_seed(read_seed, &read_seed_len));
68-
69-
will_return(__wrap_memory_is_initialized, true);
70-
assert_int_equal(
71-
keystore_encrypt_and_store_seed(_seed, 32, _some_password), KEYSTORE_ERR_MEMORY);
72-
73-
uint32_t seed_sizes[3] = {16, 24, 32};
74-
for (size_t seed_size_idx = 0; seed_size_idx < 3; seed_size_idx++) {
75-
uint32_t seed_size = seed_sizes[seed_size_idx];
76-
will_return(__wrap_memory_is_initialized, false);
77-
assert_int_equal(
78-
keystore_encrypt_and_store_seed(_seed, seed_size, _some_password), KEYSTORE_OK);
79-
uint8_t remaining_attempts;
80-
will_return(__wrap_memory_is_seeded, true);
81-
assert_int_equal(
82-
KEYSTORE_ERR_INCORRECT_PASSWORD,
83-
keystore_unlock(_some_other_password, &remaining_attempts, NULL));
84-
// First time: unlock. After unlock, it becomes a password check.
85-
for (int i = 0; i < 3; i++) {
86-
will_return(__wrap_memory_is_seeded, true);
87-
assert_int_equal(
88-
KEYSTORE_OK, keystore_unlock(_some_password, &remaining_attempts, NULL));
89-
}
90-
assert_true(keystore_copy_seed(read_seed, &read_seed_len));
91-
assert_int_equal(seed_size, read_seed_len);
92-
assert_memory_equal(read_seed, _seed, seed_size);
93-
keystore_lock();
94-
}
95-
}
96-
9760
static void _check_mnemonic(const char* expected)
9861
{
9962
uint8_t seed[KEYSTORE_MAX_SEED_LENGTH];
@@ -231,9 +194,9 @@ static void _test_fixtures(void** state)
231194
int main(void)
232195
{
233196
mock_memory_set_salt_root(_salt_root);
197+
_smarteeprom_reset();
234198

235199
const struct CMUnitTest tests[] = {
236-
cmocka_unit_test(_test_seeds),
237200
cmocka_unit_test(_test_fixtures),
238201
};
239202
return cmocka_run_group_tests(tests, NULL, NULL);

0 commit comments

Comments
 (0)