-
Notifications
You must be signed in to change notification settings - Fork 38
/
pdu_test.go
222 lines (181 loc) · 7.54 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
package smpp34
import (
"encoding/hex"
. "launchpad.net/gocheck"
"math/rand"
"testing"
"time"
)
const (
bindPdu = "000000240000000900000000000000016875676f0067676f6f687500434d540034000000"
bindRespPdu = "0000001d80000009000000000000000474657374696e67000210000134"
deliverSmPdu = "0000004d000000050000000052227280000001746573743200010174657374000000010000010000002338393261386563303634633064373639666134353366373762343a2074657374206d6f"
deliverSmRespPdu = "0000001180000005000000005222728000"
enquireLinkPdu = "00000010000000150000000000000005"
enquireLinkRespPdu = "00000010800000150000000000000005"
genericNackPdu = "00000010800000000000000200000000"
submitSmPdu = "0000002d00000004000000000000000200000074657374000000746573743200000000000000000000036d7367"
submitSmRespPdu = "0000003580000004000000005221ac3831303039343665342d356138662d343835642d386536342d65646639616133373761323200"
unbindPdu = "00000010000000060000000000000003"
unbindRespPdu = "00000010800000060000000000000003"
querySmPdu = "0000002000000003000000000000000168656c6c6f00000174657374696e6700"
querySmRespPdu = "0000001a80000003000000000000000168656c6c6f0000010000"
)
func Test(t *testing.T) { TestingT(t) }
type MySuite struct{}
var _ = Suite(&MySuite{})
func (s *MySuite) Test_PduCmdIdErrors(c *C) {
data, _ := hex.DecodeString("00000010900000060000000000000003")
_, err := ParsePdu(data)
c.Check(err.Error(), Equals, "Unknown PDU Type. ID:2415919110")
}
func (s *MySuite) Test_PduLenErrors(c *C) {
data, _ := hex.DecodeString("000000100000000600000000000000")
_, err := ParsePdu(data)
c.Check(err.Error(), Equals, "Invalid PDU length")
data, _ = hex.DecodeString("000000F00000000600000000000000")
_, err = ParsePdu(data)
c.Check(err.Error(), Equals, "Invalid PDU length")
}
func (s *MySuite) Test_BindPdu(c *C) {
data, _ := hex.DecodeString(bindPdu)
p, err := ParsePdu(data)
c.Check(err, IsNil)
c.Check(p.GetHeader(), DeepEquals, NewPduHeader(0x24, BIND_TRANSCEIVER, ESME_ROK, uint32(1)))
c.Check(p.GetField(SYSTEM_ID).String(), Equals, "hugo")
c.Check(p.GetField(PASSWORD).String(), Equals, "ggoohu")
c.Check(p.GetField(SYSTEM_TYPE).String(), Equals, "CMT")
c.Check(p.GetField(INTERFACE_VERSION).Value(), Equals, uint8(0x34))
c.Check(p.Writer(), DeepEquals, data)
// Change values
p.SetField(SYSTEM_ID, "test1")
c.Check(p.GetField(SYSTEM_ID).String(), Equals, "test1")
c.Check(hex.EncodeToString(p.Writer()), DeepEquals, "0000002500000009000000000000000174657374310067676f6f687500434d540034000000")
c.Check(p.SetTLVField(0x210, 0, []byte{0x1, 0x2}), Equals, TLVFieldPduErr)
c.Check(p.GetField("UNKNOWN_STR"), IsNil)
}
func (s *MySuite) Test_BindRespPdu(c *C) {
data, _ := hex.DecodeString(bindRespPdu)
p, err := ParsePdu(data)
c.Check(err, IsNil)
c.Check(p.GetHeader(), DeepEquals, NewPduHeader(0x1d, BIND_TRANSCEIVER_RESP, ESME_ROK, uint32(4)))
c.Check(p.GetField(SYSTEM_ID).String(), Equals, "testing")
c.Check(p.TLVFields()[0x210].Value(), DeepEquals, []uint8{0x34})
c.Check(p.Writer(), DeepEquals, data)
c.Check(p.SetTLVField(0x210, 0, []byte{0x1, 0x2}), Equals, TLVFieldLenErr)
c.Check(p.SetTLVField(0x210, 5, []byte{0x1, 0x2}), Equals, TLVFieldLenErr)
}
func (s *MySuite) Test_DeliverSmPdu(c *C) {
data, _ := hex.DecodeString(deliverSmPdu)
p, err := ParsePdu(data)
c.Check(err, IsNil)
c.Check(p.GetHeader(), DeepEquals, NewPduHeader(0x4d, DELIVER_SM, ESME_ROK, uint32(0x52227280)))
c.Check(p.Writer(), DeepEquals, data)
// Change Short Message
p.SetField(SHORT_MESSAGE, "test1")
c.Check(p.GetField(SHORT_MESSAGE).String(), Equals, "test1")
c.Check(hex.EncodeToString(p.Writer()), DeepEquals, "0000002f00000005000000005222728000000174657374320001017465737400000001000001000000057465737431")
}
func (s *MySuite) Test_DeliverSmRespPdu(c *C) {
data, _ := hex.DecodeString(deliverSmRespPdu)
p, err := ParsePdu(data)
c.Check(err, IsNil)
c.Check(p.GetHeader(), DeepEquals, NewPduHeader(0x11, DELIVER_SM_RESP, ESME_ROK, uint32(0x52227280)))
c.Check(p.Writer(), DeepEquals, data)
}
func (s *MySuite) Test_EnquireLinkPdu(c *C) {
data, _ := hex.DecodeString(enquireLinkPdu)
p, err := ParsePdu(data)
c.Check(err, IsNil)
c.Check(p.GetHeader(), DeepEquals, NewPduHeader(0x10, ENQUIRE_LINK, ESME_ROK, uint32(5)))
c.Check(p.Writer(), DeepEquals, data)
}
func (s *MySuite) Test_EnquireLinkRespPdu(c *C) {
data, _ := hex.DecodeString(enquireLinkRespPdu)
p, err := ParsePdu(data)
c.Check(err, IsNil)
c.Check(p.GetHeader(), DeepEquals, NewPduHeader(0x10, ENQUIRE_LINK_RESP, ESME_ROK, uint32(5)))
c.Check(p.Writer(), DeepEquals, data)
}
func (s *MySuite) Test_GenericNackPdu(c *C) {
data, _ := hex.DecodeString(genericNackPdu)
p, err := ParsePdu(data)
c.Check(err, IsNil)
c.Check(p.GetHeader(), DeepEquals, NewPduHeader(0x10, GENERIC_NACK, ESME_RINVCMDLEN, uint32(0)))
c.Check(p.Writer(), DeepEquals, data)
}
func (s *MySuite) Test_SubmitSmPdu(c *C) {
data, _ := hex.DecodeString(submitSmPdu)
p, err := ParsePdu(data)
c.Check(err, IsNil)
c.Check(p.GetHeader(), DeepEquals, NewPduHeader(0x2d, SUBMIT_SM, ESME_ROK, uint32(2)))
c.Check(p.SetField(SHORT_MESSAGE, 1).Error(), Equals, FieldValueErr.Error())
c.Check(p.Writer(), DeepEquals, data)
// Change Short Message
p.SetField(SHORT_MESSAGE, "test1")
c.Check(p.GetField(SHORT_MESSAGE).String(), Equals, "test1")
c.Check(hex.EncodeToString(p.Writer()), DeepEquals, "0000002f00000004000000000000000200000074657374000000746573743200000000000000000000057465737431")
}
func (s *MySuite) Test_SubmitSmRespPdu(c *C) {
data, _ := hex.DecodeString(submitSmRespPdu)
p, err := ParsePdu(data)
c.Check(err, IsNil)
c.Check(p.GetHeader(), DeepEquals, NewPduHeader(0x35, SUBMIT_SM_RESP, ESME_ROK, uint32(0x5221ac38)))
c.Check(p.Writer(), DeepEquals, data)
c.Check(p.GetField(MESSAGE_ID).String(), Equals, "100946e4-5a8f-485d-8e64-edf9aa377a22")
}
func (s *MySuite) Test_UnbindPdu(c *C) {
data, _ := hex.DecodeString(unbindPdu)
p, err := ParsePdu(data)
c.Check(err, IsNil)
c.Check(p.GetHeader(), DeepEquals, NewPduHeader(0x10, UNBIND, ESME_ROK, uint32(3)))
}
func (s *MySuite) Test_UnbindRespPdu(c *C) {
data, _ := hex.DecodeString(unbindRespPdu)
p, err := ParsePdu(data)
c.Check(err, IsNil)
c.Check(p.GetHeader(), DeepEquals, NewPduHeader(0x10, UNBIND_RESP, ESME_ROK, uint32(3)))
}
func (s *MySuite) Test_QuerySmPdu(c *C) {
data, _ := hex.DecodeString(querySmPdu)
p, err := ParsePdu(data)
c.Check(err, IsNil)
c.Check(p.GetHeader(), DeepEquals, NewPduHeader(0x20, QUERY_SM, ESME_ROK, uint32(1)))
c.Check(p.GetField(MESSAGE_ID).String(), Equals, "hello")
c.Check(p.GetField(SOURCE_ADDR_TON).Value(), Equals, uint8(0))
c.Check(p.GetField(SOURCE_ADDR_NPI).Value(), Equals, uint8(1))
c.Check(p.GetField(SOURCE_ADDR).String(), Equals, "testing")
c.Check(p.Writer(), DeepEquals, data)
}
func (s *MySuite) Test_QuerySmRespPdu(c *C) {
data, _ := hex.DecodeString(querySmRespPdu)
p, err := ParsePdu(data)
c.Check(err, IsNil)
c.Check(p.GetHeader(), DeepEquals, NewPduHeader(0x1a, QUERY_SM_RESP, ESME_ROK, uint32(1)))
c.Check(p.GetField(MESSAGE_ID).String(), Equals, "hello")
c.Check(p.GetField(FINAL_DATE).String(), Equals, "")
c.Check(p.Writer(), DeepEquals, data)
}
func (s *MySuite) BenchmarkPduParsing(c *C) {
c.StopTimer()
pdus := []string{
bindPdu,
bindRespPdu,
deliverSmPdu,
deliverSmRespPdu,
enquireLinkPdu,
enquireLinkRespPdu,
genericNackPdu,
submitSmPdu,
submitSmRespPdu,
unbindPdu,
unbindRespPdu,
}
for i := 0; i < c.N; i++ {
p, _ := hex.DecodeString(pdus[rand.Intn(len(pdus))])
c.StartTimer()
ParsePdu(p)
c.StopTimer()
rand.Seed(time.Now().UTC().UnixNano())
}
}