1#!/bin/sh
2
3##################################
4# set CREATE_EXP_FILE=1 if you want this script to create the Expected Results for the regression tests
5##################################
6CREATE_EXP_FILE=0
7
8echoerr() { echo "$@" 1>&2; }
9
10BASEDIR=$(dirname $0)
11cd $BASEDIR
12
13LISTOPTION=1
14JSONOPTION=1
15if test "$1" = "_go"
16then
17	TLSH_PROG="./tlsh_go"
18	SIMP_PROG="./tlsh_go"
19	LISTOPTION=0
20	JSONOPTION=0
21	echo "Scenario: $1	(go version)..."
22elif test ! -z "$1"
23then
24	TLSH_PROG="../bin/tlsh$1"
25	SIMP_PROG="../bin/simple_unittest$1"
26	echo "Scenario: $1	(c++ version)..."
27else
28	TLSH_PROG=../bin/tlsh
29	SIMP_PROG="../bin/simple_unittest"
30	echo "Scenario: tlsh	(c++ standard version)..."
31fi
32echo "TLSH_PROG=$TLSH_PROG"
33
34if test ! -f $TLSH_PROG
35then
36	echoerr "error: (127), you must compile $TLSH_PROG"
37	exit 127
38fi
39
40if test ! -f $SIMP_PROG
41then
42	echoerr "error: (127), you must compile $SIMP_PROG"
43	exit 127
44fi
45
46TMP="tmp"
47if test "$1" = "_go"
48then
49	HASH=128
50	CHKSUM=1
51	SLDWIN=5
52else
53	HASH=`  $TLSH_PROG -longversion | head -1           | cut -f1`
54	CHKSUM=`$TLSH_PROG -longversion | head -2 | tail -1 | cut -f1`
55	SLDWIN=`$TLSH_PROG -longversion |           tail -1 | cut -f1`
56fi
57echo "HASH is $HASH"
58echo "CHKSUM is $CHKSUM"
59echo "SLDWIN is $SLDWIN"
60
61if test -d tmp
62then
63	rm -rf tmp
64fi
65mkdir tmp
66
67############################
68# THE runit FUNCTION
69############################
70#
71# this function will be run twice, "" and "-xlen"
72#
73runit() {
74
75	if test "$1" = "-xlen"
76	then
77	    XLEN="xlen"
78	    echo
79	    echo "Scenario: not considering len, ..."
80	else
81	    XLEN="len"
82	    echo "Scenario:     considering len, ..."
83	fi
84	if test "$1" = "-tlsh_c"
85	then
86	    TLSH_PROG="tlsh_c"
87	    echo "Scenario: tlsh_c (c standalone version)..."
88	fi
89
90	########################################################
91	# Test 1
92	#	get the TLSH values for a directory of files
93	########################################################
94
95	echo
96	echo "test 1"
97	echo
98
99	echo "${TLSH_PROG} -r example_data -o $TMP/example_data.out 2> $TMP/example_data.err"
100	      ${TLSH_PROG} -r example_data -o $TMP/example_data.out 2> $TMP/example_data.err
101
102	if test ! -f $TMP/example_data.out
103	then
104		echoerr "error: (1), $TMP/example_data.out does not exist"
105		exit 1
106	fi
107	EXPECTED_OUT=exp/example_data.$HASH.$CHKSUM.$XLEN.out_EXP
108	EXPECTED_ERR=exp/example_data.$HASH.$CHKSUM.$XLEN.err_EXP
109	if test ! -f $EXPECTED_OUT
110	then
111		if test $CREATE_EXP_FILE = 0
112		then
113			echoerr "error: (1), Expected Result file $EXPECTED_OUT does not exist"
114			exit 1
115		else
116			echo "cp $TMP/example_data.out $EXPECTED_OUT"
117			      cp $TMP/example_data.out $EXPECTED_OUT
118		fi
119	fi
120	if test ! -f $EXPECTED_ERR
121	then
122		if test $CREATE_EXP_FILE = 0
123		then
124			echoerr "error: (1), Expected Result file $EXPECTED_ERR does not exist"
125			exit 1
126		else
127			echo "cp $TMP/example_data.err $EXPECTED_ERR"
128			      cp $TMP/example_data.err $EXPECTED_ERR
129		fi
130	fi
131	diffc=`diff --ignore-all-space $TMP/example_data.out $EXPECTED_OUT | wc -l`
132	if test ! $diffc = 0
133	then
134		echoerr "error: (1), diff $TMP/example_data.out $EXPECTED_OUT"
135		exit 1
136	fi
137
138	diffc=`diff --ignore-all-space $TMP/example_data.err $EXPECTED_ERR | wc -l`
139	if test ! $diffc = 0
140	then
141		echoerr "error: (1), diff $TMP/example_data.err $EXPECTED_ERR"
142		exit 1
143	fi
144
145	echo "passed"
146
147	if test $JSONOPTION = 1
148	then
149		echo "${TLSH_PROG} -r example_data -o $TMP/example_data.json_out -ojson 2> $TMP/example_data.err"
150		      ${TLSH_PROG} -r example_data -o $TMP/example_data.json_out -ojson 2> $TMP/example_data.err
151		EXPECTED_OUT=exp/example_data.$HASH.$CHKSUM.$SLDWIN.$XLEN.json_out_EXP
152		diffc=`diff --ignore-all-space $TMP/example_data.json_out $EXPECTED_OUT | wc -l`
153		if test ! $diffc = 0
154		then
155			echoerr "error: (1), diff $TMP/example_data.json_out $EXPECTED_OUT"
156			exit 1
157		fi
158		echo "passed"
159	else
160		echo
161		echo "Test1(B): we need to implement json functionality in Go version"
162	fi
163
164	########################################################
165	# Test 2
166	#	calculate scores of a file (website_course_descriptors06-07.txt) compared to the directory of files
167	########################################################
168
169	echo
170	echo "test 2"
171	echo
172
173	if test $XLEN = "xlen"
174	then
175	echo "${TLSH_PROG} -xlen -r example_data -c example_data/website_course_descriptors06-07.txt -o $TMP/example_data.scores 2> $TMP/example_data.err2"
176	      ${TLSH_PROG} -xlen -r example_data -c example_data/website_course_descriptors06-07.txt -o $TMP/example_data.scores 2> $TMP/example_data.err2
177	else
178	echo "${TLSH_PROG} -r example_data -c example_data/website_course_descriptors06-07.txt -o $TMP/example_data.scores 2> $TMP/example_data.err2"
179	      ${TLSH_PROG} -r example_data -c example_data/website_course_descriptors06-07.txt -o $TMP/example_data.scores 2> $TMP/example_data.err2
180	fi
181
182	EXPECTED_SCO=exp/example_data.$HASH.$CHKSUM.$XLEN.scores_EXP
183	EXPECTED_ERR=exp/example_data.$HASH.$CHKSUM.$XLEN.err2_EXP
184	if test ! -f $EXPECTED_SCO
185	then
186		if test $CREATE_EXP_FILE = 0
187		then
188			echoerr "error: (1), Expected Result file $EXPECTED_SCO does not exist"
189			exit 1
190		else
191			echo "cp $TMP/example_data.scores $EXPECTED_SCO"
192			      cp $TMP/example_data.scores $EXPECTED_SCO
193		fi
194	fi
195	if test ! -f $EXPECTED_ERR
196	then
197		if test $CREATE_EXP_FILE = 0
198		then
199			echoerr "error: (1), Expected Result file $EXPECTED_ERR does not exist"
200			exit 1
201		else
202			echo "cp $TMP/example_data.err2 $EXPECTED_ERR"
203			      cp $TMP/example_data.err2 $EXPECTED_ERR
204		fi
205	fi
206
207	diffc=`diff --ignore-all-space $TMP/example_data.scores $EXPECTED_SCO | wc -l`
208	if test ! $diffc = 0
209	then
210		echoerr "error: (2), diff $TMP/example_data.scores $EXPECTED_SCO"
211		exit 2
212	fi
213	diffc=`diff --ignore-all-space $TMP/example_data.err2 $EXPECTED_ERR | wc -l`
214	if test ! $diffc = 0
215	then
216		echoerr "error: (2), diff $TMP/example_data.err2 $EXPECTED_ERR"
217		exit 2
218	fi
219
220	echo "passed"
221
222	########################################################
223	# Test 3
224	#	calculate scores of a file (website_course_descriptors06-07.txt) compared to hashes listed in a file
225	#	far more efficient
226	########################################################
227
228	if test $LISTOPTION = 1
229	then
230		echo
231		echo "test 3"
232		echo
233
234		# note that test 3 will output the following error, so write stderr to /dev/null, so it will not be seen.
235		#   warning: cannot read TLSH code example_data/BookingBrochure.txt
236		if test $XLEN = "xlen"
237		then
238		echo "${TLSH_PROG} -xlen -l $TMP/example_data.out -c example_data/website_course_descriptors06-07.txt -o $TMP/example_data.scores.2"
239		      ${TLSH_PROG} -xlen -l $TMP/example_data.out -c example_data/website_course_descriptors06-07.txt -o $TMP/example_data.scores.2 2>/dev/null
240		else
241		echo "${TLSH_PROG} -l $TMP/example_data.out -c example_data/website_course_descriptors06-07.txt -o $TMP/example_data.scores.2"
242		      ${TLSH_PROG} -l $TMP/example_data.out -c example_data/website_course_descriptors06-07.txt -o $TMP/example_data.scores.2 2>/dev/null
243		fi
244
245		EXPECTED_SCO=exp/example_data.$HASH.$CHKSUM.$XLEN.scores.2_EXP
246		if test ! -f $EXPECTED_SCO
247		then
248			if test $CREATE_EXP_FILE = 0
249			then
250				echoerr "error: (3), Expected Result file $EXPECTED_SCO does not exist"
251				exit 1
252			else
253				echo "cp $TMP/example_data.scores.2 $EXPECTED_SCO"
254				      cp $TMP/example_data.scores.2 $EXPECTED_SCO
255			fi
256		fi
257
258		diffc=`diff --ignore-all-space $TMP/example_data.scores.2 $EXPECTED_SCO | wc -l`
259		if test ! $diffc = 0
260		then
261			echoerr "error: (3) diff $TMP/example_data.scores.2 $EXPECTED_SCO"
262			exit 3
263		fi
264
265		echo "passed"
266	else
267		echo "Test3: we need to implement list functionality (-l/-d) in Go version"
268	fi
269
270	########################################################
271	# Test 4
272	#	Test out the -xref parameter which computes the distance scores for each file in a directory (-r parameter) with
273	#   all other files in that directory.
274	########################################################
275	testnum=4
276	echo
277	echo "test $testnum"
278	echo
279	if [ $XLEN = "xlen" ]; then
280	echo "${TLSH_PROG} -xref -xlen -r example_data -o $TMP/example_data.xref.scores"
281	      ${TLSH_PROG} -xref -xlen -r example_data -o $TMP/example_data.xref.scores 2>/dev/null
282	else
283	echo "${TLSH_PROG} -xref -r example_data -o $TMP/example_data.xref.scores"
284	      ${TLSH_PROG} -xref -r example_data -o $TMP/example_data.xref.scores 2>/dev/null
285	fi
286
287	EXPECTED_SCO=exp/example_data.$HASH.$CHKSUM.$XLEN.xref.scores_EXP
288	if test ! -f $EXPECTED_SCO
289	then
290		if test $CREATE_EXP_FILE = 0
291		then
292			echoerr "error: ($testnum), Expected Result file $EXPECTED_SCO does not exist"
293			exit 1
294		else
295			echo "cp $TMP/example_data.xref.scores $EXPECTED_SCO"
296			      cp $TMP/example_data.xref.scores $EXPECTED_SCO
297		fi
298	fi
299
300	diff --ignore-all-space $TMP/example_data.xref.scores $EXPECTED_SCO > /dev/null 2>/dev/null
301	if [ $? -ne 0 ]; then
302		echoerr "error: ($testnum), diff $TMP/example_data.xref.scores $EXPECTED_SCO"
303		exit $testnum
304	fi
305
306	echo "passed"
307
308	if test $JSONOPTION = 1
309	then
310		if [ $XLEN = "xlen" ]; then
311		echo "${TLSH_PROG} -xref -xlen -r example_data -o $TMP/example_data.xref.json_scores -ojson"
312		      ${TLSH_PROG} -xref -xlen -r example_data -o $TMP/example_data.xref.json_scores -ojson 2>/dev/null
313		else
314		echo "${TLSH_PROG} -xref -r example_data -o $TMP/example_data.xref.json_scores -ojson"
315		      ${TLSH_PROG} -xref -r example_data -o $TMP/example_data.xref.json_scores -ojson 2>/dev/null
316		fi
317		EXPECTED_SCO=exp/example_data.$HASH.$CHKSUM.$SLDWIN.$XLEN.xref.json_scores_EXP
318		diff --ignore-all-space $TMP/example_data.xref.json_scores $EXPECTED_SCO > /dev/null 2>/dev/null
319		if [ $? -ne 0 ]; then
320			echoerr "error: ($testnum), diff $TMP/example_data.xref.json_scores $EXPECTED_SCO"
321			exit $testnum
322		fi
323		echo "passed"
324	else
325		echo
326		echo "Test4(B): we need to implement json functionality in Go version"
327	fi
328
329	########################################################
330	# Test 5
331	#	Test out the -T (threshold parameter)
332	########################################################
333	if test $LISTOPTION = 1
334	then
335		testnum=5
336		echo
337		echo "test $testnum"
338		echo
339		# note that test 5 will output the following error, so write stderr to /dev/null, so it will not be seen.
340		#   warning: cannot read TLSH code example_data/BookingBrochure.txt
341
342		if [ $XLEN = "xlen" ]; then
343		echo "${TLSH_PROG} -T 201 -xlen -l $TMP/example_data.out -c example_data/website_course_descriptors06-07.txt -o $TMP/example_data.scores.2.T-201"
344		      ${TLSH_PROG} -T 201 -xlen -l $TMP/example_data.out -c example_data/website_course_descriptors06-07.txt -o $TMP/example_data.scores.2.T-201 2>/dev/null
345		else
346		echo "${TLSH_PROG} -T 201 -l $TMP/example_data.out -c example_data/website_course_descriptors06-07.txt -o $TMP/example_data.scores.2.T-201"
347		      ${TLSH_PROG} -T 201 -l $TMP/example_data.out -c example_data/website_course_descriptors06-07.txt -o $TMP/example_data.scores.2.T-201 2>/dev/null
348		fi
349
350		EXPECTED_SCO=exp/example_data.$HASH.$CHKSUM.$XLEN.scores.2.T-201_EXP
351		if test ! -f $EXPECTED_SCO
352		then
353			if test $CREATE_EXP_FILE = 0
354			then
355				echoerr "error: ($testnum), Expected Result file $EXPECTED_SCO does not exist"
356				exit 1
357			else
358				echo "cp $TMP/example_data.scores.2.T-201 $EXPECTED_SCO"
359				      cp $TMP/example_data.scores.2.T-201 $EXPECTED_SCO
360			fi
361		fi
362
363		diff --ignore-all-space $TMP/example_data.scores.2.T-201 $EXPECTED_SCO > /dev/null 2>/dev/null
364		if [ $? -ne 0 ]; then
365			echoerr "error: ($testnum) diff $TMP/example_data.scores.2.T-201 $EXPECTED_SCO"
366			exit $testnum
367		fi
368		echo "passed"
369	else
370		echo "Test5: we need to implement list functionality (-l/-d) in Go version"
371	fi
372
373}
374############################
375# END OF THE runit FUNCTION
376############################
377
378runit
379if test "$1" = "_go"
380then
381	echo "passed all example data tests (for go implementation)"
382	exit 0
383fi
384runit "-xlen"
385if test -f tlsh_c
386then
387	runit "-tlsh_c"
388fi
389
390############################
391# Test 6
392#	Test out the TLSH digest with a wide range of lengths (testlen.sh)
393############################
394testnum=6
395echo
396echo "test $testnum"
397echo
398
399# I use the papameter value of 22 for the Fibanacci sequence for generating content
400# this generates files up to 6.7 Meg (good enough for automated testing)
401
402echo "./testlen.sh $TLSH_PROG 22 > $TMP/testlen.out"
403      ./testlen.sh $TLSH_PROG 22 > $TMP/testlen.out
404
405EXPECTED_TESTLEN=exp/testlen.$HASH.$CHKSUM.out_EXP
406if test ! -f $EXPECTED_TESTLEN
407then
408	if test $CREATE_EXP_FILE = 0
409	then
410		echoerr "error: ($testnum), Expected Result file $EXPECTED_TESTLEN does not exist"
411		exit 1
412	else
413		echo "cp $TMP/testlen.out $EXPECTED_TESTLEN"
414		      cp $TMP/testlen.out $EXPECTED_TESTLEN
415	fi
416fi
417
418diff --ignore-all-space $TMP/testlen.out $EXPECTED_TESTLEN > /dev/null 2>/dev/null
419if [ $? -ne 0 ]; then
420	echoerr "error: ($testnum) diff $TMP/testlen.out $EXPECTED_TESTLEN"
421	exit $testnum
422fi
423echo "passed"
424
425############################
426# END OF test 6
427############################
428
429############################
430# Test 7
431#	Test the -force option
432############################
433testnum=7
434echo
435echo "test $testnum"
436echo
437
438for file in small small2 ; do
439	echo "${TLSH_PROG} -force -f example_data/$file.txt -o $TMP/$file.tlsh"
440	      ${TLSH_PROG} -force -f example_data/$file.txt -o $TMP/$file.tlsh
441
442	EXPECTED_TLSH=exp/$file.$HASH.$CHKSUM.tlsh_EXP
443	if test ! -f $EXPECTED_TLSH
444	then
445		if test $CREATE_EXP_FILE = 0
446		then
447			echoerr "error: ($testnum), Expected Result file $EXPECTED_TLSH does not exist"
448			exit 1
449		else
450			echo "cp $TMP/$file.tlsh $EXPECTED_TLSH"
451			      cp $TMP/$file.tlsh $EXPECTED_TLSH
452		fi
453	fi
454
455	diff --ignore-all-space $TMP/$file.tlsh $EXPECTED_TLSH
456	if [ $? -ne 0 ]; then
457		echoerr "error: ($testnum) $TMP/$file.tlsh $EXPECTED_TLSH"
458		exit $testnum
459	fi
460done
461echo "passed"
462
463############################
464# END OF test 7
465############################
466
467############################
468# Test 8
469#	Test the -l2 and -lcsv options
470############################
471testnum=8
472echo
473echo "test $testnum"
474echo
475
476if test $SLDWIN = 5
477then
478	#
479	# Test 8(a): -l2
480	#
481	echo "${TLSH_PROG} -T 201 -l2 -l example_data_col_swap.tlsh -c example_data/website_course_descriptors06-07.txt -o $TMP/example_data.scores.l2.T-201"
482	      ${TLSH_PROG} -T 201 -l2 -l example_data_col_swap.tlsh -c example_data/website_course_descriptors06-07.txt -o $TMP/example_data.scores.l2.T-201 2>/dev/null
483
484	# same expected output as Test 5
485
486	EXPECTED_SCO=exp/example_data.$HASH.$CHKSUM.len.scores.2.T-201_EXP
487	if test ! -f $EXPECTED_SCO
488	then
489		echoerr "error: ($testnum), Expected Result file $EXPECTED_SCO does not exist"
490		exit 1
491	fi
492
493	diff --ignore-all-space $TMP/example_data.scores.l2.T-201 $EXPECTED_SCO > /dev/null 2>/dev/null
494	if [ $? -ne 0 ]; then
495		echoerr "error: ($testnum) diff $TMP/example_data.scores.l2.T-201 $EXPECTED_SCO"
496		exit $testnum
497	fi
498
499	#
500	# Test 8(a): -l2 -lcsv
501	#
502	echo "${TLSH_PROG} -T 201 -l2 -lcsv -l example_data_col_swap.csv -c example_data/website_course_descriptors06-07.txt -o $TMP/example_data.scores.l2csv.T-201"
503	      ${TLSH_PROG} -T 201 -l2 -lcsv -l example_data_col_swap.csv -c example_data/website_course_descriptors06-07.txt -o $TMP/example_data.scores.l2csv.T-201 2>/dev/null
504
505	# same expected output as Test 8(a) above
506
507	diff --ignore-all-space $TMP/example_data.scores.l2csv.T-201 $EXPECTED_SCO > /dev/null 2>/dev/null
508	if [ $? -ne 0 ]; then
509		echoerr "error: ($testnum) diff $TMP/example_data.scores.l2csv.T-201 $EXPECTED_SCO"
510		exit $testnum
511	fi
512
513	echo "passed"
514else
515	echo "test 8 not relevant to SLDWIN=$SLDWIN"
516fi
517############################
518# END OF test 8
519############################
520
521############################
522# Test 9
523#	Test the -split option
524############################
525testnum=9
526echo
527echo "test $testnum"
528echo
529
530if test $SLDWIN = 5
531then
532	echo "${TLSH_PROG} -split 50,100,200 -f example_data/Week3.txt -o $TMP/example_data.Week3.split.tlsh"
533	      ${TLSH_PROG} -split 50,100,200 -f example_data/Week3.txt -o $TMP/example_data.Week3.split.tlsh   2>/dev/null
534
535	EXPECTED_RES=exp/example_data.Week3.split.tlsh
536	if test ! -f $EXPECTED_RES
537	then
538		echoerr "error: ($testnum), Expected Result file $EXPECTED_RES does not exist"
539		exit 1
540	fi
541
542	diff --ignore-all-space $TMP/example_data.Week3.split.tlsh $EXPECTED_RES > /dev/null 2>/dev/null
543	if [ $? -ne 0 ]; then
544		echoerr "error: ($testnum) diff $TMP/example_data.Week3.split.tlsh $EXPECTED_RES"
545		exit $testnum
546	fi
547
548	echo "passed"
549else
550	echo "test 9 not relevant to SLDWIN=$SLDWIN"
551fi
552
553############################
554# END OF test 9
555############################
556
557############################
558# test 10
559############################
560testnum=10
561
562echo
563echo "Running $SIMP_PROG"
564$SIMP_PROG > $TMP/simple_unittest.out
565
566EXPECTED_STEST=exp/simple_unittest.$HASH.$CHKSUM.EXP
567if test ! -f $EXPECTED_STEST
568then
569	if test $CREATE_EXP_FILE = 0
570	then
571		echoerr "error: ($testnum), Expected Result file $EXPECTED_STEST does not exist"
572		exit 1
573	else
574		echo "cp $TMP/simple_unittest.out $EXPECTED_STEST"
575		      cp $TMP/simple_unittest.out $EXPECTED_STEST
576	fi
577fi
578
579diff --ignore-all-space $TMP/simple_unittest.out $EXPECTED_STEST > /dev/null 2>/dev/null
580if [ $? -ne 0 ]; then
581	echoerr "error: ($testnum) diff $TMP/simple_unittest.out $EXPECTED_STEST"
582	exit 255
583fi
584
585echo "passed all example data tests"
586
587echo
588echo "If you have made changes to the Tlsh python module, build and install it, and run python_test.sh"
589echo
590