Add basic test module for serde functionality
What changed, and why it matters
This commit only adds a new test file and a development-only dependency (serde_json) to verify that a recently added serde feature serializes byte arrays as expected. It does not change any production code, fix any bug, or alter any security-relevant behavior. There is no security issue here.
No security action needed. This is a routine test-only commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch introduces consensus_encoding/tests/serde.rs containing a single unit test for the serde_as_consensus module. It adds serde_json as a dev-dependency in consensus_encoding/Cargo.toml and updates the two Cargo.lock files accordingly. No library source code is modified, no unsafe code is added, and no vulnerability is addressed.
Changed components
consensus_encoding/tests/serde.rsconsensus_encoding/Cargo.tomlCargo-minimal.lockCargo-recent.lockInspect captured patch +55 / −0
diff --git a/Cargo-minimal.lock b/Cargo-minimal.lock
index 9dfa6bc7..731daf68 100644
--- a/Cargo-minimal.lock
+++ b/Cargo-minimal.lock
@@ -114,6 +114,7 @@ dependencies = [
"bitcoin-internals 0.5.0",
"hex-conservative 1.1.0",
"serde",
+ "serde_json",
]
[[package]]
diff --git a/Cargo-recent.lock b/Cargo-recent.lock
index 56e8021d..4c947df9 100644
--- a/Cargo-recent.lock
+++ b/Cargo-recent.lock
@@ -113,6 +113,7 @@ dependencies = [
"bitcoin-internals 0.5.0",
"hex-conservative 1.1.0",
"serde",
+ "serde_json",
]
[[package]]
diff --git a/consensus_encoding/Cargo.toml b/consensus_encoding/Cargo.toml
index eeabedcf..789d2bc8 100644
--- a/consensus_encoding/Cargo.toml
+++ b/consensus_encoding/Cargo.toml
@@ -27,6 +27,7 @@ serde = { version = "1.0.195", default-features = false, features = ["derive"],
[dev-dependencies]
hex = { package = "hex-conservative", version = "1.1.0" }
+serde_json = "1.0.68"
[package.metadata.docs.rs]
all-features = true
diff --git a/consensus_encoding/tests/serde.rs b/consensus_encoding/tests/serde.rs
new file mode 100644
index 00000000..53b642ae
--- /dev/null
+++ b/consensus_encoding/tests/serde.rs
@@ -0,0 +1,52 @@
+// SPDX-License-Identifier: CC0-1.0
+
+//! Tests for `serde_as_consensus`.
+
+#![cfg(all(feature = "hex", feature = "serde"))]
+
+use bitcoin_consensus_encoding::{
+ ArrayDecoder, ArrayEncoder, Decode, Decoder, DecoderStatus, Encode, UnexpectedEofError,
+};
+
+struct TestArray<const N: usize>([u8; N]);
+
+impl<const N: usize> Encode for TestArray<N> {
+ type Encoder<'e>
+ = ArrayEncoder<N>
+ where
+ Self: 'e;
+
+ fn encoder(&self) -> Self::Encoder<'_> { ArrayEncoder::without_length_prefix(self.0) }
+}
+
+#[derive(Default)]
+struct TestArrayDecoder<const N: usize>(ArrayDecoder<N>);
+
+impl<const N: usize> Decoder for TestArrayDecoder<N> {
+ type Output = TestArray<N>;
+ type Error = UnexpectedEofError;
+
+ fn push_bytes(&mut self, bytes: &mut &[u8]) -> Result<DecoderStatus, Self::Error> {
+ self.0.push_bytes(bytes)
+ }
+
+ fn end(self) -> Result<Self::Output, Self::Error> { self.0.end().map(TestArray) }
+
+ fn read_limit(&self) -> usize { self.0.read_limit() }
+}
+
+impl<const N: usize> Decode for TestArray<N> {
+ type Decoder = TestArrayDecoder<N>;
+}
+
+#[derive(serde::Serialize)]
+struct WithConsensus(
+ #[serde(with = "bitcoin_consensus_encoding::serde_as_consensus")] TestArray<4>,
+);
+
+#[test]
+fn serialize_array_bytes_as_hex_json() {
+ let value = WithConsensus(TestArray([0xef, 0xbe, 0xad, 0xde]));
+
+ assert_eq!(serde_json::to_string(&value).unwrap(), "\"efbeadde\"");
+}
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.