-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunerror_test.go
More file actions
128 lines (118 loc) · 4.54 KB
/
Copy pathrunerror_test.go
File metadata and controls
128 lines (118 loc) · 4.54 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
package starbox_test
// RunError classification (STAR-8 / BOX-08 / R9) tests:
// - TestClassifyRunError nil, arbitrary-error (Unknown, fail-loud), String() names
// - TestRunErrorKinds each failure mode classifies to its Kind (chain preserved)
// - TestRunErrorPassthrough errors.As reaches the original typed error THROUGH a RunError
import (
"errors"
"strings"
"testing"
"github.com/1set/starbox"
"github.com/1set/starlet"
)
func TestClassifyRunError(t *testing.T) {
if starbox.ClassifyRunError(nil) != nil {
t.Error("ClassifyRunError(nil) should be nil")
}
// An unrecognised error is Unknown (fail-loud zero value); the chain is kept.
base := errors.New("something else")
re := starbox.ClassifyRunError(base)
if re.Kind != starbox.RunErrorUnknown {
t.Errorf("arbitrary error: Kind = %v, want unknown", re.Kind)
}
if !errors.Is(re, base) {
t.Error("RunError should unwrap to the original error")
}
if !strings.Contains(re.Error(), "unknown") {
t.Errorf("RunError.Error() = %q, want mention of kind", re.Error())
}
// A zero/empty RunError stringifies safely (the nil-underlying guard).
if got := (&starbox.RunError{}).Error(); got == "" {
t.Error("empty RunError.Error() should be non-empty")
}
// String() is stable per kind.
for k, want := range map[starbox.RunErrorKind]string{
starbox.RunErrorUnknown: "unknown",
starbox.RunErrorSyntax: "syntax",
starbox.RunErrorCompile: "compile",
starbox.RunErrorModuleWithheld: "module_withheld",
starbox.RunErrorMaxSteps: "max_steps",
starbox.RunErrorOutputLimit: "output_limit",
starbox.RunErrorEval: "eval",
} {
if got := k.String(); got != want {
t.Errorf("Kind(%d).String() = %q, want %q", k, got, want)
}
}
}
func TestRunErrorKinds(t *testing.T) {
cases := []struct {
name string
kind starbox.RunErrorKind
build func(*starbox.Starbox)
script string
}{
{"syntax", starbox.RunErrorSyntax, nil, `x = (`},
{"compile", starbox.RunErrorCompile, nil, `y = nope`},
{"eval", starbox.RunErrorEval, nil, `x = 1 // 0`},
{"module_withheld", starbox.RunErrorModuleWithheld, func(b *starbox.Starbox) { b.SetModuleSet(starbox.SafeModuleSet) }, `load("file", "x")`},
{"max_steps", starbox.RunErrorMaxSteps, func(b *starbox.Starbox) { b.SetMaxExecutionSteps(1000) }, "s = 0\nfor i in range(100000000):\n\ts += i"},
{"output_limit", starbox.RunErrorOutputLimit, func(b *starbox.Starbox) { b.SetMaxOutputEntries(1) }, "a = 1\nb = 2\nc = 3"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
b := starbox.New("re")
b.SetPrintFunc(noopPrint)
if tc.build != nil {
tc.build(b)
}
_, err := b.Run(hereDoc(tc.script))
re := starbox.ClassifyRunError(err)
if re == nil {
t.Fatalf("want error, got nil")
}
if re.Kind != tc.kind {
t.Errorf("Kind = %v, want %v (err=%v)", re.Kind, tc.kind, err)
}
// The underlying error is preserved (some Starlark errors are
// uncomparable, so check non-nil rather than == err).
if errors.Unwrap(re) == nil {
t.Error("RunError.Unwrap should expose the underlying error")
}
})
}
}
func TestRunErrorPassthrough(t *testing.T) {
// errors.As must reach the original typed error THROUGH the RunError wrapper
// (the R9 "preserve the host's original error" contract).
b := starbox.New("passthrough")
b.SetPrintFunc(noopPrint)
b.SetMaxExecutionSteps(1000)
_, err := b.Run(hereDoc("s = 0\nfor i in range(100000000):\n\ts += i"))
re := starbox.ClassifyRunError(err)
if re == nil || re.Kind != starbox.RunErrorMaxSteps {
t.Fatalf("want max_steps RunError, got %v", re)
}
var mse starlet.MaxStepsExceededError
if !errors.As(re, &mse) {
t.Error("errors.As should reach MaxStepsExceededError through the RunError")
}
}
func TestMaxExecutionStepsSurvivesReset(t *testing.T) {
// The step budget must survive Reset. Reset's doc promises it keeps the
// Box's limits, and Reset is the sanctioned way to serially reuse one Box on
// untrusted scripts — so silently dropping the CPU-DoS guard after the first
// run is exactly the dangerous case a security-minded host would hit.
loop := hereDoc("s = 0\nfor i in range(100000000):\n\ts += i")
b := starbox.New("reset-budget")
b.SetPrintFunc(noopPrint)
b.SetMaxExecutionSteps(1000)
var mse starlet.MaxStepsExceededError
if _, err := b.Run(loop); !errors.As(err, &mse) {
t.Fatalf("first run: expected MaxStepsExceededError, got %v", err)
}
b.Reset()
if _, err := b.Run(loop); !errors.As(err, &mse) {
t.Fatalf("after Reset the step budget was dropped; expected MaxStepsExceededError, got %v", err)
}
}