test_mail.py 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Tests for large portions of L{twisted.mail}.
  5. """
  6. import os
  7. import errno
  8. import shutil
  9. import pickle
  10. import StringIO
  11. import email.message
  12. import email.parser
  13. import tempfile
  14. import signal
  15. import time
  16. from hashlib import md5
  17. from zope.interface.verify import verifyClass
  18. from zope.interface import Interface, implementer
  19. import twisted.cred.checkers
  20. import twisted.cred.credentials
  21. import twisted.cred.portal
  22. import twisted.mail.alias
  23. import twisted.mail.mail
  24. import twisted.mail.maildir
  25. import twisted.mail.protocols
  26. import twisted.mail.relay
  27. import twisted.mail.relaymanager
  28. from twisted import cred, mail
  29. from twisted.internet import (address, defer, interfaces, protocol,
  30. reactor, task)
  31. from twisted.internet.defer import Deferred
  32. from twisted.internet.error import (DNSLookupError, CannotListenError,
  33. ProcessDone, ProcessTerminated)
  34. from twisted.mail import pop3, smtp
  35. from twisted.mail.relaymanager import _AttemptManager
  36. from twisted.names import dns
  37. from twisted.names.dns import RRHeader, Record_CNAME, Record_MX
  38. from twisted.names.error import DNSNameError
  39. from twisted.python import failure, log
  40. from twisted.python.compat import range
  41. from twisted.python.filepath import FilePath
  42. from twisted.test.proto_helpers import (LineSendingProtocol,
  43. MemoryReactorClock, StringTransport)
  44. from twisted.trial import unittest
  45. class DomainWithDefaultsTests(unittest.TestCase):
  46. def testMethods(self):
  47. d = dict([(x, x + 10) for x in range(10)])
  48. d = mail.mail.DomainWithDefaultDict(d, 'Default')
  49. self.assertEqual(len(d), 10)
  50. self.assertEqual(list(iter(d)), list(range(10)))
  51. self.assertEqual(list(d.iterkeys()), list(iter(d)))
  52. items = list(d.iteritems())
  53. items.sort()
  54. self.assertEqual(items, [(x, x + 10) for x in range(10)])
  55. values = list(d.itervalues())
  56. values.sort()
  57. self.assertEqual(values, list(range(10, 20)))
  58. items = d.items()
  59. items.sort()
  60. self.assertEqual(items, [(x, x + 10) for x in range(10)])
  61. values = d.values()
  62. values.sort()
  63. self.assertEqual(values, list(range(10, 20)))
  64. for x in range(10):
  65. self.assertEqual(d[x], x + 10)
  66. self.assertEqual(d.get(x), x + 10)
  67. self.assertTrue(x in d)
  68. del d[2], d[4], d[6]
  69. self.assertEqual(len(d), 7)
  70. self.assertEqual(d[2], 'Default')
  71. self.assertEqual(d[4], 'Default')
  72. self.assertEqual(d[6], 'Default')
  73. d.update({'a': None, 'b': (), 'c': '*'})
  74. self.assertEqual(len(d), 10)
  75. self.assertEqual(d['a'], None)
  76. self.assertEqual(d['b'], ())
  77. self.assertEqual(d['c'], '*')
  78. d.clear()
  79. self.assertEqual(len(d), 0)
  80. self.assertEqual(d.setdefault('key', 'value'), 'value')
  81. self.assertEqual(d['key'], 'value')
  82. self.assertEqual(d.popitem(), ('key', 'value'))
  83. self.assertEqual(len(d), 0)
  84. dcopy = d.copy()
  85. self.assertEqual(d.domains, dcopy.domains)
  86. self.assertEqual(d.default, dcopy.default)
  87. def _stringificationTest(self, stringifier):
  88. """
  89. Assert that the class name of a L{mail.mail.DomainWithDefaultDict}
  90. instance and the string-formatted underlying domain dictionary both
  91. appear in the string produced by the given string-returning function.
  92. @type stringifier: one-argument callable
  93. @param stringifier: either C{str} or C{repr}, to be used to get a
  94. string to make assertions against.
  95. """
  96. domain = mail.mail.DomainWithDefaultDict({}, 'Default')
  97. self.assertIn(domain.__class__.__name__, stringifier(domain))
  98. domain['key'] = 'value'
  99. self.assertIn(str({'key': 'value'}), stringifier(domain))
  100. def test_str(self):
  101. """
  102. L{DomainWithDefaultDict.__str__} should return a string including
  103. the class name and the domain mapping held by the instance.
  104. """
  105. self._stringificationTest(str)
  106. def test_repr(self):
  107. """
  108. L{DomainWithDefaultDict.__repr__} should return a string including
  109. the class name and the domain mapping held by the instance.
  110. """
  111. self._stringificationTest(repr)
  112. def test_has_keyDeprecation(self):
  113. """
  114. has_key is now deprecated.
  115. """
  116. sut = mail.mail.DomainWithDefaultDict({}, 'Default')
  117. sut.has_key('anything')
  118. message = (
  119. 'twisted.mail.mail.DomainWithDefaultDict.has_key was deprecated '
  120. 'in Twisted 16.3.0. Use the `in` keyword instead.'
  121. )
  122. warnings = self.flushWarnings(
  123. [self.test_has_keyDeprecation])
  124. self.assertEqual(1, len(warnings))
  125. self.assertEqual(DeprecationWarning, warnings[0]['category'])
  126. self.assertEqual(message, warnings[0]['message'])
  127. class BounceTests(unittest.TestCase):
  128. def setUp(self):
  129. self.domain = mail.mail.BounceDomain()
  130. def testExists(self):
  131. self.assertRaises(smtp.AddressError, self.domain.exists, "any user")
  132. def testRelay(self):
  133. self.assertEqual(
  134. self.domain.willRelay("random q emailer", "protocol"),
  135. False
  136. )
  137. def testAddUser(self):
  138. self.domain.addUser("bob", "password")
  139. self.assertRaises(smtp.SMTPBadRcpt, self.domain.exists, "bob")
  140. class BounceWithSMTPServerTests(unittest.TestCase):
  141. """
  142. Tests for L{twisted.mail.mail.BounceDomain} with
  143. L{twisted.mail.smtp.SMTPServer}.
  144. """
  145. def test_rejected(self):
  146. """
  147. Incoming emails to a SMTP server with L{twisted.mail.mail.BounceDomain}
  148. are rejected.
  149. """
  150. service = mail.mail.MailService()
  151. domain = mail.mail.BounceDomain()
  152. service.addDomain(b'foo.com', domain)
  153. factory = mail.protocols.SMTPFactory(service)
  154. protocol = factory.buildProtocol(None)
  155. deliverer = mail.protocols.SMTPDomainDelivery(service, None, None)
  156. protocol.delivery = deliverer
  157. transport = StringTransport()
  158. protocol.makeConnection(transport)
  159. protocol.lineReceived(b'HELO baz.net')
  160. protocol.lineReceived(b'MAIL FROM:<a@baz.net>')
  161. protocol.lineReceived(b'RCPT TO:<any@foo.com>')
  162. protocol.lineReceived(b'QUIT')
  163. self.assertTrue(transport.disconnecting)
  164. protocol.connectionLost(None)
  165. self.assertEqual(transport.value().strip().split(b'\r\n')[-2],
  166. b'550 Cannot receive for specified address')
  167. class FileMessageTests(unittest.TestCase):
  168. def setUp(self):
  169. self.name = "fileMessage.testFile"
  170. self.final = "final.fileMessage.testFile"
  171. self.f = open(self.name, 'w')
  172. self.fp = mail.mail.FileMessage(self.f, self.name, self.final)
  173. def tearDown(self):
  174. try:
  175. self.f.close()
  176. except:
  177. pass
  178. try:
  179. os.remove(self.name)
  180. except:
  181. pass
  182. try:
  183. os.remove(self.final)
  184. except:
  185. pass
  186. def testFinalName(self):
  187. return self.fp.eomReceived().addCallback(self._cbFinalName)
  188. def _cbFinalName(self, result):
  189. self.assertEqual(result, self.final)
  190. self.assertTrue(self.f.closed)
  191. self.assertFalse(os.path.exists(self.name))
  192. def testContents(self):
  193. contents = "first line\nsecond line\nthird line\n"
  194. for line in contents.splitlines():
  195. self.fp.lineReceived(line)
  196. self.fp.eomReceived()
  197. with open(self.final) as f:
  198. self.assertEqual(f.read(), contents)
  199. def testInterrupted(self):
  200. contents = "first line\nsecond line\n"
  201. for line in contents.splitlines():
  202. self.fp.lineReceived(line)
  203. self.fp.connectionLost()
  204. self.assertFalse(os.path.exists(self.name))
  205. self.assertFalse(os.path.exists(self.final))
  206. class MailServiceTests(unittest.TestCase):
  207. def setUp(self):
  208. self.service = mail.mail.MailService()
  209. def testFactories(self):
  210. f = self.service.getPOP3Factory()
  211. self.assertTrue(isinstance(f, protocol.ServerFactory))
  212. self.assertTrue(f.buildProtocol(('127.0.0.1', 12345)), pop3.POP3)
  213. f = self.service.getSMTPFactory()
  214. self.assertTrue(isinstance(f, protocol.ServerFactory))
  215. self.assertTrue(f.buildProtocol(('127.0.0.1', 12345)), smtp.SMTP)
  216. f = self.service.getESMTPFactory()
  217. self.assertTrue(isinstance(f, protocol.ServerFactory))
  218. self.assertTrue(f.buildProtocol(('127.0.0.1', 12345)), smtp.ESMTP)
  219. def testPortals(self):
  220. o1 = object()
  221. o2 = object()
  222. self.service.portals['domain'] = o1
  223. self.service.portals[''] = o2
  224. self.assertTrue(self.service.lookupPortal('domain') is o1)
  225. self.assertTrue(self.service.defaultPortal() is o2)
  226. class StringListMailboxTests(unittest.TestCase):
  227. """
  228. Tests for L{StringListMailbox}, an in-memory only implementation of
  229. L{pop3.IMailbox}.
  230. """
  231. def test_listOneMessage(self):
  232. """
  233. L{StringListMailbox.listMessages} returns the length of the message at
  234. the offset into the mailbox passed to it.
  235. """
  236. mailbox = mail.maildir.StringListMailbox(["abc", "ab", "a"])
  237. self.assertEqual(mailbox.listMessages(0), 3)
  238. self.assertEqual(mailbox.listMessages(1), 2)
  239. self.assertEqual(mailbox.listMessages(2), 1)
  240. def test_listAllMessages(self):
  241. """
  242. L{StringListMailbox.listMessages} returns a list of the lengths of all
  243. messages if not passed an index.
  244. """
  245. mailbox = mail.maildir.StringListMailbox(["a", "abc", "ab"])
  246. self.assertEqual(mailbox.listMessages(), [1, 3, 2])
  247. def test_getMessage(self):
  248. """
  249. L{StringListMailbox.getMessage} returns a file-like object from which
  250. the contents of the message at the given offset into the mailbox can be
  251. read.
  252. """
  253. mailbox = mail.maildir.StringListMailbox(["foo", "real contents"])
  254. self.assertEqual(mailbox.getMessage(1).read(), "real contents")
  255. def test_getUidl(self):
  256. """
  257. L{StringListMailbox.getUidl} returns a unique identifier for the
  258. message at the given offset into the mailbox.
  259. """
  260. mailbox = mail.maildir.StringListMailbox(["foo", "bar"])
  261. self.assertNotEqual(mailbox.getUidl(0), mailbox.getUidl(1))
  262. def test_deleteMessage(self):
  263. """
  264. L{StringListMailbox.deleteMessage} marks a message for deletion causing
  265. further requests for its length to return 0.
  266. """
  267. mailbox = mail.maildir.StringListMailbox(["foo"])
  268. mailbox.deleteMessage(0)
  269. self.assertEqual(mailbox.listMessages(0), 0)
  270. self.assertEqual(mailbox.listMessages(), [0])
  271. def test_undeleteMessages(self):
  272. """
  273. L{StringListMailbox.undeleteMessages} causes any messages marked for
  274. deletion to be returned to their original state.
  275. """
  276. mailbox = mail.maildir.StringListMailbox(["foo"])
  277. mailbox.deleteMessage(0)
  278. mailbox.undeleteMessages()
  279. self.assertEqual(mailbox.listMessages(0), 3)
  280. self.assertEqual(mailbox.listMessages(), [3])
  281. def test_sync(self):
  282. """
  283. L{StringListMailbox.sync} causes any messages as marked for deletion to
  284. be permanently deleted.
  285. """
  286. mailbox = mail.maildir.StringListMailbox(["foo"])
  287. mailbox.deleteMessage(0)
  288. mailbox.sync()
  289. mailbox.undeleteMessages()
  290. self.assertEqual(mailbox.listMessages(0), 0)
  291. self.assertEqual(mailbox.listMessages(), [0])
  292. class FailingMaildirMailboxAppendMessageTask(mail.maildir._MaildirMailboxAppendMessageTask):
  293. _openstate = True
  294. _writestate = True
  295. _renamestate = True
  296. def osopen(self, fn, attr, mode):
  297. if self._openstate:
  298. return os.open(fn, attr, mode)
  299. else:
  300. raise OSError(errno.EPERM, "Faked Permission Problem")
  301. def oswrite(self, fh, data):
  302. if self._writestate:
  303. return os.write(fh, data)
  304. else:
  305. raise OSError(errno.ENOSPC, "Faked Space problem")
  306. def osrename(self, oldname, newname):
  307. if self._renamestate:
  308. return os.rename(oldname, newname)
  309. else:
  310. raise OSError(errno.EPERM, "Faked Permission Problem")
  311. class _AppendTestMixin(object):
  312. """
  313. Mixin for L{MaildirMailbox.appendMessage} test cases which defines a helper
  314. for serially appending multiple messages to a mailbox.
  315. """
  316. def _appendMessages(self, mbox, messages):
  317. """
  318. Deliver the given messages one at a time. Delivery is serialized to
  319. guarantee a predictable order in the mailbox (overlapped message deliver
  320. makes no guarantees about which message which appear first).
  321. """
  322. results = []
  323. def append():
  324. for m in messages:
  325. d = mbox.appendMessage(m)
  326. d.addCallback(results.append)
  327. yield d
  328. d = task.cooperate(append()).whenDone()
  329. d.addCallback(lambda ignored: results)
  330. return d
  331. class MaildirAppendStringTests(unittest.TestCase, _AppendTestMixin):
  332. """
  333. Tests for L{MaildirMailbox.appendMessage} when invoked with a C{str}.
  334. """
  335. def setUp(self):
  336. self.d = self.mktemp()
  337. mail.maildir.initializeMaildir(self.d)
  338. def _append(self, ignored, mbox):
  339. d = mbox.appendMessage('TEST')
  340. return self.assertFailure(d, Exception)
  341. def _setState(self, ignored, mbox, rename=None, write=None, open=None):
  342. """
  343. Change the behavior of future C{rename}, C{write}, or C{open} calls made
  344. by the mailbox C{mbox}.
  345. @param rename: If not L{None}, a new value for the C{_renamestate}
  346. attribute of the mailbox's append factory. The original value will
  347. be restored at the end of the test.
  348. @param write: Like C{rename}, but for the C{_writestate} attribute.
  349. @param open: Like C{rename}, but for the C{_openstate} attribute.
  350. """
  351. if rename is not None:
  352. self.addCleanup(
  353. setattr, mbox.AppendFactory, '_renamestate',
  354. mbox.AppendFactory._renamestate)
  355. mbox.AppendFactory._renamestate = rename
  356. if write is not None:
  357. self.addCleanup(
  358. setattr, mbox.AppendFactory, '_writestate',
  359. mbox.AppendFactory._writestate)
  360. mbox.AppendFactory._writestate = write
  361. if open is not None:
  362. self.addCleanup(
  363. setattr, mbox.AppendFactory, '_openstate',
  364. mbox.AppendFactory._openstate)
  365. mbox.AppendFactory._openstate = open
  366. def test_append(self):
  367. """
  368. L{MaildirMailbox.appendMessage} returns a L{Deferred} which fires when
  369. the message has been added to the end of the mailbox.
  370. """
  371. mbox = mail.maildir.MaildirMailbox(self.d)
  372. mbox.AppendFactory = FailingMaildirMailboxAppendMessageTask
  373. d = self._appendMessages(mbox, ["X" * i for i in range(1, 11)])
  374. d.addCallback(self.assertEqual, [None] * 10)
  375. d.addCallback(self._cbTestAppend, mbox)
  376. return d
  377. def _cbTestAppend(self, ignored, mbox):
  378. """
  379. Check that the mailbox has the expected number (ten) of messages in it,
  380. and that each has the expected contents, and that they are in the same
  381. order as that in which they were appended.
  382. """
  383. self.assertEqual(len(mbox.listMessages()), 10)
  384. self.assertEqual(
  385. [len(mbox.getMessage(i).read()) for i in range(10)],
  386. list(range(1, 11)))
  387. # test in the right order: last to first error location.
  388. self._setState(None, mbox, rename=False)
  389. d = self._append(None, mbox)
  390. d.addCallback(self._setState, mbox, rename=True, write=False)
  391. d.addCallback(self._append, mbox)
  392. d.addCallback(self._setState, mbox, write=True, open=False)
  393. d.addCallback(self._append, mbox)
  394. d.addCallback(self._setState, mbox, open=True)
  395. return d
  396. class MaildirAppendFileTests(unittest.TestCase, _AppendTestMixin):
  397. """
  398. Tests for L{MaildirMailbox.appendMessage} when invoked with a C{str}.
  399. """
  400. def setUp(self):
  401. self.d = self.mktemp()
  402. mail.maildir.initializeMaildir(self.d)
  403. def test_append(self):
  404. """
  405. L{MaildirMailbox.appendMessage} returns a L{Deferred} which fires when
  406. the message has been added to the end of the mailbox.
  407. """
  408. mbox = mail.maildir.MaildirMailbox(self.d)
  409. messages = []
  410. for i in range(1, 11):
  411. temp = tempfile.TemporaryFile()
  412. temp.write("X" * i)
  413. temp.seek(0, 0)
  414. messages.append(temp)
  415. self.addCleanup(temp.close)
  416. d = self._appendMessages(mbox, messages)
  417. d.addCallback(self._cbTestAppend, mbox)
  418. return d
  419. def _cbTestAppend(self, result, mbox):
  420. """
  421. Check that the mailbox has the expected number (ten) of messages in it,
  422. and that each has the expected contents, and that they are in the same
  423. order as that in which they were appended.
  424. """
  425. self.assertEqual(len(mbox.listMessages()), 10)
  426. self.assertEqual(
  427. [len(mbox.getMessage(i).read()) for i in range(10)],
  428. list(range(1, 11)))
  429. class MaildirTests(unittest.TestCase):
  430. def setUp(self):
  431. self.d = self.mktemp()
  432. mail.maildir.initializeMaildir(self.d)
  433. def tearDown(self):
  434. shutil.rmtree(self.d)
  435. def testInitializer(self):
  436. d = self.d
  437. trash = os.path.join(d, '.Trash')
  438. self.assertTrue(os.path.exists(d) and os.path.isdir(d))
  439. self.assertTrue(os.path.exists(os.path.join(d, 'new')))
  440. self.assertTrue(os.path.exists(os.path.join(d, 'cur')))
  441. self.assertTrue(os.path.exists(os.path.join(d, 'tmp')))
  442. self.assertTrue(os.path.isdir(os.path.join(d, 'new')))
  443. self.assertTrue(os.path.isdir(os.path.join(d, 'cur')))
  444. self.assertTrue(os.path.isdir(os.path.join(d, 'tmp')))
  445. self.assertTrue(os.path.exists(os.path.join(trash, 'new')))
  446. self.assertTrue(os.path.exists(os.path.join(trash, 'cur')))
  447. self.assertTrue(os.path.exists(os.path.join(trash, 'tmp')))
  448. self.assertTrue(os.path.isdir(os.path.join(trash, 'new')))
  449. self.assertTrue(os.path.isdir(os.path.join(trash, 'cur')))
  450. self.assertTrue(os.path.isdir(os.path.join(trash, 'tmp')))
  451. def test_nameGenerator(self):
  452. """
  453. Each call to L{_MaildirNameGenerator.generate} returns a unique
  454. string suitable for use as the basename of a new message file. The
  455. names are ordered such that those generated earlier sort less than
  456. those generated later.
  457. """
  458. clock = task.Clock()
  459. clock.advance(0.05)
  460. generator = mail.maildir._MaildirNameGenerator(clock)
  461. firstName = generator.generate()
  462. clock.advance(0.05)
  463. secondName = generator.generate()
  464. self.assertTrue(firstName < secondName)
  465. def test_mailbox(self):
  466. """
  467. Exercise the methods of L{IMailbox} as implemented by
  468. L{MaildirMailbox}.
  469. """
  470. j = os.path.join
  471. n = mail.maildir._generateMaildirName
  472. msgs = [j(b, n()) for b in ('cur', 'new') for x in range(5)]
  473. # Toss a few files into the mailbox
  474. i = 1
  475. for f in msgs:
  476. with open(j(self.d, f), 'w') as fObj:
  477. fObj.write('x' * i)
  478. i = i + 1
  479. mb = mail.maildir.MaildirMailbox(self.d)
  480. self.assertEqual(mb.listMessages(), list(range(1, 11)))
  481. self.assertEqual(mb.listMessages(1), 2)
  482. self.assertEqual(mb.listMessages(5), 6)
  483. self.assertEqual(mb.getMessage(6).read(), 'x' * 7)
  484. self.assertEqual(mb.getMessage(1).read(), 'x' * 2)
  485. d = {}
  486. for i in range(10):
  487. u = mb.getUidl(i)
  488. self.assertFalse(u in d)
  489. d[u] = None
  490. p, f = os.path.split(msgs[5])
  491. mb.deleteMessage(5)
  492. self.assertEqual(mb.listMessages(5), 0)
  493. self.assertTrue(os.path.exists(j(self.d, '.Trash', 'cur', f)))
  494. self.assertFalse(os.path.exists(j(self.d, msgs[5])))
  495. mb.undeleteMessages()
  496. self.assertEqual(mb.listMessages(5), 6)
  497. self.assertFalse(os.path.exists(j(self.d, '.Trash', 'cur', f)))
  498. self.assertTrue(os.path.exists(j(self.d, msgs[5])))
  499. class AbstractMaildirDomainTests(unittest.TestCase):
  500. """
  501. Tests for L{twisted.mail.maildir.AbstractMaildirDomain}.
  502. """
  503. def test_interface(self):
  504. """
  505. L{maildir.AbstractMaildirDomain} implements L{mail.IAliasableDomain}.
  506. """
  507. verifyClass(mail.mail.IAliasableDomain,
  508. mail.maildir.AbstractMaildirDomain)
  509. class MaildirDirdbmDomainTests(unittest.TestCase):
  510. """
  511. Tests for L{MaildirDirdbmDomain}.
  512. """
  513. def setUp(self):
  514. """
  515. Create a temporary L{MaildirDirdbmDomain} and parent
  516. L{MailService} before running each test.
  517. """
  518. self.P = self.mktemp()
  519. self.S = mail.mail.MailService()
  520. self.D = mail.maildir.MaildirDirdbmDomain(self.S, self.P)
  521. def tearDown(self):
  522. """
  523. Remove the temporary C{maildir} directory when the test has
  524. finished.
  525. """
  526. shutil.rmtree(self.P)
  527. def test_addUser(self):
  528. """
  529. L{MaildirDirdbmDomain.addUser} accepts a user and password
  530. argument. It stores those in a C{dbm} dictionary
  531. attribute and creates a directory for each user.
  532. """
  533. toAdd = (('user1', 'pwd1'), ('user2', 'pwd2'), ('user3', 'pwd3'))
  534. for (u, p) in toAdd:
  535. self.D.addUser(u, p)
  536. for (u, p) in toAdd:
  537. self.assertTrue(u in self.D.dbm)
  538. self.assertEqual(self.D.dbm[u], p)
  539. self.assertTrue(os.path.exists(os.path.join(self.P, u)))
  540. def test_credentials(self):
  541. """
  542. L{MaildirDirdbmDomain.getCredentialsCheckers} initializes and
  543. returns one L{ICredentialsChecker} checker by default.
  544. """
  545. creds = self.D.getCredentialsCheckers()
  546. self.assertEqual(len(creds), 1)
  547. self.assertTrue(cred.checkers.ICredentialsChecker.providedBy(creds[0]))
  548. self.assertTrue(cred.credentials.IUsernamePassword in creds[0].credentialInterfaces)
  549. def test_requestAvatar(self):
  550. """
  551. L{MaildirDirdbmDomain.requestAvatar} raises L{NotImplementedError}
  552. unless it is supplied with an L{pop3.IMailbox} interface.
  553. When called with an L{pop3.IMailbox}, it returns a 3-tuple
  554. containing L{pop3.IMailbox}, an implementation of that interface
  555. and a NOOP callable.
  556. """
  557. class ISomething(Interface):
  558. pass
  559. self.D.addUser('user', 'password')
  560. self.assertRaises(
  561. NotImplementedError,
  562. self.D.requestAvatar, 'user', None, ISomething
  563. )
  564. t = self.D.requestAvatar('user', None, pop3.IMailbox)
  565. self.assertEqual(len(t), 3)
  566. self.assertTrue(t[0] is pop3.IMailbox)
  567. self.assertTrue(pop3.IMailbox.providedBy(t[1]))
  568. t[2]()
  569. def test_requestAvatarId(self):
  570. """
  571. L{DirdbmDatabase.requestAvatarId} raises L{UnauthorizedLogin} if
  572. supplied with invalid user credentials.
  573. When called with valid credentials, L{requestAvatarId} returns
  574. the username associated with the supplied credentials.
  575. """
  576. self.D.addUser('user', 'password')
  577. database = self.D.getCredentialsCheckers()[0]
  578. creds = cred.credentials.UsernamePassword('user', 'wrong password')
  579. self.assertRaises(
  580. cred.error.UnauthorizedLogin,
  581. database.requestAvatarId, creds
  582. )
  583. creds = cred.credentials.UsernamePassword('user', 'password')
  584. self.assertEqual(database.requestAvatarId(creds), 'user')
  585. def test_userDirectory(self):
  586. """
  587. L{MaildirDirdbmDomain.userDirectory} is supplied with a user name
  588. and returns the path to that user's maildir subdirectory.
  589. Calling L{MaildirDirdbmDomain.userDirectory} with a
  590. non-existent user returns the 'postmaster' directory if there
  591. is a postmaster or returns L{None} if there is no postmaster.
  592. """
  593. self.D.addUser('user', 'password')
  594. self.assertEqual(self.D.userDirectory('user'),
  595. os.path.join(self.D.root, 'user'))
  596. self.D.postmaster = False
  597. self.assertIdentical(self.D.userDirectory('nouser'), None)
  598. self.D.postmaster = True
  599. self.assertEqual(self.D.userDirectory('nouser'),
  600. os.path.join(self.D.root, 'postmaster'))
  601. @implementer(mail.mail.IAliasableDomain)
  602. class StubAliasableDomain(object):
  603. """
  604. Minimal testable implementation of IAliasableDomain.
  605. """
  606. def exists(self, user):
  607. """
  608. No test coverage for invocations of this method on domain objects,
  609. so we just won't implement it.
  610. """
  611. raise NotImplementedError()
  612. def addUser(self, user, password):
  613. """
  614. No test coverage for invocations of this method on domain objects,
  615. so we just won't implement it.
  616. """
  617. raise NotImplementedError()
  618. def getCredentialsCheckers(self):
  619. """
  620. This needs to succeed in order for other tests to complete
  621. successfully, but we don't actually assert anything about its
  622. behavior. Return an empty list. Sometime later we should return
  623. something else and assert that a portal got set up properly.
  624. """
  625. return []
  626. def setAliasGroup(self, aliases):
  627. """
  628. Just record the value so the test can check it later.
  629. """
  630. self.aliasGroup = aliases
  631. class ServiceDomainTests(unittest.TestCase):
  632. def setUp(self):
  633. self.S = mail.mail.MailService()
  634. self.D = mail.protocols.DomainDeliveryBase(self.S, None)
  635. self.D.service = self.S
  636. self.D.protocolName = 'TEST'
  637. self.D.host = 'hostname'
  638. self.tmpdir = self.mktemp()
  639. domain = mail.maildir.MaildirDirdbmDomain(self.S, self.tmpdir)
  640. domain.addUser('user', 'password')
  641. self.S.addDomain('test.domain', domain)
  642. def tearDown(self):
  643. shutil.rmtree(self.tmpdir)
  644. def testAddAliasableDomain(self):
  645. """
  646. Test that adding an IAliasableDomain to a mail service properly sets
  647. up alias group references and such.
  648. """
  649. aliases = object()
  650. domain = StubAliasableDomain()
  651. self.S.aliases = aliases
  652. self.S.addDomain('example.com', domain)
  653. self.assertIdentical(domain.aliasGroup, aliases)
  654. def testReceivedHeader(self):
  655. hdr = self.D.receivedHeader(
  656. ('remotehost', '123.232.101.234'),
  657. smtp.Address('<someguy@someplace>'),
  658. ['user@host.name']
  659. )
  660. fp = StringIO.StringIO(hdr)
  661. emailParser = email.parser.Parser()
  662. m = emailParser.parse(fp)
  663. self.assertEqual(len(m.items()), 1)
  664. self.assertIn('Received', m)
  665. def testValidateTo(self):
  666. user = smtp.User('user@test.domain', 'helo', None, 'wherever@whatever')
  667. return defer.maybeDeferred(self.D.validateTo, user
  668. ).addCallback(self._cbValidateTo
  669. )
  670. def _cbValidateTo(self, result):
  671. self.assertTrue(callable(result))
  672. def testValidateToBadUsername(self):
  673. user = smtp.User('resu@test.domain', 'helo', None, 'wherever@whatever')
  674. return self.assertFailure(
  675. defer.maybeDeferred(self.D.validateTo, user),
  676. smtp.SMTPBadRcpt)
  677. def testValidateToBadDomain(self):
  678. user = smtp.User('user@domain.test', 'helo', None, 'wherever@whatever')
  679. return self.assertFailure(
  680. defer.maybeDeferred(self.D.validateTo, user),
  681. smtp.SMTPBadRcpt)
  682. def testValidateFrom(self):
  683. helo = ('hostname', '127.0.0.1')
  684. origin = smtp.Address('<user@hostname>')
  685. self.assertTrue(self.D.validateFrom(helo, origin) is origin)
  686. helo = ('hostname', '1.2.3.4')
  687. origin = smtp.Address('<user@hostname>')
  688. self.assertTrue(self.D.validateFrom(helo, origin) is origin)
  689. helo = ('hostname', '1.2.3.4')
  690. origin = smtp.Address('<>')
  691. self.assertTrue(self.D.validateFrom(helo, origin) is origin)
  692. self.assertRaises(
  693. smtp.SMTPBadSender,
  694. self.D.validateFrom, None, origin
  695. )
  696. class VirtualPOP3Tests(unittest.TestCase):
  697. def setUp(self):
  698. self.tmpdir = self.mktemp()
  699. self.S = mail.mail.MailService()
  700. self.D = mail.maildir.MaildirDirdbmDomain(self.S, self.tmpdir)
  701. self.D.addUser('user', 'password')
  702. self.S.addDomain('test.domain', self.D)
  703. portal = cred.portal.Portal(self.D)
  704. map(portal.registerChecker, self.D.getCredentialsCheckers())
  705. self.S.portals[''] = self.S.portals['test.domain'] = portal
  706. self.P = mail.protocols.VirtualPOP3()
  707. self.P.service = self.S
  708. self.P.magic = '<unit test magic>'
  709. def tearDown(self):
  710. shutil.rmtree(self.tmpdir)
  711. def testAuthenticateAPOP(self):
  712. resp = md5(self.P.magic + 'password').hexdigest()
  713. return self.P.authenticateUserAPOP('user', resp
  714. ).addCallback(self._cbAuthenticateAPOP
  715. )
  716. def _cbAuthenticateAPOP(self, result):
  717. self.assertEqual(len(result), 3)
  718. self.assertEqual(result[0], pop3.IMailbox)
  719. self.assertTrue(pop3.IMailbox.providedBy(result[1]))
  720. result[2]()
  721. def testAuthenticateIncorrectUserAPOP(self):
  722. resp = md5(self.P.magic + 'password').hexdigest()
  723. return self.assertFailure(
  724. self.P.authenticateUserAPOP('resu', resp),
  725. cred.error.UnauthorizedLogin)
  726. def testAuthenticateIncorrectResponseAPOP(self):
  727. resp = md5('wrong digest').hexdigest()
  728. return self.assertFailure(
  729. self.P.authenticateUserAPOP('user', resp),
  730. cred.error.UnauthorizedLogin)
  731. def testAuthenticatePASS(self):
  732. return self.P.authenticateUserPASS('user', 'password'
  733. ).addCallback(self._cbAuthenticatePASS
  734. )
  735. def _cbAuthenticatePASS(self, result):
  736. self.assertEqual(len(result), 3)
  737. self.assertEqual(result[0], pop3.IMailbox)
  738. self.assertTrue(pop3.IMailbox.providedBy(result[1]))
  739. result[2]()
  740. def testAuthenticateBadUserPASS(self):
  741. return self.assertFailure(
  742. self.P.authenticateUserPASS('resu', 'password'),
  743. cred.error.UnauthorizedLogin)
  744. def testAuthenticateBadPasswordPASS(self):
  745. return self.assertFailure(
  746. self.P.authenticateUserPASS('user', 'wrong password'),
  747. cred.error.UnauthorizedLogin)
  748. class empty(smtp.User):
  749. def __init__(self):
  750. pass
  751. class RelayTests(unittest.TestCase):
  752. def testExists(self):
  753. service = mail.mail.MailService()
  754. domain = mail.relay.DomainQueuer(service)
  755. doRelay = [
  756. address.UNIXAddress('/var/run/mail-relay'),
  757. address.IPv4Address('TCP', '127.0.0.1', 12345),
  758. ]
  759. dontRelay = [
  760. address.IPv4Address('TCP', '192.168.2.1', 62),
  761. address.IPv4Address('TCP', '1.2.3.4', 1943),
  762. ]
  763. for peer in doRelay:
  764. user = empty()
  765. user.orig = 'user@host'
  766. user.dest = 'tsoh@resu'
  767. user.protocol = empty()
  768. user.protocol.transport = empty()
  769. user.protocol.transport.getPeer = lambda: peer
  770. self.assertTrue(callable(domain.exists(user)))
  771. for peer in dontRelay:
  772. user = empty()
  773. user.orig = 'some@place'
  774. user.protocol = empty()
  775. user.protocol.transport = empty()
  776. user.protocol.transport.getPeer = lambda: peer
  777. user.dest = 'who@cares'
  778. self.assertRaises(smtp.SMTPBadRcpt, domain.exists, user)
  779. class RelayerTests(unittest.TestCase):
  780. def setUp(self):
  781. self.tmpdir = self.mktemp()
  782. os.mkdir(self.tmpdir)
  783. self.messageFiles = []
  784. for i in range(10):
  785. name = os.path.join(self.tmpdir, 'body-%d' % (i,))
  786. with open(name + '-H', 'w') as f:
  787. pickle.dump(['from-%d' % (i,), 'to-%d' % (i,)], f)
  788. f = open(name + '-D', 'w')
  789. f.write(name)
  790. f.seek(0, 0)
  791. self.messageFiles.append(name)
  792. self.R = mail.relay.RelayerMixin()
  793. self.R.loadMessages(self.messageFiles)
  794. def tearDown(self):
  795. shutil.rmtree(self.tmpdir)
  796. def testMailFrom(self):
  797. for i in range(10):
  798. self.assertEqual(self.R.getMailFrom(), 'from-%d' % (i,))
  799. self.R.sentMail(250, None, None, None, None)
  800. self.assertEqual(self.R.getMailFrom(), None)
  801. def testMailTo(self):
  802. for i in range(10):
  803. self.assertEqual(self.R.getMailTo(), ['to-%d' % (i,)])
  804. self.R.sentMail(250, None, None, None, None)
  805. self.assertEqual(self.R.getMailTo(), None)
  806. def testMailData(self):
  807. for i in range(10):
  808. name = os.path.join(self.tmpdir, 'body-%d' % (i,))
  809. self.assertEqual(self.R.getMailData().read(), name)
  810. self.R.sentMail(250, None, None, None, None)
  811. self.assertEqual(self.R.getMailData(), None)
  812. class Manager:
  813. def __init__(self):
  814. self.success = []
  815. self.failure = []
  816. self.done = []
  817. def notifySuccess(self, factory, message):
  818. self.success.append((factory, message))
  819. def notifyFailure(self, factory, message):
  820. self.failure.append((factory, message))
  821. def notifyDone(self, factory):
  822. self.done.append(factory)
  823. class ManagedRelayerTests(unittest.TestCase):
  824. def setUp(self):
  825. self.manager = Manager()
  826. self.messages = list(range(0, 20, 2))
  827. self.factory = object()
  828. self.relay = mail.relaymanager.ManagedRelayerMixin(self.manager)
  829. self.relay.messages = self.messages[:]
  830. self.relay.names = self.messages[:]
  831. self.relay.factory = self.factory
  832. def testSuccessfulSentMail(self):
  833. for i in self.messages:
  834. self.relay.sentMail(250, None, None, None, None)
  835. self.assertEqual(
  836. self.manager.success,
  837. [(self.factory, m) for m in self.messages]
  838. )
  839. def testFailedSentMail(self):
  840. for i in self.messages:
  841. self.relay.sentMail(550, None, None, None, None)
  842. self.assertEqual(
  843. self.manager.failure,
  844. [(self.factory, m) for m in self.messages]
  845. )
  846. def testConnectionLost(self):
  847. self.relay.connectionLost(failure.Failure(Exception()))
  848. self.assertEqual(self.manager.done, [self.factory])
  849. class DirectoryQueueTests(unittest.TestCase):
  850. def setUp(self):
  851. # This is almost a test case itself.
  852. self.tmpdir = self.mktemp()
  853. os.mkdir(self.tmpdir)
  854. self.queue = mail.relaymanager.Queue(self.tmpdir)
  855. self.queue.noisy = False
  856. for m in range(25):
  857. hdrF, msgF = self.queue.createNewMessage()
  858. with hdrF:
  859. pickle.dump(['header', m], hdrF)
  860. msgF.lineReceived('body: %d' % (m,))
  861. msgF.eomReceived()
  862. self.queue.readDirectory()
  863. def tearDown(self):
  864. shutil.rmtree(self.tmpdir)
  865. def testWaiting(self):
  866. self.assertTrue(self.queue.hasWaiting())
  867. self.assertEqual(len(self.queue.getWaiting()), 25)
  868. waiting = self.queue.getWaiting()
  869. self.queue.setRelaying(waiting[0])
  870. self.assertEqual(len(self.queue.getWaiting()), 24)
  871. self.queue.setWaiting(waiting[0])
  872. self.assertEqual(len(self.queue.getWaiting()), 25)
  873. def testRelaying(self):
  874. for m in self.queue.getWaiting():
  875. self.queue.setRelaying(m)
  876. self.assertEqual(
  877. len(self.queue.getRelayed()),
  878. 25 - len(self.queue.getWaiting())
  879. )
  880. self.assertFalse(self.queue.hasWaiting())
  881. relayed = self.queue.getRelayed()
  882. self.queue.setWaiting(relayed[0])
  883. self.assertEqual(len(self.queue.getWaiting()), 1)
  884. self.assertEqual(len(self.queue.getRelayed()), 24)
  885. def testDone(self):
  886. msg = self.queue.getWaiting()[0]
  887. self.queue.setRelaying(msg)
  888. self.queue.done(msg)
  889. self.assertEqual(len(self.queue.getWaiting()), 24)
  890. self.assertEqual(len(self.queue.getRelayed()), 0)
  891. self.assertFalse(msg in self.queue.getWaiting())
  892. self.assertFalse(msg in self.queue.getRelayed())
  893. def testEnvelope(self):
  894. envelopes = []
  895. for msg in self.queue.getWaiting():
  896. envelopes.append(self.queue.getEnvelope(msg))
  897. envelopes.sort()
  898. for i in range(25):
  899. self.assertEqual(
  900. envelopes.pop(0),
  901. ['header', i]
  902. )
  903. from twisted.names import server
  904. from twisted.names import client
  905. from twisted.names import common
  906. class TestAuthority(common.ResolverBase):
  907. def __init__(self):
  908. common.ResolverBase.__init__(self)
  909. self.addresses = {}
  910. def _lookup(self, name, cls, type, timeout = None):
  911. if name in self.addresses and type == dns.MX:
  912. results = []
  913. for a in self.addresses[name]:
  914. hdr = dns.RRHeader(
  915. name, dns.MX, dns.IN, 60, dns.Record_MX(0, a)
  916. )
  917. results.append(hdr)
  918. return defer.succeed((results, [], []))
  919. return defer.fail(failure.Failure(dns.DomainError(name)))
  920. def setUpDNS(self):
  921. self.auth = TestAuthority()
  922. factory = server.DNSServerFactory([self.auth])
  923. protocol = dns.DNSDatagramProtocol(factory)
  924. while 1:
  925. self.port = reactor.listenTCP(0, factory, interface='127.0.0.1')
  926. portNumber = self.port.getHost().port
  927. try:
  928. self.udpPort = reactor.listenUDP(portNumber, protocol, interface='127.0.0.1')
  929. except CannotListenError:
  930. self.port.stopListening()
  931. else:
  932. break
  933. self.resolver = client.Resolver(servers=[('127.0.0.1', portNumber)])
  934. def tearDownDNS(self):
  935. dl = []
  936. dl.append(defer.maybeDeferred(self.port.stopListening))
  937. dl.append(defer.maybeDeferred(self.udpPort.stopListening))
  938. try:
  939. self.resolver._parseCall.cancel()
  940. except:
  941. pass
  942. return defer.DeferredList(dl)
  943. class MXTests(unittest.TestCase):
  944. """
  945. Tests for L{mail.relaymanager.MXCalculator}.
  946. """
  947. def setUp(self):
  948. setUpDNS(self)
  949. self.clock = task.Clock()
  950. self.mx = mail.relaymanager.MXCalculator(self.resolver, self.clock)
  951. def tearDown(self):
  952. return tearDownDNS(self)
  953. def test_defaultClock(self):
  954. """
  955. L{MXCalculator}'s default clock is C{twisted.internet.reactor}.
  956. """
  957. self.assertIdentical(
  958. mail.relaymanager.MXCalculator(self.resolver).clock,
  959. reactor)
  960. def testSimpleSuccess(self):
  961. self.auth.addresses['test.domain'] = ['the.email.test.domain']
  962. return self.mx.getMX('test.domain').addCallback(self._cbSimpleSuccess)
  963. def _cbSimpleSuccess(self, mx):
  964. self.assertEqual(mx.preference, 0)
  965. self.assertEqual(str(mx.name), 'the.email.test.domain')
  966. def testSimpleFailure(self):
  967. self.mx.fallbackToDomain = False
  968. return self.assertFailure(self.mx.getMX('test.domain'), IOError)
  969. def testSimpleFailureWithFallback(self):
  970. return self.assertFailure(self.mx.getMX('test.domain'), DNSLookupError)
  971. def _exchangeTest(self, domain, records, correctMailExchange):
  972. """
  973. Issue an MX request for the given domain and arrange for it to be
  974. responded to with the given records. Verify that the resulting mail
  975. exchange is the indicated host.
  976. @type domain: C{str}
  977. @type records: C{list} of L{RRHeader}
  978. @type correctMailExchange: C{str}
  979. @rtype: L{Deferred}
  980. """
  981. class DummyResolver(object):
  982. def lookupMailExchange(self, name):
  983. if name == domain:
  984. return defer.succeed((
  985. records,
  986. [],
  987. []))
  988. return defer.fail(DNSNameError(domain))
  989. self.mx.resolver = DummyResolver()
  990. d = self.mx.getMX(domain)
  991. def gotMailExchange(record):
  992. self.assertEqual(str(record.name), correctMailExchange)
  993. d.addCallback(gotMailExchange)
  994. return d
  995. def test_mailExchangePreference(self):
  996. """
  997. The MX record with the lowest preference is returned by
  998. L{MXCalculator.getMX}.
  999. """
  1000. domain = "example.com"
  1001. good = "good.example.com"
  1002. bad = "bad.example.com"
  1003. records = [
  1004. RRHeader(name=domain,
  1005. type=Record_MX.TYPE,
  1006. payload=Record_MX(1, bad)),
  1007. RRHeader(name=domain,
  1008. type=Record_MX.TYPE,
  1009. payload=Record_MX(0, good)),
  1010. RRHeader(name=domain,
  1011. type=Record_MX.TYPE,
  1012. payload=Record_MX(2, bad))]
  1013. return self._exchangeTest(domain, records, good)
  1014. def test_badExchangeExcluded(self):
  1015. """
  1016. L{MXCalculator.getMX} returns the MX record with the lowest preference
  1017. which is not also marked as bad.
  1018. """
  1019. domain = "example.com"
  1020. good = "good.example.com"
  1021. bad = "bad.example.com"
  1022. records = [
  1023. RRHeader(name=domain,
  1024. type=Record_MX.TYPE,
  1025. payload=Record_MX(0, bad)),
  1026. RRHeader(name=domain,
  1027. type=Record_MX.TYPE,
  1028. payload=Record_MX(1, good))]
  1029. self.mx.markBad(bad)
  1030. return self._exchangeTest(domain, records, good)
  1031. def test_fallbackForAllBadExchanges(self):
  1032. """
  1033. L{MXCalculator.getMX} returns the MX record with the lowest preference
  1034. if all the MX records in the response have been marked bad.
  1035. """
  1036. domain = "example.com"
  1037. bad = "bad.example.com"
  1038. worse = "worse.example.com"
  1039. records = [
  1040. RRHeader(name=domain,
  1041. type=Record_MX.TYPE,
  1042. payload=Record_MX(0, bad)),
  1043. RRHeader(name=domain,
  1044. type=Record_MX.TYPE,
  1045. payload=Record_MX(1, worse))]
  1046. self.mx.markBad(bad)
  1047. self.mx.markBad(worse)
  1048. return self._exchangeTest(domain, records, bad)
  1049. def test_badExchangeExpires(self):
  1050. """
  1051. L{MXCalculator.getMX} returns the MX record with the lowest preference
  1052. if it was last marked bad longer than L{MXCalculator.timeOutBadMX}
  1053. seconds ago.
  1054. """
  1055. domain = "example.com"
  1056. good = "good.example.com"
  1057. previouslyBad = "bad.example.com"
  1058. records = [
  1059. RRHeader(name=domain,
  1060. type=Record_MX.TYPE,
  1061. payload=Record_MX(0, previouslyBad)),
  1062. RRHeader(name=domain,
  1063. type=Record_MX.TYPE,
  1064. payload=Record_MX(1, good))]
  1065. self.mx.markBad(previouslyBad)
  1066. self.clock.advance(self.mx.timeOutBadMX)
  1067. return self._exchangeTest(domain, records, previouslyBad)
  1068. def test_goodExchangeUsed(self):
  1069. """
  1070. L{MXCalculator.getMX} returns the MX record with the lowest preference
  1071. if it was marked good after it was marked bad.
  1072. """
  1073. domain = "example.com"
  1074. good = "good.example.com"
  1075. previouslyBad = "bad.example.com"
  1076. records = [
  1077. RRHeader(name=domain,
  1078. type=Record_MX.TYPE,
  1079. payload=Record_MX(0, previouslyBad)),
  1080. RRHeader(name=domain,
  1081. type=Record_MX.TYPE,
  1082. payload=Record_MX(1, good))]
  1083. self.mx.markBad(previouslyBad)
  1084. self.mx.markGood(previouslyBad)
  1085. self.clock.advance(self.mx.timeOutBadMX)
  1086. return self._exchangeTest(domain, records, previouslyBad)
  1087. def test_successWithoutResults(self):
  1088. """
  1089. If an MX lookup succeeds but the result set is empty,
  1090. L{MXCalculator.getMX} should try to look up an I{A} record for the
  1091. requested name and call back its returned Deferred with that
  1092. address.
  1093. """
  1094. ip = '1.2.3.4'
  1095. domain = 'example.org'
  1096. class DummyResolver(object):
  1097. """
  1098. Fake resolver which will respond to an MX lookup with an empty
  1099. result set.
  1100. @ivar mx: A dictionary mapping hostnames to three-tuples of
  1101. results to be returned from I{MX} lookups.
  1102. @ivar a: A dictionary mapping hostnames to addresses to be
  1103. returned from I{A} lookups.
  1104. """
  1105. mx = {domain: ([], [], [])}
  1106. a = {domain: ip}
  1107. def lookupMailExchange(self, domain):
  1108. return defer.succeed(self.mx[domain])
  1109. def getHostByName(self, domain):
  1110. return defer.succeed(self.a[domain])
  1111. self.mx.resolver = DummyResolver()
  1112. d = self.mx.getMX(domain)
  1113. d.addCallback(self.assertEqual, Record_MX(name=ip))
  1114. return d
  1115. def test_failureWithSuccessfulFallback(self):
  1116. """
  1117. Test that if the MX record lookup fails, fallback is enabled, and an A
  1118. record is available for the name, then the Deferred returned by
  1119. L{MXCalculator.getMX} ultimately fires with a Record_MX instance which
  1120. gives the address in the A record for the name.
  1121. """
  1122. class DummyResolver(object):
  1123. """
  1124. Fake resolver which will fail an MX lookup but then succeed a
  1125. getHostByName call.
  1126. """
  1127. def lookupMailExchange(self, domain):
  1128. return defer.fail(DNSNameError())
  1129. def getHostByName(self, domain):
  1130. return defer.succeed("1.2.3.4")
  1131. self.mx.resolver = DummyResolver()
  1132. d = self.mx.getMX("domain")
  1133. d.addCallback(self.assertEqual, Record_MX(name="1.2.3.4"))
  1134. return d
  1135. def test_cnameWithoutGlueRecords(self):
  1136. """
  1137. If an MX lookup returns a single CNAME record as a result, MXCalculator
  1138. will perform an MX lookup for the canonical name indicated and return
  1139. the MX record which results.
  1140. """
  1141. alias = "alias.example.com"
  1142. canonical = "canonical.example.com"
  1143. exchange = "mail.example.com"
  1144. class DummyResolver(object):
  1145. """
  1146. Fake resolver which will return a CNAME for an MX lookup of a name
  1147. which is an alias and an MX for an MX lookup of the canonical name.
  1148. """
  1149. def lookupMailExchange(self, domain):
  1150. if domain == alias:
  1151. return defer.succeed((
  1152. [RRHeader(name=domain,
  1153. type=Record_CNAME.TYPE,
  1154. payload=Record_CNAME(canonical))],
  1155. [], []))
  1156. elif domain == canonical:
  1157. return defer.succeed((
  1158. [RRHeader(name=domain,
  1159. type=Record_MX.TYPE,
  1160. payload=Record_MX(0, exchange))],
  1161. [], []))
  1162. else:
  1163. return defer.fail(DNSNameError(domain))
  1164. self.mx.resolver = DummyResolver()
  1165. d = self.mx.getMX(alias)
  1166. d.addCallback(self.assertEqual, Record_MX(name=exchange))
  1167. return d
  1168. def test_cnameChain(self):
  1169. """
  1170. If L{MXCalculator.getMX} encounters a CNAME chain which is longer than
  1171. the length specified, the returned L{Deferred} should errback with
  1172. L{CanonicalNameChainTooLong}.
  1173. """
  1174. class DummyResolver(object):
  1175. """
  1176. Fake resolver which generates a CNAME chain of infinite length in
  1177. response to MX lookups.
  1178. """
  1179. chainCounter = 0
  1180. def lookupMailExchange(self, domain):
  1181. self.chainCounter += 1
  1182. name = 'x-%d.example.com' % (self.chainCounter,)
  1183. return defer.succeed((
  1184. [RRHeader(name=domain,
  1185. type=Record_CNAME.TYPE,
  1186. payload=Record_CNAME(name))],
  1187. [], []))
  1188. cnameLimit = 3
  1189. self.mx.resolver = DummyResolver()
  1190. d = self.mx.getMX("mail.example.com", cnameLimit)
  1191. self.assertFailure(
  1192. d, twisted.mail.relaymanager.CanonicalNameChainTooLong)
  1193. def cbChainTooLong(error):
  1194. self.assertEqual(error.args[0], Record_CNAME("x-%d.example.com" % (cnameLimit + 1,)))
  1195. self.assertEqual(self.mx.resolver.chainCounter, cnameLimit + 1)
  1196. d.addCallback(cbChainTooLong)
  1197. return d
  1198. def test_cnameWithGlueRecords(self):
  1199. """
  1200. If an MX lookup returns a CNAME and the MX record for the CNAME, the
  1201. L{Deferred} returned by L{MXCalculator.getMX} should be called back
  1202. with the name from the MX record without further lookups being
  1203. attempted.
  1204. """
  1205. lookedUp = []
  1206. alias = "alias.example.com"
  1207. canonical = "canonical.example.com"
  1208. exchange = "mail.example.com"
  1209. class DummyResolver(object):
  1210. def lookupMailExchange(self, domain):
  1211. if domain != alias or lookedUp:
  1212. # Don't give back any results for anything except the alias
  1213. # or on any request after the first.
  1214. return ([], [], [])
  1215. return defer.succeed((
  1216. [RRHeader(name=alias,
  1217. type=Record_CNAME.TYPE,
  1218. payload=Record_CNAME(canonical)),
  1219. RRHeader(name=canonical,
  1220. type=Record_MX.TYPE,
  1221. payload=Record_MX(name=exchange))],
  1222. [], []))
  1223. self.mx.resolver = DummyResolver()
  1224. d = self.mx.getMX(alias)
  1225. d.addCallback(self.assertEqual, Record_MX(name=exchange))
  1226. return d
  1227. def test_cnameLoopWithGlueRecords(self):
  1228. """
  1229. If an MX lookup returns two CNAME records which point to each other,
  1230. the loop should be detected and the L{Deferred} returned by
  1231. L{MXCalculator.getMX} should be errbacked with L{CanonicalNameLoop}.
  1232. """
  1233. firstAlias = "cname1.example.com"
  1234. secondAlias = "cname2.example.com"
  1235. class DummyResolver(object):
  1236. def lookupMailExchange(self, domain):
  1237. return defer.succeed((
  1238. [RRHeader(name=firstAlias,
  1239. type=Record_CNAME.TYPE,
  1240. payload=Record_CNAME(secondAlias)),
  1241. RRHeader(name=secondAlias,
  1242. type=Record_CNAME.TYPE,
  1243. payload=Record_CNAME(firstAlias))],
  1244. [], []))
  1245. self.mx.resolver = DummyResolver()
  1246. d = self.mx.getMX(firstAlias)
  1247. self.assertFailure(d, twisted.mail.relaymanager.CanonicalNameLoop)
  1248. return d
  1249. def testManyRecords(self):
  1250. self.auth.addresses['test.domain'] = [
  1251. 'mx1.test.domain', 'mx2.test.domain', 'mx3.test.domain'
  1252. ]
  1253. return self.mx.getMX('test.domain'
  1254. ).addCallback(self._cbManyRecordsSuccessfulLookup
  1255. )
  1256. def _cbManyRecordsSuccessfulLookup(self, mx):
  1257. self.assertTrue(str(mx.name).split('.', 1)[0] in ('mx1', 'mx2', 'mx3'))
  1258. self.mx.markBad(str(mx.name))
  1259. return self.mx.getMX('test.domain'
  1260. ).addCallback(self._cbManyRecordsDifferentResult, mx
  1261. )
  1262. def _cbManyRecordsDifferentResult(self, nextMX, mx):
  1263. self.assertNotEqual(str(mx.name), str(nextMX.name))
  1264. self.mx.markBad(str(nextMX.name))
  1265. return self.mx.getMX('test.domain'
  1266. ).addCallback(self._cbManyRecordsLastResult, mx, nextMX
  1267. )
  1268. def _cbManyRecordsLastResult(self, lastMX, mx, nextMX):
  1269. self.assertNotEqual(str(mx.name), str(lastMX.name))
  1270. self.assertNotEqual(str(nextMX.name), str(lastMX.name))
  1271. self.mx.markBad(str(lastMX.name))
  1272. self.mx.markGood(str(nextMX.name))
  1273. return self.mx.getMX('test.domain'
  1274. ).addCallback(self._cbManyRecordsRepeatSpecificResult, nextMX
  1275. )
  1276. def _cbManyRecordsRepeatSpecificResult(self, againMX, nextMX):
  1277. self.assertEqual(str(againMX.name), str(nextMX.name))
  1278. class LiveFireExerciseTests(unittest.TestCase):
  1279. if interfaces.IReactorUDP(reactor, None) is None:
  1280. skip = "UDP support is required to determining MX records"
  1281. def setUp(self):
  1282. setUpDNS(self)
  1283. self.tmpdirs = [
  1284. 'domainDir', 'insertionDomain', 'insertionQueue',
  1285. 'destinationDomain', 'destinationQueue'
  1286. ]
  1287. def tearDown(self):
  1288. for d in self.tmpdirs:
  1289. if os.path.exists(d):
  1290. shutil.rmtree(d)
  1291. return tearDownDNS(self)
  1292. def testLocalDelivery(self):
  1293. service = mail.mail.MailService()
  1294. service.smtpPortal.registerChecker(cred.checkers.AllowAnonymousAccess())
  1295. domain = mail.maildir.MaildirDirdbmDomain(service, 'domainDir')
  1296. domain.addUser('user', 'password')
  1297. service.addDomain('test.domain', domain)
  1298. service.portals[''] = service.portals['test.domain']
  1299. map(service.portals[''].registerChecker, domain.getCredentialsCheckers())
  1300. service.setQueue(mail.relay.DomainQueuer(service))
  1301. f = service.getSMTPFactory()
  1302. self.smtpServer = reactor.listenTCP(0, f, interface='127.0.0.1')
  1303. client = LineSendingProtocol([
  1304. 'HELO meson',
  1305. 'MAIL FROM: <user@hostname>',
  1306. 'RCPT TO: <user@test.domain>',
  1307. 'DATA',
  1308. 'This is the message',
  1309. '.',
  1310. 'QUIT'
  1311. ])
  1312. done = Deferred()
  1313. f = protocol.ClientFactory()
  1314. f.protocol = lambda: client
  1315. f.clientConnectionLost = lambda *args: done.callback(None)
  1316. reactor.connectTCP('127.0.0.1', self.smtpServer.getHost().port, f)
  1317. def finished(ign):
  1318. mbox = domain.requestAvatar('user', None, pop3.IMailbox)[1]
  1319. msg = mbox.getMessage(0).read()
  1320. self.assertNotEqual(msg.find('This is the message'), -1)
  1321. return self.smtpServer.stopListening()
  1322. done.addCallback(finished)
  1323. return done
  1324. def testRelayDelivery(self):
  1325. # Here is the service we will connect to and send mail from
  1326. insServ = mail.mail.MailService()
  1327. insServ.smtpPortal.registerChecker(cred.checkers.AllowAnonymousAccess())
  1328. domain = mail.maildir.MaildirDirdbmDomain(insServ, 'insertionDomain')
  1329. insServ.addDomain('insertion.domain', domain)
  1330. os.mkdir('insertionQueue')
  1331. insServ.setQueue(mail.relaymanager.Queue('insertionQueue'))
  1332. insServ.domains.setDefaultDomain(mail.relay.DomainQueuer(insServ))
  1333. manager = mail.relaymanager.SmartHostSMTPRelayingManager(insServ.queue)
  1334. manager.fArgs += ('test.identity.hostname',)
  1335. helper = mail.relaymanager.RelayStateHelper(manager, 1)
  1336. # Yoink! Now the internet obeys OUR every whim!
  1337. manager.mxcalc = mail.relaymanager.MXCalculator(self.resolver)
  1338. # And this is our whim.
  1339. self.auth.addresses['destination.domain'] = ['127.0.0.1']
  1340. f = insServ.getSMTPFactory()
  1341. self.insServer = reactor.listenTCP(0, f, interface='127.0.0.1')
  1342. # Here is the service the previous one will connect to for final
  1343. # delivery
  1344. destServ = mail.mail.MailService()
  1345. destServ.smtpPortal.registerChecker(cred.checkers.AllowAnonymousAccess())
  1346. domain = mail.maildir.MaildirDirdbmDomain(destServ, 'destinationDomain')
  1347. domain.addUser('user', 'password')
  1348. destServ.addDomain('destination.domain', domain)
  1349. os.mkdir('destinationQueue')
  1350. destServ.setQueue(mail.relaymanager.Queue('destinationQueue'))
  1351. helper = mail.relaymanager.RelayStateHelper(manager, 1)
  1352. helper.startService()
  1353. f = destServ.getSMTPFactory()
  1354. self.destServer = reactor.listenTCP(0, f, interface='127.0.0.1')
  1355. # Update the port number the *first* relay will connect to, because we can't use
  1356. # port 25
  1357. manager.PORT = self.destServer.getHost().port
  1358. client = LineSendingProtocol([
  1359. 'HELO meson',
  1360. 'MAIL FROM: <user@wherever>',
  1361. 'RCPT TO: <user@destination.domain>',
  1362. 'DATA',
  1363. 'This is the message',
  1364. '.',
  1365. 'QUIT'
  1366. ])
  1367. done = Deferred()
  1368. f = protocol.ClientFactory()
  1369. f.protocol = lambda: client
  1370. f.clientConnectionLost = lambda *args: done.callback(None)
  1371. reactor.connectTCP('127.0.0.1', self.insServer.getHost().port, f)
  1372. def finished(ign):
  1373. # First part of the delivery is done. Poke the queue manually now
  1374. # so we don't have to wait for the queue to be flushed.
  1375. delivery = manager.checkState()
  1376. def delivered(ign):
  1377. mbox = domain.requestAvatar('user', None, pop3.IMailbox)[1]
  1378. msg = mbox.getMessage(0).read()
  1379. self.assertNotEqual(msg.find('This is the message'), -1)
  1380. self.insServer.stopListening()
  1381. self.destServer.stopListening()
  1382. helper.stopService()
  1383. delivery.addCallback(delivered)
  1384. return delivery
  1385. done.addCallback(finished)
  1386. return done
  1387. aliasFile = StringIO.StringIO("""\
  1388. # Here's a comment
  1389. # woop another one
  1390. testuser: address1,address2, address3,
  1391. continuation@address, |/bin/process/this
  1392. usertwo:thisaddress,thataddress, lastaddress
  1393. lastuser: :/includable, /filename, |/program, address
  1394. """)
  1395. class LineBufferMessage:
  1396. def __init__(self):
  1397. self.lines = []
  1398. self.eom = False
  1399. self.lost = False
  1400. def lineReceived(self, line):
  1401. self.lines.append(line)
  1402. def eomReceived(self):
  1403. self.eom = True
  1404. return defer.succeed('<Whatever>')
  1405. def connectionLost(self):
  1406. self.lost = True
  1407. class AliasTests(unittest.TestCase):
  1408. lines = [
  1409. 'First line',
  1410. 'Next line',
  1411. '',
  1412. 'After a blank line',
  1413. 'Last line'
  1414. ]
  1415. def setUp(self):
  1416. aliasFile.seek(0)
  1417. def testHandle(self):
  1418. result = {}
  1419. lines = [
  1420. 'user: another@host\n',
  1421. 'nextuser: |/bin/program\n',
  1422. 'user: me@again\n',
  1423. 'moreusers: :/etc/include/filename\n',
  1424. 'multiuser: first@host, second@host,last@anotherhost',
  1425. ]
  1426. for l in lines:
  1427. mail.alias.handle(result, l, 'TestCase', None)
  1428. self.assertEqual(result['user'], ['another@host', 'me@again'])
  1429. self.assertEqual(result['nextuser'], ['|/bin/program'])
  1430. self.assertEqual(result['moreusers'], [':/etc/include/filename'])
  1431. self.assertEqual(result['multiuser'], ['first@host', 'second@host', 'last@anotherhost'])
  1432. def testFileLoader(self):
  1433. domains = {'': object()}
  1434. result = mail.alias.loadAliasFile(domains, fp=aliasFile)
  1435. self.assertEqual(len(result), 3)
  1436. group = result['testuser']
  1437. s = str(group)
  1438. for a in ('address1', 'address2', 'address3', 'continuation@address', '/bin/process/this'):
  1439. self.assertNotEqual(s.find(a), -1)
  1440. self.assertEqual(len(group), 5)
  1441. group = result['usertwo']
  1442. s = str(group)
  1443. for a in ('thisaddress', 'thataddress', 'lastaddress'):
  1444. self.assertNotEqual(s.find(a), -1)
  1445. self.assertEqual(len(group), 3)
  1446. group = result['lastuser']
  1447. s = str(group)
  1448. self.assertEqual(s.find('/includable'), -1)
  1449. for a in ('/filename', 'program', 'address'):
  1450. self.assertNotEqual(s.find(a), -1, '%s not found' % a)
  1451. self.assertEqual(len(group), 3)
  1452. def testMultiWrapper(self):
  1453. msgs = LineBufferMessage(), LineBufferMessage(), LineBufferMessage()
  1454. msg = mail.alias.MultiWrapper(msgs)
  1455. for L in self.lines:
  1456. msg.lineReceived(L)
  1457. return msg.eomReceived().addCallback(self._cbMultiWrapper, msgs)
  1458. def _cbMultiWrapper(self, ignored, msgs):
  1459. for m in msgs:
  1460. self.assertTrue(m.eom)
  1461. self.assertFalse(m.lost)
  1462. self.assertEqual(self.lines, m.lines)
  1463. def testFileAlias(self):
  1464. tmpfile = self.mktemp()
  1465. a = mail.alias.FileAlias(tmpfile, None, None)
  1466. m = a.createMessageReceiver()
  1467. for l in self.lines:
  1468. m.lineReceived(l)
  1469. return m.eomReceived().addCallback(self._cbTestFileAlias, tmpfile)
  1470. def _cbTestFileAlias(self, ignored, tmpfile):
  1471. with open(tmpfile) as f:
  1472. lines = f.readlines()
  1473. self.assertEqual([L[:-1] for L in lines], self.lines)
  1474. class DummyDomain(object):
  1475. """
  1476. Test domain for L{AddressAliasTests}.
  1477. """
  1478. def __init__(self, address):
  1479. self.address = address
  1480. def exists(self, user, memo=None):
  1481. """
  1482. @returns: When a C{memo} is passed in this will raise a
  1483. L{smtp.SMTPBadRcpt} exception, otherwise a boolean
  1484. indicating if the C{user} and string version of
  1485. L{self.address} are equal or not.
  1486. @rtype: C{bool}
  1487. """
  1488. if memo:
  1489. raise mail.smtp.SMTPBadRcpt('ham')
  1490. return lambda: user == str(self.address)
  1491. class AddressAliasTests(unittest.TestCase):
  1492. """
  1493. Tests for L{twisted.mail.alias.AddressAlias}.
  1494. """
  1495. def setUp(self):
  1496. """
  1497. Setup an L{AddressAlias}.
  1498. """
  1499. self.address = mail.smtp.Address('foo@bar')
  1500. domains = {self.address.domain: DummyDomain(self.address)}
  1501. self.alias = mail.alias.AddressAlias(self.address, domains,
  1502. self.address)
  1503. def test_createMessageReceiver(self):
  1504. """
  1505. L{createMessageReceiever} calls C{exists()} on the domain object
  1506. which key matches the C{alias} passed to L{AddressAlias}.
  1507. """
  1508. self.assertTrue(self.alias.createMessageReceiver())
  1509. def test_str(self):
  1510. """
  1511. The string presentation of L{AddressAlias} includes the alias.
  1512. """
  1513. self.assertEqual(str(self.alias), '<Address foo@bar>')
  1514. def test_resolve(self):
  1515. """
  1516. L{resolve} will look for additional aliases when an C{aliasmap}
  1517. dictionary is passed, and returns L{None} if none were found.
  1518. """
  1519. self.assertEqual(self.alias.resolve({self.address: 'bar'}), None)
  1520. def test_resolveWithoutAliasmap(self):
  1521. """
  1522. L{resolve} returns L{None} when the alias could not be found in the
  1523. C{aliasmap} and no L{mail.smtp.User} with this alias exists either.
  1524. """
  1525. self.assertEqual(self.alias.resolve({}), None)
  1526. class DummyProcess(object):
  1527. __slots__ = ['onEnd']
  1528. class MockProcessAlias(mail.alias.ProcessAlias):
  1529. """
  1530. An alias processor that doesn't actually launch processes.
  1531. """
  1532. def spawnProcess(self, proto, program, path):
  1533. """
  1534. Don't spawn a process.
  1535. """
  1536. class MockAliasGroup(mail.alias.AliasGroup):
  1537. """
  1538. An alias group using C{MockProcessAlias}.
  1539. """
  1540. processAliasFactory = MockProcessAlias
  1541. class StubProcess(object):
  1542. """
  1543. Fake implementation of L{IProcessTransport}.
  1544. @ivar signals: A list of all the signals which have been sent to this fake
  1545. process.
  1546. """
  1547. def __init__(self):
  1548. self.signals = []
  1549. def loseConnection(self):
  1550. """
  1551. No-op implementation of disconnection.
  1552. """
  1553. def signalProcess(self, signal):
  1554. """
  1555. Record a signal sent to this process for later inspection.
  1556. """
  1557. self.signals.append(signal)
  1558. class ProcessAliasTests(unittest.TestCase):
  1559. """
  1560. Tests for alias resolution.
  1561. """
  1562. if interfaces.IReactorProcess(reactor, None) is None:
  1563. skip = "IReactorProcess not supported"
  1564. lines = [
  1565. 'First line',
  1566. 'Next line',
  1567. '',
  1568. 'After a blank line',
  1569. 'Last line'
  1570. ]
  1571. def exitStatus(self, code):
  1572. """
  1573. Construct a status from the given exit code.
  1574. @type code: L{int} between 0 and 255 inclusive.
  1575. @param code: The exit status which the code will represent.
  1576. @rtype: L{int}
  1577. @return: A status integer for the given exit code.
  1578. """
  1579. # /* Macros for constructing status values. */
  1580. # #define __W_EXITCODE(ret, sig) ((ret) << 8 | (sig))
  1581. status = (code << 8) | 0
  1582. # Sanity check
  1583. self.assertTrue(os.WIFEXITED(status))
  1584. self.assertEqual(os.WEXITSTATUS(status), code)
  1585. self.assertFalse(os.WIFSIGNALED(status))
  1586. return status
  1587. def signalStatus(self, signal):
  1588. """
  1589. Construct a status from the given signal.
  1590. @type signal: L{int} between 0 and 255 inclusive.
  1591. @param signal: The signal number which the status will represent.
  1592. @rtype: L{int}
  1593. @return: A status integer for the given signal.
  1594. """
  1595. # /* If WIFSIGNALED(STATUS), the terminating signal. */
  1596. # #define __WTERMSIG(status) ((status) & 0x7f)
  1597. # /* Nonzero if STATUS indicates termination by a signal. */
  1598. # #define __WIFSIGNALED(status) \
  1599. # (((signed char) (((status) & 0x7f) + 1) >> 1) > 0)
  1600. status = signal
  1601. # Sanity check
  1602. self.assertTrue(os.WIFSIGNALED(status))
  1603. self.assertEqual(os.WTERMSIG(status), signal)
  1604. self.assertFalse(os.WIFEXITED(status))
  1605. return status
  1606. def setUp(self):
  1607. """
  1608. Replace L{smtp.DNSNAME} with a well-known value.
  1609. """
  1610. self.DNSNAME = smtp.DNSNAME
  1611. smtp.DNSNAME = ''
  1612. def tearDown(self):
  1613. """
  1614. Restore the original value of L{smtp.DNSNAME}.
  1615. """
  1616. smtp.DNSNAME = self.DNSNAME
  1617. def test_processAlias(self):
  1618. """
  1619. Standard call to C{mail.alias.ProcessAlias}: check that the specified
  1620. script is called, and that the input is correctly transferred to it.
  1621. """
  1622. sh = FilePath(self.mktemp())
  1623. sh.setContent("""\
  1624. #!/bin/sh
  1625. rm -f process.alias.out
  1626. while read i; do
  1627. echo $i >> process.alias.out
  1628. done""")
  1629. os.chmod(sh.path, 0o700)
  1630. a = mail.alias.ProcessAlias(sh.path, None, None)
  1631. m = a.createMessageReceiver()
  1632. for l in self.lines:
  1633. m.lineReceived(l)
  1634. def _cbProcessAlias(ignored):
  1635. with open('process.alias.out') as f:
  1636. lines = f.readlines()
  1637. self.assertEqual([L[:-1] for L in lines], self.lines)
  1638. return m.eomReceived().addCallback(_cbProcessAlias)
  1639. def test_processAliasTimeout(self):
  1640. """
  1641. If the alias child process does not exit within a particular period of
  1642. time, the L{Deferred} returned by L{MessageWrapper.eomReceived} should
  1643. fail with L{ProcessAliasTimeout} and send the I{KILL} signal to the
  1644. child process..
  1645. """
  1646. reactor = task.Clock()
  1647. transport = StubProcess()
  1648. proto = mail.alias.ProcessAliasProtocol()
  1649. proto.makeConnection(transport)
  1650. receiver = mail.alias.MessageWrapper(proto, None, reactor)
  1651. d = receiver.eomReceived()
  1652. reactor.advance(receiver.completionTimeout)
  1653. def timedOut(ignored):
  1654. self.assertEqual(transport.signals, ['KILL'])
  1655. # Now that it has been killed, disconnect the protocol associated
  1656. # with it.
  1657. proto.processEnded(
  1658. ProcessTerminated(self.signalStatus(signal.SIGKILL)))
  1659. self.assertFailure(d, mail.alias.ProcessAliasTimeout)
  1660. d.addCallback(timedOut)
  1661. return d
  1662. def test_earlyProcessTermination(self):
  1663. """
  1664. If the process associated with an L{mail.alias.MessageWrapper} exits
  1665. before I{eomReceived} is called, the L{Deferred} returned by
  1666. I{eomReceived} should fail.
  1667. """
  1668. transport = StubProcess()
  1669. protocol = mail.alias.ProcessAliasProtocol()
  1670. protocol.makeConnection(transport)
  1671. receiver = mail.alias.MessageWrapper(protocol, None, None)
  1672. protocol.processEnded(failure.Failure(ProcessDone(0)))
  1673. return self.assertFailure(receiver.eomReceived(), ProcessDone)
  1674. def _terminationTest(self, status):
  1675. """
  1676. Verify that if the process associated with an
  1677. L{mail.alias.MessageWrapper} exits with the given status, the
  1678. L{Deferred} returned by I{eomReceived} fails with L{ProcessTerminated}.
  1679. """
  1680. transport = StubProcess()
  1681. protocol = mail.alias.ProcessAliasProtocol()
  1682. protocol.makeConnection(transport)
  1683. receiver = mail.alias.MessageWrapper(protocol, None, None)
  1684. protocol.processEnded(
  1685. failure.Failure(ProcessTerminated(status)))
  1686. return self.assertFailure(receiver.eomReceived(), ProcessTerminated)
  1687. def test_errorProcessTermination(self):
  1688. """
  1689. If the process associated with an L{mail.alias.MessageWrapper} exits
  1690. with a non-zero exit code, the L{Deferred} returned by I{eomReceived}
  1691. should fail.
  1692. """
  1693. return self._terminationTest(self.exitStatus(1))
  1694. def test_signalProcessTermination(self):
  1695. """
  1696. If the process associated with an L{mail.alias.MessageWrapper} exits
  1697. because it received a signal, the L{Deferred} returned by
  1698. I{eomReceived} should fail.
  1699. """
  1700. return self._terminationTest(self.signalStatus(signal.SIGHUP))
  1701. def test_aliasResolution(self):
  1702. """
  1703. Check that the C{resolve} method of alias processors produce the correct
  1704. set of objects:
  1705. - direct alias with L{mail.alias.AddressAlias} if a simple input is passed
  1706. - aliases in a file with L{mail.alias.FileWrapper} if an input in the format
  1707. '/file' is given
  1708. - aliases resulting of a process call wrapped by L{mail.alias.MessageWrapper}
  1709. if the format is '|process'
  1710. """
  1711. aliases = {}
  1712. domain = {'': TestDomain(aliases, ['user1', 'user2', 'user3'])}
  1713. A1 = MockAliasGroup(['user1', '|echo', '/file'], domain, 'alias1')
  1714. A2 = MockAliasGroup(['user2', 'user3'], domain, 'alias2')
  1715. A3 = mail.alias.AddressAlias('alias1', domain, 'alias3')
  1716. aliases.update({
  1717. 'alias1': A1,
  1718. 'alias2': A2,
  1719. 'alias3': A3,
  1720. })
  1721. res1 = A1.resolve(aliases)
  1722. r1 = map(str, res1.objs)
  1723. r1.sort()
  1724. expected = map(str, [
  1725. mail.alias.AddressAlias('user1', None, None),
  1726. mail.alias.MessageWrapper(DummyProcess(), 'echo'),
  1727. mail.alias.FileWrapper('/file'),
  1728. ])
  1729. expected.sort()
  1730. self.assertEqual(r1, expected)
  1731. res2 = A2.resolve(aliases)
  1732. r2 = map(str, res2.objs)
  1733. r2.sort()
  1734. expected = map(str, [
  1735. mail.alias.AddressAlias('user2', None, None),
  1736. mail.alias.AddressAlias('user3', None, None)
  1737. ])
  1738. expected.sort()
  1739. self.assertEqual(r2, expected)
  1740. res3 = A3.resolve(aliases)
  1741. r3 = map(str, res3.objs)
  1742. r3.sort()
  1743. expected = map(str, [
  1744. mail.alias.AddressAlias('user1', None, None),
  1745. mail.alias.MessageWrapper(DummyProcess(), 'echo'),
  1746. mail.alias.FileWrapper('/file'),
  1747. ])
  1748. expected.sort()
  1749. self.assertEqual(r3, expected)
  1750. def test_cyclicAlias(self):
  1751. """
  1752. Check that a cycle in alias resolution is correctly handled.
  1753. """
  1754. aliases = {}
  1755. domain = {'': TestDomain(aliases, [])}
  1756. A1 = mail.alias.AddressAlias('alias2', domain, 'alias1')
  1757. A2 = mail.alias.AddressAlias('alias3', domain, 'alias2')
  1758. A3 = mail.alias.AddressAlias('alias1', domain, 'alias3')
  1759. aliases.update({
  1760. 'alias1': A1,
  1761. 'alias2': A2,
  1762. 'alias3': A3
  1763. })
  1764. self.assertEqual(aliases['alias1'].resolve(aliases), None)
  1765. self.assertEqual(aliases['alias2'].resolve(aliases), None)
  1766. self.assertEqual(aliases['alias3'].resolve(aliases), None)
  1767. A4 = MockAliasGroup(['|echo', 'alias1'], domain, 'alias4')
  1768. aliases['alias4'] = A4
  1769. res = A4.resolve(aliases)
  1770. r = map(str, res.objs)
  1771. r.sort()
  1772. expected = map(str, [
  1773. mail.alias.MessageWrapper(DummyProcess(), 'echo')
  1774. ])
  1775. expected.sort()
  1776. self.assertEqual(r, expected)
  1777. class TestDomain:
  1778. def __init__(self, aliases, users):
  1779. self.aliases = aliases
  1780. self.users = users
  1781. def exists(self, user, memo=None):
  1782. user = user.dest.local
  1783. if user in self.users:
  1784. return lambda: mail.alias.AddressAlias(user, None, None)
  1785. try:
  1786. a = self.aliases[user]
  1787. except:
  1788. raise smtp.SMTPBadRcpt(user)
  1789. else:
  1790. aliases = a.resolve(self.aliases, memo)
  1791. if aliases:
  1792. return lambda: aliases
  1793. raise smtp.SMTPBadRcpt(user)
  1794. class DummyQueue(object):
  1795. """
  1796. A fake relay queue to use for testing.
  1797. This queue doesn't keep track of which messages are waiting to be relayed
  1798. or are in the process of being relayed.
  1799. @ivar directory: See L{__init__}.
  1800. """
  1801. def __init__(self, directory):
  1802. """
  1803. @type directory: L{bytes}
  1804. @param directory: The pathname of the directory holding messages in the
  1805. queue.
  1806. """
  1807. self.directory = directory
  1808. def done(self, message):
  1809. """
  1810. Remove a message from the queue.
  1811. @type message: L{bytes}
  1812. @param message: The base filename of a message.
  1813. """
  1814. message = os.path.basename(message)
  1815. os.remove(self.getPath(message) + '-D')
  1816. os.remove(self.getPath(message) + '-H')
  1817. def getEnvelopeFile(self, message):
  1818. """
  1819. Get the envelope file for a message in the queue.
  1820. @type message: L{bytes}
  1821. @param message: The base filename of a message.
  1822. @rtype: L{file}
  1823. @return: The envelope file for the message.
  1824. """
  1825. return open(os.path.join(self.directory, message+'-H'), 'rb')
  1826. def getPath(self, message):
  1827. """
  1828. Return the full base pathname of a message in the queue.
  1829. @type message: L{bytes}
  1830. @param message: The base filename of a message.
  1831. @rtype: L{bytes}
  1832. @return: The full base pathname of the message.
  1833. """
  1834. return os.path.join(self.directory, message)
  1835. def createNewMessage(self):
  1836. """
  1837. Create a new message in the queue.
  1838. @rtype: 2-L{tuple} of (E{1}) L{file}, (E{2}) L{FileMessage}
  1839. @return: The envelope file and a message receiver for a new message in
  1840. the queue.
  1841. """
  1842. fname = "%s_%s" % (time.time(), id(self))
  1843. headerFile = open(os.path.join(self.directory, fname+'-H'), 'wb')
  1844. tempFilename = os.path.join(self.directory, fname+'-C')
  1845. finalFilename = os.path.join(self.directory, fname+'-D')
  1846. messageFile = open(tempFilename, 'wb')
  1847. return headerFile, mail.mail.FileMessage(messageFile, tempFilename,
  1848. finalFilename)
  1849. def setWaiting(self, message):
  1850. """
  1851. Ignore the request to mark a message as waiting to be relayed.
  1852. @type message: L{bytes}
  1853. @param message: The base filename of a message.
  1854. """
  1855. pass
  1856. class DummySmartHostSMTPRelayingManager(object):
  1857. """
  1858. A fake smart host to use for testing.
  1859. @type managed: L{dict} of L{bytes} -> L{list} of
  1860. L{list} of L{bytes}
  1861. @ivar managed: A mapping of a string identifying a managed relayer to
  1862. filenames of messages the managed relayer is responsible for.
  1863. @ivar queue: See L{__init__}.
  1864. """
  1865. def __init__(self, queue):
  1866. """
  1867. Initialize the minimum necessary members of a smart host.
  1868. @type queue: L{DummyQueue}
  1869. @param queue: A queue that can be used for testing purposes.
  1870. """
  1871. self.managed = {}
  1872. self.queue = queue
  1873. class _AttemptManagerTests(unittest.TestCase):
  1874. """
  1875. Test the behavior of L{_AttemptManager}.
  1876. @type tmpdir: L{bytes}
  1877. @ivar tmpdir: The path to a temporary directory holding the message files.
  1878. @type reactor: L{MemoryReactorClock}
  1879. @ivar reactor: The reactor used for test purposes.
  1880. @type eventLog: L{None} or L{dict} of L{bytes} -> L{object}
  1881. @ivar eventLog: Information about the last informational log message
  1882. generated or none if no log message has been generated.
  1883. @type noisyAttemptMgr: L{_AttemptManager}
  1884. @ivar noisyAttemptMgr: An attempt manager which generates informational
  1885. log messages.
  1886. @type quietAttemptMgr: L{_AttemptManager}
  1887. @ivar quietAttemptMgr: An attempt manager which does not generate
  1888. informational log messages.
  1889. @type noisyMessage: L{bytes}
  1890. @ivar noisyMessage: The full base pathname of the message to be used with
  1891. the noisy attempt manager.
  1892. @type quietMessage: L{bytes}
  1893. @ivar quietMessage: The full base pathname of the message to be used with
  1894. the quiet.
  1895. """
  1896. def setUp(self):
  1897. """
  1898. Set up a temporary directory for the queue, attempt managers with the
  1899. noisy flag on and off, message files for use with each attempt manager,
  1900. and a reactor. Also, register to be notified when log messages are
  1901. generated.
  1902. """
  1903. self.tmpdir = self.mktemp()
  1904. os.mkdir(self.tmpdir)
  1905. self.reactor = MemoryReactorClock()
  1906. self.eventLog = None
  1907. log.addObserver(self._logObserver)
  1908. self.noisyAttemptMgr = _AttemptManager(
  1909. DummySmartHostSMTPRelayingManager(DummyQueue(self.tmpdir)),
  1910. True, self.reactor)
  1911. self.quietAttemptMgr = _AttemptManager(
  1912. DummySmartHostSMTPRelayingManager(DummyQueue(self.tmpdir)),
  1913. False, self.reactor)
  1914. noisyBaseName = "noisyMessage"
  1915. quietBaseName = "quietMessage"
  1916. self.noisyMessage = os.path.join(self.tmpdir, noisyBaseName)
  1917. self.quietMessage = os.path.join(self.tmpdir, quietBaseName)
  1918. open(self.noisyMessage+'-D', "w").close()
  1919. open(self.quietMessage+'-D', "w").close()
  1920. self.noisyAttemptMgr.manager.managed['noisyRelayer'] = [
  1921. noisyBaseName]
  1922. self.quietAttemptMgr.manager.managed['quietRelayer'] = [
  1923. quietBaseName]
  1924. with open(self.noisyMessage+'-H', 'w') as envelope:
  1925. pickle.dump(['from-noisy@domain', 'to-noisy@domain'], envelope)
  1926. with open(self.quietMessage+'-H', 'w') as envelope:
  1927. pickle.dump(['from-quiet@domain', 'to-quiet@domain'], envelope)
  1928. def tearDown(self):
  1929. """
  1930. Unregister for log events and remove the temporary directory.
  1931. """
  1932. log.removeObserver(self._logObserver)
  1933. shutil.rmtree(self.tmpdir)
  1934. def _logObserver(self, eventDict):
  1935. """
  1936. A log observer.
  1937. @type eventDict: L{dict} of L{bytes} -> L{object}
  1938. @param eventDict: Information about the last informational log message
  1939. generated.
  1940. """
  1941. self.eventLog = eventDict
  1942. def test_initNoisyDefault(self):
  1943. """
  1944. When an attempt manager is created without the noisy parameter, the
  1945. noisy instance variable should default to true.
  1946. """
  1947. am = _AttemptManager(DummySmartHostSMTPRelayingManager(
  1948. DummyQueue(self.tmpdir)))
  1949. self.assertTrue(am.noisy)
  1950. def test_initNoisy(self):
  1951. """
  1952. When an attempt manager is created with the noisy parameter set to
  1953. true, the noisy instance variable should be set to true.
  1954. """
  1955. self.assertTrue(self.noisyAttemptMgr.noisy)
  1956. def test_initQuiet(self):
  1957. """
  1958. When an attempt manager is created with the noisy parameter set to
  1959. false, the noisy instance variable should be set to false.
  1960. """
  1961. self.assertFalse(self.quietAttemptMgr.noisy)
  1962. def test_initReactorDefault(self):
  1963. """
  1964. When an attempt manager is created without the reactor parameter, the
  1965. reactor instance variable should default to the global reactor.
  1966. """
  1967. am = _AttemptManager(DummySmartHostSMTPRelayingManager(
  1968. DummyQueue(self.tmpdir)))
  1969. self.assertEqual(am.reactor, reactor)
  1970. def test_initReactor(self):
  1971. """
  1972. When an attempt manager is created with a reactor provided, the
  1973. reactor instance variable should default to that reactor.
  1974. """
  1975. self.assertEqual(self.noisyAttemptMgr.reactor, self.reactor)
  1976. def test_notifySuccessNoisy(self):
  1977. """
  1978. For an attempt manager with the noisy flag set, notifySuccess should
  1979. result in a log message.
  1980. """
  1981. self.noisyAttemptMgr.notifySuccess('noisyRelayer', self.noisyMessage)
  1982. self.assertTrue(self.eventLog)
  1983. def test_notifySuccessQuiet(self):
  1984. """
  1985. For an attempt manager with the noisy flag not set, notifySuccess
  1986. should result in no log message.
  1987. """
  1988. self.quietAttemptMgr.notifySuccess('quietRelayer', self.quietMessage)
  1989. self.assertFalse(self.eventLog)
  1990. def test_notifyFailureNoisy(self):
  1991. """
  1992. For an attempt manager with the noisy flag set, notifyFailure should
  1993. result in a log message.
  1994. """
  1995. self.noisyAttemptMgr.notifyFailure('noisyRelayer', self.noisyMessage)
  1996. self.assertTrue(self.eventLog)
  1997. def test_notifyFailureQuiet(self):
  1998. """
  1999. For an attempt manager with the noisy flag not set, notifyFailure
  2000. should result in no log message.
  2001. """
  2002. self.quietAttemptMgr.notifyFailure('quietRelayer', self.quietMessage)
  2003. self.assertFalse(self.eventLog)
  2004. def test_notifyDoneNoisy(self):
  2005. """
  2006. For an attempt manager with the noisy flag set, notifyDone should
  2007. result in a log message.
  2008. """
  2009. self.noisyAttemptMgr.notifyDone('noisyRelayer')
  2010. self.assertTrue(self.eventLog)
  2011. def test_notifyDoneQuiet(self):
  2012. """
  2013. For an attempt manager with the noisy flag not set, notifyDone
  2014. should result in no log message.
  2015. """
  2016. self.quietAttemptMgr.notifyDone('quietRelayer')
  2017. self.assertFalse(self.eventLog)
  2018. def test_notifyNoConnectionNoisy(self):
  2019. """
  2020. For an attempt manager with the noisy flag set, notifyNoConnection
  2021. should result in a log message.
  2022. """
  2023. self.noisyAttemptMgr.notifyNoConnection('noisyRelayer')
  2024. self.assertTrue(self.eventLog)
  2025. self.reactor.advance(60)
  2026. def test_notifyNoConnectionQuiet(self):
  2027. """
  2028. For an attempt manager with the noisy flag not set, notifyNoConnection
  2029. should result in no log message.
  2030. """
  2031. self.quietAttemptMgr.notifyNoConnection('quietRelayer')
  2032. self.assertFalse(self.eventLog)
  2033. self.reactor.advance(60)
  2034. from twisted.python.runtime import platformType
  2035. import types
  2036. if platformType != "posix":
  2037. for o in locals().values():
  2038. if isinstance(o, (types.ClassType, type)) and issubclass(o, unittest.TestCase):
  2039. o.skip = "twisted.mail only works on posix"