Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
mailinabox
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
mailinabox
Commits
0be47c41
Commit
0be47c41
authored
Apr 23, 2014
by
Joshua Tauberer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
in the DNS test, use dnspython3 rather than dig
parent
1b4dd982
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
41 additions
and
49 deletions
+41
-49
pip-requirements.txt
tests/pip-requirements.txt
+1
-0
test_dns.py
tests/test_dns.py
+40
-49
No files found.
tests/pip-requirements.txt
0 → 100644
View file @
0be47c41
dnspython3
tests/test_dns.py
View file @
0be47c41
...
@@ -7,7 +7,8 @@
...
@@ -7,7 +7,8 @@
# where ipaddr is the IP address of your Mail-in-a-Box
# where ipaddr is the IP address of your Mail-in-a-Box
# and hostname is the domain name to check the DNS for.
# and hostname is the domain name to check the DNS for.
import
sys
,
subprocess
,
re
,
difflib
import
sys
,
re
,
difflib
import
dns.reversename
,
dns
.
resolver
if
len
(
sys
.
argv
)
<
3
:
if
len
(
sys
.
argv
)
<
3
:
print
(
"Usage: tests/dns.py ipaddress hostname"
)
print
(
"Usage: tests/dns.py ipaddress hostname"
)
...
@@ -15,59 +16,49 @@ if len(sys.argv) < 3:
...
@@ -15,59 +16,49 @@ if len(sys.argv) < 3:
ipaddr
,
hostname
=
sys
.
argv
[
1
:]
ipaddr
,
hostname
=
sys
.
argv
[
1
:]
# construct the expected output
subs
=
{
"ipaddr"
:
ipaddr
,
"hostname"
:
hostname
}
expected
=
"""
{hostname}. ##### IN A {ipaddr}
{hostname}. ##### IN NS ns1.{hostname}.
{hostname}. ##### IN NS ns2.{hostname}.
ns1.{hostname}. ##### IN A {ipaddr}
ns2.{hostname}. ##### IN A {ipaddr}
www.{hostname}. ##### IN A {ipaddr}
{hostname}. ##### IN MX 10 {hostname}.
{hostname}. ##### IN TXT "v=spf1 mx -all"
mail._domainkey.{hostname}. ##### IN TXT "v=DKIM1
\
; k=rsa
\
; s=email
\
; " "p=__KEY__"
"""
.
format
(
**
subs
)
.
strip
()
+
"
\n
"
def
dig
(
server
,
digargs
):
# run dig and clean the output
response
=
subprocess
.
check_output
([
'dig'
,
'@'
+
server
,
"+noadditional"
,
"+noauthority"
]
+
digargs
)
.
decode
(
'utf8'
)
response
=
re
.
sub
(
'[
\r\n
]+'
,
'
\n
'
,
response
)
# remove blank lines
response
=
re
.
sub
(
'
\n
;.*'
,
''
,
response
)
# remove comments
response
=
re
.
sub
(
'(
\n
\
S+
\
s+)(
\
d+)'
,
r'\1#####'
,
response
)
# normalize TTLs
response
=
re
.
sub
(
r"(\"p=).*(\")"
,
r"\1__KEY__\2"
,
response
)
# normalize DKIM key
response
=
response
.
strip
()
+
"
\n
"
return
response
def
test
(
server
,
description
):
def
test
(
server
,
description
):
digoutput
=
\
tests
=
[
dig
(
server
,
[
hostname
])
\
(
hostname
,
"A"
,
ipaddr
),
+
dig
(
server
,
[
"ns"
,
hostname
])
\
(
hostname
,
"NS"
,
"ns1.
%
s.;ns2.
%
s."
%
(
hostname
,
hostname
)),
+
dig
(
server
,
[
"ns1."
+
hostname
])
\
(
"ns1."
+
hostname
,
"A"
,
ipaddr
),
+
dig
(
server
,
[
"ns2."
+
hostname
])
\
(
"ns2."
+
hostname
,
"A"
,
ipaddr
),
+
dig
(
server
,
[
"www."
+
hostname
])
\
(
"www."
+
hostname
,
"A"
,
ipaddr
),
+
dig
(
server
,
[
"mx"
,
hostname
])
\
(
hostname
,
"MX"
,
"10 "
+
hostname
+
"."
),
+
dig
(
server
,
[
"txt"
,
hostname
])
\
(
hostname
,
"TXT"
,
"
\"
v=spf1 mx -all
\"
"
),
+
dig
(
server
,
[
"txt"
,
"mail._domainkey."
+
hostname
])
(
"mail._domainkey."
+
hostname
,
"TXT"
,
"
\"
v=DKIM1; k=rsa; s=email;
\"
\"
p=__KEY__
\"
"
),
return
test2
(
digoutput
,
server
,
description
,
expected
)
]
return
test2
(
tests
,
server
,
description
)
def
test_ptr
(
server
,
description
):
def
test_ptr
(
server
,
description
):
ipaddr_reversed
=
"."
.
join
(
reversed
(
ipaddr
.
split
(
"."
)
)
)
ipaddr_rev
=
dns
.
reversename
.
from_address
(
ipaddr
)
expected
=
"
%
s.in-addr.arpa. ##### IN PTR
%
s.
\n
"
%
(
ipaddr_reversed
,
hostname
)
tests
=
[
digoutput
=
dig
(
server
,
[
"-x"
,
ipaddr
])
(
ipaddr_rev
,
"PTR"
,
hostname
+
'.'
),
return
test2
(
digoutput
,
server
,
description
,
expected
)
]
return
test2
(
tests
,
server
,
description
)
def
test2
(
tests
,
server
,
description
):
first
=
True
resolver
=
dns
.
resolver
.
get_default_resolver
()
resolver
.
nameservers
=
[
server
]
for
qname
,
rtype
,
expected_answer
in
tests
:
# do the query and format the result as a string
response
=
dns
.
resolver
.
query
(
qname
,
rtype
)
response
=
";"
.
join
(
str
(
r
)
for
r
in
response
)
response
=
re
.
sub
(
r"(\"p=).*(\")"
,
r"\1__KEY__\2"
,
response
)
# normalize DKIM key
def
test2
(
digoutput
,
server
,
description
,
expected
):
# is it right?
# Show a diff if there are any changes
if
response
==
expected_answer
:
has_diff
=
False
#print(server, ":", qname, rtype, "?", response)
def
split
(
s
):
return
[
line
+
"
\n
"
for
line
in
s
.
split
(
"
\n
"
)]
continue
for
line
in
difflib
.
unified_diff
(
split
(
expected
),
split
(
digoutput
),
fromfile
=
'expected DNS settings'
,
tofile
=
description
):
if
not
has_diff
:
# show prolem
print
(
"The response from
%
s (
%
s) is not correct:"
%
(
description
,
server
))
if
first
:
print
(
"Incorrect DNS Response from"
,
description
)
print
()
print
()
has_diff
=
True
first
=
False
sys
.
stdout
.
write
(
line
)
return
not
has_diff
print
(
qname
,
rtype
,
"got"
,
repr
(
response
),
"but we should have gotten"
,
repr
(
expected_answer
))
return
first
# success
# Test the response from the machine itself.
# Test the response from the machine itself.
if
not
test
(
ipaddr
,
"Mail-in-a-Box"
):
if
not
test
(
ipaddr
,
"Mail-in-a-Box"
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment