Skip to content

Commit b502903

Browse files
committed
tests: move keystore test_fixtures from bitbox02 to bitbox02_rust
`encode_xpub_at_keypath()` is going to be removed (ported to bitbox02_rust), but `get_xpub()` will still be in `bitbox02_rust::keystore`.
1 parent 0390741 commit b502903

File tree

2 files changed

+75
-78
lines changed

2 files changed

+75
-78
lines changed

src/rust/bitbox02-rust/src/keystore.rs

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ pub fn bip85_ln(index: u32) -> Result<zeroize::Zeroizing<Vec<u8>>, ()> {
108108
mod tests {
109109
use super::*;
110110

111-
use bitbox02::testing::{mock_unlocked, mock_unlocked_using_mnemonic, TEST_MNEMONIC};
111+
use bitbox02::testing::{
112+
mock_memory, mock_unlocked, mock_unlocked_using_mnemonic, TEST_MNEMONIC,
113+
};
112114

113115
#[test]
114116
fn test_get_bip39_mnemonic() {
@@ -252,4 +254,76 @@ mod tests {
252254
// Index too high.
253255
assert!(bip85_ln(HARDENED).is_err());
254256
}
257+
258+
#[test]
259+
fn test_fixtures() {
260+
struct Test {
261+
seed_len: usize,
262+
mnemonic_passphrase: &'static str,
263+
expected_mnemonic: &'static str,
264+
expected_xpub: &'static str,
265+
expected_u2f_seed_hex: &'static str,
266+
}
267+
let seed = hex::decode("cb33c20cea62a5c277527e2002da82e6e2b37450a755143a540a54cea8da9044")
268+
.unwrap();
269+
270+
let tests = [
271+
Test {
272+
seed_len: 32,
273+
mnemonic_passphrase: "",
274+
expected_mnemonic: "sleep own lobster state clean thrive tail exist cactus bitter pass soccer clinic riot dream turkey before sport action praise tunnel hood donate man",
275+
expected_xpub: "xpub6Cj6NNCGj2CRPHvkuEG1rbW3nrNCAnLjaoTg1P67FCGoahSsbg9WQ7YaMEEP83QDxt2kZ3hTPAPpGdyEZcfAC1C75HfR66UbjpAb39f4PnG",
276+
expected_u2f_seed_hex: "4f464a6667ad88eebcd0f02982761e474ee0dd16253160320f49d1d6681745e9",
277+
},
278+
Test {
279+
seed_len: 32,
280+
mnemonic_passphrase: "abc",
281+
expected_mnemonic: "sleep own lobster state clean thrive tail exist cactus bitter pass soccer clinic riot dream turkey before sport action praise tunnel hood donate man",
282+
expected_xpub: "xpub6DXBP3HhFdhUTafatEULxfTXUUxDVuCxfa9RAiBU5r6aRgKiABbeBDyqwWWjmKPP1BZvpvVNMbVR5LeHzhQphtLcPZ8jk3MdLBgc2sACJwR",
283+
expected_u2f_seed_hex: "d599da991ad83baaf449c789e2dff1539dd66983b47a1dec1c00ff3f352cccbc",
284+
},
285+
Test {
286+
seed_len: 24,
287+
mnemonic_passphrase: "",
288+
expected_mnemonic: "sleep own lobster state clean thrive tail exist cactus bitter pass soccer clinic riot dream turkey before subject",
289+
expected_xpub: "xpub6C7fKxGtTzEVxCC22U2VHx4GpaVy77DzU6KdZ1CLuHgoUGviBMWDc62uoQVxqcRa5RQbMPnffjpwxve18BG81VJhJDXnSpRe5NGKwVpXiAb",
290+
expected_u2f_seed_hex: "fb9dc3fb0a17390776df5c3d8f9261bc5fd5df9f00414cee1393e37e0efda7ef",
291+
},
292+
Test {
293+
seed_len: 16,
294+
mnemonic_passphrase: "",
295+
expected_mnemonic: "sleep own lobster state clean thrive tail exist cactus bitter pass sniff",
296+
expected_xpub: "xpub6DLvpzjKpJ8k4xYrWYPmZQkUe9dkG1eRig2v6Jz4iYgo8hcpHWx87gGoCGDaB2cHFZ3ExUfe1jDiMu7Ch6gA4ULCBhvwZj29mHCPYSux3YV",
297+
expected_u2f_seed_hex: "20d68b206aff9667b623a460ce61fc94762de67561d6855ca9a6df7b409b2a54",
298+
},
299+
];
300+
301+
for test in tests {
302+
mock_memory();
303+
keystore::lock();
304+
let seed = &seed[..test.seed_len];
305+
assert!(keystore::unlock_bip39(test.mnemonic_passphrase).is_err());
306+
assert!(keystore::encrypt_and_store_seed(seed, "foo").is_ok());
307+
assert!(keystore::unlock_bip39(test.mnemonic_passphrase).is_err());
308+
assert!(keystore::is_locked());
309+
assert!(keystore::unlock("foo").is_ok());
310+
assert!(keystore::is_locked());
311+
assert!(keystore::unlock_bip39(test.mnemonic_passphrase).is_ok());
312+
assert!(!keystore::is_locked());
313+
assert_eq!(
314+
get_bip39_mnemonic().unwrap().as_str(),
315+
test.expected_mnemonic,
316+
);
317+
let keypath = &[44 + HARDENED, 0 + HARDENED, 0 + HARDENED];
318+
let xpub = get_xpub(keypath).unwrap();
319+
assert_eq!(
320+
xpub.serialize_str(crate::bip32::XPubType::Xpub).unwrap(),
321+
test.expected_xpub,
322+
);
323+
assert_eq!(
324+
hex::encode(keystore::get_u2f_seed().unwrap()),
325+
test.expected_u2f_seed_hex,
326+
);
327+
}
328+
}
255329
}

src/rust/bitbox02/src/keystore.rs

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,6 @@ mod tests {
359359
use bitcoin::secp256k1;
360360

361361
use crate::testing::{mock_memory, mock_unlocked, mock_unlocked_using_mnemonic};
362-
use alloc::string::ToString;
363362
use util::bip32::HARDENED;
364363

365364
#[test]
@@ -845,80 +844,4 @@ mod tests {
845844
));
846845
}
847846
}
848-
849-
#[test]
850-
fn test_fixtures() {
851-
struct Test {
852-
seed_len: usize,
853-
mnemonic_passphrase: &'static str,
854-
expected_mnemonic: &'static str,
855-
expected_xpub: &'static str,
856-
expected_u2f_seed_hex: &'static str,
857-
}
858-
let seed = hex::decode("cb33c20cea62a5c277527e2002da82e6e2b37450a755143a540a54cea8da9044")
859-
.unwrap();
860-
861-
let tests = [
862-
Test {
863-
seed_len: 32,
864-
mnemonic_passphrase: "",
865-
expected_mnemonic: "sleep own lobster state clean thrive tail exist cactus bitter pass soccer clinic riot dream turkey before sport action praise tunnel hood donate man",
866-
expected_xpub: "xpub6Cj6NNCGj2CRPHvkuEG1rbW3nrNCAnLjaoTg1P67FCGoahSsbg9WQ7YaMEEP83QDxt2kZ3hTPAPpGdyEZcfAC1C75HfR66UbjpAb39f4PnG",
867-
expected_u2f_seed_hex: "4f464a6667ad88eebcd0f02982761e474ee0dd16253160320f49d1d6681745e9",
868-
},
869-
Test {
870-
seed_len: 32,
871-
mnemonic_passphrase: "abc",
872-
expected_mnemonic: "sleep own lobster state clean thrive tail exist cactus bitter pass soccer clinic riot dream turkey before sport action praise tunnel hood donate man",
873-
expected_xpub: "xpub6DXBP3HhFdhUTafatEULxfTXUUxDVuCxfa9RAiBU5r6aRgKiABbeBDyqwWWjmKPP1BZvpvVNMbVR5LeHzhQphtLcPZ8jk3MdLBgc2sACJwR",
874-
expected_u2f_seed_hex: "d599da991ad83baaf449c789e2dff1539dd66983b47a1dec1c00ff3f352cccbc",
875-
},
876-
Test {
877-
seed_len: 24,
878-
mnemonic_passphrase: "",
879-
expected_mnemonic: "sleep own lobster state clean thrive tail exist cactus bitter pass soccer clinic riot dream turkey before subject",
880-
expected_xpub: "xpub6C7fKxGtTzEVxCC22U2VHx4GpaVy77DzU6KdZ1CLuHgoUGviBMWDc62uoQVxqcRa5RQbMPnffjpwxve18BG81VJhJDXnSpRe5NGKwVpXiAb",
881-
expected_u2f_seed_hex: "fb9dc3fb0a17390776df5c3d8f9261bc5fd5df9f00414cee1393e37e0efda7ef",
882-
},
883-
Test {
884-
seed_len: 16,
885-
mnemonic_passphrase: "",
886-
expected_mnemonic: "sleep own lobster state clean thrive tail exist cactus bitter pass sniff",
887-
expected_xpub: "xpub6DLvpzjKpJ8k4xYrWYPmZQkUe9dkG1eRig2v6Jz4iYgo8hcpHWx87gGoCGDaB2cHFZ3ExUfe1jDiMu7Ch6gA4ULCBhvwZj29mHCPYSux3YV",
888-
expected_u2f_seed_hex: "20d68b206aff9667b623a460ce61fc94762de67561d6855ca9a6df7b409b2a54",
889-
},
890-
];
891-
892-
for test in tests {
893-
mock_memory();
894-
lock();
895-
let seed = &seed[..test.seed_len];
896-
assert!(unlock_bip39(test.mnemonic_passphrase).is_err());
897-
assert!(encrypt_and_store_seed(seed, "foo").is_ok());
898-
assert!(unlock_bip39(test.mnemonic_passphrase).is_err());
899-
assert!(is_locked());
900-
assert!(unlock("foo").is_ok());
901-
assert!(is_locked());
902-
assert!(unlock_bip39(test.mnemonic_passphrase).is_ok());
903-
assert!(!is_locked());
904-
assert_eq!(
905-
bip39_mnemonic_from_seed(&copy_seed().unwrap())
906-
.unwrap()
907-
.as_str(),
908-
test.expected_mnemonic,
909-
);
910-
let keypath = &[44 + HARDENED, 0 + HARDENED, 0 + HARDENED];
911-
let encoded_xpub = encode_xpub_at_keypath(keypath).unwrap();
912-
assert_eq!(
913-
bitcoin::bip32::Xpub::decode(&encoded_xpub)
914-
.unwrap()
915-
.to_string(),
916-
test.expected_xpub,
917-
);
918-
assert_eq!(
919-
hex::encode(get_u2f_seed().unwrap()),
920-
test.expected_u2f_seed_hex,
921-
);
922-
}
923-
}
924847
}

0 commit comments

Comments
 (0)