B
    u9aš  ã               @   sH  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mZmZmZmZmZmZ d dlZd dlmZ d dlmZmZmZ yd dlZW n ek
rÄ   dZY nX ej ej e¡pÜejd¡Z ej ej e¡pøejd¡Z!G d	d
„ d
ej"ƒZ#er0G dd„ dej$ƒZ%ej&Z&nG dd„ dƒZ%dZ&G dd„ dej'ƒZ(G dd„ dƒZ)G dd„ de)ej"ƒZ*e +ed¡G dd„ de)ej"ƒƒZ,G dd„ dej"ƒZ-e +ed¡G dd„ de-ƒƒZ.e +e /d¡d¡G dd„ dej"ƒƒZ0e +ed¡e +e /d¡d¡G dd „ d e0ƒƒƒZ1e +ed¡G d!d"„ d"e0ƒƒZ2e3d#krDe 4¡  dS )$é    )Úsupport)ÚcontextmanagerN)Úreap_threadsÚverboseÚtransient_internetÚrun_with_tzÚrun_with_localeÚcpython_only)Úmock)ÚdatetimeÚtimezoneÚ	timedeltazkeycert3.pemzpycacert.pemc               @   sX   e Zd Zdd„ Zedƒdd„ ƒZdd„ Zedd	d
ƒedƒdd„ ƒƒZdd„ Z	dd„ Z
dS )ÚTestImaplibc             C   sb   t  d¡}t d¡}|  t |¡|¡ t d¡}|  t |¡|¡ t d¡}|  t |¡|¡ d S )N)	iÐ  é   r   r   r   r   éÿÿÿÿr   r   s.   25 (INTERNALDATE "01-Jan-2000 00:00:00 +0000")s.   25 (INTERNALDATE "01-Jan-2000 11:30:00 +1130")s.   25 (INTERNALDATE "31-Dec-1999 12:30:00 -1130"))ÚcalendarZtimegmÚimaplibÚInternaldate2tupleÚassertEqualÚtimeZmktime)ÚselfZt0Útt© r   ú"/usr/lib/python3.7/test_imaplib.pyÚtest_Internaldate2tuple   s    
z#TestImaplib.test_Internaldate2tuplezMST+07MDT,M4.1.0,M10.5.0c             C   s   |   t d¡t d¡¡ d S )Ns.   25 (INTERNALDATE "02-Apr-2000 02:30:00 +0000")s.   25 (INTERNALDATE "02-Apr-2000 03:30:00 +0000"))ZassertNotEqualr   r   )r   r   r   r   Ú"test_Internaldate2tuple_issue10941)   s    z.TestImaplib.test_Internaldate2tuple_issue10941c             C   s*   ddt  d¡ddt dttddƒƒ¡dgS )Ni ”5wg    eÍÝA)	iñ  é   é   r   é!   é   r   r   r   )	iñ  r   r   r   r   r   r   r   r   r   i   z"18-May-2033 05:33:20 +0200")r   Z	localtimer   Zfromtimestampr   r   )r   r   r   r   Ú
timevalues0   s    zTestImaplib.timevaluesÚLC_ALLÚde_DEÚfr_FRzSTD-1DST,M3.2.0,M11.1.0c             C   s0   d}x&|   ¡ D ]}t |¡}|  ||¡ qW d S )Nz"18-May-2033 05:33:20 +0200")r    r   ÚTime2Internaldater   )r   ZexpectedÚtZinternalr   r   r   Útest_Time2Internaldate8   s    
z"TestImaplib.test_Time2Internaldatec             C   s    x|   ¡ D ]}t |¡ q
W d S )N)r    r   r$   )r   r%   r   r   r   Ú,test_that_Time2Internaldate_returns_a_resultC   s    z8TestImaplib.test_that_Time2Internaldate_returns_a_resultc          	   C   s„   t   ¡ <}y| dtjf¡ |  d¡ W n t jk
r>   Y nX W d Q R X t ¡ }|  t	¡}t 
¡  W d Q R X |  |jj|¡ d S )NÚ z3Cannot run the test with local IMAP server running.)ÚsocketÚconnectr   Z
IMAP4_PORTZskipTestÚerrorr   Zget_socket_conn_refused_errsÚassertRaisesÚOSErrorÚIMAP4ÚassertInZ	exceptionÚerrno)r   ÚsZexpected_errnosÚcmr   r   r   Útest_imap4_host_default_valueJ   s    

z)TestImaplib.test_imap4_host_default_valueN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r    r   r&   r'   r3   r   r   r   r   r      s   
r   c               @   s   e Zd Zdd„ ZdS )ÚSecureTCPServerc             C   s6   | j  ¡ \}}t ¡ }| t¡ |j|dd}||fS )NT)Zserver_side)r)   ZacceptÚsslÚ
SSLContextÚload_cert_chainÚCERTFILEZwrap_socket)r   Z	newsocketZfromaddrÚcontextZ
connstreamr   r   r   Úget_request^   s
    
zSecureTCPServer.get_requestN)r4   r5   r6   r=   r   r   r   r   r7   \   s   r7   c               @   s   e Zd ZdS )r7   N)r4   r5   r6   r   r   r   r   r7   i   s   c                   sh   e Zd ZdZdZdZ‡ f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‡  ZS )ÚSimpleIMAPHandlerr   Nr(   c                s   t ƒ  ¡  d | j_d S )N)ÚsuperÚsetupÚserverÚlogged)r   )Ú	__class__r   r   r@   t   s    
zSimpleIMAPHandler.setupc             C   s$   t rtd| ¡  ƒ | j |¡ d S )NzSENT: %r)r   ÚprintÚstripÚwfileÚwrite)r   Úmessager   r   r   Ú_sendx   s    zSimpleIMAPHandler._sendc             C   s   |   |d ¡ d S )Ns   
)rI   )r   rH   r   r   r   Ú
_send_line}   s    zSimpleIMAPHandler._send_linec             C   s   |   | d¡¡ d S )NÚASCII)rJ   Úencode)r   rH   r   r   r   Ú_send_textline€   s    z SimpleIMAPHandler._send_textlinec             C   s   |   d |||f¡¡ d S )Nú )rM   Újoin)r   ÚtagÚcoderH   r   r   r   Ú_send_taggedƒ   s    zSimpleIMAPHandler._send_taggedc             C   s$  |   d¡ xd}xJy$| j d¡}|dkr.d S ||7 }W n tk
rL   d S X | d¡rP qW trrtd| ¡  ƒ | jr¦y| j 	|¡ W q t
k
r¢   d | _Y qX q| d¡ ¡ }|d }|d }|dd … }t| d	| ƒr
t| d	| ƒ||ƒ}|r|| _t|ƒ q|  |d
|d ¡ qW d S )Nz* OK IMAP4rev1ó    r   s   
zGOT: %rrK   r   é   Zcmd_ZBADz unknown)rM   ZrfileÚreadr-   Úendswithr   rD   rE   ÚcontinuationÚsendÚStopIterationÚdecodeÚsplitÚhasattrÚgetattrÚnextrR   )r   ÚlineÚpartZ	splitlinerP   ÚcmdÚargsrW   r   r   r   Úhandle†   s>    


zSimpleIMAPHandler.handlec             C   s4   | j rd| j  nd}|  d| ¡ |  |dd¡ d S )Nz
IMAP4rev1 Z	IMAP4rev1z* CAPABILITY ÚOKzCAPABILITY completed)ÚcapabilitiesrM   rR   )r   rP   rb   Zcapsr   r   r   Úcmd_CAPABILITY°   s    z SimpleIMAPHandler.cmd_CAPABILITYc             C   s$   d | j _|  d¡ |  |dd¡ d S )Nz"* BYE IMAP4ref1 Server logging outrd   zLOGOUT completed)rA   rB   rM   rR   )r   rP   rb   r   r   r   Ú
cmd_LOGOUT·   s    
zSimpleIMAPHandler.cmd_LOGOUTc             C   s   |d | j _|  |dd¡ d S )Nr   rd   zLOGIN completed)rA   rB   rR   )r   rP   rb   r   r   r   Ú	cmd_LOGIN¼   s    zSimpleIMAPHandler.cmd_LOGIN)r4   r5   r6   ZtimeoutrW   re   r@   rI   rJ   rM   rR   rc   rf   rg   rh   Ú__classcell__r   r   )rC   r   r>   o   s   *r>   c               @   sÖ   e Zd ZdZd3d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e d#d$¡d%d&„ ƒZd'd(„ Zd)d*„ Zd+d,„ Zd-d.„ Zd/d0„ Zd1d2„ ZdS )4ÚNewIMAPTestsMixinNTc             C   s„   G dd„ d| j ƒ}|  | j¡ |   tjdf|¡| _tj| jd | jj	ddid| _
d| j
_| j
 ¡  |rx| j| jjŽ | _| j| jfS )	a  
        Sets up imap_handler for tests. imap_handler should inherit from either:
        - SimpleIMAPHandler - for testing IMAP commands,
        - socketserver.StreamRequestHandler - if raw access to stream is needed.
        Returns (client, server).
        c               @   s   e Zd Zdd„ ZdS )z/NewIMAPTestsMixin._setup.<locals>.TestTCPServerc             S   s   |   |¡ |  ¡  ‚ dS )zP
                End request and raise the error if one occurs.
                N)Úclose_requestÚserver_close)r   ÚrequestÚclient_addressr   r   r   Úhandle_errorÌ   s    
z<NewIMAPTestsMixin._setup.<locals>.TestTCPServer.handle_errorN)r4   r5   r6   ro   r   r   r   r   ÚTestTCPServerË   s   rp   r   z-serverÚpoll_intervalg{®Gáz„?)ÚnameÚtargetÚkwargsT)Úserver_classZ
addCleanupÚ_cleanupr   ÚHOSTrA   Ú	threadingÚThreadZ_testMethodNameÚserve_foreverÚthreadÚdaemonÚstartÚ
imap_classÚserver_addressÚclient)r   Zimap_handlerr*   rp   r   r   r   Ú_setupÄ   s    	
zNewIMAPTestsMixin._setupc             C   sL   | j dk	r | j jdkr | j  ¡  | j ¡  | j ¡  t | jd¡ d| _dS )zž
        Cleans up the test server. This method should not be called manually,
        it is added to the cleanup queue in the _setup method already.
        NÚLOGOUTg      @)r€   ÚstateÚshutdownrA   rl   r   Zjoin_threadr{   )r   r   r   r   rv   å   s    


zNewIMAPTestsMixin._cleanupc             C   sB   G dd„ dt jƒ}| j|dd\}}| jtjj| jf|jžŽ  d S )Nc               @   s   e Zd Zdd„ ZdS )zONewIMAPTestsMixin.test_EOF_without_complete_welcome_message.<locals>.EOFHandlerc             S   s   | j  d¡ d S )Ns   * OK)rF   rG   )r   r   r   r   rc   ø   s    zVNewIMAPTestsMixin.test_EOF_without_complete_welcome_message.<locals>.EOFHandler.handleN)r4   r5   r6   rc   r   r   r   r   Ú
EOFHandler÷   s   r…   F)r*   )	ÚsocketserverÚStreamRequestHandlerr   r,   r   r.   Úabortr~   r   )r   r…   Ú_rA   r   r   r   Ú)test_EOF_without_complete_welcome_messageõ   s    z;NewIMAPTestsMixin.test_EOF_without_complete_welcome_messagec             C   s@   G dd„ dt ƒ}| j|dd\}}| jtjj| jf|jžŽ  d S )Nc               @   s   e Zd Zdd„ ZdS )zBNewIMAPTestsMixin.test_line_termination.<locals>.BadNewlineHandlerc             S   s   |   d¡ |  |dd¡ d S )Ns   * CAPABILITY IMAP4rev1 AUTH
rd   zCAPABILITY completed)rI   rR   )r   rP   rb   r   r   r   rf      s    
zQNewIMAPTestsMixin.test_line_termination.<locals>.BadNewlineHandler.cmd_CAPABILITYN)r4   r5   r6   rf   r   r   r   r   ÚBadNewlineHandlerÿ   s   r‹   F)r*   )r>   r   r,   r   r.   rˆ   r~   r   )r   r‹   r‰   rA   r   r   r   Útest_line_terminationþ   s    z'NewIMAPTestsMixin.test_line_terminationc          	   C   s`   G dd„ dt ƒ}|  |¡\}}|  |j¡ |  tjjd¡ | d¡ W d Q R X |  |j¡ d S )Nc               @   s   e Zd ZdZdS )zMNewIMAPTestsMixin.test_enable_raises_error_if_not_AUTH.<locals>.EnableHandlerzAUTH ENABLE UTF8=ACCEPTN)r4   r5   r6   re   r   r   r   r   ÚEnableHandler  s   r   zENABLE.*NONAUTHÚfoo)	r>   r   ÚassertFalseÚutf8_enabledÚassertRaisesRegexr   r.   r+   Úenable)r   r   r€   r‰   r   r   r   Ú$test_enable_raises_error_if_not_AUTH  s    z6NewIMAPTestsMixin.test_enable_raises_error_if_not_AUTHc          	   C   s8   |   t¡\}}|  tjjd¡ | d¡ W d Q R X d S )Nzdoes not support ENABLErŽ   )r   r>   r‘   r   r.   r+   r’   )r   r€   r‰   r   r   r   Ú)test_enable_raises_error_if_no_capability  s    
z;NewIMAPTestsMixin.test_enable_raises_error_if_no_capabilityc          	   C   sT   |   t¡\}}| dd¡\}}|  |d¡ |  tjjd¡ | d¡ W d Q R X d S )NÚuserÚpassrd   zdoes not support ENABLEzUTF8=ACCEPT)	r   r>   Úloginr   r‘   r   r.   r+   r’   )r   r€   r‰   ÚtypÚdatar   r   r   Ú.test_enable_UTF8_raises_error_if_not_supported  s    
z@NewIMAPTestsMixin.test_enable_UTF8_raises_error_if_not_supportedc       	      C   sÈ   G dd„ dt ƒ}|  |¡\}}|  |jd¡ | ddd„ ¡\}}|  |d¡ |  |jd¡ | d	¡\}}|  |d¡ |  |jd
¡ d}| d d d | d
¡¡\}}|  |d¡ |  |jd|  d
¡¡ d S )Nc               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	zHNewIMAPTestsMixin.test_enable_UTF8_True_append.<locals>.UTF8AppendServerzENABLE UTF8=ACCEPTc             S   s   |   |dd¡ d S )Nrd   zENABLE successful)rR   )r   rP   rb   r   r   r   Ú
cmd_ENABLE!  s    zSNewIMAPTestsMixin.test_enable_UTF8_True_append.<locals>.UTF8AppendServer.cmd_ENABLEc             s   s&   |   d¡ d V | j_|  |dd¡ d S )Nú+rd   zFAKEAUTH successful)rM   rA   ÚresponserR   )r   rP   rb   r   r   r   Úcmd_AUTHENTICATE#  s    

zYNewIMAPTestsMixin.test_enable_UTF8_True_append.<locals>.UTF8AppendServer.cmd_AUTHENTICATEc             s   s&   |   d¡ d V | j_|  |dd¡ d S )Nrœ   rd   Úokay)rM   rA   r   rR   )r   rP   rb   r   r   r   Ú
cmd_APPEND'  s    

zSNewIMAPTestsMixin.test_enable_UTF8_True_append.<locals>.UTF8AppendServer.cmd_APPENDN)r4   r5   r6   re   r›   rž   r    r   r   r   r   ÚUTF8AppendServer  s   r¡   ÚasciiÚMYAUTHc             S   s   dS )Ns   faker   )Úxr   r   r   Ú<lambda>-  rS   z@NewIMAPTestsMixin.test_enable_UTF8_True_append.<locals>.<lambda>rd   s
   ZmFrZQ==
zUTF8=ACCEPTzutf-8u   Subject: Ã¼Ã±Ã­Â©Ã¶Ã°Ã©zUTF8 (%s)
)	r>   r   r   Ú	_encodingÚauthenticater   r’   ÚappendrL   )	r   r¡   r€   rA   rQ   r‰   Ú
msg_stringr˜   r™   r   r   r   Útest_enable_UTF8_True_append  s    z.NewIMAPTestsMixin.test_enable_UTF8_True_appendc          	   C   s   G dd„ dt ƒ}|  |¡\}}| ddd„ ¡\}}|  |d¡ | d¡\}}|  |d¡ |  |j¡ |  tj	j
d¡ | d	d
¡ W d Q R X d S )Nc               @   s    e Zd ZdZdd„ Zdd„ ZdS )zPNewIMAPTestsMixin.test_search_disallows_charset_in_utf8_mode.<locals>.UTF8ServerzAUTH ENABLE UTF8=ACCEPTc             S   s   |   |dd¡ d S )Nrd   zENABLE successful)rR   )r   rP   rb   r   r   r   r›   <  s    z[NewIMAPTestsMixin.test_search_disallows_charset_in_utf8_mode.<locals>.UTF8Server.cmd_ENABLEc             s   s&   |   d¡ d V | j_|  |dd¡ d S )Nrœ   rd   zFAKEAUTH successful)rM   rA   r   rR   )r   rP   rb   r   r   r   rž   >  s    

zaNewIMAPTestsMixin.test_search_disallows_charset_in_utf8_mode.<locals>.UTF8Server.cmd_AUTHENTICATEN)r4   r5   r6   re   r›   rž   r   r   r   r   Ú
UTF8Server:  s   r«   r£   c             S   s   dS )Ns   faker   )r¤   r   r   r   r¥   C  rS   zNNewIMAPTestsMixin.test_search_disallows_charset_in_utf8_mode.<locals>.<lambda>rd   zUTF8=ACCEPTzcharset.*UTF8rŽ   Úbar)r>   r   r§   r   r’   Ú
assertTruer   r‘   r   r.   r+   Úsearch)r   r«   r€   r‰   r˜   r   r   r   Ú*test_search_disallows_charset_in_utf8_mode9  s    z<NewIMAPTestsMixin.test_search_disallows_charset_in_utf8_modec          	   C   sN   G dd„ dt ƒ}|  |¡\}}|  tjjd¡ | ddd„ ¡ W d Q R X d S )Nc               @   s   e Zd Zdd„ ZdS )z6NewIMAPTestsMixin.test_bad_auth_name.<locals>.MyServerc             S   s   |   |dd |d ¡¡ d S )NÚNOz#unrecognized authentication type {}r   )rR   Úformat)r   rP   rb   r   r   r   rž   M  s    zGNewIMAPTestsMixin.test_bad_auth_name.<locals>.MyServer.cmd_AUTHENTICATEN)r4   r5   r6   rž   r   r   r   r   ÚMyServerL  s   r²   z'unrecognized authentication type METHODÚMETHODc               S   s   dS )Nr   r   r   r   r   r   r¥   S  rS   z6NewIMAPTestsMixin.test_bad_auth_name.<locals>.<lambda>)r>   r   r‘   r   r.   r+   r§   )r   r²   r€   r‰   r   r   r   Útest_bad_auth_nameK  s
    
z$NewIMAPTestsMixin.test_bad_auth_namec          	   C   sN   G dd„ dt ƒ}|  |¡\}}|  tjjd¡ | ddd„ ¡ W d Q R X d S )Nc               @   s   e Zd Zdd„ ZdS )z?NewIMAPTestsMixin.test_invalid_authentication.<locals>.MyServerc             s   s$   |   d¡ d V | _|  |dd¡ d S )Nrœ   r°   z[AUTHENTICATIONFAILED] invalid)rM   r   rR   )r   rP   rb   r   r   r   rž   W  s    
zPNewIMAPTestsMixin.test_invalid_authentication.<locals>.MyServer.cmd_AUTHENTICATEN)r4   r5   r6   rž   r   r   r   r   r²   V  s   r²   z \[AUTHENTICATIONFAILED\] invalidr£   c             S   s   dS )Ns   faker   )r¤   r   r   r   r¥   ^  rS   z?NewIMAPTestsMixin.test_invalid_authentication.<locals>.<lambda>)r>   r   r‘   r   r.   r+   r§   )r   r²   r€   r‰   r   r   r   Útest_invalid_authenticationU  s
    
z-NewIMAPTestsMixin.test_invalid_authenticationc             C   sP   G dd„ dt ƒ}|  |¡\}}| ddd„ ¡\}}|  |d¡ |  |jd¡ d S )Nc               @   s   e Zd Zdd„ ZdS )zCNewIMAPTestsMixin.test_valid_authentication_bytes.<locals>.MyServerc             s   s&   |   d¡ d V | j_|  |dd¡ d S )Nrœ   rd   zFAKEAUTH successful)rM   rA   r   rR   )r   rP   rb   r   r   r   rž   b  s    

zTNewIMAPTestsMixin.test_valid_authentication_bytes.<locals>.MyServer.cmd_AUTHENTICATEN)r4   r5   r6   rž   r   r   r   r   r²   a  s   r²   r£   c             S   s   dS )Ns   faker   )r¤   r   r   r   r¥   g  rS   zCNewIMAPTestsMixin.test_valid_authentication_bytes.<locals>.<lambda>rd   s
   ZmFrZQ==
)r>   r   r§   r   r   )r   r²   r€   rA   rQ   r‰   r   r   r   Útest_valid_authentication_bytes`  s
    z1NewIMAPTestsMixin.test_valid_authentication_bytesc             C   sP   G dd„ dt ƒ}|  |¡\}}| ddd„ ¡\}}|  |d¡ |  |jd¡ d S )Nc               @   s   e Zd Zdd„ ZdS )zHNewIMAPTestsMixin.test_valid_authentication_plain_text.<locals>.MyServerc             s   s&   |   d¡ d V | j_|  |dd¡ d S )Nrœ   rd   zFAKEAUTH successful)rM   rA   r   rR   )r   rP   rb   r   r   r   rž   m  s    

zYNewIMAPTestsMixin.test_valid_authentication_plain_text.<locals>.MyServer.cmd_AUTHENTICATEN)r4   r5   r6   rž   r   r   r   r   r²   l  s   r²   r£   c             S   s   dS )NÚfaker   )r¤   r   r   r   r¥   r  rS   zHNewIMAPTestsMixin.test_valid_authentication_plain_text.<locals>.<lambda>rd   s
   ZmFrZQ==
)r>   r   r§   r   r   )r   r²   r€   rA   rQ   r‰   r   r   r   Ú$test_valid_authentication_plain_textk  s
    z6NewIMAPTestsMixin.test_valid_authentication_plain_textc             C   sN   G dd„ dt ƒ}|  |¡\}}|  d|jk¡ | dd¡\}}|  |d¡ d S )Nc               @   s   e Zd ZdZdd„ ZdS )z@NewIMAPTestsMixin.test_login_cram_md5_bytes.<locals>.AuthHandlerzLOGINDISABLED AUTH=CRAM-MD5c             s   s:   |   d¡ d V }|dkr(|  |dd¡ n|  |dd¡ d S )Nz:+ PDE4OTYuNjk3MTcwOTUyQHBvc3RvZmZpY2UucmVzdG9uLm1jaS5uZXQ=s2   dGltIGYxY2E2YmU0NjRiOWVmYTFjY2E2ZmZkNmNmMmQ5ZjMy
rd   zCRAM-MD5 successfulr°   z	No access)rM   rR   )r   rP   rb   Úrr   r   r   rž   y  s
    
zQNewIMAPTestsMixin.test_login_cram_md5_bytes.<locals>.AuthHandler.cmd_AUTHENTICATEN)r4   r5   r6   re   rž   r   r   r   r   ÚAuthHandlerw  s   rº   zAUTH=CRAM-MD5Útims   tanstaaftanstaafrd   )r>   r   r­   re   Úlogin_cram_md5r   )r   rº   r€   r‰   Úretr   r   r   Útest_login_cram_md5_bytesv  s
    z+NewIMAPTestsMixin.test_login_cram_md5_bytesc             C   sN   G dd„ dt ƒ}|  |¡\}}|  d|jk¡ | dd¡\}}|  |d¡ d S )Nc               @   s   e Zd ZdZdd„ ZdS )zENewIMAPTestsMixin.test_login_cram_md5_plain_text.<locals>.AuthHandlerzLOGINDISABLED AUTH=CRAM-MD5c             s   s:   |   d¡ d V }|dkr(|  |dd¡ n|  |dd¡ d S )Nz:+ PDE4OTYuNjk3MTcwOTUyQHBvc3RvZmZpY2UucmVzdG9uLm1jaS5uZXQ=s2   dGltIGYxY2E2YmU0NjRiOWVmYTFjY2E2ZmZkNmNmMmQ5ZjMy
rd   zCRAM-MD5 successfulr°   z	No access)rM   rR   )r   rP   rb   r¹   r   r   r   rž   Š  s
    
zVNewIMAPTestsMixin.test_login_cram_md5_plain_text.<locals>.AuthHandler.cmd_AUTHENTICATEN)r4   r5   r6   re   rž   r   r   r   r   rº   ˆ  s   rº   zAUTH=CRAM-MD5r»   Útanstaaftanstaafrd   )r>   r   r­   re   r¼   r   )r   rº   r€   r‰   r½   r   r   r   Útest_login_cram_md5_plain_text‡  s
    z0NewIMAPTestsMixin.test_login_cram_md5_plain_textc          	   C   sN   G dd„ dt ƒ}|  |¡\}}|  tjjd¡ | ddd„ ¡ W d Q R X d S )Nc               @   s   e Zd Zdd„ ZdS )z?NewIMAPTestsMixin.test_aborted_authentication.<locals>.MyServerc             s   s>   |   d¡ d V | _| jdkr,|  |dd¡ n|  |dd¡ d S )Nrœ   s   *
r°   z[AUTHENTICATIONFAILED] abortedrd   zMYAUTH successful)rM   r   rR   )r   rP   rb   r   r   r   rž   š  s    

zPNewIMAPTestsMixin.test_aborted_authentication.<locals>.MyServer.cmd_AUTHENTICATEN)r4   r5   r6   rž   r   r   r   r   r²   ™  s   r²   z \[AUTHENTICATIONFAILED\] abortedr£   c             S   s   d S )Nr   )r¤   r   r   r   r¥   §  rS   z?NewIMAPTestsMixin.test_aborted_authentication.<locals>.<lambda>)r>   r   r‘   r   r.   r+   r§   )r   r²   r€   r‰   r   r   r   Útest_aborted_authentication˜  s
    
z-NewIMAPTestsMixin.test_aborted_authenticationzimaplib._MAXLINEé
   c          	   C   sN   G dd„ dt ƒ}| j|dd\}}|  tjjd¡ | j|jŽ  W d Q R X d S )Nc               @   s   e Zd Zdd„ ZdS )z:NewIMAPTestsMixin.test_linetoolong.<locals>.TooLongHandlerc             S   s   | j  d¡ d S )Ns   * OK xxxxxxxxxxx
)rF   rG   )r   r   r   r   rc   ¬  s    zANewIMAPTestsMixin.test_linetoolong.<locals>.TooLongHandler.handleN)r4   r5   r6   rc   r   r   r   r   ÚTooLongHandler«  s   rÃ   F)r*   zgot more than 10 bytes)r>   r   r‘   r   r.   r+   r~   r   )r   rÃ   r‰   rA   r   r   r   Útest_linetoolong©  s
    
z"NewIMAPTestsMixin.test_linetoolongc          	   C   s.   | j tdd\}}| j|jŽ  W d Q R X d S )NF)r*   )r   r>   r~   r   )r   r‰   rA   r   r   r   Útest_simple_with_statement´  s    z,NewIMAPTestsMixin.test_simple_with_statementc          	   C   sT   | j tdd\}}| j|jŽ  }| dd¡ |  |jd¡ W d Q R X |  |j¡ d S )NF)r*   r•   r–   )r   r>   r~   r   r—   r   rB   ÚassertIsNone)r   r‰   rA   Úimapr   r   r   Útest_with_statement¹  s
    z%NewIMAPTestsMixin.test_with_statementc          	   C   sh   | j tdd\}}| j|jŽ 4}| dd¡ |  |jd¡ | ¡  |  |j¡ W d Q R X |  |j¡ d S )NF)r*   r•   r–   )	r   r>   r~   r   r—   r   rB   ÚlogoutrÆ   )r   r‰   rA   rÇ   r   r   r   Útest_with_statement_logoutÀ  s    z,NewIMAPTestsMixin.test_with_statement_logoutc             C   sL   |   t¡\}}| dd¡\}}|  |d¡ |  |d d¡ |  |jd¡ d S )Nr•   r–   rd   r   s   LOGIN completedÚAUTH)r   r>   r—   r   rƒ   )r   r€   r‰   r˜   r™   r   r   r   Ú
test_loginÌ  s
    zNewIMAPTestsMixin.test_loginc             C   s€   |   t¡\}}| dd¡\}}|  |d¡ |  |d d¡ | ¡ \}}|  |d||f¡ |  |d d||f¡ |  |jd¡ d S )	Nr•   r–   rd   r   s   LOGIN completedÚBYEs   IMAP4ref1 Server logging outr‚   )r   r>   r—   r   rÉ   rƒ   )r   r€   r‰   r˜   r™   r   r   r   Útest_logoutÓ  s    zNewIMAPTestsMixin.test_logoutc             C   sV   G dd„ dt ƒ}|  |¡\}}| dd¡ | ¡ \}}|  |d¡ |  |d d¡ d S )Nc               @   s   e Zd Zdd„ ZdS )z,NewIMAPTestsMixin.test_lsub.<locals>.LsubCmdc             S   s   |   d¡ |  |dd¡S )Nz* LSUB () "." directoryArd   zLSUB completed)rM   rR   )r   rP   rb   r   r   r   Úcmd_LSUBß  s    
z5NewIMAPTestsMixin.test_lsub.<locals>.LsubCmd.cmd_LSUBN)r4   r5   r6   rÏ   r   r   r   r   ÚLsubCmdÞ  s   rÐ   r•   r–   rd   r   s   () "." directoryA)r>   r   r—   Zlsubr   )r   rÐ   r€   r‰   r˜   r™   r   r   r   Ú	test_lsubÝ  s    zNewIMAPTestsMixin.test_lsub)T)r4   r5   r6   r€   r   rv   rŠ   rŒ   r“   r”   rš   rª   r¯   r´   rµ   r¶   r¸   r¾   rÀ   rÁ   r
   ÚpatchrÄ   rÅ   rÈ   rÊ   rÌ   rÎ   rÑ   r   r   r   r   rj   Á   s0   
!			

rj   c               @   s   e Zd ZejZejZdS )ÚNewIMAPTestsN)	r4   r5   r6   r   r.   r~   r†   Ú	TCPServerru   r   r   r   r   rÓ   é  s   rÓ   zSSL not availablec               @   s0   e Zd ZeZeZdd„ Zdd„ Ze	dd„ ƒZ
dS )ÚNewIMAPSSLTestsc          	   C   sz   t  t j¡}|  |jt j¡ |  |jd¡ | t¡ |  	t j
d¡. |  t¡\}}| j|jd|iŽ}| ¡  W d Q R X d S )NTz=IP address mismatch, certificate is not valid for '127.0.0.1'Ússl_context)r8   r9   ÚPROTOCOL_TLS_CLIENTr   Úverify_modeZCERT_REQUIREDÚcheck_hostnameÚload_verify_locationsÚCAFILEr‘   ÚCertificateErrorr   r>   r~   r   r„   )r   rÖ   r‰   rA   r€   r   r   r   Útest_ssl_raisesó  s    

zNewIMAPSSLTests.test_ssl_raisesc             C   sF   t  t j¡}| t¡ |  t¡\}}| jd|jd |d}| 	¡  d S )NÚ	localhostr   )rÖ   )
r8   r9   r×   rÚ   rÛ   r   r>   r~   r   r„   )r   rÖ   r‰   rA   r€   r   r   r   Útest_ssl_verified  s    
z!NewIMAPSSLTests.test_ssl_verifiedc             C   sf   t  dtf¡N tj | jd¡2 tj | jd¡ | jddtd W d Q R X W d Q R X W d Q R X d S )Nr(   ÚopenZ_connectrÞ   é   )Úcertfile)r   Zcheck_warningsÚDeprecationWarningr
   rÒ   Úobjectr~   r;   )r   r   r   r   Útest_certfile_arg_warn  s    z&NewIMAPSSLTests.test_certfile_arg_warnN)r4   r5   r6   Ú	IMAP4_SSLr~   r7   ru   rÝ   rß   r	   rå   r   r   r   r   rÕ   î  s
   rÕ   c               @   s$  e Zd ZejZejZdd„ Z	dd„ Z
edd„ ƒZedd„ ƒZed	d
„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZG dd„ deƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd „ ƒZed!d"„ ƒZed#d$„ ƒZed%d&„ ƒZd'd(„ Zed)d*„ ƒZ ed+d,„ ƒZ!ed-d.„ ƒZ"d/S )0ÚThreadedNetworkedTestsc             C   s¨   G dd„ d| j ƒ}trtdƒ |||ƒ}|  |j|j ¡ ¡ trjtdƒ td|ƒ td| j ƒ td|jƒ tj	d| j  |j
d	d
id}d|_| ¡  tr tdƒ ||fS )Nc               @   s   e Zd Zdd„ ZdS )z4ThreadedNetworkedTests.make_server.<locals>.MyServerc             S   s   |   |¡ |  ¡  ‚ d S )N)rk   rl   )r   rm   rn   r   r   r   ro     s    
zAThreadedNetworkedTests.make_server.<locals>.MyServer.handle_errorN)r4   r5   r6   ro   r   r   r   r   r²     s   r²   zcreating serverzserver createdzADDR =zCLASS =zHDLR =z
%s servingrq   g{®Gáz„?)rr   rs   rt   Tzserver running)ru   r   rD   r   r   r)   ZgetsocknameZRequestHandlerClassrx   ry   rz   r|   r}   )r   ZaddrÚhdlrr²   rA   r%   r   r   r   Úmake_server  s&    

z"ThreadedNetworkedTests.make_serverc             C   s4   t rtdƒ | ¡  | ¡  | ¡  t r0tdƒ d S )Nzwaiting for serverZdone)r   rD   r„   rl   rO   )r   rA   r{   r   r   r   Úreap_server7  s    z"ThreadedNetworkedTests.reap_serverc          
   c   s4   |   tjdf|¡\}}z
|V  W d |  ||¡ X d S )Nr   )ré   r   rw   rê   )r   rè   rA   r{   r   r   r   Úreaped_server@  s    
z$ThreadedNetworkedTests.reaped_serverc          	   c   s@   |   |¡,}| j|jŽ }z||fV  W d | ¡  X W d Q R X d S )N)rë   r~   r   rÉ   )r   rè   rA   r€   r   r   r   Úreaped_pairH  s
    z"ThreadedNetworkedTests.reaped_pairc          	   C   s.   |   t¡}| j|jŽ }| ¡  W d Q R X d S )N)rë   r>   r~   r   r„   )r   rA   r€   r   r   r   Útest_connectQ  s    z#ThreadedNetworkedTests.test_connectc          	   C   s¬   G dd„ dt ƒ}|  |¡ˆ\}}| ddd„ ¡\}}|  |d¡ |  |jd¡ | d¡ | d	d
d¡\}\}|  d|¡ | d¡ | d¡\}\}|  d|¡ W d Q R X d S )Nc                   s4   e Zd Z‡ fdd„Zdd„ Zdd„ Zdd„ Z‡  ZS )	zEThreadedNetworkedTests.test_bracket_flags.<locals>.BracketFlagHandlerc                s   dddddg| _ tƒ  ¡  d S )NZAnsweredZFlaggedZDeletedZSeenZDraft)Úflagsr?   rc   )r   )rC   r   r   rc   b  s    zLThreadedNetworkedTests.test_bracket_flags.<locals>.BracketFlagHandler.handlec             s   s&   |   d¡ d V | j_|  |dd¡ d S )Nrœ   rd   zFAKEAUTH successful)rM   rA   r   rR   )r   rP   rb   r   r   r   rž   f  s    

zVThreadedNetworkedTests.test_bracket_flags.<locals>.BracketFlagHandler.cmd_AUTHENTICATEc             S   s^   d  | j¡}|  d|  d¡¡ |  d¡ |  d¡ d| }|  | d¡¡ |  |dd¡ d S )	Nz \z* FLAGS (%s)r¢   s
   * 2 EXISTSs
   * 0 RECENTz-* OK [PERMANENTFLAGS %s \*)] Flags permitted.rd   z[READ-WRITE] SELECT completed.)rO   rî   rJ   rL   rR   )r   rP   rb   Zflag_msgÚmsgr   r   r   Ú
cmd_SELECTk  s    

zPThreadedNetworkedTests.test_bracket_flags.<locals>.BracketFlagHandler.cmd_SELECTc             S   sf   |d   d¡  d¡ ¡ }| j |¡ dd | j¡ }d|d |f }|  | d¡¡ |  |d	d
¡ d S )NrT   ú(ú)z(FLAGS (%s))z \z* %s FETCH %sr   r¢   rd   zSTORE completed.)rE   r[   rî   ÚextendrO   rJ   rL   rR   )r   rP   rb   Z	new_flagsZ	flags_msgrï   r   r   r   Ú	cmd_STOREu  s    zOThreadedNetworkedTests.test_bracket_flags.<locals>.BracketFlagHandler.cmd_STORE)r4   r5   r6   rc   rž   rð   rô   ri   r   r   )rC   r   ÚBracketFlagHandler`  s   
rõ   r£   c             S   s   dS )Ns   faker   )r¤   r   r   r   r¥   ~  rS   z;ThreadedNetworkedTests.test_bracket_flags.<locals>.<lambda>rd   s
   ZmFrZQ==
Útestó   1z+FLAGSz[test]s   [test]ZPERMANENTFLAGS)r>   rì   r§   r   r   ZselectÚstorer/   )r   rõ   rA   r€   rQ   r™   r˜   r   r   r   Útest_bracket_flagsW  s    	

z)ThreadedNetworkedTests.test_bracket_flagsc          	   C   sF   G dd„ dt jƒ}|  |¡ }| jtjj| jf|jžŽ  W d Q R X d S )Nc               @   s   e Zd Zdd„ ZdS )z9ThreadedNetworkedTests.test_issue5949.<locals>.EOFHandlerc             S   s   | j  d¡ d S )Ns   * OK)rF   rG   )r   r   r   r   rc   Œ  s    z@ThreadedNetworkedTests.test_issue5949.<locals>.EOFHandler.handleN)r4   r5   r6   rc   r   r   r   r   r…   ‹  s   r…   )	r†   r‡   rë   r,   r   r.   rˆ   r~   r   )r   r…   rA   r   r   r   Útest_issue5949ˆ  s    
z%ThreadedNetworkedTests.test_issue5949c          	   C   sD   G dd„ dt ƒ}|  |¡ }| jtjj| jf|jžŽ  W d Q R X d S )Nc               @   s   e Zd Zdd„ ZdS )zGThreadedNetworkedTests.test_line_termination.<locals>.BadNewlineHandlerc             S   s   |   d¡ |  |dd¡ d S )Ns   * CAPABILITY IMAP4rev1 AUTH
rd   zCAPABILITY completed)rI   rR   )r   rP   rb   r   r   r   rf   ™  s    
zVThreadedNetworkedTests.test_line_termination.<locals>.BadNewlineHandler.cmd_CAPABILITYN)r4   r5   r6   rf   r   r   r   r   r‹   —  s   r‹   )r>   rë   r,   r   r.   rˆ   r~   r   )r   r‹   rA   r   r   r   rŒ   ”  s    
z,ThreadedNetworkedTests.test_line_terminationc               @   s    e Zd ZdZdd„ Zdd„ ZdS )z!ThreadedNetworkedTests.UTF8ServerzAUTH ENABLE UTF8=ACCEPTc             C   s   |   |dd¡ d S )Nrd   zENABLE successful)rR   )r   rP   rb   r   r   r   r›   ¤  s    z,ThreadedNetworkedTests.UTF8Server.cmd_ENABLEc             c   s&   |   d¡ d V | j_|  |dd¡ d S )Nrœ   rd   zFAKEAUTH successful)rM   rA   r   rR   )r   rP   rb   r   r   r   rž   §  s    

z2ThreadedNetworkedTests.UTF8Server.cmd_AUTHENTICATEN)r4   r5   r6   re   r›   rž   r   r   r   r   r«   ¡  s   r«   c          	   C   sL   |   | j¡6\}}|  |j¡ |  tjj|jd¡ |  |j¡ W d Q R X d S )NrŽ   )	rì   r«   r   r   r,   r   r.   r+   r’   )r   rA   r€   r   r   r   r“   ¬  s    z;ThreadedNetworkedTests.test_enable_raises_error_if_not_AUTHc          	   C   sD   G dd„ d| j ƒ}|  |¡\}}|  tjj|jd¡ W d Q R X d S )Nc               @   s   e Zd ZdZdS )zXThreadedNetworkedTests.test_enable_raises_error_if_no_capability.<locals>.NoEnableServerrË   N)r4   r5   r6   re   r   r   r   r   ÚNoEnableServer·  s   rû   rŽ   )r«   rì   r,   r   r.   r+   r’   )r   rû   rA   r€   r   r   r   r”   µ  s    z@ThreadedNetworkedTests.test_enable_raises_error_if_no_capabilityc          
   C   sn   G dd„ dt ƒ}|  tjj¡F |  |¡0\}}| dd¡\}}|  |d¡ | d¡ W d Q R X W d Q R X d S )Nc               @   s   e Zd ZdS )z\ThreadedNetworkedTests.test_enable_UTF8_raises_error_if_not_supported.<locals>.NonUTF8ServerN)r4   r5   r6   r   r   r   r   ÚNonUTF8Server¾  s   rü   r•   r–   rd   zUTF8=ACCEPT)	r>   r,   r   r.   r+   rì   r—   r   r’   )r   rü   rA   r€   r˜   r™   r   r   r   rš   ¼  s    
zEThreadedNetworkedTests.test_enable_UTF8_raises_error_if_not_supportedc       	   	   C   sÖ   G dd„ d| j ƒ}|  |¡°\}}|  |jd¡ | ddd„ ¡\}}|  |d¡ |  |jd¡ | d	¡\}}|  |d¡ |  |jd
¡ d}| d d d | d
¡¡\}}|  |d¡ |  |jd|  d
¡¡ W d Q R X d S )Nc               @   s   e Zd Zdd„ ZdS )zMThreadedNetworkedTests.test_enable_UTF8_True_append.<locals>.UTF8AppendServerc             s   s&   |   d¡ d V | j_|  |dd¡ d S )Nrœ   rd   rŸ   )rM   rA   r   rR   )r   rP   rb   r   r   r   r    Ë  s    

zXThreadedNetworkedTests.test_enable_UTF8_True_append.<locals>.UTF8AppendServer.cmd_APPENDN)r4   r5   r6   r    r   r   r   r   r¡   Ê  s   r¡   r¢   r£   c             S   s   dS )Ns   faker   )r¤   r   r   r   r¥   Ò  rS   zEThreadedNetworkedTests.test_enable_UTF8_True_append.<locals>.<lambda>rd   s
   ZmFrZQ==
zUTF8=ACCEPTzutf-8u   Subject: Ã¼Ã±Ã­Â©Ã¶Ã°Ã©zUTF8 (%s)
)	r«   rì   r   r¦   r§   r   r’   r¨   rL   )	r   r¡   rA   r€   rQ   r‰   r©   r˜   r™   r   r   r   rª   Ç  s"    z3ThreadedNetworkedTests.test_enable_UTF8_True_appendc          	   C   s|   |   | j¡f\}}| ddd„ ¡\}}|  |d¡ | d¡\}}|  |d¡ |  |j¡ |  tj	j
|jdd¡ W d Q R X d S )Nr£   c             S   s   dS )Ns   faker   )r¤   r   r   r   r¥   è  rS   zSThreadedNetworkedTests.test_search_disallows_charset_in_utf8_mode.<locals>.<lambda>rd   zUTF8=ACCEPTrŽ   r¬   )rì   r«   r§   r   r’   r­   r   r,   r   r.   r+   r®   )r   rA   r€   r˜   r‰   r   r   r   r¯   å  s    zAThreadedNetworkedTests.test_search_disallows_charset_in_utf8_modec          
   C   sX   G dd„ dt ƒ}|  |¡4\}}|  tjj¡ | ddd„ ¡ W d Q R X W d Q R X d S )Nc               @   s   e Zd Zdd„ ZdS )z;ThreadedNetworkedTests.test_bad_auth_name.<locals>.MyServerc             S   s   |   |dd |d ¡¡ d S )Nr°   z#unrecognized authentication type {}r   )rR   r±   )r   rP   rb   r   r   r   rž   ô  s    zLThreadedNetworkedTests.test_bad_auth_name.<locals>.MyServer.cmd_AUTHENTICATEN)r4   r5   r6   rž   r   r   r   r   r²   ò  s   r²   r³   c               S   s   dS )Nr   r   r   r   r   r   r¥   ú  rS   z;ThreadedNetworkedTests.test_bad_auth_name.<locals>.<lambda>)r>   rì   r,   r   r.   r+   r§   )r   r²   rA   r€   r   r   r   r´   ï  s    z)ThreadedNetworkedTests.test_bad_auth_namec          
   C   s\   G dd„ dt ƒ}|  |¡8\}}|  tjj¡ | ddd„ ¡\}}W d Q R X W d Q R X d S )Nc               @   s   e Zd Zdd„ ZdS )zDThreadedNetworkedTests.test_invalid_authentication.<locals>.MyServerc             s   s$   |   d¡ d V | _|  |dd¡ d S )Nrœ   r°   z[AUTHENTICATIONFAILED] invalid)rM   r   rR   )r   rP   rb   r   r   r   rž     s    
zUThreadedNetworkedTests.test_invalid_authentication.<locals>.MyServer.cmd_AUTHENTICATEN)r4   r5   r6   rž   r   r   r   r   r²   ÿ  s   r²   r£   c             S   s   dS )Ns   faker   )r¤   r   r   r   r¥     rS   zDThreadedNetworkedTests.test_invalid_authentication.<locals>.<lambda>)r>   rì   r,   r   r.   r+   r§   )r   r²   rA   r€   rQ   r™   r   r   r   rµ   ü  s    z2ThreadedNetworkedTests.test_invalid_authenticationc          	   C   s¤   G dd„ dt ƒ}|  |¡8\}}| ddd„ ¡\}}|  |d¡ |  |jd¡ W d Q R X |  |¡8\}}| ddd„ ¡\}}|  |d¡ |  |jd¡ W d Q R X d S )	Nc               @   s   e Zd Zdd„ ZdS )zBThreadedNetworkedTests.test_valid_authentication.<locals>.MyServerc             s   s&   |   d¡ d V | j_|  |dd¡ d S )Nrœ   rd   zFAKEAUTH successful)rM   rA   r   rR   )r   rP   rb   r   r   r   rž     s    

zSThreadedNetworkedTests.test_valid_authentication.<locals>.MyServer.cmd_AUTHENTICATEN)r4   r5   r6   rž   r   r   r   r   r²     s   r²   r£   c             S   s   dS )Ns   faker   )r¤   r   r   r   r¥     rS   zBThreadedNetworkedTests.test_valid_authentication.<locals>.<lambda>rd   s
   ZmFrZQ==
c             S   s   dS )Nr·   r   )r¤   r   r   r   r¥     rS   )r>   rì   r§   r   r   )r   r²   rA   r€   rQ   r™   r   r   r   Útest_valid_authentication
  s    z0ThreadedNetworkedTests.test_valid_authenticationc          	   C   s    G dd„ dt ƒ}|  |¡6\}}|  d|jk¡ | dd¡\}}|  |d¡ W d Q R X |  |¡6\}}|  d|jk¡ | dd¡\}}|  |d¡ W d Q R X d S )Nc               @   s   e Zd ZdZdd„ ZdS )z?ThreadedNetworkedTests.test_login_cram_md5.<locals>.AuthHandlerzLOGINDISABLED AUTH=CRAM-MD5c             s   s:   |   d¡ d V }|dkr(|  |dd¡ n|  |dd¡ d S )Nz:+ PDE4OTYuNjk3MTcwOTUyQHBvc3RvZmZpY2UucmVzdG9uLm1jaS5uZXQ=s2   dGltIGYxY2E2YmU0NjRiOWVmYTFjY2E2ZmZkNmNmMmQ5ZjMy
rd   zCRAM-MD5 successfulr°   z	No access)rM   rR   )r   rP   rb   r¹   r   r   r   rž   '  s
    
zPThreadedNetworkedTests.test_login_cram_md5.<locals>.AuthHandler.cmd_AUTHENTICATEN)r4   r5   r6   re   rž   r   r   r   r   rº   #  s   rº   zAUTH=CRAM-MD5r»   r¿   rd   s   tanstaaftanstaaf)r>   rì   r­   re   r¼   r   )r   rº   rA   r€   r½   r™   r   r   r   Útest_login_cram_md5   s    z*ThreadedNetworkedTests.test_login_cram_md5c          
   C   s\   G dd„ dt ƒ}|  |¡8\}}|  tjj¡ | ddd„ ¡\}}W d Q R X W d Q R X d S )Nc               @   s   e Zd Zdd„ ZdS )zDThreadedNetworkedTests.test_aborted_authentication.<locals>.MyServerc             s   s>   |   d¡ d V | _| jdkr,|  |dd¡ n|  |dd¡ d S )Nrœ   s   *
r°   z[AUTHENTICATIONFAILED] abortedrd   zMYAUTH successful)rM   r   rR   )r   rP   rb   r   r   r   rž   A  s
    

zUThreadedNetworkedTests.test_aborted_authentication.<locals>.MyServer.cmd_AUTHENTICATEN)r4   r5   r6   rž   r   r   r   r   r²   ?  s   r²   r£   c             S   s   d S )Nr   )r¤   r   r   r   r¥   L  rS   zDThreadedNetworkedTests.test_aborted_authentication.<locals>.<lambda>)r>   rì   r,   r   r.   r+   r§   )r   r²   rA   r€   rQ   r™   r   r   r   rÁ   <  s    z2ThreadedNetworkedTests.test_aborted_authenticationc          	   C   sD   G dd„ dt ƒ}|  |¡ }| jtjj| jf|jžŽ  W d Q R X d S )Nc               @   s   e Zd Zdd„ ZdS )z?ThreadedNetworkedTests.test_linetoolong.<locals>.TooLongHandlerc             S   s   | j  dtjd  d ¡ d S )Ns   * OK ó   xs   
)rF   rG   r   Z_MAXLINE)r   r   r   r   rc   Q  s    zFThreadedNetworkedTests.test_linetoolong.<locals>.TooLongHandler.handleN)r4   r5   r6   rc   r   r   r   r   rÃ   P  s   rÃ   )r>   rë   r,   r   r.   r+   r~   r   )r   rÃ   rA   r   r   r   rÄ   O  s    
z'ThreadedNetworkedTests.test_linetoolongc          
   C   s2   |   t¡}| j|jŽ  W d Q R X W d Q R X d S )N)rë   r>   r~   r   )r   rA   r   r   r   rÅ   Y  s    z1ThreadedNetworkedTests.test_simple_with_statementc          
   C   sX   |   t¡D}| j|jŽ  }| dd¡ |  |jd¡ W d Q R X |  |j¡ W d Q R X d S )Nr•   r–   )rë   r>   r~   r   r—   r   rB   rÆ   )r   rA   rÇ   r   r   r   rÈ   `  s
    z*ThreadedNetworkedTests.test_with_statementc          
   C   sl   |   t¡X}| j|jŽ 4}| dd¡ |  |jd¡ | ¡  |  |j¡ W d Q R X |  |j¡ W d Q R X d S )Nr•   r–   )	rë   r>   r~   r   r—   r   rB   rÉ   rÆ   )r   rA   rÇ   r   r   r   rÊ   h  s    z1ThreadedNetworkedTests.test_with_statement_logoutN)#r4   r5   r6   r†   rÔ   ru   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Ä   rÅ   rÈ   rÊ   r   r   r   r   rç     s2    		1	

rç   c               @   s    e Zd ZeZeZedd„ ƒZdS )ÚThreadedNetworkedTestsSSLc          
   C   s˜   t  t j¡}| t¡ |  t jd¡6 |  t¡ }| j	|j
d|iŽ}| ¡  W d Q R X W d Q R X |  t¡$}| j	d|j
d |d}| ¡  W d Q R X d S )Nz=IP address mismatch, certificate is not valid for '127.0.0.1'rÖ   rÞ   r   )rÖ   )r8   r9   r×   rÚ   rÛ   r‘   rÜ   rë   r>   r~   r   r„   )r   rÖ   rA   r€   r   r   r   rß   y  s    

z+ThreadedNetworkedTestsSSL.test_ssl_verifiedN)	r4   r5   r6   r7   ru   ræ   r~   r   rß   r   r   r   r   r   t  s   r   Znetworkznetwork resource disabledc               @   sB   e Zd ZdZdZdZdZejZ	dd„ Z
dd„ Zd	d
„ Zdd„ ZdS )ÚRemoteIMAPTestzcyrus.andrew.cmu.edurá   Z	anonymousr–   c          	   C   s,   t | jƒ |  | j| j¡| _W d Q R X d S )N)r   Úhostr~   ÚportrA   )r   r   r   r   ÚsetUp–  s    zRemoteIMAPTest.setUpc          	   C   s.   | j d k	r*t| jƒ | j  ¡  W d Q R X d S )N)rA   r   r  rÉ   )r   r   r   r   ÚtearDownš  s    
zRemoteIMAPTest.tearDownc          	   C   sz   t | jƒf x| jjD ]}|  |t¡ qW |  d| jj¡ |  d| jj¡ | j | j| j	¡}|  
|d d¡ W d Q R X d S )NÚLOGINDISABLEDzAUTH=ANONYMOUSr   rd   )r   r  rA   re   ÚassertIsInstanceÚstrr/   r—   ÚusernameÚpasswordr   )r   ÚcapÚrsr   r   r   Útest_logincapaŸ  s    zRemoteIMAPTest.test_logincapac          	   C   s<   t | jƒ( | j ¡ }d | _|  |d d|¡ W d Q R X d S )Nr   rÍ   )r   r  rA   rÉ   r   )r   r  r   r   r   rÎ   ¨  s    
zRemoteIMAPTest.test_logoutN)r4   r5   r6   r  r  r	  r
  r   r.   r~   r  r  r  rÎ   r   r   r   r   r    s   	r  c                   s$   e Zd Z‡ fdd„Zdd„ Z‡  ZS )ÚRemoteIMAP_STARTTLSTestc          	      s>   t ƒ  ¡  t| jƒ  | j ¡ }|  |d d¡ W d Q R X d S )Nr   rd   )r?   r  r   r  rA   Zstarttlsr   )r   r  )rC   r   r   r  ´  s    

zRemoteIMAP_STARTTLSTest.setUpc             C   s2   x| j jD ]}|  |t¡ q
W |  d| j j¡ d S )Nr  )rA   re   r  r  ÚassertNotIn)r   r  r   r   r   r  º  s    z&RemoteIMAP_STARTTLSTest.test_logincapa)r4   r5   r6   r  r  ri   r   r   )rC   r   r  ¯  s   r  c               @   sT   e Zd ZdZe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 )ÚRemoteIMAP_SSLTestiá  c             C   s   d S )Nr   )r   r   r   r   r  Å  s    zRemoteIMAP_SSLTest.setUpc             C   s   d S )Nr   )r   r   r   r   r  È  s    zRemoteIMAP_SSLTest.tearDownc             C   s(   t  t j¡}d|_t j|_| t¡ |S )NF)r8   r9   r×   rÙ   Z	CERT_NONErØ   r:   r;   )r   rÖ   r   r   r   Úcreate_ssl_contextË  s
    
z%RemoteIMAP_SSLTest.create_ssl_contextc             C   sl   z\x|j D ]}|  |t¡ q
W |  d|j ¡ |  d|j ¡ | | j| j¡}|  |d d¡ W d | 	¡  X d S )Nr  z
AUTH=PLAINr   rd   )
re   r  r  r  r/   r—   r	  r
  r   rÉ   )r   rA   r  r  r   r   r   Úcheck_logincapaÒ  s    z"RemoteIMAP_SSLTest.check_logincapac          	   C   s4   t | jƒ  |  | j| j¡}|  |¡ W d Q R X d S )N)r   r  r~   r  r  )r   Ú_serverr   r   r   r  Ý  s    z!RemoteIMAP_SSLTest.test_logincapac          	   C   sD   t | jƒ0 |  | j| j¡}| ¡ }|  |d d|¡ W d Q R X d S )Nr   rÍ   )r   r  r~   r  rÉ   r   )r   r  r  r   r   r   rÎ   â  s    zRemoteIMAP_SSLTest.test_logoutc          	   C   s:   t | jƒ& | jt| j| j| jt|  ¡ d W d Q R X d S )N)râ   rÖ   )r   r  r,   Ú
ValueErrorr~   r  r;   r  )r   r   r   r   Ú#test_ssl_context_certfile_exclusiveè  s    z6RemoteIMAP_SSLTest.test_ssl_context_certfile_exclusivec          	   C   s:   t | jƒ& | jt| j| j| jt|  ¡ d W d Q R X d S )N)ZkeyfilerÖ   )r   r  r,   r  r~   r  r;   r  )r   r   r   r   Ú"test_ssl_context_keyfile_exclusiveî  s    z5RemoteIMAP_SSLTest.test_ssl_context_keyfile_exclusiveN)r4   r5   r6   r  ræ   r~   r  r  r  r  r  rÎ   r  r  r   r   r   r   r  À  s   r  Ú__main__)5rö   r   Ú
contextlibr   r0   r   Zos.pathÚosr†   r   r   rx   r)   Ztest.supportr   r   r   r   r   r	   Zunittestr
   r   r   r   r8   ÚImportErrorÚpathrO   ÚdirnameÚ__file__Úcurdirr;   rÛ   ZTestCaser   rÔ   r7   ræ   r‡   r>   rj   rÓ   Z
skipUnlessrÕ   rç   r   Zis_resource_enabledr  r  r  r4   Úmainr   r   r   r   Ú<module>   s`    
@	R  *$  c
 

4
