#!/bin/sh
# 20221231

set -e

dir=`dirname "$0"`

# change directory to $AUTOPKGTEST_TMP
cd "${AUTOPKGTEST_TMP}"

echo "=== running fakedns ==="
curvednspk=`grep '^uz5' /etc/curvedns/README  | cut -d' ' -f1`
${dir}/fakedns.py "${curvednspk}" 2>fakedns.log &
fakednspid=$!
echo

cleanup() {
  ex=$?
  # kill fakedns
  kill -TERM "${fakednspid}" 1>/dev/null 2>/dev/null || :
  sleep 0.1
  kill -KILL "${fakednspid}" 1>/dev/null 2>/dev/null || :
  # reconfigure curvedns
  mv -f /etc/default/curvedns.bk /etc/default/curvedns
  /etc/init.d/curvedns stop 1>/dev/null 2>/dev/null
  # reconfigure dqcache
  cat /var/lib/dqcache/root/servers/.@ > /var/lib/dqcache/root/servers/@ || :
  killall -HUP dqcache || :
  killall -INT dqcache || :
  # print logs
  if [ "${ex}" -ne 0 ]; then
    (
      exec >&2
      echo "dqcache test failed !!!"
      echo
      echo "fakedns.log:"
      cat fakedns.log || :
      echo
      echo "systemctl status curvedns:"
      systemctl status curvedns || :
      echo
      echo "systemctl status dqcache:"
      systemctl status dqcache || :
      echo
      echo "journalctl -x --no-tail --no-pager -u curvedns.service:"
      journalctl -x --no-tail --no-pager -u curvedns.service || :
    )
  fi
  echo "journalctl -x --no-tail --no-pager -u dqcache.service:"
  journalctl -x --no-tail --no-pager -u dqcache.service || :
  echo
  rm -f *.log
  exit "${ex}"
}
trap "cleanup" EXIT TERM INT

echo '=== reconfigure curvedns ==='
cp /etc/default/curvedns /etc/default/curvedns.bk
sed -i 's/IP=.*/IP=127.0.0.4/' /etc/default/curvedns
sed -i 's/REMOTEIP=.*/REMOTEIP=127.0.0.3/' /etc/default/curvedns
systemctl reset-failed curvedns.service || :
systemctl --force restart curvedns.service
echo

echo '=== reconfigure + restart dqcache  ==='
echo '127.0.0.2' > '/var/lib/dqcache/root/servers/@'
systemctl reset-failed dqcache.service || :
systemctl --force restart dqcache.service
echo

# wait for dqcache to start
sleep 1

echo '=== dq query to dqcache server ==='
dq a localhost 127.0.0.1
echo

echo '=== dq query to fakeroot server ==='
dq a a.a 127.0.0.2 1>dq.log 2>&1
grep '^answer: \|^additional: \|^authority: ' dq.log
echo

echo '=== dq query to fakedns server ==='
dq a a.a 127.0.0.3 1>dq.log 2>&1
grep '^answer: \|^additional: \|^authority: ' dq.log
echo

echo '=== dq query to curvedns server ==='
dq a a.a 127.0.0.4 1>dq.log 2>&1
grep '^answer: \|^additional: \|^authority: ' dq.log
echo

echo '=== dq encrypted query to curvedns server ==='
dq -k "${curvednspk}" a a.a 127.0.0.4 1>dq.log 2>&1
grep '^answer: \|^additional: \|^authority: ' dq.log
echo

echo '=== dq query to dqcache server ==='
dq a a.a 127.0.0.1 1>dq.log 2>&1
grep '^answer: \|^additional: \|^authority: ' dq.log
echo

echo '=== main part ==='
domainnames="a b c d e f g h i j k l m n o p q r s t u v w x y z"

# send queries
for x in ${domainnames}; do
  for y in ${domainnames}; do
    name="$x.$y.$z"
    dq a "${name}" 127.0.0.1 1>"${name}.log" 2>&1
    if ! grep '^answer: ' "${name}.log"; then
      exit 2
    fi
  done
done

# stop fakedns, dqcache should have all queries cached
kill -TERM "${fakednspid}" 1>/dev/null 2>/dev/null || :
kill -KILL "${fakednspid}" 1>/dev/null 2>/dev/null || :

# send queries again and parallel
for x in ${domainnames}; do
  for y in ${domainnames}; do
    (
      name="$x.$y.$z"
      dq a "${name}" 127.0.0.1 1>"${name}.log" 2>&1
      if ! grep '^answer: ' "${name}.log"; then
        exit 3
      fi
    ) &
  done
  wait
done
exit 0
