-
Notifications
You must be signed in to change notification settings - Fork 1
/
pdu_test.go
414 lines (397 loc) · 14.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
package smpp
import (
"bytes"
"encoding/hex"
"errors"
"fmt"
"reflect"
"testing"
)
var invalidPduLength, _ = hex.DecodeString("0000001000")
var pduLengthMissing, _ = hex.DecodeString("000000")
var invalidCommandId, _ = hex.DecodeString("00000010000011150000000000000000")
var invalidStatusId, _ = hex.DecodeString("0000001000000015008000ff00000000")
var enquiryLinkFixture, _ = hex.DecodeString("00000010000000150000000000000000")
var enquiryLinkRespFixture, _ = hex.DecodeString("00000010800000150000000000000000")
var bindTransmitterFixture, _ = hex.DecodeString("0000001f000000020000000000000000746573740074657374000034000000")
var bindTransmitterRespFixture, _ = hex.DecodeString("000000158000000200000000000000007465737400")
var submitSmRespFixture, _ = hex.DecodeString("000000128000000400000000000000033100")
var deliverSmOptionsFixture, _ = hex.DecodeString("0000003f000000050000000000000001000000000000353535353535313233340004000000000000000000001e000631313130370004270001020425000100")
var optionalParameterReceiptMessageIdBytes, _ = hex.DecodeString("001e0006313131303700")
var optionalParameterMessageStateBytes, _ = hex.DecodeString("0427000102")
var optionalParameterDeliveryFailureReasonBytes, _ = hex.DecodeString("0425000100")
var enquiryLinkObjHeader = Header{CommandLength: 16, CommandId: "enquire_link", CommandStatus: ESME_ROK, SequenceNumber: 0}
var enquiryLinkRespObjHeader = Header{CommandLength: 16, CommandId: "enquire_link_resp", CommandStatus: ESME_ROK, SequenceNumber: 0}
var bindTransmitterObjHeader = Header{CommandLength: 31, CommandId: "bind_transmitter", CommandStatus: ESME_ROK, SequenceNumber: 0}
var bindTransmitterRespObjHeader = Header{CommandLength: 21, CommandId: "bind_transmitter_resp", CommandStatus: ESME_ROK, SequenceNumber: 0}
var submitSmRespObjHeader = Header{CommandLength: 18, CommandId: "submit_sm_resp", CommandStatus: ESME_ROK, SequenceNumber: 3}
var deliverSmRespObjHeader = Header{CommandLength: 18, CommandId: "deliver_sm_resp", CommandStatus: ESME_ROK, SequenceNumber: 3}
var deliverSmObjHeader = Header{CommandLength: 63, CommandId: "deliver_sm", CommandStatus: ESME_ROK, SequenceNumber: 1}
var dataSmObjHeader = Header{CommandLength: 63, CommandId: "data_sm", CommandStatus: ESME_ROK, SequenceNumber: 1}
var submitSmObjHeader = Header{CommandLength: 63, CommandId: "submit_sm", CommandStatus: ESME_ROK, SequenceNumber: 0}
var generickNackHeader = Header{CommandLength: 63, CommandId: "generick_nack", CommandStatus: "ESME_RINVSRCADR", SequenceNumber: 0}
var bindTransmitterObjBody = Body{
MandatoryParameter: map[string]interface{}{
"system_id": "test",
"password": "test",
"system_type": "",
"interface_version": 52,
"addr_ton": 0,
"addr_npi": 0,
"address_range": "",
},
}
var bindTransmitterRespObjBody = Body{MandatoryParameter: map[string]interface{}{"system_id": "test"}}
var submitSmRespObjBody = Body{MandatoryParameter: map[string]interface{}{"message_id": "1"}}
var optionalReceiptMessageID = map[string]interface{}{"tag": "receipted_message_id", "length": 6, "value": "11107"}
var optionalMessageState = map[string]interface{}{"tag": "message_state", "length": 1, "value": 2}
var optionalDeliveryFailureReason = map[string]interface{}{"tag": "delivery_failure_reason", "length": 1, "value": 0}
var deliverSmObjBody = Body{
MandatoryParameter: map[string]interface{}{
"service_type": "",
"source_addr_ton": 0,
"source_addr_npi": 0,
"source_addr": "",
"dest_addr_ton": 0,
"dest_addr_npi": 0,
"destination_addr": "5555551234",
"esm_class": 4,
"protocol_id": 0,
"priority_flag": 0,
"schedule_delivery_time": "",
"validity_period": "",
"registered_delivery": 0,
"replace_if_present_flag": 0,
"data_coding": 0,
"sm_default_msg_id": 0,
"sm_length": 0,
"short_message": "",
},
OptionalParameters: []map[string]interface{}{
optionalReceiptMessageID,
optionalMessageState,
optionalDeliveryFailureReason,
},
}
var submitSmRespObj = PDU{Header: submitSmRespObjHeader, Body: submitSmRespObjBody}
var deliverSmRespObj = PDU{Header: deliverSmRespObjHeader, Body: submitSmRespObjBody}
var deliverSmObj = PDU{Header: deliverSmObjHeader, Body: deliverSmObjBody}
var dataSmObj = PDU{Header: dataSmObjHeader, Body: deliverSmObjBody}
var submitSmObj = PDU{Header: submitSmObjHeader, Body: deliverSmObjBody}
var bindTransmitterObj = PDU{Header: bindTransmitterObjHeader, Body: bindTransmitterObjBody}
var bindTransmitterRespObj = PDU{Header: bindTransmitterRespObjHeader, Body: bindTransmitterRespObjBody}
var enquiryLinkObj = PDU{Header: enquiryLinkObjHeader, Body: Body{MandatoryParameter: map[string]interface{}{}}}
var enquiryLinkRespObj = PDU{Header: enquiryLinkRespObjHeader, Body: Body{MandatoryParameter: map[string]interface{}{}}}
var missingBodySubmitSMPdu = PDU{
Header: Header{
SequenceNumber: 1,
CommandId: "submit_sm",
CommandStatus: ESME_ROK,
CommandLength: 0,
},
Body: Body{},
}
var missingBodyDeliverSMPdu = PDU{
Header: Header{
SequenceNumber: 1,
CommandId: "deliver_sm",
CommandStatus: ESME_ROK,
CommandLength: 0,
},
Body: Body{},
}
var missingBodySubmitSMPduButWithServiceType = PDU{
Header: Header{
SequenceNumber: 1,
CommandId: "submit_sm",
CommandStatus: ESME_ROK,
CommandLength: 0,
},
Body: Body{MandatoryParameter: map[string]interface{}{
"service_type": "",
},
},
}
var missingHeaderPdu = PDU{
Header: Header{},
Body: Body{},
}
func Test_parseHeaders(t *testing.T) {
type args struct {
bytes []byte
}
tests := []struct {
name string
args args
wantPdu Header
}{
{"parse_enquire_link_header", args{bytes: enquiryLinkFixture}, enquiryLinkObjHeader},
{"parse_enquire_link_resp_header", args{bytes: enquiryLinkRespFixture}, enquiryLinkRespObjHeader},
{"parse_bind_transmitter_header", args{bytes: bindTransmitterFixture}, bindTransmitterObjHeader},
{"parse_bind_transmitter_resp_header", args{bytes: bindTransmitterRespFixture}, bindTransmitterRespObjHeader},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, _ := parseHeader(tt.args.bytes)
if got != tt.wantPdu {
t.Errorf("parseHeader() got = %v, wantPdu %v", got, tt.wantPdu)
return
}
})
}
}
func Test_parsePduBody(t *testing.T) {
t.Parallel()
type args struct {
bytes []byte
header Header
}
tests := []struct {
name string
args args
wantBody Body
}{
{"parse_bind_transmitter", args{header: bindTransmitterObjHeader, bytes: bindTransmitterFixture}, bindTransmitterObjBody},
{"parse_bind_transmitter_resp", args{header: bindTransmitterRespObjHeader, bytes: bindTransmitterRespFixture}, bindTransmitterRespObjBody},
{"parse_submit_sm_resp", args{bytes: submitSmRespFixture, header: submitSmRespObjHeader}, submitSmRespObjBody},
{"parse_deliver_sm_with_options", args{bytes: deliverSmOptionsFixture, header: deliverSmObjHeader}, deliverSmObjBody},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, _ := parseBody(tt.args.header, tt.args.bytes)
eq := reflect.DeepEqual(got, tt.wantBody)
if !eq {
t.Errorf("parseBody() got = %v, want Body %v", got, tt.wantBody)
return
}
})
}
}
func Test_parsePdu(t *testing.T) {
t.Parallel()
type args struct {
bytes []byte
}
tests := []struct {
name string
args args
wantPdu PDU
}{
{"parse_bind_transmitter", args{bytes: bindTransmitterFixture}, bindTransmitterObj},
{"parse_submit_sm_resp", args{bytes: submitSmRespFixture}, submitSmRespObj},
{"parse_enquire_link", args{bytes: enquiryLinkFixture}, enquiryLinkObj},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, _ := ParsePdu(tt.args.bytes)
eq := reflect.DeepEqual(got, tt.wantPdu)
if !eq {
t.Errorf("parsePdu() got = %v, wantPdu %v", got, tt.wantPdu)
return
}
})
}
}
func Test_parseHeaderInvalidPdu(t *testing.T) {
t.Parallel()
type args struct {
bytes []byte
}
tests := []struct {
name string
args args
wantErr error
}{
{"PduInvalidLength", args{bytes: pduLengthMissing}, errors.New("invalid length parameter")},
{"InvalidCommandId", args{bytes: invalidCommandId}, errors.New("unknown command_id 00001115")},
{"InvalidPdu", args{bytes: invalidPduLength}, errors.New("invalid PDU Length for pdu : 0000001000")},
{"Unknown status ID received", args{bytes: invalidStatusId}, errors.New("unknown command status 008000ff")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := ParsePdu(tt.args.bytes)
if err.Error() != tt.wantErr.Error() {
t.Errorf("parseHeader() error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}
}
func TestEncodePdu(t *testing.T) {
t.Parallel()
type args struct {
pdu_obj PDU
}
tests := []struct {
name string
args args
wantPdu []byte
}{
{"encode enquiry object into bytes", args{pdu_obj: enquiryLinkObj}, enquiryLinkFixture},
{"encode enquiry resp object into bytes", args{pdu_obj: enquiryLinkRespObj}, enquiryLinkRespFixture},
{"parse_submit_sm_resp", args{pdu_obj: submitSmRespObj}, submitSmRespFixture},
{"bind_transmitter object into bytes", args{pdu_obj: bindTransmitterObj}, bindTransmitterFixture},
{"bind_transmitter_resp object into bytes", args{pdu_obj: bindTransmitterRespObj}, bindTransmitterRespFixture},
{"deliver_sm object with optional parameter into bytes", args{pdu_obj: deliverSmObj}, deliverSmOptionsFixture},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, _ := EncodePdu(tt.args.pdu_obj)
eq := reflect.DeepEqual(got, tt.wantPdu)
if !eq {
t.Errorf("EncodePdu() got = %v, wantPdu %v", got, tt.wantPdu)
return
}
})
}
}
func Test_encodeSpecificOptionalParameter(t *testing.T) {
t.Parallel()
type args struct {
optionalParam map[string]interface{}
}
tests := []struct {
name string
args args
wantOptionalParamsBytes []byte
wantErr bool
}{
{"Encode message state optional parameter", args{optionalParam: optionalMessageState}, optionalParameterMessageStateBytes, false},
{"Encode delivery failure reason optional parameter", args{optionalParam: optionalDeliveryFailureReason}, optionalParameterDeliveryFailureReasonBytes, false},
{"Encode receipt message ID optional parameter", args{optionalParam: optionalReceiptMessageID}, optionalParameterReceiptMessageIdBytes, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotOptionalParamsBytes, err := encodeSpecificOptionalParameter(tt.args.optionalParam)
if (err != nil) != tt.wantErr {
t.Errorf("encodeSpecificOptionalParameter() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotOptionalParamsBytes, tt.wantOptionalParamsBytes) {
t.Errorf("encodeSpecificOptionalParameter() gotOptionalParamsBytes = %v, want %v", gotOptionalParamsBytes, tt.wantOptionalParamsBytes)
}
})
}
}
func Test_extractOptionalParameters(t *testing.T) {
t.Parallel()
type args struct {
optionalParameterBytes []byte
}
tests := []struct {
name string
args args
want map[string]interface{}
}{
{"Decode message state", args{optionalParameterBytes: optionalParameterMessageStateBytes}, optionalMessageState},
{"Decode ReceiptMessageId", args{optionalParameterBytes: optionalParameterReceiptMessageIdBytes}, optionalReceiptMessageID},
{"Decode DeliveryFailureReason", args{optionalParameterBytes: optionalParameterDeliveryFailureReasonBytes}, optionalDeliveryFailureReason},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got, _ := extractSpecificOptionalParameter(tt.args.optionalParameterBytes); !reflect.DeepEqual(got, tt.want) {
t.Errorf("extractOptionalParameters() = %v, want %v", got, tt.want)
}
})
}
}
func TestInvalidPduEncodingCases(t *testing.T) {
t.Parallel()
type args struct {
pdu_obj PDU
}
tests := []struct {
name string
args args
wantErr error
}{
{"missingBodySubmitSMPdu raise mandatory fields are missings", args{missingBodySubmitSMPdu}, errors.New("service_type of submit_sm pdu missing, can't encode")},
{"missingBodySubmitSMPduButWithServiceType raise mandatory fields are missings", args{missingBodySubmitSMPduButWithServiceType}, errors.New("source_addr_ton of submit_sm pdu missing, can't encode")},
{"missingHeader raise header missing error", args{missingHeaderPdu}, missingHeaderError},
{"missingBodySubmitSMPdu raise mandatory fields are missings", args{missingBodySubmitSMPdu}, errors.New("service_type of submit_sm pdu missing, can't encode")},
{"missingBodyDeliverSMPdu raise mandatory fields are missings", args{missingBodyDeliverSMPdu}, errors.New("service_type of deliver_sm pdu missing, can't encode")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := EncodePdu(tt.args.pdu_obj)
if err.Error() != tt.wantErr.Error() {
t.Errorf("EncodePdu() error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}
}
func Test_PduWriter(t *testing.T) {
t.Parallel()
type args struct {
bytes []byte
}
tests := []struct {
name string
args args
wantPdu PDU
err error
}{
{
name: "Write to the right size buffer",
args: args{bytes: bytes.Clone(bindTransmitterFixture)},
wantPdu: bindTransmitterObj,
},
{
name: "Wrong content should be returning an error",
args: args{bytes: bytes.Clone(bindTransmitterFixture[:len(bindTransmitterFixture)-20])},
wantPdu: PDU{},
err: fmt.Errorf("invalid PDU Length for pdu : %v", hex.EncodeToString(bindTransmitterFixture[:len(bindTransmitterFixture)-20])),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actualPdu := PDU{}
_, err := actualPdu.Write(tt.args.bytes)
eq := reflect.DeepEqual(actualPdu, tt.wantPdu)
if !eq {
t.Errorf("Write not working: got = %v, wantPdu = %v", actualPdu, tt.wantPdu)
}
if err != nil && err.Error() != tt.err.Error() {
t.Errorf("Error wasn't the one expected: got %v, wanted err %v", err, tt.err)
}
})
}
}
func TestPduReader(t *testing.T) {
t.Parallel()
type args struct {
Pdu PDU
}
tests := []struct {
name string
args args
wantBytes []byte
err error
}{
{
name: "Read to the right size buffer",
args: args{Pdu: bindTransmitterObj},
wantBytes: bindTransmitterFixture,
},
{
name: "Wrong content should be returning an error",
args: args{Pdu: missingHeaderPdu},
wantBytes: []byte{},
err: missingHeaderError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actualBytes := make([]byte, len(tt.wantBytes))
_, err := tt.args.Pdu.Read(actualBytes)
eq := reflect.DeepEqual(actualBytes, tt.wantBytes)
if !eq {
t.Errorf("Read not working: got = %v, wantPdu = %v", actualBytes, tt.wantBytes)
}
if err != nil && err.Error() != tt.err.Error() {
t.Errorf("Error wasn't the one expected: got %v, wanted err %v", err, tt.err)
}
})
}
}