From fe967279e5055f0786b44f13c8efc82bd0794f32 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 24 Sep 2024 12:40:30 +1000 Subject: [PATCH] Improve witness unit tests for single empty element The current unit test is incorrect, the indices field of a witness with a single element starts at 1 because 0 is encode as a single byte (compact encoded integer). Fix the debug test and add a test that pushes an empty slice. --- bitcoin/src/blockdata/witness.rs | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/bitcoin/src/blockdata/witness.rs b/bitcoin/src/blockdata/witness.rs index 432079e68..74e3e4540 100644 --- a/bitcoin/src/blockdata/witness.rs +++ b/bitcoin/src/blockdata/witness.rs @@ -645,16 +645,33 @@ mod test { v } - #[test] - fn witness_debug_can_display_empty_instruction() { - let witness = Witness { + // A witness with a single element that is empty (zero length). + fn single_empty_element() -> Witness { + // The first is 0 serialized as a compact size integer. + // The last four bytes represent start at index 0. + let content = [0_u8; 5]; + + Witness { witness_elements: 1, - content: append_u32_vec(vec![], &[0]), - indices_start: 2, - }; + content: content.to_vec(), + indices_start: 1, + } + } + + #[test] + fn witness_debug_can_display_empty_element() { + let witness = single_empty_element(); println!("{:?}", witness); } + #[test] + fn witness_single_empty_element() { + let mut got = Witness::new(); + got.push(&[]); + let want = single_empty_element(); + assert_eq!(got, want) + } + #[test] fn test_push() { let mut witness = Witness::default();