-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime_test.go
165 lines (127 loc) · 4.43 KB
/
runtime_test.go
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
package generator_test
import (
"context"
_ "embed"
"net"
"os"
"path/filepath"
"testing"
"github.com/jlevesy/dawg/generator"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
//go:generate tinygo build -o ./testdata/runtime/goodtinygo.wasm -scheduler=none --no-debug -target wasi ./testdata/runtime/good
//go:embed testdata/runtime/goodtinygo.wasm
var goodTinygo []byte
func TestRuntime_Good(t *testing.T) {
var payload = []byte(`{"some":"config"}`)
result, err := runWasm(t, goodTinygo, payload)
require.NoError(t, err)
assert.Equal(t, payload, result.Payload)
}
//go:generate env GOOS=wasip1 GOARCH=wasm go build -o ./testdata/runtime/goodgo121.wasm ./testdata/runtime/good
//go:embed testdata/runtime/goodgo121.wasm
var goodGo121Bin []byte
func TestRuntime_GoodWithGo121(t *testing.T) {
// See https://github.com/golang/go/issues/42372#issuecomment-1837330447
t.Skip("go 121 does not allow module exports, this doesn't work")
var payload = []byte(`{"some":"config"}`)
result, err := runWasm(t, goodGo121Bin, payload)
require.NoError(t, err)
assert.Equal(t, payload, result.Payload)
}
//go:generate tinygo build -o ./testdata/runtime/panic.wasm -scheduler=none --no-debug -target wasi ./testdata/runtime/panic
//go:embed testdata/runtime/panic.wasm
var panicBin []byte
func TestRuntime_ReportPanic(t *testing.T) {
_, err := runWasm(t, panicBin, nil)
assert.Error(t, err)
}
//go:generate tinygo build -o ./testdata/runtime/nofsread.wasm -scheduler=none --no-debug -target wasi ./testdata/runtime/nofsread
//go:embed testdata/runtime/nofsread.wasm
var noFSReadBin []byte
func TestRuntime_NoFSRead(t *testing.T) {
var (
filePath = filepath.Join(t.TempDir(), "secret")
)
err := os.WriteFile(filePath, []byte("sensitive info"), 0777)
require.NoError(t, err)
res, err := runWasm(t, noFSReadBin, []byte(filePath))
assert.Error(t, err)
assert.Empty(t, res, "Returned result should have been empty")
}
//go:generate tinygo build -o ./testdata/runtime/nofswrite.wasm -scheduler=none --no-debug -target wasi ./testdata/runtime/nofswrite
//go:embed testdata/runtime/nofswrite.wasm
var noFSWriteBin []byte
func TestRuntime_NoFSWrite(t *testing.T) {
_, err := runWasm(t, noFSWriteBin, nil)
assert.Error(t, err)
_, err = os.Open("./hacked")
assert.Error(t, err, "File has been written to disk by WASM binary")
}
//go:generate tinygo build -o ./testdata/runtime/noenvread.wasm -scheduler=none --no-debug -target wasi ./testdata/runtime/noenvread
//go:embed testdata/runtime/noenvread.wasm
var noEnvReadBin []byte
func TestRuntime_NoEnvRead(t *testing.T) {
err := os.Setenv("DAWG_TEST_SUITE_SECRET", "supersecret")
require.NoError(t, err)
t.Cleanup(func() {
err := os.Unsetenv("DAWG_TEST_SUITE_SECRET")
require.NoError(t, err)
})
result, err := runWasm(t, noEnvReadBin, nil)
assert.Error(t, err)
assert.Empty(t, result)
}
//go:generate env GOOS=wasip1 GOARCH=wasm go build -o ./testdata/runtime/nonet.wasm ./testdata/runtime/nonet
//go:embed testdata/runtime/nonet.wasm
var noNetBin []byte
func TestRuntime_NoNetwork(t *testing.T) {
var (
called int
listenerDone = make(chan struct{})
)
listener, err := net.Listen("tcp", "localhost:0")
require.NoError(t, err)
go func() {
for {
conn, err := listener.Accept()
if err != nil {
close(listenerDone)
return
}
called++
_, err = conn.Write([]byte("pwned"))
require.NoError(t, err)
err = conn.Close()
require.NoError(t, err)
}
}()
t.Cleanup(func() {
err := listener.Close()
require.NoError(t, err)
<-listenerDone
})
_, err = runWasm(t, noNetBin, []byte(listener.Addr().String()))
assert.ErrorContains(t, err, `"sock_open" is not exported in module "wasi_snapshot_preview1"`)
assert.Equal(t, 0, called)
}
//go:generate tinygo build -o ./testdata/runtime/hang.wasm -scheduler=none --no-debug -target wasi ./testdata/runtime/hang
//go:embed testdata/runtime/hang.wasm
var hangBin []byte
func TestRuntime_Hang(t *testing.T) {
_, err := runWasm(t, hangBin, nil)
// wazero doesn't exactly wrap this error as far as I can see.
assert.ErrorContains(t, err, context.DeadlineExceeded.Error())
}
func runWasm(t *testing.T, bin, args []byte) (*generator.ExecutionResult, error) {
t.Helper()
ctx := context.Background()
runtime, shutdown, err := generator.DefaultRuntime(ctx)
require.NoError(t, err)
t.Cleanup(func() {
err := shutdown(ctx)
require.NoError(t, err)
})
return runtime.Execute(ctx, &generator.Generator{Bin: bin}, args)
}