1
//! Default-deny over both registries and the five obligations on sample forms.
2

            
3
use super::*;
4

            
5
#[test]
6
1
fn every_head_outside_the_whitelist_is_refused() {
7
1
    let symbols = table();
8
1
    let heads = crate::compiler::special::registered_names()
9
1
        .chain(crate::compiler::native::registered_names())
10
1
        .chain(crate::runtime::registered_native_names().iter().copied());
11
1
    for head in heads {
12
193
        let call = Expr::List(vec![Expr::Symbol(head.to_string())]);
13
7021
        let whitelisted = FOLD_HEADS.iter().any(|h| h.eq_ignore_ascii_case(head));
14
193
        assert_eq!(
15
193
            fold_safe(&symbols, &call),
16
193
            whitelisted,
17
            "{head}: a head is fold-safe exactly when whitelisted"
18
        );
19
    }
20
1
}
21

            
22
#[test]
23
1
fn every_whitelisted_head_is_registered() {
24
1
    let symbols = table();
25
1
    for head in FOLD_HEADS {
26
43
        assert!(symbols.lookup(head).is_some(), "{head} is not a builtin");
27
    }
28
1
}
29

            
30
#[test]
31
1
fn a_runtime_reference_is_refused_wherever_it_hides() {
32
1
    let symbols = with_runtime("G");
33
1
    for source in [
34
1
        "g",
35
1
        "(+ 1 g)",
36
1
        "(length (progn (dolist (i '(1)) (length g)) '(1 2)))",
37
1
        "(let ((x g)) 1)",
38
1
        "(if g 1 2)",
39
1
    ] {
40
5
        assert!(!fold_safe(&symbols, &form(source)), "{source}");
41
    }
42
1
}
43

            
44
#[test]
45
1
fn a_binder_inside_the_form_is_constant_by_induction() {
46
1
    let symbols = with_runtime("G");
47
1
    for source in [
48
1
        "(let ((x 1)) (+ x 1))",
49
1
        "(let* ((x 1) (y x)) y)",
50
1
        "(dolist (x '(1 2)) x)",
51
1
        "(length (progn (dolist (x '(g)) (length x)) '(1 2)))",
52
1
        "(block b (let () (lambda () (return-from b 7)) 1))",
53
1
    ] {
54
5
        assert!(fold_safe(&symbols, &form(source)), "{source}");
55
    }
56
1
    assert!(!fold_safe(&symbols, &form("(let ((x g)) x)")));
57
1
}
58

            
59
#[test]
60
1
fn a_transfer_an_effect_or_an_opaque_macro_is_refused() {
61
1
    let mut symbols = with_constant("C", "1");
62
1
    symbols.define(
63
1
        Symbol::new("OPAQUE", SymbolKind::Macro).with_function(Expr::Lambda(
64
1
            crate::ast::LambdaParams::simple(Vec::new()),
65
1
            Box::new(form("(if c 1 2)")),
66
1
        )),
67
1
    );
68
1
    for source in [
69
1
        "(block b (return-from b 1))",
70
1
        "(progn (setf c 2) 1)",
71
1
        "(progn (print 1) 1)",
72
1
        "(progn (error 'boom \"x\") 1)",
73
1
        "(handler-case 1 (t (e) 2))",
74
1
        "(unwind-protect 1 2)",
75
1
        "(progn (defun f () 1) 1)",
76
1
        "(opaque)",
77
1
        "(funcall (lambda () 1))",
78
1
        "(lambda () 1)",
79
1
        "`(1 ,c)",
80
1
    ] {
81
11
        assert!(!fold_safe(&symbols, &form(source)), "{source}");
82
    }
83
1
}
84

            
85
#[test]
86
1
fn a_template_macro_is_scanned_through_its_expansion() {
87
1
    let mut symbols = table();
88
1
    assert!(fold_safe(&symbols, &form("(when t 1)")));
89
1
    assert!(!fold_safe(&symbols, &form("(when t (setf g 1))")));
90
1
    symbols.define(
91
1
        Symbol::new("EXIT", SymbolKind::Macro).with_function(Expr::Lambda(
92
1
            crate::ast::LambdaParams::simple(Vec::new()),
93
1
            Box::new(form("'(return-from b 7)")),
94
1
        )),
95
1
    );
96
1
    assert!(!fold_safe(&symbols, &form("(block b (exit))")));
97
1
}
98

            
99
#[test]
100
1
fn reads_only_admits_variable_reads_and_refuses_effects() {
101
1
    let symbols = with_runtime("G");
102
1
    for (source, quiet) in [
103
1
        ("g", true),
104
1
        ("(list g g)", true),
105
1
        ("(if g (list g) 2)", true),
106
1
        ("(car g)", false),
107
1
        ("(car (list (lambda () 4)))", true),
108
1
        ("(/ 1 g)", false),
109
1
        ("(mod 1 g)", false),
110
1
        ("(if (car (list (/ 1 g))) 1 nil)", false),
111
1
        ("(car (list (lambda () (print g))))", true),
112
1
        ("(print g)", false),
113
1
        ("(setf g 1)", false),
114
1
        ("(let ((y (print 1))) g)", false),
115
1
        ("(unwind-protect g (print 0))", false),
116
1
    ] {
117
13
        assert_eq!(reads_only(&symbols, &form(source)), quiet, "{source}");
118
    }
119
1
}