1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
|
import ssl
import unittest
import pkg_resources
from aiosmtpd.controller import Controller as BaseController
from aiosmtpd.handlers import Sink
from aiosmtpd.smtp import SMTP as SMTPProtocol
from aiosmtpd.testing.helpers import (
SUPPORTED_COMMANDS_TLS,
assert_auth_invalid,
)
from email.mime.text import MIMEText
from smtplib import SMTP
class Controller(BaseController):
def factory(self):
return SMTPProtocol(self.handler)
class ReceivingHandler:
def __init__(self):
self.box = []
async def handle_DATA(self, server, session, envelope):
self.box.append(envelope)
return '250 OK'
def get_tls_context():
tls_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
tls_context.load_cert_chain(
pkg_resources.resource_filename('aiosmtpd.tests.certs', 'server.crt'),
pkg_resources.resource_filename('aiosmtpd.tests.certs', 'server.key'))
return tls_context
class TLSRequiredController(Controller):
def factory(self):
return SMTPProtocol(
self.handler,
decode_data=True,
require_starttls=True,
tls_context=get_tls_context())
class TLSController(Controller):
def factory(self):
return SMTPProtocol(
self.handler,
decode_data=True,
require_starttls=False,
tls_context=get_tls_context())
class RequireTLSAuthDecodingController(Controller):
def factory(self):
return SMTPProtocol(
self.handler,
decode_data=True,
auth_require_tls=True,
tls_context=get_tls_context())
class HandshakeFailingHandler:
def handle_STARTTLS(self, server, session, envelope):
return False
class TestStartTLS(unittest.TestCase):
def test_starttls(self):
handler = ReceivingHandler()
controller = TLSController(handler)
controller.start()
self.addCleanup(controller.stop)
with SMTP(controller.hostname, controller.port) as client:
code, response = client.ehlo('example.com')
self.assertEqual(code, 250)
self.assertIn('starttls', client.esmtp_features)
code, response = client.starttls()
self.assertEqual(code, 220)
client.send_message(
MIMEText('hi'),
'sender@example.com',
'rcpt1@example.com')
self.assertEqual(len(handler.box), 1)
def test_failed_handshake(self):
controller = TLSController(HandshakeFailingHandler())
controller.start()
self.addCleanup(controller.stop)
with SMTP(controller.hostname, controller.port) as client:
client.ehlo('example.com')
code, response = client.starttls()
self.assertEqual(code, 220)
code, response = client.mail('sender@example.com')
self.assertEqual(code, 554)
code, response = client.rcpt('rcpt@example.com')
self.assertEqual(code, 554)
def test_disabled_tls(self):
controller = Controller(Sink)
controller.start()
self.addCleanup(controller.stop)
with SMTP(controller.hostname, controller.port) as client:
client.ehlo('example.com')
code, response = client.docmd('STARTTLS')
self.assertEqual(code, 454)
def test_tls_bad_syntax(self):
controller = TLSController(Sink)
controller.start()
self.addCleanup(controller.stop)
with SMTP(controller.hostname, controller.port) as client:
client.ehlo('example.com')
code, response = client.docmd('STARTTLS', 'TRUE')
self.assertEqual(code, 501)
def test_help_after_starttls(self):
controller = TLSController(Sink())
controller.start()
self.addCleanup(controller.stop)
with SMTP(controller.hostname, controller.port) as client:
# Don't get tricked by smtplib processing of the response.
code, response = client.docmd('HELP')
self.assertEqual(code, 250)
self.assertEqual(response, SUPPORTED_COMMANDS_TLS)
class TestTLSForgetsSessionData(unittest.TestCase):
def setUp(self):
controller = TLSController(Sink)
controller.start()
self.addCleanup(controller.stop)
self.address = (controller.hostname, controller.port)
def test_forget_ehlo(self):
with SMTP(*self.address) as client:
client.starttls()
code, response = client.mail('sender@example.com')
self.assertEqual(code, 503)
self.assertEqual(response, b'Error: send HELO first')
def test_forget_mail(self):
with SMTP(*self.address) as client:
client.ehlo('example.com')
client.mail('sender@example.com')
client.starttls()
client.ehlo('example.com')
code, response = client.rcpt('rcpt@example.com')
self.assertEqual(code, 503)
self.assertEqual(response, b'Error: need MAIL command')
def test_forget_rcpt(self):
with SMTP(*self.address) as client:
client.ehlo('example.com')
client.mail('sender@example.com')
client.rcpt('rcpt@example.com')
client.starttls()
client.ehlo('example.com')
client.mail('sender@example.com')
code, response = client.docmd('DATA')
self.assertEqual(code, 503)
self.assertEqual(response, b'Error: need RCPT command')
class TestRequireTLS(unittest.TestCase):
def setUp(self):
controller = TLSRequiredController(Sink)
controller.start()
self.addCleanup(controller.stop)
self.address = (controller.hostname, controller.port)
def test_hello_fails(self):
with SMTP(*self.address) as client:
code, response = client.helo('example.com')
self.assertEqual(code, 530)
def test_help_fails(self):
with SMTP(*self.address) as client:
code, response = client.docmd('HELP', 'HELO')
self.assertEqual(code, 530)
def test_ehlo(self):
with SMTP(*self.address) as client:
code, response = client.ehlo('example.com')
self.assertEqual(code, 250)
self.assertIn('starttls', client.esmtp_features)
def test_mail_fails(self):
with SMTP(*self.address) as client:
client.ehlo('example.com')
code, response = client.mail('sender@exapmle.com')
self.assertEqual(code, 530)
def test_rcpt_fails(self):
with SMTP(*self.address) as client:
client.ehlo('example.com')
code, response = client.rcpt('sender@exapmle.com')
self.assertEqual(code, 530)
def test_vrfy_fails(self):
with SMTP(*self.address) as client:
client.ehlo('example.com')
code, response = client.vrfy('sender@exapmle.com')
self.assertEqual(code, 530)
def test_data_fails(self):
with SMTP(*self.address) as client:
client.ehlo('example.com')
code, response = client.docmd('DATA')
self.assertEqual(code, 530)
class TestRequireTLSAUTH(unittest.TestCase):
def setUp(self):
controller = RequireTLSAuthDecodingController(Sink)
controller.start()
self.addCleanup(controller.stop)
self.address = (controller.hostname, controller.port)
def test_auth_notls(self):
with SMTP(*self.address) as client:
client.ehlo('example.com')
code, response = client.docmd("AUTH ")
self.assertEqual(code, 538)
self.assertEqual(response,
b"5.7.11 Encryption required for requested "
b"authentication mechanism")
def test_auth_tls(self):
with SMTP(*self.address) as client:
client.starttls()
client.ehlo('example.com')
code, response = client.docmd('AUTH PLAIN AHRlc3QAdGVzdA==')
assert_auth_invalid(self, code, response)
|