summaryrefslogtreecommitdiffstats
path: root/tests/production_surface.rs
blob: bcf3e92cb8526bf3d08dcf26837de8bdd894925d (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use std::path::{Path, PathBuf};

fn read(rel: &str) -> String {
    std::fs::read_to_string(Path::new(env!("CARGO_MANIFEST_DIR")).join(rel)).expect("read source")
}

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('\\', "/");
        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. Parse with `pkginfo`, not with the shim:\n{}",
        offenders.join("\n")
    );
}

#[test]
fn the_served_index_has_exactly_one_serialization_lock() {
    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");
    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"
        );
    }
}

#[test]
fn the_test_hooks_are_a_gated_submodule_not_config_fields() {
    let cfg_src = read("src/config.rs");
    for field in [
        "pub path_prepend",
        "pub shim_manifest",
        "pub shim_dir",
        "pub publish_fault",
        "pub shim_fail",
        "pub shim_kill_parent",
        "pub shim_plant_link",
    ] {
        let hits = cfg_src.matches(field).count();
        assert_eq!(hits, 1, "{field} must be declared once, on TestHooks — not on Config");
    }
    assert!(cfg_src.contains("pub struct TestHooks"), "the hooks must live in one named submodule");
    assert!(
        cfg_src.contains("#[cfg(feature = \"test-support\")]"),
        "the hooks must be compiled out without the test-support feature"
    );
}

#[test]
fn the_publish_hot_path_has_no_ungated_test_branch() {
    let src = read("src/pipeline/publish.rs");
    let lines: Vec<&str> = src.lines().collect();
    let mut gated = 0;
    for (i, line) in lines.iter().enumerate() {
        if line.contains("hooks.publish_fault") {
            let window = lines[i.saturating_sub(3)..i].join("\n");
            assert!(
                window.contains("#[cfg(feature = \"test-support\")]"),
                "fault injection at publish.rs:{} is compiled into production builds",
                i + 1
            );
            gated += 1;
        }
    }
    assert_eq!(gated, 2, "expected the two known fault points, found {gated}");
}

#[test]
fn a_default_config_has_no_hooks_attached() {
    let cfg = aur_build::config::Config::default();
    assert!(cfg.hooks.is_empty());
    let json = serde_json::to_string(&cfg).unwrap();
    assert!(!json.contains("publish_fault"), "hooks leaked into the serialized config: {json}");
}

#[test]
fn the_review_modules_do_not_keep_private_copies_of_the_rules() {
    for (file, forbidden) in [
        ("src/review/diff.rs", ["fn hosts(", "fn body_changed("]),
        ("src/review/risk.rs", ["fn hosts(", "fn body_changed("]),
    ] {
        let src = read(file);
        for f in forbidden {
            assert!(!src.contains(f), "{file} still defines {f} — use recipe::bodies_changed / urlutil::hosts");
        }
    }
    assert!(read("src/review/diff.rs").contains("urlutil::hosts"));
    assert!(read("src/review/risk.rs").contains("urlutil::hosts"));
    assert!(read("src/review/diff.rs").contains("recipe::bodies_changed"));
    assert!(read("src/review/risk.rs").contains("recipe::bodies_changed"));
}

#[test]
fn audit_rows_are_built_by_the_constructors_not_by_hand() {
    let src = read("src/review/approve.rs");
    assert!(!src.contains("db::AuditEntry {"), "audit rows must be built by AuditEntry::human/system");
    assert_eq!(src.matches("AuditEntry::human(").count(), 3, "approve, reject and withdraw");
}

#[test]
fn the_virtual_clock_is_compiled_out_without_test_support() {
    let src = read("src/time.rs");
    assert!(
        src.contains("#[cfg(feature = \"test-support\")]"),
        "the virtual clock must be behind the test-support feature"
    );
    let fallback = src
        .split("#[cfg(not(feature = \"test-support\"))]")
        .nth(1)
        .expect("without test-support there must be a clock module that is always real");
    assert!(
        fallback.contains("pub fn virtual_sleep() -> bool") && fallback.contains("false"),
        "the fallback must report a real wait so the seam compiles to a plain sleep"
    );
    assert!(
        !fallback.contains("Mutex"),
        "nothing may be locked on the wait path of a production build"
    );
}

#[test]
fn control_loops_wait_through_the_clock_seam() {
    for file in [
        "src/pipeline/dispatch.rs",
        "src/watch/scheduler.rs",
        "src/http/sse.rs",
        "src/sandbox/remote.rs",
    ] {
        let src = read(file);
        assert!(
            !src.contains("std::thread::sleep"),
            "{file} waits on the wall clock; go through crate::time so a test can drive it"
        );
        assert!(
            src.contains("crate::time::wait") || src.contains("crate::time::sleep"),
            "{file} never reaches the clock seam"
        );
    }
    let agent = read("src/sandbox/agent.rs");
    assert!(agent.contains("crate::time::sleep"), "the watchdog must use the seam");
    assert_eq!(
        agent.matches("std::thread::sleep").count(),
        1,
        "only the hub connect retry may sleep on the wall clock: it runs in the agent binary, which has no test-support to virtualize"
    );
}