generated from mccutchen/go-pkg-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautobahn_test.go
316 lines (288 loc) · 9.49 KB
/
autobahn_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
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
package websocket_test
// ============================================================================
// Autobahn Test Suite
// ============================================================================
//
// This test suite runs the 3rd party [Autobahn WebSocket test suite][1]'s
// "fuzzing client" tests against an echo server implemented using this
// websocket package.
//
// Because these tests are slow (60-90s on my laptop) and require a running
// docker daemon, they are disabled by default. To run them, set the AUTOBAHN=1
// environment variable.
//
// By default, the fuzzing client is run against an ephemeral httptest server,
// but it can also be run against an external server by setting the TARGET
// environment variable to the server's URL.
//
// Other customization is possible via the `CASES` and `DEBUG` environment
// variables. See the [README][2] for more info.
//
// [1]: https://github.com/crossbario/autobahn-testsuite
// [2]: https://github.com/mccutchen/websocket/blob/main/README.md#autobahn-integration-tests
import (
"encoding/json"
"fmt"
"net"
"net/http"
"net/http/httptest"
"net/url"
"os"
"os/exec"
"os/user"
"path"
"path/filepath"
"regexp"
"runtime"
"strings"
"testing"
"time"
"github.com/mccutchen/websocket"
"github.com/mccutchen/websocket/internal/testing/assert"
)
const autobahnImage = "crossbario/autobahn-testsuite:0.8.2"
var defaultIncludedTestCases = []string{
"*",
}
var defaultExcludedTestCases = []string{
// Compression extensions are not supported
"12.*",
"13.*",
}
func TestAutobahn(t *testing.T) {
t.Parallel()
// TODO: document env vars that control test functionality
if os.Getenv("AUTOBAHN") == "" {
t.Skipf("set AUTOBAHN=1 to run autobahn integration tests")
}
includedTestCases := defaultIncludedTestCases
excludedTestCases := defaultExcludedTestCases
if userTestCases := os.Getenv("CASES"); userTestCases != "" {
t.Logf("using CASES=%q", userTestCases)
includedTestCases = strings.Split(userTestCases, ",")
excludedTestCases = []string{}
}
// Hooks can be expensive, so only enable them if necessary for debugging
var hooks websocket.Hooks
if debug := os.Getenv("DEBUG"); debug == "1" {
hooks = newTestHooks(t)
}
targetURL := os.Getenv("TARGET")
if targetURL == "" {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ws, err := websocket.Accept(w, r, websocket.Options{
Hooks: hooks,
// long ReadTimeout because some autobahn test cases (e.g. 5.19)
// sleep up to 1 second between frames
ReadTimeout: 5000 * time.Millisecond,
WriteTimeout: 500 * time.Millisecond,
// some autobahn test cases send large frames, so we need to
// support frames and messages up to 16 MiB
MaxFrameSize: 16 << 20,
MaxMessageSize: 16 << 20,
})
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
ws.Serve(r.Context(), websocket.EchoHandler)
}))
defer srv.Close()
targetURL = srv.URL
} else {
t.Logf("running autobahn against external target: %s", targetURL)
}
testDir := newTestDir(t)
t.Logf("test dir: %s", testDir)
targetURL = newAutobahnTargetURL(t, targetURL)
t.Logf("target url: %s", targetURL)
autobahnCfg := map[string]any{
"servers": []map[string]string{
{
"agent": "go-httpbin",
"url": targetURL,
},
},
"outdir": "/testdir/report",
"cases": includedTestCases,
"exclude-cases": excludedTestCases,
}
autobahnCfgFile, err := os.Create(path.Join(testDir, "autobahn.json"))
assert.NilError(t, err)
assert.NilError(t, json.NewEncoder(autobahnCfgFile).Encode(autobahnCfg))
autobahnCfgFile.Close()
pullCmd := exec.Command("docker", "pull", autobahnImage)
runCmd(t, pullCmd)
localUser, err := user.Current()
assert.NilError(t, err)
testCmd := exec.Command(
"docker",
"run",
"--net=host",
"--rm",
// we run the image as the local user to ensure that, on GH Actions CI
// runners, the output generated by autobahn isn't owned by root and
// therefore can be cleaned up by the CI runner.
//
// See the `find ... -delete` command in .github/workflows/test.yml for
// where this happens.
"--user", fmt.Sprintf("%s:%s", localUser.Uid, localUser.Gid),
"-v", testDir+":/testdir:rw",
autobahnImage,
"wstest", "-m", "fuzzingclient", "--spec", "/testdir/autobahn.json",
)
runCmd(t, testCmd)
summary := loadSummary(t, testDir)
if len(summary) == 0 {
t.Fatalf("empty autobahn test summary; check autobahn logs for problems connecting to test server at %q", targetURL)
}
for _, result := range summary {
result := result
t.Run("autobahn/"+result.ID, func(t *testing.T) {
if result.Failed() {
report := loadReport(t, testDir, result.ReportFile)
t.Errorf("description: %s", report.Description)
t.Errorf("expectation: %s", report.Expectation)
t.Errorf("want result: %s", report.Result)
t.Errorf("got result: %s", report.Behavior)
t.Errorf("want close: %s", report.ResultClose)
t.Errorf("got close: %s", report.BehaviorClose)
}
})
}
t.Logf("autobahn test report: file://%s", path.Join(testDir, "report/index.html"))
if os.Getenv("REPORT") == "1" {
runCmd(t, exec.Command("open", path.Join(testDir, "report/index.html")))
}
}
// newAutobahnTargetURL returns the URL that the autobahn test client should
// use to connect to the given target URL, which may be an ephemeral httptest
// server URL listening on localhost and a random port or some external URL
// provided by the TARGET env var.
//
// Note that the autobahn client will be running inside a container using
// --net=host, so localhost inside the container *should* map to localhost
// outside the container.
//
// On macOS, however, we must use a special "host.docker.internal" hostname for
// localhost addrs, because otherwise localhost inside the container will
// resolve to localhost on the implicit guest VM where docker is running rather
// than localhost on the actual macOS host machine.
//
// See the Docker Desktop docs[1] for more information. This same special
// hostname seems to work across Docker Desktop for Mac, OrbStack, and Colima.
//
// [1]: https://docs.docker.com/desktop/networking/#i-want-to-connect-from-a-container-to-a-service-on-the-host
func newAutobahnTargetURL(t *testing.T, targetURL string) string {
t.Helper()
if matched, _ := regexp.MatchString("^https?://", targetURL); !matched {
targetURL = "http://" + targetURL
}
u, err := url.Parse(targetURL)
assert.NilError(t, err)
u.Scheme = "ws"
if runtime.GOOS == "darwin" && isLocalhost(u.Hostname()) {
host := "host.docker.internal"
_, port, _ := net.SplitHostPort(u.Host)
if port != "" {
host = net.JoinHostPort(host, port)
}
u.Host = host
}
return u.String()
}
func isLocalhost(ipAddr string) bool {
ipAddr = strings.ToLower(ipAddr)
for _, addr := range []string{
"localhost",
"127.0.0.1",
"::1",
"0:0:0:0:0:0:0:1",
} {
if ipAddr == addr {
return true
}
}
return false
}
func runCmd(t *testing.T, cmd *exec.Cmd) {
t.Helper()
t.Logf("running command: %s", cmd.String())
cmd.Stdout = os.Stderr
cmd.Stderr = os.Stderr
assert.NilError(t, cmd.Run())
}
func newTestDir(t *testing.T) string {
t.Helper()
reportDir := os.Getenv("REPORT_DIR")
if reportDir == "" {
reportDir = path.Join(
".integrationtests", fmt.Sprintf("autobahn-test-%d", time.Now().Unix()),
)
}
testDir, err := filepath.Abs(reportDir)
assert.NilError(t, err)
assert.NilError(t, os.MkdirAll(testDir, 0o755))
return testDir
}
func loadSummary(t *testing.T, testDir string) []autobahnReportResult {
t.Helper()
f, err := os.Open(path.Join(testDir, "report", "index.json"))
assert.NilError(t, err)
defer f.Close()
var summary autobahnReportSummary
assert.NilError(t, json.NewDecoder(f).Decode(&summary))
var results []autobahnReportResult
for _, serverResults := range summary {
for id, result := range serverResults {
result.ID = id
results = append(results, result)
}
}
return results
}
func loadReport(t *testing.T, testDir string, reportFile string) autobahnReportResult {
t.Helper()
reportPath := path.Join(testDir, "report", reportFile)
t.Logf("report data: %s", reportPath)
t.Logf("report html: file://%s", strings.Replace(reportPath, ".json", ".html", 1))
f, err := os.Open(reportPath)
assert.NilError(t, err)
var report autobahnReportResult
assert.NilError(t, json.NewDecoder(f).Decode(&report))
return report
}
type autobahnReportSummary map[string]map[string]autobahnReportResult // server -> case -> result
type autobahnReportResult struct {
ID string `json:"id"`
Behavior string `json:"behavior"`
BehaviorClose string `json:"behaviorClose"`
Description string `json:"description"`
Expectation string `json:"expectation"`
ReportFile string `json:"reportfile"`
Result string `json:"result"`
ResultClose string `json:"resultClose"`
}
func (r autobahnReportResult) Failed() bool {
okayBehavior := map[string]bool{
"OK": true,
"INFORMATIONAL": true,
}
allowNonStrict := map[string]bool{
// Some weirdness in these test cases, where they expect the server to
// time out and close the connection, but it's not clear after exactly
// how long the timeout should happen (and, AFAICT, other test cases
// expect a different timeout).
//
// The cases pass with "NON-STRICT" results when the timeout is not
// hit, as long as we return the expected 1007 status code.
"6.4.1": true,
"6.4.2": true,
"6.4.3": true,
"6.4.4": true,
}
if allowNonStrict[r.ID] {
okayBehavior["NON-STRICT"] = true
}
return !(okayBehavior[r.Behavior] && okayBehavior[r.BehaviorClose])
}