B
    u9aÊ  ã               @   sN  d dl Z d dlZd dlZd dlmZ d dlmZ d dl	Zd dl
Z
d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlmZmZ d dlmZmZmZ d dlmZ ej dkrêdd	„ Z!e!ej"_!d
d„ Z#G dd„ dej$ƒZ%dd„ Z&dZ'dZ(G dd„ dej$ƒZ)G dd„ dej$ƒZ*G dd„ dej$ƒZ+G dd„ dej$ƒZ,G dd„ dej$ƒZ-ddddœZ.d Z/d!Z0d"d#gd$gd%œZ1G d&d'„ d'e2ƒZ3G d(d)„ d)ej"ƒZ4G d*d+„ d+ej5ƒZ6G d,d-„ d-ej$ƒZ7G d.d/„ d/e6ƒZ8G d0d1„ d1ej$ƒZ9ed2d3d4Z:G d5d6„ d6e4ƒZ;G d7d8„ d8e6ƒZ<G d9d:„ d:ej$ƒZ=e>d;krJe ?¡  dS )<é    N)ÚEmailMessage)Úbody_encode)ÚsupportÚmock_socket)ÚHOSTÚHOSTv4ÚHOSTv6)ÚMockÚdarwinc             C   s   d S )N© )Úselfr   r   ú"/usr/lib/python3.7/test_smtplib.pyÚhandle_expt   s    r   c       
      C   sª   |  ¡  |  ¡  z‚y| ¡ \}}W n tjk
r8   Y nXX d}xH|r†|dkr†t g |gg ¡\}}}|r|| |¡}	||	d … }|d8 }q@W | ¡  W d | ¡  |  ¡  X d S )Niô  r   é   )ZlistenÚsetZacceptÚsocketÚtimeoutÚselectÚsendÚclose)
ÚevtÚbufÚservÚconnÚaddrÚnÚrÚwÚeZsentr   r   r   Úserver"   s     
r   c               @   sl   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ ZdS )ÚGeneralTestsc             C   s   t t_d| _d S )Né   )r   Úsmtplibr   Úport)r   r   r   r   ÚsetUp:   s    zGeneralTests.setUpc             C   s
   t t_ d S )N)r   r"   )r   r   r   r   ÚtearDown>   s    zGeneralTests.tearDownc             C   s   d}d}|   |t |¡¡ d S )Nzabc
.jklfoo
..bluezabc
..jkl
foo
...blue)ÚassertEqualr"   Z	quotedata)r   ZteststrÚexpectedr   r   r   ÚtestQuoteDataC   s    zGeneralTests.testQuoteDatac             C   s$   t  d¡ t t| j¡}| ¡  d S )Ns   220 Hola mundo)r   Ú
reply_withr"   ÚSMTPr   r#   r   )r   Úsmtpr   r   r   Ú
testBasic1H   s    
zGeneralTests.testBasic1c             C   s6   t  d¡ tjt| jdd}|  |jd¡ | ¡  d S )Ns   220 Hola mundo)z	127.0.0.1i¤M  )Úsource_address)	r   r)   r"   r*   r   r#   r&   r-   r   )r   r+   r   r   r   ÚtestSourceAddressN   s
    

zGeneralTests.testSourceAddressc             C   s*   t  d¡ t dt| jf ¡}| ¡  d S )Ns   220 Hola mundoz%s:%s)r   r)   r"   r*   r   r#   r   )r   r+   r   r   r   Ú
testBasic2V   s    
zGeneralTests.testBasic2c             C   s6   t  d¡ tjt| jdd}|  |jd¡ | ¡  d S )Ns   220 Hola mundoZtesthost)Úlocal_hostname)	r   r)   r"   r*   r   r#   r&   r0   r   )r   r+   r   r   r   ÚtestLocalHostName\   s    
zGeneralTests.testLocalHostNamec          	   C   sp   t  d¡ |  t  ¡ ¡ t  d¡ |  t  ¡ d¡ zt t| j	¡}W d t  d ¡ X |  |j
 ¡ d¡ | ¡  d S )Ns   220 Hola mundoé   )r   r)   ÚassertIsNoneÚgetdefaulttimeoutÚsetdefaulttimeoutr&   r"   r*   r   r#   ÚsockÚ
gettimeoutr   )r   r+   r   r   r   ÚtestTimeoutDefaultc   s    

zGeneralTests.testTimeoutDefaultc          	   C   sb   t  d¡ |  t ¡ ¡ t d¡ ztjt| j	d d}W d t d ¡ X |  |j
 ¡ ¡ | ¡  d S )Ns   220 Hola mundor2   )r   )r   r)   r3   r   r4   r5   r"   r*   r   r#   r6   r7   r   )r   r+   r   r   r   ÚtestTimeoutNoneo   s    

zGeneralTests.testTimeoutNonec             C   s:   t  d¡ tjt| jdd}|  |j ¡ d¡ | 	¡  d S )Ns   220 Hola mundor2   )r   )
r   r)   r"   r*   r   r#   r&   r6   r7   r   )r   r+   r   r   r   ÚtestTimeoutValuez   s    
zGeneralTests.testTimeoutValuec          	   C   sh   t  d¡ t ¡ }| d¡ t ¡ }| t| j	¡ W d Q R X | 
¡  t dtj¡}|  | ¡ |¡ d S )Ns   220 Hello worldr   z	^connect:)r   r)   r"   r*   Úset_debuglevelr   Úcaptured_stderrÚconnectr   r#   r   ÚreÚcompileÚ	MULTILINEÚassertRegexÚgetvalue)r   r+   Ústderrr'   r   r   r   Útest_debuglevel€   s    


zGeneralTests.test_debuglevelc          	   C   sh   t  d¡ t ¡ }| d¡ t ¡ }| t| j	¡ W d Q R X | 
¡  t dtj¡}|  | ¡ |¡ d S )Ns   220 Hello worldé   z#^\d{2}:\d{2}:\d{2}\.\d{6} connect: )r   r)   r"   r*   r;   r   r<   r=   r   r#   r   r>   r?   r@   rA   rB   )r   r+   rC   r'   r   r   r   Útest_debuglevel_2Š   s    


zGeneralTests.test_debuglevel_2N)Ú__name__Ú
__module__Ú__qualname__r$   r%   r(   r,   r.   r/   r1   r8   r9   r:   rD   rF   r   r   r   r   r    8   s   
r    c          	   C   s°   |  ¡  zvyZttdƒrtj}ntj}d}x8tjr`|dkr`|dtjƒ | ¡ rV|  ¡  P |d8 }q*W W n t	j
k
rz   Y nX W d | ¡ sšt d¡ |  ¡  t ¡  |  ¡  X d S )NÚpolliè  r   g{®Gáz„?r   g      à?)r   Úhasattrr   ÚasyncoreZpoll2rJ   Z
socket_mapZis_setr   r   r   ÚtimeÚsleepZ	close_all)r   Úserv_evtÚ
client_evtZpoll_funr   r   r   r   Údebugging_server—   s&    


rQ   z&---------- MESSAGE FOLLOWS ----------
z&------------ END MESSAGE ------------
c               @   sÈ   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Zd*d+„ Zd,d-„ Zd.d/„ ZdS )0ÚDebuggingServerTestsNc             C   sÄ   t j| _tjt _tj| _t ¡ | _	| j	t_t
 ¡ | _t
 ¡ | _tj| _t ¡ t_tjtdfddd| _| jj  ¡ d d… \| _| _| j| j| jf}t
jt|d| _| j ¡  | j ¡  | j ¡  d S )Nr   )ÚnowhereéÿÿÿÿT)Údecode_datarE   )ÚtargetÚargs)r   ÚgetfqdnÚreal_getfqdnr   ÚsysÚstdoutÚ
old_stdoutÚioÚStringIOÚoutputÚ	threadingÚEventrO   rP   ÚsmtpdÚDEBUGSTREAMÚold_DEBUGSTREAMZDebuggingServerr   r   ÚgetsocknameÚhostr#   ÚThreadrQ   ÚthreadÚstartÚwaitÚclear)r   Ú	serv_argsr   r   r   r$   Ã   s"    






zDebuggingServerTests.setUpc             C   sD   | j t_| j ¡  | j ¡  | j ¡  | j	t
_tj ¡  | jt_d S )N)rY   r   rX   rP   r   rO   rj   rh   Újoinr\   rZ   r[   rb   rc   r   rd   )r   r   r   r   r%   Ý   s    



zDebuggingServerTests.tearDownc             C   s$   | j  ¡ }tjdd|tjtjB dS )Nz(.*?)^X-Peer:\s*\S+\n(.*)z\1\2)Úflags)r_   rB   r>   Úsubr@   ÚDOTALL)r   Útest_outputr   r   r   Úget_output_without_xpeerê   s    
z-DebuggingServerTests.get_output_without_xpeerc             C   s    t jt| jddd}| ¡  d S )NÚ	localhosté   )r0   r   )r"   r*   r   r#   Úquit)r   r+   r   r   r   Ú	testBasicï   s    zDebuggingServerTests.testBasicc          
   C   s˜   t  ¡ }yLtj| j| jdd| j|fd}|  |j| j|f¡ |  |jd¡ | 	¡  W n> t
k
r’ } z |jtjkr€|  d| ¡ ‚ W d d }~X Y nX d S )Nrs   rt   )r0   r   r-   zcouldn't bind to source port %d)r   Zfind_unused_portr"   r*   rf   r#   r&   r-   r0   ru   ÚOSErrorÚerrnoZ
EADDRINUSEZskipTest)r   Zsrc_portr+   r   r   r   r   r.   ô   s    z&DebuggingServerTests.testSourceAddressc             C   s4   t jt| jddd}d}|  | ¡ |¡ | ¡  d S )Nrs   rt   )r0   r   )éú   s   OK)r"   r*   r   r#   r&   Únoopru   )r   r+   r'   r   r   r   ÚtestNOOP  s    zDebuggingServerTests.testNOOPc             C   s4   t jt| jddd}d}|  | ¡ |¡ | ¡  d S )Nrs   rt   )r0   r   )ry   s   OK)r"   r*   r   r#   r&   Zrsetru   )r   r+   r'   r   r   r   ÚtestRSET  s    zDebuggingServerTests.testRSETc             C   s4   t jt| jddd}d}|  | ¡ |¡ | ¡  d S )Nrs   rt   )r0   r   )ry   s   
SIZE 33554432
HELP)r"   r*   r   r#   r&   Úehloru   )r   r+   r'   r   r   r   ÚtestELHO  s    zDebuggingServerTests.testELHOc             C   s>   t jt| jddd}d}| d¡ |  | ¡ |¡ | ¡  d S )Nrs   rt   )r0   r   )iö  s   EXPN not implementedZEXPN)r"   r*   r   r#   Zputcmdr&   Zgetreplyru   )r   r+   r'   r   r   r   ÚtestEXPNNotImplemented  s
    
z+DebuggingServerTests.testEXPNNotImplementedc             C   sH   t jt| jddd}d}|  | d¡|¡ |  | d¡|¡ | ¡  d S )Nrs   rt   )r0   r   )éü   s>   Cannot VRFY user, but will accept message and attempt deliveryznobody@nowhere.com)r"   r*   r   r#   r&   ÚvrfyZverifyru   )r   r+   r'   r   r   r   ÚtestVRFY  s
    zDebuggingServerTests.testVRFYc             C   s<   t jt| jddd}| ¡  d}|  | ¡ |¡ | ¡  d S )Nrs   rt   )r0   r   )i÷  s   Duplicate HELO/EHLO)r"   r*   r   r#   Zhelor&   ru   )r   r+   r'   r   r   r   ÚtestSecondHELO%  s
    z#DebuggingServerTests.testSecondHELOc             C   s0   t jt| jddd}|  | ¡ d¡ | ¡  d S )Nrs   rt   )r0   r   s@   Supported commands: EHLO HELO MAIL RCPT DATA RSET NOOP QUIT VRFY)r"   r*   r   r#   r&   Úhelpru   )r   r+   r   r   r   ÚtestHELP.  s    zDebuggingServerTests.testHELPc             C   sz   d}t jt| jddd}| dd|¡ t d¡ | ¡  | j 	¡  | j
 ¡  | j ¡  dt|tf }|  | j ¡ |¡ d S )	NzA test messagers   rt   )r0   r   ÚJohnÚSallyg{®Gáz„?z%s%s
%s)r"   r*   r   r#   ÚsendmailrM   rN   ru   rP   r   rO   rj   r_   ÚflushÚ	MSG_BEGINÚMSG_ENDr&   rB   )r   Úmr+   Úmexpectr   r   r   ÚtestSend4  s    



zDebuggingServerTests.testSendc             C   s€   d}t jt| jddd}| dd|¡ t d¡ | ¡  | j 	¡  | j
 ¡  | j ¡  dt| d	¡tf }|  | j ¡ |¡ d S )
Ns   A test messagers   rt   )r0   r   r†   r‡   g{®Gáz„?z%s%s
%sÚascii)r"   r*   r   r#   rˆ   rM   rN   ru   rP   r   rO   rj   r_   r‰   rŠ   Údecoder‹   r&   rB   )r   rŒ   r+   r   r   r   r   ÚtestSendBinaryE  s    



z#DebuggingServerTests.testSendBinaryc             C   sz   d}t jt| jddd}| dd|¡ t d¡ | ¡  | j 	¡  | j
 ¡  | j ¡  dt|tf }|  | j ¡ |¡ d S )	Nz.A test
.mes.sage.rs   rt   )r0   r   r†   r‡   g{®Gáz„?z%s%s
%s)r"   r*   r   r#   rˆ   rM   rN   ru   rP   r   rO   rj   r_   r‰   rŠ   r‹   r&   rB   )r   rŒ   r+   r   r   r   r   ÚtestSendNeedingDotQuoteS  s    



z,DebuggingServerTests.testSendNeedingDotQuotec             C   sž   d}t jt| jddd}| dd|¡ t d¡ | ¡  | j 	¡  | j
 ¡  | j ¡  dt|tf }|  | j ¡ |¡ tj ¡ }t d	tj¡}|  ||¡ d S )
NzA test messagers   rt   )r0   r   z<>r‡   g{®Gáz„?z%s%s
%sz^sender: <>$)r"   r*   r   r#   rˆ   rM   rN   ru   rP   r   rO   rj   r_   r‰   rŠ   r‹   r&   rB   rb   rc   r>   r?   r@   rA   )r   rŒ   r+   r   ÚdebugoutÚsenderr   r   r   ÚtestSendNullSenderb  s    




z'DebuggingServerTests.testSendNullSenderc             C   s’   t jj d¡}tjt| jddd}|j|ddd t	 
d¡ | ¡  | j ¡  | j ¡  | j ¡  |  ¡ }|d	= d
t| ¡ tf }|  ||¡ d S )NzA test messagers   rt   )r0   r   r†   r‡   )Ú	from_addrÚto_addrsg{®Gáz„?zX-Peerz%s%s
%s)ÚemailÚmimeÚtextÚMIMETextr"   r*   r   r#   Úsend_messagerM   rN   ru   rP   r   rO   rj   r_   r‰   rr   rŠ   Ú	as_stringr‹   r&   )r   rŒ   r+   rq   r   r   r   r   ÚtestSendMessages  s    



z$DebuggingServerTests.testSendMessagec       	      C   s  t jj d¡}d|d< d|d< d|d< d|d	< tjt| jd
dd}| |¡ t	 
d¡ | ¡  |  |d	 d¡ | j ¡  | j ¡  | j ¡  |  ¡ }|d= |d	= dt| ¡ tf }|  ||¡ tj ¡ }t dtj¡}|  ||¡ x,dD ]$}t d |¡tj¡}|  ||¡ qèW d S )NzA test messagezfoo@bar.comÚFromr†   ÚTozSally, FredÚCCz?John Root <root@localhost>, "Dinsdale" <warped@silly.walks.com>ÚBccrs   rt   )r0   r   g{®Gáz„?zX-Peerz%s%s
%sz^sender: foo@bar.com$)r†   r‡   ZFredzroot@localhostzwarped@silly.walks.comz^recips: .*'{}'.*$)r˜   r™   rš   r›   r"   r*   r   r#   rœ   rM   rN   ru   r&   rP   r   rO   rj   r_   r‰   rr   rŠ   r   r‹   rb   rc   rB   r>   r?   r@   rA   Úformat)	r   rŒ   r+   rq   r   r“   r”   r   Úto_addrr   r   r   ÚtestSendMessageWithAddresses‡  s2    






z1DebuggingServerTests.testSendMessageWithAddressesc       	      C   sî   t jj d¡}d|d< d|d< tjt| jddd}| |¡ t	 
d	¡ | ¡  | j ¡  | j ¡  | j ¡  |  ¡ }|d
= dt| ¡ tf }|  ||¡ tj ¡ }t dtj¡}|  ||¡ x,dD ]$}t d |¡tj¡}|  ||¡ qÂW d S )NzA test messagezfoo@bar.comrŸ   zJohn, Dinsdaler    rs   rt   )r0   r   g{®Gáz„?zX-Peerz%s%s
%sz^sender: foo@bar.com$)r†   ÚDinsdalez^recips: .*'{}'.*$)r˜   r™   rš   r›   r"   r*   r   r#   rœ   rM   rN   ru   rP   r   rO   rj   r_   r‰   rr   rŠ   r   r‹   r&   rb   rc   rB   r>   r?   r@   rA   r£   )	r   rŒ   r+   rq   r   r“   r”   r   r¤   r   r   r   Ú testSendMessageWithSomeAddresses©  s*    






z5DebuggingServerTests.testSendMessageWithSomeAddressesc       
      C   s  t jj d¡}d|d< d|d< tjt| jddd}|j|d	d
d t	 
d¡ | ¡  | j ¡  | j ¡  | j ¡  |  ¡ }|d= dt| ¡ tf }|  ||¡ tj ¡ }t dtj¡}|  ||¡ x,dD ]$}t d |¡tj¡}|  ||¡ qÈW t dtj¡}	|  ||	¡ d S )NzA test messagezfoo@bar.comrŸ   zJohn, Dinsdaler    rs   rt   )r0   r   zjoe@example.comzfoo@example.net)r–   r—   g{®Gáz„?zX-Peerz%s%s
%sz^sender: joe@example.com$)r†   r¦   z^recips: .*'{}'.*$z^recips: .*'foo@example.net'.*$) r˜   r™   rš   r›   r"   r*   r   r#   rœ   rM   rN   ru   rP   r   rO   rj   r_   r‰   rr   rŠ   r   r‹   r&   rb   rc   rB   r>   r?   r@   rA   r£   ZassertNotRegex)
r   rŒ   r+   rq   r   r“   r”   r   r¤   Zrecipr   r   r   Ú%testSendMessageWithSpecifiedAddressesÄ  s.    





z:DebuggingServerTests.testSendMessageWithSpecifiedAddressesc       	      C   sö   t jj d¡}d|d< d|d< d|d< tjt| jdd	d
}| |¡ t	 
d¡ | ¡  | j ¡  | j ¡  | j ¡  |  ¡ }|d= dt| ¡ tf }|  ||¡ tj ¡ }t dtj¡}|  ||¡ x,dD ]$}t d |¡tj¡}|  ||¡ qÊW d S )NzA test messagezBernard, BiancarŸ   z#the_rescuers@Rescue-Aid-Society.comZSenderzJohn, Dinsdaler    rs   rt   )r0   r   g{®Gáz„?zX-Peerz%s%s
%sz-^sender: the_rescuers@Rescue-Aid-Society.com$)r†   r¦   z^recips: .*'{}'.*$)r˜   r™   rš   r›   r"   r*   r   r#   rœ   rM   rN   ru   rP   r   rO   rj   r_   r‰   rr   rŠ   r   r‹   r&   rb   rc   rB   r>   r?   r@   rA   r£   )	r   rŒ   r+   rq   r   r“   r”   r   r¤   r   r   r   ÚtestSendMessageWithMultipleFromá  s,    






z4DebuggingServerTests.testSendMessageWithMultipleFromc       	      C   s*  t jj d¡}d|d< d|d< d|d< d|d	< d
|d< d|d< d|d< d|d< tjt| jddd}| |¡ t	 
d¡ | ¡  | j ¡  | j ¡  | j ¡  |d	= |d= |  ¡ }|d= dt| ¡ tf }|  ||¡ tj ¡ }t dtj¡}|  ||¡ x,dD ]$}t d |¡tj¡}|  ||¡ qþW d S )NzA test messagezfoo@bar.comrŸ   r†   r    zSally, Fredr¡   z?John Root <root@localhost>, "Dinsdale" <warped@silly.walks.com>r¢   zThu, 1 Jan 1970 17:42:00 +0000zResent-Datezholy@grail.netzResent-Fromz&Martha <my_mom@great.cooker.com>, Jeffz	Resent-Tozdoe@losthope.netz
Resent-Bccrs   rt   )r0   r   g{®Gáz„?zX-Peerz%s%s
%sz^sender: holy@grail.net$)zmy_mom@great.cooker.comZJeffzdoe@losthope.netz^recips: .*'{}'.*$)r˜   r™   rš   r›   r"   r*   r   r#   rœ   rM   rN   ru   rP   r   rO   rj   r_   r‰   rr   rŠ   r   r‹   r&   rb   rc   rB   r>   r?   r@   rA   r£   )	r   rŒ   r+   rq   r   r“   r”   r   r¤   r   r   r   ÚtestSendMessageResentý  s:    






z*DebuggingServerTests.testSendMessageResentc          	   C   s¦   t jj d¡}d|d< d|d< d|d< d|d	< d
|d< d|d< d|d< d|d< d|d< d|d< d|d< tjt| jddd}|  t	¡ | 
|¡ W d Q R X | ¡  d S )NzA test messagezfoo@bar.comrŸ   r†   r    zSally, Fredr¡   z?John Root <root@localhost>, "Dinsdale" <warped@silly.walks.com>r¢   zThu, 1 Jan 1970 17:42:00 +0000zResent-Datezholy@grail.netzResent-Fromz&Martha <my_mom@great.cooker.com>, Jeffz	Resent-Tozdoe@losthope.netz
Resent-BcczThu, 2 Jan 1970 17:42:00 +0000rs   rt   )r0   r   )r˜   r™   rš   r›   r"   r*   r   r#   ÚassertRaisesÚ
ValueErrorrœ   r   )r   rŒ   r+   r   r   r   Ú#testSendMessageMultipleResentRaises   s     z8DebuggingServerTests.testSendMessageMultipleResentRaises)rG   rH   rI   ÚmaxDiffr$   r%   rr   rv   r.   r{   r|   r~   r   r‚   rƒ   r…   rŽ   r‘   r’   r•   rž   r¥   r§   r¨   r©   rª   r­   r   r   r   r   rR   ¿   s0   	"#rR   c               @   s   e Zd Zdd„ Zdd„ ZdS )ÚNonConnectingTestsc             C   s.   t  ¡ }|  t j|j¡ |  t j|jd¡ d S )Nztest msg)r"   r*   r«   ÚSMTPServerDisconnectedr}   r   )r   r+   r   r   r   ÚtestNotConnected4  s    z#NonConnectingTests.testNotConnectedc             C   s&   |   ttjdd¡ |   ttjd¡ d S )Nrs   Zboguszlocalhost:bogus)r«   rw   r"   r*   )r   r   r   r   ÚtestNonnumericPort>  s    

z%NonConnectingTests.testNonnumericPortN)rG   rH   rI   r±   r²   r   r   r   r   r¯   2  s   
r¯   c               @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚDefaultArgumentsTestsc             C   sD   t ƒ | _d| jd< t ¡ | _tdd| j_tƒ tƒ  | j_| j_d S )Nu   PÃ¡olo <fÅ‘o@bar.com>rŸ   )éÈ   ZOK)Zreturn_value)	r   Úmsgr"   r*   r+   r	   r}   Úhas_extnrˆ   )r   r   r   r   r$   H  s
    

zDefaultArgumentsTests.setUpc             C   s`   d}| j  | j¡ | j  | j¡ |  | j jjd d d |¡ |  | j jjd d d |¡ d S )N)ÚSMTPUTF8zBODY=8BITMIMEr   rt   r   )r+   rœ   rµ   r&   rˆ   Úcall_args_list)r   Úexpected_mail_optionsr   r   r   rž   O  s    z%DefaultArgumentsTests.testSendMessagec             C   sN   dg}d}| j  | jd d |¡ |  |dg¡ |  | j jjd d d |¡ d S )NÚSTARTTLS)rº   r·   zBODY=8BITMIMEr   rt   )r+   rœ   rµ   r&   rˆ   r¸   )r   Úmail_optionsr¹   r   r   r   ÚtestSendMessageWithMailOptionsX  s    z4DefaultArgumentsTests.testSendMessageWithMailOptionsN)rG   rH   rI   r$   rž   r¼   r   r   r   r   r³   F  s   	r³   c               @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚBadHELOServerTestsc             C   s4   t t_t  d¡ tj| _t ¡ | _	| j	t_d| _
d S )Ns   199 no hello for you!r!   )r   r"   r   r)   rZ   r[   r\   r]   r^   r_   r#   )r   r   r   r   r$   d  s    

zBadHELOServerTests.setUpc             C   s   t t_ | jt_d S )N)r   r"   r\   rZ   r[   )r   r   r   r   r%   l  s    zBadHELOServerTests.tearDownc             C   s   |   tjtjt| jdd¡ d S )Nrs   rt   )r«   r"   ZSMTPConnectErrorr*   r   r#   )r   r   r   r   ÚtestFailingHELOp  s    z"BadHELOServerTests.testFailingHELON)rG   rH   rI   r$   r%   r¾   r   r   r   r   r½   b  s   r½   c               @   s:   e Zd Zddej d  d Zdd„ Zdd„ Zd	d
„ ZdS )ÚTooLongLineTestss   250 OKó   .rE   ó   
c             C   sš   t j| _t ¡ | _| jt _t ¡ | _t	 	t	j
t	j¡| _| j d¡ t | j¡| _| j| j| jf}tjt|d}| ¡  |  |j¡ | j ¡  | j ¡  d S )Né   )rV   rW   )rZ   r[   r\   r]   r^   r_   r`   ra   r   r   ZAF_INETZSOCK_STREAMr6   Z
settimeoutr   Z	bind_portr#   Úrespdatarg   r   ri   Ú
addCleanuprm   rj   rk   )r   Zservargsrh   r   r   r   r$   x  s    


zTooLongLineTests.setUpc             C   s   | j  ¡  | jt_d S )N)r   rj   r\   rZ   r[   )r   r   r   r   r%   ˆ  s    
zTooLongLineTests.tearDownc             C   s   |   tjtjt| jdd¡ d S )Nrs   rt   )r«   r"   ÚSMTPResponseExceptionr*   r   r#   )r   r   r   r   ÚtestLineTooLongŒ  s    z TooLongLineTests.testLineTooLongN)	rG   rH   rI   r"   Z_MAXLINErÃ   r$   r%   rÆ   r   r   r   r   r¿   u  s   r¿   zJohn AzSally BzRuth C)zMr.A@somewhere.comzMs.B@xn--fo-fka.comzMrs.C@somewhereesle.com)zMr.A@somewhere.comZsomepasswordz<PENCeUxFREJoU0NnbmhNWitOMjNGNndAZWx3b29kLmlubm9zb2Z0LmNvbT4=zMr.A@somewhere.comzMrs.C@somewhereesle.comzMs.B@xn--fo-fka.com)zlist-1zlist-2c               @   s   e Zd ZdS )ÚResponseExceptionN)rG   rH   rI   r   r   r   r   rÇ   ž  s    rÇ   c                   sÞ   e Zd ZdZdZdZdZdZdZdZ	dZ
dZ‡ fdd„Z‡ fdd„Zdd	„ Zd
d„ Zdd„ Zd&dd„Zd'dd„Zd(dd„Zdd„ Zdd„ Zdd„ Z‡ fdd„Z‡ fdd„Z‡ fdd„Z‡ fd d!„Z‡ fd"d#„Zd$d%„ Z‡  ZS ))ÚSimSMTPChannelNr   éc   c                s,   d  dd„ |D ƒ¡| _tt| ƒj||Ž d S )NÚ c             S   s   g | ]}d   |¡‘qS )z	250-{0}
)r£   )Ú.0Úxr   r   r   ú
<listcomp>­  s    z+SimSMTPChannel.__init__.<locals>.<listcomp>)rm   Ú_extrafeaturesÚsuperrÈ   Ú__init__)r   Zextra_featuresrW   Úkw)Ú	__class__r   r   rÐ   «  s    zSimSMTPChannel.__init__c          
      s’   | j | jkr„| j | j¡}tdt|ƒtjd g | _y|  	|¡ W n@ t
k
r‚ } z"| j| _ |  d|j|jf ¡ d S d }~X Y nX tƒ  ¡  d S )NzData:)Úfilez%s %s)Ú
smtp_stateÚAUTHZ_emptystringrm   Zreceived_linesÚprintÚreprrb   rc   Úauth_objectrÇ   ÚCOMMANDÚpushÚ	smtp_codeÚ
smtp_errorrÏ   Úfound_terminator)r   Úliner   )rÒ   r   r   rÝ   ±  s    zSimSMTPChannel.found_terminatorc             C   sä   | j s|  d¡ d S | jr$d| jkr2|  d¡ d S | jd k	rJ|  d¡ d S | ¡ }t|ƒdkrl|  d¡ d S d|d  ¡  d	d
¡ }yt	| |ƒ| _
W n$ tk
r¸   |  d |¡¡ d S X | j| _|  
t|ƒdkrÚ|d nd ¡ d S )Nz503 Error: send EHLO firstrÕ   z(500 Error: command "AUTH" not recognizedz3503 Bad sequence of commands: already authenticated)r   rE   z/501 Syntax: AUTH <mechanism> [initial-response]z_auth_%sr   ú-Ú_zQ504 Command parameter not implemented: unsupported  authentication mechanism {!r}rE   r   )Úseen_greetingrÚ   Úextended_smtprÎ   Úauthenticated_userÚsplitÚlenÚlowerÚreplaceÚgetattrrØ   ÚAttributeErrorr£   rÕ   rÔ   )r   ÚargrW   Zauth_object_namer   r   r   Ú	smtp_AUTH¿  s.    



zSimSMTPChannel.smtp_AUTHc             C   s,   |r|| _ |  d¡ n
|  d¡ | j| _d S )Nz235 Authentication Succeededz&535 Authentication credentials invalid)rã   rÚ   rÙ   rÔ   )r   ÚuserZvalidr   r   r   Ú_authenticatedØ  s
    
zSimSMTPChannel._authenticatedc             C   s   t  | d¡¡ d¡S )Nr   zutf-8)Úbase64ZdecodebytesÚencoder   )r   Ústringr   r   r   Ú_decode_base64à  s    zSimSMTPChannel._decode_base64c          
   C   s‚   |d kr|   d¡ nj|  |¡}y| d¡^ }}}W n4 tk
rh } z|   d ||¡¡ d S d }~X Y nX |  ||td k¡ d S )Nz334 ú z=535 Splitting response {!r} into user and password failed: {}r   )rÚ   rñ   rä   r¬   r£   rí   Úsim_auth)r   rê   Úlogpassrà   rì   Úpasswordr   r   r   r   Ú_auth_plainã  s    

zSimSMTPChannel._auth_plainc             C   s^   |d kr|   d¡ nFt| dƒs6|  |¡| _|   d¡ n$|  |¡}|  | j|td k¡ | `d S )Nz334 VXNlcm5hbWU6Ú_auth_login_userz334 UGFzc3dvcmQ6r   )rÚ   rK   rñ   r÷   rí   ró   )r   rê   rõ   r   r   r   Ú_auth_loginð  s    

zSimSMTPChannel._auth_loginc          
   C   s¦   |d kr|   d t¡¡ nˆ|  |¡}y| ¡ \}}W n4 tk
rh } z|   d ||¡¡ dS d }~X Y nX t td  	d¡|  t¡ 	d¡d¡ 
¡ }|  |||k¡ d S )Nz334 {}z=535 Splitting response {!r} into user and password failed: {}Fr   r   Zmd5)rÚ   r£   Úsim_cram_md5_challengerñ   rä   r¬   ÚhmacZHMACró   rï   Z	hexdigestrí   )r   rê   rô   rì   Zhashed_passr   Zvalid_hashed_passr   r   r   Ú_auth_cram_md5ý  s    


zSimSMTPChannel._auth_cram_md5c             C   s,   d}|| j  d }|  |¡ || _d| _d S )NzH250-testhost
250-EXPN
250-SIZE 20000000
250-STARTTLS
250-DELIVERBY
z250 HELPT)rÎ   rÚ   rá   râ   )r   rê   Úrespr   r   r   Ú	smtp_EHLO  s
    
zSimSMTPChannel.smtp_EHLOc             C   s8   |t kr&|  dt | t |¡f ¡ n|  d| ¡ d S )Nz	250 %s %sz550 No such user: %s)Ú	sim_usersrÚ   r"   Ú	quoteaddr)r   rê   r   r   r   Ú	smtp_VRFY  s    zSimSMTPChannel.smtp_VRFYc             C   s†   |  ¡ }|tkrxt| }xht|ƒD ]P\}}t |¡}|t|ƒd k r\|  dt| |f ¡ q"|  dt| |f ¡ q"W n
|  d¡ d S )Nr   z	250-%s %sz	250 %s %sz550 No access for you!)ræ   Ú	sim_listsÚ	enumerater"   rÿ   rå   rÚ   rþ   )r   rê   Z	list_nameZ	user_listr   Z
user_emailZquoted_addrr   r   r   Ú	smtp_EXPN!  s    
zSimSMTPChannel.smtp_EXPNc                s4   | j d krtt| ƒ |¡ n|  | j ¡ |  ¡  d S )N)Úquit_responserÏ   rÈ   Ú	smtp_QUITrÚ   Úclose_when_done)r   rê   )rÒ   r   r   r  .  s    
zSimSMTPChannel.smtp_QUITc                s6   | j d krtƒ  |¡ n|  | j ¡ | jr2|  ¡  d S )N)Úmail_responserÏ   Ú	smtp_MAILrÚ   Ú
disconnectr  )r   rê   )rÒ   r   r   r  5  s
    
zSimSMTPChannel.smtp_MAILc                sB   | j d krtƒ  |¡ d S |  jd7  _|  | j | jd  ¡ d S )Nr   )Úrcpt_responserÏ   Ú	smtp_RCPTÚ
rcpt_countrÚ   )r   rê   )rÒ   r   r   r  =  s
    
zSimSMTPChannel.smtp_RCPTc                s   |  j d7  _ tƒ  |¡ d S )Nr   )Ú
rset_countrÏ   Ú	smtp_RSET)r   rê   )rÒ   r   r   r  D  s    zSimSMTPChannel.smtp_RSETc                s(   | j d krtƒ  |¡ n|  | j ¡ d S )N)Údata_responserÏ   Ú	smtp_DATArÚ   )r   rê   )rÒ   r   r   r  H  s    
zSimSMTPChannel.smtp_DATAc              C   s   ‚ d S )Nr   )r   r   r   r   Úhandle_errorN  s    zSimSMTPChannel.handle_error)N)N)N)rG   rH   rI   r  r  r
  r  r  r  r	  rÕ   rã   rÐ   rÝ   rë   rí   rñ   rö   rø   rû   rý   r   r  r  r  r  r  r  r  Ú__classcell__r   r   )rÒ   r   rÈ   Ÿ  s4   


rÈ   c               @   s8   e Zd ZeZdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Z	dS )ÚSimSMTPServerc             O   s$   g | _ i | _tjj| f|ž|Ž d S )N)Ú_extra_featuresÚ
_addressesrb   Ú
SMTPServerrÐ   )r   rW   rÑ   r   r   r   rÐ   V  s    zSimSMTPServer.__init__c             C   s   | j | j| ||| jd| _d S )N)rU   )Úchannel_classr  Ú_decode_dataÚ_SMTPchannel)r   r   r   r   r   r   Úhandle_accepted[  s    
zSimSMTPServer.handle_acceptedc             C   s   || j d< || j d< d S )NÚfromÚtos)r  )r   ÚpeerÚmailfromÚrcpttosÚdatar   r   r   Úprocess_message`  s    
zSimSMTPServer.process_messagec             C   s   | j  |¡ d S )N)r  Úappend)r   Zfeaturer   r   r   Úadd_featured  s    zSimSMTPServer.add_featurec              C   s   ‚ d S )Nr   )r   r   r   r   r  g  s    zSimSMTPServer.handle_errorN)
rG   rH   rI   rÈ   r  rÐ   r  r!  r#  r  r   r   r   r   r  R  s   r  c               @   s¼   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zd-S ).ÚSMTPSimTestsc             C   sŒ   t j| _tjt _t ¡ | _t ¡ | _tt	dfddd| _
| j
j  ¡ d | _| j
| j| jf}tjt|d| _| j ¡  | j ¡  | j ¡  d S )Nr   )rS   rT   T)rU   r   )rV   rW   )r   rX   rY   r   r`   ra   rO   rP   r  r   r   re   r#   rg   rQ   rh   ri   rj   rk   )r   rl   r   r   r   r$   o  s    



zSMTPSimTests.setUpc             C   s*   | j t_| j ¡  | j ¡  | j ¡  d S )N)	rY   r   rX   rP   r   rO   rj   rh   rm   )r   r   r   r   r%   €  s    

zSMTPSimTests.tearDownc             C   s    t jt| jddd}| ¡  d S )Nrs   rÂ   )r0   r   )r"   r*   r   r#   ru   )r   r+   r   r   r   rv   ˆ  s    zSMTPSimTests.testBasicc             C   s‚   t jt| jddd}|  |ji ¡ ddddddœ}| ¡  |  |j|¡ x|D ]}|  | |¡¡ qNW |  	| d¡¡ | 
¡  d S )Nrs   rÂ   )r0   r   rÊ   Z20000000)ÚexpnÚsizeZstarttlsZ	deliverbyr„   zunsupported-feature)r"   r*   r   r#   r&   Úesmtp_featuresr}   Ú
assertTruer¶   ÚassertFalseru   )r   r+   Zexpected_featuresÚkr   r   r   ÚtestEHLO  s    
zSMTPSimTests.testEHLOc             C   sŒ   t jt| jddd}xBt ¡ D ]6\}}dtd|t  |¡f dƒf}|  | 	|¡|¡ qW d}dd	|  
d¡f}|  | 	|¡|¡ | ¡  d S )
Nrs   rÂ   )r0   r   ry   z%s %sr   znobody@nowhere.comi&  zNo such user: %s)r"   r*   r   r#   rþ   ÚitemsÚbytesrÿ   r&   r   rï   ru   )r   r+   Z	addr_specÚnameÚexpected_knownÚuÚexpected_unknownr   r   r   r‚   ¢  s    zSMTPSimTests.testVRFYc       	   	   C   s¤   t jt| jddd}xht ¡ D ]\\}}g }x(|D ] }| dt| t  |¡f ¡ q0W dt	d 
|¡dƒf}|  | |¡|¡ qW d}d	}|  | |¡|¡ | ¡  d S )
Nrs   rÂ   )r0   r   z%s %sry   Ú
r   zPSU-Members-List)i&  s   No access for you!)r"   r*   r   r#   r  r,  r"  rþ   rÿ   r-  rm   r&   r%  ru   )	r   r+   ZlistnameÚmembersZusersrŒ   r/  r0  r1  r   r   r   ÚtestEXPN°  s    
 zSMTPSimTests.testEXPNc             C   sL   | j  d¡ tjt| jddd}| td td ¡}|  |d¡ | 	¡  d S )Nz
AUTH PLAINrs   rÂ   )r0   r   r   r   )éë   s   Authentication Succeeded)
r   r#  r"   r*   r   r#   Úloginró   r&   r   )r   r+   rü   r   r   r   ÚtestAUTH_PLAIN¿  s
    zSMTPSimTests.testAUTH_PLAINc             C   sL   | j  d¡ tjt| jddd}| td td ¡}|  |d¡ | 	¡  d S )Nz
AUTH LOGINrs   rÂ   )r0   r   r   r   )r5  s   Authentication Succeeded)
r   r#  r"   r*   r   r#   r6  ró   r&   r   )r   r+   rü   r   r   r   ÚtestAUTH_LOGINÆ  s
    zSMTPSimTests.testAUTH_LOGINc             C   sL   | j  d¡ tjt| jddd}| td td ¡}|  |d¡ | 	¡  d S )NzAUTH CRAM-MD5rs   rÂ   )r0   r   r   r   )r5  s   Authentication Succeeded)
r   r#  r"   r*   r   r#   r6  ró   r&   r   )r   r+   rü   r   r   r   ÚtestAUTH_CRAM_MD5Í  s
    zSMTPSimTests.testAUTH_CRAM_MD5c             C   sL   | j  d¡ tjt| jddd}| td td ¡}|  |d¡ | 	¡  d S )NzAUTH BOGUS PLAIN LOGIN CRAM-MD5rs   rÂ   )r0   r   r   r   )r5  s   Authentication Succeeded)
r   r#  r"   r*   r   r#   r6  ró   r&   r   )r   r+   rü   r   r   r   ÚtestAUTH_multipleÔ  s
    zSMTPSimTests.testAUTH_multiplec          
   C   sÂ   dddh}x|D ]}| j  d |¡¡ qW x’|D ]Š}| j|dt tjt| jddd}| d	¡ t	d
 t	d  |_
|_d| ¡  dd¡ }| |t||ƒ¡}|  |d¡ | ¡  W d Q R X q0W d S )NzCRAM-MD5ZPLAINZLOGINzAUTH {})Ú	mechanismrs   rÂ   )r0   r   Zfoor   r   Zauth_rß   rà   )r5  s   Authentication Succeeded)r   r#  r£   ZsubTestr"   r*   r   r#   r}   ró   rì   rõ   ræ   rç   Úauthrè   r&   r   )r   Z	supportedr;  r+   Úmethodrü   r   r   r   Útest_auth_functionÜ  s    





zSMTPSimTests.test_auth_functionc             C   sŽ   t jt| jddd}| ¡ \}}|  |d¡ |  d|j¡ | ¡  |  	d|j¡ | 
t| j¡ |  	d|j¡ | ¡  |  d|j¡ | ¡  d S )Nrs   rÂ   )r0   r   ry   r&  )r"   r*   r   r#   r}   r&   ÚassertInr'  ru   ZassertNotInr=   Zehlo_or_helo_if_needed)r   r+   ÚcodeÚmessager   r   r   Útest_quit_resets_greetingë  s    
z&SMTPSimTests.test_quit_resets_greetingc          	   C   s|   t  t| j¡}| ¡ \}}|  |d¡ W d Q R X |  t j|jd¡ t  t| j¡}| 	¡  W d Q R X |  t j|jd¡ d S )Nry   s   foo)
r"   r*   r   r#   rz   r&   r«   r°   r   r   )r   r+   r@  rA  r   r   r   Útest_with_statementú  s    z SMTPSimTests.test_with_statementc          
   C   sh   |   tj¡2}t t| j¡}| ¡  d| jj_	W d Q R X W d Q R X |  
|jjd¡ |  
|jjd¡ d S )Nz421 QUIT FAILEDi¥  s   QUIT FAILED)r«   r"   rÅ   r*   r   r#   rz   r   r  r  r&   Ú	exceptionrÛ   rÜ   )r   Úerrorr+   r   r   r   Ú test_with_statement_QUIT_failure  s    z-SMTPSimTests.test_with_statement_QUIT_failurec          	   C   sf   t jt| jddd}| ¡  d| jj_d| jj_|  	t j
¡ | ddd¡ W d Q R X |  |j¡ d S )	Nrs   rÂ   )r0   r   z451 Requested action abortedTr†   r‡   ztest message)r"   r*   r   r#   rz   r   r  r  r	  r«   ÚSMTPSenderRefusedrˆ   r3   r6   )r   r+   r   r   r   Útest__rest_from_mail_cmd  s    

z%SMTPSimTests.test__rest_from_mail_cmdc          	   C   sn   t jt| jddd}| ¡  d| jj_|  t j	¡ | 
ddd¡ W d Q R X |  |j¡ |  | jjjd¡ d S )	Nrs   rÂ   )r0   r   z421 closing connectionr†   r‡   ztest messager   )r"   r*   r   r#   rz   r   r  r  r«   rG  rˆ   r3   r6   r&   r  )r   r+   r   r   r   Útest_421_from_mail_cmd  s    
z#SMTPSimTests.test_421_from_mail_cmdc          	   C   s   t jt| jddd}| ¡  ddg| jj_|  t j	¡}| 
dddd	gd
¡ W d Q R X |  |j¡ |  | jjjd¡ |  |jjd ddi¡ d S )Nrs   rÂ   )r0   r   z250 acceptedz421 closingr†   r‡   ZFrankZGeorgeztest messager   )i¥  s   closing)r"   r*   r   r#   rz   r   r  r
  r«   ZSMTPRecipientsRefusedrˆ   r3   r6   r&   r  ÚassertDictEqualrD  rW   )r   r+   r   r   r   r   Útest_421_from_rcpt_cmd"  s    z#SMTPSimTests.test_421_from_rcpt_cmdc          	   C   s~   G dd„ dt ƒ}|| j_tjt| jddd}| ¡  |  tj	¡ | 
ddgd¡ W d Q R X |  |j¡ |  | jjjd	¡ d S )
Nc                   s   e Zd Z‡ fdd„Z‡  ZS )z=SMTPSimTests.test_421_from_data_cmd.<locals>.MySimSMTPChannelc                s&   | j | jkr|  d¡ n
tƒ  ¡  d S )Nz421 closing)rÔ   ZDATArÚ   rÏ   rÝ   )r   )rÒ   r   r   rÝ   .  s    zNSMTPSimTests.test_421_from_data_cmd.<locals>.MySimSMTPChannel.found_terminator)rG   rH   rI   rÝ   r  r   r   )rÒ   r   ÚMySimSMTPChannel-  s   rL  rs   rÂ   )r0   r   zJohn@foo.orgzSally@foo.orgztest messager   )rÈ   r   r  r"   r*   r   r#   rz   r«   ZSMTPDataErrorrˆ   r3   r6   r&   r  r  )r   rL  r+   r   r   r   Útest_421_from_data_cmd,  s    z#SMTPSimTests.test_421_from_data_cmdc             C   s€   t jt| jddd}|  |j¡ | ¡  |  |j¡ |  	| 
d¡¡ | jt j|jddddd	gd
 | jt j|jddd	gd d S )Nrs   rt   )r0   r   Úsmtputf8r†   r‡   rÊ   zBODY=8BITMIMEr·   )r»   )Úoptions)r"   r*   r   r#   rÄ   r   r}   r(  Ú
does_esmtpr)  r¶   r«   ÚSMTPNotSupportedErrorrˆ   Úmail)r   r+   r   r   r   Ú4test_smtputf8_NotSupportedError_if_no_server_support;  s    zASMTPSimTests.test_smtputf8_NotSupportedError_if_no_server_supportc             C   sH   t jt| jddd}|  |j¡ |  t|jddd¡ |  t|j	d¡ d S )Nrs   rt   )r0   r   ZAliceu   BÃ¶brÊ   u   Ã„lice)
r"   r*   r   r#   rÄ   r   r«   ÚUnicodeEncodeErrorrˆ   rR  )r   r+   r   r   r   Ú"test_send_unicode_without_SMTPUTF8J  s
    z/SMTPSimTests.test_send_unicode_without_SMTPUTF8c          	   C   sd   t ƒ }d|d< d|d< d|d< tjt| jddd	}|  |j¡ |  tj¡ | 	|¡ W d Q R X d S )
Nu   PÃ¡olo <fÅ‘o@bar.com>rŸ   r¦   r    u   Nudge nudge, wink, wink á½ 9ÚSubjectrs   rt   )r0   r   )
r   r"   r*   r   r#   rÄ   r   r«   rQ  rœ   )r   rµ   r+   r   r   r   Ú9test_send_message_error_on_non_ascii_addrs_if_no_smtputf8Q  s    zFSMTPSimTests.test_send_message_error_on_non_ascii_addrs_if_no_smtputf8c             C   s†   t jt| jddd}|  |j¡ tƒ }tj 	d¡|d< tj 	d¡|d< |  
| |¡i ¡ |  | jjd d	¡ |  | jjd
 dg¡ d S )Nrs   rt   )r0   r   )u   MichaÃ«lzmichael@example.comrŸ   )u   RenÃ©zrene@example.comr    r  zmichael@example.comr  zrene@example.com)r"   r*   r   r#   rÄ   r   r   r˜   ZutilsZ
formataddrrJ  rœ   r&   r   r  )r   r+   rA  r   r   r   Ú1test_name_field_not_included_in_envelop_addresses^  s    z>SMTPSimTests.test_name_field_not_included_in_envelop_addressesN)rG   rH   rI   r$   r%   rv   r+  r‚   r4  r7  r8  r9  r:  r>  rB  rC  rF  rH  rI  rK  rM  rS  rU  rW  rX  r   r   r   r   r$  m  s,   	
	
r$  c               @   s&   e Zd Zdd„ Zdd„ Zddd„ZdS )	ÚSimSMTPUTF8Serverc             O   s"   ddg| _ tjj| f|ž|Ž d S )Nr·   Z8BITMIME)r  rb   r  rÐ   )r   rW   rÑ   r   r   r   rÐ   p  s    
zSimSMTPUTF8Server.__init__c             C   s"   | j | j| ||| j| jd| _d S )N)rU   Úenable_SMTPUTF8)r  r  r  rZ  r  )r   r   r   r   r   r   r  w  s    
z!SimSMTPUTF8Server.handle_acceptedNc             C   s(   || _ || _|| _|| _|| _|| _d S )N)Z	last_peerÚlast_mailfromÚlast_rcpttosÚlast_messageÚlast_mail_optionsÚlast_rcpt_options)r   r  r  r  r   r»   Zrcpt_optionsr   r   r   r!  ~  s    z!SimSMTPUTF8Server.process_message)NN)rG   rH   rI   rÐ   r  r!  r   r   r   r   rY  n  s    rY  c               @   s@   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dS )ÚSMTPUTF8SimTestsNc             C   sŽ   t j| _tjt _t ¡ | _t ¡ | _tt	dfdddd| _
| j
j  ¡ d | _| j
| j| jf}tjt|d| _| j ¡  | j ¡  | j ¡  d S )Nr   )rS   rT   FT)rU   rZ  r   )rV   rW   )r   rX   rY   r   r`   ra   rO   rP   rY  r   r   re   r#   rg   rQ   rh   ri   rj   rk   )r   rl   r   r   r   r$   Œ  s    





zSMTPUTF8SimTests.setUpc             C   s*   | j t_| j ¡  | j ¡  | j ¡  d S )N)	rY   r   rX   rP   r   rO   rj   rh   rm   )r   r   r   r   r%   Ÿ  s    

zSMTPUTF8SimTests.tearDownc             C   sH   t jt| jddd}|  |j¡ | ¡  |  |j¡ |  | 	d¡¡ d S )Nrs   rt   )r0   r   rN  )
r"   r*   r   r#   rÄ   r   r}   r(  rP  r¶   )r   r+   r   r   r   Ú$test_test_server_supports_extensions§  s    z5SMTPUTF8SimTests.test_test_server_supports_extensionsc             C   s¦   d  d¡}tjt| jddd}|  |j¡ |jdd|dd	gd
 |  | j	j
d¡ |  | j	jdg¡ |  | j	j|¡ |  d| j	j¡ |  d	| j	j¡ |  | j	jg ¡ d S )Nu$   Â¡a test message containing unicode!zutf-8rs   rt   )r0   r   u   JÅ‘hnu   SÃ¡llyzBODY=8BITMIMEr·   )r»   )rï   r"   r*   r   r#   rÄ   r   rˆ   r&   r   r[  r\  r]  r?  r^  r_  )r   rŒ   r+   r   r   r   Ú,test_send_unicode_with_SMTPUTF8_via_sendmail¯  s    

z=SMTPUTF8SimTests.test_send_unicode_with_SMTPUTF8_via_sendmailc             C   sÖ   d  d¡}tjt| jddd}|  |j¡ | ¡  |  |j	dddgd	d
¡ |  | 
d¡d
¡ |  | |¡d
¡ |  | jjd¡ |  | jjdg¡ |  | jj|¡ |  d| jj¡ |  d| jj¡ |  | jjg ¡ d S )Nu$   Â¡a test message containing unicode!zutf-8rs   rt   )r0   r   u   JÅ‘zBODY=8BITMIMEr·   )rO  )ry   s   OKu   JÃ¡nos)rï   r"   r*   r   r#   rÄ   r   r}   r&   rR  Zrcptr   r   r[  r\  r]  r?  r^  r_  )r   rŒ   r+   r   r   r   Ú1test_send_unicode_with_SMTPUTF8_via_low_level_API½  s     
zBSMTPUTF8SimTests.test_send_unicode_with_SMTPUTF8_via_low_level_APIc             C   sÎ   t ƒ }d|d< d|d< d|d< | d¡ t d¡}tjt| jd	d
d}|  |j	¡ |  
| |¡i ¡ |  
| jjd¡ |  
| jjdg¡ |  
| jj ¡ |¡ |  d| jj¡ |  d| jj¡ |  
| jjg ¡ d S )Nu   PÃ¡olo <fÅ‘o@bar.com>rŸ   r¦   r    u   Nudge nudge, wink, wink á½ 9rV  u1   oh lÃ  lÃ , know what I mean, know what I mean?

u<              From: PÃ¡olo <fÅ‘o@bar.com>
            To: Dinsdale
            Subject: Nudge nudge, wink, wink á½ 9
            Content-Type: text/plain; charset="utf-8"
            Content-Transfer-Encoding: 8bit
            MIME-Version: 1.0

            oh lÃ  lÃ , know what I mean, know what I mean?
            rs   rt   )r0   r   u   fÅ‘o@bar.comzBODY=8BITMIMEr·   )r   Zset_contentÚtextwrapÚdedentr"   r*   r   r#   rÄ   r   r&   rœ   r   r[  r\  r]  r   r?  r^  r_  )r   rµ   r'   r+   r   r   r   Ú2test_send_message_uses_smtputf8_if_addrs_non_asciiÏ  s"    
	zCSMTPUTF8SimTests.test_send_message_uses_smtputf8_if_addrs_non_ascii)
rG   rH   rI   r®   r$   r%   ra  rb  rc  rf  r   r   r   r   r`  ˆ  s   r`  s    psu doesnotexistrÊ   )Zeolc               @   s   e Zd Zdd„ ZdS )Ú!SimSMTPAUTHInitialResponseChannelc             C   sL   |  ¡ }|d  ¡ dkr>t|ƒdkr>|d tkr>|  d¡ d S |  d¡ d S )Nr   ÚplainrE   r   z235 Okz571 Bad authentication)rä   ræ   rå   ÚEXPECTED_RESPONSErÚ   )r   rê   rW   r   r   r   rë   ò  s    
z+SimSMTPAUTHInitialResponseChannel.smtp_AUTHN)rG   rH   rI   rë   r   r   r   r   rg  ñ  s   rg  c               @   s   e Zd ZeZdS )Ú SimSMTPAUTHInitialResponseServerN)rG   rH   rI   rg  r  r   r   r   r   rj     s   rj  c               @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚSMTPAUTHInitialResponseSimTestsc             C   sŒ   t j| _tjt _t ¡ | _t ¡ | _tt	dfddd| _
| j
j  ¡ d | _| j
| j| jf}tjt|d| _| j ¡  | j ¡  | j ¡  d S )Nr   )rS   rT   T)rU   r   )rV   rW   )r   rX   rY   r   r`   ra   rO   rP   rj  r   r   re   r#   rg   rQ   rh   ri   rj   rk   )r   rl   r   r   r   r$     s    



z%SMTPAUTHInitialResponseSimTests.setUpc             C   s*   | j t_| j ¡  | j ¡  | j ¡  d S )N)	rY   r   rX   rP   r   rO   rj   rh   rm   )r   r   r   r   r%     s    

z(SMTPAUTHInitialResponseSimTests.tearDownc             C   s8   | j  d¡ tjt| jddd}| dd¡ | ¡  d S )Nz
AUTH PLAINrs   rÂ   )r0   r   ÚpsuÚdoesnotexist)r   r#  r"   r*   r   r#   r6  r   )r   r+   r   r   r   Ú%testAUTH_PLAIN_initial_response_login  s
    

zESMTPAUTHInitialResponseSimTests.testAUTH_PLAIN_initial_response_loginc             C   sV   | j  d¡ tjt| jddd}d|_d|_| d|j	¡\}}| 
¡  |  |d¡ d S )	Nz
AUTH PLAINrs   rÂ   )r0   r   rl  rm  rh  r5  )r   r#  r"   r*   r   r#   rì   rõ   r<  Z
auth_plainr   r&   )r   r+   r@  Zresponser   r   r   Ú$testAUTH_PLAIN_initial_response_auth&  s    

zDSMTPAUTHInitialResponseSimTests.testAUTH_PLAIN_initial_response_authN)rG   rH   rI   r$   r%   rn  ro  r   r   r   r   rk    s   rk  Ú__main__)@rL   rî   Zemail.mime.textr˜   Zemail.messager   Zemail.base64mimer   Zencode_base64Zemail.utilsrú   r   rb   r"   r]   r>   rZ   rM   r   rx   rd  r`   ZunittestZtestr   r   Ztest.supportr   r   r   Zunittest.mockr	   Úplatformr   ZSMTPChannelr   ZTestCaser    rQ   rŠ   r‹   rR   r¯   r³   r½   r¿   rþ   ró   rù   r  Ú	ExceptionrÇ   rÈ   r  r  r$  rY  r`  ri  rg  rj  rk  rG   Úmainr   r   r   r   Ú<module>   st   
_  u
 4  g-
