o
    Óó·i(  ã                   @  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„ZdS )Ú_SupportsMktempÚreturnÚstrc                 C  s   d S )N© ©Úselfr   r   úcC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\twisted/names/test/test_hosts.pyÚmktemp   s   z_SupportsMktemp.mktempN)r   r   )Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r      ó    r   c                   @  s   e Zd Zddd„ZdS )	ÚGoodTempPathMixinr   r   r   úFilePath[bytes]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   r   r   r   r       r   r   c                   @  ó8   e Zd ZdZddd„Zddd„Zddd	„Z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.
    r   ÚNonec                 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)   ZassertIsNoner   r+   r   r   r   Útest_notFoundAddress4   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_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   r   r   Ú__doc__r-   r.   r0   r1   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.
    r   r$   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_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
r5   s   FOOBAR.example.comNr6   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 miserr5   r/   s   miserNr6   r+   r   r   r   Útest_malformedIP€   s   ÿþz+SearchHostsFileForAllTests.test_malformedIPNr2   )r   r   r   r3   r7   r8   r9   r:   r   r   r   r   r4   [   s    


r4   c                   @  s’   e Zd ZdZddd„Zddd„Zddd	„Zdd
d„Zddd„Zddd„Z	ddd„Z
ddd„Zddd„Zddd„Zddd„Zddd„Z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   r=   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.EXAMPLETHINGrC   )s   MIXEDú1.1.1.2c                   s&   g | ]\}}ˆ j  |¡ ˆ j|¡‘qS r   )r=   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  ó\   | 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©	r=   ÚlookupAddressrI   r*   r   r   r   r<   r
   ©r   ÚdZanswersÚ	authorityÚ
additionalr   r   r   Útest_lookupAddressÈ   s   þûzHostsTests.test_lookupAddressc                 C  rL   )z†
        L{hosts.Resolver.lookupIPV6Address} returns a L{Deferred} which fires
        with AAAA records from the hosts file.
        s   ip6-multiplez::3z::4N©	r=   ÚlookupIPV6AddressrI   r*   r   r   r   r<   r   rO   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   mixedrD   N)	r=   ÚlookupAllRecordsrI   r*   r   r   r   r<   r
   rO   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.
        rB   N)ÚfailureResultOfr=   ZlookupMailExchangeÚNotImplementedErrorr   r   r   r   Útest_notImplementedö   s   ÿzHostsTests.test_notImplementedc                 C  s8   | j  tdƒ¡}|  |¡\\}}}|  |j ¡ d¡ d S )NrB   rC   )r=   Úqueryr	   rI   r*   ÚpayloadZ
dottedQuad©r   rP   ZanswerrQ   rR   r   r   r   Ú
test_queryÿ   s   zHostsTests.test_queryc                 C  rY   )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)rZ   r=   rN   r   r   r   r   r   Útest_lookupAddressNotFound  s   z%HostsTests.test_lookupAddressNotFoundc                 C  rY   )zj
        Like L{test_lookupAddressNotFound}, but for
        L{hosts.Resolver.lookupIPV6Address}.
        ra   N)rZ   r=   rU   r   r   r   r   r   Útest_lookupIPV6AddressNotFound  ó   z)HostsTests.test_lookupIPV6AddressNotFoundc                 C  rY   )zi
        Like L{test_lookupAddressNotFound}, but for
        L{hosts.Resolver.lookupAllRecords}.
        ra   N)rZ   r=   rW   r   r   r   r   r   Útest_lookupAllRecordsNotFound  rd   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.5NrM   r_   r   r   r   Útest_lookupMalformed  s   þzHostsTests.test_lookupMalformedc              
   C  rf   )zk
        Like L{test_lookupAddressMalformed}, but for
        L{hosts.Resolver.lookupIPV6Address}.
        rg   z::5NrT   r_   r   r   r   Útest_lookupIPV6Malformed'  s   þz#HostsTests.test_lookupIPV6MalformedNr2   )r   r   r   r3   r?   rA   rK   rS   rV   rX   r\   r`   rb   rc   re   rh   ri   r   r   r   r   r;   ”   s    







	



r;   N)r3   Ú
__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#   r4   r;   r   r   r   r   Ú<module>   s   (
69