HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //proc/self/root/lib/python3/dist-packages/twisted/mail/test/__pycache__/test_smtp.cpython-38.pyc
U


W[���@sLdZddlmZmZddlZddlZddlmZddlm	Z	m
Z
ddlmZddl
mZddlmZmZdd	lmZmZmZmZdd
lmZmZmZddlmZmZddlmZddlZddl Zddl!Zddl"Zdd
l m#Z#m$Z$ddl!m%Z%m&Z&ddl"m'Z'ddlm(Z(ddl)m*Z*ddl+m,Z,zddl-m.Z.m/Z/Wne0k
�rNdZ1YnXdZ1ddl2Z2dd�Z3e	e*j4�Gdd�de5��Z6Gdd�de5�Z7Gdd�de5�Z8dZ9Gdd�d�Z:Gd d!�d!e:e*j;�Z<Gd"d#�d#e:e*j=�Z>Gd$d%�d%�Z?Gd&d'�d'ej@�ZAGd(d)�d)ejBe?�ZCGd*d+�d+e5�ZDGd,d-�d-�ZEGd.d/�d/eEe*jF�ZGGd0d1�d1eEe*jH�ZIGd2d3�d3�ZJGd4d5�d5eJejB�ZKGd6d7�d7eJejB�ZLe	ejMj%�Gd8d9�d9��ZNe	e*jO�Gd:d;�d;e5��ZPGd<d=�d=�ZQGd>d?�d?ejBe?�ZRGd@dA�dAejB�ZSGdBdC�dCe>�ZTGdDdE�dEejBe?�ZUejV�We��s6eUfD]ZXdFeX_Y�q(GdGdH�dHejB�ZZGdIdJ�dJejBe?�Z[GdKdL�dLej\�Z]GdMdN�dNejB�Z^GdOdP�dPejB�Z_e	e#�GdQdR�dRe5��Z`GdSdT�dTe5�ZaGdUdV�dVejB�ZbGdWdX�dXejB�ZcGdYdZ�dZejB�ZdGd[d\�d\ejB�ZeGd]d^�d^ejB�ZfGd_d`�d`ejB�ZgGdadb�dbe�ZhGdcdd�ddejB�ZidS)ez*
Test cases for twisted.mail.smtp module.
�)�absolute_import�divisionN)�BytesIO)�implementer�directlyProvides)�LineLog)�unittest)�basic�loopback)�defer�protocol�reactor�
interfaces)�address�error�task)�
MemoryReactor�StringTransport)�cred)�IRealm�Portal)�ICredentialsChecker�AllowAnonymousAccess)�
IAnonymous)�UnauthorizedLogin)�smtp)�LOGINCredentials)�ClientTLSContext�ServerTLSContextzOpenSSL not presentcOsdS�N�)�spamZeggsr r �=/usr/lib/python3/dist-packages/twisted/mail/test/test_smtp.py�	spameater1sr#c@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)�
BrokenMessagez�
    L{BrokenMessage} is an L{IMessage} which raises an unexpected exception
    from its C{eomReceived} method.  This is useful for creating a server which
    can be used to test client retry behavior.
    cCsdSrr ��self�userr r r"�__init__=szBrokenMessage.__init__cCsdSrr �r&�liner r r"�lineReceivedAszBrokenMessage.lineReceivedcCstd��dS)Nz"Some problem, delivery is failing.)�RuntimeError�r&r r r"�eomReceivedEszBrokenMessage.eomReceivedcCsdSrr r-r r r"�connectionLostIszBrokenMessage.connectionLostN)�__name__�
__module__�__qualname__�__doc__r(r+r.r/r r r r"r$6s
r$c@s(eZdZdZdd�Zdd�Zdd�ZdS)	�DummyMessagez�
    L{BrokenMessage} is an L{IMessage} which saves the message delivered to it
    to its domain object.

    @ivar domain: A L{DummyDomain} which will be used to store the message once
        it is received.
    cCs||_||_g|_dSr)�domainr'�buffer)r&r5r'r r r"r(VszDummyMessage.__init__cCst�d|�s|j�|�dS)Ns/Received: From yyy.com \(\[.*\]\) by localhost;)�re�matchr6�appendr)r r r"r+\s�zDummyMessage.lineReceivedcCs>d�|j�d}|jj|jjj�|�t�	�}|�
d�|S)N�
�saved)�joinr6r5�messagesr'�dest�localr9r�Deferred�callback)r&�messageZdeferredr r r"r.cs

zDummyMessage.eomReceivedN)r0r1r2r3r(r+r.r r r r"r4Nsr4c@s eZdZdZdd�Zdd�ZdS)�DummyDomainzf
    L{DummyDomain} is an L{IDomain} which keeps track of messages delivered to
    it in memory.
    cCsi|_|D]}g|j|<q
dSr)r=)r&�names�namer r r"r(qszDummyDomain.__init__cs2�jj�jkr"t���fdd��St�t����S)Ncs
t���Sr�r4r r%r r"�<lambda>y�z$DummyDomain.exists.<locals>.<lambda>)r>r?r=r�succeed�failrZSMTPBadRcptr%r r%r"�existswszDummyDomain.existsN)r0r1r2r3r(rKr r r r"rClsrCsSubject: hello

Goodbye
c@s>eZdZddd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�ZdS)�MyClientNcCs:|dkrddgtt�f}|d|_|d|_|d|_dS)Nzmoshez@foo.barr��)r�mail�_sender�
_recipient�_data�r&ZmessageInfor r r"r(�s�

zMyClient.__init__cCs|jSr)rPr-r r r"�getMailFrom�szMyClient.getMailFromcCs|jSr)rQr-r r r"�	getMailTo�szMyClient.getMailTocCs|jSr)rRr-r r r"�getMailData�szMyClient.getMailDatacCs
||_dSr)�_error)r&�excr r r"�	sendError�szMyClient.sendErrorcCsd|_d|_d|_dSr)rPrQrR)r&�code�respZnumOk�	addresses�logr r r"�sentMail�szMyClient.sentMail)N)	r0r1r2r(rTrUrVrYr^r r r r"rL�s
	rLc@seZdZddd�ZdS)�MySMTPClientNcCstj�|d�t�||�dS�Nsfoo.baz)r�
SMTPClientr(rLrSr r r"r(�szMySMTPClient.__init__)N�r0r1r2r(r r r r"r_�sr_c@seZdZddd�ZdS)�
MyESMTPClientrHNcCs tj�|||d�t�|�dSr`)r�ESMTPClientr(rL)r&Zsecret�contextFactoryr r r"r(�szMyESMTPClient.__init__)rHNrbr r r r"rc�srcc@seZdZdd�ZdS)�
LoopbackMixincCst�||�Sr)r
ZloopbackTCP)r&�server�clientr r r"r
�szLoopbackMixin.loopbackN)r0r1r2r
r r r r"rf�srfc@s*eZdZdddddgZdd�Zdd�Zd	S)
�FakeSMTPServer�	220 hellos250 nice to meet yous	250 greats354 go on, ladcCs4g|_|jdd�|_|j��|�|j���dSr)r6�
clientData�reverse�sendLine�popr-r r r"�connectionMade�s
zFakeSMTPServer.connectionMadecCsn|j�|�|dkr,|j�d�|j��n(|dkrB|j�d�n|dkrT|j��|jrj|�|j���dS)NsQUITs221 see ya around
�.s250 gotcha
�RSET)r6r9�	transport�write�loseConnectionrkrmrnr)r r r"r+�s
zFakeSMTPServer.lineReceivedN)r0r1r2rkror+r r r r"ri�s�ric	@sVeZdZdZdd�Zdddddd	d
ddg	Zd
d�Zdd�Zdd�Zdd�Z	dd�Z
dS)�SMTPClientTestsz'
    Tests for L{smtp.SMTPClient}.
    cCsjg}t�}|j|_|�t��|�d�|��|�|dtj	�|�
|dj�|�t
|d�d�dS)z�
        L{smtp.SMTPClient.timeoutConnection} calls the C{sendError} hook with a
        fatal L{SMTPTimeoutError} with the current line log.
        rjrsHTimeout waiting for SMTP server response
<<< 220 hello
>>> HELO foo.baz
N)r_r9rY�makeConnectionrr+ZtimeoutConnection�assertIsInstancer�SMTPTimeoutError�
assertTrue�isFatal�assertEqual�bytes)r&�errorsrhr r r"�test_timeoutConnection�s

�z&SMTPClientTests.test_timeoutConnectionsHELO foo.bazsMAIL FROM:<moshez@foo.bar>sRCPT TO:<moshez@foo.bar>sDATAsSubject: hellorHsGoodbyerprqcs0t�}t�����|�}|���fdd��|S)z�
        L{smtp.SMTPClient} sends I{HELO}, I{MAIL FROM}, I{RCPT TO}, and I{DATA}
        commands based on the return values of its C{getMailFrom},
        C{getMailTo}, and C{getMailData} methods.
        cs���j�j�Sr)r{r6�expected_output��x�r&rgr r"rG�sz/SMTPClientTests.test_messages.<locals>.<lambda>)r_rir
�addCallback)r&rh�dr r�r"�
test_messages�s
zSMTPClientTests.test_messagescCsdtddgtd�f�}t�}|�|�|�d�|�|jd�|�|j�|j�	�|�
|jt�dS)z�
        If there is an error while producing the message body to the
        connection, the C{sendError} callback is invoked.
        zalice@example.comzbob@example.com�foo�(220 Ok
250 Ok
250 Ok
250 Ok
354 Ok
N)
r_rrrv�dataReceived�assertNotIdentical�producer�assertFalse�	streamingZ
stopProducingrwrW�	Exception�r&rhrrr r r"�test_transferErrors�
�	
z"SMTPClientTests.test_transferErrorcCsPt�d�}t�}|�|�|�tjdddd��|�|��d�|�|j	�dS)z�
        If L{smtp.SMTPClient.sendError} is called with an L{SMTPClientError}
        which is fatal, it disconnects its transport without writing anything
        more to it.
        N�{�fooT�rzrH)
rrarrvrY�SMTPClientErrorr{�valuery�
disconnectingr�r r r"�test_sendFatalError s

z#SMTPClientTests.test_sendFatalErrorcCsPt�d�}t�}|�|�|�tjdddd��|�|��d�|�|j	�dS)z�
        If L{smtp.SMTPClient.sendError} is called with an L{SMTPClientError}
        which is not fatal, it sends C{"QUIT"} and waits for the server to
        close the connection.
        Nr�r�Fr��QUIT
)
rrarrvrYr�r{r�r�r�r�r r r"�test_sendNonFatalError.s

z&SMTPClientTests.test_sendNonFatalErrorcCsHt�d�}t�}|�|�|�td��|�|��d�|�|j	�dS)z�
        If L{smtp.SMTPClient.sendError} is called with an exception which is
        not an L{SMTPClientError}, it disconnects its transport without
        writing anything more to it.
        Nr�rH)
rrarrvrYr�r{r�ryr�r�r r r"�test_sendOtherError<s

z#SMTPClientTests.test_sendOtherErrorN)r0r1r2r3r~rr�r�r�r�r�r r r r"ru�s"�ruc@s$eZdZdd�Zdd�Zdd�ZdS)�DummySMTPMessagecCs||_||_g|_dSr)r�usersr6)r&rr�r r r"r(MszDummySMTPMessage.__init__cCs|j�|�dSr)r6r9r)r r r"r+SszDummySMTPMessage.lineReceivedcCsrd�|j�d}|jdjdt|jdj�}}g}|jD]}|�t|��q<||||f|jjt	|�<t
�d�S)Nr:rr;)r<r6r��helor|Zorigr9rrB�tuplerrI)r&rBr��origin�
recipientsr'r r r"r.Ws"
�zDummySMTPMessage.eomReceivedN)r0r1r2r(r+r.r r r r"r�Ksr�c@s,eZdZdd�Zdd�Zdd�Zdd�Zd	S)
�
DummyProtocCs|j�|�i|_dSr)�dummyMixinBaserorBr-r r r"roeszDummyProto.connectionMadecGsdSrr )r!r r r"�receivedHeaderjszDummyProto.receivedHeadercstd��_��fdd�S)Ncst��g�Sr)r�r r%r r"rGprHz'DummyProto.validateTo.<locals>.<lambda>)�SimpleDelivery�deliveryr%r r%r"�
validateTons
zDummyProto.validateTocCs|Srr �r&r�r�r r r"�validateFromsszDummyProto.validateFromN)r0r1r2ror�r�r�r r r r"r�csr�c@seZdZejZdS)�	DummySMTPN)r0r1r2r�SMTPr�r r r r"r�xsr�c@seZdZejZdS)�
DummyESMTPN)r0r1r2r�ESMTPr�r r r r"r�}sr�c@s�eZdZdZdZdddgddgdfddddgdddgd	fdd
dgdd
gdfgZdddgZeD]l\ZZZ	Z
ZZe�
deddddf�e	D]Ze�
deddddf�q�e�
ddedee
eefff�q\dd�ZdS)�AnotherTestCaseN�foo.comsmoshez@foo.comsmoshez@bar.coms(From: Moshe
To: Moshe

Hi,
how are you?
stttt@rrr.comsuuu@ooosyyy@eeesSubject: pass

..rrrr..
s@this,@is,@ignored:foo@bar.coms@ignore,@this,@too:bar@foo.comsfoo@bar.comsbar@foo.coms Subject: apa
To: foo

123
.
456
)rHs220.*
$NN)sHELO foo.com
�250.*
$NN)sRSET
r�NNsMAIL FROM:<s>
s250.*
s	RCPT TO:<sDATA
s354.*
cCsJt�}|��}Gdd�d�}|�|_|�|�|jD�]\}}}}|rP|�|�|��}|��t�	||�sxt
|||��|dd�dkr4|��D].}	|	r�|	dd�dkr�d|	}	|�|	d	�q�|�d
�|��}|��|\}
}t�	|
|�s�t
|
|��|dD]8}t|dd��}
|g|
d<|�
|j|ft|
���qq4|�d�dS)a
        Exercise a lot of the SMTP client code.  This is a "shotgun" style unit
        test.  It does a lot of things and hopes that something will go really
        wrong if it is going to go wrong.  This test should be replaced with a
        suite of nicer tests.
        c@seZdZdZdS)z/AnotherTestCase.test_buffer.<locals>.fooFactoryr�N)r0r1r2r5r r r r"�
fooFactory�sr�N�s354rrMrp�
s.
rN)r�serverClass�factoryrv�datar�r��clearr7r8�AssertionError�
splitlines�listr{rBr��
setTimeout)r&rr�ar��sendZexpect�msgZ	msgexpectr�r*r[ZmsgdataZrecip�expectedr r r"�test_buffer�s>





�
zAnotherTestCase.test_buffer)r0r1r2r��clientClassr=r�Zhelo_Zfrom_Zto_ZrealfromZrealtor�r9Zrcptr�r r r r"r��sP�
������
��r�c@seZdZeZeZdS)�AnotherESMTPTestsN)r0r1r2r�r�rcr�r r r r"r��sr�c@seZdZeZeZdS)�AnotherSMTPTestsN)r0r1r2r�r�r_r�r r r r"r��sr�c@s4eZdZddiZejjejjfZdd�Z	dd�Z
dS)�DummyChecker�testuser�testpasswordcCs"t�|j|j|j��|j|j�Sr)rZ
maybeDeferred�
checkPasswordr��usernamer��_cbCheck�r&�credentialsr r r"�requestAvatarId�s
��zDummyChecker.requestAvatarIdcCs|r|Stj���dSr)rrr)r&�resultr�r r r"r��szDummyChecker._cbCheckN)r0r1r2r�rr��IUsernamePasswordZIUsernameHashedPassword�credentialInterfacesr�r�r r r r"r��s��r�c@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)r�z[
    L{SimpleDelivery} is a message delivery factory with no interesting
    behavior.
    cCs
||_dSr�Z_messageFactory)r&ZmessageFactoryr r r"r(szSimpleDelivery.__init__cCsdSrr �r&r�r�r�r r r"r�	szSimpleDelivery.receivedHeadercCs|Srr r�r r r"r�
szSimpleDelivery.validateFromcs��fdd�S)Ncs
����Srr�r r%r r"rGrHz+SimpleDelivery.validateTo.<locals>.<lambda>r r%r r%r"r�szSimpleDelivery.validateToN)r0r1r2r3r(r�r�r�r r r r"r��s
r�c@seZdZdd�ZdS)�
DummyRealmcGstjtd�dd�fS)NcSsdSrr r r r r"rGrHz*DummyRealm.requestAvatar.<locals>.<lambda>)r�IMessageDeliveryr�)r&�avatarId�mindrr r r"�
requestAvatarszDummyRealm.requestAvatarN)r0r1r2r�r r r r"r�sr�c@s$eZdZdd�Zdd�Zdd�ZdS)�	AuthTestscstt�}tj�|�}|�t��tdtjji��|�_t	d�}t
�d�}|�|���
�|�}|���fdd��|S)z�
        L{ESMTPClient} can authenticate using the I{CRAM-MD5} SASL mechanism.

        @see: U{http://tools.ietf.org/html/rfc2195}
        sCRAM-MD5r�r�cs���jd�S)NrM)r{�
authenticatedr�r�r r"rG/rHz,AuthTests.test_crammd5Auth.<locals>.<lambda>)r�r�portalr�registerCheckerr�r�r�ZCramMD5CredentialsrcrZCramMD5ClientAuthenticator�registerAuthenticatorr
r��r&�realm�prhZcAuthr�r r�r"�test_crammd5Auths

zAuthTests.test_crammd5Authcspt�}tj�|�}|�t��tdti��|�_td�}t	�
d�}|�|����|�}|�
��fdd��|S)z�
        L{ESMTPClient} can authenticate using the I{LOGIN} SASL mechanism.

        @see: U{http://sepp.oetiker.ch/sasl-2.1.19-ds/draft-murchison-sasl-login-00.txt}
        �LOGINr�r�cs���j�Sr�ryr�r�r�r r"rGErHz*AuthTests.test_loginAuth.<locals>.<lambda>)r�rr�rr�r�r�rrcr�LOGINAuthenticatorr�r
r�r�r r�r"�test_loginAuth3s

zAuthTests.test_loginAuthcsrt�}tj�|�}|�t��tdtji��|�_t	d�}t�
d�}|�|����|�}|�
��fdd��|S)a
        When communicating with a server which implements the I{LOGIN} SASL
        mechanism using C{"Username:"} as the challenge (rather than C{"User
        Name\0"}), L{ESMTPClient} can still authenticate successfully using
        the I{LOGIN} mechanism.
        r�r�r�cs���j�Srr�r�r�r r"rG\rHz8AuthTests.test_loginAgainstWeirdServer.<locals>.<lambda>)r�rr�rr�r�r�rrrcr�r�r
r�r�r r�r"�test_loginAgainstWeirdServerIs

z&AuthTests.test_loginAgainstWeirdServerN)r0r1r2r�r�r�r r r r"r�sr�c@s<eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
S)�SMTPHelperTestscCs6i}td�D]$}t�d�}|�||k�d||<qdS)Ni�Ztestcase)�rangerZ	messageidr�)r&r��i�mr r r"�
testMessageIDbs

zSMTPHelperTests.testMessageIDcCsRddgddgt�d�dgddgt�d�dgg}|D]\}}|�t�|�|�q2dS)Nsuser@host.names<user@host.name>s"User Name" <user@host.name>ssomeguy@someplaces<someguy@someplace>rHs<>)rZAddressr{Z	quoteaddr)r&�cases�c�er r r"�
testQuoteAddrjs�zSMTPHelperTests.testQuoteAddrcCs$t�dddd�}|�t|�d�dS)Ns	user@hostshelo.host.namez	user@host)rZUserr{�str)r&�ur r r"�testUserwszSMTPHelperTests.testUsercCsvddddg}|D]`\}}|�t�|�|t|�f�|�|�d�|�|�t�|�|t|�f�|�|�d�|�qdS)N)�Hello worlds
Hello+20world)zHello+worlds
Hello+2Bworld)zs+00+01+02+03+04+05)ze=mc2@example.comse+3Dmc2@example.com�xtext)r{r�xtext_encode�len�encode�xtext_decode�decode)r&r��caser�r r r"�testXtextEncoding|s�
�z!SMTPHelperTests.testXtextEncodingcCsDd}|�t�|d�|�d�t|�f�|�|�dd�|�d��dS)z�
        Specifying an error policy to C{unicode.encode} with the
        I{xtext} codec should produce the same result as not
        specifying the error policy.
        r��strictr�N)r{rr�r�r�)r&�textr r r"�test_encodeWithErrors�s
�
�z%SMTPHelperTests.test_encodeWithErrorscCsDd}|�t�|d�|�d�t|�f�|�|�dd�|�d��dS)zO
        Similar to L{test_encodeWithErrors}, but for C{bytes.decode}.
        sHello worldr�r�N)r{rr�r�r�)r&r|r r r"�test_decodeWithErrors�s
�
�z%SMTPHelperTests.test_decodeWithErrorsN)	r0r1r2r�r�r�r�r�r�r r r r"r�as
r�c@seZdZdZdd�ZdS)�NoticeTLSClientFcCst�|||�d|_dS�NT)rc�esmtpState_starttls�tls)r&rZr[r r r"r��sz#NoticeTLSClient.esmtpState_starttlsN)r0r1r2r�r�r r r r"r��sr�c@s eZdZedk	reZdd�ZdS)�TLSTestsNcsBt�}t�}t|d��t|d�����fdd�}������|�S)N)recs ���jd����jd�dSr�)r{r�Z
startedTLS�Zignored�rhr&rgr r"�check�szTLSTests.testTLS.<locals>.check)rrr�r�r
r�)r&Z	clientCTXZ	serverCTXrr rr"�testTLS�s

zTLSTests.testTLS)r0r1r2�sslSkip�skiprr r r r"r�srzReactor doesn't support SSLc@seZdZdd�ZdS)�EmptyLineTestscCspt��}t�}|�|�|�d�|�d�|����}|�t	|�d�|�
|d�d��|�|dd�dS)z�
        If L{smtp.SMTP} receives an empty line, it responds with a 500 error
        response code and a message about a syntax error.
        rHNrNrs220rMs500 Error: bad syntax)rr�rrvr+r�r�r�r{r�ry�
startswith)r&�protorr�outr r r"�test_emptyLineSyntaxError�s


z(EmptyLineTests.test_emptyLineSyntaxErrorN)r0r1r2rr r r r"r�src@s@eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dS)�TimeoutTestszE
    Check that SMTP client factories correctly use the timeout.
    cstt���|�t�ddd��}�j|_t�}|�|�||_��fdd�}��	|t
j��|�}��
d���
d�|S)zR
        Connect the clientFactory, and check the timeout on the request.
        �TCP�example.net�cs�����d�dS)N��?)r{Zseconds)Zign��clockr&r r"r�sz(TimeoutTests._timeoutTest.<locals>.checkg�������?g�������?)r�Clock�
buildProtocolr�IPv4Address�	callLaterrrvr�
assertFailurerrxr��advance)r&�onDone�
clientFactoryrh�trr�r rr"�_timeoutTest�s�
�

zTimeoutTests._timeoutTestcCs.t��}tjddtd�|ddd�}|�||�S)z�
        Test timeout for L{smtp.SMTPSenderFactory}: the response L{Deferred}
        should be errback with a L{smtp.SMTPTimeoutError}.
        �source@addresssrecipient@address�Message bodyrr�ZretriesZtimeout�rr@r�SMTPSenderFactoryrr�r&rrr r r"�test_SMTPClientRecipientBytes�s�z*TimeoutTests.test_SMTPClientRecipientBytescCs.t��}tjddtd�|ddd�}|�||�S)z-
        Use a L{unicode} recipient.
        r�recipient@addressrrrrr r"r r r"�test_SMTPClientRecipientUnicodes�z,TimeoutTests.test_SMTPClientRecipientUnicodecCs.t��}tjddtd�|ddd�}|�||�S)z.
        Use a L{list} of recipients.
        r)zrecipient1@addresssrecipient2@addressrrrrr r"r r r"�test_SMTPClientRecipientLists�z)TimeoutTests.test_SMTPClientRecipientListc
Cs2t��}tjddddtd�|ddd�}|�||�S)	z�
        Test timeout for L{smtp.ESMTPSenderFactory}: the response L{Deferred}
        should be errback with a L{smtp.SMTPTimeoutError}.
        r��passwordrr$rrrr)rr@rZESMTPSenderFactoryrrr"r r r"�test_ESMTPClients�zTimeoutTests.test_ESMTPClientcCsGdd�d�}g}t��}|�|j�tjdd|d�|ddd�}d	|_t��}|�	t
�d
dd��}|j|_t
�}|�|�|�d
�|�|jd�|�|j�|�d�|�|g�|j��|�d�|�|g�|j��|�d�|�|g�|�|��d�dS)z�
        The timeout is not allowed to expire after the server has accepted a
        DATA command and the client is actively sending data to it.
        c@s"eZdZdZdd�Zddd�ZdS)z<TimeoutTests.test_resetTimeoutWhileSending.<locals>.SlowFilez�
            A file-like which returns one byte from each read call until the
            specified number of bytes have been returned.
            cSs
||_dSr�Z_size)r&�sizer r r"r(7szETimeoutTests.test_resetTimeoutWhileSending.<locals>.SlowFile.__init__NcSs|jr|jd8_dSdS)NrM�xrHr))r&�maxr r r"�read:szATimeoutTests.test_resetTimeoutWhileSending.<locals>.SlowFile.read)N)r0r1r2r3r(r-r r r r"�SlowFile2sr.rr$rMrr�r�example.orgr
rrr�NrNs250 Ok
s]HELO example.org
MAIL FROM:<source@address>
RCPT TO:<recipient@address>
DATA
x
.
RSET
)rr@Z
addErrbackr9rr!r5rrrrrrrrvr�r�r�r�r�rr{ZresumeProducingr�)r&r.Zfailedrrrrhrrr r r"�test_resetTimeoutWhileSending-sJ��
�




�z*TimeoutTests.test_resetTimeoutWhileSendingN)
r0r1r2r3rr#r%r&r(r0r r r r"r�s
rc@s eZdZdZdd�Zdd�ZdS)�(MultipleDeliveryFactorySMTPServerFactorya
    L{MultipleDeliveryFactorySMTPServerFactory} creates SMTP server protocol
    instances with message delivery factory objects supplied to it.  Each
    factory is used for one connection and then discarded.  Factories are used
    in the order they are supplied.
    cCs
||_dSr)�_messageFactories)r&ZmessageFactoriesr r r"r(�sz1MultipleDeliveryFactorySMTPServerFactory.__init__cCs$tj�||�}t|j�d��|_|S)Nr)r�
ServerFactoryrr�r2rnr�)r&Zaddrr�r r r"r�sz6MultipleDeliveryFactorySMTPServerFactory.buildProtocolN)r0r1r2r3r(rr r r r"r1sr1c@s eZdZdZdd�Zdd�ZdS)�SMTPSenderFactoryTestsz.
    Tests for L{smtp.SMTPSenderFactory}.
    cCs^t�}t��}t�ddtd�|�}|�dd|�}|�d�|�|t	�
d��|�|jd�dS)zt
        L{smtp.SMTPSenderFactory} removes the current protocol when the client
        connection is lost.
        rr$�message�	localhostrN�Bye.)
rrr@rr!r�
connectTCPrZclientConnectionLostr�ConnectionDoner{�currentProtocol�r&r
�sentDeferredrZ	connectorr r r"�2test_removeCurrentProtocolWhenClientConnectionLost�s�
�zISMTPSenderFactoryTests.test_removeCurrentProtocolWhenClientConnectionLostcCs^t�}t��}t�ddtd�|�}|�dd|�}|�d�|�|t	�
d��|�|jd�dS)zv
        L{smtp.SMTPSenderFactory} removes the current protocol when the client
        connection is failed.
        rr$r5r6rNr7)
rrr@rr!rr8rZclientConnectionFailedrr9r{r:r;r r r"�4test_removeCurrentProtocolWhenClientConnectionFailed�s�
�zKSMTPSenderFactoryTests.test_removeCurrentProtocolWhenClientConnectionFailedN)r0r1r2r3r=r>r r r r"r4�sr4c@seZdZdZdd�ZdS)�SMTPSenderFactoryRetryTestszD
    Tests for the retry behavior of L{smtp.SMTPSenderFactory}.
    c	s�d�d�t�g��G�fdd�dtj�}tt�fdd�g�}||_tjd|dd	�}|��}��	|j
�t��}t�
d
�dt��|�}d|_t�|j|j|�}��	|j�����fd
d�}|�|�|S)z�
        If the protocol created by L{SMTPSenderFactory} loses its connection
        before receiving confirmation of message delivery, it reconnects and
        tries to deliver the message again.
        salicessome message textcseZdZdZ�fdd�ZdS)zHSMTPSenderFactoryRetryTests.test_retryAfterDisconnect.<locals>.CleanSMTPz�
            An SMTP subclass which ensures that its transport will be
            disconnected before the test ends.
            cs��|j�tj�||�dSr)�
addCleanuprtrr�rv)Z	innerSelfrrr-r r"rv�szWSMTPSenderFactoryRetryTests.test_retryAfterDisconnect.<locals>.CleanSMTP.makeConnectionN)r0r1r2r3rvr r-r r"�	CleanSMTP�srAcs
t�|�SrrF)r')r5r r"rG�rHzGSMTPSenderFactoryRetryTests.test_retryAfterDisconnect.<locals>.<lambda>r�	127.0.0.1)�	interfacesbob@example.orgs@example.comr/cs6���j�d�dgi���t��t��d�dS)z�
            Verify that the message was successfully delivered and flush the
            error which caused the first attempt to fail.
            r:rMN)r{r=r��flushLoggedErrorsr,r�r5rBZ	recipientr&r r"�cbSent�s
�zESMTPSenderFactoryRetryTests.test_retryAfterDisconnect.<locals>.cbSent)rCrr�r1r$rr
Z	listenTCPZgetHostr@Z
stopListeningrr@r!rr5r8�hostZportZ
disconnectr�)	r&rAZ
serverFactoryZ
serverPortZ
serverHostr<rZclientConnectorrFr rEr"�test_retryAfterDisconnect�s:

���

z5SMTPSenderFactoryRetryTests.test_retryAfterDisconnectN)r0r1r2r3rHr r r r"r?�sr?c@s eZdZdZdd�Zdd�ZdS)�SingletonRealmz�
    Trivial realm implementation which is constructed with an interface and an
    avatar and returns that avatar when asked for that interface.
    cCs||_||_dSr�rC�avatar)r&rCrKr r r"r(�szSingletonRealm.__init__cGs,|D]"}||jkr||jdd�fSqdS)NcSsdSrr r r r r"rGrHz.SingletonRealm.requestAvatar.<locals>.<lambda>rJ)r&r�r�rZifacer r r"r��s
zSingletonRealm.requestAvatarN)r0r1r2r3r(r�r r r r"rI�srIc@s(eZdZdZdd�Zdd�Zdd�ZdS)	�NotImplementedDeliveryz�
    Non-implementation of L{smtp.IMessageDelivery} which only has methods which
    raise L{NotImplementedError}.  Subclassed by various tests to provide the
    particular behavior being tested.
    cCstd��dS�Nz3This oughtn't be called in the course of this test.��NotImplementedErrorr�r r r"r�sz#NotImplementedDelivery.validateFromcCstd��dSrMrNr%r r r"r�sz!NotImplementedDelivery.validateTocCstd��dSrMrNr�r r r"r�sz%NotImplementedDelivery.receivedHeaderN)r0r1r2r3r�r�r�r r r r"rLsrLc@sfeZdZdZejfdd�Zdd�Zdd�Zdd	�Z	d
d�Z
dd
�Zdd�Ze
e�dd��Zdd�ZdS)�SMTPServerTestsza
    Test various behaviors of L{twisted.mail.smtp.SMTP} and
    L{twisted.mail.smtp.ESMTP}.
    cCs>|�}d|_t�}|�|�|�t���|�d|���dS)zb
        Test that the specified hostname shows up in the SMTP server's
        greeting.
        sexample.comN)rGrrvr/rr9�assertInr�)r&r��srr r r"�testSMTPGreetingHosts
z$SMTPServerTests.testSMTPGreetingHostcCs:t��}t�}|�|�|�t���|�d|���dS)z�
        Test that the string "ESMTP" does not appear in the SMTP server's
        greeting since that string strongly suggests the presence of support
        for various SMTP extensions which are not supported by L{smtp.SMTP}.
        �ESMTPN)	rr�rrvr/rr9ZassertNotInr��r&rRrr r r"�testSMTPGreetingNotExtended+s

z+SMTPServerTests.testSMTPGreetingNotExtendedcCs|�tj�dS)zS
        Similar to testSMTPGreetingHost, but for the L{smtp.ESMTP} class.
        N)rSrr�r-r r r"�testESMTPGreetingHost8sz%SMTPServerTests.testESMTPGreetingHostcCs:t��}t�}|�|�|�t���|�d|���dS)z�
        Test that the string "ESMTP" does appear in the ESMTP server's
        greeting since L{smtp.ESMTP} does support the SMTP extensions which
        that advertises to the client.
        rTN)	rr�rrvr/rr9rQr�rUr r r"�testESMTPGreetingExtended?s

z)SMTPServerTests.testESMTPGreetingExtendedcCsDt��}t�}|�|�|�d�|�t���|�d|�	��dS)zN
        Sending an unimplemented command is responded to with a 500.
        sDOAGOODTHINGs500 Command not implementedN)
rr�rrvr+r/rr9rQr�rUr r r"�test_SMTPUnknownCommandLs

z'SMTPServerTests.test_SMTPUnknownCommandcCs�Gdd�dt�}ttj|��}t|t�g�}t��}||_t�}|�	|�|�
d�|��|�
d�|�t
���|�|��d�dS)z�
        Test that a C{MAIL FROM} command with an acceptable address is
        responded to with the correct success code.
        c@seZdZdZdd�ZdS)zDSMTPServerTests.test_acceptSenderAddress.<locals>.AcceptanceDeliveryzM
            Delivery object which accepts all senders as valid.
            cSs|Srr r�r r r"r�aszQSMTPServerTests.test_acceptSenderAddress.<locals>.AcceptanceDelivery.validateFromN�r0r1r2r3r�r r r r"�AcceptanceDelivery]sr[�HELO example.com
�MAIL FROM:<alice@example.com>
s250 Sender address accepted
N�rLrIrr�rrr�r�rrvr�r�r/r�ConnectionLostr{r�)r&r[r�r�r	�transr r r"�test_acceptSenderAddressXs


�z(SMTPServerTests.test_acceptSenderAddresscCs�Gdd�dt�}ttj|��}t|t�g�}t��}||_t�}|�	|�|�
d�|��|�
d�|�t
���|�|��d�dS)z�
        Test that a C{MAIL FROM} command with an address rejected by a
        L{smtp.IMessageDelivery} instance is responded to with the correct
        error code.
        c@seZdZdZdd�ZdS)zMSMTPServerTests.test_deliveryRejectedSenderAddress.<locals>.RejectionDeliveryzO
            Delivery object which rejects all senders as invalid.
            cSst�|��dSr)rZ
SMTPBadSenderr�r r r"r��szZSMTPServerTests.test_deliveryRejectedSenderAddress.<locals>.RejectionDelivery.validateFromNrZr r r r"�RejectionDelivery�srbr\r]�V550 Cannot receive from specified address <alice@example.com>: Sender not acceptable
Nr^)r&rbr�r�r	r`r r r"�"test_deliveryRejectedSenderAddress|s


�z2SMTPServerTests.test_deliveryRejectedSenderAddresscCs�Gdd�dt�}ttjt��}t||�g�}t��}||_t�}|�	|�|�
d�|��|�
d�|�t
���|�|��d�dS)z�
        Test that a C{MAIL FROM} command with an address rejected by an
        L{smtp.SMTP} instance's portal is responded to with the correct error
        code.
        c@seZdZdZefZdd�ZdS)zQSMTPServerTests.test_portalRejectedSenderAddress.<locals>.DisallowAnonymousAccesszZ
            Checker for L{IAnonymous} which rejects authentication attempts.
            cSst�t��Sr)rrJrr�r r r"r��szaSMTPServerTests.test_portalRejectedSenderAddress.<locals>.DisallowAnonymousAccess.requestAvatarIdN)r0r1r2r3rr�r�r r r r"�DisallowAnonymousAccess�srer\r]rcN)�objectrIrr�rLrr�r�rrvr�r�r/rr_r{r�)r&rer�r�r	r`r r r"� test_portalRejectedSenderAddress�s	


�z0SMTPServerTests.test_portalRejectedSenderAddresscCstttjt��}t|g�}t��}||_t�}|�|�|�	d�|�
�|�	d�|�t�
��|�|��d�dS)z�
        Test that a C{MAIL FROM} command issued without first authenticating
        when a portal has been configured to disallow anonymous logins is
        responded to with the correct error code.
        r\r]sd550 Cannot receive from specified address <alice@example.com>: Unauthenticated senders not allowed
N)rIrr�rLrr�r�rrvr�r�r/rr_r{r�)r&r�r�r	r`r r r"�"test_portalRejectedAnonymousSender�s



�z2SMTPServerTests.test_portalRejectedAnonymousSenderN)r0r1r2r3rr�rSrVrWrXrYrardrrrgrhr r r r"rPs


$&
(rPc@sveZdZdd�Zddd�Zdd�Zd	d
�Zdd�Zd
d�Zdd�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�ZdS)�ESMTPAuthenticationTestscCs0|j��|j�|�|�||j�����dS)z�
        Assert that when the given bytes are delivered to the ESMTP server
        instance, it responds with the indicated lines.

        @type bytes: str
        @type response: list of str
        N)rrr�rgr�r{r�r�)r&r|Zresponser r r"�assertServerResponse�s
�z-ESMTPAuthenticationTests.assertServerResponse�username�passwordcCs�|��\}}}}|�|g�|�tjjj�|��|�|j|�|�|�	|��|�
tj|�|�
tj
|�|�tjddd�f�|�dg|j�����dS)a�
        Assert that a login attempt has been made, that the credentials and
        interfaces passed to it are correct, and that when the login request
        is satisfied, a successful response is sent by the ESMTP server
        instance.

        @param loginArgs: A C{list} previously passed to L{portalFactory}.
        @param username: The login user.
        @param password: The login password.
        NcSsdSrr r r r r"rGrHzDESMTPAuthenticationTests.assertServerAuthenticated.<locals>.<lambda>s235 Authentication successful.)rnr{ry�twistedrr�r��
providedByr�r�rQrZIMessageDeliveryFactoryr�rArrr�r�)r&�	loginArgsr�r'r�r�r�rr r r"�assertServerAuthenticated�s�z2ESMTPAuthenticationTests.assertServerAuthenticatedcCs@t�dti�|_d|j_tt�ddd�d�|_|j�	|j�dS)zI
        Create an ESMTP instance attached to a StringTransport.
        r�s	localhostr
rBi90)ZpeerAddressN)
rr�rrgrGrrrrrrvr-r r r"�setUps��zESMTPAuthenticationTests.setUpcCs|j�t���dS)zT
        Disconnect the ESMTP instance to clean up its timeout DelayedCall.
        N)rgr/rr9r-r r r"�tearDown!sz!ESMTPAuthenticationTests.tearDowncsG�fdd�d�}|�S)NcseZdZ�fdd�ZdS)z;ESMTPAuthenticationTests.portalFactory.<locals>.DummyPortalcst��}��||||f�|Sr)rr@r9)r&r�r�rr���	loginListr r"�login*szAESMTPAuthenticationTests.portalFactory.<locals>.DummyPortal.loginN)r0r1r2rur rsr r"�DummyPortal)srvr )r&rtrvr rsr"�
portalFactory(sz&ESMTPAuthenticationTests.portalFactorycCsX|j��|j�d�|j����}|�|dd�|�|dd�|�t|�d�dS)zV
        Test that AUTH is advertised to clients which issue an EHLO command.
        �EHLO
rs/250-localhost Hello 127.0.0.1, nice to meet yourMs250 AUTH LOGINrNN)rrr�rgr�r�r�r{r�)r&Z
responseLinesr r r"�'test_authenticationCapabilityAdvertised1s
��z@ESMTPAuthenticationTests.test_authenticationCapabilityAdvertisedcCs�g}|�|�|j_|j�d�|j��|�ddt�d��	�g�|�t�d�ddt�d��	�g�|�t�d��	�dg�|�
|�d	S)
zJ
        Test that the LOGIN authentication mechanism can be used
        rx�AUTH LOGIN
�334 �
User Namerkr��	PasswordrlN�rwrgr�r�rrr�rj�base64�	b64encode�striprp�r&ror r r"�test_plainAuthenticationAs"
���z1ESMTPAuthenticationTests.test_plainAuthenticationcCs�g}|�|�|j_|j�d�|j��|�ddt�d��	�g�|�t�d�ddt�d��	�g�|�dg�|j
|dd	�d
S)zM
        Test that giving an empty password for plain auth succeeds.
        rxrzr{r|rkr�r}rH)r'Nr~r�r r r"�%test_plainAuthenticationEmptyPasswordZs
��z>ESMTPAuthenticationTests.test_plainAuthenticationEmptyPasswordcCs~g}|�|�|j_|j�d�|j��|�dt�d��	�ddt�d��	�g�|�t�d��	�dg�|�
|�dS)	z�
        The response to the first challenge may be included on the AUTH command
        line.  Test that this is also supported.
        rx�AUTH LOGIN rkr�r{r}rlNr~r�r r r"�'test_plainAuthenticationInitialResponseps
��z@ESMTPAuthenticationTests.test_plainAuthenticationInitialResponsecCs<g}|�|�|j_|j�d�|j�d�|�ddg�dS)zW
        Test that a challenge/response sequence can be aborted by the client.
        rxrzs*
s501 Authentication abortedN)rwrgr�r�rjr�r r r"�test_abortAuthentication�s�z1ESMTPAuthenticationTests.test_abortAuthenticationcCsHg}|�|�|j_|j�d�|j�d�|�ddg�|�|g�dS)zz
        Test that a response which is not properly Base64 encoded results in
        the appropriate error code.
        rxrzsx
�+501 Syntax error in parameters or argumentsN�rwrgr�r�rjr{r�r r r"�!test_invalidBase64EncodedResponse�s�z:ESMTPAuthenticationTests.test_invalidBase64EncodedResponsecCs<g}|�|�|j_|j�d�|�ddg�|�|g�dS)z�
        Like L{test_invalidBase64EncodedResponse} but for the case of an
        initial response included with the C{AUTH} command.
        rxsAUTH LOGIN x
r�Nr�r�r r r"�(test_invalidBase64EncodedInitialResponse�s�zAESMTPAuthenticationTests.test_invalidBase64EncodedInitialResponsecCs�g}|�|�|j_|j�d�|j��|�dt�d��	�ddt�d��	�g�|�t�d��	�dg�|�
�\}}}}|�td��|�
d	|j���|�
t|�t��d
�dS)z�
        If the L{Deferred} returned by L{Portal.login} fires with an
        exception of any type other than L{UnauthorizedLogin}, the exception
        is logged and the client is informed that the authentication attempt
        has failed.
        rxr�rkr�r{r}rlzSomething wrong with the servers9451 Requested action aborted: local error in processing
rMN)rwrgr�r�rrr�rjrr�r�rnZerrbackr,r{r�r�rD)r&ror�r�r�rr r r"�test_unexpectedLoginFailure�s&
���z4ESMTPAuthenticationTests.test_unexpectedLoginFailureN)rkrl)r0r1r2rjrprqrrrwryr�r�r�r�r�r�r�r r r r"ri�s�
	ric@s(eZdZdZdd�Zdd�Zdd�ZdS)	�SMTPClientErrorTestsz,
    Tests for L{smtp.SMTPClientError}.
    cCs t�dd�}|�t|�d�dS)z�
        The string representation of a L{SMTPClientError} instance includes
        the response code and response string.
        r�z	some textz
123 some textN�rr�r{r��r&�errr r r"�test_str�szSMTPClientErrorTests.test_strcCs t�dd�}|�t|�d�dS)z�
        If the response code supplied to L{SMTPClientError} is negative, it
        is excluded from the string representation.
        ���sfoo barzfoo barNr�r�r r r"�test_strWithNegativeCode�sz-SMTPClientErrorTests.test_strWithNegativeCodecCsDtd�}|�d�|�d�tjdd|��d�}|�t|�d�dS)	z�
        If a line log is supplied to L{SMTPClientError}, its contents are
        included in the string representation of the exception instance.
        �
stestlogs
secondline�dz
test error)r]z"100 test error
testlog
secondline
N)rr9rr�r�r{)r&r]r�r r r"�test_strWithLog�s

�z$SMTPClientErrorTests.test_strWithLogN)r0r1r2r3r�r�r�r r r r"r��s		r�c@seZdZdZdd�ZdS)�SenderMixinSentMailTestsz�
    Tests for L{smtp.SenderMixin.sentMail}, used in particular by
    L{smtp.SMTPSenderFactory} and L{smtp.ESMTPSenderFactory}.
    csr��t��tj�}|��fdd��tjddtd�|ddd�}|�t	�
d	d
d��}dd
g}|�ddd||j�|S)z�
        L{smtp.SenderMixin.sentMail} adds only the addresses with failing
        SMTP response codes to the log passed to the factory's errback.
        cs��|jd�S)Ns'bob@example.com: 199 Error in sending.
)r{r])r�r-r r"rGs�zFSenderMixinSentMailTests.test_onlyLogFailedAddresses.<locals>.<lambda>rr$rrrrr
rr)salice@example.com��sNo errors here!)sbob@example.com��sError in sending.r�s
Test responserM)
rrr@rZSMTPDeliveryErrorr�r!rrrrr^r])r&rrrhr\r r-r"�test_onlyLogFailedAddressess$���z4SenderMixinSentMailTests.test_onlyLogFailedAddressesN)r0r1r2r3r�r r r r"r��sr�c@s8eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�ZdS)
�ESMTPDowngradeTestCasezU
    Tests for the ESMTP -> SMTP downgrade functionality in L{smtp.ESMTPClient}.
    cCst�ddd�|_dS)Nr�r�)rrd�clientProtocolr-r r r"rqs
�zESMTPDowngradeTestCase.setUpcCs^t�}d|j_d|j_d|j_|j�|�|j�d�|��|j�d�|�d|�	��dS)z�
        If both authentication and transport security are not required, and it
        is asked for, it will fall back to allowing HELO.
        FT�220 localhost
�500 not an esmtp server
�HELO testuser
N�
rr��requireAuthentication�requireTransportSecurity�heloFallbackrvr�r�r{r��r&rrr r r"� test_requireHELOFallbackOperates$sz7ESMTPDowngradeTestCase.test_requireHELOFallbackOperatescCs^t�}d|j_d|j_d|j_|j�|�|j�d�|��|j�d�|�d|�	��dS)z�
        If authentication is required, and HELO fallback is on, HELO fallback
        must not be honoured, as authentication requires EHLO to succeed.
        TFr�r�r�Nr�r�r r r"�!test_requireAuthFailsHELOFallback5sz8ESMTPDowngradeTestCase.test_requireAuthFailsHELOFallbackcCs^t�}d|j_d|j_d|j_|j�|�|j�d�|��|j�d�|�d|�	��dS)z�
        If TLS is required and the connection is insecure, HELO fallback must
        not be honoured, as STARTTLS requires EHLO to succeed.
        FTr�r�r�Nr�r�r r r"� test_requireTLSFailsHELOFallbackFsz7ESMTPDowngradeTestCase.test_requireTLSFailsHELOFallbackcCsjt�}t|tj�d|j_d|j_d|j_|j�|�|j�	d�|�
�|j�	d�|�d|���dS)z�
        If TLS is provided at the transport level, we can honour the HELO
        fallback if we're set to require TLS.
        FTr�r�r�N)
rrr�
ISSLTransportr�r�r�r�rvr�r�r{r�r�r r r"�/test_requireTLSAndHELOFallbackSucceedsIfOverTLSWszFESMTPDowngradeTestCase.test_requireTLSAndHELOFallbackSucceedsIfOverTLSN)	r0r1r2r3rqr�r�r�r�r r r r"r�sr�c@sleZdZdZedk	reZdZdZdd�Zdd�Z	d	d
�Z
dd�Zd
d�Zdd�Z
dd�Zdd�Zdd�ZdS)�SSLTestCasezD
    Tests for the TLS negotiation done by L{smtp.ESMTPClient}.
    Ns3220 localhost NO UCE NO UBE NO RELAY PROBES ESMTP
s1250-localhost Hello 127.0.0.1, nice to meet you
cCs*t�dt�d�|_d|j_dd�|j_dS)Nr�r�TcSsdS)Nztest@example.orgr r r r r"rGxrHz#SSLTestCase.setUp.<locals>.<lambda>)rrdrr�r�rTr-r r r"rqts�zSSLTestCase.setUpcCsZt�}t|tj�|j�|�|j�|j�|��|j�|j	|�|�
d|���dS)a�
        Verify that when L{smtp.ESMTPClient} connects to a server over a
        transport providing L{ISSLTransport}, C{requireTransportSecurity} is
        C{True}, and it is presented with the given capabilities, it will try
        to send its mail and not first attempt to negotiate TLS using the
        I{STARTTLS} protocol action.

        @param capabilities: Bytes to include in the test server's capability
            response.  These must be formatted exactly as required by the
            protocol, including a line which ends the capability response.
        @type param: L{bytes}

        @raise: C{self.failureException} if the behavior of
            C{self.clientProtocol} is not as described.
        sMAIL FROM:<test@example.org>
N)rrrr�r�rvr��SERVER_GREETINGr��
EHLO_RESPONSEr{r�)r&Zcapabilitiesrrr r r"�$_requireTransportSecurityOverSSLTest{s�z0SSLTestCase._requireTransportSecurityOverSSLTestcCs|�d�dS)z�
        When C{requireTransportSecurity} is C{True} and the client is connected
        over an SSL transport, mail may be delivered.
        �250 AUTH LOGIN
N�r�r-r r r"�$test_requireTransportSecurityOverSSL�sz0SSLTestCase.test_requireTransportSecurityOverSSLcCsNt�}|j�|�|j�|j�|��|j�|jd�|�d|���dS)z�
        When C{requireTransportSecurity} is C{True} and the client is connected
        over a non-SSL transport, if the server offers the I{STARTTLS}
        extension, it is used before mail is delivered.
        �250-AUTH LOGIN
250 STARTTLS
s
STARTTLS
N�	rr�rvr�r�r�r�r{r�r�r r r"�'test_requireTransportSecurityTLSOffered�s��z3SSLTestCase.test_requireTransportSecurityTLSOfferedcCs|�d�dS)z�
        When C{requireTransportSecurity} is C{True} and the client is connected
        over an SSL transport, if the server offers the I{STARTTLS}
        extension, it is not used before mail is delivered.
        r�Nr�r-r r r"�.test_requireTransportSecurityTLSOfferedOverSSL�s�z:SSLTestCase.test_requireTransportSecurityTLSOfferedOverSSLcCsNt�}|j�|�|j�|j�|��|j�|jd�|�d|���dS)z�
        When C{requireTransportSecurity} is C{True} and the client is connected
        over a non-SSL transport, if the server does not offer the I{STARTTLS}
        extension, mail is not delivered.
        r�r�Nr�r�r r r"�*test_requireTransportSecurityTLSNotOffered�s��z6SSLTestCase.test_requireTransportSecurityTLSNotOfferedcCsV|jj}~|j|jgd�}|�t|�d�|�|ddt�|�|ddd�dS)�<
        L{smtp.ESMTPClient.tlsMode} is deprecated.
        �ZoffendingFunctionsrMr�categoryrB�StlsMode attribute of twisted.mail.smtp.ESMTPClient is deprecated since Twisted 13.0N)r��tlsMode�
flushWarnings�%test_esmtpClientTlsModeDeprecationGetr{r��assertIdentical�DeprecationWarning)r&�val�
warningsShownr r r"r��s�
�
�z1SSLTestCase.test_esmtpClientTlsModeDeprecationGetcs��t�fdd��dS)z�
        L{smtp.ESMTPClient.__getattr__} raises an attribute error for other
        attribute names which do not exist.
        cs�jjSr)r�ZdoesNotExistr r-r r"rG�rHzQSSLTestCase.test_esmtpClientTlsModeDeprecationGetAttributeError.<locals>.<lambda>N)ZassertRaises�AttributeErrorr-r r-r"�3test_esmtpClientTlsModeDeprecationGetAttributeError�s
�z?SSLTestCase.test_esmtpClientTlsModeDeprecationGetAttributeErrorcCsTd|j_|j|jgd�}|�t|�d�|�|ddt�|�|ddd�dS)	r�Fr�rMrr�rBr�N)r�r�r��%test_esmtpClientTlsModeDeprecationSetr{r�r�r�)r&r�r r r"r��s�
�
�z1SSLTestCase.test_esmtpClientTlsModeDeprecationSet)r0r1r2r3rrr�r�rqr�r�r�r�r�r�r�r�r r r r"r�js"	r�c@seZdZdZdZdd�ZdS)�AbortableStringTransportzK
    A version of L{StringTransport} that supports C{abortConnection}.
    FcCsd|_|��dS)z�
        A testable version of the C{ITCPTransport.abortConnection} method.

        Since this is a special case of closing the connection,
        C{loseConnection} is also called.
        TN)�abortingrtr-r r r"�abortConnectionsz(AbortableStringTransport.abortConnectionN)r0r1r2r3r�r�r r r r"r�sr�c@sXeZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�ZdS)�
SendmailTestsz2
    Tests for L{twisted.mail.smtp.sendmail}.
    cCs(t�tj�\}}}}|�t|d�dS)z{
        The default C{reactor} parameter of L{twisted.mail.smtp.sendmail} is
        L{twisted.internet.reactor}.
        rNN)�inspectZ
getargspecr�sendmailr{r
)r&�argsZvarArgs�keywords�defaultsr r r"�"test_defaultReactorIsGlobalReactor sz0SendmailTests.test_defaultReactorIsGlobalReactorcCslt�}tjdddd|||ddd�	|jdd}|�|jd�|�|jd�|�|jd	�|�|jd
�dS)zk
        L{twisted.mail.smtp.sendmail} creates the ESMTP factory with the ESMTP
        arguments.
        r6rr$r5T)r
r�r'r�r�rrNr��barN)	rrr��
tcpClientsr{Z_requireTransportSecurityZ_requireAuthenticationr�r')r&r�r'r
r�r r r"�_honorsESMTPArguments)s
�z#SendmailTests._honorsESMTPArgumentscCs|jddd�S)zw
        L{twisted.mail.smtp.sendmail} should accept C{username} and C{password}
        which are L{unicode}.
        r�Zbar�r�r'�r�r-r r r"�&test_honorsESMTPArgumentsUnicodeUserPW:sz4SendmailTests.test_honorsESMTPArgumentsUnicodeUserPWcCs|jddd�S)zu
        L{twisted.mail.smtp.sendmail} should accept C{username} and C{password}
        which are L{bytes}.
        r�r�r�r�r-r r r"�$test_honorsESMTPArgumentsBytesUserPWBsz2SendmailTests.test_honorsESMTPArgumentsBytesUserPWcCsBt�}td�}tjddd||d�|jdd}|�|j|�dS)	z|
        L{twisted.mail.smtp.sendmail} will pass through the message untouched
        if it is a file-like object.
        sFile!r6rr$�r
rrNN)rrrr�r�ZassertIs�file)r&r
�messageFiler�r r r"�test_messageFilePassthroughJs
�z)SendmailTests.test_messageFilePassthroughcCsLt�}tjdddd|d�|jdd}|j}|�d�|�|��d�dS)	z�
        L{twisted.mail.smtp.sendmail} will turn non-file-like objects
        (eg. strings) into file-like objects before sending.
        r6rr$r5r�rrNN)rrr�r�r��seekr{r-)r&r
r�r�r r r"�test_messageStringMadeFileXs
�
z(SendmailTests.test_messageStringMadeFilecCs<t�}tjdddd|dd�|jdd}|�|jd	�d
S)zk
        L{twisted.mail.smtp.sendmail} passes through the sender domain name, if
        provided.
        r6rr$r5r�)r
ZsenderDomainNamerrNr�N)rrr�r�r{r5)r&r
r�r r r"�test_senderDomainNamefs
�z#SendmailTests.test_senderDomainNamecCsPt�}tjdddd|d�}|��|�|jdjd�|�|�}|�t	j
�dS)	z�
        When a user cancels L{twisted.mail.smtp.sendmail} before the connection
        is made, the connection is closed by
        L{twisted.internet.interfaces.IConnector.disconnect}.
        r6rr$r5r�rTN)rrr��cancelr{Z
connectorsZ
_disconnected�failureResultOf�trapr�CancelledError)r&r
r��failurer r r"�test_cancelBeforeConnectionMaders
�
z-SendmailTests.test_cancelBeforeConnectionMadecCs�t�}t�}tjdddd|d�}|jdd}|�d�}|�|�|��|�|j	d	�|�|j
d	�|�|�}|�t
j�dS)
z�
        When a user cancels L{twisted.mail.smtp.sendmail} after the connection
        is made, the connection is closed by
        L{twisted.internet.interfaces.ITransport.abortConnection}.
        r6rr$r5r�rrNNT)rr�rr�r�rrvr�r{r�r�r�r�rr�)r&r
rrr�r�r�r�r r r"�test_cancelAfterConnectionMade�s
�


z,SendmailTests.test_cancelAfterConnectionMadeN)
r0r1r2r3r�r�r�r�r�r�r�r�r�r r r r"r�s	r�)jr3Z
__future__rrr�r�iorZzope.interfacerrZtwisted.python.utilrZ
twisted.trialrZtwisted.protocolsr	r
Ztwisted.internetrrr
rrrrZtwisted.test.proto_helpersrrrmrZtwisted.cred.errorZtwisted.cred.portalZtwisted.cred.checkersZtwisted.cred.credentialsrrrrrrZtwisted.mailrZtwisted.mail._credrZtwisted.test.ssl_helpersrr�ImportErrorrr7r#ZIMessagerfr$r4rCrOrLrar_rdrcrfZLineReceiverriZTestCaserur�r�r�r�r�r�r�r�r�Zcheckersr�r�r�r�r�r�r�rZIReactorSSLrnr�rrrr3r1r4r?rIrLrPrir�r�r�r�r�r�r r r r"�<module>s�
"t\
EH


!'<Sl'O