Repository navigation
Expand file tree
/
Copy pathcheck_test.go
More file actions
131 lines (119 loc) · 4.32 KB
/
Copy pathcheck_test.go
File metadata and controls
131 lines (119 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package starbox_test
// Check (STAR-10 / BOX-10 / R8) tests, grouped by concern:
// - TestCheckValidAndSyntax clean script vs a syntax error
// - TestCheckUndefined undefined names become resolve diagnostics
// - TestCheckConfiguredNames Safe-set + custom + globals names resolve; excluded ones do not
// - TestCheckEdges unknown module set errors; Check does not execute the script
import (
"strings"
"testing"
"github.com/1set/starbox"
"go.starlark.net/starlark"
)
func TestCheckValidAndSyntax(t *testing.T) {
b := starbox.New("check-ok")
if d, err := b.Check(`x = 1 + 2`); err != nil || d != nil {
t.Errorf("valid script: diags=%v err=%v", d, err)
}
d, err := b.Check(`x = (`)
if err != nil {
t.Fatalf("Check syntax: %v", err)
}
if len(d) == 0 {
t.Error("syntax error produced no diagnostic")
}
}
func TestCheckUndefined(t *testing.T) {
b := starbox.New("check-undef")
d, err := b.Check(`y = nope + 1`)
if err != nil {
t.Fatalf("Check: %v", err)
}
if len(d) != 1 {
t.Fatalf("want 1 diagnostic, got %d: %v", len(d), d)
}
if !strings.Contains(d[0].Msg, "undefined: nope") {
t.Errorf("diagnostic msg = %q, want mention of undefined: nope", d[0].Msg)
}
if d[0].Line != 1 {
t.Errorf("diagnostic line = %d, want 1", d[0].Line)
}
if d[0].File != "box.star" {
t.Errorf("diagnostic file = %q, want box.star", d[0].File)
}
if s := d[0].String(); !strings.HasPrefix(s, "box.star:1:") || !strings.Contains(s, "undefined: nope") {
t.Errorf("Diagnostic.String() = %q", s)
}
}
func TestCheckConfiguredNames(t *testing.T) {
b := starbox.New("check-names")
b.SetModuleSet(starbox.SafeModuleSet)
b.AddKeyValue("word", "hi")
b.AddModuleData("conf", starlark.StringDict{"host": starlark.String("localhost")})
// math (namespaced), sleep (flat from go_idiomatic), a global, and a custom
// module name all resolve without running.
if d, err := b.Check("a = math.pi\nsleep(0)\nb = word\nc = conf.host"); err != nil || d != nil {
t.Errorf("configured names should resolve: diags=%v err=%v", d, err)
}
// net is excluded from the Safe set (BOX-05), so it is undefined here.
d, err := b.Check(`x = net`)
if err != nil {
t.Fatalf("Check: %v", err)
}
if len(d) != 1 || !strings.Contains(d[0].Msg, "undefined: net") {
t.Errorf("want 'undefined: net' diagnostic, got %v", d)
}
}
func TestCheckEdges(t *testing.T) {
// Unknown module set surfaces an error (same as Run).
bad := starbox.New("check-bad")
bad.SetModuleSet(starbox.ModuleSetName("nope"))
if _, err := bad.Check(`x = 1`); err == nil {
t.Error("expected error for unknown module set")
}
// A builtin module requested by name resolves (exercises the named-module
// branch of the predeclared set).
named := starbox.New("check-named")
named.AddNamedModules("math")
if d, err := named.Check(`x = math.pi`); err != nil || d != nil {
t.Errorf("named builtin should resolve: diags=%v err=%v", d, err)
}
// Diagnostic.String() without a file falls back to "line:col: msg".
if got := (starbox.Diagnostic{Line: 2, Col: 3, Msg: "boom"}).String(); got != "2:3: boom" {
t.Errorf("Diagnostic.String() without file = %q, want \"2:3: boom\"", got)
}
// Check must not execute the script: a side effect would show here, and a
// real Run must still work afterwards.
b := starbox.New("check-pure")
b.SetPrintFunc(noopPrint)
b.AddKeyValue("word", "World")
if _, err := b.Check(`x = word.upper()`); err != nil {
t.Fatalf("Check: %v", err)
}
out, err := b.Run(hereDoc(`x = word.upper()`))
if err != nil || out["x"] != "WORLD" {
t.Errorf("Run after Check: out=%v err=%v", out, err)
}
}
// Parsing is checked for correctness, not isolation: the pinned interpreter
// has no parser recursion budget. See SECURITY.md for the host boundary.
func TestParsingCheckAndRun(t *testing.T) {
for _, malformed := range []bool{false, true} {
source := "value = " + strings.Repeat("(", 8) + "1" + strings.Repeat(")", 8)
if malformed {
source += "\nbroken = (\n"
}
box := starbox.New("parser-depth")
diagnostics, err := box.Check(source)
if err != nil {
t.Fatal(err)
}
_, runErr := box.Run(source)
if !malformed && (len(diagnostics) != 0 || runErr != nil) {
t.Fatalf("ordinary source: %v, %v", diagnostics, runErr)
}
if malformed && (len(diagnostics) == 0 || runErr == nil) {
t.Fatalf("expected parse rejection: %v, %v", diagnostics, runErr)
}
}
}