use serde_json::Value;
use std::fs;
use vouch_core::status_list::{
decode_bitstring, encode_bitstring, get_status, set_status, verify_status,
DEFAULT_BITSTRING_LENGTH,
};
fn vector() -> Value {
let path = concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../test-vectors/bitstring-status-list/vector.json"
);
serde_json::from_str(&fs::read_to_string(path).expect("read status vector"))
.expect("parse status vector")
}
#[test]
#[cfg_attr(not(feature = "std"), ignore = "needs gunzip (std)")]
fn decodes_shared_list_and_reports_correct_bits() {
let v = vector();
let encoded = v["expected_encoded_list"].as_str().unwrap();
let bits = decode_bitstring(encoded).unwrap();
assert_eq!(
bits.len() * 8,
v["bitstring_length_bits"].as_u64().unwrap() as usize
);
for idx in v["revoked_indices"].as_array().unwrap() {
let i = idx.as_u64().unwrap() as usize;
assert!(get_status(&bits, i).unwrap(), "index {i} must be revoked");
}
for idx in v["active_indices_sample"].as_array().unwrap() {
let i = idx.as_u64().unwrap() as usize;
assert!(!get_status(&bits, i).unwrap(), "index {i} must be active");
}
}
#[test]
#[cfg_attr(not(feature = "std"), ignore = "needs gunzip (std)")]
fn verify_status_matches_sample_entries() {
let v = vector();
let sl = &v["status_list_credential"];
assert!(
verify_status(&v["sample_credential_status_revoked"], sl).unwrap(),
"revoked sample must report set"
);
assert!(
!verify_status(&v["sample_credential_status_active"], sl).unwrap(),
"active sample must report unset"
);
}
#[test]
fn reencode_matches_shared_vector_byte_for_byte() {
let v = vector();
let mut bits = vec![0u8; DEFAULT_BITSTRING_LENGTH / 8];
for idx in v["revoked_indices"].as_array().unwrap() {
set_status(&mut bits, idx.as_u64().unwrap() as usize, true).unwrap();
}
let rust_encoded = encode_bitstring(&bits).unwrap();
assert_eq!(
rust_encoded,
v["expected_encoded_list"].as_str().unwrap(),
"the core must write the shared vector's encodedList byte for byte"
);
#[cfg(feature = "std")]
{
let from_rust = decode_bitstring(&rust_encoded).unwrap();
let from_vector = decode_bitstring(v["expected_encoded_list"].as_str().unwrap()).unwrap();
assert_eq!(
from_rust, from_vector,
"Rust-encoded list must decode to the same bits as the shared vector"
);
}
}
fn strict_vector() -> Value {
let path = concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../test-vectors/bitstring-status-list/strict-profile.json"
);
serde_json::from_str(&fs::read_to_string(path).expect("read strict status vector"))
.expect("parse strict status vector")
}
#[test]
#[cfg_attr(not(feature = "std"), ignore = "needs gunzip (std)")]
fn strict_profile_cases_match_reference() {
let v = strict_vector();
let cases = v["profile_cases"].as_array().expect("profile cases");
assert!(cases.len() > 90);
for case in cases {
let name = case["name"].as_str().unwrap();
let got = verify_status(&case["credentialStatus"], &case["statusListCredential"]);
if case["expected"]["error"] == Value::Bool(true) {
assert!(got.is_err(), "case {name} must be refused, got {got:?}");
} else {
let want = case["expected"]["bit"].as_bool().unwrap();
assert_eq!(got.ok(), Some(want), "case {name}");
}
}
}
#[test]
#[cfg_attr(not(feature = "std"), ignore = "needs gunzip (std)")]
fn full_status_check_matches_reference() {
use vouch_core::status_list::check_credential_status;
let v = strict_vector();
let now = v["now"].as_str().unwrap();
let skew = v["clockSkewSeconds"].as_i64().unwrap();
let cases = v["credential_cases"].as_array().expect("credential cases");
assert!(cases.len() > 20);
for case in cases {
let name = case["name"].as_str().unwrap();
let lists = case["statusLists"].as_object().expect("statusLists");
let got = check_credential_status(&case["credential"], lists, None, None, now, skew)
.unwrap_or_else(|e| panic!("case {name}: {e}"));
assert_eq!(
got.reason,
case["expected"].as_str().unwrap(),
"case {name}: {got:?}"
);
let ok = matches!(got.reason, "status_ok" | "no_status");
assert_eq!(got.ok, ok, "case {name}");
}
}