-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathunit_test.py
executable file
·230 lines (181 loc) · 8.76 KB
/
unit_test.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
import os
import signal
import subprocess
import argparse
import textwrap
# absolute path to your nm_otool project
PROJECT_PATH = 'YOUR PATH'
NM_PATH = os.path.join(PROJECT_PATH, 'ft_nm')
OTOOL_PATH = os.path.join(PROJECT_PATH, 'ft_otool')
def execute(cmd):
proc = subprocess.Popen(cmd, shell=True, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = proc.communicate()
output = output.decode('unicode-escape').rstrip()
error = error.decode('unicode-escape').rstrip()
rc = proc.returncode
return (output, rc, error)
def title(str):
print('-' * (len(str) + 4))
print('| ' + str + ' |')
print('-' * (len(str) + 4))
print()
def tests_nm(tests_array, args, opt):
errors = 0
if len(tests_array) == 0 :
return errors
# get the longest test name, useful for padding
max_len = max(tests_array, key=len)
max_len = len(max_len)
for t in tests_array:
# execute nm and ft_nm
if opt:
nm_out , nm_rc , _ = execute("{} -{} {}".format("nm", opt, t))
out , rc , err = execute("{} -{} {}".format(NM_PATH, opt, t))
else:
nm_out , nm_rc , _ = execute("{} {}".format("nm", t))
out , rc , err = execute("{} {}".format(NM_PATH, t))
# compare their return value and output
if rc == -signal.SIGSEGV:
result = "\033[91mSEGMENTATION FAULT\033[0m"
errors += 1
elif nm_rc != 0 and rc != 0:
result = "\033[92mOK\033[0m: \033[93mWARNING\033[0m nm returned {} and ft_nm returned {} but the error messages were not compared for more flexibility".format(nm_rc, rc)
elif rc != nm_rc:
result = "\033[91mERROR\033[0m: nm returned {}, ft_nm returned {}.".format(nm_rc, rc)
errors += 1
elif nm_out != out:
result = "\033[91mERROR\033[0m: output differs"
errors += 1
else:
result = "\033[92mOK\033[0m"
if args.errors and "OK" in result:
continue
print("\t+ {:{length}} {result}".format(t, length=max_len, result=result))
return errors
def tests_otool(tests_array, args, opt):
errors = 0
if len(tests_array) == 0 :
return errors
# get the longest test name, useful for padding
max_len = max(tests_array, key=len)
max_len = len(max_len)
for t in tests_array:
if opt:
otool_out , otool_rc , _ = execute("{} -{} {}".format("otool", opt, t))
out , rc , err = execute("{} -{} {}".format(OTOOL_PATH, opt, t))
else:
otool_out , otool_rc , _ = execute("{} {}".format("otool -t ", t))
out , rc , err = execute("{} {}".format(OTOOL_PATH, t))
# compare their return value and output
if rc == -signal.SIGSEGV:
result = "\033[91mSEGMENTATION FAULT\033[0m"
errors += 1
elif otool_rc != 0 and rc != 0:
result = "\033[92mOK\033[0m: \033[93mWARNING\033[0m otool returned {} and ft_otool returned {} but the error messages were not compared for more flexibility".format(otool_rc, rc)
elif rc != otool_rc:
result = "\033[91mERROR\033[0m: otool returned {}, ft_otool returned {}.".format(otool_rc, rc)
errors += 1
elif otool_out != out:
result = "\033[91mERROR\033[0m: output differs"
errors += 1
else:
result = "\033[92mOK\033[0m"
if args.errors and "OK" in result:
continue
print("\t+ {:{length}} {result}".format(t, length=max_len, result=result))
return errors
def tests_main(args):
errors = 0
files_to_test = []
# fill the files_to_test array according to the given parameters
for arg in args.files:
absolute_path = os.path.abspath(arg)
if os.path.isdir(absolute_path):
path = absolute_path
for root, dirs, files in os.walk(path):
path = root.split(os.sep)
for file in files:
f = os.path.join(root, file)
out, rc, _ = execute("file {}".format(f))
if args.noignore or not rc and "Mach-O" in out:
files_to_test.append(f)
if not args.recursive:
break
else:
files_to_test.append(absolute_path)
print("[+] total amount of files to process: {}".format(len(files_to_test)))
# # launch tests
if args.nm:
title("NM unit_tests")
if args.options:
for opt in args.options:
print("[+] Test nm -{} option".format(opt))
errors += tests_nm(files_to_test, args, opt)
else:
errors += tests_nm(files_to_test, args, opt=None)
if args.otool:
title("OTOOL unit_tests")
if args.options:
for opt in args.options:
print("[+] Test otool -{} option".format(opt))
errors += tests_otool(files_to_test, args, opt)
else:
errors += tests_otool(files_to_test, args, opt=None)
if errors:
print("\n[!] total amount of errors: \033[91m{}\033[0m".format(errors))
else:
print("\n[T]/ no error occured, \033[92mgood job!\033[0m")
return errors
if __name__ == '__main__':
# argument parsing
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, description=textwrap.dedent('''
\033[92m███╗ ██╗███╗ ███╗ ██████╗ ████████╗ ██████╗ ██████╗ ██╗
████╗ ██║████╗ ████║ ██╔═══██╗╚══██╔══╝██╔═══██╗██╔═══██╗██║
██╔██╗ ██║██╔████╔██║ ██║ ██║ ██║ ██║ ██║██║ ██║██║
██║╚██╗██║██║╚██╔╝██║ ██║ ██║ ██║ ██║ ██║██║ ██║██║
██║ ╚████║██║ ╚═╝ ██║███████╗╚██████╔╝ ██║ ╚██████╔╝╚██████╔╝███████╗
╚═╝ ╚═══╝╚═╝ ╚═╝╚══════╝ ╚═════╝ ╚═╝ ╚═════╝ ╚═════╝ ╚══════╝\033[0m
Unit testing script made for the 42 school project nm_otool.
This script compares your ft_nm/ft_otool output and return code to the system
ones.
In order to run this script, you must edit the following configuration
variable located at the beginning of this file:
PROJECT_PATH -> absolute path to your nm_otool project
made by mguillau
'''))
parser.add_argument('files', nargs='+', help="files and/or folders to process")
parser.add_argument('--recursive', '-R', dest='recursive', action='store_true', help="recursively test binaries encountered for any folder passed as a parameter")
parser.add_argument('--no-ignore', '-N', dest='noignore', action='store_true', help="test every file encountered, even non binary ones")
parser.add_argument('--errors', '-e', dest='errors', action='store_true', help="only output errors")
parser.add_argument('--otool', '-o', dest='nm', action='store_false', help="only test otool")
parser.add_argument('--nm', '-n', dest='otool', action='store_false', help="only test nm")
parser.add_argument('--options', '-p', dest='options', help="set options to test separate by ';' ex: --options='a;n' (test -a and -n)")
parser.set_defaults(recursive=False)
parser.set_defaults(noignore=False)
parser.set_defaults(errors=False)
parser.set_defaults(otool=True)
parser.set_defaults(nm=True)
if len(sys.argv[1:]) == 0:
parser.print_usage() # for just the usage line
parser.exit()
args = parser.parse_args()
if args.options:
args.options = args.options.split(';')
# check configuration
if not os.path.exists(NM_PATH) or not os.path.exists(OTOOL_PATH):
if os.path.exists(PROJECT_PATH):
print("[!] NM_PATH and OTOOL_PATH not found but PROJECT_PATH exists")
print("[?] attempting to compile ft_nm and ft_otool")
out, rc, err = execute("make -C {}".format(PROJECT_PATH))
if rc != 0:
print(err)
sys.exit(1)
else:
print(out)
else:
print("[!] PROJECT_PATH \033[91mnot found\033[0m. It needs to be the absolute_path to your nm_otool project")
sys.exit(1)
sys.exit(tests_main(args))