Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/nornir

src/mimir_lens/room_feed/reach_tests.rs

//! The kept reach walks ([`crate::mimir_lens::reach_inc`]) through the lens
//! doors: `system_boundary`, `data_reach` and `reachable_vuln` over a room
//! that ADVANCED. ZERO-LOSS: every incremental answer equals the REFERENCE
//! (the original whole-graph walks, [`reach_inc::set_reference`]) byte for
//! byte, timing aside — and a work counter proves the advance walked less.

use serde_json::{Value, json};

use super::fixture::{self, Twist, Without, boundary_ws};
use crate::mimir_lens::reach_inc;

/// The whole answer minus the timing (`lens_meta.ms`).
fn sans_ms(v: &Value) -> Value {
    let mut v = v.clone();
    if let Some(meta) = v["lens_meta"].as_object_mut() {
        meta.remove("ms");
    }
    v
}

/// The reference answer to the same question (the original walks, no kept
/// state read or written).
fn reference(ws: &fixture::Workspace, lens: &str, params: Value) -> Value {
    reach_inc::set_reference(true);
    let v = ws.ask(lens, params);
    reach_inc::set_reference(false);
    v
}

fn assert_zero_loss(ws: &fixture::Workspace, lens: &str, params: Value, got: &Value) {
    let want = reference(ws, lens, params);
    assert_eq!(sans_ms(got), sans_ms(&want), "ZERO-LOSS: the kept walk's answer IS the reference answer");
}

/// `(full, carried, advanced)` deltas of `kernel` across `f`.
fn delta<T>(kernel: &str, f: impl FnOnce() -> T) -> (T, (u64, u64, u64)) {
    let (f0, c0, a0) = reach_inc::stats(kernel);
    let out = f();
    let (f1, c1, a1) = reach_inc::stats(kernel);
    (out, (f1 - f0, c1 - c0, a1 - a0))
}

fn class<'a>(classes: &'a Value, name: &str) -> Option<&'a Value> {
    classes.as_array().and_then(|a| a.iter().find(|c| c["class"] == name))
}

/// The first ask walks in full — and that full walk already equals the
/// reference (the kept walk's own from-scratch path is an oracle case too).
#[test]
fn the_kept_boundary_walk_from_scratch_is_the_reference() {
    let _g = fixture::serial();
    reach_inc::forget();
    let ws = boundary_ws::workspace(true);
    for (lens, params) in [("system_boundary", json!({ "repo": boundary_ws::NET })), ("data_reach", json!({})), ("reachable_vuln", json!({}))] {
        reach_inc::forget();
        let got = ws.ask(lens, params.clone());
        assert_zero_loss(&ws, lens, params, &got);
    }
}

/// `sum()` starts calling `fetch()`: ONE function's calls change. The kept
/// walk ADVANCES over the cone (sum and what reaches it) — no whole-graph
/// walk — and the answer is the reference's: sum now reaches the network.
#[test]
fn system_boundary_advances_when_one_function_changes_its_calls() {
    let _g = fixture::serial();
    reach_inc::forget();
    let ws = boundary_ws::workspace(true);
    let p = json!({ "repo": boundary_ws::NET });
    let (before, d) = delta("boundary", || ws.ask("system_boundary", p.clone()));
    assert_eq!(d, (1, 0, 0), "the first ask walks in full");
    let full = reach_inc::last_work("boundary").unwrap();
    assert_eq!(class(&before["classes"], "network").unwrap()["functions"], 2, "{before}");
    boundary_ws::advance(&ws, boundary_ws::NetTwist::SumCallsFetch);
    let (after, d) = delta("boundary", || ws.ask("system_boundary", p.clone()));
    let w = reach_inc::last_work("boundary").unwrap();
    assert_eq!(d, (0, 0, 1), "one function's calls moved: ADVANCED, not the full walk ({w:?})");
    assert!(w.rewalked < w.nodes, "the cone is smaller than the closure: {w:?}");
    assert!(w.graph_walks < full.graph_walks, "fewer whole-graph walks than the full walk: {w:?} vs {full:?}");
    eprintln!("system_boundary on the net fixture: full {full:?} · advance {w:?}");
    assert_eq!(class(&after["classes"], "network").unwrap()["functions"], 3, "sum now reaches connect: {after}");
    assert_zero_loss(&ws, "system_boundary", p, &after);
}

/// `start()` stops calling `fetch()`: a reach SHRINKS. The cone pass
/// re-derives start's classes from its new (empty) calls.
#[test]
fn system_boundary_advances_when_a_call_is_dropped() {
    let _g = fixture::serial();
    reach_inc::forget();
    let ws = boundary_ws::workspace(true);
    let p = json!({ "repo": boundary_ws::NET });
    ws.ask("system_boundary", p.clone());
    boundary_ws::advance(&ws, boundary_ws::NetTwist::StartDropsFetch);
    let (after, d) = delta("boundary", || ws.ask("system_boundary", p.clone()));
    assert_eq!(d, (0, 0, 1), "{:?}", reach_inc::last_work("boundary"));
    assert_eq!(class(&after["classes"], "network").unwrap()["functions"], 1, "start no longer reaches the network: {after}");
    assert_zero_loss(&ws, "system_boundary", p, &after);
}

/// A commit that moves no edge (a span grows): nothing to re-derive — the
/// kept walk is CARRIED.
#[test]
fn system_boundary_carries_over_an_unrelated_commit() {
    let _g = fixture::serial();
    reach_inc::forget();
    let ws = boundary_ws::workspace(true);
    let p = json!({ "repo": boundary_ws::NET });
    ws.ask("system_boundary", p.clone());
    boundary_ws::advance(&ws, boundary_ws::NetTwist::Unrelated);
    let (after, d) = delta("boundary", || ws.ask("system_boundary", p.clone()));
    let w = reach_inc::last_work("boundary").unwrap();
    assert_eq!(d, (0, 1, 0), "nothing the walk reads moved: carried ({w:?})");
    assert_eq!((w.rewalked, w.graph_walks), (0, 0), "{w:?}");
    assert_zero_loss(&ws, "system_boundary", p, &after);
}

/// data_reach's `syscalls` section shares the kept boundary walk.
#[test]
fn data_reach_syscalls_advance_with_the_kept_boundary_walk() {
    let _g = fixture::serial();
    reach_inc::forget();
    let ws = boundary_ws::workspace(true);
    let p = json!({ "class": "network" });
    ws.ask("data_reach", p.clone());
    boundary_ws::advance(&ws, boundary_ws::NetTwist::SumCallsFetch);
    let (after, d) = delta("boundary", || ws.ask("data_reach", p.clone()));
    assert_eq!(d, (0, 0, 1), "{:?}", reach_inc::last_work("boundary"));
    let fns: Vec<&str> = class(&after["syscalls"], "network").unwrap()["from"].as_array().unwrap().iter().map(|r| r["function"].as_str().unwrap()).collect();
    assert!(fns.iter().any(|f| f.ends_with("sum")), "{fns:?}");
    assert_zero_loss(&ws, "data_reach", p, &after);
}

/// reachable_vuln: the vulnerable crate's walk met no local node, so a
/// commit of our code that keeps the entries carries it (only the caller
/// prefix of the path is read again).
#[test]
fn reachable_vuln_carries_the_findings_walk_over_our_commit() {
    let _g = fixture::serial();
    reach_inc::forget();
    let ws = boundary_ws::workspace(true);
    let (_, d) = delta("vuln", || ws.ask("reachable_vuln", json!({})));
    assert_eq!(d, (1, 0, 0));
    boundary_ws::advance(&ws, boundary_ws::NetTwist::SumCallsFetch);
    let (after, d) = delta("vuln", || ws.ask("reachable_vuln", json!({})));
    let w = reach_inc::last_work("vuln").unwrap();
    assert_eq!(d, (0, 1, 0), "the finding's walk is carried: {w:?}");
    assert_eq!(w.rewalked, 0, "{w:?}");
    let row = after["worst"].as_array().unwrap().iter().find(|r| r["crate"] == "hyperish").unwrap();
    assert_eq!(row["boundary"]["classes"], json!(["network"]), "{row}");
    assert_zero_loss(&ws, "reachable_vuln", json!({}), &after);
}

/// A ring write moves the `universe_manifest` (here: the kernel's rows
/// written after the first ask): the kept walk cannot stand in — FULL, with
/// the reason — and the answer is still the reference's.
#[test]
fn a_moved_universe_plans_the_full_walk() {
    let _g = fixture::serial();
    reach_inc::forget();
    let ws = boundary_ws::workspace(true);
    let p = json!({ "repo": boundary_ws::NET });
    ws.ask("system_boundary", p.clone());
    ws.ask("reachable_vuln", json!({}));
    boundary_ws::write_ring_crate(&ws);
    let (after, d) = delta("boundary", || ws.ask("system_boundary", p.clone()));
    let w = reach_inc::last_work("boundary").unwrap();
    assert_eq!(d, (1, 0, 0), "{w:?}");
    assert!(w.path.contains("universe moved"), "the reason is said: {w:?}");
    assert_zero_loss(&ws, "system_boundary", p, &after);
    let (_, d) = delta("vuln", || ws.ask("reachable_vuln", json!({})));
    assert_eq!(d, (1, 0, 0), "{:?}", reach_inc::last_work("vuln"));
}

/// The boundary's targets moved (an `extern "C"` of our own now maps to a
/// call) under an unchanged universe: FULL with the reason.
#[test]
fn moved_targets_plan_the_full_walk() {
    let _g = fixture::serial();
    reach_inc::forget();
    let ws = boundary_ws::workspace(true);
    let p = json!({ "repo": boundary_ws::NET });
    ws.ask("system_boundary", p.clone());
    boundary_ws::advance(&ws, boundary_ws::NetTwist::OwnFfi);
    let (after, d) = delta("boundary", || ws.ask("system_boundary", p.clone()));
    let w = reach_inc::last_work("boundary").unwrap();
    assert_eq!(d, (1, 0, 0), "{w:?}");
    assert!(w.path.contains("targets moved"), "{w:?}");
    assert_zero_loss(&ws, "system_boundary", p, &after);
}

/// data_reach's table reachers on the shop fixture: a new fn that calls
/// `cart::open()` (it reads `orders`) — only the touchers a changed node can
/// reach are walked again; then a dropped call shrinks a reacher set.
#[test]
fn data_reach_tables_advance_only_the_touchers_a_change_reaches() {
    let _g = fixture::serial();
    reach_inc::forget();
    let ws = fixture::graph_only(Without::default());
    let p = json!({});
    let (before, d) = delta("tables", || ws.ask("data_reach", p.clone()));
    assert_eq!(d, (1, 0, 0));
    let touchers = reach_inc::last_work("tables").unwrap().nodes;
    assert!(touchers >= 4, "{before}");
    fixture::advance_shop(&ws, Twist::NewTableReacher);
    let (after, d) = delta("tables", || ws.ask("data_reach", p.clone()));
    let w = reach_inc::last_work("tables").unwrap();
    assert_eq!(d, (0, 0, 1), "{w:?}");
    assert!(w.rewalked < w.nodes, "only the touchers the change reaches: {w:?}");
    assert!(after.to_string().contains("shop::pay::hello2"), "hello2 now reaches orders: {after}");
    assert_zero_loss(&ws, "data_reach", p.clone(), &after);
    fixture::advance_shop(&ws, Twist::CallDropped);
    let (after, d) = delta("tables", || ws.ask("data_reach", p.clone()));
    assert_eq!(d, (0, 0, 1), "{:?}", reach_inc::last_work("tables"));
    assert_zero_loss(&ws, "data_reach", p, &after);
}