U
    c6{i—T  ã                   @   sô  d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlZ	d dl
mZmZmZmZmZ d dlmZ d dlmZmZmZ d dlmZ d d	lmZmZ d d
lmZ d dlmZ zd dl m!Z!m"Z" W n e#k
rÜ   dZ!dZ"Y nX G dd„ deƒZ$dddddddœZ%d0dd„Z&dd„ Z'G dd„ deƒZ(G dd„ dƒZ)G dd„ dƒZ*G dd„ deƒZ+G dd „ d eƒZ,G d!d"„ d"eƒZ-G d#d$„ d$eƒZ.G d%d&„ d&ƒZ/G d'd(„ d(ej0ƒZ1ee! d)ƒee 2ed¡ d)ƒG d*d+„ d+eƒƒƒZ3G d,d-„ d-e/eƒZ4G d.d/„ d/eƒZ5dS )1é    N)ÚList)ÚskipIf)ÚdirectlyProvides)ÚdeferÚerrorÚ
interfacesÚprotocolÚreactor)ÚStringTransport)ÚAdvancedPOP3ClientÚ InsecureAuthenticationDisallowedÚServerErrorResponse)Úpop3testserver)ÚbasicÚloopback)Úlog)ÚTestCase)ÚClientTLSContextÚServerTLSContextc                   @   s   e Zd Zdd„ ZdS )Ú#StringTransportWithConnectionLosingc                 C   s   | j  t ¡ ¡ d S ©N)r   ÚconnectionLostr   ZConnectionDone©Úself© r   úE/tmp/pip-unpacked-wheel-hfbvu0bz/twisted/mail/test/test_pop3client.pyÚloseConnection!   s    z2StringTransportWithConnectionLosing.loseConnectionN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r       s   r   s   180s   LOGIN)s   TOPs   LOGIN-DELAYs   UIDLó   STLSs   USERs   SASLTc                 C   s8   t ƒ }t|_tƒ }||_| |¡ | r0| d¡ ||fS )Ns   +OK Hello!
)Ú
POP3ClientÚcapCacheZ	_capCacher   r   ZmakeConnectionÚdataReceived)ÚgreetÚpÚtr   r   r   ÚsetUp/   s    

r'   c                 C   s   | fdd„S )Nc                 S   s   |ƒ S r   r   )ÚresultÚfr   r   r   Ú<lambda>A   ó    zstrip.<locals>.<lambda>r   )r)   r   r   r   Ústrip@   s    r,   c                   @   st   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S )ÚPOP3ClientLoginTestsc                    sD   t dd�\}}d|_| dd¡}| d¡ ˆ  |t¡ ‡ fdd„¡S )	NF©r$   Tó   usernameó   passwords   -ERR Offline for maintenance
c                    s   ˆ   | jd d¡S )Nr   s   Offline for maintenance©ÚassertEqualÚargs©Úexcr   r   r   r*   K   r+   z;POP3ClientLoginTests.testNegativeGreeting.<locals>.<lambda>)r'   ÚallowInsecureLoginÚloginr#   ÚassertFailurer   ÚaddCallback©r   r%   r&   Údr   r   r   ÚtestNegativeGreetingE   s    

ÿz)POP3ClientLoginTests.testNegativeGreetingc                 C   s<   t ƒ \}}| d¡}|  | ¡ d¡ | d¡ | | jd¡S )Nr/   ó   USER username
s   +OK send password
s   send password)r'   Úuserr2   Úvaluer#   r9   r:   r   r   r   Ú
testOkUserN   s
    


zPOP3ClientLoginTests.testOkUserc                    sH   t ƒ \}}| d¡}ˆ  | ¡ d¡ | d¡ ˆ  |t¡ ‡ fdd„¡S )Nr/   r=   s   -ERR account suspended
c                    s   ˆ   | jd d¡S )Nr   s   account suspendedr1   r4   r   r   r   r*   [   r+   z2POP3ClientLoginTests.testBadUser.<locals>.<lambda>)r'   r>   r2   r?   r#   r8   r   r9   r:   r   r   r   ÚtestBadUserU   s    



ÿz POP3ClientLoginTests.testBadUserc                 C   s<   t ƒ \}}| d¡}|  | ¡ d¡ | d¡ | | jd¡S )Nr0   ó   PASS password
s   +OK you're in!
s
   you're in!)r'   Úpasswordr2   r?   r#   r9   r:   r   r   r   Ú
testOkPass^   s
    


zPOP3ClientLoginTests.testOkPassc                    sH   t ƒ \}}| d¡}ˆ  | ¡ d¡ | d¡ ˆ  |t¡ ‡ fdd„¡S )Nr0   rB   s   -ERR go away
c                    s   ˆ   | jd d¡S )Nr   s   go awayr1   r4   r   r   r   r*   k   r+   z2POP3ClientLoginTests.testBadPass.<locals>.<lambda>)r'   rC   r2   r?   r#   r8   r   r9   r:   r   r   r   ÚtestBadPasse   s    



ÿz POP3ClientLoginTests.testBadPassc                 C   s^   t ƒ \}}d|_| dd¡}|  | ¡ d¡ | d¡ |  | ¡ d¡ | d¡ | | jd¡S )	NTr/   r0   r=   s   +OK go ahead
ó   USER username
PASS password
s   +OK password accepted
s   password accepted)r'   r6   r7   r2   r?   r#   r9   r:   r   r   r   ÚtestOkLoginn   s    


z POP3ClientLoginTests.testOkLoginc                    sj   t ƒ \}}d|_| dd¡}ˆ  | ¡ d¡ | d¡ ˆ  | ¡ d¡ | d¡ ˆ  |t¡ ‡ fdd	„¡S )
NTr/   r0   r=   s   +OK waiting on you
rF   ó   -ERR bogus login
c                    s   ˆ   | jd d¡S ©Nr   s   bogus loginr1   r4   r   r   r   r*   �   r+   z;POP3ClientLoginTests.testBadPasswordLogin.<locals>.<lambda>©	r'   r6   r7   r2   r?   r#   r8   r   r9   r:   r   r   r   ÚtestBadPasswordLoginx   s    



ÿz)POP3ClientLoginTests.testBadPasswordLoginc                    sP   t ƒ \}}d|_| dd¡}ˆ  | ¡ d¡ | d¡ ˆ  |t¡ ‡ fdd„¡S )NTr/   r0   r=   rH   c                    s   ˆ   | jd d¡S rI   r1   r4   r   r   r   r*   ‹   r+   z;POP3ClientLoginTests.testBadUsernameLogin.<locals>.<lambda>rJ   r:   r   r   r   ÚtestBadUsernameLogin„   s    


ÿz)POP3ClientLoginTests.testBadUsernameLoginc                 C   s*   t dd�\}}| d¡ |  |jd ¡ d S )NFr.   s"   +OK lalala this has no challenge
©r'   r#   r2   ZserverChallenge©r   r%   r&   r   r   r   ÚtestServerGreetingŽ   s    
z'POP3ClientLoginTests.testServerGreetingc                 C   s*   t dd�\}}| d¡ |  |jd¡ d S )NFr.   s   +OK <here is the challenge>
s   <here is the challenge>rM   rN   r   r   r   ÚtestServerGreetingWithChallenge“   s    
z4POP3ClientLoginTests.testServerGreetingWithChallengec                 C   sL   t dd�\}}| d¡ | dd¡}|  | ¡ d¡ | d¡ | | jd¡S )	NFr.   s"   +OK <challenge string goes here>
r/   r0   s0   APOP username f34f1e464d0d7927607753129cabe39a
s   +OK Welcome!
s   Welcome!)r'   r#   r7   r2   r?   r9   r:   r   r   r   ÚtestAPOP˜   s    
 ÿ
zPOP3ClientLoginTests.testAPOPc                 C   s>   t dd�\}}| d¡ | dd¡}|  | ¡ ¡ |  |t¡S )NFr.   ó   +OK Howdy
r/   r0   )r'   r#   r7   ZassertFalser?   r8   r   r:   r   r   r   Ú testInsecureLoginRaisesException¢   s
    
z5POP3ClientLoginTests.testInsecureLoginRaisesExceptionc                 C   sp   t dd�\}}t|tjƒ | d¡ | dd¡}|  | ¡ d¡ | ¡  | d¡ |  | ¡ d¡ | d¡ |S )	zÕ
        If a server doesn't offer APOP but the transport is secured using
        SSL or TLS, a plaintext login should be allowed, not rejected with
        an InsecureAuthenticationDisallowed exception.
        Fr.   rR   r/   r0   r=   s   +OK
rB   )	r'   r   r   ZISSLTransportr#   r7   r2   r?   Úclearr:   r   r   r   Ú testSSLTransportConsideredSecure©   s    


z5POP3ClientLoginTests.testSSLTransportConsideredSecureN)r   r   r   r<   r@   rA   rD   rE   rG   rK   rL   rO   rP   rQ   rS   rU   r   r   r   r   r-   D   s   			


r-   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚListConsumerc                 C   s
   i | _ d S r   ©Údatar   r   r   r   Ú__init__¼   s    zListConsumer.__init__c                 C   s    |\}}| j  |g ¡ |¡ d S r   )rX   Ú
setdefaultÚappend)r   r(   Úitemr?   r   r   r   Úconsume¿   s    zListConsumer.consumeN©r   r   r   rY   r]   r   r   r   r   rV   »   s   rV   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚMessageConsumerc                 C   s
   g | _ d S r   rW   r   r   r   r   rY   Å   s    zMessageConsumer.__init__c                 C   s   | j  |¡ d S r   )rX   r[   ©r   Úliner   r   r   r]   È   s    zMessageConsumer.consumeNr^   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S )ÚPOP3ClientListTestsc                 C   sJ   t ƒ \}}| ¡ }|  | ¡ d¡ | d¡ | d¡ | | jdddg¡S )Nó   LIST
ó   +OK Here it comes
s   1 3
2 2
3 1
.
é   é   é   )r'   ÚlistSizer2   r?   r#   r9   r:   r   r   r   ÚtestListSizeÍ   s    


z POP3ClientListTests.testListSizec              
   C   sª   t ƒ \}}tƒ }|j}| |¡}|  | ¡ d¡ | d¡ | d¡ |  |jdgdgdgdœ¡ | d¡ |  |jdgdgdgdgdgdgd	œ¡ | d
¡ | | j	|¡S )Nrc   rd   s   1 3
2 2
3 1
re   rf   rg   )r   rg   rf   s   5 3
6 2
7 1
)r   rg   rf   é   é   é   ó   .
)
r'   rV   r]   rh   r2   r?   r#   rX   r9   ÚassertIdentical©r   r%   r&   Úcr)   r;   r   r   r   ÚtestListSizeWithConsumerÕ   s    




(
z,POP3ClientListTests.testListSizeWithConsumerc                    sF   t ƒ \}}| ¡ }ˆ  | ¡ d¡ | d¡ ˆ  |t¡ ‡ fdd„¡S )Nrc   ó!   -ERR Fatal doom server exploded
c                    s   ˆ   | jd d¡S ©Nr   s   Fatal doom server explodedr1   r4   r   r   r   r*   é   r+   z8POP3ClientListTests.testFailedListSize.<locals>.<lambda>)r'   rh   r2   r?   r#   r8   r   r9   r:   r   r   r   ÚtestFailedListSizeã   s    


ÿz&POP3ClientListTests.testFailedListSizec                 C   sJ   t ƒ \}}| ¡ }|  | ¡ d¡ | d¡ | d¡ | | jdddg¡S )Nó   UIDL
rd   s   1 abc
2 def
3 ghi
.
ó   abcs   defs   ghi)r'   ÚlistUIDr2   r?   r#   r9   r:   r   r   r   ÚtestListUIDì   s    


zPOP3ClientListTests.testListUIDc                 C   sx   t ƒ \}}tƒ }|j}| |¡}|  | ¡ d¡ | d¡ | d¡ |  |jdgdgdgdœ¡ | d¡ | | j	|¡S )	Nru   rd   s   1 xyz
2 abc
5 mno
s   xyzrv   s   mno)r   rg   rj   rm   )
r'   rV   r]   rw   r2   r?   r#   rX   r9   rn   ro   r   r   r   ÚtestListUIDWithConsumerô   s    




z+POP3ClientListTests.testListUIDWithConsumerc                    sF   t ƒ \}}| ¡ }ˆ  | ¡ d¡ | d¡ ˆ  |t¡ ‡ fdd„¡S )Nru   rr   c                    s   ˆ   | jd d¡S rs   r1   r4   r   r   r   r*     r+   z7POP3ClientListTests.testFailedListUID.<locals>.<lambda>)r'   rw   r2   r?   r#   r8   r   r9   r:   r   r   r   ÚtestFailedListUID   s    


ÿz%POP3ClientListTests.testFailedListUIDN)	r   r   r   ri   rq   rt   rx   ry   rz   r   r   r   r   rb   Ì   s   	rb   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S )ÚPOP3ClientMessageTestsc                 C   s^   t ƒ \}}| d¡}|  | ¡ d¡ | d¡ | d¡ | d¡ | d¡ | | jddg¡S )	Né   ó   RETR 8
ó   +OK Message incoming
ó   La la la here is message text
s"   ..Further message text tra la la
rm   ó   La la la here is message texts   .Further message text tra la la©r'   Úretriever2   r?   r#   r9   r:   r   r   r   ÚtestRetrieve  s    





þz#POP3ClientMessageTests.testRetrievec                 C   s`   t ƒ \}}tƒ }|j}| d|¡}|  | ¡ d¡ | d¡ | d¡ | d¡ | | j||¡S )Nr|   r}   r~   r   s   ..Further message text
.
)	r'   r_   r]   r‚   r2   r?   r#   r9   Ú_cbTestRetrieveWithConsumerro   r   r   r   ÚtestRetrieveWithConsumer  s    



z/POP3ClientMessageTests.testRetrieveWithConsumerc                 C   s"   |   ||¡ |  |jddg¡ d S )Nr€   s   .Further message text©rn   r2   rX   ©r   r(   r)   rp   r   r   r   r„   #  s
     ÿz2POP3ClientMessageTests._cbTestRetrieveWithConsumerc                 C   sb   t ƒ \}}|jddd�}|  | ¡ d¡ | d¡ | d¡ | d¡ | d¡ | | jd	d
g¡S )Nr|   rf   ©Úlinesó	   TOP 8 2
ó   +OK 2 lines on the way
ó   Line the first!  Woop
ó   Line the last!  Bye
rm   ó   Line the first!  Woopó   Line the last!  Byer�   r:   r   r   r   ÚtestPartialRetrieve)  s    




 ÿz*POP3ClientMessageTests.testPartialRetrievec                 C   sn   t ƒ \}}tƒ }|j}|jd|dd�}|  | ¡ d¡ | d¡ | d¡ | d¡ | d¡ | | j||¡S )	Nr|   rf   rˆ   rŠ   r‹   rŒ   r�   rm   )	r'   r_   r]   r‚   r2   r?   r#   r9   Ú"_cbTestPartialRetrieveWithConsumerro   r   r   r   ÚtestPartialRetrieveWithConsumer5  s    




z6POP3ClientMessageTests.testPartialRetrieveWithConsumerc                 C   s"   |   ||¡ |  |jddg¡ d S )NrŽ   r�   r†   r‡   r   r   r   r‘   A  s    z9POP3ClientMessageTests._cbTestPartialRetrieveWithConsumerc                    sH   t ƒ \}}| d¡}ˆ  | ¡ d¡ | d¡ ˆ  |t¡ ‡ fdd„¡S )Nr   s   RETR 1
rr   c                    s   ˆ   | jd d¡S rs   r1   r4   r   r   r   r*   K  r+   z;POP3ClientMessageTests.testFailedRetrieve.<locals>.<lambda>)r'   r‚   r2   r?   r#   r8   r   r9   r:   r   r   r   ÚtestFailedRetrieveE  s    



ÿz)POP3ClientMessageTests.testFailedRetrievec                    s¢   t ƒ \‰ }‡ ‡fdd„tdƒD ƒ}tddƒD ]f}ˆ | ¡ d|f ¡ | ¡  ˆ  d¡ ˆ  d|f ¡ ˆ  d	|f ¡ ˆ | ¡ d
¡ ˆ  d¡ q,tj|dd�S )z‘
        Issue three retrieve calls immediately without waiting for any to
        succeed and make sure they all do succeed eventually.
        c              	      s8   g | ]0}ˆ   |¡ ˆjd |d f d|d f g¡‘qS )s   First line of %d.rg   s   Second line of %d.)r‚   r9   r2   )Ú.0Úi©r%   r   r   r   Ú
<listcomp>T  s
   ü
þzCPOP3ClientMessageTests.test_concurrentRetrieves.<locals>.<listcomp>re   rg   rj   s	   RETR %d
r‹   s   First line of %d.
s   Second line of %d.
r+   rm   T)ZfireOnOneErrback)r'   Úranger2   r?   rT   r#   r   ZDeferredList)r   r&   Úmessagesr•   r   r–   r   Útest_concurrentRetrievesN  s    
û
z/POP3ClientMessageTests.test_concurrentRetrievesN)r   r   r   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S )ÚPOP3ClientMiscTestsc              
   C   sb   t ƒ \}}|jdd�}|  | ¡ d¡ | d¡ | d¡ | | jd d d dddgddgdgd	œ¡S )
Nr   ©ZuseCacheó   CAPA
s   +OK Capabilities on the way
s!   X
Y
Z
A 1 2 3
B 1 2
C 1
.
ó   1ó   2ó   3)ó   Xó   Yó   Zó   Aó   Bó   C©r'   Zcapabilitiesr2   r?   r#   r9   r:   r   r   r   ÚtestCapabilityi  s    


úþz"POP3ClientMiscTests.testCapabilityc                 C   s>   t ƒ \}}|jdd�}|  | ¡ d¡ | d¡ | | ji ¡S )Nr   rœ   r�   ó   -ERR This server is lame!
r§   r:   r   r   r   ÚtestCapabilityError{  s
    

z'POP3ClientMiscTests.testCapabilityErrorc                 C   s:   t ƒ \}}| ¡ }|  | ¡ d¡ | d¡ | | jd¡S )Nó   STAT
s   +OK 1 1212
)rg   i¼  )r'   Ústatr2   r?   r#   r9   r:   r   r   r   ÚtestStat‚  s
    

zPOP3ClientMiscTests.testStatc                    sF   t ƒ \}}| ¡ }ˆ  | ¡ d¡ | d¡ ˆ  |t¡ ‡ fdd„¡S )Nr«   r©   c                    s   ˆ   | jd d¡S ©Nr   s   This server is lame!r1   r4   r   r   r   r*   �  r+   z3POP3ClientMiscTests.testStatError.<locals>.<lambda>)r'   r¬   r2   r?   r#   r8   r   r9   r:   r   r   r   ÚtestStatError‰  s    


ÿz!POP3ClientMiscTests.testStatErrorc                 C   s:   t ƒ \}}| ¡ }|  | ¡ d¡ | d¡ | | jd¡S )Nó   NOOP
s   +OK No-op to you too!
s   No-op to you too!)r'   Únoopr2   r?   r#   r9   r:   r   r   r   ÚtestNoop’  s
    

zPOP3ClientMiscTests.testNoopc                    sF   t ƒ \}}| ¡ }ˆ  | ¡ d¡ | d¡ ˆ  |t¡ ‡ fdd„¡S )Nr°   r©   c                    s   ˆ   | jd d¡S r®   r1   r4   r   r   r   r*   Ÿ  r+   z3POP3ClientMiscTests.testNoopError.<locals>.<lambda>)r'   r±   r2   r?   r#   r8   r   r9   r:   r   r   r   ÚtestNoopError™  s    


ÿz!POP3ClientMiscTests.testNoopErrorc                 C   s:   t ƒ \}}| ¡ }|  | ¡ d¡ | d¡ | | jd¡S )Nó   RSET
s   +OK Reset state
s   Reset state)r'   Úresetr2   r?   r#   r9   r:   r   r   r   ÚtestRset¢  s
    

zPOP3ClientMiscTests.testRsetc                    sF   t ƒ \}}| ¡ }ˆ  | ¡ d¡ | d¡ ˆ  |t¡ ‡ fdd„¡S )Nr´   r©   c                    s   ˆ   | jd d¡S r®   r1   r4   r   r   r   r*   ¯  r+   z3POP3ClientMiscTests.testRsetError.<locals>.<lambda>)r'   rµ   r2   r?   r#   r8   r   r9   r:   r   r   r   ÚtestRsetError©  s    


ÿz!POP3ClientMiscTests.testRsetErrorc                 C   s<   t ƒ \}}| d¡}|  | ¡ d¡ | d¡ | | jd¡S )Nre   ó   DELE 4
s   +OK Hasta la vista
s   Hasta la vista)r'   Údeleter2   r?   r#   r9   r:   r   r   r   Ú
testDelete²  s
    


zPOP3ClientMiscTests.testDeletec                    sH   t ƒ \}}| d¡}ˆ  | ¡ d¡ | d¡ ˆ  |t¡ ‡ fdd„¡S )Nre   r¸   s   -ERR Winner is not you.
c                    s   ˆ   | jd d¡S )Nr   s   Winner is not you.r1   r4   r   r   r   r*   ¿  r+   z5POP3ClientMiscTests.testDeleteError.<locals>.<lambda>)r'   r¹   r2   r?   r#   r8   r   r9   r:   r   r   r   ÚtestDeleteError¹  s    



ÿz#POP3ClientMiscTests.testDeleteErrorN)r   r   r   r¨   rª   r­   r¯   r²   r³   r¶   r·   rº   r»   r   r   r   r   r›   h  s   			r›   c                   @   s   e Zd Zddd„Zdd„ ZdS )ÚSimpleClientNc                 C   s   || _ d| _d S )NT)Údeferredr6   )r   r½   ÚcontextFactoryr   r   r   rY   Ä  s    zSimpleClient.__init__c                 C   s   | j  d ¡ d S r   )r½   Úcallback)r   Ú	challenger   r   r   ÚserverGreetingÈ  s    zSimpleClient.serverGreeting)N)r   r   r   rY   rÁ   r   r   r   r   r¼   Ã  s   
r¼   c                   @   s<   e Zd ZdZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Z	dS )ÚPOP3HelperMixinNc                 C   s:   t  ¡ }tj| jd�| _t|| jd�| _d| j_	|| _
d S )N)r¾   é   )r   ÚDeferredr   ZPOP3TestServerÚ	serverCTXÚserverr¼   Ú	clientCTXÚclientÚtimeoutÚ	connected)r   r;   r   r   r   r'   Ð  s
    zPOP3HelperMixin.setUpc                 C   s   | ` | `| `d S r   )rÆ   rÈ   rÊ   r   r   r   r   ÚtearDown×  s    zPOP3HelperMixin.tearDownc                 C   s   | j j ¡  d S r   )rÈ   Ú	transportr   )r   Úignorer   r   r   Ú_cbStopClientÜ  s    zPOP3HelperMixin._cbStopClientc                 C   s   | j j ¡  | jj ¡  |S r   )rÈ   rÌ   r   rÆ   )r   Úfailurer   r   r   Ú
_ebGeneralß  s    zPOP3HelperMixin._ebGeneralc                 C   s   t j| j| jdd�S )NF)Znoisy)r   ZloopbackTCPrÆ   rÈ   r   r   r   r   r   ä  s    zPOP3HelperMixin.loopback)
r   r   r   rÅ   rÇ   r'   rË   rÎ   rÐ   r   r   r   r   r   rÂ   Ì  s   rÂ   c                   @   s   e Zd ZG dd„ dejƒZdS )ÚTLSServerFactoryc                   @   s2   e Zd ZU dZg Zee ed< dd„ Zdd„ Z	dS )zTLSServerFactory.protocolNÚoutputc                 C   s8   g | j _| jd d … | _| j d¡D ]}|  |¡ q$d S )Nr   )ÚfactoryÚinputrÒ   ÚpopÚsendLiner`   r   r   r   ÚconnectionMadeí  s    z(TLSServerFactory.protocol.connectionMadec                    sB   ˆ j j |¡ ‡ fdd„ˆ j d¡D ƒ |dkr>ˆ j ˆ j¡ d S )Nc                    s   g | ]}ˆ   |¡‘qS r   )rÖ   )r”   Úlr   r   r   r—   õ  s     z:TLSServerFactory.protocol.lineReceived.<locals>.<listcomp>r   r    )rÓ   rÔ   r[   rÒ   rÕ   rÌ   ÚstartTLSÚcontextr`   r   r   r   ÚlineReceivedó  s    z&TLSServerFactory.protocol.lineReceived)
r   r   r   rÚ   rÒ   r   ÚbytesÚ__annotations__r×   rÛ   r   r   r   r   r   é  s   
r   N)r   r   r   r   ZLineReceiverr   r   r   r   r   rÑ   è  s   rÑ   zOpenSSL not presentc                   @   s   e Zd ZdZdd„ ZdS )ÚPOP3TLSTestsz=
    Tests for POP3Client's support for TLS connections.
    c           
         s  t ƒ ‰dgdddgdgddgdggˆj_tƒ ˆj_tjdˆdd�}ˆ |j¡ | 	¡ j
}| 	¡ j}t ¡ ‰tt ¡ tƒ ƒ‰‡‡fdd„}|ˆ_t ¡ }‡fd	d
„|_t |||¡‰ ‡fdd„}‡fdd„}‡‡fdd„}‡ ‡fdd„}	ˆj |¡ ˆj |¡ ˆj |¡ ˆj |	¡ ˆjS )zd
        POP3Client.startTLS starts a TLS session over its existing TCP
        connection.
        s   +OKr    ó   .r   z	127.0.0.1)Z	interfacec                    s   t  ˆ| ¡ ˆ  d ¡ d S r   )r¼   r   r¿   )Úreason)ÚconnLostDeferredÚcpr   r   r     s    z2POP3TLSTests.test_startTLS.<locals>.connectionLostc                      s   ˆ S r   r   r   ©râ   r   r   r*     r+   z,POP3TLSTests.test_startTLS.<locals>.<lambda>c                    s   t  d¡ ˆ  ¡ S )Nz!Connected to server; starting TLS)r   ÚmsgrÙ   ©Zignoredrã   r   r   ÚcbConnected!  s    
z/POP3TLSTests.test_startTLS.<locals>.cbConnectedc                    s   t  d¡ ˆ  ¡ S )NzStarted TLS; disconnecting)r   rä   Úquitrå   rã   r   r   ÚcbStartedTLS%  s    
z0POP3TLSTests.test_startTLS.<locals>.cbStartedTLSc                    s$   t  d¡ ˆ  ˆjddddg¡ d S )Nz.Disconnected; asserting correct input receiveds   CAPAr    s   QUIT)r   rä   r2   rÔ   )Zign)r   Úsfr   r   ÚcbDisconnected)  s    
z2POP3TLSTests.test_startTLS.<locals>.cbDisconnectedc                    s   t  d¡ ˆ  ¡  ˆS )NzEAsserted correct input; disconnecting client and shutting down server)r   rä   Z
disconnect©r(   )Úconnrá   r   r   Úcleanup-  s
    ÿz+POP3TLSTests.test_startTLS.<locals>.cleanup)rÑ   r   rÒ   r   rÚ   r	   Z	listenTCPZ
addCleanupÚstopListeningZgetHostÚhostÚportr   rÄ   r¼   r   r   ZClientFactoryZ
connectTCPr½   r9   ZaddBoth)
r   rð   ÚHÚPr   Úcfræ   rè   rê   rí   r   )rì   rá   râ   r   ré   r   Útest_startTLS  s8    û


zPOP3TLSTests.test_startTLSN)r   r   r   Ú__doc__rô   r   r   r   r   rÞ   ú  s   rÞ   c                   @   s   e Zd Zdd„ ZdS )ÚPOP3TimeoutTestsc                    s‚   ‡ ‡‡fdd„}‡fdd„‰ ‡fdd„‰‡fdd„}d	ˆj _d
t_||g}tˆjjtt|ƒƒ ˆj ˆj¡ ˆj 	ˆj
¡ ˆ ¡ S )Nc                     s&   ˆj  dd¡} |  ˆ ¡ |  ˆ¡ | S )NÚtestÚtwisted)rÈ   r7   r9   Ú
addErrback)r;   ©ÚloggedInr   ÚtimedOutr   r   r7   ?  s    

z+POP3TimeoutTests.testTimeout.<locals>.loginc                    s   ˆ   d¡ d S )Nz%Successfully logged in!?  Impossible!)Zfailrë   r   r   r   rû   E  s    z.POP3TimeoutTests.testTimeout.<locals>.loggedInc                    s   |   tj¡ ˆ  d ¡ d S r   )Ztrapr   ÚTimeoutErrorrÎ   )rÏ   r   r   r   rü   H  s    z.POP3TimeoutTests.testTimeout.<locals>.timedOutc                      s
   ˆ j  ¡ S r   )rÈ   rç   r   r   r   r   rç   L  s    z*POP3TimeoutTests.testTimeout.<locals>.quitg{®Gáz„?T)rÈ   rÉ   r   ZTIMEOUT_RESPONSEÚmaprÊ   r9   r,   rÎ   rù   rÐ   r   )r   r7   rç   Úmethodsr   rú   r   ÚtestTimeout>  s    zPOP3TimeoutTests.testTimeoutN)r   r   r   r   r   r   r   r   rö   =  s   rö   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚPOP3ClientModuleStructureTestszt
    Miscellaneous tests more to do with module/package structure than
    anything to do with the POP3 client.
    c                 C   s   |   tjjjg ¡ dS )zŒ
        twisted.mail._pop3client.__all__ should be empty because all classes
        should be imported through twisted.mail.pop3.
        N)r2   rø   ÚmailZ_pop3clientÚ__all__r   r   r   r   Útest_allb  s    z'POP3ClientModuleStructureTests.test_allc                 C   sn   dd„ t  tjd t j¡D ƒ}|D ]F}|dksT|  ttjj	|ƒ|› dtjj	› �¡ q"|  ttjj	dƒ¡ q"dS )a  
        Every public class in twisted.mail._pop3client should be available as
        a member of twisted.mail.pop3 with the exception of
        twisted.mail._pop3client.POP3Client which should be available as
        twisted.mail.pop3.AdvancedClient.
        c                 S   s$   g | ]}|d  d  dks|d  ‘qS )r   Ú_r   )r”   rp   r   r   r   r—   p  s   üz>POP3ClientModuleStructureTests.test_import.<locals>.<listcomp>ztwisted.mail._pop3clientr!   z not in r   N)
ÚinspectÚ
getmembersÚsysÚmodulesÚisclassÚ
assertTrueÚhasattrrø   r  Zpop3)r   ZpublicClassesZpcr   r   r   Útest_importi  s     ÿþþz*POP3ClientModuleStructureTests.test_importN)r   r   r   rõ   r  r  r   r   r   r   r  \  s   r  )T)6r  r  Útypingr   Zunittestr   Zzope.interfacer   Ztwisted.mail._pop3clientrø   Ztwisted.internetr   r   r   r   r	   Ztwisted.internet.testingr
   Ztwisted.mail.pop3r   r!   r   r   Ztwisted.mail.testr   Ztwisted.protocolsr   r   Ztwisted.pythonr   Ztwisted.trial.unittestr   Ztwisted.test.ssl_helpersr   r   ÚImportErrorr   r"   r'   r,   r-   rV   r_   rb   r{   r›   r¼   rÂ   ZServerFactoryrÑ   ZIReactorSSLrÞ   rö   r  r   r   r   r   Ú<module>   sR   
ú

w	>^[	
A