use cbcl_core::dialect::{DialectInstallError, DialectRegistry};
use cbcl_core::r6::r6_violations;
use cbcl_core::role::R6Violation;
use cbcl_core::sexpr::SExpr;
use cbcl_parser::parse_dialect;
fn oauth_src(widened: bool) -> String {
let (login_to, abort_to, passwd_to) = if widened {
(
"(client authoriser)",
"(client authoriser)",
"(authoriser server)",
)
} else {
("client", "client", "authoriser")
};
format!(
"(define oauth (cbcl) @example
(:roles (server client authoriser))
(:resource-requirements
((max-depth 4) (max-expansion-size 256)))
(extend login (session scope)
:from server :to {login_to}
(tell @client :session session :scope scope :domain oauth))
(extend abort (session reason)
:from server :to {abort_to}
(tell @client :session session :reason reason :domain oauth))
(extend passwd (session credential)
:from client :to {passwd_to}
(tell @authoriser :session session :credential credential :domain oauth))
(extend auth (session grant)
:from authoriser :to server
(tell @server :session session :grant grant :domain oauth))
(extend quit (session reason)
:from client :to authoriser
(tell @authoriser :session session :reason reason :domain oauth))
(protocol (then begin (any login abort))
(then login passwd auth)
(then abort quit)))"
)
}
fn parse(src: &str) -> cbcl_core::dialect::Dialect {
let sexpr: SExpr = src
.parse()
.expect("OAuth dialect must parse as an S-expression");
parse_dialect(&sexpr).expect("OAuth dialect must parse as a dialect")
}
#[test]
fn as_written_parses_and_passes_r1_r5_but_fails_r6() {
let d = parse(&oauth_src(false));
assert_eq!(d.roles.len(), 3);
assert_eq!(
d.find_performative("login")
.unwrap()
.role
.as_ref()
.unwrap()
.from,
"server"
);
let violations = r6_violations(&d);
assert!(violations.contains(&R6Violation::NotCausallyLocal {
performative: "passwd".into(),
predecessor: "login".into(),
role: "authoriser".into(),
}));
assert!(violations.contains(&R6Violation::NotCausallyLocal {
performative: "auth".into(),
predecessor: "passwd".into(),
role: "server".into(),
}));
assert!(violations.contains(&R6Violation::NotCausallyLocal {
performative: "quit".into(),
predecessor: "abort".into(),
role: "authoriser".into(),
}));
let mut reg = DialectRegistry::new();
match reg.install(d) {
Err(DialectInstallError::R6Violation { .. }) => {}
other => panic!("expected an R6 violation, got {other:?}"),
}
}
#[test]
fn recipient_widened_repair_installs_cleanly() {
let d = parse(&oauth_src(true));
assert_eq!(r6_violations(&d), Vec::new());
let mut reg = DialectRegistry::new();
reg.install(d)
.expect("the widened OAuth dialect installs (R1–R6 pass)");
}