/
ServerLib.py
434 lines (345 loc) · 16.3 KB
/
ServerLib.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
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
""" Robot library for testing E-Commerce Server API
* Keywords for sending and verifying Server API HTTP commands
* Requires m2crypto (Python interface to OpenSSL library)
"""
import os, sys
import locale
import urllib, urllib2, urlparse
import binascii, re
from M2Crypto import RSA
import hashlib
class ServerLib:
""" Robot keywords """
def __init__(self):
self.__parameters = {}
self.__expected = {}
self.__response = {}
self.__savedInvalidValues = []
self.__server = None
def setUrl(self, url):
#print "debug url "+str(url)
self.__server = ServerInterface(url)
print "debug server interface "+str(self.__server)
def add_parameter(self, key, value):
""" Add parameter, which will be sent with Send Message keyword """
self.__parameters[key] = str(value)
def send_message(self, signatureType='1', customSignature=None):
""" Send message containing parameters given with Add Parameter Keyword """
self.__response = {}
print "server" +str(self.__server)
print "debug" +str(self.__parameters)
recv = self.__server.sendAndReceive(self.__parameters,
signatureType, customSignature)
print "Received:", recv
if len(recv) <= 1:
print "Nothing received?"
else:
self.__saveResponse(recv)
print ""
self.__parameters = {}
def add_expected(self, key, value):
""" Add expected parameter value, this is checked with Verify Response.
If value is '*', any value is accepted """
self.__expected[key] = str(value)
def verify_response(self, flags=None):
""" Verify that expected parameters were received.
If 'exactMatch' is given as parameter, number of received key/value
pairs must be same as given with Add Expected keyword """
if len(self.__response) == 0:
raise TestFailed("No reply from server")
if flags == "exactMatch":
if len(self.__response) != len(self.__expected):
print "FAIL. Got %d keys, expected %d"%(len(self.__response),
len(self.__expected))
for k in self.__expected:
if not (k in self.__response):
print "Key in expected but not in response:", k
for k in self.__response:
if not (k in self.__expected):
print "Key in response but not in expected:", k
raise TestFailed("Number of keys is different than expected.")
else:
print "Correct number of keys found"
""" Verify keys and values """
for k, v in self.__expected.iteritems():
# keys belonging to a group (<N>) are handled later
if re.findall(r"[\S\s]*-<N[\d]+>$", k):
continue
print "Verifying that %s=%s is found"%(k, v)
if k in self.__response:
if v == '*':
print "PASS"
elif self.__response[k] == v:
print "PASS"
else:
print "FAIL"
raise TestFailed("Incorrect value for %s: %s, should be %s"%(k,
self.__response[k], v))
else:
print "FAIL"
raise TestFailed("Key not found: %s"%(k))
""" Verify groups of <N> keys, there can be multiple in random (?) order """
# find groups of expected keys ending with <N?> from test case
expKeyGroups = [[] for k in range(0,100)]
for k in self.__expected.keys():
match = re.match(r"[\S\s]*-<N(\d+)>$", k)
if match:
expKeyGroups[int(match.group(1))].append(k)
expKeyGroups = [g for g in expKeyGroups if g != []]
#print "Expected key Groups:", expKeyGroups
# find groups of keys ending with <N> from response
keyGroups = [[] for k in range(0,100)]
for k in self.__response.keys():
match = re.match(r"[\S\s]*-(\d+)$", k)
if match:
keyGroups[int(match.group(1))].append(k)
keyGroups = [g for g in keyGroups if g != []]
#print "Key Groups:", keyGroups
# check that any of the groups match expected groups
for expGroup in expKeyGroups:
# check there is matching group with some value of <N>
print "Matching parameter group:"
for e in expGroup:
print "%s=%s"%(e, self.__expected[e])
groupFound = False
for keyGroup in keyGroups:
# match values for each expected key
# find out index
groupIndex = re.match(r"[\S\s]*-([\d]+)$", keyGroup[0]).group(1)
found = False
for expKey in expGroup:
# remove <N>
expKeyBeginning = re.match(r"([\S\s]+)<N([\d]+)>$", expKey).group(1)
keyInGroup = expKeyBeginning + groupIndex
if not keyInGroup in keyGroup:
found = False
break # expected key not found
if (self.__expected[expKey] != '*') and \
(not self.__response[keyInGroup] == self.__expected[expKey]):
found = False
break # values do not match
found = True
if found:
groupFound = True
print "Found match:"
for k in keyGroup:
print "%s=%s"%(k, self.__response[k])
print ""
if not groupFound:
raise TestFailed("Did not find matching group of response parameters")
self.__expected = {}
def verify_no_response(self):
""" Verify that there was no response from server """
if len(self.__response) == 0:
print "No response from server as expected. PASS"
else:
print "Response should not have been received from server. FAIL"
raise TestFailed("Response was received.")
def verify_not_found(self, key):
""" Verify that the given key is not found in response
(but a response is expected). """
if len(self.__response) == 0:
print "No response from server. FAIL"
raise TestFailed("No response from server.")
else:
if key in self.__response:
print "Key %s shouldn't be found. FAIL"%(key)
raise TestFailed("Key shouldn't be found: %s"%(key))
print "Key %s not found. PASS"%(key)
def test_missing_fields(self):
""" Send several messages where one of the parameters is missing.
There should be no response from server. """
for k, v in self.__parameters.iteritems():
# remove one field
params = self.__parameters.copy()
del params[k]
print "Sending without field %s=%s"%(k,v)
recv = self.__server.sendAndReceive(params)
if len(recv) <= 1:
print "No response to invalid message. PASS.\n"
else:
print "Received response for invalid message:", recv
print "FAIL"
raise TestFailed("Received response for invalid message")
print "All PASSED."
def save_invalid_value(self, key, value, expected="NoReply"):
""" Save invalid value to be tested later in loop """
self.__savedInvalidValues.append((key, value, expected))
def count_saved_invalid_values(self):
return len(self.__savedInvalidValues)
def test_saved_invalid_value(self):
""" Test first saved invalid value which was saved earlier """
if len(self.__savedInvalidValues) == 0:
raise SystemError("No saved invalid values")
key, value, expected = self.__savedInvalidValues.pop(0)
self.test_invalid_value(key, value, expected)
self.__parameters = {}
self.__expected = {}
def test_invalid_value(self, key, value, expected="NoReply"):
""" Send message with invalid value in given field"""
params = self.__parameters.copy()
params[key] = value
print "========================================================"
print "Sending %s=%s, expecting %s"%(key, value, expected)
recv = self.__server.sendAndReceive(params)
if len(recv) <= 1:
if expected == "NoReply":
print "No reply as expected - PASS.\n"
else:
raise TestFailed("No reply, expected '%s' - FAIL.\n" % (expected))
else:
print "Received response:", recv
self.__saveResponse(recv)
print ""
error = None
if expected == "PASS" and not ("s-f-1-30_error-message" in self.__response):
print "No errors as excepted - PASS.\n"
return
#raise TestFailed("Expected no reply - FAIL.\n")
if expected == "NoReply":
raise TestFailed("Expected no reply - FAIL.\n")
if expected == "NoError":
print "Expected valid reply - PASS.\n"
return
if "s-f-1-30_error-message" in self.__response:
error = self.__response["s-f-1-30_error-message"]
if expected == error:
print "Received error message '%s' as expected - PASS\n" % (error)
else:
raise TestFailed("Received error message '%s', expected '%s' - FAIL\n" % (error, expected))
else:
raise TestFailed("No error message, expected '%s' - FAIL\n" % (expected))
def test_invalid_values(self, value):
""" Send several messages where one of parameters has invalid value."""
for k, v in self.__parameters.iteritems():
self.test_invalid_value(k, value)
print "All tested. Check admin web log for exception alerts!"
def query_response_value(self, key):
""" Get value for the given key """
return self.__response[key]
def __saveResponse(self, recv):
# decode from application/x-www-form-urlencoded
self.__response = {}
for pair in urlparse.parse_qsl(recv):
if pair[0] in self.__response:
raise TestFailed("Received several fields with key %s"%(pair[0]))
self.__response[pair[0]] = pair[1]
print ""
print "Response from server:"
for k, v in self.__response.iteritems():
print "%s=%s"%(k, v)
# check signature of response
# "openssl dgst -sha1 -verify ../resources/server_publickey.pem
# -signature signature.txt message.txt"
sortedKeys = sorted(self.__response.keys())
sortedTuples = [(key, self.__response[key]) for key in sortedKeys]
msg = ""
for key in sortedKeys:
# ; in value must be replaced with ;;
if not 'signature' in key:
msg += "%s=%s;"%(key, self.__response[key].replace(';',';;'))
if 's-t-256-256_signature-one' in self.__response:
signatureBytes = binascii.a2b_hex(self.__response['s-t-256-256_signature-one'])
keyPath = os.path.join(os.path.dirname(__file__), 'server_publickey.pem')
pkey = RSA.load_pub_key(keyPath)
result = None
try:
result = pkey.verify(hashlib.sha1(msg.encode('utf-8')).digest(),
signatureBytes, algo='sha1')
except Exception as e:
raise TestFailed("Response sha1 validation failed: " + str(e))
if not result:
raise TestFailed("Response sha1 validation failed.")
if 's-t-256-256_signature-two' in self.__response:
signatureBytes = binascii.a2b_hex(self.__response['s-t-256-256_signature-two'])
keyPath = os.path.join(os.path.dirname(__file__), 'server_publickey.pem')
pkey = RSA.load_pub_key(keyPath)
result = None
try:
result = pkey.verify(hashlib.sha512(msg.encode('utf-8')).digest(),
signatureBytes, algo='sha512')
except Exception as e:
raise TestFailed("Response sha512 validation failed: " + str(e))
if not result:
raise TestFailed("Response sha512 validation failed.")
def encrypt_card_details(self, pan, expiry):
""" Encrypt data for field s-f-256-512_encrypted-card-details """
keyPath = os.path.join(os.path.dirname(__file__), 'server_publickey.pem')
pkey = RSA.load_pub_key(keyPath)
data = pan + "=" + expiry
encrypted = pkey.public_encrypt(data.encode('utf-8'), RSA.pkcs1_padding)
encryptedHex = binascii.b2a_hex(encrypted).upper()
print "Card details encrypted:", data
return encryptedHex
class ServerInterface:
""" Low level sending and receiving functionality """
def __init__(self, url):
self.url = url
def sendAndReceive(self, parameters, signatureType='1', customSignature=None):
""" Send and receive.
parameters: dict of keys and values,
signatureType: '1', '2', 'both', 'none',
customSignature: can be given for testing wrong signature """
sortedKeys = sorted(parameters.keys())
sortedTuples = [(key, parameters[key]) for key in sortedKeys]
msg = ""
for key in sortedKeys:
# ; in value must be replaced with ;;
msg += "%s=%s;"%(key, parameters[key].replace(';',';;'))
# Add signatures
if signatureType != 'none':
keyPath = os.path.join(os.path.dirname(__file__), 'privatekey.pem')
pkey = RSA.load_key(keyPath)
# create signature 1
if signatureType == '1' or signatureType == 'both':
try:
signatureBytes1 = pkey.sign(hashlib.sha1(msg.encode('utf-8')).digest(),
algo='sha1')
except Exception as error:
print "Creating signature 1 failed."
raise error
signature1 = binascii.b2a_hex(signatureBytes1).upper()
if customSignature:
signature1 = customSignature
sortedTuples.append(("s-t-256-256_signature-one", signature1))
# create signature 2
if signatureType == '2' or signatureType == 'both':
try:
signatureBytes2 = pkey.sign(hashlib.sha512(msg.encode('utf-8')).digest(),
algo='sha512')
except Exception as error:
print "Creating signature 2 failed:", error
raise error
signature2 = binascii.b2a_hex(signatureBytes2).upper()
if customSignature:
signature2 = customSignature
sortedTuples.append(("s-t-256-256_signature-two", signature2))
# print logging
print "Sending:"
for pair in sortedTuples:
print "%s=%s"%(pair[0], pair[1])
print ""
# encode data first as utf-8, because urlencode won't accept non-ascii
strTuples = []
try:
for pair in sortedTuples:
strTuples.append((pair[0].encode('utf-8'),
pair[1].encode('utf-8')))
except Exception as error:
print "UTF-8 encoding failed for", pair
raise
# encode content as application/x-www-form-urlencoded
try:
data = urllib.urlencode(strTuples)
except Exception as error:
print "application/x-www-form-urlencoded encoding of message failed"
raise error
# HTTP sending
print "Sending data:", data
req = urllib2.Request(self.url, data)
u = urllib2.urlopen(req, timeout=90)
resp = u.read()
u.close()
return resp
class TestFailed(Exception):
pass