test_pop3.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Test cases for Ltwisted.mail.pop3} module.
  5. """
  6. from __future__ import print_function
  7. import StringIO
  8. import hmac
  9. import base64
  10. import itertools
  11. from collections import OrderedDict
  12. from zope.interface import implementer
  13. from twisted.internet import defer
  14. from twisted.trial import unittest, util
  15. from twisted import mail
  16. import twisted.mail.protocols
  17. import twisted.mail.pop3
  18. import twisted.internet.protocol
  19. from twisted import internet
  20. from twisted.mail import pop3
  21. from twisted.protocols import loopback
  22. from twisted.python import failure
  23. from twisted import cred
  24. import twisted.cred.portal
  25. import twisted.cred.checkers
  26. import twisted.cred.credentials
  27. from twisted.test.proto_helpers import LineSendingProtocol
  28. class UtilityTests(unittest.TestCase):
  29. """
  30. Test the various helper functions and classes used by the POP3 server
  31. protocol implementation.
  32. """
  33. def testLineBuffering(self):
  34. """
  35. Test creating a LineBuffer and feeding it some lines. The lines should
  36. build up in its internal buffer for a while and then get spat out to
  37. the writer.
  38. """
  39. output = []
  40. input = iter(itertools.cycle(['012', '345', '6', '7', '8', '9']))
  41. c = pop3._IteratorBuffer(output.extend, input, 6)
  42. i = iter(c)
  43. self.assertEqual(output, []) # nothing is buffer
  44. i.next()
  45. self.assertEqual(output, []) # '012' is buffered
  46. i.next()
  47. self.assertEqual(output, []) # '012345' is buffered
  48. i.next()
  49. self.assertEqual(output, ['012', '345', '6']) # nothing is buffered
  50. for n in range(5):
  51. i.next()
  52. self.assertEqual(output, ['012', '345', '6', '7', '8', '9', '012', '345'])
  53. def testFinishLineBuffering(self):
  54. """
  55. Test that a LineBuffer flushes everything when its iterator is
  56. exhausted, and itself raises StopIteration.
  57. """
  58. output = []
  59. input = iter(['a', 'b', 'c'])
  60. c = pop3._IteratorBuffer(output.extend, input, 5)
  61. for i in c:
  62. pass
  63. self.assertEqual(output, ['a', 'b', 'c'])
  64. def testSuccessResponseFormatter(self):
  65. """
  66. Test that the thing that spits out POP3 'success responses' works
  67. right.
  68. """
  69. self.assertEqual(
  70. pop3.successResponse('Great.'),
  71. '+OK Great.\r\n')
  72. def testStatLineFormatter(self):
  73. """
  74. Test that the function which formats stat lines does so appropriately.
  75. """
  76. statLine = list(pop3.formatStatResponse([]))[-1]
  77. self.assertEqual(statLine, '+OK 0 0\r\n')
  78. statLine = list(pop3.formatStatResponse([10, 31, 0, 10101]))[-1]
  79. self.assertEqual(statLine, '+OK 4 10142\r\n')
  80. def testListLineFormatter(self):
  81. """
  82. Test that the function which formats the lines in response to a LIST
  83. command does so appropriately.
  84. """
  85. listLines = list(pop3.formatListResponse([]))
  86. self.assertEqual(
  87. listLines,
  88. ['+OK 0\r\n', '.\r\n'])
  89. listLines = list(pop3.formatListResponse([1, 2, 3, 100]))
  90. self.assertEqual(
  91. listLines,
  92. ['+OK 4\r\n', '1 1\r\n', '2 2\r\n', '3 3\r\n', '4 100\r\n', '.\r\n'])
  93. def testUIDListLineFormatter(self):
  94. """
  95. Test that the function which formats lines in response to a UIDL
  96. command does so appropriately.
  97. """
  98. UIDs = ['abc', 'def', 'ghi']
  99. listLines = list(pop3.formatUIDListResponse([], UIDs.__getitem__))
  100. self.assertEqual(
  101. listLines,
  102. ['+OK \r\n', '.\r\n'])
  103. listLines = list(pop3.formatUIDListResponse([123, 431, 591], UIDs.__getitem__))
  104. self.assertEqual(
  105. listLines,
  106. ['+OK \r\n', '1 abc\r\n', '2 def\r\n', '3 ghi\r\n', '.\r\n'])
  107. listLines = list(pop3.formatUIDListResponse([0, None, 591], UIDs.__getitem__))
  108. self.assertEqual(
  109. listLines,
  110. ['+OK \r\n', '1 abc\r\n', '3 ghi\r\n', '.\r\n'])
  111. class MyVirtualPOP3(mail.protocols.VirtualPOP3):
  112. magic = '<moshez>'
  113. def authenticateUserAPOP(self, user, digest):
  114. user, domain = self.lookupDomain(user)
  115. return self.service.domains['baz.com'].authenticateUserAPOP(user, digest, self.magic, domain)
  116. class DummyDomain:
  117. def __init__(self):
  118. self.users = {}
  119. def addUser(self, name):
  120. self.users[name] = []
  121. def addMessage(self, name, message):
  122. self.users[name].append(message)
  123. def authenticateUserAPOP(self, name, digest, magic, domain):
  124. return pop3.IMailbox, ListMailbox(self.users[name]), lambda: None
  125. class ListMailbox:
  126. def __init__(self, list):
  127. self.list = list
  128. def listMessages(self, i=None):
  129. if i is None:
  130. return map(len, self.list)
  131. return len(self.list[i])
  132. def getMessage(self, i):
  133. return StringIO.StringIO(self.list[i])
  134. def getUidl(self, i):
  135. return i
  136. def deleteMessage(self, i):
  137. self.list[i] = ''
  138. def sync(self):
  139. pass
  140. class MyPOP3Downloader(pop3.POP3Client):
  141. def handle_WELCOME(self, line):
  142. pop3.POP3Client.handle_WELCOME(self, line)
  143. self.apop('hello@baz.com', 'world')
  144. def handle_APOP(self, line):
  145. parts = line.split()
  146. code = parts[0]
  147. if code != '+OK':
  148. raise AssertionError('code is: %s , parts is: %s ' % (code, parts))
  149. self.lines = []
  150. self.retr(1)
  151. def handle_RETR_continue(self, line):
  152. self.lines.append(line)
  153. def handle_RETR_end(self):
  154. self.message = '\n'.join(self.lines) + '\n'
  155. self.quit()
  156. def handle_QUIT(self, line):
  157. if line[:3] != '+OK':
  158. raise AssertionError('code is ' + line)
  159. class POP3Tests(unittest.TestCase):
  160. message = '''\
  161. Subject: urgent
  162. Someone set up us the bomb!
  163. '''
  164. expectedOutput = '''\
  165. +OK <moshez>\015
  166. +OK Authentication succeeded\015
  167. +OK \015
  168. 1 0\015
  169. .\015
  170. +OK %d\015
  171. Subject: urgent\015
  172. \015
  173. Someone set up us the bomb!\015
  174. .\015
  175. +OK \015
  176. ''' % len(message)
  177. def setUp(self):
  178. self.factory = internet.protocol.Factory()
  179. self.factory.domains = {}
  180. self.factory.domains['baz.com'] = DummyDomain()
  181. self.factory.domains['baz.com'].addUser('hello')
  182. self.factory.domains['baz.com'].addMessage('hello', self.message)
  183. def testMessages(self):
  184. client = LineSendingProtocol([
  185. 'APOP hello@baz.com world',
  186. 'UIDL',
  187. 'RETR 1',
  188. 'QUIT',
  189. ])
  190. server = MyVirtualPOP3()
  191. server.service = self.factory
  192. def check(ignored):
  193. output = '\r\n'.join(client.response) + '\r\n'
  194. self.assertEqual(output, self.expectedOutput)
  195. return loopback.loopbackTCP(server, client).addCallback(check)
  196. def testLoopback(self):
  197. protocol = MyVirtualPOP3()
  198. protocol.service = self.factory
  199. clientProtocol = MyPOP3Downloader()
  200. def check(ignored):
  201. self.assertEqual(clientProtocol.message, self.message)
  202. protocol.connectionLost(
  203. failure.Failure(Exception("Test harness disconnect")))
  204. d = loopback.loopbackAsync(protocol, clientProtocol)
  205. return d.addCallback(check)
  206. testLoopback.suppress = [util.suppress(message="twisted.mail.pop3.POP3Client is deprecated")]
  207. class DummyPOP3(pop3.POP3):
  208. magic = '<moshez>'
  209. def authenticateUserAPOP(self, user, password):
  210. return pop3.IMailbox, DummyMailbox(ValueError), lambda: None
  211. class DummyMailbox(pop3.Mailbox):
  212. messages = ['From: moshe\nTo: moshe\n\nHow are you, friend?\n']
  213. def __init__(self, exceptionType):
  214. self.messages = DummyMailbox.messages[:]
  215. self.exceptionType = exceptionType
  216. def listMessages(self, i=None):
  217. if i is None:
  218. return map(len, self.messages)
  219. if i >= len(self.messages):
  220. raise self.exceptionType()
  221. return len(self.messages[i])
  222. def getMessage(self, i):
  223. return StringIO.StringIO(self.messages[i])
  224. def getUidl(self, i):
  225. if i >= len(self.messages):
  226. raise self.exceptionType()
  227. return str(i)
  228. def deleteMessage(self, i):
  229. self.messages[i] = ''
  230. class AnotherPOP3Tests(unittest.TestCase):
  231. def runTest(self, lines, expectedOutput):
  232. dummy = DummyPOP3()
  233. client = LineSendingProtocol(lines)
  234. d = loopback.loopbackAsync(dummy, client)
  235. return d.addCallback(self._cbRunTest, client, dummy, expectedOutput)
  236. def _cbRunTest(self, ignored, client, dummy, expectedOutput):
  237. self.assertEqual('\r\n'.join(expectedOutput),
  238. '\r\n'.join(client.response))
  239. dummy.connectionLost(failure.Failure(Exception("Test harness disconnect")))
  240. return ignored
  241. def test_buffer(self):
  242. """
  243. Test a lot of different POP3 commands in an extremely pipelined
  244. scenario.
  245. This test may cover legitimate behavior, but the intent and
  246. granularity are not very good. It would likely be an improvement to
  247. split it into a number of smaller, more focused tests.
  248. """
  249. return self.runTest(
  250. ["APOP moshez dummy",
  251. "LIST",
  252. "UIDL",
  253. "RETR 1",
  254. "RETR 2",
  255. "DELE 1",
  256. "RETR 1",
  257. "QUIT"],
  258. ['+OK <moshez>',
  259. '+OK Authentication succeeded',
  260. '+OK 1',
  261. '1 44',
  262. '.',
  263. '+OK ',
  264. '1 0',
  265. '.',
  266. '+OK 44',
  267. 'From: moshe',
  268. 'To: moshe',
  269. '',
  270. 'How are you, friend?',
  271. '.',
  272. '-ERR Bad message number argument',
  273. '+OK ',
  274. '-ERR message deleted',
  275. '+OK '])
  276. def test_noop(self):
  277. """
  278. Test the no-op command.
  279. """
  280. return self.runTest(
  281. ['APOP spiv dummy',
  282. 'NOOP',
  283. 'QUIT'],
  284. ['+OK <moshez>',
  285. '+OK Authentication succeeded',
  286. '+OK ',
  287. '+OK '])
  288. def testAuthListing(self):
  289. p = DummyPOP3()
  290. p.factory = internet.protocol.Factory()
  291. p.factory.challengers = {'Auth1': None, 'secondAuth': None, 'authLast': None}
  292. client = LineSendingProtocol([
  293. "AUTH",
  294. "QUIT",
  295. ])
  296. d = loopback.loopbackAsync(p, client)
  297. return d.addCallback(self._cbTestAuthListing, client)
  298. def _cbTestAuthListing(self, ignored, client):
  299. self.assertTrue(client.response[1].startswith('+OK'))
  300. self.assertEqual(sorted(client.response[2:5]),
  301. ["AUTH1", "AUTHLAST", "SECONDAUTH"])
  302. self.assertEqual(client.response[5], ".")
  303. def testIllegalPASS(self):
  304. dummy = DummyPOP3()
  305. client = LineSendingProtocol([
  306. "PASS fooz",
  307. "QUIT"
  308. ])
  309. d = loopback.loopbackAsync(dummy, client)
  310. return d.addCallback(self._cbTestIllegalPASS, client, dummy)
  311. def _cbTestIllegalPASS(self, ignored, client, dummy):
  312. expected_output = '+OK <moshez>\r\n-ERR USER required before PASS\r\n+OK \r\n'
  313. self.assertEqual(expected_output, '\r\n'.join(client.response) + '\r\n')
  314. dummy.connectionLost(failure.Failure(Exception("Test harness disconnect")))
  315. def testEmptyPASS(self):
  316. dummy = DummyPOP3()
  317. client = LineSendingProtocol([
  318. "PASS ",
  319. "QUIT"
  320. ])
  321. d = loopback.loopbackAsync(dummy, client)
  322. return d.addCallback(self._cbTestEmptyPASS, client, dummy)
  323. def _cbTestEmptyPASS(self, ignored, client, dummy):
  324. expected_output = '+OK <moshez>\r\n-ERR USER required before PASS\r\n+OK \r\n'
  325. self.assertEqual(expected_output, '\r\n'.join(client.response) + '\r\n')
  326. dummy.connectionLost(failure.Failure(Exception("Test harness disconnect")))
  327. @implementer(pop3.IServerFactory)
  328. class TestServerFactory:
  329. def cap_IMPLEMENTATION(self):
  330. return "Test Implementation String"
  331. def cap_EXPIRE(self):
  332. return 60
  333. challengers = OrderedDict([("SCHEME_1", None), ("SCHEME_2", None)])
  334. def cap_LOGIN_DELAY(self):
  335. return 120
  336. pue = True
  337. def perUserExpiration(self):
  338. return self.pue
  339. puld = True
  340. def perUserLoginDelay(self):
  341. return self.puld
  342. class TestMailbox:
  343. loginDelay = 100
  344. messageExpiration = 25
  345. class CapabilityTests(unittest.TestCase):
  346. def setUp(self):
  347. s = StringIO.StringIO()
  348. p = pop3.POP3()
  349. p.factory = TestServerFactory()
  350. p.transport = internet.protocol.FileWrapper(s)
  351. p.connectionMade()
  352. p.do_CAPA()
  353. self.caps = p.listCapabilities()
  354. self.pcaps = s.getvalue().splitlines()
  355. s = StringIO.StringIO()
  356. p.mbox = TestMailbox()
  357. p.transport = internet.protocol.FileWrapper(s)
  358. p.do_CAPA()
  359. self.lpcaps = s.getvalue().splitlines()
  360. p.connectionLost(failure.Failure(Exception("Test harness disconnect")))
  361. def contained(self, s, *caps):
  362. for c in caps:
  363. self.assertIn(s, c)
  364. def testUIDL(self):
  365. self.contained("UIDL", self.caps, self.pcaps, self.lpcaps)
  366. def testTOP(self):
  367. self.contained("TOP", self.caps, self.pcaps, self.lpcaps)
  368. def testUSER(self):
  369. self.contained("USER", self.caps, self.pcaps, self.lpcaps)
  370. def testEXPIRE(self):
  371. self.contained("EXPIRE 60 USER", self.caps, self.pcaps)
  372. self.contained("EXPIRE 25", self.lpcaps)
  373. def testIMPLEMENTATION(self):
  374. self.contained(
  375. "IMPLEMENTATION Test Implementation String",
  376. self.caps, self.pcaps, self.lpcaps
  377. )
  378. def testSASL(self):
  379. self.contained(
  380. "SASL SCHEME_1 SCHEME_2",
  381. self.caps, self.pcaps, self.lpcaps
  382. )
  383. def testLOGIN_DELAY(self):
  384. self.contained("LOGIN-DELAY 120 USER", self.caps, self.pcaps)
  385. self.assertIn("LOGIN-DELAY 100", self.lpcaps)
  386. class GlobalCapabilitiesTests(unittest.TestCase):
  387. def setUp(self):
  388. s = StringIO.StringIO()
  389. p = pop3.POP3()
  390. p.factory = TestServerFactory()
  391. p.factory.pue = p.factory.puld = False
  392. p.transport = internet.protocol.FileWrapper(s)
  393. p.connectionMade()
  394. p.do_CAPA()
  395. self.caps = p.listCapabilities()
  396. self.pcaps = s.getvalue().splitlines()
  397. s = StringIO.StringIO()
  398. p.mbox = TestMailbox()
  399. p.transport = internet.protocol.FileWrapper(s)
  400. p.do_CAPA()
  401. self.lpcaps = s.getvalue().splitlines()
  402. p.connectionLost(failure.Failure(Exception("Test harness disconnect")))
  403. def contained(self, s, *caps):
  404. for c in caps:
  405. self.assertIn(s, c)
  406. def testEXPIRE(self):
  407. self.contained("EXPIRE 60", self.caps, self.pcaps, self.lpcaps)
  408. def testLOGIN_DELAY(self):
  409. self.contained("LOGIN-DELAY 120", self.caps, self.pcaps, self.lpcaps)
  410. class TestRealm:
  411. def requestAvatar(self, avatarId, mind, *interfaces):
  412. if avatarId == 'testuser':
  413. return pop3.IMailbox, DummyMailbox(ValueError), lambda: None
  414. assert False
  415. class SASLTests(unittest.TestCase):
  416. def testValidLogin(self):
  417. p = pop3.POP3()
  418. p.factory = TestServerFactory()
  419. p.factory.challengers = {'CRAM-MD5': cred.credentials.CramMD5Credentials}
  420. p.portal = cred.portal.Portal(TestRealm())
  421. ch = cred.checkers.InMemoryUsernamePasswordDatabaseDontUse()
  422. ch.addUser('testuser', 'testpassword')
  423. p.portal.registerChecker(ch)
  424. s = StringIO.StringIO()
  425. p.transport = internet.protocol.FileWrapper(s)
  426. p.connectionMade()
  427. p.lineReceived("CAPA")
  428. self.assertTrue(s.getvalue().find("SASL CRAM-MD5") >= 0)
  429. p.lineReceived("AUTH CRAM-MD5")
  430. chal = s.getvalue().splitlines()[-1][2:]
  431. chal = base64.decodestring(chal)
  432. response = hmac.HMAC('testpassword', chal).hexdigest()
  433. p.lineReceived(base64.encodestring('testuser ' + response).rstrip('\n'))
  434. self.assertTrue(p.mbox)
  435. self.assertTrue(s.getvalue().splitlines()[-1].find("+OK") >= 0)
  436. p.connectionLost(failure.Failure(Exception("Test harness disconnect")))
  437. class CommandMixin:
  438. """
  439. Tests for all the commands a POP3 server is allowed to receive.
  440. """
  441. extraMessage = '''\
  442. From: guy
  443. To: fellow
  444. More message text for you.
  445. '''
  446. def setUp(self):
  447. """
  448. Make a POP3 server protocol instance hooked up to a simple mailbox and
  449. a transport that buffers output to a StringIO.
  450. """
  451. p = pop3.POP3()
  452. p.mbox = self.mailboxType(self.exceptionType)
  453. p.schedule = list
  454. self.pop3Server = p
  455. s = StringIO.StringIO()
  456. p.transport = internet.protocol.FileWrapper(s)
  457. p.connectionMade()
  458. s.truncate(0)
  459. self.pop3Transport = s
  460. def tearDown(self):
  461. """
  462. Disconnect the server protocol so it can clean up anything it might
  463. need to clean up.
  464. """
  465. self.pop3Server.connectionLost(failure.Failure(Exception("Test harness disconnect")))
  466. def _flush(self):
  467. """
  468. Do some of the things that the reactor would take care of, if the
  469. reactor were actually running.
  470. """
  471. # Oh man FileWrapper is pooh.
  472. self.pop3Server.transport._checkProducer()
  473. def testLIST(self):
  474. """
  475. Test the two forms of list: with a message index number, which should
  476. return a short-form response, and without a message index number, which
  477. should return a long-form response, one line per message.
  478. """
  479. p = self.pop3Server
  480. s = self.pop3Transport
  481. p.lineReceived("LIST 1")
  482. self._flush()
  483. self.assertEqual(s.getvalue(), "+OK 1 44\r\n")
  484. s.truncate(0)
  485. p.lineReceived("LIST")
  486. self._flush()
  487. self.assertEqual(s.getvalue(), "+OK 1\r\n1 44\r\n.\r\n")
  488. def testLISTWithBadArgument(self):
  489. """
  490. Test that non-integers and out-of-bound integers produce appropriate
  491. error responses.
  492. """
  493. p = self.pop3Server
  494. s = self.pop3Transport
  495. p.lineReceived("LIST a")
  496. self.assertEqual(
  497. s.getvalue(),
  498. "-ERR Invalid message-number: 'a'\r\n")
  499. s.truncate(0)
  500. p.lineReceived("LIST 0")
  501. self.assertEqual(
  502. s.getvalue(),
  503. "-ERR Invalid message-number: 0\r\n")
  504. s.truncate(0)
  505. p.lineReceived("LIST 2")
  506. self.assertEqual(
  507. s.getvalue(),
  508. "-ERR Invalid message-number: 2\r\n")
  509. s.truncate(0)
  510. def testUIDL(self):
  511. """
  512. Test the two forms of the UIDL command. These are just like the two
  513. forms of the LIST command.
  514. """
  515. p = self.pop3Server
  516. s = self.pop3Transport
  517. p.lineReceived("UIDL 1")
  518. self.assertEqual(s.getvalue(), "+OK 0\r\n")
  519. s.truncate(0)
  520. p.lineReceived("UIDL")
  521. self._flush()
  522. self.assertEqual(s.getvalue(), "+OK \r\n1 0\r\n.\r\n")
  523. def testUIDLWithBadArgument(self):
  524. """
  525. Test that UIDL with a non-integer or an out-of-bounds integer produces
  526. the appropriate error response.
  527. """
  528. p = self.pop3Server
  529. s = self.pop3Transport
  530. p.lineReceived("UIDL a")
  531. self.assertEqual(
  532. s.getvalue(),
  533. "-ERR Bad message number argument\r\n")
  534. s.truncate(0)
  535. p.lineReceived("UIDL 0")
  536. self.assertEqual(
  537. s.getvalue(),
  538. "-ERR Bad message number argument\r\n")
  539. s.truncate(0)
  540. p.lineReceived("UIDL 2")
  541. self.assertEqual(
  542. s.getvalue(),
  543. "-ERR Bad message number argument\r\n")
  544. s.truncate(0)
  545. def testSTAT(self):
  546. """
  547. Test the single form of the STAT command, which returns a short-form
  548. response of the number of messages in the mailbox and their total size.
  549. """
  550. p = self.pop3Server
  551. s = self.pop3Transport
  552. p.lineReceived("STAT")
  553. self._flush()
  554. self.assertEqual(s.getvalue(), "+OK 1 44\r\n")
  555. def testRETR(self):
  556. """
  557. Test downloading a message.
  558. """
  559. p = self.pop3Server
  560. s = self.pop3Transport
  561. p.lineReceived("RETR 1")
  562. self._flush()
  563. self.assertEqual(
  564. s.getvalue(),
  565. "+OK 44\r\n"
  566. "From: moshe\r\n"
  567. "To: moshe\r\n"
  568. "\r\n"
  569. "How are you, friend?\r\n"
  570. ".\r\n")
  571. s.truncate(0)
  572. def testRETRWithBadArgument(self):
  573. """
  574. Test that trying to download a message with a bad argument, either not
  575. an integer or an out-of-bounds integer, fails with the appropriate
  576. error response.
  577. """
  578. p = self.pop3Server
  579. s = self.pop3Transport
  580. p.lineReceived("RETR a")
  581. self.assertEqual(
  582. s.getvalue(),
  583. "-ERR Bad message number argument\r\n")
  584. s.truncate(0)
  585. p.lineReceived("RETR 0")
  586. self.assertEqual(
  587. s.getvalue(),
  588. "-ERR Bad message number argument\r\n")
  589. s.truncate(0)
  590. p.lineReceived("RETR 2")
  591. self.assertEqual(
  592. s.getvalue(),
  593. "-ERR Bad message number argument\r\n")
  594. s.truncate(0)
  595. def testTOP(self):
  596. """
  597. Test downloading the headers and part of the body of a message.
  598. """
  599. p = self.pop3Server
  600. s = self.pop3Transport
  601. p.mbox.messages.append(self.extraMessage)
  602. p.lineReceived("TOP 1 0")
  603. self._flush()
  604. self.assertEqual(
  605. s.getvalue(),
  606. "+OK Top of message follows\r\n"
  607. "From: moshe\r\n"
  608. "To: moshe\r\n"
  609. "\r\n"
  610. ".\r\n")
  611. def testTOPWithBadArgument(self):
  612. """
  613. Test that trying to download a message with a bad argument, either a
  614. message number which isn't an integer or is an out-of-bounds integer or
  615. a number of lines which isn't an integer or is a negative integer,
  616. fails with the appropriate error response.
  617. """
  618. p = self.pop3Server
  619. s = self.pop3Transport
  620. p.mbox.messages.append(self.extraMessage)
  621. p.lineReceived("TOP 1 a")
  622. self.assertEqual(
  623. s.getvalue(),
  624. "-ERR Bad line count argument\r\n")
  625. s.truncate(0)
  626. p.lineReceived("TOP 1 -1")
  627. self.assertEqual(
  628. s.getvalue(),
  629. "-ERR Bad line count argument\r\n")
  630. s.truncate(0)
  631. p.lineReceived("TOP a 1")
  632. self.assertEqual(
  633. s.getvalue(),
  634. "-ERR Bad message number argument\r\n")
  635. s.truncate(0)
  636. p.lineReceived("TOP 0 1")
  637. self.assertEqual(
  638. s.getvalue(),
  639. "-ERR Bad message number argument\r\n")
  640. s.truncate(0)
  641. p.lineReceived("TOP 3 1")
  642. self.assertEqual(
  643. s.getvalue(),
  644. "-ERR Bad message number argument\r\n")
  645. s.truncate(0)
  646. def testLAST(self):
  647. """
  648. Test the exceedingly pointless LAST command, which tells you the
  649. highest message index which you have already downloaded.
  650. """
  651. p = self.pop3Server
  652. s = self.pop3Transport
  653. p.mbox.messages.append(self.extraMessage)
  654. p.lineReceived('LAST')
  655. self.assertEqual(
  656. s.getvalue(),
  657. "+OK 0\r\n")
  658. s.truncate(0)
  659. def testRetrieveUpdatesHighest(self):
  660. """
  661. Test that issuing a RETR command updates the LAST response.
  662. """
  663. p = self.pop3Server
  664. s = self.pop3Transport
  665. p.mbox.messages.append(self.extraMessage)
  666. p.lineReceived('RETR 2')
  667. self._flush()
  668. s.truncate(0)
  669. p.lineReceived('LAST')
  670. self.assertEqual(
  671. s.getvalue(),
  672. '+OK 2\r\n')
  673. s.truncate(0)
  674. def testTopUpdatesHighest(self):
  675. """
  676. Test that issuing a TOP command updates the LAST response.
  677. """
  678. p = self.pop3Server
  679. s = self.pop3Transport
  680. p.mbox.messages.append(self.extraMessage)
  681. p.lineReceived('TOP 2 10')
  682. self._flush()
  683. s.truncate(0)
  684. p.lineReceived('LAST')
  685. self.assertEqual(
  686. s.getvalue(),
  687. '+OK 2\r\n')
  688. def testHighestOnlyProgresses(self):
  689. """
  690. Test that downloading a message with a smaller index than the current
  691. LAST response doesn't change the LAST response.
  692. """
  693. p = self.pop3Server
  694. s = self.pop3Transport
  695. p.mbox.messages.append(self.extraMessage)
  696. p.lineReceived('RETR 2')
  697. self._flush()
  698. p.lineReceived('TOP 1 10')
  699. self._flush()
  700. s.truncate(0)
  701. p.lineReceived('LAST')
  702. self.assertEqual(
  703. s.getvalue(),
  704. '+OK 2\r\n')
  705. def testResetClearsHighest(self):
  706. """
  707. Test that issuing RSET changes the LAST response to 0.
  708. """
  709. p = self.pop3Server
  710. s = self.pop3Transport
  711. p.mbox.messages.append(self.extraMessage)
  712. p.lineReceived('RETR 2')
  713. self._flush()
  714. p.lineReceived('RSET')
  715. s.truncate(0)
  716. p.lineReceived('LAST')
  717. self.assertEqual(
  718. s.getvalue(),
  719. '+OK 0\r\n')
  720. _listMessageDeprecation = (
  721. "twisted.mail.pop3.IMailbox.listMessages may not "
  722. "raise IndexError for out-of-bounds message numbers: "
  723. "raise ValueError instead.")
  724. _listMessageSuppression = util.suppress(
  725. message=_listMessageDeprecation,
  726. category=PendingDeprecationWarning)
  727. _getUidlDeprecation = (
  728. "twisted.mail.pop3.IMailbox.getUidl may not "
  729. "raise IndexError for out-of-bounds message numbers: "
  730. "raise ValueError instead.")
  731. _getUidlSuppression = util.suppress(
  732. message=_getUidlDeprecation,
  733. category=PendingDeprecationWarning)
  734. class IndexErrorCommandTests(CommandMixin, unittest.TestCase):
  735. """
  736. Run all of the command tests against a mailbox which raises IndexError
  737. when an out of bounds request is made. This behavior will be deprecated
  738. shortly and then removed.
  739. """
  740. exceptionType = IndexError
  741. mailboxType = DummyMailbox
  742. def testLISTWithBadArgument(self):
  743. return CommandMixin.testLISTWithBadArgument(self)
  744. testLISTWithBadArgument.suppress = [_listMessageSuppression]
  745. def testUIDLWithBadArgument(self):
  746. return CommandMixin.testUIDLWithBadArgument(self)
  747. testUIDLWithBadArgument.suppress = [_getUidlSuppression]
  748. def testTOPWithBadArgument(self):
  749. return CommandMixin.testTOPWithBadArgument(self)
  750. testTOPWithBadArgument.suppress = [_listMessageSuppression]
  751. def testRETRWithBadArgument(self):
  752. return CommandMixin.testRETRWithBadArgument(self)
  753. testRETRWithBadArgument.suppress = [_listMessageSuppression]
  754. class ValueErrorCommandTests(CommandMixin, unittest.TestCase):
  755. """
  756. Run all of the command tests against a mailbox which raises ValueError
  757. when an out of bounds request is made. This is the correct behavior and
  758. after support for mailboxes which raise IndexError is removed, this will
  759. become just C{CommandTestCase}.
  760. """
  761. exceptionType = ValueError
  762. mailboxType = DummyMailbox
  763. class SyncDeferredMailbox(DummyMailbox):
  764. """
  765. Mailbox which has a listMessages implementation which returns a Deferred
  766. which has already fired.
  767. """
  768. def listMessages(self, n=None):
  769. return defer.succeed(DummyMailbox.listMessages(self, n))
  770. class IndexErrorSyncDeferredCommandTests(IndexErrorCommandTests):
  771. """
  772. Run all of the L{IndexErrorCommandTests} tests with a
  773. synchronous-Deferred returning IMailbox implementation.
  774. """
  775. mailboxType = SyncDeferredMailbox
  776. class ValueErrorSyncDeferredCommandTests(ValueErrorCommandTests):
  777. """
  778. Run all of the L{ValueErrorCommandTests} tests with a
  779. synchronous-Deferred returning IMailbox implementation.
  780. """
  781. mailboxType = SyncDeferredMailbox
  782. class AsyncDeferredMailbox(DummyMailbox):
  783. """
  784. Mailbox which has a listMessages implementation which returns a Deferred
  785. which has not yet fired.
  786. """
  787. def __init__(self, *a, **kw):
  788. self.waiting = []
  789. DummyMailbox.__init__(self, *a, **kw)
  790. def listMessages(self, n=None):
  791. d = defer.Deferred()
  792. # See AsyncDeferredMailbox._flush
  793. self.waiting.append((d, DummyMailbox.listMessages(self, n)))
  794. return d
  795. class IndexErrorAsyncDeferredCommandTests(IndexErrorCommandTests):
  796. """
  797. Run all of the L{IndexErrorCommandTests} tests with an asynchronous-Deferred
  798. returning IMailbox implementation.
  799. """
  800. mailboxType = AsyncDeferredMailbox
  801. def _flush(self):
  802. """
  803. Fire whatever Deferreds we've built up in our mailbox.
  804. """
  805. while self.pop3Server.mbox.waiting:
  806. d, a = self.pop3Server.mbox.waiting.pop()
  807. d.callback(a)
  808. IndexErrorCommandTests._flush(self)
  809. class ValueErrorAsyncDeferredCommandTests(ValueErrorCommandTests):
  810. """
  811. Run all of the L{IndexErrorCommandTests} tests with an asynchronous-Deferred
  812. returning IMailbox implementation.
  813. """
  814. mailboxType = AsyncDeferredMailbox
  815. def _flush(self):
  816. """
  817. Fire whatever Deferreds we've built up in our mailbox.
  818. """
  819. while self.pop3Server.mbox.waiting:
  820. d, a = self.pop3Server.mbox.waiting.pop()
  821. d.callback(a)
  822. ValueErrorCommandTests._flush(self)
  823. class POP3MiscTests(unittest.TestCase):
  824. """
  825. Miscellaneous tests more to do with module/package structure than
  826. anything to do with the Post Office Protocol.
  827. """
  828. def test_all(self):
  829. """
  830. This test checks that all names listed in
  831. twisted.mail.pop3.__all__ are actually present in the module.
  832. """
  833. mod = twisted.mail.pop3
  834. for attr in mod.__all__:
  835. self.assertTrue(hasattr(mod, attr))