
    Vh(                        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    M/home/dcms/DCMS/lib/python3.12/site-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@     		:	

 	ejj: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    
 		:	

 	uzz9={Kuzz3HI;Wuzz3CDeLuzz?CUKuzz<@%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"    
 	-diik>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    
 			
 	% UH-	
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=    IIK		
"  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 MM''*66t7G7G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    
 MM''4)-)=)=a)@&JaTXXx	4887TUaTXXx	4887T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    
 MM++O<)-)=)=a)@&J#T2txxUDHH9U #T2txxUDHH9U	 
	
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    
 MM**84)-)=)=a)@&Jh2txx)TXX1N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'    
 	MM,,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    MMj 12*.*>*>q*A')Z224i@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     MM''5*.*>*>q*A')Z\1b$((HY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    
 MM++L9*.*>*>q*A')Z\4TXX{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   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=   rI   r   r   r   <module>r      s    # & 0	 	 	 J I , 6h 
7 7
3I.0A 3Il6
!46G 6
r]
$&7 ]
r   