2018-09-29 12:36:47 +00:00
|
|
|
package websocket
|
2018-05-05 22:18:01 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-09-29 12:36:47 +00:00
|
|
|
"io"
|
2018-05-05 22:18:01 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSimpleMessageReading(t *testing.T) {
|
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
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-09-29 12:36:47 +00:00
|
|
|
[]byte{0x81, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f},
|
2018-05-05 22:18:01 +00:00
|
|
|
Message{},
|
2018-05-08 09:15:01 +00:00
|
|
|
ErrUnmaskedFrame,
|
2018-05-05 22:18:01 +00:00
|
|
|
},
|
2018-05-08 15:21:49 +00:00
|
|
|
{ // FIN ; TEXT ; Unmasked -> error
|
|
|
|
"must fail because of RSV bit 1 set",
|
2018-09-29 12:36:47 +00:00
|
|
|
[]byte{0x81 | 0x40, 0x10, 0x00, 0x00, 0x00, 0x00},
|
2018-05-08 15:21:49 +00:00
|
|
|
Message{},
|
|
|
|
ErrReservedBits,
|
|
|
|
},
|
|
|
|
{ // FIN ; TEXT ; Unmasked -> error
|
|
|
|
"must fail because of RSV bit 2 set",
|
2018-09-29 12:36:47 +00:00
|
|
|
[]byte{0x81 | 0x20, 0x10, 0x00, 0x00, 0x00, 0x00},
|
2018-05-08 15:21:49 +00:00
|
|
|
Message{},
|
|
|
|
ErrReservedBits,
|
|
|
|
},
|
|
|
|
{ // FIN ; TEXT ; Unmasked -> error
|
|
|
|
"must fail because of RSV bit 3 set",
|
2018-09-29 12:36:47 +00:00
|
|
|
[]byte{0x81 | 0x10, 0x10, 0x00, 0x00, 0x00, 0x00},
|
2018-05-08 15:21:49 +00:00
|
|
|
Message{},
|
|
|
|
ErrReservedBits,
|
|
|
|
},
|
2018-05-05 22:18:01 +00:00
|
|
|
{ // FIN ; TEXT ; hello
|
2018-05-06 12:45:44 +00:00
|
|
|
"simple hello text message",
|
2018-09-29 12:36:47 +00:00
|
|
|
[]byte{0x81, 0x85, 0x00, 0x00, 0x00, 0x00, 0x68, 0x65, 0x6c, 0x6c, 0x6f},
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Text, 5, []byte("hello")},
|
2018-05-05 22:18:01 +00:00
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{ // FIN ; BINARY ; hello
|
2018-05-06 12:45:44 +00:00
|
|
|
"simple hello binary message",
|
2018-09-29 12:36:47 +00:00
|
|
|
[]byte{0x82, 0x85, 0x00, 0x00, 0x00, 0x00, 0x68, 0x65, 0x6c, 0x6c, 0x6f},
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Binary, 5, []byte("hello")},
|
2018-05-05 22:18:01 +00:00
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{ // FIN ; BINARY ; test unmasking
|
2018-05-06 12:45:44 +00:00
|
|
|
"unmasking test",
|
2018-09-29 12:36:47 +00:00
|
|
|
[]byte{0x82, 0x88, 0x01, 0x02, 0x03, 0x04, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80},
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Binary, 8, []byte{0x11, 0x22, 0x33, 0x44, 0x51, 0x62, 0x73, 0x84}},
|
2018-05-05 22:18:01 +00:00
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{ // FIN=0 ; TEXT ;
|
2018-05-06 12:45:44 +00:00
|
|
|
"non final frame",
|
2018-09-29 12:36:47 +00:00
|
|
|
[]byte{0x01, 0x82, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02},
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{false, Text, 2, []byte{0x01, 0x02}},
|
2018-05-05 22:18:01 +00:00
|
|
|
nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
for _, tc := range cases {
|
2018-05-05 22:18:01 +00:00
|
|
|
|
2018-09-29 12:36:47 +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) {
|
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
cases := []struct {
|
2018-05-06 12:45:44 +00:00
|
|
|
Name string
|
|
|
|
ReadBuffer []byte
|
|
|
|
eof bool
|
|
|
|
unmaskedError bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"no byte",
|
|
|
|
[]byte{},
|
|
|
|
true, false,
|
|
|
|
}, {
|
|
|
|
"only opcode",
|
|
|
|
[]byte{0x82},
|
|
|
|
true, false,
|
|
|
|
}, {
|
|
|
|
"only opcode and 0 length",
|
2018-09-29 12:36:47 +00:00
|
|
|
[]byte{0x82, 0x00},
|
2018-05-06 12:45:44 +00:00
|
|
|
false, true,
|
|
|
|
}, {
|
|
|
|
"missing extended 16 bits length",
|
2018-09-29 12:36:47 +00:00
|
|
|
[]byte{0x82, 126},
|
2018-05-06 12:45:44 +00:00
|
|
|
true, false,
|
|
|
|
}, {
|
|
|
|
"incomplete extended 16 bits length",
|
2018-09-29 12:36:47 +00:00
|
|
|
[]byte{0x82, 126, 0x00},
|
2018-05-06 12:45:44 +00:00
|
|
|
true, false,
|
|
|
|
}, {
|
|
|
|
"complete extended 16 bits length",
|
2018-09-29 12:36:47 +00:00
|
|
|
[]byte{0x82, 126, 0x00, 0x00},
|
2018-05-06 12:45:44 +00:00
|
|
|
false, true,
|
|
|
|
}, {
|
|
|
|
"missing extended 64 bits length",
|
2018-09-29 12:36:47 +00:00
|
|
|
[]byte{0x82, 127},
|
2018-05-06 12:45:44 +00:00
|
|
|
true, false,
|
|
|
|
}, {
|
|
|
|
"incomplete extended 64 bits length",
|
2018-09-29 12:36:47 +00:00
|
|
|
[]byte{0x82, 127, 0x00},
|
2018-05-06 12:45:44 +00:00
|
|
|
true, false,
|
|
|
|
}, {
|
|
|
|
"incomplete extended 64 bits length",
|
2018-09-29 12:36:47 +00:00
|
|
|
[]byte{0x82, 127, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
2018-05-06 12:45:44 +00:00
|
|
|
true, false,
|
|
|
|
}, {
|
|
|
|
"complete extended 64 bits length",
|
2018-09-29 12:36:47 +00:00
|
|
|
[]byte{0x82, 127, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
2018-05-06 12:45:44 +00:00
|
|
|
false, true,
|
2018-05-06 12:47:34 +00:00
|
|
|
}, {
|
|
|
|
"missing mask",
|
2018-09-29 12:36:47 +00:00
|
|
|
[]byte{0x82, 0x80},
|
2018-05-06 12:47:34 +00:00
|
|
|
true, false,
|
|
|
|
}, {
|
|
|
|
"incomplete mask 1",
|
2018-09-29 12:36:47 +00:00
|
|
|
[]byte{0x82, 0x80, 0x00},
|
2018-05-06 12:47:34 +00:00
|
|
|
true, false,
|
|
|
|
}, {
|
|
|
|
"incomplete mask 2",
|
2018-09-29 12:36:47 +00:00
|
|
|
[]byte{0x82, 0x80, 0x00, 0x00, 0x00},
|
2018-05-06 12:47:34 +00:00
|
|
|
true, false,
|
2018-09-29 12:36:47 +00:00
|
|
|
}, {
|
2018-05-06 12:47:34 +00:00
|
|
|
"complete mask",
|
2018-09-29 12:36:47 +00:00
|
|
|
[]byte{0x82, 0x80, 0x00, 0x00, 0x00, 0x00},
|
2018-05-06 12:47:34 +00:00
|
|
|
false, false,
|
2018-05-06 12:45:44 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
for _, tc := range cases {
|
2018-05-06 12:45:44 +00:00
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
2018-05-06 12:45:44 +00:00
|
|
|
|
|
|
|
reader := bytes.NewBuffer(tc.ReadBuffer)
|
|
|
|
|
|
|
|
got, err := readMessage(reader)
|
|
|
|
|
|
|
|
if tc.eof {
|
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
if err != io.EOF {
|
2018-05-06 12:45:44 +00:00
|
|
|
t.Errorf("Expected EOF, got %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-08 09:15:01 +00:00
|
|
|
if tc.unmaskedError && err != ErrUnmaskedFrame {
|
|
|
|
t.Errorf("Expected UnmaskedFrameor, got %v", err)
|
2018-05-06 12:45:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
m4b1 := make([]byte, 0x7e-1)
|
2018-05-07 17:25:54 +00:00
|
|
|
m4b2 := make([]byte, 0x7e)
|
2018-09-29 12:36:47 +00:00
|
|
|
m4b3 := make([]byte, 0x7e+1)
|
2018-05-07 17:25:54 +00:00
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
m16b1 := make([]byte, 0xffff-1)
|
2018-05-07 17:25:54 +00:00
|
|
|
m16b2 := make([]byte, 0xffff)
|
2018-09-29 12:36:47 +00:00
|
|
|
m16b3 := make([]byte, 0xffff+1)
|
2018-05-07 17:25:54 +00:00
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
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",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Text, 5, []byte("hello")},
|
2018-09-29 12:36:47 +00:00
|
|
|
[]byte{0x81, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f},
|
2018-05-06 12:45:44 +00:00
|
|
|
}, {
|
|
|
|
"simple hello binary message",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Binary, 5, []byte("hello")},
|
2018-09-29 12:36:47 +00:00
|
|
|
[]byte{0x82, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f},
|
2018-05-06 12:45:44 +00:00
|
|
|
}, {
|
|
|
|
"other simple binary message",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Binary, 8, []byte{0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80}},
|
2018-09-29 12:36:47 +00:00
|
|
|
[]byte{0x82, 0x08, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80},
|
2018-05-06 12:45:44 +00:00
|
|
|
}, {
|
|
|
|
"non final frame",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{false, Text, 2, []byte{0x01, 0x02}},
|
2018-09-29 12:36:47 +00:00
|
|
|
[]byte{0x01, 0x02, 0x01, 0x02},
|
2018-05-07 17:25:54 +00:00
|
|
|
}, {
|
|
|
|
"125 > normal length",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Text, uint(len(m4b1)), m4b1},
|
2018-09-29 12:36:47 +00:00
|
|
|
append([]byte{0x81, 0x7e - 1}, m4b1...),
|
2018-05-07 17:25:54 +00:00
|
|
|
}, {
|
|
|
|
"126 > extended 16 bits length",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Text, uint(len(m4b2)), m4b2},
|
2018-09-29 12:36:47 +00:00
|
|
|
append([]byte{0x81, 126, 0x00, 0x7e}, m4b2...),
|
2018-05-07 17:25:54 +00:00
|
|
|
}, {
|
|
|
|
"127 > extended 16 bits length",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Text, uint(len(m4b3)), m4b3},
|
2018-09-29 12:36:47 +00:00
|
|
|
append([]byte{0x81, 126, 0x00, 0x7e + 1}, m4b3...),
|
2018-05-07 17:25:54 +00:00
|
|
|
}, {
|
|
|
|
"fffe > extended 16 bits length",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Text, uint(len(m16b1)), m16b1},
|
2018-09-29 12:36:47 +00:00
|
|
|
append([]byte{0x81, 126, 0xff, 0xfe}, m16b1...),
|
2018-05-07 17:25:54 +00:00
|
|
|
}, {
|
|
|
|
"ffff > extended 16 bits length",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Text, uint(len(m16b2)), m16b2},
|
2018-09-29 12:36:47 +00:00
|
|
|
append([]byte{0x81, 126, 0xff, 0xff}, m16b2...),
|
2018-05-07 17:25:54 +00:00
|
|
|
}, {
|
|
|
|
"10000 > extended 64 bits length",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Text, uint(len(m16b3)), m16b3},
|
2018-09-29 12:36:47 +00:00
|
|
|
append([]byte{0x81, 127, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00}, m16b3...),
|
2018-05-05 22:28:02 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
for _, tc := range cases {
|
2018-05-06 12:45:44 +00:00
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
2018-05-06 12:45:44 +00:00
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
writer := &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) {
|
2018-05-07 17:25:54 +00:00
|
|
|
t.Errorf("expected '%.20x', got '%.20x'", tc.Expected, writer.String())
|
2018-05-06 12:45:44 +00:00
|
|
|
}
|
2018-05-05 22:28:02 +00:00
|
|
|
|
2018-05-06 12:45:44 +00:00
|
|
|
})
|
2018-05-05 22:18:01 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-08 15:21:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMessageCheck(t *testing.T) {
|
|
|
|
|
|
|
|
type Case struct {
|
|
|
|
Name string
|
|
|
|
Msg Message
|
|
|
|
WaitingFragment bool
|
|
|
|
Expected error
|
|
|
|
}
|
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
cases := []struct {
|
|
|
|
Name string
|
2018-05-08 15:21:49 +00:00
|
|
|
Cases []Case
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "first fragment type",
|
|
|
|
Cases: []Case{
|
|
|
|
{
|
|
|
|
"CONTINUATION must fail",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{false, Continuation, 0, []byte{}}, false, ErrInvalidFragment,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"TEXT must not fail",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{false, Text, 0, []byte{}}, false, nil,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"BINARY must not fail",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{false, Binary, 0, []byte{}}, false, nil,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"CLOSE must fail",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{false, Close, 0, []byte{}}, false, ErrInvalidFragment,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"PING must fail",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{false, Ping, 0, []byte{}}, false, ErrInvalidFragment,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"PONG must fail",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{false, Pong, 0, []byte{}}, false, ErrInvalidFragment,
|
2018-05-08 15:21:49 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
Name: "frame during fragment",
|
|
|
|
Cases: []Case{
|
|
|
|
{
|
|
|
|
"CONTINUATION must not fail",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Continuation, 0, []byte{}}, true, nil,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"TEXT must fail",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Text, 0, []byte{}}, true, ErrInvalidFragment,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"BINARY must fail",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Binary, 0, []byte{}}, true, ErrInvalidFragment,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"CLOSE must not fail",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Close, 0, []byte{}}, true, ErrCloseFrame,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"PING must not fail",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Ping, 0, []byte{}}, true, nil,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"PONG must not fail",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Pong, 0, []byte{}}, true, nil,
|
2018-05-08 15:21:49 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
Name: "125-length control frame",
|
|
|
|
Cases: []Case{
|
|
|
|
{
|
|
|
|
"CLOSE must not fail",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Close, 125, []byte{0x03, 0xe8, 0}}, false, ErrCloseFrame,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"PING must not fail",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Ping, 125, []byte{}}, false, nil,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"PONG must not fail",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Pong, 125, []byte{}}, false, nil,
|
2018-05-08 15:21:49 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
Name: "126-length control frame",
|
|
|
|
Cases: []Case{
|
|
|
|
{
|
|
|
|
"CLOSE must fail",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Close, 126, []byte{0x03, 0xe8, 0}}, false, ErrTooLongControlFrame,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"PING must fail",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Ping, 126, []byte{}}, false, ErrTooLongControlFrame,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"PONG must fail",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Pong, 126, []byte{}}, false, ErrTooLongControlFrame,
|
2018-05-08 15:21:49 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
Name: "fragmented control frame",
|
|
|
|
Cases: []Case{
|
|
|
|
{
|
|
|
|
"CLOSE must fail",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{false, Close, 126, []byte{0x03, 0xe8, 0}}, false, ErrInvalidFragment,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"PING must fail",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{false, Ping, 126, []byte{}}, false, ErrInvalidFragment,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"PONG must fail",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{false, Pong, 126, []byte{}}, false, ErrInvalidFragment,
|
2018-05-08 15:21:49 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
Name: "unexpected continuation frame",
|
|
|
|
Cases: []Case{
|
|
|
|
{
|
|
|
|
"no waiting fragment final",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{false, Continuation, 126, nil}, false, ErrInvalidFragment,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"no waiting fragment non-final",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Continuation, 126, nil}, false, ErrUnexpectedContinuation,
|
2018-05-08 15:21:49 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
Name: "utf8 check",
|
|
|
|
Cases: []Case{
|
|
|
|
{
|
|
|
|
"CLOSE valid reason",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Close, 5, []byte{0x03, 0xe8, 0xe2, 0x82, 0xa1}}, false, ErrCloseFrame,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"CLOSE invalid reason byte 2",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Close, 5, []byte{0x03, 0xe8, 0xe2, 0x28, 0xa1}}, false, ErrInvalidPayload,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"CLOSE invalid reason byte 3",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Close, 5, []byte{0x03, 0xe8, 0xe2, 0x82, 0x28}}, false, ErrInvalidPayload,
|
2018-05-08 15:21:49 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"TEXT valid reason",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Text, 3, []byte{0xe2, 0x82, 0xa1}}, false, nil,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"TEXT invalid reason byte 2",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Text, 3, []byte{0xe2, 0x28, 0xa1}}, false, ErrInvalidPayload,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"TEXT invalid reason byte 3",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Text, 3, []byte{0xe2, 0x82, 0x28}}, false, ErrInvalidPayload,
|
2018-05-08 15:21:49 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
Name: "CLOSE status",
|
|
|
|
Cases: []Case{
|
|
|
|
{
|
|
|
|
"CLOSE only 1 byte",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Close, 1, []byte{0x03}}, false, ErrInvalidCloseStatus,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"valid CLOSE status 1000",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Close, 2, []byte{0x03, 0xe8}}, false, ErrCloseFrame,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"invalid CLOSE status 999 under 1000",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Close, 2, []byte{0x03, 0xe7}}, false, ErrInvalidCloseStatus,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"valid CLOSE status 1001",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Close, 2, []byte{0x03, 0xe9}}, false, ErrCloseFrame,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"valid CLOSE status 1002",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Close, 2, []byte{0x03, 0xea}}, false, ErrCloseFrame,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"valid CLOSE status 1003",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Close, 2, []byte{0x03, 0xeb}}, false, ErrCloseFrame,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"invalid CLOSE status 1004",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Close, 2, []byte{0x03, 0xec}}, false, ErrInvalidCloseStatus,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"invalid CLOSE status 1005",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Close, 2, []byte{0x03, 0xed}}, false, ErrInvalidCloseStatus,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"invalid CLOSE status 1006",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Close, 2, []byte{0x03, 0xee}}, false, ErrInvalidCloseStatus,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"valid CLOSE status 1007",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Close, 2, []byte{0x03, 0xef}}, false, ErrCloseFrame,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"valid CLOSE status 1011",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Close, 2, []byte{0x03, 0xf3}}, false, ErrCloseFrame,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"invalid CLOSE status 1012",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Close, 2, []byte{0x03, 0xf4}}, false, ErrInvalidCloseStatus,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"invalid CLOSE status 1013",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Close, 2, []byte{0x03, 0xf5}}, false, ErrInvalidCloseStatus,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"invalid CLOSE status 1014",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Close, 2, []byte{0x03, 0xf6}}, false, ErrInvalidCloseStatus,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"invalid CLOSE status 1015",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Close, 2, []byte{0x03, 0xf7}}, false, ErrInvalidCloseStatus,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"invalid CLOSE status 1016",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Close, 2, []byte{0x03, 0xf8}}, false, ErrInvalidCloseStatus,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"valid CLOSE status 1017",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Close, 2, []byte{0x03, 0xf9}}, false, ErrCloseFrame,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"valid CLOSE status 1099",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Close, 2, []byte{0x04, 0x4b}}, false, ErrCloseFrame,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"invalid CLOSE status 1100",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Close, 2, []byte{0x04, 0x4c}}, false, ErrInvalidCloseStatus,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"valid CLOSE status 1101",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Close, 2, []byte{0x04, 0x4d}}, false, ErrCloseFrame,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"valid CLOSE status 1999",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Close, 2, []byte{0x07, 0xcf}}, false, ErrCloseFrame,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"invalid CLOSE status 2000",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Close, 2, []byte{0x07, 0xd0}}, false, ErrInvalidCloseStatus,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"valid CLOSE status 2001",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Close, 2, []byte{0x07, 0xd1}}, false, ErrCloseFrame,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"valid CLOSE status 2998",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Close, 2, []byte{0x0b, 0xb6}}, false, ErrCloseFrame,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"invalid CLOSE status 2999",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Close, 2, []byte{0x0b, 0xb7}}, false, ErrInvalidCloseStatus,
|
2018-05-08 15:21:49 +00:00
|
|
|
}, {
|
|
|
|
"valid CLOSE status 3000",
|
2021-05-14 14:47:02 +00:00
|
|
|
Message{true, Close, 2, []byte{0x0b, 0xb8}}, false, ErrCloseFrame,
|
2018-05-08 15:21:49 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
Name: "OpCode check",
|
|
|
|
Cases: []Case{
|
2018-09-29 12:36:47 +00:00
|
|
|
{"0", Message{true, 0, 0, []byte{}}, false, ErrUnexpectedContinuation},
|
|
|
|
{"1", Message{true, 1, 0, []byte{}}, false, nil},
|
|
|
|
{"2", Message{true, 2, 0, []byte{}}, false, nil},
|
|
|
|
{"3", Message{true, 3, 0, []byte{}}, false, ErrInvalidOpCode},
|
|
|
|
{"4", Message{true, 4, 0, []byte{}}, false, ErrInvalidOpCode},
|
|
|
|
{"5", Message{true, 5, 0, []byte{}}, false, ErrInvalidOpCode},
|
|
|
|
{"6", Message{true, 6, 0, []byte{}}, false, ErrInvalidOpCode},
|
|
|
|
{"7", Message{true, 7, 0, []byte{}}, false, ErrInvalidOpCode},
|
2021-05-14 14:47:02 +00:00
|
|
|
{"8", Message{true, 8, 0, []byte{}}, false, ErrCloseFrame},
|
2018-09-29 12:36:47 +00:00
|
|
|
{"9", Message{true, 9, 0, []byte{}}, false, nil},
|
|
|
|
{"10", Message{true, 10, 0, []byte{}}, false, nil},
|
|
|
|
{"11", Message{true, 11, 0, []byte{}}, false, ErrInvalidOpCode},
|
|
|
|
{"12", Message{true, 12, 0, []byte{}}, false, ErrInvalidOpCode},
|
|
|
|
{"13", Message{true, 13, 0, []byte{}}, false, ErrInvalidOpCode},
|
|
|
|
{"14", Message{true, 14, 0, []byte{}}, false, ErrInvalidOpCode},
|
|
|
|
{"15", Message{true, 15, 0, []byte{}}, false, ErrInvalidOpCode},
|
2018-05-08 15:21:49 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
for _, tcc := range cases {
|
2018-05-08 15:21:49 +00:00
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
t.Run(tcc.Name, func(t *testing.T) {
|
2018-05-08 15:21:49 +00:00
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
for _, tc := range tcc.Cases {
|
2018-05-08 15:21:49 +00:00
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
2018-05-08 15:21:49 +00:00
|
|
|
|
|
|
|
actual := tc.Msg.check(tc.WaitingFragment)
|
|
|
|
|
|
|
|
if actual != tc.Expected {
|
|
|
|
t.Errorf("expected '%v', got '%v'", tc.Expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
}
|