-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRUSHBSampleTest.py
114 lines (94 loc) · 3.59 KB
/
RUSHBSampleTest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import sys
import subprocess
import time
import os
import random
STARTUP_TIMEOUT = 10
RUN_TIMEOUT = 15
VALID_MODES = ["SIMPLE", "NAK", "MULTI_NAK", "TIMEOUT", "MULTI_TIMEOUT",
"INVALID_SEQ", "INVALID_ACK", "INVALID_FLAGS",
"CHECKSUM", "INVALID_CHECKSUM_VAL", "INVALID_CHECKSUM_FLAG"]
RUSHB_TESTSUITE_VERSION = '1.0'
'''
1.0 - initial release
'''
def main(argv):
print('RUSHB_TEST_VERSION: ' + RUSHB_TESTSUITE_VERSION)
if len(argv) < 1:
print("Usage: python3 RUSHBSampleTest.py mode")
return
if os.path.isfile("makefile") or os.path.isfile("Makefile"):
try:
subprocess.check_output(["make"])
except subprocess.CalledProcessError:
print("Error occured when calling make.")
return
client_port = random.randint(11111,65534)
print("Selected client port is {}.".format(client_port))
# mode = "INVALID_CHECKSUM_FLAG"
# mode = "CHECKSUM"
# mode = "INVALID_CHECKSUM_VAL"
# mode = "SIMPLE"
# mode = "NAK"
# mode = "MULTI_NAK"
# mode = "TIMEOUT"
# mode = "MULTI_TIMEOUT"
# mode = "INVALID_SEQ"
# mode = "INVALID_ACK"
# mode = "INVALID_FLAGS"
if len(argv) > 1 and argv[1] in VALID_MODES:
mode = argv[1]
else:
print("Invalid mode, please check again. Now testing [SIMPLE] mode.")
# if os.path.isfile("RUSHBSvr2.py"):
# call = ["python3", "RUSHBSvr2.py"]
# elif os.path.isfile("RUSHBSvr2"):
# call = ["./RUSHBSvr2"]
# else:
# print("No valid file found to call.")
# return
if os.path.isfile("RUSHBSvr3.py"):
call = ["python3", "RUSHBSvr3.py"]
elif os.path.isfile("RUSHBSvr3"):
call = ["./RUSHBSvr3"]
else:
print("No valid file found to call.")
return
serv_proc = subprocess.Popen(call, stdout=subprocess.PIPE)
try:
out = serv_proc.stdout.readline().decode("UTF-8")
serv_port = int(out.partition("\n")[0].strip())
print("Found server on port " + str(serv_port) + ".\nRunning the client...")
except ValueError:
print("Server port is invalid.")
serv_proc.kill()
return
while True:
try:
cli_proc = subprocess.Popen(["python3", "RUSHBSampleClient.py", str(client_port), str(serv_port), "-m", mode, "-v", "9", "-o", mode+"_output.txt"])
break
except:
client_port = random.randint(11111, 65534)
print("Socket error found, change the client port to {}.".format(client_port))
try:
cli_proc.wait(timeout=RUN_TIMEOUT)
except subprocess.TimeoutExpired:
if mode.upper() == "INVALID_CHECKSUM_VAL":
with open(mode + "_output.txt", "w") as f:
f.close()
else:
print("\nTimeout exceeded during connection. Please use Wireshark to see what is missing.")
cli_proc.kill()
return
if serv_proc.poll() is None:
serv_proc.kill()
with open(mode+"_output.txt", "r") as f, open(os.path.join("test_files", mode + "_output.txt"), "r") as g:
output = f.read()
expected = g.read()
if output == expected:
print("\n[{}] is tested successfully. Output is as expected.".format(mode))
else:
print("\nDifferences in output detected.")
print("Compare differences using diff {}_output.txt {}_output.txt".format(mode, os.path.join("test_files", mode)))
if __name__ == "__main__":
main(sys.argv)