test_smtp.py 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Test cases for twisted.mail.smtp module.
  5. """
  6. from __future__ import absolute_import, division
  7. import inspect
  8. import base64
  9. from io import BytesIO
  10. from zope.interface import implementer, directlyProvides
  11. from twisted.python.util import LineLog
  12. from twisted.trial import unittest
  13. from twisted.protocols import basic, loopback
  14. from twisted.internet import defer, protocol, reactor, interfaces
  15. from twisted.internet import address, error, task
  16. from twisted.test.proto_helpers import MemoryReactor, StringTransport
  17. from twisted import cred
  18. import twisted.cred.error
  19. import twisted.cred.portal
  20. import twisted.cred.checkers
  21. import twisted.cred.credentials
  22. from twisted.cred.portal import IRealm, Portal
  23. from twisted.cred.checkers import ICredentialsChecker, AllowAnonymousAccess
  24. from twisted.cred.credentials import IAnonymous
  25. from twisted.cred.error import UnauthorizedLogin
  26. from twisted.mail import smtp
  27. from twisted.mail._cred import LOGINCredentials
  28. try:
  29. from twisted.test.ssl_helpers import ClientTLSContext, ServerTLSContext
  30. except ImportError:
  31. sslSkip = "OpenSSL not present"
  32. else:
  33. sslSkip = None
  34. import re
  35. def spameater(*spam, **eggs):
  36. return None
  37. @implementer(smtp.IMessage)
  38. class BrokenMessage(object):
  39. """
  40. L{BrokenMessage} is an L{IMessage} which raises an unexpected exception
  41. from its C{eomReceived} method. This is useful for creating a server which
  42. can be used to test client retry behavior.
  43. """
  44. def __init__(self, user):
  45. pass
  46. def lineReceived(self, line):
  47. pass
  48. def eomReceived(self):
  49. raise RuntimeError("Some problem, delivery is failing.")
  50. def connectionLost(self):
  51. pass
  52. class DummyMessage(object):
  53. """
  54. L{BrokenMessage} is an L{IMessage} which saves the message delivered to it
  55. to its domain object.
  56. @ivar domain: A L{DummyDomain} which will be used to store the message once
  57. it is received.
  58. """
  59. def __init__(self, domain, user):
  60. self.domain = domain
  61. self.user = user
  62. self.buffer = []
  63. def lineReceived(self, line):
  64. # Throw away the generated Received: header
  65. if not re.match(b'Received: From yyy.com \(\[.*\]\) by localhost;',
  66. line):
  67. self.buffer.append(line)
  68. def eomReceived(self):
  69. message = b'\n'.join(self.buffer) + b'\n'
  70. self.domain.messages[self.user.dest.local].append(message)
  71. deferred = defer.Deferred()
  72. deferred.callback(b"saved")
  73. return deferred
  74. class DummyDomain(object):
  75. """
  76. L{DummyDomain} is an L{IDomain} which keeps track of messages delivered to
  77. it in memory.
  78. """
  79. def __init__(self, names):
  80. self.messages = {}
  81. for name in names:
  82. self.messages[name] = []
  83. def exists(self, user):
  84. if user.dest.local in self.messages:
  85. return defer.succeed(lambda: DummyMessage(self, user))
  86. return defer.fail(smtp.SMTPBadRcpt(user))
  87. mail = b'''\
  88. Subject: hello
  89. Goodbye
  90. '''
  91. class MyClient:
  92. def __init__(self, messageInfo=None):
  93. if messageInfo is None:
  94. messageInfo = (
  95. 'moshez@foo.bar', ['moshez@foo.bar'], BytesIO(mail))
  96. self._sender = messageInfo[0]
  97. self._recipient = messageInfo[1]
  98. self._data = messageInfo[2]
  99. def getMailFrom(self):
  100. return self._sender
  101. def getMailTo(self):
  102. return self._recipient
  103. def getMailData(self):
  104. return self._data
  105. def sendError(self, exc):
  106. self._error = exc
  107. def sentMail(self, code, resp, numOk, addresses, log):
  108. # Prevent another mail from being sent.
  109. self._sender = None
  110. self._recipient = None
  111. self._data = None
  112. class MySMTPClient(MyClient, smtp.SMTPClient):
  113. def __init__(self, messageInfo=None):
  114. smtp.SMTPClient.__init__(self, b'foo.baz')
  115. MyClient.__init__(self, messageInfo)
  116. class MyESMTPClient(MyClient, smtp.ESMTPClient):
  117. def __init__(self, secret = b'', contextFactory = None):
  118. smtp.ESMTPClient.__init__(self, secret, contextFactory, b'foo.baz')
  119. MyClient.__init__(self)
  120. class LoopbackMixin:
  121. def loopback(self, server, client):
  122. return loopback.loopbackTCP(server, client)
  123. class FakeSMTPServer(basic.LineReceiver):
  124. clientData = [
  125. b'220 hello', b'250 nice to meet you',
  126. b'250 great', b'250 great', b'354 go on, lad'
  127. ]
  128. def connectionMade(self):
  129. self.buffer = []
  130. self.clientData = self.clientData[:]
  131. self.clientData.reverse()
  132. self.sendLine(self.clientData.pop())
  133. def lineReceived(self, line):
  134. self.buffer.append(line)
  135. if line == b"QUIT":
  136. self.transport.write(b"221 see ya around\r\n")
  137. self.transport.loseConnection()
  138. elif line == b".":
  139. self.transport.write(b"250 gotcha\r\n")
  140. elif line == b"RSET":
  141. self.transport.loseConnection()
  142. if self.clientData:
  143. self.sendLine(self.clientData.pop())
  144. class SMTPClientTests(unittest.TestCase, LoopbackMixin):
  145. """
  146. Tests for L{smtp.SMTPClient}.
  147. """
  148. def test_timeoutConnection(self):
  149. """
  150. L{smtp.SMTPClient.timeoutConnection} calls the C{sendError} hook with a
  151. fatal L{SMTPTimeoutError} with the current line log.
  152. """
  153. errors = []
  154. client = MySMTPClient()
  155. client.sendError = errors.append
  156. client.makeConnection(StringTransport())
  157. client.lineReceived(b"220 hello")
  158. client.timeoutConnection()
  159. self.assertIsInstance(errors[0], smtp.SMTPTimeoutError)
  160. self.assertTrue(errors[0].isFatal)
  161. self.assertEqual(
  162. bytes(errors[0]),
  163. b"Timeout waiting for SMTP server response\n"
  164. b"<<< 220 hello\n"
  165. b">>> HELO foo.baz\n")
  166. expected_output = [
  167. b'HELO foo.baz', b'MAIL FROM:<moshez@foo.bar>',
  168. b'RCPT TO:<moshez@foo.bar>', b'DATA',
  169. b'Subject: hello', b'', b'Goodbye', b'.', b'RSET'
  170. ]
  171. def test_messages(self):
  172. """
  173. L{smtp.SMTPClient} sends I{HELO}, I{MAIL FROM}, I{RCPT TO}, and I{DATA}
  174. commands based on the return values of its C{getMailFrom},
  175. C{getMailTo}, and C{getMailData} methods.
  176. """
  177. client = MySMTPClient()
  178. server = FakeSMTPServer()
  179. d = self.loopback(server, client)
  180. d.addCallback(lambda x :
  181. self.assertEqual(server.buffer, self.expected_output))
  182. return d
  183. def test_transferError(self):
  184. """
  185. If there is an error while producing the message body to the
  186. connection, the C{sendError} callback is invoked.
  187. """
  188. client = MySMTPClient(
  189. ('alice@example.com', ['bob@example.com'], BytesIO(b"foo")))
  190. transport = StringTransport()
  191. client.makeConnection(transport)
  192. client.dataReceived(
  193. b'220 Ok\r\n' # Greeting
  194. b'250 Ok\r\n' # EHLO response
  195. b'250 Ok\r\n' # MAIL FROM response
  196. b'250 Ok\r\n' # RCPT TO response
  197. b'354 Ok\r\n' # DATA response
  198. )
  199. # Sanity check - a pull producer should be registered now.
  200. self.assertNotIdentical(transport.producer, None)
  201. self.assertFalse(transport.streaming)
  202. # Now stop the producer prematurely, meaning the message was not sent.
  203. transport.producer.stopProducing()
  204. # The sendError hook should have been invoked as a result.
  205. self.assertIsInstance(client._error, Exception)
  206. def test_sendFatalError(self):
  207. """
  208. If L{smtp.SMTPClient.sendError} is called with an L{SMTPClientError}
  209. which is fatal, it disconnects its transport without writing anything
  210. more to it.
  211. """
  212. client = smtp.SMTPClient(None)
  213. transport = StringTransport()
  214. client.makeConnection(transport)
  215. client.sendError(smtp.SMTPClientError(123, "foo", isFatal=True))
  216. self.assertEqual(transport.value(), b"")
  217. self.assertTrue(transport.disconnecting)
  218. def test_sendNonFatalError(self):
  219. """
  220. If L{smtp.SMTPClient.sendError} is called with an L{SMTPClientError}
  221. which is not fatal, it sends C{"QUIT"} and waits for the server to
  222. close the connection.
  223. """
  224. client = smtp.SMTPClient(None)
  225. transport = StringTransport()
  226. client.makeConnection(transport)
  227. client.sendError(smtp.SMTPClientError(123, "foo", isFatal=False))
  228. self.assertEqual(transport.value(), b"QUIT\r\n")
  229. self.assertFalse(transport.disconnecting)
  230. def test_sendOtherError(self):
  231. """
  232. If L{smtp.SMTPClient.sendError} is called with an exception which is
  233. not an L{SMTPClientError}, it disconnects its transport without
  234. writing anything more to it.
  235. """
  236. client = smtp.SMTPClient(None)
  237. transport = StringTransport()
  238. client.makeConnection(transport)
  239. client.sendError(Exception("foo"))
  240. self.assertEqual(transport.value(), b"")
  241. self.assertTrue(transport.disconnecting)
  242. class DummySMTPMessage(object):
  243. def __init__(self, protocol, users):
  244. self.protocol = protocol
  245. self.users = users
  246. self.buffer = []
  247. def lineReceived(self, line):
  248. self.buffer.append(line)
  249. def eomReceived(self):
  250. message = b'\n'.join(self.buffer) + b'\n'
  251. helo, origin = self.users[0].helo[0], bytes(self.users[0].orig)
  252. recipients = []
  253. for user in self.users:
  254. recipients.append(bytes(user))
  255. self.protocol.message[tuple(recipients)] = (helo, origin, recipients,
  256. message)
  257. return defer.succeed(b"saved")
  258. class DummyProto:
  259. def connectionMade(self):
  260. self.dummyMixinBase.connectionMade(self)
  261. self.message = {}
  262. def receivedHeader(*spam):
  263. return None
  264. def validateTo(self, user):
  265. self.delivery = SimpleDelivery(None)
  266. return lambda: DummySMTPMessage(self, [user])
  267. def validateFrom(self, helo, origin):
  268. return origin
  269. class DummySMTP(DummyProto, smtp.SMTP):
  270. dummyMixinBase = smtp.SMTP
  271. class DummyESMTP(DummyProto, smtp.ESMTP):
  272. dummyMixinBase = smtp.ESMTP
  273. class AnotherTestCase:
  274. serverClass = None
  275. clientClass = None
  276. messages = [ (b'foo.com', b'moshez@foo.com', [b'moshez@bar.com'],
  277. b'moshez@foo.com', [b'moshez@bar.com'], b'''\
  278. From: Moshe
  279. To: Moshe
  280. Hi,
  281. how are you?
  282. '''),
  283. (b'foo.com', b'tttt@rrr.com', [b'uuu@ooo', b'yyy@eee'],
  284. b'tttt@rrr.com', [b'uuu@ooo', b'yyy@eee'], b'''\
  285. Subject: pass
  286. ..rrrr..
  287. '''),
  288. (b'foo.com', b'@this,@is,@ignored:foo@bar.com',
  289. [b'@ignore,@this,@too:bar@foo.com'],
  290. b'foo@bar.com', [b'bar@foo.com'], b'''\
  291. Subject: apa
  292. To: foo
  293. 123
  294. .
  295. 456
  296. '''),
  297. ]
  298. data = [
  299. (b'', b'220.*\r\n$', None, None),
  300. (b'HELO foo.com\r\n', b'250.*\r\n$', None, None),
  301. (b'RSET\r\n', b'250.*\r\n$', None, None),
  302. ]
  303. for helo_, from_, to_, realfrom, realto, msg in messages:
  304. data.append((b'MAIL FROM:<' + from_ + b'>\r\n', b'250.*\r\n',
  305. None, None))
  306. for rcpt in to_:
  307. data.append((b'RCPT TO:<' + rcpt + b'>\r\n', b'250.*\r\n',
  308. None, None))
  309. data.append((b'DATA\r\n', b'354.*\r\n',
  310. msg, (b'250.*\r\n',
  311. (helo_, realfrom, realto, msg))))
  312. def test_buffer(self):
  313. """
  314. Exercise a lot of the SMTP client code. This is a "shotgun" style unit
  315. test. It does a lot of things and hopes that something will go really
  316. wrong if it is going to go wrong. This test should be replaced with a
  317. suite of nicer tests.
  318. """
  319. transport = StringTransport()
  320. a = self.serverClass()
  321. class fooFactory:
  322. domain = b'foo.com'
  323. a.factory = fooFactory()
  324. a.makeConnection(transport)
  325. for (send, expect, msg, msgexpect) in self.data:
  326. if send:
  327. a.dataReceived(send)
  328. data = transport.value()
  329. transport.clear()
  330. if not re.match(expect, data):
  331. raise AssertionError(send, expect, data)
  332. if data[:3] == b'354':
  333. for line in msg.splitlines():
  334. if line and line[0:1] == b'.':
  335. line = b'.' + line
  336. a.dataReceived(line + b'\r\n')
  337. a.dataReceived(b'.\r\n')
  338. # Special case for DATA. Now we want a 250, and then
  339. # we compare the messages
  340. data = transport.value()
  341. transport.clear()
  342. resp, msgdata = msgexpect
  343. if not re.match(resp, data):
  344. raise AssertionError(resp, data)
  345. for recip in msgdata[2]:
  346. expected = list(msgdata[:])
  347. expected[2] = [recip]
  348. self.assertEqual(
  349. a.message[(recip,)],
  350. tuple(expected)
  351. )
  352. a.setTimeout(None)
  353. class AnotherESMTPTests(AnotherTestCase, unittest.TestCase):
  354. serverClass = DummyESMTP
  355. clientClass = MyESMTPClient
  356. class AnotherSMTPTests(AnotherTestCase, unittest.TestCase):
  357. serverClass = DummySMTP
  358. clientClass = MySMTPClient
  359. @implementer(cred.checkers.ICredentialsChecker)
  360. class DummyChecker:
  361. users = {
  362. b'testuser': b'testpassword'
  363. }
  364. credentialInterfaces = (cred.credentials.IUsernamePassword,
  365. cred.credentials.IUsernameHashedPassword)
  366. def requestAvatarId(self, credentials):
  367. return defer.maybeDeferred(
  368. credentials.checkPassword, self.users[credentials.username]
  369. ).addCallback(self._cbCheck, credentials.username)
  370. def _cbCheck(self, result, username):
  371. if result:
  372. return username
  373. raise cred.error.UnauthorizedLogin()
  374. @implementer(smtp.IMessageDelivery)
  375. class SimpleDelivery(object):
  376. """
  377. L{SimpleDelivery} is a message delivery factory with no interesting
  378. behavior.
  379. """
  380. def __init__(self, messageFactory):
  381. self._messageFactory = messageFactory
  382. def receivedHeader(self, helo, origin, recipients):
  383. return None
  384. def validateFrom(self, helo, origin):
  385. return origin
  386. def validateTo(self, user):
  387. return lambda: self._messageFactory(user)
  388. class DummyRealm:
  389. def requestAvatar(self, avatarId, mind, *interfaces):
  390. return smtp.IMessageDelivery, SimpleDelivery(None), lambda: None
  391. class AuthTests(unittest.TestCase, LoopbackMixin):
  392. def test_crammd5Auth(self):
  393. """
  394. L{ESMTPClient} can authenticate using the I{CRAM-MD5} SASL mechanism.
  395. @see: U{http://tools.ietf.org/html/rfc2195}
  396. """
  397. realm = DummyRealm()
  398. p = cred.portal.Portal(realm)
  399. p.registerChecker(DummyChecker())
  400. server = DummyESMTP({b'CRAM-MD5': cred.credentials.CramMD5Credentials})
  401. server.portal = p
  402. client = MyESMTPClient(b'testpassword')
  403. cAuth = smtp.CramMD5ClientAuthenticator(b'testuser')
  404. client.registerAuthenticator(cAuth)
  405. d = self.loopback(server, client)
  406. d.addCallback(lambda x: self.assertEqual(server.authenticated, 1))
  407. return d
  408. def test_loginAuth(self):
  409. """
  410. L{ESMTPClient} can authenticate using the I{LOGIN} SASL mechanism.
  411. @see: U{http://sepp.oetiker.ch/sasl-2.1.19-ds/draft-murchison-sasl-login-00.txt}
  412. """
  413. realm = DummyRealm()
  414. p = cred.portal.Portal(realm)
  415. p.registerChecker(DummyChecker())
  416. server = DummyESMTP({b'LOGIN': LOGINCredentials})
  417. server.portal = p
  418. client = MyESMTPClient(b'testpassword')
  419. cAuth = smtp.LOGINAuthenticator(b'testuser')
  420. client.registerAuthenticator(cAuth)
  421. d = self.loopback(server, client)
  422. d.addCallback(lambda x: self.assertTrue(server.authenticated))
  423. return d
  424. def test_loginAgainstWeirdServer(self):
  425. """
  426. When communicating with a server which implements the I{LOGIN} SASL
  427. mechanism using C{"Username:"} as the challenge (rather than C{"User
  428. Name\\0"}), L{ESMTPClient} can still authenticate successfully using
  429. the I{LOGIN} mechanism.
  430. """
  431. realm = DummyRealm()
  432. p = cred.portal.Portal(realm)
  433. p.registerChecker(DummyChecker())
  434. server = DummyESMTP({b'LOGIN': smtp.LOGINCredentials})
  435. server.portal = p
  436. client = MyESMTPClient(b'testpassword')
  437. cAuth = smtp.LOGINAuthenticator(b'testuser')
  438. client.registerAuthenticator(cAuth)
  439. d = self.loopback(server, client)
  440. d.addCallback(lambda x: self.assertTrue(server.authenticated))
  441. return d
  442. class SMTPHelperTests(unittest.TestCase):
  443. def testMessageID(self):
  444. d = {}
  445. for i in range(1000):
  446. m = smtp.messageid('testcase')
  447. self.assertFalse(m in d)
  448. d[m] = None
  449. def testQuoteAddr(self):
  450. cases = [
  451. [b'user@host.name', b'<user@host.name>'],
  452. [b'"User Name" <user@host.name>', b'<user@host.name>'],
  453. [smtp.Address(b'someguy@someplace'), b'<someguy@someplace>'],
  454. [b'', b'<>'],
  455. [smtp.Address(b''), b'<>'],
  456. ]
  457. for (c, e) in cases:
  458. self.assertEqual(smtp.quoteaddr(c), e)
  459. def testUser(self):
  460. u = smtp.User(b'user@host', b'helo.host.name', None, None)
  461. self.assertEqual(str(u), 'user@host')
  462. def testXtextEncoding(self):
  463. cases = [
  464. (u'Hello world', b'Hello+20world'),
  465. (u'Hello+world', b'Hello+2Bworld'),
  466. (u'\0\1\2\3\4\5', b'+00+01+02+03+04+05'),
  467. (u'e=mc2@example.com', b'e+3Dmc2@example.com')
  468. ]
  469. for (case, expected) in cases:
  470. self.assertEqual(smtp.xtext_encode(case), (expected, len(case)))
  471. self.assertEqual(case.encode('xtext'), expected)
  472. self.assertEqual(
  473. smtp.xtext_decode(expected), (case, len(expected)))
  474. self.assertEqual(expected.decode('xtext'), case)
  475. def test_encodeWithErrors(self):
  476. """
  477. Specifying an error policy to C{unicode.encode} with the
  478. I{xtext} codec should produce the same result as not
  479. specifying the error policy.
  480. """
  481. text = u'Hello world'
  482. self.assertEqual(
  483. smtp.xtext_encode(text, 'strict'),
  484. (text.encode('xtext'), len(text)))
  485. self.assertEqual(
  486. text.encode('xtext', 'strict'),
  487. text.encode('xtext'))
  488. def test_decodeWithErrors(self):
  489. """
  490. Similar to L{test_encodeWithErrors}, but for C{bytes.decode}.
  491. """
  492. bytes = b'Hello world'
  493. self.assertEqual(
  494. smtp.xtext_decode(bytes, 'strict'),
  495. (bytes.decode('xtext'), len(bytes)))
  496. self.assertEqual(
  497. bytes.decode('xtext', 'strict'),
  498. bytes.decode('xtext'))
  499. class NoticeTLSClient(MyESMTPClient):
  500. tls = False
  501. def esmtpState_starttls(self, code, resp):
  502. MyESMTPClient.esmtpState_starttls(self, code, resp)
  503. self.tls = True
  504. class TLSTests(unittest.TestCase, LoopbackMixin):
  505. if sslSkip is not None:
  506. skip = sslSkip
  507. def testTLS(self):
  508. clientCTX = ClientTLSContext()
  509. serverCTX = ServerTLSContext()
  510. client = NoticeTLSClient(contextFactory=clientCTX)
  511. server = DummyESMTP(contextFactory=serverCTX)
  512. def check(ignored):
  513. self.assertEqual(client.tls, True)
  514. self.assertEqual(server.startedTLS, True)
  515. return self.loopback(server, client).addCallback(check)
  516. if not interfaces.IReactorSSL.providedBy(reactor):
  517. for case in (TLSTests,):
  518. case.skip = "Reactor doesn't support SSL"
  519. class EmptyLineTests(unittest.TestCase):
  520. def test_emptyLineSyntaxError(self):
  521. """
  522. If L{smtp.SMTP} receives an empty line, it responds with a 500 error
  523. response code and a message about a syntax error.
  524. """
  525. proto = smtp.SMTP()
  526. transport = StringTransport()
  527. proto.makeConnection(transport)
  528. proto.lineReceived(b'')
  529. proto.setTimeout(None)
  530. out = transport.value().splitlines()
  531. self.assertEqual(len(out), 2)
  532. self.assertTrue(out[0].startswith(b'220'))
  533. self.assertEqual(out[1], b"500 Error: bad syntax")
  534. class TimeoutTests(unittest.TestCase, LoopbackMixin):
  535. """
  536. Check that SMTP client factories correctly use the timeout.
  537. """
  538. def _timeoutTest(self, onDone, clientFactory):
  539. """
  540. Connect the clientFactory, and check the timeout on the request.
  541. """
  542. clock = task.Clock()
  543. client = clientFactory.buildProtocol(
  544. address.IPv4Address('TCP', 'example.net', 25))
  545. client.callLater = clock.callLater
  546. t = StringTransport()
  547. client.makeConnection(t)
  548. t.protocol = client
  549. def check(ign):
  550. self.assertEqual(clock.seconds(), 0.5)
  551. d = self.assertFailure(onDone, smtp.SMTPTimeoutError
  552. ).addCallback(check)
  553. # The first call should not trigger the timeout
  554. clock.advance(0.1)
  555. # But this one should
  556. clock.advance(0.4)
  557. return d
  558. def test_SMTPClient(self):
  559. """
  560. Test timeout for L{smtp.SMTPSenderFactory}: the response L{Deferred}
  561. should be errback with a L{smtp.SMTPTimeoutError}.
  562. """
  563. onDone = defer.Deferred()
  564. clientFactory = smtp.SMTPSenderFactory(
  565. 'source@address', 'recipient@address',
  566. BytesIO(b"Message body"), onDone,
  567. retries=0, timeout=0.5)
  568. return self._timeoutTest(onDone, clientFactory)
  569. def test_ESMTPClient(self):
  570. """
  571. Test timeout for L{smtp.ESMTPSenderFactory}: the response L{Deferred}
  572. should be errback with a L{smtp.SMTPTimeoutError}.
  573. """
  574. onDone = defer.Deferred()
  575. clientFactory = smtp.ESMTPSenderFactory(
  576. 'username', 'password',
  577. 'source@address', 'recipient@address',
  578. BytesIO(b"Message body"), onDone,
  579. retries=0, timeout=0.5)
  580. return self._timeoutTest(onDone, clientFactory)
  581. def test_resetTimeoutWhileSending(self):
  582. """
  583. The timeout is not allowed to expire after the server has accepted a
  584. DATA command and the client is actively sending data to it.
  585. """
  586. class SlowFile:
  587. """
  588. A file-like which returns one byte from each read call until the
  589. specified number of bytes have been returned.
  590. """
  591. def __init__(self, size):
  592. self._size = size
  593. def read(self, max=None):
  594. if self._size:
  595. self._size -= 1
  596. return b'x'
  597. return b''
  598. failed = []
  599. onDone = defer.Deferred()
  600. onDone.addErrback(failed.append)
  601. clientFactory = smtp.SMTPSenderFactory(
  602. 'source@address', 'recipient@address',
  603. SlowFile(1), onDone, retries=0, timeout=3)
  604. clientFactory.domain = b"example.org"
  605. clock = task.Clock()
  606. client = clientFactory.buildProtocol(
  607. address.IPv4Address('TCP', 'example.net', 25))
  608. client.callLater = clock.callLater
  609. transport = StringTransport()
  610. client.makeConnection(transport)
  611. client.dataReceived(
  612. b"220 Ok\r\n" # Greet the client
  613. b"250 Ok\r\n" # Respond to HELO
  614. b"250 Ok\r\n" # Respond to MAIL FROM
  615. b"250 Ok\r\n" # Respond to RCPT TO
  616. b"354 Ok\r\n" # Respond to DATA
  617. )
  618. # Now the client is producing data to the server. Any time
  619. # resumeProducing is called on the producer, the timeout should be
  620. # extended. First, a sanity check. This test is only written to
  621. # handle pull producers.
  622. self.assertNotIdentical(transport.producer, None)
  623. self.assertFalse(transport.streaming)
  624. # Now, allow 2 seconds (1 less than the timeout of 3 seconds) to
  625. # elapse.
  626. clock.advance(2)
  627. # The timeout has not expired, so the failure should not have happened.
  628. self.assertEqual(failed, [])
  629. # Let some bytes be produced, extending the timeout. Then advance the
  630. # clock some more and verify that the timeout still hasn't happened.
  631. transport.producer.resumeProducing()
  632. clock.advance(2)
  633. self.assertEqual(failed, [])
  634. # The file has been completely produced - the next resume producing
  635. # finishes the upload, successfully.
  636. transport.producer.resumeProducing()
  637. client.dataReceived(b"250 Ok\r\n")
  638. self.assertEqual(failed, [])
  639. # Verify that the client actually did send the things expected.
  640. self.assertEqual(
  641. transport.value(),
  642. b"HELO example.org\r\n"
  643. b"MAIL FROM:<source@address>\r\n"
  644. b"RCPT TO:<recipient@address>\r\n"
  645. b"DATA\r\n"
  646. b"x\r\n"
  647. b".\r\n"
  648. # This RSET is just an implementation detail. It's nice, but this
  649. # test doesn't really care about it.
  650. b"RSET\r\n")
  651. class MultipleDeliveryFactorySMTPServerFactory(protocol.ServerFactory):
  652. """
  653. L{MultipleDeliveryFactorySMTPServerFactory} creates SMTP server protocol
  654. instances with message delivery factory objects supplied to it. Each
  655. factory is used for one connection and then discarded. Factories are used
  656. in the order they are supplied.
  657. """
  658. def __init__(self, messageFactories):
  659. self._messageFactories = messageFactories
  660. def buildProtocol(self, addr):
  661. p = protocol.ServerFactory.buildProtocol(self, addr)
  662. p.delivery = SimpleDelivery(self._messageFactories.pop(0))
  663. return p
  664. class SMTPSenderFactoryTests(unittest.TestCase):
  665. """
  666. Tests for L{smtp.SMTPSenderFactory}.
  667. """
  668. def test_removeCurrentProtocolWhenClientConnectionLost(self):
  669. """
  670. L{smtp.SMTPSenderFactory} removes the current protocol when the client
  671. connection is lost.
  672. """
  673. reactor = MemoryReactor()
  674. sentDeferred = defer.Deferred()
  675. clientFactory = smtp.SMTPSenderFactory(
  676. "source@address", "recipient@address",
  677. BytesIO(b"message"), sentDeferred)
  678. connector = reactor.connectTCP("localhost", 25, clientFactory)
  679. clientFactory.buildProtocol(None)
  680. clientFactory.clientConnectionLost(connector,
  681. error.ConnectionDone("Bye."))
  682. self.assertEqual(clientFactory.currentProtocol, None)
  683. def test_removeCurrentProtocolWhenClientConnectionFailed(self):
  684. """
  685. L{smtp.SMTPSenderFactory} removes the current protocol when the client
  686. connection is failed.
  687. """
  688. reactor = MemoryReactor()
  689. sentDeferred = defer.Deferred()
  690. clientFactory = smtp.SMTPSenderFactory(
  691. "source@address", "recipient@address",
  692. BytesIO(b"message"), sentDeferred)
  693. connector = reactor.connectTCP("localhost", 25, clientFactory)
  694. clientFactory.buildProtocol(None)
  695. clientFactory.clientConnectionFailed(connector,
  696. error.ConnectionDone("Bye."))
  697. self.assertEqual(clientFactory.currentProtocol, None)
  698. class SMTPSenderFactoryRetryTests(unittest.TestCase):
  699. """
  700. Tests for the retry behavior of L{smtp.SMTPSenderFactory}.
  701. """
  702. def test_retryAfterDisconnect(self):
  703. """
  704. If the protocol created by L{SMTPSenderFactory} loses its connection
  705. before receiving confirmation of message delivery, it reconnects and
  706. tries to deliver the message again.
  707. """
  708. recipient = b'alice'
  709. message = b"some message text"
  710. domain = DummyDomain([recipient])
  711. class CleanSMTP(smtp.SMTP):
  712. """
  713. An SMTP subclass which ensures that its transport will be
  714. disconnected before the test ends.
  715. """
  716. def makeConnection(innerSelf, transport):
  717. self.addCleanup(transport.loseConnection)
  718. smtp.SMTP.makeConnection(innerSelf, transport)
  719. # Create a server which will fail the first message deliver attempt to
  720. # it with a 500 and a disconnect, but which will accept a message
  721. # delivered over the 2nd connection to it.
  722. serverFactory = MultipleDeliveryFactorySMTPServerFactory([
  723. BrokenMessage,
  724. lambda user: DummyMessage(domain, user)])
  725. serverFactory.protocol = CleanSMTP
  726. serverPort = reactor.listenTCP(0, serverFactory, interface='127.0.0.1')
  727. serverHost = serverPort.getHost()
  728. self.addCleanup(serverPort.stopListening)
  729. # Set up a client to try to deliver a message to the above created
  730. # server.
  731. sentDeferred = defer.Deferred()
  732. clientFactory = smtp.SMTPSenderFactory(
  733. b"bob@example.org", recipient + b"@example.com",
  734. BytesIO(message), sentDeferred)
  735. clientFactory.domain = b"example.org"
  736. clientConnector = reactor.connectTCP(
  737. serverHost.host, serverHost.port, clientFactory)
  738. self.addCleanup(clientConnector.disconnect)
  739. def cbSent(ignored):
  740. """
  741. Verify that the message was successfully delivered and flush the
  742. error which caused the first attempt to fail.
  743. """
  744. self.assertEqual(
  745. domain.messages,
  746. {recipient: [b"\n" + message + b"\n"]})
  747. # Flush the RuntimeError that BrokenMessage caused to be logged.
  748. self.assertEqual(len(self.flushLoggedErrors(RuntimeError)), 1)
  749. sentDeferred.addCallback(cbSent)
  750. return sentDeferred
  751. @implementer(IRealm)
  752. class SingletonRealm(object):
  753. """
  754. Trivial realm implementation which is constructed with an interface and an
  755. avatar and returns that avatar when asked for that interface.
  756. """
  757. def __init__(self, interface, avatar):
  758. self.interface = interface
  759. self.avatar = avatar
  760. def requestAvatar(self, avatarId, mind, *interfaces):
  761. for iface in interfaces:
  762. if iface is self.interface:
  763. return iface, self.avatar, lambda: None
  764. class NotImplementedDelivery(object):
  765. """
  766. Non-implementation of L{smtp.IMessageDelivery} which only has methods which
  767. raise L{NotImplementedError}. Subclassed by various tests to provide the
  768. particular behavior being tested.
  769. """
  770. def validateFrom(self, helo, origin):
  771. raise NotImplementedError("This oughtn't be called in the course of this test.")
  772. def validateTo(self, user):
  773. raise NotImplementedError("This oughtn't be called in the course of this test.")
  774. def receivedHeader(self, helo, origin, recipients):
  775. raise NotImplementedError("This oughtn't be called in the course of this test.")
  776. class SMTPServerTests(unittest.TestCase):
  777. """
  778. Test various behaviors of L{twisted.mail.smtp.SMTP} and
  779. L{twisted.mail.smtp.ESMTP}.
  780. """
  781. def testSMTPGreetingHost(self, serverClass=smtp.SMTP):
  782. """
  783. Test that the specified hostname shows up in the SMTP server's
  784. greeting.
  785. """
  786. s = serverClass()
  787. s.host = b"example.com"
  788. t = StringTransport()
  789. s.makeConnection(t)
  790. s.connectionLost(error.ConnectionDone())
  791. self.assertIn(b"example.com", t.value())
  792. def testSMTPGreetingNotExtended(self):
  793. """
  794. Test that the string "ESMTP" does not appear in the SMTP server's
  795. greeting since that string strongly suggests the presence of support
  796. for various SMTP extensions which are not supported by L{smtp.SMTP}.
  797. """
  798. s = smtp.SMTP()
  799. t = StringTransport()
  800. s.makeConnection(t)
  801. s.connectionLost(error.ConnectionDone())
  802. self.assertNotIn(b"ESMTP", t.value())
  803. def testESMTPGreetingHost(self):
  804. """
  805. Similar to testSMTPGreetingHost, but for the L{smtp.ESMTP} class.
  806. """
  807. self.testSMTPGreetingHost(smtp.ESMTP)
  808. def testESMTPGreetingExtended(self):
  809. """
  810. Test that the string "ESMTP" does appear in the ESMTP server's
  811. greeting since L{smtp.ESMTP} does support the SMTP extensions which
  812. that advertises to the client.
  813. """
  814. s = smtp.ESMTP()
  815. t = StringTransport()
  816. s.makeConnection(t)
  817. s.connectionLost(error.ConnectionDone())
  818. self.assertIn(b"ESMTP", t.value())
  819. def test_SMTPUnknownCommand(self):
  820. """
  821. Sending an unimplemented command is responded to with a 500.
  822. """
  823. s = smtp.SMTP()
  824. t = StringTransport()
  825. s.makeConnection(t)
  826. s.lineReceived(b"DOAGOODTHING")
  827. s.connectionLost(error.ConnectionDone())
  828. self.assertIn(b"500 Command not implemented", t.value())
  829. def test_acceptSenderAddress(self):
  830. """
  831. Test that a C{MAIL FROM} command with an acceptable address is
  832. responded to with the correct success code.
  833. """
  834. class AcceptanceDelivery(NotImplementedDelivery):
  835. """
  836. Delivery object which accepts all senders as valid.
  837. """
  838. def validateFrom(self, helo, origin):
  839. return origin
  840. realm = SingletonRealm(smtp.IMessageDelivery, AcceptanceDelivery())
  841. portal = Portal(realm, [AllowAnonymousAccess()])
  842. proto = smtp.SMTP()
  843. proto.portal = portal
  844. trans = StringTransport()
  845. proto.makeConnection(trans)
  846. # Deal with the necessary preliminaries
  847. proto.dataReceived(b'HELO example.com\r\n')
  848. trans.clear()
  849. # Try to specify our sender address
  850. proto.dataReceived(b'MAIL FROM:<alice@example.com>\r\n')
  851. # Clean up the protocol before doing anything that might raise an
  852. # exception.
  853. proto.connectionLost(error.ConnectionLost())
  854. # Make sure that we received exactly the correct response
  855. self.assertEqual(
  856. trans.value(),
  857. b'250 Sender address accepted\r\n')
  858. def test_deliveryRejectedSenderAddress(self):
  859. """
  860. Test that a C{MAIL FROM} command with an address rejected by a
  861. L{smtp.IMessageDelivery} instance is responded to with the correct
  862. error code.
  863. """
  864. class RejectionDelivery(NotImplementedDelivery):
  865. """
  866. Delivery object which rejects all senders as invalid.
  867. """
  868. def validateFrom(self, helo, origin):
  869. raise smtp.SMTPBadSender(origin)
  870. realm = SingletonRealm(smtp.IMessageDelivery, RejectionDelivery())
  871. portal = Portal(realm, [AllowAnonymousAccess()])
  872. proto = smtp.SMTP()
  873. proto.portal = portal
  874. trans = StringTransport()
  875. proto.makeConnection(trans)
  876. # Deal with the necessary preliminaries
  877. proto.dataReceived(b'HELO example.com\r\n')
  878. trans.clear()
  879. # Try to specify our sender address
  880. proto.dataReceived(b'MAIL FROM:<alice@example.com>\r\n')
  881. # Clean up the protocol before doing anything that might raise an
  882. # exception.
  883. proto.connectionLost(error.ConnectionLost())
  884. # Make sure that we received exactly the correct response
  885. self.assertEqual(
  886. trans.value(),
  887. b'550 Cannot receive from specified address '
  888. b'<alice@example.com>: Sender not acceptable\r\n')
  889. @implementer(ICredentialsChecker)
  890. def test_portalRejectedSenderAddress(self):
  891. """
  892. Test that a C{MAIL FROM} command with an address rejected by an
  893. L{smtp.SMTP} instance's portal is responded to with the correct error
  894. code.
  895. """
  896. class DisallowAnonymousAccess(object):
  897. """
  898. Checker for L{IAnonymous} which rejects authentication attempts.
  899. """
  900. credentialInterfaces = (IAnonymous,)
  901. def requestAvatarId(self, credentials):
  902. return defer.fail(UnauthorizedLogin())
  903. realm = SingletonRealm(smtp.IMessageDelivery, NotImplementedDelivery())
  904. portal = Portal(realm, [DisallowAnonymousAccess()])
  905. proto = smtp.SMTP()
  906. proto.portal = portal
  907. trans = StringTransport()
  908. proto.makeConnection(trans)
  909. # Deal with the necessary preliminaries
  910. proto.dataReceived(b'HELO example.com\r\n')
  911. trans.clear()
  912. # Try to specify our sender address
  913. proto.dataReceived(b'MAIL FROM:<alice@example.com>\r\n')
  914. # Clean up the protocol before doing anything that might raise an
  915. # exception.
  916. proto.connectionLost(error.ConnectionLost())
  917. # Make sure that we received exactly the correct response
  918. self.assertEqual(
  919. trans.value(),
  920. b'550 Cannot receive from specified address '
  921. b'<alice@example.com>: Sender not acceptable\r\n')
  922. def test_portalRejectedAnonymousSender(self):
  923. """
  924. Test that a C{MAIL FROM} command issued without first authenticating
  925. when a portal has been configured to disallow anonymous logins is
  926. responded to with the correct error code.
  927. """
  928. realm = SingletonRealm(smtp.IMessageDelivery, NotImplementedDelivery())
  929. portal = Portal(realm, [])
  930. proto = smtp.SMTP()
  931. proto.portal = portal
  932. trans = StringTransport()
  933. proto.makeConnection(trans)
  934. # Deal with the necessary preliminaries
  935. proto.dataReceived(b'HELO example.com\r\n')
  936. trans.clear()
  937. # Try to specify our sender address
  938. proto.dataReceived(b'MAIL FROM:<alice@example.com>\r\n')
  939. # Clean up the protocol before doing anything that might raise an
  940. # exception.
  941. proto.connectionLost(error.ConnectionLost())
  942. # Make sure that we received exactly the correct response
  943. self.assertEqual(
  944. trans.value(),
  945. b'550 Cannot receive from specified address '
  946. b'<alice@example.com>: Unauthenticated senders not allowed\r\n')
  947. class ESMTPAuthenticationTests(unittest.TestCase):
  948. def assertServerResponse(self, bytes, response):
  949. """
  950. Assert that when the given bytes are delivered to the ESMTP server
  951. instance, it responds with the indicated lines.
  952. @type bytes: str
  953. @type response: list of str
  954. """
  955. self.transport.clear()
  956. self.server.dataReceived(bytes)
  957. self.assertEqual(
  958. response,
  959. self.transport.value().splitlines())
  960. def assertServerAuthenticated(self, loginArgs, username=b"username",
  961. password=b"password"):
  962. """
  963. Assert that a login attempt has been made, that the credentials and
  964. interfaces passed to it are correct, and that when the login request
  965. is satisfied, a successful response is sent by the ESMTP server
  966. instance.
  967. @param loginArgs: A C{list} previously passed to L{portalFactory}.
  968. @param username: The login user.
  969. @param password: The login password.
  970. """
  971. d, credentials, mind, interfaces = loginArgs.pop()
  972. self.assertEqual(loginArgs, [])
  973. self.assertTrue(twisted.cred.credentials.IUsernamePassword.providedBy(credentials))
  974. self.assertEqual(credentials.username, username)
  975. self.assertTrue(credentials.checkPassword(password))
  976. self.assertIn(smtp.IMessageDeliveryFactory, interfaces)
  977. self.assertIn(smtp.IMessageDelivery, interfaces)
  978. d.callback((smtp.IMessageDeliveryFactory, None, lambda: None))
  979. self.assertEqual(
  980. [b"235 Authentication successful."],
  981. self.transport.value().splitlines())
  982. def setUp(self):
  983. """
  984. Create an ESMTP instance attached to a StringTransport.
  985. """
  986. self.server = smtp.ESMTP({
  987. b'LOGIN': LOGINCredentials})
  988. self.server.host = b'localhost'
  989. self.transport = StringTransport(
  990. peerAddress=address.IPv4Address('TCP', '127.0.0.1', 12345))
  991. self.server.makeConnection(self.transport)
  992. def tearDown(self):
  993. """
  994. Disconnect the ESMTP instance to clean up its timeout DelayedCall.
  995. """
  996. self.server.connectionLost(error.ConnectionDone())
  997. def portalFactory(self, loginList):
  998. class DummyPortal:
  999. def login(self, credentials, mind, *interfaces):
  1000. d = defer.Deferred()
  1001. loginList.append((d, credentials, mind, interfaces))
  1002. return d
  1003. return DummyPortal()
  1004. def test_authenticationCapabilityAdvertised(self):
  1005. """
  1006. Test that AUTH is advertised to clients which issue an EHLO command.
  1007. """
  1008. self.transport.clear()
  1009. self.server.dataReceived(b'EHLO\r\n')
  1010. responseLines = self.transport.value().splitlines()
  1011. self.assertEqual(
  1012. responseLines[0],
  1013. b"250-localhost Hello 127.0.0.1, nice to meet you")
  1014. self.assertEqual(
  1015. responseLines[1],
  1016. b"250 AUTH LOGIN")
  1017. self.assertEqual(len(responseLines), 2)
  1018. def test_plainAuthentication(self):
  1019. """
  1020. Test that the LOGIN authentication mechanism can be used
  1021. """
  1022. loginArgs = []
  1023. self.server.portal = self.portalFactory(loginArgs)
  1024. self.server.dataReceived(b'EHLO\r\n')
  1025. self.transport.clear()
  1026. self.assertServerResponse(
  1027. b'AUTH LOGIN\r\n',
  1028. [b"334 " + base64.b64encode(b"User Name\0").strip()])
  1029. self.assertServerResponse(
  1030. base64.b64encode(b'username') + b'\r\n',
  1031. [b"334 " + base64.b64encode(b"Password\0").strip()])
  1032. self.assertServerResponse(
  1033. base64.b64encode(b'password').strip() + b'\r\n',
  1034. [])
  1035. self.assertServerAuthenticated(loginArgs)
  1036. def test_plainAuthenticationEmptyPassword(self):
  1037. """
  1038. Test that giving an empty password for plain auth succeeds.
  1039. """
  1040. loginArgs = []
  1041. self.server.portal = self.portalFactory(loginArgs)
  1042. self.server.dataReceived(b'EHLO\r\n')
  1043. self.transport.clear()
  1044. self.assertServerResponse(
  1045. b'AUTH LOGIN\r\n',
  1046. [b"334 " + base64.b64encode(b"User Name\0").strip()])
  1047. self.assertServerResponse(
  1048. base64.b64encode(b'username') + b'\r\n',
  1049. [b"334 " + base64.b64encode(b"Password\0").strip()])
  1050. self.assertServerResponse(b'\r\n', [])
  1051. self.assertServerAuthenticated(loginArgs, password=b'')
  1052. def test_plainAuthenticationInitialResponse(self):
  1053. """
  1054. The response to the first challenge may be included on the AUTH command
  1055. line. Test that this is also supported.
  1056. """
  1057. loginArgs = []
  1058. self.server.portal = self.portalFactory(loginArgs)
  1059. self.server.dataReceived(b'EHLO\r\n')
  1060. self.transport.clear()
  1061. self.assertServerResponse(
  1062. b'AUTH LOGIN ' + base64.b64encode(b"username").strip() + b'\r\n',
  1063. [b"334 " + base64.b64encode(b"Password\0").strip()])
  1064. self.assertServerResponse(
  1065. base64.b64encode(b'password').strip() + b'\r\n',
  1066. [])
  1067. self.assertServerAuthenticated(loginArgs)
  1068. def test_abortAuthentication(self):
  1069. """
  1070. Test that a challenge/response sequence can be aborted by the client.
  1071. """
  1072. loginArgs = []
  1073. self.server.portal = self.portalFactory(loginArgs)
  1074. self.server.dataReceived(b'EHLO\r\n')
  1075. self.server.dataReceived(b'AUTH LOGIN\r\n')
  1076. self.assertServerResponse(
  1077. b'*\r\n',
  1078. [b'501 Authentication aborted'])
  1079. def test_invalidBase64EncodedResponse(self):
  1080. """
  1081. Test that a response which is not properly Base64 encoded results in
  1082. the appropriate error code.
  1083. """
  1084. loginArgs = []
  1085. self.server.portal = self.portalFactory(loginArgs)
  1086. self.server.dataReceived(b'EHLO\r\n')
  1087. self.server.dataReceived(b'AUTH LOGIN\r\n')
  1088. self.assertServerResponse(
  1089. b'x\r\n',
  1090. [b'501 Syntax error in parameters or arguments'])
  1091. self.assertEqual(loginArgs, [])
  1092. def test_invalidBase64EncodedInitialResponse(self):
  1093. """
  1094. Like L{test_invalidBase64EncodedResponse} but for the case of an
  1095. initial response included with the C{AUTH} command.
  1096. """
  1097. loginArgs = []
  1098. self.server.portal = self.portalFactory(loginArgs)
  1099. self.server.dataReceived(b'EHLO\r\n')
  1100. self.assertServerResponse(
  1101. b'AUTH LOGIN x\r\n',
  1102. [b'501 Syntax error in parameters or arguments'])
  1103. self.assertEqual(loginArgs, [])
  1104. def test_unexpectedLoginFailure(self):
  1105. """
  1106. If the L{Deferred} returned by L{Portal.login} fires with an
  1107. exception of any type other than L{UnauthorizedLogin}, the exception
  1108. is logged and the client is informed that the authentication attempt
  1109. has failed.
  1110. """
  1111. loginArgs = []
  1112. self.server.portal = self.portalFactory(loginArgs)
  1113. self.server.dataReceived(b'EHLO\r\n')
  1114. self.transport.clear()
  1115. self.assertServerResponse(
  1116. b'AUTH LOGIN ' + base64.b64encode(b'username').strip() + b'\r\n',
  1117. [b'334 ' + base64.b64encode(b'Password\0').strip()])
  1118. self.assertServerResponse(
  1119. base64.b64encode(b'password').strip() + b'\r\n',
  1120. [])
  1121. d, credentials, mind, interfaces = loginArgs.pop()
  1122. d.errback(RuntimeError("Something wrong with the server"))
  1123. self.assertEqual(
  1124. b'451 Requested action aborted: local error in processing\r\n',
  1125. self.transport.value())
  1126. self.assertEqual(len(self.flushLoggedErrors(RuntimeError)), 1)
  1127. class SMTPClientErrorTests(unittest.TestCase):
  1128. """
  1129. Tests for L{smtp.SMTPClientError}.
  1130. """
  1131. def test_str(self):
  1132. """
  1133. The string representation of a L{SMTPClientError} instance includes
  1134. the response code and response string.
  1135. """
  1136. err = smtp.SMTPClientError(123, "some text")
  1137. self.assertEqual(str(err), "123 some text")
  1138. def test_strWithNegativeCode(self):
  1139. """
  1140. If the response code supplied to L{SMTPClientError} is negative, it
  1141. is excluded from the string representation.
  1142. """
  1143. err = smtp.SMTPClientError(-1, b"foo bar")
  1144. self.assertEqual(str(err), "foo bar")
  1145. def test_strWithLog(self):
  1146. """
  1147. If a line log is supplied to L{SMTPClientError}, its contents are
  1148. included in the string representation of the exception instance.
  1149. """
  1150. log = LineLog(10)
  1151. log.append(b"testlog")
  1152. log.append(b"secondline")
  1153. err = smtp.SMTPClientError(100, "test error", log=log.str())
  1154. self.assertEqual(
  1155. str(err),
  1156. "100 test error\n"
  1157. "testlog\n"
  1158. "secondline\n")
  1159. class SenderMixinSentMailTests(unittest.TestCase):
  1160. """
  1161. Tests for L{smtp.SenderMixin.sentMail}, used in particular by
  1162. L{smtp.SMTPSenderFactory} and L{smtp.ESMTPSenderFactory}.
  1163. """
  1164. def test_onlyLogFailedAddresses(self):
  1165. """
  1166. L{smtp.SenderMixin.sentMail} adds only the addresses with failing
  1167. SMTP response codes to the log passed to the factory's errback.
  1168. """
  1169. onDone = self.assertFailure(defer.Deferred(), smtp.SMTPDeliveryError)
  1170. onDone.addCallback(lambda e: self.assertEqual(
  1171. e.log, b"bob@example.com: 199 Error in sending.\n"))
  1172. clientFactory = smtp.SMTPSenderFactory(
  1173. 'source@address', 'recipient@address',
  1174. BytesIO(b"Message body"), onDone,
  1175. retries=0, timeout=0.5)
  1176. client = clientFactory.buildProtocol(
  1177. address.IPv4Address('TCP', 'example.net', 25))
  1178. addresses = [(b"alice@example.com", 200, b"No errors here!"),
  1179. (b"bob@example.com", 199, b"Error in sending.")]
  1180. client.sentMail(199, b"Test response", 1, addresses, client.log)
  1181. return onDone
  1182. class ESMTPDowngradeTestCase(unittest.TestCase):
  1183. """
  1184. Tests for the ESMTP -> SMTP downgrade functionality in L{smtp.ESMTPClient}.
  1185. """
  1186. def setUp(self):
  1187. self.clientProtocol = smtp.ESMTPClient(
  1188. b"testpassword", None, b"testuser")
  1189. def test_requireHELOFallbackOperates(self):
  1190. """
  1191. If both authentication and transport security are not required, and it
  1192. is asked for, it will fall back to allowing HELO.
  1193. """
  1194. transport = StringTransport()
  1195. self.clientProtocol.requireAuthentication = False
  1196. self.clientProtocol.requireTransportSecurity = False
  1197. self.clientProtocol.heloFallback = True
  1198. self.clientProtocol.makeConnection(transport)
  1199. self.clientProtocol.dataReceived(b"220 localhost\r\n")
  1200. transport.clear()
  1201. self.clientProtocol.dataReceived(b"500 not an esmtp server\r\n")
  1202. self.assertEqual(b"HELO testuser\r\n", transport.value())
  1203. def test_requireAuthFailsHELOFallback(self):
  1204. """
  1205. If authentication is required, and HELO fallback is on, HELO fallback
  1206. must not be honoured, as authentication requires EHLO to succeed.
  1207. """
  1208. transport = StringTransport()
  1209. self.clientProtocol.requireAuthentication = True
  1210. self.clientProtocol.requireTransportSecurity = False
  1211. self.clientProtocol.heloFallback = True
  1212. self.clientProtocol.makeConnection(transport)
  1213. self.clientProtocol.dataReceived(b"220 localhost\r\n")
  1214. transport.clear()
  1215. self.clientProtocol.dataReceived(b"500 not an esmtp server\r\n")
  1216. self.assertEqual(b"QUIT\r\n", transport.value())
  1217. def test_requireTLSFailsHELOFallback(self):
  1218. """
  1219. If TLS is required and the connection is insecure, HELO fallback must
  1220. not be honoured, as STARTTLS requires EHLO to succeed.
  1221. """
  1222. transport = StringTransport()
  1223. self.clientProtocol.requireAuthentication = False
  1224. self.clientProtocol.requireTransportSecurity = True
  1225. self.clientProtocol.heloFallback = True
  1226. self.clientProtocol.makeConnection(transport)
  1227. self.clientProtocol.dataReceived(b"220 localhost\r\n")
  1228. transport.clear()
  1229. self.clientProtocol.dataReceived(b"500 not an esmtp server\r\n")
  1230. self.assertEqual(b"QUIT\r\n", transport.value())
  1231. def test_requireTLSAndHELOFallbackSucceedsIfOverTLS(self):
  1232. """
  1233. If TLS is provided at the transport level, we can honour the HELO
  1234. fallback if we're set to require TLS.
  1235. """
  1236. transport = StringTransport()
  1237. directlyProvides(transport, interfaces.ISSLTransport)
  1238. self.clientProtocol.requireAuthentication = False
  1239. self.clientProtocol.requireTransportSecurity = True
  1240. self.clientProtocol.heloFallback = True
  1241. self.clientProtocol.makeConnection(transport)
  1242. self.clientProtocol.dataReceived(b"220 localhost\r\n")
  1243. transport.clear()
  1244. self.clientProtocol.dataReceived(b"500 not an esmtp server\r\n")
  1245. self.assertEqual(b"HELO testuser\r\n", transport.value())
  1246. class SSLTestCase(unittest.TestCase):
  1247. """
  1248. Tests for the TLS negotiation done by L{smtp.ESMTPClient}.
  1249. """
  1250. if sslSkip is not None:
  1251. skip = sslSkip
  1252. SERVER_GREETING = b"220 localhost NO UCE NO UBE NO RELAY PROBES ESMTP\r\n"
  1253. EHLO_RESPONSE = b"250-localhost Hello 127.0.0.1, nice to meet you\r\n"
  1254. def setUp(self):
  1255. self.clientProtocol = smtp.ESMTPClient(
  1256. b"testpassword", ClientTLSContext(), b"testuser")
  1257. self.clientProtocol.requireTransportSecurity = True
  1258. self.clientProtocol.getMailFrom = lambda: "test@example.org"
  1259. def _requireTransportSecurityOverSSLTest(self, capabilities):
  1260. """
  1261. Verify that when L{smtp.ESMTPClient} connects to a server over a
  1262. transport providing L{ISSLTransport}, C{requireTransportSecurity} is
  1263. C{True}, and it is presented with the given capabilities, it will try
  1264. to send its mail and not first attempt to negotiate TLS using the
  1265. I{STARTTLS} protocol action.
  1266. @param capabilities: Bytes to include in the test server's capability
  1267. response. These must be formatted exactly as required by the
  1268. protocol, including a line which ends the capability response.
  1269. @type param: L{bytes}
  1270. @raise: C{self.failureException} if the behavior of
  1271. C{self.clientProtocol} is not as described.
  1272. """
  1273. transport = StringTransport()
  1274. directlyProvides(transport, interfaces.ISSLTransport)
  1275. self.clientProtocol.makeConnection(transport)
  1276. # Get the handshake out of the way
  1277. self.clientProtocol.dataReceived(self.SERVER_GREETING)
  1278. transport.clear()
  1279. # Tell the client about the server's capabilities
  1280. self.clientProtocol.dataReceived(self.EHLO_RESPONSE + capabilities)
  1281. # The client should now try to send a message - without first trying to
  1282. # negotiate TLS, since the transport is already secure.
  1283. self.assertEqual(
  1284. b"MAIL FROM:<test@example.org>\r\n",
  1285. transport.value())
  1286. def test_requireTransportSecurityOverSSL(self):
  1287. """
  1288. When C{requireTransportSecurity} is C{True} and the client is connected
  1289. over an SSL transport, mail may be delivered.
  1290. """
  1291. self._requireTransportSecurityOverSSLTest(b"250 AUTH LOGIN\r\n")
  1292. def test_requireTransportSecurityTLSOffered(self):
  1293. """
  1294. When C{requireTransportSecurity} is C{True} and the client is connected
  1295. over a non-SSL transport, if the server offers the I{STARTTLS}
  1296. extension, it is used before mail is delivered.
  1297. """
  1298. transport = StringTransport()
  1299. self.clientProtocol.makeConnection(transport)
  1300. # Get the handshake out of the way
  1301. self.clientProtocol.dataReceived(self.SERVER_GREETING)
  1302. transport.clear()
  1303. # Tell the client about the server's capabilities - including STARTTLS
  1304. self.clientProtocol.dataReceived(
  1305. self.EHLO_RESPONSE +
  1306. b"250-AUTH LOGIN\r\n"
  1307. b"250 STARTTLS\r\n")
  1308. # The client should try to start TLS before sending the message.
  1309. self.assertEqual(b"STARTTLS\r\n", transport.value())
  1310. def test_requireTransportSecurityTLSOfferedOverSSL(self):
  1311. """
  1312. When C{requireTransportSecurity} is C{True} and the client is connected
  1313. over an SSL transport, if the server offers the I{STARTTLS}
  1314. extension, it is not used before mail is delivered.
  1315. """
  1316. self._requireTransportSecurityOverSSLTest(
  1317. b"250-AUTH LOGIN\r\n"
  1318. b"250 STARTTLS\r\n")
  1319. def test_requireTransportSecurityTLSNotOffered(self):
  1320. """
  1321. When C{requireTransportSecurity} is C{True} and the client is connected
  1322. over a non-SSL transport, if the server does not offer the I{STARTTLS}
  1323. extension, mail is not delivered.
  1324. """
  1325. transport = StringTransport()
  1326. self.clientProtocol.makeConnection(transport)
  1327. # Get the handshake out of the way
  1328. self.clientProtocol.dataReceived(self.SERVER_GREETING)
  1329. transport.clear()
  1330. # Tell the client about the server's capabilities - excluding STARTTLS
  1331. self.clientProtocol.dataReceived(
  1332. self.EHLO_RESPONSE +
  1333. b"250 AUTH LOGIN\r\n")
  1334. # The client give up
  1335. self.assertEqual(b"QUIT\r\n", transport.value())
  1336. def test_esmtpClientTlsModeDeprecationGet(self):
  1337. """
  1338. L{smtp.ESMTPClient.tlsMode} is deprecated.
  1339. """
  1340. val = self.clientProtocol.tlsMode
  1341. del val
  1342. warningsShown = self.flushWarnings(
  1343. offendingFunctions=[self.test_esmtpClientTlsModeDeprecationGet])
  1344. self.assertEqual(len(warningsShown), 1)
  1345. self.assertIdentical(
  1346. warningsShown[0]['category'], DeprecationWarning)
  1347. self.assertEqual(
  1348. warningsShown[0]['message'],
  1349. "tlsMode attribute of twisted.mail.smtp.ESMTPClient "
  1350. "is deprecated since Twisted 13.0")
  1351. def test_esmtpClientTlsModeDeprecationGetAttributeError(self):
  1352. """
  1353. L{smtp.ESMTPClient.__getattr__} raises an attribute error for other
  1354. attribute names which do not exist.
  1355. """
  1356. self.assertRaises(
  1357. AttributeError, lambda: self.clientProtocol.doesNotExist)
  1358. def test_esmtpClientTlsModeDeprecationSet(self):
  1359. """
  1360. L{smtp.ESMTPClient.tlsMode} is deprecated.
  1361. """
  1362. self.clientProtocol.tlsMode = False
  1363. warningsShown = self.flushWarnings(
  1364. offendingFunctions=[self.test_esmtpClientTlsModeDeprecationSet])
  1365. self.assertEqual(len(warningsShown), 1)
  1366. self.assertIdentical(
  1367. warningsShown[0]['category'], DeprecationWarning)
  1368. self.assertEqual(
  1369. warningsShown[0]['message'],
  1370. "tlsMode attribute of twisted.mail.smtp.ESMTPClient "
  1371. "is deprecated since Twisted 13.0")
  1372. class AbortableStringTransport(StringTransport):
  1373. """
  1374. A version of L{StringTransport} that supports C{abortConnection}.
  1375. """
  1376. # This should be replaced by a common version in #6530.
  1377. aborting = False
  1378. def abortConnection(self):
  1379. """
  1380. A testable version of the C{ITCPTransport.abortConnection} method.
  1381. Since this is a special case of closing the connection,
  1382. C{loseConnection} is also called.
  1383. """
  1384. self.aborting = True
  1385. self.loseConnection()
  1386. class SendmailTests(unittest.TestCase):
  1387. """
  1388. Tests for L{twisted.mail.smtp.sendmail}.
  1389. """
  1390. def test_defaultReactorIsGlobalReactor(self):
  1391. """
  1392. The default C{reactor} parameter of L{twisted.mail.smtp.sendmail} is
  1393. L{twisted.internet.reactor}.
  1394. """
  1395. args, varArgs, keywords, defaults = inspect.getargspec(smtp.sendmail)
  1396. self.assertEqual(reactor, defaults[2])
  1397. def test_honorsESMTPArguments(self):
  1398. """
  1399. L{twisted.mail.smtp.sendmail} creates the ESMTP factory with the ESMTP
  1400. arguments.
  1401. """
  1402. reactor = MemoryReactor()
  1403. smtp.sendmail("localhost", "source@address", "recipient@address",
  1404. b"message", reactor=reactor, username=b"foo",
  1405. password=b"bar", requireTransportSecurity=True,
  1406. requireAuthentication=True)
  1407. factory = reactor.tcpClients[0][2]
  1408. self.assertEqual(factory._requireTransportSecurity, True)
  1409. self.assertEqual(factory._requireAuthentication, True)
  1410. self.assertEqual(factory.username, b"foo")
  1411. self.assertEqual(factory.password, b"bar")
  1412. def test_messageFilePassthrough(self):
  1413. """
  1414. L{twisted.mail.smtp.sendmail} will pass through the message untouched
  1415. if it is a file-like object.
  1416. """
  1417. reactor = MemoryReactor()
  1418. messageFile = BytesIO(b"File!")
  1419. smtp.sendmail("localhost", "source@address", "recipient@address",
  1420. messageFile, reactor=reactor)
  1421. factory = reactor.tcpClients[0][2]
  1422. self.assertIs(factory.file, messageFile)
  1423. def test_messageStringMadeFile(self):
  1424. """
  1425. L{twisted.mail.smtp.sendmail} will turn non-file-like objects
  1426. (eg. strings) into file-like objects before sending.
  1427. """
  1428. reactor = MemoryReactor()
  1429. smtp.sendmail("localhost", "source@address", "recipient@address",
  1430. b"message", reactor=reactor)
  1431. factory = reactor.tcpClients[0][2]
  1432. messageFile = factory.file
  1433. messageFile.seek(0)
  1434. self.assertEqual(messageFile.read(), b"message")
  1435. def test_senderDomainName(self):
  1436. """
  1437. L{twisted.mail.smtp.sendmail} passes through the sender domain name, if
  1438. provided.
  1439. """
  1440. reactor = MemoryReactor()
  1441. smtp.sendmail("localhost", "source@address", "recipient@address",
  1442. b"message", reactor=reactor, senderDomainName="foo")
  1443. factory = reactor.tcpClients[0][2]
  1444. self.assertEqual(factory.domain, b"foo")
  1445. def test_cancelBeforeConnectionMade(self):
  1446. """
  1447. When a user cancels L{twisted.mail.smtp.sendmail} before the connection
  1448. is made, the connection is closed by
  1449. L{twisted.internet.interfaces.IConnector.disconnect}.
  1450. """
  1451. reactor = MemoryReactor()
  1452. d = smtp.sendmail("localhost", "source@address", "recipient@address",
  1453. b"message", reactor=reactor)
  1454. d.cancel()
  1455. self.assertEqual(reactor.connectors[0]._disconnected, True)
  1456. failure = self.failureResultOf(d)
  1457. failure.trap(defer.CancelledError)
  1458. def test_cancelAfterConnectionMade(self):
  1459. """
  1460. When a user cancels L{twisted.mail.smtp.sendmail} after the connection
  1461. is made, the connection is closed by
  1462. L{twisted.internet.interfaces.ITransport.abortConnection}.
  1463. """
  1464. reactor = MemoryReactor()
  1465. transport = AbortableStringTransport()
  1466. d = smtp.sendmail("localhost", "source@address", "recipient@address",
  1467. b"message", reactor=reactor)
  1468. factory = reactor.tcpClients[0][2]
  1469. p = factory.buildProtocol(None)
  1470. p.makeConnection(transport)
  1471. d.cancel()
  1472. self.assertEqual(transport.aborting, True)
  1473. self.assertEqual(transport.disconnecting, True)
  1474. failure = self.failureResultOf(d)
  1475. failure.trap(defer.CancelledError)