Fix flakiness in `test_tor_connect`
What changed, and why it matters
This commit only changes a test file to make a flaky test more reliable. It replaces hard-coded Google IP addresses with dynamically resolved addresses, so the test does not break when Google's IP addresses change. There is no change to production code and no security issue.
No security action needed. Treat as a normal test reliability improvement.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch modifies lightning-net-tokio/src/lib.rs in the test_tor_connect unit test. Instead of using literal IPv4/IPv6 addresses for google.com, it now resolves google.com:80 via tokio::net::lookup_host, selects one IPv4 and one IPv6 result, and mutates the port for failure cases. This is purely a test-hardening change; no runtime networking logic is altered.
Changed components
lightning-net-tokio/src/lib.rs test suite onlyInspect captured patch +25 / −12
diff --git a/lightning-net-tokio/src/lib.rs b/lightning-net-tokio/src/lib.rs
index ee12966..2e8e568 100644
--- a/lightning-net-tokio/src/lib.rs
+++ b/lightning-net-tokio/src/lib.rs
@@ -1120,6 +1120,19 @@ mod tests {
// Set TOR_PROXY=127.0.0.1:9050
let tor_proxy_addr: SocketAddr = std::env!("TOR_PROXY").parse().unwrap();
+ let mut google_addresses: Vec<_> =
+ tokio::net::lookup_host("google.com:80").await.unwrap().collect();
+ let ipv6_pos = google_addresses
+ .iter()
+ .position(|a| a.is_ipv6())
+ .expect("must resolve at least one ipv6 address");
+ let mut google_ipv6 = google_addresses.remove(ipv6_pos);
+ let ipv4_pos = google_addresses
+ .iter()
+ .position(|a| a.is_ipv4())
+ .expect("must resolve at least one ipv4 address");
+ let mut google_ipv4 = google_addresses.remove(ipv4_pos);
+
struct TestEntropySource;
impl EntropySource for TestEntropySource {
@@ -1132,17 +1145,16 @@ mod tests {
// Success cases
- for addr_str in [
+ for addr in [
// google.com
- "142.250.189.196:80",
+ google_ipv4.into(),
// google.com
- "[2607:f8b0:4005:813::2004]:80",
+ google_ipv6.into(),
// torproject.org
- "torproject.org:80",
+ "torproject.org:80".parse().unwrap(),
// torproject.org
- "2gzyxa5ihm7nsggfxnu52rck2vv4rvmdlkiu3zzui5du4xyclen53wid.onion:80",
+ "2gzyxa5ihm7nsggfxnu52rck2vv4rvmdlkiu3zzui5du4xyclen53wid.onion:80".parse().unwrap(),
] {
- let addr: SocketAddress = addr_str.parse().unwrap();
let tcp_stream = tor_connect(addr, tor_proxy_addr, &entropy_source).await.unwrap();
assert_eq!(
tcp_stream.try_read(&mut [0u8; 1]).unwrap_err().kind(),
@@ -1151,18 +1163,19 @@ mod tests {
}
// Failure cases
+ google_ipv4.set_port(1234);
+ google_ipv6.set_port(1234);
- for addr_str in [
+ for addr in [
// google.com, with some invalid port
- "142.250.189.196:1234",
+ google_ipv4.into(),
// google.com, with some invalid port
- "[2607:f8b0:4005:813::2004]:1234",
+ google_ipv6.into(),
// torproject.org, with some invalid port
- "torproject.org:1234",
+ "torproject.org:1234".parse().unwrap(),
// torproject.org, with a typo
- "3gzyxa5ihm7nsggfxnu52rck2vv4rvmdlkiu3zzui5du4xyclen53wid.onion:80",
+ "3gzyxa5ihm7nsggfxnu52rck2vv4rvmdlkiu3zzui5du4xyclen53wid.onion:80".parse().unwrap(),
] {
- let addr: SocketAddress = addr_str.parse().unwrap();
assert!(tor_connect(addr, tor_proxy_addr, &entropy_source).await.is_err());
}
}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.