Ë
    oj
(  ã                  óò   — 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
e«      Z G d„ d«      Z G d„ dee«      Z G d„ dee«      Z G d„ dee«      Zy)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                  ó   — e Zd Zdd„Zy)Ú_SupportsMktempc                 ó   — y )N© ©Úselfs    ú?/usr/lib/python3/dist-packages/twisted/names/test/test_hosts.pyÚmktempz_SupportsMktemp.mktemp   s   € Øó    N)ÚreturnÚstr)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r      s   „ ôr   r   c                  ó   — e Zd Zdd„Zy)ÚGoodTempPathMixinc                óR   — t        | j                  «       j                  d«      «      S )Nzutf-8)r   r   Úencoder   s    r   ÚpathzGoodTempPathMixin.path!   s   € Ü˜Ÿ™›×,Ñ,¨WÓ5Ó6Ð6r   N)r   r   r   zFilePath[bytes])r   r   r    r%   r   r   r   r"   r"       s   „ ô7r   r"   c                  ó0   — e Zd ZdZdd„Zdd„Zdd„Zdd„Zy)ÚSearchHostsFileTestsz‡
    Tests for L{searchFileFor}, a helper which finds the first address for a
    particular hostname in a I{hosts(5)}-style file.
    c                ó’   — | j                  «       }|j                  d«       | j                  dt        |j                   d«      «       y)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   Úhostss     r   Útest_findAddressz%SearchHostsFileTests.test_findAddress+   s:   € ð
 —	‘	“ˆØ×ÑÐ6Ô7Ø×Ñ˜¤]°5·:±:Ð?QÓ%RÕSr   c                ó�   — | j                  «       }|j                  d«       | j                  t        |j                   d«      «       y)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.   s     r   Útest_notFoundAddressz)SearchHostsFileTests.test_notFoundAddress4   s8   € ð
 —	‘	“ˆØ×ÑÐ6Ô7Ø×Ñœ-¨¯
©
Ð4FÓGÕHr   c                ó’   — | j                  «       }|j                  d«       | j                  dt        |j                   d«      «       y)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.   s     r   Útest_firstAddressz&SearchHostsFileTests.test_firstAddress=   s@   € ð —	‘	“ˆØ×Ñð:ô	
ð
 	×Ñ˜¤¨e¯j©jÐ:LÓ MÕNr   c                óÂ  — | j                  «       }|j                  d«       | j                  t        |j                   d«      d«       | j                  t        |j                   d«      d«       | j                  t        |j                   d«      d«       | j                  t        |j                   d«      d«       | j                  t        |j                   d«      d«       y	)
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-localhostr5   s   ip6-loopbacks	   localhostNr+   r.   s     r   Útest_searchFileForAliasesz.SearchHostsFileTests.test_searchFileForAliasesI   s­   € ð
 —	‘	“ˆØ×Ñð:ô	
ð
 	×Ñœ u§z¡z°9Ó=¸{ÔKØ×Ñœ u§z¡zÐ3HÓIÈ;ÔWØ×Ñœ u§z¡zÐ3CÓDÀeÔLØ×Ñœ u§z¡z°?ÓCÀUÔKØ×Ñœ u§z¡z°<Ó@À%ÕHr   N©r   ÚNone)r   r   r    Ú__doc__r0   r3   r6   r8   r   r   r   r'   r'   %   s   „ ñó
TóIó
OôIr   r'   c                  ó0   — e Zd ZdZdd„Zdd„Zdd„Zdd„Zy)ÚSearchHostsFileForAllTestsz†
    Tests for L{searchFileForAll}, a helper which finds all addresses for a
    particular hostname in a I{hosts(5)}-style file.
    c                ó‚   — | j                  «       }|j                  d«       | j                  g d¢t        |d«      «       y)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.2r5   s   foobar.example.comN©r%   r,   r-   r   r.   s     r   Útest_allAddressesz,SearchHostsFileForAllTests.test_allAddressesa   s@   € ð
 —	‘	“ˆØ×Ñð2ô	
ð
 	×ÑÚ-Ü˜UÐ$9Ó:õ	
r   c                ó€   — | j                  «       }|j                  d«       | j                  dgt        |d«      «       y)zL
        L{searchFileForAll} searches for names case-insensitively.
        s!   127.0.0.1     foobar.EXAMPLE.com
r?   s   FOOBAR.example.comNr@   r.   s     r   Útest_caseInsensitivelyz1SearchHostsFileForAllTests.test_caseInsensitivelyq   s9   € ð —	‘	“ˆØ×ÑÐ>Ô?Ø×Ñ˜+˜Ô(8¸Ð@UÓ(VÕWr   c                óX   — | j                  g t        | j                  «       d«      «       y)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   s    r   Útest_readErrorz)SearchHostsFileForAllTests.test_readErrory   s"   € ð
 	×Ñ˜Ô-¨d¯i©i«k¸>ÓJÕKr   c                ó‚   — | j                  «       }|j                  d«       | j                  ddgt        |d«      «       y)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?   r5   s   miserNr@   r.   s     r   Útest_malformedIPz+SearchHostsFileForAllTests.test_malformedIP€   sD   € ð
 —	‘	“ˆØ×Ñðô	
ð 	×ÑØ˜%Ð Ü˜U HÓ-õ	
r   Nr9   )r   r   r    r;   rA   rC   rE   rG   r   r   r   r=   r=   [   s   „ ñó

ó XóLô
r   r=   c                  óx   — 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y)Ú
HostsTestszJ
    Tests for the I{hosts(5)}-based L{twisted.names.hosts.Resolver}.
    c                óž   — | j                  «       }|j                  d«       d| _        t        |j                   | j                  «      | _        y )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   Úfs     r   ÚsetUpzHostsTests.setUp™   s=   € Ø�I‰I‹KˆØ	�‰ðô	
ð" ˆŒÜ  §¡¨¯©Ó2ˆ�r   c                óP   — t        «       }| j                  d|j                  «       y)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   rL   s     r   Útest_defaultPathzHostsTests.test_defaultPath¯   s   € ô
 “:ˆØ×Ñ˜¨¯©Õ6r   c                óÚ   — g d¢}|D ��cg c]:  \  }}| j                   j                  |«      j                  | j                  |«      ‘Œ< }}}| j	                  t        |«      «       yc c}}w )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.EXAMPLETHINGrT   )s   MIXEDú1.1.1.2N)rL   ÚgetHostByNameÚaddCallbackr-   ÚsuccessResultOfr   )r   ÚdataÚnÚipÚdss        r   Útest_getHostByNamezHostsTests.test_getHostByName·   sh   € ò
ˆð ÷
á��2ð �M‰M×'Ñ'¨Ó*×6Ñ6°t×7GÑ7GÈÕLð
ˆñ 
ð 	×Ñœ]¨2Ó.Õ/ùó	
s   Š?A'c                óX  — | j                   j                  d«      }| j                  |«      \  }}}| j                  t	        dt
        t        | j                  t        d| j                  «      «      t	        dt
        t        | j                  t        d| j                  «      «      f|«       y)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©	rL   ÚlookupAddressrX   r-   r   r   r   rK   r   ©r   ÚdÚanswersÚ	authorityÚ
additionals        r   Útest_lookupAddresszHostsTests.test_lookupAddressÈ   s…   € ð
 �M‰M×'Ñ'¨Ó4ˆØ)-×)=Ñ)=¸aÓ)@Ñ&ˆ�˜JØ×Ñä˜¤a¬¨T¯X©X´xÀ	È4Ï8É8Ó7TÓUÜ˜¤a¬¨T¯X©X´xÀ	È4Ï8É8Ó7TÓUðð õ	
r   c                óX  — | j                   j                  d«      }| j                  |«      \  }}}| j                  t	        dt
        t        | j                  t        d| j                  «      «      t	        dt
        t        | j                  t        d| j                  «      «      f|«       y)z†
        L{hosts.Resolver.lookupIPV6Address} returns a L{Deferred} which fires
        with AAAA records from the hosts file.
        s   ip6-multiplez::3z::4N©	rL   ÚlookupIPV6AddressrX   r-   r   r   r   rK   r   ra   s        r   Útest_lookupIPV6Addressz!HostsTests.test_lookupIPV6Address×   s�   € ð
 �M‰M×+Ñ+¨OÓ<ˆØ)-×)=Ñ)=¸aÓ)@Ñ&ˆ�˜JØ×ÑäØ#¤T¬2¨t¯x©x¼ÀUÈDÏHÉHÓ9Uóô Ø#¤T¬2¨t¯x©x¼ÀUÈDÏHÉHÓ9Uóð	ð õ
	
r   c                óð   — | j                   j                  d«      }| j                  |«      \  }}}| j                  t	        dt
        t        | j                  t        d| j                  «      «      f|«       y)z‚
        L{hosts.Resolver.lookupAllRecords} returns a L{Deferred} which fires
        with A records from the hosts file.
        s   mixedrU   N)	rL   ÚlookupAllRecordsrX   r-   r   r   r   rK   r   ra   s        r   Útest_lookupAllRecordsz HostsTests.test_lookupAllRecordsê   sb   € ð
 �M‰M×*Ñ*¨8Ó4ˆØ)-×)=Ñ)=¸aÓ)@Ñ&ˆ�˜JØ×ÑÜ�h¤¤2 t§x¡x´¸)ÀTÇXÁXÓ1NÓOÐQØõ	
r   c                ób   — | j                  | j                  j                  d«      t        «       y)z|
        L{hosts.Resolver} fails with L{NotImplementedError} for L{IResolver}
        methods it doesn't implement.
        rS   N)ÚfailureResultOfrL   ÚlookupMailExchangeÚNotImplementedErrorr   s    r   Útest_notImplementedzHostsTests.test_notImplementedö   s'   € ð
 	×ÑØ�M‰M×,Ñ,¨ZÓ8Ô:Mõ	
r   c                óÎ   — | j                   j                  t        d«      «      }| j                  |«      \  \  }}}| j	                  |j
                  j                  «       d«       y )NrS   rT   )rL   Úqueryr
   rX   r-   ÚpayloadÚ
dottedQuad©r   rb   Úanswerrd   re   s        r   Ú
test_queryzHostsTests.test_queryÿ   sQ   € Ø�M‰M×Ñ¤ jÓ 1Ó2ˆØ*.×*>Ñ*>¸qÓ*AÑ'‰ˆ�)˜ZØ×Ñ˜Ÿ™×2Ñ2Ó4°iÕ@r   c                ób   — | j                  | j                  j                  d«      t        «       y)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)ro   rL   r`   r	   r   s    r   Útest_lookupAddressNotFoundz%HostsTests.test_lookupAddressNotFound  s#   € ð 	×Ñ˜TŸ]™]×8Ñ8¸ÓCÄ[ÕQr   c                ób   — | j                  | j                  j                  d«      t        «       y)zj
        Like L{test_lookupAddressNotFound}, but for
        L{hosts.Resolver.lookupIPV6Address}.
        r{   N)ro   rL   ri   r	   r   s    r   Útest_lookupIPV6AddressNotFoundz)HostsTests.test_lookupIPV6AddressNotFound  s#   € ð
 	×Ñ˜TŸ]™]×<Ñ<¸YÓGÌÕUr   c                ób   — | j                  | j                  j                  d«      t        «       y)zi
        Like L{test_lookupAddressNotFound}, but for
        L{hosts.Resolver.lookupAllRecords}.
        r{   N)ro   rL   rl   r	   r   s    r   Útest_lookupAllRecordsNotFoundz(HostsTests.test_lookupAllRecordsNotFound  s#   € ð
 	×Ñ˜TŸ]™]×;Ñ;¸IÓFÌÕTr   c                óò   — | j                   j                  d«      }| j                  |«      \  \  }}}| j                  t	        dt
        t        | j                  t        d| j                  «      «      |«       y)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.5Nr_   rw   s        r   Útest_lookupMalformedzHostsTests.test_lookupMalformed  sa   € ð �M‰M×'Ñ'¨Ó5ˆØ*.×*>Ñ*>¸qÓ*AÑ'‰ˆ�)˜ZØ×ÑÜ�\¤1¤b¨$¯(©(´H¸YÈÏÉÓ4QÓRØõ	
r   c                óò   — | j                   j                  d«      }| j                  |«      \  \  }}}| j                  t	        dt
        t        | j                  t        d| j                  «      «      |«       y)zk
        Like L{test_lookupAddressMalformed}, but for
        L{hosts.Resolver.lookupIPV6Address}.
        r‚   z::5Nrh   rw   s        r   Útest_lookupIPV6Malformedz#HostsTests.test_lookupIPV6Malformed'  sa   € ð
 �M‰M×+Ñ+¨LÓ9ˆØ*.×*>Ñ*>¸qÓ*AÑ'‰ˆ�)˜ZØ×ÑÜ�\¤4¬¨T¯X©X´{À5È$Ï(É(Ó7SÓTØõ	
r   Nr9   )r   r   r    r;   rN   rQ   r]   rf   rj   rm   rr   ry   r|   r~   r€   rƒ   r…   r   r   r   rI   rI   ”   sL   „ ñó3ó,7ó0ó"
ó
ó&

ó
óAó
RóVóUó
ô

r   rI   N)r;   Ú
__future__r   Útypingr   Ú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'   r=   rI   r   r   r   ú<module>r�      s   ðñõ #å å 0÷	÷ 	ó 	÷ JÑ IÝ ,Ý 6ô�hô ÷
7ñ 7ô
3IÐ.Ð0Aô 3Iôl6
Ð!4Ð6Gô 6
ôr]
Ð$Ð&7õ ]
r   