| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
use std::fmt::Write as _; |
| 18 |
use std::fs; |
| 19 |
use std::path::{Path, PathBuf}; |
| 20 |
|
| 21 |
use sha2::{Digest, Sha256}; |
| 22 |
|
| 23 |
const BASES_DIR: &str = "static/bases"; |
| 24 |
|
| 25 |
|
| 26 |
fn mirrored_files() -> Vec<PathBuf> { |
| 27 |
let dir = Path::new(env!("CARGO_MANIFEST_DIR")).join(BASES_DIR); |
| 28 |
let mut out: Vec<PathBuf> = fs::read_dir(&dir) |
| 29 |
.expect("static/bases/ is readable") |
| 30 |
.map(|entry| entry.expect("readable dir entry").path()) |
| 31 |
.filter(|path| path.is_file()) |
| 32 |
.filter(|path| path.file_name().is_some_and(|n| n != "README.md")) |
| 33 |
.collect(); |
| 34 |
out.sort(); |
| 35 |
out |
| 36 |
} |
| 37 |
|
| 38 |
fn sha256_hex(bytes: &[u8]) -> String { |
| 39 |
Sha256::digest(bytes) |
| 40 |
.iter() |
| 41 |
.fold(String::new(), |mut out, b| { |
| 42 |
let _ = write!(out, "{b:02x}"); |
| 43 |
out |
| 44 |
}) |
| 45 |
} |
| 46 |
|
| 47 |
fn file_name(path: &Path) -> String { |
| 48 |
path.file_name() |
| 49 |
.expect("mirror path has a file name") |
| 50 |
.to_string_lossy() |
| 51 |
.into_owned() |
| 52 |
} |
| 53 |
|
| 54 |
#[test] |
| 55 |
fn every_mirrored_base_is_named_by_its_digest() { |
| 56 |
let files = mirrored_files(); |
| 57 |
assert!( |
| 58 |
!files.is_empty(), |
| 59 |
"static/bases/ holds no mirrored files. An empty mirror sends every \ |
| 60 |
Alloy build back to the upstream host.", |
| 61 |
); |
| 62 |
|
| 63 |
let mut wrong: Vec<String> = Vec::new(); |
| 64 |
for path in &files { |
| 65 |
let name = file_name(path); |
| 66 |
let bytes = fs::read(path).expect("read mirrored base"); |
| 67 |
let digest = sha256_hex(&bytes); |
| 68 |
if digest != name { |
| 69 |
wrong.push(format!("{name} hashes to {digest}")); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
assert!( |
| 74 |
wrong.is_empty(), |
| 75 |
"mirrored files whose name is not their sha256: {wrong:?}\n\ |
| 76 |
`quasi-type` fetches by digest, so a file filed under any other name \ |
| 77 |
is unreachable. Rename it to the digest reported here, or restore the \ |
| 78 |
bytes if they were corrupted.", |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
#[test] |
| 83 |
fn readme_table_lists_exactly_the_mirrored_files() { |
| 84 |
let readme = Path::new(env!("CARGO_MANIFEST_DIR")) |
| 85 |
.join(BASES_DIR) |
| 86 |
.join("README.md"); |
| 87 |
let text = fs::read_to_string(&readme).expect("static/bases/README.md is readable"); |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
let mut documented: Vec<String> = text |
| 92 |
.lines() |
| 93 |
.filter_map(|line| line.strip_prefix("| `")) |
| 94 |
.filter_map(|rest| rest.split('`').next()) |
| 95 |
.map(str::to_owned) |
| 96 |
.collect(); |
| 97 |
documented.sort(); |
| 98 |
|
| 99 |
let present: Vec<String> = mirrored_files().iter().map(|p| file_name(p)).collect(); |
| 100 |
|
| 101 |
assert_eq!( |
| 102 |
documented, present, |
| 103 |
"the README table and static/bases/ disagree.\n\ |
| 104 |
Adding a base to `pins.toml` means adding its files here and a row \ |
| 105 |
each; the row says which upstream file the digest is.", |
| 106 |
); |
| 107 |
} |
| 108 |
|