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
04d97a11
Commit
04d97a11
authored
Apr 17, 2014
by
Joshua Tauberer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
new tests for DNS and IMAP/SMTP
parent
304bf3d3
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
137 additions
and
42 deletions
+137
-42
.gitignore
.gitignore
+1
-0
dns.py
tests/dns.py
+58
-0
imap.py
tests/imap.py
+0
-20
mail.py
tests/mail.py
+78
-0
smtp_submission.py
tests/smtp_submission.py
+0
-22
No files found.
.gitignore
View file @
04d97a11
*~
tests/__pycache__/
tests/dns.py
0 → 100755
View file @
04d97a11
#!/usr/bin/python3
# Test DNS configuration.
#
# tests/dns.py ipaddr hostname
import
sys
,
subprocess
,
re
,
difflib
if
len
(
sys
.
argv
)
<
3
:
print
(
"Usage: tests/dns.py ipaddress hostname"
)
sys
.
exit
(
1
)
ipaddr
,
hostname
=
sys
.
argv
[
1
:]
def
dig
(
digargs
):
# run dig and clean the output
response
=
subprocess
.
check_output
([
'dig'
,
'@'
+
ipaddr
]
+
digargs
)
.
decode
(
'utf8'
)
response
=
re
.
sub
(
'[
\r\n
]+'
,
'
\n
'
,
response
)
# remove blank lines
response
=
re
.
sub
(
'
\n
;.*'
,
''
,
response
)
# remove comments
response
=
response
.
strip
()
+
"
\n
"
return
response
# call dig a few times with different parameters
digoutput
=
\
dig
([
hostname
])
\
+
dig
([
"www."
+
hostname
,
"+noadditional"
,
"+noauthority"
])
\
+
dig
([
"mx"
,
hostname
,
"+noadditional"
,
"+noauthority"
])
\
+
dig
([
"txt"
,
hostname
,
"+noadditional"
,
"+noauthority"
])
\
+
dig
([
"txt"
,
"mail._domainkey."
+
hostname
,
"+noadditional"
,
"+noauthority"
])
# normalize DKIM key
digoutput
=
re
.
sub
(
r"(\"p=).*(\")"
,
r"\1__KEY__\2"
,
digoutput
)
# construct the expected output
subs
=
{
"ipaddr"
:
ipaddr
,
"hostname"
:
hostname
}
expected
=
"""
{hostname}. 86400 IN A {ipaddr}
{hostname}. 86400 IN NS ns1.{hostname}.
{hostname}. 86400 IN NS ns2.{hostname}.
ns1.{hostname}. 86400 IN A {ipaddr}
ns2.{hostname}. 86400 IN A {ipaddr}
www.{hostname}. 86400 IN A {ipaddr}
{hostname}. 86400 IN MX 10 {hostname}.
{hostname}. 300 IN TXT "v=spf1 mx -all"
mail._domainkey.{hostname}. 86400 IN TXT "v=DKIM1
\
; k=rsa
\
; s=email
\
; " "p=__KEY__"
"""
.
format
(
**
subs
)
.
strip
()
+
"
\n
"
# Show a diff if there are any changes
has_diff
=
False
def
split
(
s
):
return
[
line
+
"
\n
"
for
line
in
s
.
split
(
"
\n
"
)]
for
line
in
difflib
.
unified_diff
(
split
(
expected
),
split
(
digoutput
),
fromfile
=
'expected DNS settings'
,
tofile
=
'output from dig'
):
sys
.
stdout
.
write
(
line
)
has_diff
=
True
if
not
has_diff
:
print
(
"DNS is OK."
)
sys
.
exit
(
0
)
sys
.
exit
(
1
)
tests/imap.py
deleted
100755 → 0
View file @
304bf3d3
#!/usr/bin/python3
import
imaplib
,
sys
if
len
(
sys
.
argv
)
<
3
:
print
(
"Usage: tests/imap.py host username password"
)
sys
.
exit
(
1
)
host
,
username
,
pw
=
sys
.
argv
[
1
:
4
]
M
=
imaplib
.
IMAP4_SSL
(
host
)
M
.
login
(
username
,
pw
)
print
(
"Login successful."
)
M
.
select
()
typ
,
data
=
M
.
search
(
None
,
'ALL'
)
for
num
in
data
[
0
]
.
split
():
typ
,
data
=
M
.
fetch
(
num
,
'(RFC822)'
)
print
(
'Message
%
s
\n
%
s
\n
'
%
(
num
,
data
[
0
][
1
]))
M
.
close
()
M
.
logout
()
tests/mail.py
0 → 100755
View file @
04d97a11
#!/usr/bin/python3
# Tests sending and receiving mail by sending a test message to yourself.
import
sys
,
imaplib
,
smtplib
,
uuid
,
time
,
dkim
if
len
(
sys
.
argv
)
<
3
:
print
(
"Usage: tests/mail.py hostname emailaddress password"
)
sys
.
exit
(
1
)
host
,
emailaddress
,
pw
=
sys
.
argv
[
1
:
4
]
# Attempt to login with IMAP. Our setup uses email addresses
# as IMAP/SMTP usernames.
M
=
imaplib
.
IMAP4_SSL
(
host
)
M
.
login
(
emailaddress
,
pw
)
M
.
select
()
print
(
"IMAP login is OK."
)
# Attempt to send a mail to ourself.
mailsubject
=
"Mail-in-a-Box Automated Test Message "
+
uuid
.
uuid4
()
.
hex
emailto
=
emailaddress
msg
=
"""From: {emailaddress}
To: {emailto}
Subject: {subject}
This is a test message. It should be automatically deleted by the test script."""
.
format
(
emailaddress
=
emailaddress
,
emailto
=
emailto
,
subject
=
mailsubject
,
)
server
=
smtplib
.
SMTP
(
host
,
587
)
#server.set_debuglevel(1)
server
.
starttls
()
server
.
login
(
emailaddress
,
pw
)
server
.
sendmail
(
emailaddress
,
[
emailto
],
msg
)
server
.
quit
()
print
(
"SMTP submission is OK."
)
while
True
:
# Wait so the message can propagate to the inbox.
time
.
sleep
(
10
)
# Read the subject lines of all of the emails in the inbox
# to find our test message, and then delete it.
found
=
False
typ
,
data
=
M
.
search
(
None
,
'ALL'
)
for
num
in
data
[
0
]
.
split
():
typ
,
data
=
M
.
fetch
(
num
,
'(BODY[HEADER.FIELDS (SUBJECT)])'
)
imapsubjectline
=
data
[
0
][
1
]
.
strip
()
.
decode
(
"utf8"
)
if
imapsubjectline
==
"Subject: "
+
mailsubject
:
# We found our test message.
found
=
True
# To test DKIM, download the whole mssage body. Unfortunately,
# pydkim doesn't actually work.
# You must 'sudo apt-get install python3-dkim python3-dnspython' first.
#typ, msgdata = M.fetch(num, '(RFC822)')
#msg = msgdata[0][1]
#if dkim.verify(msg):
# print("DKIM signature on the test message is OK (verified).")
#else:
# print("DKIM signature on the test message failed verification.")
# Delete the test message.
M
.
store
(
num
,
'+FLAGS'
,
'
\\
Deleted'
)
M
.
expunge
()
break
if
found
:
break
print
(
"Test message not present in the inbox yet..."
)
M
.
close
()
M
.
logout
()
print
(
"Test message sent & received successfully."
)
tests/smtp_submission.py
deleted
100755 → 0
View file @
304bf3d3
#!/usr/bin/python3
import
smtplib
,
sys
if
len
(
sys
.
argv
)
<
3
:
print
(
"Usage: tests/smtp_submission.py host email.from pw email.to"
)
sys
.
exit
(
1
)
host
,
fromaddr
,
pw
,
toaddr
=
sys
.
argv
[
1
:
5
]
msg
=
"""From:
%
s
To:
%
s
Subject: SMTP server test
This is a test message."""
%
(
fromaddr
,
toaddr
)
server
=
smtplib
.
SMTP
(
host
,
587
)
server
.
set_debuglevel
(
1
)
server
.
starttls
()
server
.
login
(
fromaddr
,
pw
)
server
.
sendmail
(
fromaddr
,
[
toaddr
],
msg
)
server
.
quit
()
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