diff options
author | itraviv <itraviv@cisco.com> | 2016-08-18 15:23:01 +0300 |
---|---|---|
committer | itraviv <itraviv@cisco.com> | 2016-08-18 15:23:01 +0300 |
commit | 6796bb99573f15c77a007434feabb30291ac1670 (patch) | |
tree | 4a1a160dff6ff4b28c61850b2a425b659d98792e /scripts/automation/trex_control_plane/stl | |
parent | 46b8104b0c4576994971adbdef0569007649fdc2 (diff) |
scapy_server_test:
skiping python3 tests
scapy_service.py:
minor changes to protocol_struct function
zmq_client:
added
zmq_server:
minor changes in exception handling
Diffstat (limited to 'scripts/automation/trex_control_plane/stl')
3 files changed, 78 insertions, 26 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 3519bb58..311dc8bd 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 @@ -134,26 +134,32 @@ class Scapy_service(Scapy_service_api): self.all_protocols = self._build_lib() self.protocol_tree = {'ALL':{'Ether':{'ARP':{},'IP':{'TCP':{'RAW':'payload'},'UDP':{'RAW':'payload'}}}}} - def _protocol_struct(self,protocol=''): + + def _all_protocol_structs(self): + old_stdout = sys.stdout + sys.stdout = mystdout = StringIO() + ls() + sys.stdout = old_stdout + all_protocol_data= mystdout.getvalue() + return all_protocol_data + + def _protocol_struct(self,protocol): if '_' in protocol: return [] if not protocol=='': if protocol not in self.all_protocols: return 'protocol not supported' - protocol = eval(protocol) + protocol = eval(protocol) old_stdout = sys.stdout sys.stdout = mystdout = StringIO() - if not protocol=='': - ls(protocol) - else: - ls() + ls(protocol) sys.stdout = old_stdout protocol_data= mystdout.getvalue() return protocol_data def _build_lib(self): - lib = self._protocol_struct() - lib = lib.split('\n') + lib = self._all_protocol_structs() + lib = lib.splitlines() all_protocols=[] for entry in lib: entry = entry.split(':') @@ -195,11 +201,11 @@ class Scapy_service(Scapy_service_api): for pro in self.all_protocols: details = self._get_protocol_details(pro) for i in range(0,len(details),1): - if len(details[i]) is 3: + if len(details[i]) == 3: fields.append(details[i][1]) - uniqeFields = list(set(fields)) + uniqueFields = list(set(fields)) fieldDict = {} - for f in uniqeFields: + for f in uniqueFields: if f in self.regexDB: fieldDict[f] = self.regexDB[f].stringRegex() else: @@ -261,14 +267,18 @@ class Scapy_service(Scapy_service_api): return res_md5 def get_version(self): - return {'built_by':'itraviv','version':'v1.0'} + return {'built_by':'itraviv','version':'v1.01'} def supported_methods(self,method_name=''): if method_name=='': methods = {} for f in dir(Scapy_service): if inspect.ismethod(eval('Scapy_service.'+f)): - methods[f] = inspect.getargspec(eval('Scapy_service.'+f))[0] + param_list = inspect.getargspec(eval('Scapy_service.'+f))[0] + del param_list[0] #deleting the parameter "self" that appears in every method + #because the server automatically operates on an instance, + #and this can cause confusion + methods[f] = (len(param_list), param_list) return methods if method_name in dir(Scapy_service): return True diff --git a/scripts/automation/trex_control_plane/stl/services/scapy_server/scapy_zmq_client.py b/scripts/automation/trex_control_plane/stl/services/scapy_server/scapy_zmq_client.py new file mode 100644 index 00000000..24e1593e --- /dev/null +++ b/scripts/automation/trex_control_plane/stl/services/scapy_server/scapy_zmq_client.py @@ -0,0 +1,44 @@ +
+
+import zmq
+import json
+
+
+class Scapy_server_wrapper():
+ def __init__(self,dest_scapy_port=5555,server_ip_address='localhost'):
+ self.context = zmq.Context()
+ self.socket = self.context.socket(zmq.REQ)
+ self.dest_scapy_port =dest_scapy_port
+ self.socket.connect("tcp://"+str(server_ip_address)+":"+str(self.dest_scapy_port)) #ip address of csi-trex-11
+
+ def call_method(self,method_name,method_params):
+ json_rpc_req = { "jsonrpc":"2.0","method": method_name ,"params": method_params, "id":"1"}
+ request = json.dumps(json_rpc_req)
+ self.socket.send_string(request)
+ # Get the reply.
+ message = self.socket.recv_string()
+ message_parsed = json.loads(message)
+ if 'result' in message_parsed.keys():
+ result = message_parsed['result']
+ else:
+ result = {'error':message_parsed['error']}
+ return result
+
+ def get_all(self):
+ return self.call_method('get_all',[])
+
+ def check_update(self,db_md5,field_md5):
+ result = self.call_method('check_update',[db_md5,field_md5])
+ if result!=True:
+ if 'error' in result.keys():
+ if "Fields DB is not up to date" in result['error']['message:']:
+ raise Exception("Fields DB is not up to date")
+ if "Protocol DB is not up to date" in result['error']['message:']:
+ raise Exception("Protocol DB is not up to date")
+ return result
+
+ def build_pkt(self,pkt_descriptor):
+ return self.call_method('build_pkt',[pkt_descriptor])
+
+ def _get_all_pkt_offsets(self,pkt_desc):
+ return self.call_method('_get_all_pkt_offsets',[pkt_desc])
diff --git a/scripts/automation/trex_control_plane/stl/services/scapy_server/scapy_zmq_server.py b/scripts/automation/trex_control_plane/stl/services/scapy_server/scapy_zmq_server.py index 295315e6..0b88668a 100755 --- a/scripts/automation/trex_control_plane/stl/services/scapy_server/scapy_zmq_server.py +++ b/scripts/automation/trex_control_plane/stl/services/scapy_server/scapy_zmq_server.py @@ -27,7 +27,7 @@ class Scapy_wrapper: def parse_req_msg(self,JSON_req):
try:
req = json.loads(JSON_req)
- req_id='null'
+ req_id=b'null'
if (type(req)!= type({})):
raise ParseException(req_id)
json_rpc_keys = ['jsonrpc','id','method']
@@ -57,7 +57,7 @@ class Scapy_wrapper: def create_error_response(self,error_code,error_msg,req_id='null'):
return {"jsonrpc": "2.0", "error": {"code": error_code, "message:": error_msg}, "id": req_id}
- def create_success_response(self,result,req_id='null'):
+ def create_success_response(self,result,req_id=b'null'):
return {"jsonrpc": "2.0", "result": result, "id": req_id }
def get_exception(self):
@@ -73,8 +73,6 @@ class Scapy_wrapper: def error_handler(self,e,req_id):
- print('exception message is %s' % e.message)
- print('exception type is: %s' % type(e))
try:
raise e
except ParseException as e:
@@ -87,12 +85,12 @@ class Scapy_wrapper: response = self.create_error_response(-32603,'Invalid params',req_id)
except SyntaxError as e:
response = self.create_error_response(-32097,'SyntaxError',req_id)
- except BaseException as e:
- response = self.create_error_response(-32098,'Scapy Server: '+str(e.message),req_id)
- except:
- response = self.create_error_response(-32096,'Scapy Server: Unknown Error',req_id)
+ except Exception as e:
+ if hasattr(e,'message'):
+ response = self.create_error_response(-32098,'Scapy Server: '+str(e.message),req_id)
+ else:
+ response = self.create_error_response(-32096,'Scapy Server: Unknown Error',req_id)
finally:
- print(response)
return response
class Scapy_server():
@@ -109,7 +107,7 @@ class Scapy_server(): print ('Server IP address: %s' % self.IP_address)
try:
while True:
- message = self.socket.recv()
+ message = self.socket.recv_string()
try:
req_id = 'null'
method,params,req_id = self.scapy_wrapper.parse_req_msg(message)
@@ -124,12 +122,12 @@ class Scapy_server(): finally:
json_response = json.dumps(response)
# Send reply back to client
- self.socket.send(json_response)
- if (method =='shut_down'):
+ self.socket.send_string(json_response)
+ if (method == 'shut_down'):
break
except KeyboardInterrupt:
- print('Terminated By Ctrl+C')
+ print(b'Terminated By Ctrl+C')
finally:
self.socket.close()
|