This file is indexed.

/usr/lib/python2.7/dist-packages/twisted/names/test/test_hosts.py is in python-twisted-names 14.0.2-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.

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

from __future__ import division, absolute_import

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 GoodTempPathMixin(object):
    def path(self):
        return FilePath(self.mktemp().encode('utf-8'))



class SearchHostsFileTests(TestCase, GoodTempPathMixin):
    """
    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 = self.path()
        hosts.setContent(
            b"10.2.3.4 foo.example.com\n")
        self.assertEqual(
            "10.2.3.4", searchFileFor(hosts.path, b"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 = self.path()
        hosts.setContent(
            b"10.2.3.4 foo.example.com\n")
        self.assertIs(None, searchFileFor(hosts.path, b"bar.example.com"))


    def test_firstAddress(self):
        """
        The first address associated with the given hostname is returned.
        """
        hosts = self.path()
        hosts.setContent(
            b"::1 foo.example.com\n"
            b"10.1.2.3 foo.example.com\n"
            b"fe80::21b:fcff:feee:5a1d foo.example.com\n")
        self.assertEqual("::1", searchFileFor(hosts.path, b"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 = self.path()
        hosts.setContent(
            b"127.0.1.1\thelmut.example.org\thelmut\n"
            b"# a comment\n"
            b"::1 localhost ip6-localhost ip6-loopback\n")
        self.assertEqual(searchFileFor(hosts.path, b'helmut'), '127.0.1.1')
        self.assertEqual(
            searchFileFor(hosts.path, b'helmut.example.org'), '127.0.1.1')
        self.assertEqual(searchFileFor(hosts.path, b'ip6-localhost'), '::1')
        self.assertEqual(searchFileFor(hosts.path, b'ip6-loopback'), '::1')
        self.assertEqual(searchFileFor(hosts.path, b'localhost'), '::1')



class SearchHostsFileForAllTests(TestCase, GoodTempPathMixin):
    """
    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 = self.path()
        hosts.setContent(
            b"127.0.0.1     foobar.example.com\n"
            b"127.0.0.2     foobar.example.com\n"
            b"::1           foobar.example.com\n")
        self.assertEqual(
            ["127.0.0.1", "127.0.0.2", "::1"],
            searchFileForAll(hosts, b"foobar.example.com"))


    def test_caseInsensitively(self):
        """
        L{searchFileForAll} searches for names case-insensitively.
        """
        hosts = self.path()
        hosts.setContent(b"127.0.0.1     foobar.EXAMPLE.com\n")
        self.assertEqual(
            ["127.0.0.1"], searchFileForAll(hosts, b"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(self.path(), b"example.com"))



class HostsTestCase(TestCase, GoodTempPathMixin):
    """
    Tests for the I{hosts(5)}-based L{twisted.names.hosts.Resolver}.
    """
    def setUp(self):
        f = self.path()
        f.setContent(b'''
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
''')
        self.ttl = 4200
        self.resolver = Resolver(f.path, self.ttl)


    def test_defaultPath(self):
        """
        The default hosts file used by L{Resolver} is I{/etc/hosts} if no value
        is given for the C{file} initializer parameter.
        """
        resolver = Resolver()
        self.assertEqual(b"/etc/hosts", resolver.file)


    def test_getHostByName(self):
        """
        L{hosts.Resolver.getHostByName} returns a L{Deferred} which fires with a
        string giving the address of the queried name as found in the resolver's
        hosts file.
        """
        data = [(b'EXAMPLE', '1.1.1.1'),
                (b'EXAMPLE.EXAMPLETHING', '1.1.1.1'),
                (b'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(b'multiple')
        def resolved(results):
            answers, authority, additional = results
            self.assertEqual(
                (RRHeader(b"multiple", A, IN, self.ttl,
                          Record_A("1.1.1.3", self.ttl)),
                 RRHeader(b"multiple", A, IN, self.ttl,
                          Record_A("1.1.1.4", self.ttl))),
                answers)
        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(b'ip6-multiple')
        def resolved(results):
            answers, authority, additional = results
            self.assertEqual(
                (RRHeader(b"ip6-multiple", AAAA, IN, self.ttl,
                          Record_AAAA("::3", self.ttl)),
                 RRHeader(b"ip6-multiple", AAAA, IN, self.ttl,
                          Record_AAAA("::4", self.ttl))),
                answers)
        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(b'mixed')
        def resolved(results):
            answers, authority, additional = results
            self.assertEqual(
                (RRHeader(b"mixed", A, IN, self.ttl,
                          Record_A("1.1.1.2", self.ttl)),),
                answers)
        d.addCallback(resolved)
        return d


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


    def testQuery(self):
        d = self.resolver.query(Query(b'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(b'foueoa'),
                                  DomainError)


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


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