From 63672a263397417252d7ab14dc1c78167391f5e8 Mon Sep 17 00:00:00 2001 From: Janito Vaqueiro Ferreira Filho Date: Tue, 25 May 2021 23:41:31 +0000 Subject: [PATCH] Test underflow handling If the calculation to apply the compensation offset overflows or underflows, the reported times are too distant apart, and could be sent on purpose by a malicious peer, so all addresses from that peer should be rejected. --- .../src/peer_set/candidate_set/tests/vectors.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/zebra-network/src/peer_set/candidate_set/tests/vectors.rs b/zebra-network/src/peer_set/candidate_set/tests/vectors.rs index 19e4855b..38bdc5af 100644 --- a/zebra-network/src/peer_set/candidate_set/tests/vectors.rs +++ b/zebra-network/src/peer_set/candidate_set/tests/vectors.rs @@ -99,6 +99,22 @@ fn doesnt_offsets_if_most_recent_last_seen_times_is_exactly_the_limit() { assert_eq!(validated_peers, expected_peers); } +/// Rejects all addresses if underflow occurs when applying the offset. +#[test] +fn rejects_all_addresses_if_applying_offset_causes_an_underflow() { + let last_seen_limit = DateTime32::now(); + + let input_peers = mock_gossiped_peers(vec![ + DateTime32::from(u32::MIN).to_chrono(), + last_seen_limit.to_chrono(), + DateTime32::from(u32::MAX).to_chrono(), + ]); + + let validated_peers: Vec<_> = validate_addrs(input_peers.clone(), last_seen_limit).collect(); + + assert!(validated_peers.is_empty()); +} + /// Create a mock list of gossiped [`MetaAddr`]s with the specified `last_seen_times`. /// /// The IP address and port of the generated ports should not matter for the test.