~ knotless Search/
↑↓ move↵ open⇧↵ all results

johnandersen777.bsky.social/vuln-notif

0
0
main
269 lines · 9.0 KB Raw History
//! Golden tests for record normalization and deterministic encodings.

use std::io::Read;
use std::path::PathBuf;

use serde_json::{Value, json};

use vuln_notif::db::model::{CveRow, cache_json};
use vuln_notif::records::{
    canonical_bytes, cbor_bytes, cidv1_raw, digest_rkey, normalize_cve, prepare_row, purl_rkey,
    sanitize_rkey, sha256_hex,
};

fn fixtures() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/records")
}

fn load_json(path: &std::path::Path) -> Value {
    let raw = std::fs::read_to_string(path).expect("read fixture");
    serde_json::from_str(&raw).expect("parse fixture")
}

fn manifest() -> Value {
    load_json(&fixtures().join("manifest.json"))
}

fn field<'a>(value: &'a Value, key: &str) -> &'a Value {
    value.get(key).unwrap_or_else(|| panic!("missing {key}"))
}

fn text<'a>(value: &'a Value, key: &str) -> &'a str {
    field(value, key)
        .as_str()
        .unwrap_or_else(|| panic!("{key} not a string"))
}

fn number(value: &Value, key: &str) -> u64 {
    field(value, key)
        .as_u64()
        .unwrap_or_else(|| panic!("{key} not a number"))
}

fn gunzip(bytes: &[u8]) -> Vec<u8> {
    let mut decoder = flate2::read::GzDecoder::new(bytes);
    let mut payload = Vec::new();
    decoder
        .read_to_end(&mut payload)
        .expect("gunzip fixture blob");
    payload
}

#[test]
fn golden_manifest_cases_match_reference() {
    let manifest = manifest();
    let frozen = text(&manifest, "frozen_created_at");
    let captures = field(&manifest, "captures").as_object().expect("captures");

    for (name, capture) in captures {
        let cve = load_json(&fixtures().join(text(capture, "input")));
        let data = cache_json(&cve);
        let row = CveRow {
            id: text(capture, "cve_id").to_owned(),
            data: data.clone(),
        };
        let prepared = prepare_row(row, frozen);

        assert_eq!(prepared.cve_id, text(capture, "cve_id"), "{name}");
        assert_eq!(prepared.mode, text(capture, "mode"), "{name}");
        assert_eq!(
            prepared.reason.as_deref(),
            field(capture, "reason").as_str(),
            "{name}"
        );
        assert_eq!(
            data.chars().count() as u64,
            number(capture, "raw_json_chars"),
            "{name} raw chars"
        );
        assert_eq!(
            sanitize_rkey(&prepared.cve_id),
            text(capture, "rkey"),
            "{name}"
        );

        let canonical = canonical_bytes(&prepared.record).expect("canonical");
        assert_eq!(
            sha256_hex(&canonical),
            text(capture, "canonical_json_sha256"),
            "{name} canonical sha"
        );

        let cbor = cbor_bytes(&prepared.record).expect("cbor");
        assert_eq!(
            cbor.len() as u64,
            number(capture, "normal_or_compact_cbor_bytes"),
            "{name} cbor size"
        );
        assert_eq!(
            sha256_hex(&cbor),
            text(capture, "cbor_sha256"),
            "{name} cbor sha"
        );

        let cid = atproto_dasl::compute_cid_from_json(&prepared.record)
            .expect("cid")
            .to_string();
        assert_eq!(cid, text(capture, "cid"), "{name} cid");

        assert_eq!(
            prepared.record_bytes,
            number(capture, "prepared_record_bytes"),
            "{name} record bytes"
        );

        match prepared.blob.as_deref() {
            Some(blob) => {
                let payload = gunzip(blob);
                assert_eq!(
                    payload.len() as u64,
                    number(capture, "blob_payload_bytes"),
                    "{name} payload bytes"
                );
                assert_eq!(
                    sha256_hex(&payload),
                    text(capture, "blob_payload_json_sha256"),
                    "{name} payload sha"
                );
                assert_eq!(
                    payload,
                    canonical_bytes(&normalize_cve(&cve)).expect("normalized canonical"),
                    "{name} gzip payload"
                );
                assert_eq!(prepared.blob_bytes, blob.len() as u64, "{name} gzip length");
                if prepared.blob_bytes != number(capture, "gzip_bytes") {
                    eprintln!(
                        "{name}: gzip size mismatch (rust {}, reference {})",
                        prepared.blob_bytes,
                        number(capture, "gzip_bytes")
                    );
                }
                assert_eq!(blob[3], 0, "{name} gzip mtime");
                assert_eq!(&blob[4..8], &[0, 0, 0, 0], "{name} gzip mtime bytes");
                assert_eq!(blob[9], 255, "{name} gzip OS byte");
                let rust_gzip_sha = sha256_hex(blob);
                if rust_gzip_sha != text(capture, "gzip_sha256") {
                    eprintln!(
                        "{name}: gzip byte mismatch (rust {rust_gzip_sha}, reference {})",
                        text(capture, "gzip_sha256")
                    );
                }
            }
            None => {
                assert!(field(capture, "gzip_bytes").is_null(), "{name} gzip");
                assert!(field(capture, "gzip_sha256").is_null(), "{name} gzip sha");
            }
        }

        if prepared.mode == "blob" {
            assert_eq!(
                field(capture, "compact_record"),
                &prepared.record,
                "{name} compact record"
            );
            assert!(prepared.record.get("configurations").is_none(), "{name}");
            let sha = text(&prepared.record, "dataSha256");
            assert_eq!(prepared.sha256.as_deref(), Some(sha), "{name} dataSha256");
            assert_eq!(
                number(&prepared.record, "dataBytes") as usize,
                gunzip(prepared.blob.as_deref().expect("blob")).len(),
                "{name} dataBytes"
            );
        } else {
            assert!(
                field(capture, "compact_record").is_null(),
                "{name} compact record"
            );
        }
    }
}

#[test]
fn normalize_converts_floats_and_timestamps_without_mutating_input() {
    let original = json!({
        "published": "2025-10-01T12:00:00.000",
        "lastModified": "2026-11-01T08:30:00.000+02:00",
        "cvss": 8.8,
        "nested": [1.0, {"score": 3.9, "count": 8}],
        "already": "2024-01-01T00:00:00Z",
        "lower": "2024-01-01T00:00:00z",
        "compact_zone": "2024-01-01T00:00:00-0500",
    });
    let snapshot = original.clone();
    let normalized = normalize_cve(&original);

    assert_eq!(original, snapshot, "input must not be mutated");
    assert_eq!(normalized["published"], json!("2025-10-01T12:00:00.000Z"));
    assert_eq!(
        normalized["lastModified"],
        json!("2026-11-01T08:30:00.000+02:00")
    );
    assert_eq!(normalized["cvss"], json!("8.8"));
    assert_eq!(normalized["nested"][0], json!("1.0"));
    assert_eq!(normalized["nested"][1]["score"], json!("3.9"));
    assert_eq!(normalized["nested"][1]["count"], json!(8));
    assert_eq!(normalized["already"], json!("2024-01-01T00:00:00Z"));
    assert_eq!(normalized["lower"], json!("2024-01-01T00:00:00z"));
    assert_eq!(
        normalized["compact_zone"],
        json!("2024-01-01T00:00:00-0500")
    );
}

#[test]
fn sanitize_rkey_rules_and_truncation() {
    assert_eq!(sanitize_rkey("CVE-2024-3094"), "CVE-2024-3094");
    assert_eq!(sanitize_rkey("CVE 2024/3094!"), "CVE20243094");
    assert_eq!(sanitize_rkey(&"A".repeat(600)), "A".repeat(512));
    assert_eq!(sanitize_rkey("!!!é"), sha256_hex("!!!é".as_bytes()));
}

#[test]
fn deterministic_rkeys_and_cids() {
    assert_eq!(digest_rkey(&["a"]), "2p7w34f73rjie");
    assert_eq!(
        digest_rkey(&[
            "manifest",
            "at://did:plc:x/com.nahdig.cve.nvdV2/CVE-1",
            "1.2.3"
        ]),
        "25wmvbehog4dm"
    );
    assert_eq!(digest_rkey(&[]), "2bga6cegjsb2o");
    for input in ["a", "b", "c", "", "manifest|x|y", "status|z"] {
        let rkey = digest_rkey(&[input]);
        assert!(
            matches!(rkey.as_bytes()[0], b'2'..=b'7' | b'a' | b'b'),
            "invalid tid first character: {rkey}"
        );
        assert_eq!(rkey.len(), 13);
    }
    assert_eq!(
        purl_rkey("pkg:npm/acme@1.2.3"),
        "dc0419b1eb305708e9e496be3c7a32ebff6c1a48947aadde913d8d41c513caa1"
    );
    assert_eq!(
        cidv1_raw(&sha256_hex(b"")),
        "bafkreihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku"
    );
}

#[test]
fn canonical_json_and_cbor_are_deterministic_and_sorted() {
    let unsorted = json!({
        "z": [{"b": 1, "a": 2}],
        "a": {"d": true, "c": null},
        "emoji": "😀",
    });
    let first = canonical_bytes(&unsorted).expect("canonical");
    let second = canonical_bytes(&unsorted).expect("canonical again");
    assert_eq!(first, second);
    assert_eq!(
        String::from_utf8(first).expect("utf8"),
        r#"{"a":{"c":null,"d":true},"emoji":"😀","z":[{"a":2,"b":1}]}"#
    );
    assert_eq!(
        cbor_bytes(&unsorted).expect("cbor"),
        cbor_bytes(&unsorted).expect("cbor again")
    );
}