o
    Óó·i'°  ã                   @   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ya   dZdZY nw 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                   @   ó0   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   ó   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!   úmC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\twisted/words/test/test_jabberxmlstream.pyÚ
test_basic)   ó   zHashPasswordTests.test_basicc                 C   ó   |   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   ó   z$HashPasswordTests.test_sidNotUnicodec                 C   r%   )z8
        The password must be a unicode object.
        r   s   secrÃ©tNr&   r)   r!   r!   r"   Útest_passwordNotUnicode6   r+   z)HashPasswordTests.test_passwordNotUnicodec                 C   r   )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<   r$   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Útyper4   Úid)r   r>   Ú
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 )NrB   zutf-8s   <iq type='get' id='s   '/>s   <iq id='s   ' type='get'/>)r   Ú	transportÚclearr>   Ú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 )NrA   Úresult)r   )rL   r)   r!   r"   Úcbg   s   z&IQTests.testResultResponse.<locals>.cbú<iq type='result' id='%s'/>rB   )r>   rG   ÚaddCallbackr   r<   )r   rM   Ú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'/>rB   )r>   rG   ÚassertFailurer   ZStanzaErrorr   r<   ©r   rP   rQ   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)r>   r)   r!   r"   rM   ‰   s   z*IQTests.testNonTrackedResponse.<locals>.cbz/iqéÿÿÿÿz<iq type='result' id='test'/>N)r   ZupgradeWithIQResponseTrackerrV   ÚiqDeferredsÚaddObserverr<   )r   rQ   rM   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.
        rN   rB   N)r>   rG   r   r<   ÚassertNotInrY   rT   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 peerN)r>   rG   r   ÚconnectionLostrS   r   rT   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   r=   rG   Ú
addErrback)r   rP   ©Úebr   r!   r"   rd   °   s   z'IQTests.testNoModifyingDict.<locals>.ebr^   N)r>   rG   rb   r   r_   ©r   rP   r!   rc   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.
        é<   é   N)r>   ÚtimeoutrG   rS   r   ÚTimeoutErrorr8   ÚpumprV   ÚcallsrY   re   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.
        rg   rh   rN   rB   N)
r>   ri   rG   r8   r9   r   r<   rk   rV   rl   re   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.
        rg   r^   N)
r>   ri   rG   r   r_   rS   r   rV   r8   rl   rT   r!   r!   r"   Ú!testDisconnectTimeoutCancellationÖ   s   

z)IQTests.testDisconnectTimeoutCancellationN)r.   r/   r0   r1   r@   rD   rK   rR   rU   r[   r]   r`   rf   rm   rn   ro   r!   r!   r!   r"   r2   D   s    r2   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   ó
   d| _ d S ©NT)ÚgotStreamStart©r   Úobjr!   r!   r"   ÚonStreamStartç   ó   
zXmlStreamTests.onStreamStartc                 C   rq   rr   )ÚgotStreamEndrt   r!   r!   r"   ÚonStreamEndê   rw   zXmlStreamTests.onStreamEndc                 C   rq   rr   )ÚgotStreamErrorrt   r!   r!   r"   ÚonStreamErrorí   rw   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/errorr3   ©rh   r   N)rs   rx   rz   r   r7   ÚAuthenticatorrZ   rv   ry   r{   r:   r	   Z StringTransportWithDisconnectionrE   Úprotocolr6   Úversion©r   rQ   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   rX   ó    ó   <stream:streamó/   xmlns:stream='http://etherx.jabber.org/streams'ó   xmlns='testns'ó   version='1.0'N)r   Ú
sendHeaderrE   rJ   ÚsplitrI   rC   Ú_headerSent©r   rQ   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   rX   r‚   rƒ   r„   s!   xmlns:db='jabber:server:dialback'r…   r†   N)	r   Úprefixesr‡   rE   rJ   rˆ   rI   rC   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   rX   r‚   ó   to='otherhost'ó   from='thishost'N)r   r   ÚJIDÚ
thisEntityÚotherEntityÚ
initiatingr‡   rE   rJ   rˆ   rI   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   rX   r‚   r‘   r’   s   id='session01'N)r   r   r“   r”   r•   r–   Úsidr‡   rE   rJ   rˆ   rI   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   r<   rC   rz   rx   r�   r!   r!   r"   Útest_receiveStreamError9  s   ÿ
z&XmlStreamTests.test_receiveStreamErrorc                 C   óP   | 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‡   rE   rF   ÚsendStreamErrorr   ÚStreamErrorÚassertNotEqualrJ   rC   rx   r�   r!   r!   r"   Útest_sendStreamErrorInitiatingG  ó   
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–   rE   rF   r    r   r¡   Ú	assertNotr‰   r   rJ   rC   rx   r�   r!   r!   r"   Ú&test_sendStreamErrorInitiatingNoHeaderU  ó   
z5XmlStreamTests.test_sendStreamErrorInitiatingNoHeaderc                 C   rœ   )z‘
        Test sendStreamError on a receiving xmlstream with a header sent.

        An error should be sent out and the connection lost.
        Fr�   rž   NrŸ   r�   r!   r!   r"   Útest_sendStreamErrorReceivinge  r¤   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–   rE   rF   r    r   r¡   rC   r‰   r¢   rJ   rx   r�   r!   r!   r"   Ú%test_sendStreamErrorReceivingNoHeaders  r§   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   rQ   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/>)r3   Zpresence)z http://etherx.jabber.org/streamsÚfeaturess   <stream:features/>N)r   rG   r   rE   rJ   rF   r   ÚElement)r   rQ   Ú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 ra   ©Úappendr)   )ÚconnectionMadeCallsr!   r"   ÚconnectionMade©  ó   zKXmlStreamTests.test_authenticator.<locals>.TestAuthenticator.connectionMadec                    ó   ˆ   |¡ d S ra   r³   )r   ZrootElement)ÚstreamStartedCallsr!   r"   ÚstreamStarted¬  r·   zJXmlStreamTests.test_authenticator.<locals>.TestAuthenticator.streamStartedc                    r¸   ra   r³   r�   )ÚassociateWithStreamCallsr!   r"   ÚassociateWithStream¯  r·   zPXmlStreamTests.test_authenticator.<locals>.TestAuthenticator.associateWithStreamN)r.   r/   r0   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'>rh   )r   r7   r   r¶   r<   Úlenr«   )r   r¾   ÚarQ   r!   r½   r"   Útest_authenticator   s   

ÿz!XmlStreamTests.test_authenticatorN)r.   r/   r0   rv   ry   r{   r@   r‹   rŽ   r—   r™   r›   r£   r¦   r¨   r©   r¬   r²   rÂ   r!   r!   r!   r"   rp   æ   s"    rp   c                   @   s   e Zd ZdS )Ú	TestErrorN)r.   r/   r0   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   ó   t  ¡ | _t  | j¡| _ d S ra   )r   r~   r?   r7   r)   r!   r!   r"   r@   Æ  ó   
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'>r}   NÚinvalid)r   r:   r	   r;   r<   r   r€   ÚassertIdenticalr˜   r6   r•   r”   r�   r!   r!   r"   Útest_streamStartÊ  s   ÿz#AuthenticatorTests.test_streamStartc                 C   ó0   | 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   r:   r	   r;   r<   r   r€   r�   r!   r!   r"   Útest_streamStartLegacyÝ  ó   ÿz)AuthenticatorTests.test_streamStartLegacyc                 C   rÌ   )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Î   r�   r!   r!   r"   Útest_streamBadVersionOneDigitë  rÐ   z0AuthenticatorTests.test_streamBadVersionOneDigitc                 C   rÌ   )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Î   r�   r!   r!   r"   Útest_streamBadVersionNoNumberù  rÐ   z0AuthenticatorTests.test_streamBadVersionNoNumberN)r.   r/   r0   r@   rË   rÏ   rÑ   rÒ   r!   r!   r!   r"   rÄ   Å  s    rÄ   c                   @   ó<   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   r5   r?   r7   rZ   ÚonAuthenticatedÚonInitFailedr)   r!   r!   r"   r@   	  s   zConnectAuthenticatorTests.setUpc                 C   rq   rr   )r×   rt   r!   r!   r"   rÙ     rw   z)ConnectAuthenticatorTests.onAuthenticatedc                 C   s
   || _ d S ra   )rØ   )r   r   r!   r!   r"   rÚ     rw   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                   @   ó   e Zd Zdd„ ZdS )z9ConnectAuthenticatorTests.testSucces.<locals>.Initializerc                 S   s   d S ra   r!   r)   r!   r!   r"   Ú
initialize  ó   zDConnectAuthenticatorTests.testSucces.<locals>.Initializer.initializeN©r.   r/   r0   rÜ   r!   r!   r!   r"   ÚInitializer  ó    rß   N)r   Úinitializersr?   ÚinitializeStreamr   rC   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                   @   rÛ   )z:ConnectAuthenticatorTests.testFailure.<locals>.Initializerc                 S   s   t ‚ra   )rÃ   r)   r!   r!   r"   rÜ   -  rÝ   zEConnectAuthenticatorTests.testFailure.<locals>.Initializer.initializeNrÞ   r!   r!   r!   r"   rß   ,  rà   rß   N)r   rá   r?   râ   r   rV   r×   ÚassertNotIdenticalrØ   rC   Ú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Ç   r3   z’<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' from='example.com' to='example.org' id='12345' version='1.0'>r}   r   úexample.comNz9<stream:features><test xmlns='testns'/></stream:features>©r3   Útest)r?   r6   r   r:   r	   r;   r<   r   r€   r˜   r•   ÚhostrÊ   r”   r¥   r×   rI   r¯   rC   r�   r!   r!   r"   rË   9  s"   ÿÿz*ConnectAuthenticatorTests.test_streamStartN)	r.   r/   r0   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   rÅ   ra   )r   ZListenAuthenticatorr?   r7   r)   r!   r!   r"   r@   Y  rÆ   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È   r}   r   újabber:clientré   )r   r:   r	   r;   rÊ   r˜   r<   r   r€   ræ   r¢   r6   r•   r”   rì   r�   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   r:   r	   r;   r<   ÚassertIsInstancer˜   Ústrr�   r!   r!   r"   Ú test_streamStartUnicodeSessionIDr  s   ÿz9ListenAuthenticatorTests.test_streamStartUnicodeSessionIDN)r.   r/   r0   r1   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   ÚsavedSSLr5   r?   r7   r´   rG   r¶   r<   ÚTLSInitiatingInitializerrä   r)   r!   r!   r"   r@   ‚  s   
ÿz#TLSInitiatingInitializerTests.setUpc                 C   s   | j t_d S ra   )rõ   r   r   r)   r!   r!   r"   ÚtearDown“  ó   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ä   rC   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 ¡ ˆ  
g d	¢ˆ j¡ |S )zN
        When TLS is wanted and SSL available, StartTLS is initiated.
        c                    ó   ˆ j  d¡S ©NÚTLS©rô   r´   )Úctxr)   r!   r"   Ú<lambda>£  ó    zDTLSInitiatingInitializerTests.test_wantedSupported.<locals>.<lambda>c                      rû   ©Nr«   rþ   r!   r)   r!   r"   r   ¤  r  c                      rû   ©NÚheaderrþ   r!   r)   r!   r"   r   ¥  r  é   rh   Ústarttlsú<proceed xmlns='%s'/>©rý   r«   r  N)r	   r;   r   rE   ÚstartTLSr«   r‡   rä   ÚstartrO   r   ZResetrÀ   ró   ÚnameÚNS_XMPP_TLSÚurir<   rô   )r   rP   r  r!   r)   r"   Útest_wantedSupported�  s   

z2TLSInitiatingInitializerTests.test_wantedSupportedc                    sr   ‡ fdd„}t  ¡ ˆ j_|ˆ jj_‡ fdd„ˆ j_‡ fdd„ˆ j_ˆ j ¡ }ˆ j 	dt
 ¡ ˆ  g d¢ˆ 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                      rû   r  rþ   r!   r)   r!   r"   r   ¿  r  zFTLSInitiatingInitializerTests.test_certificateVerify.<locals>.<lambda>c                      rû   r  rþ   r!   r)   r!   r"   r   À  r  r  r  N)r	   r;   r   rE   r	  r«   r‡   rä   r
  r<   r  r   rô   ©r   r  rP   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 ¡ ˆ g d¢ˆ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                      rû   r  rþ   r!   r)   r!   r"   r   Ù  r  zMTLSInitiatingInitializerTests.test_certificateVerifyContext.<locals>.<lambda>c                      rû   r  rþ   r!   r)   r!   r"   r   Ú  r  r  r  N)r   r   rö   rä   r  r	   r;   rE   r	  r«   r‡   r
  r<   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.
        NFrh   )	r   r   rä   rù   r
  rO   r   rÀ   ró   re   r!   r!   r"   Ú"test_wantedNotSupportedNotRequiredá  ó   
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.
        NTrh   )
r   r   rä   rù   r
  rS   ZTLSNotSupportedr   rÀ   ró   re   r!   r!   r"   Útest_wantedNotSupportedRequiredî  r  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ù   Frh   N)r   r°   Z
addElementr  r  r   r¯   rä   Úwantedr
  r   rÀ   ró   rS   ZTLSRequired©r   ZtlsrP   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  FNrh   )r   r°   r  r  r   r¯   rä   r  rù   r
  rO   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.
        rh   z<failure xmlns='%s'/>N)r   r   rä   r
  rS   Z	TLSFailedr<   r  re   r!   r!   r"   Útest_failed  s
   
z)TLSInitiatingInitializerTests.test_failedN)r.   r/   r0   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 ra   )r   Zsucceedr)   r!   r!   r"   r
  *  rw   zTestFeatureInitializer.startN)r.   r/   r0   Ú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 ra   )r   r7   r~   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.
        N)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:  ó   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  r%  zBBaseFeatureInitiatingInitializerTests.testNotAdvertizedNotRequiredN)r.   r/   r0   r@   r#  r$  r&  r!   r!   r!   r"   r"  .  s
    r"  c                   @   rÓ   )Ú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î   r>   r4   rA   úuser1@example.comÚtoúuser2@example.com/resourceÚfromZstanza1rB   rL   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(  r4   rA   r)  r*  r,  N)r   r°   r   r-  r   rV   ÚhasAttributer.  r!   r!   r"   Útest_toResponseNoFrom\  ó   

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(  r4   rA   r+  r,  r*  N)r   r°   r   r-  rV   r1  r   r.  r!   r!   r"   Útest_toResponseNoTog  r3  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chatrA   r*  r,  N©r   r°   r   r-  rV   r1  r.  r!   r!   r"   Útest_toResponseNoAddressingr  s
   

z+ToResponseTests.test_toResponseNoAddressingc                 C   ó(   t  d¡}t |¡}|  | d¡¡ dS )zP
        Test that a proper response is generated without id attribute.
        r5  rB   Nr7  r.  r!   r!   r"   Ú	test_noID|  ó   

zToResponseTests.test_noIDc                 C   r9  )zR
        Test that a proper response is generated without type attribute.
        r5  rA   Nr7  r.  r!   r!   r"   Útest_noType„  r;  zToResponseTests.test_noTypeN)	r.   r/   r0   r0  r2  r4  r8  r:  r<  r!   r!   r!   r"   r'  K  s    
r'  c                   @   ó    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 ra   ©Ú	callbacksr)   r!   r!   r"   Ú__init__’  rw   zDummyFactory.__init__c                 C   s   || j |< d S ra   r?  )r   ÚeventÚcallbackr!   r!   r"   ÚaddBootstrap•  r·   zDummyFactory.addBootstrapN)r.   r/   r0   r1   rA  rD  r!   r!   r!   r"   r>  �  ó    r>  c                   @   ó8   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"   rA  ž  s   
zDummyXMPPHandler.__init__c                 C   s   |   ¡  d S ra   )r¶   r�   r!   r!   r"   r:   £  rø   zDummyXMPPHandler.makeConnectionc                 C   ó   |  j d7  _ d S ©Nrh   ©rH  r)   r!   r!   r"   r¶   ¦  ó   zDummyXMPPHandler.connectionMadec                 C   rK  rL  )rI  r)   r!   r!   r"   ÚconnectionInitialized©  rN  z&DummyXMPPHandler.connectionInitializedc                 C   rK  rL  )rJ  ©r   Úreasonr!   r!   r"   r_   ¬  rN  zDummyXMPPHandler.connectionLostN)	r.   r/   r0   r1   rA  r:   r¶   rO  r_   r!   r!   r!   r"   rG  ™  s    rG  c                   @   r=  )ÚFailureReasonXMPPHandlerz>
    Dummy handler specifically for failure Reason tests.
    c                 C   rq   )NF)ÚgotFailureReasonr)   r!   r!   r"   rA  µ  rw   z!FailureReasonXMPPHandler.__init__c                 C   s   t |tjƒrd| _d S d S rr   )Ú
isinstancer   ÚFailurerS  rP  r!   r!   r"   r_   ¸  s   
ÿz'FailureReasonXMPPHandler.connectionLostN)r.   r/   r0   r1   rA  r_   r!   r!   r!   r"   rR  °  rE  rR  c                   @   r   )Ú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                   @   ó   e Zd Zdd„ Zdd„ ZdS )z6XMPPHandlerTests.test_send.<locals>.DummyStreamManagerc                 S   ó
   g | _ d S ra   )Úoutlistr)   r!   r!   r"   rA  Î  rw   z?XMPPHandlerTests.test_send.<locals>.DummyStreamManager.__init__c                 S   ó   | j  |¡ d S ra   )r[  r´   )r   Údatar!   r!   r"   rG   Ñ  ó   z;XMPPHandlerTests.test_send.<locals>.DummyStreamManager.sendN)r.   r/   r0   rA  rG   r!   r!   r!   r"   ÚDummyStreamManagerÍ  ó    r_  r­   N)r   rW  ÚparentrG   r   r[  )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                   @   rÛ   )z=XMPPHandlerTests.test_makeConnection.<locals>.TestXMPPHandlerc                 S   rq   rr   rM  r)   r!   r!   r"   r¶   ß  rw   zLXMPPHandlerTests.test_makeConnection.<locals>.TestXMPPHandler.connectionMadeN)r.   r/   r0   r¶   r!   r!   r!   r"   ÚTestXMPPHandlerÞ  rà   rc  N)r   rW  r7   r~   r:   rC   rH  rÊ   )r   rc  rb  rQ   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   rW  r7   r~   r:   r_   Ú	ExceptionrÊ   )r   rb  rQ   r!   r!   r"   Útest_connectionLostè  s
   
z$XMPPHandlerTests.test_connectionLostN)r.   r/   r0   r1   rX  r²   rd  rf  r!   r!   r!   r"   rV  ½  s    rV  c                   @   r   )ÚXMPPHandlerCollectionTestsz7
    Tests for L{xmlstream.XMPPHandlerCollection}.
    c                 C   s   t  ¡ | _d S ra   )r   ZXMPPHandlerCollectionÚ
collectionr)   r!   r!   r"   r@   ø  r·   z XMPPHandlerCollectionTests.setUpc                 C   s   t tj| jƒ dS )zZ
        L{xmlstream.StreamManager} implements L{ijabber.IXMPPHandlerCollection}.
        N)r   r   ZIXMPPHandlerCollectionrh  r)   r!   r!   r"   rX  û  s   z)XMPPHandlerCollectionTests.test_interfacec                 C   s4   t ƒ }| | j¡ |  || j¡ |  | j|j¡ dS )z:
        Test the addition of a protocol handler.
        N)rG  ÚsetHandlerParentrh  rI   rÊ   ra  ©r   rb  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)rG  ri  rh  ZdisownHandlerParentr\   rÊ   ra  rj  r!   r!   r"   Útest_removeHandler
  s
   z-XMPPHandlerCollectionTests.test_removeHandlerN)r.   r/   r0   r1   r@   rX  rk  rl  r!   r!   r!   r"   rg  ó  s    	rg  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 ra   )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Õ   r|   rÖ   )ro  rÊ   r   r   ÚhandlersÚ
_connectedrp  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.
        rh   r   N)ro  rG  ri  r   r7   r~   rr  r   rH  rI  rJ  ©r   ru  rb  rQ   r!   r!   r"   Útest_connected.  ó   

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)
ro  rG  ri  r   r7   r~   rr  rÊ   ÚrawDataInFnÚrawDataOutFnrv  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)ro  Z
logTrafficrG  ri  r   r7   r~   rr  ræ   ry  rz  rv  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   rh   N)ro  rG  ri  r   r7   r~   rs  r   rH  rI  rJ  rv  r!   r!   r"   Ú
test_authdU  rx  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   rh   N)ro  rG  ri  r   r7   r~   rt  r   rH  rI  rJ  rv  r!   r!   r"   Útest_disconnectedc  rx  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)	ro  rR  ri  rt  r   rU  re  r   rS  ©r   ru  rb  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)ro  rG  ri  r   rH  rI  rJ  r  r!   r!   r"   rk  |  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.
        rh   r   N)ro  r   r7   r~   rr  rs  rG  ri  r   rH  rI  rJ  )r   ru  rQ   rb  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   ÚXmlStreamFactoryr~   rn  ÚbuildProtocolr	   r;   rE   r¶   r<   ÚdispatchrG   r   rJ   ©r   rp  ru  rQ   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‚  r~   rn  rG  Ú
addHandlerrƒ  r	   r;   rE   rG   r   rJ   Ú_packetQueuer¶   r<   r„  rV   ©r   rp  ru  rb  rQ   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‚  r~   rn  rƒ  r	   r;   rE   r¶   r<   rG   r   rJ   rˆ  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‚  r~   rn  rG  r‡  rƒ  r¶   r	   r;   rE   r_   rG   r   rJ   rˆ  r‰  r!   r!   r"   Útest_sendDisconnectedä  s   





z(StreamManagerTests.test_sendDisconnectedN)r.   r/   r0   r1   r@   r#   rw  r{  r|  r}  r~  r€  rk  r�  r†  rŠ  r‹  rŒ  r!   r!   r!   r"   rm    s      rm  c                   @   rF  )Ú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                   @   rY  )z<XmlStreamServerFactoryTests.setUp.<locals>.TestAuthenticatorc                 S   rZ  ra   )Ú
xmlstreamsr)   r!   r!   r"   rA    rw   zEXmlStreamServerFactoryTests.setUp.<locals>.TestAuthenticator.__init__c                 S   r\  ra   )rŽ  r´   r�   r!   r!   r"   r¼     r^  zPXmlStreamServerFactoryTests.setUp.<locals>.TestAuthenticator.associateWithStreamN)r.   r/   r0   rA  r¼   r!   r!   r!   r"   r¾     r`  r¾   c                      s   ˆ ƒ S ra   r!   r!   ©r¾   r!   r"   ÚauthenticatorFactory  s   z?XmlStreamServerFactoryTests.setUp.<locals>.authenticatorFactoryN)r   ZXmlStreamServerFactoryrp  )r   r�  r!   r�  r"   r@   ÿ  s   z!XmlStreamServerFactoryTests.setUpc                 C   s   t t| jƒ dS )z<
        L{XmlStreamServerFactory} is a L{Factory}.
        N)r   r   rp  r)   r!   r!   r"   rX    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)rp  rƒ  r   r?   rŽ  r�   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)rp  rƒ  rï   r   r7   r�   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)rp  rƒ  ræ   r?   )r   Zxs1Zxs2r!   r!   r"   Útest_buildProtocolTwice*  s   z3XmlStreamServerFactoryTests.test_buildProtocolTwiceN)	r.   r/   r0   r1   r@   rX  r‘  r’  r“  r!   r!   r!   r"   r�  ú  s    r�  )9r1   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   r2   rp   re  rÃ   rÄ   rÔ   rí   rò   Z BaseFeatureInitiatingInitializerr   r"  r'  r>  rW  rG  rR  rV  rg  rm  r�  r!   r!   r!   r"   Ú<module>   sZ   þ  # \CL- 'B6" f