2016-04-06 07:06:02 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2017-05-13 17:52:47 +00:00
|
|
|
SERV_IP=$(ip -4 -o addr show scope global | awk '{print $4}' | sed -e 's:/.*::' | head -n1)
|
|
|
|
SERVER_CONF="/etc/openvpn/openvpn.conf"
|
|
|
|
TEST1_OVPN="/etc/openvpn/test1.ovpn"
|
|
|
|
|
2016-04-06 07:06:02 +00:00
|
|
|
# Function to fail
|
|
|
|
abort() { cat <<< "$@" 1>&2; exit 1; }
|
|
|
|
|
2017-05-13 17:52:47 +00:00
|
|
|
# Check a config (haystack) for a given line (needle) exit with error if not found.
|
|
|
|
test_config() {
|
|
|
|
|
|
|
|
local needle="${2}"
|
|
|
|
local file="${1}"
|
|
|
|
|
|
|
|
busybox grep -q "${needle}" "${file}"
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
abort "==> Config match not found: ${needle}"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check a config (haystack) for absence of given line (needle) exit with error
|
|
|
|
# if found.
|
|
|
|
test_not_config() {
|
|
|
|
|
|
|
|
local needle="${2}"
|
|
|
|
local file="${1}"
|
|
|
|
|
|
|
|
busybox grep -vq "${needle}" "${file}"
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
abort "==> Config match found: ${needle}"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-04-06 07:06:02 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Generate openvpn.config file
|
|
|
|
#
|
2016-09-20 16:01:56 +00:00
|
|
|
read -d '' MULTILINE_EXTRA_SERVER_CONF << EOF
|
2016-05-17 15:42:22 +00:00
|
|
|
management localhost 7505
|
|
|
|
max-clients 10
|
|
|
|
EOF
|
|
|
|
|
2017-05-13 17:52:47 +00:00
|
|
|
ovpn_genconfig \
|
|
|
|
-u udp://$SERV_IP \
|
|
|
|
-f 1400 \
|
|
|
|
-k '60 300' \
|
|
|
|
-e "$MULTILINE_EXTRA_SERVER_CONF" \
|
|
|
|
-e 'duplicate-cn' \
|
|
|
|
-e 'topology subnet' \
|
|
|
|
-p 'route 172.22.22.0 255.255.255.0' \
|
|
|
|
|
2016-04-06 07:06:02 +00:00
|
|
|
|
|
|
|
#
|
2017-05-13 17:52:47 +00:00
|
|
|
# Simple test cases
|
2016-04-06 07:06:02 +00:00
|
|
|
#
|
|
|
|
|
|
|
|
# 1. verb config
|
2017-05-13 17:52:47 +00:00
|
|
|
test_config "${SERVER_CONF}" "^verb\s\+3"
|
2016-04-06 07:06:02 +00:00
|
|
|
|
|
|
|
# 2. fragment config
|
2017-05-13 17:52:47 +00:00
|
|
|
test_config "${SERVER_CONF}" "^fragment\s\+1400"
|
2016-04-06 07:06:02 +00:00
|
|
|
|
2016-09-20 16:01:56 +00:00
|
|
|
## Tests for extra configs
|
2016-05-17 15:42:22 +00:00
|
|
|
# 3. management config
|
2017-05-13 17:52:47 +00:00
|
|
|
test_config "${SERVER_CONF}" "^management\s\+localhost\s\+7505"
|
2016-05-17 15:42:22 +00:00
|
|
|
|
|
|
|
# 4. max-clients config
|
2017-05-13 17:52:47 +00:00
|
|
|
test_config "${SERVER_CONF}" "^max-clients\s\+10"
|
2016-04-06 07:06:02 +00:00
|
|
|
|
2016-09-20 16:01:56 +00:00
|
|
|
# 5. duplicate-cn config
|
2017-05-13 17:52:47 +00:00
|
|
|
test_config "${SERVER_CONF}" "^duplicate-cn"
|
2016-09-20 16:01:56 +00:00
|
|
|
|
|
|
|
# 6. topology config
|
2017-05-13 17:52:47 +00:00
|
|
|
test_config "${SERVER_CONF}" "^topology\s\+subnet"
|
2016-09-20 16:01:56 +00:00
|
|
|
|
2016-09-22 20:02:59 +00:00
|
|
|
## Tests for push config
|
|
|
|
# 7. push route
|
2017-05-13 17:52:47 +00:00
|
|
|
test_config "${SERVER_CONF}" '^push\s\+"route\s\+172.22.22.0\s\+255.255.255.0"'
|
2016-09-22 20:02:59 +00:00
|
|
|
|
|
|
|
## Test for default
|
|
|
|
# 8. Should see default route if none provided
|
2017-05-13 17:52:47 +00:00
|
|
|
test_config "${SERVER_CONF}" "^route\s\+192.168.254.0\s\+255.255.255.0"
|
2016-09-22 20:02:59 +00:00
|
|
|
|
|
|
|
# 9. Should see a push of 'block-outside-dns' by default
|
2017-05-13 17:52:47 +00:00
|
|
|
test_config "${SERVER_CONF}" '^push\s\+"block-outside-dns"'
|
2016-09-22 20:02:59 +00:00
|
|
|
|
2016-09-22 22:12:45 +00:00
|
|
|
# 10. Should see a push of 'dhcp-option DNS' by default
|
2017-05-13 17:52:47 +00:00
|
|
|
test_config "${SERVER_CONF}" '^push\s\+"dhcp-option\s\+DNS\s\+8.8.8.8"'
|
|
|
|
test_config "${SERVER_CONF}" '^push\s\+"dhcp-option\s\+DNS\s\+8.8.4.4"'
|
2016-09-22 22:12:45 +00:00
|
|
|
|
2017-03-10 01:58:46 +00:00
|
|
|
## Test for keepalive
|
|
|
|
# 11. keepalive config
|
2017-05-13 17:52:47 +00:00
|
|
|
test_config "${SERVER_CONF}" '^keepalive\s\+60\s\+300'
|
2017-03-10 01:58:46 +00:00
|
|
|
|
2016-09-22 20:02:59 +00:00
|
|
|
|
2016-04-06 07:06:02 +00:00
|
|
|
#
|
2017-05-13 17:52:47 +00:00
|
|
|
# More elaborate route tests
|
2016-04-06 07:06:02 +00:00
|
|
|
#
|
|
|
|
|
2016-09-22 20:02:59 +00:00
|
|
|
ovpn_genconfig -u udp://$SERV_IP -r "172.33.33.0/24" -r "172.34.34.0/24"
|
|
|
|
|
2017-05-13 17:52:47 +00:00
|
|
|
test_config "${SERVER_CONF}" "^route\s\+172.33.33.0\s\+255.255.255.0"
|
|
|
|
test_config "${SERVER_CONF}" "^route\s\+172.34.34.0\s\+255.255.255.0"
|
2016-09-22 20:02:59 +00:00
|
|
|
|
|
|
|
|
2017-05-13 17:52:47 +00:00
|
|
|
#
|
|
|
|
# Block outside DNS test
|
|
|
|
#
|
2017-02-18 21:53:35 +00:00
|
|
|
|
2017-03-09 23:04:21 +00:00
|
|
|
ovpn_genconfig -u udp://$SERV_IP -b
|
|
|
|
|
2017-05-13 17:52:47 +00:00
|
|
|
test_not_config "${SERVER_CONF}" '^push "block-outside-dns"'
|
|
|
|
cat ${SERVER_CONF} >&1
|