-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect_test.go
152 lines (128 loc) · 5.15 KB
/
connect_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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package mqtt
import (
"bytes"
"testing"
"github.com/srishina/mqtt.go/internal/packettype"
"github.com/stretchr/testify/require"
)
func TestInvalidConnectProtocolVersion(t *testing.T) {
encoded := []byte{0x00, 0x04, 'T', 'T', 'Q', 'M', 0x05}
c := Connect{}
err := c.decode(bytes.NewBuffer(encoded), 4)
require.EqualError(t, err, ErrInvalidProtocolName.Error(), "Connect.Decode did not return an error code for invalid protocol name")
}
func TestInvalidConnectFlags(t *testing.T) {
packets := map[error][]byte{
ErrInvalidConnectFlags: {0x00, 0x04, 'M', 'Q', 'T', 'T', 0x05, 0x01},
ErrInvalidWillQos: {0x00, 0x04, 'M', 'Q', 'T', 'T', 0x05, 0x1C}, // 3.1.2.6
ErrInvalidWillRetain: {0x00, 0x04, 'M', 'Q', 'T', 'T', 0x05, 0x20}, // 3.1.2.7
}
for e, encoded := range packets {
c := Connect{}
err := c.decode(bytes.NewBuffer(encoded), 4)
require.EqualError(t, err, e.Error(), "Connect.Decode did not return an expected error code")
}
}
func TestCodecConnectPacket(t *testing.T) {
encoded := []byte{0x10, 0x1B,
0x00, 0x04, 'M', 'Q', 'T', 'T',
0x05, // protocol version
0xC2, // Username=1, password=1, retain=0, qos=0, will=0, clean start=1, reserved=0
0x00, 0x18, // Keep alive - 24
0x00, // properties
0x00, 0x00, // client id
0x00, 0x05, 'h', 'e', 'l', 'l', 'o', // username
0x00, 0x05, 'w', 'o', 'r', 'l', 'd', // username
}
reader := bytes.NewBuffer(encoded)
byte0, remainingLength, err := readFixedHeader(reader)
require.NoError(t, err, "Decoding CONNECT fixed header returned error")
require.Equal(t, packettype.CONNECT, packettype.PacketType(byte0>>4))
require.Equal(t, uint32(0x1B), remainingLength)
c := Connect{}
err = c.decode(reader, remainingLength)
require.NoError(t, err, "Connect.decode did returned an error")
require.Equal(t, "MQTT", c.protocolName)
require.Equal(t, byte(0x05), c.protocolVersion)
require.True(t, c.CleanStart)
require.Equal(t, uint16(24), c.KeepAlive)
require.Equal(t, c.ClientID, "")
require.Equal(t, "hello", c.UserName)
require.Equal(t, []byte{'w', 'o', 'r', 'l', 'd'}, c.Password)
var buf bytes.Buffer
err = c.encode(&buf)
require.NoError(t, err, "Connect.encode did returned an error")
if !bytes.Equal(encoded, buf.Bytes()) {
t.Errorf("Connect.encode did not return expected bytes %v but %v ", encoded, buf.Bytes())
}
}
func TestCodecConnectPacketWithProperties(t *testing.T) {
encoded := []byte{0x10, 0x23,
0x00, 0x04, 0x4d, 0x51, 0x54, 0x54, // MQTT
0x05, // protocol version
0xC2,
0x00, 0x18, // Keep alive - 24
0x08, // properties
0x21, 0x00, 0x0A, // receive maximum
0x27, 0x00, 0x00, 0x04, 0x00, // maximum packet size
0x00, 0x00, // client id
0x00, 0x05, 0x68, 0x65, 0x6C, 0x6C, 0x6F, // username - "hello"
0x00, 0x05, 0x77, 0x6F, 0x72, 0x6C, 0x64, // password - "world"
}
reader := bytes.NewBuffer(encoded)
byte0, remainingLength, err := readFixedHeader(reader)
require.NoError(t, err, "Decoding CONNECT fixed header returned error")
require.Equal(t, packettype.CONNECT, packettype.PacketType(byte0>>4))
require.Equal(t, uint32(0x23), remainingLength)
c := Connect{}
err = c.decode(reader, remainingLength)
require.NoError(t, err, "Connect.decode did returned an error")
require.NotNil(t, c.Properties)
require.NotNil(t, c.Properties.ReceiveMaximum)
require.Equal(t, uint16(10), *c.Properties.ReceiveMaximum)
require.NotNil(t, uint32(1024), *c.Properties.MaximumPacketSize)
var buf bytes.Buffer
err = c.encode(&buf)
require.NoError(t, err, "Connect.encode did returned an error")
if !bytes.Equal(encoded, buf.Bytes()) {
t.Errorf("Connect.encode did not return expected bytes %v but %v ", encoded, buf.Bytes())
}
}
func TestCodecConnectPacketWithWillMessage(t *testing.T) {
encoded := []byte{0x10, 0x38,
0x00, 0x04, 0x4d, 0x51, 0x54, 0x54, // MQTT
0x05, // protocol version
0xCE,
0x00, 0x18, // Keep alive - 24
0x08, // properties
0x21, 0x00, 0x0A, // receive maximum
0x27, 0x00, 0x00, 0x04, 0x00, // maximum packet size
0x00, 0x00, // client id
0x05,
0x18, 0x00, 0x00, 0x04, 0x00,
0x00, 0x03, 0x61, 0x2F, 0x62,
0x00, 0x08, 0x57, 0x65, 0x6C, 0x63, 0x6F, 0x6D, 0x65, 0x21,
0x00, 0x05, 0x68, 0x65, 0x6C, 0x6C, 0x6F, // username - "hello"
0x00, 0x05, 0x77, 0x6F, 0x72, 0x6C, 0x64, // password - "world"
}
reader := bytes.NewBuffer(encoded)
byte0, remainingLength, err := readFixedHeader(reader)
require.NoError(t, err, "Decoding CONNECT fixed header returned error")
require.Equal(t, packettype.CONNECT, packettype.PacketType(byte0>>4))
require.Equal(t, uint32(0x38), remainingLength)
c := Connect{}
err = c.decode(reader, remainingLength)
require.NoError(t, err, "Connect.decode did returned an error")
require.Equal(t, byte(1), c.WillQoS)
require.False(t, c.WillRetain)
require.Equal(t, "a/b", c.WillTopic)
require.Equal(t, "Welcome!", string(c.WillPayload))
require.NotNil(t, c.WillProperties)
require.Equal(t, uint32(1024), *c.WillProperties.WillDelayInterval)
var buf bytes.Buffer
err = c.encode(&buf)
require.NoError(t, err, "Connect.encode did returned an error")
if !bytes.Equal(encoded, buf.Bytes()) {
t.Errorf("Connect.encode did not return expected bytes %v but %v ", encoded, buf.Bytes())
}
}