forked from antlinker/libmqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpkt_pub_test.go
235 lines (190 loc) · 5.69 KB
/
pkt_pub_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
/*
* Copyright Go-IIoT (https://github.com/goiiot)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package libmqtt
import (
"bytes"
"testing"
std "github.com/eclipse/paho.mqtt.golang/packets"
)
// pub test data
var (
testPubMsgs []*PublishPacket
testPubAckMsg = &PubAckPacket{
PacketID: testPacketID,
Code: CodeUnspecifiedError,
Props: &PubAckProps{
Reason: "MQTT",
UserProps: testConstUserProps,
},
}
testPubRecvMsg = &PubRecvPacket{
PacketID: testPacketID,
Code: CodeUnspecifiedError,
Props: &PubRecvProps{
Reason: "MQTT",
UserProps: testConstUserProps,
},
}
testPubRelMsg = &PubRelPacket{
PacketID: testPacketID,
Code: CodeUnspecifiedError,
Props: &PubRelProps{
Reason: "MQTT",
UserProps: testConstUserProps,
},
}
testPubCompMsg = &PubCompPacket{
PacketID: testPacketID,
Code: CodeUnspecifiedError,
Props: &PubCompProps{
Reason: "MQTT",
UserProps: testConstUserProps,
},
}
// mqtt 3.1.1
testPubMsgBytesV311 [][]byte
testPubAckMsgBytesV311 []byte
testPubRecvMsgBytesV311 []byte
testPubRelMsgBytesV311 []byte
testPubCompMsgBytesV311 []byte
// mqtt 5.0
testPubMsgBytesV5 [][]byte
testPubAckMsgBytesV5 []byte
testPubRecvMsgBytesV5 []byte
testPubRelMsgBytesV5 []byte
testPubCompMsgBytesV5 []byte
)
// init pub test data
func initTestData_Pub() {
size := len(testTopics)
testPubMsgs = make([]*PublishPacket, size)
testPubMsgBytesV311 = make([][]byte, size)
testPubMsgBytesV5 = make([][]byte, size)
for i := range testTopics {
msgID := uint16(i + 1)
testPubMsgs[i] = &PublishPacket{
IsDup: testPubDup,
TopicName: testTopics[i],
Qos: testTopicQos[i],
Payload: []byte(testTopicMsgs[i]),
PacketID: msgID,
Props: &PublishProps{
PayloadFormat: 100,
MessageExpiryInterval: 100,
TopicAlias: 100,
RespTopic: "MQTT",
CorrelationData: []byte("MQTT"),
UserProps: testConstUserProps,
SubIDs: []int{1, 2, 3},
ContentType: "MQTT",
},
}
// create standard publish packet and make bytes
pktRaw, err := std.NewControlPacketWithHeader(std.FixedHeader{
MessageType: std.Publish,
Dup: testPubDup,
Qos: testTopicQos[i],
})
if err != nil {
panic(err)
}
pkt := pktRaw.(*std.PublishPacket)
pkt.TopicName = testTopics[i]
pkt.Payload = []byte(testTopicMsgs[i])
pkt.MessageID = msgID
buf := new(bytes.Buffer)
err = pkt.Write(buf)
if err != nil {
panic(err)
}
testPubMsgBytesV311[i] = buf.Bytes()
testPubMsgBytesV5[i] = newV5TestPacketBytes(CtrlPublish, 0, nil, nil)
}
// puback
pubAckPkt := std.NewControlPacket(std.Puback).(*std.PubackPacket)
pubAckPkt.MessageID = testPacketID
pubAckBuf := new(bytes.Buffer)
_ = pubAckPkt.Write(pubAckBuf)
testPubAckMsgBytesV311 = pubAckBuf.Bytes()
testPubAckMsgBytesV5 = newV5TestPacketBytes(CtrlPubAck, 0, nil, nil)
// pubrecv
pubRecvBuf := new(bytes.Buffer)
pubRecPkt := std.NewControlPacket(std.Pubrec).(*std.PubrecPacket)
pubRecPkt.MessageID = testPacketID
_ = pubRecPkt.Write(pubRecvBuf)
testPubRecvMsgBytesV311 = pubRecvBuf.Bytes()
testPubRecvMsgBytesV5 = newV5TestPacketBytes(CtrlPubRecv, 0, nil, nil)
// pubrel
pubRelBuf := new(bytes.Buffer)
pubRelPkt := std.NewControlPacket(std.Pubrel).(*std.PubrelPacket)
pubRelPkt.MessageID = testPacketID
_ = pubRelPkt.Write(pubRelBuf)
testPubRelMsgBytesV311 = pubRelBuf.Bytes()
testPubRelMsgBytesV5 = newV5TestPacketBytes(CtrlPubRel, 0, nil, nil)
// pubcomp
pubCompBuf := new(bytes.Buffer)
pubCompPkt := std.NewControlPacket(std.Pubcomp).(*std.PubcompPacket)
pubCompPkt.MessageID = testPacketID
_ = pubCompPkt.Write(pubCompBuf)
testPubCompMsgBytesV311 = pubCompBuf.Bytes()
testPubCompMsgBytesV5 = newV5TestPacketBytes(CtrlPubComp, 0, nil, nil)
}
func TestPublishPacket_Bytes(t *testing.T) {
for i, p := range testPubMsgs {
testPacketBytes(V311, p, testPubMsgBytesV311[i], t)
//testPacketBytes(V5, p, testPubMsgBytesV5[i], t)
}
}
func TestPubProps_Props(t *testing.T) {
}
func TestPubProps_SetProps(t *testing.T) {
}
func TestPubAckPacket_Bytes(t *testing.T) {
testPacketBytes(V311, testPubAckMsg, testPubAckMsgBytesV311, t)
t.Skip("v5")
testPacketBytes(V5, testPubAckMsg, testPubAckMsgBytesV5, t)
}
func TestPubAckProps_Props(t *testing.T) {
}
func TestPubAckProps_SetProps(t *testing.T) {
}
func TestPubRecvPacket_Bytes(t *testing.T) {
testPacketBytes(V311, testPubRecvMsg, testPubRecvMsgBytesV311, t)
t.Skip("v5")
testPacketBytes(V5, testPubRecvMsg, testPubRecvMsgBytesV5, t)
}
func TestPubRecvProps_Props(t *testing.T) {
}
func TestPubRecvProps_SetProps(t *testing.T) {
}
func TestPubRelPacket_Bytes(t *testing.T) {
testPacketBytes(V311, testPubRelMsg, testPubRelMsgBytesV311, t)
t.Skip("v5")
testPacketBytes(V5, testPubRelMsg, testPubRelMsgBytesV5, t)
}
func TestPubRelProps_Props(t *testing.T) {
}
func TestPubRelProps_SetProps(t *testing.T) {
}
func TestPubCompPacket_Bytes(t *testing.T) {
testPacketBytes(V311, testPubCompMsg, testPubCompMsgBytesV311, t)
t.Skip("v5")
testPacketBytes(V5, testPubCompMsg, testPubCompMsgBytesV5, t)
}
func TestPubCompProps_Props(t *testing.T) {
}
func TestPubCompProps_SetProps(t *testing.T) {
}