'Qc@`sdZddlmZmZddlmZmZddlmZddl m Z m Z ddl m Z ddlmZddlmZdd lmZmZmZmZdd lmZmZmZmZdd lmZdd lmZdd lmZddl m!Z!ddl"m#Z#m$Z$m%Z%ddl&m'Z'ddl(m)Z)m*Z*m+Z+de#j,fdYZ-de!j.fdYZ/dd)dYZ0e eeedd*dYZ1de1fdYZ2defdYZ3e ede4fdYZ5e ed e4fd!YZ6e eeeed"e4fd#YZ7x!e e7D]Z8e e8e7qfWd$e7e'fd%YZ9e eeeed&e4fd'YZ:d(S(+sJ Assorted functionality which is commonly useful when writing unit tests. i(tdivisiontabsolute_import(tAF_INETtAF_INET6(tBytesIO(t implementert implementedBy(t verifyClass(tfailure(tunicode(t ITransportt IConsumert IPushProducert IConnector(t IReactorTCPt IReactorSSLt IReactorUNIXtIReactorSocket(tIListeningPort(t isIPv6Address(tUnsupportedAddressFamily(tbasic(tprotocolterrortaddress(tClock(t IPv4Addresst UNIXAddresst IPv6AddresstAccumulatingProtocolcB`sKeZdZdZZdZdZdZdZ dZ dZ dZ RS(sd L{AccumulatingProtocol} is an L{IProtocol} implementation which collects the data delivered to it and can fire a Deferred when it is connected or disconnected. @ivar made: A flag indicating whether C{connectionMade} has been called. @ivar data: Bytes giving all the data passed to C{dataReceived}. @ivar closed: A flag indicated whether C{connectionLost} has been called. @ivar closedReason: The value of the I{reason} parameter passed to C{connectionLost}. @ivar closedDeferred: If set to a L{Deferred}, this will be fired when C{connectionLost} is called. itcC`sVd|_|jdk rR|jjdk rR|jj}d|j_|j|ndS(Ni(tmadetfactorytNonetprotocolConnectionMadetcallback(tselftd((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pytconnectionMade8s    cC`s|j|7_dS(N(tdata(R$R'((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt dataReceived@scC`sHd|_||_|jdk rD|jd}|_|jdndS(Ni(tclosedt closedReasontclosedDeferredR!R#(R$treasonR%((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pytconnectionLostCs   N( t__name__t __module__t__doc__RR)R!R*R+R'R R&R(R-(((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyR!s    tLineSendingProtocolcB`s5eZeZedZdZdZdZRS(cC`s ||_g|_||_dS(N(tlinestresponsetstart(R$R2R4((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt__init__Ns  cC`s1|jr-x!|jD]}|j|qWndS(N(R4R2tsendLine(R$tline((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyR&Ss cC`sJ|js6x|jD]}|j|qWg|_n|jj|dS(N(R4R2R6R3tappend(R$R7((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt lineReceivedXs   cC`s t|_dS(N(tTruetlostConn(R$R,((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyR-_s( R.R/tFalseR;R:R5R&R9R-(((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyR1Ks    tFakeDatagramTransportcB`s&eZeZdZedZRS(cC`s g|_dS(N(twritten(R$((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyR5fscC`s|jj||fdS(N(R>R8(R$tpackettaddr((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pytwriteis(R.R/tobjecttnoAddrR5RA(((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyR=cs  tStringTransportcB`seZdZeZdZdZdZdZ dZ dddZ dZ dZ dZdZdZdZd Zd Zd Zd Zd ZdZdZRS(s A transport implementation which buffers data in memory and keeps track of its other state without providing any behavior. L{StringTransport} has a number of attributes which are not part of any of the interfaces it claims to implement. These attributes are provided for testing purposes. Implementation code should not use any of these attributes; they are not provided by other transports. @ivar disconnecting: A C{bool} which is C{False} until L{loseConnection} is called, then C{True}. @ivar producer: If a producer is currently registered, C{producer} is a reference to it. Otherwise, C{None}. @ivar streaming: If a producer is currently registered, C{streaming} refers to the value of the second parameter passed to C{registerProducer}. @ivar hostAddr: C{None} or an object which will be returned as the host address of this transport. If C{None}, a nasty tuple will be returned instead. @ivar peerAddr: C{None} or an object which will be returned as the peer address of this transport. If C{None}, a nasty tuple will be returned instead. @ivar producerState: The state of this L{StringTransport} in its capacity as an L{IPushProducer}. One of C{'producing'}, C{'paused'}, or C{'stopped'}. @ivar io: A L{BytesIO} which holds the data which has been written to this transport since the last call to L{clear}. Use L{value} instead of accessing this directly. t producingcC`sG|j|dk r"||_n|dk r:||_nt|_dS(N(tclearR!thostAddrtpeerAddrR:t connected(R$t hostAddresst peerAddress((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyR5s      cC`st|_dS(s Discard all data written to this transport so far. This is not a transport method. It is intended for tests. Do not use it in implementation code. N(Rtio(R$((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyRFscC`s |jjS(sN Retrieve all data which has been buffered by this transport. This is not a transport method. It is intended for tests. Do not use it in implementation code. @return: A C{bytes} giving all data written to this transport since the last call to L{clear}. @rtype: C{bytes} (RLtgetvalue(R$((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pytvalues cC`s2t|trtdn|jj|dS(NsData must not be unicode(t isinstanceR t TypeErrorRLRA(R$R'((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyRAscC`s|jjdj|dS(NR(RLRAtjoin(R$R'((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt writeSequencescC`s t|_dS(s~ Close the connection. Does nothing besides toggle the C{disconnecting} instance variable to C{True}. N(R:t disconnecting(R$((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pytloseConnectionscC`s)|jdkr"tjdddS|jS(NtTCPs 192.168.1.1i1(RHR!RR(R$((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pytgetPeerscC`s)|jdkr"tjdddS|jS(NRUs10.0.0.1i90(RGR!RR(R$((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pytgetHostscC`s4|jdk rtdn||_||_dS(NsCannot register two producers(tproducerR!t RuntimeErrort streaming(R$RXRZ((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pytregisterProducers cC`s4|jdkrtdnd|_d|_dS(Ns5Cannot unregister a producer unless one is registered(RXR!RYRZ(R$((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pytunregisterProducers   cC`s:|jrtdn|jdkr6tdndS(Ns,Cannot resume producing after loseConnectiontstoppeds Cannot resume a stopped producer(RSRYt producerState(R$((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt _checkStates   cC`s|jd|_dS(Ntpaused(R_R^(R$((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pytpauseProducings cC`s d|_dS(NR](R^(R$((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt stopProducingscC`s|jd|_dS(NRE(R_R^(R$((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pytresumeProducings N(R.R/R0R<RSR!RXRZRGRHR^R5RFRNRARRRTRVRWR[R\R_RaRbRc(((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyRDns*#          t StringTransportWithDisconnectioncB`seZdZdZRS(s9 A L{StringTransport} which can be disconnected. cC`s;|jr7t|_|jjtjtjdndS(NsBye.(RIR<RR-RtFailureRtConnectionDone(R$((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyRT s   (R.R/R0RT(((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyRdstStringIOWithoutClosingcB`seZdZdZRS(s) A BytesIO that can't be closed. cC`sdS(s Do nothing. N((R$((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pytcloses(R.R/R0Rh(((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyRgst _FakePortcB`s2eZdZdZdZdZdZRS(s A fake L{IListeningPort} to be used in tests. @ivar _hostAddress: The L{IAddress} this L{IListeningPort} is pretending to be listening on. cC`s ||_dS(sz @param hostAddress: An L{IAddress} this L{IListeningPort} should pretend to be listening on. N(t _hostAddress(R$RJ((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyR5%scC`sdS(sQ Fake L{IListeningPort.startListening} that doesn't do anything. N((R$((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pytstartListening-scC`sdS(sP Fake L{IListeningPort.stopListening} that doesn't do anything. N((R$((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt stopListening3scC`s|jS(sN Fake L{IListeningPort.getHost} that returns our L{IAddress}. (Rj(R$((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyRW9s(R.R/R0R5RkRlRW(((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyRis    t_FakeConnectorcB`sGeZdZeZeZdZdZdZdZ dZ RS(s& A fake L{IConnector} that allows us to inspect if it has been told to stop connecting. @ivar stoppedConnecting: has this connector's L{FakeConnector.stopConnecting} method been invoked yet? @ivar _address: An L{IAddress} provider that represents our destination. cC`s ||_dS(ss @param address: An L{IAddress} provider that represents this connector's destination. N(t_address(R$R((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyR5OscC`s t|_dS(sv Implement L{IConnector.stopConnecting} and set L{FakeConnector.stoppedConnecting} to C{True} N(R:tstoppedConnecting(R$((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pytstopConnectingWscC`s t|_dS(s@ Implement L{IConnector.disconnect} as a no-op. N(R:t _disconnected(R$((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt disconnect_scC`sdS(s= Implement L{IConnector.connect} as a no-op. N((R$((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pytconnectfscC`s|jS(sp Implement L{IConnector.getDestination} to return the C{address} passed to C{__init__}. (Rn(R$((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pytgetDestinationls( R.R/R0R<RqRoR5RpRrRsRt(((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyRmAs     t MemoryReactorcB`seZdZdZdZdZddZdddZd dd Z ddd Z d dd Z dd ddZ d ddZ RS(s A fake reactor to be used in tests. This reactor doesn't actually do much that's useful yet. It accepts TCP connection setup attempts, but they will never succeed. @ivar tcpClients: a list that keeps track of connection attempts (ie, calls to C{connectTCP}). @type tcpClients: C{list} @ivar tcpServers: a list that keeps track of server listen attempts (ie, calls to C{listenTCP}). @type tcpServers: C{list} @ivar sslClients: a list that keeps track of connection attempts (ie, calls to C{connectSSL}). @type sslClients: C{list} @ivar sslServers: a list that keeps track of server listen attempts (ie, calls to C{listenSSL}). @type sslServers: C{list} @ivar unixClients: a list that keeps track of connection attempts (ie, calls to C{connectUNIX}). @type unixClients: C{list} @ivar unixServers: a list that keeps track of server listen attempts (ie, calls to C{listenUNIX}). @type unixServers: C{list} @ivar adoptedPorts: a list that keeps track of server listen attempts (ie, calls to C{adoptStreamPort}). @ivar adoptedStreamConnections: a list that keeps track of stream-oriented connections added using C{adoptStreamConnection}. cC`sUg|_g|_g|_g|_g|_g|_g|_g|_g|_dS(s0 Initialize the tracking lists. N( t tcpClientst tcpServerst sslClientst sslServerst unixClientst unixServerst adoptedPortstadoptedStreamConnectionst connectors(R$((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyR5s        cC`sn|tkr!tddd}n*|tkrBtddd}n t|jj|||ft|S(sv Fake L{IReactorSocket.adoptStreamPort}, that logs the call and returns an L{IListeningPort}. RUs0.0.0.0is::(RRRRRR|R8Ri(R$tfilenot addressFamilyR R@((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pytadoptStreamPorts   cC`s|jj|||fdS(s Record the given stream connection in C{adoptedStreamConnections}. @see: L{twisted.internet.interfaces.IReactorSocket.adoptStreamConnection} N(R}R8(R$tfileDescriptorRR ((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pytadoptStreamConnections i cC`sq|tkr!tddd}n*|tkrBtddd}n t|jj||||ft|S(s Fake L{IReactorSocket.adoptDatagramPort}, that logs the call and returns a fake L{IListeningPort}. @see: L{twisted.internet.interfaces.IReactorSocket.adoptDatagramPort} tUDPs0.0.0.0is::(RRRRRR|R8Ri(R$RRRt maxPacketSizeR@((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pytadoptDatagramPorts    i2RcC`sY|jj||||ft|r=td||}ntdd|}t|S(si Fake L{reactor.listenTCP}, that logs the call and returns an L{IListeningPort}. RUs0.0.0.0(RwR8RRRRi(R$tportR tbacklogt interfaceR((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt listenTCPs  icC`s|jj|||||ft|rFttd||}nttd||}|j||jj||S(sf Fake L{reactor.connectTCP}, that logs the call and returns an L{IConnector}. RU(RvR8RRmRRtstartedConnectingR~(R$thostRR ttimeoutt bindAddresstconn((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt connectTCPs  cC`s5|jj|||||fttdd|S(si Fake L{reactor.listenSSL}, that logs the call and returns an L{IListeningPort}. RUs0.0.0.0(RyR8RiR(R$RR tcontextFactoryRR((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt listenSSLs cC`s[|jj||||||fttd||}|j||jj||S(sf Fake L{reactor.connectSSL}, that logs the call and returns an L{IConnector}. RU(RxR8RmRRR~(R$RRR RRRR((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt connectSSLs   iicC`s/|jj|||||ftt|S(sj Fake L{reactor.listenUNIX}, that logs the call and returns an L{IListeningPort}. (R{R8RiR(R$RR RtmodetwantPID((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt listenUNIX scC`sO|jj||||ftt|}|j||jj||S(sg Fake L{reactor.connectUNIX}, that logs the call and returns an L{IConnector}. (RzR8RmRRR~(R$RR RtcheckPIDR((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyt connectUNIXs  N(R.R/R0R5RRRRR!RRRRR(((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyRuus$      tMemoryReactorClockcB`seZdZRS(cC`stj|tj|dS(N(RuR5R(R$((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyR5's (R.R/R5(((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyR&stRaisingMemoryReactorcB`seZdZdddZdZdddZdddZdddZddd Z dd d d Z dd d Z RS(s A fake reactor to be used in tests. It accepts TCP connection setup attempts, but they will fail. @ivar _listenException: An instance of an L{Exception} @ivar _connectException: An instance of an L{Exception} cC`s||_||_dS(s @param listenException: An instance of an L{Exception} to raise when any C{listen} method is called. @param connectException: An instance of an L{Exception} to raise when any C{connect} method is called. N(t_listenExceptiont_connectException(R$tlistenExceptiontconnectException((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyR57s cC`s |jdS(sg Fake L{IReactorSocket.adoptStreamPort}, that raises L{self._listenException}. N(R(R$RRR ((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyRCsi2RcC`s |jdS(sR Fake L{reactor.listenTCP}, that raises L{self._listenException}. N(R(R$RR RR((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyRKsicC`s |jdS(sT Fake L{reactor.connectTCP}, that raises L{self._connectException}. N(R(R$RRR RR((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyRRscC`s |jdS(sR Fake L{reactor.listenSSL}, that raises L{self._listenException}. N(R(R$RR RRR((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyRYscC`s |jdS(sT Fake L{reactor.connectSSL}, that raises L{self._connectException}. N(R(R$RRR RRR((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyRasiicC`s |jdS(sS Fake L{reactor.listenUNIX}, that raises L{self._listenException}. N(R(R$RR RRR((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyRiscC`s |jdS(sU Fake L{reactor.connectUNIX}, that raises L{self._connectException}. N(R(R$RR RR((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyRqsN( R.R/R0R!R5RRRRRRR(((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pyR-s N(((;R0t __future__RRtsocketRRRLRtzope.interfaceRRtzope.interface.verifyRttwisted.pythonRttwisted.python.compatR ttwisted.internet.interfacesR R R R RRRRRttwisted.internet.abstractRttwisted.internet.errorRttwisted.protocolsRttwisted.internetRRRttwisted.internet.taskRttwisted.internet.addressRRRtProtocolRt LineReceiverR1R=RDRdRgRBRiRmRutifaceRR(((s>/usr/lib/python2.7/dist-packages/twisted/test/proto_helpers.pytsB""* %  $ 3