1#!/bin/bash
2
3echoerr() { echo "$@" 1>&2; }
4
5BASEDIR=$(dirname $0)
6pushd $BASEDIR > /dev/null
7TMP="tmp"
8PYTHON=$1
9
10if [ -z "${PYTHON}" ]
11then
12    PYTHON="python"
13fi
14
15if test ! -d tmp
16then
17    mkdir tmp
18fi
19
20if [ -z "${CARCH}" ]
21then
22    CARCH=$(uname -m)
23fi
24
25PYTHONVERSION="$(${PYTHON} -c 'import sys; print("%s.%s" % (sys.version_info[0], sys.version_info[1]))')"
26echo "Python version is $PYTHONVERSION"
27PYTHONPATH="../py_ext/build/lib.linux-${CARCH}-${PYTHONVERSION}:${PYTHONPATH}"
28
29echo ""
30echo "Running ${PYTHON} tests for ${CARCH}-${PYTHONVERSION}"
31
32echo
33echo "testing generating hash and distance functions..."
34echo
35echo                    "${PYTHON} ../py_ext/test.py ../Testing/example_data/2005NISSE.txt ../Testing/example_data/1english-only.txt > $TMP/python_test.out"
36PYTHONPATH=${PYTHONPATH} ${PYTHON} ../py_ext/test.py ../Testing/example_data/2005NISSE.txt ../Testing/example_data/1english-only.txt > $TMP/python_test.out
37echo "diff $TMP/python_test.out exp/python_test_EXP"
38      diff $TMP/python_test.out exp/python_test_EXP > /dev/null 2>/dev/null
39if [ $? -ne 0 ]; then
40	echoerr "error: diff $TMP/python_test.out exp/python_test_EXP"
41        popd > /dev/null
42	exit -1
43fi
44echo "pass"
45echo
46echo "testing the -old and -conservative options for short strings..."
47echo
48for file in small small2 ; do
49	echo                    "${PYTHON} ../py_ext/tlsh_digest.py example_data/$file.txt > $TMP/py_$file.tlsh"
50				 ${PYTHON} ../py_ext/tlsh_digest.py example_data/$file.txt > $TMP/py_$file.tlsh
51	echo "diff $TMP/py_$file.tlsh exp/$file.128.1.tlsh_EXP"
52	      diff $TMP/py_$file.tlsh exp/$file.128.1.tlsh_EXP
53	if [ $? -ne 0 ]; then
54		echoerr "error: diff $TMP/py_$file.tlsh exp/$file.128.1.tlsh_EXP"
55		popd > /dev/null
56		exit -1
57	fi
58
59	echo                    "${PYTHON} ../py_ext/tlsh_digest.py -old example_data/$file.txt > $TMP/py_$file.old.tlsh"
60				 ${PYTHON} ../py_ext/tlsh_digest.py -old example_data/$file.txt > $TMP/py_$file.old.tlsh
61	echo "diff $TMP/py_$file.old.tlsh exp/$file.128.1.old.tlsh_EXP"
62	      diff $TMP/py_$file.old.tlsh exp/$file.128.1.old.tlsh_EXP
63	if [ $? -ne 0 ]; then
64		echoerr "error: diff $TMP/py_$file.old.tlsh exp/$file.128.1.old.tlsh_EXP"
65		popd > /dev/null
66		exit -1
67	fi
68
69	echo                    "${PYTHON} ../py_ext/tlsh_digest.py -conservative example_data/$file.txt > $TMP/py_$file.cons.tlsh"
70				 ${PYTHON} ../py_ext/tlsh_digest.py -conservative example_data/$file.txt > $TMP/py_$file.cons.tlsh
71	echo "diff $TMP/py_$file.cons.tlsh exp/$file.128.1.cons.tlsh_EXP"
72	      diff $TMP/py_$file.cons.tlsh exp/$file.128.1.cons.tlsh_EXP
73	if [ $? -ne 0 ]; then
74		echoerr "error: diff $TMP/py_$file.cons.tlsh exp/$file.128.1.cons.tlsh_EXP"
75		popd > /dev/null
76		exit -1
77	fi
78done
79
80echo
81echo "testing lvalue, q1ratio, q2ratio, checksum, bucket_value function (added in 4.7.0) ..."
82echo
83echo                    "${PYTHON} ../py_ext/tlsh_parts.py -d T11454F120A8989D5CF8CAC182F93A3E8D475C317365C5B4911C3C4A9CA5438F5E8FB6EB > $TMP/python_parts_test.out"
84PYTHONPATH=${PYTHONPATH} ${PYTHON} ../py_ext/tlsh_parts.py -d T11454F120A8989D5CF8CAC182F93A3E8D475C317365C5B4911C3C4A9CA5438F5E8FB6EB > $TMP/python_parts_test.out
85echo "diff $TMP/python_parts_test.out exp/python_parts_test_EXP"
86      diff $TMP/python_parts_test.out exp/python_parts_test_EXP > /dev/null 2>/dev/null
87if [ $? -ne 0 ]; then
88	echoerr "error: diff $TMP/python_parts_test.out exp/python_parts_test_EXP"
89        popd > /dev/null
90	exit -1
91fi
92echo "pass"
93
94echo
95echo "passed"
96echo
97### echo "Note that if py_ext/tlshmodule.cpp has changed, then 'python setup.py build; sudo python setup.py install' must be run"
98
99popd > /dev/null
100