Files
via-core/crates/roads/tests/qa_findings.rs
T
mnovak b482e264d2 [roads] QA B-3: an app road names whose devices it is for, and reaches only those
subject: app:<id> applied to every device in the mesh, so whoever could write
one road (app:steam goes-direct 0.0.0.0/0) changed the tunnel of other
people's devices, an exit and a classic router. An app road now needs the new
header key on: (a person, group or device), and reaches only those devices;
on: is forbidden on any other subject. The home fixture's steam road is on
Mark's devices, so it leaves Jana's, Petr's and the router's plans. Four
fixtures, a B-3 test (fails if app roads reach every device again), the
property generator scopes app roads to a person, and the R-55 renaming
rewrites on: like subject:.
2026-11-25 09:04:50 +01:00

393 lines
15 KiB
Rust

//! QA review of PR #1 (`feature/roads`): one test per finding that the code does not yet answer.
//!
//! Every test here **fails on the current code** and is `#[ignore]`d so the suite stays green; each `ignore`
//! names its finding in `Via/qa/review-via-core-pr1.md`. Run them with `cargo test -p roads --test qa_findings
//! -- --ignored`: a fix is done when its test passes and the `#[ignore]` is removed in the same commit. If a
//! finding is answered by a design decision instead, change the test to that decision, do not delete it.
mod common;
use common::{Parts, key};
use roads::plan::Dst;
use roads::{Hostname, Place, Store, compile, explain};
fn road(id: &str, slug: &str, lines: &[String]) -> (String, String) {
(
format!("{id}-{slug}.md"),
format!(
"---\nid: {id}\n{}\ncreated: 2026-11-25T10:00:00Z\n---\n",
lines.join("\n")
),
)
}
fn with(parts: Parts, r: (String, String)) -> Parts {
parts.with_road(&r.0, &r.1)
}
/// F-01. A road to one Ianitor service opens every service on that host that shares a port. The firewall can
/// only say "Jana's address may reach 100.96.0.2 on tcp/443", and photos and git both live there on tcp/443,
/// so "Jana can reach Photos" is also "Jana can reach Git"; `explain` admits it.
#[test]
fn f01_a_service_road_grants_only_that_service() {
// Without the household road, Jana's only road to the home server is 7tq0mz: photos, tcp/443.
let parts = Parts::home().without_road("k3f9q2");
let c = parts.compile();
let store = parts.store();
let devs = store.devices.as_ref().unwrap();
let git = Place::Service(Hostname::new("git.home.arpa").unwrap());
let why = explain(&c, devs, &key(3), &git);
let refused = c
.problems
.iter()
.any(|p| p.path.contains("7tq0mz") && p.is_error());
assert!(
why.is_empty() || refused,
"jana-phone reaches git.home.arpa through {why:?}, a road that names only photos.home.arpa"
);
}
/// F-01, the other half: without its own address, photos shares home-server:443 with git, and the road to
/// photos is refused, naming git, instead of opening both. A `ports:` that avoids the shared port is allowed.
#[test]
fn f01_a_service_that_shares_its_address_and_port_is_refused() {
let parts = Parts::home().edit_devices(|d| d.replace(r#", "address": "100.96.1.2""#, ""));
let c = parts.compile();
let p = c
.problems
.iter()
.find(|p| p.path.contains("7tq0mz") && p.code == roads::rules::SERVICE_SHARED);
assert!(
p.is_some_and(|p| p.is_error() && p.message.contains("git.home.arpa")),
"{:?}",
c.problems
);
assert!(
!c.plans[&key(3)]
.dns
.iter()
.any(|d| d.host.as_str() == "photos.home.arpa"),
"the refused road still granted"
);
// Git alone on tcp/22 shares nothing: a road narrowed to it compiles.
let narrowed = Parts::home()
.edit_devices(|d| d.replace(r#", "address": "100.96.1.2""#, ""))
.with_road(
"f01aaa-jana-git-ssh.md",
"---\nid: f01aaa\nsubject: person:jana\nverb: can-reach\nobject: service:git.home.arpa\nports: [tcp/22]\ncreated: 2026-11-25T10:00:00Z\n---\n",
)
.compile();
assert!(
!narrowed.problems.iter().any(|p| p.path.contains("f01aaa")),
"{:?}",
narrowed.problems
);
}
/// F-01: with its own address, photos is granted by itself: the rule's destination is that address, the
/// home server's plan answers on it, and Jana's DNS points there.
#[test]
fn f01_a_service_with_its_own_address_is_granted_alone() {
let c = Parts::home().without_road("k3f9q2").compile();
let server = &c.plans[&key(2)];
assert!(server.services.iter().any(|s| s.host.as_str() == "photos.home.arpa" && s.address.to_string() == "100.96.1.2"));
let to_photos: Vec<_> = server
.firewall
.rules
.iter()
.filter(|r| r.roads.iter().any(|id| id.as_str() == "7tq0mz"))
.collect();
assert!(!to_photos.is_empty());
assert!(
to_photos
.iter()
.all(|r| matches!(r.dst, Dst::Net(n) if n.to_string() == "100.96.1.2/32")),
"{to_photos:?}"
);
let phone = &c.plans[&key(3)];
assert!(phone.dns.iter().any(|d| d.host.as_str() == "photos.home.arpa" && d.address.to_string() == "100.96.1.2"));
}
/// F-02. `goes-direct` may name the mesh's own addresses or a subnet the mesh routes. Then traffic that the
/// plan also routes into the tunnel is told to bypass it, on whatever network the device is on, and the
/// kill switch does not stop it because the subnet is "private".
#[test]
fn f02_going_direct_never_overlaps_what_the_tunnel_carries() {
let mark_pc = key(1);
for subnet in ["100.96.0.0/16", "192.168.100.0/24", "192.168.0.0/16"] {
let parts = with(
Parts::home(),
road(
"f02aaa",
"mark-pc-direct",
&[
format!("subject: device:{mark_pc}"),
"verb: goes-direct".into(),
format!("object: subnet:{subnet}"),
],
),
);
let c = parts.compile();
let plan = &c.plans[&mark_pc];
assert!(plan.kill_switch.is_some(), "mark-pc has a kill switch");
let tunnelled: Vec<_> = plan
.peers
.iter()
.flat_map(|p| p.allowed_ips.iter())
.filter(|n| n.prefix_len() > 0)
.collect();
// Decision (owner session): a direct subnet that holds tunnelled nets keeps them in the tunnel, listed
// in `except`; one inside a tunnelled net is dropped; one holding a mesh address is refused. So every
// overlap must be covered by `except`.
for d in &plan.direct {
for n in &tunnelled {
assert!(
!d.subnet.overlaps(n) || d.except.iter().any(|e| e.contains(n)),
"goes-direct {} overlaps {n}, which the plan sends through the tunnel",
d.subnet
);
}
}
let refused = c
.problems
.iter()
.any(|p| p.path.contains("f02aaa") && p.code == roads::rules::DIRECT_MESH);
assert_eq!(
refused,
subnet == "100.96.0.0/16",
"{subnet}: {:?}",
c.problems
);
}
}
/// F-03. A kill switch whose road stops validating turns the protection off. The realistic trigger is not a
/// typo: the exit device stops offering itself as an exit (or leaves devices.json), and every kill switch
/// through it silently disappears while the rest of the plan compiles.
#[test]
fn f03_a_kill_switch_whose_exit_goes_away_fails_closed() {
let parts = Parts::home().edit_devices(|d| {
d.replace(
r#""address": "100.96.0.2", "exit": true,"#,
r#""address": "100.96.0.2","#,
)
});
let c = parts.compile();
assert!(c.has_plans());
assert!(
c.problems
.iter()
.any(|p| p.path.contains("w9c3ke") && p.is_error()),
"the kill-switch road is reported"
);
let ks = c.plans[&key(1)].kill_switch.as_ref();
assert!(
ks.is_some(),
"mark-pc lost its kill switch because its exit stopped being one: it now fails open"
);
assert_eq!(
ks.unwrap().through,
None,
"closed means through no exit: only the mesh"
);
// Mutation guard: the same road made valid again keeps its exit.
assert_eq!(
Parts::home().compile().plans[&key(1)]
.kill_switch
.as_ref()
.unwrap()
.through,
Some(key(2))
);
}
/// F-03, the other references: an unknown exit, and an exit road that has expired, which ends as written.
#[test]
fn f03_an_unknown_exit_fails_closed_and_an_expired_kill_switch_ends() {
let text = Parts::home().road_text("w9c3ke");
let unknown = text.replace(
"exit:AgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgI",
"exit:CQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQk",
);
let c = Parts::home().replace_road("w9c3ke", &unknown).compile();
assert!(
c.problems
.iter()
.any(|p| p.path.contains("w9c3ke") && p.is_error())
);
assert_eq!(c.plans[&key(1)].kill_switch.as_ref().unwrap().through, None);
let expired = unknown.replace("created:", "until: 2026-11-01T00:00:00+01:00\ncreated:");
let c = Parts::home().replace_road("w9c3ke", &expired).compile();
assert!(
c.plans[&key(1)].kill_switch.is_none(),
"an expired kill switch ends as written"
);
}
/// F-04. Deleting a person from the directory (the upstream event, without also editing devices.json) leaves
/// a dangling `owner`, which is a snapshot error, which means no plan for anybody. Day 3 then has to choose
/// between the household offline and the deleted person keeping their access.
#[test]
fn f04_deleting_a_person_revokes_them_and_nobody_else() {
let parts =
Parts::home().edit_directory(|d| d.replace(r#" { "id": "petr", "name": "Petr" },"#, ""));
let c = parts.compile();
assert!(
c.has_plans(),
"removing petr from directory.json produced no plans at all: {:?}",
c.errors().map(|p| p.to_string()).collect::<Vec<_>>()
);
assert!(c.plans[&key(7)].exit.is_none(), "petr-phone kept its exit");
assert!(
c.plans[&key(3)].peers.iter().any(|p| p.key == key(2)),
"jana kept hers"
);
}
/// F-05. A custom road on a classic device is carried as `classic: false` unless the road says otherwise, so the
/// plan labels a classic peer's raw block as post-quantum (CLAUDE.md: classic peers are labelled everywhere).
#[test]
fn f05_a_custom_block_on_a_classic_device_is_labelled_classic() {
let router = key(5);
let text = Parts::home()
.road_text("x1d7qa")
.replace("classic: true\n", "");
let parts = Parts::home().replace_road("x1d7qa", &text);
let c = parts.compile();
let refused = c
.problems
.iter()
.any(|p| p.path.contains("x1d7qa") && p.is_error());
let custom = &c.plans[&router].custom;
assert!(
refused || custom.iter().all(|x| x.classic),
"the router is classic, its custom block says classic: false"
);
}
/// F-06. Roads whose subject devices have an IPv4 mesh address and whose object is an IPv6 subnet (or the
/// other way round) compile cleanly into rules that can never match: a road that grants nothing, reported as
/// nothing.
#[test]
fn f06_a_road_across_address_families_is_reported() {
let parts = with(
Parts::home().edit_devices(|d| {
d.replace(
r#""routes": ["192.168.100.0/24"]"#,
r#""routes": ["192.168.100.0/24", "fd00:1::/64"]"#,
)
}),
road(
"f06aaa",
"mark-v6",
&[
"subject: person:mark".into(),
"verb: can-reach".into(),
"object: subnet:fd00:1::/64".into(),
],
),
);
let c = parts.compile();
let mixed = c
.plans
.values()
.flat_map(|p| p.firewall.rules.iter())
.any(|r| match r.dst {
Dst::Net(n) => n.inner().addr().is_ipv4() != r.src.inner().addr().is_ipv4(),
Dst::Internet => false,
});
let reported = c.problems.iter().any(|p| p.path.contains("f06aaa"));
assert!(
reported || !mixed,
"an IPv4 source to an IPv6 destination, silently"
);
}
/// F-07. A store folder without `roads/` compiles to plans with no access and no problem at all, so pointing
/// `--store` one folder too high revokes everything, kill switches included, without a word.
#[test]
fn f07_a_missing_roads_folder_is_reported() {
let dir = tempfile::tempdir().unwrap();
std::fs::copy(
common::home_dir().join("directory.json"),
dir.path().join("directory.json"),
)
.unwrap();
std::fs::copy(
common::home_dir().join("devices.json"),
dir.path().join("devices.json"),
)
.unwrap();
let store = Store::load_dir(dir.path()).unwrap();
let c = compile(&store, common::now());
assert!(
!c.problems.is_empty(),
"no roads/ folder, every plan empty, and nothing said"
);
}
/// F-08. A device's mesh address may be a public address. The exit's forward rule to `internet` then covers
/// another device's mesh address, so "exit through the friend node" also reaches that device through the exit.
#[test]
fn f08_mesh_addresses_are_never_internet_addresses() {
let parts = Parts::home()
.edit_devices(|d| d.replace(r#""address": "100.96.0.4""#, r#""address": "198.51.100.4""#));
let store = parts.store();
assert!(
store.devices.is_none(),
"198.51.100.4 was accepted as a mesh address; an exit's `internet` rule covers it"
);
}
/// F-09. An empty value means "absent" (docs/FORMATS.md), and for the two conditions that narrow a road absent
/// is the widest value: `until:` left empty is a permanent grant and `ports:` left empty is all traffic, with no
/// problem reported. The same failure as QA R-35 (a misspelt `until`), reached by deleting the value instead.
#[test]
fn f09_an_emptied_narrowing_condition_is_an_error() {
for (from, to) in [
("until: 2026-11-29T23:59:59+01:00\n", "until:\n"),
("ports: [tcp/443]\n", "ports:\n"),
("until: 2026-11-29T23:59:59+01:00\n", "until: # Sunday\n"),
] {
let text = Parts::home().road_text("7tq0mz").replace(from, to);
assert!(text.contains(to), "fixture edit {to:?}");
let c = Parts::home().replace_road("7tq0mz", &text).compile();
assert!(
c.problems.iter().any(|p| p.path.contains("7tq0mz")),
"`{}` compiled as {} with no problem reported",
to.trim_end(),
if to.starts_with("until") {
"a permanent grant"
} else {
"all traffic"
}
);
}
}
/// B-3. An `app:` road used to reach every device in the mesh, other people's included: `app:steam goes-direct
/// 0.0.0.0/0` changed the tunnel of Jana's phone, Petr's friend node and a classic router. An app road now names
/// whose devices it is for (`on:`), and reaches only those.
#[test]
fn b3_an_app_road_reaches_only_the_devices_it_is_on() {
let c = Parts::home().compile();
let steam = |k: u8| c.plans[&key(k)].direct.iter().any(|d| d.app.is_some());
assert!(steam(2), "mark's home server runs the road");
for (k, who) in [
(3, "jana-phone"),
(4, "jana-laptop"),
(5, "router"),
(6, "friend-node"),
(7, "petr-phone"),
] {
assert!(!steam(k), "{who} is not mark's, and its tunnel changed");
}
// The same road for Jana moves to Jana's devices.
let text = Parts::home()
.road_text("e5yj1s")
.replace("on: person:mark", "on: person:jana");
let c = Parts::home().replace_road("e5yj1s", &text).compile();
let steam = |k: u8| c.plans[&key(k)].direct.iter().any(|d| d.app.is_some());
assert!(steam(3) && steam(4) && !steam(2) && !steam(6));
}