-
Notifications
You must be signed in to change notification settings - Fork 0
/
connack_test.go
104 lines (84 loc) · 3.36 KB
/
connack_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package mqtt
import (
"bytes"
"testing"
"github.com/srishina/mqtt.go/internal/packettype"
"github.com/srishina/mqtt.go/internal/properties"
"github.com/stretchr/testify/require"
)
func TestCodecConnAckPacket(t *testing.T) {
encoded := []byte{0x20, 0x03,
0x01, // session present
byte(ConnAckReasonCodeNotAuthorized), // Reason code
0x00, // no properties
}
reader := bytes.NewBuffer(encoded)
byte0, remainingLength, err := readFixedHeader(reader)
require.NoError(t, err, "Decoding CONNACK fixed header returned error")
require.Equal(t, packettype.CONNACK, packettype.PacketType(byte0>>4))
require.Equal(t, uint32(0x03), remainingLength)
ca := ConnAck{}
err = ca.decode(reader, remainingLength)
require.NoError(t, err, "ConnAck.decode returned an error")
require.True(t, ca.SessionPresent)
require.Equal(t, ConnAckReasonCodeNotAuthorized, ca.ReasonCode)
var buf bytes.Buffer
err = ca.encode(&buf)
require.NoError(t, err, "ConnAck.encode returned an error")
if !bytes.Equal(encoded, buf.Bytes()) {
t.Errorf("ConnAck.encode did not return expected bytes %v but %v ", encoded, buf.Bytes())
}
}
func TestCodecConnAckInvalidPacket(t *testing.T) {
encoded := []byte{0x20, 0x03,
0x01, // session present
byte(ConnAckReasonCodeNotAuthorized), // reason code not authorized
0x0A,
byte(properties.SessionExpiryIntervalID),
0x00, 0x00, 0x00, 0x0A,
byte(properties.SessionExpiryIntervalID),
0x00, 0x00, 0x00, 0x0A,
}
reader := bytes.NewBuffer(encoded)
byte0, remainingLength, err := readFixedHeader(reader)
require.NoError(t, err, "Decoding CONNACK fixed header returned error")
require.Equal(t, packettype.CONNACK, packettype.PacketType(byte0>>4))
require.Equal(t, uint32(0x03), remainingLength)
ca := ConnAck{}
err = ca.decode(reader, remainingLength)
require.Error(t, err, "ConnAck.decode did not return an error")
}
func TestCodecConnAckPacketWithProperties(t *testing.T) {
encoded := []byte{0x20, 0x0B,
0x01, // session present
byte(ConnAckReasonCodeNotAuthorized), // Reason code
0x08, // properties
byte(properties.ReceiveMaximumID),
0x00, 0x0A, // receive maximum
byte(properties.MaximumQoSID),
0x01, // max qos = 1
byte(properties.TopicAliasMaximumID),
0x00, 0x0A, // receive maximum
}
reader := bytes.NewBuffer(encoded)
byte0, remainingLength, err := readFixedHeader(reader)
require.NoError(t, err, "Decoding CONNACK fixed header returned error")
require.Equal(t, packettype.CONNACK, packettype.PacketType(byte0>>4))
require.Equal(t, uint32(0x0B), remainingLength)
ca := ConnAck{}
err = ca.decode(reader, remainingLength)
require.NoError(t, err, "ConnAck.decode returned an error")
require.NotNil(t, ca.Properties)
require.NotNil(t, ca.Properties.TopicAliasMaximum)
require.Equal(t, uint16(10), *ca.Properties.TopicAliasMaximum)
require.NotNil(t, ca.Properties.MaximumQoS)
require.Equal(t, byte(1), *ca.Properties.MaximumQoS)
require.NotNil(t, ca.Properties.ReceiveMaximum)
require.Equal(t, uint16(10), *ca.Properties.ReceiveMaximum)
var buf bytes.Buffer
err = ca.encode(&buf)
require.NoError(t, err, "ConnAck.encode returned an error")
if !bytes.Equal(encoded, buf.Bytes()) {
t.Errorf("ConnAck.encode did not return expected bytes %v but %v ", encoded, buf.Bytes())
}
}