o
    b$                     @   s   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mZmZ ddlmZ dd	lmZ dd
lmZ ddlmZ G dd deZG dd dejZG dd dejZdS )z-
Test cases for L{twisted.names.srvconnect}.
    N)verifyObject)deferprotocol)DNSLookupErrorServiceNameUnknownError)
IConnector)clientdns
srvconnect)ResolverBase)DNSNameError)MemoryReactor)unittestc                   @   s"   e Zd ZdZdddZd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                 C   s   || _ || _g | _d S N)resultsfailurelookups)selfr   r    r   D/usr/lib/python3/dist-packages/twisted/names/test/test_srvconnect.py__init__$   s   
zFakeResolver.__init__c                 C   s<   | j ||||f | jdurt| jg g fS t| jS )z9
        Return the result or failure on lookup.
        N)r   appendr   r   succeedfailr   )r   nameclsqtypetimeoutr   r   r   _lookup)   s   
zFakeResolver._lookup)NN)__name__
__module____qualname____doc__r   r   r   r   r   r   r      s    
r   c                   @   s    e Zd ZdZdd Zdd ZdS )DummyFactoryzL
    Dummy client factory that stores the reason of connection failure.
    c                 C   s
   d | _ d S r   reasonr   r   r   r   r   9      
zDummyFactory.__init__c                 C   s
   || _ d S r   r%   )r   	connectorr&   r   r   r   clientConnectionFailed<   r(   z#DummyFactory.clientConnectionFailedN)r    r!   r"   r#   r   r*   r   r   r   r   r$   4   s    r$   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d Zdd Zdd Zdd Zdd Zd S )!SRVConnectorTestsz/
    Tests for L{srvconnect.SRVConnector}.
    c                 C   sF   |  tdt  t | _t | _t| jdd| j| _	g | _
g | _d S )NtheResolverxmpp-serverexample.org)patchr   r   r   reactorr$   factoryr
   SRVConnectorr)   randIntArgsrandIntResultsr'   r   r   r   setUpE   s   
zSRVConnectorTests.setUpc                 C   s   | 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   )r3   r   r4   pop)r   minmaxr   r   r   _randintO   s   zSRVConnectorTests._randintc                 C   s   t t| j dS )zF
        L{srvconnect.SRVConnector} implements L{IConnector}.
        N)r   r   r)   r'   r   r   r   test_interfaceb   s   z SRVConnectorTests.test_interfacec                 C   sf   t jdddd}t jdt jt jd|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ttlr.   r   typer   rA   payloadN   )r<   r;   )r	   
Record_SRVRRHeaderSRVINr   r,   r   r)   connectassertIsNoner1   r&   assertEqualr0   
tcpClientsr6   r   rD   r   r   r   test_SRVPresenth   s   
 z!SRVConnectorTests.test_SRVPresentc                 C   sD   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.orgNrE   r.   r-   )r   r   r,   r   r)   rJ   rK   r1   r&   rL   r0   rM   r6   r'   r   r   r   test_SRVNotPresentw   s   
z$SRVConnectorTests.test_SRVNotPresentc                 C   s@   g t j_| j  | | jj | | j	j
 dd d dS )zW
        Test connectTCP gets called with fallback parameters on empty result.
        NrE   rP   )r   r,   r   r)   rJ   rK   r1   r&   rL   r0   rM   r6   r'   r   r   r   test_SRVNoResult   s   
z"SRVConnectorTests.test_SRVNoResultc                 C   s\   t j| jdd| jd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.
        thisbetternotexistr.   f  )defaultPortNrE   )r.   rT   )r
   r2   r0   r1   r)   r   r   r,   r   rJ   rK   r&   rL   rM   r6   r'   r   r   r   )test_SRVNoResultUnknownServiceDefaultPort   s   

 z;SRVConnectorTests.test_SRVNoResultUnknownServiceDefaultPortc                 C   sB   t | 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.
        rS   r.   N)r
   r2   r0   r1   r)   r   r   r,   r   rJ   
assertTruer&   checkr'   r   r   r   +test_SRVNoResultUnknownServiceNoDefaultPort   s   

z=SRVConnectorTests.test_SRVNoResultUnknownServiceNoDefaultPortc                 C   sV   t jdt jt jd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.
        r.   r=   NrB   rE   rP   )r	   rG   CNAMErI   r   r,   r   r)   rJ   rK   r1   r&   rL   r0   rM   r6   r'   r   r   r   test_SRVBadResult   s   
z#SRVConnectorTests.test_SRVBadResultc                 C   sh   t jdddd}t jdt jt jd|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     .r=   r>   r.   rB   N)r	   rF   rG   rH   rI   r   r,   r   r)   rJ   assertIsNotNoner1   r&   trapr   rL   r0   rM   rN   r   r   r   test_SRVNoService   s   
z#SRVConnectorTests.test_SRVNoServicec                 C   s2   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   r,   r   r)   rJ   r   rL   )r   r   r   r   r   test_SRVLookupName   s   
z$SRVConnectorTests.test_SRVLookupNamec                 C   s*   t | 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
   r2   r0   r1   r)   rL   domainr'   r   r   r   test_unicodeDomain   s   z$SRVConnectorTests.test_unicodeDomainc                 C   s   t dddd}t 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.
        
   rT   host1.example.org   host2.example.orgrandint   r   )r         )r   rd   rE      )r   rf   N)r	   rF   r)   orderedServersserversr/   randomr9   r4   
pickServerrL   r3   r   record1record2r   r   r   test_pickServerWeights   s   





z(SRVConnectorTests.test_pickServerWeightsc                 C   sv   t dddd}t 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).
        rd   rT   re   rf   rg   rh   r   re   rT   rg   rT   Nr	   rF   r)   rm   rn   r/   ro   r9   r4   rL   rp   rq   r   r   r   test_pickServerSamePriorities     
z/SRVConnectorTests.test_pickServerSamePrioritiesc                 C   sv   t dddd}t 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).
        rd   r   rT   re   rf   rg   rh   ru   rv   Nrw   rq   r   r   r   test_srvDifferentPriorities  ry   z-SRVConnectorTests.test_srvDifferentPrioritiesN)r    r!   r"   r#   r5   r9   r:   rO   rQ   rR   rV   rY   r[   r_   ra   rc   rt   rx   rz   r   r   r   r   r+   @   s"    


!r+   )r#   ro   zope.interface.verifyr   twisted.internetr   r   twisted.internet.errorr   r   twisted.internet.interfacesr   twisted.namesr   r	   r
   twisted.names.commonr   twisted.names.errorr   twisted.test.proto_helpersr   twisted.trialr   r   ClientFactoryr$   TestCaser+   r   r   r   r   <module>   s   