diff --git a/go.mod b/go.mod index 4767664..ac570e9 100644 --- a/go.mod +++ b/go.mod @@ -1,12 +1,19 @@ module github.com/eclipse/paho.golang -go 1.15 +go 1.20 require ( - github.com/google/go-cmp v0.5.5 + github.com/google/go-cmp v0.5.9 github.com/gorilla/websocket v1.5.0 - github.com/stretchr/testify v1.8.0 + github.com/stretchr/testify v1.8.4 go.uber.org/goleak v1.2.1 - golang.org/x/net v0.14.0 - golang.org/x/sync v0.3.0 + golang.org/x/net v0.17.0 + golang.org/x/sync v0.4.0 +) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/kr/text v0.2.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index c8317d1..5e54e53 100644 --- a/go.sum +++ b/go.sum @@ -1,71 +1,24 @@ -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= -github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= -github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= -github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A= go.uber.org/goleak v1.2.1/go.mod h1:qlT2yGI9QafXHhZZLxlSuNsMw3FFLxBr+tBRlmO1xH4= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= -golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= -golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= -golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= -golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14= -golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= -golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= -golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= -golang.org/x/term v0.11.0/go.mod h1:zC9APTIj3jG3FdV/Ons+XE1riIZXG4aZ4GTHiPZJPIU= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= -golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= -golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= -golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= +golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= +golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ= +golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/packets/packets.go b/packets/packets.go index 54b704d..4f023e0 100644 --- a/packets/packets.go +++ b/packets/packets.go @@ -109,6 +109,7 @@ func (c *ControlPacket) PacketID() uint16 { } } +// PacketType returns the packet type as a string func (c *ControlPacket) PacketType() string { return [...]string{ "", @@ -130,6 +131,44 @@ func (c *ControlPacket) PacketType() string { }[c.FixedHeader.Type] } +// String implements fmt.Stringer (mainly for debugging purposes) +func (c *ControlPacket) String() string { + switch p := c.Content.(type) { + case *Connect: + return p.String() + case *Connack: + return p.String() + case *Publish: + return p.String() + case *Puback: + return p.String() + case *Pubrec: + return p.String() + case *Pubrel: + return p.String() + case *Pubcomp: + return p.String() + case *Subscribe: + return p.String() + case *Suback: + return p.String() + case *Unsubscribe: + return p.String() + case *Unsuback: + return p.String() + case *Pingreq: + return p.String() + case *Pingresp: + return p.String() + case *Disconnect: + return p.String() + case *Auth: + return p.String() + default: + return fmt.Sprintf("Unknown packet type: %d", c.Type) + } +} + // NewControlPacket takes a packetType and returns a pointer to a // ControlPacket where the VariableHeader field is a pointer to an // instance of a VariableHeader definition for that packetType @@ -239,8 +278,10 @@ func ReadPacket(r io.Reader) (*ControlPacket, error) { } cp.Flags = t[0] & 0xF - if cp.Type == PUBLISH { - cp.Content.(*Publish).QoS = (cp.Flags & 0x6) >> 1 + if cp.Type == PUBLISH { // Publish is the only packet with flags in the fixed header + cp.Content.(*Publish).QoS = (cp.Flags >> 1) & 0x3 + cp.Content.(*Publish).Duplicate = cp.Flags&(1<<3) != 0 + cp.Content.(*Publish).Retain = cp.Flags&1 != 0 } vbi, err := getVBI(r) if err != nil { @@ -272,6 +313,7 @@ func ReadPacket(r io.Reader) (*ControlPacket, error) { // WriteTo writes a packet to an io.Writer, handling packing all the parts of // a control packet. func (c *ControlPacket) WriteTo(w io.Writer) (int64, error) { + c.remainingLength = 0 // ignore previous remainingLength (if any) buffers := c.Content.Buffers() for _, b := range buffers { c.remainingLength += len(b) diff --git a/packets/publish.go b/packets/publish.go index ef834b7..24edb58 100644 --- a/packets/publish.go +++ b/packets/publish.go @@ -23,7 +23,17 @@ func (p *Publish) String() string { return fmt.Sprintf("PUBLISH: PacketID:%d QOS:%d Topic:%s Duplicate:%t Retain:%t Payload:\n%s\nProperties\n%s", p.PacketID, p.QoS, p.Topic, p.Duplicate, p.Retain, string(p.Payload), p.Properties) } -//Unpack is the implementation of the interface required function for a packet +// SetIdentifier sets the packet identifier +func (p *Publish) SetIdentifier(packetID uint16) { + p.PacketID = packetID +} + +// Type returns the current packet type +func (s *Publish) Type() byte { + return PUBLISH +} + +// Unpack is the implementation of the interface required function for a packet func (p *Publish) Unpack(r *bytes.Buffer) error { var err error p.Topic, err = readString(r) @@ -65,6 +75,11 @@ func (p *Publish) Buffers() net.Buffers { // WriteTo is the implementation of the interface required function for a packet func (p *Publish) WriteTo(w io.Writer) (int64, error) { + return p.ToControlPacket().WriteTo(w) +} + +// ToControlPacket returns the packet as a ControlPacket +func (p *Publish) ToControlPacket() *ControlPacket { f := p.QoS << 1 if p.Duplicate { f |= 1 << 3 @@ -73,8 +88,5 @@ func (p *Publish) WriteTo(w io.Writer) (int64, error) { f |= 1 } - cp := &ControlPacket{FixedHeader: FixedHeader{Type: PUBLISH, Flags: f}} - cp.Content = p - - return cp.WriteTo(w) + return &ControlPacket{FixedHeader: FixedHeader{Type: PUBLISH, Flags: f}, Content: p} } diff --git a/packets/pubrec.go b/packets/pubrec.go index c382019..7bd3045 100644 --- a/packets/pubrec.go +++ b/packets/pubrec.go @@ -41,7 +41,7 @@ func (p *Pubrec) String() string { return b.String() } -//Unpack is the implementation of the interface required function for a packet +// Unpack is the implementation of the interface required function for a packet func (p *Pubrec) Unpack(r *bytes.Buffer) error { var err error success := r.Len() == 2 @@ -84,10 +84,12 @@ func (p *Pubrec) Buffers() net.Buffers { // WriteTo is the implementation of the interface required function for a packet func (p *Pubrec) WriteTo(w io.Writer) (int64, error) { - cp := &ControlPacket{FixedHeader: FixedHeader{Type: PUBREC}} - cp.Content = p + return p.ToControlPacket().WriteTo(w) +} - return cp.WriteTo(w) +// ToControlPacket returns the packet as a ControlPacket +func (p *Pubrec) ToControlPacket() *ControlPacket { + return &ControlPacket{FixedHeader: FixedHeader{Type: PUBREC}, Content: p} } // Reason returns a string representation of the meaning of the ReasonCode diff --git a/packets/subscribe.go b/packets/subscribe.go index 3637d87..2abccbb 100644 --- a/packets/subscribe.go +++ b/packets/subscribe.go @@ -27,6 +27,16 @@ func (s *Subscribe) String() string { return b.String() } +// SetIdentifier sets the packet identifier +func (s *Subscribe) SetIdentifier(packetID uint16) { + s.PacketID = packetID +} + +// Type returns the current packet type +func (s *Subscribe) Type() byte { + return SUBSCRIBE +} + // SubOptions is the struct representing the options for a subscription type SubOptions struct { Topic string @@ -37,6 +47,7 @@ type SubOptions struct { } // Pack is the implementation of the interface required function for a packet +// Note that this does not pack the topic func (s *SubOptions) Pack() byte { var ret byte ret |= s.QoS & 0x03 @@ -46,12 +57,13 @@ func (s *SubOptions) Pack() byte { if s.RetainAsPublished { ret |= 1 << 3 } - ret |= s.RetainHandling & 0x30 + ret |= (s.RetainHandling << 4) & 0x30 return ret } // Unpack is the implementation of the interface required function for a packet +// Note that this does not unpack the topic func (s *SubOptions) Unpack(r *bytes.Buffer) error { b, err := r.ReadByte() if err != nil { @@ -59,9 +71,9 @@ func (s *SubOptions) Unpack(r *bytes.Buffer) error { } s.QoS = b & 0x03 - s.NoLocal = (b & 1 << 2) == 1 - s.RetainAsPublished = (b & 1 << 3) == 1 - s.RetainHandling = b & 0x30 + s.NoLocal = b&(1<<2) != 0 + s.RetainAsPublished = b&(1<<3) != 0 + s.RetainHandling = 3 & (b >> 4) return nil } diff --git a/packets/subscribe_test.go b/packets/subscribe_test.go new file mode 100644 index 0000000..714a85b --- /dev/null +++ b/packets/subscribe_test.go @@ -0,0 +1,128 @@ +package packets + +import ( + "bytes" + "testing" + + "github.com/stretchr/testify/require" +) + +// TestSubscribePackUnpack confirms that subscribe packets are packed/unpacked as per the spec +func TestSubscribePackUnpack(t *testing.T) { + tests := []struct { + name string + sub *Subscribe + want []byte + }{ + { + name: "Spec example (Figure 3-20)", + sub: &Subscribe{ + Properties: &Properties{}, + PacketID: 10, + Subscriptions: []SubOptions{ + { + Topic: "a/b", + QoS: 1, + RetainHandling: 0, + NoLocal: false, + RetainAsPublished: false, + }, + { + Topic: "c/d", + QoS: 2, + RetainHandling: 0, + NoLocal: false, + RetainAsPublished: false, + }}, + }, + want: []byte{0b10000010, 0x0F, 0x0, 0xa, 0x0, 0x0, 0x3, 0x61, 0x2f, 0x62, 0x01, 0x0, 0x3, 0x63, 0x2f, 0x64, 0x2}, + }, + { + name: "NoLocal", + sub: &Subscribe{ + Properties: &Properties{}, + PacketID: 10, + Subscriptions: []SubOptions{ + { + Topic: "NoLocal", + QoS: 0, + RetainHandling: 0, + NoLocal: true, + RetainAsPublished: false, + }, + }, + }, + want: []byte{0x82, 0xd, 0x0, 0xa, 0x0, 0x0, 0x7, 0x4e, 0x6f, 0x4c, 0x6f, 0x63, 0x61, 0x6c, 0b00000100}, + }, + { + name: "Retain As Published", + sub: &Subscribe{ + Properties: &Properties{}, + PacketID: 10, + Subscriptions: []SubOptions{ + { + Topic: "RAP", + QoS: 0, + RetainHandling: 0, + NoLocal: false, + RetainAsPublished: true, + }, + }, + }, + want: []byte{0x82, 0x9, 0x0, 0xa, 0x0, 0x0, 0x3, 0x52, 0x41, 0x50, 0b00001000}, + }, + { + name: "RetainHandling", + sub: &Subscribe{ + Properties: &Properties{}, + PacketID: 10, + Subscriptions: []SubOptions{ + { + Topic: "Retain0", + QoS: 0, + RetainHandling: 0, + NoLocal: false, + RetainAsPublished: false, + }, + { + Topic: "Retain1", + QoS: 0, + RetainHandling: 1, + NoLocal: false, + RetainAsPublished: false, + }, + { + Topic: "Retain2", + QoS: 0, + RetainHandling: 2, + NoLocal: false, + RetainAsPublished: false, + }, + }, + }, + want: []byte{0x82, 0x21, 0x0, 0xa, 0x0, + 0x0, 0x7, 0x52, 0x65, 0x74, 0x61, 0x69, 0x6e, 0x30, 0b00000000, + 0x0, 0x7, 0x52, 0x65, 0x74, 0x61, 0x69, 0x6e, 0x31, 0b00010000, + 0x0, 0x7, 0x52, 0x65, 0x74, 0x61, 0x69, 0x6e, 0x32, 0b00100000}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var b bytes.Buffer + _, err := tt.sub.WriteTo(&b) + require.NoError(t, err) + + require.Equal(t, tt.want, b.Bytes()) + + // Decode and compare with original + dstCp, err := ReadPacket(bytes.NewReader(b.Bytes())) + require.NoError(t, err) + + dstP, ok := dstCp.Content.(*Subscribe) + if !ok { + t.Fatalf("readPacket did not return expected type (got %T)", dstCp.Content) + } + require.Equal(t, tt.sub, dstP) + }) + } +} diff --git a/packets/unsubscribe.go b/packets/unsubscribe.go index dc4e2f8..090d7ca 100644 --- a/packets/unsubscribe.go +++ b/packets/unsubscribe.go @@ -18,6 +18,16 @@ func (u *Unsubscribe) String() string { return fmt.Sprintf("UNSUBSCRIBE: PacketID:%d Topics:%v Properties:\n%s", u.PacketID, u.Topics, u.Properties) } +// SetIdentifier sets the packet identifier +func (u *Unsubscribe) SetIdentifier(packetID uint16) { + u.PacketID = packetID +} + +// Type returns the current packet type +func (s *Unsubscribe) Type() byte { + return UNSUBSCRIBE +} + // Unpack is the implementation of the interface required function for a packet func (u *Unsubscribe) Unpack(r *bytes.Buffer) error { var err error diff --git a/paho/cp_connack.go b/paho/cp_connack.go index 9c72336..2a525eb 100644 --- a/paho/cp_connack.go +++ b/paho/cp_connack.go @@ -1,6 +1,11 @@ package paho -import "github.com/eclipse/paho.golang/packets" +import ( + "fmt" + "strings" + + "github.com/eclipse/paho.golang/packets" +) type ( // Connack is a representation of the MQTT Connack packet @@ -82,3 +87,56 @@ func ConnackFromPacketConnack(c *packets.Connack) *Connack { return v } + +// String implement fmt.Stringer (mainly to simplify debugging) +func (c *Connack) String() string { + return fmt.Sprintf("CONNACK: ReasonCode:%d SessionPresent:%t\nProperties:\n%s", c.ReasonCode, c.SessionPresent, c.Properties) +} + +// String implement fmt.Stringer (mainly to simplify debugging) +func (p *ConnackProperties) String() string { + var b strings.Builder + if p.SessionExpiryInterval != nil { + fmt.Fprintf(&b, "\tSessionExpiryInterval:%d\n", *p.SessionExpiryInterval) + } + if p.AssignedClientID != "" { + fmt.Fprintf(&b, "\tAssignedClientID:%s\n", p.AssignedClientID) + } + if p.ServerKeepAlive != nil { + fmt.Fprintf(&b, "\tServerKeepAlive:%d\n", *p.ServerKeepAlive) + } + if p.AuthMethod != "" { + fmt.Fprintf(&b, "\tAuthMethod:%s\n", p.AuthMethod) + } + if len(p.AuthData) > 0 { + fmt.Fprintf(&b, "\tAuthData:%X\n", p.AuthData) + } + if p.ServerReference != "" { + fmt.Fprintf(&b, "\tServerReference:%s\n", p.ServerReference) + } + if p.ReasonString != "" { + fmt.Fprintf(&b, "\tReasonString:%s\n", p.ReasonString) + } + if p.ReceiveMaximum != nil { + fmt.Fprintf(&b, "\tReceiveMaximum:%d\n", *p.ReceiveMaximum) + } + if p.TopicAliasMaximum != nil { + fmt.Fprintf(&b, "\tTopicAliasMaximum:%d\n", *p.TopicAliasMaximum) + } + fmt.Fprintf(&b, "\tRetainAvailable:%t\n", p.RetainAvailable) + if p.MaximumPacketSize != nil { + fmt.Fprintf(&b, "\tMaximumPacketSize:%d\n", *p.MaximumPacketSize) + } + fmt.Fprintf(&b, "\tWildcardSubAvailable:%t\n", p.WildcardSubAvailable) + fmt.Fprintf(&b, "\tSubIDAvailable:%t\n", p.SubIDAvailable) + fmt.Fprintf(&b, "\tSharedSubAvailable:%t\n", p.SharedSubAvailable) + + if len(p.User) > 0 { + fmt.Fprint(&b, "\tUser Properties:\n") + for _, v := range p.User { + fmt.Fprintf(&b, "\t\t%s:%s\n", v.Key, v.Value) + } + } + + return b.String() +}