1#!/bin/sh
2#
3# $Id: func.sh 2083 2011-10-27 04:41:39Z jkoshy $
4
5tpstart() # write test purpose banner and initialise variables
6{
7    tet_infoline "$*"
8    FAIL=N
9}
10
11tpresult() # give test purpose result
12{
13    # $1 is result code to give if FAIL=N (default PASS)
14    if [ $FAIL = N ]; then
15	tet_result ${1-PASS}
16    else
17	tet_result FAIL
18    fi
19}
20
21check_exit() # execute command (saving output) and check exit code
22{
23    # $1 is command, $2 is expected exit code (0 or "N" for non-zero)
24    eval "$1" > out.stdout 2> out.stderr
25    CODE=$?
26    if [ $2 = 0 -a $CODE -ne 0 ]; then
27	tet_infoline "Command ($1) gave exit code $CODE, expected 0"
28	FAIL=Y
29    elif [ $2 != 0 -a $CODE -eq 0 ]; then
30	tet_infoline "Command ($1) gave exit code $CODE, expected non-zero"
31	FAIL=Y
32    fi
33}
34
35check_nostdout() # check that nothing went to stdout
36{
37    if [ -s out.stdout ]; then
38	tet_infoline "Unexpected output written to stdout, as shown below:"
39	infofile out.stdout stdout:
40	FAIL=Y
41    fi
42}
43
44check_nostderr() # check that nothing went to stderr
45{
46    if [ -s out.stderr ]; then
47	tet_infoline "Unexpected output written to stderr, as shown below:"
48	infofile out.stderr stderr:
49	FAIL=Y
50    fi
51}
52
53check_stderr() # check that stderr matches expected error
54{
55    # $1 is file containing expected error
56    # if no argument supplied, just check out.stderr is not empty
57
58    case $1 in
59    "")
60	if [ ! -s out.stderr ];	then
61	    tet_infoline "Expected output to stderr, but none written"
62	    FAIL=Y
63	fi
64	;;
65    *)
66	diff -uN out.stderr ${1}.err > diff.out 2> /dev/null
67	if [ $? -ne 0 ]; then
68	    tet_infoline "Incorrect output written to stderr, as shown below"
69	    infofile "diff.out" "diff:"
70	    FAIL=Y
71	fi
72	;;
73    esac
74}
75
76check_stdout() # check that stdout matches expected output
77{
78    # $1 is file containing expected output
79    # if no argument supplied, just check out.stdout is not empty
80
81    case $1 in
82    "")
83	if [ ! -s out.stdout ]
84	then
85	    tet_infoline "Expected output to stdout, but none written"
86	    FAIL=Y
87	fi
88	;;
89    *)
90	diff -uN out.stdout ${1}.out > diff.out 2> /dev/null
91	if [ $? -ne 0 ]; then
92	    tet_infoline "Incorrect output written to stdout, as shown below"
93	    infofile "diff.out" "diff:"
94	    FAIL=Y
95	fi
96	;;
97    esac
98}
99
100infofile() # write file to journal using tet_infoline
101{
102    # $1 is file name, $2 is prefix for tet_infoline
103
104    prefix=$2
105    while read line
106    do
107	tet_infoline "$prefix$line"
108    done < $1
109}
110
111run()
112{
113    tpstart
114    cmdline=`echo $1 | sed -e 's/@/ -/g' -e 's/%/ /g'`
115    tet_infoline "$cmdline"
116    check_exit "$TET_SUITE_ROOT/../../elfdump/elfdump $cmdline" 0
117    check_stderr $1
118    check_stdout $1
119    tpresult
120}
121
122cleanup()
123{
124    rm -f out.stdout
125    rm -f out.stderr
126    rm -f diff.out
127}
128