
    Vh$                         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j2                        Z G d dej6                        Zy)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y)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                 .    || _         || _        g | _        y N)resultsfailurelookups)selfr   r   s      R/home/dcms/DCMS/lib/python3.12/site-packages/twisted/names/test/test_srvconnect.py__init__zFakeResolver.__init__$   s        c                     | j                   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<<#==$,,B!788::dll++r   )NN)__name__
__module____qualname____doc__r   r#    r   r   r   r      s    

,r   r   c                       e Zd ZdZd Zd Zy)DummyFactoryzL
    Dummy client factory that stores the reason of connection failure.
    c                     d | _         y r   reasonr   s    r   r   zDummyFactory.__init__9   s	    r   c                     || _         y 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                   j    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y)SRVConnectorTestsz/
    Tests for L{srvconnect.SRVConnector}.
    c                     | j                  t        dt                      t               | _        t               | _        t        j                  | j                  dd| j                        | _	        g | _
        g | _        y )NtheResolverxmpp-serverexample.org)patchr
   r   r	   reactorr*   factoryr   SRVConnectorr0   randIntArgsrandIntResultsr.   s    r   setUpzSRVConnectorTests.setUpE   s[    

6=,.9$#~#00LL-
  r   c                 r    | j                   j                  ||f       | j                  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   s3      	c
+""&&q))r   c                 8    t        t        | j                         y)zF
        L{srvconnect.SRVConnector} implements L{IConnector}.
        N)r   r   r0   r.   s    r   test_interfacez SRVConnectorTests.test_interfaceb   s     	Z0r   c                    t        j                  ddd      }t        j                  dt         j                  t         j                  d|      gt
        j                  _        | j                  j                          | j                  | j                  j                         | j                  | j                  j                  j!                         dd d	       y)
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LL"cff"g&
"
 	 $,,--.00446r:<VWr   c                 ,   t        d      t        j                  _        | j                  j                          | j                  | j                  j                         | j                  | j                  j                  j                         dd d       y)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   sm     &2.%A" $,,--.LL##'')"1-/M	
r   c                    g t         j                  _        | j                  j	                          | j                  | j                  j                         | j                  | j                  j                  j                         dd d       y)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   sh     &(" $,,--.LL##'')"1-/M	
r   c                    t        j                  | j                  dd| j                  d      | _        t               t        j                  _        | j                  j                          | j                  | j                  j                         | j                  | j                  j                  j                         dd d       y)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0LL LL
 &=%>" $,,--.00446r:<QRr   c                 D   t        j                  | j                  dd| j                        | _        t               t        j                  _        | j                  j                          | j                  | j                  j                  j                  t
                     y)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   sm     $00LL.t||
 &=%>" ++112IJKr   c                    t        j                  dt         j                  t         j                  dd      gt        j
                  _        | j                  j                          | j                  | j                  j                         | j                  | j                  j                  j                         dd d       y)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    
 LL"BPT&
"
 	 $,,--.LL##'')"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                         | j                  j                  j                  t               | j                  | j                   j"                  g        y)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@LL"cff"g&
"
 	 T\\001  000"5r   c                     g t         j                  _        | j                  j	                          t         j                  j
                  d   d   }| j                  d|       y)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   sN     &(" !!))"-a094@r   c                     t        j                  | j                  dd| j                        | _        | j                  d| j                  j                         y)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   sE    
 $00LL-)A4<<
 	4dnn6K6KLr   c                    t        j                  dddd      }t        j                  dddd      }||g| j                  _        g | j                  _        | j                  t        d| j                         ddg| _        | j                  j                          | j                  | j                  d   d	       | j                  j                          | j                  | j                  d
   d       ddg| _        | j                  j                          | j                  | j                  d   d	       | j                  j                          | j                  | j                  d   d       y)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   s'    ..R/BC..R/BC)0'(:%!#

69dmm4  "1g!!#))!,g6!!#))!,g6  "1g!!#))!,g6!!#))!,g6r   c                    t        j                  dddd      }t        j                  dddd      }||g| j                  _        g | j                  _        | j                  t        d| j                         ddg| _        | j                  d| j                  j                                | j                  d	| j                  j                                y
)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..R/BC)0'(:%!#

69dmm4 !f4dnn6O6O6QR4dnn6O6O6QRr   c                    t        j                  dddd      }t        j                  dddd      }||g| j                  _        g | j                  _        | j                  t        d| j                         ddg| _        | j                  d| j                  j                                | j                  d	| j                  j                                y
)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..Q.AB)0'(:%!#

69dmm4 !f4dnn6O6O6QR4dnn6O6O6Q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   @   sZ    !*&1X



S$L
 6 AM7B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      sa   
  . , J 2 2 1 1 - , ",< ,:	8)) 	aS)) aSr   