Test that offset is applied to all gossiped peers

Use some mock gossiped peers where some have `last_seen` times in the
past and some have times in the future. Check that all the peers have
an offset applied to them by the `validate_addrs` function.

This tests if the offset is applied to all peers that a malicious peer
gossiped to us.
This commit is contained in:
Janito Vaqueiro Ferreira Filho 2021-05-21 21:56:37 +00:00
parent 60f660e53f
commit f4a7026aa3
1 changed files with 27 additions and 0 deletions

View File

@ -53,6 +53,33 @@ fn doesnt_offset_last_seen_times_in_the_past() {
assert_eq!(validated_peers, expected_peers);
}
/// Test that offset is applied to all the addresses if at least one has a `last_seen` time in the
/// future.
///
/// Times that are in the past should be changed as well.
#[test]
fn offsets_all_last_seen_times_if_one_is_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(55),
last_seen_limit_chrono - Duration::days(3),
last_seen_limit_chrono - Duration::hours(2),
]);
let validated_peers: Vec<_> = validate_addrs(input_peers, last_seen_limit).collect();
let expected_offset = Duration::minutes(55);
let expected_peers = mock_gossiped_peers(vec![
last_seen_limit_chrono + Duration::minutes(55) - expected_offset,
last_seen_limit_chrono - Duration::days(3) - expected_offset,
last_seen_limit_chrono - Duration::hours(2) - 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.