add test to check read size
This commit is contained in:
parent
26864aab55
commit
613620998a
|
@ -1,6 +1,7 @@
|
||||||
package ws
|
package ws
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
"bytes"
|
"bytes"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
@ -9,71 +10,164 @@ import (
|
||||||
func TestSimpleMessageReading(t *testing.T) {
|
func TestSimpleMessageReading(t *testing.T) {
|
||||||
|
|
||||||
cases := []struct{
|
cases := []struct{
|
||||||
|
Name string
|
||||||
ReadBuffer []byte
|
ReadBuffer []byte
|
||||||
Expected Message
|
Expected Message
|
||||||
Err error
|
Err error
|
||||||
}{
|
}{
|
||||||
{ // FIN ; TEXT ; Unmasked -> error
|
{ // FIN ; TEXT ; Unmasked -> error
|
||||||
|
"must fail on unmasked frame",
|
||||||
[]byte{0x81,0x05,0x68,0x65,0x6c,0x6c,0x6f},
|
[]byte{0x81,0x05,0x68,0x65,0x6c,0x6c,0x6f},
|
||||||
Message{},
|
Message{},
|
||||||
UnmaskedFrameErr,
|
UnmaskedFrameErr,
|
||||||
},
|
},
|
||||||
{ // FIN ; TEXT ; hello
|
{ // FIN ; TEXT ; hello
|
||||||
|
"simple hello text message",
|
||||||
[]byte{0x81,0x85,0x00,0x00,0x00,0x00,0x68,0x65,0x6c,0x6c,0x6f},
|
[]byte{0x81,0x85,0x00,0x00,0x00,0x00,0x68,0x65,0x6c,0x6c,0x6f},
|
||||||
Message{ true, TEXT, 5, []byte("hello") },
|
Message{ true, TEXT, 5, []byte("hello") },
|
||||||
nil,
|
nil,
|
||||||
},
|
},
|
||||||
{ // FIN ; BINARY ; hello
|
{ // FIN ; BINARY ; hello
|
||||||
|
"simple hello binary message",
|
||||||
[]byte{0x82,0x85,0x00,0x00,0x00,0x00,0x68,0x65,0x6c,0x6c,0x6f},
|
[]byte{0x82,0x85,0x00,0x00,0x00,0x00,0x68,0x65,0x6c,0x6c,0x6f},
|
||||||
Message{ true, BINARY, 5, []byte("hello") },
|
Message{ true, BINARY, 5, []byte("hello") },
|
||||||
nil,
|
nil,
|
||||||
},
|
},
|
||||||
{ // FIN ; BINARY ; test unmasking
|
{ // FIN ; BINARY ; test unmasking
|
||||||
|
"unmasking test",
|
||||||
[]byte{0x82,0x88,0x01,0x02,0x03,0x04,0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x80},
|
[]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} },
|
Message{ true, BINARY, 8, []byte{0x11,0x22,0x33,0x44,0x51,0x62,0x73,0x84} },
|
||||||
nil,
|
nil,
|
||||||
},
|
},
|
||||||
{ // FIN=0 ; TEXT ;
|
{ // FIN=0 ; TEXT ;
|
||||||
|
"non final frame",
|
||||||
[]byte{0x01,0x82,0x00,0x00,0x00,0x00,0x01,0x02},
|
[]byte{0x01,0x82,0x00,0x00,0x00,0x00,0x01,0x02},
|
||||||
Message{ false, TEXT, 2, []byte{0x01,0x02} },
|
Message{ false, TEXT, 2, []byte{0x01,0x02} },
|
||||||
nil,
|
nil,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, tc := range cases{
|
for _, tc := range cases{
|
||||||
|
|
||||||
reader := bytes.NewBuffer(tc.ReadBuffer)
|
t.Run(tc.Name, func(t *testing.T){
|
||||||
|
|
||||||
got, err := readMessage(reader)
|
reader := bytes.NewBuffer(tc.ReadBuffer)
|
||||||
|
|
||||||
if err != tc.Err {
|
got, err := readMessage(reader)
|
||||||
t.Errorf("[%d] Expected %v error, got %v", i, tc.Err, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// do not check message if error expected
|
if err != tc.Err {
|
||||||
if tc.Err != nil {
|
t.Errorf("Expected %v error, got %v", tc.Err, err)
|
||||||
continue
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// check FIN
|
// do not check message if error expected
|
||||||
if got.Final != tc.Expected.Final {
|
if tc.Err != nil {
|
||||||
t.Errorf("[%d] Expected FIN=%t, got %t", i, tc.Expected.Final, got.Final)
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// check OpCode
|
// check FIN
|
||||||
if got.Type != tc.Expected.Type {
|
if got.Final != tc.Expected.Final {
|
||||||
t.Errorf("[%d] Expected TYPE=%x, got %x", i, tc.Expected.Type, got.Type)
|
t.Errorf("Expected FIN=%t, got %t", tc.Expected.Final, got.Final)
|
||||||
}
|
}
|
||||||
|
|
||||||
// check Size
|
// check OpCode
|
||||||
if got.Size != tc.Expected.Size {
|
if got.Type != tc.Expected.Type {
|
||||||
t.Errorf("[%d] Expected SIZE=%d, got %d", i, tc.Expected.Size, got.Size)
|
t.Errorf("Expected TYPE=%x, got %x", tc.Expected.Type, got.Type)
|
||||||
}
|
}
|
||||||
|
|
||||||
// check Data
|
// check Size
|
||||||
if string(got.Data) != string(tc.Expected.Data) {
|
if got.Size != tc.Expected.Size {
|
||||||
t.Errorf("[%d] Expected Data='%s', got '%d'", i, tc.Expected.Data, got.Data)
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,42 +178,48 @@ func TestSimpleMessageReading(t *testing.T) {
|
||||||
func TestSimpleMessageSending(t *testing.T) {
|
func TestSimpleMessageSending(t *testing.T) {
|
||||||
|
|
||||||
cases := []struct{
|
cases := []struct{
|
||||||
Base Message
|
Name string
|
||||||
Expected []byte
|
Base Message
|
||||||
|
Expected []byte
|
||||||
}{
|
}{
|
||||||
{ // FIN ; TEXT ; hello
|
{
|
||||||
|
"simple hello text message",
|
||||||
Message{ true, TEXT, 5, []byte("hello") },
|
Message{ true, TEXT, 5, []byte("hello") },
|
||||||
[]byte{0x81,0x05,0x68,0x65,0x6c,0x6c,0x6f},
|
[]byte{0x81,0x05,0x68,0x65,0x6c,0x6c,0x6f},
|
||||||
},
|
}, {
|
||||||
{ // FIN ; BINARY ; hello
|
"simple hello binary message",
|
||||||
Message{ true, BINARY, 5, []byte("hello") },
|
Message{ true, BINARY, 5, []byte("hello") },
|
||||||
[]byte{0x82,0x05,0x68,0x65,0x6c,0x6c,0x6f},
|
[]byte{0x82,0x05,0x68,0x65,0x6c,0x6c,0x6f},
|
||||||
},
|
}, {
|
||||||
{ // FIN ; BINARY ; test unmasking
|
"other simple binary message",
|
||||||
Message{ true, BINARY, 8, []byte{0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x80} },
|
Message{ true, BINARY, 8, []byte{0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x80} },
|
||||||
[]byte{0x82,0x08,0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x80},
|
[]byte{0x82,0x08,0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x80},
|
||||||
},
|
}, {
|
||||||
{ // FIN=0 ; TEXT ;
|
"non final frame",
|
||||||
Message{ false, TEXT, 2, []byte{0x01,0x02} },
|
Message{ false, TEXT, 2, []byte{0x01,0x02} },
|
||||||
[]byte{0x01,0x02,0x01,0x02},
|
[]byte{0x01,0x02,0x01,0x02},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, tc := range cases{
|
for _, tc := range cases{
|
||||||
|
|
||||||
writer := new(bytes.Buffer)
|
t.Run(tc.Name, func(t *testing.T){
|
||||||
|
|
||||||
err := tc.Base.Send(writer)
|
writer := new(bytes.Buffer)
|
||||||
|
|
||||||
if err != nil {
|
err := tc.Base.Send(writer)
|
||||||
t.Errorf("[%d] expected no error, got %v", i, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// check buffer
|
if err != nil {
|
||||||
if writer.String() != string(tc.Expected) {
|
t.Errorf("expected no error, got %v", err)
|
||||||
t.Errorf("[%d] expected '%x', got '%x'", i, tc.Expected, writer.String())
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check buffer
|
||||||
|
if writer.String() != string(tc.Expected) {
|
||||||
|
t.Errorf("expected '%x', got '%x'", tc.Expected, writer.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue