1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
18 // Test the getNonceAccept function with values in
19 // http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17
20 func TestSecWebSocketAccept(t *testing.T) {
21 nonce := []byte("dGhlIHNhbXBsZSBub25jZQ==")
22 expected := []byte("s3pPLMBiTxaQ9kYGzzhZRbK+xOo=")
23 accept, err := getNonceAccept(nonce)
25 t.Errorf("getNonceAccept: returned error %v", err)
28 if !bytes.Equal(expected, accept) {
29 t.Errorf("getNonceAccept: expected %q got %q", expected, accept)
33 func TestHybiClientHandshake(t *testing.T) {
38 {"ws://server.example.com/chat", "server.example.com"},
39 {"ws://127.0.0.1/chat", "127.0.0.1"},
41 if _, err := url.ParseRequestURI("http://[fe80::1%25lo0]"); err == nil {
42 tests = append(tests, test{"ws://[fe80::1%25lo0]/chat", "[fe80::1]"})
45 for _, tt := range tests {
47 bw := bufio.NewWriter(&b)
48 br := bufio.NewReader(strings.NewReader(`HTTP/1.1 101 Switching Protocols
51 Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
52 Sec-WebSocket-Protocol: chat
57 config.Location, err = url.ParseRequestURI(tt.url)
59 t.Fatal("location url", err)
61 config.Origin, err = url.ParseRequestURI("http://example.com")
63 t.Fatal("origin url", err)
65 config.Protocol = append(config.Protocol, "chat")
66 config.Protocol = append(config.Protocol, "superchat")
67 config.Version = ProtocolVersionHybi13
68 config.handshakeData = map[string]string{
69 "key": "dGhlIHNhbXBsZSBub25jZQ==",
71 if err := hybiClientHandshake(&config, br, bw); err != nil {
72 t.Fatal("handshake", err)
74 req, err := http.ReadRequest(bufio.NewReader(&b))
76 t.Fatal("read request", err)
78 if req.Method != "GET" {
79 t.Errorf("request method expected GET, but got %s", req.Method)
81 if req.URL.Path != "/chat" {
82 t.Errorf("request path expected /chat, but got %s", req.URL.Path)
84 if req.Proto != "HTTP/1.1" {
85 t.Errorf("request proto expected HTTP/1.1, but got %s", req.Proto)
87 if req.Host != tt.host {
88 t.Errorf("request host expected %s, but got %s", tt.host, req.Host)
90 var expectedHeader = map[string]string{
91 "Connection": "Upgrade",
92 "Upgrade": "websocket",
93 "Sec-Websocket-Key": config.handshakeData["key"],
94 "Origin": config.Origin.String(),
95 "Sec-Websocket-Protocol": "chat, superchat",
96 "Sec-Websocket-Version": fmt.Sprintf("%d", ProtocolVersionHybi13),
98 for k, v := range expectedHeader {
99 if req.Header.Get(k) != v {
100 t.Errorf("%s expected %s, but got %v", k, v, req.Header.Get(k))
106 func TestHybiClientHandshakeWithHeader(t *testing.T) {
107 b := bytes.NewBuffer([]byte{})
108 bw := bufio.NewWriter(b)
109 br := bufio.NewReader(strings.NewReader(`HTTP/1.1 101 Switching Protocols
112 Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
113 Sec-WebSocket-Protocol: chat
117 config := new(Config)
118 config.Location, err = url.ParseRequestURI("ws://server.example.com/chat")
120 t.Fatal("location url", err)
122 config.Origin, err = url.ParseRequestURI("http://example.com")
124 t.Fatal("origin url", err)
126 config.Protocol = append(config.Protocol, "chat")
127 config.Protocol = append(config.Protocol, "superchat")
128 config.Version = ProtocolVersionHybi13
129 config.Header = http.Header(make(map[string][]string))
130 config.Header.Add("User-Agent", "test")
132 config.handshakeData = map[string]string{
133 "key": "dGhlIHNhbXBsZSBub25jZQ==",
135 err = hybiClientHandshake(config, br, bw)
137 t.Errorf("handshake failed: %v", err)
139 req, err := http.ReadRequest(bufio.NewReader(b))
141 t.Fatalf("read request: %v", err)
143 if req.Method != "GET" {
144 t.Errorf("request method expected GET, but got %q", req.Method)
146 if req.URL.Path != "/chat" {
147 t.Errorf("request path expected /chat, but got %q", req.URL.Path)
149 if req.Proto != "HTTP/1.1" {
150 t.Errorf("request proto expected HTTP/1.1, but got %q", req.Proto)
152 if req.Host != "server.example.com" {
153 t.Errorf("request Host expected server.example.com, but got %v", req.Host)
155 var expectedHeader = map[string]string{
156 "Connection": "Upgrade",
157 "Upgrade": "websocket",
158 "Sec-Websocket-Key": config.handshakeData["key"],
159 "Origin": config.Origin.String(),
160 "Sec-Websocket-Protocol": "chat, superchat",
161 "Sec-Websocket-Version": fmt.Sprintf("%d", ProtocolVersionHybi13),
162 "User-Agent": "test",
164 for k, v := range expectedHeader {
165 if req.Header.Get(k) != v {
166 t.Errorf(fmt.Sprintf("%s expected %q but got %q", k, v, req.Header.Get(k)))
171 func TestHybiServerHandshake(t *testing.T) {
172 config := new(Config)
173 handshaker := &hybiServerHandshaker{Config: config}
174 br := bufio.NewReader(strings.NewReader(`GET /chat HTTP/1.1
175 Host: server.example.com
178 Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
179 Origin: http://example.com
180 Sec-WebSocket-Protocol: chat, superchat
181 Sec-WebSocket-Version: 13
184 req, err := http.ReadRequest(br)
186 t.Fatal("request", err)
188 code, err := handshaker.ReadHandshake(br, req)
190 t.Errorf("handshake failed: %v", err)
192 if code != http.StatusSwitchingProtocols {
193 t.Errorf("status expected %q but got %q", http.StatusSwitchingProtocols, code)
195 expectedProtocols := []string{"chat", "superchat"}
196 if fmt.Sprintf("%v", config.Protocol) != fmt.Sprintf("%v", expectedProtocols) {
197 t.Errorf("protocol expected %q but got %q", expectedProtocols, config.Protocol)
199 b := bytes.NewBuffer([]byte{})
200 bw := bufio.NewWriter(b)
202 config.Protocol = config.Protocol[:1]
204 err = handshaker.AcceptHandshake(bw)
206 t.Errorf("handshake response failed: %v", err)
208 expectedResponse := strings.Join([]string{
209 "HTTP/1.1 101 Switching Protocols",
210 "Upgrade: websocket",
211 "Connection: Upgrade",
212 "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=",
213 "Sec-WebSocket-Protocol: chat",
216 if b.String() != expectedResponse {
217 t.Errorf("handshake expected %q but got %q", expectedResponse, b.String())
221 func TestHybiServerHandshakeNoSubProtocol(t *testing.T) {
222 config := new(Config)
223 handshaker := &hybiServerHandshaker{Config: config}
224 br := bufio.NewReader(strings.NewReader(`GET /chat HTTP/1.1
225 Host: server.example.com
228 Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
229 Origin: http://example.com
230 Sec-WebSocket-Version: 13
233 req, err := http.ReadRequest(br)
235 t.Fatal("request", err)
237 code, err := handshaker.ReadHandshake(br, req)
239 t.Errorf("handshake failed: %v", err)
241 if code != http.StatusSwitchingProtocols {
242 t.Errorf("status expected %q but got %q", http.StatusSwitchingProtocols, code)
244 if len(config.Protocol) != 0 {
245 t.Errorf("len(config.Protocol) expected 0, but got %q", len(config.Protocol))
247 b := bytes.NewBuffer([]byte{})
248 bw := bufio.NewWriter(b)
250 err = handshaker.AcceptHandshake(bw)
252 t.Errorf("handshake response failed: %v", err)
254 expectedResponse := strings.Join([]string{
255 "HTTP/1.1 101 Switching Protocols",
256 "Upgrade: websocket",
257 "Connection: Upgrade",
258 "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=",
261 if b.String() != expectedResponse {
262 t.Errorf("handshake expected %q but got %q", expectedResponse, b.String())
266 func TestHybiServerHandshakeHybiBadVersion(t *testing.T) {
267 config := new(Config)
268 handshaker := &hybiServerHandshaker{Config: config}
269 br := bufio.NewReader(strings.NewReader(`GET /chat HTTP/1.1
270 Host: server.example.com
273 Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
274 Sec-WebSocket-Origin: http://example.com
275 Sec-WebSocket-Protocol: chat, superchat
276 Sec-WebSocket-Version: 9
279 req, err := http.ReadRequest(br)
281 t.Fatal("request", err)
283 code, err := handshaker.ReadHandshake(br, req)
284 if err != ErrBadWebSocketVersion {
285 t.Errorf("handshake expected err %q but got %q", ErrBadWebSocketVersion, err)
287 if code != http.StatusBadRequest {
288 t.Errorf("status expected %q but got %q", http.StatusBadRequest, code)
292 func testHybiFrame(t *testing.T, testHeader, testPayload, testMaskedPayload []byte, frameHeader *hybiFrameHeader) {
293 b := bytes.NewBuffer([]byte{})
294 frameWriterFactory := &hybiFrameWriterFactory{bufio.NewWriter(b), false}
295 w, _ := frameWriterFactory.NewFrameWriter(TextFrame)
296 w.(*hybiFrameWriter).header = frameHeader
297 _, err := w.Write(testPayload)
300 t.Errorf("Write error %q", err)
302 var expectedFrame []byte
303 expectedFrame = append(expectedFrame, testHeader...)
304 expectedFrame = append(expectedFrame, testMaskedPayload...)
305 if !bytes.Equal(expectedFrame, b.Bytes()) {
306 t.Errorf("frame expected %q got %q", expectedFrame, b.Bytes())
308 frameReaderFactory := &hybiFrameReaderFactory{bufio.NewReader(b)}
309 r, err := frameReaderFactory.NewFrameReader()
311 t.Errorf("Read error %q", err)
313 if header := r.HeaderReader(); header == nil {
314 t.Errorf("no header")
316 actualHeader := make([]byte, r.Len())
317 n, err := header.Read(actualHeader)
319 t.Errorf("Read header error %q", err)
321 if n < len(testHeader) {
322 t.Errorf("header too short %q got %q", testHeader, actualHeader[:n])
324 if !bytes.Equal(testHeader, actualHeader[:n]) {
325 t.Errorf("header expected %q got %q", testHeader, actualHeader[:n])
329 if trailer := r.TrailerReader(); trailer != nil {
330 t.Errorf("unexpected trailer %q", trailer)
332 frame := r.(*hybiFrameReader)
333 if frameHeader.Fin != frame.header.Fin ||
334 frameHeader.OpCode != frame.header.OpCode ||
335 len(testPayload) != int(frame.header.Length) {
336 t.Errorf("mismatch %v (%d) vs %v", frameHeader, len(testPayload), frame)
338 payload := make([]byte, len(testPayload))
339 _, err = r.Read(payload)
340 if err != nil && err != io.EOF {
341 t.Errorf("read %v", err)
343 if !bytes.Equal(testPayload, payload) {
344 t.Errorf("payload %q vs %q", testPayload, payload)
348 func TestHybiShortTextFrame(t *testing.T) {
349 frameHeader := &hybiFrameHeader{Fin: true, OpCode: TextFrame}
350 payload := []byte("hello")
351 testHybiFrame(t, []byte{0x81, 0x05}, payload, payload, frameHeader)
353 payload = make([]byte, 125)
354 testHybiFrame(t, []byte{0x81, 125}, payload, payload, frameHeader)
357 func TestHybiShortMaskedTextFrame(t *testing.T) {
358 frameHeader := &hybiFrameHeader{Fin: true, OpCode: TextFrame,
359 MaskingKey: []byte{0xcc, 0x55, 0x80, 0x20}}
360 payload := []byte("hello")
361 maskedPayload := []byte{0xa4, 0x30, 0xec, 0x4c, 0xa3}
362 header := []byte{0x81, 0x85}
363 header = append(header, frameHeader.MaskingKey...)
364 testHybiFrame(t, header, payload, maskedPayload, frameHeader)
367 func TestHybiShortBinaryFrame(t *testing.T) {
368 frameHeader := &hybiFrameHeader{Fin: true, OpCode: BinaryFrame}
369 payload := []byte("hello")
370 testHybiFrame(t, []byte{0x82, 0x05}, payload, payload, frameHeader)
372 payload = make([]byte, 125)
373 testHybiFrame(t, []byte{0x82, 125}, payload, payload, frameHeader)
376 func TestHybiControlFrame(t *testing.T) {
377 payload := []byte("hello")
379 frameHeader := &hybiFrameHeader{Fin: true, OpCode: PingFrame}
380 testHybiFrame(t, []byte{0x89, 0x05}, payload, payload, frameHeader)
382 frameHeader = &hybiFrameHeader{Fin: true, OpCode: PingFrame}
383 testHybiFrame(t, []byte{0x89, 0x00}, nil, nil, frameHeader)
385 frameHeader = &hybiFrameHeader{Fin: true, OpCode: PongFrame}
386 testHybiFrame(t, []byte{0x8A, 0x05}, payload, payload, frameHeader)
388 frameHeader = &hybiFrameHeader{Fin: true, OpCode: PongFrame}
389 testHybiFrame(t, []byte{0x8A, 0x00}, nil, nil, frameHeader)
391 frameHeader = &hybiFrameHeader{Fin: true, OpCode: CloseFrame}
392 payload = []byte{0x03, 0xe8} // 1000
393 testHybiFrame(t, []byte{0x88, 0x02}, payload, payload, frameHeader)
396 func TestHybiLongFrame(t *testing.T) {
397 frameHeader := &hybiFrameHeader{Fin: true, OpCode: TextFrame}
398 payload := make([]byte, 126)
399 testHybiFrame(t, []byte{0x81, 126, 0x00, 126}, payload, payload, frameHeader)
401 payload = make([]byte, 65535)
402 testHybiFrame(t, []byte{0x81, 126, 0xff, 0xff}, payload, payload, frameHeader)
404 payload = make([]byte, 65536)
405 testHybiFrame(t, []byte{0x81, 127, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00}, payload, payload, frameHeader)
408 func TestHybiClientRead(t *testing.T) {
409 wireData := []byte{0x81, 0x05, 'h', 'e', 'l', 'l', 'o',
410 0x89, 0x05, 'h', 'e', 'l', 'l', 'o', // ping
411 0x81, 0x05, 'w', 'o', 'r', 'l', 'd'}
412 br := bufio.NewReader(bytes.NewBuffer(wireData))
413 bw := bufio.NewWriter(bytes.NewBuffer([]byte{}))
414 conn := newHybiConn(newConfig(t, "/"), bufio.NewReadWriter(br, bw), nil, nil)
416 msg := make([]byte, 512)
417 n, err := conn.Read(msg)
419 t.Errorf("read 1st frame, error %q", err)
422 t.Errorf("read 1st frame, expect 5, got %d", n)
424 if !bytes.Equal(wireData[2:7], msg[:n]) {
425 t.Errorf("read 1st frame %v, got %v", wireData[2:7], msg[:n])
427 n, err = conn.Read(msg)
429 t.Errorf("read 2nd frame, error %q", err)
432 t.Errorf("read 2nd frame, expect 5, got %d", n)
434 if !bytes.Equal(wireData[16:21], msg[:n]) {
435 t.Errorf("read 2nd frame %v, got %v", wireData[16:21], msg[:n])
437 n, err = conn.Read(msg)
439 t.Errorf("read not EOF")
442 t.Errorf("expect read 0, got %d", n)
446 func TestHybiShortRead(t *testing.T) {
447 wireData := []byte{0x81, 0x05, 'h', 'e', 'l', 'l', 'o',
448 0x89, 0x05, 'h', 'e', 'l', 'l', 'o', // ping
449 0x81, 0x05, 'w', 'o', 'r', 'l', 'd'}
450 br := bufio.NewReader(bytes.NewBuffer(wireData))
451 bw := bufio.NewWriter(bytes.NewBuffer([]byte{}))
452 conn := newHybiConn(newConfig(t, "/"), bufio.NewReadWriter(br, bw), nil, nil)
456 expectedPos := []int{2, 5, 16, 19}
457 expectedLen := []int{3, 2, 3, 2}
459 msg := make([]byte, 3)
460 n, err := conn.Read(msg)
461 if step >= len(expectedPos) {
463 t.Errorf("read not EOF")
466 t.Errorf("expect read 0, got %d", n)
470 pos = expectedPos[step]
471 endPos := pos + expectedLen[step]
473 t.Errorf("read from %d, got error %q", pos, err)
477 t.Errorf("read from %d, expect %d, got %d", pos, endPos-pos, n)
479 if !bytes.Equal(wireData[pos:endPos], msg[:n]) {
480 t.Errorf("read from %d, frame %v, got %v", pos, wireData[pos:endPos], msg[:n])
486 func TestHybiServerRead(t *testing.T) {
487 wireData := []byte{0x81, 0x85, 0xcc, 0x55, 0x80, 0x20,
488 0xa4, 0x30, 0xec, 0x4c, 0xa3, // hello
489 0x89, 0x85, 0xcc, 0x55, 0x80, 0x20,
490 0xa4, 0x30, 0xec, 0x4c, 0xa3, // ping: hello
491 0x81, 0x85, 0xed, 0x83, 0xb4, 0x24,
492 0x9a, 0xec, 0xc6, 0x48, 0x89, // world
494 br := bufio.NewReader(bytes.NewBuffer(wireData))
495 bw := bufio.NewWriter(bytes.NewBuffer([]byte{}))
496 conn := newHybiConn(newConfig(t, "/"), bufio.NewReadWriter(br, bw), nil, new(http.Request))
498 expected := [][]byte{[]byte("hello"), []byte("world")}
500 msg := make([]byte, 512)
501 n, err := conn.Read(msg)
503 t.Errorf("read 1st frame, error %q", err)
506 t.Errorf("read 1st frame, expect 5, got %d", n)
508 if !bytes.Equal(expected[0], msg[:n]) {
509 t.Errorf("read 1st frame %q, got %q", expected[0], msg[:n])
512 n, err = conn.Read(msg)
514 t.Errorf("read 2nd frame, error %q", err)
517 t.Errorf("read 2nd frame, expect 5, got %d", n)
519 if !bytes.Equal(expected[1], msg[:n]) {
520 t.Errorf("read 2nd frame %q, got %q", expected[1], msg[:n])
523 n, err = conn.Read(msg)
525 t.Errorf("read not EOF")
528 t.Errorf("expect read 0, got %d", n)
532 func TestHybiServerReadWithoutMasking(t *testing.T) {
533 wireData := []byte{0x81, 0x05, 'h', 'e', 'l', 'l', 'o'}
534 br := bufio.NewReader(bytes.NewBuffer(wireData))
535 bw := bufio.NewWriter(bytes.NewBuffer([]byte{}))
536 conn := newHybiConn(newConfig(t, "/"), bufio.NewReadWriter(br, bw), nil, new(http.Request))
537 // server MUST close the connection upon receiving a non-masked frame.
538 msg := make([]byte, 512)
539 _, err := conn.Read(msg)
541 t.Errorf("read 1st frame, expect %q, but got %q", io.EOF, err)
545 func TestHybiClientReadWithMasking(t *testing.T) {
546 wireData := []byte{0x81, 0x85, 0xcc, 0x55, 0x80, 0x20,
547 0xa4, 0x30, 0xec, 0x4c, 0xa3, // hello
549 br := bufio.NewReader(bytes.NewBuffer(wireData))
550 bw := bufio.NewWriter(bytes.NewBuffer([]byte{}))
551 conn := newHybiConn(newConfig(t, "/"), bufio.NewReadWriter(br, bw), nil, nil)
553 // client MUST close the connection upon receiving a masked frame.
554 msg := make([]byte, 512)
555 _, err := conn.Read(msg)
557 t.Errorf("read 1st frame, expect %q, but got %q", io.EOF, err)
561 // Test the hybiServerHandshaker supports firefox implementation and
562 // checks Connection request header include (but it's not necessary
563 // equal to) "upgrade"
564 func TestHybiServerFirefoxHandshake(t *testing.T) {
565 config := new(Config)
566 handshaker := &hybiServerHandshaker{Config: config}
567 br := bufio.NewReader(strings.NewReader(`GET /chat HTTP/1.1
568 Host: server.example.com
570 Connection: keep-alive, upgrade
571 Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
572 Origin: http://example.com
573 Sec-WebSocket-Protocol: chat, superchat
574 Sec-WebSocket-Version: 13
577 req, err := http.ReadRequest(br)
579 t.Fatal("request", err)
581 code, err := handshaker.ReadHandshake(br, req)
583 t.Errorf("handshake failed: %v", err)
585 if code != http.StatusSwitchingProtocols {
586 t.Errorf("status expected %q but got %q", http.StatusSwitchingProtocols, code)
588 b := bytes.NewBuffer([]byte{})
589 bw := bufio.NewWriter(b)
591 config.Protocol = []string{"chat"}
593 err = handshaker.AcceptHandshake(bw)
595 t.Errorf("handshake response failed: %v", err)
597 expectedResponse := strings.Join([]string{
598 "HTTP/1.1 101 Switching Protocols",
599 "Upgrade: websocket",
600 "Connection: Upgrade",
601 "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=",
602 "Sec-WebSocket-Protocol: chat",
605 if b.String() != expectedResponse {
606 t.Errorf("handshake expected %q but got %q", expectedResponse, b.String())