consensus_encoding: update encoder unit tests
What changed, and why it matters
This commit only changes unit tests for the consensus encoding module. It rewrites how tests exercise the byte and array encoders, switching from a helper that collects all output into a vector to direct assertions about each encoding step. There is no change to production code, no bug fix, and no security relevance.
No security action needed. Treat as routine test maintenance.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff modifies consensus_encoding/src/encode/encoders.rs under #[cfg(test)]. It removes the alloc-dependent run_encoder helper and replaces it with Encodable test wrappers (TestBytes, TestArray) plus individual test functions that call current_chunk() and advance() directly. The production Encoder implementations remain untouched. No security-sensitive behavior is altered.
Changed components
consensus_encoding/src/encode/encoders.rs (test module only)Inspect captured patch +75 / −30
diff --git a/consensus_encoding/src/encode/encoders.rs b/consensus_encoding/src/encode/encoders.rs
index 52456e87..14362693 100644
--- a/consensus_encoding/src/encode/encoders.rs
+++ b/consensus_encoding/src/encode/encoders.rs
@@ -241,61 +241,106 @@ impl<A: Encoder, B: Encoder, C: Encoder, D: Encoder, E: Encoder, F: Encoder> Enc
}
#[cfg(test)]
-#[cfg(feature = "alloc")]
mod tests {
- use alloc::vec::Vec;
-
use super::*;
- // Run the encoder i.e., use it to encode into a vector.
- fn run_encoder(mut encoder: impl Encoder) -> Vec<u8> {
- let mut vec = Vec::new();
- while let Some(chunk) = encoder.current_chunk() {
- vec.extend_from_slice(chunk);
- encoder.advance();
+ struct TestBytes<'a>(&'a [u8], bool);
+
+ impl<'a> Encodable for TestBytes<'a> {
+ type Encoder<'s>
+ = BytesEncoder<'s>
+ where
+ Self: 's;
+
+ fn encoder(&self) -> Self::Encoder<'_> {
+ if self.1 {
+ BytesEncoder::with_length_prefix(self.0)
+ } else {
+ BytesEncoder::without_length_prefix(self.0)
+ }
}
- vec
+ }
+
+ struct TestArray<const N: usize>([u8; N]);
+
+ impl<const N: usize> Encodable for TestArray<N> {
+ type Encoder<'s>
+ = ArrayEncoder<N>
+ where
+ Self: 's;
+
+ fn encoder(&self) -> Self::Encoder<'_> { ArrayEncoder::without_length_prefix(self.0) }
+ }
+
+ #[test]
+ fn encode_array_with_data() {
+ // Should have one chunk with the array data, then exhausted.
+ let test_array = TestArray([1u8, 2, 3, 4]);
+ let mut encoder = test_array.encoder();
+ assert_eq!(encoder.current_chunk(), Some(&[1u8, 2, 3, 4][..]));
+ assert!(!encoder.advance());
+ assert_eq!(encoder.current_chunk(), None);
+ }
+
+ #[test]
+ fn encode_empty_array() {
+ // Empty array should have one empty chunk, then exhausted.
+ let test_array = TestArray([]);
+ let mut encoder = test_array.encoder();
+ assert_eq!(encoder.current_chunk(), Some(&[][..]));
+ assert!(!encoder.advance());
+ assert_eq!(encoder.current_chunk(), None);
}
#[test]
fn encode_byte_slice_without_prefix() {
+ // Should have one chunk with the byte data, then exhausted.
let obj = [1u8, 2, 3];
+ let test_bytes = TestBytes(&obj, false);
+ let mut encoder = test_bytes.encoder();
- let encoder = BytesEncoder::without_length_prefix(&obj);
- let got = run_encoder(encoder);
-
- assert_eq!(got, obj);
+ assert_eq!(encoder.current_chunk(), Some(&[1u8, 2, 3][..]));
+ assert!(!encoder.advance());
+ assert_eq!(encoder.current_chunk(), None);
}
#[test]
fn encode_empty_byte_slice_without_prefix() {
+ // Should have one empty chunk, then exhausted.
let obj = [];
+ let test_bytes = TestBytes(&obj, false);
+ let mut encoder = test_bytes.encoder();
- let encoder = BytesEncoder::without_length_prefix(&obj);
- let got = run_encoder(encoder);
-
- assert_eq!(got, obj);
+ assert_eq!(encoder.current_chunk(), Some(&[][..]));
+ assert!(!encoder.advance());
+ assert_eq!(encoder.current_chunk(), None);
}
#[test]
fn encode_byte_slice_with_prefix() {
+ // Should have length prefix chunk, then data chunk, then exhausted.
let obj = [1u8, 2, 3];
-
- let encoder = BytesEncoder::with_length_prefix(&obj);
- let got = run_encoder(encoder);
-
- let want = [3u8, 1, 2, 3];
- assert_eq!(got, want);
+ let test_bytes = TestBytes(&obj, true);
+ let mut encoder = test_bytes.encoder();
+
+ assert_eq!(encoder.current_chunk(), Some(&[3u8][..]));
+ assert!(encoder.advance());
+ assert_eq!(encoder.current_chunk(), Some(&[1u8, 2, 3][..]));
+ assert!(!encoder.advance());
+ assert_eq!(encoder.current_chunk(), None);
}
#[test]
fn encode_empty_byte_slice_with_prefix() {
+ // Should have length prefix chunk (0), then empty data chunk, then exhausted.
let obj = [];
-
- let encoder = BytesEncoder::with_length_prefix(&obj);
- let got = run_encoder(encoder);
-
- let want = [0u8];
- assert_eq!(got, want);
+ let test_bytes = TestBytes(&obj, true);
+ let mut encoder = test_bytes.encoder();
+
+ assert_eq!(encoder.current_chunk(), Some(&[0u8][..]));
+ 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.