o
    bTU                     @   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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 d dlmZ z
d dl m!Z!m"Z" W n e#ym   dZ!dZ"Y nw 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)AdvancedPOP3Client InsecureAuthenticationDisallowedServerErrorResponse)pop3testserver)basicloopback)log)StringTransport)TestCase)ClientTLSContextServerTLSContextc                   @      e Zd Zdd ZdS )#StringTransportWithConnectionLosingc                 C   s   | j t  d S N)r   connectionLostr   ConnectionDoneself r   C/usr/lib/python3/dist-packages/twisted/mail/test/test_pop3client.pyloseConnection!      z2StringTransportWithConnectionLosing.loseConnectionN)__name__
__module____qualname__r   r   r   r   r   r           r   s   180s   LOGIN)s   TOPs   LOGIN-DELAYs   UIDL   STLSs   USERs   SASLTc                 C   s8   t  }t|_t }||_|| | r|d ||fS )Ns   +OK Hello!
)
POP3ClientcapCache	_capCacher   r   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   s    zstrip.<locals>.<lambda>r   )r/   r   r   r   strip@   s   r1   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                         | jd dS )Nr   s   Offline for maintenanceassertEqualargsexcr   r   r   r0   K       z;POP3ClientLoginTests.testNegativeGreeting.<locals>.<lambda>)r-   allowInsecureLoginloginr)   assertFailurer   addCallbackr   r+   r,   dr   r   r   testNegativeGreetingE   s   

z)POP3ClientLoginTests.testNegativeGreetingc                 C   <   t  \}}|d}| | d |d || jdS )Nr4      USER username
s   +OK send password
s   send password)r-   userr8   valuer)   r@   rA   r   r   r   
testOkUserN   
   


zPOP3ClientLoginTests.testOkUserc                    H   t  \}}|d} | d |d  |t fddS )Nr4   rE   s   -ERR account suspended
c                    r6   )Nr   s   account suspendedr7   r:   r   r   r   r0   [   r<   z2POP3ClientLoginTests.testBadUser.<locals>.<lambda>)r-   rF   r8   rG   r)   r?   r   r@   rA   r   r   r   testBadUserU      



z POP3ClientLoginTests.testBadUserc                 C   rD   )Nr5      PASS password
s   +OK you're in!
s
   you're in!)r-   passwordr8   rG   r)   r@   rA   r   r   r   
testOkPass^   rI   zPOP3ClientLoginTests.testOkPassc                    rJ   )Nr5   rM   s   -ERR go away
c                    r6   )Nr   s   go awayr7   r:   r   r   r   r0   k   r<   z2POP3ClientLoginTests.testBadPass.<locals>.<lambda>)r-   rN   r8   rG   r)   r?   r   r@   rA   r   r   r   testBadPasse   rL   z POP3ClientLoginTests.testBadPassc                 C   s^   t  \}}d|_|dd}| | d |d | | d |d || jdS )	NTr4   r5   rE   s   +OK go ahead
   USER username
PASS password
s   +OK password accepted
s   password accepted)r-   r=   r>   r8   rG   r)   r@   rA   r   r   r   testOkLoginn   s   


z POP3ClientLoginTests.testOkLoginc                    sj   t  \}}d|_|dd} | d |d  | d |d  |t fdd	S )
NTr4   r5   rE   s   +OK waiting on you
rQ      -ERR bogus login
c                    r6   Nr   s   bogus loginr7   r:   r   r   r   r0      r<   z;POP3ClientLoginTests.testBadPasswordLogin.<locals>.<lambda>	r-   r=   r>   r8   rG   r)   r?   r   r@   rA   r   r   r   testBadPasswordLoginx   s   



z)POP3ClientLoginTests.testBadPasswordLoginc                    sP   t  \}}d|_|dd} | d |d  |t fddS )NTr4   r5   rE   rS   c                    r6   rT   r7   r:   r   r   r   r0      r<   z;POP3ClientLoginTests.testBadUsernameLogin.<locals>.<lambda>rU   rA   r   r   r   testBadUsernameLogin   s   


z)POP3ClientLoginTests.testBadUsernameLoginc                 C   s*   t dd\}}|d | |jd  d S )NFr3   s"   +OK lalala this has no challenge
r-   r)   r8   serverChallenger   r+   r,   r   r   r   testServerGreeting      
z'POP3ClientLoginTests.testServerGreetingc                 C   s*   t dd\}}|d | |jd d S )NFr3   s   +OK <here is the challenge>
s   <here is the challenge>rX   rZ   r   r   r   testServerGreetingWithChallenge   r\   z4POP3ClientLoginTests.testServerGreetingWithChallengec                 C   sL   t dd\}}|d |dd}| | d |d || jdS )	NFr3   s"   +OK <challenge string goes here>
r4   r5   s0   APOP username f34f1e464d0d7927607753129cabe39a
s   +OK Welcome!
s   Welcome!)r-   r)   r>   r8   rG   r@   rA   r   r   r   testAPOP   s   

zPOP3ClientLoginTests.testAPOPc                 C   s>   t dd\}}|d |dd}| |  | |tS )NFr3      +OK Howdy
r4   r5   )r-   r)   r>   assertFalserG   r?   r   rA   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.
        Fr3   r_   r4   r5   rE   s   +OK
rM   )	r-   r   r   ISSLTransportr)   r>   r8   rG   clearrA   r   r   r    testSSLTransportConsideredSecure   s   


z5POP3ClientLoginTests.testSSLTransportConsideredSecureN)r    r!   r"   rC   rH   rK   rO   rP   rR   rV   rW   r[   r]   r^   ra   rd   r   r   r   r   r2   D   s    			


r2   c                   @      e Zd Zdd Zdd ZdS )ListConsumerc                 C   s
   i | _ d S r   datar   r   r   r   __init__      
zListConsumer.__init__c                 C   s    |\}}| j |g | d S r   )rh   
setdefaultappend)r   r.   itemrG   r   r   r   consume   s   zListConsumer.consumeNr    r!   r"   ri   rn   r   r   r   r   rf          rf   c                   @   re   )MessageConsumerc                 C   s
   g | _ d S r   rg   r   r   r   r   ri      rj   zMessageConsumer.__init__c                 C   s   | j | d S r   )rh   rl   r   liner   r   r   rn         zMessageConsumer.consumeNro   r   r   r   r   rq      rp   rq   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   H   t  \}}| }| | d |d |d || jg dS )N   LIST
   +OK Here it comes
s   1 3
2 2
3 1
.
)         )r-   listSizer8   rG   r)   r@   rA   r   r   r   testListSize      


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 )Nrw   rx   s   1 3
2 2
3 1
ry   rz   r{   )r   r{   rz   s   5 3
6 2
7 1
)r   r{   rz               .
)
r-   rf   rn   r|   r8   rG   r)   rh   r@   assertIdenticalr   r+   r,   cr/   rB   r   r   r   testListSizeWithConsumer   s   




(
z,POP3ClientListTests.testListSizeWithConsumerc                    F   t  \}}| } | d |d  |t fddS )Nrw   !   -ERR Fatal doom server exploded
c                    r6   Nr   s   Fatal doom server explodedr7   r:   r   r   r   r0      r<   z8POP3ClientListTests.testFailedListSize.<locals>.<lambda>)r-   r|   r8   rG   r)   r?   r   r@   rA   r   r   r   testFailedListSize      


z&POP3ClientListTests.testFailedListSizec                 C   rv   )N   UIDL
rx   s   1 abc
2 def
3 ghi
.
)   abcs   defs   ghi)r-   listUIDr8   rG   r)   r@   rA   r   r   r   testListUID   r~   zPOP3ClientListTests.testListUIDc                 C   sx   t  \}}t }|j}||}| | d |d |d | |jdgdgdgd |d || j	|S )	Nr   rx   s   1 xyz
2 abc
5 mno
s   xyzr   s   mno)r   r{   r   r   )
r-   rf   rn   r   r8   rG   r)   rh   r@   r   r   r   r   r   testListUIDWithConsumer   s   




z+POP3ClientListTests.testListUIDWithConsumerc                    r   )Nr   r   c                    r6   r   r7   r:   r   r   r   r0     r<   z7POP3ClientListTests.testFailedListUID.<locals>.<lambda>)r-   r   r8   rG   r)   r?   r   r@   rA   r   r   r   testFailedListUID   r   z%POP3ClientListTests.testFailedListUIDN)	r    r!   r"   r}   r   r   r   r   r   r   r   r   r   ru      s    	ru   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
r      La la la here is message texts   .Further message text tra la lar-   retriever8   rG   r)   r@   rA   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-   rq   rn   r   r8   rG   r)   r@   _cbTestRetrieveWithConsumerr   r   r   r   testRetrieveWithConsumer  s   



z/POP3ClientMessageTests.testRetrieveWithConsumerc                 C   "   |  || | |jddg d S )Nr   s   .Further message textr   r8   rh   r   r.   r/   r   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   rz   lines	   TOP 8 2
   +OK 2 lines on the way
   Line the first!  Woop
   Line the last!  Bye
r      Line the first!  Woop   Line the last!  Byer   rA   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   rz   r   r   r   r   r   r   )	r-   rq   rn   r   r8   rG   r)   r@   "_cbTestPartialRetrieveWithConsumerr   r   r   r   testPartialRetrieveWithConsumer5  s   




z6POP3ClientMessageTests.testPartialRetrieveWithConsumerc                 C   r   )Nr   r   r   r   r   r   r   r   A  s   z9POP3ClientMessageTests._cbTestPartialRetrieveWithConsumerc                    rJ   )Nr   s   RETR 1
r   c                    r6   r   r7   r:   r   r   r   r0   K  r<   z;POP3ClientMessageTests.testFailedRetrieve.<locals>.<lambda>)r-   r   r8   rG   r)   r?   r   r@   rA   r   r   r   testFailedRetrieveE  rL   z)POP3ClientMessageTests.testFailedRetrievec                    s   t  \ } fddtdD }tddD ]3}| 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 | ]}  |jd |d f d|d f gqS )s   First line of %d.r{   s   Second line of %d.)r   r@   r8   ).0ir+   r   r   r   
<listcomp>T  s    
zCPOP3ClientMessageTests.test_concurrentRetrieves.<locals>.<listcomp>ry   r{   r   s	   RETR %d
r   s   First line of %d.
s   Second line of %d.
    r   T)fireOnOneErrback)r-   ranger8   rG   rc   r)   r   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   s`   t  \}}|jdd}| | d |d |d || jd d d g dddgdgd	S )
Nr   useCache   CAPA
s   +OK Capabilities on the way
s!   X
Y
Z
A 1 2 3
B 1 2
C 1
.
)   1   2   3r   r   )   X   Y   Z   A   B   Cr-   capabilitiesr8   rG   r)   r@   rA   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   rA   r   r   r   testCapabilityError{  s
   

z'POP3ClientMiscTests.testCapabilityErrorc                 C   :   t  \}}| }| | d |d || jdS )N   STAT
s   +OK 1 1212
)r{   i  )r-   statr8   rG   r)   r@   rA   r   r   r   testStat  
   

zPOP3ClientMiscTests.testStatc                    r   )Nr   r   c                    r6   Nr   s   This server is lame!r7   r:   r   r   r   r0     r<   z3POP3ClientMiscTests.testStatError.<locals>.<lambda>)r-   r   r8   rG   r)   r?   r   r@   rA   r   r   r   testStatError  r   z!POP3ClientMiscTests.testStatErrorc                 C   r   )N   NOOP
s   +OK No-op to you too!
s   No-op to you too!)r-   noopr8   rG   r)   r@   rA   r   r   r   testNoop  r   zPOP3ClientMiscTests.testNoopc                    r   )Nr   r   c                    r6   r   r7   r:   r   r   r   r0     r<   z3POP3ClientMiscTests.testNoopError.<locals>.<lambda>)r-   r   r8   rG   r)   r?   r   r@   rA   r   r   r   testNoopError  r   z!POP3ClientMiscTests.testNoopErrorc                 C   r   )N   RSET
s   +OK Reset state
s   Reset state)r-   resetr8   rG   r)   r@   rA   r   r   r   testRset  r   zPOP3ClientMiscTests.testRsetc                    r   )Nr   r   c                    r6   r   r7   r:   r   r   r   r0     r<   z3POP3ClientMiscTests.testRsetError.<locals>.<lambda>)r-   r   r8   rG   r)   r?   r   r@   rA   r   r   r   testRsetError  r   z!POP3ClientMiscTests.testRsetErrorc                 C   rD   )Nry      DELE 4
s   +OK Hasta la vista
s   Hasta la vista)r-   deleter8   rG   r)   r@   rA   r   r   r   
testDelete  rI   zPOP3ClientMiscTests.testDeletec                    rJ   )Nry   r   s   -ERR Winner is not you.
c                    r6   )Nr   s   Winner is not you.r7   r:   r   r   r   r0     r<   z5POP3ClientMiscTests.testDeleteError.<locals>.<lambda>)r-   r   r8   rG   r)   r?   r   r@   rA   r   r   r   testDeleteError  rL   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)deferredr=   )r   r   contextFactoryr   r   r   ri     s   
zSimpleClient.__init__c                 C   s   | j d  d S r   )r   callback)r   	challenger   r   r   serverGreeting  rt   zSimpleClient.serverGreetingr   )r    r!   r"   ri   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   POP3TestServer	serverCTXserverr   	clientCTXclienttimeout	connected)r   rB   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  rt   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)noisy)r   loopbackTCPr   r   r   r   r   r   r     r   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sendLinerr   r   r   r   connectionMade  s
   z(TLSServerFactory.protocol.connectionMadec                    sF    j j|  fdd jdD  |dkr! j j d S 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   rl   r   r   r   startTLScontextrr   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   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g d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.
           +OK)r  r$      .r  r   z	127.0.0.1)	interfacec                    s   t |   d  d S r   )r   r   r   )reason)connLostDeferredcpr   r   r        z2POP3TLSTests.test_startTLS.<locals>.connectionLostc                      s    S r   r   r   r  r   r   r0     s    z,POP3TLSTests.test_startTLS.<locals>.<lambda>c                       t d   S )Nz!Connected to server; starting TLS)r   msgr   ignoredr	  r   r   cbConnected!     
z/POP3TLSTests.test_startTLS.<locals>.cbConnectedc                    r
  )NzStarted TLS; disconnecting)r   r  quitr  r	  r   r   cbStartedTLS%  r  z0POP3TLSTests.test_startTLS.<locals>.cbStartedTLSc                    s    t d  jg d d S )Nz.Disconnected; asserting correct input received)   CAPAr$   r  s   QUIT)r   r  r8   r   )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  
disconnectr.   )connr  r   r   cleanup-  s
   z+POP3TLSTests.test_startTLS.<locals>.cleanup)r   r   r   r   r   r	   	listenTCP
addCleanupstopListeninggetHosthostportr   r   r   r   r   ClientFactory
connectTCPr   r@   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                   @   r   )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   r>   r@   
addErrback)rB   loggedInr   timedOutr   r   r>   ?  s   

z+POP3TimeoutTests.testTimeout.<locals>.loginc                    s     d d S )Nz%Successfully logged in!?  Impossible!)failr  r   r   r   r-  E  s   z.POP3TimeoutTests.testTimeout.<locals>.loggedInc                    s   |  tj  d  d S r   )trapr   TimeoutErrorr   )r   r   r   r   r.  H  r  z.POP3TimeoutTests.testTimeout.<locals>.timedOutc                      s
    j  S r   )r   r  r   r   r   r   r  L  rj   z*POP3TimeoutTests.testTimeout.<locals>.quitg{Gz?T)r   r   r   TIMEOUT_RESPONSEmapr   r@   r1   r   r+  r   r   )r   r>   r  methodsr   r,  r   testTimeout>  s   zPOP3TimeoutTests.testTimeoutN)r    r!   r"   r5  r   r   r   r   r(  =  r#   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)r8   r*  mail_pop3client__all__r   r   r   r   test_allb  s   z'POP3ClientModuleStructureTests.test_allc                 C   s   dd t tjd t jD }|D ]-}tjdk r|dkrq|dks4| 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   r   r   r   r   r   p  s
    z>POP3ClientModuleStructureTests.test_import.<locals>.<listcomp>ztwisted.mail._pop3client)ry   r   r   r%   z not in r
   N)inspect
getmemberssysmodulesisclassversion_info
assertTruehasattrr*  r7  pop3)r   publicClassespcr   r   r   test_importi  s   z*POP3ClientModuleStructureTests.test_importN)r    r!   r"   r'  r:  rG  r   r   r   r   r6  \  s    r6  )T)6r<  r>  typingr   unittestr   zope.interfacer   twisted.mail._pop3clientr*  twisted.internetr   r   r   r   r	   twisted.mail.pop3r
   r%   r   r   twisted.mail.testr   twisted.protocolsr   r   twisted.pythonr   twisted.test.proto_helpersr   twisted.trial.unittestr   twisted.test.ssl_helpersr   r   ImportErrorr   r&   r-   r1   r2   rf   rq   ru   r   r   r   r   ServerFactoryr   IReactorSSLr  r(  r6  r   r   r   r   <module>   sV   

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