//! Seal: every file in `static/bases/` still hashes to the name it is filed //! under, and the README table lists exactly those files. //! //! The directory is a content-addressed mirror of the font bases `quasi-type` //! pins, served by the `/static` `ServeDir` with no route of its own. Nothing //! else in the server reads it, so a rename, a truncation or a flipped byte //! ships without a compile error and without a failing request: the mirror //! answers 404 or serves bytes that fail `quasi-type`'s own digest check, and //! every Alloy build silently falls back to the upstream host the mirror exists //! to stop depending on. //! //! Hashing four files costs nothing, and the name already states the expected //! answer, so the check needs no fixture and no constant to maintain. //! //! Run with: cargo test --test bases_mirror use std::fmt::Write as _; use std::fs; use std::path::{Path, PathBuf}; use sha2::{Digest, Sha256}; const BASES_DIR: &str = "static/bases"; /// The mirror's files, README excluded, sorted by name. fn mirrored_files() -> Vec { let dir = Path::new(env!("CARGO_MANIFEST_DIR")).join(BASES_DIR); let mut out: Vec = fs::read_dir(&dir) .expect("static/bases/ is readable") .map(|entry| entry.expect("readable dir entry").path()) .filter(|path| path.is_file()) .filter(|path| path.file_name().is_some_and(|n| n != "README.md")) .collect(); out.sort(); out } fn sha256_hex(bytes: &[u8]) -> String { Sha256::digest(bytes) .iter() .fold(String::new(), |mut out, b| { let _ = write!(out, "{b:02x}"); out }) } fn file_name(path: &Path) -> String { path.file_name() .expect("mirror path has a file name") .to_string_lossy() .into_owned() } #[test] fn every_mirrored_base_is_named_by_its_digest() { let files = mirrored_files(); assert!( !files.is_empty(), "static/bases/ holds no mirrored files. An empty mirror sends every \ Alloy build back to the upstream host.", ); let mut wrong: Vec = Vec::new(); for path in &files { let name = file_name(path); let bytes = fs::read(path).expect("read mirrored base"); let digest = sha256_hex(&bytes); if digest != name { wrong.push(format!("{name} hashes to {digest}")); } } assert!( wrong.is_empty(), "mirrored files whose name is not their sha256: {wrong:?}\n\ `quasi-type` fetches by digest, so a file filed under any other name \ is unreachable. Rename it to the digest reported here, or restore the \ bytes if they were corrupted.", ); } #[test] fn readme_table_lists_exactly_the_mirrored_files() { let readme = Path::new(env!("CARGO_MANIFEST_DIR")) .join(BASES_DIR) .join("README.md"); let text = fs::read_to_string(&readme).expect("static/bases/README.md is readable"); // Table rows are `| `` | |`; the digest is the only // backticked field on a row that starts one. let mut documented: Vec = text .lines() .filter_map(|line| line.strip_prefix("| `")) .filter_map(|rest| rest.split('`').next()) .map(str::to_owned) .collect(); documented.sort(); let present: Vec = mirrored_files().iter().map(|p| file_name(p)).collect(); assert_eq!( documented, present, "the README table and static/bases/ disagree.\n\ Adding a base to `pins.toml` means adding its files here and a row \ each; the row says which upstream file the digest is.", ); }