functions.sh 2.58 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
if [ -z "$LSHD_FLAGS" ] ; then
    LSHD_FLAGS='-q --enable-core'
Niels Möller's avatar
Niels Möller committed
7
8
fi

9
10
11
12
13
14
if [ -z "$LSH_FLAGS" ] ; then
    LSH_FLAGS=-q
fi

if [ -z "$LSHG_FLAGS" ] ; then
    LSHG_FLAGS=-q
Niels Möller's avatar
Niels Möller committed
15
16
fi

17
18
19
20
if [ -z "$HOSTKEY" ] ; then
    HOSTKEY=$srcdir/key-1.private
fi

21
22
23
24
if [ -z "$PIDFILE" ] ; then
    PIDFILE=`pwd`/lshd.$$.pid
fi

25
26
INTERFACE=127.0.0.1

27
28
29
30
# if [ -z "$USERKEY" ] ; then
#     USERKEY=$srcdir/key-1.private
# fi

31
32
33
# Any error count as failure.
set -e

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

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Starting with EXIT_FAILURE and changing it to EXIT_SUCCESS on
# success is right, as long as each test script only performs one
# test. If there are several tests, it would be better to set it to
# EXIT_SUCCESS and change it as soon as one test fails.

test_result=1

test_fail () {
    test_result=1
}

test_success () {
    test_result=0
}

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

54
at_exit () {
Niels Möller's avatar
Niels Möller committed
55
56
57
  ATEXIT="$ATEXIT ; $1"
}

58
spawn_lshd () {
Niels Möller's avatar
Niels Möller committed
59

60
61
    # local is not available in /bin/sh
    # local delay
62
63
64

    # 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
65
    
Niels Möller's avatar
Niels Möller committed
66
    ../lshd -h $HOSTKEY --interface=$INTERFACE -p $PORT $LSHD_FLAGS \
67
	--pid-file $PIDFILE --daemon --no-syslog "$@"
Niels Möller's avatar
Niels Möller committed
68

Niels Möller's avatar
Niels Möller committed
69
70
71
72
    # lshd may catch the ordinary TERM signal, leading to timing
    # problems when the next lshd process tries to bind the port.
    # So we kill it harder.

73
    at_exit 'kill -9 `cat $PIDFILE`; rm -f $PIDFILE'
Niels Möller's avatar
Niels Möller committed
74
75
76

    # Wait a little for lshd to start
    for delay in 1 1 1 1 1 5 5 5 20 20 60 60; do
77
	if [ -s $PIDFILE ]; then
Niels Möller's avatar
Niels Möller committed
78
79
80
81
82
83
84
85
86
87
	    # And a little more for it to open its port
	    sleep 5
	    return
	fi
	sleep $delay
    done
    
    false
}

88
run_lsh () {
89
    cmd="$1"
Niels Möller's avatar
Niels Möller committed
90
    shift
91
    echo "$cmd" | ../lsh $LSH_FLAGS -nt --sloppy-host-authentication \
Niels Möller's avatar
Niels Möller committed
92
93
94
95
	--capture-to /dev/null -z -p $PORT "$@" localhost

}

96
exec_lsh () {
97
    ../lsh $LSH_FLAGS -nt --sloppy-host-authentication \
98
99
100
	--capture-to /dev/null -z -p $PORT localhost "$@"
}

101
102
spawn_lsh () {
    # echo spawn_lsh "$@"
103
    ../lsh $LSH_FLAGS -nt --sloppy-host-authentication \
Niels Möller's avatar
Niels Möller committed
104
	--capture-to /dev/null -z -p $PORT "$@" -N localhost &
105
    at_exit "kill $!"
Niels Möller's avatar
Niels Möller committed
106
107
}

108
exec_lshg () {
109
    ../lshg $LSHG_FLAGS -nt -p $PORT localhost "$@"
110
111
}

112
113
spawn_lshg () {
    # echo spawn_lshg "$@"
114
    ../lshg $LSHG_FLAGS -p $PORT "$@" -N localhost &
115
116
117
    at_exit "kill $!"
}

118
at_connect () {
Niels Möller's avatar
Niels Möller committed
119
    mini-inetd -m $2 localhost:$1 -- /bin/sh sh -c "$3" &
120
    at_exit "kill $!"
Niels Möller's avatar
Niels Möller committed
121
}
122
123
124
125
126
127
128
129
130
131

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
}