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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
|
mod common;
use common::{git_add_commit, make_git_repo, params, Env, P};
use aur_build::db::{self, model::*, ReleaseRow};
use aur_build::review::{approve, candidates};
use aur_build::watch::detect::{check_pkgbase, Outcome};
use aur_build::watch::mirror::{read_tree, sync_mirror, Sync};
fn aur(env: &Env, name: &str) -> std::path::PathBuf {
env.path(&format!("aur/{name}.git"))
}
fn seed(env: &Env, name: &str, pkgver: &str, pkgrel: &str, extra: &str) -> String {
make_git_repo(&aur(env, name), &[("PKGBUILD", &common::pkgbuild(name, pkgver, pkgrel, extra))], "initial")
}
fn pending(env: &Env, name: &str) -> Option<CandidateRow> {
let pb = db::get_pkgbase(&env.db, name).unwrap().unwrap();
candidates::open_reviews_for(&env.db, pb.id).unwrap().into_iter().next()
}
fn accepted(env: &Env, name: &str) -> Option<ReleaseRow> {
db::accepted_release(&env.db, name).unwrap()
}
#[test]
fn first_detection_opens_a_first_introduction_with_no_base() {
let env = Env::new();
env.subscribe("foo");
let commit = seed(&env, "foo", "1.0", "1", "");
let rep = check_pkgbase(&env.cfg, &env.db, "foo").unwrap();
match &rep.outcome {
Outcome::Candidate { kind, .. } => assert_eq!(kind, candidate_kind::FIRST_INTRODUCTION),
other => panic!("expected a candidate, got {other:?}"),
}
assert_eq!(rep.head, commit);
let c = pending(&env, "foo").expect("candidate row");
assert_eq!(c.kind, candidate_kind::FIRST_INTRODUCTION);
assert_eq!(c.base_release_id, None, "nothing was accepted yet, so there is no diff base");
assert_eq!(c.version(), "1.0-1");
let snap = db::snapshot_by_id(&env.db, c.snapshot_id).unwrap().unwrap();
assert_eq!(snap.aur_commit, commit);
assert_eq!(snap.recipe_hash.len(), 64);
}
#[test]
fn cheap_poll_does_not_fetch_when_the_tip_is_unchanged() {
let env = Env::new();
env.subscribe("foo");
seed(&env, "foo", "1.0", "1", "");
let url = format!("file://{}", aur(&env, "foo").display());
let first = sync_mirror(&env.cfg, &env.db, "foo", &url).unwrap();
assert!(matches!(first, Sync::Cloned { .. }));
let again = sync_mirror(&env.cfg, &env.db, "foo", &url).unwrap();
match again {
Sync::Unchanged { head } => assert_eq!(head, git_head(&aur(&env, "foo"))),
other => panic!("expected Unchanged, got {other:?}"),
}
git_add_commit(&aur(&env, "foo"), &[("PKGBUILD", &common::pkgbuild("foo", "1.1", "1", ""))], "bump");
let moved = sync_mirror(&env.cfg, &env.db, "foo", &url).unwrap();
assert!(matches!(moved, Sync::Fetched { .. }), "SHA moved -> fetch");
}
fn git_head(p: &std::path::Path) -> String {
aur_build::git::head(p).unwrap()
}
#[test]
fn recipe_hash_covers_shipped_patches() {
let env = Env::new();
env.subscribe("foo");
seed(&env, "foo", "1.0", "1", "source=(fix.patch)\nsha256sums=('SKIP')");
std::fs::write(aur(&env, "foo").join("fix.patch"), "--- a\n+++ b\n@@\n-old\n+new\n").unwrap();
git_add_commit(&aur(&env, "foo"), &[("fix.patch", "--- a\n+++ b\n@@\n-old\n+new\n")], "patch");
check_pkgbase(&env.cfg, &env.db, "foo").unwrap();
let c1 = pending(&env, "foo").unwrap();
let h1 = db::snapshot_by_id(&env.db, c1.snapshot_id).unwrap().unwrap().recipe_hash;
env.accept_as_release(c1.id);
git_add_commit(&aur(&env, "foo"), &[("fix.patch", "--- a\n+++ b\n@@\n-old\n+EVIL\n")], "patch2");
let rep = check_pkgbase(&env.cfg, &env.db, "foo").unwrap();
assert!(
matches!(rep.outcome, Outcome::Notice { ref kind, .. } if kind == notice_kind::RECIPE_DRIFT),
"recipe drift with an unchanged version is a notice, never a candidate: {:?}",
rep.outcome
);
let pb = db::get_pkgbase(&env.db, "foo").unwrap().unwrap();
let snap = db::snapshot_by_commit(&env.db, pb.id, &rep.head).unwrap().unwrap();
assert_ne!(snap.recipe_hash, h1, "patch bytes must change recipe_hash");
}
#[test]
fn skip_sources_are_stream_hashed_into_the_cache() {
let env = Env::new();
env.subscribe("foo");
let payload = env.path("payload.txt");
std::fs::write(&payload, b"the bytes that become the package").unwrap();
let expected = aur_build::hash::hex(b"the bytes that become the package");
seed(
&env,
"foo",
"1.0",
"1",
&format!("source=(file://{})\nsha256sums=('SKIP')\n", payload.display()),
);
check_pkgbase(&env.cfg, &env.db, "foo").unwrap();
let c = pending(&env, "foo").unwrap();
let digests = c.digests();
assert_eq!(digests.len(), 1);
assert_eq!(digests[0].digest, expected, "we hash SKIP sources ourselves");
assert!(digests[0].skip_declared);
assert_eq!(digests[0].kind, "static");
let cached = env.query_i64(
"SELECT count(*) FROM source_cache WHERE url LIKE '%payload.txt' AND digest=?1",
¶ms(vec![expected.clone()]),
);
assert_eq!(cached, Some(1));
let path = env.cfg.source_cache_path(&expected);
assert!(path.exists(), "cached file is reused by the build");
}
#[test]
fn declared_checksums_are_trusted_without_downloading() {
let env = Env::new();
env.subscribe("foo");
let payload = env.path("declared.txt");
std::fs::write(&payload, b"verified bytes").unwrap();
let d = aur_build::hash::hex(b"verified bytes");
seed(
&env,
"foo",
"1.0",
"1",
&format!("source=(file://{})\nsha256sums=('{d}')\n", payload.display()),
);
check_pkgbase(&env.cfg, &env.db, "foo").unwrap();
let c = pending(&env, "foo").unwrap();
assert_eq!(c.digests()[0].digest, d);
assert!(!c.digests()[0].skip_declared);
assert_eq!(
env.query_i64("SELECT count(*) FROM source_cache", &[]),
Some(0),
"declared sums are enforced by makepkg, not re-downloaded"
);
}
#[test]
fn i2_diff_base_is_the_last_accepted_version_however_many_detections_arrive() {
let env = Env::new();
env.subscribe("foo");
seed(&env, "foo", "1.0", "1", "");
check_pkgbase(&env.cfg, &env.db, "foo").unwrap();
let c1 = pending(&env, "foo").unwrap();
let rel1 = env.accept_as_release(c1.id);
assert_eq!(accepted(&env, "foo").unwrap().id, rel1);
git_add_commit(&aur(&env, "foo"), &[("PKGBUILD", &common::pkgbuild("foo", "2.0", "1", ""))], "v2");
check_pkgbase(&env.cfg, &env.db, "foo").unwrap();
let c2 = pending(&env, "foo").unwrap();
assert_eq!(c2.base_release_id, Some(rel1), "base is the accepted release");
assert_eq!(c2.version(), "2.0-1");
for v in ["3.0", "4.0"] {
git_add_commit(&aur(&env, "foo"), &[("PKGBUILD", &common::pkgbuild("foo", v, "1", ""))], v);
check_pkgbase(&env.cfg, &env.db, "foo").unwrap();
}
let c = pending(&env, "foo").unwrap();
assert_eq!(c.id, c2.id, "collapsed in place: no new row");
assert_eq!(c.revision, 2, "revision counts the collapsed detections (v3, v4)");
assert_eq!(c.base_release_id, Some(rel1), "base never moves: the diff is v1 -> v4");
assert_eq!(c.version(), "4.0-1");
assert_eq!(
env.query_i64("SELECT count(*) FROM candidate WHERE state='pending_review'", &[]),
Some(1)
);
}
#[test]
fn a_viewed_review_is_frozen_and_a_fresh_one_opened() {
let env = Env::new();
env.subscribe("foo");
seed(&env, "foo", "1.0", "1", "");
check_pkgbase(&env.cfg, &env.db, "foo").unwrap();
let c1 = pending(&env, "foo").unwrap();
let rel1 = env.accept_as_release(c1.id);
git_add_commit(&aur(&env, "foo"), &[("PKGBUILD", &common::pkgbuild("foo", "2.0", "1", ""))], "v2");
check_pkgbase(&env.cfg, &env.db, "foo").unwrap();
let c2 = pending(&env, "foo").unwrap();
candidates::mark_viewed(&env.db, c2.id).unwrap();
git_add_commit(&aur(&env, "foo"), &[("PKGBUILD", &common::pkgbuild("foo", "3.0", "1", ""))], "v3");
let rep = check_pkgbase(&env.cfg, &env.db, "foo").unwrap();
match rep.outcome {
Outcome::Superseded { old, new } => {
assert_eq!(old, c2.id);
assert_ne!(old, new);
}
other => panic!("expected Superseded, got {other:?}"),
}
let c3 = pending(&env, "foo").unwrap();
assert_eq!(c3.base_release_id, Some(rel1), "the fresh review keeps the same base");
assert_eq!(c3.version(), "3.0-1");
let old_state: String = env
.db
.read(|c| {
Ok(c.query_row(
"SELECT state FROM candidate WHERE id=?1",
rusqlite::params![c2.id],
|r| r.get(0),
)?)
})
.unwrap();
assert_eq!(
CandidateState::parse(&old_state),
Some(CandidateState::Superseded),
"the frozen review must be superseded in the column, not only in memory"
);
}
#[test]
fn a_rejected_identity_is_not_re_raised() {
let env = Env::new();
env.subscribe("foo");
seed(&env, "foo", "1.0", "1", "");
check_pkgbase(&env.cfg, &env.db, "foo").unwrap();
let c = pending(&env, "foo").unwrap();
approve::reject(&env.db, "tester", c.id, "not this one").unwrap();
let rep = check_pkgbase(&env.cfg, &env.db, "foo").unwrap();
assert!(
matches!(rep.outcome, Outcome::PreviouslyRejected | Outcome::NoChange),
"{:?}",
rep.outcome
);
assert!(pending(&env, "foo").is_none());
git_add_commit(&aur(&env, "foo"), &[("PKGBUILD", &common::pkgbuild("foo", "2.0", "1", ""))], "v2");
check_pkgbase(&env.cfg, &env.db, "foo").unwrap();
assert!(pending(&env, "foo").is_some());
}
#[test]
fn recipe_drift_under_a_static_version_is_a_notice_not_a_candidate() {
let env = Env::new();
env.subscribe("foo");
seed(&env, "foo", "1.0", "1", "");
check_pkgbase(&env.cfg, &env.db, "foo").unwrap();
let c = pending(&env, "foo").unwrap();
env.accept_as_release(c.id);
git_add_commit(
&aur(&env, "foo"),
&[("PKGBUILD", &common::pkgbuild("foo", "1.0", "1", "# a comment only"))],
"typo fix, no bump",
);
let rep = check_pkgbase(&env.cfg, &env.db, "foo").unwrap();
match rep.outcome {
Outcome::Notice { kind, .. } => assert_eq!(kind, notice_kind::RECIPE_DRIFT),
other => panic!("expected a notice, got {other:?}"),
}
assert!(pending(&env, "foo").is_none(), "nothing publishable, so no review");
let notices = db::list_notices(&env.db, false).unwrap();
assert_eq!(notices.len(), 1);
assert_eq!(notices[0].kind, notice_kind::RECIPE_DRIFT);
assert_eq!(notices[0].severity, "warning");
}
#[test]
fn digest_drift_with_an_unchanged_version_is_an_integrity_alert() {
let env = Env::new();
env.subscribe("foo");
let payload = env.path("payload.txt");
std::fs::write(&payload, b"good release tarball").unwrap();
seed(
&env,
"foo",
"1.0",
"1",
&format!("source=(file://{})\nsha256sums=('SKIP')\n", payload.display()),
);
check_pkgbase(&env.cfg, &env.db, "foo").unwrap();
let c = pending(&env, "foo").unwrap();
env.accept_as_release(c.id);
std::fs::write(&payload, b"EVIL swapped artifact").unwrap();
let rep = check_pkgbase(&env.cfg, &env.db, "foo").unwrap();
match rep.outcome {
Outcome::Notice { kind, .. } => assert_eq!(kind, notice_kind::INTEGRITY_ALERT),
other => panic!("expected an integrity alert, got {other:?}"),
}
assert!(pending(&env, "foo").is_none(), "approving it would not let us publish anything");
let notices = db::list_notices(&env.db, false).unwrap();
assert_eq!(notices[0].severity, "high");
assert!(notices[0].detail_json.contains("no longer serves"));
assert_eq!(db::unapproved_index_entries(&env.db).unwrap(), Vec::<String>::new());
}
#[test]
fn a_deleted_upstream_marks_the_subscription_and_alerts() {
let env = Env::new();
env.subscribe("foo");
seed(&env, "foo", "1.0", "1", "");
check_pkgbase(&env.cfg, &env.db, "foo").unwrap();
std::fs::remove_dir_all(aur(&env, "foo")).unwrap();
let rep = check_pkgbase(&env.cfg, &env.db, "foo").unwrap();
assert!(matches!(rep.outcome, Outcome::Gone), "{:?}", rep.outcome);
let sub = db::get_subscription(&env.db, "foo").unwrap().unwrap();
assert_eq!(sub.state, SubscriptionState::Orphaned);
assert!(db::list_notices(&env.db, false)
.unwrap()
.iter()
.any(|n| n.kind == notice_kind::DELETED));
}
#[test]
fn detection_is_idempotent_and_records_no_duplicate_candidate() {
let env = Env::new();
env.subscribe("foo");
seed(&env, "foo", "1.0", "1", "");
check_pkgbase(&env.cfg, &env.db, "foo").unwrap();
for _ in 0..3 {
let rep = check_pkgbase(&env.cfg, &env.db, "foo").unwrap();
assert!(matches!(rep.outcome, Outcome::NoChange), "{:?}", rep.outcome);
}
assert_eq!(
env.query_i64("SELECT count(*) FROM candidate", &[]),
Some(1),
"two byte-identical inputs never produce two candidates"
);
assert_eq!(env.query_i64("SELECT count(*) FROM snapshot", &[]), Some(1));
}
#[test]
fn srcinfo_is_preferred_over_the_pkgbuild_when_present() {
let env = Env::new();
env.subscribe("foo");
make_git_repo(
&aur(&env, "foo"),
&[
("PKGBUILD", &common::pkgbuild("foo", "9.9", "9", "")),
(
".SRCINFO",
"pkgbase = foo\n\tpkgver = 1.0\n\tpkgrel = 1\n\tarch = x86_64\npkgname = foo\n\tarch = x86_64\n",
),
],
"initial",
);
check_pkgbase(&env.cfg, &env.db, "foo").unwrap();
let c = pending(&env, "foo").unwrap();
assert_eq!(c.version(), "1.0-1", ".SRCINFO is authoritative");
}
#[test]
fn split_pkgbase_records_every_emitted_package() {
let env = Env::new();
env.subscribe("foo");
make_git_repo(
&aur(&env, "foo"),
&[(
"PKGBUILD",
"pkgbase=foo\npkgver=1.0\npkgrel=1\npkgdesc=split\narch=(x86_64)\npkgname=(foo foo-lua)\nsource=()\nsha256sums=()\n",
)],
"initial",
);
check_pkgbase(&env.cfg, &env.db, "foo").unwrap();
let c = pending(&env, "foo").unwrap();
let snap = db::snapshot_by_id(&env.db, c.snapshot_id).unwrap().unwrap();
let r: Recipe = serde_json::from_str(&snap.srcinfo_json).unwrap();
assert_eq!(r.pkgnames, vec!["foo".to_string(), "foo-lua".to_string()]);
}
#[test]
fn epoch_and_pkgrel_are_part_of_the_identity() {
let env = Env::new();
env.subscribe("foo");
seed(&env, "foo", "1.0", "1", "");
check_pkgbase(&env.cfg, &env.db, "foo").unwrap();
let c = pending(&env, "foo").unwrap();
env.accept_as_release(c.id);
git_add_commit(&aur(&env, "foo"), &[("PKGBUILD", &common::pkgbuild("foo", "1.0", "7", ""))], "rel");
check_pkgbase(&env.cfg, &env.db, "foo").unwrap();
let c2 = pending(&env, "foo").expect("pkgrel bump is publishable, so it is a candidate");
assert_eq!(c2.version(), "1.0-7");
assert_ne!(c2.identity_hash, c.identity_hash);
}
#[test]
fn notices_are_not_duplicated_by_repeated_polling() {
let env = Env::new();
env.subscribe("foo");
seed(&env, "foo", "1.0", "1", "");
check_pkgbase(&env.cfg, &env.db, "foo").unwrap();
let c = pending(&env, "foo").unwrap();
env.accept_as_release(c.id);
git_add_commit(&aur(&env, "foo"), &[("PKGBUILD", &common::pkgbuild("foo", "1.0", "1", "# x"))], "drift");
for _ in 0..4 {
check_pkgbase(&env.cfg, &env.db, "foo").unwrap();
}
assert_eq!(
env.query_i64("SELECT count(*) FROM notice", &[]),
Some(1),
"the same drift polled daily is one alert, not four"
);
let _ = P::I(0);
}
#[test]
fn read_tree_returns_every_blob_from_one_batch_read() {
let env = Env::new();
env.subscribe("foo");
make_git_repo(
&aur(&env, "foo"),
&[
("PKGBUILD", &common::pkgbuild("foo", "1.0", "1", "")),
("empty.txt", ""),
("blob.bin", "\u{0}not text"),
],
"initial",
);
let url = format!("file://{}", aur(&env, "foo").display());
sync_mirror(&env.cfg, &env.db, "foo", &url).unwrap();
let mirror = env.cfg.mirror_path("foo");
let head = aur_build::git::head(&mirror).unwrap();
let tree = read_tree(&mirror, &head).unwrap();
assert!(tree.files["PKGBUILD"].contains("pkgver=1.0"));
assert_eq!(tree.files["empty.txt"], "", "an empty blob is a file, not a miss");
assert!(
!tree.files.contains_key("blob.bin"),
"a NUL in the first block means it is not text: {:?}",
tree.files.keys().collect::<Vec<_>>()
);
}
#[test]
fn read_tree_fails_closed_when_the_commit_is_gone_from_the_mirror() {
let env = Env::new();
env.subscribe("foo");
seed(&env, "foo", "1.0", "1", "");
let url = format!("file://{}", aur(&env, "foo").display());
sync_mirror(&env.cfg, &env.db, "foo", &url).unwrap();
let mirror = env.cfg.mirror_path("foo");
for gone in ["0123456789012345678901234567890123456789", "deadbeef"] {
let err = read_tree(&mirror, gone).unwrap_err().to_string();
assert!(err.contains("missing from the local mirror"), "{gone}: {err}");
}
}
|