38a2fea043
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.
149 lines
5.3 KiB
Rust
149 lines
5.3 KiB
Rust
//! Mandatory test 5: `explain` returns exactly the roads that allow access, and nothing when access is denied
|
|
//! — expired, invalid and custom roads included in the store.
|
|
|
|
mod common;
|
|
|
|
use common::{Parts, key};
|
|
use roads::net::Net;
|
|
use roads::{Hostname, Place, explain};
|
|
|
|
fn ids(parts: &Parts, device: u8, place: &Place) -> Vec<String> {
|
|
let c = parts.compile();
|
|
let store = parts.store();
|
|
explain(&c, store.devices.as_ref().unwrap(), &key(device), place)
|
|
.iter()
|
|
.map(|r| r.to_string())
|
|
.collect()
|
|
}
|
|
|
|
fn service(h: &str) -> Place {
|
|
Place::Service(Hostname::new(h).unwrap())
|
|
}
|
|
|
|
fn subnet(s: &str) -> Place {
|
|
Place::Subnet(Net::parse_canonical(s).unwrap())
|
|
}
|
|
|
|
#[test]
|
|
fn allowed_lists_exactly_the_allowing_roads() {
|
|
let home = Parts::home();
|
|
assert_eq!(
|
|
ids(&home, 3, &service("photos.home.arpa")),
|
|
["7tq0mz", "k3f9q2"]
|
|
);
|
|
assert_eq!(
|
|
ids(&home, 3, &Place::Device(key(2))),
|
|
["7tq0mz", "k3f9q2"],
|
|
"a service road reaches part of the device"
|
|
);
|
|
assert_eq!(ids(&home, 1, &subnet("192.168.100.0/24")), ["h2x8vd"]);
|
|
assert_eq!(
|
|
ids(&home, 1, &subnet("192.168.100.10/32")),
|
|
["h2x8vd"],
|
|
"a part of a granted subnet"
|
|
);
|
|
assert_eq!(
|
|
ids(&home, 5, &Place::Device(key(2))),
|
|
["r8m2tg"],
|
|
"the custom road is not a grant"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn each_listed_road_is_enough_on_its_own() {
|
|
// The listed set is the compiler's own: keep only one listed road and access remains.
|
|
let home = Parts::home();
|
|
for keep in ["7tq0mz", "k3f9q2"] {
|
|
let mut only = Parts::home();
|
|
only.roads.retain(|(n, _)| n.starts_with(keep));
|
|
assert_eq!(ids(&only, 3, &service("photos.home.arpa")), [keep]);
|
|
}
|
|
let _ = home;
|
|
}
|
|
|
|
#[test]
|
|
fn denied_returns_nothing_even_with_expired_and_invalid_roads_about_the_place() {
|
|
let parts = Parts::home().with_road(
|
|
"d00001-jana-lan.md",
|
|
"---\nid: d00001\nsubject: person:jana\nverb: can-reach\nobject: subnet:192.168.100.0/24\nuntill: 2026-12-01T00:00:00Z\ncreated: 2026-11-25T10:00:00Z\n---\n",
|
|
);
|
|
assert!(parts.compile().errors().any(|p| p.path.contains("d00001")));
|
|
assert!(ids(&parts, 3, &subnet("192.168.100.0/24")).is_empty());
|
|
assert!(
|
|
ids(&parts, 7, &service("git.home.arpa")).is_empty(),
|
|
"the expired road is not listed"
|
|
);
|
|
assert!(ids(&parts, 7, &Place::Device(key(2))).is_empty());
|
|
assert!(
|
|
ids(&parts, 3, &subnet("10.0.0.0/8")).is_empty(),
|
|
"a place nobody routes"
|
|
);
|
|
assert!(
|
|
ids(&parts, 3, &service("nowhere.home.arpa")).is_empty(),
|
|
"a place that does not exist"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn exits_count_for_the_internet_but_not_for_private_ranges() {
|
|
let home = Parts::home();
|
|
assert_eq!(ids(&home, 7, &Place::Exit(key(6))), ["c0z5wn"]);
|
|
assert_eq!(ids(&home, 7, &subnet("8.8.8.0/24")), ["c0z5wn"]);
|
|
assert!(
|
|
ids(&home, 7, &subnet("192.168.1.0/24")).is_empty(),
|
|
"an exit is not a way into its LAN"
|
|
);
|
|
assert!(ids(&home, 7, &Place::Exit(key(2))).is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn going_direct_takes_a_subnet_out_of_the_exit_unless_the_kill_switch_wins() {
|
|
let direct = "---\nid: d00002\nsubject: device:AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE\nverb: goes-direct\nobject: subnet:8.8.8.0/24\ncreated: 2026-11-25T10:00:00Z\n---\n";
|
|
// mark-pc has a kill switch: it wins, so 8.8.8.0/24 still goes through the exit.
|
|
let with_kill = Parts::home().with_road("d00002-direct.md", direct);
|
|
assert_eq!(ids(&with_kill, 1, &subnet("8.8.8.0/24")), ["p4n6rb"]);
|
|
assert!(
|
|
with_kill
|
|
.compile()
|
|
.problems
|
|
.iter()
|
|
.any(|p| p.code == roads::rules::DIRECT_KILL_SWITCH && p.path.contains("d00002"))
|
|
);
|
|
// Without the kill switch the subnet goes direct, and the exit road no longer explains it.
|
|
let without = Parts::home()
|
|
.without_road("w9c3ke")
|
|
.with_road("d00002-direct.md", direct);
|
|
assert!(ids(&without, 1, &subnet("8.8.8.0/24")).is_empty());
|
|
assert_eq!(ids(&without, 1, &subnet("1.1.1.0/24")), ["p4n6rb"]);
|
|
}
|
|
|
|
#[test]
|
|
fn two_roads_granting_the_same_rule_give_one_rule_and_both_explain_it() {
|
|
// QA R-16: a diamond. Jana is in two groups whose roads both reach the photos on tcp/443.
|
|
let parts = Parts::home()
|
|
.edit_directory(|d| d.replace(
|
|
r#"{ "id": "household", "name": "Household", "members": ["mark", "jana"] }"#,
|
|
r#"{ "id": "household", "name": "Household", "members": ["mark", "jana"] },
|
|
{ "id": "friends", "name": "Friends", "members": ["jana"] }"#,
|
|
))
|
|
.with_road(
|
|
"d00003-friends-photos.md",
|
|
"---\nid: d00003\nsubject: group:friends\nverb: can-reach\nobject: service:photos.home.arpa\ncreated: 2026-11-25T10:00:00Z\n---\n",
|
|
);
|
|
let c = parts.compile();
|
|
assert_eq!(c.errors().count(), 0, "{:?}", c.problems);
|
|
let rules: Vec<_> = c.plans[&key(2)]
|
|
.firewall
|
|
.rules
|
|
.iter()
|
|
.filter(|r| r.src.to_string() == "100.96.0.3/32" && r.ports == Some([443, 443]))
|
|
.collect();
|
|
assert_eq!(rules.len(), 1, "{rules:?}");
|
|
let roads: Vec<String> = rules[0].roads.iter().map(|r| r.to_string()).collect();
|
|
assert_eq!(roads, ["7tq0mz", "d00003"]);
|
|
assert_eq!(
|
|
ids(&parts, 3, &service("photos.home.arpa")),
|
|
["7tq0mz", "d00003", "k3f9q2"]
|
|
);
|
|
}
|