Add UTF-8 and ISO-8859-2 test cases, make tests more reliable
This commit is contained in:
parent
037a527c44
commit
0fac54a29a
6 changed files with 41 additions and 28 deletions
10
test/e2e.ini
10
test/e2e.ini
|
@ -30,7 +30,7 @@ certs: test/certs
|
|||
|
||||
[tests]
|
||||
# Number of "test-*" sections in this file, describing test cases.
|
||||
cases: 10
|
||||
cases: 11
|
||||
e2e_log: test/logs/e2e.log
|
||||
e2e_log_format: %(asctime)s %(pathname)s:%(lineno)d %(levelname)s [%(funcName)s] %(message)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.
|
||||
|
||||
[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
|
||||
in: test/msgin/nonascii.msg
|
||||
out: ŁĄCZNOŚĆ
|
||||
|
|
|
@ -74,11 +74,11 @@ def _write_test_config(outfile, **config):
|
|||
|
||||
|
||||
def _load_file(name):
|
||||
f = open(name, 'r')
|
||||
f = open(name, 'rb')
|
||||
contents = f.read()
|
||||
f.close()
|
||||
|
||||
return bytes(contents, 'utf-8')
|
||||
return contents
|
||||
|
||||
|
||||
def _identity(x):
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
From: Dave <dave@localhost>
|
||||
To: Carlos <carlos@localhost>
|
||||
Content-Type: text/plain; charset=UTF-8
|
||||
Content-Type: text/plain; charset="iso-8859-2"
|
||||
Content-Transfer-Encoding: 8bit
|
||||
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 logging
|
||||
import email
|
||||
import email.policy
|
||||
|
||||
|
||||
EXIT_UNAVAILABLE = 1
|
||||
|
@ -19,7 +21,7 @@ EXIT_UNAVAILABLE = 1
|
|||
ENCODING = 'utf-8'
|
||||
|
||||
BUFFER_SIZE = 4096
|
||||
EOM = "\r\n.\r\n"
|
||||
EOM = b"\r\n.\r\n"
|
||||
LAST_LINE = -3
|
||||
|
||||
|
||||
|
@ -42,7 +44,7 @@ def receive_and_confirm(session):
|
|||
def localhost_at(port):
|
||||
return ('127.0.0.1', port)
|
||||
|
||||
def serve(port):
|
||||
def serve(port) -> bytes:
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
try:
|
||||
s.bind(localhost_at(port))
|
||||
|
@ -63,13 +65,14 @@ def serve(port):
|
|||
receive_and_confirm(conn) # Ignore sender 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())
|
||||
|
||||
# Consume until we get <CR><LF>.<CR><LF>, the end-of-message marker.
|
||||
message = ''
|
||||
message = b''
|
||||
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.recv(BUFFER_SIZE)
|
||||
|
@ -77,7 +80,7 @@ def serve(port):
|
|||
|
||||
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.
|
||||
return message[:-len(EOM)]
|
||||
|
@ -101,4 +104,5 @@ if len(sys.argv) < 2:
|
|||
port = int(sys.argv[1])
|
||||
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 sys
|
||||
import getopt
|
||||
from email import policy, message_from_binary_file
|
||||
|
||||
|
||||
def _load_file(name):
|
||||
f = open(name, 'r')
|
||||
contents = f.read()
|
||||
f.close()
|
||||
return contents
|
||||
with open(name, 'rb') as f:
|
||||
return message_from_binary_file(f, policy=policy.SMTP)
|
||||
|
||||
|
||||
def _send(host, port, from_addr, recipients, message):
|
||||
|
@ -16,13 +15,16 @@ def _send(host, port, from_addr, recipients, message):
|
|||
try:
|
||||
smtp = smtplib.SMTP(host, port)
|
||||
# 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:
|
||||
logging.error(f"Couldn't deliver message. Got error: {e}")
|
||||
return None
|
||||
except ConnectionRefusedError as e:
|
||||
logging.exception(f"Connection refused: {e}")
|
||||
return None
|
||||
except:
|
||||
logging.exception('Unexpected exception was thrown')
|
||||
return None
|
||||
|
||||
|
||||
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:
|
||||
if opt == "-f":
|
||||
sender = value
|
||||
logging.debug(f"Sender is {sender}")
|
||||
logging.debug(f"Sender is {sender!r}")
|
||||
if opt == "-t":
|
||||
recipient = value
|
||||
logging.debug(f"Recipient is {recipient}")
|
||||
logging.debug(f"Recipient is {recipient!r}")
|
||||
if opt == "-m":
|
||||
message = _load_file(value)
|
||||
logging.debug(f"Message is {message}")
|
||||
|
||||
if message is None:
|
||||
message = """\
|
||||
From: dave@disposlab
|
||||
To: alice@disposlab
|
||||
Subject: Test message
|
||||
|
||||
Lorem ipsum dolor sit amet.
|
||||
"""
|
||||
if message is None or sender is None or recipient is None:
|
||||
print('Use options to provide: -f sender -t recipient -m message')
|
||||
|
||||
_send('localhost', 10025, sender, [recipient], message)
|
||||
|
|
Loading…
Reference in a new issue