test_tap.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Tests for L{twisted.names.tap}.
  5. """
  6. from twisted.internet.base import ThreadedResolver
  7. from twisted.names.client import Resolver
  8. from twisted.names.dns import PORT
  9. from twisted.names.resolve import ResolverChain
  10. from twisted.names.secondary import SecondaryAuthorityService
  11. from twisted.names.tap import Options, _buildResolvers
  12. from twisted.python.compat import _PY3
  13. from twisted.python.runtime import platform
  14. from twisted.python.usage import UsageError
  15. from twisted.trial.unittest import SynchronousTestCase
  16. class OptionsTests(SynchronousTestCase):
  17. """
  18. Tests for L{Options}, defining how command line arguments for the DNS server
  19. are parsed.
  20. """
  21. def test_malformedSecondary(self):
  22. """
  23. If the value supplied for an I{--secondary} option does not provide a
  24. server IP address, optional port number, and domain name,
  25. L{Options.parseOptions} raises L{UsageError}.
  26. """
  27. options = Options()
  28. self.assertRaises(
  29. UsageError, options.parseOptions, ['--secondary', ''])
  30. self.assertRaises(
  31. UsageError, options.parseOptions, ['--secondary', '1.2.3.4'])
  32. self.assertRaises(
  33. UsageError, options.parseOptions, ['--secondary', '1.2.3.4:hello'])
  34. self.assertRaises(
  35. UsageError, options.parseOptions,
  36. ['--secondary', '1.2.3.4:hello/example.com'])
  37. def test_secondary(self):
  38. """
  39. An argument of the form C{"ip/domain"} is parsed by L{Options} for the
  40. I{--secondary} option and added to its list of secondaries, using the
  41. default DNS port number.
  42. """
  43. options = Options()
  44. options.parseOptions(['--secondary', '1.2.3.4/example.com'])
  45. self.assertEqual(
  46. [(('1.2.3.4', PORT), ['example.com'])], options.secondaries)
  47. def test_secondaryExplicitPort(self):
  48. """
  49. An argument of the form C{"ip:port/domain"} can be used to specify an
  50. alternate port number for which to act as a secondary.
  51. """
  52. options = Options()
  53. options.parseOptions(['--secondary', '1.2.3.4:5353/example.com'])
  54. self.assertEqual(
  55. [(('1.2.3.4', 5353), ['example.com'])], options.secondaries)
  56. def test_secondaryAuthorityServices(self):
  57. """
  58. After parsing I{--secondary} options, L{Options} constructs a
  59. L{SecondaryAuthorityService} instance for each configured secondary.
  60. """
  61. options = Options()
  62. options.parseOptions(['--secondary', '1.2.3.4:5353/example.com',
  63. '--secondary', '1.2.3.5:5354/example.com'])
  64. self.assertEqual(len(options.svcs), 2)
  65. secondary = options.svcs[0]
  66. self.assertIsInstance(options.svcs[0], SecondaryAuthorityService)
  67. self.assertEqual(secondary.primary, '1.2.3.4')
  68. self.assertEqual(secondary._port, 5353)
  69. secondary = options.svcs[1]
  70. self.assertIsInstance(options.svcs[1], SecondaryAuthorityService)
  71. self.assertEqual(secondary.primary, '1.2.3.5')
  72. self.assertEqual(secondary._port, 5354)
  73. def test_recursiveConfiguration(self):
  74. """
  75. Recursive DNS lookups, if enabled, should be a last-resort option.
  76. Any other lookup method (cache, local lookup, etc.) should take
  77. precedence over recursive lookups
  78. """
  79. options = Options()
  80. options.parseOptions(['--hosts-file', 'hosts.txt', '--recursive'])
  81. ca, cl = _buildResolvers(options)
  82. # Extra cleanup, necessary on POSIX because client.Resolver doesn't know
  83. # when to stop parsing resolv.conf. See #NNN for improving this.
  84. for x in cl:
  85. if isinstance(x, ResolverChain):
  86. recurser = x.resolvers[-1]
  87. if isinstance(recurser, Resolver):
  88. recurser._parseCall.cancel()
  89. # On Windows, we need to use a threaded resolver, which leaves trash
  90. # lying about that we can't easily clean up without reaching into the
  91. # reactor and cancelling them. We only cancel the cleanup functions, as
  92. # there should be no others (and it leaving a callLater lying about
  93. # should rightly cause the test to fail).
  94. if platform.getType() != 'posix':
  95. # We want the delayed calls on the reactor, which should be all of
  96. # ours from the threaded resolver cleanup
  97. from twisted.internet import reactor
  98. for x in reactor._newTimedCalls:
  99. if _PY3:
  100. self.assertEqual(x.func.__func__,
  101. ThreadedResolver._cleanup)
  102. else:
  103. self.assertEqual(x.func.__func__,
  104. ThreadedResolver._cleanup.__func__)
  105. x.cancel()
  106. self.assertIsInstance(cl[-1], ResolverChain)