consensus_encoding: Add a test for empty encoders
What changed, and why it matters
This commit only adds a new unit test to the rust-bitcoin consensus_encoding crate. It verifies that an encoder correctly handles empty byte slices when chained between non-empty slices. There is no code change to production logic, no bug fix, and no security relevance.
No action required. This is a test-only addition with no security implications.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff adds a single test function empty_encoders in consensus_encoding/tests/composition.rs. The test constructs an Encoder3 from three BytesEncoder instances, where the middle one wraps an empty slice. It then asserts the expected chunk iteration behavior: non-empty chunk, empty chunk, non-empty chunk, then exhaustion. No library code is modified.
Changed components
consensus_encoding/tests/composition.rsInspect captured patch +25 / −2
diff --git a/consensus_encoding/tests/composition.rs b/consensus_encoding/tests/composition.rs
index 2ea95ef8..e0cf3b02 100644
--- a/consensus_encoding/tests/composition.rs
+++ b/consensus_encoding/tests/composition.rs
@@ -3,8 +3,8 @@
//! Test composition of encoders and decoders.
use bitcoin_consensus_encoding::{
- ArrayDecoder, ArrayEncoder, Decodable, Decoder, Decoder2, Decoder2Error, Decoder6, Encodable,
- Encoder, Encoder2, Encoder6, UnexpectedEofError,
+ ArrayDecoder, ArrayEncoder, BytesEncoder, Decodable, Decoder, Decoder2, Decoder2Error,
+ Decoder6, Encodable, Encoder, Encoder2, Encoder3, Encoder6, UnexpectedEofError,
};
const EMPTY: &[u8] = &[];
@@ -315,3 +315,26 @@ fn composition_error_unification() {
push_result.unwrap_err()
);
}
+
+#[test]
+fn empty_encoders() {
+ let bytes = [0x01, 2, 3, 4];
+ let mut encoder = Encoder3::new(
+ BytesEncoder::without_length_prefix(&bytes[..2]),
+ BytesEncoder::without_length_prefix(&[]),
+ BytesEncoder::without_length_prefix(&bytes[2..]),
+ );
+
+ assert_eq!(encoder.current_chunk(), Some(&[1, 2][..]));
+ assert!(encoder.advance());
+
+ // Still have to advance over empty slice.
+ assert_eq!(encoder.current_chunk(), Some(&[][..]));
+ assert!(encoder.advance());
+
+ assert_eq!(encoder.current_chunk(), Some(&[3, 4][..]));
+ assert!(!encoder.advance());
+
+ // Exhausted.
+ assert!(encoder.current_chunk().is_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.