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   