-
Notifications
You must be signed in to change notification settings - Fork 0
/
frame_test.go
102 lines (81 loc) · 1.86 KB
/
frame_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
//go:build !js
// +build !js
package websocket
import (
"bufio"
"bytes"
"math/rand"
"strconv"
"testing"
"time"
"github.com/fortytw2/websocket/internal/test/assert"
)
func TestHeader(t *testing.T) {
t.Parallel()
t.Run("lengths", func(t *testing.T) {
t.Parallel()
lengths := []int{
124,
125,
126,
127,
65534,
65535,
65536,
65537,
}
for _, n := range lengths {
n := n
t.Run(strconv.Itoa(n), func(t *testing.T) {
t.Parallel()
testHeader(t, header{
payloadLength: int64(n),
})
})
}
})
t.Run("fuzz", func(t *testing.T) {
t.Parallel()
r := rand.New(rand.NewSource(time.Now().UnixNano()))
randBool := func() bool {
return rand.Int()%2 == 0
}
for i := 0; i < 10000; i++ {
h := header{
fin: randBool(),
rsv1: randBool(),
rsv2: randBool(),
rsv3: randBool(),
opcode: opcode(r.Intn(16)),
masked: randBool(),
maskKey: r.Uint32(),
payloadLength: r.Int63(),
}
testHeader(t, h)
}
})
}
func testHeader(t *testing.T, h header) {
b := &bytes.Buffer{}
w := bufio.NewWriter(b)
r := bufio.NewReader(b)
err := writeFrameHeader(h, w, make([]byte, 8))
assert.Success(t, err)
err = w.Flush()
assert.Success(t, err)
h2, err := readFrameHeader(r, make([]byte, 8))
assert.Success(t, err)
assert.Equal(t, "header fin", h.fin, h2.fin)
assert.Equal(t, "header rsv1", h.rsv1, h2.rsv1)
assert.Equal(t, "header rsv2", h.rsv2, h2.rsv2)
assert.Equal(t, "header rsv3", h.rsv3, h2.rsv3)
assert.Equal(t, "header opcode", h.opcode, h2.opcode)
// if masked we need the mask key otherwise ignorei t
if h.masked {
assert.Equal(t, "header masked", h.masked, h2.masked)
assert.Equal(t, "header maskKey", h.maskKey, h2.maskKey)
} else {
assert.Equal(t, "header masked", h.masked, h2.masked)
}
assert.Equal(t, "read header", h.payloadLength, h2.payloadLength)
}