U
    sÍ@g–²  ã                   @   s8  d Z ddlmZmZ ddlmZ ddlmZ ddlm	Z	m
Z
 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 ddlmZ ddlmZmZmZmZ zddlmZ W n ek
rÔ   dZdZ Y nX 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j0ƒZ1G d&d'„ d'ej&ƒZ2G d(d)„ d)ej&ƒZ3G d*d+„ d+e4ƒZ5G d,d-„ d-ej6ƒZ7G d.d/„ d/ej6ƒZ8G d0d1„ d1ej&ƒZ9G d2d3„ d3ej&ƒZ:G d4d5„ d5ej&ƒZ;G d6d7„ d7eƒZ<dS )8z8
Tests for L{twisted.words.protocols.jabber.xmlstream}.
é    )Úabsolute_importÚdivision)Úunittest)ÚverifyObject)ÚdeferÚtask)ÚConnectionLost)ÚIProtocolFactory)Úfailure)Úunicode)Úproto_helpers)Ú!GenericXmlStreamFactoryTestsMixin)Údomish)ÚerrorÚijabberÚjidÚ	xmlstream)ÚsslNzSSL not available)ÚCertificateOptions)ÚClientTLSOptionsúurn:ietf:params:xml:ns:xmpp-tlsc                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚHashPasswordTestsz.
    Tests for L{xmlstream.hashPassword}.
    c                 C   s   t  dd¡}|  d|¡ dS )zS
        The sid and secret are concatenated to calculate sha1 hex digest.
        Ú12345ÚsecretZ(99567ee91b2c7cabf607f10cb9f4a3634fa820e0N©r   ÚhashPasswordÚassertEqual©ÚselfÚhash© r    úI/usr/lib/python3/dist-packages/twisted/words/test/test_jabberxmlstream.pyÚ
test_basic+   s    zHashPasswordTests.test_basicc                 C   s   |   ttjdd¡ dS )zB
        The session identifier must be a unicode object.
        s   Â¹2345r   N©ÚassertRaisesÚ	TypeErrorr   r   ©r   r    r    r!   Útest_sidNotUnicode3   s    ÿz$HashPasswordTests.test_sidNotUnicodec                 C   s   |   ttjdd¡ dS )z8
        The password must be a unicode object.
        r   s   secrÃ©tNr#   r&   r    r    r!   Útest_passwordNotUnicode;   s    ÿz)HashPasswordTests.test_passwordNotUnicodec                 C   s   t  dd¡}|  d|¡ dS )z\
        The concatenated sid and password must be encoded to UTF-8 before hashing.
        r   u   secrÃ©tZ(659bf88d8f8e179081f7f3b4a8e7d224652d2853Nr   r   r    r    r!   Útest_unicodeSecretC   s    z$HashPasswordTests.test_unicodeSecretN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r"   r'   r(   r)   r    r    r    r!   r   &   s
   r   c                   @   sp   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚIQTestszG
    Tests both IQ and the associated IIQResponseTracker callback.
    c                 C   sb   t  d¡}d|_t  |¡| _ t ¡ | _| jj| j _| j  	t
 ¡ ¡ | j  d¡ t  | j d¡| _d S )NZ	otherhostÚtestnszm<stream:stream xmlns:stream='http://etherx.jabber.org/streams' xmlns='testns' from='otherhost' version='1.0'>Úget)r   ÚConnectAuthenticatorÚ	namespaceÚ	XmlStreamr   ZClockÚclockÚ	callLaterZ
_callLaterÚmakeConnectionr   ÚStringTransportÚdataReceivedÚIQÚiq)r   Úauthenticatorr    r    r!   ÚsetUpQ   s    

ÿzIQTests.setUpc                 C   s&   |   | jd d¡ |  | jd ¡ d S )NÚtyper0   Úid)r   r:   Ú
assertTruer&   r    r    r!   Ú	testBasic^   s    zIQTests.testBasicc                 C   sR   | j j ¡  | j ¡  | jd  d¡}|  | j j ¡ d| d d| d g¡ d S )Nr>   zutf-8s   <iq type='get' id='s   '/>s   <iq id='s   ' type='get'/>)r   Ú	transportÚclearr:   ÚsendÚencodeÚassertInÚvalue)r   ZidBytesr    r    r!   ÚtestSendc   s    


þzIQTests.testSendc                    s>   ‡ fdd„}ˆ j  ¡ }| |¡ ˆ j}| dˆ j d  ¡ |S )Nc                    s   ˆ   | d d¡ d S )Nr=   Úresult)r   )rH   r&   r    r!   Úcbn   s    z&IQTests.testResultResponse.<locals>.cbú<iq type='result' id='%s'/>r>   )r:   rC   ÚaddCallbackr   r8   )r   rI   ÚdÚxsr    r&   r!   ÚtestResultResponsem   s    

zIQTests.testResultResponsec                 C   s6   | j  ¡ }|  |tj¡ | j}| d| j d  ¡ |S )Nz<iq type='error' id='%s'/>r>   )r:   rC   ÚassertFailurer   ZStanzaErrorr   r8   ©r   rL   rM   r    r    r!   ÚtestErrorResponsey   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!   rI   ’   s    z*IQTests.testNonTrackedResponse.<locals>.cbz/iqéÿÿÿÿz<iq type='result' id='test'/>N)r   ZupgradeWithIQResponseTrackerrR   ÚiqDeferredsÚaddObserverr8   )r   rM   rI   r    r&   r!   ÚtestNonTrackedResponse‚   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.
        rJ   r>   )r:   rC   r   r8   ÚassertNotInrU   rP   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)r:   rC   r   ÚconnectionLostrO   r   rP   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   r9   rC   Ú
addErrback)r
   rL   ©Úebr   r    r!   r`   ¼   s    z'IQTests.testNoModifyingDict.<locals>.ebrZ   )r:   rC   r^   r   r[   ©r   rL   r    r_   r!   ÚtestNoModifyingDict¶   s
    

zIQTests.testNoModifyingDictc                 C   sP   d| j _| j  ¡ }|  |tj¡ | j ddg¡ |  | jj	¡ |  | jj
¡ |S )zK
        Test that an iq request with a defined timeout times out.
        é<   é   )r:   ÚtimeoutrC   rO   r   ÚTimeoutErrorr4   ÚpumprR   ÚcallsrU   ra   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.
        rc   rd   rJ   r>   )
r:   re   rC   r4   r5   r   r8   rg   rR   rh   ra   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.
        rc   rZ   )
r:   re   rC   r   r[   rO   r   rR   r4   rh   rP   r    r    r!   Ú!testDisconnectTimeoutCancellationâ   s    

z)IQTests.testDisconnectTimeoutCancellationN)r*   r+   r,   r-   r<   r@   rG   rN   rQ   rW   rY   r\   rb   ri   rj   rk   r    r    r    r!   r.   L   s   
	r.   c                   @   sŒ   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!S )"ÚXmlStreamTestsc                 C   s
   d| _ d S ©NT)ÚgotStreamStart©r   Úobjr    r    r!   ÚonStreamStartõ   s    zXmlStreamTests.onStreamStartc                 C   s
   d| _ d S rm   )ÚgotStreamEndro   r    r    r!   ÚonStreamEndù   s    zXmlStreamTests.onStreamEndc                 C   s
   d| _ d S rm   )ÚgotStreamErrorro   r    r    r!   ÚonStreamErrorý   s    zXmlStreamTests.onStreamErrorc                 C   sv   d| _ d| _d| _t t ¡ ¡}| d| j¡ | d| j¡ | d| j	¡ | 
t ¡ ¡ ||j_d|_d|_|| _dS )z9
        Set up XmlStream and several observers.
        Fz//event/stream/startú//event/stream/endz//event/stream/errorr/   ©rd   r   N)rn   rr   rt   r   r3   ÚAuthenticatorrV   rq   rs   ru   r6   r   Z StringTransportWithDisconnectionrA   Zprotocolr2   Úversion©r   rM   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   rT   ó    ó   <stream:streamó/   xmlns:stream='http://etherx.jabber.org/streams'ó   xmlns='testns'ó   version='1.0'N)r   Ú
sendHeaderrA   rF   ÚsplitrE   r?   Ú_headerSent©r   rM   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.
        Zdbzjabber:server:dialbackr   rT   r{   r|   r}   s!   xmlns:db='jabber:server:dialback'r~   r   N)	r   Úprefixesr€   rA   rF   r   rE   r?   r‚   rƒ   r    r    r!   Ú#test_sendHeaderAdditionalNamespaces"  s    
ÿz2XmlStreamTests.test_sendHeaderAdditionalNamespacesc                 C   s`   | j }t d¡|_t d¡|_d|_| ¡  |j ¡ dd…  	d¡}|  
d|¡ |  
d|¡ d	S )
z;
        Test addressing when initiating a stream.
        ÚthisHostÚ	otherHostTr   rT   r{   ó   to='otherhost'ó   from='thishost'N)r   r   ÚJIDÚ
thisEntityÚotherEntityÚ
initiatingr€   rA   rF   r   rE   rƒ   r    r    r!   Útest_sendHeaderInitiating3  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   rT   r{   r‰   rŠ   s   id='session01'N)r   r   r‹   rŒ   r   rŽ   Úsidr€   rA   rF   r   rE   rƒ   r    r    r!   Útest_sendHeaderReceivingA  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   r8   r?   rt   rr   rz   r    r    r!   Útest_receiveStreamErrorQ  s
    

z&XmlStreamTests.test_receiveStreamErrorc                 C   sP   | j }d|_| ¡  |j ¡  | t d¡¡ |  d|j 	¡ ¡ |  
| j¡ dS )z“
        Test sendStreamError on an initiating xmlstream with a header sent.

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

        In this case, no header should be generated. Also, the error should
        not be sent out on the stream. Just closing the connection.
        Tr”   r•   N)r   rŽ   rA   rB   r—   r   r˜   Ú	assertNotr‚   r   rF   r?   rr   rz   r    r    r!   Ú&test_sendStreamErrorInitiatingNoHeaderm  s    
z5XmlStreamTests.test_sendStreamErrorInitiatingNoHeaderc                 C   sP   | j }d|_| ¡  |j ¡  | t d¡¡ |  d|j 	¡ ¡ |  
| j¡ dS )z‘
        Test sendStreamError on a receiving xmlstream with a header sent.

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

        In this case, a header should be generated. Then, the error should
        be sent out on the stream followed by closing the connection.
        Fr”   r•   N)r   rŽ   rA   rB   r—   r   r˜   r?   r‚   r™   rF   rr   rz   r    r    r!   Ú%test_sendStreamErrorReceivingNoHeader  s    
z4XmlStreamTests.test_sendStreamErrorReceivingNoHeaderc                 C   s:   | j }| ¡  |j}| ¡  |  ||j¡ |  |j¡ dS )zE
        Test resetting the XML stream to start a new layer.
        N)r   r€   ÚstreamÚresetr™   r›   r‚   )r   rM   rŸ   r    r    r!   Ú
test_resetž  s    zXmlStreamTests.test_resetc                 C   s†   | j }| d¡ |  |j ¡ d¡ |j ¡  t d¡}| |¡ |  |j ¡ d¡ |j ¡  t d¡}| |¡ |  |j ¡ d¡ dS )z:
        Test send with various types of objects.
        ú<presence/>ó   <presence/>)r/   Zpresence)z http://etherx.jabber.org/streamsÚfeaturess   <stream:features/>N)r   rC   r   rA   rF   rB   r   ÚElement)r   rM   Zelr    r    r!   Ú	test_sendª  s    






zXmlStreamTests.test_sendc                    sŠ   g ‰g ‰g ‰ G ‡ ‡‡fdd„dƒ}|ƒ }t  |¡}|  |gˆ ¡ | ¡  |  dgˆ¡ | d¡ |  dtˆƒ¡ | ¡  |  dgˆ¡ dS )zM
        Test that the associated authenticator is correctly called.
        c                       s0   e Zd Z‡fdd„Z‡fdd„Z‡ fdd„ZdS )z<XmlStreamTests.test_authenticator.<locals>.TestAuthenticatorc                    s   ˆ   d ¡ d S r]   ©Úappendr&   )ÚconnectionMadeCallsr    r!   ÚconnectionMadeÆ  s    zKXmlStreamTests.test_authenticator.<locals>.TestAuthenticator.connectionMadec                    s   ˆ   |¡ d S r]   r§   )r   ZrootElement)ÚstreamStartedCallsr    r!   ÚstreamStartedÉ  s    zJXmlStreamTests.test_authenticator.<locals>.TestAuthenticator.streamStartedc                    s   ˆ   |¡ d S r]   r§   rz   )ÚassociateWithStreamCallsr    r!   ÚassociateWithStreamÌ  s    zPXmlStreamTests.test_authenticator.<locals>.TestAuthenticator.associateWithStreamN)r*   r+   r,   rª   r¬   r®   r    ©r­   r©   r«   r    r!   ÚTestAuthenticatorÅ  s   r°   Nús<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' from='example.com' id='12345'>rd   )r   r3   r   rª   r8   Úlenr    )r   r°   ÚarM   r    r¯   r!   Útest_authenticator½  s    


z!XmlStreamTests.test_authenticatorN)r*   r+   r,   rq   rs   ru   r<   r„   r†   r   r‘   r“   rš   rœ   r   rž   r¡   r¦   r´   r    r    r    r!   rl   ó   s    rl   c                   @   s   e Zd ZdS )Ú	TestErrorN)r*   r+   r,   r    r    r    r!   rµ   Ý  s   rµ   c                   @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚAuthenticatorTestsc                 C   s   t  ¡ | _t  | j¡| _ d S r]   )r   rx   r;   r3   r&   r    r    r!   r<   ã  s    
zAuthenticatorTests.setUpc                 C   sh   | j }| t ¡ ¡ | d¡ |  d|j¡ |  d|j¡ |  d|j	¡ |  d|j
¡ |  d|j¡ dS )úe
        Test streamStart to fill the appropriate attributes from the
        stream header.
        ú’<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' from='example.org' to='example.com' id='12345' version='1.0'>rw   NZinvalid)r   r6   r   r7   r8   r   ry   ÚassertIdenticalr   r2   r   rŒ   rz   r    r    r!   Útest_streamStartè  s    
z#AuthenticatorTests.test_streamStartc                 C   s0   | j }| t ¡ ¡ | d¡ |  d|j¡ dS )z
        Test streamStart to fill the appropriate attributes from the
        stream header for a pre-XMPP-1.0 header.
        r±   ©r   r   N©r   r6   r   r7   r8   r   ry   rz   r    r    r!   Útest_streamStartLegacyú  s    
z)AuthenticatorTests.test_streamStartLegacyc                 C   s0   | j }| t ¡ ¡ | d¡ |  d|j¡ dS )z‡
        Test streamStart to fill the appropriate attributes from the
        stream header for a version with only one digit.
        z<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' from='example.com' id='12345' version='1'>r»   Nr¼   rz   r    r    r!   Útest_streamBadVersionOneDigit  s    
z0AuthenticatorTests.test_streamBadVersionOneDigitc                 C   s0   | j }| t ¡ ¡ | d¡ |  d|j¡ dS )z}
        Test streamStart to fill the appropriate attributes from the
        stream header for a malformed version.
        z‚<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' from='example.com' id='12345' version='blah'>r»   Nr¼   rz   r    r    r!   Útest_streamBadVersionNoNumber  s    
z0AuthenticatorTests.test_streamBadVersionNoNumberN)r*   r+   r,   r<   rº   r½   r¾   r¿   r    r    r    r!   r¶   â  s
   r¶   c                   @   s<   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )ÚConnectAuthenticatorTestsc                 C   sJ   d| _ d | _t d¡| _t | j¡| _| j d| j¡ | j d| j¡ d S )NFrˆ   ú//event/stream/authdú//event/xmpp/initfailed)	ÚgotAuthenticatedÚinitFailurer   r1   r;   r3   rV   ÚonAuthenticatedÚonInitFailedr&   r    r    r!   r<   $  s    zConnectAuthenticatorTests.setUpc                 C   s
   d| _ d S rm   )rÃ   ro   r    r    r!   rÅ   -  s    z)ConnectAuthenticatorTests.onAuthenticatedc                 C   s
   || _ d S r]   )rÄ   )r   r
   r    r    r!   rÆ   1  s    z&ConnectAuthenticatorTests.onInitFailedc                 C   sH   G dd„ dƒ}|ƒ }|g| j _| j ¡  |  g | j j¡ |  | j¡ dS )zG
        Test successful completion of an initialization step.
        c                   @   s   e Zd Zdd„ ZdS )z9ConnectAuthenticatorTests.testSucces.<locals>.Initializerc                 S   s   d S r]   r    r&   r    r    r!   Ú
initialize:  s    zDConnectAuthenticatorTests.testSucces.<locals>.Initializer.initializeN©r*   r+   r,   rÇ   r    r    r    r!   ÚInitializer9  s   rÉ   N)r   Úinitializersr;   ÚinitializeStreamr   r?   rÃ   ©r   rÉ   Úinitr    r    r!   Ú
testSucces5  s    

z$ConnectAuthenticatorTests.testSuccesc                 C   sj   G dd„ dƒ}|ƒ }|g| j _| j ¡  |  |g| j j¡ |  | j¡ |  d| j¡ |  	| j 
t¡¡ dS )z9
        Test failure of an initialization step.
        c                   @   s   e Zd Zdd„ ZdS )z:ConnectAuthenticatorTests.testFailure.<locals>.Initializerc                 S   s   t ‚d S r]   )rµ   r&   r    r    r!   rÇ   J  s    zEConnectAuthenticatorTests.testFailure.<locals>.Initializer.initializeNrÈ   r    r    r    r!   rÉ   I  s   rÉ   N)r   rÊ   r;   rË   r   rR   rÃ   ÚassertNotIdenticalrÄ   r?   Zcheckrµ   rÌ   r    r    r!   ÚtestFailureE  s    

z%ConnectAuthenticatorTests.testFailurec                 C   s¢   d| j _| j}| t ¡ ¡ | d¡ |  d|j¡ |  d|j	¡ |  d|j¡ |  d|j
j¡ |  d|j¡ |  | j¡ | d¡ |  d|j¡ |  | j¡ dS )	r·   r/   z’<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' from='example.com' to='example.org' id='12345' version='1.0'>rw   r   úexample.comNz9<stream:features><test xmlns='testns'/></stream:features>©r/   Ztest)r;   r2   r   r6   r   r7   r8   r   ry   r   r   Úhostr¹   rŒ   r›   rÃ   rE   r¤   r?   rz   r    r    r!   rº   W  s    

z*ConnectAuthenticatorTests.test_streamStartN)	r*   r+   r,   r<   rÅ   rÆ   rÎ   rÐ   rº   r    r    r    r!   rÀ   "  s   	rÀ   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚListenAuthenticatorTestsz4
    Tests for L{xmlstream.ListenAuthenticator}
    c                 C   s   t  ¡ | _t  | j¡| _ d S r]   )r   ZListenAuthenticatorr;   r3   r&   r    r    r!   r<   v  s    
zListenAuthenticatorTests.setUpc                 C   s†   | j }| t ¡ ¡ |  d|j¡ | d¡ |  d|j¡ |  	d|j¡ |  
d|j¡ |  d|j¡ |  d|j¡ |  d|jj¡ dS )r·   Nr¸   rw   r   újabber:clientrÑ   )r   r6   r   r7   r¹   r   r8   r   ry   rÏ   r™   r2   r   rŒ   rÓ   rz   r    r    r!   rº   {  s    
z)ListenAuthenticatorTests.test_streamStartc                 C   s0   | j }| t ¡ ¡ | d¡ |  |jt¡ dS )zD
        The generated session id must be a unicode object.
        r¸   N)r   r6   r   r7   r8   ÚassertIsInstancer   r   rz   r    r    r!   Ú test_streamStartUnicodeSessionID  s    
z9ListenAuthenticatorTests.test_streamStartUnicodeSessionIDN)r*   r+   r,   r-   r<   rº   r×   r    r    r    r!   rÔ   q  s   rÔ   c                   @   sv   e Zd Zd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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   ÚsavedSSLr1   r;   r3   r¨   rC   rª   r8   ÚTLSInitiatingInitializerrÍ   r&   r    r    r!   r<   ž  s    
z#TLSInitiatingInitializerTests.setUpc                 C   s   | j t_d S r]   )rÛ   r   r   r&   r    r    r!   ÚtearDown®  s    z&TLSInitiatingInitializerTests.tearDownc                 C   s$   t j| j dd| _|  | jj¡ dS )z>
        Passing required sets the instance variable.
        T)ÚrequiredN)r   rÜ   rÍ   r?   rÞ   r&   r    r    r!   Útest_initRequired²  s    ÿz/TLSInitiatingInitializerTests.test_initRequiredc                    s¸   t  ¡ ˆ j_‡ fdd„ˆ jj_‡ fdd„ˆ j_‡ fdd„ˆ j_ˆ j ¡ }| 	ˆ j
tj¡ ˆ  
dtˆ jƒ¡ ˆ jd }ˆ  
d|j¡ ˆ  
t|j¡ ˆ j dt ¡ ˆ  
d	d
dgˆ j¡ |S )zN
        When TLS is wanted and SSL available, StartTLS is initiated.
        c                    s   ˆ j  d¡S ©NÚTLS©rÚ   r¨   )Úctxr&   r    r!   Ú<lambda>À  r•   zDTLSInitiatingInitializerTests.test_wantedSupported.<locals>.<lambda>c                      s   ˆ j  d¡S ©Nr    râ   r    r&   r    r!   rä   Á  r•   c                      s   ˆ j  d¡S ©NÚheaderrâ   r    r&   r    r!   rä   Â  r•   é   rd   Ústarttlsú<proceed xmlns='%s'/>rá   r    rç   )r   r7   r   rA   ÚstartTLSr    r€   rÍ   ÚstartrK   r   ZResetr²   rÙ   ÚnameÚNS_XMPP_TLSÚurir8   rÚ   )r   rL   ré   r    r&   r!   Útest_wantedSupported»  s    

z2TLSInitiatingInitializerTests.test_wantedSupportedc                    st   ‡ fdd„}t  ¡ ˆ j_|ˆ jj_‡ fdd„ˆ j_‡ fdd„ˆ j_ˆ j ¡ }ˆ j 	dt
 ¡ ˆ  ddd	gˆ j¡ |S )
z:
        The server certificate will be verified.
        c                    s*   ˆ   | t¡ ˆ  | jd¡ ˆ j d¡ d S )NrÑ   rá   )rÖ   r   r   Z	_hostnamerÚ   r¨   ©ÚcontextFactoryr&   r    r!   ÚfakeStartTLS×  s    zJTLSInitiatingInitializerTests.test_certificateVerify.<locals>.fakeStartTLSc                      s   ˆ j  d¡S rå   râ   r    r&   r    r!   rä   Þ  r•   zFTLSInitiatingInitializerTests.test_certificateVerify.<locals>.<lambda>c                      s   ˆ j  d¡S ræ   râ   r    r&   r    r!   rä   ß  r•   rê   rá   r    rç   )r   r7   r   rA   rë   r    r€   rÍ   rì   r8   rî   r   rÚ   ©r   ró   rL   r    r&   r!   Útest_certificateVerifyÒ  s    

z4TLSInitiatingInitializerTests.test_certificateVerifyc                    s–   t ƒ ‰ tjˆjˆ dˆ_ˆ ˆj_‡ ‡fdd„}t ¡ ˆj_|ˆjj_‡fdd„ˆj_	‡fdd„ˆj_
ˆj ¡ }ˆj dt ¡ ˆ dd	d
gˆj¡ |S )zH
        A custom contextFactory is passed through to startTLS.
        )ZconfigurationForTLSc                    s   ˆ  ˆ | ¡ ˆj d¡ d S rà   )ZassertIsrÚ   r¨   rñ   ©rã   r   r    r!   ró   ó  s    zQTLSInitiatingInitializerTests.test_certificateVerifyContext.<locals>.fakeStartTLSc                      s   ˆ j  d¡S rå   râ   r    r&   r    r!   rä   ù  r•   zMTLSInitiatingInitializerTests.test_certificateVerifyContext.<locals>.<lambda>c                      s   ˆ j  d¡S ræ   râ   r    r&   r    r!   rä   ú  r•   rê   rá   r    rç   )r   r   rÜ   rÍ   rò   r   r7   rA   rë   r    r€   rì   r8   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.
        NFrd   )	r   r   rÍ   rÞ   rì   rK   r   r²   rÙ   ra   r    r    r!   Ú"test_wantedNotSupportedNotRequired  s    
z@TLSInitiatingInitializerTests.test_wantedNotSupportedNotRequiredc                 C   s<   dt _d| j_| j ¡ }|  |t j¡ |  dt| j	ƒ¡ |S )zS
        TLSNotSupported is raised when TLS is required but not available.
        NTrd   )
r   r   rÍ   rÞ   rì   rO   ZTLSNotSupportedr   r²   rÙ   ra   r    r    r!   Útest_wantedNotSupportedRequired  s    
z=TLSInitiatingInitializerTests.test_wantedNotSupportedRequiredc                 C   s^   t  d¡}| d¡ |j|jf|i| j_d| j_| j 	¡ }|  
dt| jƒ¡ |  |tj¡ |S )zW
        TLSRequired is raised when TLS is not wanted, but required by server.
        ©r   ré   rÞ   Frd   )r   r¥   Z
addElementrï   rí   r   r¤   rÍ   Úwantedrì   r   r²   rÙ   rO   ZTLSRequired©r   ZtlsrL   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ú   FNrd   )r   r¥   rï   rí   r   r¤   rÍ   rû   rÞ   rì   rK   r   r²   rÙ   rü   r    r    r!   Útest_notWantedNotRequired0  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.
        rd   z<failure xmlns='%s'/>)r   r   rÍ   rì   rO   Z	TLSFailedr8   rî   ra   r    r    r!   Útest_failed?  s
    
z)TLSInitiatingInitializerTests.test_failedN)r*   r+   r,   r<   rÝ   rß   rð   ÚskipWhenNoSSLÚskiprõ   r÷   rø   rù   rý   rþ   rÿ   r    r    r    r!   rØ     s   	rØ   c                   @   s   e Zd ZdZdd„ ZdS )ÚTestFeatureInitializerrÒ   c                 C   s
   t  d ¡S r]   )r   Zsucceedr&   r    r    r!   rì   R  s    zTestFeatureInitializer.startN)r*   r+   r,   Úfeaturerì   r    r    r    r!   r  O  s   r  c                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
Ú%BaseFeatureInitiatingInitializerTestsc                 C   s    t  t  ¡ ¡| _ t| j ƒ| _d S r]   )r   r3   rx   r  rÍ   r&   r    r    r!   r<   Y  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!   ÚtestAdvertized^  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!   ÚtestNotAdvertizedRequiredg  s    z?BaseFeatureInitiatingInitializerTests.testNotAdvertizedRequiredc                 C   s   d| j _|  d| j  ¡ ¡ dS )z
        Test that when the feature is not advertized, and not required by the
        initializer, the initializer silently succeeds.
        FN)rÍ   rÞ   r¹   rÇ   r&   r    r    r!   ÚtestNotAdvertizedNotRequiredp  s    zBBaseFeatureInitiatingInitializerTests.testNotAdvertizedNotRequiredN)r*   r+   r,   r<   r  r  r  r    r    r    r!   r  W  s   		r  c                   @   s<   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )ÚToResponseTestsc                 C   s†   t  d¡}d|d< d|d< d|d< d|d	< t |d
¡}|  ||¡ |  |d d¡ |  |d d¡ |  |d d
¡ |  |d	 d¡ dS )zS
        Test that a response stanza is generated with addressing swapped.
        ©rÕ   r:   r0   r=   úuser1@example.comÚtoúuser2@example.com/resourceÚfromZstanza1r>   rH   N)r   r¥   r   Ú
toResponserÏ   r   ©r   ZstanzaZresponser    r    r!   Útest_toResponse|  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	  r0   r=   r
  r  r  N)r   r¥   r   r  r   rR   ÚhasAttributer  r    r    r!   Útest_toResponseNoFrom  s    

z%ToResponseTests.test_toResponseNoFromc                 C   sH   t  d¡}d|d< d|d< t |¡}|  | d¡¡ |  |d d¡ dS )zW
        Test that a response is generated from a stanza without a to address.
        r	  r0   r=   r  r  r  N)r   r¥   r   r  rR   r  r   r  r    r    r!   Útest_toResponseNoTo™  s    

z#ToResponseTests.test_toResponseNoToc                 C   s@   t  d¡}d|d< t |¡}|  | d¡¡ |  | d¡¡ dS )zY
        Test that a response is generated from a stanza without any addressing.
        ©rÕ   ÚmessageZchatr=   r  r  N©r   r¥   r   r  rR   r  r  r    r    r!   Útest_toResponseNoAddressing¥  s
    

z+ToResponseTests.test_toResponseNoAddressingc                 C   s(   t  d¡}t |¡}|  | d¡¡ dS )zP
        Test that a proper response is generated without id attribute.
        r  r>   Nr  r  r    r    r!   Ú	test_noID°  s    

zToResponseTests.test_noIDc                 C   s(   t  d¡}t |¡}|  | d¡¡ dS )zR
        Test that a proper response is generated without type attribute.
        r  r=   Nr  r  r    r    r!   Útest_noType¹  s    

zToResponseTests.test_noTypeN)	r*   r+   r,   r  r  r  r  r  r  r    r    r    r!   r  z  s   	r  c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚDummyFactoryzJ
    Dummy XmlStream factory that only registers bootstrap observers.
    c                 C   s
   i | _ d S r]   ©Ú	callbacksr&   r    r    r!   Ú__init__Æ  s    zDummyFactory.__init__c                 C   s   || j |< d S r]   r  )r   ZeventÚcallbackr    r    r!   ÚaddBootstrapÊ  s    zDummyFactory.addBootstrapN)r*   r+   r,   r-   r  r  r    r    r    r!   r  Â  s   r  c                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚDummyXMPPHandlerzO
    Dummy XMPP subprotocol handler to count the methods are called on it.
    c                 C   s   d| _ d| _d| _d S )Nr   )ÚdoneMadeÚdoneInitializedÚdoneLostr&   r    r    r!   r  Ó  s    zDummyXMPPHandler.__init__c                 C   s   |   ¡  d S r]   )rª   rz   r    r    r!   r6   Ù  s    zDummyXMPPHandler.makeConnectionc                 C   s   |  j d7  _ d S ©Nrd   ©r!  r&   r    r    r!   rª   Ý  s    zDummyXMPPHandler.connectionMadec                 C   s   |  j d7  _ d S r$  )r"  r&   r    r    r!   ÚconnectionInitializedá  s    z&DummyXMPPHandler.connectionInitializedc                 C   s   |  j d7  _ d S r$  )r#  ©r   Úreasonr    r    r!   r[   å  s    zDummyXMPPHandler.connectionLostN)	r*   r+   r,   r-   r  r6   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S )ÚFailureReasonXMPPHandlerz>
    Dummy handler specifically for failure Reason tests.
    c                 C   s
   d| _ d S )NF)ÚgotFailureReasonr&   r    r    r!   r  î  s    z!FailureReasonXMPPHandler.__init__c                 C   s   t |tjƒrd| _d S rm   )Ú
isinstancer
   ÚFailurer*  r'  r    r    r!   r[   ò  s    z'FailureReasonXMPPHandler.connectionLostN)r*   r+   r,   r-   r  r[   r    r    r    r!   r)  ê  s   r)  c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚXMPPHandlerTestsz-
    Tests for L{xmlstream.XMPPHandler}.
    c                 C   s   t tjt ¡ ƒ dS )zN
        L{xmlstream.XMPPHandler} implements L{ijabber.IXMPPHandler}.
        N)r   r   ZIXMPPHandlerr   ÚXMPPHandlerr&   r    r    r!   Útest_interfaceý  s    zXMPPHandlerTests.test_interfacec                 C   s@   G dd„ dt ƒ}t ¡ }|ƒ |_| d¡ |  dg|jj¡ dS )zP
        Test that data is passed on for sending by the stream manager.
        c                   @   s   e Zd Zdd„ Zdd„ ZdS )z6XMPPHandlerTests.test_send.<locals>.DummyStreamManagerc                 S   s
   g | _ d S r]   )Úoutlistr&   r    r    r!   r  	  s    z?XMPPHandlerTests.test_send.<locals>.DummyStreamManager.__init__c                 S   s   | j  |¡ d S r]   )r0  r¨   )r   Údatar    r    r!   rC     s    z;XMPPHandlerTests.test_send.<locals>.DummyStreamManager.sendN)r*   r+   r,   r  rC   r    r    r    r!   ÚDummyStreamManager  s   r2  r¢   N)Úobjectr   r.  ÚparentrC   r   r0  )r   r2  Úhandlerr    r    r!   r¦     s
    
zXMPPHandlerTests.test_sendc                 C   sN   G dd„ dt jƒ}|ƒ }t  t  ¡ ¡}| |¡ |  |j¡ |  ||j ¡ dS )zY
        Test that makeConnection saves the XML stream and calls connectionMade.
        c                   @   s   e Zd Zdd„ ZdS )z=XMPPHandlerTests.test_makeConnection.<locals>.TestXMPPHandlerc                 S   s
   d| _ d S rm   r%  r&   r    r    r!   rª     s    zLXMPPHandlerTests.test_makeConnection.<locals>.TestXMPPHandler.connectionMadeN)r*   r+   r,   rª   r    r    r    r!   ÚTestXMPPHandler  s   r6  N)r   r.  r3   rx   r6   r?   r!  r¹   )r   r6  r5  rM   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   r.  r3   rx   r6   r[   Ú	Exceptionr¹   )r   r5  rM   r    r    r!   Útest_connectionLost$  s
    
z$XMPPHandlerTests.test_connectionLostN)r*   r+   r,   r-   r/  r¦   r7  r9  r    r    r    r!   r-  ø  s
   r-  c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚXMPPHandlerCollectionTestsz7
    Tests for L{xmlstream.XMPPHandlerCollection}.
    c                 C   s   t  ¡ | _d S r]   )r   ZXMPPHandlerCollectionÚ
collectionr&   r    r    r!   r<   5  s    z XMPPHandlerCollectionTests.setUpc                 C   s   t tj| jƒ dS )zZ
        L{xmlstream.StreamManager} implements L{ijabber.IXMPPHandlerCollection}.
        N)r   r   ZIXMPPHandlerCollectionr;  r&   r    r    r!   r/  9  s    z)XMPPHandlerCollectionTests.test_interfacec                 C   s4   t ƒ }| | j¡ |  || j¡ |  | j|j¡ dS )z:
        Test the addition of a protocol handler.
        N)r   ÚsetHandlerParentr;  rE   r¹   r4  ©r   r5  r    r    r!   Útest_addHandler@  s    z*XMPPHandlerCollectionTests.test_addHandlerc                 C   s>   t ƒ }| | j¡ | | j¡ |  || j¡ |  d|j¡ dS )z5
        Test removal of a protocol handler.
        N)r   r<  r;  ZdisownHandlerParentrX   r¹   r4  r=  r    r    r!   Útest_removeHandlerJ  s
    z-XMPPHandlerCollectionTests.test_removeHandlerN)r*   r+   r,   r-   r<   r/  r>  r?  r    r    r    r!   r:  0  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 r]   )r  r   ÚStreamManagerÚstreamManager)r   Úfactoryr    r    r!   r<   [  s    zStreamManagerTests.setUpc                 C   s~   | j }|  d|j¡ |  g |j¡ |  |j|jjd ¡ |  |j|jjd ¡ |  |j	|jjd ¡ |  |j
|jjd ¡ dS )zM
        Test correct initialization and setup of factory observers.
        Nz//event/stream/connectedrÁ   rv   rÂ   )rB  r¹   r   r   ZhandlersÚ
_connectedrC  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.
        rd   r   N)rB  r   r<  r   r3   rx   rD  r   r!  r"  r#  ©r   rG  r5  rM   r    r    r!   Útest_connectedq  s    

z!StreamManagerTests.test_connectedc                 C   sN   | j }tƒ }| |¡ t t ¡ ¡}| |¡ |  d|j¡ |  d|j	¡ dS )zP
        Test raw data functions unset when logTraffic is set to False.
        N)
rB  r   r<  r   r3   rx   rD  r¹   ÚrawDataInFnÚrawDataOutFnrH  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)rB  Z
logTrafficr   r<  r   r3   rx   rD  rÏ   rJ  rK  rH  r    r    r!   Útest_connectedLogTrafficTrue  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   rd   N)rB  r   r<  r   r3   rx   rE  r   r!  r"  r#  rH  r    r    r!   Ú
test_authd›  s    

zStreamManagerTests.test_authdc                 C   s\   | j }tƒ }| |¡ t t ¡ ¡}| |¡ |  d|j¡ |  d|j	¡ |  d|j
¡ dS )z‚
        Test that protocol handlers have their connectionLost method
        called when the XML stream is disconnected.
        r   rd   N)rB  r   r<  r   r3   rx   rF  r   r!  r"  r#  rH  r    r    r!   Útest_disconnectedª  s    

z$StreamManagerTests.test_disconnectedc                 C   s<   | j }tƒ }| |¡ | t tdƒ¡¡ |  d|j¡ dS )zŸ
        A L{STREAM_END_EVENT} results in L{StreamManager} firing the handlers
        L{connectionLost} methods, passing a L{failure.Failure} reason.
        z	no reasonTN)	rB  r)  r<  rF  r
   r,  r8  r   r*  ©r   rG  r5  r    r    r!   Útest_disconnectedReason¹  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)rB  r   r<  r   r!  r"  r#  rP  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.
        rd   r   N)rB  r   r3   rx   rD  rE  r   r<  r   r!  r"  r#  )r   rG  rM   r5  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   ÚXmlStreamFactoryrx   rA  ÚbuildProtocolr   r7   rA   rª   r8   ÚdispatchrC   r   rF   ©r   rC  rG  rM   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   rS  rx   rA  r   Ú
addHandlerrT  r   r7   rA   rC   r   rF   Ú_packetQueuerª   r8   rU  rR   ©r   rC  rG  r5  rM   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   rS  rx   rA  rT  r   r7   rA   rª   r8   rC   r   rF   rY  rV  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   rS  rx   rA  r   rX  rT  rª   r   r7   rA   r[   rC   r   rF   rY  rZ  r    r    r!   Útest_sendDisconnected,  s    





z(StreamManagerTests.test_sendDisconnectedN)r*   r+   r,   r-   r<   r"   rI  rL  rM  rN  rO  rQ  r>  rR  rW  r[  r\  r]  r    r    r    r!   r@  V  s   r@  c                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚXmlStreamServerFactoryTestsz8
    Tests for L{xmlstream.XmlStreamServerFactory}.
    c                    s,   G dd„ dt ƒ‰ ‡ fdd„}t |¡| _dS )zQ
        Set up a server factory with an authenticator factory function.
        c                   @   s   e Zd Zdd„ Zdd„ ZdS )z<XmlStreamServerFactoryTests.setUp.<locals>.TestAuthenticatorc                 S   s
   g | _ d S r]   )Ú
xmlstreamsr&   r    r    r!   r  M  s    zEXmlStreamServerFactoryTests.setUp.<locals>.TestAuthenticator.__init__c                 S   s   | j  |¡ d S r]   )r_  r¨   rz   r    r    r!   r®   P  s    zPXmlStreamServerFactoryTests.setUp.<locals>.TestAuthenticator.associateWithStreamN)r*   r+   r,   r  r®   r    r    r    r!   r°   L  s   r°   c                      s   ˆ ƒ S r]   r    r    ©r°   r    r!   ÚauthenticatorFactoryS  s    z?XmlStreamServerFactoryTests.setUp.<locals>.authenticatorFactoryN)r3  r   ZXmlStreamServerFactoryrC  )r   ra  r    r`  r!   r<   H  s    z!XmlStreamServerFactoryTests.setUpc                 C   s   t t| jƒ dS )z<
        L{XmlStreamServerFactory} is a L{Factory}.
        N)r   r	   rC  r&   r    r    r!   r/  Y  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)rC  rT  r   r;   r_  rz   r    r    r!   Ú,test_buildProtocolAuthenticatorInstantiation`  s    
zHXmlStreamServerFactoryTests.test_buildProtocolAuthenticatorInstantiationc                 C   s   | j  d¡}|  |tj¡ dS )zV
        The protocol factory creates Jabber XML Stream protocols by default.
        N)rC  rT  rÖ   r   r3   rz   r    r    r!   Útest_buildProtocolXmlStreamn  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)rC  rT  rÏ   r;   )r   Zxs1Zxs2r    r    r!   Útest_buildProtocolTwicev  s    z3XmlStreamServerFactoryTests.test_buildProtocolTwiceN)	r*   r+   r,   r-   r<   r/  rb  rc  rd  r    r    r    r!   r^  C  s   r^  )=r-   Z
__future__r   r   Ztwisted.trialr   Zzope.interface.verifyr   Ztwisted.internetr   r   Ztwisted.internet.errorr   Ztwisted.internet.interfacesr	   Ztwisted.pythonr
   Ztwisted.python.compatr   Ztwisted.testr   Z!twisted.words.test.test_xmlstreamr   Ztwisted.words.xishr   Ztwisted.words.protocols.jabberr   r   r   r   r   ÚImportErrorr   Ztwisted.internet.sslr   Ztwisted.internet._sslverifyr   rî   ZTestCaser   r.   rl   r8  rµ   r¶   rÀ   rÔ   rØ   Z BaseFeatureInitiatingInitializerr  r  r  r3  r  r.  r   r)  r-  r:  r@  r^  r    r    r    r!   Ú<module>   sV   
& ( k@O, 3#H8& n