Skip to content

Commit 571a348

Browse files
committed
add coverage
1 parent 68a5b16 commit 571a348

11 files changed

+107
-2
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
*.sdf
1111
*.status
1212
*.tar.*
13+
*.gcov
14+
*.gcda
15+
*.gcno
16+
*.html
1317
*~
1418
.DS_Store
1519
.deps

.jenkins.sh

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/bin/bash
2+
3+
result=0
4+
5+
function run_test {
6+
printf '\e[0;36m'
7+
echo "running test: $command $@"
8+
printf '\e[0m'
9+
10+
$command "$@"
11+
status=$?
12+
if [ $status -ne 0 ]; then
13+
printf '\e[0;31m'
14+
echo "test failed: $command $@"
15+
printf '\e[0m'
16+
echo
17+
result=1
18+
else
19+
printf '\e[0;32m'
20+
echo OK
21+
printf '\e[0m'
22+
echo
23+
fi
24+
return 0
25+
}
26+
27+
run_test ./autogen.sh
28+
run_test ./configure --enable-debug
29+
run_test make
30+
run_test tests/test.py tests/google.com
31+
run_test tests/test.py tests/facebook.com
32+
run_test tests/test.py tests/twitter.com
33+
34+
gcov src/*.c
35+
cd src && gcovr -r . --html --html-details -o index.html
36+
37+
exit $result

.travis.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ compiler:
99
- gcc
1010

1111
before_script:
12+
- pip install gcovr
1213
- ./autogen.sh
1314
- ./configure
1415

1516
script:
16-
- make
17+
- ./.jenkins.sh

configure.ac

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ AC_CHECK_FUNCS([inet_ntoa memset select socket strchr strdup strrchr])
3333

3434
AC_ARG_ENABLE([debug],
3535
[ --enable-debug build with additional debugging code],
36-
[CFLAGS='-g -DDEBUG'])
36+
[CFLAGS='-g -DDEBUG -fprofile-arcs -ftest-coverage -O0'])
3737

3838
AM_CONDITIONAL(DEBUG, test x"$debug" = x"true")
3939

src/chinadns.c

+10
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ static const char *help_message =
157157

158158
#ifdef DEBUG
159159
#define DLOG(s...) LOG(s)
160+
void __gcov_flush(void);
161+
static void gcov_handler(int signum)
162+
{
163+
__gcov_flush();
164+
exit(1);
165+
}
160166
#else
161167
#define DLOG(s...)
162168
#endif
@@ -165,6 +171,10 @@ int main(int argc, char **argv) {
165171
fd_set readset, errorset;
166172
int max_fd;
167173

174+
#ifdef DEBUG
175+
signal(SIGTERM, gcov_handler);
176+
#endif
177+
168178
memset(&id_addr_queue, 0, sizeof(id_addr_queue));
169179
memset(&delay_queue, 0, sizeof(delay_queue));
170180
if (0 != parse_args(argc, argv))

tests/facebook.com

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dig @127.0.0.1 a facebook.com

tests/google.com

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dig @127.0.0.1 a google.com

tests/iplist.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/python
2+
3+
from subprocess import Popen, PIPE
4+
5+
if __name__ == '__main__':
6+
result = set()
7+
for i in range(400, 1400):
8+
p = Popen((('dig +short @114.114.114.114 a '
9+
'r%d-1.googlevideo.com') % i).split(),
10+
stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
11+
output = p.stdout.read()
12+
if output:
13+
result.add(output.strip())
14+
for r in result:
15+
print r

tests/test.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
import sys
5+
import os
6+
import signal
7+
import time
8+
from subprocess import Popen
9+
10+
chinadns = ['src/chinadns', '-l', 'iplist.txt', '-c', 'chnroute.txt',
11+
'-p', '15353', '-v']
12+
13+
p1 = Popen(chinadns, shell=False, bufsize=0, close_fds=True)
14+
15+
with open(sys.argv[-1]) as f:
16+
dig_cmd = f.read()
17+
18+
time.sleep(1)
19+
20+
p2 = Popen(dig_cmd.split() + ['-p', '15353'], shell=False,
21+
bufsize=0, close_fds=True)
22+
23+
if p2 is not None:
24+
r = p2.wait()
25+
if r == 0:
26+
print 'test passed'
27+
28+
for p in [p1]:
29+
try:
30+
os.kill(p.pid, signal.SIGTERM)
31+
except OSError:
32+
pass
33+
34+
sys.exit(r)

tests/twitter.com

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dig @127.0.0.1 a twitter.com

tests/www.facebook.com

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dig @127.0.0.1 a www.facebook.com

0 commit comments

Comments
 (0)