zebra_test/net.rs
1//! Network testing utility functions for Zebra.
2
3use std::env;
4
5/// The name of the env var that skips Zebra tests which need reliable,
6/// fast network connectivity.
7///
8/// We use a constant so that the compiler detects typos.
9const SKIP_NETWORK_TESTS: &str = "SKIP_NETWORK_TESTS";
10
11/// The name of the env var that skips Zebra's IPv6 tests.
12///
13/// We use a constant so that the compiler detects typos.
14const SKIP_IPV6_TESTS: &str = "SKIP_IPV6_TESTS";
15
16/// Should we skip Zebra tests which need reliable, fast network connectivity?
17//
18// TODO: separate "good and reliable" from "any network"?
19#[allow(clippy::print_stderr)]
20pub fn zebra_skip_network_tests() -> bool {
21 if env::var_os(SKIP_NETWORK_TESTS).is_some() {
22 // This message is captured by the test runner, use
23 // `cargo test -- --nocapture` to see it.
24 eprintln!("Skipping network test because '$SKIP_NETWORK_TESTS' is set.");
25 return true;
26 }
27
28 false
29}
30
31/// Should we skip Zebra tests which need a local IPv6 network stack and
32/// IPv6 interface addresses?
33///
34/// Since `zebra_skip_network_tests` only disables tests which need reliable network connectivity,
35/// we allow IPv6 tests even when `SKIP_NETWORK_TESTS` is set.
36#[allow(clippy::print_stderr)]
37pub fn zebra_skip_ipv6_tests() -> bool {
38 if env::var_os(SKIP_IPV6_TESTS).is_some() {
39 eprintln!("Skipping IPv6 network test because '$SKIP_IPV6_TESTS' is set.");
40 return true;
41 }
42
43 // TODO: if we separate "good and reliable" from "any network",
44 // also skip IPv6 tests when we're skipping all network tests.
45 false
46}
47
48/// Returns the "magic" port number that tells the operating system to
49/// choose a random unallocated port.
50///
51/// The OS chooses a different port each time it opens a connection or
52/// listener with this magic port number.
53///
54/// Bind the real listener using this port, then query its local address while keeping it open.
55/// Do not probe and release a temporary listener: another process can take the released port.
56pub fn random_unallocated_port() -> u16 {
57 0
58}