| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
# test suite for hddemux |
| 4 |
|
| 5 |
# requires: |
| 6 |
# - nginx |
| 7 |
# - knot-resolver |
| 8 |
# - kdig (from knot-dnsutils) |
| 9 |
# - curl |
| 10 |
# - certtool (from gnutls-bin) |
| 11 |
|
| 12 |
# environment variables: |
| 13 |
# - WORKDIR: a place for all generated files. |
| 14 |
# if unset, it will be auto-generated. |
| 15 |
# it will be created as needed. |
| 16 |
# if the directory doesn't currently exist, it will be cleaned up at exit. |
| 17 |
# if it already exists, it will not be cleaned up. |
| 18 |
# - TESTIP: the IP address to use for testing. |
| 19 |
# the user needs to be able to open listening sockets, and to connect to them |
| 20 |
# by default, choose a random IP in 127.0.0.0/8 |
| 21 |
|
| 22 |
# Author: Daniel Kahn Gillmor <[email protected]> |
| 23 |
# 2018-08-29 |
| 24 |
# License: GPLv3+ |
| 25 |
|
| 26 |
# error on exit |
| 27 |
set -e |
| 28 |
# for handling jobspecs: |
| 29 |
set -m |
| 30 |
|
| 31 |
# Unset proxy to make sure curl behaves correctly |
| 32 |
unset https_proxy http_proxy |
| 33 |
|
| 34 |
hddemux=$(which hddemux) || hddemux=./hddemux |
| 35 |
|
| 36 |
[ -x "$hddemux" ] |
| 37 |
|
| 38 |
if [ -z "$WORKDIR" ]; then |
| 39 |
d="$(mktemp -d)" |
| 40 |
remove="$d" |
| 41 |
else |
| 42 |
d="$WORKDIR" |
| 43 |
fi |
| 44 |
ip="${TESTIP:-127.$(( $RANDOM % 256 )).$(( $RANDOM % 256 )).$(( $RANDOM % 256 ))}" |
| 45 |
|
| 46 |
printf "hddemux test\n------------\n binary: %s\n workdir: %s\n IP addr: %s\n" "$hddemux" "$d" "$ip" |
| 47 |
|
| 48 |
section() { |
| 49 |
printf "\n%s\n" "$1" |
| 50 |
sed 's/./-/g' <<<"$1" |
| 51 |
} |
| 52 |
|
| 53 |
cleanup () { |
| 54 |
section "cleaning up" |
| 55 |
find "$d" -ls |
| 56 |
tail "$d/"*.err |
| 57 |
/usr/sbin/nginx -c "$d/nginx.conf" -p "$d" -s stop 2> "$d/nginx-stop.err" || true |
| 58 |
kill %2 || true |
| 59 |
kill %1 || true |
| 60 |
if [ "$remove" ]; then |
| 61 |
printf "cleaning up working directory %s\n" "$remove" |
| 62 |
rm -rf "$remove" |
| 63 |
fi |
| 64 |
} |
| 65 |
|
| 66 |
trap cleanup EXIT |
| 67 |
|
| 68 |
|
| 69 |
section "simple failing run" |
| 70 |
# hddemux with no arguments and no listening file descriptors should fail: |
| 71 |
if "$hddemux" 2>&1; then |
| 72 |
false |
| 73 |
fi |
| 74 |
|
| 75 |
|
| 76 |
section "make Certificate Authority key and certificate" |
| 77 |
cat > "$d/ca.template" <<EOF |
| 78 |
cn = "testing certificate authority (NOT FOR PRODUCTION)" |
| 79 |
expiration_days = 12 |
| 80 |
ca |
| 81 |
path_len = 1 |
| 82 |
nc_permit_dns = example |
| 83 |
cert_signing_key |
| 84 |
EOF |
| 85 |
certtool --stdout-info --generate-privkey --outfile "$d/ca-key.pem" |
| 86 |
certtool --stdout-info --generate-self-signed --template "$d/ca.template" --load-privkey "$d/ca-key.pem" --outfile "$d/ca-cert.pem" |
| 87 |
|
| 88 |
section "make End Entity key and certificate" |
| 89 |
cat > "$d/ee.template" <<EOF |
| 90 |
cn = "test.example" |
| 91 |
dns_name = test.example |
| 92 |
expiration_days = 10 |
| 93 |
signing_key |
| 94 |
tls_www_server |
| 95 |
EOF |
| 96 |
certtool --stdout-info --generate-privkey --outfile "$d/ee-key.pem" |
| 97 |
certtool --stdout-info --pubkey-info --load-privkey "$d/ee-key.pem" --outfile "$d/ee-pubkey.pem" |
| 98 |
certtool --stdout-info --generate-certificate --load-ca-privkey "$d/ca-key.pem" --load-ca-certificate "$d/ca-cert.pem" --template "$d/ee.template" --load-pubkey "$d/ee-pubkey.pem" --outfile "$d/ee-cert.pem" |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
section "make knot-resolver configuration on $ip:8853" |
| 103 |
cat > "$d/kresd.conf" <<EOF |
| 104 |
modules = { 'hints > iterate' } |
| 105 |
net.tls("$d/ee-cert.pem", "$d/ee-key.pem") |
| 106 |
hints["monkeys.example"] = "127.15.23.5" |
| 107 |
EOF |
| 108 |
/usr/sbin/kresd --config "$d/kresd.conf" --tls "$ip@8853" --noninteractive "$d" 2> "$d/kresd.err" & |
| 109 |
|
| 110 |
section "make hddemux configuration on $ip:2000" |
| 111 |
systemd-socket-activate -l "$ip:2000" -E HTTP_TARGET="$ip:8853" -E DNS_TARGET="$ip:8853" "$hddemux" 2> "$d/hddemux.err" & |
| 112 |
|
| 113 |
section "set up nginx on $ip:4433" |
| 114 |
mkdir -p "$d/nginx" |
| 115 |
cat >"$d/nginx.conf" <<EOF |
| 116 |
# note: an alert might still show up: https://trac.nginx.org/nginx/ticket/147 |
| 117 |
error_log stderr; |
| 118 |
worker_processes 1; |
| 119 |
pid nginx.pid; |
| 120 |
|
| 121 |
events { |
| 122 |
worker_connections 10; |
| 123 |
} |
| 124 |
http { |
| 125 |
default_type text/plain; |
| 126 |
ssl_protocols TLSv1.2; |
| 127 |
ssl_prefer_server_ciphers on; |
| 128 |
client_body_temp_path nginx/body; |
| 129 |
fastcgi_temp_path nginx/fastcgi; |
| 130 |
proxy_temp_path nginx/proxy; |
| 131 |
scgi_temp_path nginx/scgi; |
| 132 |
uwsgi_temp_path nginx/uwsgi; |
| 133 |
server { |
| 134 |
listen $ip:4433 ssl; |
| 135 |
server_name test.example; |
| 136 |
ssl_certificate ee-cert.pem; |
| 137 |
ssl_certificate_key ee-key.pem; |
| 138 |
access_log access.log; |
| 139 |
location / { |
| 140 |
root data; |
| 141 |
index index.txt; |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
EOF |
| 146 |
mkdir -p "$d/data" |
| 147 |
echo "Hello, world!" > "$d/data/index.txt" |
| 148 |
/usr/sbin/nginx -c "$d/nginx.conf" -p "$d" 2> "$d/nginx.err" |
| 149 |
|
| 150 |
section "test with kdig" |
| 151 |
x=$(kdig +short +tls +tls-ca="$d/ca-cert.pem" +tls-hostname=test.example @"$ip:2000" monkeys.example) |
| 152 |
[ "$x" = "127.15.23.5" ] |
| 153 |
echo "successful DNS-over-TLS request to $ip on port 2000" |
| 154 |
|
| 155 |
section "test with curl" |
| 156 |
x=$(curl --silent --show-error --cacert "$d/ca-cert.pem" --resolve "test.example:2000:$ip" --resolve "test.example:4433:$ip" https://test.example:4433/) |
| 157 |
[ "$x" = "Hello, world!" ] |
| 158 |
echo "successful HTTPS request to $ip on port 2000" |