-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathlibmqtt_test.go
325 lines (280 loc) · 8.45 KB
/
libmqtt_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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package libmqtt
import (
"bytes"
"math"
"testing"
std "github.com/eclipse/paho.mqtt.golang/packets"
)
// common test data
var (
testUsername = "foo"
testPassword = "bar"
testClientID = "1"
testCleanSession = true
testWill = true
testWillQos = Qos2
testWillRetain = true
testWillTopic = "foo"
testWillMessage = []byte("bar")
testKeepalive = uint16(1)
testProtoVersion = V311
testPubDup = false
testTopics = []string{
"/test",
"/test/foo",
"/test/bar",
}
testTopicQos = []QosLevel{
Qos0,
Qos1,
Qos2,
}
testTopicMsgs = []string{
"test data qos 0",
"foo data qos 1",
"bar data qos 2",
}
testPacketID uint16 = math.MaxUint16 / 2
testConnackPresent = true
testConnackCode = ConnAccepted
testSubAckCodes = []SubAckCode{SubOkMaxQos0, SubOkMaxQos1, SubFail}
)
// conn test data
var (
testConnWillMsg *ConnPacket
testConnWillMsgBytes []byte
testConnMsg *ConnPacket
testConnMsgBytes []byte
testConnAckMsg *ConnAckPacket
testConnAckMsgBytes []byte
testDisConnMsg = DisConnPacket
testDisConnMsgBytes []byte
)
func initConn() {
testConnWillMsg = &ConnPacket{
Username: testUsername,
Password: testPassword,
protoLevel: testProtoVersion,
ClientID: testClientID,
CleanSession: testCleanSession,
IsWill: testWill,
WillQos: testWillQos,
WillRetain: testWillRetain,
WillTopic: testWillTopic,
WillMessage: testWillMessage,
Keepalive: testKeepalive,
}
connWillPkt := std.NewControlPacket(std.Connect).(*std.ConnectPacket)
connWillPkt.Username = testUsername
connWillPkt.UsernameFlag = true
connWillPkt.Password = []byte(testPassword)
connWillPkt.PasswordFlag = true
connWillPkt.ProtocolName = "MQTT"
connWillPkt.ProtocolVersion = testProtoVersion
connWillPkt.ClientIdentifier = testClientID
connWillPkt.CleanSession = testCleanSession
connWillPkt.WillFlag = testWill
connWillPkt.WillQos = testWillQos
connWillPkt.WillRetain = testWillRetain
connWillPkt.WillTopic = testWillTopic
connWillPkt.WillMessage = testWillMessage
connWillPkt.Keepalive = testKeepalive
connWillBuf := &bytes.Buffer{}
connWillPkt.Write(connWillBuf)
testConnWillMsgBytes = connWillBuf.Bytes()
testConnMsg = &ConnPacket{
Username: testUsername,
Password: testPassword,
protoLevel: testProtoVersion,
ClientID: testClientID,
CleanSession: testCleanSession,
Keepalive: testKeepalive,
}
connPkt := std.NewControlPacket(std.Connect).(*std.ConnectPacket)
connPkt.Username = testUsername
connPkt.UsernameFlag = true
connPkt.Password = []byte(testPassword)
connPkt.PasswordFlag = true
connPkt.ProtocolName = "MQTT"
connPkt.ProtocolVersion = testProtoVersion
connPkt.ClientIdentifier = testClientID
connPkt.CleanSession = testCleanSession
connPkt.Keepalive = testKeepalive
connBuf := &bytes.Buffer{}
connPkt.Write(connBuf)
testConnMsgBytes = connBuf.Bytes()
testConnAckMsg = &ConnAckPacket{
Present: testConnackPresent,
Code: testConnackCode,
}
connackPkt := std.NewControlPacket(std.Connack).(*std.ConnackPacket)
connackPkt.SessionPresent = testConnackPresent
connackPkt.ReturnCode = testConnackCode
connAckBuf := &bytes.Buffer{}
connackPkt.Write(connAckBuf)
testConnAckMsgBytes = connAckBuf.Bytes()
disConnPkt := std.NewControlPacket(std.Disconnect).(*std.DisconnectPacket)
disconnBuf := &bytes.Buffer{}
disConnPkt.Write(disconnBuf)
testDisConnMsgBytes = disconnBuf.Bytes()
}
// sub test data
var (
testSubTopics []*Topic
testSubMsgs []*SubscribePacket
testSubMsgBytes [][]byte
testSubAckMsgs []*SubAckPacket
testSubAckMsgBytes [][]byte
testUnSubMsgs []*UnSubPacket
testUnSubMsgBytes [][]byte
testUnSubAckMsg *UnSubAckPacket
testUnSubAckMsgBytes []byte
)
func initSub() {
testSubTopics = make([]*Topic, len(testTopics))
for i := range testSubTopics {
testSubTopics[i] = &Topic{Name: testTopics[i], Qos: testTopicQos[i]}
}
testSubMsgs = make([]*SubscribePacket, len(testTopics))
testSubMsgBytes = make([][]byte, len(testTopics))
testSubAckMsgs = make([]*SubAckPacket, len(testTopics))
testSubAckMsgBytes = make([][]byte, len(testTopics))
testUnSubMsgs = make([]*UnSubPacket, len(testTopics))
testUnSubMsgBytes = make([][]byte, len(testTopics))
for i := range testTopics {
testSubMsgs[i] = &SubscribePacket{
Topics: testSubTopics[:i+1],
PacketID: testPacketID,
}
subPkt := std.NewControlPacket(std.Subscribe).(*std.SubscribePacket)
subPkt.Topics = testTopics[:i+1]
subPkt.Qoss = testTopicQos[:i+1]
subPkt.MessageID = testPacketID
subBuf := &bytes.Buffer{}
subPkt.Write(subBuf)
testSubMsgBytes[i] = subBuf.Bytes()
testSubAckMsgs[i] = &SubAckPacket{
PacketID: testPacketID,
Codes: testSubAckCodes[:i+1],
}
subAckPkt := std.NewControlPacket(std.Suback).(*std.SubackPacket)
subAckPkt.MessageID = testPacketID
subAckPkt.ReturnCodes = testSubAckCodes[:i+1]
subAckBuf := &bytes.Buffer{}
subAckPkt.Write(subAckBuf)
testSubAckMsgBytes[i] = subAckBuf.Bytes()
testUnSubMsgs[i] = &UnSubPacket{
PacketID: testPacketID,
TopicNames: testTopics[:i+1],
}
unsubPkt := std.NewControlPacket(std.Unsubscribe).(*std.UnsubscribePacket)
unsubPkt.Topics = testTopics[:i+1]
unsubPkt.MessageID = testPacketID
unSubBuf := &bytes.Buffer{}
unsubPkt.Write(unSubBuf)
testUnSubMsgBytes[i] = unSubBuf.Bytes()
}
unSunAckBuf := &bytes.Buffer{}
testUnSubAckMsg = &UnSubAckPacket{PacketID: testPacketID}
unsubAckPkt := std.NewControlPacket(std.Unsuback).(*std.UnsubackPacket)
unsubAckPkt.MessageID = testPacketID
unsubAckPkt.Write(unSunAckBuf)
testUnSubAckMsgBytes = unSunAckBuf.Bytes()
}
// pub test data
var (
testPubMsgs []*PublishPacket
testPubMsgBytes [][]byte
testPubAckMsg *PubAckPacket
testPubAckMsgBytes []byte
testPubRecvMsg *PubRecvPacket
testPubRecvMsgBytes []byte
testPubRelMsg *PubRelPacket
testPubRelMsgBytes []byte
testPubCompMsg *PubCompPacket
testPubCompMsgBytes []byte
)
// init pub test data
func initPub() {
testPubMsgs = make([]*PublishPacket, len(testTopics))
testPubMsgBytes = make([][]byte, len(testTopics))
for i := range testTopics {
testPubMsgs[i] = &PublishPacket{
IsDup: testPubDup,
TopicName: testTopics[i],
Qos: testTopicQos[i],
Payload: []byte(testTopicMsgs[i]),
PacketID: testPacketID,
}
// create standard publish packet and make bytes
pkt := std.NewControlPacketWithHeader(std.FixedHeader{
MessageType: std.Publish,
Dup: testPubDup,
Qos: testTopicQos[i],
}).(*std.PublishPacket)
pkt.TopicName = testTopics[i]
pkt.Payload = []byte(testTopicMsgs[i])
pkt.MessageID = testPacketID
buf := &bytes.Buffer{}
pkt.Write(buf)
testPubMsgBytes[i] = buf.Bytes()
}
testPubAckMsg = &PubAckPacket{PacketID: testPacketID}
pubAckPkt := std.NewControlPacket(std.Puback).(*std.PubackPacket)
pubAckPkt.MessageID = testPacketID
pubAckBuf := &bytes.Buffer{}
pubAckPkt.Write(pubAckBuf)
testPubAckMsgBytes = pubAckBuf.Bytes()
pubRecvBuf := &bytes.Buffer{}
testPubRecvMsg = &PubRecvPacket{PacketID: testPacketID}
pubRecPkt := std.NewControlPacket(std.Pubrec).(*std.PubrecPacket)
pubRecPkt.MessageID = testPacketID
pubRecPkt.Write(pubRecvBuf)
testPubRecvMsgBytes = pubRecvBuf.Bytes()
pubRelBuf := &bytes.Buffer{}
testPubRelMsg = &PubRelPacket{PacketID: testPacketID}
pubRelPkt := std.NewControlPacket(std.Pubrel).(*std.PubrelPacket)
pubRelPkt.MessageID = testPacketID
pubRelPkt.Write(pubRelBuf)
testPubRelMsgBytes = pubRelBuf.Bytes()
pubCompBuf := &bytes.Buffer{}
testPubCompMsg = &PubCompPacket{PacketID: testPacketID}
pubCompPkt := std.NewControlPacket(std.Pubcomp).(*std.PubcompPacket)
pubCompPkt.MessageID = testPacketID
pubCompPkt.Write(pubCompBuf)
testPubCompMsgBytes = pubCompBuf.Bytes()
}
var (
testPingReqMsg = PingReqPacket
testPingReqMsgBytes []byte
testPingRespMsg = PingRespPacket
testPingRespMsgBytes []byte
)
func initPing() {
reqBuf := &bytes.Buffer{}
req := std.NewControlPacket(std.Pingreq).(*std.PingreqPacket)
req.Write(reqBuf)
testPingReqMsgBytes = reqBuf.Bytes()
respBuf := &bytes.Buffer{}
resp := std.NewControlPacket(std.Pingresp).(*std.PingrespPacket)
resp.Write(respBuf)
testPingRespMsgBytes = respBuf.Bytes()
}
func testBytes(pkt Packet, target []byte, t *testing.T) {
buf := &bytes.Buffer{}
if err := pkt.WriteTo(buf); err != nil {
t.Log(err)
t.Fail()
}
if bytes.Compare(buf.Bytes(), target) != 0 {
t.Log("S", buf.Bytes())
t.Log("T", target)
t.Fail()
}
}
func init() {
initPing()
initConn()
initSub()
initPub()
}