~ubuntu-branches/ubuntu/saucy/openvpn/saucy-proposed

« back to all changes in this revision

Viewing changes to t_client.sh

  • Committer: Package Import Robot
  • Author(s): Stéphane Graber
  • Date: 2013-05-24 17:42:45 UTC
  • mfrom: (1.1.19) (10.2.22 sid)
  • Revision ID: package-import@ubuntu.com-20130524174245-g9y6wlforycufqy5
Tags: 2.3.1-2ubuntu1
* Merge from Debian unstable. Remaining changes:
  - debian/openvpn.init.d:
    + Do not use start-stop-daemon and </dev/null to avoid blocking boot.
    + Show per-VPN result messages.
    + Add "--script-security 2" by default for backwards compatabliity.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/bin/sh
2
 
#
3
 
# run OpenVPN client against ``test reference'' server
4
 
# - check that ping, http, ... via tunnel works
5
 
# - check that interface config / routes are properly cleaned after test end
6
 
#
7
 
# prerequisites:
8
 
# - openvpn binary in current directory
9
 
# - writable current directory to create subdir for logs
10
 
# - t_client.rc in current directory OR source dir that specifies tests
11
 
# - for "ping4" checks: fping binary in $PATH
12
 
# - for "ping6" checks: fping6 binary in $PATH
13
 
#
14
 
 
15
 
if [ -r ./t_client.rc ] ; then
16
 
    . ./t_client.rc
17
 
elif [ -r "${srcdir}"/t_client.rc ] ; then
18
 
    . "${srcdir}"/t_client.rc
19
 
else
20
 
    echo "$0: cannot find 't_client.rc' in current directory or" >&2
21
 
    echo "$0: source dir ('${srcdir}').  SKIPPING TEST." >&2
22
 
    exit 77
23
 
fi
24
 
 
25
 
if [ ! -x ./openvpn ]
26
 
then
27
 
    echo "no (executable) openvpn binary in current directory. FAIL." >&2
28
 
    exit 1
29
 
fi
30
 
 
31
 
if [ ! -w . ]
32
 
then
33
 
    echo "current directory is not writable (required for logging). FAIL." >&2
34
 
    exit 1
35
 
fi
36
 
 
37
 
if [ -z "$CA_CERT" ] ; then
38
 
    echo "CA_CERT not defined in 't_client.rc'. SKIP test." >&2
39
 
    exit 77
40
 
fi
41
 
 
42
 
if [ -z "$TEST_RUN_LIST" ] ; then
43
 
    echo "TEST_RUN_LIST empty, no tests defined.  SKIP test." >&2
44
 
    exit 77
45
 
fi
46
 
 
47
 
# make sure we have permissions to run ifconfig/route from OpenVPN
48
 
# can't use "id -u" here - doesn't work on Solaris
49
 
ID=`id`
50
 
if expr "$ID" : "uid=0" >/dev/null
51
 
then :
52
 
else
53
 
    echo "$0: this test must run be as root. SKIP." >&2
54
 
    exit 77
55
 
fi
56
 
 
57
 
LOGDIR=t_client-`hostname`-`date +%Y%m%d-%H%M%S`
58
 
if mkdir $LOGDIR
59
 
then :
60
 
else
61
 
    echo "can't create log directory '$LOGDIR'. FAIL." >&2
62
 
    exit 1
63
 
fi
64
 
 
65
 
exit_code=0
66
 
 
67
 
# ----------------------------------------------------------
68
 
# helper functions
69
 
# ----------------------------------------------------------
70
 
# print failure message, increase FAIL counter
71
 
fail()
72
 
{
73
 
    echo ""
74
 
    echo "FAIL: $@" >&2
75
 
    fail_count=$(( $fail_count + 1 ))
76
 
}
77
 
 
78
 
# print "all interface IP addresses" + "all routes"
79
 
# this is higly system dependent...
80
 
get_ifconfig_route()
81
 
{
82
 
    # linux / iproute2? (-> if configure got a path)
83
 
    if [ "/sbin/ip" != "ip" ]
84
 
    then
85
 
        echo "-- linux iproute2 --"
86
 
        /sbin/ip addr show     | grep -v valid_lft
87
 
        /sbin/ip route show
88
 
        /sbin/ip -6 route show | sed -e 's/expires [0-9]*sec //'
89
 
        return
90
 
    fi
91
 
 
92
 
    # try uname
93
 
    case `uname -s` in
94
 
        Linux)
95
 
           echo "-- linux / ifconfig --"
96
 
           LANG=C /sbin/ifconfig -a |egrep  "( addr:|encap:)"
97
 
           LANG=C /bin/netstat -rn -4 -6
98
 
           return
99
 
           ;;
100
 
        FreeBSD|NetBSD|Darwin)
101
 
           echo "-- FreeBSD/NetBSD/Darwin [MacOS X] --"
102
 
           /sbin/ifconfig -a | egrep "(flags=|inet)"
103
 
           /bin/netstat -rn | awk '$3 !~ /^UHL/ { print $1,$2,$3,$NF }'
104
 
           return
105
 
           ;;
106
 
        OpenBSD)
107
 
           echo "-- OpenBSD --"
108
 
           /sbin/ifconfig -a | egrep "(flags=|inet)" | \
109
 
                sed -e 's/pltime [0-9]*//' -e 's/vltime [0-9]*//'
110
 
           /bin/netstat -rn | awk '$3 !~ /^UHL/ { print $1,$2,$3,$NF }'
111
 
           return
112
 
           ;;
113
 
        SunOS)
114
 
           echo "-- Solaris --"
115
 
           /sbin/ifconfig -a | egrep "(flags=|inet)"
116
 
           /bin/netstat -rn | awk '$3 !~ /^UHL/ { print $1,$2,$3,$6 }'
117
 
           return
118
 
           ;;
119
 
    esac
120
 
 
121
 
    echo "get_ifconfig_route(): no idea how to get info on your OS.  FAIL." >&2
122
 
    exit 20
123
 
}
124
 
 
125
 
# ----------------------------------------------------------
126
 
# check ifconfig
127
 
#  arg1: "4" or "6" -> for message
128
 
#  arg2: IPv4/IPv6 address that must show up in out of "get_ifconfig_route"
129
 
check_ifconfig()
130
 
{
131
 
    proto=$1 ; shift
132
 
    expect_list="$@"
133
 
 
134
 
    if [ -z "$expect_list" ] ; then return ; fi
135
 
 
136
 
    for expect in $expect_list
137
 
    do
138
 
        if get_ifconfig_route | fgrep "$expect" >/dev/null
139
 
        then :
140
 
        else
141
 
            fail "check_ifconfig(): expected IPv$proto address '$expect' not found in ifconfig output."
142
 
        fi
143
 
    done
144
 
}
145
 
 
146
 
# ----------------------------------------------------------
147
 
# run pings
148
 
#  arg1: "4" or "6" -> fping/fing6
149
 
#  arg2: "want_ok" or "want_fail" (expected ping result)
150
 
#  arg3... -> fping arguments (host list)
151
 
run_ping_tests()
152
 
{
153
 
    proto=$1 ; want=$2 ; shift ; shift
154
 
    targetlist="$@"
155
 
 
156
 
    # "no targets" is fine
157
 
    if [ -z "$targetlist" ] ; then return ; fi
158
 
 
159
 
    case $proto in
160
 
        4) cmd=fping ;;
161
 
        6) cmd=fping6 ;;
162
 
        *) echo "internal error in run_ping_tests arg 1: '$proto'" >&2
163
 
           exit 1 ;;
164
 
    esac
165
 
 
166
 
    case $want in
167
 
        want_ok)   sizes_list="64 1440 3000" ;;
168
 
        want_fail) sizes_list="64" ;;
169
 
    esac
170
 
 
171
 
    for bytes in $sizes_list
172
 
    do
173
 
        echo "run IPv$proto ping tests ($want), $bytes byte packets..."
174
 
 
175
 
        echo "$cmd -b $bytes -C 20 -p 250 -q $targetlist" >>$LOGDIR/$SUF:fping.out
176
 
        $cmd -b $bytes -C 20 -p 250 -q $targetlist >>$LOGDIR/$SUF:fping.out 2>&1
177
 
 
178
 
        # while OpenVPN is running, pings must succeed (want='want_ok')
179
 
        # before OpenVPN is up, pings must NOT succeed (want='want_fail')
180
 
 
181
 
        rc=$?
182
 
        if [ $rc = 0 ]                          # all ping OK
183
 
        then
184
 
            if [ $want = "want_fail" ]          # not what we want
185
 
            then
186
 
                fail "IPv$proto ping test succeeded, but needs to *fail*."
187
 
            fi
188
 
        else                                    # ping failed
189
 
            if [ $want = "want_ok" ]            # not what we wanted
190
 
            then
191
 
                fail "IPv$proto ping test ($bytes bytes) failed, but should succeed."
192
 
            fi
193
 
        fi
194
 
    done
195
 
}
196
 
 
197
 
# ----------------------------------------------------------
198
 
# main test loop
199
 
# ----------------------------------------------------------
200
 
for SUF in $TEST_RUN_LIST
201
 
do
202
 
    echo -e "\n### test run $SUF ###\n"
203
 
    fail_count=0
204
 
 
205
 
    echo "save pre-openvpn ifconfig + route"
206
 
    get_ifconfig_route >$LOGDIR/$SUF:ifconfig_route_pre.txt
207
 
 
208
 
    # get config variables
209
 
    eval openvpn_conf=\"\$OPENVPN_CONF_$SUF\"
210
 
    eval expect_ifconfig4=\"\$EXPECT_IFCONFIG4_$SUF\"
211
 
    eval expect_ifconfig6=\"\$EXPECT_IFCONFIG6_$SUF\"
212
 
    eval ping4_hosts=\"\$PING4_HOSTS_$SUF\"
213
 
    eval ping6_hosts=\"\$PING6_HOSTS_$SUF\"
214
 
 
215
 
    echo -e "\nrun pre-openvpn ping tests - targets must not be reachable..."
216
 
    run_ping_tests 4 want_fail "$ping4_hosts"
217
 
    run_ping_tests 6 want_fail "$ping6_hosts"
218
 
    if [ "$fail_count" = 0 ] ; then
219
 
        echo -e "OK.\n"
220
 
    else
221
 
        echo -e "FAIL: make sure that ping hosts are ONLY reachable via VPN, SKIP test $SUF".
222
 
        exit_code=31
223
 
        continue
224
 
    fi
225
 
 
226
 
    echo " run ./openvpn $openvpn_conf"
227
 
    ./openvpn $openvpn_conf >$LOGDIR/$SUF:openvpn.log &
228
 
    opid=$!
229
 
 
230
 
    # make sure openvpn client is terminated in case shell exits
231
 
    trap "kill $opid" 0
232
 
    trap "kill $opid ; trap - 0 ; exit 1" 1 2 3 15
233
 
 
234
 
    echo "wait for connection to establish..."
235
 
    sleep 10
236
 
 
237
 
    # test whether OpenVPN process is still there
238
 
    if kill -0 $opid
239
 
    then :
240
 
    else
241
 
        echo -e "OpenVPN process has failed to start up, check log ($LOGDIR/$SUF:openvpn.log).  FAIL.\ntail of logfile follows:\n..." >&2
242
 
        tail $LOGDIR/$SUF:openvpn.log >&2
243
 
        trap - 0 1 2 3 15
244
 
        exit 10
245
 
    fi
246
 
 
247
 
    # compare whether anything changed in ifconfig/route setup?
248
 
    echo "save ifconfig+route"
249
 
    get_ifconfig_route >$LOGDIR/$SUF:ifconfig_route.txt
250
 
 
251
 
    echo -n "compare pre-openvpn ifconfig+route with current values..."
252
 
    if diff $LOGDIR/$SUF:ifconfig_route_pre.txt \
253
 
            $LOGDIR/$SUF:ifconfig_route.txt >/dev/null
254
 
    then
255
 
        fail "no differences between ifconfig/route before OpenVPN start and now."
256
 
    else
257
 
        echo -e " OK!\n"
258
 
    fi
259
 
 
260
 
    # expected ifconfig values in there?
261
 
    check_ifconfig 4 "$expect_ifconfig4"
262
 
    check_ifconfig 6 "$expect_ifconfig6"
263
 
 
264
 
    run_ping_tests 4 want_ok "$ping4_hosts"
265
 
    run_ping_tests 6 want_ok "$ping6_hosts"
266
 
    echo -e "ping tests done.\n"
267
 
 
268
 
    echo "stopping OpenVPN"
269
 
    kill $opid
270
 
    wait $!
271
 
    rc=$?
272
 
    if [ $rc != 0 ] ; then
273
 
        fail "OpenVPN return code $rc, expect 0"
274
 
    fi
275
 
 
276
 
    echo -e "\nsave post-openvpn ifconfig + route..."
277
 
    get_ifconfig_route >$LOGDIR/$SUF:ifconfig_route_post.txt
278
 
 
279
 
    echo -n "compare pre- and post-openvpn ifconfig + route..."
280
 
    if diff $LOGDIR/$SUF:ifconfig_route_pre.txt \
281
 
            $LOGDIR/$SUF:ifconfig_route_post.txt >$LOGDIR/$SUF:ifconfig_route_diff.txt
282
 
    then
283
 
        echo -e " OK.\n"
284
 
    else
285
 
        cat $LOGDIR/$SUF:ifconfig_route_diff.txt >&2
286
 
        fail "differences between pre- and post-ifconfig/route"
287
 
    fi
288
 
    if [ "$fail_count" = 0 ] ; then
289
 
        echo -e "test run $SUF: all tests OK.\n"
290
 
    else
291
 
        echo -e "test run $SUF: $fail_count test failures. FAIL.\n";
292
 
        exit_code=30
293
 
    fi
294
 
done
295
 
 
296
 
# remove trap handler
297
 
trap - 0 1 2 3 15
298
 
exit $exit_code