use std::process::Command;
use serde_json::Value;
fn spock() -> Command {
Command::new(env!("CARGO_BIN_EXE_spock"))
}
fn has_z3() -> bool {
Command::new("z3").arg("--version").output().is_ok_and(|o| o.status.success())
}
fn write_spec(dir: &tempfile::TempDir, name: &str, body: &str) -> String {
let path = dir.path().join(name);
std::fs::write(&path, body).unwrap();
path.to_str().unwrap().to_owned()
}
#[test]
fn prove_succeeds_and_emits_json() {
if !has_z3() {
return;
}
let dir = tempfile::tempdir().unwrap();
let spec = write_spec(
&dir,
"counter.spock",
r#"
struct State { x: Int }
impl State {
action init() -> State { State { x: 0 } }
action inc(self) -> State { Self { ..self, x: self.x + 1 } }
fn nonneg(self) -> Bool { self.x >= 0 }
}
model m {
init = State::init;
next = State::inc;
invariants = [State::nonneg];
}
"#,
);
let out = spock().args(["prove", &spec]).output().unwrap();
assert_eq!(out.status.code(), Some(0), "stderr: {}", String::from_utf8_lossy(&out.stderr));
let stdout = String::from_utf8(out.stdout).unwrap();
assert!(stdout.contains("m: proved "), "stdout: {stdout}");
assert!(stdout.contains('🖖'), "stdout: {stdout}");
let out = spock().args(["prove", &spec, "--json"]).output().unwrap();
assert_eq!(out.status.code(), Some(0));
let v: Value = serde_json::from_slice(&out.stdout).unwrap();
assert_eq!(v["outcome"]["kind"], "proved");
assert_eq!(v["failed"], 0);
assert!(v["proved"].as_u64().unwrap() > 0);
}
#[test]
fn prove_reports_cti_and_quote_on_failure() {
if !has_z3() {
return;
}
let dir = tempfile::tempdir().unwrap();
let spec = write_spec(
&dir,
"bad.spock",
r#"
struct State { x: Int, y: Int }
impl State {
action init() -> State { State { x: 0, y: 0 } }
action step(self) -> State { Self { ..self, x: self.y } }
fn x_nonneg(self) -> Bool { self.x >= 0 }
}
model m {
init = State::init;
next = State::step;
invariants = [State::x_nonneg];
}
"#,
);
let out = spock().args(["prove", &spec]).output().unwrap();
assert_eq!(out.status.code(), Some(12));
let stdout = String::from_utf8(out.stdout).unwrap();
assert!(stdout.contains("doesn't preserve State::x_nonneg"), "stdout: {stdout}");
assert!(stdout.contains("counterexample to induction:"), "stdout: {stdout}");
assert!(stdout.contains(" pre-state:\n"), "stdout: {stdout}");
assert!(stdout.contains(" step: State::step\n"), "stdout: {stdout}");
assert!(!stdout.contains("1 State::init"), "CTI pre-state is not initial:\n{stdout}");
assert!(stdout.contains("— Spock"), "stdout: {stdout}");
let out = spock().args(["prove", &spec, "--json"]).output().unwrap();
assert_eq!(out.status.code(), Some(12));
let v: Value = serde_json::from_slice(&out.stdout).unwrap();
assert_eq!(v["outcome"]["kind"], "failed");
assert!(!v["failures"].as_array().unwrap().is_empty());
assert!(v["failures"][0]["pre"].is_object());
assert!(v["failures"][0]["post"].is_object());
}
#[test]
fn check_symbolic_finds_bug_and_passes_safe_model() {
if !has_z3() {
return;
}
let dir = tempfile::tempdir().unwrap();
let bad = write_spec(
&dir,
"bad_sym.spock",
r#"
struct State { x: Int }
impl State {
action init() -> State { State { x: 0 } }
action inc(self) -> State { Self { ..self, x: self.x + 1 } }
fn below_three(self) -> Bool { self.x < 3 }
}
model m {
init = State::init;
next = State::inc;
invariants = [State::below_three];
}
"#,
);
let out = spock().args(["check", &bad, "--symbolic", "--depth", "5"]).output().unwrap();
assert_eq!(out.status.code(), Some(12));
let stdout = String::from_utf8(out.stdout).unwrap();
assert!(stdout.contains("invariant State::below_three is violated"), "stdout: {stdout}");
assert!(stdout.contains("4 states"), "stdout: {stdout}");
assert!(stdout.contains("— Spock"), "stdout: {stdout}");
let out =
spock().args(["check", &bad, "--symbolic", "--depth", "5", "--json"]).output().unwrap();
assert_eq!(out.status.code(), Some(12));
let v: Value = serde_json::from_slice(&out.stdout).unwrap();
assert_eq!(v["mode"], "symbolic");
assert_eq!(v["outcome"]["kind"], "invariant");
assert_eq!(v["trace"].as_array().unwrap().len(), 4);
let out = spock().args(["check", &bad, "--symbolic", "--depth", "2"]).output().unwrap();
assert_eq!(out.status.code(), Some(0));
let stdout = String::from_utf8(out.stdout).unwrap();
assert!(stdout.contains("ok, symbolic up to depth 2"), "stdout: {stdout}");
assert!(stdout.contains('🖖'), "stdout: {stdout}");
}
#[test]
fn prove_handles_unbound_abstract_types_and_consts() {
if !has_z3() {
return;
}
let dir = tempfile::tempdir().unwrap();
let spec = write_spec(
&dir,
"tcommit_unbounded.spock",
r#"
type RM;
const N: Int;
type RmState = | Working | Prepared | Committed | Aborted
struct State { rm: Map<RM, RmState>, count: Int }
fn n_pos() -> Bool { N >= 1 }
impl State {
action init() -> State {
State { rm: { r -> Working for r: RM }, count: 0 }
}
action prepare(self, r: RM) -> State {
require self.rm[r] == Working;
Self { ..self, rm: self.rm.update(r, Prepared), count: self.count + 1 }
}
action commit(self, r: RM) -> State {
require self.rm[r] == Prepared;
require all other: RM { self.rm[other] == Prepared || self.rm[other] == Committed };
Self { ..self, rm: self.rm.update(r, Committed) }
}
action abort(self, r: RM) -> State {
require self.rm[r] == Working || self.rm[r] == Prepared;
require all other: RM { self.rm[other] != Committed };
Self { ..self, rm: self.rm.update(r, Aborted) }
}
action next(self) -> State {
any r: RM {
any {
self.prepare(r),
self.commit(r),
self.abort(r),
}
}
}
fn consistent(self) -> Bool {
all r1: RM, r2: RM { !(self.rm[r1] == Aborted && self.rm[r2] == Committed) }
}
fn ind_inv(self) -> Bool {
self.consistent()
&& self.count >= 0
&& (all r: RM { self.rm[r] == Committed ==> all o: RM { self.rm[o] != Working } })
}
}
model unbounded {
init = State::init;
next = State::next;
assumptions = [n_pos];
invariants = [State::consistent];
inductive = [State::ind_inv];
}
"#,
);
let out = spock().args(["prove", &spec, "--model", "unbounded"]).output().unwrap();
assert_eq!(
out.status.code(),
Some(0),
"stdout:\n{}\nstderr:\n{}",
String::from_utf8_lossy(&out.stdout),
String::from_utf8_lossy(&out.stderr)
);
let stdout = String::from_utf8(out.stdout).unwrap();
assert!(stdout.contains("unbounded: proved "), "stdout: {stdout}");
assert!(stdout.contains('🖖'), "stdout: {stdout}");
let chk = spock().args(["check", &spec, "--model", "unbounded"]).output().unwrap();
assert_eq!(chk.status.code(), Some(150));
let stderr = String::from_utf8(chk.stderr).unwrap();
assert!(stderr.contains("spock prove"), "stderr: {stderr}");
}
#[test]
fn prove_and_symbolic_report_unchecked_deadlock_and_liveness() {
if !has_z3() {
return;
}
let dir = tempfile::tempdir().unwrap();
let spec = write_spec(
&dir,
"live.spock",
r#"
struct State { x: Int }
impl State {
action init() -> State { State { x: 0 } }
action next(self) -> State { Self { ..self, x: self.x + 1 } }
fn nonneg(self) -> Bool { self.x >= 0 }
fn reaches_five() -> Temporal { eventually(|s: State| s.x == 5) }
}
model m {
init = State::init;
next = State::next;
invariants = [State::nonneg];
properties = [State::reaches_five];
}
"#,
);
let out = spock().args(["prove", &spec]).output().unwrap();
assert_eq!(out.status.code(), Some(0));
let stdout = String::from_utf8(out.stdout).unwrap();
assert!(
stdout.contains("warning: not checked by `spock prove`: deadlock, State::reaches_five"),
"stdout: {stdout}"
);
let out = spock().args(["prove", &spec, "--json"]).output().unwrap();
assert_eq!(out.status.code(), Some(0));
let v: Value = serde_json::from_slice(&out.stdout).unwrap();
assert_eq!(v["unchecked"], serde_json::json!(["deadlock", "State::reaches_five"]));
let out =
spock().args(["check", &spec, "--symbolic", "--depth", "2", "--json"]).output().unwrap();
assert_eq!(out.status.code(), Some(0));
let v: Value = serde_json::from_slice(&out.stdout).unwrap();
assert_eq!(v["unchecked"], serde_json::json!(["deadlock", "State::reaches_five"]));
}
#[test]
fn prove_conjunct_failure_shows_source_span_and_liveness_only_says_nothing_to_prove() {
if !has_z3() {
return;
}
let dir = tempfile::tempdir().unwrap();
let split = write_spec(
&dir,
"split.spock",
r#"
struct State { x: Int, y: Int }
impl State {
action init() -> State { State { x: 0, y: 0 } }
action step(self) -> State { Self { ..self, x: self.y } }
fn both(self) -> Bool { self.x >= 0 && self.y <= 10 }
}
model m {
init = State::init;
next = State::step;
invariants = [State::both];
deadlock = false;
}
"#,
);
let out = spock().args(["prove", &split]).output().unwrap();
assert_eq!(out.status.code(), Some(12));
let stdout = String::from_utf8(out.stdout).unwrap();
assert!(stdout.contains("--> "), "expected source span for split conjunct:\n{stdout}");
assert!(stdout.contains("fn both"), "expected span on `fn both`:\n{stdout}");
let liveness_only = write_spec(
&dir,
"live_only.spock",
r#"
struct State { x: Int }
impl State {
action init() -> State { State { x: 0 } }
action step(self) -> State { Self { ..self, x: self.x + 1 } }
fn reaches_five() -> Temporal { eventually(|s: State| s.x == 5) }
}
model m {
init = State::init;
next = State::step;
properties = [State::reaches_five];
deadlock = false;
}
"#,
);
let out = spock().args(["prove", &liveness_only]).output().unwrap();
assert_eq!(out.status.code(), Some(0));
let stdout = String::from_utf8(out.stdout).unwrap();
assert!(stdout.contains("nothing to prove (0 obligations"), "stdout:\n{stdout}");
assert!(!stdout.contains("proved 0/0"), "stdout:\n{stdout}");
assert!(
stdout.contains("warning: not checked by `spock prove`: State::reaches_five"),
"stdout:\n{stdout}"
);
}
#[test]
fn prove_exits_10_on_contradictory_assumptions() {
if !has_z3() {
return;
}
let dir = tempfile::tempdir().unwrap();
let vacuous = write_spec(
&dir,
"vacuous.spock",
r#"
const N: Int;
fn bad_a() -> Bool { N > 5 }
fn bad_b() -> Bool { N < 0 }
struct State { x: Int }
impl State {
action init() -> State { State { x: 0 } }
action next(self) -> State { self }
fn ok(self) -> Bool { self.x >= 0 }
}
model m {
init = State::init;
next = State::next;
assumptions = [bad_a, bad_b];
invariants = [State::ok];
deadlock = false;
}
"#,
);
let out = spock().args(["prove", &vacuous]).output().unwrap();
assert_eq!(
out.status.code(),
Some(10),
"stdout:\n{}\nstderr:\n{}",
String::from_utf8_lossy(&out.stdout),
String::from_utf8_lossy(&out.stderr)
);
}
#[test]
fn prove_triages_reachable_and_unreachable_ctis() {
if !has_z3() {
return;
}
let dir = tempfile::tempdir().unwrap();
let weak = write_spec(
&dir,
"weak.spock",
r#"
struct State { x: Int, y: Int }
impl State {
action init() -> State { State { x: 0, y: 0 } }
action step(self) -> State { Self { ..self, x: self.y } }
fn x_nonneg(self) -> Bool { self.x >= 0 }
}
model m {
init = State::init;
next = State::step;
invariants = [State::x_nonneg];
deadlock = false;
}
"#,
);
let out = spock().args(["prove", &weak]).output().unwrap();
assert_eq!(out.status.code(), Some(12));
let stdout = String::from_utf8(out.stdout).unwrap();
assert!(
stdout.contains("triage: pre-state is unreachable within 6 steps (strengthen `inductive`)"),
"stdout:\n{stdout}"
);
let out_json = spock().args(["prove", &weak, "--json"]).output().unwrap();
assert_eq!(out_json.status.code(), Some(12));
let v: Value = serde_json::from_slice(&out_json.stdout).unwrap();
assert_eq!(v["failures"][0]["reachability"]["kind"], "unreachable");
assert_eq!(v["failures"][0]["reachability"]["depth"], 6);
let out_no_triage =
spock().args(["prove", &weak, "--triage-depth", "0", "--json"]).output().unwrap();
assert_eq!(out_no_triage.status.code(), Some(12));
let v_off: Value = serde_json::from_slice(&out_no_triage.stdout).unwrap();
assert!(v_off["failures"][0].get("reachability").is_none());
let bug = write_spec(
&dir,
"bug.spock",
r#"
struct State { x: Int }
impl State {
action init() -> State { State { x: 0 } }
action step(self) -> State { Self { ..self, x: self.x + 1 } }
fn small(self) -> Bool { self.x < 2 }
}
model m {
init = State::init;
next = State::step;
invariants = [State::small];
deadlock = false;
}
"#,
);
let out = spock().args(["prove", &bug]).output().unwrap();
assert_eq!(out.status.code(), Some(12));
let stdout = String::from_utf8(out.stdout).unwrap();
assert!(
stdout.contains("triage: pre-state is reachable in 1 step (real bug):"),
"stdout:\n{stdout}"
);
let out_json = spock().args(["prove", &bug, "--json"]).output().unwrap();
assert_eq!(out_json.status.code(), Some(12));
let v: Value = serde_json::from_slice(&out_json.stdout).unwrap();
assert_eq!(v["failures"][0]["reachability"]["kind"], "reachable");
assert_eq!(v["failures"][0]["reachability"]["steps"], 1);
assert_eq!(v["failures"][0]["reachability"]["witness"].as_array().unwrap().len(), 2);
}