From 45a5ab41169fce84fcd3743d26cf8cc04cf76e41 Mon Sep 17 00:00:00 2001 From: Ajay Shenoy Date: Thu, 16 Jun 2016 11:04:31 -0400 Subject: [PATCH 01/14] port strace to r2py, and some unit tests --- strace.py => strace.r2py | 72 ++++-- tests/strace.r2py | 303 ++++++++++++++++++++++++++ tests/ut_seattlelib_filetracer.r2py | 105 +++++++++ tests/ut_seattlelib_lockobj.r2py | 50 +++++ tests/ut_seattlelib_tcpserverobj.r2py | 59 +++++ tests/ut_seattlelib_udpserverobj.r2py | 53 +++++ 6 files changed, 622 insertions(+), 20 deletions(-) rename strace.py => strace.r2py (83%) create mode 100644 tests/strace.r2py create mode 100644 tests/ut_seattlelib_filetracer.r2py create mode 100644 tests/ut_seattlelib_lockobj.r2py create mode 100644 tests/ut_seattlelib_tcpserverobj.r2py create mode 100644 tests/ut_seattlelib_udpserverobj.r2py diff --git a/strace.py b/strace.r2py similarity index 83% rename from strace.py rename to strace.r2py index 37ff473..b6e1c46 100644 --- a/strace.py +++ b/strace.r2py @@ -1,10 +1,18 @@ """ Author: Armon Dadgar Description: - This namespace can be used as an intermediary logging namespace to - log calls to the Repy API functions. + strace.r2py is a diagnostic, debugging utility for for the repyV2 api + with similar api calls that is used as an intermediary logging namespace to + log calls of the Repy API functions. + + The output is as follows: + time_for_api_call_compleation api_function [api_paramaters] location_in_memory [api_paramaters] """ +#file to log a single trace call +tracefile = "traced.txt" + + # These API functions do _not_ return an object, and can be handled uniformly. NON_OBJ_API_CALLS = ["gethostbyname","getmyip","sendmessage","listfiles","removefile", "exitall","getruntime","randombytes","createthread","sleep","getthreadname", @@ -53,9 +61,18 @@ def traced_call(self,name,func,args,kwargs,no_return=False,print_args=True,print # Print if there is no return if no_return: - PRINT_LOCK.acquire(True) - print call_string - PRINT_LOCK.release() + # PRINT_LOCK.acquire(True) + # log(call_string) + # PRINT_LOCK.release() + + #remove previvous trace file if exists + if tracefile in listfiles(): + removefile(tracefile) + + #write to file + fileobj = openfile(tracefile, True) + fileobj.writeat(call_string, 0) + fileobj.close() # Get the result try: @@ -63,9 +80,18 @@ def traced_call(self,name,func,args,kwargs,no_return=False,print_args=True,print # On an exception, print the call at least except Exception, e: - PRINT_LOCK.acquire(True) - print call_string,"->",str(type(e))+" "+str(e) - PRINT_LOCK.release() + # PRINT_LOCK.acquire(True) + # log(call_string,"->",str(type(e))+" "+str(e)) + # PRINT_LOCK.release() + + #remove previvous trace file if exists + if tracefile in listfiles(): + removefile(tracefile) + + #write to file + fileobj = openfile(tracefile, True) + fileobj.writeat(call_string, 0) + fileobj.close() raise # Return if there is no result @@ -79,10 +105,18 @@ def traced_call(self,name,func,args,kwargs,no_return=False,print_args=True,print str_result = str_result[:MAX_PRINT_VALS] + "..." call_string += " = " + str_result - PRINT_LOCK.acquire(True) - print call_string - PRINT_LOCK.release() + # PRINT_LOCK.acquire(True) + # log(call_string) + # PRINT_LOCK.release() + + #remove previvous trace file if exists + if tracefile in listfiles(): + removefile(tracefile) + #write to file + fileobj = openfile(tracefile, True) + fileobj.writeat(call_string, 0) + fileobj.close() return result @@ -239,33 +273,31 @@ def wrap_all(): # Handle the normal calls for call in NON_OBJ_API_CALLS: CHILD_CONTEXT[call] = NonObjAPICall(call).call - # Wrap openconnection CHILD_CONTEXT["openconnection"] = wrapped_openconnection - # Wrap listenformessage CHILD_CONTEXT["listenformessage"] = wrapped_listenformessage - # Wrap listenforconnection CHILD_CONTEXT["listenforconnection"] = wrapped_listenforconnection - # Wrap createlock CHILD_CONTEXT["createlock"] = wrapped_createlock - # Wrap openfile CHILD_CONTEXT["openfile"] = wrapped_openfile - # Wrap createvirtualnamespace CHILD_CONTEXT["createvirtualnamespace"] = wrapped_virtual_namespace -# Wrap all the functions +# # Wrap all the functions wrap_all() + + # Print the header -print "Call-time function [instance] [args] [ = result ]" +# log("Call-time function [instance] [args] [ = result ]") + + # Dispatch the next module -dy_dispatch_module() +# dy_dispatch_module() diff --git a/tests/strace.r2py b/tests/strace.r2py new file mode 100644 index 0000000..b6e1c46 --- /dev/null +++ b/tests/strace.r2py @@ -0,0 +1,303 @@ +""" +Author: Armon Dadgar +Description: + strace.r2py is a diagnostic, debugging utility for for the repyV2 api + with similar api calls that is used as an intermediary logging namespace to + log calls of the Repy API functions. + + The output is as follows: + time_for_api_call_compleation api_function [api_paramaters] location_in_memory [api_paramaters] +""" + +#file to log a single trace call +tracefile = "traced.txt" + + +# These API functions do _not_ return an object, and can be handled uniformly. +NON_OBJ_API_CALLS = ["gethostbyname","getmyip","sendmessage","listfiles","removefile", + "exitall","getruntime","randombytes","createthread","sleep","getthreadname", + "getresources","getlasterror"] + +# Global print lock +PRINT_LOCK = createlock() + +# If this is True, then output will be serialized, +# this avoids jumbling but is a performance hit +ENABLE_LOCKING = True + +# Limit the maximum print length of arguments and results +MAX_PRINT_VALS = 200 # Print the first 200 characters worth + +# Handle when locking is disabled +if not ENABLE_LOCKING: + def _noop(*args,**kwargs): + return True + + PRINT_LOCK.acquire = _noop + PRINT_LOCK.release = _noop + + +# Replace getruntime +_orig_getruntime = getruntime +def getruntime(): + return round(_orig_getruntime(), 5) + + +# Do a traced function call +def traced_call(self,name,func,args,kwargs,no_return=False,print_args=True,print_result=True): + # Store the time, function call and arguments + call_string = str(getruntime()) + " " + name + + # Print the optional stuff + if not self is None: + call_string += " " + str(self) + if print_args and not args == (): + str_args = str(args) + if len(str_args) > MAX_PRINT_VALS: + str_args = str_args[:MAX_PRINT_VALS] + "...)" + call_string += " " + str_args + if print_args and not kwargs == {}: + call_string += " " + str(kwargs)[:MAX_PRINT_VALS] + + # Print if there is no return + if no_return: + # PRINT_LOCK.acquire(True) + # log(call_string) + # PRINT_LOCK.release() + + #remove previvous trace file if exists + if tracefile in listfiles(): + removefile(tracefile) + + #write to file + fileobj = openfile(tracefile, True) + fileobj.writeat(call_string, 0) + fileobj.close() + + # Get the result + try: + result = func(*args,**kwargs) + + # On an exception, print the call at least + except Exception, e: + # PRINT_LOCK.acquire(True) + # log(call_string,"->",str(type(e))+" "+str(e)) + # PRINT_LOCK.release() + + #remove previvous trace file if exists + if tracefile in listfiles(): + removefile(tracefile) + + #write to file + fileobj = openfile(tracefile, True) + fileobj.writeat(call_string, 0) + fileobj.close() + raise + + # Return if there is no result + if no_return: + return + + # Lock to print + if print_result: + str_result = str(result) + if len(str_result) > MAX_PRINT_VALS: + str_result = str_result[:MAX_PRINT_VALS] + "..." + call_string += " = " + str_result + + # PRINT_LOCK.acquire(True) + # log(call_string) + # PRINT_LOCK.release() + + #remove previvous trace file if exists + if tracefile in listfiles(): + removefile(tracefile) + + #write to file + fileobj = openfile(tracefile, True) + fileobj.writeat(call_string, 0) + fileobj.close() + return result + + +# This class is used for API calls that don't return objects +class NonObjAPICall(): + # Initialize with the name of the call + def __init__(self, name): + self.name = name + self.func = _context[name] + + # This method will be called by sub-namespaces + def call(self,*args,**kwargs): + # Trace the call + return traced_call(None,self.name,self.func,args,kwargs) + + +# This class is used for socket objects +class SocketObj(): + # Store the socket object + def __init__(self,sock): + self.sock = sock + + # Emulate the other functions + def close(self,*args,**kwargs): + return traced_call(self.sock,"socket.close",self.sock.close,args,kwargs) + + def recv(self,*args,**kwargs): + return traced_call(self.sock,"socket.recv",self.sock.recv,args,kwargs) + + def send(self,*args,**kwargs): + return traced_call(self.sock,"socket.send",self.sock.send,args,kwargs) + + +# This class is used for lock objects +class LockObj(): + # Store the lock object + def __init__(self,lock): + self.lock = lock + + # Emulate the functions + def acquire(self, *args,**kwargs): + return traced_call(self.lock,"lock.acquire",self.lock.acquire,args,kwargs) + + def release(self, *args, **kwargs): + return traced_call(self.lock,"lock.release",self.lock.release,args,kwargs,True) + + +# This class is used for file objects +class FileObj(): + # Store the file object + def __init__(self,fileo): + self.fileo = fileo + + # Emulate the functions + def close(self,*args,**kwargs): + return traced_call(self.fileo,"file.close",self.fileo.close,args,kwargs,True) + + def readat(self,*args,**kwargs): + return traced_call(self.fileo,"file.readat",self.fileo.readat,args,kwargs) + + def writeat(self,*args,**kwargs): + return traced_call(self.fileo,"file.writeat",self.fileo.writeat,args,kwargs,True) + + +class VNObj(): + # Store the virt object + def __init__(self,virt): + self.virt = virt + + # Emulate the functions + def evaluate(self,*args,**kwargs): + return traced_call(self.virt,"VirtualNamespace.evaluate",self.virt.evaluate,args,kwargs,print_args=False,print_result=False) + + +# This class is used for TCPServerObjects +class TCPServerObj(): + # Store the object + def __init__(self,sock): + self.sock = sock + + # Emulate the functions + def getconnection(self, *args,**kwargs): + ip,port,conn = traced_call(self.sock,"TCPServerSocket.getconnection",self.sock.getconnection,args,kwargs) + return (ip,port, SocketObj(conn)) + + def close(self, *args, **kwargs): + return traced_call(self.sock,"TCPServerSocket.close",self.sock.close,args,kwargs,True) + + +# This class is used for UDPServerObjects +class UDPServerObj(): + # Store the object + def __init__(self,sock): + self.sock = sock + + # Emulate the functions + def getmessage(self, *args,**kwargs): + return traced_call(self.sock,"UDPServerSocket.getmessage",self.sock.getmessage,args,kwargs) + + def close(self, *args, **kwargs): + return traced_call(self.sock,"UDPServerSocket.close",self.sock.close,args,kwargs,True) + + +# Wrap the call to openconnection +def wrapped_openconnection(*args, **kwargs): + # Trace the call + sock = traced_call(None,"openconnection",openconnection,args,kwargs) + + # Wrap the socket object + return SocketObj(sock) + +# Wrap the call to listenformessage +def wrapped_listenformessage(*args, **kwargs): + # Trace the call + sock = traced_call(None, "listenformessage", listenformessage, args, kwargs) + + # Return the socket object + return UDPServerObj(sock) + +# Wrap the call to listenforconnection +def wrapped_listenforconnection(*args, **kwargs): + # Trace the call + sock = traced_call(None, "listenforconnection", listenforconnection, args, kwargs) + + # Return the socket object + return TCPServerObj(sock) + + +# Wrap the call to createlock +def wrapped_createlock(*args,**kwargs): + # Trace the call to get the lock + lock = traced_call(None,"createlock",createlock,args,kwargs) + + # Return the wrapped lock + return LockObj(lock) + +# Wrap the call to openfile +def wrapped_openfile(*args,**kwargs): + # Trace the call to get the file object + fileo = traced_call(None,"openfile",openfile,args,kwargs) + + # Return the wrapped object + return FileObj(fileo) + + +# Wrap the call to createvirtualnamespace +def wrapped_virtual_namespace(*args,**kwargs): + # Trace the call to get the object + return VNObj(traced_call(None,"VirtualNamespace(...)",createvirtualnamespace,args,kwargs,print_args=False)) + + +# Wrap all the API calls so they can be traced +def wrap_all(): + # Handle the normal calls + for call in NON_OBJ_API_CALLS: + CHILD_CONTEXT[call] = NonObjAPICall(call).call + # Wrap openconnection + CHILD_CONTEXT["openconnection"] = wrapped_openconnection + # Wrap listenformessage + CHILD_CONTEXT["listenformessage"] = wrapped_listenformessage + # Wrap listenforconnection + CHILD_CONTEXT["listenforconnection"] = wrapped_listenforconnection + # Wrap createlock + CHILD_CONTEXT["createlock"] = wrapped_createlock + # Wrap openfile + CHILD_CONTEXT["openfile"] = wrapped_openfile + # Wrap createvirtualnamespace + CHILD_CONTEXT["createvirtualnamespace"] = wrapped_virtual_namespace + + +# # Wrap all the functions +wrap_all() + + + +# Print the header +# log("Call-time function [instance] [args] [ = result ]") + + + +# Dispatch the next module +# dy_dispatch_module() + + diff --git a/tests/ut_seattlelib_filetracer.r2py b/tests/ut_seattlelib_filetracer.r2py new file mode 100644 index 0000000..6659c60 --- /dev/null +++ b/tests/ut_seattlelib_filetracer.r2py @@ -0,0 +1,105 @@ +#this unit test checks the functionality of the file traced object + +from repyportability import * +add_dy_support(locals()) +dy_import_module_symbols("strace.r2py") + + +#create a file using the traced call +fielobj = wrapped_openfile("../hi.txt", True) + +#open traced file +fileobjtrace = openfile(tracefile, True) +tracedCall = fileobjtrace.readat(None, 0) +fileobjtrace.close() + +#Check apicall to traced call +if("openfile" not in tracedCall): + print "Trace api call failed to trace correct function, openfile" +if("('../hi.txt', True)" not in tracedCall): + print "Trace api call failed to trace correct function with correct parameters, openfile('../hi.txt', True)" +if("emulfile.emulated_file object"not in tracedCall): + print "Failed to trace trace correct function as correct object" + + +#write something using trace call +fielobj.writeat("asdfasdf",0) + +#open traced file +fileobjtrace = openfile(tracefile, True) +tracedCall = fileobjtrace.readat(None, 0) +fileobjtrace.close() + +#Check apicall to traced call +if("file.writeat" not in tracedCall): + print "Trace api call failed to trace correct function, writeat" +if("('asdfasdf', 0)" not in tracedCall): + print "Trace api call failed to trace correct function with correct parameters, fielobj.writeat('asdfasdf',0)" +if("emulfile.emulated_file object"not in tracedCall): + print "Failed to trace trace correct function as correct object" + + + +#close file using trace call +fielobj.close() + +#open traced file +fileobjtrace = openfile(tracefile, True) +tracedCall = fileobjtrace.readat(None, 0) +fileobjtrace.close() + +#Check apicall to traced call +if("file.close" not in tracedCall): + print "Trace api call failed to trace correct function, close" +if("emulfile.emulated_file object"not in tracedCall): + print "Failed to trace trace correct function as correct object" + + +#open existing file with trace call +fielobj = wrapped_openfile("../hi.txt", True) + +#open traced file +fileobjtrace = openfile(tracefile, True) +tracedCall = fileobjtrace.readat(None, 0) +fileobjtrace.close() + +#Check apicall to traced call +if("openfile" not in tracedCall): + print "Trace api call failed to trace correct function, openfile" +if("('../hi.txt', True)" not in tracedCall): + print "Trace api call failed to trace correct function with correct parameters, openfile('../hi.txt', True)" +if("emulfile.emulated_file object"not in tracedCall): + print "Failed to trace trace correct function as correct object" + +#read from file usiung trace call +fielobj.readat(0,4) + +#open traced file +fileobjtrace = openfile(tracefile, True) +tracedCall = fileobjtrace.readat(None, 0) +fileobjtrace.close() + +print fileobjtrace +#Check apicall to traced call +if("file.readat" not in tracedCall): + print "Trace api call failed to trace correct function, readat" +if("(0, 4)" not in tracedCall): + print "Trace api call failed to trace correct function with correct parameters, fileobj.readat (0, 4)" +if("emulfile.emulated_file object"not in tracedCall): + print "Failed to trace trace correct function as correct object" + +#close file using trace call +fielobj.close() + +#open traced file +fileobjtrace = openfile(tracefile, True) +tracedCall = fileobjtrace.readat(None, 0) +fileobjtrace.close() + +#Check apicall to traced call +if("file.close" not in tracedCall): + print "Trace api call failed to trace correct function, close" +if("emulfile.emulated_file object"not in tracedCall): + print "Failed to trace trace correct function as correct object" + + diff --git a/tests/ut_seattlelib_lockobj.r2py b/tests/ut_seattlelib_lockobj.r2py new file mode 100644 index 0000000..2aaaf04 --- /dev/null +++ b/tests/ut_seattlelib_lockobj.r2py @@ -0,0 +1,50 @@ +#this unit test checks the functionality of the lock traced object + +from repyportability import * +add_dy_support(locals()) +dy_import_module_symbols("strace.r2py") + + +#create a lock +PRINT_LOCK = wrapped_createlock() + +#open traced file +fileobjtrace = openfile(tracefile, True) +tracedCall = fileobjtrace.readat(None, 0) +fileobjtrace.close() + +#Check apicall to traced call +if("createlock" not in tracedCall): + print "Trace api call failed to trace correct function, createlock" +if("emulmisc.emulated_lock object"not in tracedCall): + print "Failed to trace trace correct function as correct object" + +PRINT_LOCK.acquire(True) + +#open traced file +fileobjtrace = openfile(tracefile, True) +tracedCall = fileobjtrace.readat(None, 0) +fileobjtrace.close() + +#Check apicall to traced call +if("lock.acquire" not in tracedCall): + print "Trace api call failed to trace correct function, lock.acquire" +if("(True,)" not in tracedCall): + print "Trace api call failed to trace correct function with correct paramaters, lock.acquire(True)" +if("emulmisc.emulated_lock object"not in tracedCall): + print "Failed to trace trace correct function as correct object" + + + +PRINT_LOCK.release() + +#open traced file +fileobjtrace = openfile(tracefile, True) +tracedCall = fileobjtrace.readat(None, 0) +fileobjtrace.close() + +#Check apicall to traced call +if("lock.release" not in tracedCall): + print "Trace api call failed to trace correct function, createlock" +if("emulmisc.emulated_lock object"not in tracedCall): + print "Failed to trace trace correct function as correct object" \ No newline at end of file diff --git a/tests/ut_seattlelib_tcpserverobj.r2py b/tests/ut_seattlelib_tcpserverobj.r2py new file mode 100644 index 0000000..d44c3ba --- /dev/null +++ b/tests/ut_seattlelib_tcpserverobj.r2py @@ -0,0 +1,59 @@ +#this unit test checks the functionality of the tcp server traced object + +from repyportability import * +add_dy_support(locals()) +dy_import_module_symbols("strace.r2py") + + +#create a file using the traced call +# wrapped_openconnection("127.0.0.1", 12345, getmyip(),63100, 10) + + + + +tcpserverobj = wrapped_listenforconnection(getmyip(), 12345) + +#open traced file +fileobjtrace = openfile(tracefile, True) +tracedCall = fileobjtrace.readat(None, 0) +fileobjtrace.close() + +#Check apicall to traced call +if("listenforconnection" not in tracedCall): + print "Trace api call failed to trace correct function, openfile" +if("('192.168.183.128', 12345)" not in tracedCall): + print "Trace api call failed to trace correct function with correct parameters, openfile('../hi.txt', True)" +if("emulcomm.TCPServerSocket"not in tracedCall): + print "Failed to trace trace correct function as correct object" + + +tcpserverobj.getconnection() + +#open traced file +fileobjtrace = openfile(tracefile, True) +tracedCall = fileobjtrace.readat(None, 0) +fileobjtrace.close() + +#Check apicall to traced call +if("TCPServerSocket.getconnection" not in tracedCall): + print "Trace api call failed to trace correct function, openfile" +if("emulcomm.TCPServerSocket"not in tracedCall): + print "Failed to trace trace correct function as correct object" + + +tcpserverobj.close() + +#open traced file +fileobjtrace = openfile(tracefile, True) +tracedCall = fileobjtrace.readat(None, 0) +fileobjtrace.close() + +#Check apicall to traced call +if("TCPServerSocket.close" not in tracedCall): + print "Trace api call failed to trace correct function, openfile" +if("emulcomm.TCPServerSocket"not in tracedCall): + print "Failed to trace trace correct function as correct object" + + +# wrapped_openconnection("127.0.0.1", 12345, getmyip(),63100, 10) + diff --git a/tests/ut_seattlelib_udpserverobj.r2py b/tests/ut_seattlelib_udpserverobj.r2py new file mode 100644 index 0000000..0d23abc --- /dev/null +++ b/tests/ut_seattlelib_udpserverobj.r2py @@ -0,0 +1,53 @@ +#this unit test checks the functionality of the udp server traced object + +from repyportability import * +add_dy_support(locals()) +dy_import_module_symbols("strace.r2py") + + +#create a file using the traced call +# wrapped_openconnection("127.0.0.1", 12345, getmyip(),63100, 10) + + + + +udpserverobj = wrapped_listenformessage(getmyip(), 12345) + +#open traced file +fileobjtrace = openfile(tracefile, True) +tracedCall = fileobjtrace.readat(None, 0) +fileobjtrace.close() + +#Check apicall to traced call +if("listenformessage" not in tracedCall): + print "Trace api call failed to trace correct function, openfile" +if("('192.168.183.128', 12345)" not in tracedCall): + print "Trace api call failed to trace correct function with correct parameters, openfile('../hi.txt', True)" +if("emulcomm.UDPServerSocket"not in tracedCall): + print "Failed to trace trace correct function as correct object" + +udpserverobj.getmessage() +#open traced file +fileobjtrace = openfile(tracefile, True) +tracedCall = fileobjtrace.readat(None, 0) +fileobjtrace.close() + +#Check apicall to traced call +if("UDPServerSocket.getmessage" not in tracedCall): + print "Trace api call failed to trace correct function, openfile" +if("emulcomm.UDPServerSocket"not in tracedCall): + print "Failed to trace trace correct function as correct object" + + +udpserverobj.close() + +#open traced file +fileobjtrace = openfile(tracefile, True) +tracedCall = fileobjtrace.readat(None, 0) +fileobjtrace.close() + +#Check apicall to traced call +if("UDPServerSocket.close" not in tracedCall): + print "Trace api call failed to trace correct function, openfile" +if("emulcomm.UDPServerSocket"not in tracedCall): + print "Failed to trace trace correct function as correct object" From fc48271fb5b1b309b1b137cc8e0212c0dcda76db Mon Sep 17 00:00:00 2001 From: Ajay Shenoy Date: Thu, 16 Jun 2016 11:07:53 -0400 Subject: [PATCH 02/14] renamed ut files --- tests/strace.r2py | 303 ------------------ ...r2py => ut_seattlelib_strace_fileobj.r2py} | 0 ...r2py => ut_seattlelib_strace_lockobj.r2py} | 0 ...=> ut_seattlelib_strace_tcpserverobj.r2py} | 0 ...=> ut_seattlelib_strace_udpserverobj.r2py} | 0 5 files changed, 303 deletions(-) delete mode 100644 tests/strace.r2py rename tests/{ut_seattlelib_filetracer.r2py => ut_seattlelib_strace_fileobj.r2py} (100%) rename tests/{ut_seattlelib_lockobj.r2py => ut_seattlelib_strace_lockobj.r2py} (100%) rename tests/{ut_seattlelib_tcpserverobj.r2py => ut_seattlelib_strace_tcpserverobj.r2py} (100%) rename tests/{ut_seattlelib_udpserverobj.r2py => ut_seattlelib_strace_udpserverobj.r2py} (100%) diff --git a/tests/strace.r2py b/tests/strace.r2py deleted file mode 100644 index b6e1c46..0000000 --- a/tests/strace.r2py +++ /dev/null @@ -1,303 +0,0 @@ -""" -Author: Armon Dadgar -Description: - strace.r2py is a diagnostic, debugging utility for for the repyV2 api - with similar api calls that is used as an intermediary logging namespace to - log calls of the Repy API functions. - - The output is as follows: - time_for_api_call_compleation api_function [api_paramaters] location_in_memory [api_paramaters] -""" - -#file to log a single trace call -tracefile = "traced.txt" - - -# These API functions do _not_ return an object, and can be handled uniformly. -NON_OBJ_API_CALLS = ["gethostbyname","getmyip","sendmessage","listfiles","removefile", - "exitall","getruntime","randombytes","createthread","sleep","getthreadname", - "getresources","getlasterror"] - -# Global print lock -PRINT_LOCK = createlock() - -# If this is True, then output will be serialized, -# this avoids jumbling but is a performance hit -ENABLE_LOCKING = True - -# Limit the maximum print length of arguments and results -MAX_PRINT_VALS = 200 # Print the first 200 characters worth - -# Handle when locking is disabled -if not ENABLE_LOCKING: - def _noop(*args,**kwargs): - return True - - PRINT_LOCK.acquire = _noop - PRINT_LOCK.release = _noop - - -# Replace getruntime -_orig_getruntime = getruntime -def getruntime(): - return round(_orig_getruntime(), 5) - - -# Do a traced function call -def traced_call(self,name,func,args,kwargs,no_return=False,print_args=True,print_result=True): - # Store the time, function call and arguments - call_string = str(getruntime()) + " " + name - - # Print the optional stuff - if not self is None: - call_string += " " + str(self) - if print_args and not args == (): - str_args = str(args) - if len(str_args) > MAX_PRINT_VALS: - str_args = str_args[:MAX_PRINT_VALS] + "...)" - call_string += " " + str_args - if print_args and not kwargs == {}: - call_string += " " + str(kwargs)[:MAX_PRINT_VALS] - - # Print if there is no return - if no_return: - # PRINT_LOCK.acquire(True) - # log(call_string) - # PRINT_LOCK.release() - - #remove previvous trace file if exists - if tracefile in listfiles(): - removefile(tracefile) - - #write to file - fileobj = openfile(tracefile, True) - fileobj.writeat(call_string, 0) - fileobj.close() - - # Get the result - try: - result = func(*args,**kwargs) - - # On an exception, print the call at least - except Exception, e: - # PRINT_LOCK.acquire(True) - # log(call_string,"->",str(type(e))+" "+str(e)) - # PRINT_LOCK.release() - - #remove previvous trace file if exists - if tracefile in listfiles(): - removefile(tracefile) - - #write to file - fileobj = openfile(tracefile, True) - fileobj.writeat(call_string, 0) - fileobj.close() - raise - - # Return if there is no result - if no_return: - return - - # Lock to print - if print_result: - str_result = str(result) - if len(str_result) > MAX_PRINT_VALS: - str_result = str_result[:MAX_PRINT_VALS] + "..." - call_string += " = " + str_result - - # PRINT_LOCK.acquire(True) - # log(call_string) - # PRINT_LOCK.release() - - #remove previvous trace file if exists - if tracefile in listfiles(): - removefile(tracefile) - - #write to file - fileobj = openfile(tracefile, True) - fileobj.writeat(call_string, 0) - fileobj.close() - return result - - -# This class is used for API calls that don't return objects -class NonObjAPICall(): - # Initialize with the name of the call - def __init__(self, name): - self.name = name - self.func = _context[name] - - # This method will be called by sub-namespaces - def call(self,*args,**kwargs): - # Trace the call - return traced_call(None,self.name,self.func,args,kwargs) - - -# This class is used for socket objects -class SocketObj(): - # Store the socket object - def __init__(self,sock): - self.sock = sock - - # Emulate the other functions - def close(self,*args,**kwargs): - return traced_call(self.sock,"socket.close",self.sock.close,args,kwargs) - - def recv(self,*args,**kwargs): - return traced_call(self.sock,"socket.recv",self.sock.recv,args,kwargs) - - def send(self,*args,**kwargs): - return traced_call(self.sock,"socket.send",self.sock.send,args,kwargs) - - -# This class is used for lock objects -class LockObj(): - # Store the lock object - def __init__(self,lock): - self.lock = lock - - # Emulate the functions - def acquire(self, *args,**kwargs): - return traced_call(self.lock,"lock.acquire",self.lock.acquire,args,kwargs) - - def release(self, *args, **kwargs): - return traced_call(self.lock,"lock.release",self.lock.release,args,kwargs,True) - - -# This class is used for file objects -class FileObj(): - # Store the file object - def __init__(self,fileo): - self.fileo = fileo - - # Emulate the functions - def close(self,*args,**kwargs): - return traced_call(self.fileo,"file.close",self.fileo.close,args,kwargs,True) - - def readat(self,*args,**kwargs): - return traced_call(self.fileo,"file.readat",self.fileo.readat,args,kwargs) - - def writeat(self,*args,**kwargs): - return traced_call(self.fileo,"file.writeat",self.fileo.writeat,args,kwargs,True) - - -class VNObj(): - # Store the virt object - def __init__(self,virt): - self.virt = virt - - # Emulate the functions - def evaluate(self,*args,**kwargs): - return traced_call(self.virt,"VirtualNamespace.evaluate",self.virt.evaluate,args,kwargs,print_args=False,print_result=False) - - -# This class is used for TCPServerObjects -class TCPServerObj(): - # Store the object - def __init__(self,sock): - self.sock = sock - - # Emulate the functions - def getconnection(self, *args,**kwargs): - ip,port,conn = traced_call(self.sock,"TCPServerSocket.getconnection",self.sock.getconnection,args,kwargs) - return (ip,port, SocketObj(conn)) - - def close(self, *args, **kwargs): - return traced_call(self.sock,"TCPServerSocket.close",self.sock.close,args,kwargs,True) - - -# This class is used for UDPServerObjects -class UDPServerObj(): - # Store the object - def __init__(self,sock): - self.sock = sock - - # Emulate the functions - def getmessage(self, *args,**kwargs): - return traced_call(self.sock,"UDPServerSocket.getmessage",self.sock.getmessage,args,kwargs) - - def close(self, *args, **kwargs): - return traced_call(self.sock,"UDPServerSocket.close",self.sock.close,args,kwargs,True) - - -# Wrap the call to openconnection -def wrapped_openconnection(*args, **kwargs): - # Trace the call - sock = traced_call(None,"openconnection",openconnection,args,kwargs) - - # Wrap the socket object - return SocketObj(sock) - -# Wrap the call to listenformessage -def wrapped_listenformessage(*args, **kwargs): - # Trace the call - sock = traced_call(None, "listenformessage", listenformessage, args, kwargs) - - # Return the socket object - return UDPServerObj(sock) - -# Wrap the call to listenforconnection -def wrapped_listenforconnection(*args, **kwargs): - # Trace the call - sock = traced_call(None, "listenforconnection", listenforconnection, args, kwargs) - - # Return the socket object - return TCPServerObj(sock) - - -# Wrap the call to createlock -def wrapped_createlock(*args,**kwargs): - # Trace the call to get the lock - lock = traced_call(None,"createlock",createlock,args,kwargs) - - # Return the wrapped lock - return LockObj(lock) - -# Wrap the call to openfile -def wrapped_openfile(*args,**kwargs): - # Trace the call to get the file object - fileo = traced_call(None,"openfile",openfile,args,kwargs) - - # Return the wrapped object - return FileObj(fileo) - - -# Wrap the call to createvirtualnamespace -def wrapped_virtual_namespace(*args,**kwargs): - # Trace the call to get the object - return VNObj(traced_call(None,"VirtualNamespace(...)",createvirtualnamespace,args,kwargs,print_args=False)) - - -# Wrap all the API calls so they can be traced -def wrap_all(): - # Handle the normal calls - for call in NON_OBJ_API_CALLS: - CHILD_CONTEXT[call] = NonObjAPICall(call).call - # Wrap openconnection - CHILD_CONTEXT["openconnection"] = wrapped_openconnection - # Wrap listenformessage - CHILD_CONTEXT["listenformessage"] = wrapped_listenformessage - # Wrap listenforconnection - CHILD_CONTEXT["listenforconnection"] = wrapped_listenforconnection - # Wrap createlock - CHILD_CONTEXT["createlock"] = wrapped_createlock - # Wrap openfile - CHILD_CONTEXT["openfile"] = wrapped_openfile - # Wrap createvirtualnamespace - CHILD_CONTEXT["createvirtualnamespace"] = wrapped_virtual_namespace - - -# # Wrap all the functions -wrap_all() - - - -# Print the header -# log("Call-time function [instance] [args] [ = result ]") - - - -# Dispatch the next module -# dy_dispatch_module() - - diff --git a/tests/ut_seattlelib_filetracer.r2py b/tests/ut_seattlelib_strace_fileobj.r2py similarity index 100% rename from tests/ut_seattlelib_filetracer.r2py rename to tests/ut_seattlelib_strace_fileobj.r2py diff --git a/tests/ut_seattlelib_lockobj.r2py b/tests/ut_seattlelib_strace_lockobj.r2py similarity index 100% rename from tests/ut_seattlelib_lockobj.r2py rename to tests/ut_seattlelib_strace_lockobj.r2py diff --git a/tests/ut_seattlelib_tcpserverobj.r2py b/tests/ut_seattlelib_strace_tcpserverobj.r2py similarity index 100% rename from tests/ut_seattlelib_tcpserverobj.r2py rename to tests/ut_seattlelib_strace_tcpserverobj.r2py diff --git a/tests/ut_seattlelib_udpserverobj.r2py b/tests/ut_seattlelib_strace_udpserverobj.r2py similarity index 100% rename from tests/ut_seattlelib_udpserverobj.r2py rename to tests/ut_seattlelib_strace_udpserverobj.r2py From 42842b98e7bac7911d30cbe1783889fb0007a52c Mon Sep 17 00:00:00 2001 From: Ajay Shenoy Date: Tue, 21 Jun 2016 08:51:46 -0400 Subject: [PATCH 03/14] tried to read form stdout failed...... --- strace.r2py | 60 +++-------- tests/ut_seattlelib_strace_fileobj.py | 104 ++++++++++++++++++ tests/ut_seattlelib_strace_fileobj.r2py | 105 ------------------- tests/ut_seattlelib_strace_lockobj.py | 49 +++++++++ tests/ut_seattlelib_strace_lockobj.r2py | 50 --------- tests/ut_seattlelib_strace_socketobj.py | 76 ++++++++++++++ tests/ut_seattlelib_strace_tcpserverobj.py | 55 ++++++++++ tests/ut_seattlelib_strace_tcpserverobj.r2py | 59 ----------- tests/ut_seattlelib_strace_udpserverobj.py | 52 +++++++++ tests/ut_seattlelib_strace_udpserverobj.r2py | 53 ---------- tests/ut_seattlelib_strace_vnobj.py | 53 ++++++++++ 11 files changed, 403 insertions(+), 313 deletions(-) create mode 100644 tests/ut_seattlelib_strace_fileobj.py delete mode 100644 tests/ut_seattlelib_strace_fileobj.r2py create mode 100644 tests/ut_seattlelib_strace_lockobj.py delete mode 100644 tests/ut_seattlelib_strace_lockobj.r2py create mode 100644 tests/ut_seattlelib_strace_socketobj.py create mode 100644 tests/ut_seattlelib_strace_tcpserverobj.py delete mode 100644 tests/ut_seattlelib_strace_tcpserverobj.r2py create mode 100644 tests/ut_seattlelib_strace_udpserverobj.py delete mode 100644 tests/ut_seattlelib_strace_udpserverobj.r2py create mode 100644 tests/ut_seattlelib_strace_vnobj.py diff --git a/strace.r2py b/strace.r2py index b6e1c46..9ccbae3 100644 --- a/strace.r2py +++ b/strace.r2py @@ -5,8 +5,8 @@ Description: with similar api calls that is used as an intermediary logging namespace to log calls of the Repy API functions. - The output is as follows: - time_for_api_call_compleation api_function [api_paramaters] location_in_memory [api_paramaters] + Header: + Call-time function [instance] [args] [ = result ] """ #file to log a single trace call @@ -61,18 +61,10 @@ def traced_call(self,name,func,args,kwargs,no_return=False,print_args=True,print # Print if there is no return if no_return: - # PRINT_LOCK.acquire(True) - # log(call_string) - # PRINT_LOCK.release() + PRINT_LOCK.acquire(True) + log(call_string) + PRINT_LOCK.release() - #remove previvous trace file if exists - if tracefile in listfiles(): - removefile(tracefile) - - #write to file - fileobj = openfile(tracefile, True) - fileobj.writeat(call_string, 0) - fileobj.close() # Get the result try: @@ -80,18 +72,10 @@ def traced_call(self,name,func,args,kwargs,no_return=False,print_args=True,print # On an exception, print the call at least except Exception, e: - # PRINT_LOCK.acquire(True) - # log(call_string,"->",str(type(e))+" "+str(e)) - # PRINT_LOCK.release() - - #remove previvous trace file if exists - if tracefile in listfiles(): - removefile(tracefile) - - #write to file - fileobj = openfile(tracefile, True) - fileobj.writeat(call_string, 0) - fileobj.close() + PRINT_LOCK.acquire(True) + log(call_string,"->",str(type(e))+" "+str(e)) + PRINT_LOCK.release() + raise # Return if there is no result @@ -105,18 +89,11 @@ def traced_call(self,name,func,args,kwargs,no_return=False,print_args=True,print str_result = str_result[:MAX_PRINT_VALS] + "..." call_string += " = " + str_result - # PRINT_LOCK.acquire(True) - # log(call_string) - # PRINT_LOCK.release() + PRINT_LOCK.acquire(True) + log(call_string) + PRINT_LOCK.release() - #remove previvous trace file if exists - if tracefile in listfiles(): - removefile(tracefile) - #write to file - fileobj = openfile(tracefile, True) - fileobj.writeat(call_string, 0) - fileobj.close() return result @@ -181,6 +158,7 @@ class FileObj(): return traced_call(self.fileo,"file.writeat",self.fileo.writeat,args,kwargs,True) +# This class is used for VNObjects class VNObj(): # Store the virt object def __init__(self,virt): @@ -244,7 +222,6 @@ def wrapped_listenforconnection(*args, **kwargs): # Return the socket object return TCPServerObj(sock) - # Wrap the call to createlock def wrapped_createlock(*args,**kwargs): # Trace the call to get the lock @@ -261,7 +238,6 @@ def wrapped_openfile(*args,**kwargs): # Return the wrapped object return FileObj(fileo) - # Wrap the call to createvirtualnamespace def wrapped_virtual_namespace(*args,**kwargs): # Trace the call to get the object @@ -291,13 +267,5 @@ def wrap_all(): wrap_all() - -# Print the header -# log("Call-time function [instance] [args] [ = result ]") - - - # Dispatch the next module -# dy_dispatch_module() - - +dy_dispatch_module() \ No newline at end of file diff --git a/tests/ut_seattlelib_strace_fileobj.py b/tests/ut_seattlelib_strace_fileobj.py new file mode 100644 index 0000000..bc032c0 --- /dev/null +++ b/tests/ut_seattlelib_strace_fileobj.py @@ -0,0 +1,104 @@ +#this unit test checks the functionality of the file traced object + +from repyportability import * +add_dy_support(locals()) +dy_import_module_symbols("strace.r2py") + +import subprocess + + +fielobj = wrapped_openfile("hi.txt", True) + +process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) +output = process.stdout.readlines() +tracedcall = output[0] + +#Check apicall to traced call +if("openfile ('hi.txt', True)" not in tracedcall): + log("Trace api call failed to trace correct function, openfile ('hi.txt', True)") +if("emulfile.emulated_file object"not in tracedcall): + log("Failed to trace trace correct function as correct object") + + +# #write something using trace call +fielobj.writeat("asdfasdf",0) +process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) +output = process.stdout.readlines() + +#Check apicall to traced call +if("file.writeat" not in tracedcall): + log("Trace api call failed to trace correct function, writeat") +if("(\'asdfasdf\', 0)" not in tracedcall): + log("Trace api call failed to trace correct function with correct parameters, fielobj.writeat(\'asdfasdf\',0)") +if("emulfile.emulated_file object"not in tracedcall): + log("Failed to trace trace correct function as correct object") + + + +#close file using trace call +fielobj.close() + +#open traced file +process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) +output = process.stdout.readlines() +tracedcall = output[0] + + +#Check apicall to traced call +if("file.close" not in tracedcall): + log("Trace api call failed to trace correct function, close") +if("emulfile.emulated_file object"not in tracedcall): + log("Failed to trace trace correct function as correct object") + + +#open existing file with trace call +fielobj = wrapped_openfile("../hi.txt", True) + +#open traced file +process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) +output = process.stdout.readlines() +tracedcall = output[0] + + +#Check apicall to traced call +if("openfile" not in tracedcall): + log("Trace api call failed to trace correct function, openfile") +if("(\'../hi.txt\', True)" not in tracedcall): + log("Trace api call failed to trace correct function with correct parameters, openfile(\'../hi.txt\', True)") +if("emulfile.emulated_file object"not in tracedcall): + log("Failed to trace trace correct function as correct object") + +#read from file usiung trace call +fielobj.readat(0,4) + +#open traced file +process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) +output = process.stdout.readlines() +tracedcall = output[0] + + + +#Check apicall to traced call +if("file.readat" not in tracedcall): + log("Trace api call failed to trace correct function, readat") +if("(0, 4)" not in tracedcall): + log("Trace api call failed to trace correct function with correct parameters, fileobj.readat (0, 4)") +if("emulfile.emulated_file object"not in tracedcall): + log("Failed to trace trace correct function as correct object") + +#close file using trace call +fielobj.close() + +#open traced file +process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) +output = process.stdout.readlines() +tracedcall = output[0] + + +#Check apicall to traced call +if("file.close" not in tracedcall): + log("Trace api call failed to trace correct function, close") +if("emulfile.emulated_file object"not in tracedcall): + log("Failed to trace trace correct function as correct object") + + diff --git a/tests/ut_seattlelib_strace_fileobj.r2py b/tests/ut_seattlelib_strace_fileobj.r2py deleted file mode 100644 index 6659c60..0000000 --- a/tests/ut_seattlelib_strace_fileobj.r2py +++ /dev/null @@ -1,105 +0,0 @@ -#this unit test checks the functionality of the file traced object - -from repyportability import * -add_dy_support(locals()) -dy_import_module_symbols("strace.r2py") - - -#create a file using the traced call -fielobj = wrapped_openfile("../hi.txt", True) - -#open traced file -fileobjtrace = openfile(tracefile, True) -tracedCall = fileobjtrace.readat(None, 0) -fileobjtrace.close() - -#Check apicall to traced call -if("openfile" not in tracedCall): - print "Trace api call failed to trace correct function, openfile" -if("('../hi.txt', True)" not in tracedCall): - print "Trace api call failed to trace correct function with correct parameters, openfile('../hi.txt', True)" -if("emulfile.emulated_file object"not in tracedCall): - print "Failed to trace trace correct function as correct object" - - -#write something using trace call -fielobj.writeat("asdfasdf",0) - -#open traced file -fileobjtrace = openfile(tracefile, True) -tracedCall = fileobjtrace.readat(None, 0) -fileobjtrace.close() - -#Check apicall to traced call -if("file.writeat" not in tracedCall): - print "Trace api call failed to trace correct function, writeat" -if("('asdfasdf', 0)" not in tracedCall): - print "Trace api call failed to trace correct function with correct parameters, fielobj.writeat('asdfasdf',0)" -if("emulfile.emulated_file object"not in tracedCall): - print "Failed to trace trace correct function as correct object" - - - -#close file using trace call -fielobj.close() - -#open traced file -fileobjtrace = openfile(tracefile, True) -tracedCall = fileobjtrace.readat(None, 0) -fileobjtrace.close() - -#Check apicall to traced call -if("file.close" not in tracedCall): - print "Trace api call failed to trace correct function, close" -if("emulfile.emulated_file object"not in tracedCall): - print "Failed to trace trace correct function as correct object" - - -#open existing file with trace call -fielobj = wrapped_openfile("../hi.txt", True) - -#open traced file -fileobjtrace = openfile(tracefile, True) -tracedCall = fileobjtrace.readat(None, 0) -fileobjtrace.close() - -#Check apicall to traced call -if("openfile" not in tracedCall): - print "Trace api call failed to trace correct function, openfile" -if("('../hi.txt', True)" not in tracedCall): - print "Trace api call failed to trace correct function with correct parameters, openfile('../hi.txt', True)" -if("emulfile.emulated_file object"not in tracedCall): - print "Failed to trace trace correct function as correct object" - -#read from file usiung trace call -fielobj.readat(0,4) - -#open traced file -fileobjtrace = openfile(tracefile, True) -tracedCall = fileobjtrace.readat(None, 0) -fileobjtrace.close() - -print fileobjtrace -#Check apicall to traced call -if("file.readat" not in tracedCall): - print "Trace api call failed to trace correct function, readat" -if("(0, 4)" not in tracedCall): - print "Trace api call failed to trace correct function with correct parameters, fileobj.readat (0, 4)" -if("emulfile.emulated_file object"not in tracedCall): - print "Failed to trace trace correct function as correct object" - -#close file using trace call -fielobj.close() - -#open traced file -fileobjtrace = openfile(tracefile, True) -tracedCall = fileobjtrace.readat(None, 0) -fileobjtrace.close() - -#Check apicall to traced call -if("file.close" not in tracedCall): - print "Trace api call failed to trace correct function, close" -if("emulfile.emulated_file object"not in tracedCall): - print "Failed to trace trace correct function as correct object" - - diff --git a/tests/ut_seattlelib_strace_lockobj.py b/tests/ut_seattlelib_strace_lockobj.py new file mode 100644 index 0000000..9bd9d1c --- /dev/null +++ b/tests/ut_seattlelib_strace_lockobj.py @@ -0,0 +1,49 @@ +#this unit test checks the functionality of the lock traced object + +from repyportability import * +add_dy_support(locals()) +dy_import_module_symbols("strace.r2py") + +import subprocess + + +#create a lock +PRINT_LOCK = wrapped_createlock() + +process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) +output = process.stdout.readlines() +tracedcall = output[0] + +#Check apicall to traced call +if("createlock" not in tracedcall): + log("Trace api call failed to trace correct function, createlock") +if("emulmisc.emulated_lock object"not in tracedcall): + log("Failed to trace trace correct function as correct object") + +PRINT_LOCK.acquire(True) + +process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) +output = process.stdout.readlines() +tracedcall = output[0] + +#Check apicall to traced call +if("lock.acquire" not in tracedcall): + log("Trace api call failed to trace correct function, lock.acquire") +if("(True,)" not in tracedcall): + log("Trace api call failed to trace correct function with correct paramaters, lock.acquire(True)") +if("emulmisc.emulated_lock object"not in tracedcall): + log("Failed to trace trace correct function as correct object") + + + +PRINT_LOCK.release() + +process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) +output = process.stdout.readlines() +tracedcall = output[0] + +#Check apicall to traced call +if("lock.release" not in tracedcall): + log("Trace api call failed to trace correct function, createlock") +if("emulmisc.emulated_lock object"not in tracedcall): + log("Failed to trace trace correct function as correct object") \ No newline at end of file diff --git a/tests/ut_seattlelib_strace_lockobj.r2py b/tests/ut_seattlelib_strace_lockobj.r2py deleted file mode 100644 index 2aaaf04..0000000 --- a/tests/ut_seattlelib_strace_lockobj.r2py +++ /dev/null @@ -1,50 +0,0 @@ -#this unit test checks the functionality of the lock traced object - -from repyportability import * -add_dy_support(locals()) -dy_import_module_symbols("strace.r2py") - - -#create a lock -PRINT_LOCK = wrapped_createlock() - -#open traced file -fileobjtrace = openfile(tracefile, True) -tracedCall = fileobjtrace.readat(None, 0) -fileobjtrace.close() - -#Check apicall to traced call -if("createlock" not in tracedCall): - print "Trace api call failed to trace correct function, createlock" -if("emulmisc.emulated_lock object"not in tracedCall): - print "Failed to trace trace correct function as correct object" - -PRINT_LOCK.acquire(True) - -#open traced file -fileobjtrace = openfile(tracefile, True) -tracedCall = fileobjtrace.readat(None, 0) -fileobjtrace.close() - -#Check apicall to traced call -if("lock.acquire" not in tracedCall): - print "Trace api call failed to trace correct function, lock.acquire" -if("(True,)" not in tracedCall): - print "Trace api call failed to trace correct function with correct paramaters, lock.acquire(True)" -if("emulmisc.emulated_lock object"not in tracedCall): - print "Failed to trace trace correct function as correct object" - - - -PRINT_LOCK.release() - -#open traced file -fileobjtrace = openfile(tracefile, True) -tracedCall = fileobjtrace.readat(None, 0) -fileobjtrace.close() - -#Check apicall to traced call -if("lock.release" not in tracedCall): - print "Trace api call failed to trace correct function, createlock" -if("emulmisc.emulated_lock object"not in tracedCall): - print "Failed to trace trace correct function as correct object" \ No newline at end of file diff --git a/tests/ut_seattlelib_strace_socketobj.py b/tests/ut_seattlelib_strace_socketobj.py new file mode 100644 index 0000000..f36f1cf --- /dev/null +++ b/tests/ut_seattlelib_strace_socketobj.py @@ -0,0 +1,76 @@ +#this unit test checks the functionality of the strace socket object + +from repyportability import * +add_dy_support(locals()) +dy_import_module_symbols("strace.r2py") + +import subprocess + +localip = "127.0.0.1" +localport1 = 12345 +localport2 = 12347 +targetip = "127.0.0.1" +targetport = 12346 +timeout = 1.0 + +conn = wrapped_openconnection(targetip, targetport, localip, localport1, timeout) + +process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) +output = process.stdout.readlines() +tracedcall = output[0] + +#Check apicall to traced call +if("openconnection" not in tracedcall): + log("Trace api call failed to trace correct function, openconnection") +if("('127.0.0.1', 12346, '127.0.0.1', 12345, 1.0)" not in tracedcall): + log("Trace api call failed to trace correct function with correct parameters, openconnection('127.0.0.1', 12346, '127.0.0.1', 12345, 1.0)") +if("emulcomm.EmulatedSocket" not in tracedcall): + log("Failed to trace trace correct function as correct object") + + + +conn.send("Hi from repy program!") + + +# 0.01771 socket.send ('Hi from repy program!',) = 21 + +process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) +output = process.stdout.readlines() +tracedcall = output[0] + +#Check apicall to traced call +if("socket.send" not in tracedcall): + log("Trace api call failed to trace correct function, socket.send") +if("('Hi from repy program!',)" not in tracedcall and "21" not in tracedcall): + log("Trace api call failed to trace correct function with correct parameters, send('Hi from repy program!',)") +if("emulcomm.EmulatedSocket" not in tracedcall): + log("Failed to trace trace correct function as correct object") + + +conn.recv(21) + +process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) +output = process.stdout.readlines() +tracedcall = output[0] + +#Check apicall to traced call +if("socket.recv" not in tracedcall): + log("Trace api call failed to trace correct function, socket.send") +if("(21,)" not in tracedcall): + log("Trace api call failed to trace correct function with correct parameters, send('Hi from repy program!',)") +if("emulcomm.EmulatedSocket" not in tracedcall): + log("Failed to trace trace correct function as correct object") + + + +conn.close() + +process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) +output = process.stdout.readlines() +tracedcall = output[0] + +#Check apicall to traced call +if("socket.close" not in tracedcall): + log("Trace api call failed to trace correct function, socket.send") +if("emulcomm.EmulatedSocket" not in tracedcall): + log("Failed to trace trace correct function as correct object") \ No newline at end of file diff --git a/tests/ut_seattlelib_strace_tcpserverobj.py b/tests/ut_seattlelib_strace_tcpserverobj.py new file mode 100644 index 0000000..196a891 --- /dev/null +++ b/tests/ut_seattlelib_strace_tcpserverobj.py @@ -0,0 +1,55 @@ +#this unit test checks the functionality of the tcp server traced object + +from repyportability import * +add_dy_support(locals()) +dy_import_module_symbols("strace.r2py") + +import subprocess + + +tcpserverobj = wrapped_listenforconnection(getmyip(), 12345) + +process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) +output = process.stdout.readlines() +tracedcall = output[0] + +#Check apicall to traced call +if("listenforconnection" not in tracedcall): + log("Trace api call failed to trace correct function, openfile") +if("('192.168.183.128', 12345)" not in tracedcall): + log("Trace api call failed to trace correct function with correct parameters, openfile('../hi.txt', True)") +if("emulcomm.TCPServerSocket"not in tracedcall): + log("Failed to trace trace correct function as correct object") + +try: + tcpserverobj.getconnection() +except SocketWouldBlockError: +#expected + pass + +process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) +output = process.stdout.readlines() +tracedcall = output[0] + +#Check apicall to traced call +if("TCPServerSocket.getconnection" not in tracedcall): + log("Trace api call failed to trace correct function, openfile") +if("emulcomm.TCPServerSocket"not in tracedcall): + log("Failed to trace trace correct function as correct object") + + +tcpserverobj.close() + +process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) +output = process.stdout.readlines() +tracedcall = output[0] + +#Check apicall to traced call +if("TCPServerSocket.close" not in tracedcall): + log("Trace api call failed to trace correct function, openfile") +if("emulcomm.TCPServerSocket"not in tracedcall): + log("Failed to trace trace correct function as correct object") + + +# wrapped_openconnection("127.0.0.1", 12345, getmyip(),63100, 10) + diff --git a/tests/ut_seattlelib_strace_tcpserverobj.r2py b/tests/ut_seattlelib_strace_tcpserverobj.r2py deleted file mode 100644 index d44c3ba..0000000 --- a/tests/ut_seattlelib_strace_tcpserverobj.r2py +++ /dev/null @@ -1,59 +0,0 @@ -#this unit test checks the functionality of the tcp server traced object - -from repyportability import * -add_dy_support(locals()) -dy_import_module_symbols("strace.r2py") - - -#create a file using the traced call -# wrapped_openconnection("127.0.0.1", 12345, getmyip(),63100, 10) - - - - -tcpserverobj = wrapped_listenforconnection(getmyip(), 12345) - -#open traced file -fileobjtrace = openfile(tracefile, True) -tracedCall = fileobjtrace.readat(None, 0) -fileobjtrace.close() - -#Check apicall to traced call -if("listenforconnection" not in tracedCall): - print "Trace api call failed to trace correct function, openfile" -if("('192.168.183.128', 12345)" not in tracedCall): - print "Trace api call failed to trace correct function with correct parameters, openfile('../hi.txt', True)" -if("emulcomm.TCPServerSocket"not in tracedCall): - print "Failed to trace trace correct function as correct object" - - -tcpserverobj.getconnection() - -#open traced file -fileobjtrace = openfile(tracefile, True) -tracedCall = fileobjtrace.readat(None, 0) -fileobjtrace.close() - -#Check apicall to traced call -if("TCPServerSocket.getconnection" not in tracedCall): - print "Trace api call failed to trace correct function, openfile" -if("emulcomm.TCPServerSocket"not in tracedCall): - print "Failed to trace trace correct function as correct object" - - -tcpserverobj.close() - -#open traced file -fileobjtrace = openfile(tracefile, True) -tracedCall = fileobjtrace.readat(None, 0) -fileobjtrace.close() - -#Check apicall to traced call -if("TCPServerSocket.close" not in tracedCall): - print "Trace api call failed to trace correct function, openfile" -if("emulcomm.TCPServerSocket"not in tracedCall): - print "Failed to trace trace correct function as correct object" - - -# wrapped_openconnection("127.0.0.1", 12345, getmyip(),63100, 10) - diff --git a/tests/ut_seattlelib_strace_udpserverobj.py b/tests/ut_seattlelib_strace_udpserverobj.py new file mode 100644 index 0000000..4da90e2 --- /dev/null +++ b/tests/ut_seattlelib_strace_udpserverobj.py @@ -0,0 +1,52 @@ +#this unit test checks the functionality of the udp server traced object + +from repyportability import * +add_dy_support(locals()) +dy_import_module_symbols("strace.r2py") + +import subprocess + + + +udpserverobj = wrapped_listenformessage(getmyip(), 12345) + +process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) +output = process.stdout.readlines() +tracedcall = output[0] + +#Check apicall to traced call +if("listenformessage" not in tracedcall): + log("Trace api call failed to trace correct function, openfile") +if("('192.168.183.128', 12345)" not in tracedcall): + log("Trace api call failed to trace correct function with correct parameters, openfile('../hi.txt', True)") +if("emulcomm.UDPServerSocket"not in tracedcall): + log("Failed to trace trace correct function as correct object") + +try: + udpserverobj.getmessage() +except SocketWouldBlockError: +#expected + pass + +process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) +output = process.stdout.readlines() +tracedcall = output[0] + +#Check apicall to traced call +if("UDPServerSocket.getmessage" not in tracedcall): + log("Trace api call failed to trace correct function, openfile") +if("emulcomm.UDPServerSocket"not in tracedcall): + log("Failed to trace trace correct function as correct object") + + +udpserverobj.close() + +process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) +output = process.stdout.readlines() +tracedcall = output[0] + +#Check apicall to traced call +if("UDPServerSocket.close" not in tracedcall): + log("Trace api call failed to trace correct function, openfile") +if("emulcomm.UDPServerSocket"not in tracedcall): + log("Failed to trace trace correct function as correct object") diff --git a/tests/ut_seattlelib_strace_udpserverobj.r2py b/tests/ut_seattlelib_strace_udpserverobj.r2py deleted file mode 100644 index 0d23abc..0000000 --- a/tests/ut_seattlelib_strace_udpserverobj.r2py +++ /dev/null @@ -1,53 +0,0 @@ -#this unit test checks the functionality of the udp server traced object - -from repyportability import * -add_dy_support(locals()) -dy_import_module_symbols("strace.r2py") - - -#create a file using the traced call -# wrapped_openconnection("127.0.0.1", 12345, getmyip(),63100, 10) - - - - -udpserverobj = wrapped_listenformessage(getmyip(), 12345) - -#open traced file -fileobjtrace = openfile(tracefile, True) -tracedCall = fileobjtrace.readat(None, 0) -fileobjtrace.close() - -#Check apicall to traced call -if("listenformessage" not in tracedCall): - print "Trace api call failed to trace correct function, openfile" -if("('192.168.183.128', 12345)" not in tracedCall): - print "Trace api call failed to trace correct function with correct parameters, openfile('../hi.txt', True)" -if("emulcomm.UDPServerSocket"not in tracedCall): - print "Failed to trace trace correct function as correct object" - -udpserverobj.getmessage() -#open traced file -fileobjtrace = openfile(tracefile, True) -tracedCall = fileobjtrace.readat(None, 0) -fileobjtrace.close() - -#Check apicall to traced call -if("UDPServerSocket.getmessage" not in tracedCall): - print "Trace api call failed to trace correct function, openfile" -if("emulcomm.UDPServerSocket"not in tracedCall): - print "Failed to trace trace correct function as correct object" - - -udpserverobj.close() - -#open traced file -fileobjtrace = openfile(tracefile, True) -tracedCall = fileobjtrace.readat(None, 0) -fileobjtrace.close() - -#Check apicall to traced call -if("UDPServerSocket.close" not in tracedCall): - print "Trace api call failed to trace correct function, openfile" -if("emulcomm.UDPServerSocket"not in tracedCall): - print "Failed to trace trace correct function as correct object" diff --git a/tests/ut_seattlelib_strace_vnobj.py b/tests/ut_seattlelib_strace_vnobj.py new file mode 100644 index 0000000..502a7f1 --- /dev/null +++ b/tests/ut_seattlelib_strace_vnobj.py @@ -0,0 +1,53 @@ +#this unit test checks the functionality of the strace virtual namespace object + +from repyportability import * +add_dy_support(locals()) +dy_import_module_symbols("strace.r2py") + +import subprocess + +#most of code taken from ut_repyv2api_virtualnamespace-eval.py + +# Small code snippet, safe +safe_code = "meaning_of_life = 42\n" + +# Try to make the safe virtual namespace +safe_virt = wrapped_virtual_namespace(safe_code, "Test VN") + + +process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) +output = process.stdout.readlines() +tracedcall = output[0] + +#Check apicall to traced call +if("VirtualNamespace(...)" not in tracedcall): + log("Trace api call failed to trace correct function, VirtualNamespace(...)") +if("virtual_namespace.VirtualNamespace " not in tracedcall): + log("Failed to trace trace correct function as correct object") + + + +# Create a execution context +context = SafeDict() + +# Evaluate +context_2 = safe_virt.evaluate(context) + +process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) +output = process.stdout.readlines() +tracedcall = output[0] + +#Check apicall to traced call +if("VirtualNamespace.evaluate" not in tracedcall): + log("Trace api call failed to trace correct function, evaluate") +if("virtual_namespace.VirtualNamespace " not in tracedcall): + log("Failed to trace trace correct function as correct object") + + +# Check that the context is the same +if context is not context_2: + log("Error! Context mis-match!",'\n') + +# Check for the meaning of life +if "meaning_of_life" not in context: + log("Meaning of life is undefined! Existential error!",'\n') \ No newline at end of file From 4f2eaf16ef283e57bd7a3126f3458ad8daa9392d Mon Sep 17 00:00:00 2001 From: Ajay Shenoy Date: Thu, 7 Jul 2016 09:55:01 -0400 Subject: [PATCH 04/14] att 1 at redefining log --- seclayer.r2py | 21 +++++ tests/ut_seattlelib_strace_fileobj.r2py | 105 ++++++++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 seclayer.r2py create mode 100644 tests/ut_seattlelib_strace_fileobj.r2py diff --git a/seclayer.r2py b/seclayer.r2py new file mode 100644 index 0000000..4b892d1 --- /dev/null +++ b/seclayer.r2py @@ -0,0 +1,21 @@ +TYPE="type" +ARGS="args" +RETURN="return" +EXCP="exceptions" +TARGET="target" +FUNC="func" +OBJC="objc" + + +# OLD_SLEEP = CHILD_CONTEXT_DEF["log"] + +# dy_import_module_symbols("oldlog.r2py") + + + + +# Mapping our function to sleep() +# CHILD_CONTEXT_DEF["log"] = OLD_SLEEP + +# Dispatch +secure_dispatch_module() \ No newline at end of file diff --git a/tests/ut_seattlelib_strace_fileobj.r2py b/tests/ut_seattlelib_strace_fileobj.r2py new file mode 100644 index 0000000..6d3447a --- /dev/null +++ b/tests/ut_seattlelib_strace_fileobj.r2py @@ -0,0 +1,105 @@ +#this unit test checks the functionality of the file traced object + +# from repyportability import * +# add_dy_support(locals()) +# dy_import_module_symbols("strace.r2py") + +# import subprocess + + +fielobj = openfile("hi.txt", True) +# log("hi") + +# process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) +# output = process.stdout.readlines() +# tracedcall = output[0] + +# #Check apicall to traced call +# if("openfile ('hi.txt', True)" not in tracedcall): +# log("Trace api call failed to trace correct function, openfile ('hi.txt', True)") +# if("emulfile.emulated_file object"not in tracedcall): +# log("Failed to trace trace correct function as correct object") + + +# # #write something using trace call +# fielobj.writeat("asdfasdf",0) +# process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) +# output = process.stdout.readlines() + +# #Check apicall to traced call +# if("file.writeat" not in tracedcall): +# log("Trace api call failed to trace correct function, writeat") +# if("(\'asdfasdf\', 0)" not in tracedcall): +# log("Trace api call failed to trace correct function with correct parameters, fielobj.writeat(\'asdfasdf\',0)") +# if("emulfile.emulated_file object"not in tracedcall): +# log("Failed to trace trace correct function as correct object") + + + +# #close file using trace call +# fielobj.close() + +# #open traced file +# process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) +# output = process.stdout.readlines() +# tracedcall = output[0] + + +# #Check apicall to traced call +# if("file.close" not in tracedcall): +# log("Trace api call failed to trace correct function, close") +# if("emulfile.emulated_file object"not in tracedcall): +# log("Failed to trace trace correct function as correct object") + + +# #open existing file with trace call +# fielobj = wrapped_openfile("../hi.txt", True) + +# #open traced file +# process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) +# output = process.stdout.readlines() +# tracedcall = output[0] + + +# #Check apicall to traced call +# if("openfile" not in tracedcall): +# log("Trace api call failed to trace correct function, openfile") +# if("(\'../hi.txt\', True)" not in tracedcall): +# log("Trace api call failed to trace correct function with correct parameters, openfile(\'../hi.txt\', True)") +# if("emulfile.emulated_file object"not in tracedcall): +# log("Failed to trace trace correct function as correct object") + +# #read from file usiung trace call +# fielobj.readat(0,4) + +# #open traced file +# process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) +# output = process.stdout.readlines() +# tracedcall = output[0] + + + +# #Check apicall to traced call +# if("file.readat" not in tracedcall): +# log("Trace api call failed to trace correct function, readat") +# if("(0, 4)" not in tracedcall): +# log("Trace api call failed to trace correct function with correct parameters, fileobj.readat (0, 4)") +# if("emulfile.emulated_file object"not in tracedcall): +# log("Failed to trace trace correct function as correct object") + +# #close file using trace call +# fielobj.close() + +# #open traced file +# process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) +# output = process.stdout.readlines() +# tracedcall = output[0] + + +# #Check apicall to traced call +# if("file.close" not in tracedcall): +# log("Trace api call failed to trace correct function, close") +# if("emulfile.emulated_file object"not in tracedcall): +# log("Failed to trace trace correct function as correct object") + + From e9218f44bd1f904675b6cb3df90fc356c2db9035 Mon Sep 17 00:00:00 2001 From: Ajay Shenoy Date: Thu, 7 Jul 2016 09:55:22 -0400 Subject: [PATCH 05/14] part of att 1 --- tests/ut_seattlelib_strace_fileobj.r2py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/ut_seattlelib_strace_fileobj.r2py b/tests/ut_seattlelib_strace_fileobj.r2py index 6d3447a..b949515 100644 --- a/tests/ut_seattlelib_strace_fileobj.r2py +++ b/tests/ut_seattlelib_strace_fileobj.r2py @@ -6,6 +6,7 @@ # import subprocess +#pragma repy restrictions.default dylink.r2py encasementlib.r2py securitylayer.r2py strace.r2py fielobj = openfile("hi.txt", True) # log("hi") From f97a3c5886d1e51fa0716642c8528c49f989025e Mon Sep 17 00:00:00 2001 From: Ajay Shenoy Date: Thu, 7 Jul 2016 10:46:51 -0400 Subject: [PATCH 06/14] att1 redo --- seclayer.r2py | 21 --------------------- securitylayer.r2py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 21 deletions(-) delete mode 100644 seclayer.r2py create mode 100644 securitylayer.r2py diff --git a/seclayer.r2py b/seclayer.r2py deleted file mode 100644 index 4b892d1..0000000 --- a/seclayer.r2py +++ /dev/null @@ -1,21 +0,0 @@ -TYPE="type" -ARGS="args" -RETURN="return" -EXCP="exceptions" -TARGET="target" -FUNC="func" -OBJC="objc" - - -# OLD_SLEEP = CHILD_CONTEXT_DEF["log"] - -# dy_import_module_symbols("oldlog.r2py") - - - - -# Mapping our function to sleep() -# CHILD_CONTEXT_DEF["log"] = OLD_SLEEP - -# Dispatch -secure_dispatch_module() \ No newline at end of file diff --git a/securitylayer.r2py b/securitylayer.r2py new file mode 100644 index 0000000..22edde1 --- /dev/null +++ b/securitylayer.r2py @@ -0,0 +1,33 @@ +TYPE="type" +ARGS="args" +RETURN="return" +EXCP="exceptions" +TARGET="target" +FUNC="func" +OBJC="objc" + + +# dy_import_module_symbols("oldlog.r2py") + +OLD_SLEEP = CHILD_CONTEXT_DEF["log"] + +#no idea how to pipe into anohter module..... +def log_between_moules(args): + myfile = openfile("tracer.txt", True) + myfile.writeat(args, 0) + myfile.close() + + + + +# Mapping our function to log() +CHILD_CONTEXT_DEF["log"] = {TYPE:FUNC,ARGS:((str),),EXCP:None,RETURN:None,TARGET:log_between_moules} + + + +#no idea how to restore old log for UT only! +CHILD_CONTEXT_DEF["log"] = OLD_SLEEP + + +# Dispatch +secure_dispatch_module() \ No newline at end of file From abdf72d1920ab75533b2624e067151aad224abbd Mon Sep 17 00:00:00 2001 From: Ajay Shenoy Date: Thu, 7 Jul 2016 10:47:46 -0400 Subject: [PATCH 07/14] att 2 --- seclayer.r2py | 21 +++++++++++++++++++++ tests/ut_seattlelib_strace_fileobj.r2py | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 seclayer.r2py diff --git a/seclayer.r2py b/seclayer.r2py new file mode 100644 index 0000000..4b892d1 --- /dev/null +++ b/seclayer.r2py @@ -0,0 +1,21 @@ +TYPE="type" +ARGS="args" +RETURN="return" +EXCP="exceptions" +TARGET="target" +FUNC="func" +OBJC="objc" + + +# OLD_SLEEP = CHILD_CONTEXT_DEF["log"] + +# dy_import_module_symbols("oldlog.r2py") + + + + +# Mapping our function to sleep() +# CHILD_CONTEXT_DEF["log"] = OLD_SLEEP + +# Dispatch +secure_dispatch_module() \ No newline at end of file diff --git a/tests/ut_seattlelib_strace_fileobj.r2py b/tests/ut_seattlelib_strace_fileobj.r2py index b949515..d92774b 100644 --- a/tests/ut_seattlelib_strace_fileobj.r2py +++ b/tests/ut_seattlelib_strace_fileobj.r2py @@ -6,7 +6,7 @@ # import subprocess -#pragma repy restrictions.default dylink.r2py encasementlib.r2py securitylayer.r2py strace.r2py +#pragma repy restrictions.default dylink.r2py encasementlib.r2py securitylayer.r2py strace.r2py seclayer.r2py fielobj = openfile("hi.txt", True) # log("hi") From 21f84c8014e1d0edc393de62de8e7d65b8661050 Mon Sep 17 00:00:00 2001 From: Ajay Shenoy Date: Tue, 12 Jul 2016 08:11:50 -0400 Subject: [PATCH 08/14] librepy attempt --- securitylayer.r2py | 15 ++++++++------- tests/ut_seattlelib_strace_fileobj.r2py | 15 ++++++++++++--- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/securitylayer.r2py b/securitylayer.r2py index 22edde1..431a07b 100644 --- a/securitylayer.r2py +++ b/securitylayer.r2py @@ -7,11 +7,14 @@ FUNC="func" OBJC="objc" -# dy_import_module_symbols("oldlog.r2py") +#an attempt at loding a module into security layer +# libfile = dy_import_module("librepyfile.r2py") +# librepyfile = libfile.open("tracer.txt") +# librepyfile.write("hi") +# librepyfile.close() -OLD_SLEEP = CHILD_CONTEXT_DEF["log"] +OLD_LOG = CHILD_CONTEXT_DEF["log"] -#no idea how to pipe into anohter module..... def log_between_moules(args): myfile = openfile("tracer.txt", True) myfile.writeat(args, 0) @@ -23,10 +26,8 @@ def log_between_moules(args): # Mapping our function to log() CHILD_CONTEXT_DEF["log"] = {TYPE:FUNC,ARGS:((str),),EXCP:None,RETURN:None,TARGET:log_between_moules} - - -#no idea how to restore old log for UT only! -CHILD_CONTEXT_DEF["log"] = OLD_SLEEP +#restoring log, giving it a new name +CHILD_CONTEXT_DEF["the_original_log_function"] = OLD_LOG # Dispatch diff --git a/tests/ut_seattlelib_strace_fileobj.r2py b/tests/ut_seattlelib_strace_fileobj.r2py index d92774b..79676dd 100644 --- a/tests/ut_seattlelib_strace_fileobj.r2py +++ b/tests/ut_seattlelib_strace_fileobj.r2py @@ -4,12 +4,21 @@ # add_dy_support(locals()) # dy_import_module_symbols("strace.r2py") -# import subprocess -#pragma repy restrictions.default dylink.r2py encasementlib.r2py securitylayer.r2py strace.r2py seclayer.r2py +#pragma repy restrictions.default dylink.r2py encasementlib.r2py securitylayer.r2py strace.r2py +#this gets traced +dy_import_module_symbols("librepyfile.r2py") +librepyfile = open("tracer.txt") + +#want to read this traced output... but exception fielobj = openfile("hi.txt", True) -# log("hi") + +#this gets traced also +value = librepyfile.readline() +the_original_log_function(value) + + # process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) # output = process.stdout.readlines() From 76e1adbb2add51a74413924699a70909e7fe8430 Mon Sep 17 00:00:00 2001 From: Ajay Shenoy Date: Thu, 14 Jul 2016 10:34:55 -0400 Subject: [PATCH 09/14] Package Here is the nearly completed UTs for strace, where log_between_modules is the security layer that redefines the log function --- ...ritylayer.r2py => log_between_modules.r2py | 18 +-- strace.r2py | 13 +- tests/ut_seattlelib_strace_fileobj.py | 104 ------------- tests/ut_seattlelib_strace_fileobj.r2py | 140 ++++++++---------- tests/ut_seattlelib_strace_lockobj.py | 49 ------ tests/ut_seattlelib_strace_lockobj.r2py | 46 ++++++ tests/ut_seattlelib_strace_socketobj.py | 76 ---------- tests/ut_seattlelib_strace_socketobj.r2py | 73 +++++++++ tests/ut_seattlelib_strace_tcpserverobj.py | 55 ------- tests/ut_seattlelib_strace_tcpserverobj.r2py | 51 +++++++ tests/ut_seattlelib_strace_udpserverobj.py | 52 ------- tests/ut_seattlelib_strace_udpserverobj.r2py | 50 +++++++ tests/ut_seattlelib_strace_vnobj.py | 53 ------- tests/ut_seattlelib_strace_vnobj.r2py | 53 +++++++ 14 files changed, 344 insertions(+), 489 deletions(-) rename securitylayer.r2py => log_between_modules.r2py (51%) delete mode 100644 tests/ut_seattlelib_strace_fileobj.py delete mode 100644 tests/ut_seattlelib_strace_lockobj.py create mode 100644 tests/ut_seattlelib_strace_lockobj.r2py delete mode 100644 tests/ut_seattlelib_strace_socketobj.py create mode 100644 tests/ut_seattlelib_strace_socketobj.r2py delete mode 100644 tests/ut_seattlelib_strace_tcpserverobj.py create mode 100644 tests/ut_seattlelib_strace_tcpserverobj.r2py delete mode 100644 tests/ut_seattlelib_strace_udpserverobj.py create mode 100644 tests/ut_seattlelib_strace_udpserverobj.r2py delete mode 100644 tests/ut_seattlelib_strace_vnobj.py create mode 100644 tests/ut_seattlelib_strace_vnobj.r2py diff --git a/securitylayer.r2py b/log_between_modules.r2py similarity index 51% rename from securitylayer.r2py rename to log_between_modules.r2py index 431a07b..3ee7b1c 100644 --- a/securitylayer.r2py +++ b/log_between_modules.r2py @@ -7,24 +7,18 @@ FUNC="func" OBJC="objc" -#an attempt at loding a module into security layer -# libfile = dy_import_module("librepyfile.r2py") -# librepyfile = libfile.open("tracer.txt") -# librepyfile.write("hi") -# librepyfile.close() - OLD_LOG = CHILD_CONTEXT_DEF["log"] -def log_between_moules(args): - myfile = openfile("tracer.txt", True) - myfile.writeat(args, 0) - myfile.close() - +def log_between_modules(args): + libfile = dy_import_module("librepyfile.r2py") + librepyfile = libfile.open("tracer.txt") + librepyfile.write(args) + librepyfile.close() # Mapping our function to log() -CHILD_CONTEXT_DEF["log"] = {TYPE:FUNC,ARGS:((str),),EXCP:None,RETURN:None,TARGET:log_between_moules} +CHILD_CONTEXT_DEF["log"] = {TYPE:FUNC,ARGS:((str),),EXCP:None,RETURN:None,TARGET:log_between_modules} #restoring log, giving it a new name CHILD_CONTEXT_DEF["the_original_log_function"] = OLD_LOG diff --git a/strace.r2py b/strace.r2py index 9ccbae3..be5b0fc 100644 --- a/strace.r2py +++ b/strace.r2py @@ -9,10 +9,6 @@ Description: Call-time function [instance] [args] [ = result ] """ -#file to log a single trace call -tracefile = "traced.txt" - - # These API functions do _not_ return an object, and can be handled uniformly. NON_OBJ_API_CALLS = ["gethostbyname","getmyip","sendmessage","listfiles","removefile", "exitall","getruntime","randombytes","createthread","sleep","getthreadname", @@ -43,6 +39,9 @@ def getruntime(): return round(_orig_getruntime(), 5) + + + # Do a traced function call def traced_call(self,name,func,args,kwargs,no_return=False,print_args=True,print_result=True): # Store the time, function call and arguments @@ -65,7 +64,6 @@ def traced_call(self,name,func,args,kwargs,no_return=False,print_args=True,print log(call_string) PRINT_LOCK.release() - # Get the result try: result = func(*args,**kwargs) @@ -75,7 +73,6 @@ def traced_call(self,name,func,args,kwargs,no_return=False,print_args=True,print PRINT_LOCK.acquire(True) log(call_string,"->",str(type(e))+" "+str(e)) PRINT_LOCK.release() - raise # Return if there is no result @@ -92,8 +89,6 @@ def traced_call(self,name,func,args,kwargs,no_return=False,print_args=True,print PRINT_LOCK.acquire(True) log(call_string) PRINT_LOCK.release() - - return result @@ -263,6 +258,8 @@ def wrap_all(): CHILD_CONTEXT["createvirtualnamespace"] = wrapped_virtual_namespace + + # # Wrap all the functions wrap_all() diff --git a/tests/ut_seattlelib_strace_fileobj.py b/tests/ut_seattlelib_strace_fileobj.py deleted file mode 100644 index bc032c0..0000000 --- a/tests/ut_seattlelib_strace_fileobj.py +++ /dev/null @@ -1,104 +0,0 @@ -#this unit test checks the functionality of the file traced object - -from repyportability import * -add_dy_support(locals()) -dy_import_module_symbols("strace.r2py") - -import subprocess - - -fielobj = wrapped_openfile("hi.txt", True) - -process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) -output = process.stdout.readlines() -tracedcall = output[0] - -#Check apicall to traced call -if("openfile ('hi.txt', True)" not in tracedcall): - log("Trace api call failed to trace correct function, openfile ('hi.txt', True)") -if("emulfile.emulated_file object"not in tracedcall): - log("Failed to trace trace correct function as correct object") - - -# #write something using trace call -fielobj.writeat("asdfasdf",0) -process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) -output = process.stdout.readlines() - -#Check apicall to traced call -if("file.writeat" not in tracedcall): - log("Trace api call failed to trace correct function, writeat") -if("(\'asdfasdf\', 0)" not in tracedcall): - log("Trace api call failed to trace correct function with correct parameters, fielobj.writeat(\'asdfasdf\',0)") -if("emulfile.emulated_file object"not in tracedcall): - log("Failed to trace trace correct function as correct object") - - - -#close file using trace call -fielobj.close() - -#open traced file -process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) -output = process.stdout.readlines() -tracedcall = output[0] - - -#Check apicall to traced call -if("file.close" not in tracedcall): - log("Trace api call failed to trace correct function, close") -if("emulfile.emulated_file object"not in tracedcall): - log("Failed to trace trace correct function as correct object") - - -#open existing file with trace call -fielobj = wrapped_openfile("../hi.txt", True) - -#open traced file -process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) -output = process.stdout.readlines() -tracedcall = output[0] - - -#Check apicall to traced call -if("openfile" not in tracedcall): - log("Trace api call failed to trace correct function, openfile") -if("(\'../hi.txt\', True)" not in tracedcall): - log("Trace api call failed to trace correct function with correct parameters, openfile(\'../hi.txt\', True)") -if("emulfile.emulated_file object"not in tracedcall): - log("Failed to trace trace correct function as correct object") - -#read from file usiung trace call -fielobj.readat(0,4) - -#open traced file -process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) -output = process.stdout.readlines() -tracedcall = output[0] - - - -#Check apicall to traced call -if("file.readat" not in tracedcall): - log("Trace api call failed to trace correct function, readat") -if("(0, 4)" not in tracedcall): - log("Trace api call failed to trace correct function with correct parameters, fileobj.readat (0, 4)") -if("emulfile.emulated_file object"not in tracedcall): - log("Failed to trace trace correct function as correct object") - -#close file using trace call -fielobj.close() - -#open traced file -process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) -output = process.stdout.readlines() -tracedcall = output[0] - - -#Check apicall to traced call -if("file.close" not in tracedcall): - log("Trace api call failed to trace correct function, close") -if("emulfile.emulated_file object"not in tracedcall): - log("Failed to trace trace correct function as correct object") - - diff --git a/tests/ut_seattlelib_strace_fileobj.r2py b/tests/ut_seattlelib_strace_fileobj.r2py index 79676dd..79d7bcc 100644 --- a/tests/ut_seattlelib_strace_fileobj.r2py +++ b/tests/ut_seattlelib_strace_fileobj.r2py @@ -1,115 +1,95 @@ #this unit test checks the functionality of the file traced object -# from repyportability import * -# add_dy_support(locals()) -# dy_import_module_symbols("strace.r2py") +#pragma repy restrictions.default dylink.r2py encasementlib.r2py log_between_modules.r2py strace.r2py -#pragma repy restrictions.default dylink.r2py encasementlib.r2py securitylayer.r2py strace.r2py - -#this gets traced +# First dylink import gives: +# 0.510231971741 Fatal Error: Function 'the_original_log_function' tried to raise exception of type: '' which is forbidden. dy_import_module_symbols("librepyfile.r2py") librepyfile = open("tracer.txt") -#want to read this traced output... but exception fielobj = openfile("hi.txt", True) -#this gets traced also -value = librepyfile.readline() -the_original_log_function(value) - - +tracedcall = librepyfile.readline() -# process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) -# output = process.stdout.readlines() -# tracedcall = output[0] +#Check apicall to traced call +if("openfile ('hi.txt', True)" not in tracedcall): + the_original_log_function("Trace api call failed to trace correct function, openfile ('hi.txt', True)") +if("emulfile.emulated_file object"not in tracedcall): + the_original_log_function("Failed to trace trace correct function as correct object") -# #Check apicall to traced call -# if("openfile ('hi.txt', True)" not in tracedcall): -# log("Trace api call failed to trace correct function, openfile ('hi.txt', True)") -# if("emulfile.emulated_file object"not in tracedcall): -# log("Failed to trace trace correct function as correct object") +# #write something using trace call +fielobj.writeat("asdfasdf",0) -# # #write something using trace call -# fielobj.writeat("asdfasdf",0) -# process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) -# output = process.stdout.readlines() +tracedcall = librepyfile.readline() -# #Check apicall to traced call -# if("file.writeat" not in tracedcall): -# log("Trace api call failed to trace correct function, writeat") -# if("(\'asdfasdf\', 0)" not in tracedcall): -# log("Trace api call failed to trace correct function with correct parameters, fielobj.writeat(\'asdfasdf\',0)") -# if("emulfile.emulated_file object"not in tracedcall): -# log("Failed to trace trace correct function as correct object") +#Check apicall to traced call +if("file.writeat" not in tracedcall): + the_original_log_function("Trace api call failed to trace correct function, writeat") +if("(\'asdfasdf\', 0)" not in tracedcall): + the_original_log_function("Trace api call failed to trace correct function with correct parameters, fielobj.writeat(\'asdfasdf\',0)") +if("emulfile.emulated_file object"not in tracedcall): + the_original_log_function("Failed to trace trace correct function as correct object") -# #close file using trace call -# fielobj.close() +#close file using trace call +fielobj.close() -# #open traced file -# process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) -# output = process.stdout.readlines() -# tracedcall = output[0] +#open traced file +tracedcall = librepyfile.readline() -# #Check apicall to traced call -# if("file.close" not in tracedcall): -# log("Trace api call failed to trace correct function, close") -# if("emulfile.emulated_file object"not in tracedcall): -# log("Failed to trace trace correct function as correct object") +#Check apicall to traced call +if("file.close" not in tracedcall): + the_original_log_function("Trace api call failed to trace correct function, close") +if("emulfile.emulated_file object"not in tracedcall): + the_original_log_function("Failed to trace trace correct function as correct object") -# #open existing file with trace call -# fielobj = wrapped_openfile("../hi.txt", True) - -# #open traced file -# process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) -# output = process.stdout.readlines() -# tracedcall = output[0] +#open existing file with trace call +fielobj = openfile("hi.txt", True) +#open traced file +tracedcall = librepyfile.readline() -# #Check apicall to traced call -# if("openfile" not in tracedcall): -# log("Trace api call failed to trace correct function, openfile") -# if("(\'../hi.txt\', True)" not in tracedcall): -# log("Trace api call failed to trace correct function with correct parameters, openfile(\'../hi.txt\', True)") -# if("emulfile.emulated_file object"not in tracedcall): -# log("Failed to trace trace correct function as correct object") -# #read from file usiung trace call -# fielobj.readat(0,4) +#Check apicall to traced call +if("openfile" not in tracedcall): + the_original_log_function("Trace api call failed to trace correct function, openfile") +if("(\'hi.txt\', True)" not in tracedcall): + the_original_log_function("Trace api call failed to trace correct function with correct parameters, openfile(\'hi.txt\', True)") +if("emulfile.emulated_file object"not in tracedcall): + the_original_log_function("Failed to trace trace correct function as correct object") -# #open traced file -# process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) -# output = process.stdout.readlines() -# tracedcall = output[0] +#read from file usiung trace call +fielobj.readat(0,4) +#open traced file +tracedcall = librepyfile.readline() -# #Check apicall to traced call -# if("file.readat" not in tracedcall): -# log("Trace api call failed to trace correct function, readat") -# if("(0, 4)" not in tracedcall): -# log("Trace api call failed to trace correct function with correct parameters, fileobj.readat (0, 4)") -# if("emulfile.emulated_file object"not in tracedcall): -# log("Failed to trace trace correct function as correct object") -# #close file using trace call -# fielobj.close() +#Check apicall to traced call +if("file.readat" not in tracedcall): + the_original_log_function("Trace api call failed to trace correct function, readat") +if("(0, 4)" not in tracedcall): + the_original_log_function("Trace api call failed to trace correct function with correct parameters, fileobj.readat (0, 4)") +if("emulfile.emulated_file object"not in tracedcall): + the_original_log_function("Failed to trace trace correct function as correct object") -# #open traced file -# process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) -# output = process.stdout.readlines() -# tracedcall = output[0] +#close file using trace call +fielobj.close() +#open traced file +tracedcall = librepyfile.readline() -# #Check apicall to traced call -# if("file.close" not in tracedcall): -# log("Trace api call failed to trace correct function, close") -# if("emulfile.emulated_file object"not in tracedcall): -# log("Failed to trace trace correct function as correct object") +#Check apicall to traced call +if("file.close" not in tracedcall): + the_original_log_function("Trace api call failed to trace correct function, close") +if("emulfile.emulated_file object"not in tracedcall): + the_original_log_function("Failed to trace trace correct function as correct object") +librepyfile.close() \ No newline at end of file diff --git a/tests/ut_seattlelib_strace_lockobj.py b/tests/ut_seattlelib_strace_lockobj.py deleted file mode 100644 index 9bd9d1c..0000000 --- a/tests/ut_seattlelib_strace_lockobj.py +++ /dev/null @@ -1,49 +0,0 @@ -#this unit test checks the functionality of the lock traced object - -from repyportability import * -add_dy_support(locals()) -dy_import_module_symbols("strace.r2py") - -import subprocess - - -#create a lock -PRINT_LOCK = wrapped_createlock() - -process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) -output = process.stdout.readlines() -tracedcall = output[0] - -#Check apicall to traced call -if("createlock" not in tracedcall): - log("Trace api call failed to trace correct function, createlock") -if("emulmisc.emulated_lock object"not in tracedcall): - log("Failed to trace trace correct function as correct object") - -PRINT_LOCK.acquire(True) - -process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) -output = process.stdout.readlines() -tracedcall = output[0] - -#Check apicall to traced call -if("lock.acquire" not in tracedcall): - log("Trace api call failed to trace correct function, lock.acquire") -if("(True,)" not in tracedcall): - log("Trace api call failed to trace correct function with correct paramaters, lock.acquire(True)") -if("emulmisc.emulated_lock object"not in tracedcall): - log("Failed to trace trace correct function as correct object") - - - -PRINT_LOCK.release() - -process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) -output = process.stdout.readlines() -tracedcall = output[0] - -#Check apicall to traced call -if("lock.release" not in tracedcall): - log("Trace api call failed to trace correct function, createlock") -if("emulmisc.emulated_lock object"not in tracedcall): - log("Failed to trace trace correct function as correct object") \ No newline at end of file diff --git a/tests/ut_seattlelib_strace_lockobj.r2py b/tests/ut_seattlelib_strace_lockobj.r2py new file mode 100644 index 0000000..1afc49b --- /dev/null +++ b/tests/ut_seattlelib_strace_lockobj.r2py @@ -0,0 +1,46 @@ +#this unit test checks the functionality of the lock traced object + + +#pragma repy restrictions.default dylink.r2py encasementlib.r2py log_between_modules.r2py strace.r2py + +# First dylink import gives: +# 0.510231971741 Fatal Error: Function 'the_original_log_function' tried to raise exception of type: '' which is forbidden. +dy_import_module_symbols("librepyfile.r2py") +librepyfile = open("tracer.txt") + +#create a lock +LOCK = createlock() + +tracedcall = librepyfile.readline() + +#Check apicall to traced call +if("createlock" not in tracedcall): + the_original_log_function("Trace api call failed to trace correct function, createlock") +if("emulmisc.emulated_lock object"not in tracedcall): + the_original_log_function("Failed to trace trace correct function as correct object") + +LOCK.acquire(True) + +tracedcall = librepyfile.readline() + +#Check apicall to traced call +if("lock.acquire" not in tracedcall): + the_original_log_function("Trace api call failed to trace correct function, lock.acquire") +if("(True,)" not in tracedcall): + the_original_log_function("Trace api call failed to trace correct function with correct paramaters, lock.acquire(True)") +if("emulmisc.emulated_lock object"not in tracedcall): + the_original_log_function("Failed to trace trace correct function as correct object") + + +LOCK.release() + +tracedcall = librepyfile.readline() + +#Check apicall to traced call +if("lock.release" not in tracedcall): + the_original_log_function("Trace api call failed to trace correct function, createlock") +if("emulmisc.emulated_lock object"not in tracedcall): + the_original_log_function("Failed to trace trace correct function as correct object") + + +librepyfile.close() diff --git a/tests/ut_seattlelib_strace_socketobj.py b/tests/ut_seattlelib_strace_socketobj.py deleted file mode 100644 index f36f1cf..0000000 --- a/tests/ut_seattlelib_strace_socketobj.py +++ /dev/null @@ -1,76 +0,0 @@ -#this unit test checks the functionality of the strace socket object - -from repyportability import * -add_dy_support(locals()) -dy_import_module_symbols("strace.r2py") - -import subprocess - -localip = "127.0.0.1" -localport1 = 12345 -localport2 = 12347 -targetip = "127.0.0.1" -targetport = 12346 -timeout = 1.0 - -conn = wrapped_openconnection(targetip, targetport, localip, localport1, timeout) - -process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) -output = process.stdout.readlines() -tracedcall = output[0] - -#Check apicall to traced call -if("openconnection" not in tracedcall): - log("Trace api call failed to trace correct function, openconnection") -if("('127.0.0.1', 12346, '127.0.0.1', 12345, 1.0)" not in tracedcall): - log("Trace api call failed to trace correct function with correct parameters, openconnection('127.0.0.1', 12346, '127.0.0.1', 12345, 1.0)") -if("emulcomm.EmulatedSocket" not in tracedcall): - log("Failed to trace trace correct function as correct object") - - - -conn.send("Hi from repy program!") - - -# 0.01771 socket.send ('Hi from repy program!',) = 21 - -process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) -output = process.stdout.readlines() -tracedcall = output[0] - -#Check apicall to traced call -if("socket.send" not in tracedcall): - log("Trace api call failed to trace correct function, socket.send") -if("('Hi from repy program!',)" not in tracedcall and "21" not in tracedcall): - log("Trace api call failed to trace correct function with correct parameters, send('Hi from repy program!',)") -if("emulcomm.EmulatedSocket" not in tracedcall): - log("Failed to trace trace correct function as correct object") - - -conn.recv(21) - -process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) -output = process.stdout.readlines() -tracedcall = output[0] - -#Check apicall to traced call -if("socket.recv" not in tracedcall): - log("Trace api call failed to trace correct function, socket.send") -if("(21,)" not in tracedcall): - log("Trace api call failed to trace correct function with correct parameters, send('Hi from repy program!',)") -if("emulcomm.EmulatedSocket" not in tracedcall): - log("Failed to trace trace correct function as correct object") - - - -conn.close() - -process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) -output = process.stdout.readlines() -tracedcall = output[0] - -#Check apicall to traced call -if("socket.close" not in tracedcall): - log("Trace api call failed to trace correct function, socket.send") -if("emulcomm.EmulatedSocket" not in tracedcall): - log("Failed to trace trace correct function as correct object") \ No newline at end of file diff --git a/tests/ut_seattlelib_strace_socketobj.r2py b/tests/ut_seattlelib_strace_socketobj.r2py new file mode 100644 index 0000000..c6844e5 --- /dev/null +++ b/tests/ut_seattlelib_strace_socketobj.r2py @@ -0,0 +1,73 @@ +#this unit test checks the functionality of the strace socket object + + +#pragma repy restrictions.default dylink.r2py encasementlib.r2py log_between_modules.r2py strace.r2py + + +# First dylink import gives: +# 0.510231971741 Fatal Error: Function 'the_original_log_function' tried to raise exception of type: '' which is forbidden. +dy_import_module_symbols("librepyfile.r2py") +librepyfile = open("tracer.txt") + + +localip = "127.0.0.1" +localport1 = 12345 +localport2 = 12347 +targetip = "127.0.0.1" +targetport = 12346 +timeout = 1.0 + +conn = openconnection(targetip, targetport, localip, localport1, timeout) + +tracedcall = librepyfile.readline() + + +#Check apicall to traced call +if("openconnection" not in tracedcall): + the_original_log_function("Trace api call failed to trace correct function, openconnection") +if("('127.0.0.1', 12346, '127.0.0.1', 12345, 1.0)" not in tracedcall): + the_original_log_function("Trace api call failed to trace correct function with correct parameters, openconnection('127.0.0.1', 12346, '127.0.0.1', 12345, 1.0)") +if("emulcomm.EmulatedSocket" not in tracedcall): + the_original_log_function("Failed to trace trace correct function as correct object") + + + +conn.send("Hi from repy program!") + + +tracedcall = librepyfile.readline() + +#Check apicall to traced call +if("socket.send" not in tracedcall): + the_original_log_function("Trace api call failed to trace correct function, socket.send") +if("('Hi from repy program!',)" not in tracedcall and "21" not in tracedcall): + the_original_log_function("Trace api call failed to trace correct function with correct parameters, send('Hi from repy program!',)") +if("emulcomm.EmulatedSocket" not in tracedcall): + the_original_log_function("Failed to trace trace correct function as correct object") + + +conn.recv(21) + +tracedcall = librepyfile.readline() + +#Check apicall to traced call +if("socket.recv" not in tracedcall): + the_original_log_function("Trace api call failed to trace correct function, socket.send") +if("(21,)" not in tracedcall): + the_original_log_function("Trace api call failed to trace correct function with correct parameters, send('Hi from repy program!',)") +if("emulcomm.EmulatedSocket" not in tracedcall): + the_original_log_function("Failed to trace trace correct function as correct object") + + + +conn.close() + +tracedcall = librepyfile.readline() + +#Check apicall to traced call +if("socket.close" not in tracedcall): + the_original_log_function("Trace api call failed to trace correct function, socket.send") +if("emulcomm.EmulatedSocket" not in tracedcall): + the_original_log_function("Failed to trace trace correct function as correct object") + +librepyfile.close() diff --git a/tests/ut_seattlelib_strace_tcpserverobj.py b/tests/ut_seattlelib_strace_tcpserverobj.py deleted file mode 100644 index 196a891..0000000 --- a/tests/ut_seattlelib_strace_tcpserverobj.py +++ /dev/null @@ -1,55 +0,0 @@ -#this unit test checks the functionality of the tcp server traced object - -from repyportability import * -add_dy_support(locals()) -dy_import_module_symbols("strace.r2py") - -import subprocess - - -tcpserverobj = wrapped_listenforconnection(getmyip(), 12345) - -process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) -output = process.stdout.readlines() -tracedcall = output[0] - -#Check apicall to traced call -if("listenforconnection" not in tracedcall): - log("Trace api call failed to trace correct function, openfile") -if("('192.168.183.128', 12345)" not in tracedcall): - log("Trace api call failed to trace correct function with correct parameters, openfile('../hi.txt', True)") -if("emulcomm.TCPServerSocket"not in tracedcall): - log("Failed to trace trace correct function as correct object") - -try: - tcpserverobj.getconnection() -except SocketWouldBlockError: -#expected - pass - -process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) -output = process.stdout.readlines() -tracedcall = output[0] - -#Check apicall to traced call -if("TCPServerSocket.getconnection" not in tracedcall): - log("Trace api call failed to trace correct function, openfile") -if("emulcomm.TCPServerSocket"not in tracedcall): - log("Failed to trace trace correct function as correct object") - - -tcpserverobj.close() - -process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) -output = process.stdout.readlines() -tracedcall = output[0] - -#Check apicall to traced call -if("TCPServerSocket.close" not in tracedcall): - log("Trace api call failed to trace correct function, openfile") -if("emulcomm.TCPServerSocket"not in tracedcall): - log("Failed to trace trace correct function as correct object") - - -# wrapped_openconnection("127.0.0.1", 12345, getmyip(),63100, 10) - diff --git a/tests/ut_seattlelib_strace_tcpserverobj.r2py b/tests/ut_seattlelib_strace_tcpserverobj.r2py new file mode 100644 index 0000000..2346b41 --- /dev/null +++ b/tests/ut_seattlelib_strace_tcpserverobj.r2py @@ -0,0 +1,51 @@ +#this unit test checks the functionality of the tcp server traced object + +#pragma repy restrictions.default dylink.r2py encasementlib.r2py log_between_modules.r2py strace.r2py + + +# First dylink import gives: +# 0.510231971741 Fatal Error: Function 'the_original_log_function' tried to raise exception of type: '' which is forbidden. +dy_import_module_symbols("librepyfile.r2py") +librepyfile = open("tracer.txt") + +listenforconnection(getmyip(), 12347) + +tracedcall = librepyfile.readline() + + +#Check apicall to traced call +if("listenforconnection" not in tracedcall): + the_original_log_function("Trace api call failed to trace correct function, openfile") +if("('192.168.183.128', 12345)" not in tracedcall): + the_original_log_function("Trace api call failed to trace correct function with correct parameters, openfile('../hi.txt', True)") +if("emulcomm.TCPServerSocket"not in tracedcall): + the_original_log_function("Failed to trace trace correct function as correct object") + +try: + tcpserverobj.getconnection() +except SocketWouldBlockError: + #expected + pass + +tracedcall = librepyfile.readline() + + +#Check apicall to traced call +if("TCPServerSocket.getconnection" not in tracedcall): + the_original_log_function("Trace api call failed to trace correct function, openfile") +if("emulcomm.TCPServerSocket"not in tracedcall): + the_original_log_function("Failed to trace trace correct function as correct object") + + +tcpserverobj.close() + +tracedcall = librepyfile.readline() + +#Check apicall to traced call +if("TCPServerSocket.close" not in tracedcall): + the_original_log_function("Trace api call failed to trace correct function, openfile") +if("emulcomm.TCPServerSocket"not in tracedcall): + the_original_log_function("Failed to trace trace correct function as correct object") + + +librepyfile.close() diff --git a/tests/ut_seattlelib_strace_udpserverobj.py b/tests/ut_seattlelib_strace_udpserverobj.py deleted file mode 100644 index 4da90e2..0000000 --- a/tests/ut_seattlelib_strace_udpserverobj.py +++ /dev/null @@ -1,52 +0,0 @@ -#this unit test checks the functionality of the udp server traced object - -from repyportability import * -add_dy_support(locals()) -dy_import_module_symbols("strace.r2py") - -import subprocess - - - -udpserverobj = wrapped_listenformessage(getmyip(), 12345) - -process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) -output = process.stdout.readlines() -tracedcall = output[0] - -#Check apicall to traced call -if("listenformessage" not in tracedcall): - log("Trace api call failed to trace correct function, openfile") -if("('192.168.183.128', 12345)" not in tracedcall): - log("Trace api call failed to trace correct function with correct parameters, openfile('../hi.txt', True)") -if("emulcomm.UDPServerSocket"not in tracedcall): - log("Failed to trace trace correct function as correct object") - -try: - udpserverobj.getmessage() -except SocketWouldBlockError: -#expected - pass - -process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) -output = process.stdout.readlines() -tracedcall = output[0] - -#Check apicall to traced call -if("UDPServerSocket.getmessage" not in tracedcall): - log("Trace api call failed to trace correct function, openfile") -if("emulcomm.UDPServerSocket"not in tracedcall): - log("Failed to trace trace correct function as correct object") - - -udpserverobj.close() - -process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) -output = process.stdout.readlines() -tracedcall = output[0] - -#Check apicall to traced call -if("UDPServerSocket.close" not in tracedcall): - log("Trace api call failed to trace correct function, openfile") -if("emulcomm.UDPServerSocket"not in tracedcall): - log("Failed to trace trace correct function as correct object") diff --git a/tests/ut_seattlelib_strace_udpserverobj.r2py b/tests/ut_seattlelib_strace_udpserverobj.r2py new file mode 100644 index 0000000..5391d03 --- /dev/null +++ b/tests/ut_seattlelib_strace_udpserverobj.r2py @@ -0,0 +1,50 @@ +#this unit test checks the functionality of the udp server traced object + + +#pragma repy restrictions.default dylink.r2py encasementlib.r2py log_between_modules.r2py strace.r2py + +# First dylink import gives: +# 0.510231971741 Fatal Error: Function 'the_original_log_function' tried to raise exception of type: '' which is forbidden. +dy_import_module_symbols("librepyfile.r2py") +librepyfile = open("tracer.txt") + +udpserverobj = wrapped_listenformessage(getmyip(), 12345) + +tracedcall = librepyfile.readline() + + +#Check apicall to traced call +if("listenformessage" not in tracedcall): + the_original_log_function("Trace api call failed to trace correct function, openfile") +if("('192.168.183.128', 12345)" not in tracedcall): + the_original_log_function("Trace api call failed to trace correct function with correct parameters, openfile('../hi.txt', True)") +if("emulcomm.UDPServerSocket"not in tracedcall): + the_original_log_function("Failed to trace trace correct function as correct object") + +try: + udpserverobj.getmessage() +except SocketWouldBlockError: + #expected + pass + +tracedcall = librepyfile.readline() + + +#Check apicall to traced call +if("UDPServerSocket.getmessage" not in tracedcall): + the_original_log_function("Trace api call failed to trace correct function, openfile") +if("emulcomm.UDPServerSocket"not in tracedcall): + the_original_log_function("Failed to trace trace correct function as correct object") + + +udpserverobj.close() + +tracedcall = librepyfile.readline() + +#Check apicall to traced call +if("UDPServerSocket.close" not in tracedcall): + the_original_log_function("Trace api call failed to trace correct function, openfile") +if("emulcomm.UDPServerSocket"not in tracedcall): + the_original_log_function("Failed to trace trace correct function as correct object") + +librepyfile.close() diff --git a/tests/ut_seattlelib_strace_vnobj.py b/tests/ut_seattlelib_strace_vnobj.py deleted file mode 100644 index 502a7f1..0000000 --- a/tests/ut_seattlelib_strace_vnobj.py +++ /dev/null @@ -1,53 +0,0 @@ -#this unit test checks the functionality of the strace virtual namespace object - -from repyportability import * -add_dy_support(locals()) -dy_import_module_symbols("strace.r2py") - -import subprocess - -#most of code taken from ut_repyv2api_virtualnamespace-eval.py - -# Small code snippet, safe -safe_code = "meaning_of_life = 42\n" - -# Try to make the safe virtual namespace -safe_virt = wrapped_virtual_namespace(safe_code, "Test VN") - - -process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) -output = process.stdout.readlines() -tracedcall = output[0] - -#Check apicall to traced call -if("VirtualNamespace(...)" not in tracedcall): - log("Trace api call failed to trace correct function, VirtualNamespace(...)") -if("virtual_namespace.VirtualNamespace " not in tracedcall): - log("Failed to trace trace correct function as correct object") - - - -# Create a execution context -context = SafeDict() - -# Evaluate -context_2 = safe_virt.evaluate(context) - -process = subprocess.Popen('', stderr=subprocess.PIPE, stdout=subprocess.PIPE) -output = process.stdout.readlines() -tracedcall = output[0] - -#Check apicall to traced call -if("VirtualNamespace.evaluate" not in tracedcall): - log("Trace api call failed to trace correct function, evaluate") -if("virtual_namespace.VirtualNamespace " not in tracedcall): - log("Failed to trace trace correct function as correct object") - - -# Check that the context is the same -if context is not context_2: - log("Error! Context mis-match!",'\n') - -# Check for the meaning of life -if "meaning_of_life" not in context: - log("Meaning of life is undefined! Existential error!",'\n') \ No newline at end of file diff --git a/tests/ut_seattlelib_strace_vnobj.r2py b/tests/ut_seattlelib_strace_vnobj.r2py new file mode 100644 index 0000000..16437a0 --- /dev/null +++ b/tests/ut_seattlelib_strace_vnobj.r2py @@ -0,0 +1,53 @@ +#this unit test checks the functionality of the strace virtual namespace object + +#pragma repy restrictions.default dylink.r2py encasementlib.r2py log_between_modules.r2py strace.r2py + + +# First dylink import gives: +# 0.510231971741 Fatal Error: Function 'the_original_log_function' tried to raise exception of type: '' which is forbidden. +dy_import_module_symbols("librepyfile.r2py") +librepyfile = open("tracer.txt") + +# Small code snippet, safe +safe_code = "meaning_of_life = 42\n" + +# Try to make the safe virtual namespace +safe_virt = wrapped_virtual_namespace(safe_code, "Test VN") + + +tracedcall = librepyfile.readline() + +#Check apicall to traced call +if("VirtualNamespace(...)" not in tracedcall): + the_original_log_function("Trace api call failed to trace correct function, VirtualNamespace(...)") +if("virtual_namespace.VirtualNamespace " not in tracedcall): + the_original_log_function("Failed to trace trace correct function as correct object") + + + +# Create a execution context +context = SafeDict() + +# Evaluate +context_2 = safe_virt.evaluate(context) + +tracedcall = librepyfile.readline() + +#Check apicall to traced call +if("VirtualNamespace.evaluate" not in tracedcall): + the_original_log_function("Trace api call failed to trace correct function, evaluate") +if("virtual_namespace.VirtualNamespace " not in tracedcall): + the_original_log_function("Failed to trace trace correct function as correct object") + + +# Check that the context is the same +if context is not context_2: + the_original_log_function("Error! Context mis-match!",'\n') + +# Check for the meaning of life +if "meaning_of_life" not in context: + the_original_log_function("Meaning of life is undefined! Existential error!",'\n') + + + +librepyfile.close() From 720130f518c79fcc132df5b6fa6287356d21f68e Mon Sep 17 00:00:00 2001 From: Ajay Shenoy Date: Tue, 19 Jul 2016 12:07:03 -0400 Subject: [PATCH 10/14] Changed to a methord --- log_between_modules.r2py | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/log_between_modules.r2py b/log_between_modules.r2py index 3ee7b1c..59afca4 100644 --- a/log_between_modules.r2py +++ b/log_between_modules.r2py @@ -9,16 +9,33 @@ OBJC="objc" OLD_LOG = CHILD_CONTEXT_DEF["log"] -def log_between_modules(args): - libfile = dy_import_module("librepyfile.r2py") - librepyfile = libfile.open("tracer.txt") - librepyfile.write(args) - librepyfile.close() +class LogToFile(): + def __init__(self, arg, filename): + self.argument = arg + self.filename = filename + + + def writetofile(self): + libfile = dy_import_module("librepyfile.r2py") + librepyfile = libfile.open(self.filename) + librepyfile.write(self.argument) + librepyfile.close() + +log_file_def = {"obj-type":LogToFile, + "name":"LogToFile", + "writetofile":{TYPE:FUNC,ARGS:None, EXCP:Exception,RETURN:None,TARGET:LogToFile.writetofile} + } + + + +def log_between_modules(arg): + logObj = LogToFile(arg, "tracer.txt") + logObj.writetofile() # Mapping our function to log() -CHILD_CONTEXT_DEF["log"] = {TYPE:FUNC,ARGS:((str),),EXCP:None,RETURN:None,TARGET:log_between_modules} +CHILD_CONTEXT_DEF["log"] = {TYPE:FUNC,ARGS:((str),),EXCP:Exception,RETURN:None,TARGET:log_between_modules} #restoring log, giving it a new name CHILD_CONTEXT_DEF["the_original_log_function"] = OLD_LOG From 9db7ffbd1d3d19e59ddebd52f4ccd87272c51b28 Mon Sep 17 00:00:00 2001 From: Ajay Shenoy Date: Fri, 9 Sep 2016 16:38:42 -0400 Subject: [PATCH 11/14] finally fixed error I was getting earlier Formated all uts to at least 80 lines (took too long) Fixed any spelling mistakes organized functions into specific blocks etc. --- strace.r2py | 2 +- tests/ut_seattlelib_strace_fileobj.r2py | 135 ++++++++-- tests/ut_seattlelib_strace_lockobj.r2py | 74 ++++-- tests/ut_seattlelib_strace_nonobjapi.r2py | 257 +++++++++++++++++++ tests/ut_seattlelib_strace_socketobj.r2py | 165 +++++++++--- tests/ut_seattlelib_strace_tcpserverobj.r2py | 123 +++++++-- tests/ut_seattlelib_strace_udpserverobj.r2py | 119 +++++++-- tests/ut_seattlelib_strace_vnobj.r2py | 60 +++-- 8 files changed, 799 insertions(+), 136 deletions(-) create mode 100644 tests/ut_seattlelib_strace_nonobjapi.r2py diff --git a/strace.r2py b/strace.r2py index be5b0fc..5a35bc0 100644 --- a/strace.r2py +++ b/strace.r2py @@ -71,7 +71,7 @@ def traced_call(self,name,func,args,kwargs,no_return=False,print_args=True,print # On an exception, print the call at least except Exception, e: PRINT_LOCK.acquire(True) - log(call_string,"->",str(type(e))+" "+str(e)) + log(call_string + "->" + str(type(e)) + " " + str(e)) PRINT_LOCK.release() raise diff --git a/tests/ut_seattlelib_strace_fileobj.r2py b/tests/ut_seattlelib_strace_fileobj.r2py index 79d7bcc..7d1cdbc 100644 --- a/tests/ut_seattlelib_strace_fileobj.r2py +++ b/tests/ut_seattlelib_strace_fileobj.r2py @@ -1,36 +1,71 @@ -#this unit test checks the functionality of the file traced object +""" + + this unit test checks the functionality of the file traced object + Function Tested: + openfile(filename, create) + file.close() + file.readat(sizelimit, offset) + file.writeat(data, offset) +""" #pragma repy restrictions.default dylink.r2py encasementlib.r2py log_between_modules.r2py strace.r2py +#librepy is used for 2 files handles +#one file handle is for the ut (reading) +#the other is for the security module (writing) +#data is passed between security module and unit test +libfile = dy_import_module("librepy.r2py") -# First dylink import gives: -# 0.510231971741 Fatal Error: Function 'the_original_log_function' tried to raise exception of type: '' which is forbidden. -dy_import_module_symbols("librepyfile.r2py") -librepyfile = open("tracer.txt") - -fielobj = openfile("hi.txt", True) +#open file using trace call +fielobj = openfile("testfile.txt", True) +#open traced file +librepyfile = libfile.open("tracer.txt") tracedcall = librepyfile.readline() +librepyfile.close() + +""" + +1.42788 openfile ('testfile.txt', True) = > +""" #Check apicall to traced call -if("openfile ('hi.txt', True)" not in tracedcall): - the_original_log_function("Trace api call failed to trace correct function, openfile ('hi.txt', True)") +if("openfile ('testfile.txt', True)" not in tracedcall): + the_original_log_function("Trace api call failed to trace correct \ + function, openfile ('testfile.txt', True)") + if("emulfile.emulated_file object"not in tracedcall): - the_original_log_function("Failed to trace trace correct function as correct object") + the_original_log_function("Failed to trace trace correct function \ + as correct object") -# #write something using trace call +#write something using trace call fielobj.writeat("asdfasdf",0) +#open traced file +librepyfile = libfile.open("tracer.txt") tracedcall = librepyfile.readline() +librepyfile.close() + +""" + +1.43081 file.writeat > ('asdfasdf', 0) +""" #Check apicall to traced call if("file.writeat" not in tracedcall): - the_original_log_function("Trace api call failed to trace correct function, writeat") + the_original_log_function("Trace api call failed to trace correct \ + function, writeat") + if("(\'asdfasdf\', 0)" not in tracedcall): - the_original_log_function("Trace api call failed to trace correct function with correct parameters, fielobj.writeat(\'asdfasdf\',0)") + the_original_log_function("Trace api call failed to trace correct \ + function with correct parameters, fielobj.writeat(\'asdfasdf\',0)") + if("emulfile.emulated_file object"not in tracedcall): - the_original_log_function("Failed to trace trace correct function as correct object") + the_original_log_function("Failed to trace trace correct function \ + as correct object") @@ -38,58 +73,104 @@ if("emulfile.emulated_file object"not in tracedcall): fielobj.close() #open traced file +librepyfile = libfile.open("tracer.txt") tracedcall = librepyfile.readline() +librepyfile.close() +""" + +1.43271 file.close >>> ('asdfasdf', 0) +""" #Check apicall to traced call if("file.close" not in tracedcall): - the_original_log_function("Trace api call failed to trace correct function, close") + the_original_log_function("Trace api call failed to trace \ + correct function, close") + if("emulfile.emulated_file object"not in tracedcall): - the_original_log_function("Failed to trace trace correct function as correct object") + the_original_log_function("Failed to trace trace correct \ + function as correct object") #open existing file with trace call -fielobj = openfile("hi.txt", True) +fielobj = openfile("testfile.txt", True) #open traced file +librepyfile = libfile.open("tracer.txt") tracedcall = librepyfile.readline() +librepyfile.close() +""" + +1.43453 openfile ('testfile.txt', True) = >) +""" #Check apicall to traced call if("openfile" not in tracedcall): - the_original_log_function("Trace api call failed to trace correct function, openfile") -if("(\'hi.txt\', True)" not in tracedcall): - the_original_log_function("Trace api call failed to trace correct function with correct parameters, openfile(\'hi.txt\', True)") + the_original_log_function("Trace api call failed to trace \ + correct function, openfile") + +if("(\'testfile.txt\', True)" not in tracedcall): + the_original_log_function("Trace api call failed to trace correct \ + function with correct parameters, openfile(\'testfile.txt\', True)") + if("emulfile.emulated_file object"not in tracedcall): - the_original_log_function("Failed to trace trace correct function as correct object") + the_original_log_function("Failed to trace trace correct \ + function as correct object") #read from file usiung trace call fielobj.readat(0,4) #open traced file +librepyfile = libfile.open("tracer.txt") tracedcall = librepyfile.readline() +librepyfile.close() +""" + +1.43618 file.readat > (0, 4) = 0940>>) +""" #Check apicall to traced call if("file.readat" not in tracedcall): - the_original_log_function("Trace api call failed to trace correct function, readat") + the_original_log_function("Trace api call failed to trace \ + correct function, readat") + if("(0, 4)" not in tracedcall): - the_original_log_function("Trace api call failed to trace correct function with correct parameters, fileobj.readat (0, 4)") + the_original_log_function("Trace api call failed to trace correct\ + function with correct parameters, fileobj.readat (0, 4)") + if("emulfile.emulated_file object"not in tracedcall): - the_original_log_function("Failed to trace trace correct function as correct object") + the_original_log_function("Failed to trace trace correct \ + function as correct object") #close file using trace call fielobj.close() #open traced file +librepyfile = libfile.open("tracer.txt") tracedcall = librepyfile.readline() +librepyfile.close() +""" + +1.43795 file.close >> (0, 4) = 0940>>) +""" #Check apicall to traced call if("file.close" not in tracedcall): - the_original_log_function("Trace api call failed to trace correct function, close") + the_original_log_function("Trace api call failed to \ + trace correct function, close") + if("emulfile.emulated_file object"not in tracedcall): - the_original_log_function("Failed to trace trace correct function as correct object") + the_original_log_function("Failed to trace trace \ + correct function as correct object") + -librepyfile.close() \ No newline at end of file +#gets tested in Ut_stattlelib_strace_nonobjapi.r2py +removefile("testfile.txt") \ No newline at end of file diff --git a/tests/ut_seattlelib_strace_lockobj.r2py b/tests/ut_seattlelib_strace_lockobj.r2py index 1afc49b..cac14de 100644 --- a/tests/ut_seattlelib_strace_lockobj.r2py +++ b/tests/ut_seattlelib_strace_lockobj.r2py @@ -1,46 +1,90 @@ -#this unit test checks the functionality of the lock traced object - +""" + + this unit test checks the functionality of the lock traced object + Function Tested: + createlock() + lock.acquire(blocking) + lock.release() +""" #pragma repy restrictions.default dylink.r2py encasementlib.r2py log_between_modules.r2py strace.r2py -# First dylink import gives: -# 0.510231971741 Fatal Error: Function 'the_original_log_function' tried to raise exception of type: '' which is forbidden. -dy_import_module_symbols("librepyfile.r2py") -librepyfile = open("tracer.txt") +#librepy is used for 2 files handles +#one file handle is for the ut (reading) +#the other is for the security module (writing) +#data is passed between security module and unit test +libfile = dy_import_module("librepy.r2py") + #create a lock LOCK = createlock() +#open traced file +librepyfile = libfile.open("tracer.txt") tracedcall = librepyfile.readline() +librepyfile.close() + +""" + +1.35753 createlock = >(0, 4) = 0940>>) +""" #Check apicall to traced call if("createlock" not in tracedcall): - the_original_log_function("Trace api call failed to trace correct function, createlock") + the_original_log_function("Trace api call failed to trace\ + correct function, createlock") + if("emulmisc.emulated_lock object"not in tracedcall): - the_original_log_function("Failed to trace trace correct function as correct object") + the_original_log_function("Failed to trace trace correct \ + function as correct object") LOCK.acquire(True) +#open traced file +librepyfile = libfile.open("tracer.txt") tracedcall = librepyfile.readline() +librepyfile.close() + +""" + +# 1.35935 lock.acquire > (True,) = True) +""" #Check apicall to traced call if("lock.acquire" not in tracedcall): - the_original_log_function("Trace api call failed to trace correct function, lock.acquire") + the_original_log_function("Trace api call failed to trace \ + correct function, lock.acquire") + if("(True,)" not in tracedcall): - the_original_log_function("Trace api call failed to trace correct function with correct paramaters, lock.acquire(True)") + the_original_log_function("Trace api call failed to trace \ + correct function with correct paramaters, lock.acquire(True)") + if("emulmisc.emulated_lock object"not in tracedcall): - the_original_log_function("Failed to trace trace correct function as correct object") + the_original_log_function("Failed to trace trace correct\ + function as correct object") LOCK.release() +#open traced file +librepyfile = libfile.open("tracer.txt") tracedcall = librepyfile.readline() +librepyfile.close() + +""" + +# 1.36101 lock.release > (True,) = True) +""" #Check apicall to traced call if("lock.release" not in tracedcall): - the_original_log_function("Trace api call failed to trace correct function, createlock") -if("emulmisc.emulated_lock object"not in tracedcall): - the_original_log_function("Failed to trace trace correct function as correct object") + the_original_log_function("Trace api call failed to \ + trace correct function, createlock") +if("emulmisc.emulated_lock object"not in tracedcall): + the_original_log_function("Failed to trace trace \ + correct function as correct object") -librepyfile.close() diff --git a/tests/ut_seattlelib_strace_nonobjapi.r2py b/tests/ut_seattlelib_strace_nonobjapi.r2py new file mode 100644 index 0000000..dfdf4a9 --- /dev/null +++ b/tests/ut_seattlelib_strace_nonobjapi.r2py @@ -0,0 +1,257 @@ +""" + + this unit test checks the functionality of the udp server traced object + Function Tested: + gethostbyname(name) + getmyip() + listfiles() + removefile(filename) + getruntime() + randombytes() + sleep(seconds) + createthread(function) + getthreadname() + getresources() + getlasterror() + sendmessage(destip, destport, message, localip, localport) + exitall() +""" +#pragma repy restrictions.default dylink.r2py encasementlib.r2py log_between_modules.r2py strace.r2py + +#librepy is used for 2 files handles +#one file handle is for the ut (reading) +#the other is for the security module (writing) +#data is passed between security module and unit test +libfile = dy_import_module("librepy.r2py") + +""" + +0.33592 gethostbyname ('google.com',) = 74.125.29.101 +""" + +gethostbyname("google.com") + +librepyfile = libfile.open("tracer.txt") +tracedcall = librepyfile.readline() +librepyfile.close() + +if("gethostbyname ('google.com',)" not in tracedcall): + the_original_log_function("Trace api call failed to trace correct \ + function, gethostbyname ('google.com',)") + + +""" + +0.35959 getmyip = 192.168.183.129 +""" + +myip = getmyip() + +librepyfile = libfile.open("tracer.txt") +tracedcall = librepyfile.readline() +librepyfile.close() + +if("getmyip = " + str(myip) not in tracedcall): + the_original_log_function("Trace api call failed to trace correct \ + function, "+ "getmyip = " + str(myip)) + + +""" + +0.36067 listfiles = ['dylink_r2py.py', 'utfutil.pyc', 'testuniqueid.r2py', +'safe_check.py', 'natlayer_rpc.r2py', 'tracebackrepy.py', 'xmlparse.r2py', +'ut_seattlelib_strace_udpserverobj.r2py', 'testserialize_tuple.r2py', ... +""" + +listfiles() + +librepyfile = libfile.open("tracer.txt") +tracedcall = librepyfile.readline() +librepyfile.close() + +if("listfiles" not in tracedcall): + the_original_log_function("Trace api call failed to trace correct \ + function, listfiles") + +""" + +#0.3628 removefile ('traced.txt',) = None +""" + +filemy = openfile("traced.txt", True) +filemy.close() +removefile("traced.txt") + +librepyfile = libfile.open("tracer.txt") +tracedcall = librepyfile.readline() +librepyfile.close() + +if("removefile ('traced.txt',)" not in tracedcall): + the_original_log_function("Trace api call failed to trace correct \ + function, removefile('traced.txt')") + +""" + +#0.36357 getruntime = 0.36361 +""" + +getruntime() + +librepyfile = libfile.open("tracer.txt") +tracedcall = librepyfile.readline() +librepyfile.close() + +if("getruntime" not in tracedcall): + the_original_log_function("Trace api call failed to trace \ + correct function, getruntime") + +""" + +#0.36399 randombytes = ™òk;9´ÓšP¤ì¢ é®Ô}¹¸Z>Ô3=ó3Ó»ðóÒN7È» +MèÙÀ«üÆÂ„ªÏh1ÁsÃËXz\ˆW„0~F™©Ãk¯àÖ-L¢`aQÊg—¤gV +S6–ß30žõýàY +""" + +randombytes() + +librepyfile = libfile.open("tracer.txt") +tracedcall = librepyfile.readline() +librepyfile.close() + +if("randombytes" not in tracedcall): + the_original_log_function("Trace api call failed to trace correct \ + function, randombytes") + +""" + +#0.36458 sleep (5,) = None +""" + +sleep(5) + +librepyfile = libfile.open("tracer.txt") +tracedcall = librepyfile.readline() +librepyfile.close() + +if("sleep (5,)" not in tracedcall): + the_original_log_function("Trace api call failed to trace correct\ + function, sleep(5)") + +""" + +0.31708 createthread (,) = None +""" + +def doNothing(): + exitall() + +createthread(doNothing) + +if("createthread" not in tracedcall): + the_original_log_function("Trace api call failed to trace \ + correct function, createthread()") + + +""" + +#5.37007 getthreadname = MainThread +""" +getthreadname() + +librepyfile = libfile.open("tracer.txt") +tracedcall = librepyfile.readline() +librepyfile.close() + +if("getthreadname" not in tracedcall): + the_original_log_function("Trace api call failed to trace \ + correct function, getthreadname()") + + +""" + +#5.37049 getresources = ({'loopsend': 10000000.0, 'netrecv': +300000000.0, 'random': 100000.0, 'insockets': 500, 'fileread': +10000000.0, 'netsend': 300000000.0, 'connport': set([63100, 63101, +63102, 63103, 63104, 63105, 63106... +""" + +getresources() + +librepyfile = libfile.open("tracer.txt") +tracedcall = librepyfile.readline() +librepyfile.close() + +if("getresources" not in tracedcall): + the_original_log_function("Trace api call failed to trace \ + correct function, getresources()") + + +""" + +#5.37116 getlasterror = None +""" + +value = getlasterror() + +librepyfile = libfile.open("tracer.txt") +tracedcall = librepyfile.readline() +librepyfile.close() + +if("getlasterror" not in tracedcall): + the_original_log_function("Trace api call failed to trace \ + correct function, getlasterror()") + +""" + +5.31396 sendmessage ('192.168.183.129', 63100, 'Hello World', +'192.168.183.129', 63102) = 11ance at 0x7fea51f57638>>-> No messages +currently available! +""" + +server_port = 63100 +msg_to_send = "Hello World" +client_port = 63102 + +# Create a server that receives and verifies the message +# sent from the client. +def server(): + # If we get any exception, then the test fails, so we + # don't bother catching them. + server_sock = listenformessage(myip, server_port) + # recieved = server_sock.recv(11) + # server_sock.send(recieved) + while True: + try: + msg_received = server_sock.getmessage() + break + except SocketWouldBlockError: + sleep(0.01) + # Receive the message using the session library then + # confirm that the message matches what was sent. + + assert(msg_received == msg_to_send) + + #so socket dosent close on remote end! + exitall() + +# Launch the server and wait a few seconds for it to start. +createthread(server) +sleep(2) + +sendmessage(myip, server_port, msg_to_send, myip, client_port) + +librepyfile = libfile.open("tracer.txt") +tracedcall = librepyfile.readline() +librepyfile.close() + +param = "('" + str(myip) + "', " + str(server_port) + ", '" + str(myip) \ + + "', '" + msg_to_send + "', '" + str(myip) + "', " + str(client_port) + ")" + +if("sendmessage " + param not in tracedcall): + the_original_log_function("Trace api call failed to trace \ + correct function, sendmessage " + param) + + +#has no strace call no point in checking.... +exitall() diff --git a/tests/ut_seattlelib_strace_socketobj.r2py b/tests/ut_seattlelib_strace_socketobj.r2py index c6844e5..2c12795 100644 --- a/tests/ut_seattlelib_strace_socketobj.r2py +++ b/tests/ut_seattlelib_strace_socketobj.r2py @@ -1,73 +1,174 @@ -#this unit test checks the functionality of the strace socket object - +""" + + this unit test checks the functionality of the strace socket object + Function Tested: + openconnection(destip, destport, localip, localport, timeout) + socket.close() + socket.recv(numbytes) + socket.send(message) +""" #pragma repy restrictions.default dylink.r2py encasementlib.r2py log_between_modules.r2py strace.r2py +#librepy is used for 2 files handles +#one file handle is for the ut (reading) +#the other is for the security module (writing) +#data is passed between security module and unit test +libfile = dy_import_module("librepy.r2py") + + + +myip = getmyip() +msg_to_send = "Hello World" +server_port = 63100 +client_port = 63102 +timeout = 10 + +# Create a server that receives and verifies the message +# sent from the client. +def server(): + # If we get any exception, then the test fails, so we + # don't bother catching them. + server_sock = listenforconnection(myip, server_port) + # recieved = server_sock.recv(11) + # server_sock.send(recieved) + while True: + try: + rip, rport, mysock = server_sock.getconnection() + break + except SocketWouldBlockError: + sleep(0.01) + + # Receive the message using the session library then + # confirm that the message matches what was sent. + msg_received = mysock.recv(11) + mysock.send(msg_received) + -# First dylink import gives: -# 0.510231971741 Fatal Error: Function 'the_original_log_function' tried to raise exception of type: '' which is forbidden. -dy_import_module_symbols("librepyfile.r2py") -librepyfile = open("tracer.txt") + assert(msg_received == msg_to_send) + #so socket dosent close on remote end! + sleep(4) + exitall() -localip = "127.0.0.1" -localport1 = 12345 -localport2 = 12347 -targetip = "127.0.0.1" -targetport = 12346 -timeout = 1.0 -conn = openconnection(targetip, targetport, localip, localport1, timeout) +# Launch the server and wait a few seconds for it to start. +createthread(server) +sleep(2) +client_sock = openconnection(myip, server_port, myip, client_port, timeout) + +#open traced file +librepyfile = libfile.open("tracer.txt") tracedcall = librepyfile.readline() +librepyfile.close() +""" + +# 3.32584 openconnection ('192.168.183.129', 63100, '192.168.183.129', 63102, 10) = +>Error'> No connections currently available!! +""" +param = "('" + str(myip) + "', " + str(server_port) + ", '" + str(myip) + \ + "', " + str(client_port) + ", " + str(timeout) + ")" #Check apicall to traced call if("openconnection" not in tracedcall): - the_original_log_function("Trace api call failed to trace correct function, openconnection") -if("('127.0.0.1', 12346, '127.0.0.1', 12345, 1.0)" not in tracedcall): - the_original_log_function("Trace api call failed to trace correct function with correct parameters, openconnection('127.0.0.1', 12346, '127.0.0.1', 12345, 1.0)") -if("emulcomm.EmulatedSocket" not in tracedcall): - the_original_log_function("Failed to trace trace correct function as correct object") + the_original_log_function("Trace api call failed to trace correct function,\ + openconnection") +if(param not in tracedcall): + the_original_log_function("Trace api call failed to trace correct function \ + with correct parameters, openconnection('" + param) +if("emulcomm.EmulatedSocket" not in tracedcall): + the_original_log_function("Failed to trace trace correct function as \ + correct object") -conn.send("Hi from repy program!") +client_sock.send(msg_to_send) +#open traced file +librepyfile = libfile.open("tracer.txt") tracedcall = librepyfile.readline() +librepyfile.close() + +""" + +# 3.32844 socket.send > ('Hello World',) = +11latedSocket instance at 0x7f219fa968c0>>Error'> No connections currently available!! +""" #Check apicall to traced call if("socket.send" not in tracedcall): - the_original_log_function("Trace api call failed to trace correct function, socket.send") -if("('Hi from repy program!',)" not in tracedcall and "21" not in tracedcall): - the_original_log_function("Trace api call failed to trace correct function with correct parameters, send('Hi from repy program!',)") + the_original_log_function("Trace api call failed to trace correct \ + function, socket.send") + +if("('" + msg_to_send + "',)" not in tracedcall and "21" not in tracedcall): + the_original_log_function("Trace api call failed to trace correct \ + function with correct parameters, send('Hi from repy program!',)") + if("emulcomm.EmulatedSocket" not in tracedcall): - the_original_log_function("Failed to trace trace correct function as correct object") + the_original_log_function("Failed to trace trace correct function \ + as correct object") +#so the client can wait for the servers message to be sent back before rasing an error +sleep(3) -conn.recv(21) +msg_received = client_sock.recv(11) +#open traced file +librepyfile = libfile.open("tracer.txt") tracedcall = librepyfile.readline() +librepyfile.close() + + +assert(msg_received == msg_to_send) + +""" + +# 5.22834 socket.recv > (11,) = Hello World11192.168.183.129', 63102, +>) +""" #Check apicall to traced call if("socket.recv" not in tracedcall): - the_original_log_function("Trace api call failed to trace correct function, socket.send") -if("(21,)" not in tracedcall): - the_original_log_function("Trace api call failed to trace correct function with correct parameters, send('Hi from repy program!',)") + the_original_log_function("Trace api call failed to trace correct \ + function, socket.send") + +if("(11,) = " + msg_received not in tracedcall): + the_original_log_function("Trace api call failed to trace correct\ + function with correct parameters, recv('" + msg_received +"',)") + if("emulcomm.EmulatedSocket" not in tracedcall): - the_original_log_function("Failed to trace trace correct function as correct object") + the_original_log_function("Failed to trace trace correct function\ + as correct object") -conn.close() +client_sock.close() +#open traced file +librepyfile = libfile.open("tracer.txt") tracedcall = librepyfile.readline() +librepyfile.close() + +""" + +#3.47004 socket.close > = True World',) += 11latedSocket instance at 0x7f2411327950>>kError'> +No connections currently available! +""" #Check apicall to traced call if("socket.close" not in tracedcall): - the_original_log_function("Trace api call failed to trace correct function, socket.send") + the_original_log_function("Trace api call failed to trace \ + correct function, socket.send") + if("emulcomm.EmulatedSocket" not in tracedcall): - the_original_log_function("Failed to trace trace correct function as correct object") + the_original_log_function("Failed to trace trace correct \ + function as correct object") -librepyfile.close() diff --git a/tests/ut_seattlelib_strace_tcpserverobj.r2py b/tests/ut_seattlelib_strace_tcpserverobj.r2py index 2346b41..ed8d1f5 100644 --- a/tests/ut_seattlelib_strace_tcpserverobj.r2py +++ b/tests/ut_seattlelib_strace_tcpserverobj.r2py @@ -1,51 +1,130 @@ -#this unit test checks the functionality of the tcp server traced object +""" + + this unit test checks the functionality of the tcp server traced object + Function Tested: + listenforconnection(localip, localport) + tcpserversocket.getconnection() + tcpserversocket.close() +""" #pragma repy restrictions.default dylink.r2py encasementlib.r2py log_between_modules.r2py strace.r2py +#librepy is used for 2 files handles +#one file handle is for the ut (reading) +#the other is for the security module (writing) +#data is passed between security module and unit test +libfile = dy_import_module("librepy.r2py") -# First dylink import gives: -# 0.510231971741 Fatal Error: Function 'the_original_log_function' tried to raise exception of type: '' which is forbidden. -dy_import_module_symbols("librepyfile.r2py") -librepyfile = open("tracer.txt") +myip = getmyip() +msg_to_send = "Hello World" +server_port = 63100 +client_port = 63102 +timeout = 10 -listenforconnection(getmyip(), 12347) +tcpserverobj = listenforconnection(myip, server_port) + +#open traced file +librepyfile = libfile.open("tracer.txt") tracedcall = librepyfile.readline() +librepyfile.close() + +param = "('" + str(myip) + "', " + str(server_port) +")" +""" + +#1.33392 listenforconnection ('192.168.183.129', 63100) = +>n was refused! +""" #Check apicall to traced call if("listenforconnection" not in tracedcall): - the_original_log_function("Trace api call failed to trace correct function, openfile") -if("('192.168.183.128', 12345)" not in tracedcall): - the_original_log_function("Trace api call failed to trace correct function with correct parameters, openfile('../hi.txt', True)") + the_original_log_function("Trace api call failed to trace \ + correct function, openfile") + +if(param not in tracedcall): + the_original_log_function("Trace api call failed to trace\ + correct function with correct parameters") + if("emulcomm.TCPServerSocket"not in tracedcall): - the_original_log_function("Failed to trace trace correct function as correct object") - -try: - tcpserverobj.getconnection() -except SocketWouldBlockError: - #expected - pass - + the_original_log_function("Failed to trace trace correct \ + function as correct object") + + +def client(): + client_sock = openconnection(myip, server_port, myip, client_port, timeout) + #so the client can wait for the servers message to be sent back before rasing an error + sleep(5) + client_sock.close() + exitall() + + +# Launch the client +createthread(client) + +#wait for a TCP connection +while True: + try: + rip, rport, mysock = tcpserverobj.getconnection() + break + except SocketWouldBlockError: + sleep(0.01) + + +#open traced file +librepyfile = libfile.open("tracer.txt") tracedcall = librepyfile.readline() +librepyfile.close() + +""" + +#1.33578 TCPServerSocket.getconnection >-> + +No connections currently available! +or + +#1.30136 TCPServerSocket.getconnection > = + ('192.168.183.129', 63102, >) +""" #Check apicall to traced call if("TCPServerSocket.getconnection" not in tracedcall): - the_original_log_function("Trace api call failed to trace correct function, openfile") + the_original_log_function("Trace api call failed to trace \ + correct function, openfile") + if("emulcomm.TCPServerSocket"not in tracedcall): - the_original_log_function("Failed to trace trace correct function as correct object") + the_original_log_function("Failed to trace trace correct \ + function as correct object") tcpserverobj.close() +#open traced file +librepyfile = libfile.open("tracer.txt") tracedcall = librepyfile.readline() +librepyfile.close() + +""" + +#1.33758 TCPServerSocket.lose >4053f8>>-> + +No connections currently available! +""" #Check apicall to traced call if("TCPServerSocket.close" not in tracedcall): - the_original_log_function("Trace api call failed to trace correct function, openfile") + the_original_log_function("Trace api call failed to trace\ + correct function, openfile") + if("emulcomm.TCPServerSocket"not in tracedcall): - the_original_log_function("Failed to trace trace correct function as correct object") + the_original_log_function("Failed to trace trace correct\ + function as correct object") + -librepyfile.close() diff --git a/tests/ut_seattlelib_strace_udpserverobj.r2py b/tests/ut_seattlelib_strace_udpserverobj.r2py index 5391d03..af6f3ad 100644 --- a/tests/ut_seattlelib_strace_udpserverobj.r2py +++ b/tests/ut_seattlelib_strace_udpserverobj.r2py @@ -1,50 +1,125 @@ -#this unit test checks the functionality of the udp server traced object - +""" + + this unit test checks the functionality of the udp server traced object + Function Tested: + listenformessage(localip, localport) + udpserversocket.getmessage() + udpserversocket.close() +""" #pragma repy restrictions.default dylink.r2py encasementlib.r2py log_between_modules.r2py strace.r2py -# First dylink import gives: -# 0.510231971741 Fatal Error: Function 'the_original_log_function' tried to raise exception of type: '' which is forbidden. -dy_import_module_symbols("librepyfile.r2py") -librepyfile = open("tracer.txt") +#librepy is used for 2 files handles +#one file handle is for the ut (reading) +#the other is for the security module (writing) +#data is passed between security module and unit test +libfile = dy_import_module("librepy.r2py") + +myip = getmyip() +msg_to_send = "Hello World" +server_port = 63100 +client_port = 63102 -udpserverobj = wrapped_listenformessage(getmyip(), 12345) +udpserverobj = listenformessage(myip, server_port) +#open traced file +librepyfile = libfile.open("tracer.txt") tracedcall = librepyfile.readline() +librepyfile.close() +param = "('" + str(myip) + "', " + str(server_port) + ")" + +""" + +# 1.32405 listenformessage ('192.168.183.129', 63100) = >n_hierarchy.SocketWouldBlockError'> No connections +currently available! +""" #Check apicall to traced call if("listenformessage" not in tracedcall): - the_original_log_function("Trace api call failed to trace correct function, openfile") -if("('192.168.183.128', 12345)" not in tracedcall): - the_original_log_function("Trace api call failed to trace correct function with correct parameters, openfile('../hi.txt', True)") + the_original_log_function("Trace api call failed to trace \ + correct function, openfile") + +if(param not in tracedcall): + the_original_log_function("Trace api call failed to trace \ + correct function with correct parameters") + if("emulcomm.UDPServerSocket"not in tracedcall): - the_original_log_function("Failed to trace trace correct function as correct object") + the_original_log_function("Failed to trace trace correct\ + function as correct object") + + +def client(): + sendmessage(myip, server_port, msg_to_send, myip, client_port) + sleep(2) + #so socket dosent close on remote end! + exitall() + + +# Launch the server and wait a few seconds for it to start. +createthread(client) + -try: - udpserverobj.getmessage() -except SocketWouldBlockError: - #expected - pass +while True: + try: + msg_received = udpserverobj.getmessage() + break + except SocketWouldBlockError: + sleep(0.01) + +#open traced file +librepyfile = libfile.open("tracer.txt") tracedcall = librepyfile.readline() +librepyfile.close() + +""" + +1.32599 UDPServerSocket.getmessage >-> + No messages currently available!ble! +or + +1.34704 UDPServerSocket.getmessage > = + ('192.168.183.129', 63102, 'Hello World')apped socket: + >) +""" #Check apicall to traced call if("UDPServerSocket.getmessage" not in tracedcall): - the_original_log_function("Trace api call failed to trace correct function, openfile") + the_original_log_function("Trace api call failed to \ + trace correct function, openfile") + if("emulcomm.UDPServerSocket"not in tracedcall): - the_original_log_function("Failed to trace trace correct function as correct object") + the_original_log_function("Failed to trace trace \ + correct function as correct object") udpserverobj.close() - +#open traced file +librepyfile = libfile.open("tracer.txt") tracedcall = librepyfile.readline() +librepyfile.close() + +""" + +# 1.3278 UDPServerSocket.close >f3b0>>-> + + No messages currently available!ble! +""" #Check apicall to traced call if("UDPServerSocket.close" not in tracedcall): - the_original_log_function("Trace api call failed to trace correct function, openfile") + the_original_log_function("Trace api call failed to trace \ + correct function, openfile") + if("emulcomm.UDPServerSocket"not in tracedcall): - the_original_log_function("Failed to trace trace correct function as correct object") + the_original_log_function("Failed to trace trace correct \ + function as correct object") -librepyfile.close() diff --git a/tests/ut_seattlelib_strace_vnobj.r2py b/tests/ut_seattlelib_strace_vnobj.r2py index 16437a0..5c6b257 100644 --- a/tests/ut_seattlelib_strace_vnobj.r2py +++ b/tests/ut_seattlelib_strace_vnobj.r2py @@ -1,27 +1,44 @@ -#this unit test checks the functionality of the strace virtual namespace object - +""" + + this unit test checks the functionality of the strace virtual namespace object + Function Tested: + createvirtualnamespace(code, name) + virtualnamespace.evaluate(context) +""" #pragma repy restrictions.default dylink.r2py encasementlib.r2py log_between_modules.r2py strace.r2py +#librepy is used for 2 files handles +#one file handle is for the ut (reading) +#the other is for the security module (writing) +#data is passed between security module and unit test +libfile = dy_import_module("librepy.r2py") -# First dylink import gives: -# 0.510231971741 Fatal Error: Function 'the_original_log_function' tried to raise exception of type: '' which is forbidden. -dy_import_module_symbols("librepyfile.r2py") -librepyfile = open("tracer.txt") # Small code snippet, safe safe_code = "meaning_of_life = 42\n" # Try to make the safe virtual namespace -safe_virt = wrapped_virtual_namespace(safe_code, "Test VN") +safe_virt = createvirtualnamespace(safe_code, "Test VN") +#open traced file +librepyfile = libfile.open("tracer.txt") tracedcall = librepyfile.readline() +librepyfile.close() +""" + +# 1.22645 VirtualNamespace(...) = <.DylinkNamespace object at +0x7fa20d4d1110>t at 0x7fa20d571a98>> (True,) = True) +""" #Check apicall to traced call if("VirtualNamespace(...)" not in tracedcall): - the_original_log_function("Trace api call failed to trace correct function, VirtualNamespace(...)") -if("virtual_namespace.VirtualNamespace " not in tracedcall): - the_original_log_function("Failed to trace trace correct function as correct object") + the_original_log_function("Trace api call failed to trace\ + correct function, VirtualNamespace(...)") +if(".DylinkNamespace object" not in tracedcall): + the_original_log_function("Failed to trace trace correct \ + function as correct object") + @@ -31,13 +48,24 @@ context = SafeDict() # Evaluate context_2 = safe_virt.evaluate(context) +#open traced file +librepyfile = libfile.open("tracer.txt") tracedcall = librepyfile.readline() +librepyfile.close() + +""" + +# 1.29208 VirtualNamespace.evaluate <.DylinkNamespace +object at 0x7fa20d4d1110>at 0x7fa20d571a98>> (True,) = True) +""" #Check apicall to traced call if("VirtualNamespace.evaluate" not in tracedcall): - the_original_log_function("Trace api call failed to trace correct function, evaluate") -if("virtual_namespace.VirtualNamespace " not in tracedcall): - the_original_log_function("Failed to trace trace correct function as correct object") + the_original_log_function("Trace api call failed to trace\ + correct function, evaluate") +if(".DylinkNamespace object" not in tracedcall): + the_original_log_function("Failed to trace trace correct \ + function as correct object") # Check that the context is the same @@ -46,8 +74,6 @@ if context is not context_2: # Check for the meaning of life if "meaning_of_life" not in context: - the_original_log_function("Meaning of life is undefined! Existential error!",'\n') - + the_original_log_function("Meaning of life is undefined! \ + Existential error!",'\n') - -librepyfile.close() From e1622f2575565ebeb487c99492be99db2cc7662b Mon Sep 17 00:00:00 2001 From: Ajay Shenoy Date: Thu, 15 Sep 2016 09:16:28 -0400 Subject: [PATCH 12/14] posssible fix for OS socket close and recv() error in CI --- tests/ut_seattlelib_strace_socketobj.r2py | 31 ++++++++++++-------- tests/ut_seattlelib_strace_tcpserverobj.r2py | 3 +- tests/ut_seattlelib_strace_udpserverobj.r2py | 2 +- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/tests/ut_seattlelib_strace_socketobj.r2py b/tests/ut_seattlelib_strace_socketobj.r2py index 2c12795..cc1ad69 100644 --- a/tests/ut_seattlelib_strace_socketobj.r2py +++ b/tests/ut_seattlelib_strace_socketobj.r2py @@ -41,7 +41,12 @@ def server(): # Receive the message using the session library then # confirm that the message matches what was sent. - msg_received = mysock.recv(11) + while True: + try: + msg_received = mysock.recv(11) + break + except SocketWouldBlockError: + sleep(0.01) mysock.send(msg_received) @@ -74,15 +79,15 @@ param = "('" + str(myip) + "', " + str(server_port) + ", '" + str(myip) + \ #Check apicall to traced call if("openconnection" not in tracedcall): - the_original_log_function("Trace api call failed to trace correct function,\ + the_original_log_function("Trace api call failed to trace correct function,\ openconnection") if(param not in tracedcall): - the_original_log_function("Trace api call failed to trace correct function \ + the_original_log_function("Trace api call failed to trace correct function \ with correct parameters, openconnection('" + param) if("emulcomm.EmulatedSocket" not in tracedcall): - the_original_log_function("Failed to trace trace correct function as \ + the_original_log_function("Failed to trace trace correct function as \ correct object") @@ -102,15 +107,15 @@ librepyfile.close() #Check apicall to traced call if("socket.send" not in tracedcall): - the_original_log_function("Trace api call failed to trace correct \ + the_original_log_function("Trace api call failed to trace correct \ function, socket.send") if("('" + msg_to_send + "',)" not in tracedcall and "21" not in tracedcall): - the_original_log_function("Trace api call failed to trace correct \ - function with correct parameters, send('Hi from repy program!',)") + the_original_log_function("Trace api call failed to trace correct \ + function with correct parameters, ('" + msg_to_send + "',)") if("emulcomm.EmulatedSocket" not in tracedcall): - the_original_log_function("Failed to trace trace correct function \ + the_original_log_function("Failed to trace trace correct function \ as correct object") #so the client can wait for the servers message to be sent back before rasing an error @@ -135,15 +140,15 @@ instance at 0x7f625d4d3b48>> (11,) = Hello World11192.168.183.129', 63102, #Check apicall to traced call if("socket.recv" not in tracedcall): - the_original_log_function("Trace api call failed to trace correct \ + the_original_log_function("Trace api call failed to trace correct \ function, socket.send") if("(11,) = " + msg_received not in tracedcall): - the_original_log_function("Trace api call failed to trace correct\ + the_original_log_function("Trace api call failed to trace correct\ function with correct parameters, recv('" + msg_received +"',)") if("emulcomm.EmulatedSocket" not in tracedcall): - the_original_log_function("Failed to trace trace correct function\ + the_original_log_function("Failed to trace trace correct function\ as correct object") @@ -165,10 +170,10 @@ No connections currently available! #Check apicall to traced call if("socket.close" not in tracedcall): - the_original_log_function("Trace api call failed to trace \ + the_original_log_function("Trace api call failed to trace \ correct function, socket.send") if("emulcomm.EmulatedSocket" not in tracedcall): - the_original_log_function("Failed to trace trace correct \ + the_original_log_function("Failed to trace trace correct \ function as correct object") diff --git a/tests/ut_seattlelib_strace_tcpserverobj.r2py b/tests/ut_seattlelib_strace_tcpserverobj.r2py index ed8d1f5..6d89129 100644 --- a/tests/ut_seattlelib_strace_tcpserverobj.r2py +++ b/tests/ut_seattlelib_strace_tcpserverobj.r2py @@ -53,9 +53,8 @@ if("emulcomm.TCPServerSocket"not in tracedcall): def client(): - client_sock = openconnection(myip, server_port, myip, client_port, timeout) - #so the client can wait for the servers message to be sent back before rasing an error sleep(5) + client_sock = openconnection(myip, server_port, myip, client_port, timeout) client_sock.close() exitall() diff --git a/tests/ut_seattlelib_strace_udpserverobj.r2py b/tests/ut_seattlelib_strace_udpserverobj.r2py index af6f3ad..138f80c 100644 --- a/tests/ut_seattlelib_strace_udpserverobj.r2py +++ b/tests/ut_seattlelib_strace_udpserverobj.r2py @@ -53,8 +53,8 @@ if("emulcomm.UDPServerSocket"not in tracedcall): def client(): sendmessage(myip, server_port, msg_to_send, myip, client_port) - sleep(2) #so socket dosent close on remote end! + sleep(2) exitall() From 933e1f291c67cfe5250bab145825147971fa8866 Mon Sep 17 00:00:00 2001 From: Ajay Shenoy Date: Tue, 4 Oct 2016 10:03:48 -0400 Subject: [PATCH 13/14] fied socket clean up errors on mac, and added security module test --- tests/ut_seattlelib_log_between_modules_test.r2py | 14 ++++++++++++++ tests/ut_seattlelib_strace_tcpserverobj.r2py | 7 ++++++- 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 tests/ut_seattlelib_log_between_modules_test.r2py diff --git a/tests/ut_seattlelib_log_between_modules_test.r2py b/tests/ut_seattlelib_log_between_modules_test.r2py new file mode 100644 index 0000000..df518d0 --- /dev/null +++ b/tests/ut_seattlelib_log_between_modules_test.r2py @@ -0,0 +1,14 @@ +#pragma repy restrictions.default dylink.r2py encasementlib.r2py log_between_modules.r2py + + +log("This is a test") +libfile = dy_import_module("librepyfile.r2py") + +#open traced file +librepyfile = libfile.open("tracer.txt") +tracedcall = librepyfile.readline() +librepyfile.close() + +if("This is a test" not in tracedcall): + the_original_log_function("Log between modules does not work openconnection") + diff --git a/tests/ut_seattlelib_strace_tcpserverobj.r2py b/tests/ut_seattlelib_strace_tcpserverobj.r2py index 6d89129..5393b4b 100644 --- a/tests/ut_seattlelib_strace_tcpserverobj.r2py +++ b/tests/ut_seattlelib_strace_tcpserverobj.r2py @@ -54,7 +54,12 @@ if("emulcomm.TCPServerSocket"not in tracedcall): def client(): sleep(5) - client_sock = openconnection(myip, server_port, myip, client_port, timeout) + while True: + try: + client_sock = openconnection(myip, server_port, myip, client_port, timeout) + break + except CleanupInProgressError: + sleep(0.01) client_sock.close() exitall() From 8a10d374f1af240a5058b7285697643c357a0058 Mon Sep 17 00:00:00 2001 From: Ajay Shenoy Date: Tue, 18 Oct 2016 10:06:18 -0400 Subject: [PATCH 14/14] Sleep should fix name space threading added sleep function to fix name space threading issue --- tests/ut_seattlelib_strace_fileobj.r2py | 8 ++++++-- tests/ut_seattlelib_strace_lockobj.r2py | 7 +++++-- tests/ut_seattlelib_strace_nonobjapi.r2py | 7 +++++-- tests/ut_seattlelib_strace_socketobj.r2py | 7 +++++-- tests/ut_seattlelib_strace_tcpserverobj.r2py | 14 ++++++-------- tests/ut_seattlelib_strace_udpserverobj.r2py | 10 +++++++--- tests/ut_seattlelib_strace_vnobj.r2py | 8 +++++--- 7 files changed, 39 insertions(+), 22 deletions(-) diff --git a/tests/ut_seattlelib_strace_fileobj.r2py b/tests/ut_seattlelib_strace_fileobj.r2py index 7d1cdbc..533d85a 100644 --- a/tests/ut_seattlelib_strace_fileobj.r2py +++ b/tests/ut_seattlelib_strace_fileobj.r2py @@ -10,11 +10,15 @@ #pragma repy restrictions.default dylink.r2py encasementlib.r2py log_between_modules.r2py strace.r2py -#librepy is used for 2 files handles + +#sleep is because there is threading issue between namespace initilization and start of UT +sleep(5) + +#librepyfile is used for 2 files handles #one file handle is for the ut (reading) #the other is for the security module (writing) #data is passed between security module and unit test -libfile = dy_import_module("librepy.r2py") +libfile = dy_import_module("librepyfile.r2py") #open file using trace call fielobj = openfile("testfile.txt", True) diff --git a/tests/ut_seattlelib_strace_lockobj.r2py b/tests/ut_seattlelib_strace_lockobj.r2py index cac14de..a22ef2f 100644 --- a/tests/ut_seattlelib_strace_lockobj.r2py +++ b/tests/ut_seattlelib_strace_lockobj.r2py @@ -9,11 +9,14 @@ #pragma repy restrictions.default dylink.r2py encasementlib.r2py log_between_modules.r2py strace.r2py -#librepy is used for 2 files handles +#sleep is because there is threading issue between namespace initilization and start of UT +sleep(5) + +#librepyfile is used for 2 files handles #one file handle is for the ut (reading) #the other is for the security module (writing) #data is passed between security module and unit test -libfile = dy_import_module("librepy.r2py") +libfile = dy_import_module("librepyfile.r2py") #create a lock diff --git a/tests/ut_seattlelib_strace_nonobjapi.r2py b/tests/ut_seattlelib_strace_nonobjapi.r2py index dfdf4a9..3191c2d 100644 --- a/tests/ut_seattlelib_strace_nonobjapi.r2py +++ b/tests/ut_seattlelib_strace_nonobjapi.r2py @@ -18,11 +18,14 @@ """ #pragma repy restrictions.default dylink.r2py encasementlib.r2py log_between_modules.r2py strace.r2py -#librepy is used for 2 files handles +#sleep is because there is threading issue between namespace initilization and start of UT +sleep(5) + +#librepyfile is used for 2 files handles #one file handle is for the ut (reading) #the other is for the security module (writing) #data is passed between security module and unit test -libfile = dy_import_module("librepy.r2py") +libfile = dy_import_module("librepyfile.r2py") """ diff --git a/tests/ut_seattlelib_strace_socketobj.r2py b/tests/ut_seattlelib_strace_socketobj.r2py index cc1ad69..d3a5883 100644 --- a/tests/ut_seattlelib_strace_socketobj.r2py +++ b/tests/ut_seattlelib_strace_socketobj.r2py @@ -10,11 +10,14 @@ #pragma repy restrictions.default dylink.r2py encasementlib.r2py log_between_modules.r2py strace.r2py -#librepy is used for 2 files handles +#sleep is because there is threading issue between namespace initilization and start of UT +sleep(5) + +#librepyfile is used for 2 files handles #one file handle is for the ut (reading) #the other is for the security module (writing) #data is passed between security module and unit test -libfile = dy_import_module("librepy.r2py") +libfile = dy_import_module("librepyfile.r2py") diff --git a/tests/ut_seattlelib_strace_tcpserverobj.r2py b/tests/ut_seattlelib_strace_tcpserverobj.r2py index 5393b4b..3b23b69 100644 --- a/tests/ut_seattlelib_strace_tcpserverobj.r2py +++ b/tests/ut_seattlelib_strace_tcpserverobj.r2py @@ -9,11 +9,14 @@ #pragma repy restrictions.default dylink.r2py encasementlib.r2py log_between_modules.r2py strace.r2py -#librepy is used for 2 files handles +#sleep is because there is threading issue between namespace initilization and start of UT +sleep(5) + +#librepyfile is used for 2 files handles #one file handle is for the ut (reading) #the other is for the security module (writing) #data is passed between security module and unit test -libfile = dy_import_module("librepy.r2py") +libfile = dy_import_module("librepyfile.r2py") myip = getmyip() msg_to_send = "Hello World" @@ -54,12 +57,7 @@ if("emulcomm.TCPServerSocket"not in tracedcall): def client(): sleep(5) - while True: - try: - client_sock = openconnection(myip, server_port, myip, client_port, timeout) - break - except CleanupInProgressError: - sleep(0.01) + client_sock = openconnection(myip, server_port, myip, client_port, timeout) client_sock.close() exitall() diff --git a/tests/ut_seattlelib_strace_udpserverobj.r2py b/tests/ut_seattlelib_strace_udpserverobj.r2py index 138f80c..c02b68e 100644 --- a/tests/ut_seattlelib_strace_udpserverobj.r2py +++ b/tests/ut_seattlelib_strace_udpserverobj.r2py @@ -9,11 +9,15 @@ #pragma repy restrictions.default dylink.r2py encasementlib.r2py log_between_modules.r2py strace.r2py -#librepy is used for 2 files handles +#sleep is because there is threading issue between namespace initilization and start of UT +sleep(5) + +#librepyfile is used for 2 files handles #one file handle is for the ut (reading) #the other is for the security module (writing) #data is passed between security module and unit test -libfile = dy_import_module("librepy.r2py") +libfile = dy_import_module("librepyfile.r2py") + myip = getmyip() msg_to_send = "Hello World" @@ -53,8 +57,8 @@ if("emulcomm.UDPServerSocket"not in tracedcall): def client(): sendmessage(myip, server_port, msg_to_send, myip, client_port) - #so socket dosent close on remote end! sleep(2) + #so socket dosent close on remote end! exitall() diff --git a/tests/ut_seattlelib_strace_vnobj.r2py b/tests/ut_seattlelib_strace_vnobj.r2py index 5c6b257..10a077a 100644 --- a/tests/ut_seattlelib_strace_vnobj.r2py +++ b/tests/ut_seattlelib_strace_vnobj.r2py @@ -7,12 +7,14 @@ """ #pragma repy restrictions.default dylink.r2py encasementlib.r2py log_between_modules.r2py strace.r2py -#librepy is used for 2 files handles +#sleep is because there is threading issue between namespace initilization and start of UT +sleep(5) + +#librepyfile is used for 2 files handles #one file handle is for the ut (reading) #the other is for the security module (writing) #data is passed between security module and unit test -libfile = dy_import_module("librepy.r2py") - +libfile = dy_import_module("librepyfile.r2py") # Small code snippet, safe safe_code = "meaning_of_life = 42\n"