U
    
W[âU  ã                   @   s,  d dl Z d dlZd dlmZ d dlmZmZmZmZm	Z	 d dl
mZ d dl
mZ d dl
mZ d dlmZ d dlmZmZ d d	lmZ d d
l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ZY nX G dd„ deƒZ dddddddœZ!d2dd„Z"dd„ Z#G dd„ dej$ƒZ%G dd„ dƒZ&G dd„ 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ƒZ+G d&d'„ d'ƒZ,G d(d)„ d)ej-ƒZ.G d*d+„ d+ej$ƒZ/G d,d-„ d-e,ej$ƒZ0edkrèe/fD ]Z1d.e1_2qØn&e	 3ed¡dkre/fD ]Z1d/e1_2q d dl4Z5G d0d1„ d1ej$ƒZ6dS )3é    N)ÚdirectlyProvides)ÚreactorÚdeferÚerrorÚprotocolÚ
interfaces)ÚAdvancedPOP3Client)Ú InsecureAuthenticationDisallowed)ÚServerErrorResponse)Úpop3testserver)ÚbasicÚloopback)Úlog)Ú
intToBytes)ÚStringTransport)Úunittest)Ú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   úC/usr/lib/python3/dist-packages/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>7   ó    zstrip.<locals>.<lambda>r   )r(   r   r   r   Ústrip6   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)   C   s   ÿz;POP3ClientLoginTests.testNegativeGreeting.<locals>.<lambda>)r&   ÚallowInsecureLoginÚloginr"   ÚassertFailurer
   ÚaddCallback©r   r$   r%   Údr   r   r   ÚtestNegativeGreeting<   s    
 ÿ
þz)POP3ClientLoginTests.testNegativeGreetingc                 C   s<   t ƒ \}}| d¡}|  | ¡ d¡ | d¡ | | jd¡S )Nr.   ó   USER username
s   +OK send password
s   send password)r&   Úuserr1   Úvaluer"   r8   r9   r   r   r   Ú
testOkUserG   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 suspendedr0   r3   r   r   r   r)   V   r*   z2POP3ClientLoginTests.testBadUser.<locals>.<lambda>)r&   r=   r1   r>   r"   r7   r
   r8   r9   r   r   r   ÚtestBadUserO   s    


 ÿ
þz POP3ClientLoginTests.testBadUserc                 C   s<   t ƒ \}}| d¡}|  | ¡ d¡ | d¡ | | jd¡S )Nr/   ó   PASS password
s   +OK you're in!
s
   you're in!)r&   Úpasswordr1   r>   r"   r8   r9   r   r   r   Ú
testOkPassY   s
    


zPOP3ClientLoginTests.testOkPassc                    sH   t ƒ \}}| d¡}ˆ  | ¡ d¡ | d¡ ˆ  |t¡ ‡ fdd„¡S )Nr/   rA   s   -ERR go away
c                    s   ˆ   | jd d¡S )Nr   s   go awayr0   r3   r   r   r   r)   h   r*   z2POP3ClientLoginTests.testBadPass.<locals>.<lambda>)r&   rB   r1   r>   r"   r7   r
   r8   r9   r   r   r   ÚtestBadPassa   s    


 ÿ
þz POP3ClientLoginTests.testBadPassc                 C   s^   t ƒ \}}d|_| dd¡}|  | ¡ d¡ | d¡ |  | ¡ d¡ | d¡ | | jd¡S )	NTr.   r/   r<   s   +OK go ahead
ó   USER username
PASS password
s   +OK password accepted
s   password accepted)r&   r5   r6   r1   r>   r"   r8   r9   r   r   r   ÚtestOkLogink   s    


z POP3ClientLoginTests.testOkLoginc                    sj   t ƒ \}}d|_| dd¡}ˆ  | ¡ d¡ | d¡ ˆ  | ¡ d¡ | d¡ ˆ  |t¡ ‡ fdd	„¡S )
NTr.   r/   r<   s   +OK waiting on you
rE   ó   -ERR bogus login
c                    s   ˆ   | jd d¡S ©Nr   s   bogus loginr0   r3   r   r   r   r)   €   r*   z;POP3ClientLoginTests.testBadPasswordLogin.<locals>.<lambda>©	r&   r5   r6   r1   r>   r"   r7   r
   r8   r9   r   r   r   ÚtestBadPasswordLoginv   s    


 ÿ
þz)POP3ClientLoginTests.testBadPasswordLoginc                    sP   t ƒ \}}d|_| dd¡}ˆ  | ¡ d¡ | d¡ ˆ  |t¡ ‡ fdd„¡S )NTr.   r/   r<   rG   c                    s   ˆ   | jd d¡S rH   r0   r3   r   r   r   r)   ‹   r*   z;POP3ClientLoginTests.testBadUsernameLogin.<locals>.<lambda>rI   r9   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"   r1   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>rL   rM   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.   r/   s0   APOP username f34f1e464d0d7927607753129cabe39a
s   +OK Welcome!
s   Welcome!)r&   r"   r6   r1   r>   r8   r9   r   r   r   ÚtestAPOPš   s    

ÿ
zPOP3ClientLoginTests.testAPOPc                 C   s>   t dd\}}| d¡ | dd¡}|  | ¡ ¡ |  |t¡S )NFr-   ó   +OK Howdy
r.   r/   )r&   r"   r6   ZassertFalser>   r7   r	   r9   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-   rQ   r.   r/   r<   s   +OK
rA   )	r&   r   r   ZISSLTransportr"   r6   r1   r>   Úclearr9   r   r   r   Ú testSSLTransportConsideredSecure­   s    


z5POP3ClientLoginTests.testSSLTransportConsideredSecureN)r   r   r   r;   r?   r@   rC   rD   rF   rJ   rK   rN   rO   rP   rR   rT   r   r   r   r   r,   ;   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   )rW   Ú
setdefaultÚappend)r   r'   Úitemr>   r   r   r   ÚconsumeÅ   s    zListConsumer.consumeN©r   r   r   rX   r\   r   r   r   r   rU   À   s   rU   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚMessageConsumerc                 C   s
   g | _ d S r   rV   r   r   r   r   rX   Ì   s    zMessageConsumer.__init__c                 C   s   | j  |¡ d S r   )rW   rZ   ©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&   ÚlistSizer1   r>   r"   r8   r9   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 )Nrb   rc   s   1 3
2 2
3 1
rd   re   rf   )r   rf   re   s   5 3
6 2
7 1
)r   rf   re   é   é   é   ó   .
)
r&   rU   r\   rg   r1   r>   r"   rW   r8   ÚassertIdentical©r   r$   r%   Úcr(   r:   r   r   r   ÚtestListSizeWithConsumerß   s    




ÿ
z,POP3ClientListTests.testListSizeWithConsumerc                    sF   t ƒ \}}| ¡ }ˆ  | ¡ d¡ | d¡ ˆ  |t¡ ‡ fdd„¡S )Nrb   ó!   -ERR Fatal doom server exploded
c                    s   ˆ   | jd d¡S ©Nr   s   Fatal doom server explodedr0   r3   r   r   r   r)   ö   s   ÿz8POP3ClientListTests.testFailedListSize.<locals>.<lambda>)r&   rg   r1   r>   r"   r7   r
   r8   r9   r   r   r   ÚtestFailedListSizeï   s    

 ÿ
þz&POP3ClientListTests.testFailedListSizec                 C   sJ   t ƒ \}}| ¡ }|  | ¡ d¡ | d¡ | d¡ | | jdddg¡S )Nó   UIDL
rc   s   1 abc
2 def
3 ghi
.
ó   abcs   defs   ghi)r&   ÚlistUIDr1   r>   r"   r8   r9   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 )	Nrt   rc   s   1 xyz
2 abc
5 mno
s   xyzru   s   mno)r   rf   ri   rl   )
r&   rU   r\   rv   r1   r>   r"   rW   r8   rm   rn   r   r   r   ÚtestListUIDWithConsumer  s    




z+POP3ClientListTests.testListUIDWithConsumerc                    sF   t ƒ \}}| ¡ }ˆ  | ¡ d¡ | d¡ ˆ  |t¡ ‡ fdd„¡S )Nrt   rq   c                    s   ˆ   | jd d¡S rr   r0   r3   r   r   r   r)     s   ÿz7POP3ClientListTests.testFailedListUID.<locals>.<lambda>)r&   rv   r1   r>   r"   r7   r
   r8   r9   r   r   r   ÚtestFailedListUID  s    

 ÿ
þz%POP3ClientListTests.testFailedListUIDN)	r   r   r   rh   rp   rs   rw   rx   ry   r   r   r   r   ra   Õ   s   		ra   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
rl   ó   La la la here is message texts   .Further message text tra la la©r&   Úretriever1   r>   r"   r8   r9   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   r1   r>   r"   r8   Ú_cbTestRetrieveWithConsumerrn   r   r   r   ÚtestRetrieveWithConsumer+  s    



z/POP3ClientMessageTests.testRetrieveWithConsumerc                 C   s"   |   ||¡ |  |jddg¡ d S )Nr   s   .Further message text©rm   r1   rW   ©r   r'   r(   ro   r   r   r   rƒ   7  s    
ÿz2POP3ClientMessageTests._cbTestRetrieveWithConsumerc                 C   sb   t ƒ \}}|jddd}|  | ¡ d¡ | d¡ | d¡ | d¡ | d¡ | | jd	d
g¡S )Nr{   re   ©Úlinesó	   TOP 8 2
ó   +OK 2 lines on the way
ó   Line the first!  Woop
ó   Line the last!  Bye
rl   ó   Line the first!  Woopó   Line the last!  Byer€   r9   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{   re   r‡   r‰   rŠ   r‹   rŒ   rl   )	r&   r^   r\   r   r1   r>   r"   r8   Ú"_cbTestPartialRetrieveWithConsumerrn   r   r   r   ÚtestPartialRetrieveWithConsumerK  s    




z6POP3ClientMessageTests.testPartialRetrieveWithConsumerc                 C   s"   |   ||¡ |  |jddg¡ d S )Nr   rŽ   r…   r†   r   r   r   r   X  s    
ÿz9POP3ClientMessageTests._cbTestPartialRetrieveWithConsumerc                    sH   t ƒ \}}| d¡}ˆ  | ¡ d¡ | d¡ ˆ  |t¡ ‡ fdd„¡S )Nr   s   RETR 1
rq   c                    s   ˆ   | jd d¡S rr   r0   r3   r   r   r   r)   e  s   ÿz;POP3ClientMessageTests.testFailedRetrieve.<locals>.<lambda>)r&   r   r1   r>   r"   r7   r
   r8   r9   r   r   r   ÚtestFailedRetrieve^  s    


 ÿ
þz)POP3ClientMessageTests.testFailedRetrievec                    s´   t ƒ \‰ }‡ ‡fdd„tdƒD ƒ}tddƒD ]x}ˆ | ¡ dt|ƒ d ¡ | ¡  ˆ  d¡ ˆ  d	t|ƒ d
 ¡ ˆ  dt|ƒ d
 ¡ ˆ | ¡ 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              
      sD   g | ]<}ˆ   |¡ ˆjd t|d ƒ d dt|d ƒ d g¡‘qS )ó   First line of rf   ó   .ó   Second line of )r   r8   r1   r   )Ú.0Úi©r$   r   r   r   Ú
<listcomp>o  s   ü
ÿþzCPOP3ClientMessageTests.test_concurrentRetrieves.<locals>.<listcomp>rd   rf   ri   s   RETR s   
rŠ   r“   rl   r•   r*   T)ZfireOnOneErrback)	r&   Úranger1   r>   r   rS   r"   r   ZDeferredList)r   r%   Zmessagesr—   r   r˜   r   Útest_concurrentRetrievesi  s    
ú
z/POP3ClientMessageTests.test_concurrentRetrievesN)r   r   r   r‚   r„   rƒ   r   r‘   r   r’   r›   r   r   r   r   rz     s   rz   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capabilitiesr1   r>   r"   r8   r9   r   r   r   ÚtestCapability…  s    


ýþz"POP3ClientMiscTests.testCapabilityc                 C   s>   t ƒ \}}|jdd}|  | ¡ d¡ | d¡ | | ji ¡S )Nr   r   rž   ó   -ERR This server is lame!
r¨   r9   r   r   r   ÚtestCapabilityError“  s
    

z'POP3ClientMiscTests.testCapabilityErrorc                 C   s:   t ƒ \}}| ¡ }|  | ¡ d¡ | d¡ | | jd¡S )Nó   STAT
s   +OK 1 1212
)rf   i¼  )r&   Ústatr1   r>   r"   r8   r9   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!r0   r3   r   r   r   r)   ª  r*   z3POP3ClientMiscTests.testStatError.<locals>.<lambda>)r&   r­   r1   r>   r"   r7   r
   r8   r9   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&   Únoopr1   r>   r"   r8   r9   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¯   r0   r3   r   r   r   r)   ¼  r*   z3POP3ClientMiscTests.testNoopError.<locals>.<lambda>)r&   r²   r1   r>   r"   r7   r
   r8   r9   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&   Úresetr1   r>   r"   r8   r9   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¯   r0   r3   r   r   r   r)   Î  r*   z3POP3ClientMiscTests.testRsetError.<locals>.<lambda>)r&   r¶   r1   r>   r"   r7   r
   r8   r9   r   r   r   ÚtestRsetErrorÇ  s    

 ÿ
þz!POP3ClientMiscTests.testRsetErrorc                 C   s<   t ƒ \}}| d¡}|  | ¡ d¡ | d¡ | | jd¡S )Nrd   ó   DELE 4
s   +OK Hasta la vista
s   Hasta la vista)r&   Údeleter1   r>   r"   r8   r9   r   r   r   Ú
testDeleteÑ  s
    


zPOP3ClientMiscTests.testDeletec                    sH   t ƒ \}}| d¡}ˆ  | ¡ d¡ | d¡ ˆ  |t¡ ‡ fdd„¡S )Nrd   r¹   s   -ERR Winner is not you.
c                    s   ˆ   | jd d¡S )Nr   s   Winner is not you.r0   r3   r   r   r   r)   à  r*   z5POP3ClientMiscTests.testDeleteError.<locals>.<lambda>)r&   rº   r1   r>   r"   r7   r
   r8   r9   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œ   „  s   


rœ   c                   @   s   e Zd Zddd„Zdd„ ZdS )ÚSimpleClientNc                 C   s   || _ d| _d S )NT)Údeferredr5   )r   r¾   ÚcontextFactoryr   r   r   rX   å  s    zSimpleClient.__init__c                 C   s   | j  d ¡ d S r   )r¾   Úcallback)r   Z	challenger   r   r   ÚserverGreetingê  s    zSimpleClient.serverGreeting)N)r   r   r   rX   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                   @   s$   e Zd ZdZg Zdd„ Zdd„ ZdS )zTLSServerFactory.protocolNc                 C   s8   g | j _| jd d … | _| j d¡D ]}|  |¡ q$d S )Nr   )ÚfactoryÚinputÚoutputÚ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Ó   rZ   rÔ   rÕ   rÌ   ÚstartTLSÚcontextr_   r   r   r   ÚlineReceived  s    z&TLSServerFactory.protocol.lineReceived)r   r   r   rÚ   rÔ   r×   rÛ   r   r   r   r   r     s   r   N)r   r   r   r   ZLineReceiverr   r   r   r   r   rÑ     s   rÑ   c                   @   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”   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)   C  r*   z,POP3TLSTests.test_startTLS.<locals>.<lambda>c                    s   t  d¡ ˆ  ¡ S )Nz!Connected to server; starting TLS)r   ÚmsgrÙ   ©Zignoredrà   r   r   ÚcbConnectedG  s    
z/POP3TLSTests.test_startTLS.<locals>.cbConnectedc                    s   t  d¡ ˆ  ¡ S )NzStarted TLS; disconnecting)r   rá   Úquitrâ   rà   r   r   ÚcbStartedTLSK  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á   r1   rÓ   )Zign)r   Úsfr   r   ÚcbDisconnectedO  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   ÚcleanupU  s    
z+POP3TLSTests.test_startTLS.<locals>.cleanup)rÑ   r   rÔ   r   rÚ   r   Z	listenTCPZ
addCleanupZstopListeningZgetHostZhostÚportr   rÄ   r½   r   r   ZClientFactoryZ
connectTCPr¾   r8   ZaddBoth)
r   rë   ÚHÚPr   Z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 )NZtestÚtwisted)rÈ   r6   r8   Ú
addErrback)r:   ©ÚloggedInr   ÚtimedOutr   r   r6   f  s    

z+POP3TimeoutTests.testTimeout.<locals>.loginc                    s   ˆ   d¡ d S )Nz%Successfully logged in!?  Impossible!)Zfailrè   r   r   r   rô   l  s    z.POP3TimeoutTests.testTimeout.<locals>.loggedInc                    s   |   tj¡ ˆ  d ¡ d S r   )Ztrapr   ÚTimeoutErrorrÎ   )rÏ   r   r   r   rõ   p  s    z.POP3TimeoutTests.testTimeout.<locals>.timedOutc                      s
   ˆ j  ¡ S r   )rÈ   rä   r   r   r   r   rä   t  s    z*POP3TimeoutTests.testTimeout.<locals>.quitg{®Gáz„?T)rÈ   rÉ   r   ZTIMEOUT_RESPONSEÚmaprÊ   r8   r+   rÎ   rò   rÐ   r   )r   r6   rä   Úmethodsr   ró   r   ÚtestTimeoute  s    zPOP3TimeoutTests.testTimeoutN)r   r   r   rù   r   r   r   r   rð   d  s   rð   zOpenSSL not presentzReactor doesn't support SSLc                   @   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)r1   rñ   ÚmailZ
pop3clientÚ__all__r   r   r   r   Útest_all•  s    z'POP3ClientModuleStructureTests.test_allc                 C   s^   dd„ t  tjd t j¡D ƒ}|D ]6}|dksD|  t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–   ro   r   r   r   r™   ¤  s    ýz>POP3ClientModuleStructureTests.test_import.<locals>.<listcomp>ztwisted.mail.pop3clientr    r   N)
ÚinspectZ
getmembersÚsysÚmodulesZisclassZ
assertTrueÚhasattrrñ   rû   Zpop3)r   ZpublicClassesZpcr   r   r   Útest_import  s    
þÿz*POP3ClientModuleStructureTests.test_importN)r   r   r   rï   rý   r  r   r   r   r   rú     s   rú   )T)7r   rÿ   Zzope.interfacer   Ztwisted.internetr   r   r   r   r   Ztwisted.mail.pop3r   r    r	   r
   Ztwisted.mail.testr   Ztwisted.protocolsr   r   Ztwisted.pythonr   Ztwisted.python.compatr   Ztwisted.test.proto_helpersr   Ztwisted.trialr   Ztwisted.test.ssl_helpersr   r   ÚImportErrorr   r!   r&   r+   ZTestCaser,   rU   r^   ra   rz   rœ   r½   rÂ   ZServerFactoryrÑ   rÜ   rð   ZcaseÚskipZIReactorSSLZtwisted.mail.pop3clientrñ   rú   r   r   r   r   Ú<module>   sX     ÿ
 
Gh`"@!



