-
Notifications
You must be signed in to change notification settings - Fork 31
/
shunit2_test_unit.sh
executable file
·304 lines (247 loc) · 7.81 KB
/
shunit2_test_unit.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/bin/bash
#
# Copyright (C) 2013 Dan Fruehauf <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
######################
# CORE FUNCTIONALITY #
######################
# test check_open_port, unreachable host
test_check_open_port_unresolvable() {
source $CHECK_VPN
check_open_port some.domain.that.doesnt.exist.com 1111 >& /dev/null
assertFalse "host unresolvable" \
"[ $? -eq 0 ]"
}
# test check_open_port
test_check_open_port_filtered() {
source $CHECK_VPN
check_open_port www.google.com 1111 >& /dev/null
assertFalse "port filtered" \
"[ $? -eq 0 ]"
}
# test check_open_port, filtered port
test_check_open_port_closed() {
source $CHECK_VPN
check_open_port localhost 1111 >& /dev/null
assertFalse "port closed" \
"[ $? -eq 0 ]"
}
# test check_open_port
test_check_open_port_open() {
source $CHECK_VPN
check_open_port www.google.com 80 >& /dev/null
assertTrue "port open" \
"[ $? -eq 0 ]"
}
# test the is_specific_device function
test_is_specific_device() {
source $CHECK_VPN
assertTrue "tun1: specific" "is_specific_device tun1"
assertTrue "tap10: specific" "is_specific_device tap10"
assertTrue "ppp250: specific" "is_specific_device ppp250"
assertFalse "tun: specific" "is_specific_device tun"
assertFalse "tap: specific" "is_specific_device tap"
assertFalse "ppp: specific" "is_specific_device ppp"
assertFalse "ttt20: specific" "is_specific_device ttt20"
assertFalse "ttt: specific" "is_specific_device ttt"
}
# test check_vpn locking
test_lock_check_vpn() {
source $CHECK_VPN
# mock CHECK_VPN_LOCK
export CHECK_VPN_LOCK=`mktemp -d -u`
assertFalse "lock doesn't exists" "test -d $CHECK_VPN_LOCK"
lock_check_vpn
assertTrue "lock exists" "test -d $CHECK_VPN_LOCK"
rmdir $CHECK_VPN_LOCK
}
# test check_vpn locking
test_unlock_check_vpn() {
source $CHECK_VPN
# mock CHECK_VPN_LOCK
export CHECK_VPN_LOCK=`mktemp -d`
assertTrue "lock exists" "test -d $CHECK_VPN_LOCK"
unlock_check_vpn
assertFalse "lock doesn't exists" "test -d $CHECK_VPN_LOCK"
}
# test routing table used for device
test_routing_table_for_device() {
source $CHECK_VPN
local -i routing_table
routing_table=`get_routing_table_for_device tap101`
assertTrue "routing table for tap101" "[ $routing_table -eq 2101 ]"
routing_table=`get_routing_table_for_device tun32`
assertTrue "routing table for tun32" "[ $routing_table -eq 3032 ]"
routing_table=`get_routing_table_for_device ppp250`
assertTrue "routing table for ppp250" "[ $routing_table -eq 4250 ]"
routing_table=`get_routing_table_for_device crapper1020`
assertTrue "routing table for crapper1020" "[ $routing_table -eq 7020 ]"
}
###########
# MODULES #
###########
########
# L2TP #
########
# allocate device for l2tp
test_l2tp_allocate_vpn_device() {
source check_vpn_plugins/l2tp.sh
local device=`_l2tp_allocate_vpn_device`
assertTrue "allocate l2tp device" "[ x$device = x'ppp0' ]"
}
# test _l2tp_generate_ppp_options
test_l2tp_generate_ppp_options() {
source check_vpn_plugins/l2tp.sh
local tmp_ppp_options=`mktemp`
local tmp_ppp_options_expected=`mktemp`
_l2tp_generate_ppp_options \
l2tp.vpn.com my-username my-password ppp140 \
require-mppe-128,require-mschapv2 > $tmp_ppp_options
cat > $tmp_ppp_options_expected <<EOF
user my-username
password my-password
unit 140
lock
noauth
nodefaultroute
noipdefault
debug
require-mppe-128
require-mschapv2
EOF
local -i diff_lines=`diff -urN $tmp_ppp_options $tmp_ppp_options_expected | wc -l`
assertTrue "ppp options not same as expected" "[ $diff_lines -eq 0 ]"
rm -f $tmp_ppp_options $tmp_ppp_options_expected
}
# test _l2tp_generate_xl2tpd_options
test_l2tp_generate_xl2tpd_options() {
source check_vpn_plugins/l2tp.sh
local tmp_l2tp_options=`mktemp`
local tmp_l2tp_options_expected=`mktemp`
_l2tp_generate_xl2tpd_options \
/tmp/ppp-options l2tp.vpn.com \
my-username my-password ppp140 > $tmp_l2tp_options
cat > $tmp_l2tp_options_expected <<EOF
[global]
port = 0
access control = no
[lac /tmp/ppp-options]
name = /tmp/ppp-options
lns = /tmp/ppp-options
pppoptfile = ppp140
ppp debug = yes
require authentication = yes
require chap = yes
length bit = yes
EOF
local -i diff_lines=`diff -urN $tmp_l2tp_options $tmp_l2tp_options_expected | wc -l`
assertTrue "l2tp options not same as expected" "[ $diff_lines -eq 0 ]"
rm -f $tmp_l2tp_options $tmp_l2tp_options_expected
}
###########
# OPENVPN #
###########
# allocate device for openvpn
test_openvpn_allocate_vpn_device() {
source check_vpn_plugins/openvpn.sh
local device=`_openvpn_allocate_vpn_device tap`
assertTrue "allocate openvpn tap device" "[ x$device = x'tap0' ]"
local device=`_openvpn_allocate_vpn_device tun`
assertTrue "allocate openvpn tun device" "[ x$device = x'tun0' ]"
}
# test argument parsing
test_openvpn_argument_parsing() {
source check_vpn_plugins/openvpn.sh
local arguments="--port 1194 --proto tcp --ca /etc/openvpn/ca.crt --config /etc/openvpn/vpn.com.conf"
local -i port=`_openvpn_parse_arg_from_extra_options port $arguments`
assertTrue "parsing port" \
"[ $port -eq 1194 ]"
local proto=`_openvpn_parse_arg_from_extra_options proto $arguments`
assertTrue "parsing proto" \
"[ x$proto = x'tcp' ]"
local ca=`_openvpn_parse_arg_from_extra_options ca $arguments`
assertTrue "parsing ca" \
"[ x$ca = x'/etc/openvpn/ca.crt' ]"
local config=`_openvpn_parse_arg_from_extra_options config $arguments`
assertTrue "parsing config" \
"[ x$config = x'/etc/openvpn/vpn.com.conf' ]"
}
########
# PPTP #
########
# allocate device for pptp
test_pptp_allocate_vpn_device() {
source check_vpn_plugins/pptp.sh
local device=`_pptp_allocate_vpn_device`
assertTrue "allocate pptp device" "[ x$device = x'ppp0' ]"
}
#######
# SSH #
#######
# allocate device for ssh
test_ssh_allocate_vpn_device() {
source check_vpn_plugins/ssh.sh
local device=`_ssh_allocate_vpn_device tap`
assertTrue "allocate ssh tap device" "[ x$device = x'tap0' ]"
local device=`_ssh_allocate_vpn_device tun`
assertTrue "allocate ssh tun device" "[ x$device = x'tun0' ]"
}
# test argument parsing
test_ssh_argument_parsing() {
source check_vpn_plugins/ssh.sh
local arguments="-p 5009 -o LogLevel=Debug -o Host=test.example.com"
local -i port=`_ssh_parse_option -p Port $arguments`
assertTrue "parsing port" \
"[ $port -eq 5009 ]"
local log_level=`_ssh_parse_option UNUSED LogLevel $arguments`
assertTrue "parsing LogLevel" \
"[ x$log_level = x'Debug' ]"
local host=`_ssh_parse_option UNUSED Host $arguments`
assertTrue "parsing Host" \
"[ x$host = x'test.example.com' ]"
}
# test device prefix Tunnel=ethernet
test_ssh_device_prefix_ethernet() {
source check_vpn_plugins/ssh.sh
local device_prefix=`_ssh_parse_device_prefix -o Tunnel=ethernet`
assertTrue "device prefix (Tunnel=ethernet)" \
"[ x$device_prefix = x'tap' ]"
}
# test device prefix Tunnel=point-to-point
test_ssh_device_prefix_ptp() {
source check_vpn_plugins/ssh.sh
local device_prefix=`_ssh_parse_device_prefix -o Tunnel=point-to-point`
assertTrue "device prefix (Tunnel=point-to-point)" \
"[ x$device_prefix = x'tun' ]"
}
##################
# SETUP/TEARDOWN #
##################
oneTimeSetUp() {
CHECK_VPN=`dirname $0`/check_vpn
}
oneTimeTearDown() {
true
}
setUp() {
true
}
tearDown() {
true
}
# load and run shUnit2
. /usr/share/shunit2/shunit2