-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
290 lines (258 loc) · 6.63 KB
/
server_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
package main
import (
"bufio"
"bytes"
"encoding/binary"
"encoding/json"
"log"
"net/http"
"strings"
"testing"
)
// Test different scenarios for passing
// an arbitrary JSON from the browser
// to the server
func TestUnmarshalJsonStruct(t *testing.T) {
type User struct {
User string
}
type Obj struct {
Broadcast User
}
str := `{"Broadcast": {"User": "name"}}`
b := Obj{}
err := json.Unmarshal([]byte(str), &b)
if err != nil {
t.Fatalf(`JSON struct unmarshal error: "%s", should not have error`, err.Error())
}
}
func TestUnmarshalJsonStructString(t *testing.T) {
type User struct {
User string
}
type Obj struct {
Broadcast User
}
str := `{"Broadcast": "{\"User\": \"name\"}"}`
b := Obj{}
err := json.Unmarshal([]byte(str), &b)
log.Println(err.Error())
if err == nil {
t.Fatalf(`JSON struct unmarshal gives no error, there should be an error`)
}
}
func TestUnmarshalJsonString(t *testing.T) {
type Obj struct {
Broadcast string
}
str := `{"Broadcast": "{\"User\": \"name\"}"}`
b := Obj{}
err := json.Unmarshal([]byte(str), &b)
if err != nil {
t.Fatalf(`JSON string unmarshal error: "%s", should not have error`, err.Error())
}
type User struct {
User string
}
s := User{}
err = json.Unmarshal([]byte(b.Broadcast), &s)
if err != nil {
t.Fatalf(`JSON sub-struct unmarshal error: "%s", should not have error`, err.Error())
}
}
func TestUnmarshalArbitraryJson(t *testing.T) {
str := `{"Broadcast": {"User": "name"}}`
b := ClientMessage{}
err := json.Unmarshal([]byte(str), &b)
if err != nil {
t.Fatalf(`JSON arbitrary struct unmarshal error: "%s", should not have error`, err.Error())
}
if b.Broadcast["User"] != "name" {
t.Fatalf(`JSON arbitrary struct unmarshal: got "%s", want "name"`, b.Broadcast["User"])
}
}
func TestMarshalArbitraryJson(t *testing.T) {
s := ClientMessage{Broadcast: map[string]interface{}{
"User": "name",
}}
s.Broadcast["User"] = "name"
b, err := json.Marshal(s)
if err != nil {
t.Fatalf(`JSON arbitrary json marshal error: "%s", should not have error`, err.Error())
}
want := `{"Broadcast":{"User":"name"}}`
if string(b) != want {
t.Fatalf(`JSON arbitrary struct unmarshal: got "%s", want "%s"`, string(b), want)
}
}
// Payload length < 126
func TestRecvShortMessage(t *testing.T) {
want := strings.Repeat("a", 125)
msg, err := mockWebsocketMessage(want)
if err != nil {
log.Println("error creating mock websocket message: ", err.Error())
}
r := bytes.NewReader(msg)
rr := bufio.NewReader(r)
rw := bufio.NewReadWriter(rr, nil)
req, _ := http.NewRequest("GET", "localhost:8080", rw.Reader)
ws := Ws{
conn: nil,
bufrw: rw,
request: req,
}
unmaskedMsg, _, err := ws.Recv()
if want != unmaskedMsg {
t.Fatalf(`TestRecvShortMessage wrong msg: got "%s", want "%s"`, unmaskedMsg, want)
}
if err != nil {
t.Fatalf(`TestRecvShortMessage error: "%s", should not have error`, err.Error())
}
}
// Payload length == 126
func TestRecvMediumMessageLowerLimit(t *testing.T) {
want := strings.Repeat("a", 126)
msg, err := mockWebsocketMessage(want)
if err != nil {
log.Println("error creating mock websocket message: ", err.Error())
}
r := bytes.NewReader(msg)
rr := bufio.NewReader(r)
rw := bufio.NewReadWriter(rr, nil)
req, _ := http.NewRequest("GET", "localhost:8080", rw.Reader)
ws := Ws{
conn: nil,
bufrw: rw,
request: req,
}
unmaskedMsg, _, err := ws.Recv()
if want != unmaskedMsg {
t.Fatalf(`TestRecvShortMessage wrong msg: got "%s", want "%s"`, unmaskedMsg, want)
}
if err != nil {
t.Fatalf(`TestRecvShortMessage error: "%s", should not have error`, err.Error())
}
}
// Payload length == 126
func TestRecvMediumMessageUpperLimit(t *testing.T) {
want := strings.Repeat("a", 65535)
msg, err := mockWebsocketMessage(want)
if err != nil {
log.Println("error creating mock websocket message: ", err.Error())
}
r := bytes.NewReader(msg)
rr := bufio.NewReader(r)
rw := bufio.NewReadWriter(rr, nil)
req, _ := http.NewRequest("GET", "localhost:8080", rw.Reader)
ws := Ws{
conn: nil,
bufrw: rw,
request: req,
}
unmaskedMsg, _, err := ws.Recv()
if want != unmaskedMsg {
t.Fatalf(`TestRecvShortMessage wrong msg: got "%s", want "%s"`, unmaskedMsg, want)
}
if err != nil {
t.Fatalf(`TestRecvShortMessage error: "%s", should not have error`, err.Error())
}
}
// Payload length == 127 bytes
func TestRecvLongMessage(t *testing.T) {
want := strings.Repeat("a", 65536)
msg, err := mockWebsocketMessage(want)
if err != nil {
log.Println("error creating mock websocket message: ", err.Error())
}
r := bytes.NewReader(msg)
rr := bufio.NewReader(r)
rw := bufio.NewReadWriter(rr, nil)
req, _ := http.NewRequest("GET", "localhost:8080", rw.Reader)
ws := Ws{
conn: nil,
bufrw: rw,
request: req,
}
unmaskedMsg, _, err := ws.Recv()
if want != unmaskedMsg {
t.Fatalf(`TestRecvShortMessage wrong msg: got "%s", want "%s"`, unmaskedMsg, want)
}
if err != nil {
t.Fatalf(`TestRecvShortMessage error: "%s", should not have error`, err.Error())
}
}
var maskKey = []byte{0xbb, 0x76, 0x44, 0xbf}
func mockWebsocketMessage(msg string) ([]byte, error) {
maskedPay := maskPayload(msg)
payLen := len(msg)
var fin uint8 = 1
var rsv1 uint8 = 0
var rsv2 uint8 = 0
var rsv3 uint8 = 0
var opcode uint8 = 1
var masked uint8 = 1
frame := new(bytes.Buffer)
byte1 := (fin << 7) | (rsv1 << 6) | (rsv2 << 5) | (rsv3 << 4) | opcode
err := frame.WriteByte(byte1)
if err != nil {
return []byte{}, err
}
switch {
case payLen < 126:
// 7 bits to denote payload length (in bytes)
byte2 := (masked << 7) | uint8(payLen)
err = frame.WriteByte(byte2)
if err != nil {
return []byte{}, err
}
case payLen < (1 << 16):
// 7+16 bits
byte2 := (masked << 7) | (uint8(126))
err = frame.WriteByte(byte2)
if err != nil {
return []byte{}, err
}
bytes34 := uint16(payLen)
err = binary.Write(frame, binary.BigEndian, bytes34)
if err != nil {
return []byte{}, err
}
default:
// 7+64 bits
byte2 := (masked << 7) | (uint8(127))
err = frame.WriteByte(byte2)
if err != nil {
return []byte{}, err
}
nextBytes := uint64(payLen)
err = binary.Write(frame, binary.BigEndian, nextBytes)
if err != nil {
return []byte{}, err
}
}
for _, b := range maskKey {
err = binary.Write(frame, binary.BigEndian, b)
if err != nil {
return []byte{}, err
}
}
_, err = frame.Write(maskedPay)
if err != nil {
return []byte{}, err
}
return frame.Bytes(), nil
}
func maskPayload(payload string) []byte {
payLen := len(payload)
masked := make([]byte, payLen)
keyIdx := 0
for i, e := range []byte(payload) {
masked[i] = e ^ maskKey[keyIdx]
if keyIdx == len(maskKey)-1 {
keyIdx = 0
continue
}
keyIdx++
}
return masked
}