functions.sh 3.47 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
2
# Helper functions for the test scripts.

Niels Möller's avatar
Niels Möller committed
3
4
5
# Any error count as failure.
set -e

Niels Möller's avatar
Niels Möller committed
6
# echo srcdir = $srcdir
Niels Möller's avatar
Niels Möller committed
7

8
9
: ${TEST_HOME:=`pwd`/home}
: ${LSH_YARROW_SEED_FILE:="$TEST_HOME/.lsh/yarrow-seed-file"}
Niels Möller's avatar
Niels Möller committed
10

Niels Möller's avatar
Niels Möller committed
11
# For lsh-authorize
12
: ${SEXP_CONV:="`pwd`/../nettle/tools/sexp-conv"}
Niels Möller's avatar
Niels Möller committed
13

Niels Möller's avatar
Niels Möller committed
14
15
export LSH_YARROW_SEED_FILE SEXP_CONV

Niels Möller's avatar
Niels Möller committed
16
: ${LSHD_FLAGS:='-q --enable-core'}
17
18
19
20
: ${LSH_FLAGS:=-q}
: ${LSHG_FLAGS:=-q}
: ${HOSTKEY:="$srcdir/key-1.private"}
: ${PIDFILE:="`pwd`/lshd.$$.pid"}
21
22
: ${LSH_PIDFILE:="`pwd`/lsh.$$.pid"}
: ${LSHG_PIDFILE:="`pwd`/lshg.$$.pid"}
23
: ${INTERFACE:=localhost}
24

25
26
# Ignore any options the tester might have put in the environment.

Niels Möller's avatar
Niels Möller committed
27
28
29
30
31
# With bash, unset returns a non-zero exit status for non-existing
# variables. We have to ignore that error.

unset LSHGFLAGS || :
unset LSHFLAGS || :
32

Niels Möller's avatar
Niels Möller committed
33
PORT=11147
34
ATEXIT='set +e'
Niels Möller's avatar
Niels Möller committed
35

36
37
# We start with EXIT_FAILURE, and changing it to EXIT_SUCCESS only if
# test_success is invoked.
38
39
40
41
42

test_result=1

test_fail () {
    test_result=1
43
    exit
44
45
46
47
}

test_success () {
    test_result=0
48
49
50
51
52
53
54
55
    exit
}

test_skip () {
    test_result=77
    exit
}

56
57
58
59
60
61
62
63
64
werror () {
    echo 1>&2 "$1"
}

die () {
    werror "$1"
    test_fail
}

65
66
check_x11_support () {
    ../lsh --help | grep 'x11-forward' >/dev/null || test_skip
67
68
}

69
70
need_tcputils () {
    if type tcpconnect >/dev/null 2>&1 ; then : ; else
Niels Möller's avatar
Niels Möller committed
71
72
73
74
75
76
	test_skip
    fi
}

need_tsocks () {
    if type tsocks >/dev/null 2>&1 ; then : ; else
77
	test_skip
78
79
80
    fi
}

81
trap 'eval "$ATEXIT ; exit \$test_result"' 0
Niels Möller's avatar
Niels Möller committed
82

83
at_exit () {
Niels Möller's avatar
Niels Möller committed
84
85
86
  ATEXIT="$ATEXIT ; $1"
}

87
spawn_lshd () {
Niels Möller's avatar
Niels Möller committed
88

89
90
    # local is not available in /bin/sh
    # local delay
91
92
93

    # Note that --daemon not only forks into the background, it also changes
    # the cwd, uses syslog, etc.
Niels Möller's avatar
Niels Möller committed
94
    
95
96
    HOME="$TEST_HOME" ../lshd -h $HOSTKEY \
	-p $PORT --interface=$INTERFACE $LSHD_FLAGS \
97
98
	--pid-file $PIDFILE --daemon --no-syslog "$@" || test_fail
    
99
    # lshd should release its port after receiving HUP, but we may get
100
101
    # timing problems when the next lshd process tries to bind the
    # port. So we also wait a little.
Niels Möller's avatar
Niels Möller committed
102

103
    at_exit 'kill -HUP `cat $PIDFILE` ; sleep 5'
Niels Möller's avatar
Niels Möller committed
104
105
106

    # Wait a little for lshd to start
    for delay in 1 1 1 1 1 5 5 5 20 20 60 60; do
107
	if [ -s $PIDFILE ]; then
Niels Möller's avatar
Niels Möller committed
108
109
	    # And a little more for the pid file to be written properly
	    sleep 1
110
	    echo lshd pid: `cat $PIDFILE`
Niels Möller's avatar
Niels Möller committed
111
112
113
114
115
	    return
	fi
	sleep $delay
    done
    
116
    test_fail
Niels Möller's avatar
Niels Möller committed
117
118
}

119
run_lsh () {
120
    cmd="$1"
Niels Möller's avatar
Niels Möller committed
121
    shift
Niels Möller's avatar
Niels Möller committed
122
123
    echo "$cmd" | HOME="$TEST_HOME" ../lsh $LSH_FLAGS -nt \
	--sloppy-host-authentication \
Niels Möller's avatar
Niels Möller committed
124
125
126
127
	--capture-to /dev/null -z -p $PORT "$@" localhost

}

128
exec_lsh () {
Niels Möller's avatar
Niels Möller committed
129
    HOME="$TEST_HOME" ../lsh $LSH_FLAGS -nt --sloppy-host-authentication \
130
131
132
	--capture-to /dev/null -z -p $PORT localhost "$@"
}

Niels Möller's avatar
Niels Möller committed
133
# FIXME: Use -B
134
135
spawn_lsh () {
    # echo spawn_lsh "$@"
Niels Möller's avatar
Niels Möller committed
136
    HOME="$TEST_HOME" ../lsh $LSH_FLAGS -nt --sloppy-host-authentication \
137
138
139
	--capture-to /dev/null -z -p $PORT "$@" --write-pid -B localhost > "$LSH_PIDFILE"

    at_exit 'kill `cat $LSH_PIDFILE`'
Niels Möller's avatar
Niels Möller committed
140
141
}

142
exec_lshg () {
143
    ../lshg $LSHG_FLAGS -nt -p $PORT localhost "$@"
144
145
}

146
147
spawn_lshg () {
    # echo spawn_lshg "$@"
148
149
    ../lshg $LSHG_FLAGS -p $PORT "$@" --write-pid -B localhost > "$LSHG_PIDFILE"
    at_exit 'kill `cat $LSHG_PIDFILE`'
150
151
}

152
# at_connect local-port max-connections shell-command
153
at_connect () {
154
    # sleep 1 # Allow some time for earlier processes to die
155
    mini-inetd -m $2 -- localhost:$1 /bin/sh sh -c "$3" &
156
    at_exit "kill $!"
Niels Möller's avatar
Niels Möller committed
157
}
158
159
160
161
162
163
164
165
166
167

compare_output() {
    if cmp test.out1 test.out2; then
	echo "$1: Ok, files match."
	test_success
    else
	echo "$1: Error, files are different."
	test_fail
    fi
}