node: improve ping/pong tests

Signed-off-by: Slack Coder <slackcoder@server.ky>
This commit is contained in:
Slack Coder 2022-10-18 09:50:21 -05:00 committed by Alexis Sellier
parent 09c438f8fb
commit 10d8519101
No known key found for this signature in database
2 changed files with 55 additions and 2 deletions

View File

@ -40,20 +40,35 @@ use crate::{client, git, identity, rad, service, test};
fn test_ping_response() {
let mut alice = Peer::new("alice", [8, 8, 8, 8], MockStorage::empty());
let bob = Peer::new("bob", [9, 9, 9, 9], MockStorage::empty());
let eve = Peer::new("eve", [7, 7, 7, 7], MockStorage::empty());
alice.connect_to(&bob);
alice.receive(
&bob.addr(),
Message::Ping(Ping {
ponglen: 21,
ponglen: Ping::MAX_PONG_ZEROES,
zeroes: ZeroBytes::new(42),
}),
);
assert_matches!(
alice.messages(&bob.addr()).next(),
Some(Message::Pong { zeroes }) if zeroes.len() == 21,
Some(Message::Pong { zeroes }) if zeroes.len() == Ping::MAX_PONG_ZEROES as usize,
"respond with correctly formatted pong",
);
alice.connect_to(&eve);
alice.receive(
&eve.addr(),
Message::Ping(Ping {
ponglen: Ping::MAX_PONG_ZEROES + 1,
zeroes: ZeroBytes::new(42),
}),
);
assert_matches!(
alice.messages(&eve.addr()).next(),
None,
"ignore unsupported ping message",
);
}
#[test]

View File

@ -376,6 +376,44 @@ mod tests {
use crate::decoder::Decoder;
use crate::wire::{self, Encode};
#[test]
fn test_pingpong_encode_max_size() {
let mut buf = Vec::new();
let ping = Message::Ping(Ping {
ponglen: 0,
zeroes: ZeroBytes::new(Ping::MAX_PING_ZEROES),
});
ping.encode(&mut buf)
.expect("ping should be within max message size");
let pong = Message::Pong {
zeroes: ZeroBytes::new(Ping::MAX_PONG_ZEROES),
};
pong.encode(&mut buf)
.expect("pong should be within max message size");
}
#[test]
fn test_pingpong_encode_size_overflow() {
let ping = Message::Ping(Ping {
ponglen: 0,
zeroes: ZeroBytes::new(Ping::MAX_PING_ZEROES + 1),
});
let mut buf = Vec::new();
ping.encode(&mut buf)
.expect_err("ping should exceed max message size");
let pong = Message::Pong {
zeroes: ZeroBytes::new(Ping::MAX_PONG_ZEROES + 1),
};
let mut buf = Vec::new();
pong.encode(&mut buf)
.expect_err("pong should exceed max message size");
}
#[quickcheck]
fn prop_message_encode_decode(message: Message) {
assert_eq!(