ϪfhBdZddlmZddlmZddlmZddlmZddl m Z ddl m Z ddl mZeejGd d ZGd d ej"ZGd dej"ZGddej(ZGddej"Zy)z$ Tests for L{twisted.conch.telnet}. ) implementer) verifyObject)telnet)defer) iterbytes) proto_helpers)unittestc\eZdZdZdZdZdZdZdZdZ dZ dZ d Z d Z d Zd Zd Zy) TestProtocolcfd|_g|_g|_g|_g|_g|_g|_y)N)datasubcmdcalls enabledLocal enabledRemote disabledLocaldisabledRemoteselfs @/usr/lib/python3/dist-packages/twisted/conch/test/test_telnet.py__init__zTestProtocol.__init__s7    rcix}|_j|d<|jjx}|_dD]}|ffd |t t |<y)N) EORNOPDMBRKIPAOAYTECELGAc:jj|SN)rappend)argcmdrs rz-TestProtocol.makeConnection..)s4::;L;LS;Qr)negotiationMapneg_TEST_COMMAND commandMapcopygetattrr)r transportdr*s` rmakeConnectionzTestProtocol.makeConnection#s`'))I $**' #,#7#7#<#<#>>I S RC69&QAgfc" # Rrc.|xj|z c_yr')rrrs r dataReceivedzTestProtocol.dataReceived+s T rcyr'r )rreasons rconnectionLostzTestProtocol.connectionLost.s rc||_yr')r)rpayloads rr-zTestProtocol.neg_TEST_COMMAND1s  rcX||jvr|jj|yyNTF)localEnableablerr(roptions r enableLocalzTestProtocol.enableLocal4s* T)) )    $ $V ,rc:|jj|yr')rr(r?s r disableLocalzTestProtocol.disableLocal:s !!&)rcX||jvr|jj|yyr=)remoteEnableablerr(r?s r enableRemotezTestProtocol.enableRemote=s* T** *    % %f -rc:|jj|yr')rr(r?s r disableRemotezTestProtocol.disableRemoteCs ""6*rcyr'r rs rconnectionMadezTestProtocol.connectionMadeF rcyr'r )rcommandarguments runhandledCommandzTestProtocol.unhandledCommandJrKrcyr'r rrMrs runhandledSubnegotiationz$TestProtocol.unhandledSubnegotiationNrKrN)__name__ __module__ __qualname__r>rErr3r6r9r-rArCrFrHrJrOrRr rrr r sJO!R  * +   rr ceZdZdZy)InterfacesTestsc`tj}ttj|y)zO L{telnet.TelnetProtocol} implements L{telnet.ITelnetProtocol} N)rTelnetProtocolrITelnetProtocol)rps rtest_interfacezInterfacesTests.test_interfaceTs"  ! ! #V++Q/rN)rSrTrUr\r rrrWrWSs0rrWceZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZdZdZdZdZggggfdZdZdZdZdZdZdZdZdZdZdZ dZ!d Z"d!Z#d"Z$d#Z%d$Z&d%Z'd&Z(y')(TelnetTransportTestsz. Tests for L{telnet.TelnetTransport}. ctjt|_t j |_|jj|j yr')rTelnetTransportr r[rStringTransporttr3rs rsetUpzTelnetTransportTests.setUpas:'' 5..0 dff%rc|jj}gd}|D]}|jj||j|jdj |y)N)shere are some bytes la la lassome more arrive hereslots of bytes to play withsla la lasta de dasdumr)r[protocolr6 assertEqualrjoinrhLbs rtestRegularBytesz%TelnetTransportTests.testRegularBytesfsY FFOO  #A FF   " # !-rc|jj}gd}|D]}|jj||j|j|ddddz|dddzdz|dddzdz|dddzdzy) N)shere is the first line shere is the second line shere is the third line shere is the last line r  )r[rer6rfrrhs rtestNewlineHandlingz(TelnetTransportTests.testNewlineHandlingys FFOO  #A FF   " #  FF aD"I d3Bi  d3Bi      d3Bi    rc|jj}gd}|D]}|jj||j|jdj |j ddy)N)s*here are some bytes with an embedded IACs&and here is a test of a border escapes did you get that IAC?rs)r[rer6rfrrgreplacerhs r testIACEscapez"TelnetTransportTests.testIACEscapesf FFOO   #A FF   " # !!4!4['!JKrcz|jj}tjt t|z}dd|zdzg}|D]}|jj ||j |j|g|j |jdj|j|dy)NsHere's some bytes, tra la lasBut ono!s an interruptr) r[rerIACr0r6rfrrrgrw)rcmdNamerir*rjrks r_simpleCommandTestz'TelnetTransportTests._simpleCommandTests FFOOjj76733 ,kC.?BR.R S #A FF   " # 7), !!4!4S#!>?rc&|jdy)Nr r|rs r testInterruptz"TelnetTransportTests.testInterrupt %rc&|jdy)Nrr~rs rtestEndOfRecordz$TelnetTransportTests.testEndOfRecord &rc&|jdy)Nrr~rs rtestNoOperationz$TelnetTransportTests.testNoOperationrrc&|jdy)Nrr~rs r testDataMarkz!TelnetTransportTests.testDataMarkrrc&|jdy)Nrr~rs r testBreakzTelnetTransportTests.testBreakrrc&|jdy)Nr!r~rs rtestAbortOutputz$TelnetTransportTests.testAbortOutputrrc&|jdy)Nr"r~rs rtestAreYouTherez$TelnetTransportTests.testAreYouThererrc&|jdy)Nr#r~rs rtestEraseCharacterz'TelnetTransportTests.testEraseCharacterrrc&|jdy)Nr$r~rs r testEraseLinez"TelnetTransportTests.testEraseLinerrc&|jdy)Nr%r~rs r testGoAheadz TelnetTransportTests.testGoAheadrrc|jj}tjtjzdztjztj z}d|zdg}|D]}|jj ||j|jdj|j|d|j|jttdy)Ns hello worldsThese are some bytes but soonsthere will be some morers hello world)r[rerrzSBSEr6rfrrgrwrlistrrrir*rjrks rtestSubnegotiationz'TelnetTransportTests.testSubnegotiations FFOOjj699$'99FJJFR - 35O P #A FF   " # !!4!4S#!>? 4 .(A#BCrc|jj}tjtjzdztj ztjztj z}d|zdzdg}|D]}|jj ||j|jdj|j|d|j|jtj gy)NrsSome bytes are heresand herer) r[rerrzrrr6rfrrgrwrrs r testSubnegotiationWithEmbeddedSEz5TelnetTransportTests.testSubnegotiationWithEmbeddedSEs FFOOjj699$w.:VZZG&))S #c )K 7 E #A FF   " # !!4!4S#!>? FII;/rc tjtjzdztjzdztjztjz}t t |D]}t x}|j_|j|j|d|||d}}d|z|dzg}|D]}|jj||j|jdj|j|d|j|jtjgt!t#dzy)Nrshellos first parts last partr)rrzrrrangelenr r[rer3r6rfrrgrwrrr)rr*iriarkrjrs rtestBoundarySubnegotiationz/TelnetTransportTests.testBoundarySubnegotiations5 JJii  ii    jj   ii   s3x PA".. 0A  TVV $r7CGqA"A $45A *##D) *   QVVSXXa[%8%8c%B C   QXX {T)H:M5N'N O Prc|j|j||j|j||j|j||j|j|yr')rfrrrr)roeLeRdLdRs r_enabledHelperz#TelnetTransportTests._enabledHelper sT , "- "- ))2.rctjtjzdz}d|zdz}|jj ||j |jj j|j|d|j |jjtjtjzdz|j|jj yNrssurrounding bytessto spice things upr) rrzWILLr[r6rfrerrwrbvalueDONTrrr*rs rtestRefuseWillz#TelnetTransportTests.testRefuseWillsjj6;;&0#c),AA D! --t||C/EF fkk)AG)KL DFFOO,rctjtjzdz}d|zdz}|jj ||j |jj j|j|d|j |jjtjtjzdz|j|jj yr) rrzDOr[r6rfrerrwrbrWONTrrs r testRefuseDoz!TelnetTransportTests.testRefuseDosjj699$w.#c),AA D! --t||C/EF fkk)AG)KL DFFOO,rctjtjzdz}d|zdz}|jj}d|_|jj ||j|jjtjtjzdz|j|dgy)Npaddingstrailer)r)r) rrzrr[rer>r6rfrbrrrrr*rris r testAcceptDoz!TelnetTransportTests.testAcceptDo&sjj699$w.C*, FFOO& D! fkk)AG)KL A7),rctjtjzdz}d|zdz}|jj}d|_|jj ||j|jjtjtjzdz|j|dgy)Nsheaderr)rr) rrzrr[rerEr6rfrbrrrrs rtestAcceptWillz#TelnetTransportTests.testAcceptWill3sjj6;;&03+ FFOO' D! fii)?')IJ A7),rctjtjzdz}|jj d}d|j _d|z}|jj||j|jjj|j|d|j|jjtjtjzdz|j|j j d|j|jjdgy)N)yess fiddle deernor)rrzrr[getOptionStatehimstater6rfrerrwrbrrrrr*srs rtestAcceptWontz#TelnetTransportTests.testAcceptWont?sjj6;;&0 FF ! !' * s" D! --t||C/EF fkk)AG)KL d+ DFFOO :rctjtjzdz}|jj d}d|j _d|z}|jj||j|jjj|j|d|j|jjtjtjzdz|j|j j d|j|jjdgy)Nrrs fiddle dum rr)r)rrzrr[rusrr6rfrerrwrbrrrrs rtestAcceptDontz#TelnetTransportTests.testAcceptDontSsjj6;;&0 FF ! !' * # D! --t||C/EF fkk)AG)KL T* DFFOO :rctjtjzdz}d|zdz}|jj ||j |jj j|j|d|j |jjd|j|jj yNG dum de dum tra la lar) rrzrr[r6rfrerrwrbrrrs rtestIgnoreWontz#TelnetTransportTests.testIgnoreWontgsjj6;;&0s"\1 D! --t||C/EF - DFFOO,rctjtjzdz}d|zdz}|jj ||j |jj j|j|d|j |jjd|j|jj yr) rrzrr[r6rfrerrwrbrrrs rtestIgnoreDontz#TelnetTransportTests.testIgnoreDontssjj6;;&0s"\1 D! --t||C/EF - DFFOO,rctjtjzdz}|jj d}d|j _d|zdz}|jj||j|jjj|j|d|j|jjd|j|jjyNVrrrr)rrzrr[rrrr6rfrerrwrbrrrs rtestIgnoreWillz#TelnetTransportTests.testIgnoreWillsjj6;;&0 FF ! !' * c!M1 D! --t||C/EF - DFFOO,rctjtjzdz}|jj d}d|j _d|zdz}|jj||j|jjj|j|d|j|jjd|j|jjyr)rrzrr[rrrr6rfrerrwrbrrrs r testIgnoreDoz!TelnetTransportTests.testIgnoreDosjj699$w. FF ! !' * c!M1 D! --t||C/EF - DFFOO,rcjjd}jjd_j j j tjtjzdzjjtjtjzdz|jjd|jfd|S)NBrTc,jdgS)Nrr)r)_rirs rr+z@TelnetTransportTests.testAcceptedEnableRequest..s 3 3A7) 3 Dr) r[dorerErfrbrrrzrr6r addCallback)rr2ris` @rtestAcceptedEnableRequestz.TelnetTransportTests.testAcceptedEnableRequests FFIIg  FFOO' fii)?')IJ FJJ4w>? d&&- DErc|djj_jjd}j j j tjtjzdzjjdj jjdj jjdjjjj!jjjj#tjtj$zdzj'|tj(}|j+fd|j+fd|S)z If the peer refuses to enable an option we request it to enable, the L{Deferred} returned by L{TelnetProtocol.do} fires with an L{OptionRefused} L{Failure}. rrrcNjjjSr'rr[reignoredrs rr+z@TelnetTransportTests.test_refusedEnableRequest..d&9&9$&&//&JrcNjjjSr') assertFalser negotiatingrrrs rr+z@TelnetTransportTests.test_refusedEnableRequest..sd&6&6quu7H7H&Ir)r[rerErrfrbrrrzrrrrr assertTruerrr6r assertFailure OptionRefusedrrr2rs` @rtest_refusedEnableRequestz.TelnetTransportTests.test_refusedEnableRequests ,6( FFIIg  fii)?')IJ FF ! !' * d+ T* ))* ))* FJJ4w>?   q&"6"6 7 JK IJrc|djj_jjd}j j j tjtjzdzjjdj jjdj jjdjjjj!jjjj#tjtj$zdzj'|tj(}|j+fd|j+fd|S)z If the peer refuses to allow us to enable an option, the L{Deferred} returned by L{TelnetProtocol.will} fires with an L{OptionRefused} L{Failure}. rrrcNjjjSr'rrs rr+z>TelnetTransportTests.test_refusedEnableOffer..rrcNjjjSr')rrrrs rr+z>TelnetTransportTests.test_refusedEnableOffer..sd&6&6qtt7G7G&Hr)r[rer>willrfrbrrrzrrrrrrrrr6rrrrrs` @rtest_refusedEnableOfferz,TelnetTransportTests.test_refusedEnableOffers +5' FFKK  fkk)AG)KL FF ! !' * d+ T* **+ (() FJJ4w>?   q&"6"6 7 JK HIrcjjd}d|j_jj d}j j jtjtjzdzjjtjtjzdz|jj d|jfd|S)NrrTcTjjjdgS)Nrrrrrs rr+zATelnetTransportTests.testAcceptedDisableRequest..s! 3 3DFFOO 3 Rr)r[rrrdontrfrbrrrzrr6rrrrr2s` rtestAcceptedDisableRequestz/TelnetTransportTests.testAcceptedDisableRequests FF ! !' * FFKK  fkk)AG)KL FJJ4w>? d&&- RSrcjjd}d|j_jj dfd}fd}fd}|d}|j ||j ||S)N$rczjjd}j|tjSNr)r[rrrAlreadyNegotiatingxr2rs r_dozITelnetTransportTests.testNegotiationBlocksFurtherNegotiation.._do s. '"A%%a)B)BC Crczjjd}j|tjSr)r[rrrrrs r_dontzKTelnetTransportTests.testNegotiationBlocksFurtherNegotiation.._donts. G$A%%a)B)BC Crcjjtjtjzdzj jj dgdjj _jjd}jjtjtjzdz|jjd|jfd|S)Nrr)rTcXjjjdgdgS)Nr)rrrrs rr+z^TelnetTransportTests.testNegotiationBlocksFurtherNegotiation.._final..s*$--FFOO wi.r) r[r6rrzrrrerErrrrfrs r_finalzLTelnetTransportTests.testNegotiationBlocksFurtherNegotiation.._finals FF   V[[ 87 B C   WI  >/9DFFOO , '"A FF   V[[ 87 B C MM$**D 1 MM  Hr)r[rrrrr)rrrrrr2s` r'testNegotiationBlocksFurtherNegotiationz.TestExceptionN)rSrTrUr rr TestExceptionr8s rrzTotal failure!)r[r Exceptionr9rr gatherResults)rd1d2d3rs r testLostConnectionFailsDeferredsz5TelnetTransportTests.testLostConnectionFailsDeferreds3s VVYYw  VVYYw  VVYYw  I  m,<=>   M 2   M 2   M 2""BB<00rN))rSrTrU__doc__rcrlrtrxr|rrrrrrrrrrrrrrrrrrrrrrrrrrrrrr rrr rrr^r^\s& .& 6L" @&''&'&'&&& D 0P6$&"/ - - - -;(;( - --&-&$88$#J= < 1rr^c(eZdZdZdZdZdZdZy) TestTelnetzP A trivial extension of the telnet protocol class useful to unit tests. cPtjj|g|_yr')rTelnetreventsrs rrzTestTelnet.__init__Hs t$ rc>|jjd|fy)z: Record the given data in C{self.events}. bytesNr!r(r5s rapplicationDataReceivedz"TestTelnet.applicationDataReceivedLs GT?+rc@|jjd||fy)z= Record the given command in C{self.events}. rMNr$rQs rrOzTestTelnet.unhandledCommandRs Iw56rc@|jjd||fy)zL Record the given subnegotiation command in C{self.events}. negotiateNr$rQs rrRz"TestTelnet.unhandledSubnegotiationXs K$78rN)rSrTrUrrr%rOrRr rrrrCs, 7 9rrcpeZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZdZdZy) TelnetTestsz Tests for L{telnet.Telnet}. L{telnet.Telnet} implements the TELNET protocol (RFC 854), including option and suboption negotiation, and option state tracking. c"t|_y)zM Create an unconnected L{telnet.Telnet} to be used by tests. N)rrers rrczTelnetTests.setUpgs #  rcX|j|jjdy)z L{telnet.Telnet.enableLocal} should reject all options, since L{telnet.Telnet} does not know how to implement any options. N)rrerArs rtest_enableLocalzTelnetTests.test_enableLocalms! 2259:rcX|j|jjdy)z L{telnet.Telnet.enableRemote} should reject all options, since L{telnet.Telnet} does not know how to implement any options. r-N)rrerFrs rtest_enableRemotezTelnetTests.test_enableRemotets! 33E:;rcZ|jt|jjdy)a  It is an error for L{telnet.Telnet.disableLocal} to be called, since L{telnet.Telnet.enableLocal} will never allow any options to be enabled locally. If a subclass overrides enableLocal, it must also override disableLocal. r-N) assertRaisesNotImplementedErrorrerCrs rtest_disableLocalzTelnetTests.test_disableLocal{s! -t}}/I/I5QrcZ|jt|jjdy)a It is an error for L{telnet.Telnet.disableRemote} to be called, since L{telnet.Telnet.enableRemote} will never allow any options to be enabled remotely. If a subclass overrides enableRemote, it must also override disableRemote. r-N)r2r3rerHrs rtest_disableRemotezTelnetTests.test_disableRemotes! -t}}/J/JERrctj}|jj||jj dd|j |j dy)z L{telnet.Telnet.requestNegotiation} formats the feature byte and the payload bytes into the subnegotiation format and sends them. See RFC 855. ssNrrarer3requestNegotiationrfrrr1s rtest_requestNegotiationz#TelnetTests.test_requestNegotiationsS"113  $$Y/ ((+>  OO  + rctj}|jj||jj dd|j |j dy)z If the payload for a subnegotiation includes I{IAC}, it is escaped by L{telnet.Telnet.requestNegotiation} with another I{IAC}. See RFC 855. r8rvsNr9r;s r!test_requestNegotiationEscapesIACz-TelnetTests.test_requestNegotiationEscapesIACsP"113  $$Y/ ((': *,KLrcgx}|j_|jj||j|t |y)z Pass the given bytes to the protocol's C{dataReceived} method and assert that the given events occur. N)rer!r6rfr)rrexpectedreceiveds r_deliverzTelnetTests._delivers; +-,4==' ""4( 4>2rc(|jddy)zc One application-data byte in the default state gets delivered right away. a)r#rDNrBrs rtest_oneApplicationDataBytez'TelnetTests.test_oneApplicationDataBytes dO,rc(|jddy)za Two application-data bytes in the default state get delivered together. bc)r#rHNrErs rtest_twoApplicationDataBytesz(TelnetTests.test_twoApplicationDataBytess e-.rcJ|jdtjzdy)z~ Three application-data bytes followed by a control byte get delivered, but the control byte doesn't. def)r#rKNrBrrzrs rtest_threeApplicationDataBytesz*TelnetTests.test_threeApplicationDataBytess fvzz)+<=rc|jtj|jtjdzdtjdzfy)zy IAC in the escaped state gets delivered and so does another application-data byte following it. gr#NrLrs rtest_escapedControlzTelnetTests.test_escapedControls9 fjj! fjj4''6::3D)EFrc|jd|jdd|jdd|jd|jdd|jdd|jd|jdd |jd d |jd|jtjtjzd zd dtjzd zfy )a A carriage return only puts the protocol into the newline state. A linefeed in the newline state causes just the newline to be delivered. A nul in the newline state causes a carriage return to be delivered. An IAC in the newline state causes a carriage return to be delivered and puts the protocol into the escaped state. Anything else causes a carriage return and that thing to be delivered. rqro)r#ros r-)r#rqs rD)r# arRxr#NrLrs rtest_carriageReturnzTelnetTests.test_carriageReturns e e-. g/0 e e-. g/0 e d-. f/0 e JJ #d *Wefjj6H46O,P rc|jdtjztjzddtjdfy)zt Application bytes received before a command are delivered before the command is processed. rS)r#rSrMN)rBrrzrrs r'test_applicationDataBeforeSimpleCommandz3TelnetTests.test_applicationDataBeforeSimpleCommands6 6::  *   D ) rci|j_|jdtjztj zdzddtj dfy)z~ Application bytes received before a WILL/WONT/DO/DONT are delivered before the command is processed. yr-)r#rXrMN)rer.rBrrzrrs r!test_applicationDataBeforeCommandz-TelnetTests.test_applicationDataBeforeCommandsG $&   6::  +g 5   W - rc|jdtjztjzdztjztjzddddgfy)z Application bytes received before a subnegotiation command are delivered before the negotiation is processed. zsQx)r#r[r(QrSN)rBrrzrrrs r(test_applicationDataBeforeSubnegotiationz4TelnetTests.test_applicationDataBeforeSubnegotiationsK 6::  )E 1FJJ > J  $ ' rN)rSrTrUrrcr.r0r4r6r<r>rBrFrIrMrPrTrVrYr]r rrr*r*_s[% ;<RS M3-/>G 6      rr*N)rzope.interfacerzope.interface.verifyr twisted.conchrtwisted.internetrtwisted.python.compatr twisted.testr twisted.trialr rZr TestCaserWr^r rr*r rrrfs  '. "+&" V # #$; ; %; |0h''0d18,,d1N998k (##k r