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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
//! The §16a test harness itself: toolchain shims and the fake-package corpus.
//!
//! If these tests fail, nothing above the harness can be trusted.
mod common;
use std::path::{Path, PathBuf};
use std::process::Command;
use common::{corpus_root, env_with_fixture, shim_dir, Env};
use aur_build::shims;
/// Run a shim binary with the harness protocol in the environment.
fn shim(tool: &str, cwd: &Path, envs: &[(&str, &Path)], args: &[&str]) -> (i32, String, String) {
let exe = shim_dir().join(tool);
assert!(exe.exists(), "shim {tool} not built at {}", exe.display());
let out = Command::new(&exe)
.current_dir(cwd)
.envs(
envs
.iter()
.map(|(k, v)| (*k, v.as_os_str().to_os_string())),
)
.args(args)
.output()
.expect("run shim");
(
out.status.code().unwrap_or(-1),
String::from_utf8_lossy(&out.stdout).into_owned(),
String::from_utf8_lossy(&out.stderr).into_owned(),
)
}
fn fixture_manifest(name: &str) -> PathBuf {
corpus_root().join(name).join("manifest.json")
}
#[test]
fn makechrootpkg_emits_pkginfo_buildinfo_and_sig() {
let tmp = tempfile::tempdir().unwrap();
let build = tmp.path().join("build");
std::fs::create_dir_all(&build).unwrap();
std::fs::write(
build.join("PKGBUILD"),
common::pkgbuild("plain-make", "1.0", "1", ""),
)
.unwrap();
let log = tmp.path().join("shimlog");
std::fs::create_dir_all(&log).unwrap();
// Exactly the invocation §8.1 step 5 makes.
let chroot_args = ["-c", "-r", "chroot", "--", "-siTESTKEY", "-f"];
let (code, out, err) = shim(
"makechrootpkg",
&build,
&[("AUR_SHIM_MANIFEST", &fixture_manifest("plain-make")), ("AUR_SHIM_DIR", &log)],
&chroot_args,
);
assert_eq!(code, 0, "stdout={out} stderr={err}");
let pkg = build.join("plain-make-1.0-1-x86_64.pkg.tar.zst");
assert!(pkg.exists(), "artifact left in the build dir like the real tool does");
let bytes = std::fs::read(&pkg).unwrap();
let pkginfo = shims::read_pkginfo(&bytes).unwrap();
assert!(pkginfo.contains("pkgname = plain-make"));
assert!(pkginfo.contains("pkgver = 1.0-1"));
assert!(pkginfo.contains("pkgarch = x86_64"));
let buildinfo = shims::read_buildinfo(&bytes).unwrap();
assert!(buildinfo.contains("format = 2"));
assert!(buildinfo.contains("installed ="), "BUILDINFO carries the chroot world (§10.1)");
assert!(buildinfo.contains(" glibc 2.39-1 x86_64 -"));
assert!(build.with_extension("pkg.tar.zst.sig").exists() || pkg.with_extension("pkg.tar.zst.sig").exists());
assert_eq!(
shims::invocations(&log)
.iter()
.filter(|e| e["tool"] == "makechrootpkg")
.count(),
1,
"invocation recorded for the pipeline to assert on"
);
}
#[test]
fn makechrootpkg_refuses_a_missing_pkgbuild() {
// Step 0 materializes from the mirror; if that silently produced nothing,
// the fake tool must notice rather than build an empty package.
let tmp = tempfile::tempdir().unwrap();
let build = tmp.path().join("build");
std::fs::create_dir_all(&build).unwrap();
let (code, _, err) = shim(
"makechrootpkg",
&build,
&[("AUR_SHIM_MANIFEST", &fixture_manifest("plain-make"))],
&["-c", "-r", "chroot"],
);
assert_eq!(code, 1);
assert!(err.contains("no PKGBUILD"), "{err}");
}
#[test]
fn scripted_failures_are_honoured() {
for (fixture, expect) in [("build-fails", 1), ("check-fails", 1), ("plain-make", 0)] {
let tmp = tempfile::tempdir().unwrap();
let build = tmp.path().join("build");
std::fs::create_dir_all(&build).unwrap();
std::fs::write(
build.join("PKGBUILD"),
std::fs::read_to_string(corpus_root().join(fixture).join("PKGBUILD")).unwrap(),
)
.unwrap();
let (code, _, err) = shim(
"makechrootpkg",
&build,
&[("AUR_SHIM_MANIFEST", &fixture_manifest(fixture))],
&["-c", "-r", "chroot"],
);
assert_eq!(code, expect, "{fixture}: {err}");
}
}
#[test]
fn repo_add_writes_a_pacman_database() {
let tmp = tempfile::tempdir().unwrap();
let build = tmp.path().join("build");
std::fs::create_dir_all(&build).unwrap();
std::fs::write(build.join("PKGBUILD"), common::pkgbuild("plain-make", "1.0", "1", "")).unwrap();
shim(
"makechrootpkg",
&build,
&[("AUR_SHIM_MANIFEST", &fixture_manifest("plain-make"))],
&["-c", "-r", "chroot"],
);
let pkg = build.join("plain-make-1.0-1-x86_64.pkg.tar.zst");
let db = tmp.path().join("aur-build.db.tar.gz");
let out = Command::new(shim_dir().join("repo-add"))
.current_dir(tmp.path())
.env("AUR_SHIM_MANIFEST", fixture_manifest("plain-make"))
.args(["-R", "-s", "-p", "TESTKEY", &db.display().to_string(), &pkg.display().to_string()])
.output()
.unwrap();
assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));
assert!(db.exists(), "database written");
// The db is a gzip'd tar with one DESC per package — what pacman downloads.
let dec = flate2::read::GzDecoder::new(std::fs::File::open(&db).unwrap());
let mut ar = tar::Archive::new(dec);
let names: Vec<String> = ar
.entries()
.unwrap()
.map(|e| e.unwrap().path().unwrap().display().to_string())
.collect();
assert!(names.iter().any(|n| n == "plain-make/DESC"), "{names:?}");
let desc = {
let dec = flate2::read::GzDecoder::new(std::fs::File::open(&db).unwrap());
let mut ar = tar::Archive::new(dec);
let mut found = String::new();
for e in ar.entries().unwrap() {
let mut e = e.unwrap();
if e.path().unwrap().ends_with("plain-make/DESC") {
use std::io::Read;
e.read_to_string(&mut found).unwrap();
}
}
found
};
assert!(desc.contains("%FILENAME%"));
assert!(desc.contains("plain-make-1.0-1-x86_64.pkg.tar.zst"));
assert!(desc.contains("%VERSION%\n1.0-1"));
assert!(desc.contains("%SHA256SUM%"));
}
#[test]
fn gpg_writes_a_detached_signature() {
let tmp = tempfile::tempdir().unwrap();
let f = tmp.path().join("thing.pkg.tar.zst");
std::fs::write(&f, b"bytes").unwrap();
let sig = tmp.path().join("thing.pkg.tar.zst.sig");
let out = Command::new(shim_dir().join("gpg"))
.current_dir(tmp.path())
.args(["--batch", "--detach-sign", "--output", &sig.display().to_string(), &f.display().to_string()])
.output()
.unwrap();
assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));
let body = std::fs::read_to_string(&sig).unwrap();
assert!(body.contains("PGP SIGNATURE"));
assert!(body.contains(&aur_build::hash::hex(b"bytes")));
}
#[test]
fn mkarchroot_creates_a_warm_root() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path().join("chroot/extra-x86_64");
let out = Command::new(shim_dir().join("mkarchroot"))
.args(["-C", "/dev/null", &root.display().to_string()])
.output()
.unwrap();
assert!(out.status.success());
assert!(root.join("SHIM-ROOT").exists());
}
/// Every fixture in the corpus must be drivable through the shims. This is the
/// executable documentation of the §16 edge cases.
#[test]
fn every_fixture_is_drivable_through_the_shims() {
let mut names = Vec::new();
for entry in std::fs::read_dir(corpus_root()).unwrap() {
let dir = entry.unwrap().path();
if !dir.is_dir() || !dir.join("manifest.json").exists() {
continue;
}
let name = dir.file_name().unwrap().to_string_lossy().to_string();
names.push(name.clone());
let tmp = tempfile::tempdir().unwrap();
let build = tmp.path().join("build");
std::fs::create_dir_all(&build).unwrap();
for f in std::fs::read_dir(&dir).unwrap() {
let p = f.unwrap().path();
if p.file_name().unwrap() == "manifest.json" {
continue;
}
std::fs::copy(&p, build.join(p.file_name().unwrap())).unwrap();
}
let manifest = dir.join("manifest.json");
let want_fail = serde_json::from_str::<serde_json::Value>(&std::fs::read_to_string(&manifest).unwrap())
.unwrap()["build"]
.as_str()
.unwrap_or("succeed")
!= "succeed";
let out = Command::new(shim_dir().join("makechrootpkg"))
.current_dir(&build)
.env("AUR_SHIM_MANIFEST", &manifest)
.args(["-c", "-r", "chroot", "--", "-siTESTKEY", "-f"])
.output()
.unwrap();
if want_fail {
assert!(!out.status.success(), "{name} should fail");
} else {
assert!(out.status.success(), "{name}: {}", String::from_utf8_lossy(&out.stderr));
let pkgs: Vec<_> = std::fs::read_dir(&build)
.unwrap()
.filter_map(|e| e.ok())
.map(|e| e.file_name().to_string_lossy().into_owned())
.filter(|n| n.ends_with(".pkg.tar.zst"))
.collect();
assert!(!pkgs.is_empty(), "{name} produced no package");
}
}
for required in [
"plain-make",
"with-patch",
"install-hooks",
"skip-checksum",
"split-pkgbase",
"build-fails",
"check-fails",
"missing-dep",
"dep-cycle",
"epoch-bump",
"pkgrel-reset",
"downgrade",
"digest-drift",
"pin-drift",
"git-sourced",
"unresolvable",
] {
assert!(names.iter().any(|n| n == required), "missing fixture {required}");
}
}
/// The corpus doubles as detection fixtures: each one must be detectable through
/// the real watcher against its local git repo.
#[test]
fn fixtures_are_detected_through_the_watcher() {
for name in ["plain-make", "with-patch", "install-hooks", "skip-checksum", "split-pkgbase", "epoch-bump"] {
let (env, _fx) = env_with_fixture(name);
let rep = aur_build::watch::check_pkgbase(&env.cfg, &env.db, name).unwrap();
match &rep.outcome {
aur_build::watch::Outcome::Candidate { kind, .. } => {
assert_eq!(kind, "first_introduction", "{name}");
}
other => panic!("fixture {name}: expected a first_introduction candidate, got {other:?}"),
}
assert!(
aur_build::db::unapproved_index_entries(&env.db).unwrap().is_empty(),
"detection alone must never touch the index (I1)"
);
}
}
#[test]
fn shim_environment_reaches_child_processes_through_the_config() {
let env = Env::new();
let mut cfg = env.cfg.clone();
cfg.path_prepend = Some(shim_dir());
cfg.shim_manifest = Some(fixture_manifest("plain-make"));
cfg.shim_dir = Some(env.path("shimlog"));
std::fs::create_dir_all(env.path("shimlog")).unwrap();
// Prove the config is what puts the shims on PATH, not the test process env.
let mut cmd = Command::new("sh");
cfg.child_env(&mut cmd);
let out = cmd.args(["-c", "command -v makechrootpkg; echo $AUR_SHIM_MANIFEST"]).output().unwrap();
let s = String::from_utf8_lossy(&out.stdout);
assert!(s.contains("makechrootpkg"), "{s}");
assert!(s.contains("manifest.json"), "{s}");
}
|