Files
craft-libs/crates/update/tests/https_only.rs
T
aiongg 6e308319c0 update: local-mirror, the feature that accepts a developer's loopback mirror
ArtCraft Launcher's development builds install from a mirror on this computer (its `local-mirror` feature, `cargo xtask local serve`). With the feature, `check_download_link` accepts loopback links and `Http::new` leaves the agents for remote hosts off HTTPS-only.

Without it nothing changes: a build refuses a loopback download link and the remote agents refuse plain HTTP before they connect. `default`, `ui`, `screen-testing`, `suite` and `oci_migration` do not turn it on; the facade forwards it only as its own `update-local-mirror`, which no other facade feature turns on.
2026-11-25 17:06:02 +00:00

54 lines
2.5 KiB
Rust

//! `Http::new` as an app gets it, through the public API only: the release configuration, or the `local-mirror` one with
//! that feature on.
use craft_ui::testing::trial;
use std::io::{BufRead, BufReader, Write};
use std::net::TcpListener;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use craft_update::http::{Fetched, Http};
/// A plain-HTTP server on loopback that answers every request with `{}`; counts the requests.
fn serve() -> (String, Arc<AtomicUsize>) {
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
let base = format!("http://{}", listener.local_addr().unwrap());
let seen = Arc::new(AtomicUsize::new(0));
let count = seen.clone();
std::thread::spawn(move || {
for stream in listener.incoming() {
let Ok(mut stream) = stream else { return };
let mut reader = BufReader::new(stream.try_clone().unwrap());
let mut line = String::new();
while reader.read_line(&mut line).unwrap_or(0) > 0 && line != "\r\n" {
line.clear();
}
count.fetch_add(1, Ordering::SeqCst);
let _ = write!(stream, "HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\n{{}}");
}
});
(base, seen)
}
#[trial]
fn the_remote_agents_take_plain_http_only_with_a_local_mirror_and_loopback_links_use_the_direct_agents() {
let (base, seen) = serve();
let http = Http::new("craft-update-tests/0").unwrap();
// A loopback link goes to the direct agents, which serve plain HTTP in every build.
let api = http.get_api(&format!("{base}/repos/storytold/x/releases"), None);
assert!(matches!(api, Ok(Fetched::Body { .. })), "{api:?}");
let redirect = http.redirect_target(&format!("{base}/releases/latest"));
assert!(matches!(redirect, Err(ref e) if e.contains("HTTP 200")), "{redirect:?}");
assert_eq!(http.get_text(&format!("{base}/SHA256SUMS.txt")).unwrap().as_deref(), Some("{}"));
assert_eq!(seen.load(Ordering::SeqCst), 3);
// Any other plain-HTTP link is refused before it connects, in debug builds too. An upper-case scheme isn't a
// loopback link, so it goes to a remote agent.
let remote = http.get_text(&format!("HTTP{}", &base[4..]));
if cfg!(feature = "local-mirror") {
assert_eq!(remote.unwrap().as_deref(), Some("{}"));
} else {
assert!(remote.as_ref().is_err_and(|e| e.contains("refused plain HTTP")), "{remote:?}");
assert_eq!(seen.load(Ordering::SeqCst), 3, "a refused request never connects");
}
}