Skip to content
This repository was archived by the owner on Feb 3, 2024. It is now read-only.

Commit 0498291

Browse files
committed
create test tool to help with teecting missing or wrong info
1 parent 38f874e commit 0498291

File tree

3 files changed

+277
-45
lines changed

3 files changed

+277
-45
lines changed

makeTestdataAll.sh

Lines changed: 168 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
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

78
prepTempDir()
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,142 @@ prepTempDir()
1416

1517
getAllTldSupported()
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+
[ ! -s "$d/input" -o "$FORCE" = "1" ] && {
70+
# for whois force english, force no cache
71+
# LANG=EN whois --force-lookup "meta.$tld" >"$d/input" || {
72+
whois --force-lookup "meta.$tld" >"$d/input" || {
73+
# whois has a problem
74+
local ret=$?
75+
echo "ERROR: whois returns $ret for domain: $zz" >&2
76+
cat "$d/input" >&2
77+
cleanupTldTestDirectory "$tld" "$domain"
78+
return 1
79+
}
2980
}
81+
}
3082

83+
getTestDataOutputForTldAndDomain()
84+
{
85+
local tld="$1"
86+
local domain="$2"
87+
local d="$TMPDIR/$tld"
88+
89+
# make the testing output data
90+
# dont overwrite the output file unless FORCE is requested
91+
[ ! -s "$d/output" -o "$FORCE" = "1" ] && {
92+
./test2.py -d "$domain.$tld" >"$d/output"
93+
}
94+
}
95+
96+
getDnsSoaRecordAndLeaveEvidenceTldDomain()
97+
{
98+
local tld="$1"
99+
local domain="$2"
100+
local d="$TMPDIR/$tld"
31101
local zz="$domain.$tld"
32102

103+
[ -f "$d/_NO_SOA_$zz" ] && return 2
104+
105+
# get the soa record , if it exists proceed otherwise ignore this domain
106+
# along the way we store the raw soa record also
33107
host -t soa "$zz" |
34108
tee "$d/__dns-soa" |
35-
grep " has SOA record " || {
109+
grep -q " has SOA record " || {
36110
# 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
111+
cleanupTldTestDirectory "$tld" "$domain"
112+
echo "WARN: no SOD for domain: $zz" >&2
113+
>"$d/_NO_SOA_$zz"
39114
return 1
40115
}
116+
}
41117

42-
host -t ns "$zz" > "$d/__dns-ns"
118+
makeDirectoryForTld()
119+
{
120+
local tld="$1"
121+
local domain="$2"
122+
local d="$TMPDIR/$tld"
123+
124+
mkdir -p "$d" || {
125+
echo "FATAL: cannot make tld directory: '$d'" >&2
126+
exit 101
127+
}
128+
}
129+
130+
makeTestDataOriginalOneTldDomain()
131+
{
132+
# see if we have a proper soa record for the domain before attempting to run whois
133+
# collect the raw whois data in input, the test2 data in output
134+
# and get the dns nameservers for later comparison,
135+
# for many tld patterns we have no nameserver info although they are in the input,
136+
# we may need to improve the parsing of the whois nameservers
137+
# or skip them alltogether and use dns diectly for nameservers
138+
139+
local tld="$1"
140+
local domain="$2"
141+
local d="$TMPDIR/$tld"
142+
local zz="$domain.$tld"
143+
144+
getDnsSoaRecordAndLeaveEvidenceTldDomain "$tld" "$domain" || return 1
43145

44146
# what domain did we test
45147
touch "$d/__domain__$zz"
46148

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-
}
149+
# store the nameservers from dns
150+
host -t ns "$zz" > "$d/__dns-ns"
55151

56-
[ ! -s "$d/output" -o "$FORCE" = "1" ] && {
57-
./test2.py -d "meta.$tld" >"$d/output"
58-
}
152+
getTestDataInputForTldAndDomain || return 1
59153

154+
getTestDataOutputForTldAndDomain "$tld" "$domain"
60155
return 0
61156
}
62157

@@ -74,31 +169,66 @@ google
74169
'
75170
}
76171

77-
makeTestDataOriginalAll()
172+
makeTestDataTldFromDomains()
173+
{
174+
local tld="$1"
175+
176+
domainsToTry |
177+
while read domain
178+
do
179+
[ "$VERBOSE" = "1" ] && echo "try: $domain.$tld"
180+
makeTestDataOriginalOneTldDomain "$tld" "$domain"
181+
[ -s "$TMPDIR/$tld/input" ] && {
182+
[ "$VERBOSE" = "1" ] && ls -l "$TMPDIR/$tld/"
183+
testNameserverExistsInInputAndOutput "$tld" && break
184+
}
185+
done
186+
}
187+
188+
makeRulesFromTldIfExist()
189+
{
190+
local tld="$1"
191+
local tld2=$(echo $tld | tr "." "_" ) # here we need to replace all . into _
192+
193+
./test2.py -R -d "$tld2" > "$TMPDIR/$tld/__rules__"
194+
[ -s "$TMPDIR/$tld/__rules__" ]
195+
}
196+
197+
makeTestDataOriginalOneTld()
198+
{
199+
local tld="$1"
200+
201+
[ "$VERBOSE" = "1" ] && echo "try: $tld"
202+
203+
makeDirectoryForTld "$tld" "$domain" || exit 101
204+
makeRulesFromTldIfExist "$tld"
205+
makeTestDataTldFromDomains "$tld"
206+
}
207+
208+
makeTestDataOriginalAllTldSupported()
78209
{
79210
getAllTldSupported |
211+
sort |
80212
while read tld
81213
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
214+
makeTestDataOriginalOneTld "$tld"
95215
done
96216
}
97217

98218
main()
99219
{
100220
prepTempDir
101-
makeTestDataOriginalAll
221+
222+
[ "$#" = "0" ] && {
223+
makeTestDataOriginalAllTldSupported
224+
return
225+
}
226+
227+
for tld in $*
228+
do
229+
makeTestDataOriginalOneTld "$tld"
230+
done
102231
}
103232

104-
main
233+
main $* 2>&1 |
234+
tee out

0 commit comments

Comments
 (0)