o
    >h(                     @  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   }/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/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)   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-localhostr0   s   ip6-loopbacks	   localhostNr(   r+   r   r   r   test_searchFileForAliasesI   s   z.SearchHostsFileTests.test_searchFileForAliasesNr   r$   )r   r   r   __doc__r-   r/   r1   r2   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.2r0   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
r6   s   FOOBAR.example.comNr7   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 miserr6   r0   s   miserNr7   r+   r   r   r   test_malformedIP   s   z+SearchHostsFileForAllTests.test_malformedIPNr3   )r   r   r   r4   r8   r9   r:   r;   r   r   r   r   r5   [   s    


r5   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.EXAMPLETHINGrD   )s   MIXED1.1.1.2c                   s&   g | ]\}} j | j|qS r   )r>   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	r>   lookupAddressrL   r*   r   r   r   r=   r
   r   danswers	authority
additionalr   r   r   test_lookupAddress   s   zHostsTests.test_lookupAddressc                 C  rP   )z
        L{hosts.Resolver.lookupIPV6Address} returns a L{Deferred} which fires
        with AAAA records from the hosts file.
        s   ip6-multiplez::3z::4N	r>   lookupIPV6AddressrL   r*   r   r   r   r=   r   rS   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   mixedrE   N)	r>   lookupAllRecordsrL   r*   r   r   r   r=   r
   rS   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.
        rC   N)failureResultOfr>   lookupMailExchangeNotImplementedErrorr   r   r   r   test_notImplemented   s   zHostsTests.test_notImplementedc                 C  s8   | j td}| |\\}}}| |j d d S )NrC   rD   )r>   queryr	   rL   r*   payload
dottedQuadr   rT   answerrV   rW   r   r   r   
test_query   s   zHostsTests.test_queryc                 C  r^   )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)r_   r>   rR   r   r   r   r   r   test_lookupAddressNotFound  s   z%HostsTests.test_lookupAddressNotFoundc                 C  r^   )zj
        Like L{test_lookupAddressNotFound}, but for
        L{hosts.Resolver.lookupIPV6Address}.
        ri   N)r_   r>   rZ   r   r   r   r   r   test_lookupIPV6AddressNotFound     z)HostsTests.test_lookupIPV6AddressNotFoundc                 C  r^   )zi
        Like L{test_lookupAddressNotFound}, but for
        L{hosts.Resolver.lookupAllRecords}.
        ri   N)r_   r>   r\   r   r   r   r   r   test_lookupAllRecordsNotFound  rl   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.5NrQ   rf   r   r   r   test_lookupMalformed  s   zHostsTests.test_lookupMalformedc              
   C  rn   )zk
        Like L{test_lookupAddressMalformed}, but for
        L{hosts.Resolver.lookupIPV6Address}.
        ro   z::5NrY   rf   r   r   r   test_lookupIPV6Malformed'  s   z#HostsTests.test_lookupIPV6MalformedNr3   )r   r   r   r4   r@   rB   rO   rX   r[   r]   rb   rh   rj   rk   rm   rp   rq   r   r   r   r   r<      s    







	



r<   N)r4   
__future__r   typing_extensionsr   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#   r5   r<   r   r   r   r   <module>   s   (
69