diff --git a/Cargo.lock b/Cargo.lock index 2f99144..4731bc7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,15 +11,6 @@ dependencies = [ "memchr", ] -[[package]] -name = "android_system_properties" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae221649c9976a6f6c56ae1facf410f3ddb33cc661c4b7b61020a912d4237fbc" -dependencies = [ - "libc", -] - [[package]] name = "anstream" version = "1.0.0" @@ -82,24 +73,12 @@ version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" -[[package]] -name = "autocfg" -version = "1.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" - [[package]] name = "bitflags" version = "2.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" -[[package]] -name = "bumpalo" -version = "3.20.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649" - [[package]] name = "bytes" version = "1.12.1" @@ -133,18 +112,6 @@ dependencies = [ "rand_core 0.10.1", ] -[[package]] -name = "chrono" -version = "0.4.45" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1aa79e62e7697b8e29b513a68abacf485adcd1fe8284a4316c5ae868e6633327" -dependencies = [ - "iana-time-zone", - "num-traits", - "serde", - "windows-link", -] - [[package]] name = "clap" version = "4.6.7" @@ -238,6 +205,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "deranged" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e9de72ce2ad1f90dc62fa25f0f430ef85eb4b0d8fa0be4f30373bc40a21d28e" + [[package]] name = "displaydoc" version = "0.2.7" @@ -262,7 +235,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -537,30 +510,6 @@ dependencies = [ "tracing", ] -[[package]] -name = "iana-time-zone" -version = "0.1.65" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e31bc9ad994ba00e440a8aa5c9ef0ec67d5cb5e5cb0cc7f8b744a35b389cc470" -dependencies = [ - "android_system_properties", - "core-foundation-sys", - "iana-time-zone-haiku", - "js-sys", - "log", - "wasm-bindgen", - "windows-core", -] - -[[package]] -name = "iana-time-zone-haiku" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" -dependencies = [ - "cc", -] - [[package]] name = "icu_collections" version = "2.2.0" @@ -686,17 +635,6 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" -[[package]] -name = "js-sys" -version = "0.3.103" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "53b44bfcdb3f8d5837a46dae1ca9660a837176eee74a28b229bc626816589102" -dependencies = [ - "cfg-if", - "futures-util", - "wasm-bindgen", -] - [[package]] name = "libc" version = "0.2.191" @@ -709,7 +647,6 @@ version = "1.2.1" dependencies = [ "anyhow", "bytes", - "chrono", "clap", "csv", "futures-util", @@ -731,6 +668,7 @@ dependencies = [ "serde_json", "socket2", "surge-ping", + "time", "tokio", "tokio-native-tls", "tower-service", @@ -805,12 +743,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "43794a0ace135be66a25d3ae77d41b91615fb68ae937f904090203e81f755b65" [[package]] -name = "num-traits" -version = "0.2.19" +name = "num-conv" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "521739c6d2bac4aa25192232afe6841231376b2b26d4d9fae5ecf8ca5772e441" + +[[package]] +name = "num_threads" +version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +checksum = "cabcc654086861f9e1bc5115258de62ef92c79b20f4615a1b0cc57c4e82027d4" dependencies = [ - "autocfg", + "libc", ] [[package]] @@ -960,6 +904,12 @@ dependencies = [ "zerovec", ] +[[package]] +name = "powerfmt" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a6394b9e965e73d0a289ee54f589087e2c676aedf60885baf52c76b771e4958" + [[package]] name = "ppv-lite86" version = "0.2.21" @@ -1107,7 +1057,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -1157,12 +1107,6 @@ dependencies = [ "untrusted", ] -[[package]] -name = "rustversion" -version = "1.0.23" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" - [[package]] name = "ryu" version = "1.0.23" @@ -1365,7 +1309,7 @@ dependencies = [ "getrandom 0.4.3", "once_cell", "rustix", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -1398,6 +1342,27 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "time" +version = "0.3.55" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdb87b95ec50ddfa440816d227a17b2ccbdda963a316a727fda0fc4334f7d134" +dependencies = [ + "deranged", + "libc", + "num-conv", + "num_threads", + "powerfmt", + "serde_core", + "time-core", +] + +[[package]] +name = "time-core" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e1c906769ad99c88eaa54e728060edef082f8e358ff32030cb7c7d315e81109" + [[package]] name = "tinystr" version = "0.8.5" @@ -1587,110 +1552,12 @@ dependencies = [ "wit-bindgen", ] -[[package]] -name = "wasm-bindgen" -version = "0.2.126" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4b067c0c11094aef6b7a801c1e34a26affafdf3d051dba08456b868789aaf9a4" -dependencies = [ - "cfg-if", - "once_cell", - "rustversion", - "wasm-bindgen-macro", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-macro" -version = "0.2.126" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "167ce5e579f6bcf889c4f7175a8a5a585de84e8ff93976ce393efa5f2837aab1" -dependencies = [ - "quote", - "wasm-bindgen-macro-support", -] - -[[package]] -name = "wasm-bindgen-macro-support" -version = "0.2.126" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3997c7839262f4ef12cf90b818d6340c18e80f263f1a94bf157d0ec4420380e" -dependencies = [ - "bumpalo", - "proc-macro2", - "quote", - "syn 2.0.119", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-shared" -version = "0.2.126" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc1b4cb0cc549fcf58d7dfc081778139b3d283a081644e833e84682ad71cea24" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "windows-core" -version = "0.62.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb" -dependencies = [ - "windows-implement", - "windows-interface", - "windows-link", - "windows-result", - "windows-strings", -] - -[[package]] -name = "windows-implement" -version = "0.60.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.119", -] - -[[package]] -name = "windows-interface" -version = "0.59.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.119", -] - [[package]] name = "windows-link" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" -[[package]] -name = "windows-result" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5" -dependencies = [ - "windows-link", -] - -[[package]] -name = "windows-strings" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091" -dependencies = [ - "windows-link", -] - [[package]] name = "windows-sys" version = "0.52.0" diff --git a/Cargo.toml b/Cargo.toml index bcbd56e..9f3dfc6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -64,7 +64,6 @@ http2 = ["hyper/http2", "hyper-util/http2", "hyper-rustls?/http2"] [dependencies] anyhow = "1" bytes = "1" -chrono = { version = "0.4", default-features = false, features = ["std", "clock", "serde"] } clap = { version = "4", features = ["derive", "string", "wrap_help"] } csv = "1" futures-util = { version = "0.3", default-features = false, features = ["std"] } @@ -89,6 +88,11 @@ tokio-native-tls = { version = "0.3", optional = true } serde_json = { version = "1", features = ["raw_value"] } socket2 = { version = "0.6", features = ["all"] } surge-ping = "0.8" +# The local time for report timestamps and the telemetry log, which +# local-offset asks the C library for on Unix and the system for on Windows. +# Before 0.3.37 it refused on Linux once a second thread ran, as one always +# does here, and every timestamp fell back to UTC. +time = { version = "0.3.37", default-features = false, features = ["std", "local-offset"] } tokio = { version = "1", features = ["rt-multi-thread", "macros", "net", "time", "io-util", "sync", "fs", "signal"] } tower-service = "0.3" url = "2" @@ -101,7 +105,7 @@ strip = true panic = "abort" [build-dependencies] -chrono = { version = "0.4", default-features = false, features = ["std", "clock"] } +time = { version = "0.3", default-features = false, features = ["std"] } [dev-dependencies] # Unicode data to check the sanitizer's own table against. Already in the diff --git a/README.md b/README.md index 1c49e0d..e6d0b37 100644 --- a/README.md +++ b/README.md @@ -22,9 +22,14 @@ a Rust port of the Go [librespeed-cli](https://github.com/librespeed/speedtest-c | Linux binaries | [Releases](https://github.com/librespeed/speedtest-cli-rust/releases) for x86_64, aarch64, armv7 and arm (musl) | | From source | `cargo build --release`, the binary is `target/release/librespeed-cli` | -Set `SOURCE_DATE_EPOCH` for a reproducible build date in `--version`. It -fixes only the date and does not make the binary bit-for-bit reproducible, see -[RELEASING.md](RELEASING.md#how-to-release). +Set `SOURCE_DATE_EPOCH` for a reproducible build date in `--version`. A value +other than decimal seconds from 1970 to the end of 9999, an empty one included, +fails the build. It fixes only the date and does not make the binary +bit-for-bit reproducible, see [RELEASING.md](RELEASING.md#how-to-release). + +The crate is published for the command. Its library exists so the binary, the +tests and the fuzz targets can share code, and its API can change in any +release, a minor or patch one included. ## Usage @@ -136,9 +141,10 @@ deliberately different. - **CSV formulas are defused.** A field starting with `=`, `+`, `-` or `@` gets a leading single quote, because spreadsheets evaluate what is left after unquoting. -- **Response sizes are capped.** 8 MiB for a server list or a getIP answer, - 64 KiB for a telemetry reply and 8 KiB for the check that a backend is up. - The cap bounds memory, not time, and `--timeout 0` leaves no time bound. +- **Response sizes are capped.** Go reads a server list, a getIP answer and a + telemetry reply whole. This client stops at 8 MiB for the first two and at + 64 KiB for the third. The cap bounds memory, not time, and `--timeout 0` + leaves no time bound. - **A redirect may not leave https for http.** It is an error here. Go follows it and only leaves out the Referer. - **A request body is not sent on to another origin.** A 307 or 308 that would @@ -177,6 +183,9 @@ deliberately different. connection or a JSON syntax error, reads differently. - **A server URL's scheme keeps its case.** Go lowercases `HTTP://` when it parses the list. +- **A POSIX rule in `TZ` sets the clock.** With `TZ=CET-1CEST,M3.5.0,M10.5.0/3` + the report timestamps and the telemetry log follow the rule, as 1.2.1 did. + Go takes only a zone name or file from `TZ` and writes UTC for a rule. - **The process exits rather than returning from `main`.** On 32-bit PowerPC musl, dropping the tokio runtime never finished and every run hung after its output. `main` ends with `process::exit(0)` as a workaround. Output is flushed diff --git a/build.rs b/build.rs index d1f4630..753e41b 100644 --- a/build.rs +++ b/build.rs @@ -3,19 +3,42 @@ use std::process::Command; fn main() { + println!("cargo:rerun-if-env-changed=SOURCE_DATE_EPOCH"); + println!("cargo:rerun-if-env-changed=LIBRESPEED_REVISION"); + // SOURCE_DATE_EPOCH keeps reproducible builds reproducible; OpenWrt and // most distro packaging set it. Computing the date here rather than // shelling out to `date` avoids depending on GNU vs BSD flag differences. - let date = match std::env::var("SOURCE_DATE_EPOCH") - .ok() - .and_then(|epoch| epoch.parse::().ok()) - .and_then(|epoch| chrono::DateTime::from_timestamp(epoch, 0)) - { - Some(dt) => dt.format("%Y-%m-%d %H:%M:%S UTC").to_string(), - None => chrono::Utc::now() - .format("%Y-%m-%d %H:%M:%S UTC") - .to_string(), + // A value that is not the decimal digits of a time in seconds up to the + // end of 9999, an empty or signed one included, fails the build, as the + // specification asks and as clang does, rather than giving it the date of + // today. + let at = match std::env::var_os("SOURCE_DATE_EPOCH") { + None => time::OffsetDateTime::now_utc(), + Some(epoch) => match epoch + .to_str() + .filter(|epoch| epoch.bytes().all(|b| b.is_ascii_digit())) + .and_then(|epoch| epoch.parse::().ok()) + .and_then(|epoch| time::OffsetDateTime::from_unix_timestamp(epoch).ok()) + { + Some(at) => at, + None => { + println!( + "cargo::error=SOURCE_DATE_EPOCH is {epoch:?}, which is not a non-negative decimal integer up to 253402300799" + ); + return; + } + }, }; + let date = format!( + "{:04}-{:02}-{:02} {:02}:{:02}:{:02} UTC", + at.year(), + u8::from(at.month()), + at.day(), + at.hour(), + at.minute(), + at.second() + ); // The build date alone cannot identify a build: distro packaging derives // SOURCE_DATE_EPOCH from the package, so two builds of different commits @@ -37,6 +60,4 @@ fn main() { println!("cargo:rustc-env=LIBRESPEED_BUILD_DATE={date}"); println!("cargo:rustc-env=LIBRESPEED_REVISION={revision}"); - println!("cargo:rerun-if-env-changed=SOURCE_DATE_EPOCH"); - println!("cargo:rerun-if-env-changed=LIBRESPEED_REVISION"); } diff --git a/src/defs/server.rs b/src/defs/server.rs index 4c668a0..7fd7cc4 100644 --- a/src/defs/server.rs +++ b/src/defs/server.rs @@ -141,7 +141,12 @@ impl Server { )); match result { - Ok((status, body, facts)) => { + Ok(crate::http::Prefix { + status, + body, + body_error, + facts, + }) => { // Say what the connection settled on. On hardware without AES // acceleration the cipher, not the link, is what bounds the // result, and under TLS 1.3 the server picks it from the set @@ -158,33 +163,16 @@ impl Server { (None, _) => write_debug!(out, "Connection is not encrypted\n"), } - if !body.is_empty() { + // A body that broke off is quoted too, with what it got, as + // the Go client does. + if body_error.is_some() || !body.is_empty() { write_debug!( out, "Failed when parsing get IP result: {}\n", GoQuote(&body) ); } - // Status first: a web server's 404 or 500 comes with an error - // page, and the status names that fault better than the body. - let down_reason = if status != StatusCode::OK { - let code = status.as_u16(); - Some(match go_status_text(status) { - Some(text) => format!("the ping request returned HTTP {code} {text}"), - None => format!("the ping request returned HTTP {code}"), - }) - } else if !body.is_empty() { - Some("the ping request returned a non-empty body".to_string()) - } else { - None - }; - ServerStatus { - up: down_reason.is_none(), - down_reason, - // As before, a probe answered with a body reports nothing - // about its connection. - tls: facts.tls.filter(|_| body.is_empty()), - } + ServerStatus::of_answer(status, &body, body_error, facts.tls) } Err(e) => { write_debug!(out, "Error checking for server status: {e:#}\n"); @@ -648,23 +636,60 @@ fn go_status_text(status: StatusCode) -> Option<&'static str> { } /// Whether a backend answered its probe, and what that connection negotiated. -#[derive(Debug, Default)] +#[derive(Debug)] pub struct ServerStatus { - pub up: bool, - /// Why the backend is down, when it is: the refused connection, the - /// unreachable network, the wrong status or a body where none belongs. + /// Why the backend is down, `None` when it is up: the refused connection, + /// the unreachable network, the wrong status, a body where none belongs or + /// a body that broke off. pub down_reason: Option, + /// What the probe's connection negotiated. Kept whatever the status, but + /// not for an answer that carried a body or broke off. pub tls: Option, } impl ServerStatus { + /// Whether the backend answered its probe as one that is up. + pub fn up(&self) -> bool { + self.down_reason.is_none() + } + fn down(reason: String) -> Self { ServerStatus { - up: false, down_reason: Some(reason), tls: None, } } + + /// What a probe's answer says about its backend, decided as Go's + /// `CheckUp` decides it. + fn of_answer( + status: StatusCode, + body: &[u8], + body_error: Option, + tls: Option, + ) -> Self { + let complete_and_empty = body_error.is_none() && body.is_empty(); + // Status first: a web server's 404 or 500 comes with an error page, + // and the status names that fault better than the body, or than a + // body that broke off. + let down_reason = if status != StatusCode::OK { + let code = status.as_u16(); + Some(match go_status_text(status) { + Some(text) => format!("the ping request returned HTTP {code} {text}"), + None => format!("the ping request returned HTTP {code}"), + }) + } else if let Some(e) = body_error { + Some(format!("reading the ping response: {e:#}")) + } else if !body.is_empty() { + Some("the ping request returned a non-empty body".to_string()) + } else { + None + }; + ServerStatus { + down_reason, + tls: tls.filter(|_| complete_and_empty), + } + } } /// A running `--json-stream` progress ticker. @@ -983,6 +1008,30 @@ mod tests { assert_eq!(Server::default().sponsor(Output::default()), ""); } + /// The probe's connection is reported whatever the status, but not for an + /// answer that carried a body or broke off. + #[test] + fn a_probe_reports_its_connection_unless_its_answer_had_a_body_or_broke_off() { + let broke = || Some(anyhow::anyhow!("unexpected EOF")); + for (status, body, body_error, up, tls) in [ + (StatusCode::OK, &b""[..], None, true, true), + (StatusCode::NOT_FOUND, b"", None, false, true), + (StatusCode::OK, b"x", None, false, false), + (StatusCode::NOT_FOUND, b"x", None, false, false), + (StatusCode::OK, b"", broke(), false, false), + (StatusCode::NOT_FOUND, b"x", broke(), false, false), + ] { + let case = format!("{status} {body:?} {body_error:?}"); + let facts = crate::http::TlsFacts { + version: "TLSv1.3".into(), + cipher: "TLS_AES_128_GCM_SHA256".into(), + }; + let s = ServerStatus::of_answer(status, body, body_error, Some(facts)); + assert_eq!(s.up(), up, "{case}"); + assert_eq!(s.tls.is_some(), tls, "{case}"); + } + } + // Go's net/http status table, where it differs from the http crate's, a // few where they agree, and a code neither knows. #[test] diff --git a/src/defs/telemetry.rs b/src/defs/telemetry.rs index 0f1d36b..4740c58 100644 --- a/src/defs/telemetry.rs +++ b/src/defs/telemetry.rs @@ -36,10 +36,7 @@ impl TelemetryLog { } fn push(&self, prefix: &str, msg: &str) { - let now = chrono::Local::now(); - // Close to Go's `time.Time.String()` layout. The zone abbreviation Go - // prints last is omitted: it would need a tz database to resolve. - let stamp = now.format("%Y-%m-%d %H:%M:%S%.f %z"); + let stamp = log_stamp(crate::report::local_now()); self.content .lock() .unwrap() @@ -81,6 +78,41 @@ impl TelemetryLog { } } +/// When a log line was written: close to Go's `time.Time.String()` layout, +/// without the zone abbreviation and the monotonic clock reading Go adds. The +/// fraction keeps 3, 6 or 9 digits and the offset is rounded to the minute, as +/// earlier releases wrote them. +fn log_stamp(t: time::OffsetDateTime) -> String { + let nanos = t.nanosecond(); + let fraction = if nanos == 0 { + String::new() + } else if nanos.is_multiple_of(1_000_000) { + format!(".{:03}", nanos / 1_000_000) + } else if nanos.is_multiple_of(1_000) { + format!(".{:06}", nanos / 1_000) + } else { + format!(".{nanos:09}") + }; + let offset = t.offset().whole_seconds(); + let (sign, offset) = if offset < 0 { + ('-', -offset) + } else { + ('+', offset) + }; + let minutes = (offset + 30) / 60; + format!( + "{:04}-{:02}-{:02} {:02}:{:02}:{:02}{fraction} {sign}{:02}{:02}", + t.year(), + u8::from(t.month()), + t.day(), + t.hour(), + t.minute(), + t.second(), + minutes / 60, + minutes % 60 + ) +} + /// The `extra` field of the telemetry payload. #[derive(Debug, Default, Serialize)] pub struct TelemetryExtra { @@ -136,3 +168,41 @@ impl TelemetryServer { Ok(u) } } + +#[cfg(test)] +mod tests { + use super::*; + + /// The log stamp of 2026-08-06 15:41:36 and `nanos` on a clock + /// `offset_seconds` east of UTC. + fn at(nanos: u32, offset_seconds: i32) -> String { + let offset = time::UtcOffset::from_whole_seconds(offset_seconds).unwrap(); + let t = time::Date::from_calendar_date(2026, time::Month::August, 6) + .unwrap() + .with_hms_nano(15, 41, 36, nanos) + .unwrap() + .assume_offset(offset); + log_stamp(t) + } + + #[test] + fn log_stamps_keep_three_six_or_nine_digits_of_the_second() { + assert_eq!(at(0, 7200), "2026-08-06 15:41:36 +0200"); + assert_eq!(at(120_000_000, 7200), "2026-08-06 15:41:36.120 +0200"); + assert_eq!(at(120_500_000, 7200), "2026-08-06 15:41:36.120500 +0200"); + assert_eq!(at(1, -19_800), "2026-08-06 15:41:36.000000001 -0530"); + assert_eq!(at(999_999_999, 0), "2026-08-06 15:41:36.999999999 +0000"); + } + + #[test] + fn log_stamps_round_the_offset_to_the_minute() { + assert_eq!(at(0, 19_829), "2026-08-06 15:41:36 +0530"); + assert_eq!(at(0, 19_830), "2026-08-06 15:41:36 +0531"); + assert_eq!(at(0, -19_830), "2026-08-06 15:41:36 -0531"); + assert_eq!(at(0, -29), "2026-08-06 15:41:36 -0000"); + assert_eq!(at(0, -30), "2026-08-06 15:41:36 -0001"); + assert_eq!(at(0, -31), "2026-08-06 15:41:36 -0001"); + assert_eq!(at(0, 29), "2026-08-06 15:41:36 +0000"); + assert_eq!(at(0, 30), "2026-08-06 15:41:36 +0001"); + } +} diff --git a/src/helper.rs b/src/helper.rs index ee9a75a..89671d2 100644 --- a/src/helper.rs +++ b/src/helper.rs @@ -69,7 +69,6 @@ pub async fn do_speed_test( let delimiter = cli.csv_delimiter_byte()?; let mut reps_json: Vec = Vec::new(); let mut reps_csv: Vec = Vec::new(); - let mut measured = false; // Each server that did not answer its probe, and why. let mut down: Vec<(String, String)> = Vec::new(); @@ -105,26 +104,20 @@ pub async fn do_speed_test( } let status = current_server.is_up(out, ctx.client, &tlog).await; - if !status.up { - write_ui!( - out, - "Selected server {} ({}) is not responding at the moment, try again later\n", + if let Some(why) = status.down_reason { + let label = format!( + "{} ({})", output::sanitize(¤t_server.name), output::sanitize(&hostname) ); + write_ui!( + out, + "Selected server {label} is not responding at the moment, try again later\n" + ); if servers.len() > 1 && !out.quiet { output::write_ui_blank(); } - down.push(( - format!( - "{} ({})", - output::sanitize(¤t_server.name), - output::sanitize(&hostname) - ), - status - .down_reason - .unwrap_or_else(|| "its probe failed".to_string()), - )); + down.push((label, why)); continue; } @@ -358,8 +351,6 @@ pub async fn do_speed_test( }); } - measured = true; - // Add a blank line after each test when testing multiple servers. if servers.len() > 1 && !out.quiet { output::write_ui_blank(); @@ -367,8 +358,10 @@ pub async fn do_speed_test( } // No server answered: fail instead of printing an empty report, which with - // exit status 0 would pass for a result. - if !measured && !down.is_empty() { + // exit status 0 would pass for a result. A server that got this far was + // measured or is in `down`, every other path returns, so the report has + // something in it whenever this lets the run through. + if down.len() == servers.len() { return Err(none_responding(&down)); } @@ -380,12 +373,12 @@ pub async fn do_speed_test( } else if cli.json_stream { // The reports are the same array --json prints, wrapped as the final // event so a consumer needs one parser for the whole stream. - match report::to_go_json(&json_reports(&reps_json)) { + match report::to_go_json(&reps_json) { Ok(s) => out.stream_event(&format!(r#"{{"event":"result","reports":{s}}}"#)), Err(e) => write_error!("Error generating JSON report: {e}\n"), } } else if cli.json { - match report::to_go_json(&json_reports(&reps_json)) { + match report::to_go_json(&reps_json) { // serde_json does not terminate its output, and a document that // ends mid-line makes a shell prompt land on top of it and leaves // line-oriented tools with an unterminated last line. @@ -419,14 +412,6 @@ fn none_responding(down: &[(String, String)]) -> anyhow::Error { } } -/// The reports as JSON sees them: `null` when nothing was measured. -/// -/// Go declares a nil slice, which marshals to `null`. No run gets here with -/// nothing measured any more: down servers and an empty list fail it first. -fn json_reports(reports: &[JSONReport]) -> Option<&[JSONReport]> { - (!reports.is_empty()).then_some(reports) -} - /// Sends the result to the telemetry server and returns the share link. #[allow(clippy::too_many_arguments)] async fn send_telemetry( diff --git a/src/http/mod.rs b/src/http/mod.rs index e13cf76..53d1334 100644 --- a/src/http/mod.rs +++ b/src/http/mod.rs @@ -157,9 +157,16 @@ async fn drain_body(body: Incoming) -> anyhow::Result { /// For deciding what a body *is* rather than reading it: emptiness, or the /// opening of an error page worth showing. Reading stops once `limit` bytes /// are kept, so a body that never ends cannot keep the caller waiting. -async fn body_prefix(body: Incoming, limit: usize) -> anyhow::Result { - let mut body = body; - let mut out = BytesMut::new(); +/// +/// The bytes are kept in `out`, so a read that fails, or is dropped when the +/// time runs out, leaves what it got there, as Go's `io.ReadAll` returns it. +/// Frames other than data, such as trailers, are skipped. +async fn body_prefix(body: B, limit: usize, out: &mut BytesMut) -> anyhow::Result<()> +where + B: hyper::body::Body, + B::Error: std::error::Error + Send + Sync + 'static, +{ + let mut body = std::pin::pin!(body); while out.len() < limit { let Some(frame) = body.frame().await else { @@ -171,7 +178,23 @@ async fn body_prefix(body: Incoming, limit: usize) -> anyhow::Result { } } - Ok(out.freeze()) + Ok(()) +} + +/// What a probe got back: the status and connection of the response, and as +/// much of its body as was read. +#[derive(Debug)] +#[non_exhaustive] +pub struct Prefix { + pub status: StatusCode, + /// The start of the body, up to the limit asked for. A body that reached + /// the limit may go on, and the rest is never read. + pub body: Bytes, + /// Why the body broke off before its end or the limit, if it did. `None` + /// does not say the body ended, only that nothing failed before it ended + /// or reached the limit. + pub body_error: Option, + pub facts: ConnectionFacts, } /// The hyper client configuration `HttpClient` is built from. @@ -257,11 +280,30 @@ impl HttpClient { &self, fut: impl std::future::Future>, ) -> anyhow::Result { + self.until(self.deadline(), fut).await + } + + /// When the configured timeout runs out if it starts now, or `None` when + /// it is zero or too far off to represent, as `tokio::time::timeout` + /// treats it. + fn deadline(&self) -> Option { if self.timeout.is_zero() { - return fut.await; + return None; } + tokio::time::Instant::now().checked_add(self.timeout) + } - tokio::time::timeout(self.timeout, fut) + /// Runs `fut` until `deadline`, failing it with the timeout's error after. + async fn until( + &self, + deadline: Option, + fut: impl std::future::Future>, + ) -> anyhow::Result { + let Some(deadline) = deadline else { + return fut.await; + }; + + tokio::time::timeout_at(deadline, fut) .await .map_err(|_| anyhow::anyhow!("request timed out after {:?}", self.timeout))? } @@ -419,22 +461,34 @@ impl HttpClient { /// timeout set. The rest is dropped unread, which closes an HTTP/1.1 /// connection instead of returning it to the pool. The backend probe can /// afford that, because a body at all already marks the backend down. - pub async fn get_prefix( - &self, - url: &Url, - limit: usize, - ) -> anyhow::Result<(StatusCode, Bytes, ConnectionFacts)> { - let fut = async { - let resp = self - .request(Method::GET, url, &[], RequestBody::Empty) - .await?; - let status = resp.status(); - let facts = ConnectionFacts::of(&resp); - let body = body_prefix(resp.into_body(), limit).await?; - Ok::<_, anyhow::Error>((status, body, facts)) - }; + /// + /// The timeout covers the response and its body together, as Go's client + /// timeout does. Once the response is in, a body that fails or runs out of + /// time is reported in the result, so its status is not lost. A body that + /// stalls short of the limit holds the call until the timeout, and for + /// good with no timeout set, as it holds Go's client. + pub async fn get_prefix(&self, url: &Url, limit: usize) -> anyhow::Result { + let deadline = self.deadline(); + let resp = self + .until( + deadline, + self.request(Method::GET, url, &[], RequestBody::Empty), + ) + .await?; + let status = resp.status(); + let facts = ConnectionFacts::of(&resp); + let mut body = BytesMut::new(); + let body_error = self + .until(deadline, body_prefix(resp.into_body(), limit, &mut body)) + .await + .err(); - self.with_timeout(fut).await + Ok(Prefix { + status, + body: body.freeze(), + body_error, + facts, + }) } /// Posts a body and reads the whole response, used for telemetry. @@ -1696,19 +1750,161 @@ ZD/4gnUj9TooNmtCjXdP4GAKxDoCb0FzQoHhDoi8BXY4DFLQYpbnX4Nu }); let client = client(Duration::ZERO); - let (status, prefix, _) = tokio::time::timeout(TIMEOUT, client.get_prefix(&url, 10_000)) + let prefix = tokio::time::timeout(TIMEOUT, client.get_prefix(&url, 10_000)) .await .expect("reading a prefix of an endless body did not return") .unwrap(); - assert_eq!(status, StatusCode::OK); - assert_eq!(prefix.len(), 10_000); + assert_eq!(prefix.status, StatusCode::OK); + assert_eq!(prefix.body.len(), 10_000); + assert!(prefix.body_error.is_none()); tokio::time::timeout(TIMEOUT, closed) .await .expect("the connection was not closed") .unwrap(); } + /// A body that breaks off, by a hang-up or by the timeout, still leaves + /// the status and the bytes read before it, with the reason beside them. + #[tokio::test] + async fn a_prefix_that_breaks_off_keeps_its_status_and_bytes() { + for hang_up in [true, false] { + let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let url = Url::parse(&format!("http://{}/", listener.local_addr().unwrap())).unwrap(); + tokio::spawn(async move { + let (mut stream, _) = listener.accept().await.unwrap(); + let _ = read_request(&mut stream).await; + let head = "HTTP/1.1 404 Not Found\r\nContent-Length: 64\r\n\r\n0123456789"; + let _ = stream.write_all(head.as_bytes()).await; + if !hang_up { + tokio::time::sleep(TIMEOUT).await; + } + }); + + // Long enough for the response head on a busy runner, which the + // timeout covers too. + let client = client(Duration::from_secs(1)); + let prefix = tokio::time::timeout(TIMEOUT, client.get_prefix(&url, 512)) + .await + .expect("a body that broke off did not return") + .expect("the response itself arrived"); + + assert_eq!(prefix.status, StatusCode::NOT_FOUND, "hang up: {hang_up}"); + assert_eq!(&prefix.body[..], b"0123456789", "hang up: {hang_up}"); + let error = prefix.body_error.expect("the break is reported"); + if hang_up { + // hyper reports a body cut short of its Content-Length as an + // unexpected end of the connection. + assert!( + error.chain().any(|e| e + .downcast_ref::() + .is_some_and(|e| e.kind() == io::ErrorKind::UnexpectedEof)), + "not the hang-up: {error:#}" + ); + } else { + assert_eq!(error.to_string(), "request timed out after 1s"); + } + } + } + + /// What `body_prefix` keeps of a body arriving in the given frames, and + /// whether it reported an error. + async fn prefix_of(frames: Vec, io::Error>>) -> (usize, bool) { + let body = http_body_util::StreamBody::new(futures_util::stream::iter(frames)); + let mut out = BytesMut::new(); + let failed = body_prefix(body, 512, &mut out).await.is_err(); + (out.len(), failed) + } + + fn data(len: usize) -> Result, io::Error> { + Ok(hyper::body::Frame::data(Bytes::from(vec![b'x'; len]))) + } + + fn trailers() -> Result, io::Error> { + Ok(hyper::body::Frame::trailers(http::HeaderMap::new())) + } + + fn broken() -> Result, io::Error> { + Err(io::Error::from(io::ErrorKind::UnexpectedEof)) + } + + /// The limit holds however the body is cut into frames, and a body that + /// reaches it is not read further, so what comes after cannot fail it. + #[tokio::test] + async fn a_prefix_keeps_the_limit_whatever_the_frames() { + for (frames, kept, failed, case) in [ + (vec![data(512)], 512, false, "exactly the limit"), + (vec![data(513)], 512, false, "one byte over"), + ( + vec![data(300), data(300)], + 512, + false, + "over it across two frames", + ), + ( + vec![data(100), data(0), data(100)], + 200, + false, + "an empty frame between", + ), + ( + vec![data(512), broken()], + 512, + false, + "a break after the limit", + ), + (vec![trailers()], 0, false, "trailers alone"), + (vec![data(10), trailers()], 10, false, "data, then trailers"), + ( + vec![data(10), broken()], + 10, + true, + "a break after ten bytes", + ), + (vec![], 0, false, "no frame at all"), + ] { + assert_eq!(prefix_of(frames).await, (kept, failed), "{case}"); + } + } + + /// The response and its body share one deadline, as under Go's client + /// timeout: a head that took half the timeout leaves the body the other + /// half, not a timeout of its own. + #[tokio::test] + async fn a_body_gets_only_what_the_head_left_of_the_timeout() { + // Each half leaves a busy runner a second and a half of slack. + const LIMIT: Duration = Duration::from_secs(3); + const HEAD_AFTER: Duration = Duration::from_millis(1500); + + let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let url = Url::parse(&format!("http://{}/", listener.local_addr().unwrap())).unwrap(); + tokio::spawn(async move { + let (mut stream, _) = listener.accept().await.unwrap(); + let _ = read_request(&mut stream).await; + tokio::time::sleep(HEAD_AFTER).await; + let head = "HTTP/1.1 200 OK\r\nContent-Length: 64\r\n\r\n0123456789"; + let _ = stream.write_all(head.as_bytes()).await; + tokio::time::sleep(TIMEOUT).await; + }); + + let client = client(LIMIT); + let started = std::time::Instant::now(); + let prefix = tokio::time::timeout(TIMEOUT, client.get_prefix(&url, 512)) + .await + .expect("a stalled body did not return") + .expect("the head arrived within the timeout"); + let took = started.elapsed(); + + let error = prefix.body_error.expect("the stall is reported"); + assert_eq!(error.to_string(), "request timed out after 3s"); + // Timers never fire early, so a timeout of its own for the body would + // end it no sooner than this. + assert!( + took < HEAD_AFTER + LIMIT, + "the body got a timeout of its own: {took:?}" + ); + } + /// Over TLS the meter counts what the TLS session accepted, which is what /// makes it comparable with body bytes, and what it excludes is whatever /// hyper is still holding. diff --git a/src/lib.rs b/src/lib.rs index 7db02f3..de7244a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,9 @@ //! The binary is a thin wrapper around this library, so the parsing and //! measurement logic can be exercised by tests and fuzz targets without //! spawning a process. +//! +//! The library is not meant for other crates. Its API can change in any +//! release, a minor or patch one included. pub mod cli; pub mod defs; diff --git a/src/report.rs b/src/report.rs index e0476c1..d69c140 100644 --- a/src/report.rs +++ b/src/report.rs @@ -143,37 +143,101 @@ fn escape_like_go(s: &str) -> String { /// The current time, formatted the way Go's `time.Time` marshals to JSON. pub fn timestamp_now() -> String { - format_timestamp(chrono::Local::now().fixed_offset()) + render(local_now()) } -/// Formats a timestamp as Go's RFC 3339 "nano" layout does: the fractional -/// second keeps only as many digits as it needs, disappearing entirely when it -/// is zero, and a zero offset is written `Z`. +/// Formats `t` as `format_timestamp` does, for any year the clock can show, +/// so a report always has its timestamp. +fn render(t: time::OffsetDateTime) -> String { + go_rfc3339_nano( + time::PrimitiveDateTime::new(t.date(), t.time()), + t.offset().whole_seconds(), + ) +} + +/// The current time on the local clock, or in UTC when the system cannot +/// say what the local offset is or the local date would pass 9999. /// -/// Not chrono's `to_rfc3339_opts`, which rounds the fraction to 0, 3, 6 or 9 -/// digits and spells a zero offset `+00:00`. -pub fn format_timestamp(t: chrono::DateTime) -> String { - use chrono::Timelike as _; +/// The C library reads `TZ` for the offset, which a thread setting it at the +/// same moment would make undefined. Nothing in this program sets it. +pub(crate) fn local_now() -> time::OffsetDateTime { + let now = time::OffsetDateTime::now_utc(); + if !has_local_zone() { + return now; + } + time::UtcOffset::local_offset_at(now) + .ok() + .and_then(|offset| now.checked_to_offset(offset)) + .unwrap_or(now) +} - let date = t.format("%Y-%m-%dT%H:%M:%S"); +/// Whether to ask the C library for the local offset: for a `TZ` that is not +/// empty, or else when `/etc/localtime` exists, the two places Go looks. +/// OpenWrt's C library falls back to `/etc/TZ`, which Go does not read, so a +/// router with neither stays on UTC, as with Go. A `TZ` holding a POSIX rule +/// such as `CET-1CEST,M3.5.0,M10.5.0/3` is followed, as in 1.2.1, where Go +/// writes UTC. +#[cfg(unix)] +fn has_local_zone() -> bool { + local_zone_in(std::env::var_os("TZ").as_deref(), || { + std::path::Path::new("/etc/localtime").exists() + }) +} - // A leap second is carried in the nanosecond field as a value past one - // second; Go has no such representation, so it is folded back in. - let nanos = t.nanosecond() % 1_000_000_000; +/// `has_local_zone` for a given `TZ` and a way to see whether +/// `/etc/localtime` exists. A `TZ` the C library cannot read counts too: it +/// answers UTC for it, as Go does. +#[cfg(unix)] +fn local_zone_in(tz: Option<&std::ffi::OsStr>, localtime_exists: impl FnOnce() -> bool) -> bool { + match tz { + Some(tz) => !tz.is_empty(), + None => localtime_exists(), + } +} + +#[cfg(not(unix))] +fn has_local_zone() -> bool { + true +} + +/// Formats the instant `unix_seconds` and `nanos` after the Unix epoch, seen +/// `offset_seconds` east of UTC, the way Go marshals +/// `time.Unix(sec, nsec).In(time.FixedZone("", offset))` to JSON. +/// +/// `None` where Go's marshal fails: a year outside 0 to 9999 on that clock, +/// or an offset of a day or more. +pub fn format_timestamp(unix_seconds: i64, nanos: u32, offset_seconds: i32) -> Option { + if offset_seconds.unsigned_abs() >= 86_400 { + return None; + } + let since_epoch = time::Duration::seconds(unix_seconds.checked_add(i64::from(offset_seconds))?) + .checked_add(time::Duration::nanoseconds(i64::from(nanos)))?; + let epoch = time::OffsetDateTime::UNIX_EPOCH; + let wall = time::PrimitiveDateTime::new(epoch.date(), epoch.time()).checked_add(since_epoch)?; + (0..=9999) + .contains(&wall.year()) + .then(|| go_rfc3339_nano(wall, offset_seconds)) +} + +/// Formats the time `wall` on a clock `offset_seconds` east of UTC as Go's +/// RFC 3339 "nano" layout does: the fractional second keeps only as many +/// digits as it needs, disappearing entirely when it is zero, and a zero +/// offset is written `Z`. +fn go_rfc3339_nano(wall: time::PrimitiveDateTime, offset_seconds: i32) -> String { + let nanos = wall.nanosecond(); let fraction = if nanos == 0 { String::new() } else { format!(".{}", format!("{nanos:09}").trim_end_matches('0')) }; - let offset = t.offset().local_minus_utc(); - let zone = if offset == 0 { + let zone = if offset_seconds == 0 { "Z".to_string() } else { // Go truncates the offset to whole minutes and takes the sign from // what is left, so an offset less than a minute behind UTC is written // as ahead of it. - let minutes = offset / 60; + let minutes = offset_seconds / 60; let (sign, minutes) = if minutes < 0 { ('-', -minutes) } else { @@ -182,7 +246,15 @@ pub fn format_timestamp(t: chrono::DateTime) -> String { format!("{sign}{:02}:{:02}", minutes / 60, minutes % 60) }; - format!("{date}{fraction}{zone}") + format!( + "{:04}-{:02}-{:02}T{:02}:{:02}:{:02}{fraction}{zone}", + wall.year(), + u8::from(wall.month()), + wall.day(), + wall.hour(), + wall.minute(), + wall.second() + ) } /// The speed test server's information in a JSON report. @@ -349,15 +421,13 @@ pub fn csv_rows(reports: &[CSVReport], delimiter: u8) -> anyhow::Result #[cfg(test)] mod tests { use super::*; - use chrono::{TimeZone as _, Timelike as _}; - - fn at(nanos: u32, offset_seconds: i32) -> chrono::DateTime { - chrono::FixedOffset::east_opt(offset_seconds) - .unwrap() - .with_ymd_and_hms(2026, 8, 6, 15, 41, 36) - .unwrap() - .with_nanosecond(nanos) - .unwrap() + + /// 2026-08-06 15:41:36 and `nanos` on a clock `offset_seconds` east of + /// UTC, formatted. + fn at(nanos: u32, offset_seconds: i32) -> String { + // 2026-08-06T15:41:36Z + const WALL: i64 = 1_786_030_896; + format_timestamp(WALL - i64::from(offset_seconds), nanos, offset_seconds).unwrap() } #[test] @@ -440,31 +510,100 @@ mod tests { #[test] fn timestamps_trim_the_fraction_the_way_go_does() { + assert_eq!(at(411_388_000, 7200), "2026-08-06T15:41:36.411388+02:00"); + assert_eq!(at(380_000_000, 7200), "2026-08-06T15:41:36.38+02:00"); + assert_eq!(at(0, 7200), "2026-08-06T15:41:36+02:00"); + assert_eq!(at(1, 7200), "2026-08-06T15:41:36.000000001+02:00"); + } + + #[test] + fn a_zero_offset_is_written_z() { + assert_eq!(at(20_091_000, 0), "2026-08-06T15:41:36.020091Z"); + assert_eq!(at(0, -19_800), "2026-08-06T15:41:36-05:30"); + } + + /// The clock's path writes what `format_timestamp`, which the golden test + /// checks against Go, writes for the same instant. + #[test] + fn the_current_time_is_written_as_format_timestamp_writes_it() { + for secs in [ + -62_135_596_800, + -1, + 0, + 1, + 951_782_400, + 1_786_023_696, + 4_102_444_800, + 253_402_214_400, + ] { + for nanos in [0, 1, 120_000_000, 999_999_999] { + for offset in [0, 3600, -3600, 19_800, -19_800, 30, -30, 86_399, -86_399] { + let Some(want) = format_timestamp(secs, nanos, offset) else { + continue; + }; + let t = time::OffsetDateTime::from_unix_timestamp_nanos( + i128::from(secs) * 1_000_000_000 + i128::from(nanos), + ) + .unwrap() + .to_offset(time::UtcOffset::from_whole_seconds(offset).unwrap()); + assert_eq!(render(t), want, "{secs}.{nanos} at {offset}"); + } + } + } + } + + #[cfg(unix)] + #[test] + fn the_local_zone_is_asked_for_where_go_looks_for_one() { + use std::ffi::OsStr; + // Set to a name, or to one the C library cannot read and answers UTC + // for: ask it either way. + assert!(local_zone_in(Some(OsStr::new("Europe/Prague")), || false)); + assert!(local_zone_in(Some(OsStr::new("garbage")), || false)); + // Set but empty is UTC, as in Go, whatever /etc/localtime says. + assert!(!local_zone_in(Some(OsStr::new("")), || true)); + // Unset: /etc/localtime decides. + assert!(local_zone_in(None, || true)); + assert!(!local_zone_in(None, || false)); + } + + #[test] + fn a_year_go_cannot_marshal_has_no_timestamp() { + // 0000-01-01T00:00:00Z and 9999-12-31T23:59:59Z, then a second + // outside each. assert_eq!( - format_timestamp(at(411_388_000, 7200)), - "2026-08-06T15:41:36.411388+02:00" - ); - assert_eq!( - format_timestamp(at(380_000_000, 7200)), - "2026-08-06T15:41:36.38+02:00" + format_timestamp(-62_167_219_200, 0, 0).as_deref(), + Some("0000-01-01T00:00:00Z") ); - assert_eq!(format_timestamp(at(0, 7200)), "2026-08-06T15:41:36+02:00"); assert_eq!( - format_timestamp(at(1, 7200)), - "2026-08-06T15:41:36.000000001+02:00" + format_timestamp(253_402_300_799, 0, 0).as_deref(), + Some("9999-12-31T23:59:59Z") ); + assert_eq!(format_timestamp(-62_167_219_201, 0, 0), None); + assert_eq!(format_timestamp(253_402_300_800, 0, 0), None); + // The year is the clock's: an hour ahead of UTC is already 10000. + assert_eq!(format_timestamp(253_402_297_200, 0, 3600), None); + // Go refuses these too: "year outside of range [0,9999]". + assert_eq!(format_timestamp(i64::MAX, 0, 0), None); + assert_eq!(format_timestamp(i64::MIN, 0, 0), None); + assert_eq!(format_timestamp(i64::MAX, 999_999_999, 86_399), None); } #[test] - fn a_zero_offset_is_written_z() { + fn an_offset_go_cannot_marshal_has_no_timestamp() { + // Go 1.26 writes 23:59 either side and refuses a whole day: + // "timezone hour outside of range [0,23]". assert_eq!( - format_timestamp(at(20_091_000, 0)), - "2026-08-06T15:41:36.020091Z" + format_timestamp(1_786_023_696, 0, 86_399).as_deref(), + Some("2026-08-07T13:41:35+23:59") ); assert_eq!( - format_timestamp(at(0, -19_800)), - "2026-08-06T15:41:36-05:30" + format_timestamp(1_786_023_696, 0, -86_399).as_deref(), + Some("2026-08-05T13:41:37-23:59") ); + assert_eq!(format_timestamp(1_786_023_696, 0, 86_400), None); + assert_eq!(format_timestamp(1_786_023_696, 0, -86_400), None); + assert_eq!(format_timestamp(1_786_023_696, 0, i32::MIN), None); } #[test] diff --git a/src/speedtest.rs b/src/speedtest.rs index ac502fd..2224654 100644 --- a/src/speedtest.rs +++ b/src/speedtest.rs @@ -485,7 +485,7 @@ async fn ping_all(servers: &[Server], ctx: &TestContext<'_>) -> Vec<(usize, f64) }; // Check the server is up before spending time on a ping. - if !server.is_up(out, ctx.client, &tlog).await.up { + if !server.is_up(out, ctx.client, &tlog).await.up() { write_debug!( out, "Server {} ({}) doesn't seem to be up, skipping\n", diff --git a/tests/data/go_timestamps.tsv b/tests/data/go_timestamps.tsv index 67f3c09..2386e31 100644 --- a/tests/data/go_timestamps.tsv +++ b/tests/data/go_timestamps.tsv @@ -56,6 +56,12 @@ 1786023696 20091000 0 2026-08-06T13:41:36.020091Z 1786023696 500000000 7200 2026-08-06T15:41:36.5+02:00 1786023696 500000000 0 2026-08-06T13:41:36.5Z +1786023696 1000000000 7200 2026-08-06T15:41:37+02:00 +1786023696 1000000000 0 2026-08-06T13:41:37Z +1786023696 1999999999 7200 2026-08-06T15:41:37.999999999+02:00 +1786023696 1999999999 0 2026-08-06T13:41:37.999999999Z +1786023696 4294967295 7200 2026-08-06T15:41:40.294967295+02:00 +1786023696 4294967295 0 2026-08-06T13:41:40.294967295Z 1786023696 0 0 2026-08-06T13:41:36Z 1786023696 123000000 0 2026-08-06T13:41:36.123Z 1786023696 0 3600 2026-08-06T14:41:36+01:00 diff --git a/tests/data/parity/servers-garbled.json b/tests/data/parity/servers-garbled.json index f7ce6e5..dc08913 100644 --- a/tests/data/parity/servers-garbled.json +++ b/tests/data/parity/servers-garbled.json @@ -31,5 +31,93 @@ "getIpURL": "getIP.php", "sponsorName": "", "sponsorURL": "" + }, + { + "id": 4, + "name": "Truncated 404", + "server": "http://127.0.0.1:@GARBLED@/", + "dlURL": "garbage.php", + "ulURL": "empty.php", + "pingURL": "truncated-404.php", + "getIpURL": "getIP.php", + "sponsorName": "", + "sponsorURL": "" + }, + { + "id": 5, + "name": "Stalled 404", + "server": "http://127.0.0.1:@GARBLED@/", + "dlURL": "garbage.php", + "ulURL": "empty.php", + "pingURL": "stalled-404.php", + "getIpURL": "getIP.php", + "sponsorName": "", + "sponsorURL": "" + }, + { + "id": 6, + "name": "Truncated probe", + "server": "http://127.0.0.1:@GARBLED@/", + "dlURL": "garbage.php", + "ulURL": "empty.php", + "pingURL": "truncated-ping.php", + "getIpURL": "getIP.php", + "sponsorName": "", + "sponsorURL": "" + }, + { + "id": 7, + "name": "Stalled probe", + "server": "http://127.0.0.1:@GARBLED@/", + "dlURL": "garbage.php", + "ulURL": "empty.php", + "pingURL": "stalled-ping.php", + "getIpURL": "getIP.php", + "sponsorName": "", + "sponsorURL": "" + }, + { + "id": 8, + "name": "No content", + "server": "http://127.0.0.1:@GARBLED@/", + "dlURL": "garbage.php", + "ulURL": "empty.php", + "pingURL": "no-content.php", + "getIpURL": "getIP.php", + "sponsorName": "", + "sponsorURL": "" + }, + { + "id": 9, + "name": "Chunked 404", + "server": "http://127.0.0.1:@GARBLED@/", + "dlURL": "garbage.php", + "ulURL": "empty.php", + "pingURL": "chunked-404.php", + "getIpURL": "getIP.php", + "sponsorName": "", + "sponsorURL": "" + }, + { + "id": 10, + "name": "Chunked probe", + "server": "http://127.0.0.1:@GARBLED@/", + "dlURL": "garbage.php", + "ulURL": "empty.php", + "pingURL": "chunked-ping.php", + "getIpURL": "getIP.php", + "sponsorName": "", + "sponsorURL": "" + }, + { + "id": 11, + "name": "Long probe", + "server": "http://127.0.0.1:@GARBLED@/", + "dlURL": "garbage.php", + "ulURL": "empty.php", + "pingURL": "long-ping.php", + "getIpURL": "getIP.php", + "sponsorName": "", + "sponsorURL": "" } ] diff --git a/tests/golden.rs b/tests/golden.rs index c886d18..4f24066 100644 --- a/tests/golden.rs +++ b/tests/golden.rs @@ -6,7 +6,6 @@ //! Feeding the same inputs to this client pins the two renderings together //! without a Go toolchain at hand, so this runs in every `cargo test`. -use chrono::{DateTime, FixedOffset}; use librespeed_cli::defs::IPInfoResponse; use librespeed_cli::report::{ csv_rows, format_timestamp, to_go_json, CSVReport, Client, JSONReport, ProgressEvent, @@ -151,27 +150,18 @@ fn timestamps_render_as_time_marshals_them() { let [secs, nanos, offset, want] = row[..] else { panic!("a row of seconds, nanoseconds, offset and text: {row:?}"); }; - let at = instant( - secs.parse().expect("unix seconds"), - nanos.parse().expect("nanoseconds"), - offset.parse().expect("an offset in seconds"), - ); + let secs = secs.parse().expect("unix seconds"); + let nanos = nanos.parse().expect("nanoseconds"); + let offset = offset.parse().expect("an offset in seconds"); bad.check( &format!("{secs}.{nanos} at {offset}"), - &format_timestamp(at), + &format_timestamp(secs, nanos, offset).expect("an instant and offset in range"), want, ); } bad.verdict(); } -/// The instant Go's `time.Unix(sec, nsec).In(time.FixedZone("", offset))` is. -fn instant(secs: i64, nanos: u32, offset: i32) -> DateTime { - DateTime::from_timestamp(secs, nanos) - .expect("an instant chrono can hold") - .with_timezone(&FixedOffset::east_opt(offset).expect("an offset chrono can hold")) -} - /// A timestamp in the data, built the same way on both sides. #[derive(Deserialize)] struct Instant { @@ -219,7 +209,8 @@ impl From for JSONReport { let [ip, hostname, city, region, country, location, organization, postal, timezone, readme] = r.client; JSONReport { - timestamp: format_timestamp(instant(r.at.sec, r.at.nsec, r.at.offset)), + timestamp: format_timestamp(r.at.sec, r.at.nsec, r.at.offset) + .expect("an instant and offset in range"), server: ReportServer { name: r.name, url: r.url, diff --git a/tests/integration.rs b/tests/integration.rs index 67e2e53..4fdbe64 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -11,6 +11,9 @@ use std::sync::Arc; const BIN: &str = env!("CARGO_BIN_EXE_librespeed-cli"); const GARBAGE_LEN: usize = 2 * 1024 * 1024; +/// How long a stalled answer stays silent: longer than the tests' one-second +/// timeout, with room for a slow runner. +const STALL: std::time::Duration = std::time::Duration::from_secs(5); /// A mock LibreSpeed backend. Dropping it leaves the thread running; tests are /// short-lived processes, so that is fine. @@ -161,6 +164,19 @@ fn handle(mut stream: TcpStream, telemetry_hits: Arc) -> std::io::R stream.write_all(page.as_bytes())?; stream.flush() }; + // A response that promises 64 bytes and sends only `sent`, then hangs up, + // or with `stall` goes quiet for longer than the client waits. + let broken_off = + |stream: &mut TcpStream, status: &str, sent: &[u8], stall: bool| -> std::io::Result<()> { + let head = format!("HTTP/1.1 {status}\r\nContent-Length: 64\r\n\r\n"); + stream.write_all(head.as_bytes())?; + stream.write_all(sent)?; + stream.flush()?; + if stall { + std::thread::sleep(STALL); + } + Ok(()) + }; match (method.as_str(), path.as_str()) { (_, "/empty.php") => respond(&mut stream, b"", "text/plain")?, @@ -194,6 +210,24 @@ fn handle(mut stream: TcpStream, telemetry_hits: Arc) -> std::io::R // names neither the way the server put it. ("GET", "/too-large.php") => error_page(&mut stream, "413 Content Too Large")?, ("GET", "/unknown-status.php") => error_page(&mut stream, "599 Made Up")?, + // A body after a 204, where none can be: the status still decides. + ("GET", "/no-content.php") => error_page(&mut stream, "204 No Content")?, + // Answers that break off: ten bytes of a promised 64, then a hang-up + // or a silence longer than the tests' one-second timeout. + ("GET", p @ ("/truncated-error-page.php" | "/stalled-error-page.php")) => broken_off( + &mut stream, + "404 Not Found", + b"0123456789", + p.starts_with("/stalled"), + )?, + ("GET", p @ ("/truncated-ping.php" | "/stalled-ping.php")) => broken_off( + &mut stream, + "200 OK", + b"0123456789", + p.starts_with("/stalled"), + )?, + ("GET", "/cut-error-page.php") => broken_off(&mut stream, "404 Not Found", b"", false)?, + ("GET", "/cut-ping.php") => broken_off(&mut stream, "200 OK", b"", false)?, _ => { stream.write_all( b"HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\nConnection: close\r\n\r\n", @@ -452,6 +486,53 @@ fn telemetry_produces_a_share_link() { assert_eq!(backend.telemetry_hits.load(Ordering::Relaxed), 1); } +/// The report's timestamp is on the clock `TZ` describes, and in UTC when it +/// is empty or describes nothing the C library can read, as in the Go +/// client. A POSIX rule is followed as in 1.2.1, where Go writes UTC. Each +/// case is a process of its own, so no two share a `TZ`. +#[cfg(unix)] +#[test] +fn the_report_timestamp_follows_tz() { + let backend = MockBackend::start(); + let list = backend.server_list("tz"); + let mut cases = vec![ + ("", "Z"), + ("UTC", "Z"), + ("garbage", "Z"), + ("XXX-5:45", "+05:45"), + ("XXX3:30", "-03:30"), + ]; + // A zone name needs the system's time zone database. + if std::path::Path::new("/usr/share/zoneinfo/Asia/Kathmandu").exists() { + cases.push(("Asia/Kathmandu", "+05:45")); + } + for (tz, suffix) in cases { + let out = Command::new(BIN) + .args([ + "--local-json", + list.to_str().unwrap(), + "--server", + "1", + "--no-icmp", + "--no-download", + "--no-upload", + "--json", + ]) + .env("TZ", tz) + .output() + .expect("run librespeed-cli"); + assert!( + out.status.success(), + "TZ={tz}: {}", + String::from_utf8_lossy(&out.stderr) + ); + let reports: serde_json::Value = + serde_json::from_str(&stdout_of(&out)).expect("valid JSON"); + let stamp = reports[0]["timestamp"].as_str().expect("a timestamp"); + assert!(stamp.ends_with(suffix), "TZ={tz}: {stamp}"); + } +} + #[test] fn no_download_and_no_upload_skip_their_tests() { let backend = MockBackend::start(); @@ -1099,7 +1180,7 @@ fn a_server_that_is_down_fails_the_run_in_every_mode() { assert_eq!(out.status.code(), Some(1), "{mode:?}: {stderr}"); assert_eq!(stdout_of(&out), "", "{mode:?}: no report"); let msg = failure(&out); - // The cause is worded by reqwest and the OS; only its presence counts. + // The cause is worded by hyper and the OS. Only its presence counts. let cause = msg .strip_prefix(DOWN) .unwrap_or_else(|| panic!("{mode:?}: {msg}")); @@ -1126,6 +1207,7 @@ fn a_server_answering_its_probe_wrongly_fails_the_run_with_what_it_answered() { ("server-error.php", "HTTP 500 Internal Server Error"), ("too-large.php", "HTTP 413 Request Entity Too Large"), ("unknown-status.php", "HTTP 599"), + ("no-content.php", "HTTP 204 No Content"), ("getIP.php", "a non-empty body"), ] { let list = write_server_list("probe-wrong", &[(1, "Wrong", backend.url(), ping)]); @@ -1149,6 +1231,83 @@ fn a_server_answering_its_probe_wrongly_fails_the_run_with_what_it_answered() { } } +// A probe answer that breaks off marks the backend down. A wrong status is +// still what the error names, as in the Go client, and only a 200 is reported +// by the read that failed, whether the peer hung up or the timeout ran out. +#[test] +fn a_probe_answer_that_breaks_off_is_named_by_its_status_first() { + const NOT_FOUND: &str = "the ping request returned HTTP 404 Not Found"; + const TEN: &str = "\"0123456789\""; + let backend = MockBackend::start(); + for (ping, answered, quoted) in [ + ("truncated-error-page.php", NOT_FOUND, TEN), + ("cut-error-page.php", NOT_FOUND, "\"\""), + ("stalled-error-page.php", NOT_FOUND, TEN), + ( + "stalled-ping.php", + "reading the ping response: request timed out after 1s", + TEN, + ), + ] { + let list = write_server_list("probe-broken", &[(1, "Broken", backend.url(), ping)]); + let out = run(&[ + "--local-json", + list.to_str().unwrap(), + "--server", + "1", + "--no-icmp", + "--timeout", + "1", + "--json", + "--debug", + ]); + let stderr = String::from_utf8_lossy(&out.stderr); + assert_eq!(out.status.code(), Some(1), "{ping}: {stderr}"); + assert_eq!(stdout_of(&out), "", "{ping}"); + assert_eq!( + failure(&out), + format!("Selected server Broken (127.0.0.1) is not responding: {answered}"), + "{ping}" + ); + // What arrived before the break is quoted, whatever the status. + assert!( + stderr.contains(&format!("\nFailed when parsing get IP result: {quoted}\n")), + "{ping}: {stderr}" + ); + } + + // A hang-up is worded by hyper, so only the prefix is ours. The debug line + // quotes what arrived, nothing included, as Go's does. + for (ping, quoted) in [ + ("truncated-ping.php", "\"0123456789\""), + ("cut-ping.php", "\"\""), + ] { + let list = write_server_list("probe-cut", &[(1, "Cut", backend.url(), ping)]); + let out = run(&[ + "--local-json", + list.to_str().unwrap(), + "--server", + "1", + "--no-icmp", + "--json", + "--debug", + ]); + let stderr = String::from_utf8_lossy(&out.stderr); + assert_eq!(out.status.code(), Some(1), "{ping}: {stderr}"); + let cause = failure(&out) + .strip_prefix( + "Selected server Cut (127.0.0.1) is not responding: reading the ping response: ", + ) + .unwrap_or_else(|| panic!("{ping}: {stderr}")) + .to_string(); + assert!(!cause.is_empty(), "{ping}: the cause is named: {stderr}"); + assert!( + stderr.contains(&format!("\nFailed when parsing get IP result: {quoted}\n")), + "{ping}: {stderr}" + ); + } +} + // With several servers all down, the error names each one with its own cause, // not only the last one tried. #[test] diff --git a/tests/parity/cases.rs b/tests/parity/cases.rs index 8c166da..6dbf42e 100644 --- a/tests/parity/cases.rs +++ b/tests/parity/cases.rs @@ -68,10 +68,10 @@ impl Why { } /// The entries of the README's list that no case here exercises: they need a -/// second origin, a certificate, another operating system or hardware a test -/// does not have -- and the one entry that only heads a group of entries that -/// are exercised. Naming them is what lets the check insist that every other -/// entry of the list has a case behind it. +/// second origin, a certificate, another operating system, hardware a test +/// does not have or a timestamp the comparison hides -- and the one entry that +/// only heads a group of entries that are exercised. Naming them is what lets +/// the check insist that every other entry of the list has a case behind it. pub const UNEXERCISED: &[&str] = &[ "**Response sizes are capped.**", "**A request body is not sent on to another origin.**", @@ -83,6 +83,10 @@ pub const UNEXERCISED: &[&str] = &[ "**`--interface` works on macOS**", "**TLS verification is stricter.**", "**The process exits rather than returning from `main`.**", + // Timestamps become `` before the outputs are compared, so no case + // here sees the clock. `the_report_timestamp_follows_tz` in + // tests/integration.rs runs a POSIX rule. + "**A POSIX rule in `TZ` sets the clock.**", "**Go's command line quirks are not copied.**", ]; @@ -887,6 +891,44 @@ fn garbled() -> Vec { "--simple", ], ), + // A probe answer that breaks off: the status still decides, and only + // a 200 is reported by the read that failed, in each client's words. + listed("truncated_404_probe", GARBLED, &["--server", "4", "--json", "--debug"]), + listed("stalled_404_probe", GARBLED, &["--timeout", "1", "--server", "5", "--json", "--debug"]), + listed("truncated_probe", GARBLED, &["--server", "6", "--json", "--debug"]) + .stderr( + &[Why::ErrorCauses], + &[ + "Terminated due to error: Selected server Truncated probe (127.0.0.1) is not responding: reading the ping response: unexpected EOF", + ], + &[ + "Terminated due to error: Selected server Truncated probe (127.0.0.1) is not responding: reading the ping response: error reading a body from connection: end of file before message length reached", + ], + ), + listed("stalled_probe", GARBLED, &["--timeout", "1", "--server", "7", "--json", "--debug"]) + .stderr( + &[Why::ErrorCauses], + &[ + "Terminated due to error: Selected server Stalled probe (127.0.0.1) is not responding: reading the ping response: context deadline exceeded (Client.Timeout or context cancellation while reading body)", + ], + &[ + "Terminated due to error: Selected server Stalled probe (127.0.0.1) is not responding: reading the ping response: request timed out after 1s", + ], + ), + listed("no_content_probe", GARBLED, &["--server", "8", "--json", "--debug"]), + listed("chunked_404_probe", GARBLED, &["--server", "9", "--json", "--debug"]), + listed("chunked_probe", GARBLED, &["--server", "10", "--json", "--debug"]) + .stderr( + &[Why::ErrorCauses], + &[ + "Terminated due to error: Selected server Chunked probe (127.0.0.1) is not responding: reading the ping response: unexpected EOF", + ], + &[ + "Terminated due to error: Selected server Chunked probe (127.0.0.1) is not responding: reading the ping response: error reading a body from connection: unexpected EOF during chunk size line", + ], + ), + // Both read 512 bytes of a probe and quote them. + listed("long_probe", GARBLED, &["--server", "11", "--json", "--debug"]), telemetry("telemetry_500_id", "/telemetry-500-id.php") .stdout( &[Why::TelemetryStatus], diff --git a/tests/parity/fixture.rs b/tests/parity/fixture.rs index e2bb0e9..4cc27f7 100644 --- a/tests/parity/fixture.rs +++ b/tests/parity/fixture.rs @@ -51,6 +51,15 @@ pub enum Variant { /// - `POST /telemetry-500-id.php`: 500 with a body shaped like a share /// reply. /// - `POST /telemetry-500-page.php`: 500 with an HTML error page. + /// - `GET /truncated-404.php` and `GET /truncated-ping.php`: 404 and 200 + /// with ten bytes of a promised 64, then a hang-up. + /// - `GET /stalled-404.php` and `GET /stalled-ping.php`: the same ten + /// bytes, then [`STALL`] of silence before the hang-up. + /// - `GET /no-content.php`: 204. + /// - `GET /chunked-404.php` and `GET /chunked-ping.php`: 404 and 200 with + /// a chunked body that hangs up after its first chunk. + /// - `GET /long-ping.php`: 200 with 4096 bytes, more than either client + /// reads of a probe. Garbled, } @@ -211,10 +220,54 @@ fn respond(writer: &mut TcpStream, variant: Variant, request: &Request) -> io::R "text/html", b"Internal Server Error\n", ), + "/truncated-404.php" if get && garbled => broken_off(writer, "404 Not Found", false), + "/stalled-404.php" if get && garbled => broken_off(writer, "404 Not Found", true), + "/truncated-ping.php" if get && garbled => broken_off(writer, "200 OK", false), + "/stalled-ping.php" if get && garbled => broken_off(writer, "200 OK", true), + "/no-content.php" if get && garbled => send(writer, "204 No Content", "text/plain", b""), + "/chunked-404.php" if get && garbled => chunked_broken_off(writer, "404 Not Found"), + "/chunked-ping.php" if get && garbled => chunked_broken_off(writer, "200 OK"), + "/long-ping.php" if get && garbled => send( + writer, + "200 OK", + "text/plain", + &b"0123456789abcdef".repeat(256), + ), _ => send(writer, "404 Not Found", "text/plain", b""), } } +/// How long a stalled reply stays silent: longer than the one-second timeout +/// of the cases, with room for a slow runner. +const STALL: std::time::Duration = std::time::Duration::from_secs(5); + +/// Ten bytes of a promised 64, then a hang-up, after [`STALL`] of silence +/// with `stall`. +fn broken_off(writer: &mut TcpStream, status: &str, stall: bool) -> io::Result<()> { + let head = + format!("HTTP/1.1 {status}\r\nContent-Type: text/html\r\nContent-Length: 64\r\n\r\n"); + writer.write_all(head.as_bytes())?; + writer.write_all(b"0123456789")?; + writer.flush()?; + if stall { + std::thread::sleep(STALL); + } + // An error ends the connection's loop, which is the hang-up. + Err(io::ErrorKind::ConnectionAborted.into()) +} + +/// A chunked body that hangs up after its first chunk of ten bytes, before +/// the last chunk that would end it. +fn chunked_broken_off(writer: &mut TcpStream, status: &str) -> io::Result<()> { + let head = format!( + "HTTP/1.1 {status}\r\nContent-Type: text/html\r\nTransfer-Encoding: chunked\r\n\r\n" + ); + writer.write_all(head.as_bytes())?; + writer.write_all(b"a\r\n0123456789\r\n")?; + writer.flush()?; + Err(io::ErrorKind::ConnectionAborted.into()) +} + fn send(writer: &mut TcpStream, status: &str, content_type: &str, body: &[u8]) -> io::Result<()> { let head = format!( "HTTP/1.1 {status}\r\nContent-Type: {content_type}\r\nContent-Length: {}\r\n\r\n", diff --git a/tools/golden/main.go b/tools/golden/main.go index 6fc2a9e..d86e9fd 100644 --- a/tools/golden/main.go +++ b/tools/golden/main.go @@ -325,6 +325,11 @@ func instants() []instant { } { out = append(out, instant{base, ns, 7200}, instant{base, ns, 0}) } + // Nanoseconds of a whole second or more, which time.Unix carries into the + // seconds. The largest is the most a u32 holds. + for _, ns := range []int64{1000000000, 1999999999, 4294967295} { + out = append(out, instant{base, ns, 7200}, instant{base, ns, 0}) + } // Offsets: zero, whole hours, half and quarter hours, the extremes in // use, and ones a minute cannot express, which Go truncates. for _, off := range []int{