
    tf(                       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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                      e Zd ZddZdS )_SupportsMktempreturnstrc                    d S )N selfs    \/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/twisted/names/test/test_hosts.pymktempz_SupportsMktemp.mktemp   s        N)r   r   )__name__
__module____qualname__r   r   r   r   r   r      s(             r   r   c                      e Zd ZddZdS )GoodTempPathMixinr   r   r   FilePath[bytes]c                j    t          |                                                     d                    S )Nzutf-8)r   r   encoder   s    r   pathzGoodTempPathMixin.path!   s&    ,,W55666r   N)r   r   r   r#   )r   r   r    r&   r   r   r   r"   r"       s(        7 7 7 7 7 7r   r"   c                  2    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.
    r   Nonec                    |                                  }|                    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   hostss     r   test_findAddressz%SearchHostsFileTests.test_findAddress+   sO    
 		6777]5:?Q%R%RSSSSSr   c                    |                                  }|                    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   r0   s     r   test_notFoundAddressz)SearchHostsFileTests.test_notFoundAddress4   sM    
 		6777-
4FGGHHHHHr   c                    |                                  }|                    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-   r0   s     r   test_firstAddressz&SearchHostsFileTests.test_firstAddress=   sY     		:	
 	
 	

 	ej:L M MNNNNNr   c                   |                                  }|                    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-localhostr7   s   ip6-loopbacks	   localhostNr-   r0   s     r   test_searchFileForAliasesz.SearchHostsFileTests.test_searchFileForAliasesI   s    
 		:	
 	
 	

 	uz9=={KKKuz3HII;WWWuz3CDDeLLLuz?CCUKKKuz<@@%HHHHHr   Nr   r)   )r   r   r    __doc__r2   r5   r8   r:   r   r   r   r(   r(   %   s         
T T T TI I I I
O 
O 
O 
OI I I I I Ir   r(   c                  2    e Zd ZdZd	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.
    r   r)   c                    |                                  }|                    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.2r7   s   foobar.example.comNr&   r.   r/   r   r0   s     r   test_allAddressesz,SearchHostsFileForAllTests.test_allAddressesa   si    
 		2	
 	
 	

 	---U$9::	
 	
 	
 	
 	
r   c                    |                                  }|                    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.comNrA   r0   s     r   test_caseInsensitivelyz1SearchHostsFileForAllTests.test_caseInsensitivelyq   sP     		>???+(8@U(V(VWWWWWr   c                r    |                      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   s    r   test_readErrorz)SearchHostsFileForAllTests.test_readErrory   s3    
 	-diikk>JJKKKKKr   c                    |                                  }|                    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@   r7   s   miserNrA   r0   s     r   test_malformedIPz+SearchHostsFileForAllTests.test_malformedIP   sg    
 			
 	
 	
 	% UH--	
 	
 	
 	
 	
r   Nr;   )r   r   r    r<   rB   rD   rF   rH   r   r   r   r>   r>   [   sv         

 
 
 
 X X X XL L L L
 
 
 
 
 
r   r>   c                  z    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}.
    r   r)   c                    |                                  }|                    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   fs     r   setUpzHostsTests.setUp   sL    IIKK		
 	
 	
"  22r   c                X    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   rM   s     r   test_defaultPathzHostsTests.test_defaultPath   s+    
 ::66666r   c                p     g d} f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.EXAMPLETHINGrU   )s   MIXED1.1.1.2c                |    g | ]8\  }}j                             |                              j        |          9S r   )rM   getHostByNameaddCallbackr/   ).0nipr   s      r   
<listcomp>z1HostsTests.test_getHostByName.<locals>.<listcomp>   sP     
 
 
2 M''**66t7GLL
 
 
r   N)successResultOfr   )r   datadss   `  r   test_getHostByNamezHostsTests.test_getHostByName   s`    
 
 


 
 
 

 
 
 	]2../////r   c                h   | 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	rM   lookupAddressr^   r/   r   r   r   rL   r   r   danswers	authority
additionals        r   test_lookupAddresszHostsTests.test_lookupAddress   s    
 M''44)-)=)=a)@)@&JaTXx	487T7TUUaTXx	487T7TUU 	
 	
 	
 	
 	
r   c                h   | 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.lookupIPV6Address} returns a L{Deferred} which fires
        with AAAA records from the hosts file.
        s   ip6-multiplez::3z::4N	rM   lookupIPV6Addressr^   r/   r   r   r   rL   r   re   s        r   test_lookupIPV6Addressz!HostsTests.test_lookupIPV6Address   s    
 M++O<<)-)=)=a)@)@&J#T2txUDH9U9U  #T2txUDH9U9U 	 
	
 
	
 
	
 
	
 
	
r   c                    | 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   mixedrV   N)	rM   lookupAllRecordsr^   r/   r   r   r   rL   r   re   s        r   test_lookupAllRecordsz HostsTests.test_lookupAllRecords   s|    
 M**844)-)=)=a)@)@&Jh2tx)TX1N1NOOQ	
 	
 	
 	
 	
r   c                l    |                      | j                            d          t                     dS )z|
        L{hosts.Resolver} fails with L{NotImplementedError} for L{IResolver}
        methods it doesn't implement.
        rT   N)failureResultOfrM   lookupMailExchangeNotImplementedErrorr   s    r   test_notImplementedzHostsTests.test_notImplemented   s<    
 	M,,Z88:M	
 	
 	
 	
 	
r   c                    | j                             t          d                    }|                     |          \  \  }}}|                     |j                                        d           d S )NrT   rU   )rM   queryr
   r^   r/   payload
dottedQuadr   rf   answerrh   ri   s        r   
test_queryzHostsTests.test_query   se    Mj 1 122*.*>*>q*A*A')Z2244i@@@@@r   c                l    |                      | j                            d          t                     d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.
           foueoaN)rs   rM   rd   r	   r   s    r   test_lookupAddressNotFoundz%HostsTests.test_lookupAddressNotFound  s0     	T]88CC[QQQQQr   c                l    |                      | j                            d          t                     dS )zj
        Like L{test_lookupAddressNotFound}, but for
        L{hosts.Resolver.lookupIPV6Address}.
        r   N)rs   rM   rm   r	   r   s    r   test_lookupIPV6AddressNotFoundz)HostsTests.test_lookupIPV6AddressNotFound  s0    
 	T]<<YGGUUUUUr   c                l    |                      | j                            d          t                     dS )zi
        Like L{test_lookupAddressNotFound}, but for
        L{hosts.Resolver.lookupAllRecords}.
        r   N)rs   rM   rp   r	   r   s    r   test_lookupAllRecordsNotFoundz(HostsTests.test_lookupAllRecordsNotFound  s0    
 	T];;IFFTTTTTr   c                   | 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.5Nrc   r{   s        r   test_lookupMalformedzHostsTests.test_lookupMalformed  s{     M''55*.*>*>q*A*A')Z\1b$(HY4Q4QRR	
 	
 	
 	
 	
r   c                   | j                             d          }|                     |          \  \  }}}|                     t	          dt
          t          | j        t          d| j                            |           dS )zk
        Like L{test_lookupAddressMalformed}, but for
        L{hosts.Resolver.lookupIPV6Address}.
        r   z::5Nrl   r{   s        r   test_lookupIPV6Malformedz#HostsTests.test_lookupIPV6Malformed'  s{    
 M++L99*.*>*>q*A*A')Z\4TX{5$(7S7STT	
 	
 	
 	
 	
r   Nr;   )r   r   r    r<   rO   rR   ra   rj   rn   rq   rv   r}   r   r   r   r   r   r   r   r   rJ   rJ      s2        3 3 3 3,7 7 7 70 0 0 0"
 
 
 

 
 
 
&

 

 

 


 
 
 
A A A A
R R R RV V V VU U U U
 
 
 


 

 

 

 

 

r   rJ   N)r<   
__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(   r>   rJ   r   r   r   <module>r      s    # " " " " " & & & & & & 0 0 0 0 0 0	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 J I I I I I I I I I , , , , , , 6 6 6 6 6 6    h   
7 7 7 7 7 7 7 7
3I 3I 3I 3I 3I.0A 3I 3I 3Il6
 6
 6
 6
 6
!46G 6
 6
 6
r]
 ]
 ]
 ]
 ]
$&7 ]
 ]
 ]
 ]
 ]
r   