diff options
author | 2016-12-20 12:57:59 +0700 | |
---|---|---|
committer | 2017-01-05 16:36:29 +0200 | |
commit | 10a1d2e33a110a091f539bdf3de5e5c69e5887f8 (patch) | |
tree | 1e80e38d349aa82a897509708a524f5a0ec5af0d /scripts/automation/trex_control_plane/stl/services/scapy_server | |
parent | 3484a1699e36953d8c8dc95743b468eb026a09e1 (diff) |
Add support for predefined packet templates.
Change-Id: Ib2bdcbe6ea18933394527f06be001607344c75e4
Signed-off-by: Igor Chebykin <igrche@gmail.com>
Diffstat (limited to 'scripts/automation/trex_control_plane/stl/services/scapy_server')
11 files changed, 147 insertions, 1 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 7ed03ec2..7b19896b 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 @@ -264,6 +264,34 @@ class Scapy_service_api(): """ pass + def get_templates(self,client_v_handler): + """ get_templates(self,client_v_handler) + + Returns an array of templates, which normally can be used for creating packet + + Parameters + ---------- + + Returns + ------- + array of templates + """ + pass + + def get_template(self,client_v_handler,template): + """ get_template(self,client_v_handler,template) + + Returns a template, which normally can be used for creating packet + + Parameters + ---------- + + Returns + ------- + base64 of template content + """ + pass + def is_python(version): return version == sys.version_info[0] @@ -933,7 +961,7 @@ class Scapy_service(Scapy_service_api): else: return pkt_class() - + def _get_payload_classes(self, pkt_class): # tries to find, which subclasses allowed. # this can take long time, since it tries to build packets with all subclasses(O(N)) @@ -950,6 +978,61 @@ class Scapy_service(Scapy_service_api): pass return allowed_subclasses + + + def _get_templates(self): + # Make sure you understand the three return values of os.walk: + # + # for root, subdirs, files in os.walk(rootdir): + # has the following meaning: + # + # root: Current path which is "walked through" + # subdirs: Files in root of type directory + # files: Files in root (not in subdirs) of type other than directory + # And please use os.path.join instead of concatenating with a slash! + # Your problem is filePath = rootdir + '/' + file - you must concatenate the currently "walked" folder instead of the topmost folder. + # So that must be filePath = os.path.join(root, file). BTW "file" is a builtin, so you don't normally use it as variable name. + + templates = [] + for root, subdirs, files in os.walk("templates"): + for file in files: + if file.endswith('.trp'): + try: + f = os.path.join(root, file) + o = open(f) + c = json.loads(o.read()) + o.close() + id = f.replace("templates" + os.path.sep, "", 1) + id = id.split(os.path.sep) + id[-1] = id[-1].replace(".trp", "", 1) + id = "/".join(id) + t = { + "id": id, + "meta": { + "name": c["metadata"]["caption"], + "description": "" + } + } + templates.append(t) + except: + pass + return templates + + def _get_template(self,template): + id = template["id"] + f2 = "templates" + os.path.sep + os.path.sep.join(id.split("/")) + ".trp" + for c in r'[]\;,><&*:%=+@!#^()|?^': + id = id.replace(c,'') + id = id.replace("..", "") + id = id.split("/") + f = "templates" + os.path.sep + os.path.sep.join(id) + ".trp" + if f != f2: + return "" + with open(f, 'r') as content_file: + content = base64.b64encode(content_file.read()) + return content + + def _get_fields_definition(self, pkt_class, fieldsDef): # fieldsDef - array of field definitions(or empty array) fields = [] @@ -1010,6 +1093,12 @@ class Scapy_service(Scapy_service_api): return protocolDef['payload'] return [c.__name__ for c in self._get_payload_classes(pkt_class)] + def get_templates(self,client_v_handler): + return self._get_templates() + + def get_template(self,client_v_handler,template): + return self._get_template(template) + #input in string encoded base64 def check_update_of_dbs(self,client_v_handler,db_md5,field_md5): if not (self._verify_version_handler(client_v_handler)): diff --git a/scripts/automation/trex_control_plane/stl/services/scapy_server/templates/ICMP echo request.trp b/scripts/automation/trex_control_plane/stl/services/scapy_server/templates/ICMP echo request.trp new file mode 100644 index 00000000..f8988a5f --- /dev/null +++ b/scripts/automation/trex_control_plane/stl/services/scapy_server/templates/ICMP echo request.trp @@ -0,0 +1 @@ +{"fileType":"trex-packet-editor","version":"1.0.0","metadata":{"caption":"ICMP echo request"},"packet":[{"id":"Ether","fields":[]},{"id":"IP","fields":[]},{"id":"ICMP","fields":[{"id":"type","value":"8"}]}],"fePrarameters":{"cache_size":"1000"},"feInstructions":[]}
\ No newline at end of file diff --git a/scripts/automation/trex_control_plane/stl/services/scapy_server/templates/IPv4/ICMP.trp b/scripts/automation/trex_control_plane/stl/services/scapy_server/templates/IPv4/ICMP.trp new file mode 100644 index 00000000..4ab1a1ae --- /dev/null +++ b/scripts/automation/trex_control_plane/stl/services/scapy_server/templates/IPv4/ICMP.trp @@ -0,0 +1 @@ +{"fileType":"trex-packet-editor","version":"1.0.0","metadata":{"caption":"ICMP echo request"},"packet":[{"id":"Ether","fields":[{"id":"type","value":"2048"}]},{"id":"IP","fields":[]},{"id":"ICMP","fields":[]}],"fePrarameters":{"cache_size":"1000"},"feInstructions":[]}
\ No newline at end of file diff --git a/scripts/automation/trex_control_plane/stl/services/scapy_server/templates/IPv4/TCP.trp b/scripts/automation/trex_control_plane/stl/services/scapy_server/templates/IPv4/TCP.trp new file mode 100644 index 00000000..6c94592c --- /dev/null +++ b/scripts/automation/trex_control_plane/stl/services/scapy_server/templates/IPv4/TCP.trp @@ -0,0 +1 @@ +{"fileType":"trex-packet-editor","version":"1.0.0","metadata":{"caption":"ICMP echo request"},"packet":[{"id":"Ether","fields":[{"id":"type","value":"2048"}]},{"id":"IP","fields":[]},{"id":"TCP","fields":[]}],"fePrarameters":{"cache_size":"1000"},"feInstructions":[]}
\ No newline at end of file diff --git a/scripts/automation/trex_control_plane/stl/services/scapy_server/templates/IPv4/UDP.trp b/scripts/automation/trex_control_plane/stl/services/scapy_server/templates/IPv4/UDP.trp new file mode 100644 index 00000000..bef92993 --- /dev/null +++ b/scripts/automation/trex_control_plane/stl/services/scapy_server/templates/IPv4/UDP.trp @@ -0,0 +1 @@ +{"fileType":"trex-packet-editor","version":"1.0.0","metadata":{"caption":"ICMP echo request"},"packet":[{"id":"Ether","fields":[{"id":"type","value":"2048"}]},{"id":"IP","fields":[]},{"id":"UDP","fields":[]}],"fePrarameters":{"cache_size":"1000"},"feInstructions":[]}
\ No newline at end of file diff --git a/scripts/automation/trex_control_plane/stl/services/scapy_server/templates/IPv6/ICMP.trp b/scripts/automation/trex_control_plane/stl/services/scapy_server/templates/IPv6/ICMP.trp new file mode 100644 index 00000000..c0387a0a --- /dev/null +++ b/scripts/automation/trex_control_plane/stl/services/scapy_server/templates/IPv6/ICMP.trp @@ -0,0 +1 @@ +{"fileType":"trex-packet-editor","version":"1.0.0","metadata":{"caption":"ICMP echo request"},"packet":[{"id":"Ether","fields":[{"id":"type","value":"34525"}]},{"id":"IPv6","fields":[]},{"id":"ICMPv6ND_Redirect","fields":[]}],"fePrarameters":{"cache_size":"1000"},"feInstructions":[]}
\ No newline at end of file diff --git a/scripts/automation/trex_control_plane/stl/services/scapy_server/templates/IPv6/TCP.trp b/scripts/automation/trex_control_plane/stl/services/scapy_server/templates/IPv6/TCP.trp new file mode 100644 index 00000000..1cb9576f --- /dev/null +++ b/scripts/automation/trex_control_plane/stl/services/scapy_server/templates/IPv6/TCP.trp @@ -0,0 +1 @@ +{"fileType":"trex-packet-editor","version":"1.0.0","metadata":{"caption":"ICMP echo request"},"packet":[{"id":"Ether","fields":[{"id":"type","value":"34525"}]},{"id":"IPv6","fields":[]},{"id":"TCP","fields":[]}],"fePrarameters":{"cache_size":"1000"},"feInstructions":[]}
\ No newline at end of file diff --git a/scripts/automation/trex_control_plane/stl/services/scapy_server/templates/IPv6/UDP.trp b/scripts/automation/trex_control_plane/stl/services/scapy_server/templates/IPv6/UDP.trp new file mode 100644 index 00000000..da96ae89 --- /dev/null +++ b/scripts/automation/trex_control_plane/stl/services/scapy_server/templates/IPv6/UDP.trp @@ -0,0 +1 @@ +{"fileType":"trex-packet-editor","version":"1.0.0","metadata":{"caption":"ICMP echo request"},"packet":[{"id":"Ether","fields":[{"id":"type","value":"34525"}]},{"id":"IPv6","fields":[]},{"id":"UDP","fields":[]}],"fePrarameters":{"cache_size":"1000"},"feInstructions":[]}
\ No newline at end of file diff --git a/scripts/automation/trex_control_plane/stl/services/scapy_server/templates/TCP-SYN.trp b/scripts/automation/trex_control_plane/stl/services/scapy_server/templates/TCP-SYN.trp new file mode 100644 index 00000000..8d7668cc --- /dev/null +++ b/scripts/automation/trex_control_plane/stl/services/scapy_server/templates/TCP-SYN.trp @@ -0,0 +1 @@ +{"fileType":"trex-packet-editor","version":"1.0.0","metadata":{"caption":"TCP-SYN"},"packet":[{"id":"Ether","fields":[]},{"id":"IP","fields":[]},{"id":"TCP","fields":[{"id":"flags","value":""}]}],"fePrarameters":{"cache_size":"1000"},"feInstructions":[]}
\ No newline at end of file diff --git a/scripts/automation/trex_control_plane/stl/services/scapy_server/unit_tests/basetest.py b/scripts/automation/trex_control_plane/stl/services/scapy_server/unit_tests/basetest.py index e48880e8..9836c794 100644 --- a/scripts/automation/trex_control_plane/stl/services/scapy_server/unit_tests/basetest.py +++ b/scripts/automation/trex_control_plane/stl/services/scapy_server/unit_tests/basetest.py @@ -88,3 +88,12 @@ def adapt_json_protocol_fields(protocols_array): # change structure for easier if protocol.get("fields"): protocol["fields"] = fields_to_map(protocol["fields"]) + +def get_templates(): + return pass_result(service.get_templates(v_handler)) + + + +def get_template(t): + return pass_result(service.get_template(v_handler, t)) + diff --git a/scripts/automation/trex_control_plane/stl/services/scapy_server/unit_tests/test_scapy_service.py b/scripts/automation/trex_control_plane/stl/services/scapy_server/unit_tests/test_scapy_service.py index e1094a79..1ece5d1e 100644 --- a/scripts/automation/trex_control_plane/stl/services/scapy_server/unit_tests/test_scapy_service.py +++ b/scripts/automation/trex_control_plane/stl/services/scapy_server/unit_tests/test_scapy_service.py @@ -289,3 +289,43 @@ def test_generate_vm_instructions(): ttl_instruction = res['field_engine']['instructions']['instructions'][2] assert(ttl_instruction['min_value'] == 32) assert(ttl_instruction['max_value'] == 64) + + +def test_get_templates(): + tt = get_templates() + assert(tt[0]['id']) + assert(tt[7]["meta"]['name']) + try: + assert(tt[9]['id']) + except: + pass + + +def test_get_template(): + tt = get_templates() + t = tt[0] + res = get_template(t) + res2 = base64.b64decode(res) + obj = json.loads(res2) + assert(obj['packet'][0]['id'] == 'Ether') + assert(obj['packet'][1]['id'] == 'IP') + assert(obj['packet'][2]['id'] == 'ICMP') + + +def test_get_template2(): + tt = get_templates() + t = tt[7] + res = get_template(t) + res2 = base64.b64decode(res) + obj = json.loads(res2) + assert(obj['packet'][0]['id'] == 'Ether') + assert(obj['packet'][1]['id'] == 'IPv6') + assert(obj['packet'][2]['id'] == 'UDP') + + +def test_get_template3(): + tt = get_templates() + t = tt[7] + t["id"] = "../../" + t["id"] + res = get_template(t) + assert(res == '') |