#!/bin/sh -xe

test "$TEST_DNSPERF_WITH_NETWORK" = "1" || exit 0

dumdumd=`which dumdumd`
dumdohd=`which dumdohd`

pkill -9 dumdumd || true
pkill -9 dumdohd || true

if [ -n "$dumdumd" ]; then
    rm -f key.pem cert.pem
    openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes -subj "/C=AU/ST=Some-State/O=Internet Widgits Pty Ltd"

    $dumdumd 127.0.0.1 5353 -r -T &
    pid="$!"
    sleep 2
    ../dnsperf -s 127.0.0.1 -p 5353 -d "$srcdir/datafile" -t 2 -l 10 -Q 100 -m dot -O tls-session-resumption -O num-queries-per-conn=10 > test_tls_resum.log
    kill "$pid"
    wait "$pid" || true
    resumed=`grep "TLS sessions resumed:" test_tls_resum.log | awk '{print $4}'`
    test "$resumed" -gt "0"
    rm test_tls_resum.log

    $dumdumd 127.0.0.1 5353 -r -T -o no-ssl-cache &
    pid="$!"
    sleep 2
    ../dnsperf -s 127.0.0.1 -p 5353 -d "$srcdir/datafile" -t 2 -l 10 -Q 100 -m dot -O tls-session-resumption -O num-queries-per-conn=10 > test_tls_resum.log
    kill "$pid"
    wait "$pid" || true
    resumed=`grep "TLS sessions resumed:" test_tls_resum.log | awk '{print $4}'`
    test "$resumed" -eq "0"
    rm test_tls_resum.log
fi

if [ -n "$dumdohd" ]; then
    rm -f key.pem cert.pem
    openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes -subj "/C=AU/ST=Some-State/O=Internet Widgits Pty Ltd"

    $dumdohd 5354 key.pem cert.pem &
    pid="$!"
    sleep 2
    ../dnsperf -s 127.0.0.1 -p 5354 -d "$srcdir/datafile" -t 2 -l 10 -Q 100 -m doh -O tls-session-resumption -O num-queries-per-conn=10 > test_tls_resum.log
    kill "$pid"
    wait "$pid" || true
    resumed=`grep "TLS sessions resumed:" test_tls_resum.log | awk '{print $4}'`
    test "$resumed" -gt "0"
    rm test_tls_resum.log

    $dumdohd 5354 key.pem cert.pem -o no-ssl-cache &
    pid="$!"
    sleep 2
    ../dnsperf -s 127.0.0.1 -p 5354 -d "$srcdir/datafile" -t 2 -l 10 -Q 100 -m doh -O tls-session-resumption -O num-queries-per-conn=10 > test_tls_resum.log
    kill "$pid"
    wait "$pid" || true
    resumed=`grep "TLS sessions resumed:" test_tls_resum.log | awk '{print $4}'`
    test "$resumed" -eq "0"
    rm test_tls_resum.log
fi
