2009-11-29 15:22:44 -07:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package websocket
|
|
|
|
|
|
|
|
import (
|
2010-10-20 20:36:06 -06:00
|
|
|
"bufio"
|
2009-12-15 16:41:46 -07:00
|
|
|
"bytes"
|
2010-02-18 19:32:40 -07:00
|
|
|
"fmt"
|
2009-12-15 16:41:46 -07:00
|
|
|
"http"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net"
|
2010-08-05 14:14:41 -06:00
|
|
|
"sync"
|
2009-12-15 16:41:46 -07:00
|
|
|
"testing"
|
2009-11-29 15:22:44 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var serverAddr string
|
2010-08-05 14:14:41 -06:00
|
|
|
var once sync.Once
|
2009-11-29 15:22:44 -07:00
|
|
|
|
2009-12-15 16:41:46 -07:00
|
|
|
func echoServer(ws *Conn) { io.Copy(ws, ws) }
|
2009-11-29 15:22:44 -07:00
|
|
|
|
|
|
|
func startServer() {
|
2010-06-21 14:05:38 -06:00
|
|
|
l, e := net.Listen("tcp", "127.0.0.1:0") // any available address
|
2009-11-29 15:22:44 -07:00
|
|
|
if e != nil {
|
|
|
|
log.Exitf("net.Listen tcp :0 %v", e)
|
|
|
|
}
|
2009-12-15 16:41:46 -07:00
|
|
|
serverAddr = l.Addr().String()
|
log: new interface
New logging interface simplifies and generalizes.
1) Loggers now have only one output.
2) log.Stdout, Stderr, Crash and friends are gone.
Logging is now always to standard error by default.
3) log.Panic* replaces log.Crash*.
4) Exiting and panicking are not part of the logger's state; instead
the functions Exit* and Panic* simply call Exit or panic after
printing.
5) There is now one 'standard logger'. Instead of calling Stderr,
use Print etc. There are now triples, by analogy with fmt:
Print, Println, Printf
What was log.Stderr is now best represented by log.Println,
since there are now separate Print and Println functions
(and methods).
6) New functions SetOutput, SetFlags, and SetPrefix allow global
editing of the standard logger's properties. This is new
functionality. For instance, one can call
log.SetFlags(log.Lshortfile|log.Ltime|log.Lmicroseconds)
to get all logging output to show file name, line number, and
time stamp.
In short, for most purposes
log.Stderr -> log.Println or log.Print
log.Stderrf -> log.Printf
log.Crash -> log.Panicln or log.Panic
log.Crashf -> log.Panicf
log.Exit -> log.Exitln or log.Exit
log.Exitf -> log.Exitf (no change)
This has a slight breakage: since loggers now write only to one
output, existing calls to log.New() need to delete the second argument.
Also, custom loggers with exit or panic properties will need to be
reworked.
All package code updated to new interface.
The test has been reworked somewhat.
The old interface will be removed after the new release.
For now, its elements are marked 'deprecated' in their comments.
Fixes #1184.
R=rsc
CC=golang-dev
https://golang.org/cl/2419042
2010-10-12 13:59:18 -06:00
|
|
|
log.Print("Test WebSocket server listening on ", serverAddr)
|
2009-12-15 16:41:46 -07:00
|
|
|
http.Handle("/echo", Handler(echoServer))
|
2010-03-23 19:09:24 -06:00
|
|
|
http.Handle("/echoDraft75", Draft75Handler(echoServer))
|
2009-12-15 16:41:46 -07:00
|
|
|
go http.Serve(l, nil)
|
2009-11-29 15:22:44 -07:00
|
|
|
}
|
|
|
|
|
2010-08-03 15:34:44 -06:00
|
|
|
// Test the getChallengeResponse function with values from section
|
|
|
|
// 5.1 of the specification steps 18, 26, and 43 from
|
|
|
|
// http://www.whatwg.org/specs/web-socket-protocol/
|
|
|
|
func TestChallenge(t *testing.T) {
|
|
|
|
var part1 uint32 = 777007543
|
|
|
|
var part2 uint32 = 114997259
|
|
|
|
key3 := []byte{0x47, 0x30, 0x22, 0x2D, 0x5A, 0x3F, 0x47, 0x58}
|
|
|
|
expected := []byte("0st3Rl&q-2ZU^weu")
|
|
|
|
|
|
|
|
response, err := getChallengeResponse(part1, part2, key3)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("getChallengeResponse: returned error %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !bytes.Equal(expected, response) {
|
|
|
|
t.Errorf("getChallengeResponse: expected %q got %q", expected, response)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-29 15:22:44 -07:00
|
|
|
func TestEcho(t *testing.T) {
|
2009-12-15 16:41:46 -07:00
|
|
|
once.Do(startServer)
|
2009-11-29 15:22:44 -07:00
|
|
|
|
2010-03-23 19:09:24 -06:00
|
|
|
// websocket.Dial()
|
2009-12-15 16:41:46 -07:00
|
|
|
client, err := net.Dial("tcp", "", serverAddr)
|
2009-11-29 15:22:44 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("dialing", err)
|
|
|
|
}
|
|
|
|
ws, err := newClient("/echo", "localhost", "http://localhost",
|
2010-03-23 19:09:24 -06:00
|
|
|
"ws://localhost/echo", "", client, handshake)
|
|
|
|
if err != nil {
|
2010-03-30 11:51:11 -06:00
|
|
|
t.Errorf("WebSocket handshake error: %v", err)
|
2010-03-23 19:09:24 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := []byte("hello, world\n")
|
|
|
|
if _, err := ws.Write(msg); err != nil {
|
2010-03-30 11:51:11 -06:00
|
|
|
t.Errorf("Write: %v", err)
|
2010-03-23 19:09:24 -06:00
|
|
|
}
|
|
|
|
var actual_msg = make([]byte, 512)
|
|
|
|
n, err := ws.Read(actual_msg)
|
|
|
|
if err != nil {
|
2010-03-30 11:51:11 -06:00
|
|
|
t.Errorf("Read: %v", err)
|
2010-03-23 19:09:24 -06:00
|
|
|
}
|
|
|
|
actual_msg = actual_msg[0:n]
|
|
|
|
if !bytes.Equal(msg, actual_msg) {
|
|
|
|
t.Errorf("Echo: expected %q got %q", msg, actual_msg)
|
|
|
|
}
|
|
|
|
ws.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEchoDraft75(t *testing.T) {
|
|
|
|
once.Do(startServer)
|
|
|
|
|
|
|
|
// websocket.Dial()
|
|
|
|
client, err := net.Dial("tcp", "", serverAddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("dialing", err)
|
|
|
|
}
|
|
|
|
ws, err := newClient("/echoDraft75", "localhost", "http://localhost",
|
|
|
|
"ws://localhost/echoDraft75", "", client, draft75handshake)
|
2009-11-29 15:22:44 -07:00
|
|
|
if err != nil {
|
2010-03-30 11:51:11 -06:00
|
|
|
t.Errorf("WebSocket handshake: %v", err)
|
2009-12-15 16:41:46 -07:00
|
|
|
return
|
2009-11-29 15:22:44 -07:00
|
|
|
}
|
2010-03-23 19:09:24 -06:00
|
|
|
|
2010-02-25 17:01:29 -07:00
|
|
|
msg := []byte("hello, world\n")
|
2009-11-29 15:22:44 -07:00
|
|
|
if _, err := ws.Write(msg); err != nil {
|
|
|
|
t.Errorf("Write: error %v", err)
|
|
|
|
}
|
2009-12-15 16:41:46 -07:00
|
|
|
var actual_msg = make([]byte, 512)
|
|
|
|
n, err := ws.Read(actual_msg)
|
2009-11-29 15:22:44 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Read: error %v", err)
|
|
|
|
}
|
2009-12-15 16:41:46 -07:00
|
|
|
actual_msg = actual_msg[0:n]
|
2009-11-29 15:22:44 -07:00
|
|
|
if !bytes.Equal(msg, actual_msg) {
|
|
|
|
t.Errorf("Echo: expected %q got %q", msg, actual_msg)
|
|
|
|
}
|
2009-12-15 16:41:46 -07:00
|
|
|
ws.Close()
|
2009-11-29 15:22:44 -07:00
|
|
|
}
|
2010-02-18 19:32:40 -07:00
|
|
|
|
2010-03-19 15:18:02 -06:00
|
|
|
func TestWithQuery(t *testing.T) {
|
|
|
|
once.Do(startServer)
|
|
|
|
|
|
|
|
client, err := net.Dial("tcp", "", serverAddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("dialing", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ws, err := newClient("/echo?q=v", "localhost", "http://localhost",
|
2010-03-23 19:09:24 -06:00
|
|
|
"ws://localhost/echo?q=v", "", client, handshake)
|
2010-03-19 15:18:02 -06:00
|
|
|
if err != nil {
|
2010-03-30 11:51:11 -06:00
|
|
|
t.Errorf("WebSocket handshake: %v", err)
|
2010-03-19 15:18:02 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ws.Close()
|
|
|
|
}
|
|
|
|
|
2010-08-25 10:52:04 -06:00
|
|
|
func TestWithProtocol(t *testing.T) {
|
|
|
|
once.Do(startServer)
|
|
|
|
|
|
|
|
client, err := net.Dial("tcp", "", serverAddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("dialing", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ws, err := newClient("/echo", "localhost", "http://localhost",
|
|
|
|
"ws://localhost/echo", "test", client, handshake)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("WebSocket handshake: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ws.Close()
|
|
|
|
}
|
|
|
|
|
2010-02-18 19:32:40 -07:00
|
|
|
func TestHTTP(t *testing.T) {
|
|
|
|
once.Do(startServer)
|
|
|
|
|
2010-03-23 19:09:24 -06:00
|
|
|
// If the client did not send a handshake that matches the protocol
|
|
|
|
// specification, the server should abort the WebSocket connection.
|
|
|
|
_, _, err := http.Get(fmt.Sprintf("http://%s/echo", serverAddr))
|
|
|
|
if err == nil {
|
2010-12-07 14:42:54 -07:00
|
|
|
t.Error("Get: unexpected success")
|
2010-03-23 19:09:24 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
urlerr, ok := err.(*http.URLError)
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("Get: not URLError %#v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if urlerr.Error != io.ErrUnexpectedEOF {
|
|
|
|
t.Errorf("Get: error %#v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHTTPDraft75(t *testing.T) {
|
|
|
|
once.Do(startServer)
|
|
|
|
|
|
|
|
r, _, err := http.Get(fmt.Sprintf("http://%s/echoDraft75", serverAddr))
|
2010-02-18 19:32:40 -07:00
|
|
|
if err != nil {
|
2010-03-23 19:09:24 -06:00
|
|
|
t.Errorf("Get: error %#v", err)
|
2010-02-18 19:32:40 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if r.StatusCode != http.StatusBadRequest {
|
|
|
|
t.Errorf("Get: got status %d", r.StatusCode)
|
|
|
|
}
|
|
|
|
}
|
2010-07-30 13:27:03 -06:00
|
|
|
|
|
|
|
func TestTrailingSpaces(t *testing.T) {
|
|
|
|
// http://code.google.com/p/go/issues/detail?id=955
|
|
|
|
// The last runs of this create keys with trailing spaces that should not be
|
|
|
|
// generated by the client.
|
|
|
|
once.Do(startServer)
|
|
|
|
for i := 0; i < 30; i++ {
|
|
|
|
// body
|
|
|
|
_, err := Dial(fmt.Sprintf("ws://%s/echo", serverAddr), "",
|
|
|
|
"http://localhost/")
|
|
|
|
if err != nil {
|
|
|
|
panic("Dial failed: " + err.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-10-20 20:36:06 -06:00
|
|
|
|
|
|
|
func TestSmallBuffer(t *testing.T) {
|
|
|
|
// http://code.google.com/p/go/issues/detail?id=1145
|
|
|
|
// Read should be able to handle reading a fragment of a frame.
|
|
|
|
once.Do(startServer)
|
|
|
|
|
|
|
|
// websocket.Dial()
|
|
|
|
client, err := net.Dial("tcp", "", serverAddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("dialing", err)
|
|
|
|
}
|
|
|
|
ws, err := newClient("/echo", "localhost", "http://localhost",
|
|
|
|
"ws://localhost/echo", "", client, handshake)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("WebSocket handshake error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := []byte("hello, world\n")
|
|
|
|
if _, err := ws.Write(msg); err != nil {
|
|
|
|
t.Errorf("Write: %v", err)
|
|
|
|
}
|
|
|
|
var small_msg = make([]byte, 8)
|
|
|
|
n, err := ws.Read(small_msg)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Read: %v", err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(msg[:len(small_msg)], small_msg) {
|
|
|
|
t.Errorf("Echo: expected %q got %q", msg[:len(small_msg)], small_msg)
|
|
|
|
}
|
|
|
|
var second_msg = make([]byte, len(msg))
|
|
|
|
n, err = ws.Read(second_msg)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Read: %v", err)
|
|
|
|
}
|
|
|
|
second_msg = second_msg[0:n]
|
|
|
|
if !bytes.Equal(msg[len(small_msg):], second_msg) {
|
|
|
|
t.Errorf("Echo: expected %q got %q", msg[len(small_msg):], second_msg)
|
|
|
|
}
|
|
|
|
ws.Close()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSkipLengthFrame(t *testing.T) {
|
|
|
|
b := []byte{'\x80', '\x01', 'x', 0, 'h', 'e', 'l', 'l', 'o', '\xff'}
|
|
|
|
buf := bytes.NewBuffer(b)
|
|
|
|
br := bufio.NewReader(buf)
|
|
|
|
bw := bufio.NewWriter(buf)
|
|
|
|
ws := newConn("http://127.0.0.1/", "ws://127.0.0.1/", "", bufio.NewReadWriter(br, bw), nil)
|
|
|
|
msg := make([]byte, 5)
|
|
|
|
n, err := ws.Read(msg)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Read: %v", err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(b[4:8], msg[0:n]) {
|
|
|
|
t.Errorf("Read: expected %q got %q", msg[4:8], msg[0:n])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSkipNoUTF8Frame(t *testing.T) {
|
|
|
|
b := []byte{'\x01', 'n', '\xff', 0, 'h', 'e', 'l', 'l', 'o', '\xff'}
|
|
|
|
buf := bytes.NewBuffer(b)
|
|
|
|
br := bufio.NewReader(buf)
|
|
|
|
bw := bufio.NewWriter(buf)
|
|
|
|
ws := newConn("http://127.0.0.1/", "ws://127.0.0.1/", "", bufio.NewReadWriter(br, bw), nil)
|
|
|
|
msg := make([]byte, 5)
|
|
|
|
n, err := ws.Read(msg)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Read: %v", err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(b[4:8], msg[0:n]) {
|
|
|
|
t.Errorf("Read: expected %q got %q", msg[4:8], msg[0:n])
|
|
|
|
}
|
|
|
|
}
|