111 lines
4.4 KiB
Rust
111 lines
4.4 KiB
Rust
//! Integration tests for `cargo xtask registry-sign`: sign, verify with the engine, tamper, refuse.
|
|
#![allow(clippy::unwrap_used, clippy::expect_used)]
|
|
|
|
use std::fs;
|
|
use std::path::{Path, PathBuf};
|
|
use std::process::{Command, Output};
|
|
|
|
use notescraft_engine::addons::{Ed25519, Index, Kind, Verifier};
|
|
use notescraft_engine::decode_base64;
|
|
|
|
/// Key id the tool signs with by default.
|
|
const KEY_ID: &str = "k1";
|
|
/// This crate's manifest, which lives inside the repository.
|
|
const REPO_CARGO_TOML: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/Cargo.toml");
|
|
|
|
/// A fresh scratch directory outside the repository.
|
|
fn scratch(name: &str) -> PathBuf {
|
|
let dir = std::env::temp_dir().join(format!("registry-sign-test-{}-{name}", std::process::id()));
|
|
let _ = fs::remove_dir_all(&dir);
|
|
fs::create_dir_all(&dir).unwrap();
|
|
dir
|
|
}
|
|
|
|
fn xtask(args: &[&str]) -> Output {
|
|
Command::new(env!("CARGO_BIN_EXE_xtask")).arg("registry-sign").args(args).output().unwrap()
|
|
}
|
|
|
|
fn path_arg(path: &Path) -> &str {
|
|
path.to_str().unwrap()
|
|
}
|
|
|
|
/// A registry checkout with one grammar that has two versions; `1.0.0` is the one indexed.
|
|
fn fixture(root: &Path) {
|
|
let item = root.join("files/grammar/twig");
|
|
fs::create_dir_all(item.join("1.0.0")).unwrap();
|
|
fs::create_dir_all(item.join("0.9.0")).unwrap();
|
|
fs::write(item.join("entry.toml"), "title = \"Twig\"\nlicense = \"MIT\"\nmin_app_version = \"0.1.0\"\naliases = [\"twig\"]\n").unwrap();
|
|
fs::write(item.join("0.9.0/twig.sublime-syntax"), "old\n").unwrap();
|
|
fs::write(item.join("1.0.0/twig.sublime-syntax"), "name: Twig\n").unwrap();
|
|
}
|
|
|
|
/// Generates a key outside the repository and returns `(key file, public key base64)`.
|
|
fn keygen(dir: &Path) -> (PathBuf, String) {
|
|
let key = dir.join("owner.key");
|
|
let out = xtask(&["keygen", "--out", path_arg(&key)]);
|
|
assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));
|
|
let public = String::from_utf8(out.stdout).unwrap().trim().to_owned();
|
|
(key, public)
|
|
}
|
|
|
|
fn public_bytes(public: &str) -> [u8; 32] {
|
|
decode_base64(public).unwrap().try_into().unwrap()
|
|
}
|
|
|
|
#[test]
|
|
fn keygen_prints_only_the_public_key_and_refuses_to_overwrite() {
|
|
let dir = scratch("keygen");
|
|
let (key, public) = keygen(&dir);
|
|
assert_eq!(public_bytes(&public).len(), 32);
|
|
let again = xtask(&["keygen", "--out", path_arg(&key)]);
|
|
assert!(!again.status.success(), "a second keygen must not replace the key");
|
|
fs::remove_dir_all(&dir).unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn signed_index_verifies_with_the_engine_and_check_catches_tampering() {
|
|
let dir = scratch("sign");
|
|
let reg = dir.join("registry");
|
|
fixture(®);
|
|
let (key, public) = keygen(&dir);
|
|
|
|
let signed = xtask(&["--dir", path_arg(®), "--key", path_arg(&key)]);
|
|
assert!(signed.status.success(), "{}", String::from_utf8_lossy(&signed.stderr));
|
|
|
|
let index = Index::parse(&fs::read(reg.join("index.json")).unwrap()).unwrap();
|
|
assert!(index.skipped.is_empty(), "{:?}", index.skipped);
|
|
let entry = index.find(Kind::Grammar, "twig").expect("twig is indexed");
|
|
assert_eq!(entry.version.to_string(), "1.0.0", "the highest version folder is indexed");
|
|
Ed25519::new(vec![(KEY_ID.to_owned(), public_bytes(&public))]).verify(entry).expect("the signature verifies");
|
|
|
|
let check = |extra: &[&str]| {
|
|
let mut args = vec!["--check", "--dir", path_arg(®), "--public-key", public.as_str()];
|
|
args.extend_from_slice(extra);
|
|
xtask(&args)
|
|
};
|
|
assert!(check(&[]).status.success());
|
|
|
|
fs::write(reg.join("files/grammar/twig/1.0.0/twig.sublime-syntax"), "name: Tw1g\n").unwrap();
|
|
let tampered = check(&[]);
|
|
assert!(!tampered.status.success());
|
|
assert!(String::from_utf8_lossy(&tampered.stderr).contains("differs from the index"));
|
|
fs::remove_dir_all(&dir).unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn key_inside_the_repository_is_refused_and_nothing_is_written() {
|
|
let dir = scratch("refuse");
|
|
let reg = dir.join("registry");
|
|
fixture(®);
|
|
let out = xtask(&["--dir", path_arg(®), "--key", REPO_CARGO_TOML]);
|
|
assert!(!out.status.success());
|
|
assert!(String::from_utf8_lossy(&out.stderr).contains("inside a repository"));
|
|
assert!(!reg.join("index.json").exists());
|
|
|
|
let in_repo = concat!(env!("CARGO_MANIFEST_DIR"), "/should-not-exist.key");
|
|
let made = xtask(&["keygen", "--out", in_repo]);
|
|
assert!(!made.status.success());
|
|
assert!(!Path::new(in_repo).exists());
|
|
fs::remove_dir_all(&dir).unwrap();
|
|
}
|