summaryrefslogtreecommitdiffstats
path: root/tests/production_surface.rs
blob: 5ade95beb7fbb48d9b1a1b6b96ccd30500a61fb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//! The production surface cannot see the test scaffolding (§6 M-4, M-9).
//!
//! The shim harness is load-bearing: the whole step machine is tested by driving
//! the real binaries with fakes for `makepkg`, `mkarchroot`, `gpg` and friends.
//! That is fine. What is not fine is production code reaching into it — the
//! publish path once read a package's name through the shim module's copy of the
//! `.PKGINFO` parser, which meant the bytes we signed and the bytes the tests
//! believed were parsed by two pieces of code that could drift apart.
//!
//! The compile-time half of this rule is real: `shims` is compiled out unless the
//! `test-support` feature is on, and only this crate's own dev-dependency turns
//! it on. This test covers the half the compiler cannot: a module in `src/`
//! naming the scaffolding directly.

use std::path::{Path, PathBuf};

fn rust_files(dir: &Path, out: &mut Vec<PathBuf>) {
    for entry in std::fs::read_dir(dir).expect("read_dir") {
        let path = entry.expect("entry").path();
        if path.is_dir() {
            rust_files(&path, out);
        } else if path.extension().is_some_and(|e| e == "rs") {
            out.push(path);
        }
    }
}

#[test]
fn no_production_module_references_the_shim_harness() {
    let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("src");
    let mut files = Vec::new();
    rust_files(&root, &mut files);

    let mut offenders: Vec<String> = Vec::new();
    for file in files {
        let rel = file.strip_prefix(&root).unwrap_or(&file).to_string_lossy().replace('\\', "/");
        // The harness itself, and the shim binaries that are its front ends, are
        // allowed to know about it.
        if rel.starts_with("shims") || rel.starts_with("bin/") {
            continue;
        }
        let text = std::fs::read_to_string(&file).expect("read source");
        for (no, line) in text.lines().enumerate() {
            let t = line.trim_start();
            if t.starts_with("//") {
                continue;
            }
            if line.contains("shims::") {
                offenders.push(format!("{rel}:{}: {t}", no + 1));
            }
        }
    }
    assert!(
        offenders.is_empty(),
        "production code must not depend on the test harness (§6 M-4). Parse with `pkginfo`, not with the shim:\n{}",
        offenders.join("\n")
    );
}

#[test]
fn the_served_index_has_exactly_one_serialization_lock() {
    // §6 M-5: two mutexes in `publish.rs`, each documented as *the* "one publish
    // per repo" guarantee, with `withdraw_package` taking only one of them, is
    // worse than either answer — a reader cannot tell whether the guarantee is
    // redundant or incomplete. There is one lock now, and this keeps it that way.
    let src = std::fs::read_to_string(Path::new(env!("CARGO_MANIFEST_DIR")).join("src/pipeline/publish.rs"))
        .expect("read publish.rs");
    let mutexes = src
        .lines()
        .filter(|l| l.contains("static") && l.contains("Mutex<"))
        .collect::<Vec<_>>();
    assert_eq!(mutexes.len(), 1, "publish.rs must declare exactly one serialization mutex, found {mutexes:?}");
    assert!(!src.contains("PUBLISH_LOCK"), "the second lock must stay deleted");
    // And both writers of the served index take it, not just one of them.
    let acquisitions = src
        .lines()
        .filter(|l| l.contains("lock_repo()") && !l.trim_start().starts_with("fn "))
        .count();
    assert!(
        acquisitions >= 2,
        "publish and withdraw_package must both take the repo lock; found {acquisitions} acquisitions"
    );
}

#[test]
fn the_shim_binaries_are_test_only() {
    let manifest = std::fs::read_to_string(Path::new(env!("CARGO_MANIFEST_DIR")).join("Cargo.toml"))
        .expect("read Cargo.toml");
    for tool in ["mkarchroot", "makechrootpkg", "makepkg", "repo-add", "gpg", "pacman"] {
        let block = manifest
            .split("[[bin]]")
            .find(|b| b.contains(&format!("name = \"{tool}\"")))
            .unwrap_or_else(|| panic!("no [[bin]] for {tool}"));
        assert!(
            block.contains("required-features = [\"test-support\"]"),
            "the {tool} shim is a test binary and must not be built for a production target"
        );
    }
}