functions.sh 4.04 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
14
: ${LSH_TRANSPORT:="`pwd`/../lsh-transport"}
: ${LSHD_CONNECTION:="`pwd`/../lshd-connection"}
Niels Möller's avatar
Niels Möller committed
15
: ${LSHD_PTY_HELPER:="`pwd`/../lshd-pty-helper"}
Niels Möller's avatar
Niels Möller committed
16
: ${LSHD_USERAUTH:="`pwd`/../lshd-userauth"}
Niels Möller's avatar
Niels Möller committed
17

Niels Möller's avatar
Niels Möller committed
18
19
20
: ${LSHD_UTMP:="`pwd`/home/utmpx"}
: ${LSHD_WTMP:="`pwd`/home/wtmpx"}

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

Niels Möller's avatar
Niels Möller committed
23
24
25
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
26
27

: ${LSHD_FLAGS:=''}
28
29
30
31
: ${LSH_FLAGS:=-q}
: ${LSHG_FLAGS:=-q}
: ${HOSTKEY:="$srcdir/key-1.private"}
: ${PIDFILE:="`pwd`/lshd.$$.pid"}
32
33
: ${LSH_PIDFILE:="`pwd`/lsh.$$.pid"}
: ${LSHG_PIDFILE:="`pwd`/lshg.$$.pid"}
34

35
36
# Ignore any options the tester might have put in the environment.

Niels Möller's avatar
Niels Möller committed
37
38
39
40
41
# With bash, unset returns a non-zero exit status for non-existing
# variables. We have to ignore that error.

unset LSHGFLAGS || :
unset LSHFLAGS || :
42

Niels Möller's avatar
Niels Möller committed
43
PORT=11147
44
ATEXIT='set +e'
Niels Möller's avatar
Niels Möller committed
45

46
47
# We start with EXIT_FAILURE, and changing it to EXIT_SUCCESS only if
# test_success is invoked.
48
49
50

test_result=1

51
52
53
54
55
56
57
58
59
werror () {
    echo 1>&2 "$1"
}

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

60
61
test_fail () {
    test_result=1
62
    test_done
63
64
65
66
}

test_success () {
    test_result=0
67
    test_done
68
69
70
71
}

test_skip () {
    test_result=77
72
    test_done
73
74
75
76
77
78
79
}

die () {
    werror "$1"
    test_fail
}

80
81
check_x11_support () {
    ../lsh --help | grep 'x11-forward' >/dev/null || test_skip
82
83
}

84
85
need_tcputils () {
    if type tcpconnect >/dev/null 2>&1 ; then : ; else
Niels Möller's avatar
Niels Möller committed
86
87
88
89
90
91
	test_skip
    fi
}

need_tsocks () {
    if type tsocks >/dev/null 2>&1 ; then : ; else
92
	test_skip
93
94
95
    fi
}

96
at_exit () {
Niels Möller's avatar
Niels Möller committed
97
98
99
  ATEXIT="$ATEXIT ; $1"
}

100
spawn_lshd () {
Niels Möller's avatar
Niels Möller committed
101

102
103
    # local is not available in /bin/sh
    # local delay
104
105

    # Note that --daemon not only forks into the background, it also changes
106
    # the cwd, uses syslog, etc. So we use --background instead.
Niels Möller's avatar
Niels Möller committed
107
    
108
109
    HOME="$TEST_HOME" ../lshd -h $HOSTKEY \
	-p $PORT --interface=$INTERFACE $LSHD_FLAGS \
110
	--pid-file $PIDFILE --background "$@" || return 1
111
    
112
    # lshd should release its port after receiving HUP, but we may get
113
114
    # 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
115

116
    at_exit 'kill -HUP `cat $PIDFILE` ; sleep 5'
Niels Möller's avatar
Niels Möller committed
117
118
119

    # Wait a little for lshd to start
    for delay in 1 1 1 1 1 5 5 5 20 20 60 60; do
120
	if [ -s $PIDFILE ]; then
Niels Möller's avatar
Niels Möller committed
121
122
	    # And a little more for the pid file to be written properly
	    sleep 1
123
	    echo lshd pid: `cat $PIDFILE`
124
	    return 0
Niels Möller's avatar
Niels Möller committed
125
126
127
128
	fi
	sleep $delay
    done
    
129
    return 1
Niels Möller's avatar
Niels Möller committed
130
131
}

132
# FIXME: Enable -z, when implemented on the server side.
133
run_lsh () {
134
    cmd="$1"
Niels Möller's avatar
Niels Möller committed
135
    shift
Niels Möller's avatar
Niels Möller committed
136
    echo "$cmd" | HOME="$TEST_HOME" ../lsh -nt $LSH_FLAGS \
137
	--no-use-gateway --sloppy-host-authentication \
138
	--capture-to /dev/null -p $PORT "$@" localhost
Niels Möller's avatar
Niels Möller committed
139
140
141

}

142
exec_lsh () {
Niels Möller's avatar
Niels Möller committed
143
    HOME="$TEST_HOME" ../lsh $LSH_FLAGS --sloppy-host-authentication \
144
	--no-use-gateway \
145
146
147
	--capture-to /dev/null -z -p $PORT localhost "$@"
}

Niels Möller's avatar
Niels Möller committed
148
# FIXME: Use -B
149
150
spawn_lsh () {
    # echo spawn_lsh "$@"
Niels Möller's avatar
Niels Möller committed
151
    HOME="$TEST_HOME" ../lsh $LSH_FLAGS -nt --sloppy-host-authentication \
152
	--no-use-gateway \
153
154
155
	--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
156
157
}

158
exec_lshg () {
Niels Möller's avatar
Niels Möller committed
159
    ../lsh --use-gateway --program-name lshg $LSHG_FLAGS -p $PORT localhost "$@"
160
161
}

162
163
spawn_lshg () {
    # echo spawn_lshg "$@"
164
    ../lsh --use-gateway --program-name lshg $LSHG_FLAGS -p $PORT "$@" --write-pid -B localhost > "$LSHG_PIDFILE"
165
    at_exit 'kill `cat $LSHG_PIDFILE`'
166
167
}

168
# at_connect local-port max-connections shell-command
169
at_connect () {
170
    # sleep 1 # Allow some time for earlier processes to die
171
    mini-inetd -m $2 -- localhost:$1 /bin/sh sh -c "$3" &
172
    at_exit "kill $!"
Niels Möller's avatar
Niels Möller committed
173
}
174
175
176
177
178
179
180
181
182
183

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
}