Initial import to Tizen
[profile/ivi/python-twisted.git] / twisted / names / test / test_tap.py
1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
3
4 """
5 Tests for L{twisted.names.tap}.
6 """
7
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
15
16 class OptionsTests(TestCase):
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
38
39     def test_secondary(self):
40         """
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.
44         """
45         options = Options()
46         options.parseOptions(['--secondary', '1.2.3.4/example.com'])
47         self.assertEqual(
48             [(('1.2.3.4', PORT), ['example.com'])], options.secondaries)
49
50
51     def test_secondaryExplicitPort(self):
52         """
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.
55         """
56         options = Options()
57         options.parseOptions(['--secondary', '1.2.3.4:5353/example.com'])
58         self.assertEqual(
59             [(('1.2.3.4', 5353), ['example.com'])], options.secondaries)
60
61
62     def test_secondaryAuthorityServices(self):
63         """
64         After parsing I{--secondary} options, L{Options} constructs a
65         L{SecondaryAuthorityService} instance for each configured secondary.
66         """
67         options = Options()
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)
79
80
81     def test_recursiveConfiguration(self):
82         """
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
86         """
87         options = Options()
88         options.parseOptions(['--hosts-file', 'hosts.txt', '--recursive'])
89         ca, cl = _buildResolvers(options)
90
91         # Extra cleanup, necessary on POSIX because client.Resolver doesn't know
92         # when to stop parsing resolv.conf.  See #NNN for improving this.
93         for x in cl:
94             if isinstance(x, ResolverChain):
95                 recurser = x.resolvers[-1]
96                 if isinstance(recurser, Resolver):
97                     recurser._parseCall.cancel()
98
99         self.assertIsInstance(cl[-1], ResolverChain)