diff options
author | Anton Kiselev <anton.kisel@gmail.com> | 2016-11-01 19:19:48 +0700 |
---|---|---|
committer | Anton Kiselev <anton.kisel@gmail.com> | 2016-11-04 19:55:01 +0700 |
commit | 3e48c0b5d1b21554eea8d12bc66175a9157a6951 (patch) | |
tree | 130d2b97e0e78a1e2f769ad2be467da2b45dab23 /scripts | |
parent | 7fc45ee2375e9e5a346595e29dfd526e6a5bec87 (diff) |
scapy server python 3 compatibility fixes(file and pseudo-random generator)
Signed-off-by: Anton Kiselev <anton.kisel@gmail.com>
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/automation/trex_control_plane/stl/services/scapy_server/scapy_service.py | 13 | ||||
-rw-r--r-- | scripts/automation/trex_control_plane/stl/services/scapy_server/unit_tests/test_utils.py | 2 |
2 files changed, 10 insertions, 5 deletions
diff --git a/scripts/automation/trex_control_plane/stl/services/scapy_server/scapy_service.py b/scripts/automation/trex_control_plane/stl/services/scapy_server/scapy_service.py index 118d7d6e..80e2fc99 100755 --- a/scripts/automation/trex_control_plane/stl/services/scapy_server/scapy_service.py +++ b/scripts/automation/trex_control_plane/stl/services/scapy_server/scapy_service.py @@ -282,12 +282,17 @@ def get_sample_field_val(scapy_layer, fieldId): def generate_random_bytes(sz, seed, start, end): # generate bytes of specified range with a fixed seed and size - rnd = random.Random(seed) - end = end + 1 # to include end value - res = [rnd.randrange(start, end) for _i in range(sz)] + rnd = random.Random() + n = end - start + 1 if is_python(2): + rnd = random.Random(seed) + res = [start + int(rnd.random()*n) for _i in range(sz)] return ''.join(chr(x) for x in res) else: + rnd = random.Random() + # to generate same random sequence as 2.x + rnd.seed(seed, version=1) + res = [start + int(rnd.random()*n) for _i in range(sz)] return bytes(res) def generate_bytes_from_template(sz, template): @@ -363,7 +368,7 @@ class Scapy_service(Scapy_service_api): def _load_definitions_from_json(self): # load protocol definitions from a json file self.protocol_definitions = {} - with file('protocols.json') as f: + with open('protocols.json', 'r') as f: protocols = json.load(f) for protocol in protocols: self.protocol_definitions[ protocol['id'] ] = protocol diff --git a/scripts/automation/trex_control_plane/stl/services/scapy_server/unit_tests/test_utils.py b/scripts/automation/trex_control_plane/stl/services/scapy_server/unit_tests/test_utils.py index 82261859..c1fb0478 100644 --- a/scripts/automation/trex_control_plane/stl/services/scapy_server/unit_tests/test_utils.py +++ b/scripts/automation/trex_control_plane/stl/services/scapy_server/unit_tests/test_utils.py @@ -7,7 +7,7 @@ def test_generate_random_bytes(): res = generate_random_bytes(10, 333, ord('0'), ord('9')) print(res) assert(len(res) == 10) - assert(res == '5390532937') # random value with this seed + assert(res == b'5390532937') # random value with this seed def test_generate_bytes_from_template_empty(): res = generate_bytes_from_template(5, b"") |