diff --git a/log_between_modules.r2py b/log_between_modules.r2py new file mode 100644 index 0000000..59afca4 --- /dev/null +++ b/log_between_modules.r2py @@ -0,0 +1,45 @@ +TYPE="type" +ARGS="args" +RETURN="return" +EXCP="exceptions" +TARGET="target" +FUNC="func" +OBJC="objc" + + +OLD_LOG = CHILD_CONTEXT_DEF["log"] + + +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:Exception,RETURN:None,TARGET:log_between_modules} + +#restoring log, giving it a new name +CHILD_CONTEXT_DEF["the_original_log_function"] = OLD_LOG + + +# Dispatch +secure_dispatch_module() \ No newline at end of file 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/strace.py b/strace.r2py similarity index 94% rename from strace.py rename to strace.r2py index 37ff473..5a35bc0 100644 --- a/strace.py +++ b/strace.r2py @@ -1,8 +1,12 @@ """ 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. + + Header: + Call-time function [instance] [args] [ = result ] """ # These API functions do _not_ return an object, and can be handled uniformly. @@ -35,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 @@ -54,7 +61,7 @@ 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 + log(call_string) PRINT_LOCK.release() # Get the result @@ -64,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) - print call_string,"->",str(type(e))+" "+str(e) + log(call_string + "->" + str(type(e)) + " " + str(e)) PRINT_LOCK.release() raise @@ -80,9 +87,8 @@ def traced_call(self,name,func,args,kwargs,no_return=False,print_args=True,print call_string += " = " + str_result PRINT_LOCK.acquire(True) - print call_string + log(call_string) PRINT_LOCK.release() - return result @@ -147,6 +153,7 @@ def writeat(self,*args,**kwargs): 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): @@ -210,7 +217,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 @@ -227,7 +233,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 @@ -239,33 +244,25 @@ 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 -print "Call-time function [instance] [args] [ = result ]" -# Dispatch the next module -dy_dispatch_module() +# # Wrap all the functions +wrap_all() +# Dispatch the next module +dy_dispatch_module() \ No newline at end of file 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_fileobj.r2py b/tests/ut_seattlelib_strace_fileobj.r2py new file mode 100644 index 0000000..533d85a --- /dev/null +++ b/tests/ut_seattlelib_strace_fileobj.r2py @@ -0,0 +1,180 @@ +""" + + 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 + + +#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("librepyfile.r2py") + +#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 ('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") + + +#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") + +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() + +#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") + +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 = 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("(\'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") + +#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") + +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") + +#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") + +if("emulfile.emulated_file object"not in tracedcall): + the_original_log_function("Failed to trace trace \ + correct function as correct object") + + +#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 new file mode 100644 index 0000000..a22ef2f --- /dev/null +++ b/tests/ut_seattlelib_strace_lockobj.r2py @@ -0,0 +1,93 @@ +""" + + 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 + +#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("librepyfile.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") + +if("emulmisc.emulated_lock object"not in tracedcall): + 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") + +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() + +#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") + diff --git a/tests/ut_seattlelib_strace_nonobjapi.r2py b/tests/ut_seattlelib_strace_nonobjapi.r2py new file mode 100644 index 0000000..3191c2d --- /dev/null +++ b/tests/ut_seattlelib_strace_nonobjapi.r2py @@ -0,0 +1,260 @@ +""" + + 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 + +#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("librepyfile.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 new file mode 100644 index 0000000..d3a5883 --- /dev/null +++ b/tests/ut_seattlelib_strace_socketobj.r2py @@ -0,0 +1,182 @@ +""" + + 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 + +#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("librepyfile.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. + while True: + try: + msg_received = mysock.recv(11) + break + except SocketWouldBlockError: + sleep(0.01) + mysock.send(msg_received) + + + assert(msg_received == msg_to_send) + + #so socket dosent close on remote end! + sleep(4) + exitall() + + +# 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(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") + + +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("('" + 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, ('" + msg_to_send + "',)") + +if("emulcomm.EmulatedSocket" not in tracedcall): + 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) + +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("(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") + + + +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") + +if("emulcomm.EmulatedSocket" not in tracedcall): + 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 new file mode 100644 index 0000000..3b23b69 --- /dev/null +++ b/tests/ut_seattlelib_strace_tcpserverobj.r2py @@ -0,0 +1,132 @@ +""" + + 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 + +#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("librepyfile.r2py") + +myip = getmyip() +msg_to_send = "Hello World" +server_port = 63100 +client_port = 63102 +timeout = 10 + + +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(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") + + +def client(): + sleep(5) + client_sock = openconnection(myip, server_port, myip, client_port, timeout) + 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") + +if("emulcomm.TCPServerSocket"not in tracedcall): + 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") + +if("emulcomm.TCPServerSocket"not in tracedcall): + the_original_log_function("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..c02b68e --- /dev/null +++ b/tests/ut_seattlelib_strace_udpserverobj.r2py @@ -0,0 +1,129 @@ +""" + + 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 + +#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("librepyfile.r2py") + + +myip = getmyip() +msg_to_send = "Hello World" +server_port = 63100 +client_port = 63102 + +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(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") + + +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) + + +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") + +if("emulcomm.UDPServerSocket"not in tracedcall): + 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") + +if("emulcomm.UDPServerSocket"not in tracedcall): + the_original_log_function("Failed to trace trace correct \ + function as correct object") + diff --git a/tests/ut_seattlelib_strace_vnobj.r2py b/tests/ut_seattlelib_strace_vnobj.r2py new file mode 100644 index 0000000..10a077a --- /dev/null +++ b/tests/ut_seattlelib_strace_vnobj.r2py @@ -0,0 +1,81 @@ +""" + + 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 + +#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("librepyfile.r2py") + +# Small code snippet, safe +safe_code = "meaning_of_life = 42\n" + +# Try to make the safe virtual namespace +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(".DylinkNamespace object" 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) + +#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(".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 +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') +