U
    ª€lj(  ã                   @  sÔ   d Z ddlmZ ddlm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
eƒ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}.
é    )Úannotations)ÚProtocol)ÚgatherResults)ÚAAAAÚINÚAÚDomainErrorÚQueryÚRecord_AÚRecord_AAAAÚRRHeader)ÚResolverÚsearchFileForÚsearchFileForAll)ÚFilePath)ÚSynchronousTestCasec                   @  s   e Zd Zddœdd„ZdS )Ú_SupportsMktempÚstr©Úreturnc                 C  s   d S )N© ©Úselfr   r   úA/tmp/pip-unpacked-wheel-aghyhbke/twisted/names/test/test_hosts.pyÚmktemp   s    z_SupportsMktemp.mktempN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r      s   r   c                   @  s   e Zd Zdddœdd„ZdS )ÚGoodTempPathMixinr   zFilePath[bytes])r   r   c                 C  s   t |  ¡  d¡ƒS )Nzutf-8)r   r   Úencoder   r   r   r   Úpath!   s    zGoodTempPathMixin.pathN)r   r   r   r    r   r   r   r   r       s   r   c                   @  sH   e Zd ZdZddœdd„Zddœdd„Zddœdd	„Zddœ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.
    ÚNoner   c                 C  s*   |   ¡ }| 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&   ZassertIsNoner   r(   r   r   r   Útest_notFoundAddress4   s    
z)SearchHostsFileTests.test_notFoundAddressc                 C  s*   |   ¡ }| d¡ |  dt|j dƒ¡ dS )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_firstAddress=   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_searchFileForAliasesI   s    ÿz.SearchHostsFileTests.test_searchFileForAliasesN)r   r   r   Ú__doc__r*   r+   r-   r.   r   r   r   r   r!   %   s
   		r!   c                   @  sH   e Zd ZdZddœdd„Zddœdd„Zddœdd	„Zddœd
d„ZdS )ÚSearchHostsFileForAllTestsz†
    Tests for L{searchFileForAll}, a helper which finds all addresses for a
    particular hostname in a I{hosts(5)}-style file.
    r"   r   c                 C  s.   |   ¡ }| d¡ |  dddg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_allAddressesa   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
r1   s   FOOBAR.example.comNr2   r(   r   r   r   Útest_caseInsensitivelyq   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_readErrory   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 miserr1   r,   s   miserNr2   r(   r   r   r   Útest_malformedIP€   s    ÿþz+SearchHostsFileForAllTests.test_malformedIPN)r   r   r   r/   r3   r4   r5   r6   r   r   r   r   r0   [   s
   r0   c                   @  sÆ   e Zd ZdZddœdd„Zddœdd„Zddœdd	„Zddœd
d„Zddœdd„Zddœdd„Z	ddœdd„Z
ddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœdd„ZdS )Ú
HostsTestszJ
    Tests for the I{hosts(5)}-based L{twisted.names.hosts.Resolver}.
    r"   r   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   r9   r   r   r   Útest_defaultPath¯   s    zHostsTests.test_defaultPathc                   s.   dddg}‡ 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   )r9   ZgetHostByNameZaddCallbackr'   )Ú.0ÚnÚipr   r   r   Ú
<listcomp>Â   s   ÿz1HostsTests.test_getHostByName.<locals>.<listcomp>N)ÚsuccessResultOfr   )r   ÚdataZdsr   r   r   Útest_getHostByName·   s    ý
þzHostsTests.test_getHostByNamec                 C  s\   | 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©	r9   ÚlookupAddressrE   r'   r   r   r   r8   r
   ©r   ÚdZanswersÚ	authorityÚ
additionalr   r   r   Útest_lookupAddressÈ   s    þûzHostsTests.test_lookupAddressc                 C  s\   | 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.lookupIPV6Address} returns a L{Deferred} which fires
        with AAAA records from the hosts file.
        s   ip6-multiplez::3z::4N©	r9   ÚlookupIPV6AddressrE   r'   r   r   r   r8   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)	r9   ÚlookupAllRecordsrE   r'   r   r   r   r8   r
   rJ   r   r   r   Útest_lookupAllRecordsê   s    þz HostsTests.test_lookupAllRecordsc                 C  s   |   | j d¡t¡ dS )z|
        L{hosts.Resolver} fails with L{NotImplementedError} for L{IResolver}
        methods it doesn't implement.
        r>   N)ÚfailureResultOfr9   ZlookupMailExchangeÚNotImplementedErrorr   r   r   r   Útest_notImplementedö   s    
 ÿzHostsTests.test_notImplementedc                 C  s8   | j  tdƒ¡}|  |¡\\}}}|  |j ¡ d¡ d S )Nr>   r?   )r9   Úqueryr	   rE   r'   ÚpayloadZ
dottedQuad©r   rK   ZanswerrL   rM   r   r   r   Ú
test_queryÿ   s    zHostsTests.test_queryc                 C  s   |   | j d¡t¡ dS )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)rT   r9   rI   r   r   r   r   r   Útest_lookupAddressNotFound  s    z%HostsTests.test_lookupAddressNotFoundc                 C  s   |   | j d¡t¡ dS )zj
        Like L{test_lookupAddressNotFound}, but for
        L{hosts.Resolver.lookupIPV6Address}.
        r[   N)rT   r9   rP   r   r   r   r   r   Útest_lookupIPV6AddressNotFound  s    z)HostsTests.test_lookupIPV6AddressNotFoundc                 C  s   |   | j d¡t¡ dS )zi
        Like L{test_lookupAddressNotFound}, but for
        L{hosts.Resolver.lookupAllRecords}.
        r[   N)rT   r9   rR   r   r   r   r   r   Útest_lookupAllRecordsNotFound  s    z(HostsTests.test_lookupAllRecordsNotFoundc              
   C  sD   | 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   rY   r   r   r   Útest_lookupMalformed  s    þzHostsTests.test_lookupMalformedc              
   C  sD   | j  d¡}|  |¡\\}}}|  tdtt| jtd| jƒƒ|¡ dS )zk
        Like L{test_lookupAddressMalformed}, but for
        L{hosts.Resolver.lookupIPV6Address}.
        r_   z::5NrO   rY   r   r   r   Útest_lookupIPV6Malformed'  s    þz#HostsTests.test_lookupIPV6MalformedN)r   r   r   r/   r;   r=   rG   rN   rQ   rS   rV   rZ   r\   r]   r^   r`   ra   r   r   r   r   r7   ”   s   	r7   N)r/   Ú
__future__r   Ztyping_extensionsr   Ztwisted.internet.deferr   Ztwisted.names.dnsr   r   r   r   r	   r
   r   r   Ztwisted.names.hostsr   r   r   Ztwisted.python.filepathr   Ztwisted.trial.unittestr   r   r   r!   r0   r7   r   r   r   r   Ú<module>   s   (
69