-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdu_test.go
451 lines (425 loc) · 11.9 KB
/
pdu_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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
package zkm
import (
"encoding/hex"
"fmt"
"reflect"
"testing"
)
func BenchmarkPduDeserialize(b *testing.B) {
raw, err := hex.DecodeString("000000D70000000500000000000000030001013739353030383932353638000001373737000400000000000" +
"001007A69643A63623963343066312D306161312D346233652D616662382D376464323464303361373136207375623A3030312" +
"0646C7672643A303031207375626D697420646174653A3230313032343132303520646F6E6520646174653A323031303234313" +
"2303620737461743A44454C49565244206572723A303030001E002563623963343066312D306161312D346233652D616662382" +
"D376464323464303361373136000427000102")
if err != nil {
panic(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
pdu := NewEmptyPdu()
if err := pdu.Deserialize(raw); err != nil {
panic(err)
}
}
}
func TestPdu(t *testing.T) {
type header struct {
l uint32
id Id
status Status
seq uint32
}
type mandatoryParams []struct {
name Name
value interface{}
}
type optionalParams []struct {
tag Tag
value interface{}
}
createPdu := func(header header, mandatoryParams mandatoryParams, optionalParams optionalParams) (*Pdu, error) {
pdu := NewPdu(header.id)
pdu.SetStatus(header.status)
pdu.SetSeq(header.seq)
for _, p := range mandatoryParams {
if mp, err := pdu.mandatoryParams.get(p.name); err != nil {
return nil, err
} else {
mp.value().set(p.value)
}
}
for _, p := range optionalParams {
if err := pdu.optionalParams.add(p.tag, p.value); err != nil {
return nil, err
}
}
return pdu, nil
}
assertHeader := func(pdu *Pdu, expected header) error {
if pdu.Len() != expected.l {
return fmt.Errorf("len [%v] not equals expected [%v]", pdu.Len(), expected.l)
}
if pdu.Id() != expected.id {
return fmt.Errorf("id [%v] not equals expected [%v]", pdu.Id(), expected.id)
}
if pdu.Status() != expected.status {
return fmt.Errorf("status [%v] not equals expected [%v]", pdu.Status(), expected.status)
}
if pdu.Seq() != expected.seq {
return fmt.Errorf("seq [%v] not equals expected [%v]", pdu.Seq(), expected.seq)
}
return nil
}
assertMandatoryParams := func(pdu *Pdu, expected mandatoryParams) error {
for _, e := range expected {
if p, err := pdu.mandatoryParams.get(e.name); err != nil {
return fmt.Errorf("[%v] %v", e.name, err)
} else {
equals := false
switch v := e.value.(type) {
case []byte:
equals = reflect.DeepEqual(p.value().raw(), v)
case string:
equals = p.value().String() == v
case int:
if u, err := p.value().uint32(); err == nil {
equals = u == uint32(v)
}
}
if !equals {
return fmt.Errorf("[%v] value %v not equals expected [%v]", e.name, p.value(), e.value)
}
}
}
return nil
}
assertOptionalParams := func(pdu *Pdu, expected optionalParams) error {
for _, e := range expected {
if p, err := pdu.optionalParams.get(e.tag); err != nil {
return fmt.Errorf("[%v] %v", e.tag, err)
} else {
equals := false
switch v := e.value.(type) {
case []byte:
equals = reflect.DeepEqual(p.value().raw(), v)
case string:
equals = p.value().String() == v
case int:
if u, err := p.value().uint32(); err == nil {
equals = u == uint32(v)
}
}
if !equals {
return fmt.Errorf("[%v] value %v not equals expected [%v]", e.tag, p.value(), e.value)
}
}
}
return nil
}
assert := func(pdu *Pdu, expectedHeader header, expectedMandatoryParams mandatoryParams,
expectedOptionalParams optionalParams, b []byte) error {
if err := assertHeader(pdu, expectedHeader); err != nil {
return err
}
if err := assertMandatoryParams(pdu, expectedMandatoryParams); err != nil {
return err
}
if err := assertOptionalParams(pdu, expectedOptionalParams); err != nil {
return err
}
if !reflect.DeepEqual(pdu.Serialize(), b) {
return fmt.Errorf("serialize [%v] not equals expected [%v]", pdu.Serialize(), b)
}
return nil
}
createBytes := func(s string) []byte {
if b, err := hex.DecodeString(s); err == nil {
return b
} else {
panic(err)
}
}
tests := []struct {
id int
b []byte
ok bool
header header
mandatoryParams mandatoryParams
optionalParams optionalParams
}{
{
id: 1,
b: createBytes("0000002A0000000200000000000000026175746F5F636C69656E740070617373776F7264000034000000"),
ok: true,
header: header{
l: 42,
id: BindTransmitter,
status: EsmeROk,
seq: 2,
},
mandatoryParams: mandatoryParams{
{AddrTON, 0},
{AddrNPI, 0},
{AddressRange, ""},
{SystemID, "auto_client"},
{Password, "password"},
{SystemType, ""},
{InterfaceVersion, 52},
},
},
{
id: 2,
b: createBytes("00000016800000010000000000000001000210000134"),
ok: true,
header: header{
l: 22,
id: BindReceiverResp,
status: EsmeROk,
seq: 1,
},
mandatoryParams: mandatoryParams{
{SystemID, ""},
},
optionalParams: optionalParams{
{TagScInterfaceVersion, 52},
},
},
{
id: 3,
b: createBytes("0000004A00000004000000000000000300000137373700010137393530303839323536380000000000323031" +
"3032343132303630343030302B00010000000B48656C6C6F20576F726C64"),
ok: true,
header: header{
l: 74,
id: SubmitSm,
status: EsmeROk,
seq: 3,
},
mandatoryParams: mandatoryParams{
{SourceAddrTON, 0},
{SourceAddr, "777"},
{ScheduleDeliveryTime, ""},
{ReplaceIfPresentFlag, 0},
{SMLength, 11},
{DestAddrTON, 1},
{ShortMessage, []byte{72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100}},
{ServiceType, ""},
{SourceAddrNPI, 1},
{DestinationAddr, "79500892568"},
{ESMClass, 0},
{ProtocolID, 0},
{SMDefaultMsgID, 0},
{DestAddrNPI, 1},
{PriorityFlag, 0},
{ValidityPeriod, "201024120604000+"},
{RegisteredDelivery, 1},
{DataCoding, 0},
},
},
{
id: 4,
b: createBytes("0000003580000004000000000000000363623963343066312D306161312D346233652D616662382D37646432" +
"346430336137313600"),
ok: true,
header: header{
l: 53,
id: SubmitSmResp,
status: EsmeROk,
seq: 3,
},
mandatoryParams: mandatoryParams{
{MessageID, "cb9c40f1-0aa1-4b3e-afb8-7dd24d03a716"},
},
},
{
id: 5,
b: createBytes("000000D70000000500000000000000030001013739353030383932353638000001373737000400000000000" +
"001007A69643A63623963343066312D306161312D346233652D616662382D376464323464303361373136207375623A3030312" +
"0646C7672643A303031207375626D697420646174653A3230313032343132303520646F6E6520646174653A323031303234313" +
"2303620737461743A44454C49565244206572723A303030001E002563623963343066312D306161312D346233652D616662382" +
"D376464323464303361373136000427000102"),
ok: true,
header: header{
l: 215,
id: DeliverSm,
status: EsmeROk,
seq: 3,
},
mandatoryParams: mandatoryParams{
{DestinationAddr, "777"},
{ReplaceIfPresentFlag, 0},
{DataCoding, 1},
{ShortMessage, []byte("id:cb9c40f1-0aa1-4b3e-afb8-7dd24d03a716 sub:001 dlvrd:001 submit " +
"date:2010241205 done date:2010241206 stat:DELIVRD err:000")},
{DestAddrTON, 0},
{DestAddrNPI, 1},
{ProtocolID, 0},
{PriorityFlag, 0},
{ScheduleDeliveryTime, ""},
{ValidityPeriod, ""},
{SMLength, 122},
{ServiceType, ""},
{SourceAddr, "79500892568"},
{SMDefaultMsgID, 0},
{ESMClass, 4},
{RegisteredDelivery, 0},
{SourceAddrTON, 1},
{SourceAddrNPI, 1},
},
optionalParams: optionalParams{
{TagReceiptedMessageID, "cb9c40f1-0aa1-4b3e-afb8-7dd24d03a716"},
{TagMessageStateOption, 2},
},
},
{
id: 6,
b: createBytes("0000001180000005000000000000000300"),
ok: true,
header: header{
l: 17,
id: DeliverSmResp,
status: EsmeROk,
seq: 3,
},
mandatoryParams: mandatoryParams{
{MessageID, ""},
},
},
{
id: 7,
b: createBytes("00000010000000150000000000000008"),
ok: true,
header: header{
l: 16,
id: EnquireLink,
status: EsmeROk,
seq: 8,
},
},
{
id: 8,
b: createBytes("00000010800000150000000000000008"),
ok: true,
header: header{
l: 16,
id: EnquireLinkResp,
status: EsmeROk,
seq: 8,
},
},
{
id: 9,
b: createBytes("0000001000000006000000000000000A"),
ok: true,
header: header{
l: 16,
id: Unbind,
status: EsmeROk,
seq: 10,
},
},
{
id: 10,
b: createBytes("0000001080000006000000000000000A"),
ok: true,
header: header{
l: 16,
id: UnbindResp,
status: EsmeROk,
seq: 10,
},
},
{
id: 11,
b: createBytes("000000AF0000000500000000000000030001013739353030383932353638000001373737000400000000000" +
"001007A69643A63623963343066312D306161312D346233652D616662382D376464323464303361373136207375623A3030312" +
"0646C7672643A303031207375626D697420646174653A3230313032343132303520646F6E6520646174653A323031303234313" +
"2303620737461743A44454C49565244206572723A303030001E002563623963343066312D306161312D346233652D616662382" +
"D376464323464303361373136000427000102"),
ok: false,
header: header{
l: 215,
id: DeliverSm,
status: EsmeROk,
seq: 3,
},
mandatoryParams: mandatoryParams{
{DestinationAddr, "777"},
{ReplaceIfPresentFlag, 0},
{DataCoding, 1},
{ShortMessage, []byte("id:cb9c40f1-0aa1-4b3e-afb8-7dd24d03a716 sub:001 dlvrd:001 submit " +
"date:2010241205 done date:2010241206 stat:DELIVRD err:000")},
{DestAddrTON, 0},
{DestAddrNPI, 1},
{ProtocolID, 0},
{PriorityFlag, 0},
{ScheduleDeliveryTime, ""},
{ValidityPeriod, ""},
{SMLength, 122},
{ServiceType, ""},
{SourceAddr, "79500892568"},
{SMDefaultMsgID, 0},
{ESMClass, 4},
{RegisteredDelivery, 0},
{SourceAddrTON, 1},
{SourceAddrNPI, 1},
},
optionalParams: optionalParams{
{TagReceiptedMessageID, "cb9c40f1-0aa1-4b3e-afb8-7dd24d03a716"},
{TagMessageStateOption, 2},
},
},
{
id: 12,
b: createBytes("0000FFF0000000500000000000000030001013739353030383932353638000001373737000400000000000" +
"001007A69643A63623963343066312D306161312D346233652D616662382D376464323464303361373136207375623A3030312" +
"0646C7672643A303031207375626D697420646174653A3230313032343132303520646F6E6520646174653A323031303234313" +
"2303620737461743A44454C49565244206572723A303030001E002563623963343066312D306161312D346233652D616662382" +
"D3764643234643"),
ok: false,
},
{
id: 13,
b: createBytes("000000AF0000000500000000000000030001013739353030383932353638000001373737000400000000000" +
"001007A69643A63623963343066312D306161312D346233652D616662382D376464323464303361373136207375623A3030312" +
"0646C7672643A303031207375626D697420646174653A3230313032343132303520646F6E6520646174653A323031303234313" +
"2303620737461743A44454C49565244206572723A303030001E002563623963343066312D306161312D346233652D616662382" +
"D376464323464303361373136000427000102"),
ok: false,
},
{
id: 14,
b: createBytes("00000016800000010000000000000001000210000234"),
ok: false,
},
{
id: 14,
b: createBytes("0000001680000001000000000000000100021000013456"),
ok: false,
},
{
id: 15,
b: createBytes("000000158000000100000000000000010210000134"),
ok: false,
},
}
for _, test := range tests {
pdu := NewEmptyPdu()
if err := pdu.Deserialize(test.b); (err == nil) != test.ok {
t.Errorf("[%v] err [%v] not equals expected [%v] after deserialize", test.id, err, test.ok)
}
if !test.ok {
continue
}
if err := assert(pdu, test.header, test.mandatoryParams, test.optionalParams, test.b); err != nil {
t.Errorf("[%v] %v after deserialize", test.id, err)
}
pdu, err := createPdu(test.header, test.mandatoryParams, test.optionalParams)
if err != nil {
t.Errorf("[%v] %v", test.id, err)
}
if err := assert(pdu, test.header, test.mandatoryParams, test.optionalParams, test.b); err != nil {
t.Errorf("[%v] %v after create and set", test.id, err)
}
}
}