U
    /Wj'°  ã                   @   s*  d Z ddlmZ ddlmZ ddlmZmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZ dd	lmZ dd
lmZmZmZmZ ddlmZ ddlmZ zddlmZ W n ek
rÄ   dZdZY n"X eZdZddlmZ ddl m!Z! dZ"G dd„ dej#ƒZ$G dd„ dej#ƒZ%G dd„ dej#ƒZ&G dd„ de'ƒZ(G dd„ dej#ƒZ)G dd„ dej#ƒZ*G d d!„ d!ej#ƒZ+G d"d#„ d#ej#ƒZ,G d$d%„ d%ej-ƒZ.G d&d'„ d'ej#ƒZ/G d(d)„ d)ej#ƒZ0G d*d+„ d+ƒZ1G d,d-„ d-ej2ƒZ3G d.d/„ d/ej2ƒZ4G d0d1„ d1ej#ƒZ5G d2d3„ d3ej#ƒZ6G d4d5„ d5ej#ƒZ7G d6d7„ d7eƒZ8dS )8z8
Tests for L{twisted.words.protocols.jabber.xmlstream}.
é    )ÚskipIf)ÚverifyObject)ÚdeferÚtask)ÚConnectionLost)ÚIProtocolFactory)Úfailure)Úproto_helpers)Úunittest)ÚerrorÚijabberÚjidÚ	xmlstream)Ú!GenericXmlStreamFactoryTestsMixin)Údomish)ÚsslN)TzSSL not available)FÚ )ÚClientTLSOptions)ÚCertificateOptionsúurn:ietf:params:xml:ns:xmpp-tlsc                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚHashPasswordTestsz.
    Tests for L{xmlstream.hashPassword}.
    c                 C   s   t  dd¡}|  d|¡ dS )zS
        The sid and secret are concatenated to calculate sha1 hex digest.
        Ú12345ÚsecretZ(99567ee91b2c7cabf607f10cb9f4a3634fa820e0N©r   ÚhashPasswordÚassertEqual©ÚselfÚhash© r   úrc:\users\psant\downloads\hotstarbackend\school\venv38\Lib\site-packages\twisted/words/test/test_jabberxmlstream.pyÚ
test_basic)   s    zHashPasswordTests.test_basicc                 C   s   |   ttjdd¡ dS )zB
        The session identifier must be a unicode object.
        s   Â¹2345r   N©ÚassertRaisesÚ	TypeErrorr   r   ©r   r   r   r    Útest_sidNotUnicode0   s    z$HashPasswordTests.test_sidNotUnicodec                 C   s   |   ttjdd¡ dS )z8
        The password must be a unicode object.
        r   s   secrÃ©tNr"   r%   r   r   r    Útest_passwordNotUnicode6   s    z)HashPasswordTests.test_passwordNotUnicodec                 C   s   t  dd¡}|  d|¡ dS )z\
        The concatenated sid and password must be encoded to UTF-8 before hashing.
        r   u   secrÃ©tZ(659bf88d8f8e179081f7f3b4a8e7d224652d2853Nr   r   r   r   r    Útest_unicodeSecret<   s    z$HashPasswordTests.test_unicodeSecretN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r!   r&   r'   r(   r   r   r   r    r   $   s
   r   c                   @   sp   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S )ÚIQTestszG
    Tests both IQ and the associated IIQResponseTracker callback.
    c                 C   sb   t  d¡}d|_t  |¡| _ t ¡ | _| jj| j _| j  	t
 ¡ ¡ | j  d¡ t  | j d¡| _d S )NZ	otherhostÚtestnszm<stream:stream xmlns:stream='http://etherx.jabber.org/streams' xmlns='testns' from='otherhost' version='1.0'>Úget)r   ÚConnectAuthenticatorÚ	namespaceÚ	XmlStreamr   ZClockÚclockÚ	callLaterZ
_callLaterÚmakeConnectionr	   ÚStringTransportÚdataReceivedÚIQÚiq)r   Úauthenticatorr   r   r    ÚsetUpI   s    

ÿzIQTests.setUpc                 C   s&   |   | jd d¡ |  | jd ¡ d S )NÚtyper/   Úid)r   r9   Ú
assertTruer%   r   r   r    Ú	testBasicV   s    zIQTests.testBasicc                 C   sR   | j j ¡  | j ¡  | jd  d¡}|  | j j ¡ d| d d| d g¡ d S )Nr=   zutf-8s   <iq type='get' id='s   '/>s   <iq id='s   ' type='get'/>)r   Ú	transportÚclearr9   ÚsendÚencodeÚassertInÚvalue)r   ZidBytesr   r   r    ÚtestSendZ   s    



þþzIQTests.testSendc                    s>   ‡ fdd„}ˆ j  ¡ }| |¡ ˆ j}| dˆ j d  ¡ |S )Nc                    s   ˆ   | d d¡ d S )Nr<   Úresult)r   )rG   r%   r   r    Úcbg   s    z&IQTests.testResultResponse.<locals>.cbú<iq type='result' id='%s'/>r=   )r9   rB   ÚaddCallbackr   r7   )r   rH   ÚdÚxsr   r%   r    ÚtestResultResponsef   s    

zIQTests.testResultResponsec                 C   s6   | j  ¡ }|  |tj¡ | j}| d| j d  ¡ |S )Nz<iq type='error' id='%s'/>r=   )r9   rB   ÚassertFailurer   ZStanzaErrorr   r7   ©r   rK   rL   r   r   r    ÚtestErrorResponseq   s
    
zIQTests.testErrorResponsec                    sD   ˆ j }t  |¡ ˆ  |j¡ ‡ fdd„}| d|d¡ | d¡ dS )zÇ
        Test that untracked iq responses don't trigger any action.

        Untracked means that the id of the incoming response iq is not
        in the stream's C{iqDeferreds} dictionary.
        c                    s   ˆ   t| ddƒ¡ d S )NZhandledF)ÚassertFalseÚgetattr)r9   r%   r   r    rH   ‰   s    z*IQTests.testNonTrackedResponse.<locals>.cbz/iqéÿÿÿÿz<iq type='result' id='test'/>N)r   ZupgradeWithIQResponseTrackerrQ   ÚiqDeferredsÚaddObserverr7   )r   rL   rH   r   r%   r    ÚtestNonTrackedResponsey   s    
zIQTests.testNonTrackedResponsec                 C   s<   | j  ¡ }| j}| d| j d  ¡ |  | j d |j¡ |S )z¦
        Test if the deferred associated with an iq request is removed
        from the list kept in the L{XmlStream} object after it has
        been fired.
        rI   r=   )r9   rB   r   r7   ÚassertNotInrT   rO   r   r   r    ÚtestCleanup‘   s
    
zIQTests.testCleanupc                 C   s*   | j  ¡ }| j}| d¡ |  |t¡ |S )z‰
        Test if deferreds for iq's that haven't yet received a response
        have their errback called on stream disconnect.
        úClosed by peer)r9   rB   r   ÚconnectionLostrN   r   rO   r   r   r    ÚtestDisconnectCleanupž   s
    

zIQTests.testDisconnectCleanupc                    s2   ‡ ‡fdd„‰ ˆj  ¡ }| ˆ ¡ ˆj d¡ |S )z~
        Test to make sure the errbacks cannot cause the iteration of the
        iqDeferreds to blow up in our face.
        c                    s   t  ˆj ¡ ¡ }| ˆ ¡ d S ©N)r   r8   rB   Ú
addErrback)r   rK   ©Úebr   r   r    r_   °   s    z'IQTests.testNoModifyingDict.<locals>.ebrY   )r9   rB   r]   r   rZ   ©r   rK   r   r^   r    ÚtestNoModifyingDictª   s
    

zIQTests.testNoModifyingDictc                 C   sP   d| j _| j  ¡ }|  |tj¡ | j ddg¡ |  | jj	¡ |  | jj
¡ |S )zK
        Test that an iq request with a defined timeout times out.
        é<   é   )r9   ÚtimeoutrB   rN   r   ÚTimeoutErrorr3   ÚpumprQ   ÚcallsrT   r`   r   r   r    ÚtestRequestTimingOut¹   s    
zIQTests.testRequestTimingOutc                 C   sR   d| j _| j  ¡ }| j d| jjd| j d  ¡ | j ddg¡ |  | jj	¡ |S )zš
        Test that an iq request with a defined timeout does not time out
        when a response was received before the timeout period elapsed.
        rb   rc   rI   r=   )
r9   rd   rB   r3   r4   r   r7   rf   rQ   rg   r`   r   r   r    ÚtestRequestNotTimingOutÆ   s    
ýzIQTests.testRequestNotTimingOutc                 C   s@   d| j _| j  ¡ }| j}| d¡ |  |t¡ |  | jj	¡ |S )z|
        Test if timeouts for iq's that haven't yet received a response
        are cancelled on stream disconnect.
        rb   rY   )
r9   rd   rB   r   rZ   rN   r   rQ   r3   rg   rO   r   r   r    Ú!testDisconnectTimeoutCancellationÖ   s    

z)IQTests.testDisconnectTimeoutCancellationN)r)   r*   r+   r,   r;   r?   rF   rM   rP   rV   rX   r[   ra   rh   ri   rj   r   r   r   r    r-   D   s   r-   c                   @   sŒ   e 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d „ Zd!S )"ÚXmlStreamTestsc                 C   s
   d| _ d S ©NT)ÚgotStreamStart©r   Úobjr   r   r    ÚonStreamStartç   s    zXmlStreamTests.onStreamStartc                 C   s
   d| _ d S rl   )ÚgotStreamEndrn   r   r   r    ÚonStreamEndê   s    zXmlStreamTests.onStreamEndc                 C   s
   d| _ d S rl   )ÚgotStreamErrorrn   r   r   r    ÚonStreamErrorí   s    zXmlStreamTests.onStreamErrorc                 C   sv   d| _ d| _d| _t t ¡ ¡}| d| j¡ | d| j¡ | d| j	¡ | 
t ¡ ¡ ||j_d|_d|_|| _dS )z9
        Set up XmlStream and several observers.
        Fz//event/stream/startú//event/stream/endz//event/stream/errorr.   ©rc   r   N)rm   rq   rs   r   r2   ÚAuthenticatorrU   rp   rr   rt   r5   r	   Z StringTransportWithDisconnectionr@   Úprotocolr1   Úversion©r   rL   r   r   r    r;   ð   s    zXmlStreamTests.setUpc                 C   sh   | j }| ¡  | j j ¡ dd…  d¡}|  d|¡ |  d|¡ |  d|¡ |  d|¡ |  |j¡ dS )	z>
        Basic test on the header sent by sendHeader.
        r   rS   ó    ó   <stream:streamó/   xmlns:stream='http://etherx.jabber.org/streams'ó   xmlns='testns'ó   version='1.0'N)r   Ú
sendHeaderr@   rE   ÚsplitrD   r>   Ú_headerSent©r   rL   ZsplitHeaderr   r   r    Útest_sendHeaderBasic  s    z#XmlStreamTests.test_sendHeaderBasicc                 C   s~   | j }d|jd< | ¡  | j j ¡ dd…  d¡}|  d|¡ |  d|¡ |  d|¡ |  d	|¡ |  d
|¡ |  |j¡ dS )z=
        Test for additional namespace declarations.
        Údbzjabber:server:dialbackr   rS   r{   r|   r}   s!   xmlns:db='jabber:server:dialback'r~   r   N)	r   Úprefixesr€   r@   rE   r�   rD   r>   r‚   rƒ   r   r   r    Ú#test_sendHeaderAdditionalNamespaces  s    
z2XmlStreamTests.test_sendHeaderAdditionalNamespacesc                 C   s`   | j }t d¡|_t d¡|_d|_| ¡  |j ¡ dd…  	d¡}|  
d|¡ |  
d|¡ d	S )
z;
        Test addressing when initiating a stream.
        ÚthisHostÚ	otherHostTr   rS   r{   ó   to='otherhost'ó   from='thishost'N)r   r   ÚJIDÚ
thisEntityÚotherEntityÚ
initiatingr€   r@   rE   r�   rD   rƒ   r   r   r    Útest_sendHeaderInitiating  s    z(XmlStreamTests.test_sendHeaderInitiatingc                 C   sr   | j }t d¡|_t d¡|_d|_d|_| ¡  |j 	¡ dd…  
d¡}|  d|¡ |  d	|¡ |  d
|¡ dS )z:
        Test addressing when receiving a stream.
        rˆ   r‰   FZ	session01r   rS   r{   rŠ   r‹   s   id='session01'N)r   r   rŒ   r�   rŽ   r�   Úsidr€   r@   rE   r�   rD   rƒ   r   r   r    Útest_sendHeaderReceiving*  s    z'XmlStreamTests.test_sendHeaderReceivingc                 C   s6   | j }| d¡ | d¡ |  | j¡ |  | j¡ dS )z>
        Test events when a stream error is received.
        ú�<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' from='example.com' id='12345' version='1.0'>z<stream:error/>N)r   r7   r>   rs   rq   rz   r   r   r    Útest_receiveStreamError9  s    ÿ
z&XmlStreamTests.test_receiveStreamErrorc                 C   sP   | j }d|_| ¡  |j ¡  | t d¡¡ |  d|j 	¡ ¡ |  
| j¡ dS )z“
        Test sendStreamError on an initiating xmlstream with a header sent.

        An error should be sent out and the connection lost.
        Túversion-unsupportedó    N©r   r�   r€   r@   rA   ÚsendStreamErrorr   ÚStreamErrorÚassertNotEqualrE   r>   rq   rz   r   r   r    Útest_sendStreamErrorInitiatingG  s    
z-XmlStreamTests.test_sendStreamErrorInitiatingc                 C   sT   | j }d|_|j ¡  | t d¡¡ |  |j¡ |  	d|j 
¡ ¡ |  | j¡ dS )zø
        Test sendStreamError on an initiating xmlstream without having sent a
        header.

        In this case, no header should be generated. Also, the error should
        not be sent out on the stream. Just closing the connection.
        Tr•   r–   N)r   r�   r@   rA   r˜   r   r™   Ú	assertNotr‚   r   rE   r>   rq   rz   r   r   r    Ú&test_sendStreamErrorInitiatingNoHeaderU  s    
z5XmlStreamTests.test_sendStreamErrorInitiatingNoHeaderc                 C   sP   | j }d|_| ¡  |j ¡  | t d¡¡ |  d|j 	¡ ¡ |  
| j¡ dS )z‘
        Test sendStreamError on a receiving xmlstream with a header sent.

        An error should be sent out and the connection lost.
        Fr•   r–   Nr—   rz   r   r   r    Útest_sendStreamErrorReceivinge  s    
z,XmlStreamTests.test_sendStreamErrorReceivingc                 C   sT   | j }d|_|j ¡  | t d¡¡ |  |j¡ |  	d|j 
¡ ¡ |  | j¡ dS )z÷
        Test sendStreamError on a receiving xmlstream without having sent a
        header.

        In this case, a header should be generated. Then, the error should
        be sent out on the stream followed by closing the connection.
        Fr•   r–   N)r   r�   r@   rA   r˜   r   r™   r>   r‚   rš   rE   rq   rz   r   r   r    Ú%test_sendStreamErrorReceivingNoHeaders  s    
z4XmlStreamTests.test_sendStreamErrorReceivingNoHeaderc                 C   s:   | j }| ¡  |j}| ¡  |  ||j¡ |  |j¡ dS )zE
        Test resetting the XML stream to start a new layer.
        N)r   r€   ÚstreamÚresetrš   rœ   r‚   )r   rL   r    r   r   r    Ú
test_resetƒ  s    zXmlStreamTests.test_resetc                 C   s†   | j }| d¡ |  |j ¡ d¡ |j ¡  t d¡}| |¡ |  |j ¡ d¡ |j ¡  t d¡}| |¡ |  |j ¡ d¡ dS )z:
        Test send with various types of objects.
        ú<presence/>ó   <presence/>)r.   Zpresence)z http://etherx.jabber.org/streamsÚfeaturess   <stream:features/>N)r   rB   r   r@   rE   rA   r   ÚElement)r   rL   Úelr   r   r    Ú	test_sendŽ  s    






zXmlStreamTests.test_sendc                    sŠ   g ‰g ‰g ‰ G ‡ ‡‡fdd„dƒ}|ƒ }t  |¡}|  |gˆ ¡ | ¡  |  dgˆ¡ | d¡ |  dtˆƒ¡ | ¡  |  dgˆ¡ dS )zM
        Test that the associated authenticator is correctly called.
        c                       s0   e Zd Z‡fdd„Z‡fdd„Z‡ fdd„ZdS )z<XmlStreamTests.test_authenticator.<locals>.TestAuthenticatorc                    s   ˆ   d ¡ d S r\   ©Úappendr%   )ÚconnectionMadeCallsr   r    ÚconnectionMade©  s    zKXmlStreamTests.test_authenticator.<locals>.TestAuthenticator.connectionMadec                    s   ˆ   |¡ d S r\   r©   )r   ZrootElement)ÚstreamStartedCallsr   r    ÚstreamStarted¬  s    zJXmlStreamTests.test_authenticator.<locals>.TestAuthenticator.streamStartedc                    s   ˆ   |¡ d S r\   r©   rz   )ÚassociateWithStreamCallsr   r    ÚassociateWithStream¯  s    zPXmlStreamTests.test_authenticator.<locals>.TestAuthenticator.associateWithStreamN)r)   r*   r+   r¬   r®   r°   r   ©r¯   r«   r­   r   r    ÚTestAuthenticator¨  s   r²   Nús<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' from='example.com' id='12345'>rc   )r   r2   r   r¬   r7   Úlenr¡   )r   r²   ÚarL   r   r±   r    Útest_authenticator   s    

ÿz!XmlStreamTests.test_authenticatorN)r)   r*   r+   rp   rr   rt   r;   r„   r‡   r�   r’   r”   r›   r�   rž   rŸ   r¢   r¨   r¶   r   r   r   r    rk   æ   s    rk   c                   @   s   e Zd ZdS )Ú	TestErrorN)r)   r*   r+   r   r   r   r    r·   Á  s   r·   c                   @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚAuthenticatorTestsc                 C   s   t  ¡ | _t  | j¡| _ d S r\   )r   rw   r:   r2   r%   r   r   r    r;   Æ  s    
zAuthenticatorTests.setUpc                 C   sh   | j }| t ¡ ¡ | d¡ |  d|j¡ |  d|j¡ |  d|j	¡ |  d|j
¡ |  d|j¡ dS )úe
        Test streamStart to fill the appropriate attributes from the
        stream header.
        ú’<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' from='example.org' to='example.com' id='12345' version='1.0'>rv   NÚinvalid)r   r5   r	   r6   r7   r   ry   ÚassertIdenticalr‘   r1   rŽ   r�   rz   r   r   r    Útest_streamStartÊ  s    ÿz#AuthenticatorTests.test_streamStartc                 C   s0   | j }| t ¡ ¡ | d¡ |  d|j¡ dS )z
        Test streamStart to fill the appropriate attributes from the
        stream header for a pre-XMPP-1.0 header.
        r³   ©r   r   N©r   r5   r	   r6   r7   r   ry   rz   r   r   r    Útest_streamStartLegacyÝ  s    ÿz)AuthenticatorTests.test_streamStartLegacyc                 C   s0   | j }| t ¡ ¡ | d¡ |  d|j¡ dS )z‡
        Test streamStart to fill the appropriate attributes from the
        stream header for a version with only one digit.
        z<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' from='example.com' id='12345' version='1'>r¾   Nr¿   rz   r   r   r    Útest_streamBadVersionOneDigitë  s    ÿz0AuthenticatorTests.test_streamBadVersionOneDigitc                 C   s0   | j }| t ¡ ¡ | d¡ |  d|j¡ dS )z}
        Test streamStart to fill the appropriate attributes from the
        stream header for a malformed version.
        z‚<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' from='example.com' id='12345' version='blah'>r¾   Nr¿   rz   r   r   r    Útest_streamBadVersionNoNumberù  s    ÿz0AuthenticatorTests.test_streamBadVersionNoNumberN)r)   r*   r+   r;   r½   rÀ   rÁ   rÂ   r   r   r   r    r¸   Å  s
   r¸   c                   @   s<   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )ÚConnectAuthenticatorTestsc                 C   sJ   d| _ d | _t d¡| _t | j¡| _| j d| j¡ | j d| j¡ d S )NFr‰   ú//event/stream/authdú//event/xmpp/initfailed)	ÚgotAuthenticatedÚinitFailurer   r0   r:   r2   rU   ÚonAuthenticatedÚonInitFailedr%   r   r   r    r;   	  s    zConnectAuthenticatorTests.setUpc                 C   s
   d| _ d S rl   )rÆ   rn   r   r   r    rÈ     s    z)ConnectAuthenticatorTests.onAuthenticatedc                 C   s
   || _ d S r\   )rÇ   )r   r   r   r   r    rÉ     s    z&ConnectAuthenticatorTests.onInitFailedc                 C   sH   G dd„ dƒ}|ƒ }|g| j _| j ¡  |  g | j j¡ |  | j¡ dS )zG
        Test successful completion of an initialization step.
        c                   @   s   e Zd Zdd„ ZdS )z9ConnectAuthenticatorTests.testSucces.<locals>.Initializerc                 S   s   d S r\   r   r%   r   r   r    Ú
initialize  s    zDConnectAuthenticatorTests.testSucces.<locals>.Initializer.initializeN©r)   r*   r+   rÊ   r   r   r   r    ÚInitializer  s   rÌ   N)r   Úinitializersr:   ÚinitializeStreamr   r>   rÆ   ©r   rÌ   Úinitr   r   r    Ú
testSucces  s    

z$ConnectAuthenticatorTests.testSuccesc                 C   sj   G dd„ dƒ}|ƒ }|g| j _| j ¡  |  |g| j j¡ |  | j¡ |  d| j¡ |  	| j 
t¡¡ dS )z9
        Test failure of an initialization step.
        c                   @   s   e Zd Zdd„ ZdS )z:ConnectAuthenticatorTests.testFailure.<locals>.Initializerc                 S   s   t ‚d S r\   )r·   r%   r   r   r    rÊ   -  s    zEConnectAuthenticatorTests.testFailure.<locals>.Initializer.initializeNrË   r   r   r   r    rÌ   ,  s   rÌ   N)r   rÍ   r:   rÎ   r   rQ   rÆ   ÚassertNotIdenticalrÇ   r>   Úcheckr·   rÏ   r   r   r    ÚtestFailure'  s    

z%ConnectAuthenticatorTests.testFailurec                 C   s¢   d| j _| j}| t ¡ ¡ | d¡ |  d|j¡ |  d|j	¡ |  d|j¡ |  d|j
j¡ |  d|j¡ |  | j¡ | d¡ |  d|j¡ |  | j¡ dS )	r¹   r.   z’<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' from='example.com' to='example.org' id='12345' version='1.0'>rv   r   úexample.comNz9<stream:features><test xmlns='testns'/></stream:features>©r.   Útest)r:   r1   r   r5   r	   r6   r7   r   ry   r‘   rŽ   Úhostr¼   r�   rœ   rÆ   rD   r¥   r>   rz   r   r   r    r½   9  s"    ÿÿz*ConnectAuthenticatorTests.test_streamStartN)	r)   r*   r+   r;   rÈ   rÉ   rÑ   rÔ   r½   r   r   r   r    rÃ     s   rÃ   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚListenAuthenticatorTestsz4
    Tests for L{xmlstream.ListenAuthenticator}
    c                 C   s   t  ¡ | _t  | j¡| _ d S r\   )r   ZListenAuthenticatorr:   r2   r%   r   r   r    r;   Y  s    
zListenAuthenticatorTests.setUpc                 C   s†   | j }| t ¡ ¡ |  d|j¡ | d¡ |  d|j¡ |  	d|j¡ |  
d|j¡ |  d|j¡ |  d|j¡ |  d|jj¡ dS )r¹   Nrº   rv   r   újabber:clientrÕ   )r   r5   r	   r6   r¼   r‘   r7   r   ry   rÒ   rš   r1   rŽ   r�   rØ   rz   r   r   r    r½   ]  s    ÿz)ListenAuthenticatorTests.test_streamStartc                 C   s0   | j }| t ¡ ¡ | d¡ |  |jt¡ dS )zD
        The generated session id must be a unicode object.
        rº   N)r   r5   r	   r6   r7   ÚassertIsInstancer‘   Ústrrz   r   r   r    Ú test_streamStartUnicodeSessionIDr  s    ÿz9ListenAuthenticatorTests.test_streamStartUnicodeSessionIDN)r)   r*   r+   r,   r;   r½   rÝ   r   r   r   r    rÙ   T  s   rÙ   c                   @   s|   e Zd Zdd„ Zdd„ Zdd„ ZeeŽ dd„ ƒZeeŽ d	d
„ ƒZ	eeŽ dd„ ƒZ
dd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚTLSInitiatingInitializerTestsc                 C   sb   g | _ g | _tj| _t d¡| _t | j¡| _| j j| j_	| j 
¡  | j d¡ t | j¡| _d S )NrÕ   r“   )ÚoutputÚdoner   r   ÚsavedSSLr0   r:   r2   rª   rB   r¬   r7   ÚTLSInitiatingInitializerrÐ   r%   r   r   r    r;   ‚  s    
ÿz#TLSInitiatingInitializerTests.setUpc                 C   s   | j t_d S r\   )rá   r   r   r%   r   r   r    ÚtearDown“  s    z&TLSInitiatingInitializerTests.tearDownc                 C   s$   t j| j dd�| _|  | jj¡ dS )z>
        Passing required sets the instance variable.
        T)ÚrequiredN)r   râ   rÐ   r>   rä   r%   r   r   r    Útest_initRequired–  s    z/TLSInitiatingInitializerTests.test_initRequiredc                    s¸   t  ¡ ˆ j_‡ fdd„ˆ jj_‡ fdd„ˆ j_‡ fdd„ˆ j_ˆ j ¡ }| 	ˆ j
tj¡ ˆ  
dtˆ jƒ¡ ˆ jd }ˆ  
d|j¡ ˆ  
t|j¡ ˆ j dt ¡ ˆ  
d	d
dgˆ j¡ |S )zN
        When TLS is wanted and SSL available, StartTLS is initiated.
        c                    s   ˆ j  d¡S ©NÚTLS©rà   rª   )Úctxr%   r   r    Ú<lambda>£  r–   zDTLSInitiatingInitializerTests.test_wantedSupported.<locals>.<lambda>c                      s   ˆ j  d¡S ©Nr¡   rè   r   r%   r   r    rê   ¤  r–   c                      s   ˆ j  d¡S ©NÚheaderrè   r   r%   r   r    rê   ¥  r–   é   rc   Ústarttlsú<proceed xmlns='%s'/>rç   r¡   rí   )r	   r6   r   r@   ÚstartTLSr¡   r€   rÐ   ÚstartrJ   r   ÚResetr´   rß   ÚnameÚNS_XMPP_TLSÚurir7   rà   )r   rK   rï   r   r%   r    Útest_wantedSupported�  s    

z2TLSInitiatingInitializerTests.test_wantedSupportedc                    st   ‡ fdd„}t  ¡ ˆ j_|ˆ jj_‡ fdd„ˆ j_‡ fdd„ˆ j_ˆ j ¡ }ˆ j 	dt
 ¡ ˆ  ddd	gˆ j¡ |S )
z:
        The server certificate will be verified.
        c                    s*   ˆ   | t¡ ˆ  | jd¡ ˆ j d¡ d S )NrÕ   rç   )rÛ   r   r   Z	_hostnamerà   rª   ©ÚcontextFactoryr%   r   r    ÚfakeStartTLS¸  s    zJTLSInitiatingInitializerTests.test_certificateVerify.<locals>.fakeStartTLSc                      s   ˆ j  d¡S rë   rè   r   r%   r   r    rê   ¿  r–   zFTLSInitiatingInitializerTests.test_certificateVerify.<locals>.<lambda>c                      s   ˆ j  d¡S rì   rè   r   r%   r   r    rê   À  r–   rð   rç   r¡   rí   )r	   r6   r   r@   rñ   r¡   r€   rÐ   rò   r7   rõ   r   rà   ©r   rú   rK   r   r%   r    Útest_certificateVerify²  s    

z4TLSInitiatingInitializerTests.test_certificateVerifyc                    s–   t ƒ ‰ tjˆjˆ d�ˆ_ˆ ˆj_‡ ‡fdd„}t ¡ ˆj_|ˆjj_‡fdd„ˆj_	‡fdd„ˆj_
ˆj ¡ }ˆj dt ¡ ˆ dd	d
gˆj¡ |S )zH
        A custom contextFactory is passed through to startTLS.
        )ZconfigurationForTLSc                    s   ˆ  ˆ | ¡ ˆj d¡ d S ræ   )ZassertIsrà   rª   rø   ©ré   r   r   r    rú   Ó  s    zQTLSInitiatingInitializerTests.test_certificateVerifyContext.<locals>.fakeStartTLSc                      s   ˆ j  d¡S rë   rè   r   r%   r   r    rê   Ù  r–   zMTLSInitiatingInitializerTests.test_certificateVerifyContext.<locals>.<lambda>c                      s   ˆ j  d¡S rì   rè   r   r%   r   r    rê   Ú  r–   rð   rç   r¡   rí   )r   r   râ   rÐ   rù   r	   r6   r@   rñ   r¡   r€   rò   r7   rõ   r   rà   rû   r   rý   r    Útest_certificateVerifyContextÇ  s     ÿ

z;TLSInitiatingInitializerTests.test_certificateVerifyContextc                 C   s<   dt _d| j_| j ¡ }| | jd¡ |  dt| jƒ¡ |S )zX
        No StartTLS is initiated when wanted, not required, SSL not available.
        NFrc   )	r   r   rÐ   rä   rò   rJ   r   r´   rß   r`   r   r   r    Ú"test_wantedNotSupportedNotRequiredá  s    
z@TLSInitiatingInitializerTests.test_wantedNotSupportedNotRequiredc                 C   s<   dt _d| j_| j ¡ }|  |t j¡ |  dt| j	ƒ¡ |S )zS
        TLSNotSupported is raised when TLS is required but not available.
        NTrc   )
r   r   rÐ   rä   rò   rN   ZTLSNotSupportedr   r´   rß   r`   r   r   r    Útest_wantedNotSupportedRequiredî  s    
z=TLSInitiatingInitializerTests.test_wantedNotSupportedRequiredc                 C   s^   t  d¡}| d¡ |j|jf|i| j_d| j_| j 	¡ }|  
dt| jƒ¡ |  |tj¡ |S )zW
        TLSRequired is raised when TLS is not wanted, but required by server.
        ©r   rï   rä   Frc   )r   r¦   Z
addElementrö   rô   r   r¥   rÐ   Úwantedrò   r   r´   rß   rN   ZTLSRequired©r   ZtlsrK   r   r   r    Útest_notWantedRequiredû  s    


z4TLSInitiatingInitializerTests.test_notWantedRequiredc                 C   s\   t  d¡}|j|jf|i| j_d| j_d| j_| j 	¡ }| 
| jd¡ |  dt| jƒ¡ |S )zL
        No StartTLS is initiated when not wanted and not required.
        r  FNrc   )r   r¦   rö   rô   r   r¥   rÐ   r  rä   rò   rJ   r   r´   rß   r  r   r   r    Útest_notWantedNotRequired
  s    

z7TLSInitiatingInitializerTests.test_notWantedNotRequiredc                 C   s2   dt _| j ¡ }|  |t j¡ | j  dt ¡ |S )zN
        TLSFailed is raised when the server responds with a failure.
        rc   z<failure xmlns='%s'/>)r   r   rÐ   rò   rN   Z	TLSFailedr7   rõ   r`   r   r   r    Útest_failed  s
    
z)TLSInitiatingInitializerTests.test_failedN)r)   r*   r+   r;   rã   rå   r   ÚskipWhenNoSSLr÷   rü   rþ   rÿ   r   r  r  r  r   r   r   r    rÞ   �  s   


rÞ   c                   @   s   e Zd ZdZdd„ ZdS )ÚTestFeatureInitializerrÖ   c                 C   s
   t  d ¡S r\   )r   Zsucceedr%   r   r   r    rò   *  s    zTestFeatureInitializer.startN)r)   r*   r+   Úfeaturerò   r   r   r   r    r  '  s   r  c                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
Ú%BaseFeatureInitiatingInitializerTestsc                 C   s    t  t  ¡ ¡| _ t| j ƒ| _d S r\   )r   r2   rw   r  rÐ   r%   r   r   r    r;   /  s    z+BaseFeatureInitiatingInitializerTests.setUpc                 C   s$   | j jt | j j¡i| j_| j  ¡ S )zW
        Test that an advertized feature results in successful initialization.
        )rÐ   r	  r   r¦   r   r¥   rÊ   r%   r   r   r    ÚtestAdvertized3  s    z4BaseFeatureInitiatingInitializerTests.testAdvertizedc                 C   s   d| j _|  tj| j j¡ dS )z€
        Test that when the feature is not advertized, but required by the
        initializer, an exception is raised.
        TN)rÐ   rä   r#   r   ZFeatureNotAdvertizedrÊ   r%   r   r   r    ÚtestNotAdvertizedRequired:  s    z?BaseFeatureInitiatingInitializerTests.testNotAdvertizedRequiredc                 C   s   d| j _|  d| j  ¡ ¡ dS )z�
        Test that when the feature is not advertized, and not required by the
        initializer, the initializer silently succeeds.
        FN)rÐ   rä   r¼   rÊ   r%   r   r   r    ÚtestNotAdvertizedNotRequiredB  s    zBBaseFeatureInitiatingInitializerTests.testNotAdvertizedNotRequiredN)r)   r*   r+   r;   r  r  r  r   r   r   r    r
  .  s   r
  c                   @   s<   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )ÚToResponseTestsc                 C   s†   t  d¡}d|d< d|d< d|d< d|d	< t |d
¡}|  ||¡ |  |d d¡ |  |d d¡ |  |d d
¡ |  |d	 d¡ dS )zS
        Test that a response stanza is generated with addressing swapped.
        ©rÚ   r9   r/   r<   úuser1@example.comÚtoúuser2@example.com/resourceÚfromZstanza1r=   rG   N)r   r¦   r   Ú
toResponserÒ   r   ©r   ZstanzaÚresponser   r   r    Útest_toResponseL  s    
zToResponseTests.test_toResponsec                 C   sH   t  d¡}d|d< d|d< t |¡}|  |d d¡ |  | d¡¡ dS )zY
        Test that a response is generated from a stanza without a from address.
        r  r/   r<   r  r  r  N)r   r¦   r   r  r   rQ   ÚhasAttributer  r   r   r    Útest_toResponseNoFrom\  s    

z%ToResponseTests.test_toResponseNoFromc                 C   sH   t  d¡}d|d< d|d< t |¡}|  | d¡¡ |  |d d¡ dS )zW
        Test that a response is generated from a stanza without a to address.
        r  r/   r<   r  r  r  N)r   r¦   r   r  rQ   r  r   r  r   r   r    Útest_toResponseNoTog  s    

z#ToResponseTests.test_toResponseNoToc                 C   s@   t  d¡}d|d< t |¡}|  | d¡¡ |  | d¡¡ dS )zY
        Test that a response is generated from a stanza without any addressing.
        ©rÚ   ÚmessageZchatr<   r  r  N©r   r¦   r   r  rQ   r  r  r   r   r    Útest_toResponseNoAddressingr  s
    

z+ToResponseTests.test_toResponseNoAddressingc                 C   s(   t  d¡}t |¡}|  | d¡¡ dS )zP
        Test that a proper response is generated without id attribute.
        r  r=   Nr  r  r   r   r    Ú	test_noID|  s    

zToResponseTests.test_noIDc                 C   s(   t  d¡}t |¡}|  | d¡¡ dS )zR
        Test that a proper response is generated without type attribute.
        r  r<   Nr  r  r   r   r    Útest_noType„  s    

zToResponseTests.test_noTypeN)	r)   r*   r+   r  r  r  r  r  r   r   r   r   r    r  K  s   
r  c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚDummyFactoryzJ
    Dummy XmlStream factory that only registers bootstrap observers.
    c                 C   s
   i | _ d S r\   ©Ú	callbacksr%   r   r   r    Ú__init__’  s    zDummyFactory.__init__c                 C   s   || j |< d S r\   r"  )r   ÚeventÚcallbackr   r   r    ÚaddBootstrap•  s    zDummyFactory.addBootstrapN)r)   r*   r+   r,   r$  r'  r   r   r   r    r!  �  s   r!  c                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚDummyXMPPHandlerzO
    Dummy XMPP subprotocol handler to count the methods are called on it.
    c                 C   s   d| _ d| _d| _d S )Nr   )ÚdoneMadeÚdoneInitializedÚdoneLostr%   r   r   r    r$  ž  s    zDummyXMPPHandler.__init__c                 C   s   |   ¡  d S r\   )r¬   rz   r   r   r    r5   £  s    zDummyXMPPHandler.makeConnectionc                 C   s   |  j d7  _ d S ©Nrc   ©r)  r%   r   r   r    r¬   ¦  s    zDummyXMPPHandler.connectionMadec                 C   s   |  j d7  _ d S r,  )r*  r%   r   r   r    ÚconnectionInitialized©  s    z&DummyXMPPHandler.connectionInitializedc                 C   s   |  j d7  _ d S r,  )r+  ©r   Úreasonr   r   r    rZ   ¬  s    zDummyXMPPHandler.connectionLostN)	r)   r*   r+   r,   r$  r5   r¬   r.  rZ   r   r   r   r    r(  ™  s   r(  c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚFailureReasonXMPPHandlerz>
    Dummy handler specifically for failure Reason tests.
    c                 C   s
   d| _ d S )NF)ÚgotFailureReasonr%   r   r   r    r$  µ  s    z!FailureReasonXMPPHandler.__init__c                 C   s   t |tjƒrd| _d S rl   )Ú
isinstancer   ÚFailurer2  r/  r   r   r    rZ   ¸  s    z'FailureReasonXMPPHandler.connectionLostN)r)   r*   r+   r,   r$  rZ   r   r   r   r    r1  °  s   r1  c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚXMPPHandlerTestsz-
    Tests for L{xmlstream.XMPPHandler}.
    c                 C   s   t tjt ¡ ƒ dS )zN
        L{xmlstream.XMPPHandler} implements L{ijabber.IXMPPHandler}.
        N)r   r   ZIXMPPHandlerr   ÚXMPPHandlerr%   r   r   r    Útest_interfaceÂ  s    zXMPPHandlerTests.test_interfacec                 C   s>   G dd„ dƒ}t  ¡ }|ƒ |_| d¡ |  dg|jj¡ dS )zP
        Test that data is passed on for sending by the stream manager.
        c                   @   s   e Zd Zdd„ Zdd„ ZdS )z6XMPPHandlerTests.test_send.<locals>.DummyStreamManagerc                 S   s
   g | _ d S r\   )Úoutlistr%   r   r   r    r$  Î  s    z?XMPPHandlerTests.test_send.<locals>.DummyStreamManager.__init__c                 S   s   | j  |¡ d S r\   )r8  rª   )r   Údatar   r   r    rB   Ñ  s    z;XMPPHandlerTests.test_send.<locals>.DummyStreamManager.sendN)r)   r*   r+   r$  rB   r   r   r   r    ÚDummyStreamManagerÍ  s   r:  r£   N)r   r6  ÚparentrB   r   r8  )r   r:  Úhandlerr   r   r    r¨   È  s
    
zXMPPHandlerTests.test_sendc                 C   sN   G dd„ dt jƒ}|ƒ }t  t  ¡ ¡}| |¡ |  |j¡ |  ||j ¡ dS )zY
        Test that makeConnection saves the XML stream and calls connectionMade.
        c                   @   s   e Zd Zdd„ ZdS )z=XMPPHandlerTests.test_makeConnection.<locals>.TestXMPPHandlerc                 S   s
   d| _ d S rl   r-  r%   r   r   r    r¬   ß  s    zLXMPPHandlerTests.test_makeConnection.<locals>.TestXMPPHandler.connectionMadeN)r)   r*   r+   r¬   r   r   r   r    ÚTestXMPPHandlerÞ  s   r=  N)r   r6  r2   rw   r5   r>   r)  r¼   )r   r=  r<  rL   r   r   r    Útest_makeConnectionÙ  s    
z$XMPPHandlerTests.test_makeConnectionc                 C   s>   t  ¡ }t  t  ¡ ¡}| |¡ | tƒ ¡ |  d|j ¡ dS )zB
        Test that connectionLost forgets the XML stream.
        N)r   r6  r2   rw   r5   rZ   Ú	Exceptionr¼   )r   r<  rL   r   r   r    Útest_connectionLostè  s
    
z$XMPPHandlerTests.test_connectionLostN)r)   r*   r+   r,   r7  r¨   r>  r@  r   r   r   r    r5  ½  s
   r5  c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚXMPPHandlerCollectionTestsz7
    Tests for L{xmlstream.XMPPHandlerCollection}.
    c                 C   s   t  ¡ | _d S r\   )r   ZXMPPHandlerCollectionÚ
collectionr%   r   r   r    r;   ø  s    z XMPPHandlerCollectionTests.setUpc                 C   s   t tj| jƒ dS )zZ
        L{xmlstream.StreamManager} implements L{ijabber.IXMPPHandlerCollection}.
        N)r   r   ZIXMPPHandlerCollectionrB  r%   r   r   r    r7  û  s    z)XMPPHandlerCollectionTests.test_interfacec                 C   s4   t ƒ }| | j¡ |  || j¡ |  | j|j¡ dS )z:
        Test the addition of a protocol handler.
        N)r(  ÚsetHandlerParentrB  rD   r¼   r;  ©r   r<  r   r   r    Útest_addHandler  s    z*XMPPHandlerCollectionTests.test_addHandlerc                 C   s>   t ƒ }| | j¡ | | j¡ |  || j¡ |  d|j¡ dS )z5
        Test removal of a protocol handler.
        N)r(  rC  rB  ZdisownHandlerParentrW   r¼   r;  rD  r   r   r    Útest_removeHandler
  s
    z-XMPPHandlerCollectionTests.test_removeHandlerN)r)   r*   r+   r,   r;   r7  rE  rF  r   r   r   r    rA  ó  s
   	rA  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S )ÚStreamManagerTestsz/
    Tests for L{xmlstream.StreamManager}.
    c                 C   s   t ƒ }t |¡| _d S r\   )r!  r   ÚStreamManagerÚstreamManager)r   Úfactoryr   r   r    r;     s    zStreamManagerTests.setUpc                 C   s~   | j }|  d|j¡ |  g |j¡ |  |j|jjd ¡ |  |j|jjd ¡ |  |j	|jjd ¡ |  |j
|jjd ¡ dS )zM
        Test correct initialization and setup of factory observers.
        Nz//event/stream/connectedrÄ   ru   rÅ   )rI  r¼   r   r   ÚhandlersÚ
_connectedrJ  r#  Ú_authdÚ_disconnectedZinitializationFailed)r   Úsmr   r   r    r!     s     
ÿ 
ÿzStreamManagerTests.test_basicc                 C   s\   | j }tƒ }| |¡ t t ¡ ¡}| |¡ |  d|j¡ |  d|j	¡ |  d|j
¡ dS )z
        Test that protocol handlers have their connectionMade method called
        when the XML stream is connected.
        rc   r   N)rI  r(  rC  r   r2   rw   rL  r   r)  r*  r+  ©r   rO  r<  rL   r   r   r    Útest_connected.  s    

z!StreamManagerTests.test_connectedc                 C   sN   | j }tƒ }| |¡ t t ¡ ¡}| |¡ |  d|j¡ |  d|j	¡ dS )zP
        Test raw data functions unset when logTraffic is set to False.
        N)
rI  r(  rC  r   r2   rw   rL  r¼   ÚrawDataInFnÚrawDataOutFnrP  r   r   r    Útest_connectedLogTrafficFalse<  s    

z0StreamManagerTests.test_connectedLogTrafficFalsec                 C   sT   | j }d|_tƒ }| |¡ t t ¡ ¡}| |¡ |  d|j	¡ |  d|j
¡ dS )zM
        Test raw data functions set when logTraffic is set to True.
        TN)rI  Z
logTrafficr(  rC  r   r2   rw   rL  rÒ   rR  rS  rP  r   r   r    Útest_connectedLogTrafficTrueH  s    

z/StreamManagerTests.test_connectedLogTrafficTruec                 C   s\   | j }tƒ }| |¡ t t ¡ ¡}| |¡ |  d|j¡ |  d|j	¡ |  d|j
¡ dS )zˆ
        Test that protocol handlers have their connectionInitialized method
        called when the XML stream is initialized.
        r   rc   N)rI  r(  rC  r   r2   rw   rM  r   r)  r*  r+  rP  r   r   r    Ú
test_authdU  s    

zStreamManagerTests.test_authdc                 C   s\   | j }tƒ }| |¡ t t ¡ ¡}| |¡ |  d|j¡ |  d|j	¡ |  d|j
¡ dS )z‚
        Test that protocol handlers have their connectionLost method
        called when the XML stream is disconnected.
        r   rc   N)rI  r(  rC  r   r2   rw   rN  r   r)  r*  r+  rP  r   r   r    Útest_disconnectedc  s    

z$StreamManagerTests.test_disconnectedc                 C   s<   | j }tƒ }| |¡ | t tdƒ¡¡ |  d|j¡ dS )zŸ
        A L{STREAM_END_EVENT} results in L{StreamManager} firing the handlers
        L{connectionLost} methods, passing a L{failure.Failure} reason.
        z	no reasonTN)	rI  r1  rC  rN  r   r4  r?  r   r2  ©r   rO  r<  r   r   r    Útest_disconnectedReasonq  s
    
z*StreamManagerTests.test_disconnectedReasonc                 C   sD   | j }tƒ }| |¡ |  d|j¡ |  d|j¡ |  d|j¡ dS )zN
        Test the addition of a protocol handler while not connected.
        r   N)rI  r(  rC  r   r)  r*  r+  rX  r   r   r    rE  |  s    
z"StreamManagerTests.test_addHandlerc                 C   sf   | j }t t ¡ ¡}| |¡ | |¡ tƒ }| |¡ |  d|j	¡ |  d|j
¡ |  d|j¡ dS )a  
        Test the addition of a protocol handler after the stream
        have been initialized.

        Make sure that the handler will have the connected stream
        passed via C{makeConnection} and have C{connectionInitialized}
        called.
        rc   r   N)rI  r   r2   rw   rL  rM  r(  rC  r   r)  r*  r+  )r   rO  rL   r<  r   r   r    Útest_addHandlerInitializedˆ  s    	


z-StreamManagerTests.test_addHandlerInitializedc                 C   sj   t  t  ¡ ¡}t  |¡}| d¡}t ¡ |_| ¡  | 	d¡ | 
|d¡ | d¡ |  d|j ¡ ¡ dS )z€
        Test send when the stream has been initialized.

        The data should be sent directly over the XML stream.
        Nr³   rÄ   r£   r¤   )r   ÚXmlStreamFactoryrw   rH  ÚbuildProtocolr	   r6   r@   r¬   r7   ÚdispatchrB   r   rE   ©r   rJ  rO  rL   r   r   r    Útest_sendInitializedœ  s    


ÿ
z'StreamManagerTests.test_sendInitializedc                 C   sÎ   t  t  ¡ ¡}t  |¡}tƒ }| |¡ | d¡}t ¡ |_	| 
d¡ |  d|j	 ¡ ¡ |  d|jd ¡ | ¡  |  d|j	 ¡ ¡ |  d|jd ¡ | d¡ | |d¡ |  d|j	 ¡ ¡ |  |j¡ dS )z©
        Test send when there is no established XML stream.

        The data should be cached until an XML stream has been established and
        initialized.
        Nr£   r–   r   r³   rÄ   r¤   )r   r[  rw   rH  r(  Ú
addHandlerr\  r	   r6   r@   rB   r   rE   Ú_packetQueuer¬   r7   r]  rQ   ©r   rJ  rO  r<  rL   r   r   r    Útest_sendNotConnected°  s$    




ÿz(StreamManagerTests.test_sendNotConnectedc                 C   sp   t  t  ¡ ¡}t  |¡}| d¡}t ¡ |_| ¡  | 	d¡ | 
d¡ |  d|j ¡ ¡ |  d|jd ¡ dS )zŸ
        Test send when the stream is connected but not yet initialized.

        The data should be cached until the XML stream has been initialized.
        Nr³   r£   r–   r   )r   r[  rw   rH  r\  r	   r6   r@   r¬   r7   rB   r   rE   ra  r^  r   r   r    Útest_sendNotInitializedÐ  s    


ÿ
z*StreamManagerTests.test_sendNotInitializedc                 C   s€   t  t  ¡ ¡}t  |¡}tƒ }| |¡ | d¡}| ¡  t 	¡ |_
| d¡ | d¡ |  d|j
 ¡ ¡ |  d|jd ¡ dS )z£
        Test send after XML stream disconnection.

        The data should be cached until a new XML stream has been established
        and initialized.
        Nr£   r–   r   )r   r[  rw   rH  r(  r`  r\  r¬   r	   r6   r@   rZ   rB   r   rE   ra  rb  r   r   r    Útest_sendDisconnectedä  s    





z(StreamManagerTests.test_sendDisconnectedN)r)   r*   r+   r,   r;   r!   rQ  rT  rU  rV  rW  rY  rE  rZ  r_  rc  rd  re  r   r   r   r    rG    s    rG  c                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚXmlStreamServerFactoryTestsz8
    Tests for L{xmlstream.XmlStreamServerFactory}.
    c                    s*   G dd„ dƒ‰ ‡ fdd„}t  |¡| _dS )zQ
        Set up a server factory with an authenticator factory function.
        c                   @   s   e Zd Zdd„ Zdd„ ZdS )z<XmlStreamServerFactoryTests.setUp.<locals>.TestAuthenticatorc                 S   s
   g | _ d S r\   )Ú
xmlstreamsr%   r   r   r    r$    s    zEXmlStreamServerFactoryTests.setUp.<locals>.TestAuthenticator.__init__c                 S   s   | j  |¡ d S r\   )rg  rª   rz   r   r   r    r°     s    zPXmlStreamServerFactoryTests.setUp.<locals>.TestAuthenticator.associateWithStreamN)r)   r*   r+   r$  r°   r   r   r   r    r²     s   r²   c                      s   ˆ ƒ S r\   r   r   ©r²   r   r    ÚauthenticatorFactory  s    z?XmlStreamServerFactoryTests.setUp.<locals>.authenticatorFactoryN)r   ZXmlStreamServerFactoryrJ  )r   ri  r   rh  r    r;   ÿ  s    z!XmlStreamServerFactoryTests.setUpc                 C   s   t t| jƒ dS )z<
        L{XmlStreamServerFactory} is a L{Factory}.
        N)r   r   rJ  r%   r   r   r    r7    s    z*XmlStreamServerFactoryTests.test_interfacec                 C   s"   | j  d¡}|  |g|jj¡ dS )a‹  
        The authenticator factory should be used to instantiate the
        authenticator and pass it to the protocol.

        The default protocol, L{XmlStream} stores the authenticator it is
        passed, and calls its C{associateWithStream} method. so we use that to
        check whether our authenticator factory is used and the protocol
        instance gets an authenticator.
        N)rJ  r\  r   r:   rg  rz   r   r   r    Ú,test_buildProtocolAuthenticatorInstantiation  s    
zHXmlStreamServerFactoryTests.test_buildProtocolAuthenticatorInstantiationc                 C   s   | j  d¡}|  |tj¡ dS )zV
        The protocol factory creates Jabber XML Stream protocols by default.
        N)rJ  r\  rÛ   r   r2   rz   r   r   r    Útest_buildProtocolXmlStream#  s    z7XmlStreamServerFactoryTests.test_buildProtocolXmlStreamc                 C   s8   | j  d¡}| j  d¡}|  ||¡ |  |j|j¡ dS )z’
        Subsequent calls to buildProtocol should result in different instances
        of the protocol, as well as their authenticators.
        N)rJ  r\  rÒ   r:   )r   Zxs1Zxs2r   r   r    Útest_buildProtocolTwice*  s    z3XmlStreamServerFactoryTests.test_buildProtocolTwiceN)	r)   r*   r+   r,   r;   r7  rj  rk  rl  r   r   r   r    rf  ú  s   rf  )9r,   r
   r   Zzope.interface.verifyr   Ztwisted.internetr   r   Ztwisted.internet.errorr   Ztwisted.internet.interfacesr   Ztwisted.pythonr   Ztwisted.testr	   Ztwisted.trialZtwisted.words.protocols.jabberr   r   r   r   Z!twisted.words.test.test_xmlstreamr   Ztwisted.words.xishr   r   Ú_sslÚImportErrorr  Ztwisted.internet._sslverifyr   Ztwisted.internet.sslr   rõ   ZTestCaser   r-   rk   r?  r·   r¸   rÃ   rÙ   rÞ   Z BaseFeatureInitiatingInitializerr  r
  r  r!  r6  r(  r1  r5  rA  rG  rf  r   r   r   r    Ú<module>   sV   
  # \CL- 'B6" f