Repository navigation
Expand file tree
/
Copy pathscript_test.go
More file actions
373 lines (360 loc) · 12.1 KB
/
Copy pathscript_test.go
File metadata and controls
373 lines (360 loc) · 12.1 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package glua
import (
"context"
"errors"
"strings"
"sync"
"testing"
)
type scriptReq struct {
Method string
Path string
Cost int64
}
func mustLoad(t *testing.T, rt *Runtime, src string) *Script {
t.Helper()
s, err := rt.Load(context.Background(), "policy.lua", src)
if err != nil {
t.Fatal(err)
}
return s
}
func TestLoadAndCall(t *testing.T) {
ctx := context.Background()
s := mustLoad(t, MustNew(), `
local prefix = "/api/"
calls = 0
function allow(req, limit)
if req.method ~= "GET" then return false end
return req.path:sub(1, #prefix) == prefix and req.cost <= limit
end
handlers = {deny = function() return false, "denied" end}
function count() calls = calls + 1 end
local n = 0
function bump() n = n + 1 return n end
function echo(...) return ... end
return function(x) return x * 2 end`)
allow, err := s.Func2[*scriptReq, int64, bool]("allow")
if err != nil {
t.Fatal(err)
}
for _, tc := range []struct {
req scriptReq
want bool
}{
{scriptReq{"GET", "/api/x", 3}, true},
{scriptReq{"POST", "/api/x", 3}, false},
{scriptReq{"GET", "/web", 3}, false},
{scriptReq{"GET", "/api/x", 30}, false},
} {
if got, err := allow(ctx, &tc.req, 10); err != nil || got != tc.want {
t.Errorf("allow(%+v) = %v, %v", tc.req, got, err)
}
}
deny, err := s.Func0[Pair[bool, string]]("handlers.deny")
if err != nil {
t.Fatal(err)
}
if p, err := deny(ctx); err != nil || p.First || p.Second != "denied" {
t.Errorf("deny: %+v %v", p, err)
}
double, err := s.Func1[int64, int64]("")
if err != nil {
t.Fatal(err)
}
if n, err := double(ctx, 21); err != nil || n != 42 {
t.Errorf("double: %v %v", n, err)
}
// a shared script cannot keep state
count, _ := s.Func0[struct{}]("count")
if _, err := count(ctx); err == nil || !strings.Contains(err.Error(), "attempt to modify a frozen table") {
t.Errorf("count: %v", err)
}
bump, _ := s.Func0[int64]("bump")
if _, err := bump(ctx); err == nil || !strings.Contains(err.Error(), "policy.lua:11: attempt to assign to frozen upvalue") {
t.Errorf("bump: %v", err)
}
if _, err := s.Func0[int64]("nope"); !errors.Is(err, ErrUndefined) {
t.Errorf("nope: %v", err)
}
if _, err := s.Func0[int64]("handlers.x.y"); !errors.Is(err, ErrUndefined) {
t.Errorf("handlers.x.y: %v", err)
}
if _, err := s.Func0[int64]("calls"); !errors.Is(err, ErrNotFunction) {
t.Errorf("calls: %v", err)
}
var be *BindError
if _, err := s.Func1[chan int, int64]("echo"); !errors.As(err, &be) || be.Name != "echo" {
t.Errorf("chan argument: %v", err)
}
// a Go function as the target
upper, err := s.Func1[string, string]("string.upper")
if err != nil {
t.Fatal(err)
}
if u, err := upper(ctx, "abc"); err != nil || u != "ABC" {
t.Errorf("upper: %v %v", u, err)
}
// Call and CallN
echo := s.Globals().Get(String("echo"))
res, err := s.CallN(ctx, nil, echo, Int(1), String("b"), Bool(true))
if err != nil || joinValues(res) != "1\tb\ttrue" {
t.Errorf("CallN: %v %v", joinValues(res), err)
}
if v, err := s.Call(ctx, s.Result(), Int(4)); err != nil || v.String() != "8" {
t.Errorf("Call: %v %v", v, err)
}
if _, err := s.Call(ctx, Int(1)); !errors.Is(err, ErrNotFunction) {
t.Errorf("Call(1): %v", err)
}
if v, err := s.Call(ctx, s.Globals().Get(String("tostring")), Int(5)); err != nil || v.String() != "5" {
t.Errorf("Call(tostring): %v %v", v, err)
}
}
func TestLoadErrors(t *testing.T) {
ctx := context.Background()
rt := MustNew(LibSafe | LibCoroutine)
for _, tc := range []struct{ src, want string }{
{"x = = 1", "policy.lua:1: unexpected symbol near '='"},
{"error('boom')", "policy.lua:1: boom"},
{"co = coroutine.create(print)", "policy.lua: cannot freeze: global 'co' is a coroutine"},
{"t = {f = coroutine.wrap(print)}", "global 't'.f is a coroutine.wrap function with hidden state"},
{"it = string.gmatch('a', 'a')", "global 'it' is a gmatch iterator function with hidden state"},
{"return coroutine.create(print)", "result 1 is a coroutine"},
{"local co = coroutine.create(print) function f() return co end", "global 'f' (upvalue 'co') is a coroutine"},
{"while true do end", "step limit exceeded"},
} {
_, err := rt.Load(ctx, "policy.lua", tc.src)
if err == nil || !strings.Contains(err.Error(), tc.want) {
t.Errorf("%s: got %v, want %q", tc.src, err, tc.want)
}
}
var se *SyntaxError
if _, err := rt.Load(ctx, "x.lua", "local = 1"); !errors.As(err, &se) || se.Source != "x.lua" || se.Line != 1 {
t.Errorf("syntax error: %#v", err)
}
cctx, cancel := context.WithCancel(ctx)
cancel()
if _, err := rt.Load(cctx, "x.lua", "return 1"); !errors.Is(err, context.Canceled) {
t.Errorf("cancelled: %v", err)
}
// a failed freeze leaves the libraries alone
if _, err := rt.Load(ctx, "ok.lua", "string.x = 1"); err == nil {
t.Error("a script modified the string library")
}
}
func TestLoadModules(t *testing.T) {
ctx := context.Background()
runs := 0
rt := MustNew(
SourceModule("util", "local M = {n = 0} function M.get() return M.n end return M"),
Loader(func(name string) (string, error) {
runs++
return "return {name = '" + name + "'}", nil
}),
)
s := mustLoad(t, rt, `util = require("util") ext = require("ext") function late() return require("other") end
function again() return require("ext").name end`)
if v, err := s.Get[int]("util.n"); err != nil || v != 0 {
t.Errorf("util.n: %v %v", v, err)
}
late, _ := s.Func0[Value]("late")
if _, err := late(ctx); err == nil || !strings.Contains(err.Error(), "module 'other' not found: modules can be loaded only while the script loads") {
t.Errorf("late require: %v", err)
}
again, _ := s.Func0[string]("again")
if name, err := again(ctx); err != nil || name != "ext" {
t.Errorf("again: %v %v", name, err)
}
if runs != 1 {
t.Errorf("the loader ran %d times", runs)
}
if !s.Globals().Get(String("util")).isObj(kindTable) || !(*Table)(s.Globals().Get(String("util")).p).Frozen() {
t.Error("the module is not frozen")
}
}
func TestScriptGet(t *testing.T) {
type config struct {
Port int
Hosts []string
}
s := mustLoad(t, MustNew(), `config = {port = 8080, hosts = {"a", "b"}}`)
cfg, err := s.Get[config]("config")
if err != nil || cfg.Port != 8080 || len(cfg.Hosts) != 2 {
t.Errorf("config: %+v %v", cfg, err)
}
tab, err := s.Get[*Table]("config")
if err != nil || !tab.Frozen() {
t.Fatalf("config table: %v", err)
}
if port, err := tab.Field[int]("port"); err != nil || port != 8080 {
t.Errorf("port: %v %v", port, err)
}
if _, err := s.Get[int]("missing"); !errors.Is(err, ErrUndefined) {
t.Errorf("missing: %v", err)
}
if _, err := s.Get[int]("config.hosts"); err == nil {
t.Error("a table converted to int")
}
}
func TestScriptStructByValue(t *testing.T) {
ctx := context.Background()
s := mustLoad(t, MustNew(), `
function cost(r) return r.cost * 2 end
function keep(r) return r end
function wrap(a, b) return {a, b, n = a.cost + b.cost} end
function write(r) r.cost = 1 end`)
cost, err := s.Func1[scriptReq, int64]("cost")
if err != nil {
t.Fatal(err)
}
if n, err := cost(ctx, scriptReq{Cost: 21}); err != nil || n != 42 {
t.Errorf("cost: %v %v", n, err)
}
// a struct by value that escapes in the results is copied out of the slot
keep, _ := s.Func1[scriptReq, Value]("keep")
v, err := keep(ctx, scriptReq{Path: "/a"})
if err != nil {
t.Fatal(err)
}
if v.n&(flagCallscoped|flagReadonly) != 0 || (*scriptReq)(v.p).Path != "/a" {
t.Errorf("escaped userdata: flags %x", v.n>>48&0xff)
}
v2, _ := keep(ctx, scriptReq{Path: "/b"})
if (*scriptReq)(v.p).Path != "/a" || (*scriptReq)(v2.p).Path != "/b" {
t.Error("an escaped result shares the slot")
}
kp, _ := s.Func1[scriptReq, *scriptReq]("keep")
if p, err := kp(ctx, scriptReq{Cost: 3}); err != nil || p.Cost != 3 {
t.Errorf("as pointer: %v %v", p, err)
}
wrap, _ := s.Func2[scriptReq, scriptReq, *Table]("wrap")
tab, err := wrap(ctx, scriptReq{Cost: 1}, scriptReq{Cost: 2})
if err != nil {
t.Fatal(err)
}
a, b := tab.Get(Int(1)), tab.Get(Int(2))
if a.p == b.p || (*scriptReq)(a.p).Cost != 1 || (*scriptReq)(b.p).Cost != 2 || a.n&flagCallscoped != 0 {
t.Error("two arguments of one struct type share a slot, or stayed callscoped")
}
write, _ := s.Func1[scriptReq, struct{}]("write")
if _, err := write(ctx, scriptReq{}); err == nil {
t.Error("a script wrote a struct passed by value")
}
}
func TestScriptConcurrent(t *testing.T) {
s := mustLoad(t, MustNew(), `
local base = {k = 10}
function f(x) local t = {} for i = 1, 10 do t[i] = x + i end return t[10] + base.k end`)
f, err := s.Func1[int64, int64]("f")
if err != nil {
t.Fatal(err)
}
var wg sync.WaitGroup
for g := range 8 {
wg.Go(func() {
for i := range int64(200) {
x := int64(g)*1000 + i
if n, err := f(context.Background(), x); err != nil || n != x+20 {
t.Errorf("f(%d) = %d, %v", x, n, err)
return
}
}
})
}
wg.Wait()
}
func TestScriptLimits(t *testing.T) {
ctx := context.Background()
rt := MustNew(Limits{Steps: 1000, InitSteps: 100000})
s := mustLoad(t, rt, `for i = 1, 5000 do end function spin(n) for i = 1, n do end end`)
spin, _ := s.Func1[int64, struct{}]("spin")
if _, err := spin(ctx, 500); err != nil {
t.Errorf("500 steps: %v", err)
}
if _, err := spin(ctx, 5000); !errors.Is(err, ErrSteps) {
t.Errorf("5000 steps: %v", err)
}
big, err := s.WithLimits(Limits{Steps: 10000})
if err != nil {
t.Fatal(err)
}
spinBig, _ := big.Func1[int64, struct{}]("spin")
if _, err := spinBig(ctx, 5000); err != nil {
t.Errorf("5000 steps with more: %v", err)
}
if _, err := spin(ctx, 5000); !errors.Is(err, ErrSteps) {
t.Errorf("the original script's limit changed: %v", err)
}
if _, err := MustNew(Hardened()).Load(ctx, "x", "return 1"); err != nil {
t.Fatal(err)
}
hs := mustLoad(t, MustNew(Hardened()), "return 1")
if _, err := hs.WithLimits(Limits{Steps: NoLimit}); err == nil {
t.Error("the hardened profile took NoLimit")
}
}
func TestPanics(t *testing.T) {
ctx := context.Background()
boom := Global("boom", GoFunc(func(c *Call) error { panic("host bug") }))
s := mustLoad(t, MustNew(boom), "function f() return pcall(boom) end")
f, _ := s.Func0[bool]("f")
func() {
defer func() {
if r := recover(); r != "host bug" {
t.Errorf("recovered %v", r)
}
}()
f(ctx)
t.Error("the panic did not continue")
}()
s = mustLoad(t, MustNew(boom, RecoverPanics()), "function f() return pcall(boom) end")
f, _ = s.Func0[bool]("f")
var pe *PanicError
if _, err := f(ctx); !errors.As(err, &pe) || pe.Value != "host bug" || len(pe.Stack) == 0 {
t.Errorf("RecoverPanics: %v", err)
}
// the executor that panicked is not pooled again; the next call works
g, _ := s.Func0[int64]("string.len")
_ = g
ok, _ := s.Func1[string, int64]("string.len")
if n, err := ok(ctx, "abc"); err != nil || n != 3 {
t.Errorf("after a panic: %v %v", n, err)
}
}
func TestObserve(t *testing.T) {
var stats []CallStats
rt := MustNew(Observe(func(cs CallStats) { stats = append(stats, cs) }))
s := mustLoad(t, rt, "function f(n) local t = {} for i = 1, n do t[i] = i end return #t end")
f, _ := s.Func1[int64, int64]("f")
f(context.Background(), 100)
if len(stats) != 2 {
t.Fatalf("%d entries observed", len(stats))
}
if stats[0].Script != "policy.lua" || stats[0].Func != "" {
t.Errorf("load: %+v", stats[0])
}
if cs := stats[1]; cs.Func != "f" || cs.Steps < 95 || cs.Steps > 110 || cs.AllocBytes <= 0 || cs.Err != nil {
t.Errorf("call: %+v", cs)
}
}
func TestScriptFuncNoAlloc(t *testing.T) {
if raceEnabled || stackStress {
t.Skip("sync.Pool drops items in race builds; lua_stackstress allocates stacks")
}
s := mustLoad(t, MustNew(), "function add(a, b) return a + b end function rule(r) return r.cost > 1 and r.method == 'GET' end")
add, _ := s.Func2[int64, int64, int64]("add")
rule, _ := s.Func1[scriptReq, bool]("rule")
byPtr, _ := s.Func1[*scriptReq, bool]("rule")
ctx := context.Background()
req := &scriptReq{Method: "GET", Cost: 2}
for name, f := range map[string]func(){
"add": func() { add(ctx, 1, 2) },
"by value": func() { rule(ctx, scriptReq{Method: "GET", Cost: 2}) },
"pointer": func() { byPtr(ctx, req) },
} {
if a := testing.AllocsPerRun(1000, f); a != 0 {
t.Errorf("%s allocates %v times per call", name, a)
}
}
}