Add UTF-8 and ISO-8859-2 test cases, make tests more reliable
This commit is contained in:
parent
037a527c44
commit
0fac54a29a
10
test/e2e.ini
10
test/e2e.ini
|
@ -30,7 +30,7 @@ certs: test/certs
|
||||||
|
|
||||||
[tests]
|
[tests]
|
||||||
# Number of "test-*" sections in this file, describing test cases.
|
# Number of "test-*" sections in this file, describing test cases.
|
||||||
cases: 10
|
cases: 11
|
||||||
e2e_log: test/logs/e2e.log
|
e2e_log: test/logs/e2e.log
|
||||||
e2e_log_format: %(asctime)s %(pathname)s:%(lineno)d %(levelname)s [%(funcName)s] %(message)s
|
e2e_log_format: %(asctime)s %(pathname)s:%(lineno)d %(levelname)s [%(funcName)s] %(message)s
|
||||||
e2e_log_datefmt: %Y-%m-%d %H:%M:%S
|
e2e_log_datefmt: %Y-%m-%d %H:%M:%S
|
||||||
|
@ -92,7 +92,13 @@ in: test/msgin/with-markers2clear.msg
|
||||||
out-not: This message includes inline PGP markers.
|
out-not: This message includes inline PGP markers.
|
||||||
|
|
||||||
[case-10]
|
[case-10]
|
||||||
descr: Non-ASCII message
|
descr: UTF-8 message
|
||||||
|
to: carlos@disposlab
|
||||||
|
in: test/msgin/utf8.msg
|
||||||
|
out: ŁĄCZNOŚĆ
|
||||||
|
|
||||||
|
[case-11]
|
||||||
|
descr: Non-ASCII message (ISO-8859-2)
|
||||||
to: carlos@disposlab
|
to: carlos@disposlab
|
||||||
in: test/msgin/nonascii.msg
|
in: test/msgin/nonascii.msg
|
||||||
out: ŁĄCZNOŚĆ
|
out: ŁĄCZNOŚĆ
|
||||||
|
|
|
@ -74,11 +74,11 @@ def _write_test_config(outfile, **config):
|
||||||
|
|
||||||
|
|
||||||
def _load_file(name):
|
def _load_file(name):
|
||||||
f = open(name, 'r')
|
f = open(name, 'rb')
|
||||||
contents = f.read()
|
contents = f.read()
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
return bytes(contents, 'utf-8')
|
return contents
|
||||||
|
|
||||||
|
|
||||||
def _identity(x):
|
def _identity(x):
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
From: Dave <dave@localhost>
|
From: Dave <dave@localhost>
|
||||||
To: Carlos <carlos@localhost>
|
To: Carlos <carlos@localhost>
|
||||||
Content-Type: text/plain; charset=UTF-8
|
Content-Type: text/plain; charset="iso-8859-2"
|
||||||
Content-Transfer-Encoding: 8bit
|
Content-Transfer-Encoding: 8bit
|
||||||
Subject: Test
|
Subject: Test
|
||||||
|
|
||||||
ŁĄCZNOŚĆ. Zaźółć gęślą jaźń.
|
£¡CZNO¦Æ. Za¼ó³æ gê¶l± ja¼ñ.
|
||||||
|
|
7
test/msgin/utf8.msg
Normal file
7
test/msgin/utf8.msg
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
From: Dave <dave@localhost>
|
||||||
|
To: Carlos <carlos@localhost>
|
||||||
|
Content-Type: text/plain; charset="utf-8"
|
||||||
|
Content-Transfer-Encoding: 8bit
|
||||||
|
Subject: Test
|
||||||
|
|
||||||
|
ŁĄCZNOŚĆ. Zaźółć gęślą jaźń.
|
|
@ -12,6 +12,8 @@ import sys
|
||||||
import socket
|
import socket
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
import email
|
||||||
|
import email.policy
|
||||||
|
|
||||||
|
|
||||||
EXIT_UNAVAILABLE = 1
|
EXIT_UNAVAILABLE = 1
|
||||||
|
@ -19,7 +21,7 @@ EXIT_UNAVAILABLE = 1
|
||||||
ENCODING = 'utf-8'
|
ENCODING = 'utf-8'
|
||||||
|
|
||||||
BUFFER_SIZE = 4096
|
BUFFER_SIZE = 4096
|
||||||
EOM = "\r\n.\r\n"
|
EOM = b"\r\n.\r\n"
|
||||||
LAST_LINE = -3
|
LAST_LINE = -3
|
||||||
|
|
||||||
|
|
||||||
|
@ -42,7 +44,7 @@ def receive_and_confirm(session):
|
||||||
def localhost_at(port):
|
def localhost_at(port):
|
||||||
return ('127.0.0.1', port)
|
return ('127.0.0.1', port)
|
||||||
|
|
||||||
def serve(port):
|
def serve(port) -> bytes:
|
||||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
try:
|
try:
|
||||||
s.bind(localhost_at(port))
|
s.bind(localhost_at(port))
|
||||||
|
@ -63,13 +65,14 @@ def serve(port):
|
||||||
receive_and_confirm(conn) # Ignore sender address
|
receive_and_confirm(conn) # Ignore sender address
|
||||||
receive_and_confirm(conn) # Ignore recipient address
|
receive_and_confirm(conn) # Ignore recipient address
|
||||||
|
|
||||||
conn.recv(BUFFER_SIZE)
|
b = conn.recv(BUFFER_SIZE)
|
||||||
|
logging.debug('Got data: %s', repr(b))
|
||||||
conn.sendall(provide_message())
|
conn.sendall(provide_message())
|
||||||
|
|
||||||
# Consume until we get <CR><LF>.<CR><LF>, the end-of-message marker.
|
# Consume until we get <CR><LF>.<CR><LF>, the end-of-message marker.
|
||||||
message = ''
|
message = b''
|
||||||
while not message.endswith(EOM):
|
while not message.endswith(EOM):
|
||||||
message += conn.recv(BUFFER_SIZE).decode(ENCODING)
|
message += conn.recv(BUFFER_SIZE)
|
||||||
conn.sendall(ok(b"OK, id=test"))
|
conn.sendall(ok(b"OK, id=test"))
|
||||||
|
|
||||||
conn.recv(BUFFER_SIZE)
|
conn.recv(BUFFER_SIZE)
|
||||||
|
@ -77,7 +80,7 @@ def serve(port):
|
||||||
|
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
logging.debug(f"Received {len(message)} characters of data")
|
logging.debug(f"Received {len(message)} bytes of data")
|
||||||
|
|
||||||
# Trim EOM marker as we're only interested in the message body.
|
# Trim EOM marker as we're only interested in the message body.
|
||||||
return message[:-len(EOM)]
|
return message[:-len(EOM)]
|
||||||
|
@ -101,4 +104,5 @@ if len(sys.argv) < 2:
|
||||||
port = int(sys.argv[1])
|
port = int(sys.argv[1])
|
||||||
body = serve(port)
|
body = serve(port)
|
||||||
|
|
||||||
print(body)
|
msg = email.message_from_bytes(body, policy=email.policy.SMTP)
|
||||||
|
print(msg)
|
||||||
|
|
|
@ -2,13 +2,12 @@ import logging
|
||||||
import smtplib
|
import smtplib
|
||||||
import sys
|
import sys
|
||||||
import getopt
|
import getopt
|
||||||
|
from email import policy, message_from_binary_file
|
||||||
|
|
||||||
|
|
||||||
def _load_file(name):
|
def _load_file(name):
|
||||||
f = open(name, 'r')
|
with open(name, 'rb') as f:
|
||||||
contents = f.read()
|
return message_from_binary_file(f, policy=policy.SMTP)
|
||||||
f.close()
|
|
||||||
return contents
|
|
||||||
|
|
||||||
|
|
||||||
def _send(host, port, from_addr, recipients, message):
|
def _send(host, port, from_addr, recipients, message):
|
||||||
|
@ -16,13 +15,16 @@ def _send(host, port, from_addr, recipients, message):
|
||||||
try:
|
try:
|
||||||
smtp = smtplib.SMTP(host, port)
|
smtp = smtplib.SMTP(host, port)
|
||||||
# smtp.starttls()
|
# smtp.starttls()
|
||||||
return smtp.sendmail(from_addr, recipients, message)
|
return smtp.sendmail(from_addr, recipients, message.as_bytes(policy=policy.SMTP))
|
||||||
except smtplib.SMTPDataError as e:
|
except smtplib.SMTPDataError as e:
|
||||||
logging.error(f"Couldn't deliver message. Got error: {e}")
|
logging.error(f"Couldn't deliver message. Got error: {e}")
|
||||||
return None
|
return None
|
||||||
except ConnectionRefusedError as e:
|
except ConnectionRefusedError as e:
|
||||||
logging.exception(f"Connection refused: {e}")
|
logging.exception(f"Connection refused: {e}")
|
||||||
return None
|
return None
|
||||||
|
except:
|
||||||
|
logging.exception('Unexpected exception was thrown')
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
logging.basicConfig(filename="test/logs/sendmail.log",
|
logging.basicConfig(filename="test/logs/sendmail.log",
|
||||||
|
@ -36,21 +38,15 @@ opts, _ = getopt.getopt(sys.argv[1:], "f:t:m:")
|
||||||
for opt, value in opts:
|
for opt, value in opts:
|
||||||
if opt == "-f":
|
if opt == "-f":
|
||||||
sender = value
|
sender = value
|
||||||
logging.debug(f"Sender is {sender}")
|
logging.debug(f"Sender is {sender!r}")
|
||||||
if opt == "-t":
|
if opt == "-t":
|
||||||
recipient = value
|
recipient = value
|
||||||
logging.debug(f"Recipient is {recipient}")
|
logging.debug(f"Recipient is {recipient!r}")
|
||||||
if opt == "-m":
|
if opt == "-m":
|
||||||
message = _load_file(value)
|
message = _load_file(value)
|
||||||
logging.debug(f"Message is {message}")
|
logging.debug(f"Message is {message}")
|
||||||
|
|
||||||
if message is None:
|
if message is None or sender is None or recipient is None:
|
||||||
message = """\
|
print('Use options to provide: -f sender -t recipient -m message')
|
||||||
From: dave@disposlab
|
|
||||||
To: alice@disposlab
|
|
||||||
Subject: Test message
|
|
||||||
|
|
||||||
Lorem ipsum dolor sit amet.
|
|
||||||
"""
|
|
||||||
|
|
||||||
_send('localhost', 10025, sender, [recipient], message)
|
_send('localhost', 10025, sender, [recipient], message)
|
||||||
|
|
Loading…
Reference in a new issue