From 76d9fc3865efb010c0dfc33f796df4af981bed8f Mon Sep 17 00:00:00 2001 From: Ionut Filip Date: Mon, 5 Nov 2018 17:28:40 +0200 Subject: [PATCH] Rewrite of email test script and added eicar virus file --- tests/compose/filters/01_email_test.sh | 7 ++- tests/compose/filters/eicar.com | 1 + tests/email_test.py | 86 +++++++++++++++----------- 3 files changed, 56 insertions(+), 38 deletions(-) create mode 100644 tests/compose/filters/eicar.com diff --git a/tests/compose/filters/01_email_test.sh b/tests/compose/filters/01_email_test.sh index 64fa3fba..5af395c4 100755 --- a/tests/compose/filters/01_email_test.sh +++ b/tests/compose/filters/01_email_test.sh @@ -1 +1,6 @@ -python3 tests/email_test.py message-filters \ No newline at end of file +python3 tests/email_test.py message-virus "tests/compose/filters/eicar.com" +if [ $? -eq 99 ]; then + exit 0 +else + exit 1 +fi \ No newline at end of file diff --git a/tests/compose/filters/eicar.com b/tests/compose/filters/eicar.com new file mode 100644 index 00000000..704cac85 --- /dev/null +++ b/tests/compose/filters/eicar.com @@ -0,0 +1 @@ +X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H* diff --git a/tests/email_test.py b/tests/email_test.py index 7148365e..853b76b5 100755 --- a/tests/email_test.py +++ b/tests/email_test.py @@ -2,46 +2,58 @@ import smtplib import imaplib import time import sys +from email.mime.multipart import MIMEMultipart +from email.mime.text import MIMEText +import ntpath +from email.mime.base import MIMEBase +from email import encoders -email_msg = sys.argv[1] +msg = MIMEMultipart() +msg['From'] = "admin@mailu.io" +msg['To'] = "user@mailu.io" +msg['Subject'] = "File Test" +msg.attach(MIMEText(sys.argv[1], 'plain')) -#Login to smt server and sending email with secret message -def send_email(msg): - print("Sending email ...") - server = smtplib.SMTP('localhost') - server.set_debuglevel(1) - server.connect('localhost', 587) - server.ehlo() - server.starttls() - server.ehlo() - server.login("admin@mailu.io", "password") - - server.sendmail("admin@mailu.io", "user@mailu.io", msg) - server.quit() +if len(sys.argv) == 3: + part = MIMEBase('application', 'octet-stream') + part.set_payload((open(sys.argv[2], "rb")).read()) + encoders.encode_base64(part) + part.add_header('Content-Disposition', "attachment; filename=%s" % ntpath.basename(sys.argv[2])) + msg.attach(part) - print("email sent with message " + msg) +try: + smtp_server = smtplib.SMTP('localhost') + smtp_server.set_debuglevel(1) + smtp_server.connect('localhost', 587) + smtp_server.ehlo() + smtp_server.starttls() + smtp_server.ehlo() + smtp_server.login("admin@mailu.io", "password") + + smtp_server.sendmail("admin@mailu.io", "user@mailu.io", msg.as_string()) + smtp_server.quit() +except: + sys.exit(25) -#Login to imap server, read latest email and check for secret message -def read_email(): - print("Receiving email ...") - server = imaplib.IMAP4_SSL('localhost') - server.login('user@mailu.io', 'password') - - stat, count = server.select('inbox') - stat, data = server.fetch(count[0], '(UID BODY[TEXT])') - - print("email received with message " + str(data[0][1])) - - if email_msg in str(data[0][1]): - print("Success!") - else: - print("Failed receiving email with message %s" % email_msg) - sys.exit(1) - server.close() - server.logout() +time.sleep(30) +try: + imap_server = imaplib.IMAP4_SSL('localhost') + imap_server.login('user@mailu.io', 'password') +except: + sys.exit(110) + +stat, count = imap_server.select('inbox') +try: + stat, data = imap_server.fetch(count[0], '(UID BODY[TEXT])') +except : + sys.exit(99) + +if sys.argv[1] in str(data[0][1]): + print("Success sending and receiving email!") +else: + print("Failed receiving email with message %s" % sys.argv[1]) + sys.exit(99) -send_email(email_msg) -print("Sleeping for 1m") -time.sleep(60) -read_email() +imap_server.close() +imap_server.logout()