summaryrefslogtreecommitdiff
path: root/test/runtests.sh
blob: 3b14189112c70de5a772b5305d3c376dca983f41 (plain)
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
#!/bin/bash
#

# Test files have the extention 'tst'. They contain sections giving config
# data, function names, SQL code and expected output.

#   ##### FNC #####
#   Function name.
#   ##### CNF #####
#   Configuration data.
#   ##### SQL #####
#   SQL code to run ahead of the test.
#   ##### PCY #####
#   Policy to use for the test.
#   ##### EXP #####
#   Expected Output.
#######################################

cd `dirname $0`


function die()
{
	echo
	echo "$@"
	echo
	exit 1
}

function sig_exit()
{
	echo
	exit 1;
}

errors=0

#trap sig_exit SIGINT
#trap sig_exit SIGQUIT

COLM=../colm/colm

[ -d $DATA ] || die "error: data directory not found"

# Parse args.
while getopts vdm opt; do
	case $opt in
		v)
			verbose=true;
		;;
		d)
			diff=true;
		;;
		m)
			VALGRIND="valgrind --leak-check=full --show-reachable=yes --suppressions=../dpi.supp"
		;;
	esac
done
shift $(($OPTIND - 1))

# The files to process. If none given then glob all functions and pcap test confs.
if [ $# != 0 ]; then
	TEST_PAT="$*"
else
	TEST_PAT='*.lm'
fi 

function runtests()
{
	for TST in $TEST_PAT; do
		INP=${TST/.lm}.in
		EXP=${TST/.lm}.exp
		ARGS=${TST/.lm}.args

		BIN=${TST/.lm}.bin
		OUT=${TST/.lm}.out
		DIFF=${TST/.lm}.diff
		LOG=${TST/.lm}.log

		cmdargs=""
		if [ -f $ARGS ]; then
			cmdargs=`cat $ARGS`
		fi

		echo -n "running test $TST ... "
		if [ "$verbose" = true ]; then
			echo
			echo command here
			echo -n ...
		fi

		# Check for expected output.
		if [ '!' -f $EXP ]; then
			echo "FAILED no expected output"
			errors=$(( errors + 1 ))
			continue
		
		fi

		# Compilation.
		$COLM $TST &> $LOG 
		if [ $? != 0 ]; then
			echo "FAILED compilation"
			errors=$(( errors + 1 ))
			continue
		fi


		# Execution
		if [ -f $INP ]; then
			./$BIN $cmdargs < $INP > $OUT 2>> $LOG
		else
			./$BIN $cmdargs > $OUT
		fi
		if [ $? != 0 ]; then
			echo "FAILED execution"
			errors=$(( errors + 1 ))
			continue
		fi

		# Diff of output
		diff -u $EXP $OUT > $DIFF
		if [ $? != 0 ]; then
			echo "FAILED output diff"
			errors=$(( errors + 1 ))
			continue
		fi

		echo ok
	done

	if [ $errors != 0 ]; then
		[ $errors != 1 ] && plural="s";
		echo
		echo "TESTING FAILED: $errors failure$plural"
		echo
		EXIT=1
	fi
}

function diffs()
{
	for TST in $TEST_PAT; do
		EXP=${TST/.*}.exp
		OUT=${TST/.*}.out

		[ -f $EXP ] || die "error: the expected output file $EXP was not found"
		diff -u $EXP $OUT
	done
}

[ -d $workingdir ] || mkdir $workingdir

runtests;

if [ "$diff" = true ]; then
	diffs;
fi

exit $EXIT;