
    Yf$                         d Z ddlZddl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 dd	lmZ dd
lmZ ddlmZ  G d de          Z G d dej                  Z G d dej                  ZdS )z-
Test cases for L{twisted.names.srvconnect}.
    N)verifyObject)deferprotocol)DNSLookupErrorServiceNameUnknownError)
IConnector)MemoryReactor)clientdns
srvconnect)ResolverBase)DNSNameError)unittestc                        e Zd ZdZddZd ZdS )FakeResolvera  
    Resolver that only gives out one given result.

    Either L{results} or L{failure} must be set and will be used for
    the return value of L{_lookup}

    @ivar results: List of L{dns.RRHeader} for the desired result.
    @type results: C{list}
    @ivar failure: Failure with an exception from L{twisted.names.error}.
    @type failure: L{Failure<twisted.python.failure.Failure>}
    Nc                 0    || _         || _        g | _        d S N)resultsfailurelookups)selfr   r   s      T/var/www/html/env/lib/python3.11/site-packages/twisted/names/test/test_srvconnect.py__init__zFakeResolver.__init__$   s        c                     | j                             ||||f           | j        t          j        | j        g g f          S t          j        | j                  S )z9
        Return the result or failure on lookup.
        )r   appendr   r   succeedfailr   )r   nameclsqtypetimeouts        r   _lookupzFakeResolver._lookup)   sV     	T3w7888<#=$,B!7888:dl+++r   )NN)__name__
__module____qualname____doc__r   r#    r   r   r   r      sA        
 
   
, , , , ,r   r   c                       e Zd ZdZd Zd ZdS )DummyFactoryzL
    Dummy client factory that stores the reason of connection failure.
    c                     d | _         d S r   reasonr   s    r   r   zDummyFactory.__init__9   s    r   c                     || _         d S r   r,   )r   	connectorr-   s      r   clientConnectionFailedz#DummyFactory.clientConnectionFailed<   s    r   N)r$   r%   r&   r'   r   r1   r(   r   r   r*   r*   4   s<               r   r*   c                   l    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd ZdS )SRVConnectorTestsz/
    Tests for L{srvconnect.SRVConnector}.
    c                 
   |                      t          dt                                 t                      | _        t                      | _        t          j        | j        dd| j                  | _	        g | _
        g | _        d S )NtheResolverxmpp-serverexample.org)patchr
   r   r	   reactorr*   factoryr   SRVConnectorr0   randIntArgsrandIntResultsr.   s    r   setUpzSRVConnectorTests.setUpE   sk    

6=,..999$#~~#0L-
 
  r   c                 n    | j                             ||f           | j                            d          S )a  
        Fake randint.

        Returns the first element of L{randIntResults} and records the
        arguments passed to it in L{randIntArgs}.

        @param min: Lower bound of the random number.
        @type min: L{int}

        @param max: Higher bound of the random number.
        @type max: L{int}

        @return: Fake random number from L{randIntResults}.
        @rtype: L{int}
        r   )r<   r   r=   pop)r   minmaxs      r   _randintzSRVConnectorTests._randintO   s6      	c
+++"&&q)))r   c                 :    t          t          | j                   dS )zF
        L{srvconnect.SRVConnector} implements L{IConnector}.
        N)r   r   r0   r.   s    r   test_interfacez SRVConnectorTests.test_interfaceb   s     	Z00000r   c                    t          j        ddd          }t          j        dt           j        t           j        d|          gt
          j        _        | j        	                                 | 
                    | j        j                   |                     | j        j                                        dd         d	           dS )
zS
        Test connectTCP gets called with the address from the SRV record.
        }  host.example.org<   porttargetttlr7   r   typer    rM   payloadN   )rH   rG   )r   
Record_SRVRRHeaderSRVINr
   r5   r   r0   connectassertIsNoner:   r-   assertEqualr9   
tcpClientsr@   r   rP   s     r   test_SRVPresentz!SRVConnectorTests.test_SRVPresenth   s     .d3E2NNNL"cf"g  &
"
 	   $,-...04466rr:<VWWWWWr   c                 &   t          d          t          j        _        | j                                         |                     | j        j                   | 	                    | j
        j                                        dd         d           dS )zS
        Test connectTCP gets called with fallback parameters on NXDOMAIN.
        s   example.orgNrQ   r7   r6   )r   r
   r5   r   r0   rV   rW   r:   r-   rX   r9   rY   r@   r.   s    r   test_SRVNotPresentz$SRVConnectorTests.test_SRVNotPresentw   s     &2.%A%A"   $,-...L#''))"1"-/M	
 	
 	
 	
 	
r   c                    g t           j        _        | j                                         |                     | j        j                   |                     | j	        j
                                        dd         d           dS )zW
        Test connectTCP gets called with fallback parameters on empty result.
        NrQ   r]   )r
   r5   r   r0   rV   rW   r:   r-   rX   r9   rY   r@   r.   s    r   test_SRVNoResultz"SRVConnectorTests.test_SRVNoResult   s~     &("   $,-...L#''))"1"-/M	
 	
 	
 	
 	
r   c                 t   t          j        | j        dd| j        d          | _        t                      t          j        _        | j        	                                 | 
                    | j        j                   |                     | j        j                                        dd         d           dS )zY
        connectTCP gets called with default port if the service is not defined.
        thisbetternotexistr7   f  )defaultPortNrQ   )r7   rc   )r   r;   r9   r:   r0   r   r
   r5   r   rV   rW   r-   rX   rY   r@   r.   s    r   )test_SRVNoResultUnknownServiceDefaultPortz;SRVConnectorTests.test_SRVNoResultUnknownServiceDefaultPort   s     $0L L
 
 
 &=%>%>"   $,-...04466rr:<QRRRRRr   c                 ,   t          j        | j        dd| j                  | _        t                      t          j        _        | j        	                                 | 
                    | j        j                            t
                               dS )zR
        Connect fails on no result, unknown service and no default port.
        rb   r7   N)r   r;   r9   r:   r0   r   r
   r5   r   rV   
assertTruer-   checkr.   s    r   +test_SRVNoResultUnknownServiceNoDefaultPortz=SRVConnectorTests.test_SRVNoResultUnknownServiceNoDefaultPort   sy     $0L.t|
 
 &=%>%>"   +112IJJKKKKKr   c                 d   t          j        dt           j        t           j        dd          gt          j        _        | j                                         | 	                    | j
        j                   |                     | j        j                                        dd         d           dS )zU
        Test connectTCP gets called with fallback parameters on bad result.
        r7   rI   NrN   rQ   r]   )r   rS   CNAMErU   r
   r5   r   r0   rV   rW   r:   r-   rX   r9   rY   r@   r.   s    r   test_SRVBadResultz#SRVConnectorTests.test_SRVBadResult   s    
 L"BPT  &
"
 	   $,-...L#''))"1"-/M	
 	
 	
 	
 	
r   c                    t          j        ddd          }t          j        dt           j        t           j        d|          gt
          j        _        | j        	                                 | 
                    | j        j                   | j        j                            t                     |                     | j        j        g            dS )zH
        Test that connecting fails when no service is present.
        i     .rI   rJ   r7   rN   N)r   rR   rS   rT   rU   r
   r5   r   r0   rV   assertIsNotNoner:   r-   trapr   rX   r9   rY   rZ   s     r   test_SRVNoServicez#SRVConnectorTests.test_SRVNoService   s     .d4R@@@L"cf"g  &
"
 	   T\0111  0000"55555r   c                     g t           j        _        | j                                         t           j        j        d         d         }|                     d|           dS )zW
        The lookup name is a native string from service, protocol and domain.
        r   s   _xmpp-server._tcp.example.orgN)r
   r5   r   r0   rV   r   rX   )r   r   s     r   test_SRVLookupNamez$SRVConnectorTests.test_SRVLookupName   sU     &("   !)"-a094@@@@@r   c                     t          j        | j        dd| j                  | _        |                     d| j        j                   dS )zq
        L{srvconnect.SRVConnector} automatically encodes unicode domain using
        C{idna} encoding.
        zxmpp-clientu   échec.example.orgs   xn--chec-9oa.example.orgN)r   r;   r9   r:   r0   rX   domainr.   s    r   test_unicodeDomainz$SRVConnectorTests.test_unicodeDomain   sJ    
 $0L-)A4<
 
 	4dn6KLLLLLr   c                    t          j        dddd          }t          j        dddd          }||g| j        _        g | j        _        |                     t          d| j                   ddg| _        | j        	                                 | 
                    | j        d         d	           | j        	                                 | 
                    | j        d
         d           ddg| _        | j        	                                 | 
                    | j        d         d	           | j        	                                 | 
                    | j        d         d           dS )a  
        pickServer calculates running sum of weights and calls randint.

        This exercises the server selection algorithm specified in RFC 2782 by
        preparing fake L{random.randint} results and checking the values it was
        called with.
        
   rc   host1.example.org   host2.example.orgrandint   r   )r         )r   ry   rQ      )r   r{   N)r   rR   r0   orderedServersserversr8   randomrC   r=   
pickServerrX   r<   r   record1record2s      r   test_pickServerWeightsz(SRVConnectorTests.test_pickServerWeights   sK    .R/BCC.R/BCC)0'(:%!#

69dm444  "1g!!###)!,g666!!###)!,g666  "1g!!###)!,g666!!###)!,g66666r   c                    t          j        dddd          }t          j        dddd          }||g| j        _        g | j        _        |                     t          d| j                   ddg| _        | 	                    d| j        
                                           | 	                    d	| j        
                                           d
S )zR
        Two records with equal priorities compare on weight (ascending).
        ry   rc   rz   r{   r|   r}   r   rz   rc   r|   rc   Nr   rR   r0   r   r   r8   r   rC   r=   rX   r   r   s      r   test_pickServerSamePrioritiesz/SRVConnectorTests.test_pickServerSamePriorities  s     .R/BCC.R/BCC)0'(:%!#

69dm444 !f4dn6O6O6Q6QRRR4dn6O6O6Q6QRRRRRr   c                    t          j        dddd          }t          j        dddd          }||g| j        _        g | j        _        |                     t          d| j                   ddg| _        | 	                    d| j        
                                           | 	                    d	| j        
                                           d
S )zX
        Two records with differing priorities compare on priority (ascending).
        ry   r   rc   rz   r{   r|   r}   r   r   Nr   r   s      r   test_srvDifferentPrioritiesz-SRVConnectorTests.test_srvDifferentPriorities  s     .Q.ABB.Q.ABB)0'(:%!#

69dm444 !f4dn6O6O6Q6QRRR4dn6O6O6Q6QRRRRRr   N)r$   r%   r&   r'   r>   rC   rE   r[   r^   r`   re   ri   rl   rq   rt   rw   r   r   r   r(   r   r   r3   r3   @   s        ! ! !* * *&1 1 1X X X

 

 



 

 

S S S$L L L
 
 
 6 6 6 A A AM M M7 7 7BS S S S S S S Sr   r3   )r'   r   zope.interface.verifyr   twisted.internetr   r   twisted.internet.errorr   r   twisted.internet.interfacesr   twisted.internet.testingr	   twisted.namesr
   r   r   twisted.names.commonr   twisted.names.errorr   twisted.trialr   r   ClientFactoryr*   TestCaser3   r(   r   r   <module>r      sv   
  . . . . . . , , , , , , , , J J J J J J J J 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 - - - - - - , , , , , , " " " " " ", , , , ,< , , ,:	 	 	 	 	8) 	 	 	aS aS aS aS aS) aS aS aS aS aSr   