Your IP : 172.28.240.42


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


:Nc@sdZddlmZddlmZmZmZmZddlm	Z	ddl
mZddlm
Z
ddlmZddlmZdd	lZdd	lZy:dd
lmamZddlmaddlmZWn!ek
rd
ZenXyddl
mZWnek
r+d	ZnXe
je dZ!dej"fdYZ#dej"fdYZ$dej%fdYZ&dej%fdYZ'dej%fdYZ(dZ)dZ*dd1dYZ+td	k	rdtj,fdYZ-nd eej.fd!YZ/d"ej.fd#YZ0d$e0fd%YZ1d&ej.fd'YZ2d(ej.e+fd)YZ3d*d2d+YZ4d,ej.fd-YZ5d.ej.fd/YZ6ej7ed	d	krx/e/e0e1e2e3e5e6gD]Z8d0e8_9qWnd	S(3s 
Tests for twisted SSL support.
i(tunittest(tprotocoltreactort
interfacestdefer(tConnectionDone(tbasic(tutil(tplatform(tProperlyCloseFilesMixinN(tSSLtcrypto(tssl(tClientTLSContextcCsdaadS(N(tNoneR
R(((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyt_noSSLs(ttlss
server.pemtUnintelligentProtocolcBsMeZdZdddgZddgZdZdZdZd	ZRS(
s

    @ivar deferred: a deferred that will fire at connection lost.
    @type deferred: L{defer.Deferred}

    @cvar pretext: text sent before TLS is set up.
    @type pretext: C{str}

    @cvar posttext: text sent after TLS is set up.
    @type posttext: C{str}
    s
first lineslast thing before tls startstSTARTTLSsfirst thing after tls startedslast thing evercCstj|_dS(N(RtDeferredtdeferred(tself((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyt__init__<scCs%x|jD]}|j|q
WdS(N(tpretexttsendLine(Rtl((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pytconnectionMade@scCs]|dkrY|jjt|jjx|jD]}|j|q2W|jjndS(NtREADY(t	transporttstartTLSR
tfactorytclienttposttextRtloseConnection(RtlineR((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pytlineReceivedEs
cCs|jjddS(N(RtcallbackR(Rtreason((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pytconnectionLostMs(	t__name__t
__module__t__doc__RR RRR#R&(((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyR(s
					t
LineCollectorcBs>eZdZedZdZdZdZdZRS(sJ
    @ivar deferred: a deferred that will fire at connection lost.
    @type deferred: L{defer.Deferred}

    @ivar doTLS: whether the protocol is initiate TLS or not.
    @type doTLS: C{bool}

    @ivar fillBuffer: if set to True, it will send lots of data once
        C{STARTTLS} is received.
    @type fillBuffer: C{bool}
    cCs%||_||_tj|_dS(N(tdoTLSt
fillBufferRRR(RR+R,((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyR_s		cCsd|j_g|j_dS(Nt(Rtrawdatatlines(R((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyRescCs|jjj||dkr|jrSx(tdD]}|jddq5Wn|jd|jrtdtdt}|j	j
||jjq|jndS(NRitXiRtprivateKeyFileNametcertificateFileName(
RR/tappendR,trangeRR+tServerTLSContexttcertPathRRtservert
setRawMode(RR"txtctx((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyR#js	
		cCs#|jj|7_|jjdS(N(RR.RR!(Rtdata((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pytrawDataReceived{scCs|jjddS(N(RR$R(RR%((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyR&s(	R'R(R)tFalseRRR#R<R&(((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyR*Rs			tSingleLineServerProtocolcBseZdZdZRS(sK
    A protocol that sends a single line of data at C{connectionMade}.
    cCs!|jjd|jjdS(Ns+OK <some crap>
(RtwritetgetPeerCertificate(R((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyRs(R'R(R)R(((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyR>stRecordingClientProtocolcBs)eZdZdZdZdZRS(sv
    @ivar deferred: a deferred that will fire with first received content.
    @type deferred: L{defer.Deferred}
    cCstj|_dS(N(RRR(R((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyRscCs|jjdS(N(RR@(R((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyRscCs|jj|dS(N(RR$(RR;((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pytdataReceiveds(R'R(R)RRRB(((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyRAs		t ImmediatelyDisconnectingProtocolcBs eZdZdZdZRS(s
    A protocol that disconnect immediately on connection. It fires the
    C{connectionDisconnected} deferred of its factory on connetion lost.
    cCs|jjdS(N(RR!(R((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyRscCs|jjjddS(N(RtconnectionDisconnectedR$R(RR%((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyR&s(R'R(R)RR&(((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyRCs	cCstj}|jtjdtj}|j}||_||_|j||j	|dtj
}|jd|jd|j
d|j|j|j|j|j|j|j	|d|||fS(s
    Create a certificate for given C{organization} and C{organizationalUnit}.

    @return: a tuple of (key, request, certificate) objects.
    itmd5iii<(RtPKeytgenerate_keytTYPE_RSAtX509Reqtget_subjecttOtOUt
set_pubkeytsigntX509tset_serial_numbertgmtime_adj_notBeforetgmtime_adj_notAftert
set_issuertset_subjectt
get_pubkey(torganizationtorganizationalUnittpkeytreqtsubjecttcert((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pytgenerateCertificateObjectss"		



cCst||\}}}xd|tjfd|tjfd|tjfgD]Y\}}}tjj||f}	t|	d}
|
j	|tj
||
jqLWdS(s
    Create certificate files key, req and cert prefixed by C{basename} for
    given C{organization} and C{organizationalUnit}.
    tkeyRYR[twN(R\Rtdump_privatekeytdump_certificate_requesttdump_certificatetostextseptjointfileR?tFILETYPE_PEMtclose(tbasenameRVRWRXRYR[texttobjtdumpFunctfNametfObj((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pytgenerateCertificateFiless"tContextGeneratingMixincBs eZdZdZdZRS(s`
    Offer methods to create L{ssl.DefaultOpenSSLContextFactory} for both client
    and server.

    @ivar clientBase: prefix of client certificate files.
    @type clientBase: C{str}

    @ivar serverBase: prefix of server certificate files.
    @type serverBase: C{str}

    @ivar clientCtxFactory: a generated context factory to be used in
        C{reactor.connectSSL}.
    @type clientCtxFactory: L{ssl.DefaultOpenSSLContextFactory}

    @ivar serverCtxFactory: a generated context factory to be used in
        C{reactor.listenSSL}.
    @type serverCtxFactory: L{ssl.DefaultOpenSSLContextFactory}
    cOsb|j}t|||tjtjj|dftjj|df||}||fS(NR]R[(tmktempRnRtDefaultOpenSSLContextFactoryRbRcRd(RtorgtorgUnittargstkwArgstbasetserverCtxFactory((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pytmakeContextFactoryscCs@|j||\|_|_|j||\|_|_dS(N(Rxt
clientBasetclientCtxFactoryt
serverBaseRw(Rt
clientArgstclientKwArgst
serverArgstserverKwArgs((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pytsetupServerAndClients(R'R(R)RxR(((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyRos	R5cBseZdZeZdZRS(sU
        A context factory with a default method set to L{SSL.TLSv1_METHOD}.
        cOs'tj|d<tjj|||dS(Nt	sslmethod(R
tTLSv1_METHODRRqR(RRttkw((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyRs
(R'R(R)R=tisClientR(((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyR5
stStolenTCPTestCasecBs2eZdZdZdZdZdZRS(sc
    For SSL transports, test many of the same things which are tested for
    TCP transports.
    cCsCtjjttj}|j}tj|||d|S(sY
        Create an SSL server with a certificate using L{IReactorSSL.listenSSL}.
        t	interface(	RtPrivateCertificatetloadPEMReR6treadtoptionsRt	listenSSL(Rtaddresst
portNumberRR[tcontextFactory((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pytcreateServerscCstj}|j|||S(sG
        Create an SSL client using L{IReactorSSL.connectSSL}.
        (RtCertificateOptionst
connectSSL(RRRt
clientCreatorR((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyt
connectClient&scCstjS(s
        Return L{SSL.Error} as the expected error type which will be raised by
        a write to the L{OpenSSL.SSL.Connection} object after it has been
        closed.
        (R
tError(R((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pytgetHandleExceptionType.scCsDyddl}Wn*tk
r<tjdkr=tjSnXdgS(s
        Return the argument L{SSL.Error} will be constructed with for this
        case.  This is basically just a random OpenSSL implementation detail.
        It would be better if this test worked in a way which did not require
        this.
        iNtwin32sSSL routinest	SSL_writesprotocol is shutdown(sSSL routinesRsprotocol is shutdown(ttwisted.protocols.tlstImportErrorRtgetTypeterrnotWSAENOTSOCK(Rttwisted((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pytgetHandleErrorCode7s
(R'R(R)RRRR(((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyRs
	
			tTLSTestCasecBsPeZdZeZdZdZdZedZ	dZ
dZdZRS(s
    Tests for startTLS support.

    @ivar fillBuffer: forwarded to L{LineCollector.fillBuffer}
    @type fillBuffer: C{bool}
    cCsN|jjdk	r%|jjjn|jjdk	rJ|jjjndS(N(tclientProtoRRR!tserverProto(R((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyttearDowndscs|_tj}|_fd|_|r@t|_n	t|_|_tj	}|_
fd|_|rt|_n	t|_tjd|dd}|j
|jtjd|jj|tjjjgS(s
        Helper method to run TLS tests.

        @param clientProto: protocol instance attached to the client
            connection.
        @param serverProto: protocol instance attached to the server
            connection.
        @param clientIsServer: flag indicated if client should initiate
            startTLS instead of server.

        @return: a L{defer.Deferred} that will fire when both connections are
            lost.
        csS(N(((R(s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyt<lambda>{scsS(N(((R(s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyRsiRs	127.0.0.1(RRt
ClientFactoryt
clientFactoryR=R7tTrueRRt
ServerFactoryt
serverFactoryRt	listenTCPt
addCleanupt
stopListeningt
connectTCPtgetHosttportRt
gatherResultsR(RRRtclientIsServertcftsfR((RRs9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyt_runTestks 				cs=fd}jtttj}|j|S(s~
        Test for server and client startTLS: client should received data both
        before and after the startTLS.
        cs$jjjtjtjdS(N(tassertEqualRR/RRR (tignore(R(s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pytchecks	(RRR*RR,taddCallback(RRtd((Rs9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyttest_TLSscs=fd}jtttj}|j|S(s
        Test for server startTLS not followed by a startTLS in client: the data
        received after server startTLS should be received as raw.
        cs3jjjtjjjjddS(NsNo encrypted bytes received(RRR/RRt
failUnlessR.(tignored(R(s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyRs
	
(RRR*R=R,R(RRR((Rs9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyt
test_unTLSscs@fd}jttjtt}|j|S(s:
        Test startTLS first initiated by client.
        cs$jjjtjtjdS(N(RRR/RRR (R(R(s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyRs	(RR*RR,RR(RRR((Rs9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyttest_backwardsTLSsN(
R'R(R)R=R,RRRRRRRR(((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyRWs	&		tSpammyTLSTestCasecBseZdZeZRS(sA
    Test TLS features with bytes sitting in the out buffer.
    (R'R(R)RR,(((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyRstBufferingTestCasecBs&eZdZdZdZdZRS(cCsN|jjdk	r%|jjjn|jjdk	rJ|jjjndS(N(RRRR!R(R((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyRscst|_t|_tj}tj}|_fd|_fd|_tj	t
t
}tj}tj
d||dd}|j|jtjd|jj||jj|jdS(NcsS(N(((R(s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyRscsS(N(((R(s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyRsiRs	127.0.0.1s+OK <some crap>
(R>RRARRRRRRRqR6tClientContextFactoryRRRRRRRRRR(RR7RtsCTXtcCTXR((RRs9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyttest_openSSLBufferings	N(R'R(RRRRR(((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyRs	tConnectionLostTestCasecBsJeZdZdZdZedkr6de_ndZdZ	RS(s'
    SSL connection closing tests.
    csd}j||dfi||dfitj}tj|_tjd|j_}tj}t	|_t
j|_tj
d|jj|j}|jjfdS(Nstwisted.test.test_ssls, clients, serveris	127.0.0.1cs
jjS(N(t
serverPortR(t
ignoredResult(R(s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyRs(RRRtProtocolRRRwRRRCRRRDRRRRzR(RRrtserverProtocolFactoryRtclientProtocolFactorytclientConnector((Rs9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyttestImmediateDisconnects				csdtjfdY}d}|j||dfi||dfi|tj}fd|_tjd||j}|j|j|tj	}fd|_tj
d	|jj||j
}d
}tjjj|jj|gS(s
        Both sides of SSL connection close connection; the connections should
        close cleanly, and only after the underlying TCP connection has
        disconnected.
        tCloseAfterHandshakecBs,eZdZdZdZdZRS(cSstj|_dS(N(RRtdone(R((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyRscSs|jjddS(Nta(RR?(R((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyRscSs|jjdS(N(RR!(RR;((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyRBscSs|jj||`dS(N(Rterrback(tself2R%((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyR&s(R'R(RRRBR&(((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyR
s			stwisted.test.test_ssls, clients, servercsS(N(((tserverProtocol(s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyR#sicsS(N(((tclientProtocol(s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyR*ss	127.0.0.1cSs|jtdS(N(ttrapR(tfailure((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pytcheckResult.s(RRRRRRRwRRRRRRRzRRRt
addErrback(RRRrRRRRR((RRs9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyttest_bothSidesLoseConnections(					s*Old SSL code doesn't always close cleanly.c
sGd}|j||dfi||dfid}|jjjtj|tj}tj	|j
_tj}fd|_t
jd||j|_}tj}tj	|j
_tj}fd|_t
jd|jj||j}tj||gd	t}	|	j|jS(
Nstwisted.test.test_ssls, clients, servercWstS(N(R=(R((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pytverify>scsS(N(((R(s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyRFsicsS(N(((R(s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyRNss	127.0.0.1t
consumeErrors(RRzt
getContextt
set_verifyR
tVERIFY_PEERRRRRR$R&RRRRwRRRRRtDeferredListRRt_cbLostConns(
RRrRtserverConnLostRRtclientConnLostRRtdl((RRs9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyttestFailedVerify8s,			cCs|\\}}\}}|j||j|tjg}tjrjddlm}|j|n|j||j||j	j
S(Ni(tConnectionLost(tfailIfR
RRt	isWindowsttwisted.internet.errorRR3RRR(RtresultstsSuccesstsResulttcSuccesstcResulttacceptableErrorsR((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyRVs




N(
R'R(R)RRtnewTLSRtskipRR(((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyRs		-	tFakeContextcBs2eZdZdZdZdZdZRS(sK
    L{OpenSSL.SSL.Context} double which can more easily be inspected.
    cCs||_d|_dS(Ni(t_methodt_options(Rtmethod((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyRus	cCs|j|O_dS(N(R(RR((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pytset_optionszscCsdS(N((RtfileName((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pytuse_certificate_file~scCsdS(N((RR((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pytuse_privatekey_files(R'R(R)RRRR(((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyRqs
			t!DefaultOpenSSLContextFactoryTestscBs2eZdZdZdZdZdZRS(s8
    Tests for L{ssl.DefaultOpenSSLContextFactory}.
    cCs1tjttdt|_|jj|_dS(Nt_contextFactory(RRqR6RRRtcontext(R((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pytsetUpscCsk|j|jjtj|j|jjtj@|j|jjtj	@|j|jjtj
@dS(s
        L{ssl.DefaultOpenSSLContextFactory.getContext} returns an SSL context
        which can use SSLv3 or TLSv1 but not SSLv2.
        N(RRRR
t
SSLv23_METHODt
assertTrueRtOP_NO_SSLv2tassertFalsetOP_NO_SSLv3tOP_NO_TLSv1(R((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyttest_methodscCs&|jtjtjt|jdS(s
        Instantiating L{ssl.DefaultOpenSSLContextFactory} with a certificate
        filename which does not identify an existing file results in the
        initializer raising L{OpenSSL.SSL.Error}.
        N(tassertRaisesR
RRRqR6Rp(R((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyttest_missingCertificateFilescCs&|jtjtj|jtdS(s
        Instantiating L{ssl.DefaultOpenSSLContextFactory} with a private key
        filename which does not identify an existing file results in the
        initializer raising L{OpenSSL.SSL.Error}.
        N(RR
RRRqRpR6(R((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyttest_missingPrivateKeyFiles(R'R(R)RRRR(((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyRs
			tClientContextFactoryTestscBs eZdZdZdZRS(s0
    Tests for L{ssl.ClientContextFactory}.
    cCs1tj|_t|j_|jj|_dS(N(RRRRRRR(R((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyRscCsk|j|jjtj|j|jjtj@|j|jjtj	@|j|jjtj
@dS(s~
        L{ssl.ClientContextFactory.getContext} returns a context which can use
        SSLv3 or TLSv1 but not SSLv2.
        N(RRRR
RRRRRRR(R((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyRs(R'R(R)RR(((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyRs	s2Reactor does not support SSL, cannot run SSL tests(((:R)t
twisted.trialRttwisted.internetRRRRRRttwisted.protocolsRttwisted.pythonRttwisted.python.runtimeRttwisted.test.test_tcpR	RbRtOpenSSLR
RRttwisted.test.ssl_helpersR
RRRRRtsibpatht__file__R6tLineReceiverRR*RR>RARCR\RnRoRqR5tTestCaseRRRRRRRRtIReactorSSLttCaseR(((s9/usr/lib/python2.7/dist-packages/twisted/test/test_ssl.pyt<module>sX"
	

*3		(Ai#3