-
Notifications
You must be signed in to change notification settings - Fork 1
/
peasocket_test.go
296 lines (276 loc) · 8.85 KB
/
peasocket_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
package peasocket
import (
"bytes"
"context"
"encoding/binary"
"io"
"math/bits"
"strings"
"testing"
"time"
"unsafe"
)
func TestMask(t *testing.T) {
for _, tc := range []struct {
desc string
maskKey uint32
msg, expect string
}{
{desc: "zero message becomes mask", maskKey: 0xff00_ff00, msg: "\x00\x00\x00\x00", expect: "\xff\x00\xff\x00"},
{desc: "zero mask no effect", maskKey: 0, msg: "\x00\xff\x00\xf0", expect: "\x00\xff\x00\xf0"},
{desc: "proper xor", maskKey: 0xff16ff39, msg: "\xf0\xfa\xaa\x1e", expect: "\x0f\xec\x55\x27"},
{desc: "fifth byte xor by last key byte", maskKey: 0xff16ff39, msg: "\xf0\xfa\xaa\x1e\xd8", expect: "\x0f\xec\x55\x27\x27"},
{desc: "correct key rotation", maskKey: 0xff16ff39, msg: "\xf0\xfa\xaa\x1e\x1e\xfa", expect: "\x0f\xec\x55\x27\xe1\xec"},
} {
got := []byte(tc.msg)
gotLE := []byte(tc.msg)
le := tc.maskKey // binary.BigEndian.Uint32((*[4]byte)(unsafe.Pointer(&tc.maskKey))[:])
expectKey := maskWS(tc.maskKey, got)
_ = mask(le, gotLE)
if string(got) != tc.expect {
t.Errorf("%v: key=%#X\n\tmessage: %q\n\texpected: %q\n\tgot: %q", tc.desc, tc.maskKey, tc.msg, tc.expect, got)
}
if string(gotLE) != tc.expect {
t.Errorf("%v: LE key=%#X\n\tmessage: %q\n\texpected: %q\n\tgot: %q", tc.desc, le, tc.msg, tc.expect, gotLE)
}
split1 := []byte(tc.msg[:len(tc.msg)/2+1])
split2 := []byte(tc.msg[len(split1):])
intermediateKey := maskWS(tc.maskKey, split1)
finalKey := maskWS(intermediateKey, split2)
if finalKey != expectKey {
t.Error("split message key and full message keyt not match:", finalKey, expectKey)
}
got = append(split1, split2...)
if string(got) != tc.expect {
t.Errorf("2STEP %v: key=%#X\n\tmessage: %q\n\texpected: %q\n\tgot: %q", tc.desc, tc.maskKey, tc.msg, tc.expect, got)
}
}
}
func TestFragment(t *testing.T) {
loopback := bytes.NewBuffer(nil)
tx := TxBuffered{}
rx := Rx{}
tx.SetTxTransport(&closer{Writer: loopback})
rx.SetRxTransport(io.NopCloser(loopback))
// e := defaultEntropy()
const message = "hello world!"
buffer := make([]byte, 24)
rd := strings.NewReader(message)
n, err := tx.WriteFragmentedMessage(0, rd, buffer)
wireLen := loopback.Len()
t.Log(loopback.String())
if err != nil {
t.Fatal(err)
} else if n != wireLen {
t.Error("returned bytes written of WriteFragmentedMessage not match written to loopback", n, wireLen)
}
done := false
var buf bytes.Buffer
rx.RxCallbacks.OnMessage = func(rx *Rx, message io.Reader) error {
done = rx.LastReceivedHeader.Fin()
buf.ReadFrom(message)
return nil
}
i := 0
for !done {
i++
n, err = rx.ReadNextFrame()
if err != nil || n != wireLen {
t.Error("error reading frame", n, wireLen, err)
}
if i > 8 {
t.Error("too many cycles, fin bit may have been incorrectly set")
break
}
}
got := buf.String()
if got != message {
t.Errorf("message garbled/incorrect after transferring with fragmentation\n\tgot:%q\n\twant:%q", got, message)
}
}
func TestServerClientPingPong(t *testing.T) {
const waitTimes = 1e7 * time.Millisecond
svRxloopback := &transport{}
svTxloopback := &transport{}
sv := NewServer(make([]byte, 1024))
cl := NewClient("abc", make([]byte, 1024), nil)
sv.rx.trp = svRxloopback
sv.tx.trp = svTxloopback
sv.state.OnConnect()
cl.rx.trp = svTxloopback
cl.tx.trp = svRxloopback
cl.state.OnConnect()
mainCtx := context.Background()
svPingMessage := []byte("hello, buenos dias")
for i := 0; i < 5; i++ {
doneSv := false
go func() {
ctx, cancel := context.WithTimeout(mainCtx, waitTimes)
defer cancel()
err := sv.Ping(ctx, svPingMessage)
if err != nil {
panic(err)
}
doneSv = true
}()
time.Sleep(50 * time.Millisecond)
err := cl.HandleNextFrame()
if err != nil {
t.Fatal(err)
}
err = sv.HandleNextFrame()
if err != nil {
t.Error("reading next frame", err)
}
time.Sleep(50 * time.Millisecond)
if sv.state.PendingAction() {
t.Error("server pending action")
}
if !doneSv {
t.Fatalf("server did not get response rx=%q tx=%q", svRxloopback.buf.String(), svTxloopback.buf.String())
}
}
}
// mask applies the WebSocket masking algorithm to b
// with the given key. Taken from nhooyr's implementation.
// See https://tools.ietf.org/html/rfc6455#section-5.3
//
// The returned value is the correctly rotated key to
// to continue to mask/unmask the message.
//
// It is optimized for LittleEndian architectures.
//
// See https://github.com/golang/go/issues/31586
func mask(key uint32, b []byte) uint32 {
// Convert key to little endian:
key = binary.BigEndian.Uint32((*[4]byte)(unsafe.Pointer(&key))[:])
if len(b) >= 8 {
key64 := uint64(key)<<32 | uint64(key)
// At some point in the future we can clean these unrolled loops up.
// See https://github.com/golang/go/issues/31586#issuecomment-487436401
// Then we xor until b is less than 128 bytes.
for len(b) >= 128 {
v := binary.LittleEndian.Uint64(b)
binary.LittleEndian.PutUint64(b, v^key64)
v = binary.LittleEndian.Uint64(b[8:16])
binary.LittleEndian.PutUint64(b[8:16], v^key64)
v = binary.LittleEndian.Uint64(b[16:24])
binary.LittleEndian.PutUint64(b[16:24], v^key64)
v = binary.LittleEndian.Uint64(b[24:32])
binary.LittleEndian.PutUint64(b[24:32], v^key64)
v = binary.LittleEndian.Uint64(b[32:40])
binary.LittleEndian.PutUint64(b[32:40], v^key64)
v = binary.LittleEndian.Uint64(b[40:48])
binary.LittleEndian.PutUint64(b[40:48], v^key64)
v = binary.LittleEndian.Uint64(b[48:56])
binary.LittleEndian.PutUint64(b[48:56], v^key64)
v = binary.LittleEndian.Uint64(b[56:64])
binary.LittleEndian.PutUint64(b[56:64], v^key64)
v = binary.LittleEndian.Uint64(b[64:72])
binary.LittleEndian.PutUint64(b[64:72], v^key64)
v = binary.LittleEndian.Uint64(b[72:80])
binary.LittleEndian.PutUint64(b[72:80], v^key64)
v = binary.LittleEndian.Uint64(b[80:88])
binary.LittleEndian.PutUint64(b[80:88], v^key64)
v = binary.LittleEndian.Uint64(b[88:96])
binary.LittleEndian.PutUint64(b[88:96], v^key64)
v = binary.LittleEndian.Uint64(b[96:104])
binary.LittleEndian.PutUint64(b[96:104], v^key64)
v = binary.LittleEndian.Uint64(b[104:112])
binary.LittleEndian.PutUint64(b[104:112], v^key64)
v = binary.LittleEndian.Uint64(b[112:120])
binary.LittleEndian.PutUint64(b[112:120], v^key64)
v = binary.LittleEndian.Uint64(b[120:128])
binary.LittleEndian.PutUint64(b[120:128], v^key64)
b = b[128:]
}
// Then we xor until b is less than 64 bytes.
for len(b) >= 64 {
v := binary.LittleEndian.Uint64(b)
binary.LittleEndian.PutUint64(b, v^key64)
v = binary.LittleEndian.Uint64(b[8:16])
binary.LittleEndian.PutUint64(b[8:16], v^key64)
v = binary.LittleEndian.Uint64(b[16:24])
binary.LittleEndian.PutUint64(b[16:24], v^key64)
v = binary.LittleEndian.Uint64(b[24:32])
binary.LittleEndian.PutUint64(b[24:32], v^key64)
v = binary.LittleEndian.Uint64(b[32:40])
binary.LittleEndian.PutUint64(b[32:40], v^key64)
v = binary.LittleEndian.Uint64(b[40:48])
binary.LittleEndian.PutUint64(b[40:48], v^key64)
v = binary.LittleEndian.Uint64(b[48:56])
binary.LittleEndian.PutUint64(b[48:56], v^key64)
v = binary.LittleEndian.Uint64(b[56:64])
binary.LittleEndian.PutUint64(b[56:64], v^key64)
b = b[64:]
}
// Then we xor until b is less than 32 bytes.
for len(b) >= 32 {
v := binary.LittleEndian.Uint64(b)
binary.LittleEndian.PutUint64(b, v^key64)
v = binary.LittleEndian.Uint64(b[8:16])
binary.LittleEndian.PutUint64(b[8:16], v^key64)
v = binary.LittleEndian.Uint64(b[16:24])
binary.LittleEndian.PutUint64(b[16:24], v^key64)
v = binary.LittleEndian.Uint64(b[24:32])
binary.LittleEndian.PutUint64(b[24:32], v^key64)
b = b[32:]
}
// Then we xor until b is less than 16 bytes.
for len(b) >= 16 {
v := binary.LittleEndian.Uint64(b)
binary.LittleEndian.PutUint64(b, v^key64)
v = binary.LittleEndian.Uint64(b[8:16])
binary.LittleEndian.PutUint64(b[8:16], v^key64)
b = b[16:]
}
// Then we xor until b is less than 8 bytes.
for len(b) >= 8 {
v := binary.LittleEndian.Uint64(b)
binary.LittleEndian.PutUint64(b, v^key64)
b = b[8:]
}
}
// Then we xor until b is less than 4 bytes.
for len(b) >= 4 {
v := binary.LittleEndian.Uint32(b)
binary.LittleEndian.PutUint32(b, v^key)
b = b[4:]
}
// xor remaining bytes.
for i := range b {
b[i] ^= byte(key)
key = bits.RotateLeft32(key, -8)
}
// Convert key back to big endian.
key = binary.BigEndian.Uint32((*[4]byte)(unsafe.Pointer(&key))[:])
return key
}
type closer struct {
io.Writer
closed bool
}
func (c *closer) Close() error {
c.closed = true
return nil
}
type transport struct {
buf bytes.Buffer
closed bool
}
func (c *transport) Close() error {
c.closed = true
return nil
}
func (c *transport) Read(b []byte) (int, error) {
if c.closed {
return 0, io.EOF
}
return c.buf.Read(b)
}
func (c *transport) Write(b []byte) (int, error) {
if c.closed {
return 0, io.EOF
}
return c.buf.Write(b)
}