test_options.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Tests for L{twisted.mail.tap}.
  5. """
  6. from twisted.trial.unittest import TestCase
  7. from twisted.python.usage import UsageError
  8. from twisted.mail import protocols
  9. from twisted.mail.tap import Options, makeService
  10. from twisted.python.reflect import requireModule
  11. from twisted.internet import endpoints, defer
  12. if requireModule('OpenSSL') is None:
  13. sslSkip = 'Missing OpenSSL package.'
  14. else:
  15. sslSkip = None
  16. class OptionsTests(TestCase):
  17. """
  18. Tests for the command line option parser used for I{twistd mail}.
  19. """
  20. def setUp(self):
  21. self.aliasFilename = self.mktemp()
  22. with open(self.aliasFilename, 'w') as aliasFile:
  23. aliasFile.write('someuser:\tdifferentuser\n')
  24. def testAliasesWithoutDomain(self):
  25. """
  26. Test that adding an aliases(5) file before adding a domain raises a
  27. UsageError.
  28. """
  29. self.assertRaises(
  30. UsageError,
  31. Options().parseOptions,
  32. ['--aliases', self.aliasFilename])
  33. def testAliases(self):
  34. """
  35. Test that adding an aliases(5) file to an IAliasableDomain at least
  36. doesn't raise an unhandled exception.
  37. """
  38. Options().parseOptions([
  39. '--maildirdbmdomain', 'example.com=example.com',
  40. '--aliases', self.aliasFilename])
  41. def _endpointTest(self, service):
  42. """
  43. Use L{Options} to parse a single service configuration parameter and
  44. verify that an endpoint of the correct type is added to the list for
  45. that service.
  46. """
  47. options = Options()
  48. options.parseOptions(['--' + service, 'tcp:1234'])
  49. self.assertEqual(len(options[service]), 1)
  50. self.assertIsInstance(
  51. options[service][0], endpoints.TCP4ServerEndpoint)
  52. def test_endpointSMTP(self):
  53. """
  54. When I{--smtp} is given a TCP endpoint description as an argument, a
  55. TCPServerEndpoint is added to the list of SMTP endpoints.
  56. """
  57. self._endpointTest('smtp')
  58. def test_endpointPOP3(self):
  59. """
  60. When I{--pop3} is given a TCP endpoint description as an argument, a
  61. TCPServerEndpoint is added to the list of POP3 endpoints.
  62. """
  63. self._endpointTest('pop3')
  64. def test_protoDefaults(self):
  65. """
  66. POP3 and SMTP each listen on a TCP4ServerEndpoint by default.
  67. """
  68. options = Options()
  69. options.parseOptions([])
  70. self.assertEqual(len(options['pop3']), 1)
  71. self.assertIsInstance(
  72. options['pop3'][0], endpoints.TCP4ServerEndpoint)
  73. self.assertEqual(len(options['smtp']), 1)
  74. self.assertIsInstance(
  75. options['smtp'][0], endpoints.TCP4ServerEndpoint)
  76. def test_protoDisable(self):
  77. """
  78. The I{--no-pop3} and I{--no-smtp} options disable POP3 and SMTP
  79. respectively.
  80. """
  81. options = Options()
  82. options.parseOptions(['--no-pop3'])
  83. self.assertEqual(options._getEndpoints(None, 'pop3'), [])
  84. self.assertNotEqual(options._getEndpoints(None, 'smtp'), [])
  85. options = Options()
  86. options.parseOptions(['--no-smtp'])
  87. self.assertNotEqual(options._getEndpoints(None, 'pop3'), [])
  88. self.assertEqual(options._getEndpoints(None, 'smtp'), [])
  89. def test_allProtosDisabledError(self):
  90. """
  91. If all protocols are disabled, L{UsageError} is raised.
  92. """
  93. options = Options()
  94. self.assertRaises(
  95. UsageError, options.parseOptions, (['--no-pop3', '--no-smtp']))
  96. def test_esmtpWithoutHostname(self):
  97. """
  98. If I{--esmtp} is given without I{--hostname}, L{Options.parseOptions}
  99. raises L{UsageError}.
  100. """
  101. options = Options()
  102. exc = self.assertRaises(UsageError, options.parseOptions, ['--esmtp'])
  103. self.assertEqual("--esmtp requires --hostname", str(exc))
  104. def test_auth(self):
  105. """
  106. Tests that the --auth option registers a checker.
  107. """
  108. options = Options()
  109. options.parseOptions(['--auth', 'memory:admin:admin:bob:password'])
  110. self.assertEqual(len(options['credCheckers']), 1)
  111. checker = options['credCheckers'][0]
  112. interfaces = checker.credentialInterfaces
  113. registered_checkers = options.service.smtpPortal.checkers
  114. for iface in interfaces:
  115. self.assertEqual(checker, registered_checkers[iface])
  116. class SpyEndpoint(object):
  117. """
  118. SpyEndpoint remembers what factory it is told to listen with.
  119. """
  120. listeningWith = None
  121. def listen(self, factory):
  122. self.listeningWith = factory
  123. return defer.succeed(None)
  124. class MakeServiceTests(TestCase):
  125. """
  126. Tests for L{twisted.mail.tap.makeService}
  127. """
  128. def _endpointServerTest(self, key, factoryClass):
  129. """
  130. Configure a service with two endpoints for the protocol associated with
  131. C{key} and verify that when the service is started a factory of type
  132. C{factoryClass} is used to listen on each of them.
  133. """
  134. cleartext = SpyEndpoint()
  135. secure = SpyEndpoint()
  136. config = Options()
  137. config[key] = [cleartext, secure]
  138. service = makeService(config)
  139. service.privilegedStartService()
  140. service.startService()
  141. self.addCleanup(service.stopService)
  142. self.assertIsInstance(cleartext.listeningWith, factoryClass)
  143. self.assertIsInstance(secure.listeningWith, factoryClass)
  144. def test_pop3(self):
  145. """
  146. If one or more endpoints is included in the configuration passed to
  147. L{makeService} for the C{"pop3"} key, a service for starting a POP3
  148. server is constructed for each of them and attached to the returned
  149. service.
  150. """
  151. self._endpointServerTest("pop3", protocols.POP3Factory)
  152. def test_smtp(self):
  153. """
  154. If one or more endpoints is included in the configuration passed to
  155. L{makeService} for the C{"smtp"} key, a service for starting an SMTP
  156. server is constructed for each of them and attached to the returned
  157. service.
  158. """
  159. self._endpointServerTest("smtp", protocols.SMTPFactory)