test(p2p): Update serialize for `AddrV2`
What changed, and why it matters
This commit only updates a test file. It refactors an existing unit test for the `AddrV2` network address type so that expected byte sequences are stored in variables and also compared against a second serialization helper (`encoding::encode_to_vec`). There is no change to production code, no bug fix, and no security-related behavior change.
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 in p2p/src/address.rs modifies the serialize_addrv2 test. Previously each AddrV2 variant was checked with a single assert_eq!(serialize(&ip), hex!(...)). The patch extracts each expected byte string into a local variable and adds a second assertion using encoding::encode_to_vec(&ip). This is a test-only refactor that increases coverage of the encoding path but does not alter the serialize or AddrV2 implementations.
Changed components
p2p/src/address.rs (test module only)Inspect captured patch +18 / −12
diff --git a/p2p/src/address.rs b/p2p/src/address.rs
index f2db4127..cae81e22 100644
--- a/p2p/src/address.rs
+++ b/p2p/src/address.rs
@@ -799,36 +799,42 @@ mod test {
fn serialize_addrv2() {
// Taken from https://github.com/bitcoin/bitcoin/blob/12a1c3ad1a43634d2a98717e49e3f02c4acea2fe/src/test/net_tests.cpp#L348
+ let ip_bytes = hex!("010401020304");
let ip = AddrV2::Ipv4(Ipv4Addr::new(1, 2, 3, 4));
- assert_eq!(serialize(&ip), hex!("010401020304"));
+ assert_eq!(serialize(&ip), ip_bytes);
+ assert_eq!(encoding::encode_to_vec(&ip).as_slice(), ip_bytes);
+ let ip_bytes = hex!("02101a1b2a2b3a3b4a4b5a5b6a6b7a7b8a8b");
let ip =
AddrV2::Ipv6("1a1b:2a2b:3a3b:4a4b:5a5b:6a6b:7a7b:8a8b".parse::<Ipv6Addr>().unwrap());
- assert_eq!(serialize(&ip), hex!("02101a1b2a2b3a3b4a4b5a5b6a6b7a7b8a8b"));
+ assert_eq!(serialize(&ip), ip_bytes);
+ assert_eq!(encoding::encode_to_vec(&ip).as_slice(), ip_bytes);
+ let tor_bytes = hex!("042053cd5648488c4707914182655b7664034e09e66f7e8cbf1084e654eb56c5bd88");
let ip = AddrV2::TorV3(
FromHex::from_hex("53cd5648488c4707914182655b7664034e09e66f7e8cbf1084e654eb56c5bd88")
.unwrap(),
);
- assert_eq!(
- serialize(&ip),
- hex!("042053cd5648488c4707914182655b7664034e09e66f7e8cbf1084e654eb56c5bd88")
- );
+ assert_eq!(serialize(&ip), tor_bytes);
+ assert_eq!(encoding::encode_to_vec(&ip), tor_bytes);
+ let i2p_bytes = hex!("0520a2894dabaec08c0051a481a6dac88b64f98232ae42d4b6fd2fa81952dfe36a87");
let ip = AddrV2::I2p(
FromHex::from_hex("a2894dabaec08c0051a481a6dac88b64f98232ae42d4b6fd2fa81952dfe36a87")
.unwrap(),
);
- assert_eq!(
- serialize(&ip),
- hex!("0520a2894dabaec08c0051a481a6dac88b64f98232ae42d4b6fd2fa81952dfe36a87")
- );
+ assert_eq!(serialize(&ip), i2p_bytes);
+ assert_eq!(encoding::encode_to_vec(&ip).as_slice(), i2p_bytes);
+ let cjdns_bytes = hex!("0610fc010001000200030004000500060007");
let ip = AddrV2::Cjdns("fc01:1:2:3:4:5:6:7".parse::<Ipv6Addr>().unwrap());
- assert_eq!(serialize(&ip), hex!("0610fc010001000200030004000500060007"));
+ assert_eq!(serialize(&ip), cjdns_bytes);
+ assert_eq!(encoding::encode_to_vec(&ip).as_slice(), cjdns_bytes);
+ let unk_bytes = hex!("aa0401020304");
let ip = AddrV2::Unknown(170, hex!("01020304").to_vec());
- assert_eq!(serialize(&ip), hex!("aa0401020304"));
+ assert_eq!(serialize(&ip), unk_bytes);
+ assert_eq!(encoding::encode_to_vec(&ip).as_slice(), unk_bytes);
}
#[test]
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.