File: run.sh

package info (click to toggle)
readpe 0.85.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,636 kB
  • sloc: ansic: 21,151; xml: 558; makefile: 448; sh: 422
file content (323 lines) | stat: -rwxr-xr-x 9,727 bytes parent folder | download | duplicates (2)
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#!/bin/bash

ROOT_DIR=.
INC_DIR=include
SRC_DIR=src
TOOLS_DIR=$SRC_DIR/build
TESTS_DIR=tests
REPORTS_DIR=$TESTS_DIR/running_report
EXPECTED_OUTPUTS_DIR=$TESTS_DIR/expected_outputs
SUPPORTED_FORMATS="csv html json text xml"
BINDIFF=$(which diff)

now=$(date +"%F_%H-%M")
arch=$(uname -m)
so=$(uname -s) # We use `-s` because `-o` is not supported on Mac OS X
so=${so#*/}
version=$(sed -n 's/^.*VERSION \"\([0-9]\.[0-9]*\)\"/\1/p' $INC_DIR/common.h)
err=0

function test_build
{
	. $TESTS_DIR/test_build.sh
}

function test_binary
{
	local onsuccess=$1; shift;
	local onfailure=$1; shift;
	local logname=$1; shift;
	local binname=$1; shift;
	local args=$*

	if [ ! -d $REPORTS_DIR/${binname} ]
	then
		mkdir -p $REPORTS_DIR/${binname}
	fi

	echo -n "Testing ${binname} ${args}... "
	if $TOOLS_DIR/${binname} ${args} > "$REPORTS_DIR/${binname}/${now}_${binname}_${logname}.txt"
	then
		eval ${onsuccess}
	else
		eval ${onfailure}
		err=1
		return # Stop at error
	fi
}

function test_binary_using_all_formats
{
	local onsuccess=$1; shift;
	local onfailure=$1; shift;
	local logname=$1; shift;
	local binname=$1; shift;
	local args=$*

	# First run using the default output format.
	test_binary "${onsuccess}" "${onfailure}" "${logname}" ${binname} ${args}

	# Then run using every supported output format.
	for format in $SUPPORTED_FORMATS
	do
		echo -n "Testing ${binname} -f ${format} ${args}... "
		if $TOOLS_DIR/${binname} -f ${format} ${args} > "$REPORTS_DIR/${binname}/${now}_${binname}_${logname}_${format}.txt"
		then
			eval ${onsuccess}
		else
			eval ${onfailure}
			err=1
			break # Stop at 1st error
		fi
	done
}

function test_binary_output_against_expected_output
{
	local onsuccess=$1; shift;
	local onfailure=$1; shift;
	local logname=$1; shift;
	local binname=$1; shift;
	local binsample=$1; shift;
	local args=$*;
	local reported_output="$EXPECTED_OUTPUTS_DIR/_tmp/${binsample}/${binname}_${logname}.txt";
	local expected_output_ok="$EXPECTED_OUTPUTS_DIR/${binsample}/${binname}_${logname}.txt";
	local expected_output_fail="$EXPECTED_OUTPUTS_DIR/${binsample}/${binname}_${logname}_fail.txt";

	if [ ! -d $EXPECTED_OUTPUTS_DIR/_tmp/${binsample} ]
	then
		mkdir -p $EXPECTED_OUTPUTS_DIR/_tmp/${binsample}
	fi

	echo -n "Running $TOOLS_DIR/${binname} -f json ${args} ${binsample} &> \"${reported_output}\"... "
	$TOOLS_DIR/${binname} -f json ${args} ${binsample} &> "${reported_output}"
	local ret="$?"
	echo "ret=$ret"

	if [ "$ret" -eq "0" ]
	then
		expected_output=${expected_output_ok}
	else
		expected_output=${expected_output_fail}
	fi

	echo -n "Comparing \"${expected_output}\" against \"${reported_output}\"... "
	${BINDIFF} -u "${expected_output}" "${reported_output}" &> /dev/null
	ret="$?"

	if [ "$ret" -eq "0" ]
	then
		eval ${onsuccess}
	else
		eval ${onfailure}
		#echo "Showing differences:"
		#head -n 5 tmp.diff
		err=1
		return # Stop at error
	fi
}

function run_pepack
{
	local binname=pepack
	local args=$*
	echo "---------- ${binname} ----------"
	test_binary_using_all_formats "echo OK" "echo NOK" "default"            ${binname} ${args}
}

function run_pehash
{
	local binname=pehash
	local args=$*
	echo "---------- ${binname} ----------"
	test_binary_using_all_formats "echo OK" "echo NOK" "default"            ${binname} ${args}
	test_binary_using_all_formats "echo OK" "echo NOK" "h_dos"              ${binname} -h dos ${args}
	test_binary_using_all_formats "echo OK" "echo NOK" "a_sha512"           ${binname} -a sha512 ${args}
	test_binary_using_all_formats "echo OK" "echo NOK" "s_text"             ${binname} -s '.text' ${args}
	test_binary_using_all_formats "echo OK" "echo NOK" "section_index_1"    ${binname} --section-index 1 ${args}
}

function run_pescan
{
	local binname=pescan
	local args=$*
	echo "---------- ${binname} ----------"
	test_binary_using_all_formats "echo OK" "echo NOK" "default"    ${binname} ${args}
	test_binary_using_all_formats "echo OK" "echo NOK" "v"          ${binname} -v ${args}
}

function run_pestr
{
	local binname=pestr
	local args=$*
	echo "---------- ${binname} ----------"
	test_binary "echo OK" "echo NOK" "default"  ${binname} ${args}
	test_binary "echo OK" "echo NOK" "n_5"      ${binname} -n 5 ${args}
	test_binary "echo OK" "echo NOK" "o"        ${binname} -o ${args}
	test_binary "echo OK" "echo NOK" "s"        ${binname} -s ${args}
}

function peres_on_success
{
	if [ -d resources ]
	then
		echo "OK"
		rm -rf resources
	else
		echo "binary returns OK, but no resource was extracted"
	fi
}

function run_peres
{
	local binname=peres
	local args=$*
	echo "---------- ${binname} ----------"
	test_binary "echo OK"        "echo NOK" "i" ${binname} -i ${args}
	test_binary "echo OK"        "echo NOK" "s" ${binname} -s ${args}
	test_binary peres_on_success "echo NOK" "x" ${binname} -x ${args}
	test_binary peres_on_success "echo NOK" "a" ${binname} -a ${args}
}

function pesec_on_success
{
	if [ -f tmp_cert -a -s tmp_cert ]
	then
		echo "OK"
	else
		echo "Command returns OK but don't export the cert to file."
	fi
	rm tmp_cert
}

function run_pesec
{
	local binname=pesec
	local args=$*
	echo "---------- ${binname} ----------"
	test_binary_using_all_formats "echo OK"         "echo NOK" "default"    ${binname} ${args}
	test_binary_using_all_formats "echo OK"         "echo NOK" "c_pem"      ${binname} -c pem ${args}
	test_binary_using_all_formats pesec_on_success  "echo NOK" "o_tmp_cert" ${binname} -o tmp_cert ${args}
}

function run_readpe
{
	local binname=readpe
	local args=$*
	echo "---------- ${binname} ----------"
	test_binary_using_all_formats "echo OK" "echo NOK" "default"    ${binname} ${args}
	test_binary_using_all_formats "echo OK" "echo NOK" "H"          ${binname} -H ${args}
	test_binary_using_all_formats "echo OK" "echo NOK" "S"          ${binname} -S ${args}
	test_binary_using_all_formats "echo OK" "echo NOK" "d"          ${binname} -d ${args}
	test_binary_using_all_formats "echo OK" "echo NOK" "h_dos"      ${binname} -h dos ${args}
	test_binary_using_all_formats "echo OK" "echo NOK" "i"          ${binname} -i ${args}
	test_binary_using_all_formats "echo OK" "echo NOK" "e"          ${binname} -e ${args}
}

function test_regression
{
	if [ ! -d $EXPECTED_OUTPUTS_DIR ]
	then
		mkdir -p $EXPECTED_OUTPUTS_DIR
	fi

	local binsample="$1"

	test_binary_output_against_expected_output "echo OK" "echo NOK" "default"           pepack ${binsample}

	test_binary_output_against_expected_output "echo OK" "echo NOK" "default"           pehash ${binsample}
	test_binary_output_against_expected_output "echo OK" "echo NOK" "h_dos"             pehash ${binsample} -h dos
	test_binary_output_against_expected_output "echo OK" "echo NOK" "a_sha512"          pehash ${binsample} -a sha512
	test_binary_output_against_expected_output "echo OK" "echo NOK" "s_text"            pehash ${binsample} -s '.text'
	test_binary_output_against_expected_output "echo OK" "echo NOK" "section_index_1"   pehash ${binsample} --section-index 1

	test_binary_output_against_expected_output "echo OK" "echo NOK" "default"           pescan ${binsample}
	test_binary_output_against_expected_output "echo OK" "echo NOK" "v"                 pescan ${binsample} -v

	test_binary_output_against_expected_output "echo OK" "echo NOK" "i"                 peres ${binsample} -i
	test_binary_output_against_expected_output "echo OK" "echo NOK" "s"                 peres ${binsample} -s
	test_binary_output_against_expected_output "echo OK" "echo NOK" "x"                 peres ${binsample} -x
	test_binary_output_against_expected_output "echo OK" "echo NOK" "a"                 peres ${binsample} -a

	test_binary_output_against_expected_output "echo OK" "echo NOK" "default"           pesec ${binsample}
	test_binary_output_against_expected_output "echo OK" "echo NOK" "c_pem"             pesec ${binsample} -c pem
	test_binary_output_against_expected_output "echo OK" "echo NOK" "o_tmp_cert"        pesec ${binsample} -o tmp_cert

	test_binary_output_against_expected_output "echo OK" "echo NOK" "default"           readpe ${binsample}
	test_binary_output_against_expected_output "echo OK" "echo NOK" "H"                 readpe ${binsample} -H
	test_binary_output_against_expected_output "echo OK" "echo NOK" "S"                 readpe ${binsample} -S
	test_binary_output_against_expected_output "echo OK" "echo NOK" "d"                 readpe ${binsample} -d
	test_binary_output_against_expected_output "echo OK" "echo NOK" "h_dos"             readpe ${binsample} -h dos
	test_binary_output_against_expected_output "echo OK" "echo NOK" "i"                 readpe ${binsample} -i
	test_binary_output_against_expected_output "echo OK" "echo NOK" "e"                 readpe ${binsample} -e
}

function test_pe32
{
	if [ ! -d $REPORTS_DIR ]
	then
		mkdir -p $REPORTS_DIR
	fi

	run_pepack $1
	run_pehash $1
	run_pescan $1
	run_peres $1
	run_pestr $1
	run_pesec $1
	run_readpe $1
}   

function test_pe64
{
	echo 'coming soon...'
}

function clean
{
	if [ -d $REPORTS_DIR ]
	then
		rm -rf $REPORTS_DIR
	fi

	rm -rf $TESTS_DIR/*.log
}

case "$1" in
	"clean")
		clean ;;
	"build")
		test_build ;;
	"pe32")
		if [ $# -ne 2 ]
		then
			echo "missing argument: use $0 pe32 <binary file>"
			exit 1
		else
			test_pe32 $2
			exit $err
		fi
		;;
	"pe64")
		test_pe64
		exit $err ;;
	"regression")
		if [ $# -ne 2 ]
		then
			echo "missing argument: use $0 regression <binary file>"
			exit 1
		else
			test_regression $2
			exit $err
		fi
		;;
	*)
		echo "illegal option -- $1"
		echo "usage: run.sh <option>"
		echo "       run.sh clean"
		echo "       run.sh build"
		echo "       run.sh pe32 <binary_file_for_testing>"
		echo "       run.sh pe64 <binary_file_to_testing>"
		echo "       run.sh regression <binary_file_for_testing>"
		exit 1 ;;
esac