1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
5 Tests for L{twisted.names.tap}.
8 from twisted.trial.unittest import TestCase
9 from twisted.python.usage import UsageError
10 from twisted.names.tap import Options, _buildResolvers
11 from twisted.names.dns import PORT
12 from twisted.names.secondary import SecondaryAuthorityService
13 from twisted.names.resolve import ResolverChain
14 from twisted.names.client import Resolver
16 class OptionsTests(TestCase):
18 Tests for L{Options}, defining how command line arguments for the DNS server
21 def test_malformedSecondary(self):
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}.
29 UsageError, options.parseOptions, ['--secondary', ''])
31 UsageError, options.parseOptions, ['--secondary', '1.2.3.4'])
33 UsageError, options.parseOptions, ['--secondary', '1.2.3.4:hello'])
35 UsageError, options.parseOptions,
36 ['--secondary', '1.2.3.4:hello/example.com'])
39 def test_secondary(self):
41 An argument of the form C{"ip/domain"} is parsed by L{Options} for the
42 I{--secondary} option and added to its list of secondaries, using the
43 default DNS port number.
46 options.parseOptions(['--secondary', '1.2.3.4/example.com'])
48 [(('1.2.3.4', PORT), ['example.com'])], options.secondaries)
51 def test_secondaryExplicitPort(self):
53 An argument of the form C{"ip:port/domain"} can be used to specify an
54 alternate port number for for which to act as a secondary.
57 options.parseOptions(['--secondary', '1.2.3.4:5353/example.com'])
59 [(('1.2.3.4', 5353), ['example.com'])], options.secondaries)
62 def test_secondaryAuthorityServices(self):
64 After parsing I{--secondary} options, L{Options} constructs a
65 L{SecondaryAuthorityService} instance for each configured secondary.
68 options.parseOptions(['--secondary', '1.2.3.4:5353/example.com',
69 '--secondary', '1.2.3.5:5354/example.com'])
70 self.assertEqual(len(options.svcs), 2)
71 secondary = options.svcs[0]
72 self.assertIsInstance(options.svcs[0], SecondaryAuthorityService)
73 self.assertEqual(secondary.primary, '1.2.3.4')
74 self.assertEqual(secondary._port, 5353)
75 secondary = options.svcs[1]
76 self.assertIsInstance(options.svcs[1], SecondaryAuthorityService)
77 self.assertEqual(secondary.primary, '1.2.3.5')
78 self.assertEqual(secondary._port, 5354)
81 def test_recursiveConfiguration(self):
83 Recursive DNS lookups, if enabled, should be a last-resort option.
84 Any other lookup method (cache, local lookup, etc.) should take
85 precedence over recursive lookups
88 options.parseOptions(['--hosts-file', 'hosts.txt', '--recursive'])
89 ca, cl = _buildResolvers(options)
91 # Extra cleanup, necessary on POSIX because client.Resolver doesn't know
92 # when to stop parsing resolv.conf. See #NNN for improving this.
94 if isinstance(x, ResolverChain):
95 recurser = x.resolvers[-1]
96 if isinstance(recurser, Resolver):
97 recurser._parseCall.cancel()
99 self.assertIsInstance(cl[-1], ResolverChain)