forked from antlinker/libmqtt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pkt_sub_test.go
138 lines (120 loc) · 3.99 KB
/
pkt_sub_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
/*
* 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"
)
// sub test data
var (
testSubTopics []*Topic
testSubMsgs []*SubscribePacket
testSubAckMsgs []*SubAckPacket
testUnSubMsgs []*UnSubPacket
testUnSubAckMsg *UnSubAckPacket
// mqtt 3.1.1
testSubMsgBytesV311 [][]byte
testSubAckMsgBytesV311 [][]byte
testUnSubMsgBytesV311 [][]byte
testUnSubAckMsgBytesV311 []byte
// mqtt 5.0
testSubMsgBytesV5 [][]byte
testSubAckMsgBytesV5 [][]byte
testUnSubMsgBytesV5 [][]byte
testUnSubAckMsgBytesV5 []byte
)
func initTestData_Sub() {
testSubTopics = make([]*Topic, len(testTopics))
for i := range testSubTopics {
testSubTopics[i] = &Topic{Name: testTopics[i], Qos: testTopicQos[i]}
}
testSubMsgs = make([]*SubscribePacket, len(testTopics))
testSubMsgBytesV311 = make([][]byte, len(testTopics))
testSubAckMsgs = make([]*SubAckPacket, len(testTopics))
testSubAckMsgBytesV311 = make([][]byte, len(testTopics))
testUnSubMsgs = make([]*UnSubPacket, len(testTopics))
testUnSubMsgBytesV311 = 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)
testSubMsgBytesV311[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)
testSubAckMsgBytesV311[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)
testUnSubMsgBytesV311[i] = unSubBuf.Bytes()
}
unSunAckBuf := &bytes.Buffer{}
testUnSubAckMsg = &UnSubAckPacket{PacketID: testPacketID}
unsubAckPkt := std.NewControlPacket(std.Unsuback).(*std.UnsubackPacket)
unsubAckPkt.MessageID = testPacketID
unsubAckPkt.Write(unSunAckBuf)
testUnSubAckMsgBytesV311 = unSunAckBuf.Bytes()
}
func TestSubscribePacket_Bytes(t *testing.T) {
for i, p := range testSubMsgs {
p.ProtoVersion = V311
testPacketBytes(p, testSubMsgBytesV311[i], t)
// p.ProtoVersion = V5
// testPacketBytes(p, testSubMsgBytesV5[i], t)
}
}
func TestSubAckPacket_Bytes(t *testing.T) {
for i, p := range testSubAckMsgs {
p.ProtoVersion = V311
testPacketBytes(p, testSubAckMsgBytesV311[i], t)
// p.ProtoVersion = V5
// testPacketBytes(p, testSubAckMsgBytesV5[i], t)
}
}
func TestUnSubPacket_Bytes(t *testing.T) {
for i, p := range testUnSubMsgs {
p.ProtoVersion = V311
testPacketBytes(p, testUnSubMsgBytesV311[i], t)
// p.ProtoVersion = V5
// testPacketBytes(p, testUnSubMsgBytesV5[i], t)
}
}
func TestUnSubAckPacket_Bytes(t *testing.T) {
testUnSubAckMsg.ProtoVersion = V311
testPacketBytes(testUnSubAckMsg, testUnSubAckMsgBytesV311, t)
// testUnSubAckMsg.ProtoVersion = V5
// testPacketBytes(testUnSubAckMsg, testUnSubAckMsgBytesV5, t)
}