Fix zero port error (#5021)

* Fix zero port error and add tests

* Tweak indentation

* assign 0 to quic_port if tcp_port == 0

* Remove unnecessary deps
This commit is contained in:
Akihito Nakano 2024-01-16 10:19:49 +09:00 committed by GitHub
parent 0613eb7a21
commit e10e4b7811
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 109 additions and 6 deletions

View File

@ -973,13 +973,13 @@ pub fn parse_listening_addresses(
.then(unused_port::unused_udp6_port)
.transpose()?
.or(maybe_disc_port)
.unwrap_or(port);
.unwrap_or(tcp_port);
let quic_port = use_zero_ports
.then(unused_port::unused_udp6_port)
.transpose()?
.or(maybe_quic_port)
.unwrap_or(port + 1);
.unwrap_or(if tcp_port == 0 { 0 } else { tcp_port + 1 });
ListenAddress::V6(lighthouse_network::ListenAddr {
addr: ipv6,
@ -1002,14 +1002,14 @@ pub fn parse_listening_addresses(
.then(unused_port::unused_udp4_port)
.transpose()?
.or(maybe_disc_port)
.unwrap_or(port);
.unwrap_or(tcp_port);
// use zero ports if required. If not, use the specific quic port. If none given, use
// the tcp port + 1.
let quic_port = use_zero_ports
.then(unused_port::unused_udp4_port)
.transpose()?
.or(maybe_quic_port)
.unwrap_or(port + 1);
.unwrap_or(if tcp_port == 0 { 0 } else { tcp_port + 1 });
ListenAddress::V4(lighthouse_network::ListenAddr {
addr: ipv4,
@ -1032,7 +1032,11 @@ pub fn parse_listening_addresses(
.then(unused_port::unused_udp4_port)
.transpose()?
.or(maybe_quic_port)
.unwrap_or(port + 1);
.unwrap_or(if ipv4_tcp_port == 0 {
0
} else {
ipv4_tcp_port + 1
});
// Defaults to 9090 when required
let ipv6_tcp_port = use_zero_ports
@ -1048,7 +1052,11 @@ pub fn parse_listening_addresses(
.then(unused_port::unused_udp6_port)
.transpose()?
.or(maybe_quic6_port)
.unwrap_or(ipv6_tcp_port + 1);
.unwrap_or(if ipv6_tcp_port == 0 {
0
} else {
ipv6_tcp_port + 1
});
ListenAddress::DualStack(
lighthouse_network::ListenAddr {

View File

@ -859,6 +859,23 @@ fn network_port_flag_over_ipv4() {
listen_addr.quic_port,
listen_addr.tcp_port
)),
// quic_port should be 0 if tcp_port is given as 0.
Some((port, 0, port))
);
});
let port = 9000;
CommandLineTest::new()
.flag("port", Some(port.to_string().as_str()))
.run()
.with_config(|config| {
assert_eq!(
config.network.listen_addrs().v4().map(|listen_addr| (
listen_addr.disc_port,
listen_addr.quic_port,
listen_addr.tcp_port
)),
// quic_port should be (tcp_port + 1) if tcp_port is given as non-zero.
Some((port, port + 1, port))
);
});
@ -877,11 +894,89 @@ fn network_port_flag_over_ipv6() {
listen_addr.quic_port,
listen_addr.tcp_port
)),
// quic_port should be 0 if tcp_port is given as 0.
Some((port, 0, port))
);
});
let port = 9000;
CommandLineTest::new()
.flag("listen-address", Some("::1"))
.flag("port", Some(port.to_string().as_str()))
.run()
.with_config(|config| {
assert_eq!(
config.network.listen_addrs().v6().map(|listen_addr| (
listen_addr.disc_port,
listen_addr.quic_port,
listen_addr.tcp_port
)),
// quic_port should be (tcp_port + 1) if tcp_port is given as non-zero.
Some((port, port + 1, port))
);
});
}
#[test]
fn network_port_flag_over_ipv4_and_ipv6() {
let port = 0;
let port6 = 0;
CommandLineTest::new()
.flag("listen-address", Some("127.0.0.1"))
.flag("listen-address", Some("::1"))
.flag("port", Some(port.to_string().as_str()))
.flag("port6", Some(port6.to_string().as_str()))
.run()
.with_config(|config| {
assert_eq!(
config.network.listen_addrs().v4().map(|listen_addr| (
listen_addr.disc_port,
listen_addr.quic_port,
listen_addr.tcp_port
)),
// quic_port should be 0 if tcp_port is given as 0.
Some((port, 0, port))
);
assert_eq!(
config.network.listen_addrs().v6().map(|listen_addr| (
listen_addr.disc_port,
listen_addr.quic_port,
listen_addr.tcp_port
)),
// quic_port should be 0 if tcp_port is given as 0.
Some((port6, 0, port6))
);
});
let port = 19000;
let port6 = 29000;
CommandLineTest::new()
.flag("listen-address", Some("127.0.0.1"))
.flag("listen-address", Some("::1"))
.flag("port", Some(port.to_string().as_str()))
.flag("port6", Some(port6.to_string().as_str()))
.run()
.with_config(|config| {
assert_eq!(
config.network.listen_addrs().v4().map(|listen_addr| (
listen_addr.disc_port,
listen_addr.quic_port,
listen_addr.tcp_port
)),
// quic_port should be (tcp_port + 1) if tcp_port is given as non-zero.
Some((port, port + 1, port))
);
assert_eq!(
config.network.listen_addrs().v6().map(|listen_addr| (
listen_addr.disc_port,
listen_addr.quic_port,
listen_addr.tcp_port
)),
// quic_port should be (tcp_port + 1) if tcp_port is given as non-zero.
Some((port6, port6 + 1, port6))
);
});
}
#[test]
fn network_port_and_discovery_port_flags_over_ipv4() {
let tcp4_port = 0;
let disc4_port = 0;