Skip to content

Commit

Permalink
Add packet parse error test cases (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
at-wat authored Jan 21, 2020
1 parent 59f1a0a commit 37bf964
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 19 deletions.
2 changes: 1 addition & 1 deletion connack.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type pktConnAck struct {
Code ConnectionReturnCode
}

func (p *pktConnAck) parse(flag byte, contents []byte) (*pktConnAck, error) {
func (p *pktConnAck) Parse(flag byte, contents []byte) (*pktConnAck, error) {
if flag != 0 {
return nil, wrapError(ErrInvalidPacket, "parsing CONNACK")
}
Expand Down
82 changes: 82 additions & 0 deletions packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ package mqtt

import (
"bytes"
"reflect"
"strings"
"testing"

"github.com/at-wat/mqtt-go/internal/errs"
)

func TestRemainingLength(t *testing.T) {
Expand Down Expand Up @@ -49,3 +52,82 @@ func TestPacketType(t *testing.T) {
)
}
}

func TestPacketParseError(t *testing.T) {
cases := map[string]struct {
flag byte
contents []byte
pkt interface{}
err error
}{
"ConnAck_InvalidFlag": {
0x01, []byte{}, &pktConnAck{}, ErrInvalidPacket,
},
"ConnAck_ShortLength": {
0x00, []byte{}, &pktConnAck{}, ErrInvalidPacketLength,
},
"PingResp_InvalidFlag": {
0x01, []byte{}, &pktPingResp{}, ErrInvalidPacket,
},
"PubAck_InvalidFlag": {
0x01, []byte{}, &pktPubAck{}, ErrInvalidPacket,
},
"PubAck_ShortLength": {
0x00, []byte{}, &pktPubAck{}, ErrInvalidPacketLength,
},
"PubComp_InvalidFlag": {
0x01, []byte{}, &pktPubComp{}, ErrInvalidPacket,
},
"PubComp_ShortLength": {
0x00, []byte{}, &pktPubComp{}, ErrInvalidPacketLength,
},
"Publish_InvalidQoS": {
0x06, []byte{}, &pktPublish{}, ErrInvalidPacket,
},
"Publish_ShortLength": {
0x02, []byte{}, &pktPublish{}, ErrInvalidPacketLength,
},
"PubRec_InvalidFlag": {
0x01, []byte{}, &pktPubRec{}, ErrInvalidPacket,
},
"PubRec_ShortLength": {
0x00, []byte{}, &pktPubRec{}, ErrInvalidPacketLength,
},
"PubRel_InvalidFlag": {
0x01, []byte{}, &pktPubRel{}, ErrInvalidPacket,
},
"PubRel_ShortLength": {
0x02, []byte{}, &pktPubRel{}, ErrInvalidPacketLength,
},
"SubAck_InvalidFlag": {
0x01, []byte{}, &pktSubAck{}, ErrInvalidPacket,
},
"UnsubAck_InvalidFlag": {
0x01, []byte{}, &pktUnsubAck{}, ErrInvalidPacket,
},
}

for name, c := range cases {
t.Run(name, func(t *testing.T) {
f := reflect.ValueOf(c.pkt).MethodByName("Parse")
if !f.IsValid() {
t.Fatal("Packet struct doesn't have Parse method.")
}
ret := f.Call(
[]reflect.Value{
reflect.ValueOf(c.flag), reflect.ValueOf(c.contents),
},
)
if len(ret) != 2 {
t.Fatal("Invalid number of the return value of the Parse method.")
}
err, ok := ret[1].Interface().(error)
if !ok {
t.Fatal("Second return value of Parse is not error.")
}
if !errs.Is(err, c.err) {
t.Errorf("Parse result is expected to be: %v, got: %v", c.err, err)
}
})
}
}
2 changes: 1 addition & 1 deletion pingresp.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package mqtt
type pktPingResp struct {
}

func (p *pktPingResp) parse(flag byte, contents []byte) (*pktPingResp, error) {
func (p *pktPingResp) Parse(flag byte, contents []byte) (*pktPingResp, error) {
if flag != 0 {
return nil, ErrInvalidPacket
}
Expand Down
2 changes: 1 addition & 1 deletion puback.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type pktPubAck struct {
ID uint16
}

func (p *pktPubAck) parse(flag byte, contents []byte) (*pktPubAck, error) {
func (p *pktPubAck) Parse(flag byte, contents []byte) (*pktPubAck, error) {
if flag != 0 {
return nil, wrapError(ErrInvalidPacket, "parsing PUBACK")
}
Expand Down
2 changes: 1 addition & 1 deletion pubcomp.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type pktPubComp struct {
ID uint16
}

func (p *pktPubComp) parse(flag byte, contents []byte) (*pktPubComp, error) {
func (p *pktPubComp) Parse(flag byte, contents []byte) (*pktPubComp, error) {
if flag != 0 {
return nil, wrapError(ErrInvalidPacket, "parsing PUBCOMP")
}
Expand Down
2 changes: 1 addition & 1 deletion publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type pktPublish struct {
Message *Message
}

func (p *pktPublish) parse(flag byte, contents []byte) (*pktPublish, error) {
func (p *pktPublish) Parse(flag byte, contents []byte) (*pktPublish, error) {
p.Message = &Message{
Dup: (publishFlag(flag) & publishFlagDup) != 0,
Retain: (publishFlag(flag) & publishFlagRetain) != 0,
Expand Down
2 changes: 1 addition & 1 deletion publish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestPublish_ParseError(t *testing.T) {
}

for _, c := range cases {
_, err := (&pktPublish{}).parse(c.flag, c.contents)
_, err := (&pktPublish{}).Parse(c.flag, c.contents)
if !errs.Is(err, c.err) {
t.Errorf("Parsing packet with flag=%x, contents=%v expected error: %v, got: %v",
c.flag, c.contents,
Expand Down
2 changes: 1 addition & 1 deletion pubrec.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type pktPubRec struct {
ID uint16
}

func (p *pktPubRec) parse(flag byte, contents []byte) (*pktPubRec, error) {
func (p *pktPubRec) Parse(flag byte, contents []byte) (*pktPubRec, error) {
if flag != 0 {
return nil, wrapError(ErrInvalidPacket, "parsing PUBREC")
}
Expand Down
2 changes: 1 addition & 1 deletion pubrel.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type pktPubRel struct {
ID uint16
}

func (p *pktPubRel) parse(flag byte, contents []byte) (*pktPubRel, error) {
func (p *pktPubRel) Parse(flag byte, contents []byte) (*pktPubRel, error) {
if flag != 0x02 {
return nil, wrapError(ErrInvalidPacket, "parsing PUBREL")
}
Expand Down
18 changes: 9 additions & 9 deletions serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (c *BaseClient) serve() error {

switch pktType {
case packetConnAck:
connAck, err := (&pktConnAck{}).parse(pktFlag, contents)
connAck, err := (&pktConnAck{}).Parse(pktFlag, contents)
if err != nil {
// Client must close connection if packet is invalid.
return err
Expand All @@ -57,7 +57,7 @@ func (c *BaseClient) serve() error {
default:
}
case packetPublish:
publish, err := (&pktPublish{}).parse(pktFlag, contents)
publish, err := (&pktPublish{}).Parse(pktFlag, contents)
if err != nil {
return err
}
Expand Down Expand Up @@ -89,7 +89,7 @@ func (c *BaseClient) serve() error {
subBuffer[publish.Message.ID] = publish.Message
}
case packetPubAck:
pubAck, err := (&pktPubAck{}).parse(pktFlag, contents)
pubAck, err := (&pktPubAck{}).Parse(pktFlag, contents)
if err != nil {
return err
}
Expand All @@ -100,7 +100,7 @@ func (c *BaseClient) serve() error {
}
}
case packetPubRec:
pubRec, err := (&pktPubRec{}).parse(pktFlag, contents)
pubRec, err := (&pktPubRec{}).Parse(pktFlag, contents)
if err != nil {
return err
}
Expand All @@ -111,7 +111,7 @@ func (c *BaseClient) serve() error {
}
}
case packetPubRel:
pubRel, err := (&pktPubRel{}).parse(pktFlag, contents)
pubRel, err := (&pktPubRel{}).Parse(pktFlag, contents)
if err != nil {
return err
}
Expand All @@ -131,7 +131,7 @@ func (c *BaseClient) serve() error {
return wrapError(err, "sending PUBCOMP")
}
case packetPubComp:
pubComp, err := (&pktPubComp{}).parse(pktFlag, contents)
pubComp, err := (&pktPubComp{}).Parse(pktFlag, contents)
if err != nil {
return err
}
Expand All @@ -142,7 +142,7 @@ func (c *BaseClient) serve() error {
}
}
case packetSubAck:
subAck, err := (&pktSubAck{}).parse(pktFlag, contents)
subAck, err := (&pktSubAck{}).Parse(pktFlag, contents)
if err != nil {
return err
}
Expand All @@ -153,7 +153,7 @@ func (c *BaseClient) serve() error {
}
}
case packetUnsubAck:
unsubAck, err := (&pktUnsubAck{}).parse(pktFlag, contents)
unsubAck, err := (&pktUnsubAck{}).Parse(pktFlag, contents)
if err != nil {
return err
}
Expand All @@ -164,7 +164,7 @@ func (c *BaseClient) serve() error {
}
}
case packetPingResp:
pingResp, err := (&pktPingResp{}).parse(pktFlag, contents)
pingResp, err := (&pktPingResp{}).Parse(pktFlag, contents)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion suback.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type pktSubAck struct {
Codes []subscribeFlag
}

func (p *pktSubAck) parse(flag byte, contents []byte) (*pktSubAck, error) {
func (p *pktSubAck) Parse(flag byte, contents []byte) (*pktSubAck, error) {
if flag != 0 {
return nil, wrapError(ErrInvalidPacket, "parsing SUBSCK")
}
Expand Down
2 changes: 1 addition & 1 deletion unsuback.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type pktUnsubAck struct {
ID uint16
}

func (p *pktUnsubAck) parse(flag byte, contents []byte) (*pktUnsubAck, error) {
func (p *pktUnsubAck) Parse(flag byte, contents []byte) (*pktUnsubAck, error) {
if flag != 0 {
return nil, wrapError(ErrInvalidPacket, "parsing SUBACK")
}
Expand Down

0 comments on commit 37bf964

Please sign in to comment.