use std::path::{Path, PathBuf};
use zenkey_model::bundle::Bundle;
use zenkey_model::canonical::Fingerprint;
use zenkey_model::contract::{Contract, check_set, load_path};
fn examples() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join("../examples/zk2")
}
fn contract_files(dir: &Path) -> Vec<PathBuf> {
let mut out = Vec::new();
for e in std::fs::read_dir(dir).unwrap().flatten() {
let p = e.path();
if p.is_dir() {
out.extend(contract_files(&p));
} else if let Some(n) = p.file_name().and_then(|n| n.to_str())
&& n.ends_with(".toml")
&& !n.ends_with(".bindings.toml")
&& !n.ends_with(".enrollment.toml")
&& !n.ends_with(".face.toml")
{
out.push(p);
}
}
out.sort();
out
}
fn profile_contracts() -> Vec<PathBuf> {
let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../spec/profiles");
let mut out = Vec::new();
for e in std::fs::read_dir(&root).unwrap().flatten() {
let dir = e.path();
let Some(name) = dir.file_name().and_then(|n| n.to_str()).map(str::to_owned) else {
continue;
};
if !dir.is_dir() || name.starts_with('.') {
continue;
}
for f in std::fs::read_dir(&dir).unwrap().flatten() {
let p = f.path();
if p.is_file()
&& p.file_name()
.and_then(|n| n.to_str())
.is_some_and(|n| n.starts_with(&format!("{name}.v")) && n.ends_with(".toml"))
{
out.push(p);
}
}
}
out.sort();
out
}
#[test]
fn every_example_contract_validates_fingerprints_and_bundles() {
let mut failures = Vec::new();
let mut contracts: Vec<Contract> = Vec::new();
for p in contract_files(&examples()) {
let l = load_path(&p);
let rel = p.strip_prefix(examples()).unwrap().display().to_string();
if !l.report.0.is_empty() {
eprintln!("== {rel}\n{}", l.report);
failures.push(rel.clone());
}
match l.contract {
None => {}
Some(c) => {
let b = Bundle::build(&c);
let v = Bundle::verify_expecting(&b.to_bytes(), &Fingerprint::of(&c));
assert!(v.is_ok(), "{rel}: {:?}", v.err());
contracts.push(c);
}
}
}
assert!(
failures.is_empty(),
"example contracts with findings: {failures:?}"
);
assert!(
contracts.len() >= 22,
"only {} contracts found",
contracts.len()
);
let standard: Vec<Contract> = profile_contracts()
.iter()
.map(|p| load_path(p).contract.expect("tests/profiles.rs: it loads"))
.collect();
assert!(!standard.is_empty(), "health.v1's standard contract");
let refs: Vec<&Contract> = contracts.iter().chain(&standard).collect();
let set = check_set(&refs);
assert!(!set.has_errors(), "{set}");
}
const HOST_AGAIN: [(&str, &str); 13] = [
("zenoh-modem/modem.v3.toml", "sdu/dropped_total"),
("zenoh-modem/modem.v3.toml", "sched/queue_depth"),
("zenoh-modem/modem.v3.toml", "air/duty_cycle_available"),
("zenoh-modem/modem.v3.toml", "air/duty_cycle_wait_ms"),
("zenoh-modem/modem.v3.toml", "frames/link"),
("zenoh-modem/modem.v3.toml", "frames/sdu"),
("zenoh-modem/modem.v3.toml", "frames/sched"),
("zenoh-modem/modem.v3.toml", "frames/air"),
("zenoh-modem/modem.v3.toml", "frames/layer"),
("zenoh-modem/modem.v3.toml", "frames/if"),
("zenoh-modem/modem.v3.toml", "frames/radio"),
("zenoh-modem/modem.v3.toml", "frames/bearer"),
("zenoh-modem/modem.v3.toml", "frames/message"),
];
#[test]
fn every_example_is_published_and_compatible_with_its_history() {
use zenkey_model::compat::{Class, Revision, check_history, same_revision};
let root = examples().join(".history");
let problems = zenkey_model::history::check_tagged(&root);
assert!(problems.is_empty(), "{problems:#?}");
for p in contract_files(&examples()) {
let rel = p.strip_prefix(examples()).unwrap().display().to_string();
let c = load_path(&p).contract.expect("validated above");
let dir = root.join(c.iface.to_string());
let mut revs = Vec::new();
for e in std::fs::read_dir(&dir)
.unwrap_or_else(|_| panic!("{rel}: no history at {}", dir.display()))
{
let bytes = std::fs::read(e.unwrap().path()).unwrap();
revs.push(Revision::of_bundle(&Bundle::verify(&bytes).unwrap()));
}
let new = Revision::of(&c);
assert!(
revs.iter().any(|r| same_revision(r, &new)),
"{rel}: the current revision is not published; run zk2 contract bundle {rel} --history examples/zk2/.history"
);
let v = check_history(&revs, &new);
let accepted = v.findings.iter().all(|f| {
f.rule == "uses_changed"
|| (f.rule == "annotations_changed"
&& HOST_AGAIN
.iter()
.any(|(file, at)| rel == *file && f.at == format!("resources.\"{at}\"")))
});
assert!(
v.class() == Class::Compatible || (v.class() == Class::Review && accepted),
"{rel}: {:#?}",
v.findings
);
}
}
#[test]
fn every_example_telemetry_declaration_is_valid() {
use zenkey_model::telemetry::{self, Carrier, Declaration};
let mut failures = Vec::new();
let (mut kinds, mut units, mut points) = (0, 0, 0);
for p in contract_files(&examples()) {
let rel = p.strip_prefix(examples()).unwrap().display().to_string();
let c = load_path(&p).contract.expect("validated above");
for r in &c.resources {
let at = format!("{rel}: {} {}", r.token.as_str(), r.template.as_str());
match telemetry::declaration_of(&c, r) {
Declaration::Declared(d) => {
kinds += usize::from(d.kind.is_some());
units += usize::from(d.unit.is_some());
points += usize::from(d.carrier == Carrier::Point);
if d.carrier == Carrier::Point {
assert!(
c.uses.iter().any(|u| u.to_string() == telemetry::PROFILE),
"{at}: a contract carrying the Point lists telemetry.v1 (ยง2.1)"
);
}
}
Declaration::Invalid(why) => failures.push(format!("{at}: {why:?}")),
Declaration::Ignored => failures.push(format!("{at}: ignored")),
Declaration::NotThisProfiles => {}
}
}
}
assert!(failures.is_empty(), "{failures:#?}");
assert_eq!(kinds, 73, "zenoh-modem's declared kinds");
assert_eq!(units, 25, "zenoh-modem's 23 units and netring's 2");
assert_eq!(
points, 355,
"ZenSight's Point-typed resources: sysinfo 136, snmp 127, netring 75, parallax 17"
);
}
#[test]
fn every_example_timing_declaration_is_valid() {
use zenkey_model::timing::{self, Declaration};
let mut failures = Vec::new();
let (mut periods, mut deadlines, mut lifespans) = (0, 0, 0);
for p in contract_files(&examples()) {
let rel = p.strip_prefix(examples()).unwrap().display().to_string();
let c = load_path(&p).contract.expect("validated above");
for r in &c.resources {
let at = format!("{rel}: {} {}", r.token.as_str(), r.template.as_str());
match timing::declaration_of(r) {
Declaration::Declared(d) => {
periods += usize::from(d.period.is_some());
deadlines += usize::from(d.deadline.is_some());
lifespans += usize::from(d.lifespan.is_some());
if d.below_period() {
failures.push(format!("{at}: a deadline below its period"));
}
assert!(
c.uses.iter().any(|u| u.to_string() == timing::PROFILE),
"{at}: a contract writing the vocabulary lists timing.v1"
);
}
Declaration::Invalid(why) => failures.push(format!("{at}: {why:?}")),
Declaration::Ignored => failures.push(format!("{at}: ignored")),
Declaration::NotThisProfiles => {}
}
}
for (role, req) in &c.requires {
if timing::requirement_declaration(&req.annotations) != Declaration::NotThisProfiles {
failures.push(format!("{rel}: requirement {role}: ignored"));
}
}
}
assert!(failures.is_empty(), "{failures:#?}");
assert_eq!(
(periods, deadlines, lifespans),
(2, 1, 2),
"twist_cmd.v1's cmd and nav.v2's position"
);
}
#[test]
fn every_example_arbitrated_role_is_valid() {
use std::collections::BTreeMap;
use std::time::Duration;
use zenkey_model::arbitration::{self, Declaration};
let contracts: Vec<(String, Contract)> = contract_files(&examples())
.into_iter()
.map(|p| {
let rel = p.strip_prefix(examples()).unwrap().display().to_string();
(rel, load_path(&p).contract.expect("validated above"))
})
.collect();
let by_iface: BTreeMap<String, &Contract> = contracts
.iter()
.map(|(_, c)| (c.iface.to_string(), c))
.collect();
let mut arbitrated = Vec::new();
for (rel, c) in &contracts {
for (role, req) in &c.requires {
let provider = by_iface.get(&req.interface.to_string()).copied();
let d = arbitration::declaration_of(c, role, provider, &BTreeMap::new())
.expect("the role is declared");
match d {
Declaration::NotArbitrated => {}
Declaration::Arbitrated(a) => arbitrated.push((rel.clone(), role.clone(), a)),
other => panic!("{rel}: requires.{role}: {other:?}"),
}
}
}
assert_eq!(arbitrated.len(), 1, "{arbitrated:#?}");
let (rel, role, a) = &arbitrated[0];
assert_eq!(
(rel.as_str(), role.as_str()),
("walkthrough/thruster.v1.toml", "cmd")
);
assert_eq!(a.iface.to_string(), "twist_cmd.v1");
assert_eq!(a.resource(), "stream/cmd");
assert_eq!(a.deadline, Duration::from_millis(100));
assert_eq!(a.period, Some(Duration::from_millis(20)));
}
#[test]
fn the_zenoh_modem_catalog_holds_its_contracts() {
use zenkey_model::catalog::Catalog;
let path = examples().join("zenoh-modem/catalog.json");
let bytes = std::fs::read(&path).unwrap();
let c = Catalog::parse_bytes(&bytes).unwrap_or_else(|e| panic!("{}: {e}", path.display()));
let mut jcs = c.to_jcs();
jcs.push(b'\n');
assert_eq!(bytes, jcs, "catalog.json is its JCS bytes and a newline");
assert_eq!(c.epoch(), 1_767_225_600);
let profiles = Path::new(env!("CARGO_MANIFEST_DIR")).join("../spec/profiles");
for file in [
examples().join("zenoh-modem/modem.v3.toml"),
examples().join("zenoh-modem/config/sx1262_config.v1.toml"),
profiles.join("config/config.v1.toml"),
profiles.join("health/health.v1.toml"),
profiles.join("freshness/freshness.v2.toml"),
] {
let k = load_path(&file).contract.expect("validated above");
let fp = Fingerprint::of(&k);
assert!(
c.index_of(&k.iface, &fp).is_some(),
"{}: {} {fp} is not in catalog.json; run zk2 catalog add",
file.display(),
k.iface
);
}
for e in c.entries() {
let published = [examples().join(".history"), profiles.join(".history")]
.iter()
.any(|root| zenkey_model::history::path_for(root, &e.iface, &e.contract).is_file());
assert!(
published,
"{} {} is published in no history",
e.iface, e.contract
);
}
}
#[test]
fn every_adopter_frame_has_its_largest_frame() {
use std::collections::BTreeMap;
let largest = |file: PathBuf| -> BTreeMap<String, u64> {
let c: Contract = load_path(&file).contract.expect("validated above");
c.resources
.iter()
.filter_map(|r| {
zenkey_model::frame::Layout::of_resource(&c, r)
.expect("its layout reads")
.map(|l| (r.template.as_str().to_owned(), l.largest_payload_bytes()))
})
.collect()
};
let modem = largest(examples().join("zenoh-modem/modem.v3.toml"));
let want: BTreeMap<String, u64> = [
("frames/air", 50),
("frames/bearer", 33),
("frames/fast", 216),
("frames/if", 60),
("frames/layer", 109),
("frames/link", 22),
("frames/message", 213),
("frames/paid", 207),
("frames/radio", 22),
("frames/sched", 33),
("frames/sdu", 52),
("frames/slow", 216),
]
.into_iter()
.map(|(k, v)| (k.to_owned(), v))
.collect();
assert_eq!(modem, want, "zenoh-modem/README.md's payloads");
let lane = ["unixsock-stream".to_owned()];
let fits = |f: &str, batch: u64| {
zenkey_model::link::fits_batch(
modem[f],
batch,
zenkey_model::link::PutQos::DATA_DROP,
zenkey_model::link::Sequence::Bit16,
&lane,
)
};
assert!(fits("frames/paid", 222), "RF's batch: a full record");
for f in ["frames/fast", "frames/slow"] {
assert!(!fits(f, 222) && !fits(f, 224), "{f}: RF's and LoRa's batch");
assert!(fits(f, 226), "{f}: 216 B and a 10-B allowance");
}
assert!(!fits("frames/fast", 117), "SF9's batch: frame_over_batch");
let gendev = largest(
Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../docs/zk2/measurements/wire-baseline/contracts/gendev.v1.toml"),
);
assert_eq!(gendev["frames/sample"], 100);
}
#[test]
fn the_zenoh_modem_node_descriptor_is_22_bytes_compact() {
use zenkey_model::catalog::Catalog;
use zenkey_model::descriptor::compact::{Compact, decode, encode, gated};
use zenkey_model::descriptor::{Descriptor, FORMAT, InterfaceEntry};
use zenkey_model::grammar::{Addr, InstanceId};
use zenkey_model::wire::Seq;
let catalog =
Catalog::parse_bytes(&std::fs::read(examples().join("zenoh-modem/catalog.json")).unwrap())
.unwrap();
let profiles = Path::new(env!("CARGO_MANIFEST_DIR")).join("../spec/profiles");
let revisions: Vec<Contract> = [
examples().join("zenoh-modem/modem.v3.toml"),
profiles.join("config/config.v1.toml"),
profiles.join("health/health.v1.toml"),
profiles.join("freshness/freshness.v2.toml"),
]
.iter()
.map(|f| load_path(f).contract.expect("validated above"))
.collect();
let refs: Vec<&Contract> = revisions.iter().collect();
let capabilities = gated(&revisions[0]);
assert_eq!(capabilities.len(), 30, "modem.v3's capabilities");
assert!(revisions[1..].iter().all(|c| gated(c).is_empty()));
let node = |run: &str, zid: Option<&str>| Compact {
descriptor: Descriptor {
format: FORMAT.to_owned(),
service: "h-3fa9c2d41b7e/rf0".into(),
instance: "3fa9c2d41b7e0012".into(),
run: Some(run.into()),
catalog: Some(catalog.id().hex()),
interfaces: revisions
.iter()
.map(|c| InterfaceEntry {
iface: c.iface.to_string(),
contract: Fingerprint::of(c).to_string(),
minor: None,
standby: false,
token: true,
unavailable: Vec::new(),
cardinality: Default::default(),
})
.collect(),
capabilities: capabilities.clone(),
requires: Vec::new(),
profiles: Vec::new(),
meta: zid
.map(|z| [("zid".to_owned(), z.into())].into_iter().collect())
.unwrap_or_default(),
},
seq: Seq::new(41).unwrap(),
minted: false,
};
let held = [catalog.clone()];
let size = |v: &Compact| encode(v, &held, &refs).unwrap().len();
assert_eq!(size(&node("0003", None)), 22, "the memo's 22 B");
assert_eq!(size(&node("0000a1b2", None)), 24, "a 32-bit run");
assert_eq!(
size(&node("0003", Some("a1b2c3d4e5f60718293a4b5c6d7e8f90"))),
38,
"with the zid"
);
let v = node("0003", None);
let bytes = encode(&v, &held, &refs).unwrap();
let addr = Addr::new("h-3fa9c2d41b7e", "rf0").unwrap();
let instance: InstanceId = "3fa9c2d41b7e0012".parse().unwrap();
let back = decode(&bytes, &addr, &instance, &held, &refs).unwrap();
assert_eq!(back.descriptor.interfaces, {
let mut i = v.descriptor.interfaces.clone();
i.sort_by_key(|e| {
catalog.index_of(
&e.iface.parse().unwrap(),
&Fingerprint::parse(&e.contract).unwrap(),
)
});
i
});
assert_eq!(back.descriptor.capabilities, capabilities);
assert_eq!(
back.descriptor.profiles,
["freshness.v2", "link.v1", "telemetry.v2"],
"the revisions' uses, sorted"
);
assert_eq!(encode(&back, &held, &refs).unwrap(), bytes, "writes back");
}