use crate::brain::tools::evolve::verify::{
asset_basename, expected_digest, is_allowed_download_host,
};
#[test]
fn github_release_hosts_are_allowed() {
assert!(is_allowed_download_host(
"https://github.com/adolfousier/opencrabs/releases/download/v0.5.0/opencrabs-v0.5.0-aarch64-apple-darwin.tar.gz"
));
assert!(is_allowed_download_host(
"https://objects.githubusercontent.com/github-production-release-asset/x/y"
));
}
#[test]
fn any_other_host_is_refused() {
assert!(!is_allowed_download_host(
"https://example.invalid/opencrabs.tar.gz"
));
assert!(!is_allowed_download_host(
"https://evil.com/opencrabs.tar.gz"
));
assert!(!is_allowed_download_host("https://github.com.evil.com/x"));
assert!(!is_allowed_download_host("https://notgithub.com/x"));
}
#[test]
fn plaintext_http_is_refused() {
assert!(!is_allowed_download_host(
"http://github.com/adolfousier/opencrabs/releases/download/v0.5.0/x.tar.gz"
));
}
const SUMS: &str = "\
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa opencrabs-v0.5.0-x86_64-unknown-linux-gnu.tar.gz
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb opencrabs-v0.5.0-aarch64-apple-darwin.tar.gz
";
#[test]
fn the_matching_asset_digest_is_returned() {
assert_eq!(
expected_digest(SUMS, "opencrabs-v0.5.0-aarch64-apple-darwin.tar.gz").as_deref(),
Some("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
);
}
#[test]
fn a_missing_asset_has_no_digest() {
assert!(expected_digest(SUMS, "opencrabs-v0.5.0-windows.zip").is_none());
}
#[test]
fn a_binary_mode_star_prefix_is_tolerated() {
let sums =
"cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc *opencrabs-x.tar.gz\n";
assert_eq!(
expected_digest(sums, "opencrabs-x.tar.gz").as_deref(),
Some("cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc")
);
}
#[test]
fn a_malformed_digest_is_rejected() {
let sums = "deadbeef opencrabs-x.tar.gz\nnothex_nothex_nothex_nothex_nothex_nothex_nothex_nothex_nothe opencrabs-y.tar.gz\n";
assert!(expected_digest(sums, "opencrabs-x.tar.gz").is_none());
assert!(expected_digest(sums, "opencrabs-y.tar.gz").is_none());
}
#[test]
fn the_asset_basename_is_the_url_filename() {
assert_eq!(
asset_basename("https://github.com/o/r/releases/download/v1/opencrabs-v1-linux.tar.gz")
.as_deref(),
Some("opencrabs-v1-linux.tar.gz")
);
assert!(asset_basename("https://github.com/o/r/releases/download/v1/").is_none());
}