U
    
W[c!                     @   s   d Z ddlmZm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 G dd	 d	e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}.
    )divisionabsolute_import)TestCase)FilePathgatherResults)AAAAAINDomainErrorRRHeaderQueryRecord_ARecord_AAAA)ResolversearchFileForsearchFileForAllc                   @   s   e Zd Zdd ZdS )GoodTempPathMixinc                 C   s   t |  dS )Nzutf-8)r   Z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                   @   s0   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   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   Z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_notFoundAddress*   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_firstAddress5   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_searchFileForAliasesA   s    
 z.SearchHostsFileTests.test_searchFileForAliasesN)r   r   r   __doc__r$   r%   r'   r(   r   r   r   r   r      s
   r   c                   @   s(   e Zd ZdZdd Zdd Z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.
    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_allAddressesY   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_caseInsensitivelyh   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_readErrorN)r   r   r   r)   r-   r.   r/   r   r   r   r   r*   T   s   
r*   c                   @   sh   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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
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   r2   r   r   r   test_defaultPath   s    zHostsTests.test_defaultPathc                    s$   dddg} fdd|D }t |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.EXAMPLETHINGr8   )s   MIXED1.1.1.2c                    s&   g | ]\}} j | j|qS r   )r2   ZgetHostByNameaddCallbackr"   ).0nZipr   r   r   
<listcomp>   s   z1HostsTests.test_getHostByName.<locals>.<listcomp>r   )r   dataZdsr   r   r   test_getHostByName   s    
zHostsTests.test_getHostByNamec                    s&    j d} fdd}|| |S )z
        L{hosts.Resolver.lookupAddress} returns a L{Deferred} which fires with A
        records from the hosts file.
           multiplec                    sJ   | \}}}  tdtt jtd jtdtt jtd jf| d S )Nr@   z1.1.1.3z1.1.1.4r"   r   r   r
   r1   r   ZresultsZanswersZ	authorityZ
additionalr   r   r   resolved   s    


z/HostsTests.test_lookupAddress.<locals>.resolved)r2   lookupAddressr:   r   drC   r   r   r   test_lookupAddress   s    
zHostsTests.test_lookupAddressc                    s&    j d} fdd}|| |S )z
        L{hosts.Resolver.lookupIPV6Address} returns a L{Deferred} which fires
        with AAAA records from the hosts file.
           ip6-multiplec                    sJ   | \}}}  tdtt jtd jtdtt jtd jf| d S )NrH   z::3z::4)r"   r   r	   r
   r1   r   rB   r   r   r   rC      s    


z3HostsTests.test_lookupIPV6Address.<locals>.resolved)r2   lookupIPV6Addressr:   rE   r   r   r   test_lookupIPV6Address   s    
z!HostsTests.test_lookupIPV6Addressc                    s&    j d} fdd}|| |S )z
        L{hosts.Resolver.lookupAllRecords} returns a L{Deferred} which fires
        with A records from the hosts file.
           mixedc              
      s2   | \}}}  tdtt jtd jf| d S )NrK   r9   rA   rB   r   r   r   rC      s    

z2HostsTests.test_lookupAllRecords.<locals>.resolved)r2   lookupAllRecordsr:   rE   r   r   r   test_lookupAllRecords   s    
z HostsTests.test_lookupAllRecordsc                 C   s   |  | jdtS )Nr7   )assertFailurer2   ZlookupMailExchangeNotImplementedErrorr   r   r   r   test_notImplemented   s    zHostsTests.test_notImplementedc                    s&    j td}| fdd |S )Nr7   c                    s     | d d j dS )Nr   r8   )r"   ZpayloadZ
dottedQuad)xr   r   r   <lambda>   s   z'HostsTests.test_query.<locals>.<lambda>)r2   Zqueryr   r:   )r   rF   r   r   r   
test_query   s    zHostsTests.test_queryc                 C   s   |  | jdt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.
           foueoa)rN   r2   rD   r   r   r   r   r   test_lookupAddressNotFound   s    z%HostsTests.test_lookupAddressNotFoundc                 C   s   |  | jdtS )zj
        Like L{test_lookupAddressNotFound}, but for
        L{hosts.Resolver.lookupIPV6Address}.
        rT   )rN   r2   rI   r   r   r   r   r   test_lookupIPV6AddressNotFound   s    z)HostsTests.test_lookupIPV6AddressNotFoundc                 C   s   |  | jdtS )zi
        Like L{test_lookupAddressNotFound}, but for
        L{hosts.Resolver.lookupAllRecords}.
        rT   )rN   r2   rL   r   r   r   r   r   test_lookupAllRecordsNotFound   s    z(HostsTests.test_lookupAllRecordsNotFoundN)r   r   r   r)   r4   r6   r?   rG   rJ   rM   rP   rS   rU   rV   rW   r   r   r   r   r0   |   s   	
	r0   N)r)   Z
__future__r   r   Ztwisted.trial.unittestr   Ztwisted.python.filepathr   Ztwisted.internet.deferr   Ztwisted.names.dnsr   r	   r
   r   r   r   r   r   Ztwisted.names.hostsr   r   r   objectr   r   r*   r0   r   r   r   r   <module>   s   (;(