2018-05-05 22:18:01 +00:00
|
|
|
package ws
|
|
|
|
|
|
|
|
import (
|
2018-05-06 12:45:44 +00:00
|
|
|
"io"
|
2018-05-05 22:18:01 +00:00
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func TestSimpleMessageReading(t *testing.T) {
|
|
|
|
|
|
|
|
cases := []struct{
|
2018-05-06 12:45:44 +00:00
|
|
|
Name string
|
2018-05-05 22:18:01 +00:00
|
|
|
ReadBuffer []byte
|
|
|
|
Expected Message
|
|
|
|
Err error
|
|
|
|
}{
|
|
|
|
{ // FIN ; TEXT ; Unmasked -> error
|
2018-05-06 12:45:44 +00:00
|
|
|
"must fail on unmasked frame",
|
2018-05-05 22:18:01 +00:00
|
|
|
[]byte{0x81,0x05,0x68,0x65,0x6c,0x6c,0x6f},
|
|
|
|
Message{},
|
|
|
|
UnmaskedFrameErr,
|
|
|
|
},
|
|
|
|
{ // FIN ; TEXT ; hello
|
2018-05-06 12:45:44 +00:00
|
|
|
"simple hello text message",
|
2018-05-05 22:18:01 +00:00
|
|
|
[]byte{0x81,0x85,0x00,0x00,0x00,0x00,0x68,0x65,0x6c,0x6c,0x6f},
|
|
|
|
Message{ true, TEXT, 5, []byte("hello") },
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{ // FIN ; BINARY ; hello
|
2018-05-06 12:45:44 +00:00
|
|
|
"simple hello binary message",
|
2018-05-05 22:18:01 +00:00
|
|
|
[]byte{0x82,0x85,0x00,0x00,0x00,0x00,0x68,0x65,0x6c,0x6c,0x6f},
|
|
|
|
Message{ true, BINARY, 5, []byte("hello") },
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{ // FIN ; BINARY ; test unmasking
|
2018-05-06 12:45:44 +00:00
|
|
|
"unmasking test",
|
2018-05-05 22:18:01 +00:00
|
|
|
[]byte{0x82,0x88,0x01,0x02,0x03,0x04,0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x80},
|
|
|
|
Message{ true, BINARY, 8, []byte{0x11,0x22,0x33,0x44,0x51,0x62,0x73,0x84} },
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{ // FIN=0 ; TEXT ;
|
2018-05-06 12:45:44 +00:00
|
|
|
"non final frame",
|
2018-05-05 22:18:01 +00:00
|
|
|
[]byte{0x01,0x82,0x00,0x00,0x00,0x00,0x01,0x02},
|
|
|
|
Message{ false, TEXT, 2, []byte{0x01,0x02} },
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-05-06 12:45:44 +00:00
|
|
|
for _, tc := range cases{
|
2018-05-05 22:18:01 +00:00
|
|
|
|
2018-05-06 12:45:44 +00:00
|
|
|
t.Run(tc.Name, func(t *testing.T){
|
2018-05-05 22:18:01 +00:00
|
|
|
|
2018-05-06 12:45:44 +00:00
|
|
|
reader := bytes.NewBuffer(tc.ReadBuffer)
|
2018-05-05 22:18:01 +00:00
|
|
|
|
2018-05-06 12:45:44 +00:00
|
|
|
got, err := readMessage(reader)
|
2018-05-05 22:18:01 +00:00
|
|
|
|
2018-05-06 12:45:44 +00:00
|
|
|
if err != tc.Err {
|
|
|
|
t.Errorf("Expected %v error, got %v", tc.Err, err)
|
|
|
|
}
|
2018-05-05 22:18:01 +00:00
|
|
|
|
2018-05-06 12:45:44 +00:00
|
|
|
// do not check message if error expected
|
|
|
|
if tc.Err != nil {
|
|
|
|
return
|
|
|
|
}
|
2018-05-05 22:18:01 +00:00
|
|
|
|
2018-05-06 12:45:44 +00:00
|
|
|
// check FIN
|
|
|
|
if got.Final != tc.Expected.Final {
|
|
|
|
t.Errorf("Expected FIN=%t, got %t", tc.Expected.Final, got.Final)
|
|
|
|
}
|
2018-05-05 22:18:01 +00:00
|
|
|
|
2018-05-06 12:45:44 +00:00
|
|
|
// check OpCode
|
|
|
|
if got.Type != tc.Expected.Type {
|
|
|
|
t.Errorf("Expected TYPE=%x, got %x", tc.Expected.Type, got.Type)
|
|
|
|
}
|
2018-05-05 22:18:01 +00:00
|
|
|
|
2018-05-06 12:45:44 +00:00
|
|
|
// check Size
|
|
|
|
if got.Size != tc.Expected.Size {
|
|
|
|
t.Errorf("Expected SIZE=%d, got %d", tc.Expected.Size, got.Size)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check Data
|
|
|
|
if string(got.Data) != string(tc.Expected.Data) {
|
|
|
|
t.Errorf("Expected Data='%s', got '%d'", tc.Expected.Data, got.Data)
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestReadEOF(t *testing.T) {
|
|
|
|
|
|
|
|
cases := []struct{
|
|
|
|
Name string
|
|
|
|
ReadBuffer []byte
|
|
|
|
eof bool
|
|
|
|
unmaskedError bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"no byte",
|
|
|
|
[]byte{},
|
|
|
|
true, false,
|
|
|
|
}, {
|
|
|
|
"only opcode",
|
|
|
|
[]byte{0x82},
|
|
|
|
true, false,
|
|
|
|
}, {
|
|
|
|
"only opcode and 0 length",
|
|
|
|
[]byte{0x82,0x00},
|
|
|
|
false, true,
|
|
|
|
}, {
|
|
|
|
"missing extended 16 bits length",
|
|
|
|
[]byte{0x82,126},
|
|
|
|
true, false,
|
|
|
|
}, {
|
|
|
|
"incomplete extended 16 bits length",
|
|
|
|
[]byte{0x82,126, 0x00},
|
|
|
|
true, false,
|
|
|
|
}, {
|
|
|
|
"complete extended 16 bits length",
|
|
|
|
[]byte{0x82,126, 0x00, 0x00},
|
|
|
|
false, true,
|
|
|
|
}, {
|
|
|
|
"missing extended 64 bits length",
|
|
|
|
[]byte{0x82,127},
|
|
|
|
true, false,
|
|
|
|
}, {
|
|
|
|
"incomplete extended 64 bits length",
|
|
|
|
[]byte{0x82,127, 0x00},
|
|
|
|
true, false,
|
|
|
|
}, {
|
|
|
|
"incomplete extended 64 bits length",
|
|
|
|
[]byte{0x82,127, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
|
|
|
true, false,
|
|
|
|
}, {
|
|
|
|
"complete extended 64 bits length",
|
|
|
|
[]byte{0x82,127, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
|
|
|
false, true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases{
|
|
|
|
|
|
|
|
t.Run(tc.Name, func(t *testing.T){
|
|
|
|
|
|
|
|
reader := bytes.NewBuffer(tc.ReadBuffer)
|
|
|
|
|
|
|
|
got, err := readMessage(reader)
|
|
|
|
|
|
|
|
if tc.eof {
|
|
|
|
|
|
|
|
if err != io.EOF{
|
|
|
|
t.Errorf("Expected EOF, got %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if tc.unmaskedError && err != UnmaskedFrameErr {
|
|
|
|
t.Errorf("Expected UnmaskedFrameError, got %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got.Size != 0x00 {
|
|
|
|
t.Errorf("Expected a size of 0, got %d", got.Size)
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
2018-05-05 22:28:02 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func TestSimpleMessageSending(t *testing.T) {
|
|
|
|
|
|
|
|
cases := []struct{
|
2018-05-06 12:45:44 +00:00
|
|
|
Name string
|
|
|
|
Base Message
|
|
|
|
Expected []byte
|
2018-05-05 22:28:02 +00:00
|
|
|
}{
|
2018-05-06 12:45:44 +00:00
|
|
|
{
|
|
|
|
"simple hello text message",
|
2018-05-05 22:28:02 +00:00
|
|
|
Message{ true, TEXT, 5, []byte("hello") },
|
|
|
|
[]byte{0x81,0x05,0x68,0x65,0x6c,0x6c,0x6f},
|
2018-05-06 12:45:44 +00:00
|
|
|
}, {
|
|
|
|
"simple hello binary message",
|
2018-05-05 22:28:02 +00:00
|
|
|
Message{ true, BINARY, 5, []byte("hello") },
|
|
|
|
[]byte{0x82,0x05,0x68,0x65,0x6c,0x6c,0x6f},
|
2018-05-06 12:45:44 +00:00
|
|
|
}, {
|
|
|
|
"other simple binary message",
|
2018-05-05 22:28:02 +00:00
|
|
|
Message{ true, BINARY, 8, []byte{0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x80} },
|
|
|
|
[]byte{0x82,0x08,0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x80},
|
2018-05-06 12:45:44 +00:00
|
|
|
}, {
|
|
|
|
"non final frame",
|
2018-05-05 22:28:02 +00:00
|
|
|
Message{ false, TEXT, 2, []byte{0x01,0x02} },
|
|
|
|
[]byte{0x01,0x02,0x01,0x02},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-05-06 12:45:44 +00:00
|
|
|
for _, tc := range cases{
|
|
|
|
|
|
|
|
t.Run(tc.Name, func(t *testing.T){
|
|
|
|
|
|
|
|
writer := new(bytes.Buffer)
|
2018-05-05 22:28:02 +00:00
|
|
|
|
2018-05-06 12:45:44 +00:00
|
|
|
err := tc.Base.Send(writer)
|
2018-05-05 22:28:02 +00:00
|
|
|
|
2018-05-06 12:45:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("expected no error, got %v", err)
|
|
|
|
return
|
|
|
|
}
|
2018-05-05 22:28:02 +00:00
|
|
|
|
2018-05-06 12:45:44 +00:00
|
|
|
// check buffer
|
|
|
|
if writer.String() != string(tc.Expected) {
|
|
|
|
t.Errorf("expected '%x', got '%x'", tc.Expected, writer.String())
|
|
|
|
}
|
2018-05-05 22:28:02 +00:00
|
|
|
|
2018-05-06 12:45:44 +00:00
|
|
|
})
|
2018-05-05 22:18:01 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|