ϪfHrBdZddlZddlmZddlmZddlmZddlm Z edZ ddl m Z e r dd lm Z mZn Gd d ZGd d ejZGddZGddej$ZGdde j(ZGdde j(Zy)z1 This module tests twisted.conch.ssh.connection. N)channel) test_userauth) requireModule)unittest cryptography)error)common connectionc eZdZGddZy)r c eZdZy)connection.SSHConnectionN)__name__ __module__ __qualname__D/usr/lib/python3/dist-packages/twisted/conch/test/test_connection.py SSHConnectionr s rrN)rrrrrrrr r s   rr cReZdZdZdZdZdZdZdZdZ dZ dZ d Z d Z d Zd Zy ) TestChannela A mocked-up version of twisted.conch.ssh.channel.SSHChannel. @ivar gotOpen: True if channelOpen has been called. @type gotOpen: L{bool} @ivar specificData: the specific channel open data passed to channelOpen. @type specificData: L{bytes} @ivar openFailureReason: the reason passed to openFailed. @type openFailed: C{error.ConchError} @ivar inBuffer: a C{list} of strings received by the channel. @type inBuffer: C{list} @ivar extBuffer: a C{list} of 2-tuples (type, extended data) of received by the channel. @type extBuffer: C{list} @ivar numberRequests: the number of requests that have been made to this channel. @type numberRequests: L{int} @ivar gotEOF: True if the other side sent EOF. @type gotEOF: L{bool} @ivar gotOneClose: True if the other side closed the connection. @type gotOneClose: L{bool} @ivar gotClosed: True if the channel is closed. @type gotClosed: L{bool} TestChannelFc d|jzS)NzTestChannel %i)idselfs r logPrefixzTestChannel.logPrefix:s$''))rctd|_||_g|_g|_d|_d|_d|_d|_y)zF The channel is open. Set up the instance variables. TrFN)gotOpen specificDatainBuffer extBuffernumberRequestsgotEOF gotOneClose gotClosed)rrs r channelOpenzTestChannel.channelOpen=s? (   rc||_y)zD Opening the channel failed. Store the reason why. N)openFailureReason)rreasons r openFailedzTestChannel.openFailedJs "(rc6|xjdz c_|dk(S)z_ A test request. Return True if data is 'data'. @type data: L{bytes} data)r"rdatas r request_testzTestChannel.request_testPs q wrc:|jj|y)z= Data was received. Store it in the buffer. N)r appendr.s r dataReceivedzTestChannel.dataReceivedYs T"rc>|jj||fy)zF Extended data was received. Store it in the buffer. N)r!r2)rcoder/s r extReceivedzTestChannel.extReceived_s tTl+rcd|_y)z1 EOF was received. Remember it. TN)r#rs r eofReceivedzTestChannel.eofReceivedes  rcd|_y)z3 Close was received. Remember it. TN)r$rs r closeReceivedzTestChannel.closeReceivedks  rcd|_y)z7 The channel is closed. Rembember it. TN)r%rs rclosedzTestChannel.closedqs rN)rrr__doc__namerr%rr&r*r0r3r6r8r:r<rrrrrsE2 DGI* ( # ,   rrc eZdZdZdZdZdZy) TestAvatarz? A mocked-up version of twisted.conch.avatar.ConchUser {c|tjk(rt||||S|dk(r tj|jdy)z The server wants us to return a channel. If the requested channel is our TestChannel, return it, otherwise return None. ) remoteWindowremoteMaxPacketr/avatarconch-error-argserror args in wrong orderN)rr>r ConchError_ARGS_ERROR_CODE)r channelType windowSize maxPacketr/s r lookupChannelzTestAvatar.lookupChannelsW +** *' )   / / ""4#8#8:UV V 0rc"|dk(ry|dk(rd|fSy)z The client has made a global request. If the global request is 'TestGlobal', return True. If the global request is 'TestData', return True and the request-specific data we received. Otherwise, return False. TestGlobalTTestDataFr)r requestTyper/s rgotGlobalRequestzTestAvatar.gotGlobalRequests$ - ' K ': rN)rrrr=rIrMrRrrrr@r@xsW& rr@c6eZdZdZesdZdZdZdZdZ dZ y) TestConnectionz} A subclass of SSHConnection for testing. @ivar channel: the current channel. @type channel. C{TestChannel} Cannot run without cryptographycy)NrTrrs rrzTestConnection.logPrefixsrcy)zT The other side made the 'TestGlobal' global request. Return True. Trr.s rglobal_TestGlobalz TestConnection.global_TestGlobalsrc d|fS)zt The other side made the 'Test-Data' global request. Return True and the data we received. Trr.s rglobal_Test_DatazTestConnection.global_Test_Datas Tzrc@t||||_|jS)z The other side is requesting the TestChannel. Create a C{TestChannel} instance, store it, and return it. )rCrDr/)rrrrKrLr/s rchannel_TestChannelz"TestConnection.channel_TestChannels# ##YT  ||rctd)zU The other side is requesting the ErrorChannel. Raise an exception. z no such thing)AssertionErrorr\s rchannel_ErrorChannelz#TestConnection.channel_ErrorChannels_--rN) rrrr=rskiprrXrZr]r`rrrrTrTs* 0  .rrTceZdZesdZdZdZdZdZdZ dZ dZ d Z d Z d Zd Zd ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZ dZ!dZ"d Z#d!Z$d"Z%y#)$ConnectionTestsrUctjd|_t|j_t |_|j|j _|j jyN)r FakeTransport transportr@rErTconnserviceStartedrs rsetUpzConnectionTests.setUpsL&44T: * "$ "nn    "rc|jj||jjdd|j_|jj t j d|jddzy)z? Open the channel with the default connection. Nz>2Ls)rh openChannelrgpacketsssh_CHANNEL_OPEN_CONFIRMATIONstructpackrrrs r _openChannelzConnectionTests._openChannels_ g&!%!7!7!< // KKwzz3 /2U U rc8|jjyre)rhserviceStoppedrs rtearDownzConnectionTests.tearDowns   "rcx|j|jjj|jy)z_ Test that the connection links itself to the avatar in the transport. N)assertIsrgrErhrs rtest_linkAvatarzConnectionTests.test_linkAvatars& dnn++00$)).check!s   T7 +r)rhsendGlobalRequestssh_REQUEST_SUCCESS addCallback addErrbackfailrdrs` rtest_REQUEST_SUCCESSz$ConnectionTests.test_REQUEST_SUCCESSsW II ' ' GT B %%g. , e TYYrcjjddd}jjdfd}|jj|j ||S)zg Test that global request failure packets cause the Deferred to be erred back. rr-TcRj|jjdyr)rvaluer/)frs rrz3ConnectionTests.test_REQUEST_FAILURE..check0s   QWW\\7 3r)rhrssh_REQUEST_FAILURErrrrs` rtest_REQUEST_FAILUREz$ConnectionTests.test_REQUEST_FAILURE(sW II ' ' GT B %%g. 4 dii  Urc|j`|jjt j ddz|j |jjj|j|jjj|j|j|jjjd|j|jjjd|j|jjjd|j|jjjd|j|jjtj dfgg|j_|jjt j ddz|j#|j|jjtj$dt j d zt j d zfgg|j_|jjt j d dz|j#|j|jjtj$d t j d zt j d zfgy)z Test that open channel packets cause a channel to be created and opened or a failure message to be returned. rr,s BadChannelsssunknown channelrs ErrorChannelssunknown failureN)rgrErhssh_CHANNEL_OPENr rr}rrrr/rremoteWindowLeftrDror MSG_CHANNEL_OPEN_CONFIRMATIONflushLoggedErrorsMSG_CHANNEL_OPEN_FAILURErs rtest_CHANNEL_OPENz!ConnectionTests.test_CHANNEL_OPEN7sF NN ! ""699^#  NN " "<<(  "$ ""699]#;>U#UV    NN " "777ii 234iin%  "$ ""699_#=@W#WX    NN " "777ii 234iin%  rc0||jj_|jj t j ddz|jtj}|jt|dd||j|djjd|j|jjtj dt j dzt j d zfgy ) z Deliver a request for a channel open which will result in an exception being raised during channel lookup. Assert that an error response is delivered as a result. rFrr,zExpected one error, got: r)rArGs{serror args in wrong orderrN)rgrErIrhrr rrrrHrlenrargsror r)rr5errorss r_lookupChannelErrorTestz'ConnectionTests._lookupChannelErrorTestks 26. "" II) *-D D ''(8(89 Va+DVJ)OP --/QR  NN " "778ii <=>iin%   rc&|jdy)a If a C{lookupChannel} implementation raises L{error.ConchError} with the arguments in the wrong order, a C{MSG_CHANNEL_OPEN} failure is still sent in response to the message. This is a temporary work-around until L{error.ConchError} is given better attributes and all of the Conch code starts constructing instances of it properly. Eventually this functionality should be deprecated and then removed. rAN)rrs rtest_lookupChannelErrorz'ConnectionTests.test_lookupChannelErrors $$S)rct}|jj||jjd|j |j d|j |j d|j |jd|j |jj|d|j |jjddy)zv Test that channel open confirmation packets cause the channel to be notified that it's open. srN) rrhrnrprrrDrchannelsToRemoteChannellocalToRemoteChannelrss rtest_CHANNEL_OPEN_CONFIRMATIONz.ConnectionTests.test_CHANNEL_OPEN_CONFIRMATIONs - g& //0GH 1115 00!4 --/BC ::7CQG 77:A>rcdt}|jj||jjdt j dz|j |jjd|j|jjj|y)zz Test that channel open failure packets cause the channel to be notified that its opening failed. failure!)rr,N) rrhrnssh_CHANNEL_OPEN_FAILUREr rrr(r assertIsNonechannelsgetrss rtest_CHANNEL_OPEN_FAILUREz)ConnectionTests.test_CHANNEL_OPEN_FAILUREs - g& ** 3fii 6L L  22779IJ $)),,009:rct}|j||j}|jj d|j |j|dzy)zc Test that channel window adjust messages add bytes to the channel window. rr,N)rrtrrhssh_CHANNEL_WINDOW_ADJUSTr)rr oldWindowSizes rtest_CHANNEL_WINDOW_ADJUSTz*ConnectionTests.test_CHANNEL_WINDOW_ADJUSTsR - '"00  ++,ST 11=13DErctdd}|j||jjdt j dz|j |jdg|j |jjtjdfgg|j_ d|jdzz}|jjdt j |z|j |jdg|j |jjtjd fgt}|j|d|jdzz}g|j_ |jjd t j |z|j |jg|j |jjtjd fgy ) z Test that channel data messages are passed up to the channel, or cause the channel to be closed if the data is too large.  localWindowlocalMaxPacketrr-ar,rN)rrtrhssh_CHANNEL_DATAr rrr rgror MSG_CHANNEL_WINDOW_ADJUSTlocalWindowLeftMSG_CHANNEL_CLOSErrrlongDatabigDatas rtest_CHANNEL_DATAz!ConnectionTests.test_CHANNEL_DATAs !A> '" ""#679K#KL ))G95  NN " "88;  "$722Q67 ""#689L#LM ))G95  NN " "**,? @ A - '"'00145!# ""#679K#KL ))2.  NN " "**,? @ A rctdd}|j||jjdt j dz|j |jdg|j |jjtjdfgg|j_ d|jd zz}|jjdt j |z|j |jdg|j |jjtjd fgt}|j|d|jd zz}g|j_ |jjd t j |z|j |jg|j |jjtjd fgy ) z Test that channel extended data messages are passed up to the channel, or cause the channel to be closed if they're too big. rrrsr-)rr-rrr,rsN)rrtrhssh_CHANNEL_EXTENDED_DATAr rrr!rgror rrrrrs rtest_CHANNEL_EXTENDED_DATAz*ConnectionTests.test_CHANNEL_EXTENDED_DATAs !A> '" ++ 3fii6H H  **\N;  NN " "88;  "$722Q67 ++ 3fii6I I  **\N;  NN " "**,? @ A - '"'00145!# ++ 3fii6H H  **B/  NN " "**,? @ A rct}|j||jjd|j |j y)zN Test that channel eof messages are passed up to the channel. rN)rrtrhssh_CHANNEL_EOFr}r#rss rtest_CHANNEL_EOFz ConnectionTests.test_CHANNEL_EOF s<- '" !!"56 'rct}|j||j|j|j |j |j |j |jj||jjd|j|j |j|j y)z Test that channel close messages are passed up to the channel. Also, test that channel.close() is called if both sides are closed when this message is received. rN) rrtr}rr~r$r%rh sendClosessh_CHANNEL_CLOSErss rtest_CHANNEL_CLOSEz"ConnectionTests.test_CHANNEL_CLOSEs - '" ( ,,- **+ G$ ##$78 ++, ))*rcxt}j|jjdt j dzdzj |jdjjdt j dzdzdz}fd}|j||S)zS Test that channel requests that succeed send MSG_CHANNEL_SUCCESS. rtestrr,rr-ctjjjtjdfgyNr)rrgror MSG_CHANNEL_SUCCESSresultrs rrz;ConnectionTests.test_CHANNEL_REQUEST_success..check10   &&002EFG r) rrtrhssh_CHANNEL_REQUESTr rrr"rrrrrs` rtest_CHANNEL_REQUEST_successz,ConnectionTests.test_CHANNEL_REQUEST_success#s- '" %% &))G"4 4w >  //3 II ) ) &))G"4 4w > H   erct}j|jjdt j dzdz}fd}|j j|j||S)zP Test that channel requests that fail send MSG_CHANNEL_FAILURE. rrrctjjjtjdfgyr)rrgror MSG_CHANNEL_FAILURErs rrz;ConnectionTests.test_CHANNEL_REQUEST_failure..checkDrr) rrtrhrr rrrrrs` rtest_CHANNEL_REQUEST_failurez,ConnectionTests.test_CHANNEL_REQUEST_failure:sl- '" II ) ) &))G"4 4w >   dii  Urct}j|jj|ddd}jj dfd}|S)zj Test that channel request success messages cause the Deferred to be called back. rr-Trc(j|yre)r}rs rrz;ConnectionTests.test_CHANNEL_REQUEST_SUCCESS..checkXs OOF #r)rrtrh sendRequestssh_CHANNEL_SUCCESSrs` rtest_CHANNEL_REQUEST_SUCCESSz,ConnectionTests.test_CHANNEL_REQUEST_SUCCESSNsS - '" II ! !'7GT B %%&9: $rct}j|jj|ddd}jj dfd}|j j |j||S)zi Test that channel request failure messages cause the Deferred to be erred back. rrTrcRj|jjdy)Nzchannel request failed)rrrs rrz;ConnectionTests.test_CHANNEL_REQUEST_FAILURE..checkgs   V\\//1I Jr)rrtrhrssh_CHANNEL_FAILURErrrrs` rtest_CHANNEL_REQUEST_FAILUREz,ConnectionTests.test_CHANNEL_REQUEST_FAILURE]sp - '" II ! !'7C > %%&9: K dii  Urc|jjddd}|jd|jjddd|j|jj t jtjddzft jtjdd zfg|j|jjd |giy ) zQ Test that global request messages are sent in the right format. s wantReplyr-Tcyrerfailures rz8ConnectionTests.test_sendGlobalRequest..trsnoReplyrFsdatarglobalN) rhrrrrgror MSG_GLOBAL_REQUESTr r deferredsrrs rtest_sendGlobalRequestz&ConnectionTests.test_sendGlobalRequestns II ' ' gt D )* ##JU;  NN " ".. ,0G+0UV.. *0E0OP   ,,x!o>rcnt}|jj|d|j|jj t jtjddzfg|j|jd|j|jjdy)zO Test that open channel messages are sent in the right format. saaaarsaaaarr,N) rrhrnrrgror MSG_CHANNEL_OPENr rrlocalChannelIDrss rtest_openChannelz ConnectionTests.test_openChannels- gw/  NN " "//IIn-MN   Q' 1115rc ht}|j||jj|ddd}|j d|jj|dddd|_|jj|ddd|j |jjtjdtjdzd zftjdtjdzd zfg|j |jjd |gy ) zR Test that channel request messages are sent in the right format. rTcyrerrs rrz2ConnectionTests.test_sendRequest..rrtest2rFstest3rstestrrN)rrtrhrr localClosedrrgror MSG_CHANNEL_REQUESTr rrrrrs rtest_sendRequestz ConnectionTests.test_sendRequests- '" II ! !'7GT B )* gxe<" gxd;  NN " "22'&))G*<<{J 22'&))H*==G   ,,Q/!5rctd}|j|d|_|jj |d|j |jdd|_|jj |d|j |jd|j |jjtjdfgy) zi Test that channel window adjust messages cause bytes to be added to the window. r)rrr,TN) rrtrrh adjustWindowrrrgror rrss rtest_adjustWindowz!ConnectionTests.test_adjustWindows !, '""# w* 00!4" w* 00!4  NN " "88;  rcRt}|j||jj|dd|_|jj|d|j |j jtjdtjdzfgy)zO Test that channel data messages are sent in the right format. rTbrN) rrtrhsendDatarrrgror MSG_CHANNEL_DATAr rrss r test_sendDatazConnectionTests.test_sendDatas- '" 7D)" 7D)  NN " "))+>4+P Q R rcVt}|j||jj|ddd|_|jj|dd|j |j jtjdtjdzfgy)zX Test that channel extended data messages are sent in the right format. r,rTr rr N) rrtrhsendExtendedDatarrrgror MSG_CHANNEL_EXTENDED_DATAr rrss rtest_sendExtendedDataz%ConnectionTests.test_sendExtendedDatas- '" ""7Aw7" ""7Ax8  NN " "88= '@RR  rct}|j||jj||j |j j tjdfgd|_ |jj||j |j j tjdfgy)zN Test that channel EOF messages are sent in the right format. rTN) rrtrhsendEOFrrgror MSG_CHANNEL_EOFrrss r test_sendEOFzConnectionTests.test_sendEOFs- '" '"  NN " "j&@&@BU%V$W # '"  NN " "j&@&@BU%V$W rct}|j||jj||j |j |j |jjtjdfg|jj||j |jjtjdfgt}|j||j |j|j|jd|_|jj||j |jy)zP Test that channel close messages are sent in the right format. rTN)rrtrhrr}rrrgror rrr~r% remoteClosed)rrrs rtest_sendClosezConnectionTests.test_sendCloses- '" G$ ++,  NN " "**,? @ A  G$  NN " "**,? @ A = (# (() ++, $ H% **+rcX|jjdddd}|j|jd|j|jd|j|j d|j tj|jjddddy)z Test that getChannel dispatches to the avatar when an avatar is present. Correct functioning without the avatar is verified in test_CHANNEL_OPEN. r2r-rN) rh getChannelrr/rrD assertRaisesrrHrss rtest_getChannelWithAvatarz)ConnectionTests.test_getChannelWithAvatars ))&&~r2wG w/ 1126 00"5    dii22M2r7 rc |j`|j|jj dd|j |jj ddd|j |jj ddy)zW Test that gotGlobalRequests dispatches to global_* without an avatar. rOr-s Test-Data)Tr-s BadGlobalN)rgrEr}rhrRrr~rs r"test_gotGlobalRequestWithoutAvatarz2ConnectionTests.test_gotGlobalRequestWithoutAvatarsp NN !  22='JK  II & &|W =  33L'JKrct}|j||jj|ddd}|j |t j }|jj||S)z Whenever an SSH channel gets closed any Deferred that was returned by a sendRequest() on its parent connection must be errbacked. dummyrequest dummydatar, wantReply)rrtrhr assertFailurerrH channelClosedr s r9test_channelClosedCausesLeftoverChannelDeferredsToErrbackzIConnectionTests.test_channelClosedCausesLeftoverChannelDeferredsToErrback(sf - '" II ! !'?LTU ! V   q%"2"2 3 (rN)&rrrrrarjrtrwrzrrrrrrrrrrrrrrrrrrrrr rrrrrr%r'r/rrrrcrcs 0# #=J*50  2 h 8 * ? ; F# J) V(+ .( "?"6&66 .   & ,4   L rrcc$eZdZdZesdZdZdZy)CleanConnectionShutdownTestszL Check whether correct cleanup is performed on connection shutdown. rUctjd|_t|j_t |_|j|j _yre)rrfrgr@rErTrhrs rrjz"CleanConnectionShutdownTests.setUp>s<&44T: * "$ "nn rc|jj|jjddd}|j|tj }|jj |S)z Once the service is stopped any leftover global deferred returned by a sendGlobalRequest() call must be errbacked. r)r*r,r+)rhrirr-rrHrvrs r9test_serviceStoppedCausesLeftoverGlobalDeferredsToErrbackzVCleanConnectionShutdownTests.test_serviceStoppedCausesLeftoverGlobalDeferredsToErrbackDs]   " II ' 'QR ' S   q%"2"2 3   "rN)rrrr=rrarjr4rrrr1r16s 0- rr1)r=rqtwisted.conch.sshrtwisted.conch.testrtwisted.python.reflectr twisted.trialrr twisted.conchrr r SSHChannelrr@rrTTestCasercr1rrrr<s%,0"^, 44 Y'$$Yx&&R).Z--).Xf h''f R8#4#4r