diff --git a/.gitignore b/.gitignore index ca75d9a..cfef8af 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,10 @@ *.sdf *.status *.tar.* +*.gcov +*.gcda +*.gcno +*.html *~ .DS_Store .deps diff --git a/.jenkins.sh b/.jenkins.sh new file mode 100755 index 0000000..dc87b3c --- /dev/null +++ b/.jenkins.sh @@ -0,0 +1,37 @@ +#!/bin/bash + +result=0 + +function run_test { + printf '\e[0;36m' + echo "running test: $command $@" + printf '\e[0m' + + $command "$@" + status=$? + if [ $status -ne 0 ]; then + printf '\e[0;31m' + echo "test failed: $command $@" + printf '\e[0m' + echo + result=1 + else + printf '\e[0;32m' + echo OK + printf '\e[0m' + echo + fi + return 0 +} + +run_test ./autogen.sh +run_test ./configure --enable-debug +run_test make +run_test tests/test.py tests/google.com +run_test tests/test.py tests/facebook.com +run_test tests/test.py tests/twitter.com + +gcov src/*.c +cd src && gcovr -r . --html --html-details -o index.html + +exit $result diff --git a/.travis.yml b/.travis.yml index 0bce413..858389a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,8 +9,9 @@ compiler: - gcc before_script: + - pip install gcovr - ./autogen.sh - ./configure script: - - make + - ./.jenkins.sh diff --git a/configure.ac b/configure.ac index c086e65..64bb271 100644 --- a/configure.ac +++ b/configure.ac @@ -33,7 +33,7 @@ AC_CHECK_FUNCS([inet_ntoa memset select socket strchr strdup strrchr]) AC_ARG_ENABLE([debug], [ --enable-debug build with additional debugging code], - [CFLAGS='-g -DDEBUG']) + [CFLAGS='-g -DDEBUG -fprofile-arcs -ftest-coverage -O0']) AM_CONDITIONAL(DEBUG, test x"$debug" = x"true") diff --git a/src/chinadns.c b/src/chinadns.c index 765b7a2..34ddade 100644 --- a/src/chinadns.c +++ b/src/chinadns.c @@ -157,6 +157,12 @@ static const char *help_message = #ifdef DEBUG #define DLOG(s...) LOG(s) +void __gcov_flush(void); +static void gcov_handler(int signum) +{ + __gcov_flush(); + exit(1); +} #else #define DLOG(s...) #endif @@ -165,6 +171,10 @@ int main(int argc, char **argv) { fd_set readset, errorset; int max_fd; +#ifdef DEBUG + signal(SIGTERM, gcov_handler); +#endif + memset(&id_addr_queue, 0, sizeof(id_addr_queue)); memset(&delay_queue, 0, sizeof(delay_queue)); if (0 != parse_args(argc, argv)) diff --git a/tests/facebook.com b/tests/facebook.com new file mode 100644 index 0000000..d608bbc --- /dev/null +++ b/tests/facebook.com @@ -0,0 +1 @@ +dig @127.0.0.1 a facebook.com diff --git a/tests/google.com b/tests/google.com new file mode 100644 index 0000000..1008740 --- /dev/null +++ b/tests/google.com @@ -0,0 +1 @@ +dig @127.0.0.1 a google.com diff --git a/tests/iplist.py b/tests/iplist.py new file mode 100644 index 0000000..9c60ec0 --- /dev/null +++ b/tests/iplist.py @@ -0,0 +1,15 @@ +#!/usr/bin/python + +from subprocess import Popen, PIPE + +if __name__ == '__main__': + result = set() + for i in range(400, 1400): + p = Popen((('dig +short @114.114.114.114 a ' + 'r%d-1.googlevideo.com') % i).split(), + stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True) + output = p.stdout.read() + if output: + result.add(output.strip()) + for r in result: + print r diff --git a/tests/test.py b/tests/test.py new file mode 100755 index 0000000..bafa7ed --- /dev/null +++ b/tests/test.py @@ -0,0 +1,34 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +import sys +import os +import signal +import time +from subprocess import Popen + +chinadns = ['src/chinadns', '-l', 'iplist.txt', '-c', 'chnroute.txt', + '-p', '15353', '-v'] + +p1 = Popen(chinadns, shell=False, bufsize=0, close_fds=True) + +with open(sys.argv[-1]) as f: + dig_cmd = f.read() + +time.sleep(1) + +p2 = Popen(dig_cmd.split() + ['-p', '15353'], shell=False, + bufsize=0, close_fds=True) + +if p2 is not None: + r = p2.wait() +if r == 0: + print 'test passed' + +for p in [p1]: + try: + os.kill(p.pid, signal.SIGTERM) + except OSError: + pass + +sys.exit(r) diff --git a/tests/twitter.com b/tests/twitter.com new file mode 100644 index 0000000..fd70fcc --- /dev/null +++ b/tests/twitter.com @@ -0,0 +1 @@ +dig @127.0.0.1 a twitter.com diff --git a/tests/www.facebook.com b/tests/www.facebook.com new file mode 100644 index 0000000..1b58113 --- /dev/null +++ b/tests/www.facebook.com @@ -0,0 +1 @@ +dig @127.0.0.1 a www.facebook.com