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
//! Append-only release status transition tests (`src/release/status.rs`).
//!
//! The module is included by path so its `crate::` imports resolve inside this
//! integration crate; `tests/common` supplies the scripted PDS fake.
use std::sync::Arc;
use serde::Deserialize;
use serde_json::json;
use vuln_notif::constants::STATUS_COLLECTION;
use vuln_notif::db::DbHandle;
use vuln_notif::db::model::{ReleaseStatusEventRow, ReleaseStatusRow};
use vuln_notif::pds::{Publisher, PublisherConfig};
use vuln_notif::release::source::StatusInfo;
pub use vuln_notif::{constants, db, links, pds, records, release, time};
#[path = "../src/release/status.rs"]
mod status;
mod common;
use common::{FakePds, FakeTime, response};
use status::{StatusCounters, StatusTransition, apply_transitions, plan_transitions};
const REPO: &str = "did:plc:test";
const SUBJECT: &str = "at://did:plc:test/software.release.version/acme-widget:1.2.3";
const OBSERVED: &str = include_str!("fixtures/status/observed.json");
const EXPECTED_EVENTS: &str = include_str!("fixtures/status/expected_events.json");
const OBSERVED_SECURITY: &str = include_str!("fixtures/status/observed_security.json");
const OBSERVED_DEPRECATED: &str = include_str!("fixtures/status/observed_deprecated.json");
const OBSERVED_DEPRECATED_CHANGED: &str =
include_str!("fixtures/status/observed_deprecated_changed.json");
const OBSERVED_DEPRECATED_PLAIN: &str =
include_str!("fixtures/status/observed_deprecated_plain.json");
const CURRENT_DEPRECATED: &str = include_str!("fixtures/status/current_deprecated.json");
#[derive(Deserialize)]
struct SignalFixture {
status: String,
#[serde(default)]
kind: Option<String>,
#[serde(default)]
reason: String,
}
impl SignalFixture {
fn info(&self) -> StatusInfo {
StatusInfo {
status: self.status.clone(),
kind: self.kind.clone(),
reason: self.reason.clone(),
}
}
}
#[derive(Deserialize)]
struct CurrentFixture {
subject_uri: String,
status: String,
}
impl CurrentFixture {
fn row(&self) -> ReleaseStatusRow {
ReleaseStatusRow {
repo_did: REPO.to_owned(),
subject_uri: self.subject_uri.clone(),
status: self.status.clone(),
..ReleaseStatusRow::default()
}
}
}
#[derive(Deserialize)]
struct EventFixture {
status: String,
#[serde(default)]
kind: Option<String>,
#[serde(default)]
reason: String,
source: String,
}
fn signals(raw: &str) -> Vec<StatusInfo> {
let fixtures: Vec<SignalFixture> = serde_json::from_str(raw).expect("signal fixture");
fixtures.iter().map(SignalFixture::info).collect()
}
fn expected_events(raw: &str) -> Vec<EventFixture> {
serde_json::from_str(raw).expect("event fixture")
}
struct Fixture {
_dir: tempfile::TempDir,
db: DbHandle,
pds: Arc<FakePds>,
publisher: Publisher,
}
async fn fixture() -> Fixture {
let dir = tempfile::tempdir().expect("temp dir");
let db = DbHandle::open(dir.path().join("release-status.db"))
.await
.expect("db");
let pds = FakePds::new();
let time = FakeTime::new(1_700_000_000.0);
let governor = common::governor(&db, time.clone()).await;
let (logger, _sink) = common::logger();
let publisher = Publisher::with_api(
PublisherConfig {
pds_url: "http://127.0.0.1:1".to_owned(),
handle: "alice.test".to_owned(),
app_password: "app-pw".to_owned(),
collection: STATUS_COLLECTION.to_owned(),
timeout_secs: 5.0,
governor,
},
pds.clone(),
logger,
time,
)
.expect("publisher");
Fixture {
_dir: dir,
db,
pds,
publisher,
}
}
async fn plan(
fixture: &Fixture,
subject: &str,
observed: &[StatusInfo],
current: &[ReleaseStatusRow],
) -> Vec<StatusTransition> {
plan_transitions(&fixture.db, REPO, subject, observed, current)
.await
.expect("plan transitions")
}
async fn apply(
fixture: &Fixture,
subject: &str,
transitions: &[StatusTransition],
) -> (StatusCounters, Vec<ReleaseStatusEventRow>) {
let events = fixture
.db
.list_release_status_events(REPO, subject, 0)
.await
.expect("events");
let counters =
apply_transitions(&fixture.publisher, &fixture.db, REPO, transitions, &events).await;
let events = fixture
.db
.list_release_status_events(REPO, subject, 0)
.await
.expect("events");
(counters, events)
}
async fn current_rows(fixture: &Fixture, statuses: &[&str]) -> Vec<ReleaseStatusRow> {
let mut rows = Vec::new();
for status in statuses {
if let Some(row) = fixture
.db
.get_release_status(REPO, SUBJECT, status)
.await
.expect("current status")
{
rows.push(row);
}
}
rows
}
#[tokio::test]
async fn transitions_create_append_only_records_and_rerun_is_a_noop() {
let fixture = fixture().await;
let observed = signals(OBSERVED);
let expected = expected_events(EXPECTED_EVENTS);
let transitions = plan(&fixture, SUBJECT, &observed, &[]).await;
assert_eq!(transitions.len(), expected.len());
for (transition, expected) in transitions.iter().zip(&expected) {
assert_eq!(transition.subject_uri, SUBJECT);
assert_eq!(transition.status, expected.status);
assert_eq!(transition.kind, expected.kind);
assert_eq!(transition.reason, expected.reason);
assert_eq!(transition.source, expected.source);
}
let (counters, events) = apply(&fixture, SUBJECT, &transitions).await;
assert_eq!(
counters,
StatusCounters {
planned: expected.len() as u64,
created: expected.len() as u64,
unchanged: 0,
failures: 0,
}
);
assert_eq!(events.len(), expected.len());
for expected in &expected {
let event = events
.iter()
.find(|event| event.status == expected.status)
.expect("event row");
assert_eq!(event.kind, expected.kind);
assert_eq!(event.reason.as_deref().unwrap_or(""), expected.reason);
assert_eq!(event.cid.as_deref(), Some(common::PUT_CID));
}
let puts = fixture.pds.puts();
assert_eq!(puts.len(), expected.len());
for put in &puts {
assert_eq!(put.collection, STATUS_COLLECTION);
let record = put.record.as_ref().expect("status record");
let expected = expected
.iter()
.find(|expected| record["status"].as_str() == Some(expected.status.as_str()))
.expect("expected status");
assert_eq!(record["subject"].as_str(), Some(SUBJECT));
match &expected.kind {
Some(kind) => assert_eq!(record["kind"].as_str(), Some(kind.as_str())),
None => assert!(record.get("kind").is_none()),
}
if expected.reason.is_empty() {
assert!(record.get("reason").is_none());
} else {
assert_eq!(record["reason"].as_str(), Some(expected.reason.as_str()));
}
assert!(
fixture.pds.record(STATUS_COLLECTION, &put.rkey).is_some(),
"record must be stored on the PDS"
);
}
// Every status record indexes its subject link.
let backlinks = fixture
.db
.backlinks(REPO, SUBJECT, Some(STATUS_COLLECTION), Some("subject"), 10)
.await
.expect("backlinks");
assert_eq!(backlinks.len(), expected.len());
for link in &backlinks {
assert_eq!(link.path, "subject");
assert_eq!(link.subject_uri, SUBJECT);
}
// Identical re-run: no new records, no new events.
let statuses: Vec<&str> = expected.iter().map(|event| event.status.as_str()).collect();
let current = current_rows(&fixture, &statuses).await;
assert_eq!(current.len(), expected.len());
let replanned = plan(&fixture, SUBJECT, &observed, ¤t).await;
assert!(
replanned.is_empty(),
"identical signals must be a no-op: {replanned:?}"
);
let (counters, events) = apply(&fixture, SUBJECT, &replanned).await;
assert_eq!(counters, StatusCounters::default());
assert_eq!(events.len(), expected.len());
assert_eq!(fixture.pds.puts().len(), expected.len());
}
#[tokio::test]
async fn reason_change_appends_without_rewriting_history() {
let fixture = fixture().await;
let observed = signals(OBSERVED_DEPRECATED);
let transitions = plan(&fixture, SUBJECT, &observed, &[]).await;
assert_eq!(transitions.len(), 1);
let (counters, events) = apply(&fixture, SUBJECT, &transitions).await;
assert_eq!(counters.created, 1);
assert_eq!(events.len(), 1);
let first = events[0].clone();
assert_eq!(first.reason.as_deref(), Some("superseded by 2.0"));
let current = current_rows(&fixture, &["deprecated"]).await;
assert_eq!(current.len(), 1);
let changed = signals(OBSERVED_DEPRECATED_CHANGED);
let transitions = plan(&fixture, SUBJECT, &changed, ¤t).await;
assert_eq!(transitions.len(), 1);
assert_eq!(transitions[0].reason, "superseded by 3.0");
let (counters, events) = apply(&fixture, SUBJECT, &transitions).await;
assert_eq!(counters.planned, 1);
assert_eq!(counters.created, 1);
assert_eq!(counters.failures, 0);
assert_eq!(events.len(), 2);
let reasons: Vec<&str> = events
.iter()
.filter_map(|event| event.reason.as_deref())
.collect();
assert!(reasons.contains(&"superseded by 2.0"));
assert!(reasons.contains(&"superseded by 3.0"));
assert!(
events.iter().any(|event| event.rkey == first.rkey),
"the old event must remain"
);
assert!(fixture.pds.record(STATUS_COLLECTION, &first.rkey).is_some());
let row = fixture
.db
.get_release_status(REPO, SUBJECT, "deprecated")
.await
.expect("status row")
.expect("current row");
assert_ne!(row.rkey, first.rkey);
assert!(events.iter().any(|event| event.rkey == row.rkey));
assert_eq!(fixture.pds.puts().len(), 2);
}
#[tokio::test]
async fn security_kind_comes_only_from_the_signal() {
let fixture = fixture().await;
let observed = signals(OBSERVED_SECURITY);
let transitions = plan(&fixture, SUBJECT, &observed, &[]).await;
assert_eq!(transitions.len(), 2);
let yanked = transitions
.iter()
.find(|transition| transition.status == "yanked")
.expect("yanked transition");
assert_eq!(yanked.kind.as_deref(), Some("security"));
let archived = transitions
.iter()
.find(|transition| transition.status == "archived")
.expect("archived transition");
assert_eq!(
archived.kind, None,
"a CVE ID in the reason must not promote kind"
);
let (counters, _) = apply(&fixture, SUBJECT, &transitions).await;
assert_eq!(counters.created, 2);
let records: Vec<_> = fixture
.pds
.puts()
.into_iter()
.filter_map(|put| put.record)
.collect();
let yanked = records
.iter()
.find(|record| record["status"].as_str() == Some("yanked"))
.expect("yanked record");
assert_eq!(yanked["kind"].as_str(), Some("security"));
let archived = records
.iter()
.find(|record| record["status"].as_str() == Some("archived"))
.expect("archived record");
assert!(archived.get("kind").is_none());
}
#[tokio::test]
async fn cve_signal_alone_never_creates_a_status() {
let fixture = fixture().await;
let transitions = plan(&fixture, SUBJECT, &[], &[]).await;
assert!(transitions.is_empty());
let (counters, events) = apply(&fixture, SUBJECT, &transitions).await;
assert_eq!(counters, StatusCounters::default());
assert!(events.is_empty());
assert_eq!(fixture.pds.count("putRecord"), 0);
for status in status::STATUSES {
assert!(
fixture
.db
.get_release_status(REPO, SUBJECT, status)
.await
.expect("status row")
.is_none()
);
}
}
#[tokio::test]
async fn pds_failure_does_not_append_the_local_event() {
let fixture = fixture().await;
let observed = vec![StatusInfo {
status: "archived".to_owned(),
kind: None,
reason: String::new(),
}];
let transitions = plan(&fixture, SUBJECT, &observed, &[]).await;
assert_eq!(transitions.len(), 1);
fixture.pds.push_put(response(
400,
json!({"error": "InvalidRequest", "message": "rejected"}),
));
let (counters, events) = apply(&fixture, SUBJECT, &transitions).await;
assert_eq!(counters.planned, 1);
assert_eq!(counters.created, 0);
assert_eq!(counters.failures, 1);
assert!(events.is_empty(), "the local event waits for the PDS write");
assert!(
fixture
.db
.get_release_status(REPO, SUBJECT, "archived")
.await
.expect("status row")
.is_none()
);
let put = &fixture.pds.puts()[0];
assert!(fixture.pds.record(STATUS_COLLECTION, &put.rkey).is_none());
}
#[tokio::test]
async fn legacy_current_row_without_events_is_not_duplicated() {
let fixture = fixture().await;
let current: Vec<CurrentFixture> =
serde_json::from_str(CURRENT_DEPRECATED).expect("current fixture");
let current: Vec<ReleaseStatusRow> = current.iter().map(CurrentFixture::row).collect();
let observed = signals(OBSERVED_DEPRECATED_PLAIN);
let transitions = plan(&fixture, SUBJECT, &observed, ¤t).await;
assert!(transitions.is_empty());
}