Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fragile Address Limit Implementation in Tests #6674

Closed
Tracked by #6277
mpguerra opened this issue May 14, 2023 · 1 comment · Fixed by #6724
Closed
Tracked by #6277

Fragile Address Limit Implementation in Tests #6674

mpguerra opened this issue May 14, 2023 · 1 comment · Fixed by #6724
Assignees
Labels
A-network Area: Network protocol updates or fixes C-audit Category: Issues arising from audit findings C-testing Category: These are tests

Comments

@mpguerra
Copy link
Contributor

mpguerra commented May 14, 2023

In the following code snippet from zebra-network’s AddressBook implementation, the helper function adds a list of peer addresses to the address book. However, it first takes addr_limit elements from the list and then deduplicates it, as such if there are repeated elements in the truncated list, the resulting address book will be smaller than the required limit. It is more robust to deduplicate the list and then take addr_limit elements from it. As this function is only used in tests, it is left as a note:

/// Construct an [`AddressBook`] with the given `local_listener`, `network`,
/// `addr_limit`, [`tracing::Span`], and addresses.
///
/// `addr_limit` is enforced by this method, and by [`AddressBook::update`].
///
/// If there are multiple [`MetaAddr`]s with the same address,
/// an arbitrary address is inserted into the address book,
/// and the rest are dropped.
///
/// This constructor can be used to break address book invariants,
/// so it should only be used in tests.
#[cfg(any(test, feature = "proptest-impl"))]
pub fn new_with_addrs(
local_listener: SocketAddr,
network: Network,
addr_limit: usize,
span: Span,
addrs: impl IntoIterator<Item = MetaAddr>,
) -> AddressBook {
let constructor_span = span.clone();
let _guard = constructor_span.enter();
let instant_now = Instant::now();
let chrono_now = Utc::now();
let mut new_book = AddressBook::new(local_listener, network, span);
new_book.addr_limit = addr_limit;
let addrs = addrs
.into_iter()
.map(|mut meta_addr| {
meta_addr.addr = canonical_socket_addr(meta_addr.addr);
meta_addr
})
.filter(|meta_addr| meta_addr.address_is_valid_for_outbound(network))
.take(addr_limit)
.map(|meta_addr| (meta_addr.addr, meta_addr));
for (socket_addr, meta_addr) in addrs {
// overwrite any duplicate addresses
new_book.by_addr.insert(socket_addr, meta_addr);
}
new_book.update_metrics(instant_now, chrono_now);
new_book
}

@mpguerra mpguerra added this to Zebra May 14, 2023
@github-project-automation github-project-automation bot moved this to 🆕 New in Zebra May 14, 2023
@mpguerra mpguerra added P-Medium ⚡ C-audit Category: Issues arising from audit findings A-network Area: Network protocol updates or fixes C-testing Category: These are tests labels May 14, 2023
@teor2345 teor2345 changed the title Fragile Address Limit Implementation Fragile Address Limit Implementation in Tests May 14, 2023
@mpguerra
Copy link
Contributor Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-network Area: Network protocol updates or fixes C-audit Category: Issues arising from audit findings C-testing Category: These are tests
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants