Test if validation offsets times in the future

Use some mock gossiped peers that all have `last_seen` times in the
future and check that they all have a specific offset applied to them.
This commit is contained in:
Janito Vaqueiro Ferreira Filho 2021-05-21 21:52:36 +00:00
parent 82452621e0
commit 3c9c920bbd
3 changed files with 62 additions and 0 deletions

View File

@ -8,6 +8,9 @@ use zebra_chain::serialization::DateTime32;
use crate::{constants, types::MetaAddr, AddressBook, BoxError, Request, Response};
#[cfg(test)]
mod tests;
/// The `CandidateSet` manages the `PeerSet`'s peer reconnection attempts.
///
/// It divides the set of all possible candidate peers into disjoint subsets,

View File

@ -0,0 +1,3 @@
//! [`CandidateSet`] tests.
mod vectors;

View File

@ -0,0 +1,56 @@
use std::{
convert::TryInto,
net::{IpAddr, SocketAddr},
};
use chrono::{DateTime, Duration, Utc};
use zebra_chain::serialization::DateTime32;
use super::super::validate_addrs;
use crate::types::{MetaAddr, PeerServices};
/// Test that offset is applied when all addresses have `last_seen` times in the future.
#[test]
fn offsets_last_seen_times_in_the_future() {
let last_seen_limit = DateTime32::now();
let last_seen_limit_chrono = last_seen_limit.to_chrono();
let input_peers = mock_gossiped_peers(vec![
last_seen_limit_chrono + Duration::minutes(30),
last_seen_limit_chrono + Duration::minutes(15),
last_seen_limit_chrono + Duration::minutes(45),
]);
let validated_peers: Vec<_> = validate_addrs(input_peers, last_seen_limit).collect();
let expected_offset = Duration::minutes(45);
let expected_peers = mock_gossiped_peers(vec![
last_seen_limit_chrono + Duration::minutes(30) - expected_offset,
last_seen_limit_chrono + Duration::minutes(15) - expected_offset,
last_seen_limit_chrono + Duration::minutes(45) - expected_offset,
]);
assert_eq!(validated_peers, expected_peers);
}
/// 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.
fn mock_gossiped_peers(last_seen_times: impl IntoIterator<Item = DateTime<Utc>>) -> Vec<MetaAddr> {
last_seen_times
.into_iter()
.enumerate()
.map(|(index, last_seen_chrono)| {
let last_seen = last_seen_chrono
.try_into()
.expect("`last_seen` time doesn't fit in a `DateTime32`");
MetaAddr::new_gossiped_meta_addr(
SocketAddr::new(IpAddr::from([192, 168, 1, index as u8]), 20_000),
PeerServices::NODE_NETWORK,
last_seen,
)
})
.collect()
}