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