crates/dwarves-slsa/tests/subjects.rs
//! Subject hashing: every regular file of the out directory, nothing else.
use dwarves_slsa::subjects::{sha256_file, sha256_hex, subjects_of_dir};
#[test]
fn sha256_of_abc_is_the_fips_vector() {
assert_eq!(
sha256_hex(b"abc"),
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
);
}
#[test]
fn a_file_hashes_as_its_bytes_even_past_one_buffer() {
let d = tempfile::tempdir().unwrap();
let bytes: Vec<u8> = (0..300_000u32).map(|i| (i % 251) as u8).collect();
let p = d.path().join("big");
std::fs::write(&p, &bytes).unwrap();
assert_eq!(sha256_file(&p).unwrap(), sha256_hex(&bytes));
}
#[test]
fn every_regular_file_is_a_subject_by_its_relative_path_sorted() {
let d = tempfile::tempdir().unwrap();
std::fs::create_dir_all(d.path().join("crates")).unwrap();
std::fs::create_dir_all(d.path().join("bin")).unwrap();
std::fs::write(d.path().join("crates/b-1.0.0.crate"), b"b").unwrap();
std::fs::write(d.path().join("crates/a-1.0.0.crate"), b"a").unwrap();
std::fs::write(d.path().join("bin/holger"), b"elf").unwrap();
let s = subjects_of_dir(d.path()).unwrap();
let names: Vec<&str> = s.iter().map(|s| s.name.as_str()).collect();
assert_eq!(
names,
vec!["bin/holger", "crates/a-1.0.0.crate", "crates/b-1.0.0.crate"]
);
assert_eq!(s[1].sha256_hex().unwrap(), sha256_hex(b"a"));
assert_eq!(s[0].digest.len(), 1, "sha256 only");
}
#[test]
fn a_symlink_in_the_out_directory_is_refused() {
let d = tempfile::tempdir().unwrap();
std::fs::write(d.path().join("real"), b"x").unwrap();
std::os::unix::fs::symlink("/etc/hostname", d.path().join("link")).unwrap();
let e = subjects_of_dir(d.path()).unwrap_err();
assert!(e.0.contains("link"), "{e}");
}
#[test]
fn an_empty_out_directory_has_no_subjects() {
let d = tempfile::tempdir().unwrap();
assert!(subjects_of_dir(d.path()).unwrap().is_empty());
}
#[test]
fn a_missing_out_directory_is_refused() {
let d = tempfile::tempdir().unwrap();
assert!(subjects_of_dir(&d.path().join("nope")).is_err());
}