U
     T‚gÆ$  ã                   @   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 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„ 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)ÚMemoryReactor)ÚclientÚdnsÚ
srvconnect)ÚResolverBase)ÚDNSNameError)Ú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   úF/tmp/pip-unpacked-wheel-1_7di8ic/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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   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!   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   )r/   r   r0   Ú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   r=   ÚpayloadNé   )r8   r7   )r
   Ú
Record_SRVÚRRHeaderÚSRVÚINr	   r(   r   r%   ÚconnectÚassertIsNoner-   r#   ÚassertEqualr,   Ú
tcpClientsr2   ©r   r@   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.orgNrA   ©r*   r)   )r   r	   r(   r   r%   rF   rG   r-   r#   rH   r,   rI   r2   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.
        NrA   rL   )r	   r(   r   r%   rF   rG   r-   r#   rH   r,   rI   r2   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defaultPortNrA   )r*   rP   )r   r.   r,   r-   r%   r   r	   r(   r   rF   rG   r#   rH   rI   r2   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.
        rO   r*   N)r   r.   r,   r-   r%   r   r	   r(   r   rF   Ú
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*   r9   Nr>   rA   rL   )r
   rC   ZCNAMErE   r	   r(   r   r%   rF   rG   r-   r#   rH   r,   rI   r2   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•  ó   .r9   r:   r*   r>   N)r
   rB   rC   rD   rE   r	   r(   r   r%   rF   ZassertIsNotNoner-   r#   Ztrapr   rH   r,   rI   rJ   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%   rF   r   rH   )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   r.   r,   r-   r%   rH   Ú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.
        é
   rP   úhost1.example.orgé   úhost2.example.orgÚrandinté   r   )r   é   é   )r   r\   rA   é   )r   r^   N)r
   rB   r%   ÚorderedServersÚserversr+   Úrandomr5   r0   Ú
pickServerrH   r/   ©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).
        r\   rP   r]   r^   r_   r`   r   ©r]   rP   ©r_   rP   N©r
   rB   r%   re   rf   r+   rg   r5   r0   rH   rh   ri   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).
        r\   r   rP   r]   r^   r_   r`   rk   rl   Nrm   ri   r   r   r   Útest_srvDifferentPriorities  s    
z-SRVConnectorTests.test_srvDifferentPrioritiesN)r   r   r   r    r1   r5   r6   rK   rM   rN   rQ   rT   rU   rW   rY   r[   rj   rn   ro   r   r   r   r   r'   @   s    


!r'   )r    rg   Zzope.interface.verifyr   Ztwisted.internetr   r   Ztwisted.internet.errorr   r   Ztwisted.internet.interfacesr   Ztwisted.internet.testingr   Ztwisted.namesr	   r
   r   Ztwisted.names.commonr   Ztwisted.names.errorr   Ztwisted.trialr   r   ZClientFactoryr!   ZTestCaser'   r   r   r   r   Ú<module>   s   