use pkix_path::{DefaultVerifier, Error, TrustAnchor, ValidationPolicy, validate_path};
use x509_cert::Certificate;
use x509_cert::der::Decode as _;
const NOW: u64 = 1_600_000_000; const DIR: &str = "tests/fixtures/nc-intersection";
fn load(name: &str) -> Certificate {
let path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join(DIR)
.join(name);
let der = std::fs::read(&path).unwrap_or_else(|e| panic!("read {name}: {e}"));
Certificate::from_der(&der).unwrap_or_else(|e| panic!("parse {name}: {e}"))
}
fn run(chain: &[&str]) -> pkix_path::Result<pkix_path::ValidatedPath> {
let certs: Vec<Certificate> = chain.iter().map(|n| load(n)).collect();
let anchors =
[TrustAnchor::from_cert(load("root.der")).expect("fixture CA is a usable trust anchor")];
validate_path(
&certs,
&anchors,
&ValidationPolicy::new(NOW),
&DefaultVerifier,
)
}
#[test]
fn intersecting_against_a_broader_subtree_keeps_the_specific_entries() {
run(&["leaf-other-a.der", "ca-broad.der", "ca-specific.der"]).expect(
"both CAs permit other.a.example: ca-specific through a.example, \
ca-broad through example",
);
}
#[test]
fn a_name_outside_the_intersection_is_refused() {
let r = run(&["leaf-other-example.der", "ca-broad.der", "ca-specific.der"]);
assert!(
matches!(r, Err(Error::NameConstraintViolation { index: 0 })),
"host.other.example is permitted by ca-broad alone, got: {r:?}"
);
}
#[test]
fn a_second_exclusion_of_the_same_name_form_still_applies() {
let r = run(&["leaf-in-b.der", "ca-excl-b.der", "ca-excl-a.der"]);
assert!(
matches!(r, Err(Error::NameConstraintViolation { index: 0 })),
"host.b.example is excluded by ca-excl-b, got: {r:?}"
);
}
#[test]
fn a_broader_exclusion_is_not_a_duplicate_of_a_narrower_one() {
let r = run(&["leaf-other-bad.der", "ca-excl-bad.der", "ca-excl-sub.der"]);
assert!(
matches!(r, Err(Error::NameConstraintViolation { index: 0 })),
"other.bad.example is excluded by ca-excl-bad, got: {r:?}"
);
}