Your IP : 172.28.240.42


Current Path : /usr/lib/python2.7/dist-packages/twisted/protocols/test/
Upload File :
Current File : //usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyc


Nc@sldZddlmZy0ddlmZmZddlmZmZWnek
r_dZ	n_Xddl
mZddlm
Z
mZmZmZmZddlmZmZdd	lmZdd
lmZddlmZddlmZdd
lmZmZddlmZddl m!Z!m"Z"ddl#m$Z$m%Z%ddl&m'Z'm(Z(m)Z)ddl*m+Z+ddl,m-Z-m.Z.ddl/m0Z0ddl1m2Z2ddl3m4Z4ddl5m6Z6dfdYZ7de'fdYZ8e9e:dZ;de0fdYZ<de0fd YZ=d!e0fd"YZ>d#e?fd$YZ@d%e0fd&YZAd'S((s%
Tests for L{twisted.protocols.tls}.
i(tverifyObject(tTLSMemoryBIOProtocoltTLSMemoryBIOFactory(t_PullToPusht_ProducerMembranes9pyOpenSSL 0.10 or newer required for twisted.protocol.tls(tX509Type(tTLSv1_METHODtErrortContexttConnectionTypet
WantReadError(tClientContextFactorytPrivateCertificate(tDefaultOpenSSLContextFactory(tFilePath(tFailure(tlog(t
ISystemHandlet
ISSLTransport(t
IPushProducer(tConnectionDonetConnectionLost(tDeferredt
gatherResults(tProtocolt
ClientFactoryt
ServerFactory(tTaskStopped(t
loopbackAsynctcollapsingPumpPolicy(tTestCase(tConnectionLostNotifyingProtocol(tcertPath(tStringTransporttHandshakeCallbackContextFactorycBsDeZdZdZdZdZeeZdZdZRS(s
    L{HandshakeCallbackContextFactory} is a factory for SSL contexts which
    allows applications to get notification when the SSL handshake completes.

    @ivar _finished: A L{Deferred} which will be called back when the handshake
        is done.
    i cCst|_dS(N(Rt	_finished(tself((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyt__init__4scCs|}||jfS(s
        Create a new L{HandshakeCallbackContextFactory} and return a two-tuple
        of it and a L{Deferred} which will fire when a connection created with
        it completes a TLS handshake.
        (R#(tclstcontextFactory((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pytfactoryAndDeferred8s	cCs$||j@r |jjdndS(s
        This is the "info callback" on the context.  It will be called
        periodically by pyOpenSSL with information about the state of a
        connection.  When it indicates the handshake is complete, it will fire
        C{self._finished}.
        N(tSSL_CB_HANDSHAKE_DONER#tcallbacktNone(R$t
connectiontwheretret((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyt_infoCs
cCs tt}|j|j|S(sp
        Create and return an SSL context configured to use L{self._info} as the
        info callback.
        (RRtset_info_callbackR/(R$tcontext((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyt
getContextNs(	t__name__t
__module__t__doc__R)R%R(tclassmethodR/R2(((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyR"(s			tAccumulatingProtocolcBs2eZdZdZdZdZdZRS(s-
    A protocol which collects the bytes it receives and closes its connection
    after receiving a certain minimum of data.

    @ivar howMany: The number of bytes of data to wait for before closing the
        connection.

    @ivar receiving: A C{list} of C{str} of the bytes received so far.
    cCs
||_dS(N(thowMany(R$R8((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyR%cscCs
g|_dS(N(treceived(R$((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pytconnectionMadegscCsE|jj|ttt|j|jkrA|jjndS(N(R9tappendtsumtmaptlenR8t	transporttloseConnection(R$tbytes((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pytdataReceivedks!cCs#|jtstj|ndS(N(tcheckRRterr(R$treason((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pytconnectionLostqs(R3R4R5R%R:RBRF(((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyR7Ys
				cstdt}fd|_|r?ttt}n	t}t|||}|jd}|dkrt	}n|j
||fS(s\
    Create a protocol hooked up to a TLS transport hooked up to a
    StringTransport.
    IcsS(N(((tclientProtocol(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyt<lambda>sN(R7RtprotocolR
R RRt
buildProtocolR+R!tmakeConnection(tserverR?t
clientFactoryR'twrapperFactorytsslProtocol((RGsC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pytbuildTLSProtocolws		
tTLSMemoryBIOFactoryTestscBs)eZdZdZdZdZRS(s<
    Ensure TLSMemoryBIOFactory logging acts correctly.
    cCsg}|j}tj||jtj|t}d|_d|_tdt
|}|j|j|j|gdS(ss
        L{TLSMemoryBIOFactory.doStart} and L{TLSMemoryBIOFactory.doStop} do
        not log any messages.
        cSsdS(N(R+(((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRHscSsdS(N(R+(((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRHsN(R;RtaddObservert
addCleanuptremoveObserverRtdoStarttdoStopRR+tFalsetassertEqual(R$tlogstloggertwrappedFactorytfactory((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyt
test_quiets	
	

cCs/tdtt}|jd|jdS(s
        L{TLSMemoryBIOFactory.logPrefix} amends the wrapped factory's log prefix
        with a short string (C{"TLS"}) indicating the wrapping, rather than its
        full class name.
        sServerFactory (TLS)N(RR+RWRRXt	logPrefix(R$R\((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyttest_logPrefixscCsEdtfdY}tdt|}|jd|jdS(s
        If the wrapped factory does not provide L{ILoggingContext},
        L{TLSMemoryBIOFactory.logPrefix} uses the wrapped factory's class name.
        t	NoFactorycBseZRS((R3R4(((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyR`ssNoFactory (TLS)N(tobjectRR+RWRXR^(R$R`R\((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyttest_logPrefixFallbacks(R3R4R5R]R_Rb(((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRQs		
tTLSMemoryBIOTestscBseZdZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZ
dZd
ZdZdZdZdZdZdZdZRS(se
    Tests for the implementation of L{ISSLTransport} which runs over another
    L{ITransport}.
    cCs?tdd}|jtj||jtj|dS(sj
        L{TLSMemoryBIOProtocol} instances provide L{ISSLTransport} and
        L{ISystemHandle}.
        N(RR+t
assertTrueRt
providedByR(R$tproto((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyttest_interfacesscCsft}t}t|t|}t|t}t}|j||j|j	t
dS(s/
        L{TLSMemoryBIOProtocol.getHandle} returns the L{OpenSSL.SSL.Connection}
        instance it uses to actually implement TLS.

        This may seem odd.  In fact, it is.  The L{OpenSSL.SSL.Connection} is
        not actually the "system handle" here, nor even an object the reactor
        knows about directly.  However, L{twisted.internet.ssl.Certificate}'s
        C{peerFromTransport} and C{hostFromTransport} methods depend on being
        able to get an L{OpenSSL.SSL.Connection} object in order to work
        properly.  Implementing L{ISystemHandle.getHandle} like this is the
        easiest way for those APIs to be made to work.  If they are changed,
        then it may make sense to get rid of this implementation of
        L{ISystemHandle} and return the underlying socket instead.
        N(RRRtTrueRRR!RKtassertIsInstancet	getHandleR	(R$R\R'RNRfR?((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyttest_getHandles			
cstt}fd|_t}t|t|}|jd}t}|j	||j
jd|j
j||jj|dS(s
        When L{TLSMemoryBIOProtocol} is connected to a transport, it connects
        the protocol it wraps to a transport.
        csS(N(((RG(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRHsN(
RRRIRRRhRJR+R!RKtassertNotIdenticalR?tassertIdentical(R$RMR'RNROR?((RGsC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyttest_makeConnections				
c
Cst}t|_tj\}}t|t|}|jd}t	}t|_t
tt}t|t|}|jd}t
||}	||||	fS(s@
        Start handshake between TLS client and server.
        N(RRRIR"R(RRhRJR+RR
R RWR(
R$RMtclientContextFactorythandshakeDeferredRNtsslClientProtocolt
serverFactorytserverContextFactorytsslServerProtocoltconnectionDeferred((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pythandshakeProtocolss					cCs|j\}}}}|S(sr
        The TLS handshake is performed when L{TLSMemoryBIOProtocol} is
        connected to a transport.
        (Rv(R$t	tlsClientt	tlsServerRpt_((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyttest_handshakescstt}fd|_t}t|t|}|jd}tt}fd|_t	t
j}tj
|}|j|}t|t|}|jd}	t|	|}
d}j|j|t|
gS(s
        L{TLSMemoryBIOProtocol} reports errors in the handshake process to the
        application-level protocol object using its C{connectionLost} method
        and disconnects the underlying transport.
        cs
tS(N(R((tclientConnectionLost(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRH,scs
tS(N(R((tserverConnectionLost(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRH7scSs|jjtdS(N(tlostConnectionReasonttrapR(RI((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pytcbConnectionLostEsN(RRRIR"RRhRJR+RRR t
getContentRtloadPEMtoptionsRWRtaddCallbackR(R$RMRoRNRqRrtcertificateDatatcertificateRsRtRuR((R{R|sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyttest_handshakeFailure#s.						

c	st}t|_tj\}}t|t|}|jdt	}t|_t
tt}t|t|}|jd}t
|fd}|j||S(s
        L{TLSMemoryBIOProtocol.getPeerCertificate} returns the
        L{OpenSSL.crypto.X509Type} instance representing the peer's
        certificate.
        cs9j}j|tj|jdddS(Ntmd5s/9B:A4:AB:43:10:BE:82:AE:94:3E:6B:91:F2:F3:40:E8(tgetPeerCertificateRiRRXtdigest(tignoredtcert(R$Rq(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pytcbHandshookps
N(RRRIR"R(RRhRJR+RR
R RWRR(	R$RMRoRpRNRrRsRtR((R$RqsC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyttest_getPeerCertificateUs 				

csdtt}fd|_tj\}}t|t|}|jd}t	t
t}fd|_tt
t
}t|t|}|jd}t||fd}	|j|	fd}
|j|
|S(s
        Bytes written to L{TLSMemoryBIOProtocol} before the handshake is
        complete are received by the protocol on the other side of the
        connection once the handshake succeeds.
        s
some bytescsS(N(((RG(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRHscsS(N(((tserverProtocol(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRHscsjjS(N(R?twrite(R(RuRARG(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRscs jdjjdS(Nt(RXtjoinR9(R(R$RRA(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pytcbDisconnectedsN(RRRIR"R(RRhRJR+R7R>RR
R RWRR(R$RMRoRpRNRqRrRsRtRR((RuR$RGRRAsC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyttest_writeAfterHandshake{s*			

c
st}||_tj\}}t|t|}|jd}tt	t
}fd|_ttt}	t|	t
|}|jd}
t|
|}fd}|j||S(s{
        Run test where client sends data before handshake, given the sending
        protocol and expected bytes.
        csS(N(((R(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRHscs jdjjdS(NR(RXRR9(R(R$RRA(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pytcbConnectionDonesN(RRIR"R(RRhRJR+R7R>RR
R RWRR(
R$tsendingProtocolRARMRoRpRNRqRrRsRtRuR((R$RRAsC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pytwriteBeforeHandshakeTests"			
cs2ddtffdY}|j|S(s
        Bytes written to L{TLSMemoryBIOProtocol} before the handshake is
        complete are received by the protocol on the other side of the
        connection once the handshake succeeds.
        s
some bytestSimpleSendingProtocolcseZfdZRS(cs|jjdS(N(R?R(R$(RA(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyR:s(R3R4R:((RA(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRs(RR(R$R((RAsC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyttest_writeBeforeHandshakescs2ddtffdY}|j|S(s
        Bytes written to L{TLSMemoryBIOProtocol} with C{writeSequence} are
        received by the protocol on the other side of the connection.
        s
some bytesRcseZfdZRS(cs|jjtdS(N(R?t
writeSequencetlist(R$(RA(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyR:s(R3R4R:((RA(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRs(RR(R$R((RAsC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyttest_writeSequencescs2ddtffdY}|j|S(s
        Bytes written to L{TLSMemoryBIOProtocol} after C{loseConnection} is
        called are not transmitted (unless there is a registered producer,
        which will be tested elsewhere).
        s
some bytesRcseZfdZRS(csD|jj|jj|jjd|jjdgdS(Nthellotworld(R?RR@R(R$(RA(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyR:s
(R3R4R:((RA(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRs(RR(R$R((RAsC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyttest_writeAfterLoseConnectionscsgtdD]}t|^q
dtffdY}t}||_t}t|t|}|jd}t
ttt
t}fd|_ttt}t|t|}|jd}	t|	|t}
fd}|
j||
S(s
        If multiple separate TLS messages are received in a single chunk from
        the underlying transport, all of the application bytes from each
        message are delivered to the application-level protocol.
        i
RcseZfdZRS(cs%xD]}|jj|qWdS(N(R?R(R$tb(RA(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyR:s
(R3R4R:((RA(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRscsS(N(((R(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRHscs)jdjjdjdS(NR(RXRR9(R(R$RRA(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRsN(trangetstrRRRIR"RRhRJR+R7R<R=R>RR
R RWRRR(R$tiRRMRoRNRqRrRsRtRuR((R$RRAsC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyttest_multipleWritess&%				
csdddtffdY}t}||_t}t|t|}|jd}tt	t
}fd|_ttt}t|t
|}|jd}t||}	fd}
|	j|
|	S(s
        If a very long string is passed to L{TLSMemoryBIOProtocol.write}, any
        trailing part of it which cannot be send immediately is buffered and
        sent later.
        s
some bytesi RcseZfdZRS(cs|jjdS(N(R?R(R$(RAtfactor(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyR:&s(R3R4R:((RAR(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyR%scsS(N(((R(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRH3scs$jdjjdS(NR(RXRR9(R(R$RRAR(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyR>sN(RRRIR"RRhRJR+R7R>RR
R RWRR(R$RRMRoRNRqRrRsRtRuR((R$RRRAsC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyttest_hugeWrites(				
cstt}fd|_t}t|t|}|jd}t}t	|||j
jd}j|S(s~
        If a L{TLSMemoryBIOProtocol} loses its connection unexpectedly, this is
        reported to the application.
        cs
tS(N(R((R{(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRHLscSs|jjtdS(N(R}R~R(RG((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyR]sN(
RRRIR"RRhRJR+RRR?R@R(R$RMRoRNRqRR((R{sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyttest_disorderlyShutdownDs				

	
cs;dtfdY}tt}|fd|_tj\}}t|t|}|jd	}t|t
}fd|_ttt}t|t
|}|jd	}	t|	|ddfd}
|j|
fd}|j||S(
s
        L{TLSMemoryBIOProtocol.loseConnection} sends a TLS close alert and
        shuts down the underlying connection cleanly on both sides, after
        transmitting all buffered data.
        tNotifyingProtocolcBseZdZdZRS(cSstj||g|_dS(N(RR%tdata(R$tonConnectionLost((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyR%js	cSs|jj|dS(N(RR;(R$RA((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRBos(R3R4R%RB(((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRis	csS(N(((RG(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRHuscsS(N(((R(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRHst	123456890ics-jjjjtgS(N(R?RR@R(R(R{tchunkOfBytesR|RRG(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pytcbHandshakes
csx|\}}|jjt|jjtjdj|jj|jjj	j|jjj	dS(NR(
R}R~RRXRRRdR?tqt
disconnect(t.0RGR(RR$(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRs	N(RRRRIR"R(RRhRJR+RR
R RWRR(R$RRMRoRpRNRqRrRsRtRR((R{R$RGRR|RsC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyt!test_loseConnectionAfterHandshakecs0					



cCsdtfdY}tttt}|}t||}t}|j||jd|j
|jt|j
|jd|j
ttd|j|jjt|j
|jjjddS(sz
        The user protocol's connectionLost is only called when transport
        underlying TLS is disconnected.
        tLostProtocolcBseZdZdZRS(cSs
||_dS(N(tdisconnected(R$RE((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRFsN(R3R4R+RRF(((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRstonoN(R(RRRRhRRR!RKt_tlsShutdownFinishedR+RXt
disconnectingRRFRRRdRCtvaluetargs(R$RRNRIttlsProtocolR?((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyt,test_connectionLostOnlyAfterUnderlyingClosess			

cstttt}t|t}t}|j||j|j	t
g|jfd}||_|j|j|j	t|jdg|j|jdgdS(s
        If TLSMemoryBIOProtocol.loseConnection is called multiple times, all
        but the first call have no effect.
        csjd|S(Ni(R;(tshutdown(tcalls(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyt_shutdownTLSs
iN(
RRRhRRRR!RKRXRRWRR@(R$RNRR?R((RsC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyttest_loseConnectionTwices		
	

csj\}}}|j}g}g|j|_j|_fd}|j|fd}|j||S(sP
        Unexpected disconnects get converted to ConnectionLost errors.
        csjdjjdS(NR(RR?R@(tign(Rw(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyt
handshakeDones
cs%jdjtddS(Ni(RdRCR(R(R$RE(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRs(RvtwrappedProtocolR;RBRFR(R$RxRptdisconnectDeferredRRRR((R$RwREsC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyttest_unexpectedEOFs	

csj\}}}gjj_dtfdY}|j_fd}|j|fd}|j||S(sN
        Errors while writing cause the protocols to be disconnected.
        tWrappercBs#eZdZdZdZRS(cSs
||_dS(N(t_wrapped(R$twrapped((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyR%scSst|j|S(N(tgetattrR(R$tattr((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyt__getattr__scWstddS(NsONO!(R(R$R((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pytsends(R3R4R%RR(((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRs		csjddS(NR(R(R(Rw(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRscs%jdjtddS(Ni(RdRCR(R(R$RE(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRs(RvR;RRFRat_tlsConnectionR(R$RxRpRRRR((R$RwREsC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyttest_errorWritings

(R3R4R5RgRkRnRvRzRRRRRRRRRRRRRRR(((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRcs*	
					2	&	.	!		
		'	'		L			tTLSProducerTestscBseZdZddZdZdZdZdZdZ	dZ
dZd	Zd
Z
dZdZd
ZdZdZRS(sA
    The TLS transport must support the IConsumer interface.
    cCsjdtfdY}td|\}}|}|jj|t|j|jjt|||fS(NtHistoryStringTransportcBs,eZdZdZdZdZRS(cSstj|g|_dS(N(R!R%tproducerHistory(R$((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyR%+s
cSs!|jjdtj|dS(Ntpause(RR;R!tpauseProducing(R$((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyR/scSs!|jjdtj|dS(Ntresume(RR;R!tresumeProducing(R$((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyR3scSs!|jjdtj|dS(Ntstop(RR;R!t
stopProducing(R$((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyR7s(R3R4R%RRR(((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyR*s			R?(R!RPR?tregisterProducerRhRXt	streaming(R$R?RRGRtproducer((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pytsetupStreamingProducer)s	cCsxtdD]|}|jj}|rE|j||jjn|jj}|rw|j||jjn|r
|r
Pq
q
W|j|jjd|j|jjddS(sJ
        Transfer bytes back and forth between two TLS protocols.
        iRN(RR?RRBtclearRX(R$RtserverTLSProtocolRt
clientDatat
serverData((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pytflushTwoTLSProtocolsBs

cCsh|j\}}}|jjj|j|jd|j|jdg|j|jjt	dS(s
        When the TLS transport is not blocked on reads, it correctly calls
        pauseProducing on the registered producer.
        tpausedRN(
RR?RRRXt
producerStateRt	_producert_producerPausedRh(R$RyRR((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyt(test_streamingProducerPausedInNormalModeWs
cCs|j\}}}|jjj|j|jdg|jjj|j|jd|j|jddg|j|jj	t
dS(s
        When the TLS transport is not blocked on reads, it correctly calls
        resumeProducing on the registered producer.
        Rt	producingRN(RR?RRRXRRRRRRW(R$RyRR((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyt)test_streamingProducerResumedInNormalModefscCsh|j\}}}|jjd|j|jd|j|jdg|j|jjtdS(s
        When the TLS transport is blocked on reads, it correctly calls
        pauseProducing on the registered producer.
        RRRN(	RR?RRXRRRRRh(R$RGRR((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyt4test_streamingProducerPausedInWriteBlockedOnReadModews
cCs|j\}}}|jjdd|j|jdgtdt\}}|j|||j|jddg|j|jj	t
|j|jjt
|j|jddS(s
        When the TLS transport is blocked on reads, it correctly calls
        resumeProducing on the registered producer.
        shello worldiRRLRRN(
RR?RRXRRPRhRRRRWRR(R$RGRRRR((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyt5test_streamingProducerResumedInWriteBlockedOnReadModescCsZ|j\}}}|j}t}|jt|jj|t|j|j|dS(sM
        Registering a streaming producer twice throws an exception.
        N(	RRRatassertRaisestRuntimeErrorR?RRhRm(R$RGRRtoriginalProducert	producer2((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyttest_streamingProducerTwices			cCsO|j\}}}|jj|j|jd|j|jjddS(s\
        Unregistering a streaming producer removes it, reverting to initial state.
        N(RR?tunregisterProducerRXRR+R(R$RGRR((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyt test_streamingProducerUnregisters
cCsg|j\}}}tdt\}}|sC|j||n|jjd|jj|j|||j|jjt	|j
d|jk|jjd|jjddg|jj
|j|jjd|j|jjt	|jjd|jjd	g|j|||j|jjt|jdj|jd
dS(sl
        Common code for tests involving writes by producer after
        loseConnection is called.
        RLsx RRt RRswon'tswon't!s
x hello worldN(RRPRhRR?RR@RXRRWtassertFalseRRRtassertNotEqualRRR9(R$twriteBlockedOnReadRGRRRR((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pytloseConnectionWithProducers&

cCs
|jtS(s
        loseConnection() waits for the producer to unregister itself, then
        does a clean TLS close alert, then closes the underlying connection.
        (RRW(R$((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyt0test_streamingProducerLoseConnectionWithProducerscCs
|jtS(s
        Even when writes are blocked on reading, loseConnection() waits for
        the producer to unregister itself, then does a clean TLS close alert,
        then closes the underlying connection.
        (RRh(R$((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyt4test_streamingProducerLoseConnectionWithProducerWBORscCsdtfdY}dtfdY}|}|j|\}}}|j|jd||_|jjd|j|jd|j|jdg|jj	j
|j|jd|j|jdd	g|jd|j|jd|j|jdd	gd
S(s
        pauseProducing() events can come from both the TLS transport layer and
        the underlying transport. In this case, both decide to pause,
        underlying first.
        tPausingStringTransportcBseZeZdZRS(cSsF|jr2|jdk	r2t|_|jjntj||dS(N(t	_didPauseRR+RhRR!R(R$R((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRs	(R3R4RWRR(((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRst
TLSConnectioncBs5eZdZdZdZdZdZRS(cSs
g|_dS(N(tl(R$((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyR%scSsa|js|d }nt|jdkrG|jjdtn|jj|t|S(NiiR(RR>R;R
(R$RA((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRs	
cSsdS(N((R$R((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyt	bio_writescSsttdt|jS(NtA(tchrtordR>R(R$tsize((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pytbio_readscSs
tdS(N(R
(R$R((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pytrecv s(R3R4R%RRRR(((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyR
s
				RRRRRN(R!RaRRXRRR?RRRRRB(R$RRR?RGRR((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyt1test_streamingProducerBothTransportsDecideToPauses 
	
cCs<|j\}}}|jjj|j|jddS(s
        If the underlying transport tells its producer to stopProducing(),
        this is passed on to the high-level producer.
        tstoppedN(RR?RRRXR(R$RyRR((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyt#test_streamingProducerStopProducing<scst\}t|j|jjtjjjjtj	jj	j
|jj	jjtfd}j
j|tdt\}}j|j
S(sM
        Non-streaming producers get wrapped as streaming producers.
        cs@jjdjjjdjjtdS(N(RXtconsumerR+R?RR#Rh(tignore(RRtstreamingProducerR$(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pytdoneXsRL(RPtNonStreamingProducerR?RRWRRRiRRXt	_consumerRRhtresultRR(R$RGRRR((RRRR$sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyttest_nonStreamingProducerFscCs/t}t|}|jtt|dS(sC
        L{_ProducerMembrane} implements L{IPushProducer}.
        N(R!RRdRR(R$Rtmembrane((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyttest_interfacefs	N(R3R4R5R+RRRRRRRRRRRRRR
R(((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyR$s 							
	1				B	
	 RcBsGeZdZdZeZdZdZdZdZ	dZ
RS(s5
    A pull producer which writes 10 times only.
    icCs||_t|_dS(N(RRR	(R$R((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyR%xs	cCs|jdkrc|jjt|j|jd7_|jdkr|jj|jqn|jdkrtdndS(Ni
is"BUG: resume after unregister/stop.(tcounterRRRRt_doneR+R(R$((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyR|s
cCstddS(Ns"BUG: pause should never be called.(R(R$((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRscCs)d|_|j}|`|jddS(N(R+RR	R*(R$td((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRs		cCst|_|jdS(N(RhRR(R$((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRs	(R3R4R5R
RWRR%RRRR(((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRps				tNonStreamingProducerTestscBsheZdZdZdZdZdZdZdZdZ	dZ
d	Zd
ZRS(sP
    Non-streaming producers can be adapted into being streaming producers.
    cst}t|jtjfd}|_|j}fd}|j|j|S(sd
        Verify the consumer writes out all its data, but is not called after
        that.
        cs|jdS(N(t
stopStreaming(torig(R(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyt
unregisterscs@jjdjjdjjtdS(Nt
0123456789(RXRRR+R#Rh(Ry(RRR$(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyt
doneStreamings(RRRRhRR	RtstartStreaming(R$Rt
nsProducerRRR((RR$RsC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pytstreamUntilEnds		

cCst}|j|S(s
        When converted to a streaming producer, the non-streaming producer
        writes out all its data, but is not called after that.
        (R!R(R$R((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyttest_writeUntilDones	csdtfdY}|}t|}t|||jtfd}|jj||j}fd}|j|j|S(s}
        When the streaming producer is paused, the underlying producer stops
        getting resumeProducing calls.
        RcBs eZdZdZdZRS(icSstj|t|_dS(N(R!R%RR(R$((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyR%s
cSs^|jd7_tj|||jdkrZ|jj|j}|`|jdndS(Nii(twritesR!RRRRR*R+(R$RR((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRs
	(R3R4RR%R(((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRs	csjddS(Ns$BUG: The producer should not finish!(tfail(R(R$(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pytshouldNotBeCalledscsjjjddS(Ni(RXt	_coopTaskt_pauseCount(R(R$R(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRs(	R!RRRRhR	RRR(R$RRRRRR((RR$sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyt
test_pauses		

cCs,dtfdY}|}|j|S(s
        When the streaming producer is paused and then resumed, the underlying
        producer starts getting resumeProducing calls again after the resume.

        The test will never finish (or rather, time out) if the resume
        producing call is not working.
        RcBseZdZdZRS(icSsO|jd7_tj|||jdkrK|jj|jjndS(Nii(RR!RRRR(R$R((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRs

(R3R4RR(((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRs(R!R(R$RR((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyttest_resumes
	csdtfdY}|ttjtj}fd}|j|j|S(s
        When the streaming producer is stopped by the consumer, the underlying
        producer is stopped, and streaming is stopped.
        tStoppingStringTransportcBseZdZdZRS(icSsB|jd7_tj|||jdkr>|jjndS(Nii(RR!RRR(R$R((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRs(R3R4RR(((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyR!
scs@jjdjjtjjtdS(Nt012(RXRRRhR#(Ry(RRR$R(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRs(R!RRRRhR	RR(R$R!RR((RR$RRsC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyttest_stopProducings			

csdtfdY}|}t|jtgtjjjtjjj	fd}|_	j
jj}|j
dfd}|j||S(s
        Common implementation for tests where the underlying producer throws
        an exception when its resumeProducing is called.
        tThrowingProducercBseZdZRS(cSs(|jdkrddStj|dS(Niii(R
RR(R$((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyR0s(R3R4R(((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyR$.scs|jdS(N(R(R(R(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyR@scSs
|jtS(N(R~R(RE((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRHIscsjjdj}jt|txSt|D]?\}\}}}j|j|j||dqQWjjt	dS(Nt01twhy(
RXRtflushLoggedErrorsR>tzipRdRCtassertInR#Rh(RterrorstftexpectedtmsgtlogMsg(R$texpectedExceptionst
loggedMsgsRR(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRJs"(RRRRhRRRR;RSRTRRRtwhenDonet
addErrbackR(R$RR/R$RRRR((RR$R/R0RsC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pytresumeProducingRaises)s	

csGtjtdfg}fd}|j||S(s
        If the underlying producer raises an exception when resumeProducing is
        called, the streaming wrapper should log the error, unregister from
        the consumer and stop streaming.
        s!failed, producing will be stoppedcsjjddS(N(RXRR+(R(R$R(sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyt
cleanShutdowncs(R!R3tZeroDivisionErrorR(R$RR4((R$RsC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyttest_resumeProducingRaisesYs	
cCs=t}d}||_|j|tdftdfgS(s
        If the underlying producer raises an exception when resumeProducing is
        called, the streaming wrapper should log the error, unregister from
        the consumer and stop streaming even if the unregisterProducer call
        also raise.
        cSs
tdS(N(R(((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pytraiserrss!failed, producing will be stoppedsfailed to unregister producer(R!RR3R5R(R$RR7((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyt4test_resumeProducingRaiseAndUnregisterProducerRaisesjs				cCsYt}t|}t||}|j|j|j|j|jtdS(s
        stopStreaming() can be called more than once without blowing
        up. This is useful for error-handling paths.
        N(R!RRRRRXR#Rh(R$RRR((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyttest_stopStreamingTwice{s	


cCs>t}t|}t||}|jtt|dS(s=
        L{_PullToPush} implements L{IPushProducer}.
        N(R!RRRdRR(R$RRR((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRs	(
R3R4R5RRRR R#R3R6R8R9R(((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyRs	 			+		!	0			N(BR5tzope.interface.verifyRttwisted.protocols.tlsRRRRtImportErrortskiptOpenSSL.cryptoRtOpenSSL.SSLRRRR	R
ttwisted.internet.sslRRR
ttwisted.python.filepathRttwisted.python.failureRttwisted.pythonRttwisted.internet.interfacesRRRttwisted.internet.errorRRttwisted.internet.deferRRttwisted.internet.protocolRRRttwisted.internet.taskRttwisted.protocols.loopbackRRttwisted.trial.unittestRttwisted.test.test_tcpRttwisted.test.test_sslR ttwisted.test.proto_helpersR!R"R7RWR+RPRQRcRRaRR(((sC/usr/lib/python2.7/dist-packages/twisted/protocols/test/test_tls.pyt<module>sF

(1/gM)