aboutsummaryrefslogtreecommitdiffstats
path: root/lib/python2.7/site-packages/Twisted-12.2.0-py2.7-linux-x86_64.egg/twisted/names/test/test_hosts.py
blob: d4cdb6984abcc3299825637e0190b279679a2598 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.

"""
Tests for the I{hosts(5)}-based resolver, L{twisted.names.hosts}.
"""

from twisted.trial.unittest import TestCase
from twisted.python.filepath import FilePath
from twisted.internet.defer import gatherResults

from twisted.names.dns import (
    A, AAAA, IN, DomainError, RRHeader, Query, Record_A, Record_AAAA)
from twisted.names.hosts import Resolver, searchFileFor, searchFileForAll


class SearchHostsFileTests(TestCase):
    """
    Tests for L{searchFileFor}, a helper which finds the first address for a
    particular hostname in a I{hosts(5)}-style file.
    """
    def test_findAddress(self):
        """
        If there is an IPv4 address for the hostname passed to
        L{searchFileFor}, it is returned.
        """
        hosts = FilePath(self.mktemp())
        hosts.setContent(
            "10.2.3.4 foo.example.com\n")
        self.assertEqual(
            "10.2.3.4", searchFileFor(hosts.path, "foo.example.com"))


    def test_notFoundAddress(self):
        """
        If there is no address information for the hostname passed to
        L{searchFileFor}, C{None} is returned.
        """
        hosts = FilePath(self.mktemp())
        hosts.setContent(
            "10.2.3.4 foo.example.com\n")
        self.assertIdentical(
            None, searchFileFor(hosts.path, "bar.example.com"))


    def test_firstAddress(self):
        """
        The first address associated with the given hostname is returned.
        """
        hosts = FilePath(self.mktemp())
        hosts.setContent(
            "::1 foo.example.com\n"
            "10.1.2.3 foo.example.com\n"
            "fe80::21b:fcff:feee:5a1d foo.example.com\n")
        self.assertEqual(
            "::1", searchFileFor(hosts.path, "foo.example.com"))


    def test_searchFileForAliases(self):
        """
        For a host with a canonical name and one or more aliases,
        L{searchFileFor} can find an address given any of the names.
        """
        hosts = FilePath(self.mktemp())
        hosts.setContent(
            "127.0.1.1	helmut.example.org	helmut\n"
            "# a comment\n"
            "::1     localhost ip6-localhost ip6-loopback\n")
        self.assertEqual(searchFileFor(hosts.path, 'helmut'), '127.0.1.1')
        self.assertEqual(
            searchFileFor(hosts.path, 'helmut.example.org'), '127.0.1.1')
        self.assertEqual(searchFileFor(hosts.path, 'ip6-localhost'), '::1')
        self.assertEqual(searchFileFor(hosts.path, 'ip6-loopback'), '::1')
        self.assertEqual(searchFileFor(hosts.path, 'localhost'), '::1')



class SearchHostsFileForAllTests(TestCase):
    """
    Tests for L{searchFileForAll}, a helper which finds all addresses for a
    particular hostname in a I{hosts(5)}-style file.
    """
    def test_allAddresses(self):
        """
        L{searchFileForAll} returns a list of all addresses associated with the
        name passed to it.
        """
        hosts = FilePath(self.mktemp())
        hosts.setContent(
            "127.0.0.1     foobar.example.com\n"
            "127.0.0.2     foobar.example.com\n"
            "::1           foobar.example.com\n")
        self.assertEqual(
            ["127.0.0.1", "127.0.0.2", "::1"],
            searchFileForAll(hosts, "foobar.example.com"))


    def test_caseInsensitively(self):
        """
        L{searchFileForAll} searches for names case-insensitively.
        """
        hosts = FilePath(self.mktemp())
        hosts.setContent("127.0.0.1     foobar.EXAMPLE.com\n")
        self.assertEqual(
            ["127.0.0.1"], searchFileForAll(hosts, "FOOBAR.example.com"))


    def test_readError(self):
        """
        If there is an error reading the contents of the hosts file,
        L{searchFileForAll} returns an empty list.
        """
        self.assertEqual(
            [], searchFileForAll(FilePath(self.mktemp()), "example.com"))



class HostsTestCase(TestCase):
    """
    Tests for the I{hosts(5)}-based L{twisted.names.hosts.Resolver}.
    """
    def setUp(self):
        f = open('EtcHosts', 'w')
        f.write('''
1.1.1.1    EXAMPLE EXAMPLE.EXAMPLETHING
::2        mixed
1.1.1.2    MIXED
::1        ip6thingy
1.1.1.3    multiple
1.1.1.4    multiple
::3        ip6-multiple
::4        ip6-multiple
''')
        f.close()
        self.ttl = 4200
        self.resolver = Resolver('EtcHosts', self.ttl)

    def testGetHostByName(self):
        data = [('EXAMPLE', '1.1.1.1'),
                ('EXAMPLE.EXAMPLETHING', '1.1.1.1'),
                ('MIXED', '1.1.1.2'),
                ]
        ds = [self.resolver.getHostByName(n).addCallback(self.assertEqual, ip)
              for n, ip in data]
        return gatherResults(ds)


    def test_lookupAddress(self):
        """
        L{hosts.Resolver.lookupAddress} returns a L{Deferred} which fires with A
        records from the hosts file.
        """
        d = self.resolver.lookupAddress('multiple')
        def resolved((results, authority, additional)):
            self.assertEqual(
                (RRHeader("multiple", A, IN, self.ttl,
                          Record_A("1.1.1.3", self.ttl)),
                 RRHeader("multiple", A, IN, self.ttl,
                          Record_A("1.1.1.4", self.ttl))),
                results)
        d.addCallback(resolved)
        return d


    def test_lookupIPV6Address(self):
        """
        L{hosts.Resolver.lookupIPV6Address} returns a L{Deferred} which fires
        with AAAA records from the hosts file.
        """
        d = self.resolver.lookupIPV6Address('ip6-multiple')
        def resolved((results, authority, additional)):
            self.assertEqual(
                (RRHeader("ip6-multiple", AAAA, IN, self.ttl,
                          Record_AAAA("::3", self.ttl)),
                 RRHeader("ip6-multiple", AAAA, IN, self.ttl,
                          Record_AAAA("::4", self.ttl))),
                results)
        d.addCallback(resolved)
        return d


    def test_lookupAllRecords(self):
        """
        L{hosts.Resolver.lookupAllRecords} returns a L{Deferred} which fires
        with A records from the hosts file.
        """
        d = self.resolver.lookupAllRecords('mixed')
        def resolved((results, authority, additional)):
            self.assertEqual(
                (RRHeader("mixed", A, IN, self.ttl,
                          Record_A("1.1.1.2", self.ttl)),),
                results)
        d.addCallback(resolved)
        return d


    def testNotImplemented(self):
        return self.assertFailure(self.resolver.lookupMailExchange('EXAMPLE'),
                                  NotImplementedError)

    def testQuery(self):
        d = self.resolver.query(Query('EXAMPLE'))
        d.addCallback(lambda x: self.assertEqual(x[0][0].payload.dottedQuad(),
                                                 '1.1.1.1'))
        return d

    def test_lookupAddressNotFound(self):
        """
        L{hosts.Resolver.lookupAddress} returns a L{Deferred} which fires with
        L{dns.DomainError} if the name passed in has no addresses in the hosts
        file.
        """
        return self.assertFailure(self.resolver.lookupAddress('foueoa'),
                                  DomainError)

    def test_lookupIPV6AddressNotFound(self):
        """
        Like L{test_lookupAddressNotFound}, but for
        L{hosts.Resolver.lookupIPV6Address}.
        """
        return self.assertFailure(self.resolver.lookupIPV6Address('foueoa'),
                                  DomainError)

    def test_lookupAllRecordsNotFound(self):
        """
        Like L{test_lookupAddressNotFound}, but for
        L{hosts.Resolver.lookupAllRecords}.
        """
        return self.assertFailure(self.resolver.lookupAllRecords('foueoa'),
                                  DomainError)