o
    ¼>h'°  ã                   @   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ÚsecretÚ(99567ee91b2c7cabf607f10cb9f4a3634fa820e0N©r   ÚhashPasswordÚassertEqual©ÚselfÚhash© r"   ú‡/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/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Ã©tÚ(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 )NÚ	otherhostÚtestnszm<stream:stream xmlns:stream='http://etherx.jabber.org/streams' xmlns='testns' from='otherhost' version='1.0'>Úget)r   ÚConnectAuthenticatorÚ	namespaceÚ	XmlStreamr   ÚClockÚclockÚ	callLaterÚ
_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Útyper7   Úid)r   rC   Ú
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 )NrG   zutf-8s   <iq type='get' id='s   '/>s   <iq id='s   ' type='get'/>)r   Ú	transportÚclearrC   ÚsendÚencodeÚassertInÚvalue)r    ÚidBytesr"   r"   r#   ÚtestSendZ   s   



þþzIQTests.testSendc                    s>   ‡ fdd„}ˆ j  ¡ }| |¡ ˆ j}| dˆ j d  ¡ |S )Nc                    s   ˆ   | d d¡ d S )NrF   Úresult)r   )rR   r*   r"   r#   Úcbg   s   z&IQTests.testResultResponse.<locals>.cbú<iq type='result' id='%s'/>rG   )rC   rL   ÚaddCallbackr   rA   )r    rS   Ú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'/>rG   )rC   rL   ÚassertFailurer   ÚStanzaErrorr   rA   ©r    rV   rW   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 )NÚhandledF)ÚassertFalseÚgetattr)rC   r*   r"   r#   rS   ‰   s   z*IQTests.testNonTrackedResponse.<locals>.cbz/iqéÿÿÿÿz<iq type='result' id='test'/>N)r   ÚupgradeWithIQResponseTrackerr^   ÚiqDeferredsÚaddObserverrA   )r    rW   rS   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.
        rT   rG   )rC   rL   r   rA   ÚassertNotInrb   r[   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)rC   rL   r   ÚconnectionLostrY   r   r[   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   rB   rL   Ú
addErrback)r   rV   ©Úebr    r"   r#   rm   °   s   z'IQTests.testNoModifyingDict.<locals>.ebrg   )rC   rL   rk   r   rh   ©r    rV   r"   rl   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.
        é<   é   )rC   ÚtimeoutrL   rY   r   ÚTimeoutErrorr<   Úpumpr^   Úcallsrb   rn   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.
        rp   rq   rT   rG   )
rC   rr   rL   r<   r=   r   rA   rt   r^   ru   rn   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.
        rp   rg   )
rC   rr   rL   r   rh   rY   r   r^   r<   ru   r[   r"   r"   r#   Ú!testDisconnectTimeoutCancellationÖ   s   

z)IQTests.testDisconnectTimeoutCancellationN)r0   r1   r2   r3   rE   rI   rQ   rX   r\   rd   rf   ri   ro   rv   rw   rx   r"   r"   r"   r#   r4   D   s    r4   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   rz   r{   )ÚgotStreamEndr}   r"   r"   r#   ÚonStreamEndê   r€   zXmlStreamTests.onStreamEndc                 C   rz   r{   )ÚgotStreamErrorr}   r"   r"   r#   ÚonStreamErrorí   r€   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/errorr6   ©rq   r   N)r|   r   rƒ   r   r:   ÚAuthenticatorrc   r   r‚   r„   r?   r	   Ú StringTransportWithDisconnectionrJ   Úprotocolr9   Úversion©r    rW   r"   r"   r#   rE   ð   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   r`   ó    ó   <stream:streamó/   xmlns:stream='http://etherx.jabber.org/streams'ó   xmlns='testns'ó   version='1.0'N)r   Ú
sendHeaderrJ   rO   ÚsplitrN   rH   Ú_headerSent©r    rW   Ú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   r`   rŒ   r   rŽ   s!   xmlns:db='jabber:server:dialback'r   r   N)	r   Úprefixesr‘   rJ   rO   r’   rN   rH   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   r`   rŒ   ó   to='otherhost'ó   from='thishost'N)r   r   ÚJIDÚ
thisEntityÚotherEntityÚ
initiatingr‘   rJ   rO   r’   rN   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›   FÚ	session01r   r`   rŒ   rœ   r   s   id='session01'N)r   r   rž   rŸ   r    r¡   Úsidr‘   rJ   rO   r’   rN   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   rA   rH   rƒ   r   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‘   rJ   rK   ÚsendStreamErrorr   ÚStreamErrorÚassertNotEqualrO   rH   r   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¡   rJ   rK   r¬   r   r­   Ú	assertNotr“   r   rO   rH   r   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¡   rJ   rK   r¬   r   r­   rH   r“   r®   rO   r   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    rW   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/>)r6   Úpresence)z http://etherx.jabber.org/streamsÚfeaturess   <stream:features/>N)r   rL   r   rJ   rO   rK   r   ÚElement)r    rW   Ú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 rj   ©Úappendr*   )ÚconnectionMadeCallsr"   r#   ÚconnectionMade©  ó   zKXmlStreamTests.test_authenticator.<locals>.TestAuthenticator.connectionMadec                    ó   ˆ   |¡ d S rj   rÀ   )r    ÚrootElement)ÚstreamStartedCallsr"   r#   ÚstreamStarted¬  rÄ   zJXmlStreamTests.test_authenticator.<locals>.TestAuthenticator.streamStartedc                    rÅ   rj   rÀ   r‹   )ÚassociateWithStreamCallsr"   r#   ÚassociateWithStream¯  rÄ   zPXmlStreamTests.test_authenticator.<locals>.TestAuthenticator.associateWithStreamN)r0   r1   r2   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'>rq   )r   r:   r   rÃ   rA   Úlenr·   )r    rÌ   ÚarW   r"   rË   r#   Útest_authenticator   s   

ÿz!XmlStreamTests.test_authenticatorN)r0   r1   r2   r   r‚   r„   rE   r–   r™   r¢   r¥   r§   r¯   r²   r´   rµ   r¸   r¿   rÐ   r"   r"   r"   r#   ry   æ   s"    ry   c                   @   s   e Zd ZdS )Ú	TestErrorN)r0   r1   r2   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 rj   )r   r‡   rD   r:   r*   r"   r"   r#   rE   Æ  ó   
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@   rA   r   rŠ   ÚassertIdenticalr¤   r9   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@   rA   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)r0   r1   r2   rE   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   r8   rD   r:   rc   ÚonAuthenticatedÚonInitFailedr*   r"   r"   r#   rE   	  s   zConnectAuthenticatorTests.setUpc                 C   rz   r{   )rå   r}   r"   r"   r#   rç     r€   z)ConnectAuthenticatorTests.onAuthenticatedc                 C   s
   || _ d S rj   )ræ   )r    r   r"   r"   r#   rè     r€   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 rj   r"   r*   r"   r"   r#   Ú
initialize  ó   zDConnectAuthenticatorTests.testSucces.<locals>.Initializer.initializeN©r0   r1   r2   rê   r"   r"   r"   r#   ÚInitializer  ó    rí   N)r   ÚinitializersrD   ÚinitializeStreamr   rH   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 ‚rj   )rÑ   r*   r"   r"   r#   rê   -  rë   zEConnectAuthenticatorTests.testFailure.<locals>.Initializer.initializeNrì   r"   r"   r"   r#   rí   ,  rî   rí   N)r   rï   rD   rð   r   r^   rå   ÚassertNotIdenticalræ   rH   Ú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Õ   r6   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>©r6   Útest)rD   r9   r   r?   r	   r@   rA   r   rŠ   r¤   r    ÚhostrØ   rŸ   r±   rå   rN   r¼   rH   r‹   r"   r"   r#   rÙ   9  s"   ÿÿz*ConnectAuthenticatorTests.test_streamStartN)	r0   r1   r2   rE   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Ó   rj   )r   ÚListenAuthenticatorrD   r:   r*   r"   r"   r#   rE   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¤   rA   r   rŠ   rô   r®   r9   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@   rA   ÚassertIsInstancer¤   Ústrr‹   r"   r"   r#   Ú test_streamStartUnicodeSessionIDr  s   ÿz9ListenAuthenticatorTests.test_streamStartUnicodeSessionIDN)r0   r1   r2   r3   rE   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   ÚsavedSSLr8   rD   r:   rÁ   rL   rÃ   rA   ÚTLSInitiatingInitializerrò   r*   r"   r"   r#   rE   ‚  s   
ÿz#TLSInitiatingInitializerTests.setUpc                 C   s   | j t_d S rj   )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ò   rH   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  é   rq   Ústarttlsú<proceed xmlns='%s'/>©r  r·   r  )r	   r@   r   rJ   ÚstartTLSr·   r‘   rò   ÚstartrU   r   ÚResetrÎ   r  ÚnameÚNS_XMPP_TLSÚurirA   r  )r    rV   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   Ú	_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  )r	   r@   r   rJ   r  r·   r‘   rò   r  rA   r  r   r  ©r    r"  rV   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.
        )ÚconfigurationForTLSc                    s   ˆ  ˆ | ¡ ˆj d¡ d S r  )Ú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  )r   r   r  rò   r!  r	   r@   rJ   r  r·   r‘   r  rA   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.
        NFrq   )	r   r   rò   r  r  rU   r   rÎ   r  rn   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.
        NTrq   )
r   r   rò   r  r  rY   ÚTLSNotSupportedr   rÎ   r  rn   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  Frq   )r   r½   Ú
addElementr  r  r   r¼   rò   Úwantedr  r   rÎ   r  rY   ÚTLSRequired©r    ÚtlsrV   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-  FNrq   )r   r½   r  r  r   r¼   rò   r/  r  r  rU   r   rÎ   r  r1  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.
        rq   z<failure xmlns='%s'/>)r   r   rò   r  rY   Ú	TLSFailedrA   r  rn   r"   r"   r#   Útest_failed  s
   
z)TLSInitiatingInitializerTests.test_failedN)r0   r1   r2   rE   r  r	  r   ÚskipWhenNoSSLr  r$  r(  r)  r,  r3  r4  r6  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 rj   )r   Úsucceedr*   r"   r"   r#   r  *  r€   zTestFeatureInitializer.startN)r0   r1   r2   Úfeaturer  r"   r"   r"   r#   r8  '  s    r8  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 rj   )r   r:   r‡   r8  rò   r*   r"   r"   r#   rE   /  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   Ú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)r0   r1   r2   rE   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ý   rC   r7   rF   úuser1@example.comÚtoúuser2@example.com/resourceÚfromÚstanza1rG   rR   N)r   r½   r   Ú
toResponserô   r   ©r    Ú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.
        rB  r7   rF   rC  rD  rF  N)r   r½   r   rH  r   r^   ÚhasAttributerI  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.
        rB  r7   rF   rE  rF  rD  N)r   r½   r   rH  r^   rM  r   rI  r"   r"   r#   Útest_toResponseNoTog  rO  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ý   ÚmessageÚchatrF   rD  rF  N©r   r½   r   rH  r^   rM  rI  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.
        rQ  rG   NrT  rI  r"   r"   r#   Ú	test_noID|  ó   

zToResponseTests.test_noIDc                 C   rV  )zR
        Test that a proper response is generated without type attribute.
        rQ  rF   NrT  rI  r"   r"   r#   Útest_noType„  rX  zToResponseTests.test_noTypeN)	r0   r1   r2   rL  rN  rP  rU  rW  rY  r"   r"   r"   r#   rA  K  s    
rA  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 rj   ©Ú	callbacksr*   r"   r"   r#   Ú__init__’  r€   zDummyFactory.__init__c                 C   s   || j |< d S rj   r\  )r    ÚeventÚcallbackr"   r"   r#   ÚaddBootstrap•  rÄ   zDummyFactory.addBootstrapN)r0   r1   r2   r3   r^  ra  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#   r^  ž  s   
zDummyXMPPHandler.__init__c                 C   s   |   ¡  d S rj   )rÃ   r‹   r"   r"   r#   r?   £  r  zDummyXMPPHandler.makeConnectionc                 C   ó   |  j d7  _ d S ©Nrq   ©re  r*   r"   r"   r#   rÃ   ¦  ó   zDummyXMPPHandler.connectionMadec                 C   rh  ri  )rf  r*   r"   r"   r#   ÚconnectionInitialized©  rk  z&DummyXMPPHandler.connectionInitializedc                 C   rh  ri  )rg  ©r    Úreasonr"   r"   r#   rh   ¬  rk  zDummyXMPPHandler.connectionLostN)	r0   r1   r2   r3   r^  r?   rÃ   rl  rh   r"   r"   r"   r#   rd  ™  s    rd  c                   @   rZ  )ÚFailureReasonXMPPHandlerz>
    Dummy handler specifically for failure Reason tests.
    c                 C   rz   )NF)ÚgotFailureReasonr*   r"   r"   r#   r^  µ  r€   z!FailureReasonXMPPHandler.__init__c                 C   s   t |tjƒrd| _d S d S r{   )Ú
isinstancer   ÚFailurerp  rm  r"   r"   r#   rh   ¸  s   
ÿz'FailureReasonXMPPHandler.connectionLostN)r0   r1   r2   r3   r^  rh   r"   r"   r"   r#   ro  °  rb  ro  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   Ú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 rj   )Úoutlistr*   r"   r"   r#   r^  Î  r€   z?XMPPHandlerTests.test_send.<locals>.DummyStreamManager.__init__c                 S   ó   | j  |¡ d S rj   )ry  rÁ   )r    Údatar"   r"   r#   rL   Ñ  ó   z;XMPPHandlerTests.test_send.<locals>.DummyStreamManager.sendN)r0   r1   r2   r^  rL   r"   r"   r"   r#   ÚDummyStreamManagerÍ  ó    r}  r¹   N)r   ru  ÚparentrL   r   ry  )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   rz   r{   rj  r*   r"   r"   r#   rÃ   ß  r€   zLXMPPHandlerTests.test_makeConnection.<locals>.TestXMPPHandler.connectionMadeN)r0   r1   r2   rÃ   r"   r"   r"   r#   ÚTestXMPPHandlerÞ  rî   r  N)r   ru  r:   r‡   r?   rH   re  rØ   )r    r  r€  rW   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   ru  r:   r‡   r?   rh   Ú	ExceptionrØ   )r    r€  rW   r"   r"   r#   Útest_connectionLostè  s
   
z$XMPPHandlerTests.test_connectionLostN)r0   r1   r2   r3   rv  r¿   r‚  r„  r"   r"   r"   r#   rs  ½  s    rs  c                   @   r   )ÚXMPPHandlerCollectionTestsz7
    Tests for L{xmlstream.XMPPHandlerCollection}.
    c                 C   s   t  ¡ | _d S rj   )r   ÚXMPPHandlerCollectionÚ
collectionr*   r"   r"   r#   rE   ø  rÄ   z XMPPHandlerCollectionTests.setUpc                 C   s   t tj| jƒ dS )zZ
        L{xmlstream.StreamManager} implements L{ijabber.IXMPPHandlerCollection}.
        N)r   r   ÚIXMPPHandlerCollectionr‡  r*   r"   r"   r#   rv  û  s   z)XMPPHandlerCollectionTests.test_interfacec                 C   s4   t ƒ }| | j¡ |  || j¡ |  | j|j¡ dS )z:
        Test the addition of a protocol handler.
        N)rd  ÚsetHandlerParentr‡  rN   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)rd  r‰  r‡  ÚdisownHandlerParentre   rØ   r  rŠ  r"   r"   r#   Útest_removeHandler
  s
   z-XMPPHandlerCollectionTests.test_removeHandlerN)r0   r1   r2   r3   rE   rv  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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 rj   )r[  r   ÚStreamManagerÚstreamManager)r    Úfactoryr"   r"   r#   rE     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ä   )r  rØ   r   r   ÚhandlersÚ
_connectedr‘  r]  Ú_authdÚ_disconnectedÚ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.
        rq   r   N)r  rd  r‰  r   r:   r‡   r“  r   re  rf  rg  ©r    r—  r€  rW   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)
r  rd  r‰  r   r:   r‡   r“  rØ   ÚrawDataInFnÚrawDataOutFnr˜  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)r  Ú
logTrafficrd  r‰  r   r:   r‡   r“  rô   r›  rœ  r˜  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   rq   N)r  rd  r‰  r   r:   r‡   r”  r   re  rf  rg  r˜  r"   r"   r#   Ú
test_authdU  rš  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   rq   N)r  rd  r‰  r   r:   r‡   r•  r   re  rf  rg  r˜  r"   r"   r#   Útest_disconnectedc  rš  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)	r  ro  r‰  r•  r   rr  rƒ  r   rp  ©r    r—  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)r  rd  r‰  r   re  rf  rg  r¢  r"   r"   r#   r‹  |  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.
        rq   r   N)r  r   r:   r‡   r“  r”  rd  r‰  r   re  rf  rg  )r    r—  rW   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   ÚXmlStreamFactoryr‡   r  ÚbuildProtocolr	   r@   rJ   rÃ   rA   ÚdispatchrL   r   rO   ©r    r‘  r—  rW   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‡   r  rd  Ú
addHandlerr¦  r	   r@   rJ   rL   r   rO   Ú_packetQueuerÃ   rA   r§  r^   ©r    r‘  r—  r€  rW   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‡   r  r¦  r	   r@   rJ   rÃ   rA   rL   r   rO   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‡   r  rd  rª  r¦  rÃ   r	   r@   rJ   rh   rL   r   rO   r«  r¬  r"   r"   r#   Útest_sendDisconnectedä  s   





z(StreamManagerTests.test_sendDisconnectedN)r0   r1   r2   r3   rE   r$   r™  r  rŸ  r   r¡  r£  r‹  r¤  r©  r­  r®  r¯  r"   r"   r"   r#   rŽ    s      rŽ  c                   @   rc  )Ú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                   @   rw  )z<XmlStreamServerFactoryTests.setUp.<locals>.TestAuthenticatorc                 S   rx  rj   )Ú
xmlstreamsr*   r"   r"   r#   r^    r€   zEXmlStreamServerFactoryTests.setUp.<locals>.TestAuthenticator.__init__c                 S   rz  rj   )r±  rÁ   r‹   r"   r"   r#   rÊ     r|  zPXmlStreamServerFactoryTests.setUp.<locals>.TestAuthenticator.associateWithStreamN)r0   r1   r2   r^  rÊ   r"   r"   r"   r#   rÌ     r~  rÌ   c                      s   ˆ ƒ S rj   r"   r"   ©rÌ   r"   r#   ÚauthenticatorFactory  s   z?XmlStreamServerFactoryTests.setUp.<locals>.authenticatorFactoryN)r   ÚXmlStreamServerFactoryr‘  )r    r³  r"   r²  r#   rE   ÿ  s   z!XmlStreamServerFactoryTests.setUpc                 C   s   t t| jƒ dS )z<
        L{XmlStreamServerFactory} is a L{Factory}.
        N)r   r   r‘  r*   r"   r"   r#   rv    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)r‘  r¦  r   rD   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)r‘  r¦  rþ   r   r:   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)r‘  r¦  rô   rD   )r    Úxs1Úxs2r"   r"   r#   Útest_buildProtocolTwice*  s   z3XmlStreamServerFactoryTests.test_buildProtocolTwiceN)	r0   r1   r2   r3   rE   rv  rµ  r¶  r¹  r"   r"   r"   r#   r°  ú  s    r°  )9r3   r
   r   Úzope.interface.verifyr   Útwisted.internetr   r   Útwisted.internet.errorr   Útwisted.internet.interfacesr   Útwisted.pythonr   Útwisted.testr	   Útwisted.trialÚtwisted.words.protocols.jabberr   r   r   r   Ú!twisted.words.test.test_xmlstreamr   Útwisted.words.xishr   r   Ú_sslÚImportErrorr7  Útwisted.internet._sslverifyr   Útwisted.internet.sslr   r  ÚTestCaser   r4   ry   rƒ  rÑ   rÒ   râ   rû   r  Ú BaseFeatureInitiatingInitializerr8  r;  rA  r[  ru  rd  ro  rs  r…  rŽ  r°  r"   r"   r"   r#   Ú<module>   sZ   þ  # \CL- 'B6" f