HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //usr/lib/python3/dist-packages/twisted/words/test/__pycache__/test_jabberxmlstream.cpython-38.pyc
U

s�@g���@s8dZddlmZmZddlmZddlmZddlm	Z	m
Z
ddlmZddl
mZddlmZdd	lmZdd
lmZddlmZddlmZdd
lmZmZmZmZzddlmZWnek
r�dZdZ YnXdZ ddl!m"Z"ddl#m$Z$dZ%Gdd�dej&�Z'Gdd�dej&�Z(Gdd�dej&�Z)Gdd�de*�Z+Gdd�dej&�Z,Gdd�dej&�Z-Gd d!�d!ej&�Z.Gd"d#�d#ej&�Z/Gd$d%�d%ej0�Z1Gd&d'�d'ej&�Z2Gd(d)�d)ej&�Z3Gd*d+�d+e4�Z5Gd,d-�d-ej6�Z7Gd.d/�d/ej6�Z8Gd0d1�d1ej&�Z9Gd2d3�d3ej&�Z:Gd4d5�d5ej&�Z;Gd6d7�d7e�Z<dS)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)�sslNzSSL not available)�CertificateOptions)�ClientTLSOptions�urn:ietf:params:xml:ns:xmpp-tlsc@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)�HashPasswordTestsz.
    Tests for L{xmlstream.hashPassword}.
    cCst�dd�}|�d|�dS)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zHashPasswordTests.test_basiccCs|�ttjdd�dS)zB
        The session identifier must be a unicode object.
        s¹2345rN��assertRaises�	TypeErrorrr�rr r r!�test_sidNotUnicode3s�z$HashPasswordTests.test_sidNotUnicodecCs|�ttjdd�dS)z8
        The password must be a unicode object.
        rssecrétNr#r&r r r!�test_passwordNotUnicode;s�z)HashPasswordTests.test_passwordNotUnicodecCst�dd�}|�d|�dS)z\
        The concatenated sid and password must be encoded to UTF-8 before hashing.
        rusecrétZ(659bf88d8f8e179081f7f3b4a8e7d224652d2853Nrrr r r!�test_unicodeSecretCsz$HashPasswordTests.test_unicodeSecretN)�__name__�
__module__�__qualname__�__doc__r"r'r(r)r r r r!r&s
rc@speZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�ZdS)�IQTestszG
    Tests both IQ and the associated IIQResponseTracker callback.
    cCsbt�d�}d|_t�|�|_t��|_|jj|j_|j�	t
���|j�d�t�
|jd�|_dS)NZ	otherhost�testnszm<stream:stream xmlns:stream='http://etherx.jabber.org/streams' xmlns='testns' from='otherhost' version='1.0'>�get)r�ConnectAuthenticator�	namespace�	XmlStreamrZClock�clock�	callLaterZ
_callLater�makeConnectionr�StringTransport�dataReceived�IQ�iq)r�
authenticatorr r r!�setUpQs

�z
IQTests.setUpcCs&|�|jdd�|�|jd�dS)N�typer0�id)rr:�
assertTruer&r r r!�	testBasic^szIQTests.testBasiccCsR|jj��|j��|jd�d�}|�|jj��d|dd|dg�dS)Nr>zutf-8s<iq type='get' id='s'/>s<iq id='s' type='get'/>)r�	transport�clearr:�send�encode�assertIn�value)rZidBytesr r r!�testSendcs


�zIQTests.testSendcs>�fdd�}�j��}|�|��j}|�d�jd�|S)Ncs��|dd�dS)Nr=�result)r)rHr&r r!�cbnsz&IQTests.testResultResponse.<locals>.cb�<iq type='result' id='%s'/>r>)r:rC�addCallbackrr8)rrI�d�xsr r&r!�testResultResponsems

zIQTests.testResultResponsecCs6|j��}|�|tj�|j}|�d|jd�|S)Nz<iq type='error' id='%s'/>r>)r:rC�
assertFailurerZStanzaErrorrr8�rrLrMr r r!�testErrorResponseys

zIQTests.testErrorResponsecsD�j}t�|���|j��fdd�}|�d|d�|�d�dS)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.
        cs��t|dd��dS)NZhandledF)�assertFalse�getattr)r:r&r r!rI�sz*IQTests.testNonTrackedResponse.<locals>.cbz/iq���z<iq type='result' id='test'/>N)rZupgradeWithIQResponseTrackerrR�iqDeferreds�addObserverr8)rrMrIr r&r!�testNonTrackedResponse�s
zIQTests.testNonTrackedResponsecCs<|j��}|j}|�d|jd�|�|jd|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.
        rJr>)r:rCrr8�assertNotInrUrPr r r!�testCleanup�s

zIQTests.testCleanupcCs*|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:rCr�connectionLostrOrrPr r r!�testDisconnectCleanup�s


zIQTests.testDisconnectCleanupcs2��fdd���j��}|����j�d�|S)z~
        Test to make sure the errbacks cannot cause the iteration of the
        iqDeferreds to blow up in our face.
        cst��j���}|���dS�N)rr9rC�
addErrback)r
rL��ebrr r!r`�sz'IQTests.testNoModifyingDict.<locals>.ebrZ)r:rCr^rr[�rrLr r_r!�testNoModifyingDict�s


zIQTests.testNoModifyingDictcCsPd|j_|j��}|�|tj�|j�ddg�|�|jj	�|�|jj
�|S)zK
        Test that an iq request with a defined timeout times out.
        �<�)r:�timeoutrCrOr�TimeoutErrorr4�pumprR�callsrUrar r r!�testRequestTimingOut�s
zIQTests.testRequestTimingOutcCsRd|j_|j��}|j�d|jjd|jd�|j�ddg�|�|jj	�|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.
        rcrdrJr>)
r:rerCr4r5rr8rgrRrhrar r r!�testRequestNotTimingOut�s
�zIQTests.testRequestNotTimingOutcCs@d|j_|j��}|j}|�d�|�|t�|�|jj	�|S)z|
        Test if timeouts for iq's that haven't yet received a response
        are cancelled on stream disconnect.
        rcrZ)
r:rerCrr[rOrrRr4rhrPr r r!�!testDisconnectTimeoutCancellation�s

z)IQTests.testDisconnectTimeoutCancellationN)r*r+r,r-r<r@rGrNrQrWrYr\rbrirjrkr r r r!r.Ls

	
r.c@s�eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd �Zd!S)"�XmlStreamTestscCs
d|_dS�NT)�gotStreamStart�r�objr r r!�
onStreamStart�szXmlStreamTests.onStreamStartcCs
d|_dSrm)�gotStreamEndror r r!�onStreamEnd�szXmlStreamTests.onStreamEndcCs
d|_dSrm)�gotStreamErrorror r r!�
onStreamError�szXmlStreamTests.onStreamErrorcCsvd|_d|_d|_t�t���}|�d|j�|�d|j�|�d|j	�|�
t���||j
_d|_d|_||_dS)z9
        Set up XmlStream and several observers.
        Fz//event/stream/start�//event/stream/endz//event/stream/errorr/�rdrN)rnrrrtrr3�
AuthenticatorrVrqrsrur6rZ StringTransportWithDisconnectionrAZprotocolr2�version�rrMr r r!r<szXmlStreamTests.setUpcCsh|j}|��|jj��dd��d�}|�d|�|�d|�|�d|�|�d|�|�|j�dS)	z>
        Basic test on the header sent by sendHeader.
        rrT� �<stream:stream�/xmlns:stream='http://etherx.jabber.org/streams'�xmlns='testns'�
version='1.0'N)r�
sendHeaderrArF�splitrEr?�_headerSent�rrMZsplitHeaderr r r!�test_sendHeaderBasics�z#XmlStreamTests.test_sendHeaderBasiccCs~|j}d|jd<|��|jj��dd��d�}|�d|�|�d|�|�d|�|�d	|�|�d
|�|�|j�dS)z=
        Test for additional namespace declarations.
        Zdbzjabber:server:dialbackrrTr{r|r}s!xmlns:db='jabber:server:dialback'r~rN)	r�prefixesr�rArFr�rEr?r�r�r r r!�#test_sendHeaderAdditionalNamespaces"s
�z2XmlStreamTests.test_sendHeaderAdditionalNamespacescCs`|j}t�d�|_t�d�|_d|_|��|j��dd��	d�}|�
d|�|�
d|�d	S)
z;
        Test addressing when initiating a stream.
        �thisHost�	otherHostTrrTr{�to='otherhost'�from='thishost'N)rr�JID�
thisEntity�otherEntity�
initiatingr�rArFr�rEr�r r r!�test_sendHeaderInitiating3sz(XmlStreamTests.test_sendHeaderInitiatingcCsr|j}t�d�|_t�d�|_d|_d|_|��|j�	�dd��
d�}|�d|�|�d	|�|�d
|�dS)z:
        Test addressing when receiving a stream.
        r�r�FZ	session01rrTr{r�r�sid='session01'N)rrr�r�r�r��sidr�rArFr�rEr�r r r!�test_sendHeaderReceivingAsz'XmlStreamTests.test_sendHeaderReceivingcCs6|j}|�d�|�d�|�|j�|�|j�dS)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)rr8r?rtrrrzr r r!�test_receiveStreamErrorQs


z&XmlStreamTests.test_receiveStreamErrorcCsP|j}d|_|��|j��|�t�d��|�d|j�	��|�
|j�dS)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�rr�r�rArB�sendStreamErrorr�StreamError�assertNotEqualrFr?rrrzr r r!�test_sendStreamErrorInitiating^s
z-XmlStreamTests.test_sendStreamErrorInitiatingcCsT|j}d|_|j��|�t�d��|�|j�|�	d|j�
��|�|j�dS)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)
rr�rArBr�rr��	assertNotr�rrFr?rrrzr r r!�&test_sendStreamErrorInitiatingNoHeaderms
z5XmlStreamTests.test_sendStreamErrorInitiatingNoHeadercCsP|j}d|_|��|j��|�t�d��|�d|j�	��|�
|j�dS)z�
        Test sendStreamError on a receiving xmlstream with a header sent.

        An error should be sent out and the connection lost.
        Fr�r�Nr�rzr r r!�test_sendStreamErrorReceiving~s
z,XmlStreamTests.test_sendStreamErrorReceivingcCsT|j}d|_|j��|�t�d��|�|j�|�	d|j�
��|�|j�dS)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)rr�rArBr�rr�r?r�r�rFrrrzr r r!�%test_sendStreamErrorReceivingNoHeader�s
z4XmlStreamTests.test_sendStreamErrorReceivingNoHeadercCs:|j}|��|j}|��|�||j�|�|j�dS)zE
        Test resetting the XML stream to start a new layer.
        N)rr��stream�resetr�r�r�)rrMr�r r r!�
test_reset�szXmlStreamTests.test_resetcCs�|j}|�d�|�|j��d�|j��t�d�}|�|�|�|j��d�|j��t�d�}|�|�|�|j��d�dS)z:
        Test send with various types of objects.
        �<presence/>�<presence/>)r/Zpresence)z http://etherx.jabber.org/streams�featuress<stream:features/>N)rrCrrArFrBr�Element)rrMZelr r r!�	test_send�s






zXmlStreamTests.test_sendcs�g�g�g�G���fdd�d�}|�}t�|�}|�|g��|��|�dg��|�d�|�dt���|��|�dg��dS)zM
        Test that the associated authenticator is correctly called.
        cs0eZdZ�fdd�Z�fdd�Z�fdd�ZdS)z<XmlStreamTests.test_authenticator.<locals>.TestAuthenticatorcs��d�dSr]��appendr&)�connectionMadeCallsr r!�connectionMade�szKXmlStreamTests.test_authenticator.<locals>.TestAuthenticator.connectionMadecs��|�dSr]r�)rZrootElement)�streamStartedCallsr r!�
streamStarted�szJXmlStreamTests.test_authenticator.<locals>.TestAuthenticator.streamStartedcs��|�dSr]r�rz)�associateWithStreamCallsr r!�associateWithStream�szPXmlStreamTests.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)rr3rr�r8�lenr�)rr��arMr r�r!�test_authenticator�s


z!XmlStreamTests.test_authenticatorN)r*r+r,rqrsrur<r�r�r�r�r�r�r�r�r�r�r�r�r r r r!rl�s 
rlc@seZdZdS)�	TestErrorN)r*r+r,r r r r!r��sr�c@s4eZdZdd�Zdd�Zdd�Zdd�Zd	d
�ZdS)�AuthenticatorTestscCst��|_t�|j�|_dSr])rrxr;r3r&r r r!r<�s
zAuthenticatorTests.setUpcCsh|j}|�t���|�d�|�d|j�|�d|j�|�d|j	�|�d|j
�|�d|j�dS)�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'>rwNZinvalid)rr6rr7r8rry�assertIdenticalr�r2r�r�rzr r r!�test_streamStart�s
z#AuthenticatorTests.test_streamStartcCs0|j}|�t���|�d�|�d|j�dS)z
        Test streamStart to fill the appropriate attributes from the
        stream header for a pre-XMPP-1.0 header.
        r��rrN�rr6rr7r8rryrzr r r!�test_streamStartLegacy�s
z)AuthenticatorTests.test_streamStartLegacycCs0|j}|�t���|�d�|�d|j�dS)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�rzr r r!�test_streamBadVersionOneDigits
z0AuthenticatorTests.test_streamBadVersionOneDigitcCs0|j}|�t���|�d�|�d|j�dS)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�rzr r r!�test_streamBadVersionNoNumbers
z0AuthenticatorTests.test_streamBadVersionNoNumberN)r*r+r,r<r�r�r�r�r r r r!r��s


r�c@s<eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
S)�ConnectAuthenticatorTestscCsJd|_d|_t�d�|_t�|j�|_|j�d|j�|j�d|j�dS)NFr��//event/stream/authd�//event/xmpp/initfailed)	�gotAuthenticated�initFailurerr1r;r3rV�onAuthenticated�onInitFailedr&r r r!r<$szConnectAuthenticatorTests.setUpcCs
d|_dSrm)r�ror r r!r�-sz)ConnectAuthenticatorTests.onAuthenticatedcCs
||_dSr])r�)rr
r r r!r�1sz&ConnectAuthenticatorTests.onInitFailedcCsHGdd�d�}|�}|g|j_|j��|�g|jj�|�|j�dS)zG
        Test successful completion of an initialization step.
        c@seZdZdd�ZdS)z9ConnectAuthenticatorTests.testSucces.<locals>.InitializercSsdSr]r r&r r r!�
initialize:szDConnectAuthenticatorTests.testSucces.<locals>.Initializer.initializeN�r*r+r,r�r r r r!�Initializer9sr�N)r�initializersr;�initializeStreamrr?r��rr��initr r r!�
testSucces5s

z$ConnectAuthenticatorTests.testSuccescCsjGdd�d�}|�}|g|j_|j��|�|g|jj�|�|j�|�d|j�|�	|j�
t��dS)z9
        Test failure of an initialization step.
        c@seZdZdd�ZdS)z:ConnectAuthenticatorTests.testFailure.<locals>.InitializercSst�dSr])r�r&r r r!r�JszEConnectAuthenticatorTests.testFailure.<locals>.Initializer.initializeNr�r r r r!r�Isr�N)rr�r;r�rrRr��assertNotIdenticalr�r?Zcheckr�r�r r r!�testFailureEs

z%ConnectAuthenticatorTests.testFailurecCs�d|j_|j}|�t���|�d�|�d|j�|�d|j	�|�d|j�|�d|j
j�|�d|j
�|�|j�|�d�|�d|j�|�|j�dS)	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'>rwr�example.comNz9<stream:features><test xmlns='testns'/></stream:features>�r/Ztest)r;r2rr6rr7r8rryr�r��hostr�r�r�r�rEr�r?rzr r r!r�Ws

z*ConnectAuthenticatorTests.test_streamStartN)	r*r+r,r<r�r�r�r�r�r r r r!r�"s	r�c@s(eZdZdZdd�Zdd�Zdd�ZdS)	�ListenAuthenticatorTestsz4
    Tests for L{xmlstream.ListenAuthenticator}
    cCst��|_t�|j�|_dSr])rZListenAuthenticatorr;r3r&r r r!r<vs
zListenAuthenticatorTests.setUpcCs�|j}|�t���|�d|j�|�d�|�d|j�|�	d|j�|�
d|j�|�d|j�|�d|j�|�d|j
j�dS)r�Nr�rwr�
jabber:clientr�)rr6rr7r�r�r8rryr�r�r2r�r�r�rzr r r!r�{s
z)ListenAuthenticatorTests.test_streamStartcCs0|j}|�t���|�d�|�|jt�dS)zD
        The generated session id must be a unicode object.
        r�N)rr6rr7r8�assertIsInstancer�rrzr r r!� test_streamStartUnicodeSessionID�s
z9ListenAuthenticatorTests.test_streamStartUnicodeSessionIDN)r*r+r,r-r<r�r�r r r r!r�qsr�c@sveZdZdd�Zdd�Zdd�Zdd�Zee_d	d
�Z	ee	_dd�Z
ee
_d
d�Zdd�Zdd�Z
dd�Zdd�ZdS)�TLSInitiatingInitializerTestscCsbg|_g|_tj|_t�d�|_t�|j�|_|jj|j_	|j�
�|j�d�t�|j�|_
dS)Nr�r�)�output�donerr�savedSSLr1r;r3r�rCr�r8�TLSInitiatingInitializerr�r&r r r!r<�s
z#TLSInitiatingInitializerTests.setUpcCs|jt_dSr])r�rrr&r r r!�tearDown�sz&TLSInitiatingInitializerTests.tearDowncCs$tj|jdd�|_|�|jj�dS)z>
        Passing required sets the instance variable.
        T)�requiredN)rr�r�r?r�r&r r r!�test_initRequired�s�z/TLSInitiatingInitializerTests.test_initRequiredcs�t���j_�fdd��jj_�fdd��j_�fdd��j_�j��}|�	�j
tj���
dt�j
���j
d}��
d|j���
t|j��j�dt���
d	d
dg�j�|S)zN
        When TLS is wanted and SSL available, StartTLS is initiated.
        cs�j�d�S�N�TLS�r�r�)�ctxr&r r!�<lambda>�r�zDTLSInitiatingInitializerTests.test_wantedSupported.<locals>.<lambda>cs�j�d�S�Nr�r�r r&r r!r��r�cs�j�d�S�N�headerr�r r&r r!r��r��rd�starttls�<proceed xmlns='%s'/>r�r�r�)rr7rrA�startTLSr�r�r��startrKrZResetr�r��name�NS_XMPP_TLS�urir8r�)rrLr�r r&r!�test_wantedSupported�s

z2TLSInitiatingInitializerTests.test_wantedSupportedcst�fdd�}t���j_|�jj_�fdd��j_�fdd��j_�j��}�j�	dt
���ddd	g�j�|S)
z:
        The server certificate will be verified.
        cs*��|t���|jd��j�d�dS)Nr�r�)r�rrZ	_hostnamer�r���contextFactoryr&r r!�fakeStartTLS�szJTLSInitiatingInitializerTests.test_certificateVerify.<locals>.fakeStartTLScs�j�d�Sr�r�r r&r r!r��r�zFTLSInitiatingInitializerTests.test_certificateVerify.<locals>.<lambda>cs�j�d�Sr�r�r r&r r!r��r�r�r�r�r�)
rr7rrAr�r�r�r�r�r8r�rr��rr�rLr r&r!�test_certificateVerify�s

z4TLSInitiatingInitializerTests.test_certificateVerifycs�t��tj�j�d��_��j_��fdd�}t���j_|�jj_�fdd��j_	�fdd��j_
�j��}�j�dt
���dd	d
g�j�|S)zH
        A custom contextFactory is passed through to startTLS.
        )ZconfigurationForTLScs���|��j�d�dSr�)ZassertIsr�r�r��r�rr r!r��szQTLSInitiatingInitializerTests.test_certificateVerifyContext.<locals>.fakeStartTLScs�j�d�Sr�r�r r&r r!r��r�zMTLSInitiatingInitializerTests.test_certificateVerifyContext.<locals>.<lambda>cs�j�d�Sr�r�r r&r r!r��r�r�r�r�r�)rrr�r�r�rr7rAr�r�r�r�r8r�rr�r�r r�r!�test_certificateVerifyContext�s�

z;TLSInitiatingInitializerTests.test_certificateVerifyContextcCs<dt_d|j_|j��}|�|jd�|�dt|j��|S)zX
        No StartTLS is initiated when wanted, not required, SSL not available.
        NFrd)	rrr�r�r�rKrr�r�rar r r!�"test_wantedNotSupportedNotRequireds
z@TLSInitiatingInitializerTests.test_wantedNotSupportedNotRequiredcCs<dt_d|j_|j��}|�|tj�|�dt|j	��|S)zS
        TLSNotSupported is raised when TLS is required but not available.
        NTrd)
rrr�r�r�rOZTLSNotSupportedrr�r�rar r r!�test_wantedNotSupportedRequireds
z=TLSInitiatingInitializerTests.test_wantedNotSupportedRequiredcCs^t�d�}|�d�|j|jf|i|j_d|j_|j�	�}|�
dt|j��|�
|tj�|S)zW
        TLSRequired is raised when TLS is not wanted, but required by server.
        �rr�r�Frd)rr�Z
addElementr�r�rr�r��wantedr�rr�r�rOZTLSRequired�rZtlsrLr r r!�test_notWantedRequired s


z4TLSInitiatingInitializerTests.test_notWantedRequiredcCs\t�d�}|j|jf|i|j_d|j_d|j_|j�	�}|�
|jd�|�dt|j
��|S)zL
        No StartTLS is initiated when not wanted and not required.
        r�FNrd)rr�r�r�rr�r�r�r�r�rKrr�r�r�r r r!�test_notWantedNotRequired0s

z7TLSInitiatingInitializerTests.test_notWantedNotRequiredcCs2dt_|j��}|�|tj�|j�dt�|S)zN
        TLSFailed is raised when the server responds with a failure.
        rdz<failure xmlns='%s'/>)rrr�r�rOZ	TLSFailedr8r�rar 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@seZdZdZdd�ZdS)�TestFeatureInitializerr�cCs
t�d�Sr])rZsucceedr&r r r!r�RszTestFeatureInitializer.startN)r*r+r,�featurer�r r r r!rOsrc@s,eZdZdd�Zdd�Zdd�Zdd�Zd	S)
�%BaseFeatureInitiatingInitializerTestscCs t�t���|_t|j�|_dSr])rr3rxrr�r&r r r!r<Ysz+BaseFeatureInitiatingInitializerTests.setUpcCs$|jjt�|jj�i|j_|j��S)zW
        Test that an advertized feature results in successful initialization.
        )r�rrr�rr�r�r&r r r!�testAdvertized^s�z4BaseFeatureInitiatingInitializerTests.testAdvertizedcCsd|j_|�tj|jj�dS)z�
        Test that when the feature is not advertized, but required by the
        initializer, an exception is raised.
        TN)r�r�r$rZFeatureNotAdvertizedr�r&r r r!�testNotAdvertizedRequiredgsz?BaseFeatureInitiatingInitializerTests.testNotAdvertizedRequiredcCsd|j_|�d|j���dS)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!�testNotAdvertizedNotRequiredpszBBaseFeatureInitiatingInitializerTests.testNotAdvertizedNotRequiredN)r*r+r,r<rrrr r r r!rWs		rc@s<eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
S)�ToResponseTestscCs�t�d�}d|d<d|d<d|d<d|d	<t�|d
�}|�||�|�|dd�|�|dd�|�|dd
�|�|d	d�dS)zS
        Test that a response stanza is generated with addressing swapped.
        �r�r:r0r=�user1@example.com�to�user2@example.com/resource�fromZstanza1r>rHN)rr�r�
toResponser�r�rZstanzaZresponser r r!�test_toResponse|s
zToResponseTests.test_toResponsecCsHt�d�}d|d<d|d<t�|�}|�|dd�|�|�d��dS)zY
        Test that a response is generated from a stanza without a from address.
        r	r0r=r
rr
N)rr�rrrrR�hasAttributerr r r!�test_toResponseNoFrom�s

z%ToResponseTests.test_toResponseNoFromcCsHt�d�}d|d<d|d<t�|�}|�|�d��|�|dd�dS)zW
        Test that a response is generated from a stanza without a to address.
        r	r0r=rr
rN)rr�rrrRrrrr r r!�test_toResponseNoTo�s

z#ToResponseTests.test_toResponseNoTocCs@t�d�}d|d<t�|�}|�|�d��|�|�d��dS)zY
        Test that a response is generated from a stanza without any addressing.
        �r��messageZchatr=rr
N�rr�rrrRrrr r r!�test_toResponseNoAddressing�s


z+ToResponseTests.test_toResponseNoAddressingcCs(t�d�}t�|�}|�|�d��dS)zP
        Test that a proper response is generated without id attribute.
        rr>Nrrr r r!�	test_noID�s

zToResponseTests.test_noIDcCs(t�d�}t�|�}|�|�d��dS)zR
        Test that a proper response is generated without type attribute.
        rr=Nrrr r r!�test_noType�s

zToResponseTests.test_noTypeN)	r*r+r,rrrrrrr r r r!rzs	rc@s eZdZdZdd�Zdd�ZdS)�DummyFactoryzJ
    Dummy XmlStream factory that only registers bootstrap observers.
    cCs
i|_dSr]��	callbacksr&r r r!�__init__�szDummyFactory.__init__cCs||j|<dSr]r)rZevent�callbackr r r!�addBootstrap�szDummyFactory.addBootstrapN)r*r+r,r-rrr r r r!r�src@s8eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�ZdS)
�DummyXMPPHandlerzO
    Dummy XMPP subprotocol handler to count the methods are called on it.
    cCsd|_d|_d|_dS)Nr)�doneMade�doneInitialized�doneLostr&r r r!r�szDummyXMPPHandler.__init__cCs|��dSr])r�rzr r r!r6�szDummyXMPPHandler.makeConnectioncCs|jd7_dS�Nrd�r!r&r r r!r��szDummyXMPPHandler.connectionMadecCs|jd7_dSr$)r"r&r r r!�connectionInitialized�sz&DummyXMPPHandler.connectionInitializedcCs|jd7_dSr$)r#�r�reasonr r r!r[�szDummyXMPPHandler.connectionLostN)	r*r+r,r-rr6r�r&r[r r r r!r �sr c@s eZdZdZdd�Zdd�ZdS)�FailureReasonXMPPHandlerz>
    Dummy handler specifically for failure Reason tests.
    cCs
d|_dS)NF)�gotFailureReasonr&r r r!r�sz!FailureReasonXMPPHandler.__init__cCst|tj�rd|_dSrm)�
isinstancer
�Failurer*r'r r r!r[�sz'FailureReasonXMPPHandler.connectionLostN)r*r+r,r-rr[r r r r!r)�sr)c@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)�XMPPHandlerTestsz-
    Tests for L{xmlstream.XMPPHandler}.
    cCsttjt���dS)zN
        L{xmlstream.XMPPHandler} implements L{ijabber.IXMPPHandler}.
        N)rrZIXMPPHandlerr�XMPPHandlerr&r r r!�test_interface�szXMPPHandlerTests.test_interfacecCs@Gdd�dt�}t��}|�|_|�d�|�dg|jj�dS)zP
        Test that data is passed on for sending by the stream manager.
        c@seZdZdd�Zdd�ZdS)z6XMPPHandlerTests.test_send.<locals>.DummyStreamManagercSs
g|_dSr])�outlistr&r r r!r	sz?XMPPHandlerTests.test_send.<locals>.DummyStreamManager.__init__cSs|j�|�dSr])r0r�)r�datar r r!rCsz;XMPPHandlerTests.test_send.<locals>.DummyStreamManager.sendN)r*r+r,rrCr r r r!�DummyStreamManagersr2r�N)�objectrr.�parentrCrr0)rr2�handlerr r r!r�s

zXMPPHandlerTests.test_sendcCsNGdd�dtj�}|�}t�t���}|�|�|�|j�|�||j�dS)zY
        Test that makeConnection saves the XML stream and calls connectionMade.
        c@seZdZdd�ZdS)z=XMPPHandlerTests.test_makeConnection.<locals>.TestXMPPHandlercSs
d|_dSrmr%r&r r r!r�szLXMPPHandlerTests.test_makeConnection.<locals>.TestXMPPHandler.connectionMadeN)r*r+r,r�r r r r!�TestXMPPHandlersr6N)rr.r3rxr6r?r!r�)rr6r5rMr r r!�test_makeConnections
z$XMPPHandlerTests.test_makeConnectioncCs>t��}t�t���}|�|�|�t��|�d|j�dS)zB
        Test that connectionLost forgets the XML stream.
        N)rr.r3rxr6r[�	Exceptionr�)rr5rMr r r!�test_connectionLost$s

z$XMPPHandlerTests.test_connectionLostN)r*r+r,r-r/r�r7r9r r r r!r-�s
r-c@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)�XMPPHandlerCollectionTestsz7
    Tests for L{xmlstream.XMPPHandlerCollection}.
    cCst��|_dSr])rZXMPPHandlerCollection�
collectionr&r r r!r<5sz XMPPHandlerCollectionTests.setUpcCsttj|j�dS)zZ
        L{xmlstream.StreamManager} implements L{ijabber.IXMPPHandlerCollection}.
        N)rrZIXMPPHandlerCollectionr;r&r r r!r/9sz)XMPPHandlerCollectionTests.test_interfacecCs4t�}|�|j�|�||j�|�|j|j�dS)z:
        Test the addition of a protocol handler.
        N)r �setHandlerParentr;rEr�r4�rr5r r r!�test_addHandler@sz*XMPPHandlerCollectionTests.test_addHandlercCs>t�}|�|j�|�|j�|�||j�|�d|j�dS)z5
        Test removal of a protocol handler.
        N)r r<r;ZdisownHandlerParentrXr�r4r=r r r!�test_removeHandlerJs
z-XMPPHandlerCollectionTests.test_removeHandlerN)r*r+r,r-r<r/r>r?r r r r!r:0s

r:c@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�ZdS)�StreamManagerTestsz/
    Tests for L{xmlstream.StreamManager}.
    cCst�}t�|�|_dSr])rr�
StreamManager�
streamManager)r�factoryr r r!r<[szStreamManagerTests.setUpcCs~|j}|�d|j�|�g|j�|�|j|jjd�|�|j|jjd�|�|j	|jjd�|�|j
|jjd�dS)zM
        Test correct initialization and setup of factory observers.
        Nz//event/stream/connectedr�rvr�)rBr�rrZhandlers�
_connectedrCr�_authd�
_disconnectedZinitializationFailed)r�smr r r!r"`s
�
�
�
�zStreamManagerTests.test_basiccCs\|j}t�}|�|�t�t���}|�|�|�d|j�|�d|j	�|�d|j
�dS)z
        Test that protocol handlers have their connectionMade method called
        when the XML stream is connected.
        rdrN)rBr r<rr3rxrDrr!r"r#�rrGr5rMr r r!�test_connectedqs

z!StreamManagerTests.test_connectedcCsN|j}t�}|�|�t�t���}|�|�|�d|j�|�d|j	�dS)zP
        Test raw data functions unset when logTraffic is set to False.
        N)
rBr r<rr3rxrDr��rawDataInFn�rawDataOutFnrHr r r!�test_connectedLogTrafficFalse�s

z0StreamManagerTests.test_connectedLogTrafficFalsecCsT|j}d|_t�}|�|�t�t���}|�|�|�d|j	�|�d|j
�dS)zM
        Test raw data functions set when logTraffic is set to True.
        TN)rBZ
logTrafficr r<rr3rxrDr�rJrKrHr r r!�test_connectedLogTrafficTrue�s

z/StreamManagerTests.test_connectedLogTrafficTruecCs\|j}t�}|�|�t�t���}|�|�|�d|j�|�d|j	�|�d|j
�dS)z�
        Test that protocol handlers have their connectionInitialized method
        called when the XML stream is initialized.
        rrdN)rBr r<rr3rxrErr!r"r#rHr r r!�
test_authd�s

zStreamManagerTests.test_authdcCs\|j}t�}|�|�t�t���}|�|�|�d|j�|�d|j	�|�d|j
�dS)z�
        Test that protocol handlers have their connectionLost method
        called when the XML stream is disconnected.
        rrdN)rBr r<rr3rxrFrr!r"r#rHr r r!�test_disconnected�s

z$StreamManagerTests.test_disconnectedcCs<|j}t�}|�|�|�t�td���|�d|j�dS)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)	rBr)r<rFr
r,r8rr*�rrGr5r r r!�test_disconnectedReason�s

z*StreamManagerTests.test_disconnectedReasoncCsD|j}t�}|�|�|�d|j�|�d|j�|�d|j�dS)zN
        Test the addition of a protocol handler while not connected.
        rN)rBr r<rr!r"r#rPr r r!r>�s
z"StreamManagerTests.test_addHandlercCsf|j}t�t���}|�|�|�|�t�}|�|�|�d|j	�|�d|j
�|�d|j�dS)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.
        rdrN)rBrr3rxrDrEr r<rr!r"r#)rrGrMr5r r r!�test_addHandlerInitialized�s	


z-StreamManagerTests.test_addHandlerInitializedcCsjt�t���}t�|�}|�d�}t��|_|��|�	d�|�
|d�|�d�|�d|j�
��dS)z�
        Test send when the stream has been initialized.

        The data should be sent directly over the XML stream.
        Nr�r�r�r�)r�XmlStreamFactoryrxrA�
buildProtocolrr7rAr�r8�dispatchrCrrF�rrCrGrMr r r!�test_sendInitialized�s




z'StreamManagerTests.test_sendInitializedcCs�t�t���}t�|�}t�}|�|�|�d�}t��|_	|�
d�|�d|j	���|�d|j
d�|��|�d|j	���|�d|j
d�|�d�|�|d�|�d|j	���|�|j
�dS)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�rr�r�r�)rrSrxrAr �
addHandlerrTrr7rArCrrF�_packetQueuer�r8rUrR�rrCrGr5rMr r r!�test_sendNotConnected�s 





z(StreamManagerTests.test_sendNotConnectedcCspt�t���}t�|�}|�d�}t��|_|��|�	d�|�
d�|�d|j���|�d|j
d�dS)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)rrSrxrArTrr7rAr�r8rCrrFrYrVr r r!�test_sendNotInitializeds




z*StreamManagerTests.test_sendNotInitializedcCs�t�t���}t�|�}t�}|�|�|�d�}|��t�	�|_
|�d�|�d�|�
d|j
���|�
d|jd�dS)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)rrSrxrAr rXrTr�rr7rAr[rCrrFrYrZr r r!�test_sendDisconnected,s





z(StreamManagerTests.test_sendDisconnectedN)r*r+r,r-r<r"rIrLrMrNrOrQr>rRrWr[r\r]r r r r!r@Vs

r@c@s8eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�ZdS)
�XmlStreamServerFactoryTestsz8
    Tests for L{xmlstream.XmlStreamServerFactory}.
    cs,Gdd�dt���fdd�}t�|�|_dS)zQ
        Set up a server factory with an authenticator factory function.
        c@seZdZdd�Zdd�ZdS)z<XmlStreamServerFactoryTests.setUp.<locals>.TestAuthenticatorcSs
g|_dSr])�
xmlstreamsr&r r r!rMszEXmlStreamServerFactoryTests.setUp.<locals>.TestAuthenticator.__init__cSs|j�|�dSr])r_r�rzr r r!r�PszPXmlStreamServerFactoryTests.setUp.<locals>.TestAuthenticator.associateWithStreamN)r*r+r,rr�r r r r!r�Lsr�cs��Sr]r r �r�r r!�authenticatorFactorySsz?XmlStreamServerFactoryTests.setUp.<locals>.authenticatorFactoryN)r3rZXmlStreamServerFactoryrC)rrar r`r!r<Hsz!XmlStreamServerFactoryTests.setUpcCstt|j�dS)z<
        L{XmlStreamServerFactory} is a L{Factory}.
        N)rr	rCr&r r r!r/Ysz*XmlStreamServerFactoryTests.test_interfacecCs"|j�d�}|�|g|jj�dS)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)rCrTrr;r_rzr r r!�,test_buildProtocolAuthenticatorInstantiation`s
zHXmlStreamServerFactoryTests.test_buildProtocolAuthenticatorInstantiationcCs|j�d�}|�|tj�dS)zV
        The protocol factory creates Jabber XML Stream protocols by default.
        N)rCrTr�rr3rzr r r!�test_buildProtocolXmlStreamnsz7XmlStreamServerFactoryTests.test_buildProtocolXmlStreamcCs8|j�d�}|j�d�}|�||�|�|j|j�dS)z�
        Subsequent calls to buildProtocol should result in different instances
        of the protocol, as well as their authenticators.
        N)rCrTr�r;)rZxs1Zxs2r r r!�test_buildProtocolTwicevsz3XmlStreamServerFactoryTests.test_buildProtocolTwiceN)	r*r+r,r-r<r/rbrcrdr r r r!r^Csr^)=r-Z
__future__rrZ
twisted.trialrZzope.interface.verifyrZtwisted.internetrrZtwisted.internet.errorrZtwisted.internet.interfacesr	Ztwisted.pythonr
Ztwisted.python.compatrZtwisted.testrZ!twisted.words.test.test_xmlstreamr
Ztwisted.words.xishrZtwisted.words.protocols.jabberrrrrr�ImportErrorrZtwisted.internet.sslrZtwisted.internet._sslverifyrr�ZTestCaserr.rlr8r�r�r�r�r�Z BaseFeatureInitiatingInitializerrrrr3rr.r r)r-r:r@r^r r r r!�<module>sV
&(k@O,3#H
8&n