-
Notifications
You must be signed in to change notification settings - Fork 64
/
connect_test.go
304 lines (260 loc) · 8.89 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
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
// Copyright 2015 Tony Bai.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package cmpp_test
import (
"testing"
"github.com/bigwhite/gocmpp"
)
var (
connSourceAddr = "900001"
connSecret = "888888"
connTimestamp uint32 = 1021080510
connVersion = cmpp.V21
connVersion1 = cmpp.V30
seqId uint32 = 0x17
)
func TestCmppConnReqPktPack(t *testing.T) {
p := &cmpp.CmppConnReqPkt{
SrcAddr: connSourceAddr,
Version: connVersion,
Secret: connSecret,
Timestamp: connTimestamp, //usually , we don't need to assign timestamp
}
data, err := p.Pack(seqId)
if err != nil {
t.Fatal("CmppConnReqPkt pack error:", err)
}
if p.SeqId != seqId {
t.Fatalf("After pack, seqId is %d, not equal to expected: %d\n", p.SeqId, seqId)
}
// data after pack expected:
//
// 00000000 00 00 00 27 00 00 00 01 00 00 00 17 39 30 30 30 |...'........9000|
// 00000010 30 31 90 d0 0c 1d 51 7a bd 0b 4f 65 f6 bc f8 53 |01....Qz..Oe...S|
// 00000020 5d 16 21 3c dc 73 be |].!<.s.|
dataExpected := []byte{
0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x17, 0x39, 0x30, 0x30, 0x30,
0x30, 0x31, 0x90, 0xd0, 0x0c, 0x1d, 0x51, 0x7a, 0xbd, 0x0b, 0x4f, 0x65, 0xf6, 0xbc, 0xf8, 0x53,
0x5d, 0x16, 0x21, 0x3c, 0xdc, 0x73, 0xbe,
}
l1 := len(data)
l2 := len(dataExpected)
if l1 != l2 {
t.Fatalf("After pack, data length is %d, not equal to length expected: %d\n", l1, l2)
}
for i := 0; i < l1; i++ {
if data[i] != dataExpected[i] {
t.Fatalf("After pack, data[%d] is %x, not equal to dataExpected[%d]: %x\n", i, data[i], i, dataExpected[i])
}
}
}
func TestCmppConnReqPktUnpack(t *testing.T) {
// connect request packet data:
//
// 00000000 00 00 00 27 00 00 00 01 00 00 00 17 39 30 30 30 |...'........9000|
// 00000010 30 31 90 d0 0c 1d 51 7a bd 0b 4f 65 f6 bc f8 53 |01....Qz..Oe...S|
// 00000020 5d 16 21 3c dc 73 be |].!<.s.|
data := []byte{
0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x17, 0x39, 0x30, 0x30, 0x30,
0x30, 0x31, 0x90, 0xd0, 0x0c, 0x1d, 0x51, 0x7a, 0xbd, 0x0b, 0x4f, 0x65, 0xf6, 0xbc, 0xf8, 0x53,
0x5d, 0x16, 0x21, 0x3c, 0xdc, 0x73, 0xbe,
}
p := &cmpp.CmppConnReqPkt{}
err := p.Unpack(data[8:])
if err != nil {
t.Fatal("CmppConnReqPkt unpack error:", err)
}
if p.SeqId != seqId {
t.Fatalf("After unpack, seqId in packet is %x, not equal to the expected value: %x\n", p.SeqId, seqId)
}
if p.SrcAddr != connSourceAddr {
t.Fatalf("After unpack, SrcAddr in packet is %s, not equal to the expected value: %s\n", p.SrcAddr, connSourceAddr)
}
if p.Version != connVersion {
t.Fatalf("After unpack, Version in packet is %x, not equal to the expected value: %x\n",
p.Version, connVersion)
}
if p.Timestamp != connTimestamp {
t.Fatalf("After unpack, Timestamp in packet is %d, not equal to the expected value: %d\n", p.Timestamp, connTimestamp)
}
authSrcExpected := []byte{
0x90, 0xd0, 0x0c, 0x1d, 0x51, 0x7a, 0xbd, 0x0b,
0x4f, 0x65, 0xf6, 0xbc, 0xf8, 0x53, 0x5d, 0x16,
}
authSrc := []byte(p.AuthSrc)
for i := 0; i < len(authSrc); i++ {
if authSrc[i] != authSrcExpected[i] {
t.Fatalf("After unpack, authSrc[%d] is %x, not equal to authsrcExpected[%d]: %x\n", i, authSrc[i], i, authSrcExpected[i])
}
}
}
func TestCmpp2ConnRspPktPack(t *testing.T) {
//AuthSrc: 90 d0 0c 1d 51 7a bd 0b 4f 65 f6 bc f8 53 5d 16
authSrc := []byte{
0x90, 0xd0, 0x0c, 0x1d, 0x51, 0x7a, 0xbd, 0x0b,
0x4f, 0x65, 0xf6, 0xbc, 0xf8, 0x53, 0x5d, 0x16,
}
p := &cmpp.Cmpp2ConnRspPkt{
Status: 0x0,
Version: connVersion,
Secret: connSecret,
AuthSrc: string(authSrc),
}
data, err := p.Pack(seqId)
if err != nil {
t.Fatal("Cmpp2ConnRspPkt pack error:", err)
}
if p.SeqId != seqId {
t.Fatalf("After pack, seqId is %d, not equal to expected: %d\n", p.SeqId, seqId)
}
// data after pack expected
dataExpected := []byte{
0x00, 0x00, 0x00, 0x1e, 0x80, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x17, 0x00,
0x6c, 0x0b, 0x84, 0x6e, 0x25, 0xba, 0xb6, 0xda, 0xa4, 0xed, 0x1c, 0x46, 0x6e,
0x0f, 0x4b, 0xd8, 0x21,
}
l1 := len(data)
l2 := len(dataExpected)
if l1 != l2 {
t.Fatalf("After pack, data length is %d, not equal to length expected: %d\n", l1, l2)
}
for i := 0; i < l1; i++ {
if data[i] != dataExpected[i] {
t.Fatalf("After pack, data[%d] is %x, not equal to dataExpected[%d]: %x\n", i, data[i], i, dataExpected[i])
}
}
}
func TestCmpp2ConnRspUnpack(t *testing.T) {
// cmpp2 connect response packet data:
data := []byte{
0x00, 0x00, 0x00, 0x1e, 0x80, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x17, 0x00,
0x6c, 0x0b, 0x84, 0x6e, 0x25, 0xba, 0xb6, 0xda, 0xa4, 0xed, 0x1c, 0x46, 0x6e,
0x0f, 0x4b, 0xd8, 0x21,
}
p := &cmpp.Cmpp2ConnRspPkt{}
err := p.Unpack(data[8:])
if err != nil {
t.Fatal("Cmpp2ConnRspPkt unpack error:", err)
}
if p.SeqId != seqId {
t.Fatalf("After unpack, seqId in packet is %x, not equal to the expected value: %x\n", p.SeqId, seqId)
}
if p.Version != connVersion {
t.Fatalf("After unpack, Version in packet is %x, not equal to the expected value: %x\n",
p.Version, connVersion)
}
if p.Status != 0x0 {
t.Fatalf("After unpack, Status in packet is %d, not equal to the expected value: %d\n", p.Status, 0x0)
}
authIsmgExpected := []byte{
0x6c, 0x0b, 0x84, 0x6e, 0x25, 0xba, 0xb6, 0xda,
0xa4, 0xed, 0x1c, 0x46, 0x6e, 0x0f, 0x4b, 0xd8,
}
authIsmg := []byte(p.AuthIsmg)
for i := 0; i < len(authIsmg); i++ {
if authIsmg[i] != authIsmgExpected[i] {
t.Fatalf("After unpack, authIsmg[%d] is %x, not equal to authIsmgExpected[%d]: %x\n", i, authIsmg[i], i, authIsmgExpected[i])
}
}
}
func TestCmpp3ConnRspPktPack(t *testing.T) {
//AuthSrc: 90 d0 0c 1d 51 7a bd 0b 4f 65 f6 bc f8 53 5d 16
authSrc := []byte{
0x90, 0xd0, 0x0c, 0x1d, 0x51, 0x7a, 0xbd, 0x0b,
0x4f, 0x65, 0xf6, 0xbc, 0xf8, 0x53, 0x5d, 0x16,
}
p := &cmpp.Cmpp3ConnRspPkt{
Status: 0x0,
Version: connVersion1,
Secret: connSecret,
AuthSrc: string(authSrc),
}
data, err := p.Pack(seqId)
if err != nil {
t.Fatal("Cmpp3ConnRspPkt pack error:", err)
}
if p.SeqId != seqId {
t.Fatalf("After pack, seqId is %d, not equal to expected: %d\n", p.SeqId, seqId)
}
// data after pack expected
dataExpected := []byte{
0x00, 0x00, 0x00, 0x21, 0x80, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00, 0x00,
0x79, 0x42, 0x97, 0x72, 0x74, 0x09, 0x8c, 0xf2, 0x10, 0xab, 0x0c, 0x16, 0xc3, 0x67, 0xbc, 0x8d,
0x30,
}
l1 := len(data)
l2 := len(dataExpected)
if l1 != l2 {
t.Fatalf("After pack, data length is %d, not equal to length expected: %d\n", l1, l2)
}
for i := 0; i < l1; i++ {
if data[i] != dataExpected[i] {
t.Fatalf("After pack, data[%d] is %x, not equal to dataExpected[%d]: %x\n", i, data[i], i, dataExpected[i])
}
}
}
func TestCmpp3ConnRspUnpack(t *testing.T) {
// cmpp3 connect response packet data:
data := []byte{
0x00, 0x00, 0x00, 0x21, 0x80, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00, 0x00,
0x79, 0x42, 0x97, 0x72, 0x74, 0x09, 0x8c, 0xf2, 0x10, 0xab, 0x0c, 0x16, 0xc3, 0x67, 0xbc, 0x8d,
0x30,
}
p := &cmpp.Cmpp3ConnRspPkt{}
err := p.Unpack(data[8:])
if err != nil {
t.Fatal("Cmpp3ConnRspPkt unpack error:", err)
}
if p.SeqId != seqId {
t.Fatalf("After unpack, seqId in packet is %x, not equal to the expected value: %x\n", p.SeqId, seqId)
}
if p.Version != connVersion1 {
t.Fatalf("After unpack, Version in packet is %x, not equal to the expected value: %x\n",
p.Version, connVersion)
}
if p.Status != 0x0 {
t.Fatalf("After unpack, Status in packet is %d, not equal to the expected value: %d\n", p.Status, 0x0)
}
authIsmgExpected := []byte{
0x79, 0x42, 0x97, 0x72, 0x74, 0x09, 0x8c, 0xf2,
0x10, 0xab, 0x0c, 0x16, 0xc3, 0x67, 0xbc, 0x8d,
}
authIsmg := []byte(p.AuthIsmg)
for i := 0; i < len(authIsmg); i++ {
if authIsmg[i] != authIsmgExpected[i] {
t.Fatalf("After unpack, authIsmg[%d] is %x, not equal to authIsmgExpected[%d]: %x\n", i, authIsmg[i], i, authIsmgExpected[i])
}
}
}
func BenchmarkCmppConnReqPktPack(b *testing.B) {
var p = &cmpp.CmppConnReqPkt{
SrcAddr: connSourceAddr,
Version: connVersion,
Secret: connSecret,
Timestamp: connTimestamp,
}
for i := 0; i < b.N; i++ {
p.Pack(seqId)
}
}
func BenchmarkCmppConnReqPktUnpack(b *testing.B) {
data := []byte{
0x00, 0x00, 0x00, 0x17, 0x39, 0x30, 0x30, 0x30,
0x30, 0x31, 0x90, 0xd0, 0x0c, 0x1d, 0x51, 0x7a, 0xbd, 0x0b, 0x4f, 0x65, 0xf6, 0xbc, 0xf8, 0x53,
0x5d, 0x16, 0x21, 0x3c, 0xdc, 0x73, 0xbe,
}
p := &cmpp.CmppConnReqPkt{}
for i := 0; i < b.N; i++ {
p.Unpack(data)
}
}