Skip to content

Commit 6f80c44

Browse files
committed
ll: add more tests
1 parent d285d0a commit 6f80c44

File tree

1 file changed

+111
-2
lines changed

1 file changed

+111
-2
lines changed

src/ll.rs

Lines changed: 111 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ pub fn parse_version(bytes: &[u8]) -> Result<(usize, u8), Error> {
474474

475475
pub fn parse_encryption(bytes: &[u8]) -> Result<(usize, u8), Error> {
476476
if bytes.is_empty() {
477-
return Err(Error::ContentMetadata);
477+
return Err(Error::Encryption);
478478
}
479479
let encryption = bytes[0];
480480
Ok((1, encryption))
@@ -542,10 +542,10 @@ pub fn parse_individual_secrets(
542542
}
543543
// <COUNT>
544544
let count = bytes[0];
545-
let mut offset = init_offset(bytes, 1)?;
546545
if count < 1 {
547546
return Err(Error::IndividualSecretsEmpty);
548547
}
548+
let mut offset = init_offset(bytes, 1)?;
549549

550550
let mut individual_secrets = BTreeSet::new();
551551
for _ in 0..count {
@@ -682,6 +682,68 @@ mod tests {
682682
assert_eq!(res, Err(Error::Version));
683683
}
684684

685+
#[test]
686+
pub fn test_parse_encryption() {
687+
let (l, e) = parse_encryption(&[0]).unwrap();
688+
assert_eq!(l, 1);
689+
assert_eq!(e, 0);
690+
let (l, e) = parse_encryption(&[0, 2]).unwrap();
691+
assert_eq!(l, 1);
692+
assert_eq!(e, 0);
693+
let (l, e) = parse_encryption(&[2, 0]).unwrap();
694+
assert_eq!(l, 1);
695+
assert_eq!(e, 2);
696+
let failed = parse_encryption(&[]).unwrap_err();
697+
assert_eq!(failed, Error::Encryption)
698+
}
699+
700+
#[test]
701+
pub fn test_parse_derivation_path() {
702+
// single deriv path
703+
let (_, p) = parse_derivation_paths(&[0x01, 0x01, 0x00, 0x00, 0x00, 0x01]).unwrap();
704+
assert_eq!(p.len(), 1);
705+
706+
// child number must be encoded on 4 bytes
707+
let p = parse_derivation_paths(&[0x01, 0x01, 0x00]).unwrap_err();
708+
assert_eq!(p, Error::Corrupted);
709+
let p = parse_derivation_paths(&[0x01, 0x01, 0x00, 0x00]).unwrap_err();
710+
assert_eq!(p, Error::Corrupted);
711+
let p = parse_derivation_paths(&[0x01, 0x01, 0x00, 0x00, 0x00]).unwrap_err();
712+
assert_eq!(p, Error::Corrupted);
713+
714+
// empty childs
715+
let p = parse_derivation_paths(&[0x01, 0x00]).unwrap_err();
716+
assert_eq!(p, Error::DerivPathEmpty);
717+
}
718+
719+
#[test]
720+
pub fn test_parse_individual_secrets() {
721+
// empty bytes
722+
let fail = parse_individual_secrets(&[]).unwrap_err();
723+
assert_eq!(fail, Error::EmptyBytes);
724+
725+
// empty vector
726+
let fail = parse_individual_secrets(&[0x00]).unwrap_err();
727+
assert_eq!(fail, Error::IndividualSecretsEmpty);
728+
729+
let is1 = [1u8; 32].to_vec();
730+
let is2 = [2u8; 32].to_vec();
731+
732+
// single secret
733+
let mut bytes = vec![0x01];
734+
bytes.append(&mut is1.clone());
735+
let (_, is) = parse_individual_secrets(&bytes).unwrap();
736+
assert_eq!(is[0].to_vec(), is1);
737+
738+
// multiple secrets
739+
let mut bytes = vec![0x02];
740+
bytes.append(&mut is1.clone());
741+
bytes.append(&mut is2.clone());
742+
let (_, is) = parse_individual_secrets(&bytes).unwrap();
743+
assert_eq!(is[0].to_vec(), is1);
744+
assert_eq!(is[1].to_vec(), is2);
745+
}
746+
685747
#[test]
686748
fn test_parse_content() {
687749
// empty bytes must fail
@@ -710,6 +772,53 @@ mod tests {
710772
assert_eq!(c, Content::Proprietary(vec![0, 0, 0]));
711773
}
712774

775+
#[test]
776+
fn test_serialize_content() {
777+
// Proprietary
778+
let mut c = Content::Proprietary(vec![0, 0, 0]);
779+
let mut serialized: Vec<u8> = c.into();
780+
assert_eq!(serialized, vec![3, 0, 0, 0]);
781+
// BIP 380
782+
c = Content::Bip380;
783+
serialized = c.into();
784+
assert_eq!(serialized, vec![0x02, 0x01, 0x7C]);
785+
c = Content::BIP(380);
786+
serialized = c.into();
787+
assert_eq!(serialized, vec![0x02, 0x01, 0x7C]);
788+
// BIP 388
789+
c = Content::Bip388;
790+
serialized = c.into();
791+
assert_eq!(serialized, vec![0x02, 0x01, 0x84]);
792+
c = Content::BIP(388);
793+
serialized = c.into();
794+
assert_eq!(serialized, vec![0x02, 0x01, 0x84]);
795+
// BIP 329
796+
c = Content::Bip329;
797+
serialized = c.into();
798+
assert_eq!(serialized, vec![0x02, 0x01, 0x49]);
799+
c = Content::BIP(329);
800+
serialized = c.into();
801+
assert_eq!(serialized, vec![0x02, 0x01, 0x49]);
802+
}
803+
804+
#[test]
805+
fn test_content_is_known() {
806+
let mut c = Content::None;
807+
assert!(!c.is_known());
808+
c = Content::Unknown;
809+
assert!(!c.is_known());
810+
c = Content::Proprietary(vec![0, 0, 0]);
811+
assert!(!c.is_known());
812+
c = Content::Bip380;
813+
assert!(c.is_known());
814+
c = Content::Bip388;
815+
assert!(c.is_known());
816+
c = Content::Bip329;
817+
assert!(c.is_known());
818+
c = Content::BIP(0);
819+
assert!(c.is_known());
820+
}
821+
713822
#[test]
714823
fn test_simple_encode_decode_encrypted_payload() {
715824
let bytes = encode_encrypted_payload([3; 12], &[1, 2, 3, 4]).unwrap();

0 commit comments

Comments
 (0)