#![cfg(all(feature = "gguf", feature = "onnx"))]
use std::path::PathBuf;
use std::process::Command;
#[derive(Clone, Copy)]
enum Format {
Gguf,
Onnx,
}
struct Pinned {
name: &'static str,
url: &'static str,
file_sha256: &'static str,
kappa: &'static str,
format: Format,
}
const MODELS: &[Pinned] = &[
Pinned {
name: "qwen2-0_5b-instruct-q8_0.gguf",
url: "https://huggingface.co/Qwen/Qwen2-0.5B-Instruct-GGUF/resolve/main/qwen2-0_5b-instruct-q8_0.gguf",
file_sha256: "834f4115ad5a836c9f17716b1577290fda96de3deb881ba45a4d5476fd202e96",
kappa: "sha256:66c2ea8fa51317c6da91d10f131a5de64d45cb859edaf7a4f8d2557277f45b2d",
format: Format::Gguf,
},
Pinned {
name: "mobilenetv2-7.onnx",
url: "https://media.githubusercontent.com/media/onnx/models/main/validated/vision/classification/mobilenet/model/mobilenetv2-7.onnx",
file_sha256: "c1c513582d56afceff8516c73804e484c81c6a830712ab6d682253f4a3cd042f",
kappa: "sha256:f71c815228869e2c56ad00fcf4691ffbad45ecb72a503eef35cbaabe40287378",
format: Format::Onnx,
},
Pinned {
name: "all-MiniLM-L6-v2.onnx",
url: "https://huggingface.co/Xenova/all-MiniLM-L6-v2/resolve/main/onnx/model.onnx",
file_sha256: "759c3cd2b7fe7e93933ad23c4c9181b7396442a2ed746ec7c1d46192c469c46e",
kappa: "sha256:a036c7fec3409bb71116dcf79a37ce368166898b8621a75bc19299340e127422",
format: Format::Onnx,
},
];
fn live() -> bool {
std::env::var("UOR_ADDR_LIVE").as_deref() == Ok("1")
}
fn cache_dir() -> PathBuf {
PathBuf::from(concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/fixtures/models"
))
}
fn file_sha256_hex(path: &PathBuf) -> String {
let out = Command::new("sha256sum")
.arg(path)
.output()
.expect("run sha256sum");
assert!(out.status.success(), "sha256sum failed");
String::from_utf8(out.stdout).unwrap()[..64].to_string()
}
fn ensure_cached(m: &Pinned) -> PathBuf {
let path = cache_dir().join(m.name);
if !path.exists() {
std::fs::create_dir_all(cache_dir()).unwrap();
let status = Command::new("curl")
.args(["-sL", "--fail", "-o"])
.arg(&path)
.arg(m.url)
.status()
.expect("run curl");
assert!(status.success(), "download failed for {}", m.name);
}
let got = file_sha256_hex(&path);
assert_eq!(
got, m.file_sha256,
"{}: cached file SHA-256 does not match the pin — corrupted or \
substituted model (delete the cache file to re-download)",
m.name
);
path
}
fn python_label(m: &Pinned, path: &PathBuf) -> String {
let tool = match m.format {
Format::Gguf => concat!(env!("CARGO_MANIFEST_DIR"), "/../../tools/canonical-gguf.py"),
Format::Onnx => concat!(env!("CARGO_MANIFEST_DIR"), "/../../tools/canonical-onnx.py"),
};
let out = Command::new("python3")
.arg(tool)
.arg(path)
.output()
.expect("run python canonical tool");
assert!(
out.status.success(),
"python tool failed for {}: {}",
m.name,
String::from_utf8_lossy(&out.stderr)
);
String::from_utf8(out.stdout).unwrap().trim().to_string()
}
#[test]
#[ignore = "CM-EXT: requires UOR_ADDR_LIVE=1 + network (~635 MB) + python3"]
fn pinned_external_models_validate_and_verify() {
if !live() {
return;
}
for m in MODELS {
let path = ensure_cached(m);
let bytes = std::fs::read(&path).unwrap();
let (outcome, skeleton) = match m.format {
Format::Gguf => (
uor_addr::gguf::address(&bytes).expect("gguf address"),
uor_addr::gguf::canonicalize(&bytes).expect("gguf canonicalize"),
),
Format::Onnx => (
uor_addr::onnx::address(&bytes).expect("onnx address"),
uor_addr::onnx::canonicalize(&bytes).expect("onnx canonicalize"),
),
};
assert_eq!(
outcome.address.as_str(),
m.kappa,
"{}: Rust κ-label",
m.name
);
assert_eq!(
python_label(m, &path),
m.kappa,
"{}: Python κ-label",
m.name
);
if bytes.len() >= (1 << 20) {
assert!(
skeleton.len() * 20 < bytes.len(),
"{}: skeleton {} not << model {} (streaming/bounded-carrier property)",
m.name,
skeleton.len(),
bytes.len()
);
}
assert_eq!(
outcome.witness.verify().expect("replay verify"),
outcome.address,
"{}: witness verify",
m.name
);
}
}