PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Print: the no-listener case refuses through a seam instead of a port it hopes is free
Codex82a1f54cc81b
diff --git a/crates/ipp/src/lib.rs b/crates/ipp/src/lib.rsindex 9f1209df4..79d4f0775 100644--- a/crates/ipp/src/lib.rs+++ b/crates/ipp/src/lib.rs@@ -12,7 +12,7 @@#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]use std::io::{Read, Write};-use std::net::{TcpStream, ToSocketAddrs};+use std::net::{SocketAddr, TcpStream, ToSocketAddrs};use std::path::PathBuf;use std::time::Duration;@@ -142,10 +142,7 @@ impl Endpoint {Endpoint::Tcp(host, port) => {let addr = (host.as_str(), *port).to_socket_addrs().map_err(|e| Error::Unreachable(format!("{host}:{port}: {e}")))?.next();let addr = addr.ok_or_else(|| Error::Unreachable(format!("{host}:{port}: no address")))?;- let s = TcpStream::connect_timeout(&addr, CONNECT).map_err(|e| Error::Unreachable(format!("{host}:{port}: {e}")))?;- let _ = s.set_read_timeout(Some(REQUEST));- let _ = s.set_write_timeout(Some(REQUEST));- Ok(Stream::Tcp(s))+ open_tcp(host, *port, addr, |addr| TcpStream::connect_timeout(&addr, CONNECT))}}}@@ -167,6 +164,16 @@ impl std::fmt::Display for Endpoint {}}+/// Open one address and put the request timeouts on it. The connect call is a parameter so the+/// refusal path can be tested at all: no port is guaranteed to stay free between picking one and+/// using it, and what a refused connect reported is what the caller is supposed to hear.+fn open_tcp(host: &str, port: u16, addr: SocketAddr, open: impl FnOnce(SocketAddr) -> std::io::Result<TcpStream>) -> Result<Stream> {+ let s = open(addr).map_err(|e| Error::Unreachable(format!("{host}:{port}: {e}")))?;+ let _ = s.set_read_timeout(Some(REQUEST));+ let _ = s.set_write_timeout(Some(REQUEST));+ Ok(Stream::Tcp(s))+}+enum Stream {#[cfg(unix)]Unix(std::os::unix::net::UnixStream),diff --git a/crates/ipp/src/tests.rs b/crates/ipp/src/tests.rsindex 0737236c6..091429187 100644--- a/crates/ipp/src/tests.rs+++ b/crates/ipp/src/tests.rs@@ -186,13 +186,36 @@ fn error_statuses_and_broken_replies_are_errors() {assert!(matches!(printers(&ep), Err(Error::Protocol(_))));let (ep, _rx) = fake(b"not ipp at all".to_vec(), false, 200);assert!(matches!(printers(&ep), Err(Error::Protocol(_))));- // Nobody listening. No port is guaranteed free - on a CI VM something can hold port 1 and reset the- // connection - so take one the OS says is free and release it again.- let free = std::net::TcpListener::bind("127.0.0.1:0").unwrap();- let port = free.local_addr().unwrap().port();- drop(free);- let e = printers(&Endpoint::Tcp("127.0.0.1".into(), port)).unwrap_err();+ // Nobody reachable. A port is never guaranteed to be free, let alone to stay free: that is what+ // made the old port-1 case fail on a FreeBSD CI VM. A name that can never resolve (RFC 2606)+ // reaches the same exit without owning or releasing anything.+ let e = printers(&Endpoint::Tcp("printer.invalid".into(), 631)).unwrap_err();assert!(matches!(e, Error::Unreachable(_)), "{e}");+ assert!(e.to_string().contains("printer.invalid"), "{e} does not name the server it tried");+}++#[test]+fn a_refused_connection_is_unreachable_and_keeps_why() {+ // The refused-connect path, with the machine no longer deciding what is closed: the connect call+ // is a parameter, so this test cannot race for an ephemeral port with anything.+ let addr: SocketAddr = "192.0.2.9:631".parse().unwrap();+ let asked_for = std::cell::RefCell::new(None);+ let open = |seen: SocketAddr| -> std::io::Result<TcpStream> {+ *asked_for.borrow_mut() = Some(seen);+ Err(std::io::Error::new(std::io::ErrorKind::ConnectionRefused, "nobody is listening on this port"))+ };+ let e = match open_tcp("printserver", 631, addr, open) {+ Ok(_) => panic!("nothing opens {addr}"),+ Err(e) => e,+ };+ assert_eq!(*asked_for.borrow(), Some(addr), "the connector is handed the resolved address");+ match &e {+ Error::Unreachable(m) => {+ assert!(m.contains("printserver:631"), "{e} does not name the endpoint");+ assert!(m.contains("nobody is listening on this port"), "{e} loses why the connect failed");+ }+ other => panic!("expected Unreachable, got {other}"),+ }}#[test]