o
    ¯b³&  ã                   @   s¬   d Z ddlmZ ddlmZmZmZmZmZm	Z	m
Z
mZ ddlmZmZmZ ddlmZ ddlmZ G dd„ dƒZG d	d
„ d
eeƒZG dd„ deeƒZG dd„ deeƒZdS )zC
Tests for the I{hosts(5)}-based resolver, L{twisted.names.hosts}.
é    )ÚgatherResults)ÚAAAAÚINÚAÚDomainErrorÚQueryÚRecord_AÚRecord_AAAAÚRRHeader)ÚResolverÚsearchFileForÚsearchFileForAll)ÚFilePath)ÚSynchronousTestCasec                   @   s   e Zd Zdd„ ZdS )ÚGoodTempPathMixinc                 C   s   t |  ¡  d¡ƒS )Nzutf-8)r   ÚmktempÚencode©Úself© r   ú?/usr/lib/python3/dist-packages/twisted/names/test/test_hosts.pyÚpath   s   zGoodTempPathMixin.pathN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r      s    r   c                   @   ó0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚSearchHostsFileTestsz‡
    Tests for L{searchFileFor}, a helper which finds the first address for a
    particular hostname in a I{hosts(5)}-style file.
    c                 C   ó*   |   ¡ }| d¡ |  dt|j dƒ¡ dS )zr
        If there is an IPv4 address for the hostname passed to L{searchFileFor},
        it is returned.
        ó   10.2.3.4 foo.example.com
z10.2.3.4ó   foo.example.comN©r   Ú
setContentÚassertEqualr   ©r   Úhostsr   r   r   Útest_findAddress$   s   
z%SearchHostsFileTests.test_findAddressc                 C   s(   |   ¡ }| d¡ |  t|j dƒ¡ dS )z~
        If there is no address information for the hostname passed to
        L{searchFileFor}, L{None} is returned.
        r   s   bar.example.comN)r   r!   ÚassertIsNoner   r#   r   r   r   Útest_notFoundAddress-   s   
z)SearchHostsFileTests.test_notFoundAddressc                 C   r   )zS
        The first address associated with the given hostname is returned.
        sV   ::1 foo.example.com
10.1.2.3 foo.example.com
fe80::21b:fcff:feee:5a1d foo.example.com
ú::1r   Nr    r#   r   r   r   Útest_firstAddress6   s
   ÿz&SearchHostsFileTests.test_firstAddressc                 C   sz   |   ¡ }| d¡ |  t|j dƒd¡ |  t|j dƒd¡ |  t|j dƒd¡ |  t|j dƒd¡ |  t|j dƒd¡ d	S )
z�
        For a host with a canonical name and one or more aliases,
        L{searchFileFor} can find an address given any of the names.
        sY   127.0.1.1	helmut.example.org	helmut
# a comment
::1 localhost ip6-localhost ip6-loopback
s   helmutz	127.0.1.1s   helmut.example.orgs   ip6-localhostr(   s   ip6-loopbacks	   localhostNr    r#   r   r   r   Útest_searchFileForAliasesB   s   ÿz.SearchHostsFileTests.test_searchFileForAliasesN)r   r   r   Ú__doc__r%   r'   r)   r*   r   r   r   r   r      s    		r   c                   @   r   )ÚSearchHostsFileForAllTestsz†
    Tests for L{searchFileForAll}, a helper which finds all addresses for a
    particular hostname in a I{hosts(5)}-style file.
    c                 C   s,   |   ¡ }| d¡ |  g d¢t|dƒ¡ dS )zt
        L{searchFileForAll} returns a list of all addresses associated with the
        name passed to it.
        sc   127.0.0.1     foobar.example.com
127.0.0.2     foobar.example.com
::1           foobar.example.com
)ú	127.0.0.1z	127.0.0.2r(   s   foobar.example.comN©r   r!   r"   r   r#   r   r   r   Útest_allAddressesZ   s   ÿþz,SearchHostsFileForAllTests.test_allAddressesc                 C   s*   |   ¡ }| d¡ |  dgt|dƒ¡ dS )zL
        L{searchFileForAll} searches for names case-insensitively.
        s!   127.0.0.1     foobar.EXAMPLE.com
r-   s   FOOBAR.example.comNr.   r#   r   r   r   Útest_caseInsensitivelyj   s   
z1SearchHostsFileForAllTests.test_caseInsensitivelyc                 C   s   |   g t|  ¡ dƒ¡ dS )z�
        If there is an error reading the contents of the hosts file,
        L{searchFileForAll} returns an empty list.
        s   example.comN)r"   r   r   r   r   r   r   Útest_readErrorr   s   z)SearchHostsFileForAllTests.test_readErrorc                 C   s,   |   ¡ }| d¡ |  ddgt|dƒ¡ dS )zw
        L{searchFileForAll} ignores any malformed IP addresses associated with
        the name passed to it.
        s[   127.0.0.1	miser.example.org	miser
not-an-ip	miser
ÿnot-ascii	 miser
# miser
miser
::1 miserr-   r(   s   miserNr.   r#   r   r   r   Útest_malformedIPy   s   ÿþz+SearchHostsFileForAllTests.test_malformedIPN)r   r   r   r+   r/   r0   r1   r2   r   r   r   r   r,   T   s    r,   c                   @   sx   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )Ú
HostsTestszJ
    Tests for the I{hosts(5)}-based L{twisted.names.hosts.Resolver}.
    c                 C   s,   |   ¡ }| d¡ d| _t|j | jƒ| _d S )Ns  
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
not-an-ip  malformed
malformed
# malformed
1.1.1.5    malformed
::5        malformed
ih  )r   r!   Úttlr   Úresolver)r   Úfr   r   r   ÚsetUp’   s   ÿzHostsTests.setUpc                 C   s   t ƒ }|  d|j¡ dS )z‘
        The default hosts file used by L{Resolver} is I{/etc/hosts} if no value
        is given for the C{file} initializer parameter.
        s
   /etc/hostsN)r   r"   Úfile)r   r5   r   r   r   Útest_defaultPath¨   s   zHostsTests.test_defaultPathc                    s,   g d¢}‡ fdd„|D ƒ}ˆ   t|ƒ¡ dS )z¿
        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.
        ))ó   EXAMPLEú1.1.1.1)s   EXAMPLE.EXAMPLETHINGr;   )s   MIXEDú1.1.1.2c                    s&   g | ]\}}ˆ j  |¡ ˆ j|¡‘qS r   )r5   ÚgetHostByNameÚaddCallbackr"   )Ú.0ÚnÚipr   r   r   Ú
<listcomp>»   s    ÿÿz1HostsTests.test_getHostByName.<locals>.<listcomp>N)ÚsuccessResultOfr   )r   ÚdataÚdsr   r   r   Útest_getHostByName°   s
   
þzHostsTests.test_getHostByNamec                 C   ó\   | j  d¡}|  |¡\}}}|  tdtt| jtd| jƒƒtdtt| jtd| jƒƒf|¡ dS )z
        L{hosts.Resolver.lookupAddress} returns a L{Deferred} which fires with A
        records from the hosts file.
        s   multiplez1.1.1.3z1.1.1.4N©	r5   ÚlookupAddressrC   r"   r
   r   r   r4   r   ©r   ÚdÚanswersÚ	authorityÚ
additionalr   r   r   Útest_lookupAddressÁ   s   þûzHostsTests.test_lookupAddressc                 C   rG   )z†
        L{hosts.Resolver.lookupIPV6Address} returns a L{Deferred} which fires
        with AAAA records from the hosts file.
        s   ip6-multiplez::3z::4N©	r5   ÚlookupIPV6AddressrC   r"   r
   r   r   r4   r	   rJ   r   r   r   Útest_lookupIPV6AddressÐ   s   ÿÿü÷z!HostsTests.test_lookupIPV6Addressc              
   C   sD   | j  d¡}|  |¡\}}}|  tdtt| jtd| jƒƒf|¡ dS )z‚
        L{hosts.Resolver.lookupAllRecords} returns a L{Deferred} which fires
        with A records from the hosts file.
        s   mixedr<   N)	r5   ÚlookupAllRecordsrC   r"   r
   r   r   r4   r   rJ   r   r   r   Útest_lookupAllRecordsã   s   þz HostsTests.test_lookupAllRecordsc                 C   ó   |   | j d¡t¡ dS )z|
        L{hosts.Resolver} fails with L{NotImplementedError} for L{IResolver}
        methods it doesn't implement.
        r:   N)ÚfailureResultOfr5   ÚlookupMailExchangeÚNotImplementedErrorr   r   r   r   Útest_notImplementedï   s   ÿzHostsTests.test_notImplementedc                 C   s8   | j  tdƒ¡}|  |¡\\}}}|  |j ¡ d¡ d S )Nr:   r;   )r5   Úqueryr   rC   r"   ÚpayloadÚ
dottedQuad©r   rK   ÚanswerrM   rN   r   r   r   Ú
test_queryø   s   zHostsTests.test_queryc                 C   rU   )zµ
        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.
        ó   foueoaN)rV   r5   rI   r   r   r   r   r   Útest_lookupAddressNotFoundý   s   z%HostsTests.test_lookupAddressNotFoundc                 C   rU   )zj
        Like L{test_lookupAddressNotFound}, but for
        L{hosts.Resolver.lookupIPV6Address}.
        r`   N)rV   r5   rQ   r   r   r   r   r   Útest_lookupIPV6AddressNotFound  ó   z)HostsTests.test_lookupIPV6AddressNotFoundc                 C   rU   )zi
        Like L{test_lookupAddressNotFound}, but for
        L{hosts.Resolver.lookupAllRecords}.
        r`   N)rV   r5   rS   r   r   r   r   r   Útest_lookupAllRecordsNotFound  rc   z(HostsTests.test_lookupAllRecordsNotFoundc              
   C   óD   | j  d¡}|  |¡\\}}}|  tdtt| jtd| jƒƒ|¡ dS )zÆ
        L{hosts.Resolver.lookupAddress} returns a L{Deferred} which fires with
        the valid addresses from the hosts file, ignoring any entries that
        aren't valid IP addresses.
        ó	   malformedz1.1.1.5NrH   r]   r   r   r   Útest_lookupMalformed  s   þzHostsTests.test_lookupMalformedc              
   C   re   )zk
        Like L{test_lookupAddressMalformed}, but for
        L{hosts.Resolver.lookupIPV6Address}.
        rf   z::5NrP   r]   r   r   r   Útest_lookupIPV6Malformed   s   þz#HostsTests.test_lookupIPV6MalformedN)r   r   r   r+   r7   r9   rF   rO   rR   rT   rY   r_   ra   rb   rd   rg   rh   r   r   r   r   r3   �   s    	r3   N)r+   Útwisted.internet.deferr   Útwisted.names.dnsr   r   r   r   r   r   r	   r
   Útwisted.names.hostsr   r   r   Útwisted.python.filepathr   Útwisted.trial.unittestr   r   r   r,   r3   r   r   r   r   Ú<module>   s   (
69