diff options
| author | tv <tv@krebsco.de> | 2026-11-01 05:22:38 +0100 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-01 05:22:38 +0100 |
| commit | 6bab0c4af15988e3c0d4e38de3ee9c1f8c4a8be2 (patch) | |
| tree | 088b1c5156ce237f0be885f620b28a7d1252b333 /src/transport/libp2p/tests.rs | |
| parent | 35eade856a9b445920ec07eacdeaf153239d8ec9 (diff) | |
libp2p: autoignore unroutable address ranges
Diffstat (limited to 'src/transport/libp2p/tests.rs')
| -rw-r--r-- | src/transport/libp2p/tests.rs | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/transport/libp2p/tests.rs b/src/transport/libp2p/tests.rs index 7311ebf..801dfd3 100644 --- a/src/transport/libp2p/tests.rs +++ b/src/transport/libp2p/tests.rs @@ -33,6 +33,7 @@ fn memory_node_with( upnp: false, ignore: Vec::new(), prefer: Vec::new(), + internet_hosts: Vec::new(), public: false, memory: true, }) @@ -181,6 +182,7 @@ async fn announced_addresses_are_published_instead() { upnp: false, ignore: Vec::new(), prefer: Vec::new(), + internet_hosts: Vec::new(), public: false, memory: true, }) @@ -421,6 +423,7 @@ async fn a_relay_that_is_no_node() { upnp: false, ignore: Vec::new(), prefer: Vec::new(), + internet_hosts: Vec::new(), public: false, memory: true, }) @@ -725,6 +728,52 @@ fn better_paths_explored() { assert_eq!(again, both); } +/// Addresses without a route from here are left out, as if ignored: not kept, nor +/// dialled; and own public ones of a family without a route to the internet (by +/// `--libp2p-internet-hosts`) aren't published. The routing table is asked for real only by `has_route`. +#[test] +fn unroutable_addresses_left_out() { + assert!(has_route("127.0.0.1".parse().unwrap())); + // No IPv6 here. + let mut book = Book { + routes: Routes::with_probe(|ip| ip.is_ipv4()), + listening: vec![ + "/ip4/192.168.1.2/tcp/9661".parse().unwrap(), + "/ip6/2a0a:4cc0::2/tcp/9661".parse().unwrap(), + ], + ..Book::default() + }; + let them = peer(); + let v4: Multiaddr = "/ip4/192.168.1.7/tcp/9661".parse().unwrap(); + let v6: Multiaddr = "/ip6/2a0a:4cc0::7/tcp/9661".parse().unwrap(); + book.identified(them, vec![v4.clone(), v6.clone()]); + assert!(book.knows(them, &[v4])); + assert!(!book.knows(them, &[v6])); + // Nobody asked whether there's a route to the internet: everything is published. + assert_eq!(book.direct().len(), 2); + book.routes.internet(&[ + "1.1.1.1".parse().unwrap(), + "2606:4700:4700::1111".parse().unwrap(), + ]); + assert_eq!( + book.direct(), + ["/ip4/192.168.1.2/tcp/9661".parse::<Multiaddr>().unwrap()] + ); +} + +/// `--libp2p-internet-hosts` takes addresses as they are (names are resolved). +#[tokio::test] +async fn internet_hosts_resolved() { + let resolved = resolve_internet_hosts(&["1.1.1.1".to_owned(), "::1".to_owned()]).await; + assert_eq!( + resolved, + [ + "1.1.1.1".parse::<std::net::IpAddr>().unwrap(), + "::1".parse().unwrap() + ] + ); +} + /// Peers nobody mentions any more are forgotten, but connected ones. #[test] fn stale_peers_are_forgotten() { |
