Files
via-core/crates/roads/tests/explain.rs
mnovak 69e3a9377d [roads] QA F-01: a service road grants only that service
A firewall sees an address and a port, never a hostname, so a road to
photos.home.arpa compiled to home-server:443 and also opened git.home.arpa
there. Rules are now per service:

- devices.json services may carry their own mesh address (same rules as a
  device address, unique among all of them). A road to such a service compiles
  to that address only; the hosting device's plan lists it in the new field
  services[] so it answers on it, and DNS points there.
- A road to a service that shares its address and a port with another service
  is refused (service.shared), naming the others, instead of opening both.
  Narrowing ports: to a port nobody else serves on that address is allowed.
- A device: road reaches the whole device: its own address and its services'
  own addresses.

No Ianitor identity check is needed for this. The home fixture gives photos
its own address (100.96.1.2); tests that encoded the old host:port model are
updated, three new snapshot fixtures cover the address rules, and the golden
plans and pinned digest are regenerated. Mutation-checked: dropping the
refusal, or ignoring the service address, each fails an F-01 test.
2026-11-25 09:01:18 +01:00

149 lines
5.4 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))),
["k3f9q2"],
"photos has its own address (QA F-01): its road reaches that, not the device's own address"
);
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"]
);
}