-
Notifications
You must be signed in to change notification settings - Fork 48
/
tests.py
339 lines (303 loc) · 11 KB
/
tests.py
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
# -*- coding: utf-8 -*-
import unittest, collections
from datetime import datetime, timedelta
from smpp.esme import *
from smpp.clickatell import *
from smpp import pdu
import credentials_test
try:import credentials_priv
except:pass
from test.pdu import pdu_objects
from test import pdu_asserts
from test.pdu_hex import pdu_hex_strings
from test import pdu_hex_asserts
def unpack_hex(pdu_hex):
"""Unpack PDU hex string and return it as a dictionary"""
return unpack_pdu(binascii.a2b_hex(hexclean(pdu_hex)))
def hexclean(dirtyhex):
"""Remove whitespace, comments & newlines from hex string"""
return re.sub(r'\s','',re.sub(r'#.*\n','\n',dirtyhex))
def prettydump(pdu_obj):
"""Unpack PDU dictionary and dump it as a JSON formatted string"""
return json.dumps(pdu_obj, indent=4, sort_keys=True)
def hex_to_named(dictionary):
"""
Recursive function to convert values in test dictionaries to
their named counterparts that unpack_pdu returns
"""
clone = dictionary.copy()
for key, value in clone.items():
if isinstance(value, collections.Mapping):
clone[key] = hex_to_named(value)
else:
lookup_table = pdu.maps.get('%s_by_hex' % key)
if lookup_table:
# overwrite with mapped value or keep using
# default if the dictionary key doesn't exist
clone[key] = lookup_table.get("%.2d" % value, value)
return clone
def create_pdu_asserts():
pdu_index = 0
for pdu in pdu_objects:
pdu_index += 1
pstr = "\n########################################\n"
pstr += "pdu_json_"
pstr += ('%010d' % pdu_index)
pstr += " = '''"
pstr += prettydump(unpack_pdu(pack_pdu(pdu)))
pstr += "'''"
print pstr
def create_pdu_hex_asserts():
pdu_index = 0
for pdu_hex in pdu_hex_strings:
pdu_index += 1
pstr = "\n########################################\n"
pstr += "pdu_json_"
pstr += ('%010d' % pdu_index)
pstr += " = '''"
pstr += prettydump(unpack_hex(pdu_hex))
pstr += "'''"
print pstr
## :w|!python % > test/pdu_asserts.py
#create_pdu_asserts()
#quit()
## :w|!python % > test/pdu_hex_asserts.py
#create_pdu_hex_asserts()
#quit()
class PduTestCase(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def assertDictEquals(self, dictionary1, dictionary2, depth=[]):
"""
Recursive dictionary comparison, will fail if any keys and values
in the two dictionaries don't match. Displays the key chain / depth
and which parts of the two dictionaries didn't match.
"""
d1_keys = dictionary1.keys()
d1_keys.sort()
d2_keys = dictionary2.keys()
d2_keys.sort()
self.failUnlessEqual(d1_keys, d2_keys,
"Dictionary keys do not match, %s vs %s" % (
d1_keys, d2_keys))
for key, value in dictionary1.items():
if isinstance(value, collections.Mapping):
# go recursive
depth.append(key)
self.assertDictEquals(value, dictionary2[key], depth)
else:
self.failUnlessEqual(value, dictionary2[key],
"Dictionary values do not match for key '%s' " \
"(%s vs %s) at depth: %s.\nDictionary 1: %s\n" \
"Dictionary 2: %s\n" % (
key, value, dictionary2[key], ".".join(depth),
prettydump(dictionary1), prettydump(dictionary2)))
def test_pack_unpack_pdu_objects(self):
print ''
"""
Take a dictionary, pack and unpack it and dump it as JSON correctly
"""
pdu_index = 0
for pdu in pdu_objects:
pdu_index += 1
padded_index = '%010d' % pdu_index
print '...', padded_index
self.assertEquals(
re.sub('\n *','',
prettydump(unpack_pdu(pack_pdu(pdu)))),
re.sub('\n *','',
eval('pdu_asserts.pdu_json_'+padded_index)))
def test_pack_unpack_pdu_hex_strings(self):
print ''
"""
Read the hex data, clean it, and unpack it to JSON correctly
"""
pdu_index = 0
for pdu_hex in pdu_hex_strings:
pdu_index += 1
padded_index = '%010d' % pdu_index
print '...', padded_index
self.assertEquals(
re.sub('\n *','',
prettydump(unpack_hex(pdu_hex))),
re.sub('\n *','',
eval('pdu_hex_asserts.pdu_json_'+padded_index)))
def test_pack_unpack_performance(self):
print ''
"""
Pack & unpack 500 submit_sm PDUs in under 1 second
"""
submit_sm = {
'header': {
'command_length': 0,
'command_id': 'submit_sm',
'command_status': 'ESME_ROK',
'sequence_number': 0,
},
'body': {
'mandatory_parameters': {
'service_type':'',
'source_addr_ton':1,
'source_addr_npi':1,
'source_addr':'',
'dest_addr_ton':1,
'dest_addr_npi':1,
'destination_addr':'',
'esm_class':0,
'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':1,
'short_message':'',
},
},
}
start = datetime.now()
for x in range(500):
x += 1
submit_sm['header']['sequence_number'] = x
sm = 'testing: x = '+str(x)+''
submit_sm['body']['mandatory_parameters']['short_message'] = sm
u = unpack_pdu(pack_pdu(submit_sm))
delta = datetime.now() - start
print '... 500 pack & unpacks in:', delta
self.assertTrue(delta < timedelta(seconds=1))
def test_pack_unpack_of_unicode(self):
"""
SMPP module should be able to pack & unpack unicode characters
without a problem
"""
submit_sm = {
'header': {
'command_length': 67,
'command_id': 'submit_sm',
'command_status': 'ESME_ROK',
'sequence_number': 0,
},
'body': {
'mandatory_parameters': {
'service_type':'',
'source_addr_ton':'international',
'source_addr_npi':'unknown',
'source_addr':'',
'dest_addr_ton':'international',
'dest_addr_npi':'unknown',
'destination_addr':'',
'esm_class':0,
'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':34,
'short_message':u'Vumi says: أبن الشرموطة'.encode('utf-8'),
},
},
}
self.assertDictEquals(
hex_to_named(submit_sm),
unpack_pdu(pack_pdu(submit_sm))
)
def test_pack_unpack_of_ascii_and_unicode_8_16_32(self):
"""
SMPP module should be able to pack & unpack unicode characters
without a problem
"""
submit_sm = {
'header': {
'command_length': 65,
'command_id': 'submit_sm',
'command_status': 'ESME_ROK',
'sequence_number': 0,
},
'body': {
'mandatory_parameters': {
'service_type':'',
'source_addr_ton':'international',
'source_addr_npi':'unknown',
'source_addr':'',
'dest_addr_ton':'international',
'dest_addr_npi':'unknown',
'destination_addr':'',
'esm_class':0,
'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':32,
'short_message':u'a \xf0\x20\u0373\u0020\u0433\u0020\u0533\u0020\u05f3\u0020\u0633\u0020\u13a3\u0020\u16a3 \U0001f090'.encode('utf-8'),
},
},
}
self.assertDictEquals(
hex_to_named(submit_sm),
unpack_pdu(pack_pdu(submit_sm))
)
class PduBuilderTestCase(unittest.TestCase):
def test_true(self):
print ''
self.assertTrue(True)
if __name__ == '__main__':
print '\n##########################################################\n'
#deliv_sm_resp = DeliverSMResp(23)
#print deliv_sm_resp.get_obj()
#print deliv_sm_resp.get_hex()
#enq_lnk = EnquireLink(7)
#print enq_lnk.get_obj()
#print enq_lnk.get_hex()
#sub_sm = SubmitSM(5, short_message='testing testing')
#print sub_sm.get_obj()
#print sub_sm.get_hex()
#sub_sm.add_message_payload('01020304')
#print sub_sm.get_obj()
#print sub_sm.get_hex()
#print unpack_pdu(sub_sm.get_bin())
print '\n##########################################################\n'
esme = ESME()
esme.loadDefaults(clickatell_defaults)
esme.loadDefaults(credentials_test.logica)
print esme.defaults
esme.bind_transmitter()
print esme.state
start = datetime.now()
for x in range(1):
esme.submit_sm(
short_message = 'gobbledygook',
destination_addr = '555',
)
print esme.state
for x in range(1):
esme.submit_multi(
short_message = 'gobbledygook',
dest_address = ['444','333'],
)
print esme.state
for x in range(1):
esme.submit_multi(
short_message = 'gobbledygook',
dest_address = [
{'dest_flag':1, 'destination_addr':'111'},
{'dest_flag':2, 'dl_name':'list22222'},
],
)
print esme.state
delta = datetime.now() - start
esme.disconnect()
print esme.state
print 'excluding binding ... time to send messages =', delta
#if __name__ == '__main__':
#unittest.main()