Lines
100 %
Functions
Branches
//! Default-deny over both registries and the five obligations on sample forms.
use super::*;
#[test]
fn every_head_outside_the_whitelist_is_refused() {
let symbols = table();
let heads = crate::compiler::special::registered_names()
.chain(crate::compiler::native::registered_names())
.chain(crate::runtime::registered_native_names().iter().copied());
for head in heads {
let call = Expr::List(vec![Expr::Symbol(head.to_string())]);
let whitelisted = FOLD_HEADS.iter().any(|h| h.eq_ignore_ascii_case(head));
assert_eq!(
fold_safe(&symbols, &call),
whitelisted,
"{head}: a head is fold-safe exactly when whitelisted"
);
}
fn every_whitelisted_head_is_registered() {
for head in FOLD_HEADS {
assert!(symbols.lookup(head).is_some(), "{head} is not a builtin");
fn a_runtime_reference_is_refused_wherever_it_hides() {
let symbols = with_runtime("G");
for source in [
"g",
"(+ 1 g)",
"(length (progn (dolist (i '(1)) (length g)) '(1 2)))",
"(let ((x g)) 1)",
"(if g 1 2)",
] {
assert!(!fold_safe(&symbols, &form(source)), "{source}");
fn a_binder_inside_the_form_is_constant_by_induction() {
"(let ((x 1)) (+ x 1))",
"(let* ((x 1) (y x)) y)",
"(dolist (x '(1 2)) x)",
"(length (progn (dolist (x '(g)) (length x)) '(1 2)))",
"(block b (let () (lambda () (return-from b 7)) 1))",
assert!(fold_safe(&symbols, &form(source)), "{source}");
assert!(!fold_safe(&symbols, &form("(let ((x g)) x)")));
fn a_transfer_an_effect_or_an_opaque_macro_is_refused() {
let mut symbols = with_constant("C", "1");
symbols.define(
Symbol::new("OPAQUE", SymbolKind::Macro).with_function(Expr::Lambda(
crate::ast::LambdaParams::simple(Vec::new()),
Box::new(form("(if c 1 2)")),
)),
"(block b (return-from b 1))",
"(progn (setf c 2) 1)",
"(progn (print 1) 1)",
"(progn (error 'boom \"x\") 1)",
"(handler-case 1 (t (e) 2))",
"(unwind-protect 1 2)",
"(progn (defun f () 1) 1)",
"(opaque)",
"(funcall (lambda () 1))",
"(lambda () 1)",
"`(1 ,c)",
fn a_template_macro_is_scanned_through_its_expansion() {
let mut symbols = table();
assert!(fold_safe(&symbols, &form("(when t 1)")));
assert!(!fold_safe(&symbols, &form("(when t (setf g 1))")));
Symbol::new("EXIT", SymbolKind::Macro).with_function(Expr::Lambda(
Box::new(form("'(return-from b 7)")),
assert!(!fold_safe(&symbols, &form("(block b (exit))")));
fn reads_only_admits_variable_reads_and_refuses_effects() {
for (source, quiet) in [
("g", true),
("(list g g)", true),
("(if g (list g) 2)", true),
("(car g)", false),
("(car (list (lambda () 4)))", true),
("(/ 1 g)", false),
("(mod 1 g)", false),
("(if (car (list (/ 1 g))) 1 nil)", false),
("(car (list (lambda () (print g))))", true),
("(print g)", false),
("(setf g 1)", false),
("(let ((y (print 1))) g)", false),
("(unwind-protect g (print 0))", false),
assert_eq!(reads_only(&symbols, &form(source)), quiet, "{source}");