Add encoder wrapper type integration test
What changed, and why it matters
This commit only adds a new test file that exercises existing encoding wrapper types. It does not change any production code, fix bugs, or alter behavior. There is no security relevance.
No action required; this is a routine test-only commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit introduces consensus_encoding/tests/wrappers.rs, an integration test verifying ArrayEncoder, BytesEncoder (with/without length prefix), and Encoder2 through custom wrapper types created via the encoder_newtype! macro. The diff is purely additive test code (+111 lines, 1 file) with no changes to library implementation, APIs, or dependencies.
Changed components
consensus_encoding/tests/wrappers.rsInspect captured patch +111 / −0
diff --git a/consensus_encoding/tests/wrappers.rs b/consensus_encoding/tests/wrappers.rs
new file mode 100644
index 00000000..53bf5803
--- /dev/null
+++ b/consensus_encoding/tests/wrappers.rs
@@ -0,0 +1,111 @@
+//! Test using wrapper types as we expect the lib to be used.
+
+#![cfg(feature = "std")]
+
+use consensus_encoding as encoding;
+use encoding::{ArrayEncoder, BytesEncoder, Encodable, Encoder2};
+
+encoding::encoder_newtype! {
+ /// An encoder that uses an inner `ArrayEncoder`.
+ pub struct TestArrayEncoder(ArrayEncoder<4>);
+}
+
+encoding::encoder_newtype! {
+ /// An encoder that uses an inner `BytesEncoder`.
+ pub struct TestBytesEncoder<'e>(BytesEncoder<'e>);
+}
+
+#[test]
+fn array_encoder() {
+ #[derive(Debug, Default, Clone)]
+ pub struct Test(u32);
+
+ impl Encodable for Test {
+ type Encoder<'e> = TestArrayEncoder;
+ fn encoder(&self) -> Self::Encoder<'_> {
+ TestArrayEncoder(ArrayEncoder::without_length_prefix(self.0.to_le_bytes()))
+ }
+ }
+
+ let t = Test(0xcafe_babe); // Encodes using an array.
+
+ let want = [0xbe, 0xba, 0xfe, 0xca];
+ let got = encoding::encode_to_vec(&t);
+
+ assert_eq!(got, want);
+}
+
+#[test]
+fn bytes_encoder_without_length_prefix() {
+ #[derive(Debug, Default, Clone)]
+ pub struct Test(Vec<u8>);
+
+ impl Encodable for Test {
+ type Encoder<'e>
+ = TestBytesEncoder<'e>
+ where
+ Self: 'e;
+
+ fn encoder(&self) -> Self::Encoder<'_> {
+ TestBytesEncoder(BytesEncoder::without_length_prefix(self.0.as_ref()))
+ }
+ }
+
+ let t = Test(vec![0xca, 0xfe]);
+
+ let want = [0xca, 0xfe];
+ let got = encoding::encode_to_vec(&t);
+
+ assert_eq!(got, want);
+}
+
+#[test]
+fn bytes_encoder_with_length_prefix() {
+ #[derive(Debug, Default, Clone)]
+ pub struct Test(Vec<u8>);
+
+ impl Encodable for Test {
+ type Encoder<'e>
+ = TestBytesEncoder<'e>
+ where
+ Self: 'e;
+
+ fn encoder(&self) -> Self::Encoder<'_> {
+ TestBytesEncoder(BytesEncoder::with_length_prefix(self.0.as_ref()))
+ }
+ }
+
+ let t = Test(vec![0xca, 0xfe]);
+
+ let want = [0x02, 0xca, 0xfe];
+ let got = encoding::encode_to_vec(&t);
+
+ assert_eq!(got, want);
+}
+
+#[test]
+fn two_encoder() {
+ #[derive(Debug, Default, Clone)]
+ pub struct Test {
+ a: Vec<u8>, // Encode without prefix.
+ b: Vec<u8>, // Encode with prefix.
+ }
+
+ impl Encodable for Test {
+ type Encoder<'e> = Encoder2<TestBytesEncoder<'e>, TestBytesEncoder<'e>>;
+
+ fn encoder(&self) -> Self::Encoder<'_> {
+ let a = TestBytesEncoder(BytesEncoder::without_length_prefix(self.a.as_ref()));
+ let b = TestBytesEncoder(BytesEncoder::with_length_prefix(self.b.as_ref()));
+
+ Encoder2::new(a, b)
+ }
+ }
+
+ let t = Test { a: vec![0xca, 0xfe], b: (vec![0xba, 0xbe]) };
+
+ let want = [0xca, 0xfe, 0x02, 0xba, 0xbe];
+ let got = encoding::encode_to_vec(&t);
+
+ assert_eq!(got, want);
+}
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.