Ϫf4UdZddlZddlZddlmZdZeeed<ddlm Z ddl m Z ddl m Z mZmZmZmZddlmZmZdd lmZdd lmZd ZeZGd d ej4ZGddej4ZGddej4ZGddZGddeej4eZGdde ej4eZ Gddeej4eZ!Gddej4Z"y)z% Tests for L{twisted.conch.manhole}. N)Optionalssh)manhole)insults) _SSHMixin _StdioMixin _TelnetMixinrstdio)defererror)StringTransport)unittestcb ddzy#t$rtjddcYSwxYw)z Return the string used by Python as the name for code objects which are compiled from interactive input or at the top-level of modules. rN) BaseException traceback extract_stackA/usr/lib/python3/dist-packages/twisted/conch/test/test_manhole.pydetermineDefaultFunctionNamers8 0 Q 0&&(,Q// 0s #..ceZdZdZdZy)ManholeInterpreterTestsz2 Tests for L{manhole.ManholeInterpreter}. ctjd}|jjddg|j |j |jy)zR L{ManholeInterpreter.resetBuffer} should empty the input buffer. N12)rManholeInterpreterbufferextend resetBuffer assertFalse)self interpreters rtest_resetBufferz(ManholeInterpreterTests.test_resetBuffer5sL006 !!3*-! ++,rN)__name__ __module__ __qualname____doc__r&rrrrr0s -rrceZdZdZdZy)ManholeProtocolTestsz' Tests for L{manhole.Manhole}. c@t}tjtj}|j ||j }|j}|jjddg|j|j|jy)zn L{manhole.Manhole.handle_INT} should cause the interpreter input buffer to be reset. rrN) r rServerProtocolrManholemakeConnectionterminalProtocolr%r r! handle_INTr#)r$ transportterminalprotocolr%s r%test_interruptResetsInterpreterBufferz:ManholeProtocolTests.test_interruptResetsInterpreterBufferDs| $% ))'//: *,,** !!3*- ++,rN)r'r(r)r*r6rrrr,r,?s  -rr,cHeZdZdZdZdZdZdZdZdZ dZ d Z d Z y ) WriterTestsc.tjdy)z& Colorize an integer. rNrlastColorizedLiner$s r test_IntegerzWriterTests.test_IntegerTs !!#&rc.tjdy)z7 Colorize an integer in double quotes. z"1"Nr:r<s rtest_DoubleQuoteStringz"WriterTests.test_DoubleQuoteStringZ !!%(rc.tjdy)z7 Colorize an integer in single quotes. z'1'Nr:r<s rtest_SingleQuoteStringz"WriterTests.test_SingleQuoteString`r@rc.tjdy)z7 Colorize an integer in triple quotes. z'''1'''Nr:r<s rtest_TripleSingleQuotedStringz)WriterTests.test_TripleSingleQuotedStringf !!),rc.tjdy)zB Colorize an integer in triple and double quotes. z"""1"""Nr:r<s rtest_TripleDoubleQuotedStringz)WriterTests.test_TripleDoubleQuotedStringlrErc.tjdy)z1 Colorize a function definition. z def foo():Nr:r<s rtest_FunctionDefinitionz#WriterTests.test_FunctionDefinitionr !!,/rc.tjdy)z. Colorize a class definition. z class foo:Nr:r<s rtest_ClassDefinitionz WriterTests.test_ClassDefinitionxrJrcltjd}|jt|ty)z, Colorize a Unicode string. иNrr; assertTrue isinstancebytesr$ress r test_unicodezWriterTests.test_unicode~s'''1  3./rcltjd}|jt|ty)z/ Colorize a UTF-8 byte string. иNrOrSs r test_byteszWriterTests.test_bytess''' 4  3./rct|jtjdtjdy)zT The output of UTF-8 bytestrings and Unicode strings are identical. rWrNN) assertEqualrr;r<s rtest_identicalOutputz WriterTests.test_identicalOutputs-   % %k 2G4M4Mh4W rN) r'r(r)r=r?rBrDrGrIrLrUrXr[rrrr8r8Ss4' ) ) - - 0 0 00 rr8ceZdZejZdZdZdZdZ dZ dZ dZ dZ d Zej d Zej d Zd Zd Zej dZy)ManholeLoopbackMixincjjd}jdfd}|j|S)z- Evaluate simple expression. dones 1 + 1 donec,jgdy)N) >>> 1 + 12>>> done _assertBufferignr$s rfinishedz.finisheds   @ ArrecvlineClientexpect _testwrite addCallbackr$donerhs` rtest_SimpleExpressionz*ManholeLoopbackMixin.test_SimpleExpressionsA""))'2 *+ B))rcjjd}jdfd}|j|S)z> Evaluate line continuation in triple quotes. r_s ''' ''' donec,jgdy)N)s>>> '''s... '''s'\n'rcrdrfs rrhzGManholeLoopbackMixin.test_TripleQuoteLineContinuation..finisheds   N Orrirns` r test_TripleQuoteLineContinuationz5ManholeLoopbackMixin.test_TripleQuoteLineContinuationsA""))'2 -. P))rcjjd}jdfd}|j|S)z/ Evaluate function definition. r_s'def foo(bar): print(bar) foo(42) donec,jgdy)N)s>>> def foo(bar):s... print(bar)... s >>> foo(42)s42rcrdrfs rrhz>ManholeLoopbackMixin.test_FunctionDefinition..finisheds    rrirns` rrIz,ManholeLoopbackMixin.test_FunctionDefinitions@""))'2 ST ))rcjjd}jdfd}|j|S)z, Evaluate class definition. r_sEclass Foo: def bar(self): print('Hello, world!') Foo().bar() donec,jgdy)N)s>>> class Foo:s... def bar(self):s"... print('Hello, world!')rvs>>> Foo().bar()s Hello, world!rcrdrfs rrhz;ManholeLoopbackMixin.test_ClassDefinition..finisheds    rrirns` rrLz)ManholeLoopbackMixin.test_ClassDefinitionsD""))'2    ))rcjjd}jdfd}|j||S)z0 Evaluate raising an exception. r_s#raise Exception('foo bar baz') donec^jdddtjdzddgy)Ns">>> raise Exception('foo bar baz')s"Traceback (most recent call last):s File "", line 1, in zutf-8sException: foo bar bazrc)redefaultFunctionNameencoderfs rrhz5ManholeLoopbackMixin.test_Exception..finisheds8   996)009:-  rrirns` rtest_Exceptionz#ManholeLoopbackMixin.test_ExceptionsB""))'2 CD  " rcR|jtdd|jS)z Raised exceptions are handled the same way even if L{sys.excepthook} has been modified from its original value. excepthookcy)Nr)argss rzIManholeLoopbackMixin.test_ExceptionWithCustomExcepthook..sr)patchsysr}r<s r"test_ExceptionWithCustomExcepthookz7ManholeLoopbackMixin.test_ExceptionWithCustomExcepthooks$ 3 &89""$$rcjjd}jdtjzdzfd}|j |S)z4 Evaluate interrupting with CTRL-C. r_cancelled linec,jgdy)N)>>> cancelled lineKeyboardInterruptrcrdrfs rrhz4ManholeLoopbackMixin.test_ControlC..finished s   J r)rjrkrlrCTRL_Crmrns` r test_ControlCz"ManholeLoopbackMixin.test_ControlCsN""))'2 )GNN:WDE  ))rcjjd}jdfd}|j|fd}|j||S)z Sending ^C to Manhole while in a state where more input is required to complete a statement should discard the entire ongoing statement and reset the input prompt to the non-continuation prompt. sthingss( thingscjddgjjd}jtj |S)N>>> ( ... things>>> )rerjrkrlrr)ignored interruptedr$s rgotContinuationzNManholeLoopbackMixin.test_interruptDuringContinuation..gotContinuationsB   -8 9--44W=K OOGNN + rc,jgdy)N)rrrrrd)rr$s rgotInterruptionzNManholeLoopbackMixin.test_interruptDuringContinuation..gotInterruption&s   W Xrri)r$ continuingrrs` r test_interruptDuringContinuationz5ManholeLoopbackMixin.test_interruptDuringContinuationsX ((// :   %  / Y /rcjdjjd}fd}fd}|j|j|S)z2 Evaluate cancelling with CTRL-\. rcjdgjtjjj }j |tjS)Nr) rerlrCTRL_BACKSLASHrjonDisconnection assertFailurer ConnectionDone)rgdr$s rgotPartialLinezBManholeLoopbackMixin.test_ControlBackslash..gotPartialLine3sQ    56 7 OOG22 3##33A%%a)=)=> >rc*jdgy)Nrrdrfs rgotClearedLinezBManholeLoopbackMixin.test_ControlBackslash..gotClearedLine:s   u %rrlrjrkrm)r$ partialLinerrs` rtest_ControlBackslashz*ManholeLoopbackMixin.test_ControlBackslash,sQ )*))001BC  ? &&&~6BB>RRrc#8K|jd|jjd|jdg|jtj dz|jjd|jdg|jd|jjd|jtj |jj }|j|tjyw) z~ A CTRL+D in the middle of a line doesn't close a connection, but at the beginning of a line it does. s1 + 1\+ 1ra + 1 >>> 1 + 1 + 1 s3 >>> N) rlrjrkrerCTRL_Drrr r)r$rs r test_controlDz"ManholeLoopbackMixin.test_controlD?s !!!((22 L>* 01!!((22 ,-. !!((44 '    / /  E$8$899sDDc#0K|jd|jjd|jddg|jtj dz|jjd|jdgyw) a CTRL+L is generally used as a redraw-screen command in terminal applications. Manhole doesn't currently respect this usage of it, but it should at least do something reasonable in response to this event (rather than, say, eating your face). s 1 + 1rrrars 1 \+ 1 \+ 1rN)rlrjrkrerCTRL_Lr<s r test_ControlLz"ManholeLoopbackMixin.test_ControlLTs|  #!!((22 G\23 01!!((99 ,-.sBBcjdjjd}fd}|j|S)zl CTRL-A can be used as HOME - returning cursor to beginning of current line buffer. srint "hello"p print "hello"c*jdgyNs>>> print "hello"rdignorer$s rcbz.ManholeLoopbackMixin.test_controlA..cbn    45 6rrr$rrs` r test_controlAz"ManholeLoopbackMixin.test_controlAfs? 89    & &'7 8 7}}R  rcjdjjd}fd}|j|S)zc CTRL-E can be used as END - setting cursor to end of current line buffer. srint "hellop"rc*jdgyrrdrs rrz.ManholeLoopbackMixin.test_controlE..cb{rrrrs` r test_controlEz"ManholeLoopbackMixin.test_controlEss? HI    & &'7 8 7}}R  rc#$K|jd|jjd|jd|jjd|jjd|jgdyw)z When a deferred is returned to the manhole REPL, it is displayed with a sequence number, and when the deferred fires, the result is printed. sCfrom twisted.internet import defer, reactor d = defer.Deferred() d s.c = reactor.callLater(0.1, d.callback, 'Hi!') rs#Deferred #0 called back: 'Hi!' >>> )s/>>> from twisted.internet import defer, reactors>>> d = defer.Deferred()s>>> drs1>>> c = reactor.callLater(0.1, d.callback, 'Hi!')sDeferred #0 called back: 'Hi!'rN)rlrjrkrer<s r test_deferredz"ManholeLoopbackMixin.test_deferreds   !!(()9:: JK!!((11!!(()PQQ   sBBN)r'r(r)rColoredManholeserverProtocolrprsrIrLr}rrrrr inlineCallbacksrrrrrrrrr]r]s++N * **,*8.% *0S& ::( //" ! !   rr]ceZdZdZy)ManholeLoopbackTelnetTestsz, Test manhole loopback over Telnet. N)r'r(r)r*rrrrrs  rrceZdZdZedZyy)ManholeLoopbackSSHTestsz) Test manhole loopback over SSH. Nz!cryptography requirements missing)r'r(r)r*rskiprrrrrs {2rrc2eZdZdZedZyej Zy)ManholeLoopbackStdioTestsz1 Test manhole loopback over standard IO. NTerminal requirements missing)r'r(r)r*r rConsoleManholerrrrrrs }.--rrceZdZdZedZdZy)ManholeMainTestsz= Test the I{main} method from the I{manhole} module. Nrc|jttjdg}|j d|j dy)z Will raise an exception when called with an argument which is a dotted patch which can not be imported.. z no-such-class)argvzEmpty module namerN) assertRaises ValueErrorr mainrZr)r$ exceptions rtest_mainClassNotFoundz'ManholeMainTests.test_mainClassNotFoundsE %%  JJ!"& ,innQ.?@r)r'r(r)r*r rrrrrrrs }. Arr)#r*rrtypingrrbool__annotations__ twisted.conchrtwisted.conch.insultsr twisted.conch.test.test_recvlinerrr r twisted.internetr r twisted.internet.testingr twisted.trialrrr{TestCaserr,r8r]rrrrrrrrs Xd^!)*4" 034 -h// --8,,-(? (##? DG G T x/@/@BV 3i):):->@T.Ax((Ar