functions.sh 4.6 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
# echo srcdir = $srcdir
Niels Möller's avatar
Niels Möller committed
4

5
6
7
8
9
10
11
12
13
14
15
type_p () {
  ( IFS=':'
    for d in $PATH ; do
      if [ -x "$d/$1" ] ; then
        echo "$d/$1"
	exit 0
      fi
    done
    exit 1
  ) }

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

19
20
: ${LFIB_STREAM:="`PATH="../nettle_builddir/tools:$PATH" type_p nettle-lfib-stream`"}
: ${SEXP_CONV:="`PATH="../nettle_builddir/tools:$PATH" type_p sexp-conv`"}
21

22
: ${LSH_TRANSPORT:="`cd .. && pwd`/lsh-transport"}
23
LSH_MAKE_SEED=/bin/false
24
25
26
: ${LSHD_CONNECTION:="`cd .. && pwd`/lshd-connection"}
: ${LSHD_PTY_HELPER:="`cd .. && pwd`/lshd-pty-helper"}
: ${LSHD_USERAUTH:="`cd .. && pwd`/lshd-userauth"}
Niels Möller's avatar
Niels Möller committed
27

Niels Möller's avatar
Niels Möller committed
28
29
: ${LSHD_UTMP:="`pwd`/home/utmpx"}
: ${LSHD_WTMP:="`pwd`/home/wtmpx"}
Niels Möller's avatar
Niels Möller committed
30

31
: ${LSHD_CONFIG_DIR:="$srcdir/config"}
Niels Möller's avatar
Niels Möller committed
32

Niels Möller's avatar
Niels Möller committed
33
34
35
export LSH_YARROW_SEED_FILE SEXP_CONV LSH_TRANSPORT
export LSHD_CONNECTION LSHD_PTY_HELPER LSHD_USERAUTH
export LSHD_UTMP LSHD_WTMP LSHD_CONFIG_DIR
36
37

: ${LSHD_FLAGS:=''}
38
39
40
41
: ${LSH_FLAGS:=-q}
: ${LSHG_FLAGS:=-q}
: ${HOSTKEY:="$srcdir/key-1.private"}
: ${PIDFILE:="`pwd`/lshd.$$.pid"}
42
43
: ${LSH_PIDFILE:="`pwd`/lsh.$$.pid"}
: ${LSHG_PIDFILE:="`pwd`/lshg.$$.pid"}
Niels Möller's avatar
Niels Möller committed
44
: ${INTERFACE:=localhost}
45

46
47
48
49
unset DISPLAY
TEST_DISPLAY=:17
export XAUTHORITY="`pwd`/xauthority"

50
51
# FIXME: Are these flags obsolete?

52
53
# Ignore any options the tester might have put in the environment.

Niels Möller's avatar
Niels Möller committed
54
55
56
57
58
# With bash, unset returns a non-zero exit status for non-existing
# variables. We have to ignore that error.

unset LSHGFLAGS || :
unset LSHFLAGS || :
59

Niels Möller's avatar
Niels Möller committed
60
PORT=11147
61
ATEXIT='true '
Niels Möller's avatar
Niels Möller committed
62

63
64
# We start with EXIT_FAILURE, and changing it to EXIT_SUCCESS only if
# test_success is invoked.
65
66
67

test_result=1

68
69
70
71
72
73
74
75
76
werror () {
    echo 1>&2 "$1"
}

test_done () {
    eval "$ATEXIT"
    exit $test_result;
}

77
78
test_fail () {
    test_result=1
79
    test_done
80
81
82
83
}

test_success () {
    test_result=0
84
    test_done
85
86
87
88
}

test_skip () {
    test_result=77
89
    test_done
90
91
92
93
94
95
96
}

die () {
    werror "$1"
    test_fail
}

97
98
check_x11_support () {
    ../lsh --help | grep 'x11-forward' >/dev/null || test_skip
99
100
}

101
102
103
104
105
need_xvfb () {
    check_x11_support
    type Xvfb >/dev/null 2>&1 || test_skip
}

106
need_tcputils () {
107
    type tcpconnect >/dev/null 2>&1 || test_skip
Niels Möller's avatar
Niels Möller committed
108
109
110
}

need_tsocks () {
111
    type tsocks >/dev/null 2>&1 || test_skip
112
113
}

114
at_exit () {
Niels Möller's avatar
Niels Möller committed
115
116
117
  ATEXIT="$ATEXIT ; $1"
}

118
spawn_lshd () {
Niels Möller's avatar
Niels Möller committed
119

120
121
    # local is not available in /bin/sh
    # local delay
122

123
124
    HOME="$TEST_HOME" ../lshd -h $HOSTKEY \
	-p $PORT --interface=$INTERFACE $LSHD_FLAGS \
125
	--pid-file $PIDFILE --daemonic --no-setsid "$@" || return 1
126
    
127
    # lshd should release its port after receiving HUP, but we may get
128
129
    # 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
130

131
    at_exit 'kill -HUP `cat $PIDFILE` ; sleep 5'
Niels Möller's avatar
Niels Möller committed
132
133
134

    # Wait a little for lshd to start
    for delay in 1 1 1 1 1 5 5 5 20 20 60 60; do
135
	if [ -s $PIDFILE ]; then
Niels Möller's avatar
Niels Möller committed
136
137
	    # And a little more for the pid file to be written properly
	    sleep 1
138
	    echo lshd pid: `cat $PIDFILE`
139
	    return 0
Niels Möller's avatar
Niels Möller committed
140
141
142
143
	fi
	sleep $delay
    done
    
144
    return 1
Niels Möller's avatar
Niels Möller committed
145
146
}

147
# FIXME: Enable -z, when implemented on the server side.
148
run_lsh () {
149
    cmd="$1"
Niels Möller's avatar
Niels Möller committed
150
    shift
Niels Möller's avatar
Niels Möller committed
151
    echo "$cmd" | HOME="$TEST_HOME" ../lsh -nt $LSH_FLAGS \
152
	--no-use-gateway --sloppy-host-authentication \
153
	--host-db-update /dev/null -p $PORT "$@" localhost
Niels Möller's avatar
Niels Möller committed
154
}
Niels Möller's avatar
Niels Möller committed
155

Niels Möller's avatar
Niels Möller committed
156
157
158
stdin_lsh () {
    HOME="$TEST_HOME" ../lsh -nt $LSH_FLAGS \
	--no-use-gateway --sloppy-host-authentication \
159
	--host-db-update /dev/null -p $PORT "$@" localhost
Niels Möller's avatar
Niels Möller committed
160
161
}

162
exec_lsh () {
Niels Möller's avatar
Niels Möller committed
163
    HOME="$TEST_HOME" ../lsh $LSH_FLAGS --sloppy-host-authentication \
164
	--no-use-gateway \
165
	--host-db-update /dev/null -z -p $PORT localhost "$@"
166
167
}

Niels Möller's avatar
Niels Möller committed
168
# FIXME: Use -B
169
170
spawn_lsh () {
    # echo spawn_lsh "$@"
Niels Möller's avatar
Niels Möller committed
171
    HOME="$TEST_HOME" ../lsh $LSH_FLAGS -nt --sloppy-host-authentication \
172
	--no-use-gateway \
173
	--host-db-update /dev/null -z -p $PORT "$@" --write-pid -B localhost > "$LSH_PIDFILE"
174
175

    at_exit 'kill `cat $LSH_PIDFILE`'
Niels Möller's avatar
Niels Möller committed
176
177
}

178
exec_lshg () {
Niels Möller's avatar
Niels Möller committed
179
    ../lsh --use-gateway --program-name lshg $LSHG_FLAGS -p $PORT localhost "$@"
180
181
}

182
183
spawn_lshg () {
    # echo spawn_lshg "$@"
184
    ../lsh --use-gateway --program-name lshg $LSHG_FLAGS -p $PORT "$@" --write-pid -B localhost > "$LSHG_PIDFILE"
185
    at_exit 'kill `cat $LSHG_PIDFILE`'
186
187
}

188
189
190
191
192
193
194
spawn_xvfb () {
    Xvfb -nolisten tcp $TEST_DISPLAY &
    at_exit "kill $!"
    xauth generate $TEST_DISPLAY . 
}


195
# at_connect local-port max-connections shell-command
196
at_connect () {
197
    # sleep 1 # Allow some time for earlier processes to die
198
    mini-inetd -m $2 -- localhost:$1 /bin/sh sh -c "$3" &
199
    at_exit "kill $!"
Niels Möller's avatar
Niels Möller committed
200
}
201
202
203
204
205
206
207
208
209
210

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
}