6
6
import unittest
7
7
import statistics
8
8
import time
9
+ import random
10
+ import logging
9
11
10
12
DEBUG_FLAG = False
11
13
if len (sys .argv ) == 2 :
29
31
TIMES_TOTAL = 2
30
32
else :
31
33
TIMES_TOTAL = 100
34
+ TIMES_TOTAL = 100
35
+ LEN_TAG = 27
36
+ LEN_ADDR = 81
37
+ LEN_MSG_SIGN = 2187
38
+ tryte_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ9"
32
39
33
40
34
41
def eval_stat (time_cost , func_name ):
@@ -73,15 +80,20 @@ def test_mam_recv_msg(self):
73
80
eval_stat (time_cost , "mam recv message" )
74
81
75
82
76
- def is_addr_trytes (trytes ):
77
- tryte_alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ9"
78
- if len (trytes ) != 81 :
83
+ def gen_rand_trytes (tryte_len ):
84
+ trytes = ""
85
+ for i in range (tryte_len ):
86
+ trytes = trytes + tryte_alphabet [random .randint (0 , 26 )]
87
+ return trytes
88
+
89
+
90
+ def valid_trytes (trytes , trytes_len ):
91
+ if len (trytes ) != trytes_len :
79
92
return False
80
93
81
94
for char in trytes :
82
95
if char not in tryte_alphabet :
83
96
return False
84
-
85
97
return True
86
98
87
99
@@ -115,6 +127,7 @@ def API(get_query, get_data=None, post_data=None):
115
127
return None
116
128
if not response :
117
129
response = ""
130
+ time .sleep (2 )
118
131
return response
119
132
120
133
@@ -170,7 +183,7 @@ def test_mam_recv_msg(self):
170
183
4. Not existing bundle hash (address)
171
184
"""
172
185
test_cases = [
173
- "QBFXQETKSHDYPFUDO9ILVCAVQIXOHXKCECZYFLPBNVIX9JUXQZJE9URQEEUWPWYZOIACTCGZX9IDIODCA " ,
186
+ "BDIQXTDSGAWKCEPEHLRBSLDEFLXMX9ZOTUZW9JAIGZBFKPICXPEO9LLVTNIFGFDWWHEQNZXJZ9F9HTXD9 " ,
174
187
"" , "生れてすみません"
175
188
]
176
189
expect_cases = [
@@ -187,6 +200,95 @@ def test_mam_recv_msg(self):
187
200
for i in range (len (test_cases )):
188
201
self .assertTrue (expect_cases [i ] in response [i ])
189
202
203
+ def test_send_transfer (self ):
204
+ # cmd
205
+ # 0. positive value, tryte maessage, tryte tag, tryte address
206
+ # 1. zero value, tryte message, tryte tag, tryte address
207
+ # 2. chinese value, tryte message, tryte tag, tryte address
208
+ # 3. zero value, chinese message, tryte tag, tryte address
209
+ # 4. zero value, tryte message, chinese tag, tryte address
210
+ # 5. negative value, tryte maessage, tryte tag, tryte address
211
+ # 6. no value, tryte maessage, tryte tag, tryte address
212
+ # 7. zero value, no maessage, tryte tag, tryte address
213
+ # 8. zero value, tryte maessage, no tag, tryte address
214
+ # 9. zero value, tryte maessage, tryte tag, no address
215
+ # 10. zero value, tryte maessage, tryte tag, unicode address
216
+ rand_msg = gen_rand_trytes (30 )
217
+ rand_tag = gen_rand_trytes (27 )
218
+ rand_addr = gen_rand_trytes (81 )
219
+ test_cases = [
220
+ [420 , rand_msg , rand_tag , rand_addr ],
221
+ [0 , rand_msg , rand_tag , rand_addr ],
222
+ [
223
+ "生而為人, 我很抱歉" .encode (encoding = 'utf-8' ), rand_msg , rand_tag ,
224
+ rand_addr
225
+ ], [0 , "生而為人, 我很抱歉" .encode (encoding = 'utf-8' ), rand_tag , rand_addr ],
226
+ [0 , rand_msg , "生而為人, 我很抱歉" .encode (encoding = 'utf-8' ), rand_addr ],
227
+ [- 5 , rand_msg , rand_tag , rand_addr ],
228
+ [None , rand_msg , rand_tag , rand_addr ],
229
+ [0 , None , rand_tag , rand_addr ], [0 , rand_msg , None , rand_addr ],
230
+ [0 , rand_msg , rand_tag , None ], [0 , rand_msg , rand_tag , "我思故我在" ]
231
+ ]
232
+
233
+ response = []
234
+ for i in range (len (test_cases )):
235
+ logging .debug ("testing case = " + str (test_cases [i ]))
236
+ post_data = {
237
+ "value" : test_cases [i ][0 ],
238
+ "message" : test_cases [i ][1 ],
239
+ "tag" : test_cases [i ][2 ],
240
+ "address" : test_cases [i ][3 ]
241
+ }
242
+ logging .debug ("post_data = " + repr (post_data ))
243
+ post_data_json = json .dumps (post_data )
244
+ response .append (API ("/transaction/" , post_data = post_data_json ))
245
+
246
+ if DEBUG_FLAG == True :
247
+ for i in range (len (response )):
248
+ logging .debug ("send transfer i = " + str (i ) + ", response = " +
249
+ response [i ])
250
+
251
+ pass_case = [0 , 1 , 2 ]
252
+ for i in range (len (response )):
253
+ if i in pass_case :
254
+ logging .debug ("i = " + str (i ) + ", response = " + response [i ])
255
+ res_split = response [i ].split (", " )
256
+ res_json = json .loads (res_split [0 ])
257
+
258
+ # we only send zero tx at this moment
259
+ self .assertEqual (0 , res_json ["value" ])
260
+ self .assertTrue (valid_trytes (res_json ["tag" ], LEN_TAG ))
261
+ self .assertTrue (valid_trytes (res_json ["address" ], LEN_ADDR ))
262
+ self .assertTrue (
263
+ valid_trytes (res_json ["trunk_transaction_hash" ], LEN_ADDR ))
264
+ self .assertTrue (
265
+ valid_trytes (res_json ["branch_transaction_hash" ],
266
+ LEN_ADDR ))
267
+ self .assertTrue (valid_trytes (res_json ["bundle_hash" ],
268
+ LEN_ADDR ))
269
+ self .assertTrue (valid_trytes (res_json ["hash" ], LEN_ADDR ))
270
+ self .assertTrue (
271
+ valid_trytes (res_json ["signature_and_message_fragment" ],
272
+ LEN_MSG_SIGN ))
273
+ else :
274
+ self .assertTrue (EMPTY_REPLY in response [i ]
275
+ or MSG_STATUS_CODE_404 in response [i ])
276
+
277
+ # Time Statistics
278
+ time_cost = []
279
+ rand_msg = gen_rand_trytes (30 )
280
+ rand_tag = gen_rand_trytes (27 )
281
+ rand_addr = gen_rand_trytes (81 )
282
+ for i in range (TIMES_TOTAL ):
283
+ start_time = time .time ()
284
+ post_data = "{\" value\" :0,\" message\" :\" " + str (
285
+ rand_msg ) + "\" ,\" tag\" :\" " + str (
286
+ rand_tag ) + "\" ,\" address\" :\" " + str (rand_addr ) + "\" }"
287
+ API ("/transaction/" , post_data = post_data )
288
+ time_cost .append (time .time () - start_time )
289
+
290
+ eval_stat (time_cost , "send transfer" )
291
+
190
292
191
293
"""
192
294
API List
0 commit comments