fix send length + add test for simple/extended-16/extended-64 length

This commit is contained in:
xdrm-brackets 2018-05-07 19:25:54 +02:00
parent 6759c489cc
commit 1891bdff1d
2 changed files with 35 additions and 3 deletions

View File

@ -178,7 +178,7 @@ func (m Message) Send(writer io.Writer) error {
header = append(header, byte(m.Size) ) header = append(header, byte(m.Size) )
} else if m.Size < 0xffff { // extended: 16 bits } else if m.Size <= 0xffff { // extended: 16 bits
header = append(header, 126) header = append(header, 126)
@ -186,7 +186,7 @@ func (m Message) Send(writer io.Writer) error {
binary.BigEndian.PutUint16(buf, uint16(m.Size)) binary.BigEndian.PutUint16(buf, uint16(m.Size))
header = append(header, buf...) header = append(header, buf...)
} else if m.Size < 0xffffffffffffffff { // extended: 64 bits } else if m.Size <= 0xffffffffffffffff { // extended: 64 bits
header = append(header, 127) header = append(header, 127)

View File

@ -193,6 +193,14 @@ func TestReadEOF(t *testing.T) {
func TestSimpleMessageSending(t *testing.T) { func TestSimpleMessageSending(t *testing.T) {
m4b1 := make([]byte, 0x7e - 1)
m4b2 := make([]byte, 0x7e)
m4b3 := make([]byte, 0x7e + 1)
m16b1 := make([]byte, 0xffff - 1)
m16b2 := make([]byte, 0xffff)
m16b3 := make([]byte, 0xffff + 1)
cases := []struct{ cases := []struct{
Name string Name string
Base Message Base Message
@ -214,6 +222,30 @@ func TestSimpleMessageSending(t *testing.T) {
"non final frame", "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},
}, {
"125 > normal length",
Message{ true, TEXT, uint(len(m4b1)), m4b1 },
append([]byte{0x81,0x7e-1}, m4b1...),
}, {
"126 > extended 16 bits length",
Message{ true, TEXT, uint(len(m4b2)), m4b2 },
append([]byte{0x81,126,0x00,0x7e}, m4b2...),
}, {
"127 > extended 16 bits length",
Message{ true, TEXT, uint(len(m4b3)), m4b3 },
append([]byte{0x81,126,0x00,0x7e+1}, m4b3...),
}, {
"fffe > extended 16 bits length",
Message{ true, TEXT, uint(len(m16b1)), m16b1 },
append([]byte{0x81,126, 0xff, 0xfe}, m16b1...),
}, {
"ffff > extended 16 bits length",
Message{ true, TEXT, uint(len(m16b2)), m16b2 },
append([]byte{0x81,126,0xff,0xff}, m16b2...),
}, {
"10000 > extended 64 bits length",
Message{ true, TEXT, uint(len(m16b3)), m16b3 },
append([]byte{0x81,127, 0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,}, m16b3...),
}, },
} }
@ -232,7 +264,7 @@ func TestSimpleMessageSending(t *testing.T) {
// check buffer // check buffer
if writer.String() != string(tc.Expected) { if writer.String() != string(tc.Expected) {
t.Errorf("expected '%x', got '%x'", tc.Expected, writer.String()) t.Errorf("expected '%.20x', got '%.20x'", tc.Expected, writer.String())
} }
}) })