consensus_encoding: add slice encode unit tests
What changed, and why it matters
This commit only adds new unit tests for a slice encoder in the consensus_encoding crate. It does not change any production code, fix any bug, or alter behavior. There is no security relevance.
No action needed; this is a test-only change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff adds three test functions to consensus_encoding/src/encode/encoders.rs: encode_slice_with_elements, encode_empty_slice, and encode_slice_with_zero_sized_arrays. They exercise SliceEncoder::with_length_prefix for non-empty, empty, and zero-sized element slices. No implementation code is modified.
Changed components
Inspect captured patch +41 / −0
diff --git a/consensus_encoding/src/encode/encoders.rs b/consensus_encoding/src/encode/encoders.rs
index 207ea1f0..8d8e06f2 100644
--- a/consensus_encoding/src/encode/encoders.rs
+++ b/consensus_encoding/src/encode/encoders.rs
@@ -343,4 +343,45 @@ mod tests {
assert!(!encoder.advance());
assert_eq!(encoder.current_chunk(), None);
}
+
+ #[test]
+ fn encode_slice_with_elements() {
+ // Should have length prefix chunk, then element chunks, then exhausted.
+ let slice = &[TestArray([0x34, 0x12, 0x00, 0x00]), TestArray([0x78, 0x56, 0x00, 0x00])];
+ let mut encoder = SliceEncoder::with_length_prefix(slice);
+
+ assert_eq!(encoder.current_chunk(), Some(&[2u8][..]));
+ assert!(encoder.advance());
+ assert_eq!(encoder.current_chunk(), Some(&[0x34, 0x12, 0x00, 0x00][..]));
+ assert!(encoder.advance());
+ assert_eq!(encoder.current_chunk(), Some(&[0x78, 0x56, 0x00, 0x00][..]));
+ assert!(!encoder.advance());
+ assert_eq!(encoder.current_chunk(), None);
+ }
+
+ #[test]
+ fn encode_empty_slice() {
+ // Should have only length prefix chunk (0), then exhausted.
+ let slice: &[TestArray<4>] = &[];
+ let mut encoder = SliceEncoder::with_length_prefix(slice);
+
+ assert_eq!(encoder.current_chunk(), Some(&[0u8][..]));
+ assert!(!encoder.advance());
+ assert_eq!(encoder.current_chunk(), None);
+ }
+
+ #[test]
+ fn encode_slice_with_zero_sized_arrays() {
+ // Should have length prefix chunk, then empty array chunks, then exhausted.
+ let slice = &[TestArray([]), TestArray([])];
+ let mut encoder = SliceEncoder::with_length_prefix(slice);
+
+ assert_eq!(encoder.current_chunk(), Some(&[2u8][..]));
+ assert!(encoder.advance());
+ assert_eq!(encoder.current_chunk(), Some(&[][..]));
+ assert!(encoder.advance());
+ assert_eq!(encoder.current_chunk(), Some(&[][..]));
+ assert!(!encoder.advance());
+ assert_eq!(encoder.current_chunk(), None);
+ }
}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.