U
    
W['                     @   s   d Z ddlmZmZ ddlZddlm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}.
    )absolute_importdivisionN)verifyObject)unittest)deferprotocol)DNSLookupErrorServiceNameUnknownError)
IConnector)clientdns
srvconnect)ResolverBase)DNSNameError)nativeString)MemoryReactorc                   @   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k	r0t| jg g fS t| jS dS )z9
        Return the result or failure on lookup.
        N)r   appendr   r   ZsucceedZfailr   )r   nameclsZqtypeZ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   <   s    zDummyFactory.__init__c                 C   s
   || _ d S r   r$   )r   	connectorr%   r   r   r   clientConnectionFailed?   s    z#DummyFactory.clientConnectionFailedN)r   r    r!   r"   r   r(   r   r   r   r   r#   8   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   setUpI   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   )r1   r   r2   pop)r   minmaxr   r   r   _randintS   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_interfaceg   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<   Zporttargetttlr,   r   typer   r>   payloadN   )r:   r9   )r   
Record_SRVRRHeaderSRVINr   r*   r   r'   connectassertIsNoner/   r%   assertEqualr.   
tcpClientsr4   r   rA   r   r   r   test_SRVPresentn   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.
        r,   NrB   r,   r+   )r   r   r*   r   r'   rG   rH   r/   r%   rI   r.   rJ   r4   r&   r   r   r   test_SRVNotPresent~   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.
        NrB   rM   )r   r*   r   r'   rG   rH   r/   r%   rI   r.   rJ   r4   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  )ZdefaultPortNrB   )r,   rQ   )r   r0   r.   r/   r'   r	   r   r*   r   rG   rH   r%   rI   rJ   r4   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.
        rP   r,   N)r   r0   r.   r/   r'   r	   r   r*   r   rG   Z
assertTruer%   Z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;   Nr?   rB   rM   )r   rD   ZCNAMErF   r   r*   r   r'   rG   rH   r/   r%   rI   r.   rJ   r4   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,   r?   N)r   rC   rD   rE   rF   r   r*   r   r'   rG   ZassertIsNotNoner/   r%   Ztrapr   rI   r.   rJ   rK   r   r   r   test_SRVNoService   s     
z#SRVConnectorTests.test_SRVNoServicec                 C   s6   g t j_| j  t jjd d }| td| dS )zW
        The lookup name is a native string from service, protocol and domain.
        r   z_xmpp-server._tcp.example.orgN)r   r*   r   r'   rG   r   rI   r   )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.orgzxn--chec-9oa.example.orgN)r   r0   r.   r/   r'   rI   Z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.
        
   rQ   host1.example.org   host2.example.orgrandint   r   )r         )r   rZ   rB      )r   r\   N)r   rC   r'   orderedServersserversr-   randomr7   r2   
pickServerrI   r1   r   Zrecord1Z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).
        rZ   rQ   r[   r\   r]   r^   r   r[   rQ   r]   rQ   Nr   rC   r'   rc   rd   r-   re   r7   r2   rI   rf   rg   r   r   r   test_pickServerSamePriorities  s    
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).
        rZ   r   rQ   r[   r\   r]   r^   ri   rj   Nrk   rg   r   r   r   test_srvDifferentPriorities  s    
z-SRVConnectorTests.test_srvDifferentPrioritiesN)r   r    r!   r"   r3   r7   r8   rL   rN   rO   rR   rS   rT   rV   rX   rY   rh   rl   rm   r   r   r   r   r)   D   s    
"r)   )"r"   Z
__future__r   r   re   Zzope.interface.verifyr   Ztwisted.trialr   Ztwisted.internetr   r   Ztwisted.internet.errorr   r	   Ztwisted.internet.interfacesr
   Ztwisted.namesr   r   r   Ztwisted.names.commonr   Ztwisted.names.errorr   Ztwisted.python.compatr   Ztwisted.test.proto_helpersr   r   ZClientFactoryr#   ZTestCaser)   r   r   r   r   <module>   s   