//! Mandatory test 2: deleting a road, expiring it, or removing a device from a person removes the access //! from the compiled plan — on the receiving side (the firewall of the device that serves the place) and on //! the sending side (the peer and its allowed IPs). mod common; use common::{Parts, at, fw_accepts, key}; use roads::{Hostname, Place, compile, explain}; use std::net::IpAddr; const PHOTOS_HOST: &str = "100.96.0.2"; fn home_server() -> IpAddr { PHOTOS_HOST.parse().unwrap() } fn photos() -> Place { Place::Service(Hostname::new("photos.home.arpa").unwrap()) } /// jana-phone → home-server on tcp/443, judged on the receiving side. fn jana_reaches_photos(c: &roads::Compiled) -> bool { fw_accepts(c, &key(3), home_server(), Some(("tcp", 443))) } fn has_peer(c: &roads::Compiled, on: u8, peer: u8) -> bool { c.plans[&key(on)].peers.iter().any(|p| p.key == key(peer)) } #[test] fn the_baseline_grants_it() { let c = Parts::home().compile(); assert!(jana_reaches_photos(&c)); assert!(has_peer(&c, 3, 2) && has_peer(&c, 2, 3)); } #[test] fn deleting_the_roads_removes_the_access() { let c = Parts::home() .without_road("7tq0mz") .without_road("k3f9q2") .compile(); assert!(!jana_reaches_photos(&c)); assert!( !has_peer(&c, 3, 2), "jana-phone kept a peer for the home server" ); assert!( !has_peer(&c, 2, 3), "the home server kept a peer for jana-phone" ); assert!( c.plans[&key(2)] .firewall .rules .iter() .all(|r| r.src.to_string() != "100.96.0.3/32") ); assert!(c.plans[&key(3)].dns.is_empty()); } #[test] fn deleting_one_of_two_roads_leaves_the_other_visible() { // QA R-46: revoking the person-level road leaves the group road, and explain says so. let parts = Parts::home().without_road("7tq0mz"); let c = parts.compile(); assert!(jana_reaches_photos(&c)); let devs = parts.store().devices.unwrap(); let why: Vec = explain(&c, &devs, &key(3), &photos()) .iter() .map(|r| r.to_string()) .collect(); assert_eq!(why, ["k3f9q2"]); } #[test] fn an_expired_road_grants_nothing_from_the_instant_of_until() { // until is 2026-11-29T23:59:59+01:00 and exclusive (QA R-22). Without the household road only it grants. let parts = Parts::home().without_road("k3f9q2"); let store = parts.store(); assert!(jana_reaches_photos(&compile( &store, at("2026-11-29T23:59:58+01:00") ))); for now in [ "2026-11-29T23:59:59+01:00", "2026-11-29T22:59:59Z", "2026-11-30T00:00:00+01:00", "2027-11-25T12:00:00Z", ] { let c = compile(&store, at(now)); assert!(!jana_reaches_photos(&c), "still granted at {now}"); assert!( !has_peer(&c, 2, 3), "the home server kept jana-phone at {now}" ); assert!( c.problems .iter() .any(|p| p.code == roads::rules::ROAD_EXPIRED && p.path.contains("7tq0mz")), "{now}" ); } } #[test] fn each_plan_carries_its_own_expiry_on_both_sides() { // QA R-26: a road with an until stops on time even before the next compile, if the engine honours // `expires`. Both the subject's and the serving device's plans carry it. let c = Parts::home().compile(); for dev in [2, 3, 4] { assert_eq!( c.plans[&key(dev)].expires.as_deref(), Some("2026-11-29T22:59:59Z"), "device {dev}" ); } assert_eq!(c.plans[&key(1)].expires, None, "mark-pc has no timed road"); assert_eq!( c.plans[&key(7)].expires, None, "the expired road no longer counts" ); } #[test] fn moving_a_device_to_another_person_moves_its_access() { // QA R-18: the receiving side's rules follow the device's owner in this snapshot, nothing cached. let moved = Parts::home().edit_devices(|d| { d.replace( r#""name": "jana-phone", "owner": "jana""#, r#""name": "jana-phone", "owner": "petr""#, ) }); let c = moved.compile(); assert!(!jana_reaches_photos(&c), "the phone kept Jana's access"); assert!(!has_peer(&c, 2, 3)); // It gains Petr's road: exits through the friend node. assert_eq!( c.plans[&key(3)].exit.as_ref().map(|e| e.through.clone()), Some(key(6)) ); // Jana's other device keeps hers. assert!(fw_accepts(&c, &key(4), home_server(), Some(("tcp", 443)))); } #[test] fn removing_a_device_removes_it_from_every_plan() { // QA P-6: no plan but its own (which no longer exists) names its key or its address. let removed = Parts::home().edit_devices(|d| { d.lines() .filter(|l| !l.contains("\"jana-phone\"")) .collect::>() .join("\n") }); let c = removed.compile(); assert!(c.errors().next().is_none(), "{:?}", c.problems); assert!(!c.plans.contains_key(&key(3))); for p in c.plans.values() { let json = p.to_json(); assert!( !json.contains(key(3).as_str()) && !json.contains("100.96.0.3"), "{} still names jana-phone", p.name ); } } #[test] fn removing_a_person_from_a_group_removes_the_group_road_only() { let parts = Parts::home() .edit_directory(|d| d.replace(r#""members": ["mark", "jana"]"#, r#""members": ["mark"]"#)); let c = parts.compile(); // The group road no longer covers Jana: no rule from her address for all traffic. assert!( !fw_accepts(&c, &key(3), home_server(), Some(("tcp", 22))), "group access remained" ); // Her own road to the photos still holds. assert!(jana_reaches_photos(&c)); } #[test] fn a_person_deleted_from_the_directory_leaves_their_roads_reported_and_void() { // QA R-20: the road naming petr is an error, and the device that petr owned fails the snapshot. let parts = Parts::home() .edit_directory(|d| d.replace(r#" { "id": "petr", "name": "Petr" },"#, "")) .edit_devices(|d| d.replace(r#""owner": "petr", "#, "")); let c = parts.compile(); assert!( c.problems .iter() .any(|p| p.code == roads::rules::SUBJECT_UNKNOWN && p.path.contains("c0z5wn")) ); assert!(c.plans[&key(7)].exit.is_none(), "petr-phone kept its exit"); assert!( c.plans[&key(6)].peers.is_empty(), "the friend node kept petr-phone as a peer" ); }