[roads] The road model, validator, compiler and explain; proto's versioned CBOR messages; the via command
Road files in Pensum's strict header subset (unknown keys are errors, random ids), the directory and device snapshots, deny-by-default compilation to one plan per device, explain from the compiler's own grants, CBOR envelope v1 with version-first refusals. Tests: the six mandatory ones (validation fixtures for every rule, revocation, the least-privilege property against an independent oracle, determinism with golden plans, explain, custom round-trip), QA's attack cases, the protocol and the CLI.
This commit is contained in:
@@ -0,0 +1,151 @@
|
||||
//! Shared test helpers: the `home` fixture store and in-memory stores. No real network, no real keys: device
|
||||
//! keys are 32 repeated bytes.
|
||||
|
||||
#![allow(dead_code)]
|
||||
|
||||
use jiff::Timestamp as Instant;
|
||||
use roads::{Compiled, DeviceKey, Store, compile};
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub const NOW: &str = "2026-11-25T12:00:00+01:00";
|
||||
|
||||
pub fn now() -> Instant {
|
||||
at(NOW)
|
||||
}
|
||||
|
||||
pub fn at(s: &str) -> Instant {
|
||||
s.parse().unwrap()
|
||||
}
|
||||
|
||||
/// The fixture device key `n`: 32 bytes of `n`.
|
||||
pub fn key(n: u8) -> DeviceKey {
|
||||
DeviceKey::from_bytes(&[n; 32])
|
||||
}
|
||||
|
||||
pub fn fixtures() -> PathBuf {
|
||||
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("tests")
|
||||
.join("fixtures")
|
||||
}
|
||||
|
||||
pub fn home_dir() -> PathBuf {
|
||||
fixtures().join("home")
|
||||
}
|
||||
|
||||
/// The home store as bytes: road files (name, contents), directory.json, devices.json.
|
||||
pub struct Parts {
|
||||
pub roads: Vec<(String, Vec<u8>)>,
|
||||
pub directory: Vec<u8>,
|
||||
pub devices: Vec<u8>,
|
||||
}
|
||||
|
||||
impl Parts {
|
||||
pub fn home() -> Parts {
|
||||
let dir = home_dir();
|
||||
let mut roads = Vec::new();
|
||||
for e in std::fs::read_dir(dir.join("roads")).unwrap() {
|
||||
let e = e.unwrap();
|
||||
roads.push((
|
||||
e.file_name().to_string_lossy().into_owned(),
|
||||
std::fs::read(e.path()).unwrap(),
|
||||
));
|
||||
}
|
||||
roads.sort();
|
||||
Parts {
|
||||
roads,
|
||||
directory: std::fs::read(dir.join("directory.json")).unwrap(),
|
||||
devices: std::fs::read(dir.join("devices.json")).unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn store(&self) -> Store {
|
||||
Store::from_parts(&self.roads, Some(&self.directory), Some(&self.devices))
|
||||
}
|
||||
|
||||
pub fn compile(&self) -> Compiled {
|
||||
compile(&self.store(), now())
|
||||
}
|
||||
|
||||
pub fn with_road(mut self, name: &str, text: &str) -> Parts {
|
||||
self.roads
|
||||
.push((name.to_string(), text.as_bytes().to_vec()));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn without_road(mut self, prefix: &str) -> Parts {
|
||||
let before = self.roads.len();
|
||||
self.roads.retain(|(n, _)| !n.starts_with(prefix));
|
||||
assert_eq!(
|
||||
self.roads.len() + 1,
|
||||
before,
|
||||
"no road file starts with {prefix}"
|
||||
);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn road_text(&self, prefix: &str) -> String {
|
||||
let (_, b) = self
|
||||
.roads
|
||||
.iter()
|
||||
.find(|(n, _)| n.starts_with(prefix))
|
||||
.expect("a road");
|
||||
String::from_utf8(b.clone()).unwrap()
|
||||
}
|
||||
|
||||
pub fn replace_road(mut self, prefix: &str, text: &str) -> Parts {
|
||||
let slot = self
|
||||
.roads
|
||||
.iter_mut()
|
||||
.find(|(n, _)| n.starts_with(prefix))
|
||||
.expect("a road");
|
||||
slot.1 = text.as_bytes().to_vec();
|
||||
self
|
||||
}
|
||||
|
||||
pub fn edit_devices(mut self, f: impl FnOnce(&str) -> String) -> Parts {
|
||||
self.devices = f(std::str::from_utf8(&self.devices).unwrap()).into_bytes();
|
||||
self
|
||||
}
|
||||
|
||||
pub fn edit_directory(mut self, f: impl FnOnce(&str) -> String) -> Parts {
|
||||
self.directory = f(std::str::from_utf8(&self.directory).unwrap()).into_bytes();
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// Every plan as JSON, in device order: the bytes the determinism tests compare.
|
||||
pub fn plans_bytes(c: &Compiled) -> Vec<u8> {
|
||||
let mut out = Vec::new();
|
||||
for p in c.plans.values() {
|
||||
out.extend_from_slice(p.to_json().as_bytes());
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Whether `from` may open a connection to `dst` on the receiving side, according to the plans: the
|
||||
/// firewall of the device that owns or routes `dst` has a rule from `from`'s address.
|
||||
pub fn fw_accepts(
|
||||
c: &Compiled,
|
||||
from: &DeviceKey,
|
||||
dst: std::net::IpAddr,
|
||||
proto_port: Option<(&str, u16)>,
|
||||
) -> bool {
|
||||
let src = c.plans[from].address;
|
||||
c.plans.values().any(|p| {
|
||||
p.firewall.rules.iter().any(|r| {
|
||||
let dst_ok = match r.dst {
|
||||
roads::plan::Dst::Net(n) => n.contains_addr(&dst),
|
||||
roads::plan::Dst::Internet => roads::net::Net::host(dst).reaches_internet(),
|
||||
};
|
||||
let port_ok = match (r.proto, r.ports, proto_port) {
|
||||
(roads::ports::Proto::Any, _, _) => true,
|
||||
(_, _, None) => false,
|
||||
(proto, ports, Some((want, port))) => {
|
||||
format!("{proto:?}").eq_ignore_ascii_case(want)
|
||||
&& ports.is_none_or(|[lo, hi]| lo <= port && port <= hi)
|
||||
}
|
||||
};
|
||||
r.src.contains_addr(&src.0) && dst_ok && port_ok
|
||||
})
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user