11#! /bin/bash
22
3- TMPDIR=" ./tmp"
4- FORCE=0
5- VERBOSE=1
3+ TMPDIR=" ./tmp" # the default work directory is a local tmp (exclude by .gitignore )
4+
5+ FORCE=0 # force whois loogup by not using cached data
6+ VERBOSE=0 # along the way inform us on progress and results
67
78prepTempDir ()
89{
10+ # make the work dir if it does nt exist
911 mkdir -p " $TMPDIR " || {
1012 echo " FATAL: cannot make test dir: $TMPDIR " >&2
1113 exit 101
@@ -14,49 +16,152 @@ prepTempDir()
1416
1517getAllTldSupported ()
1618{
19+ # get all currently supported tld's
1720 ./test2.py -S
1821}
1922
20- makeTestDataOriginalOne ()
23+ testNameserverExistsInInputAndOutput ()
24+ {
25+ local tld=" $1 "
26+ local d=" $TMPDIR /$tld "
27+ local dns=" $d /__dns-ns"
28+
29+ [ -s " $dns " ] || return # dont bother at all if we have no dns file
30+
31+ rm -f " $d /error.ns"
32+
33+ cat " $dns " |
34+ awk ' { print $NF }' |
35+ while read ns
36+ do
37+ [ -s " $d /input" ] || return # dont bother if we have no input file
38+
39+ grep -q -i " $ns " " $d /input" && {
40+ # only test in the output if it is present in the input
41+ grep -q -i " $ns " " $d /output" || {
42+ echo " ERROR: output; missing nameserver '$ns ' for tld: $tld " |
43+ tee -a " $d /error.ns"
44+ }
45+ }
46+ done
47+ }
48+
49+ cleanupTldTestDirectory ()
2150{
2251 local tld=" $1 "
2352 local domain=" $2 "
53+ local d=" $TMPDIR /$tld "
54+ local zz=" $domain .$tld "
2455
56+ rm -f " $d /input" " $d /output" " $d /__domain__$zz " " $d /__dns-soa" " $d /__dns-ns"
57+ }
58+
59+ getTestDataInputForTldAndDomain ()
60+ {
61+ local tld=" $1 "
62+ local domain=" $2 "
2563 local d=" $TMPDIR /$tld "
26- mkdir -p " $d " || {
27- echo " FATAL: cannot make tld directory: '$d '" >&2
28- exit 101
64+ local zz=" $domain .$tld "
65+
66+ # make the testing input data
67+ # dont overwire the input file unless FORCE is requested
68+
69+ [ " $FORCE " == " 1" ] && {
70+ rm -f " $d /input"
71+ }
72+
73+ [ -s " $d /input" ] || {
74+ # for whois force english, force no cache
75+ LANG=EN whois --force-lookup " meta.$tld " > " $d /input" || {
76+ # whois has a problem
77+ local ret=$?
78+ echo " ERROR: whois returns $ret for domain: $zz " >&2
79+ cat " $d /input" >&2
80+ cleanupTldTestDirectory " $tld " " $domain "
81+ return 1
82+ }
2983 }
3084
85+ # parse the input and annotate it and split the body in sections
86+ ./test2.py -C " $d /input" > " $d /input.out"
87+ }
88+
89+ getTestDataOutputForTldAndDomain ()
90+ {
91+ local tld=" $1 "
92+ local domain=" $2 "
93+ local d=" $TMPDIR /$tld "
94+
95+ # make the testing output data
96+ # dont overwrite the output file unless FORCE is requested
97+ [ " $FORCE " == " 1" ] && {
98+ rm -f " $d /output"
99+ }
100+
101+ [ -s " $d /output" ] || {
102+ ./test2.py -d " $domain .$tld " > " $d /output"
103+ }
104+ }
105+
106+ getDnsSoaRecordAndLeaveEvidenceTldDomain ()
107+ {
108+ local tld=" $1 "
109+ local domain=" $2 "
110+ local d=" $TMPDIR /$tld "
31111 local zz=" $domain .$tld "
32112
113+ [ -f " $d /_NO_SOA_$zz " ] && return 2
114+
115+ # get the soa record , if it exists proceed otherwise ignore this domain
116+ # along the way we store the raw soa record also
33117 host -t soa " $zz " |
34- tee " $d /__dns-soa " |
35- grep " has SOA record " || {
118+ tee " $d /__dns-soa__ $zz " |
119+ grep -q " has SOA record " || {
36120 # no soa record so that domain does not exist, cleanup the test dir
37- rm -f " $d /input" " $d /output" " $d /__domain__$zz " " $d /__dns-soa" " $d /dns-ns"
38- echo " INFO: no domain for $zz " >&2
121+ cleanupTldTestDirectory " $tld " " $domain "
122+ echo " WARN: no SOA for domain: $zz " >&2
123+ > " $d /_NO_SOA_$zz "
39124 return 1
40125 }
126+ }
127+
128+ makeDirectoryForTld ()
129+ {
130+ local tld=" $1 "
131+ local domain=" $2 "
132+ local d=" $TMPDIR /$tld "
133+
134+ mkdir -p " $d " || {
135+ echo " FATAL: cannot make tld directory: '$d '" >&2
136+ exit 101
137+ }
138+ }
139+
140+ makeTestDataOriginalOneTldDomain ()
141+ {
142+ # see if we have a proper soa record for the domain before attempting to run whois
143+ # collect the raw whois data in input, the test2 data in output
144+ # and get the dns nameservers for later comparison,
145+ # for many tld patterns we have no nameserver info although they are in the input,
146+ # we may need to improve the parsing of the whois nameservers
147+ # or skip them alltogether and use dns diectly for nameservers
41148
42- host -t ns " $zz " > " $d /__dns-ns"
149+ local tld=" $1 "
150+ local domain=" $2 "
151+ local d=" $TMPDIR /$tld "
152+ local zz=" $domain .$tld "
153+
154+ getDnsSoaRecordAndLeaveEvidenceTldDomain " $tld " " $domain " || return 1
43155
44156 # what domain did we test
45157 touch " $d /__domain__$zz "
46158
47- # force english, force no cache
48- [ ! -s " $d /input" -o " $FORCE " = " 1" ] && {
49- LANG=EN whois --force-lookup " meta.$tld " > " $d /input" || {
50- # whois has a problem
51- rm -f " $d /input" " $d /output" " $d /__domain__$zz " " $d /__dns-soa" " $d /dns-ns"
52- return 1
53- }
54- }
159+ # store the nameservers from dns
160+ host -t ns " $zz " > " $d /__dns-ns__$zz "
55161
56- [ ! -s " $d /output" -o " $FORCE " = " 1" ] && {
57- ./test2.py -d " meta.$tld " > " $d /output"
58- }
162+ getTestDataInputForTldAndDomain " $tld " " $domain " || return 1
59163
164+ getTestDataOutputForTldAndDomain " $tld " " $domain "
60165 return 0
61166}
62167
@@ -74,31 +179,68 @@ google
74179 '
75180}
76181
77- makeTestDataOriginalAll ()
182+ makeTestDataTldFromDomains ()
183+ {
184+ local tld=" $1 "
185+
186+ domainsToTry |
187+ while read domain
188+ do
189+ [ " $VERBOSE " = " 1" ] && echo " try: $domain .$tld "
190+
191+ makeTestDataOriginalOneTldDomain " $tld " " $domain "
192+
193+ [ -s " $TMPDIR /$tld /input" ] && {
194+ [ " $VERBOSE " = " 1" ] && ls -l " $TMPDIR /$tld /"
195+ testNameserverExistsInInputAndOutput " $tld " && break
196+ }
197+ done
198+ }
199+
200+ makeRulesFromTldIfExist ()
201+ {
202+ local tld=" $1 "
203+ local tld2=$( echo $tld | tr " ." " _" ) # here we need to replace all . into _
204+
205+ ./test2.py -R -d " $tld2 " > " $TMPDIR /$tld /__rules__"
206+ [ -s " $TMPDIR /$tld /__rules__" ]
207+ }
208+
209+ makeTestDataOriginalOneTld ()
210+ {
211+ local tld=" $1 "
212+
213+ [ " $VERBOSE " = " 1" ] && echo " try: $tld "
214+
215+ makeDirectoryForTld " $tld " " $domain " || exit 101
216+ makeRulesFromTldIfExist " $tld "
217+ makeTestDataTldFromDomains " $tld "
218+ }
219+
220+ makeTestDataOriginalAllTldSupported ()
78221{
79222 getAllTldSupported |
223+ sort |
80224 while read tld
81225 do
82- echo " try: $tld "
83-
84- domainsToTry |
85- while read domain
86- do
87- makeTestDataOriginalOne " $tld " " $domain "
88- [ -f " $TMPDIR /$tld /input" ] && {
89- [ " $VERBOSE " ] && {
90- ls -l " $TMPDIR /$tld /"
91- }
92- break
93- }
94- done
226+ makeTestDataOriginalOneTld " $tld "
95227 done
96228}
97229
98230main ()
99231{
100232 prepTempDir
101- makeTestDataOriginalAll
233+
234+ [ " $# " = " 0" ] && {
235+ makeTestDataOriginalAllTldSupported
236+ return
237+ }
238+
239+ for tld in $*
240+ do
241+ makeTestDataOriginalOneTld " $tld "
242+ done
102243}
103244
104- main
245+ main $* 2>&1 |
246+ tee out
0 commit comments