Skip to content

Commit

Permalink
Test deserialized IPv4 and IPv6 values in addr messages
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 committed Nov 11, 2021
1 parent 4b30aca commit f246cbc
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 16 deletions.
96 changes: 94 additions & 2 deletions zebra-network/src/protocol/external/tests/vectors.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
use std::io::{self, Write};
//! Fixed test vectors for external protocol messages.
use std::{
convert::TryInto,
io::{self, Write},
};

use byteorder::{LittleEndian, WriteBytesExt};

use chrono::{DateTime, Utc};
use zebra_chain::serialization::ZcashDeserializeInto;

use crate::protocol::external::{Codec, InventoryHash, Message};
use crate::{
meta_addr::MetaAddr,
protocol::external::{types::PeerServices, Codec, InventoryHash, Message},
};

/// Test if deserializing [`InventoryHash::Wtx`] does not produce an error.
#[test]
Expand All @@ -29,6 +38,8 @@ fn parses_msg_wtx_inventory_type() {

/// Test that deserializing [`AddrV1`] into [`MetaAddr`] succeeds,
/// and produces the expected number of addresses.
///
/// Also checks some of the deserialized address values.
#[test]
fn parses_msg_addr_v1_ip() {
zebra_test::init();
Expand Down Expand Up @@ -58,6 +69,33 @@ fn parses_msg_addr_v1_ip() {
case_idx,
addrs
);

// Check all the fields in the first test case
if case_idx == 0 {
assert_eq!(
addrs,
vec![
MetaAddr::new_gossiped_meta_addr(
"[::1]:0".parse().unwrap(),
PeerServices::empty(),
DateTime::parse_from_rfc3339("2009-01-09T02:54:25+00:00")
.unwrap()
.with_timezone(&Utc)
.try_into()
.unwrap(),
),
MetaAddr::new_gossiped_meta_addr(
"[::1]:241".parse().unwrap(),
PeerServices::NODE_NETWORK,
DateTime::parse_from_rfc3339("2039-11-22T11:22:33+00:00")
.unwrap()
.with_timezone(&Utc)
.try_into()
.unwrap(),
),
],
);
}
} else {
panic!(
"unexpected message variant in case {}: {:?}",
Expand Down Expand Up @@ -103,6 +141,8 @@ fn parses_msg_addr_v1_empty() {

/// Test that deserializing [`AddrV2`] into [`MetaAddr`] succeeds,
/// and produces the expected number of addresses.
///
/// Also checks some of the deserialized address values.
#[test]
fn parses_msg_addr_v2_ip() {
zebra_test::init();
Expand Down Expand Up @@ -132,6 +172,58 @@ fn parses_msg_addr_v2_ip() {
case_idx,
addrs
);

// Check all the fields in the IPv4 and IPv6 test cases
if case_idx == 0 {
assert_eq!(
addrs,
vec![
MetaAddr::new_gossiped_meta_addr(
"[::1]:0".parse().unwrap(),
PeerServices::empty(),
DateTime::parse_from_rfc3339("2009-01-09T02:54:25+00:00")
.unwrap()
.with_timezone(&Utc)
.try_into()
.unwrap(),
),
MetaAddr::new_gossiped_meta_addr(
"[::1]:241".parse().unwrap(),
PeerServices::NODE_NETWORK,
DateTime::parse_from_rfc3339("2039-11-22T11:22:33+00:00")
.unwrap()
.with_timezone(&Utc)
.try_into()
.unwrap(),
),
// torv3 is unsupported, so it's not in the parsed list
],
);
} else if case_idx == 1 {
assert_eq!(
addrs,
vec![
MetaAddr::new_gossiped_meta_addr(
"127.0.0.1:1".parse().unwrap(),
PeerServices::NODE_NETWORK,
DateTime::parse_from_rfc3339("2039-11-22T11:22:33+00:00")
.unwrap()
.with_timezone(&Utc)
.try_into()
.unwrap(),
),
MetaAddr::new_gossiped_meta_addr(
"[::1]:241".parse().unwrap(),
PeerServices::NODE_NETWORK,
DateTime::parse_from_rfc3339("2039-11-22T11:22:33+00:00")
.unwrap()
.with_timezone(&Utc)
.try_into()
.unwrap(),
),
],
);
}
} else {
panic!(
"unexpected message variant in case {}: {:?}",
Expand Down
28 changes: 14 additions & 14 deletions zebra-test/src/network_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,6 @@ lazy_static! {

// Extra test cases:
//
// all services flags set
<Vec<u8>>::from_hex(
concat!(
"01", // number of entries

"79627683", // time, Tue Nov 22 11:22:33 UTC 2039
"ffffffffffffffffff", // service flags, COMPACTSIZE(all flags set)
"02", // network id, IPv6
"10", // address length, COMPACTSIZE(16)
"00000000000000000000000000000001", // address
"0000", // port
)
).expect("Message bytes are in valid hex representation"),

// IPv4
<Vec<u8>>::from_hex(
concat!(
Expand All @@ -157,6 +143,20 @@ lazy_static! {
)
).expect("Message bytes are in valid hex representation"),

// all services flags set
<Vec<u8>>::from_hex(
concat!(
"01", // number of entries

"79627683", // time, Tue Nov 22 11:22:33 UTC 2039
"ffffffffffffffffff", // service flags, COMPACTSIZE(all flags set)
"02", // network id, IPv6
"10", // address length, COMPACTSIZE(16)
"00000000000000000000000000000001", // address
"0000", // port
)
).expect("Message bytes are in valid hex representation"),

// Unknown Network ID: address within typical size range
<Vec<u8>>::from_hex(
concat!(
Expand Down

0 comments on commit f246cbc

Please sign in to comment.