ϪfVHdZddlmZddlmZddlmZddlmZddl m Z m Z m Z m Z ddlmZmZmZmZmZmZddlmZmZdd lmZmZdd lmZdd lmZGd d eZGddeZ GddeZ!GddeZ"GddeZ#y)z) Tests for L{twisted.internet.protocol}. )BytesIO) implementer) verifyObject)CancelledError) IConsumerILoggingContext IProtocolIProtocolFactory) ClientCreatorConsumerToProtocolAdapterFactory FileWrapperProtocolProtocolToConsumerAdapter)MemoryReactorClockStringTransport)LogLevelglobalLogPublisher)Failure)TestCasecpeZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZdZdZy)ClientCreatorTestsz? Tests for L{twisted.internet.protocol.ClientCreator}. cGddt}t}t||}|||}|jd}|j ||y)a Helper for implementing a test to verify that one of the I{connect} methods of L{ClientCreator} passes the right arguments to the right reactor method. @param check: A function which will be invoked with a reactor and a L{ClientCreator} instance and which should call one of the L{ClientCreator}'s I{connect} methods and assert that all of its arguments except for the factory are passed on as expected to the reactor. The factory should be returned. c eZdZy):ClientCreatorTests._basicConnectTest..SomeProtocolN__name__ __module__ __qualname__E/usr/lib/python3/dist-packages/twisted/internet/test/test_protocol.py SomeProtocolr5 r!r#N)rrr buildProtocolassertIsInstance)selfcheckr#reactorccfactoryprotocols r"_basicConnectTestz$ClientCreatorTests._basicConnectTest(sP 8 %& 7L 1$((. h 5r!c2fd}j|y)z L{ClientCreator.connectTCP} calls C{reactor.connectTCP} with the host and port information passed to it, and with a factory which will construct the protocol passed to L{ClientCreator.__init__}. c|jdddd|jj\}}}}}j|dj|dj|dj|d|S)N example.com)z1.2.3.4i&) connectTCP tcpClientspop assertEqual)r)r*hostportr+timeout bindAddressr's r"r(z1ClientCreatorTests.test_connectTCP..checkEs{ MM-t5F G8?8J8J8N8N8P 5D$+   T= 1   T4 (   Wd +   [*; <Nr!Nr-r'r(s` r"test_connectTCPz"ClientCreatorTests.test_connectTCP>s  u%r!c2fd}j|y)z L{ClientCreator.connectUNIX} calls C{reactor.connectUNIX} with the filename passed to it, and with a factory which will construct the protocol passed to L{ClientCreator.__init__}. c|jddd|jj\}}}}j|dj|dj ||S)N/foo/bar{T) connectUNIX unixClientsr5r6 assertTrue)r)r*addressr+r9checkPIDr's r"r(z2ClientCreatorTests.test_connectUNIX..checkWsa NN:sD 1292E2E2I2I2K /GWgx   Wj 1   Wc * OOH %Nr!Nr;r<s` r"test_connectUNIXz#ClientCreatorTests.test_connectUNIXPs  u%r!c2fd}j|y)z L{ClientCreator.connectSSL} calls C{reactor.connectSSL} with the host, port, and context factory passed to it, and with a factory which will construct the protocol passed to L{ClientCreator.__init__}. c<t}|jdd|dd|jj\}}}}}} j |d j |d j || j |d j |d|S)Nr0r1r2)z4.3.2.1i.)object connectSSL sslClientsr5r6assertIs) r)r*expectedContextFactoryr7r8r+contextFactoryr9r:r's r"r(z1ClientCreatorTests.test_connectSSL..checkhs%+X " MMt%;TCT ""&&(    T= 1   T4 ( MM.*@ A   Wd +   [*; <Nr!Nr;r<s` r"test_connectSSLz"ClientCreatorTests.test_connectSSLas ( u%r!c2t}t|t}||}|jj }|j |j |j|j|j |j|tS)a Helper for implementing a test to verify that cancellation of the L{Deferred} returned by one of L{ClientCreator}'s I{connect} methods is implemented to cancel the underlying connector. @param connect: A function which will be invoked with a L{ClientCreator} instance as an argument and which should call one its I{connect} methods and return the result. @return: A L{Deferred} which fires when the test is complete or fails if there is a problem. ) rr r connectorsr5 assertFalse _disconnectedcancelrD assertFailurerr'connectr)r*d connectors r"_cancelConnectTestz%ClientCreatorTests._cancelConnectTest~sw%& 7H - BK&&**,  001    //0!!!^44r!c*d}|j|S) The L{Deferred} returned by L{ClientCreator.connectTCP} can be cancelled to abort the connection attempt before it completes. c&|jddSNr0r1)r3r*s r"rXz9ClientCreatorTests.test_cancelConnectTCP..connects==5 5r!r[r'rXs r"test_cancelConnectTCPz(ClientCreatorTests.test_cancelConnectTCPs  6&&w//r!c*d}|j|S)r]c$|jdSNr@)rBr`s r"rXz:ClientCreatorTests.test_cancelConnectUNIX..connects>>*- -r!rarbs r"test_cancelConnectUNIXz)ClientCreatorTests.test_cancelConnectUNIXs  .&&w//r!c*d}|j|S)r]c8|jddtSr_)rKrJr`s r"rXz9ClientCreatorTests.test_cancelConnectSSL..connects==fh? ?r!rarbs r"test_cancelConnectSSLz(ClientCreatorTests.test_cancelConnectSSLs  @&&w//r!ct}t|t}|||}|jj }|j t |jd|j|j |jg|j|j|j|tS)z Like L{_cancelConnectTest}, but for the case where the L{Deferred} is cancelled after the connection is set up but before it is fired with the resulting protocol instance. ) rr rrRr5r6lengetDelayedCallsrUrDrTrVrrWs r"_cancelConnectTimeoutTestz,ClientCreatorTests._cancelConnectTimeoutTests %& 7H - GR &&**,  W4467;   002B7  //0!!!^44r!c*d}|j|S)a L{ClientCreator.connectTCP} inserts a very short delayed call between the time the connection is established and the time the L{Deferred} returned from one of its connect methods actually fires. If the L{Deferred} is cancelled in this interval, the established connection is closed, the timeout is cancelled, and the L{Deferred} fails with L{CancelledError}. c|jdd}|jj\}}}}}|jd}t } |j | |Sr_)r3r4r5r%rmakeConnection) r)r*rYr7r8r+r9r:r, transports r"rXz@ClientCreatorTests.test_cancelConnectTCPTimeout..connects\ mT2A8?8J8J8N8N8P 5D$+,,T2H')I  # #I .Hr!rorbs r"test_cancelConnectTCPTimeoutz/ClientCreatorTests.test_cancelConnectTCPTimeout --g66r!c*d}|j|S)a L{ClientCreator.connectUNIX} inserts a very short delayed call between the time the connection is established and the time the L{Deferred} returned from one of its connect methods actually fires. If the L{Deferred} is cancelled in this interval, the established connection is closed, the timeout is cancelled, and the L{Deferred} fails with L{CancelledError}. c|jd}|jj\}}}}|jd}t }|j ||Srf)rBrCr5r%rrr) r)r*rYrEr+r9r:r,rss r"rXzAClientCreatorTests.test_cancelConnectUNIXTimeout..connectsXz*A5<5H5H5L5L5N 2GWg{,,T2H')I  # #I .Hr!rtrbs r"test_cancelConnectUNIXTimeoutz0ClientCreatorTests.test_cancelConnectUNIXTimeoutrvr!c*d}|j|S)a L{ClientCreator.connectSSL} inserts a very short delayed call between the time the connection is established and the time the L{Deferred} returned from one of its connect methods actually fires. If the L{Deferred} is cancelled in this interval, the established connection is closed, the timeout is cancelled, and the L{Deferred} fails with L{CancelledError}. c|jddt}|jj\}}}}}}|j d} t } | j | |Sr_)rKrJrLr5r%rrr) r)r*rYr7r8r+rOr9 bindADdressr,rss r"rXz@ClientCreatorTests.test_cancelConnectSSLTimeout..connectsl mT68 W4467;   002B7!!!^44r!c*d}|j|S)zz Similar to L{test_cancelConnectTCPTimeout}, but for the case where the connection attempt fails. cn|jdd}|jj\}}}}}||fSr_)r3r4r5)r)r*rYr7r8r+r9r:s r"rXzFClientCreatorTests.test_cancelConnectTCPFailedTimeout..connect2s; mT2A8?8J8J8N8N8P 5D$+g: r!rrbs r""test_cancelConnectTCPFailedTimeoutz5ClientCreatorTests.test_cancelConnectTCPFailedTimeout,   33G<.connect?s7z*A5<5H5H5L5L5N 2GWg{g: r!rrbs r"#test_cancelConnectUNIXFailedTimeoutz6ClientCreatorTests.test_cancelConnectUNIXFailedTimeout9rr!c*d}|j|S)zz Similar to L{test_cancelConnectSSLTimeout}, but for the case where the connection attempt fails. c|jddt}|jj\}}}}}}||fSr_)rKrJrLr5) r)r*rYr7r8r+rOr9r|s r"rXzFClientCreatorTests.test_cancelConnectSSLFailedTimeout..connectLsK mT68.SomeThingNrr r!r" SomeThingrmr$r!rN)rr6 logPrefix)r'rs r"test_logPrefixzProtocolTests.test_logPrefixhs+    ik&;&;&=>r!cgGfddt}t}|}|j||j|gy)zz L{Protocol.makeConnection} sets the given transport on itself, and then calls C{connectionMade}. ceZdZfdZy)7ProtocolTests.test_makeConnection..SomeProtocolc<j|jyN)appendrs)r'results r"connectionMadezFProtocolTests.test_makeConnection..SomeProtocol.connectionMadezs dnn-r!N)rrrrrsr"r#rys .r!r#N)rrJrrr6)r'r#rsr,rs @r"test_makeConnectionz!ProtocolTests.test_makeConnectionrsF  .8 .H > * )-r!N)rrrrrrrr r!r"rr[s>?.r!rc4eZdZdZdZdZdZdZdZdZ y) FactoryTestsz( Tests for L{protocol.Factory}. ct}|jtt||jtt|y)zg L{Factory} instances provide both L{IProtocolFactory} and L{ILoggingContext}. N)r rDrr r)r'r+s r"rzFactoryTests.test_interfacess2 )  %5w?@  _g>?r!clGddt}|jd|jy)zM L{Factory.logPrefix} returns the name of the factory class. c eZdZy)6FactoryTests.test_logPrefix..SomeKindOfFactoryNrr r!r"SomeKindOfFactoryrr$r!rN)r r6r)r'rs r"rzFactoryTests.test_logPrefixs.    ,.?.A.K.K.MNr!cGddt}t}||_|jd}|j |||j |j |y)z L{Factory.buildProtocol} by default constructs a protocol by calling its C{protocol} attribute, and attaches the factory to the result. c eZdZy).SomeProtocolNrr r!r"r#rr$r!r#N)rr r,r%r&rMr+)r'r#fr,s r"test_defaultBuildProtocolz&FactoryTests.test_defaultBuildProtocolsP  8  I! ??4( h 5 h&&*r!cGddt}|jtddd}|j|jt|j|j d|j|j ddiy ) z L{Factory.forProtocol} constructs a Factory, passing along any additional arguments, and sets its C{protocol} attribute to the given Protocol subclass. ceZdZdZy)7FactoryTests.test_forProtocol..ArgTakingFactoryc"||c|_|_yr)argskwargs)r'rrs r"__init__z@FactoryTests.test_forProtocol..ArgTakingFactory.__init__s)-v& 4;r!N)rrrrr r!r"ArgTakingFactoryrs 6r!rrl )foo)rlrrN)r forProtocolrr6r,rr)r'rr+s r"test_forProtocolzFactoryTests.test_forProtocolsk 6w 6#..xA2.F ))84 v. %5r!cPgtjj|jfdt }|j |j dd||jddtj|jdddy)z L{Factory.doStart} logs that it is starting a factory, followed by the L{repr} of the L{Factory} instance that is being started. cBtjjSrrremoveObserverreventssr"z;FactoryTests.test_doStartLoggingStatement.. 2 A A&-- Pr!rr+ log_level log_formatzStarting factory {factory!r}N) r addObserverr addCleanupr doStartrMr6rinfo)r'rrs @r"test_doStartLoggingStatementz)FactoryTests.test_doStartLoggingStatements &&v}}5 PQ I  fQi *A. ;/? <02PQr!chgtjj|jfdGddt}|}|j |j dd||jddtj|jdddy ) z L{Factory.doStop} logs that it is stopping a factory, followed by the L{repr} of the L{Factory} instance that is being stopped. cBtjjSrrrsr"rz:FactoryTests.test_doStopLoggingStatement..rr!ceZdZdZy);FactoryTests.test_doStopLoggingStatement..MyFactoryrlN)rrrnumPortsr r!r" MyFactoryrsHr!rrr+rrzStopping factory {factory!r}N) rrrrr doStoprMr6rr)r'rrrs @r"test_doStopLoggingStatementz(FactoryTests.test_doStopLoggingStatements &&v}}5 PQ   K   fQi *A. ;/? <02PQr!N) rrrrrrrrrrr r!r"rrs)@O +6 R Rr!rceZdZdZdZdZy) AdapterTestszR Tests for L{ProtocolToConsumerAdapter} and L{ConsumerToProtocolAdapter}. cg}t}|j|_t|}|j d|j |dg|j |ty)z} L{IProtocol} providers can be adapted to L{IConsumer} providers using L{ProtocolToConsumerAdapter}. helloN)rr dataReceivedrwriter6r&r)r'rpconsumers r"test_protocolToConsumerz$AdapterTests.test_protocolToConsumersS  JQ<x  (, h(ABr!cgttGfdd}|}t|}|jd|j dg|j |t y)z} L{IConsumer} providers can be adapted to L{IProtocol} providers using L{ProtocolToConsumerAdapter}. ceZdZfdZy)6AdapterTests.test_consumerToProtocol..Consumerc(j|yr)r)r'rYrs r"rz.Consumer.writes a r!N)rrrrrsr"Consumerrs !r!rrN)rrr rr6r&r )r'rcr,rs @r"test_consumerToProtocolz$AdapterTests.test_consumerToProtocolsi  Y  ! !  ! JQ<h' (, h(ABr!N)rrrrrrr r!r"rrs CCr!rceZdZdZdZdZy)FileWrapperTestsz2 L{twisted.internet.protocol.FileWrapper} c<tt}|jd|j|jj dtt}|jd|j |jj dy)z@ L{twisted.internet.protocol.FileWrapper.write} test1stuffN)rrrr6filegetvalueassertNotEqualr'wrappers r" test_writezFileWrapperTests.test_writespgi( h ..0(;gi(  g GLL113W=r!ctt}|jddg|j|jj dtt}|j t|jddgy)zH L{twisted.internet.protocol.FileWrapper.writeSequence} rstest2s test1test2test3test4N)rr writeSequencer6rr assertRaises TypeErrorrs r"test_writeSequencez#FileWrapperTests.test_writeSequencesigi(x23 ..0-@gi( )W%:%:Wg Pr!rN)$riorzope.interfacerzope.interface.verifyrtwisted.internet.deferrtwisted.internet.interfacesrrr r twisted.internet.protocolr r r rrrtwisted.internet.testingrrtwisted.loggerrrtwisted.python.failurertwisted.trial.unittestrrrrrrr r!r"rs &.1 I7*+u=u=p %.H%.PXR8XRv"C8"CJPxPr!