o
    Òó·išL  ã                   @   s¢  d 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mZ ddlmZmZ ddlmZ ddlmZ ddlmZmZmZ ddlmZmZmZ dd	lmZmZ dd
lm Z m!Z! ddl"m#Z# ddl$m%Z% ddl&m'Z'm(Z(m)Z)m*Z* ddl+m,Z, ddl-m.Z/ G dd„ de,ƒZ0dd„ Z1dd„ Z2G dd„ dƒZ3ee)ƒG dd„ dƒƒZ4G dd„ de/ƒZ5G dd„ de/ƒZ6ee*ƒG dd„ dƒƒZ7G d d!„ d!e/ƒZ8G d"d#„ d#e!ƒZ9G d$d%„ d%e/ƒZ:d&S )'zh
Tests for implementations of L{IHostnameResolver} and their interactions with
reactor implementations.
é    )Údefaultdict)	ÚAF_INETÚAF_INET6Ú	AF_UNSPECÚ
EAI_NONAMEÚIPPROTO_TCPÚ
SOCK_DGRAMÚSOCK_STREAMÚgaierrorÚgetaddrinfo)ÚLockÚlocal)Úimplementer)ÚverifyObject)Ú
LockWorkerÚTeamÚcreateMemoryWorker)ÚComplexResolverSimplifierÚGAIResolverÚSimpleResolverComplexifier)ÚIPv4AddressÚIPv6Address)ÚPluggableResolverMixinÚReactorBase)ÚDeferred)ÚDNSLookupError)ÚIHostnameResolverÚIReactorPluggableNameResolverÚIResolutionReceiverÚIResolverSimple)Ú
ThreadPool)ÚSynchronousTestCasec                   @   ó   e Zd ZdZdd„ ZdS )ÚDeterministicThreadPoolz6
    Create a deterministic L{ThreadPool} object.
    c                 C   s"   d| _ d| _d| _g | _|| _dS )zE
        Create a L{DeterministicThreadPool} from a L{Team}.
        é   N)ÚminÚmaxÚnameÚthreadsZ_team)ÚselfZteam© r*   úiC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\twisted/internet/test/test_resolver.pyÚ__init__4   s
   
z DeterministicThreadPool.__init__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r,   r*   r*   r*   r+   r#   /   ó    r#   c                     s4   t ƒ \‰ } ttttƒ tƒ ƒ‡ fdd„dd„ ƒƒ| fS )zž
    Create a deterministic threadpool.

    @return: 2-tuple of L{ThreadPool}, 0-argument C{work} callable; when
        C{work} is called, do the work.
    c                      s   ˆ S ©Nr*   r*   ©Zworkerr*   r+   Ú<lambda>I   ó    z#deterministicPool.<locals>.<lambda>c                   S   s   d S r2   r*   r*   r*   r*   r+   r4   I   r5   N)r   r#   r   r   r   r   )Údoerr*   r3   r+   ÚdeterministicPool?   s   
 ÿür7   c                     s&   t ƒ \‰ } G ‡ fdd„dƒ}|ƒ | fS )zü
    Create a deterministic L{IReactorThreads}

    @return: a 2-tuple consisting of an L{IReactorThreads}-like object and a
        0-argument callable that will perform one unit of work invoked via that
        object's C{callFromThread} method.
    c                       s   e Zd Z‡ fdd„ZdS )z(deterministicReactorThreads.<locals>.CFTc                    s   ˆ  ‡ ‡‡fdd„¡ d S )Nc                      s   ˆˆ i ˆ¤ŽS r2   r*   r*   ©ÚaÚfÚkr*   r+   r4   [   s    zIdeterministicReactorThreads.<locals>.CFT.callFromThread.<locals>.<lambda>)Údo)r)   r:   r9   r;   r3   r8   r+   ÚcallFromThreadZ   s   z7deterministicReactorThreads.<locals>.CFT.callFromThreadN)r-   r.   r/   r=   r*   r3   r*   r+   ÚCFTY   s    r>   N)r   )r6   r>   r*   r3   r+   ÚdeterministicReactorThreadsO   s   

r?   c                   @   s4   e Zd ZdZdd„ Zddd„Zeeedfdd	„Z	d
S )ÚFakeAddrInfoGetterz/
    Test object implementing getaddrinfo.
    c                 C   s   g | _ ttƒ| _dS )z1
        Create a L{FakeAddrInfoGetter}.
        N)Úcallsr   ÚlistÚresults©r)   r*   r*   r+   r,   e   s   zFakeAddrInfoGetter.__init__r   c                 C   s4   | j  ||||||f¡ | j| }|r|S ttdƒ‚)a�  
        Mock for L{socket.getaddrinfo}.

        @param host: see L{socket.getaddrinfo}

        @param port: see L{socket.getaddrinfo}

        @param family: see L{socket.getaddrinfo}

        @param socktype: see L{socket.getaddrinfo}

        @param proto: see L{socket.getaddrinfo}

        @param flags: see L{socket.getaddrinfo}

        @return: L{socket.getaddrinfo}
        z,nodename nor servname provided, or not knownN)rA   ÚappendrC   r
   r   )r)   ÚhostÚportÚfamilyÚsocktypeÚprotoÚflagsrC   r*   r*   r+   r   l   s
   

zFakeAddrInfoGetter.getaddrinfoó    c                 C   s   | j |  |||||f¡ dS )aà  
        Add a result for a given hostname.  When this hostname is resolved, the
        result will be a L{list} of all results C{addResultForHost} has been
        called with using that hostname so far.

        @param host: The hostname to give this result for.  This will be the
            next result from L{FakeAddrInfoGetter.getaddrinfo} when passed this
            host.

        @type canonname: native L{str}

        @param sockaddr: The resulting socket address; should be a 2-tuple for
            IPv4 or a 4-tuple for IPv6.

        @param family: An C{AF_*} constant that will be returned from
            C{getaddrinfo}.

        @param socktype: A C{SOCK_*} constant that will be returned from
            C{getaddrinfo}.

        @param proto: An C{IPPROTO_*} constant that will be returned from
            C{getaddrinfo}.

        @param canonname: A canonical name that will be returned from
            C{getaddrinfo}.
        @type canonname: native L{str}
        N)rC   rE   )r)   rF   ÚsockaddrrH   rI   rJ   Ú	canonnamer*   r*   r+   ÚaddResultForHost…   s   $z#FakeAddrInfoGetter.addResultForHostN)r   r   r   r   )
r-   r.   r/   r0   r,   r   r   r	   r   rO   r*   r*   r*   r+   r@   `   s    
ùr@   c                   @   s8   e Zd ZdZdZdZdd„ Zdd„ Zdd„ Zd	d
„ Z	dS )ÚResultHolderzI
    A resolution receiver which holds onto the results it received.
    Fc                 C   s
   || _ dS )z>
        Create a L{ResultHolder} with a L{UnitTest}.
        N)Z	_testCase)r)   ZtestCaser*   r*   r+   r,   µ   ó   
zResultHolder.__init__c                 C   s   d| _ || _g | _dS )zg
        Hostname resolution began.

        @param hostResolution: see L{IResolutionReceiver}
        TN)Ú_startedÚ_resolutionÚ
_addresses)r)   ZhostResolutionr*   r*   r+   ÚresolutionBegan»   s   
zResultHolder.resolutionBeganc                 C   s   | j  |¡ dS )z^
        An address was resolved.

        @param address: see L{IResolutionReceiver}
        N)rT   rE   )r)   Úaddressr*   r*   r+   ÚaddressResolvedÅ   s   zResultHolder.addressResolvedc                 C   s
   d| _ dS )z2
        Hostname resolution is complete.
        TN)Ú_endedrD   r*   r*   r+   ÚresolutionCompleteÍ   rQ   zResultHolder.resolutionCompleteN)
r-   r.   r/   r0   rR   rX   r,   rU   rW   rY   r*   r*   r*   r+   rP   ¬   s    
rP   c                   @   r"   )ÚHelperTestsz?
    Tests for error cases of helpers used in this module.
    c                 C   sD   t ƒ \| _| _dd„ }| j |¡ |  ¡  |  t|  t¡ƒd¡ dS )zq
        L{DeterministicThreadPool} will log any exceptions that its "thread"
        workers encounter.
        c                   S   s   dd S )Nr$   r   r*   r*   r*   r*   r+   ÚdivideByZeroà   s   z9HelperTests.test_logErrorsInThreads.<locals>.divideByZeror$   N)r7   ÚpoolÚdoThreadWorkZcallInThreadÚassertEqualÚlenÚflushLoggedErrorsÚZeroDivisionError)r)   r[   r*   r*   r+   Útest_logErrorsInThreadsÙ   s
   z#HelperTests.test_logErrorsInThreadsN)r-   r.   r/   r0   rb   r*   r*   r*   r+   rZ   Ô   r1   rZ   c                   @   s`   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S )ÚHostnameResolutionTestsz(
    Tests for hostname resolution.
    c                    sD   t ƒ \ˆ _ˆ _tƒ \ˆ _ˆ _tƒ ˆ _tˆ j‡ fdd„ˆ jj	ƒˆ _
dS )z*
        Set up a L{GAIResolver}.
        c                      ó   ˆ j S r2   ©r\   r*   rD   r*   r+   r4   õ   ó    z/HostnameResolutionTests.setUp.<locals>.<lambda>N)r7   r\   r]   r?   ÚreactorÚdoReactorWorkr@   Úgetterr   r   ÚresolverrD   r*   rD   r+   ÚsetUpí   s   
ÿzHostnameResolutionTests.setUpc                 C   sˆ   t | ƒ}| j dd¡ | j |d¡}|  |j|¡ |  |jd¡ |  |j	d¡ |  
¡  |  ¡  |  |j	d¡ |  |jtdddƒg¡ dS )	zÓ
        Resolving an individual hostname that results in one address from
        getaddrinfo results in a single call each to C{resolutionBegan},
        C{addressResolved}, and C{resolutionComplete}.
        úsample.example.com)ú4.3.2.1r   TFÚTCPrm   r   N)rP   ri   rO   rj   ÚresolveHostNameÚassertIsrS   r^   rR   rX   r]   rh   rT   r   ©r)   ÚreceiverÚ
resolutionr*   r*   r+   Útest_resolveOneHostø   s   z+HostnameResolutionTests.test_resolveOneHostc              	   C   s    t | ƒ}d}d}| jjddd||ftd� | j |d¡}|  |j|¡ |  |j	d¡ |  |j
d¡ |  ¡  |  ¡  |  |j
d¡ |  |jtd	dd||ƒg¡ d
S )a  
        Resolving an individual hostname that results in one address from
        getaddrinfo results in a single call each to C{resolutionBegan},
        C{addressResolved}, and C{resolutionComplete}; C{addressResolved} will
        receive an L{IPv6Address}.
        r$   é   rl   ú::1r   )rH   TFrn   N)rP   ri   rO   r   rj   ro   rp   rS   r^   rR   rX   r]   rh   rT   r   )r)   rr   ÚflowInfoÚscopeIDrs   r*   r*   r+   Útest_resolveOneIPv6Host	  s    ÿÿz/HostnameResolutionTests.test_resolveOneIPv6Hostc                 C   sb   t | ƒ}| j |d¡}|  |j|¡ |  ¡  |  ¡  |  |jd¡ |  |j	d¡ |  |j
g ¡ dS )a
  
        Resolving a hostname that results in C{getaddrinfo} raising a
        L{gaierror} will result in the L{IResolutionReceiver} receiving a call
        to C{resolutionComplete} with no C{addressResolved} calls in between;
        no failure is logged.
        rl   TN)rP   rj   ro   rp   rS   r]   rh   r^   rR   rX   rT   rq   r*   r*   r+   Útest_gaierror!  s   z%HostnameResolutionTests.test_gaierrorc                 C   s`   t | ƒ}| jj|d|d�}|  |j|¡ |  ¡  |  ¡  | jjd \}}}}}	}
|  	||¡ dS )zù
        Verify that the given set of address types results in the given C{AF_}
        constant being passed to C{getaddrinfo}.

        @param addrTypes: iterable of L{IAddress} implementers

        @param expectedAF: an C{AF_*} constant
        rl   )ZaddressTypesr   N)
rP   rj   ro   rp   rS   r]   rh   ri   rA   r^   )r)   Z	addrTypesZ
expectedAFrr   rs   rF   rG   rH   rI   rJ   rK   r*   r*   r+   Ú_resolveOnlyTest1  s   	ÿz(HostnameResolutionTests._resolveOnlyTestc                 C   ó   |   tgt¡ dS )z™
        When passed an C{addressTypes} parameter containing only
        L{IPv4Address}, L{GAIResolver} will pass C{AF_INET} to C{getaddrinfo}.
        N)r{   r   r   rD   r*   r*   r+   Útest_resolveOnlyIPv4D  ó   z,HostnameResolutionTests.test_resolveOnlyIPv4c                 C   r|   )zš
        When passed an C{addressTypes} parameter containing only
        L{IPv6Address}, L{GAIResolver} will pass C{AF_INET6} to C{getaddrinfo}.
        N)r{   r   r   rD   r*   r*   r+   Útest_resolveOnlyIPv6K  r~   z,HostnameResolutionTests.test_resolveOnlyIPv6c                 C   s    |   ttgt¡ |   dt¡ dS )zò
        When passed an C{addressTypes} parameter containing both L{IPv4Address}
        and L{IPv6Address} (or the default of C{None}, which carries the same
        meaning), L{GAIResolver} will pass C{AF_UNSPEC} to C{getaddrinfo}.
        N)r{   r   r   r   rD   r*   r*   r+   Útest_resolveBothR  s   z(HostnameResolutionTests.test_resolveBothc           
      C   s    t | ƒ}| jj|ddd� t | ƒ}| jj|ddd� |  ¡  |  ¡  |  ¡  |  ¡  | jjd \}}}}}}| jjd \}}}}	}}|  |t¡ |  |	t	¡ dS )zÝ
        When passed a C{transportSemantics} paramter, C{'TCP'} (the value
        present in L{IPv4Address.type} to indicate a stream transport) maps to
        C{SOCK_STREAM} and C{'UDP'} maps to C{SOCK_DGRAM}.
        úexample.comrn   )ZtransportSemanticsÚUDPr   r$   N)
rP   rj   ro   r]   rh   ri   rA   r^   r	   r   )
r)   rr   Z	receiver2rF   rG   rH   Z	socktypeTrJ   rK   Z	socktypeUr*   r*   r+   Ú#test_transportSemanticsToSocketType[  s   ÿz;HostnameResolutionTests.test_transportSemanticsToSocketTypec           	      C   s¶   t | ƒ}d}d}ttfD ]}| jjddd||ft|d� | jjddt|d� q| j |d¡ |  	¡  |  
¡  |j\}}}}|  |jd¡ |  |jd¡ |  |jd	¡ |  |jd	¡ d
S )zé
        When L{GAIResolver} receives a C{SOCK_DGRAM} result from
        C{getaddrinfo}, it returns a C{'TCP'} L{IPv4Address} or L{IPv6Address};
        if it receives C{SOCK_STREAM} then it returns a C{'UDP'} type of same.
        r$   ru   r�   rv   r   )rH   rI   )z	127.0.0.3r   rn   r‚   N)rP   r	   r   ri   rO   r   r   rj   ro   r]   rh   rT   r^   Útype)	r)   rr   rw   rx   rI   Zstream4Zstream6Zdgram4Zdgram6r*   r*   r+   Útest_socketTypeToAddressTypep  s*   
üÿz4HostnameResolutionTests.test_socketTypeToAddressTypeN)r-   r.   r/   r0   rk   rt   ry   rz   r{   r}   r   r€   rƒ   r…   r*   r*   r*   r+   rc   è   s    	rc   c                   @   s"   e Zd ZdZdd„ Zddd„ZdS )	ÚSillyResolverSimplez6
    Trivial implementation of L{IResolverSimple}
    c                 C   s
   g | _ dS )zd
        Create a L{SillyResolverSimple} with a queue of requests it is working
        on.
        N)Ú	_requestsrD   r*   r*   r+   r,   “  s   
zSillyResolverSimple.__init__r*   c                 C   s   | j  tƒ ¡ | j d S )zð
        Implement L{IResolverSimple.getHostByName}.

        @param name: see L{IResolverSimple.getHostByName}.

        @param timeout: see L{IResolverSimple.getHostByName}.

        @return: see L{IResolverSimple.getHostByName}.
        éÿÿÿÿN)r‡   rE   r   )r)   r'   Útimeoutr*   r*   r+   ÚgetHostByNameš  s   

z!SillyResolverSimple.getHostByNameN)r*   )r-   r.   r/   r0   r,   rŠ   r*   r*   r*   r+   r†   �  s    r†   c                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚLegacyCompatibilityTestszÌ
    Older applications may supply an object to the reactor via
    C{installResolver} that only provides L{IResolverSimple}.
    L{SimpleResolverComplexifier} is a wrapper for an L{IResolverSimple}.
    c                 C   s”   t ƒ }t|ƒ}t| ƒ}|  |jd¡ | |d¡ |  |jd¡ |  |jd¡ |  |jg ¡ |jd  	d¡ |  |jt
dddƒg¡ |  |jd¡ dS )z‚
        L{SimpleResolverComplexifier} translates C{resolveHostName} into
        L{IResolutionReceiver.addressResolved}.
        Fr�   Tr   ú192.168.1.1rn   N)r†   r   rP   r^   rR   ro   rX   rT   r‡   Úcallbackr   ©r)   ÚsimpleÚcomplexrr   r*   r*   r+   Útest_success¯  s   z%LegacyCompatibilityTests.test_successc                 C   sŽ   t ƒ }t|ƒ}t| ƒ}|  |jd¡ | |d¡ |  |jd¡ |  |jd¡ |  |jg ¡ |jd  	t
dƒ¡ |  |jd¡ |  |jg ¡ dS )z•
        L{SimpleResolverComplexifier} translates a known error result from
        L{IResolverSimple.resolveHostName} into an empty result.
        Fr�   Tr   ZnopeN)r†   r   rP   r^   rR   ro   rX   rT   r‡   Úerrbackr   rŽ   r*   r*   r+   Útest_failureÀ  s   z%LegacyCompatibilityTests.test_failurec                 C   s¤   t ƒ }t|ƒ}t| ƒ}|  |jd¡ | |d¡ |  |jd¡ |  |jd¡ |  |jg ¡ |jd  	t
dƒ¡ |  t|  t
¡ƒd¡ |  |jd¡ |  |jg ¡ dS )z³
        L{SimpleResolverComplexifier} translates an unknown error result from
        L{IResolverSimple.resolveHostName} into an empty result and a logged
        error.
        Fr�   Tr   Zzowr$   N)r†   r   rP   r^   rR   ro   rX   rT   r‡   r’   ra   r_   r`   rŽ   r*   r*   r+   Ú
test_errorÑ  s   z#LegacyCompatibilityTests.test_errorc                    s¶   t ƒ \ˆ _ˆ _tƒ \ˆ _ˆ _tƒ ˆ _tˆ j‡ fdd„ˆ jj	ƒˆ _
tˆ j
ƒ}ˆ j dd¡ | d¡}| d¡}ˆ  ¡  ˆ  ¡  ˆ  ¡  ˆ  ¡  ˆ  ˆ  |¡jt¡ ˆ  ˆ  |¡d¡ dS )zÂ
        L{ComplexResolverSimplifier} translates an L{IHostnameResolver} into an
        L{IResolverSimple} for applications that still expect the old
        interfaces to be in place.
        c                      rd   r2   re   r*   rD   r*   r+   r4   î  rf   z:LegacyCompatibilityTests.test_simplifier.<locals>.<lambda>r�   )ú192.168.3.4éá  znx.example.comr•   N)r7   r\   r]   r?   rg   rh   r@   ri   r   r   rj   r   rO   rŠ   r^   ZfailureResultOfr„   r   ZsuccessResultOf)r)   ZsimpleResolverÚsuccessZfailurer*   rD   r+   Útest_simplifierä  s    ÿ


z(LegacyCompatibilityTests.test_simplifierc                 C   sˆ   t ƒ }t|ƒ}t| ƒ}| |dd¡ |  |jd¡ |  |jd¡ |  |jg ¡ |jd  	d¡ |  |jt
dddƒg¡ |  |jd¡ dS )	zŽ
        L{SimpleResolverComplexifier} preserves the C{port} argument passed to
        C{resolveHostName} in its returned addresses.
        r�   r–   TFr   rŒ   rn   N)r†   r   rP   ro   r^   rR   rX   rT   r‡   r�   r   rŽ   r*   r*   r+   Útest_portNumberû  s   z(LegacyCompatibilityTests.test_portNumberN)	r-   r.   r/   r0   r‘   r“   r”   r˜   r™   r*   r*   r*   r+   r‹   ¨  s    r‹   c                   @   r"   )ÚJustEnoughReactorzT
    Just enough subclass implementation to be a valid L{ReactorBase} subclass.
    c                 C   s   dS )z
        Do nothing.
        Nr*   rD   r*   r*   r+   ÚinstallWaker  r5   zJustEnoughReactor.installWakerN)r-   r.   r/   r0   r›   r*   r*   r*   r+   rš     r1   rš   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚReactorInstallationTestsz®
    Tests for installing old and new resolvers onto a
    L{PluggableResolverMixin} and L{ReactorBase} (from which all of Twisted's
    reactor implementations derive).
    c                 C   s,   t ƒ }tt|ƒ tt|jƒ tt|jƒ dS )zš
        L{PluggableResolverMixin} (and its subclasses) implement both
        L{IReactorPluggableNameResolver} and L{IReactorPluggableResolver}.
        N)r   r   r   r   rj   r   ÚnameResolver©r)   rg   r*   r*   r+   Útest_interfaceCompliance  s   
z1ReactorInstallationTests.test_interfaceCompliancec                 C   s>   t ƒ }tƒ }tt| |¡ƒ |  |jt¡ |  |jj	|¡ dS )zf
        L{PluggableResolverMixin} will wrap an L{IResolverSimple} in a
        complexifier.
        N)
r   r†   r   r   ZinstallResolverÚassertIsInstancer�   r   rp   Z_simpleResolver)r)   rg   Úitr*   r*   r+   Útest_installingOldStyleResolver(  s
   z8ReactorInstallationTests.test_installingOldStyleResolverc                 C   sX   t ƒ }|  |jt¡ |  |jjt¡ |  |jt¡ |  |jj	|¡ |  |jj
|j¡ dS )zD
        L{ReactorBase} defaults to using a L{GAIResolver}.
        N)rš   r    r�   r   rp   Z_getaddrinfor   rj   r   Z_reactorZ_nameResolverrž   r*   r*   r+   Útest_defaultToGAIResolver3  s   z2ReactorInstallationTests.test_defaultToGAIResolverN)r-   r.   r/   r0   rŸ   r¢   r£   r*   r*   r*   r+   rœ     s
    
rœ   N);r0   Úcollectionsr   Úsocketr   r   r   r   r   r   r	   r
   r   Ú	threadingr   r   Zzope.interfacer   Zzope.interface.verifyr   Ztwisted._threadsr   r   r   Ztwisted.internet._resolverr   r   r   Ztwisted.internet.addressr   r   Ztwisted.internet.baser   r   Ztwisted.internet.deferr   Ztwisted.internet.errorr   Ztwisted.internet.interfacesr   r   r   r   Ztwisted.python.threadpoolr    Ztwisted.trial.unittestr!   ZUnitTestr#   r7   r?   r@   rP   rZ   rc   r†   r‹   rš   rœ   r*   r*   r*   r+   Ú<module>   s:   ,L' &d