Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a test for dns records #603

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
language: c
compiler: gcc

script: make
script:
- make
- 'if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then make checkdns; fi'

addons:
apt:
Expand All @@ -18,4 +20,5 @@ addons:
- libicu-dev
- libunistring0
- libunistring-dev
- dnsutils

5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,7 @@ libpsl-libidn2: libpsl-config

# TEST PSL data with libidn (IDNA2003)
libpsl-libidn: libpsl-config
cd libpsl && ./configure -q -C --enable-runtime=libidn --enable-builtin=libidn $(Options) && make -s clean && make -s check -j4
cd libpsl && ./configure -q -C --enable-runtime=libidn --enable-builtin=libidn $(Options) && make -s clean && make -s check -j4

make checkdns:
./tests/check_dns.sh -v
122 changes: 122 additions & 0 deletions tests/check_dns.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#!/bin/bash
#
# check the changes in the psl for correct dns entries
# we will look at the domains nameservers if possible, to aviod wating until
# changed records are propagated

# constants
REPO="https://github.com/publicsuffix/list"
PULL_REQUEST="$TRAVIS_PULL_REQUEST"
TEMPFILE=$(mktemp)

# check for arguments
DEBUG=false
TEST=false
for arg in $*; do
case $arg in
-v)
DEBUG=true
;;
--test-file=*)
TEST=true
TEST_FILE=${arg/--test-file=/}
;;
--test-pullrequest=*)
TEST=true
TEST_PULLREQUEST=${arg/--test-pullrequest=/}
;;
esac
done

if ! $TEST; then
# get only the lines added by pull request without comments
git diff master public_suffix_list.dat \
| sed -n '/^@@/,/^diff/{/^+/s/^+//p}' \
| grep -v ^// \
> $TEMPFILE
else
# use testing data
$DEBUG && echo TESTING MODE
if [ -f "$TEST_FILE" ]; then
cp "$TEST_FILE" $TEMPFILE
if grep -qE '^[0-9]+$' <<< "$TEST_PULLREQUEST"; then # if numeric
PULL_REQUEST="$TEST_PULLREQUEST"
else
echo "ERROR: test-pullrequest missing or invalid."
exit 2
fi
else
echo "ERROR: test-file missing or not a file."
exit 2
fi
fi

# ask for pullrequest if not specified
# i.e. if user is checking if he has done everything right
if [ "$TRAVIS" != "true" ] && [ "x$PULL_REQUEST" == "x" ]; then
echo -n "Pull request number not specified, please fill in: "
read PULL_REQUEST
fi



# work on domains from $TEMPFILE
while read domain; do
# kill empty lines
if [ "x$domain" == "x" ]; then
continue
fi

$DEBUG && echo "!! $domain"

# try to find nameservers for domain
# this speeds the process up a lot if there where any mistakes,
# otherwise changes would have to propagate at first.
domain_to_lookup="${domain/\*.}"
while : ; do
$DEBUG && echo " checking nameservers for $domain_to_lookup"
nameservers=$(dig +short NS "$domain_to_lookup")

if [ "x$nameservers" != "x" ]; then
break;
fi

domain_to_lookup=$(sed 's/^[^\.]*\.//' <<< "$domain_to_lookup")

# break if domain_to_lookup is not a domain
if ! grep -q '\.' <<< $domain_to_lookup; then
break;
fi
done

# or use default if none are there
nameservers="${nameservers:-"8.8.8.8 4.4.4.4"}"
$DEBUG && echo " nameservers:" $nameservers

# check domain against nameservers
expected_result="\"${REPO}/pull/${PULL_REQUEST}\""
$DEBUG && echo " expected result: $expected_result"
ok=false
for nameserver in $nameservers; do
dig_result=$(dig +short TXT "_psl.$domain_to_lookup" "$nameserver")
$DEBUG && echo " query result from $nameserver: ${dig_result:-NONE}"
if [ "$dig_result" == "$expected_result" ]; then
$DEBUG && echo " -> OK!"
ok=true
break
fi
done

# fail for issues
if ! $ok; then
echo "ERROR: $domain returned invalid TXT-record: ${dig_result:-NONE}"
exit 1
fi
done < $TEMPFILE

# cleanup
rm $TEMPFILE

# if we get to here, everything is fine
echo "OK: All new domains verified"
exit 0
2 changes: 2 additions & 0 deletions tests/check_dns.sh_tests/additions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@


2 changes: 2 additions & 0 deletions tests/check_dns.sh_tests/domain_with_incorrect_txt_record.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
virtualuser.de
*.virtual-user.de
1 change: 1 addition & 0 deletions tests/check_dns.sh_tests/domain_without_txt_record.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
publicsuffix.org
2 changes: 2 additions & 0 deletions tests/check_dns.sh_tests/everything_ok.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
virtualuser.de
*.virtual-user.de
2 changes: 2 additions & 0 deletions tests/check_dns.sh_tests/multiple_subdomains.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
this.is.a.test.with.a.lot.of.subdomains.virtualuser.de

2 changes: 2 additions & 0 deletions tests/check_dns.sh_tests/no_new_domains.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@


1 change: 1 addition & 0 deletions tests/check_dns.sh_tests/non_existing_domain.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this-domain-does-not-exist.because-the-tld-does-not-exist
27 changes: 27 additions & 0 deletions tests/check_dns.sh_tests/run_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

# test the create_dns.sh for expected output

cd $(dirname $0)
tests=$(grep -v '^#' <<EOF
# fromat:
# pullrequest number:filename:expected exitcode
600:everything_ok.txt:0
123:non_existing_domain.txt:1
123:domain_without_txt_record.txt:1
123:domain_with_incorrect_txt_record.txt:1
600:multiple_subdomains.txt:0
123:no_new_domains.txt:0
EOF
)

while IFS=':' read pullrequest file exitcode; do
echo ../check_dns.sh --test-pullrequest=$pullrequest --test-file=$file
../check_dns.sh --test-pullrequest=$pullrequest --test-file=$file > /dev/null
if [ "$?" -eq "$exitcode" ]; then
echo "-> Test OK!"
else
echo Last test failed!
exit 1
fi
done <<< $tests