add test to check read size

This commit is contained in:
xdrm-brackets 2018-05-06 14:45:44 +02:00
parent 26864aab55
commit 613620998a
1 changed files with 146 additions and 46 deletions

View File

@ -1,6 +1,7 @@
package ws package ws
import ( import (
"io"
"bytes" "bytes"
"testing" "testing"
) )
@ -9,72 +10,165 @@ 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{
t.Run(tc.Name, func(t *testing.T){
reader := bytes.NewBuffer(tc.ReadBuffer) reader := bytes.NewBuffer(tc.ReadBuffer)
got, err := readMessage(reader) got, err := readMessage(reader)
if err != tc.Err { if err != tc.Err {
t.Errorf("[%d] Expected %v error, got %v", i, tc.Err, err) t.Errorf("Expected %v error, got %v", tc.Err, err)
} }
// do not check message if error expected // do not check message if error expected
if tc.Err != nil { if tc.Err != nil {
continue return
} }
// check FIN // check FIN
if got.Final != tc.Expected.Final { if got.Final != tc.Expected.Final {
t.Errorf("[%d] Expected FIN=%t, got %t", i, tc.Expected.Final, got.Final) t.Errorf("Expected FIN=%t, got %t", tc.Expected.Final, got.Final)
} }
// check OpCode // check OpCode
if got.Type != tc.Expected.Type { if got.Type != tc.Expected.Type {
t.Errorf("[%d] Expected TYPE=%x, got %x", i, tc.Expected.Type, got.Type) t.Errorf("Expected TYPE=%x, got %x", tc.Expected.Type, got.Type)
} }
// check Size // check Size
if got.Size != tc.Expected.Size { if got.Size != tc.Expected.Size {
t.Errorf("[%d] Expected SIZE=%d, got %d", i, tc.Expected.Size, got.Size) t.Errorf("Expected SIZE=%d, got %d", tc.Expected.Size, got.Size)
} }
// check Data // check Data
if string(got.Data) != string(tc.Expected.Data) { if string(got.Data) != string(tc.Expected.Data) {
t.Errorf("[%d] Expected Data='%s', got '%d'", i, tc.Expected.Data, got.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,43 +178,49 @@ func TestSimpleMessageReading(t *testing.T) {
func TestSimpleMessageSending(t *testing.T) { func TestSimpleMessageSending(t *testing.T) {
cases := []struct{ cases := []struct{
Name string
Base Message Base Message
Expected []byte 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{
t.Run(tc.Name, func(t *testing.T){
writer := new(bytes.Buffer) writer := new(bytes.Buffer)
err := tc.Base.Send(writer) err := tc.Base.Send(writer)
if err != nil { if err != nil {
t.Errorf("[%d] expected no error, got %v", i, err) t.Errorf("expected no error, got %v", err)
continue return
} }
// check buffer // check buffer
if writer.String() != string(tc.Expected) { if writer.String() != string(tc.Expected) {
t.Errorf("[%d] expected '%x', got '%x'", i, tc.Expected, writer.String()) t.Errorf("expected '%x', got '%x'", tc.Expected, writer.String())
} }
})
} }
} }