packageurl 0.7.0

Rust implementation of the package url specification
Documentation
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
449
450
451
452
//! Conformance test against the official Package URL specification suite.
//!
//! The suite files are vendored under `tests/purl-spec/`, fetched from
//! `package-url/purl-spec` at the commit pinned in
//! `scripts/update-purl-spec-tests.sh` and verified against
//! `scripts/purl-spec-tests.sha256`. Every case is run through the public API
//! and reconciled against `KNOWN_GAPS`, the list of cases the crate does not
//! yet satisfy.
//!
//! The single test is a two-sided guard:
//!
//!   * a non-conformant case that is *not* listed in `KNOWN_GAPS` fails the test
//!     as a regression;
//!   * a `KNOWN_GAPS` entry that no non-conformant case reproduces fails the test
//!     so that a gap fixed upstream (or by this crate) gets pruned.
//!
//! To bump the suite, edit the pin in the update script, run it with
//! `--refresh`, then re-baseline:
//! `PURL_CONFORMANCE_DUMP=1 cargo test --test purl_conformance -- --nocapture`.

use std::collections::{BTreeMap, HashMap, HashSet};
use std::fs;
use std::path::PathBuf;
use std::str::FromStr;

use packageurl::PackageUrl;
use serde::Deserialize;

// ---------------------------------------------------------------------------
// Vendored test-suite model (schema: schemas/purl-test.schema-0.1.json upstream)
// ---------------------------------------------------------------------------

#[derive(Deserialize)]
struct SuiteFile {
    #[serde(default)]
    tests: Vec<Case>,
}

#[derive(Deserialize)]
struct Case {
    description: String,
    test_type: TestType,
    #[serde(default)]
    expected_failure: bool,
    input: Io,
    #[serde(default)]
    expected_output: Option<Io>,
}

#[derive(Clone, Copy, Deserialize)]
#[serde(rename_all = "lowercase")]
enum TestType {
    Parse,
    Build,
    Roundtrip,
}

impl TestType {
    fn as_str(self) -> &'static str {
        match self {
            TestType::Parse => "parse",
            TestType::Build => "build",
            TestType::Roundtrip => "roundtrip",
        }
    }
}

/// `input` and `expected_output` are a purl string for parse/roundtrip and a
/// component object for build; the two JSON shapes are disjoint, so `untagged`
/// resolves them unambiguously.
#[derive(Deserialize)]
#[serde(untagged)]
enum Io {
    Purl(String),
    Components(Box<Components>),
}

impl Io {
    fn purl(&self) -> Option<&str> {
        match self {
            Io::Purl(purl) => Some(purl.as_str()),
            Io::Components(_) => None,
        }
    }

    fn components(&self) -> Option<&Components> {
        match self {
            Io::Components(components) => Some(components),
            Io::Purl(_) => None,
        }
    }
}

#[derive(Deserialize)]
struct Components {
    #[serde(rename = "type", default)]
    ty: Option<String>,
    #[serde(default)]
    namespace: Option<String>,
    #[serde(default)]
    name: Option<String>,
    #[serde(default)]
    version: Option<String>,
    #[serde(default)]
    qualifiers: Option<BTreeMap<String, String>>,
    #[serde(default)]
    subpath: Option<String>,
}

// ---------------------------------------------------------------------------
// Known non-conformances
// ---------------------------------------------------------------------------

/// Cases the crate does not yet satisfy, as `(logical_key, note)` entries.
/// Rebuild from the `PURL_CONFORMANCE_DUMP` block after refreshing the suite.
/// Empty: every case at the pinned suite commit is conformant.
#[rustfmt::skip]
static KNOWN_GAPS: &[(&str, &str)] = &[];

// ---------------------------------------------------------------------------
// Evaluation
// ---------------------------------------------------------------------------

/// A conformance verdict: `Ok(())` is conformant, `Err(detail)` is not.
type Check = Result<(), String>;

fn evaluate(case: &Case) -> Check {
    match case.test_type {
        TestType::Parse => eval_parse(case),
        TestType::Build => eval_build(case),
        TestType::Roundtrip => eval_roundtrip(case),
    }
}

fn eval_parse(case: &Case) -> Check {
    let input = case.input.purl().expect("parse input is a purl string");
    let parsed = PackageUrl::from_str(input);
    if case.expected_failure {
        return match parsed {
            Err(_) => Ok(()),
            Ok(purl) => Err(format!("expected parse to fail, got {purl}")),
        };
    }
    let expected = case
        .expected_output
        .as_ref()
        .and_then(Io::components)
        .expect("parse output is a component object");
    match parsed {
        Err(err) => Err(format!("expected success, parse failed: {err}")),
        Ok(purl) => compare(&purl, expected),
    }
}

fn eval_roundtrip(case: &Case) -> Check {
    let input = case.input.purl().expect("roundtrip input is a purl string");
    let parsed = PackageUrl::from_str(input);
    if case.expected_failure {
        return match parsed {
            Err(_) => Ok(()),
            Ok(purl) => Err(format!("expected roundtrip to fail, got {purl}")),
        };
    }
    let expected = case
        .expected_output
        .as_ref()
        .and_then(Io::purl)
        .expect("roundtrip output is a purl string");
    match parsed {
        Err(err) => Err(format!("expected success, parse failed: {err}")),
        Ok(purl) => {
            let got = purl.to_string();
            if got == expected {
                Ok(())
            } else {
                Err(format!("got {got:?}, want {expected:?}"))
            }
        }
    }
}

fn eval_build(case: &Case) -> Check {
    let input = case.input.components().expect("build input is components");
    let built = build(input);
    if case.expected_failure {
        return match built {
            Err(_) => Ok(()),
            Ok(got) => Err(format!("expected build to fail, got {got:?}")),
        };
    }
    let expected = case
        .expected_output
        .as_ref()
        .and_then(Io::purl)
        .expect("build output is a purl string");
    match built {
        Err(err) => Err(format!("expected success, build failed: {err}")),
        Ok(got) if got == expected => Ok(()),
        Ok(got) => Err(format!("got {got:?}, want {expected:?}")),
    }
}

/// Build a canonical purl from decoded components via the public builder API,
/// validated like the parser validates. A null type or name maps to `""`,
/// which the builder rejects.
fn build(components: &Components) -> Result<String, packageurl::Error> {
    let mut purl = PackageUrl::new(
        components.ty.as_deref().unwrap_or(""),
        components.name.as_deref().unwrap_or(""),
    )?;
    if let Some(namespace) = components.namespace.as_deref() {
        purl.with_namespace(namespace)?;
    }
    if let Some(version) = components.version.as_deref() {
        purl.with_version(version)?;
    }
    if let Some(subpath) = components.subpath.as_deref() {
        purl.with_subpath(subpath)?;
    }
    for (key, value) in components.qualifiers.iter().flatten() {
        purl.add_qualifier(key.as_str(), value.as_str())?;
    }
    purl.validate()?;
    Ok(purl.to_string())
}

fn compare(purl: &PackageUrl, expected: &Components) -> Check {
    let mut diffs: Vec<String> = Vec::new();

    let want_ty = expected.ty.as_deref().unwrap_or("");
    if purl.ty() != want_ty {
        diffs.push(format!("type got {:?} want {want_ty:?}", purl.ty()));
    }
    let want_name = expected.name.as_deref().unwrap_or("");
    if purl.name() != want_name {
        diffs.push(format!("name got {:?} want {want_name:?}", purl.name()));
    }
    if purl.namespace() != expected.namespace.as_deref() {
        diffs.push(format!(
            "namespace got {:?} want {:?}",
            purl.namespace(),
            expected.namespace.as_deref()
        ));
    }
    if purl.version() != expected.version.as_deref() {
        diffs.push(format!(
            "version got {:?} want {:?}",
            purl.version(),
            expected.version.as_deref()
        ));
    }
    if purl.subpath() != expected.subpath.as_deref() {
        diffs.push(format!(
            "subpath got {:?} want {:?}",
            purl.subpath(),
            expected.subpath.as_deref()
        ));
    }
    let want_quals: BTreeMap<&str, &str> = expected
        .qualifiers
        .iter()
        .flatten()
        .map(|(key, value)| (key.as_str(), value.as_str()))
        .collect();
    let got_quals: BTreeMap<&str, &str> = purl
        .qualifiers()
        .iter()
        .map(|(key, value)| (key.as_ref(), value.as_ref()))
        .collect();
    if want_quals != got_quals {
        diffs.push(format!("qualifiers got {got_quals:?} want {want_quals:?}"));
    }

    if diffs.is_empty() {
        Ok(())
    } else {
        Err(diffs.join(", "))
    }
}

// ---------------------------------------------------------------------------
// Case identity
// ---------------------------------------------------------------------------

fn opt(value: &Option<String>) -> &str {
    value.as_deref().unwrap_or("")
}

/// A stable identity for a case: `(file, test_type, input)`. Cases that share
/// this key always share their expected outcome, so collapsing them is safe.
fn logical_key(file: &str, case: &Case) -> String {
    let test_type = case.test_type.as_str();
    match &case.input {
        Io::Purl(purl) => format!("{file}::{test_type}::purl={purl}"),
        Io::Components(components) => {
            let qualifiers = components
                .qualifiers
                .as_ref()
                .map(|map| {
                    map.iter()
                        .map(|(key, value)| format!("{key}={value}"))
                        .collect::<Vec<_>>()
                        .join(",")
                })
                .unwrap_or_default();
            format!(
                "{file}::{test_type}::build[type={}|ns={}|name={}|ver={}|qual={}|sub={}]",
                opt(&components.ty),
                opt(&components.namespace),
                opt(&components.name),
                opt(&components.version),
                qualifiers,
                opt(&components.subpath),
            )
        }
    }
}

// ---------------------------------------------------------------------------
// Loading
// ---------------------------------------------------------------------------

/// Loads every case from the vendored suite.
fn load() -> Vec<(String, Case)> {
    let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/purl-spec");
    assert!(
        root.join("spec").is_dir(),
        "vendored purl-spec suite missing at {} — run scripts/update-purl-spec-tests.sh",
        root.display()
    );
    let mut cases = Vec::new();
    for sub in ["spec", "types"] {
        let dir = root.join(sub);
        let mut files: Vec<PathBuf> = fs::read_dir(&dir)
            .unwrap_or_else(|err| panic!("cannot read {}: {err}", dir.display()))
            .filter_map(|entry| entry.ok().map(|entry| entry.path()))
            .filter(|path| path.extension().and_then(|ext| ext.to_str()) == Some("json"))
            .collect();
        files.sort();
        for file in files {
            let name = file
                .file_name()
                .expect("directory entry has a file name")
                .to_string_lossy()
                .into_owned();
            let text = fs::read_to_string(&file)
                .unwrap_or_else(|err| panic!("cannot read {}: {err}", file.display()));
            let suite: SuiteFile = serde_json::from_str(&text)
                .unwrap_or_else(|err| panic!("cannot parse {name}: {err}"));
            for case in suite.tests {
                cases.push((name.clone(), case));
            }
        }
    }
    cases
}

// ---------------------------------------------------------------------------
// Test
// ---------------------------------------------------------------------------

/// Print every non-conformant key so `KNOWN_GAPS` can be rebuilt. Enabled with
/// `PURL_CONFORMANCE_DUMP=1`.
fn dump(cases: &[(String, Case)], gaps: &HashMap<&str, &str>) {
    let mut seen: HashSet<String> = HashSet::new();
    let mut lines: Vec<String> = Vec::new();
    for (file, case) in cases {
        if let Err(detail) = evaluate(case) {
            let key = logical_key(file, case);
            if seen.insert(key.clone()) {
                let status = if gaps.contains_key(key.as_str()) {
                    "GAP"
                } else {
                    "NEW"
                };
                lines.push(format!("{status}\t{key}\t{detail}"));
            }
        }
    }
    lines.sort();
    println!("=== PURL_CONFORMANCE_DUMP BEGIN ({}) ===", lines.len());
    for line in &lines {
        println!("{line}");
    }
    println!("=== PURL_CONFORMANCE_DUMP END ===");
}

#[test]
fn purl_spec_conformance() {
    let cases = load();
    assert!(!cases.is_empty(), "the vendored suite contains no cases");

    let mut gaps: HashMap<&str, &str> = HashMap::new();
    for &(key, note) in KNOWN_GAPS {
        assert!(
            gaps.insert(key, note).is_none(),
            "duplicate KNOWN_GAPS key: {key}"
        );
    }

    let mut total = 0usize;
    let mut nonconformant: HashSet<String> = HashSet::new();
    let mut regressions: Vec<String> = Vec::new();

    for (file, case) in &cases {
        total += 1;
        if let Err(detail) = evaluate(case) {
            let key = logical_key(file, case);
            if nonconformant.insert(key.clone()) && !gaps.contains_key(key.as_str()) {
                regressions.push(format!("  {key}\n      {}{detail}", case.description));
            }
        }
    }

    let mut resolved: Vec<String> = KNOWN_GAPS
        .iter()
        .filter(|(key, _)| !nonconformant.contains(*key))
        .map(|(key, note)| format!("  {key}{note}"))
        .collect();

    if std::env::var_os("PURL_CONFORMANCE_DUMP").is_some() {
        dump(&cases, &gaps);
    }

    regressions.sort();
    resolved.sort();

    if regressions.is_empty() && resolved.is_empty() {
        return;
    }

    let mut report = vec![format!(
        "purl-spec conformance: {total} cases, {} non-conformant, {} known-gap keys",
        nonconformant.len(),
        KNOWN_GAPS.len(),
    )];
    if !regressions.is_empty() {
        report.push(format!(
            "\nREGRESSIONS — {} case(s) fail that are not in KNOWN_GAPS:",
            regressions.len()
        ));
        report.extend(regressions);
    }
    if !resolved.is_empty() {
        report.push(format!(
            "\nRESOLVED — {} KNOWN_GAPS entr(y|ies) now conformant; remove them:",
            resolved.len()
        ));
        report.extend(resolved);
    }
    panic!("{}", report.join("\n"));
}