o
    Óó·iÆ$  ã                   @   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   úhC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-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   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   ó   
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   N)r0   r   r1   Ú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é   )r9   r8   )r
   Ú
Record_SRVÚRRHeaderÚSRVÚINr	   r)   r   r&   ÚconnectÚassertIsNoner.   r#   ÚassertEqualr-   Ú
tcpClientsr3   ©r   rA   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.orgNrB   ©r+   r*   )r   r	   r)   r   r&   rG   rH   r.   r#   rI   r-   rJ   r3   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.
        NrB   rM   )r	   r)   r   r&   rG   rH   r.   r#   rI   r-   rJ   r3   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   r/   r-   r.   r&   r   r	   r)   r   rG   rH   r#   rI   rJ   r3   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   r/   r-   r.   r&   r   r	   r)   r   rG   Ú
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:   Nr?   rB   rM   )r
   rD   ZCNAMErF   r	   r)   r   r&   rG   rH   r.   r#   rI   r-   rJ   r3   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   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&   rG   r   rI   )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&   rI   Ú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   r]   rB   é   )r   r_   N)r
   rC   r&   ÚorderedServersÚserversr,   Úrandomr6   r1   Ú
pickServerrI   r0   ©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]   rQ   r^   r_   r`   ra   r   ©r^   rQ   ©r`   rQ   N©r
   rC   r&   rf   rg   r,   rh   r6   r1   rI   ri   rj   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).
        r]   r   rQ   r^   r_   r`   ra   rl   rm   Nrn   rj   r   r   r   Útest_srvDifferentPriorities  rp   z-SRVConnectorTests.test_srvDifferentPrioritiesN)r   r   r   r    r2   r6   r7   rL   rN   rO   rR   rU   rV   rX   rZ   r\   rk   ro   rq   r   r   r   r   r(   @   s"    


!r(   )r    rh   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   