use std::fmt::Write as _;
use sha2::{Digest as _, Sha256};
use super::{LockedPackage, Lockfile};
const CHECKSUM: &str = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
fn package(name: &str, version: &str) -> LockedPackage {
LockedPackage {
checksum: CHECKSUM.to_string(),
name: name.to_string(),
version: version.to_string(),
}
}
#[test]
fn parse_keeps_only_valid_crates_io_packages() {
let contents = format!(
r#"version = 4
[[package]]
name = "app"
version = "0.1.0"
dependencies = ["leaf"]
[[package]]
name = "leaf"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "{CHECKSUM}"
[[package]]
name = "leaf"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "{CHECKSUM}"
[[package]]
name = "Sparse_Leaf"
version = "2.0.0-rc.1"
source = "sparse+https://index.crates.io/"
checksum = "{CHECKSUM}"
[[package]]
name = "gitdep"
version = "1.0.0"
source = "git+ssh://git@example.com/private/gitdep#0123"
[[package]]
name = "private"
version = "1.0.0"
source = "registry+https://registry.example.com/index"
checksum = "{CHECKSUM}"
[[package]]
name = "../escape"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "{CHECKSUM}"
[[package]]
name = "short"
version = "1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "{CHECKSUM}"
[[package]]
name = "upper"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "{}"
[[package]]
name = "unsummed"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
"#,
CHECKSUM.to_uppercase()
);
let lockfile = Lockfile::parse(contents.as_bytes()).expect("lockfile");
assert_eq!(
lockfile.packages,
[
package("Sparse_Leaf", "2.0.0-rc.1"),
package("leaf", "1.0.0")
]
);
assert_eq!(
lockfile.digest,
hex::encode(Sha256::digest(contents.as_bytes()))
);
}
#[test]
fn parse_rejects_malformed_and_oversized_lockfiles() {
let mut oversized = String::new();
for index in 0..=20_000 {
let _ = write!(
oversized,
"[[package]]\nname = \"p{index}\"\nversion = \"1.0.0\"\n"
);
}
let invalid_utf8 = Lockfile::parse(&[0xff, 0xfe]);
let invalid_toml = Lockfile::parse(b"[[package]\n");
let too_many = Lockfile::parse(oversized.as_bytes());
let empty = Lockfile::parse(b"version = 4\n");
assert!(invalid_utf8.is_none() && invalid_toml.is_none() && too_many.is_none());
assert_eq!(empty.expect("empty lockfile").packages, []);
}
#[test]
fn read_accepts_only_a_regular_file_within_the_limit() {
let root = tempfile::tempdir().expect("root");
let lockfile = root.path().join("Cargo.lock");
let mut outcomes = Vec::new();
outcomes.push(Lockfile::read(root.path()));
std::os::unix::fs::symlink("/etc/hosts", &lockfile).expect("link");
outcomes.push(Lockfile::read(root.path()));
std::fs::remove_file(&lockfile).expect("remove link");
let fifo = std::process::Command::new("mkfifo")
.arg(&lockfile)
.status()
.expect("mkfifo");
assert!(fifo.success());
outcomes.push(Lockfile::read(root.path()));
std::fs::remove_file(&lockfile).expect("remove FIFO");
std::fs::File::create(&lockfile)
.and_then(|file| file.set_len(17 * 1024 * 1024))
.expect("large lockfile");
outcomes.push(Lockfile::read(root.path()));
std::fs::write(&lockfile, "version = 4\n").expect("lockfile");
let regular = Lockfile::read(root.path());
assert_eq!(outcomes, [None, None, None, None]);
assert!(regular.is_some());
}
#[test]
fn packages_name_their_archive_and_index_entry() {
let names = ["a", "Ab", "abc", "Serde_Json"];
let entries = names.map(|name| package(name, "1.0.0").index_entry());
assert_eq!(
entries,
[
".cache/1/a",
".cache/2/ab",
".cache/3/a/abc",
".cache/se/rd/serde_json"
]
);
assert_eq!(package("leaf", "1.0.0").archive_name(), "leaf-1.0.0.crate");
}
#[test]
fn synthetic_workspace_pins_exactly_the_locked_packages() {
let lockfile = Lockfile {
digest: String::new(),
packages: vec![package("leaf", "1.0.0"), package("leaf", "2.0.0")],
};
let manifest = lockfile.synthetic_manifest();
let synthetic_lockfile = lockfile.synthetic_lockfile();
assert!(manifest.ends_with(
"[dependencies]\np0 = { package = \"leaf\", version = \"=1.0.0\", default-features = \
false }\np1 = { package = \"leaf\", version = \"=2.0.0\", default-features = false }\n"
));
assert!(manifest.starts_with("[package]\nname = \"agentty-cargo-cache\""));
assert_eq!(
synthetic_lockfile,
format!(
"version = 4\n\n[[package]]\nname = \"leaf\"\nversion = \"1.0.0\"\nsource = \
\"registry+https://github.com/rust-lang/crates.io-index\"\nchecksum = \
\"{CHECKSUM}\"\n\n[[package]]\nname = \"leaf\"\nversion = \"2.0.0\"\nsource = \
\"registry+https://github.com/rust-lang/crates.io-index\"\nchecksum = \"{CHECKSUM}\"\n"
)
);
}