Ϫf@dZddlmZddlmZddlmZddlmZddlm Z m Z m Z m Z ddl mZddlmZ dd lmZd Zd ZdZdZdezZdZdezZGddej4ZGddZGddeej4ZGddeej4ZGddeej4ZGddej4Z Gdd ej4Z!y #e$rd Zd ZYwxYw)!z4 Tests for L{twisted.words.protocols.jabber.client} )sha1)skipIf)defer)unittest)clienterrorjid xmlstream)SASLInitiatingInitializer)utility)ssl)FN)TzSSL not availablez//iq[@type="get"]/query[@xmlns="jabber:iq:auth"]z//iq[@type="set"]/query[@xmlns="jabber:iq:auth"]z urn:ietf:params:xml:ns:xmpp-bindz"/iq[@type="set"]/bind[@xmlns="%s"]z#urn:ietf:params:xml:ns:xmpp-sessionz%/iq[@type="set"]/session[@xmlns="%s"]ceZdZdZdZdZy)CheckVersionInitializerTestsctj}tj|}tj||_yN)r Authenticator XmlStreamrCheckVersionInitializerinit)selfaxss F/usr/lib/python3/dist-packages/twisted/words/test/test_jabberclient.pysetUpz"CheckVersionInitializerTests.setUp#s2  # # %   #2226 cnd|jj_|jjy)z3 Test supported version number 1.0 )rN)rr version initializers r testSupportedz*CheckVersionInitializerTests.testSupported(s&'- # rcd|jj_|jtj |jj }|jd|jy)zK Test unsupported version number 0.0, and check exception. )rrzunsupported-versionN) rr r assertRaisesr StreamErrorr assertEqual condition)rexcs rtestNotSupportedz-CheckVersionInitializerTests.testNotSupported/sM'- # 1 14993G3GH . >rN)__name__ __module__ __qualname__rr"r)rrrr"s7 ?rrceZdZdZdZdZy)InitiatingInitializerHarnessa Testing harness for interacting with XML stream initializers. This sets up an L{utility.XmlPipe} to create a communication channel between the initializer and the stubbed receiving entity. It features a sink and source side that both act similarly to a real L{xmlstream.XmlStream}. The sink is augmented with an authenticator to which initializers can be added. The harness also provides some utility methods to work with event observers and deferreds. cg|_tj|_|jj|_t j d|_|j|j _y)Nz example.org)outputr XmlPipepipesinkr ConnectAuthenticator authenticatorr!s rrz"InitiatingInitializerHarness.setUpEsK OO% &;;MJ'+'9'9$rctj}|j||jjj ||j |S)a Observe an output event, returning a deferred. The returned deferred will be fired when the given event has been observed on the source end of the L{XmlPipe} tied to the protocol under test. The handler is added as the first callback. @param event: The event to be observed. See L{utility.EventDispatcher.addOnetimeObserver}. @param handler: The handler to be called with the observed event object. @rtype: L{defer.Deferred}. )rDeferred addCallbackr3sourceaddOnetimeObservercallback)reventhandlerds rwaitForz$InitiatingInitializerHarness.waitForLs? NN  g ++E1::>rN)r*r+r,__doc__rr@r-rrr/r/8s :rr/c:eZdZdZfdZdZdZdZdZxZ S)IQAuthInitializerTestsz0 Tests for L{client.IQAuthInitializer}. ct|tj|j|_t jd|j_d|j_ y)Nuser@example.com/resourcesecret) superrrIQAuthInitializerr rr JIDr6passwordr __class__s rrzIQAuthInitializerTests.setUpdsH  ,,T^^< !$)D!E&.#rcfd}fdjt|}jj}t j ||gS)z Test plain-text authentication. Act as a server supporting plain-text authentication and expect the C{password} field to be filled with the password. Then act as if authentication succeeds. cntj|d}|jd|jjd|jjd|jjdj t }j jj||Sz Called when the initializer sent a query for authentication methods. The response informs the client that plain-text authentication is supported. resultzjabber:iq:authqueryusernamerJresource r toResponse addElementrRr@ IQ_AUTH_SETr3r:sendiqresponser? onAuthSetrs r onAuthGetz7IQAuthInitializerTests.testPlainText..onAuthGets!++B9H    ; < NN % %j 1 NN % %j 1 NN % %j 1 [)4A II   ! !( +Hrcjdt|jjjdt|jjjdt|jj t j|d}jjj|y) Called when the initializer sent the authentication request. The server checks the credentials and responds with an empty result signalling success. userrFrTrPN) r&strrRrSrJrTr rVr3r:rYr[r\rs rr]z7IQAuthInitializerTests.testPlainText..onAuthSets   VS):):%; <   Xs288+<+<'= >   ZRXX->->)? @!++B9H II   ! !( +r)r@ IQ_AUTH_GETrr r gatherResultsrr^d1d2r]s` @r testPlainTextz$IQAuthInitializerTests.testPlainTextjsH . , \\+y 1YY ! ! #""B8,,rcfd}fddj_jt|}jj }t j||gS)a" Test digest authentication. Act as a server supporting digest authentication and expect the C{digest} field to be filled with a sha1 digest of the concatenated stream session identifier and password. Then act as if authentication succeeds. cntj|d}|jd|jjd|jjd|jjdj t }j jj||S)z Called when the initializer sent a query for authentication methods. The response informs the client that digest authentication is supported. rPrQrSdigestrTrUrZs rr^z4IQAuthInitializerTests.testDigest..onAuthGets!++B9H    ; < NN % %j 1 NN % %h / NN % %j 1 [)4A II   ! !( +Hrcjdt|jjjt dj t|jj jdt|jjtj|d}jjj|y)rarbs 12345secretrTrPN) r&rcrRrSr hexdigestrmrTr rVr3r:rYrds rr]z4IQAuthInitializerTests.testDigest..onAuthSets   VS):):%; <   T.1;;=s288???S T   ZRXX->->)? @!++B9H II   ! !( +r12345)r sidr@rerr rrfrgs` @r testDigestz!IQAuthInitializerTests.testDigestsV . , %\\+y 1YY ! ! #""B8,,rcfd}jt|}jj}j |t j tj||gS)zT Test initializer failure of request for fields for authentication. ctjdj|}jjj |y)z Called when the initializer sent a query for authentication methods. The server responds that the client is not authorized to authenticate. not-authorizedNr StanzaErrorrVr3r:rYrds rr^z?IQAuthInitializerTests.testFailRequestFields..onAuthGets9 (()9:EEbIH II   ! !( +r r@rerr assertFailurerrwrrf)rr^rhris` rtestFailRequestFieldsz,IQAuthInitializerTests.testFailRequestFieldss[  ,\\+y 1YY ! ! # 2u001""B8,,rcfd}fdjt|}jj}j |t j tj||gS)z; Test initializer failure to authenticate. cntj|d}|jd|jjd|jjd|jjdj t }j jj||SrOrUrZs rr^z6IQAuthInitializerTests.testFailAuth..onAuthGetr_rctjdj|}jjj |y)z Called when the initializer sent the authentication request. The server checks the credentials and responds with a not-authorized stanza error. ruNrvrds rr]z6IQAuthInitializerTests.testFailAuth..onAuthSets9(()9:EEbIH II   ! !( +rrxrgs` @r testFailAuthz#IQAuthInitializerTests.testFailAuths`  . ,\\+y 1YY ! ! # 2u001""B8,,r) r*r+r,rArrjrrrzr~ __classcell__rLs@rrCrC_s#/ 4-l9-v-2/-rrCc.eZdZdZfdZdZdZxZS)BindInitializerTestsz. Tests for L{client.BindInitializer}. ct|tj|j|_t jd|j_y)NrE) rGrrBindInitializerr rr rIr6rKs rrzBindInitializerTests.setUp+s:  **4>>: !$)D!Ercfd}fd}jt|}jj}|j |t j ||gS)zK Set up a stream, and act as if resource binding succeeds. ctj|d}|jtdf|jjddj j j|y)NrPbindr user@example.com/other resource)content)r rVrWNS_BINDrr3r:rYrds ronBindz.BindInitializerTests.testBasic..onBind5sX ++B9H   & 1 2 MM $ $U4U $ V II   ! !( +rcxjtjdjjy)Nr)r&r rIr6)rPrs rcbz*BindInitializerTests.testBasic..cb;s,   9:D.onBindJs6((4??CH II   ! !( +r) r@rrrryrrwrrf)rrrhris` r testFailurez BindInitializerTests.testFailureEsU  ,\\+v . YY__  2u001""B8,,r)r*r+r,rArrrrrs@rrr&sF -* -rrc.eZdZdZfdZdZdZxZS)SessionInitializerTestsz1 Tests for L{client.SessionInitializer}. cjt|tj|j|_yr)rGrrSessionInitializerr rrKs rrzSessionInitializerTests.setUpYs"  --dnn= rcfd}jt|}jj}t j ||gS)zP Set up a stream, and act as if session establishment succeeds. c|tj|d}jjj |y)NrP)r rVr3r:rYrds r onSessionz6SessionInitializerTests.testSuccess..onSessionbs- ++B9H II   ! !( +r)r@IQ_SESSION_SETrrrrfrrrhris` r testSuccessz#SessionInitializerTests.testSuccess]s?  ,\\.) 4 YY__ ""B8,,rcfd}jt|}jj}j |t j tj||gS)zM Set up a stream, and act as if session establishment fails. ctjdj|}jjj |y)N forbiddenrvrds rrz6SessionInitializerTests.testFailure..onSessionos6((5@@DH II   ! !( +r) r@rrrryrrwrrfrs` rrz#SessionInitializerTests.testFailurejsU  ,\\.) 4 YY__  2u001""B8,,r)r*r+r,rArrrrrs@rrrTs> - -rrceZdZdZdZy)BasicAuthenticatorTestszB Test for both BasicAuthenticator and basicClientFactory. cbtjd|_tj|jdj d}|j d|jj|j |j|jj|j d|jj|j\}}|j|tj|j|tj|j|j y)a Authenticator and stream are properly constructed by the factory. The L{xmlstream.XmlStream} protocol created by the factory has the new L{client.BasicAuthenticator} instance in its C{authenticator} attribute. It is set up with C{jid} and C{password} as passed to the factory, C{otherHost} taken from the client JID. The stream futher has two initializers, for TLS and authentication, of which the first has its C{required} attribute set to C{True}. rErFN example.com)r rI client_jidrbasicClientFactory buildProtocolr&r6 otherHostrJ initializersassertIsInstancer TLSInitiatingInitializerrH assertFalserequired)rrtlsauths r test_basicz"BasicAuthenticatorTests.test_basic~s''"=>  & &t A O OPT U (8(8(B(BC "*:*:*>*>? 2#3#3#<#<=OO T c9#E#EF dF$<$<= &rN)r*r+r,rArr-rrrrys 'rrc,eZdZdZdZeedZy)XMPPAuthenticatorTestsz@ Test for both XMPPAuthenticator and XMPPClientFactory. cvtjd|_tj|jdj d}|j d|jj|j |j|jj|j d|jj|j\}}}}}|j|tj|j|t|j|tj|j|tj |j#|j$|j#|j$|j#|j$|j'|j$y)z Test basic operations. Setup an XMPPClientFactory, which sets up an XMPPAuthenticator, and let it produce a protocol instance. Then inspect the instance variables of the authenticator and XML stream objects. rErFNr)r rIrrXMPPClientFactoryrr&r6rrJrrr rr rr assertTruerr)rrrrsaslrsessions rrz!XMPPAuthenticatorTests.test_basics?''"=>  % %doox @ N Nt T (8(8(B(BC "*:*:*>*>? 2#3#3#<#<=-/OO)dD' c9#E#EF d$=> dF$:$:; gv'@'@A  %  &  & ))*rc g d fd }tjd|_tj}t j |jd|}|jtjd||jd}|j\}}}}} |j|tj|j| dy) zG A TLS configuration is passed to the TLS initializer. Nc(j|yr)append)rrrconfigurationForTLSconfigss rrz:XMPPAuthenticatorTests.test_tlsConfiguration..inits NN. /rrErF)r__init__r)TN)r rIrr CertificateOptionsrrpatchr rrrrassertIs) rrrfactoryrrrrrrrs @rtest_tlsConfigurationz,XMPPAuthenticatorTests.test_tlsConfigurations  0''"=> "446** OOX;N  955z4H  " "4 (-/OO)dD' c9#E#EF )71:6rN)r*r+r,rArr skipWhenNoSSLrr-rrrrs&+B ]77rr)"rAhashlibrrrtwisted.internetr twisted.trialtwisted.words.protocols.jabberrrr r #twisted.words.protocols.jabber.saslr twisted.words.xishr r r ImportErrorrerXrr NS_SESSIONrTestCaserr/rCrrrrr-rrrs ""HHI& $  M? ? ,2W< 2 8:E?8#4#4?,$$ND-98;L;LD-N+-79J9J+-\"-:H