o
    ¯b \ ã                   @   s¨  d Z ddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlZddlmZ ddlmZ ddlmZmZ ddlmZ ddlZddlZddlZddlZddlZddlZddlZddlZddl Zddlm!Z!m"Z" ddl#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z) dd	l*m+Z+ dd
l,m-Z-m.Z.m/Z/m0Z0 ddl1m2Z2m3Z3 ddl m4Z4 ddl5m6Z6 ddl7m8Z8m9Z9m:Z: ddl;m<Z< ddl=m>Z>m?Z? ddl@mAZA ddlBmCZC ddlDmEZEmFZFmGZG ddlHmIZI eeCdkdƒG dd„ deIƒƒZJeeCdkdƒG dd„ deIƒƒZKeeCdkdƒG dd„ deIƒƒZLeeCdkdƒG dd„ deIƒƒZMeeCdkdƒG dd „ d eIƒƒZNeeCdkdƒG d!d"„ d"eIƒƒZOeeCdkdƒG d#d$„ d$e"jPjQƒƒZRG d%d&„ d&ƒZSeeCdkdƒG d'd(„ d(eIeSƒƒZTeeCdkdƒG d)d*„ d*eIeSƒƒZUeeCdkdƒG d+d,„ d,eIƒƒZVeeCdkdƒG d-d.„ d.eIƒƒZWeeCdkdƒG d/d0„ d0eIƒƒZXee"j"jYƒG d1d2„ d2ƒƒZZeeCdkdƒG d3d4„ d4eIƒƒZ[eeCdkdƒG d5d6„ d6eIƒƒZ\G d7d8„ d8e3j]ƒZ^eeCdkdƒG d9d:„ d:eIƒƒZ_eeCdkdƒG d;d<„ d<eIƒƒZ`G d=d>„ d>ƒZaeeCdkdƒG d?d@„ d@eIƒƒZbeeCdkdƒG dAdB„ dBeIƒƒZcddCl5mdZdmeZemfZf G dDdE„ dEeejgƒZhdFdG„ ZidHdI„ ZjeeCdkdƒG dJdK„ dKeIƒƒZkeeCdkdƒG dLdM„ dMeIƒƒZlG dNdO„ dOƒZmeeCdkdƒG dPdQ„ dQeIƒƒZnG dRdS„ dSƒZoeeCdkdƒG dTdU„ dUeIƒƒZpG dVdW„ dWƒZqG dXdY„ dYe"jrjsƒZtG dZd[„ d[e"jrjuƒZvG d\d]„ d]ƒZweeCdkdƒG d^d_„ d_eIƒƒZxG d`da„ daƒZyG dbdc„ dcƒZzG ddde„ deƒZ{eeCdkdƒG dfdg„ dgeIƒƒZ|dS )hz.
Tests for large portions of L{twisted.mail}.
é    N)Úmd5)ÚskipIf)Ú	InterfaceÚimplementer)ÚverifyClass)ÚcredÚmail)ÚaddressÚdeferÚ
interfacesÚprotocolÚreactorÚtask)ÚDeferred)ÚCannotListenErrorÚDNSLookupErrorÚProcessDoneÚProcessTerminated)Úpop3Úsmtp)Ú_AttemptManager)Údns)ÚRecord_CNAMEÚ	Record_MXÚRRHeader)ÚDNSNameError)ÚfailureÚlog)ÚFilePath)ÚplatformType)ÚLineSendingProtocolÚMemoryReactorClockÚStringTransport)ÚTestCaseÚposixz twisted.mail only works on posixc                   @   sD   e Zd Zeejdkdƒdd„ ƒZdd„ Zdd„ Zd	d
„ Z	dd„ Z
dS )ÚDomainWithDefaultsTests©é   únot ported to Python 3c                 C   s‚  dd„ t dƒD ƒ}tj |d¡}|  t|ƒd¡ |  tt|ƒƒtt dƒƒ¡ |  t| ¡ ƒtt|ƒƒ¡ t| ¡ ƒ}| 	¡  |  |dd„ t dƒD ƒ¡ t| 
¡ ƒ}| 	¡  |  |tt ddƒƒ¡ | ¡ }| 	¡  |  |dd„ t dƒD ƒ¡ | ¡ }| 	¡  |  |tt ddƒƒ¡ t dƒD ]}|  || |d ¡ |  | |¡|d ¡ |  ||v ¡ qŒ|d	= |d
= |d= |  t|ƒd¡ |  |d	 d¡ |  |d
 d¡ |  |d d¡ | d dddœ¡ |  t|ƒd¡ |  |d d ¡ |  |d d¡ |  |d d¡ | ¡  |  t|ƒd¡ |  | dd¡d¡ |  |d d¡ |  | ¡ d¡ |  t|ƒd¡ | ¡ }|  |j|j¡ |  |j|j¡ d S )Nc                 S   s   i | ]}||d  “qS ©é
   © ©Ú.0Úxr+   r+   ú=/usr/lib/python3/dist-packages/twisted/mail/test/test_mail.pyÚ
<dictcomp>@   ó    z7DomainWithDefaultsTests.testMethods.<locals>.<dictcomp>r*   ÚDefaultc                 S   ó   g | ]}||d  f‘qS r)   r+   r,   r+   r+   r/   Ú
<listcomp>I   ó    z7DomainWithDefaultsTests.testMethods.<locals>.<listcomp>é   c                 S   r3   r)   r+   r,   r+   r+   r/   r4   Q   r5   é   é   é   é   r+   Ú*)ÚaÚbÚcr<   r=   r>   r   ÚkeyÚvalue)r?   r@   )Úranger   ÚDomainWithDefaultDictÚassertEqualÚlenÚlistÚiterÚiterkeysÚ	iteritemsÚsortÚ
itervaluesÚitemsÚvaluesÚgetÚ
assertTrueÚupdateÚclearÚ
setdefaultÚpopitemÚcopyÚdomainsÚdefault)ÚselfÚdrK   rL   r.   Údcopyr+   r+   r/   ÚtestMethods>   sP   z#DomainWithDefaultsTests.testMethodsc                 C   sF   t j  i d¡}|  |jj||ƒ¡ d|d< |  tddiƒ||ƒ¡ dS )a™  
        Assert that the class name of a L{mail.mail.DomainWithDefaultDict}
        instance and the string-formatted underlying domain dictionary both
        appear in the string produced by the given string-returning function.

        @type stringifier: one-argument callable
        @param stringifier: either C{str} or C{repr}, to be used to get a
            string to make assertions against.
        r2   r@   r?   N)r   rB   ÚassertInÚ	__class__Ú__name__Ústr)rV   ÚstringifierÚdomainr+   r+   r/   Ú_stringificationTestv   s   
z,DomainWithDefaultsTests._stringificationTestc                 C   ó   |   t¡ dS )z—
        L{DomainWithDefaultDict.__str__} should return a string including
        the class name and the domain mapping held by the instance.
        N)r`   r]   ©rV   r+   r+   r/   Útest_str…   ó   z DomainWithDefaultsTests.test_strc                 C   ra   )z˜
        L{DomainWithDefaultDict.__repr__} should return a string including
        the class name and the domain mapping held by the instance.
        N)r`   Úreprrb   r+   r+   r/   Ú	test_reprŒ   rd   z!DomainWithDefaultsTests.test_reprc                 C   sf   t j  i d¡}| d¡ d}|  | jg¡}|  dt|ƒ¡ |  t|d d ¡ |  ||d d ¡ dS )	z,
        has_key is now deprecated.
        r2   Úanythingzotwisted.mail.mail.DomainWithDefaultDict.has_key was deprecated in Twisted 16.3.0. Use the `in` keyword instead.é   r   ÚcategoryÚmessageN)r   rB   Úhas_keyÚflushWarningsÚtest_has_keyDeprecationrC   rD   ÚDeprecationWarning)rV   Úsutrj   Úwarningsr+   r+   r/   rm   “   s   
ÿz/DomainWithDefaultsTests.test_has_keyDeprecationN)r\   Ú
__module__Ú__qualname__r   ÚsysÚversion_inforY   r`   rc   rf   rm   r+   r+   r+   r/   r%   <   s    
7r%   c                   @   ó,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚBounceTestsc                 C   ó   t j  ¡ | _d S ©N)r   ÚBounceDomainr_   rb   r+   r+   r/   ÚsetUp§   ó   zBounceTests.setUpc                 C   s   |   tj| jjd¡ d S )Nzany user)ÚassertRaisesr   ÚAddressErrorr_   Úexistsrb   r+   r+   r/   Ú
testExistsª   s   zBounceTests.testExistsc                 C   s   |   | j dd¡d¡ d S )Nzrandom q emailerr   F)rC   r_   Ú	willRelayrb   r+   r+   r/   Ú	testRelay­   s   zBounceTests.testRelayc                 C   s&   | j  dd¡ |  tj| j jd¡ d S )NÚbobÚpassword)r_   ÚaddUserr|   r   ÚSMTPBadRcptr~   rb   r+   r+   r/   ÚtestAddUser°   s   zBounceTests.testAddUserN)r\   rq   rr   rz   r   r   r†   r+   r+   r+   r/   rv   ¥   s
    rv   c                   @   ó   e Zd ZdZdd„ ZdS )ÚBounceWithSMTPServerTestsz_
    Tests for L{twisted.mail.mail.BounceDomain} with
    L{twisted.mail.smtp.SMTPServer}.
    c                 C   s¼   t j  ¡ }t j  ¡ }| d|¡ t j |¡}| d¡}t j |dd¡}||_t	ƒ }| 
|¡ | d¡ | d¡ | d¡ | d¡ |  |j¡ | d¡ |  | ¡  ¡  d¡d d	¡ dS )
zo
        Incoming emails to a SMTP server with L{twisted.mail.mail.BounceDomain}
        are rejected.
        s   foo.comNs   HELO baz.nets   MAIL FROM:<a@baz.net>s   RCPT TO:<any@foo.com>s   QUITs   
éþÿÿÿs(   550 Cannot receive for specified address)r   ÚMailServicery   Ú	addDomainÚ	protocolsÚSMTPFactoryÚbuildProtocolÚSMTPDomainDeliveryÚdeliveryr"   ÚmakeConnectionÚlineReceivedrN   ÚdisconnectingÚconnectionLostrC   r@   ÚstripÚsplit)rV   Úservicer_   Úfactoryr   Ú	delivererÚ	transportr+   r+   r/   Útest_rejected¼   s&   








þz'BounceWithSMTPServerTests.test_rejectedN)r\   rq   rr   Ú__doc__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eej	d	kd
ƒdd„ ƒZ
eej	d	kd
ƒdd„ ƒZdS )ÚFileMessageTestsc                 C   s6   d| _ d| _t| j dƒ| _tj | j| j | j¡| _d S )NzfileMessage.testFilezfinal.fileMessage.testFileÚw)ÚnameÚfinalÚopenÚfr   ÚFileMessageÚfprb   r+   r+   r/   rz   Þ   s   zFileMessageTests.setUpc                 C   sn   z| j  ¡  W n	 ty   Y nw zt | j¡ W n	 ty"   Y nw z	t | j¡ W d S  ty6   Y d S w rx   )r¢   ÚcloseÚBaseExceptionÚosÚremoverŸ   r    rb   r+   r+   r/   ÚtearDownä   s   ÿÿÿzFileMessageTests.tearDownc                 C   s   | j  ¡  | j¡S rx   )r¤   ÚeomReceivedÚaddCallbackÚ_cbFinalNamerb   r+   r+   r/   ÚtestFinalNameò   ó   zFileMessageTests.testFinalNamec                 C   s4   |   || j¡ |  | jj¡ |  tj | j	¡¡ d S rx   )
rC   r    rN   r¢   ÚclosedÚassertFalser§   Úpathr~   rŸ   ©rV   Úresultr+   r+   r/   r¬   õ   s   zFileMessageTests._cbFinalNamer&   r(   c                 C   sh   d}|  ¡ D ]}| j |¡ q| j ¡  t| jƒ}|  | ¡ |¡ W d   ƒ d S 1 s-w   Y  d S )Nz"first line
second line
third line
)Ú
splitlinesr¤   r’   rª   r¡   r    rC   Úread)rV   ÚcontentsÚliner¢   r+   r+   r/   ÚtestContentsú   s   
"ÿzFileMessageTests.testContentsc                 C   sT   d}|  ¡ D ]}| j |¡ q| j ¡  |  tj | j¡¡ |  tj | j	¡¡ d S )Nzfirst line
second line
)
r´   r¤   r’   r”   r°   r§   r±   r~   rŸ   r    )rV   r¶   r·   r+   r+   r/   ÚtestInterrupted  s   
z FileMessageTests.testInterruptedN)r\   rq   rr   rz   r©   r­   r¬   r   rs   rt   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S )ÚMailServiceTestsc                 C   rw   rx   )r   rŠ   r—   rb   r+   r+   r/   rz     r{   zMailServiceTests.setUpc                 C   s”   | j  ¡ }|  t|tjƒ¡ |  | d¡tj¡ | j  	¡ }|  t|tjƒ¡ |  | d¡t
j¡ | j  ¡ }|  t|tjƒ¡ |  | d¡t
j¡ d S )N)ú	127.0.0.1é90  )r—   ÚgetPOP3FactoryrN   Ú
isinstancer   ÚServerFactoryrŽ   r   ÚPOP3ÚgetSMTPFactoryr   ÚSMTPÚgetESMTPFactoryÚESMTP)rV   r¢   r+   r+   r/   ÚtestFactories  s   


zMailServiceTests.testFactoriesc                 C   sR   t ƒ }t ƒ }|| jjd< || jjd< |  | j d¡|u ¡ |  | j ¡ |u ¡ d S )Nr_   Ú )Úobjectr—   ÚportalsrN   ÚlookupPortalÚdefaultPortal)rV   Úo1Úo2r+   r+   r/   ÚtestPortals  s   zMailServiceTests.testPortalsN)r\   rq   rr   rz   rÅ   rÍ   r+   r+   r+   r/   rº     s    rº   c                   @   sh   e Zd ZdZdd„ Zdd„ Zeejdkdƒdd	„ ƒZ	eejdkdƒd
d„ ƒZ
dd„ Zdd„ Zdd„ ZdS )ÚStringListMailboxTestszc
    Tests for L{StringListMailbox}, an in-memory only implementation of
    L{pop3.IMailbox}.
    c                 C   sJ   t j g d¢¡}|  | d¡d¡ |  | d¡d¡ |  | d¡d¡ dS )zŠ
        L{StringListMailbox.listMessages} returns the length of the message at
        the offset into the mailbox passed to it.
        )ÚabcÚabr<   r   r'   rh   r7   N©r   ÚmaildirÚStringListMailboxrC   ÚlistMessages©rV   Úmailboxr+   r+   r/   Útest_listOneMessage0  s   z*StringListMailboxTests.test_listOneMessagec                 C   s(   t j g d¢¡}|  | ¡ g d¢¡ dS )z
        L{StringListMailbox.listMessages} returns a list of the lengths of all
        messages if not passed an index.
        )r<   rÏ   rÐ   )rh   r'   r7   NrÑ   rÕ   r+   r+   r/   Útest_listAllMessages:  s   z+StringListMailboxTests.test_listAllMessagesr&   r(   c                 C   s*   t j ddg¡}|  | d¡ ¡ d¡ dS )zµ
        L{StringListMailbox.getMessage} returns a file-like object from which
        the contents of the message at the given offset into the mailbox can be
        read.
        Úfoozreal contentsrh   N)r   rÒ   rÓ   rC   Ú
getMessagerµ   rÕ   r+   r+   r/   Útest_getMessageB  s   z&StringListMailboxTests.test_getMessagec                 C   s,   t j ddg¡}|  | d¡| d¡¡ dS )zˆ
        L{StringListMailbox.getUidl} returns a unique identifier for the
        message at the given offset into the mailbox.
        rÙ   Úbarr   rh   N)r   rÒ   rÓ   ÚassertNotEqualÚgetUidlrÕ   r+   r+   r/   Útest_getUidlL  s   z#StringListMailboxTests.test_getUidlc                 C   s@   t j dg¡}| d¡ |  | d¡d¡ |  | ¡ dg¡ dS )zŽ
        L{StringListMailbox.deleteMessage} marks a message for deletion causing
        further requests for its length to return 0.
        rÙ   r   N)r   rÒ   rÓ   ÚdeleteMessagerC   rÔ   rÕ   r+   r+   r/   Útest_deleteMessageU  s   
z)StringListMailboxTests.test_deleteMessagec                 C   sH   t j dg¡}| d¡ | ¡  |  | d¡d¡ |  | ¡ dg¡ dS )z
        L{StringListMailbox.undeleteMessages} causes any messages marked for
        deletion to be returned to their original state.
        rÙ   r   r'   N)r   rÒ   rÓ   rà   ÚundeleteMessagesrC   rÔ   rÕ   r+   r+   r/   Útest_undeleteMessages_  s
   
z,StringListMailboxTests.test_undeleteMessagesc                 C   sP   t j dg¡}| d¡ | ¡  | ¡  |  | d¡d¡ |  | ¡ dg¡ dS )zy
        L{StringListMailbox.sync} causes any messages as marked for deletion to
        be permanently deleted.
        rÙ   r   N)r   rÒ   rÓ   rà   Úsyncrâ   rC   rÔ   rÕ   r+   r+   r/   Ú	test_syncj  s   
z StringListMailboxTests.test_syncN)r\   rq   rr   rœ   r×   rØ   r   rs   rt   rÛ   rß   rá   rã   rå   r+   r+   r+   r/   rÎ   )  s    

	

rÎ   c                   @   s0   e Zd ZdZdZdZdd„ Zdd„ Zdd„ ZdS )	Ú&FailingMaildirMailboxAppendMessageTaskTc                 C   s    | j r
t |||¡S ttjdƒ‚©NzFaked Permission Problem)Ú
_openstater§   r¡   ÚOSErrorÚerrnoÚEPERM)rV   ÚfnÚattrÚmoder+   r+   r/   Úosopen  s   z-FailingMaildirMailboxAppendMessageTask.osopenc                 C   ó   | j r	t ||¡S ttjdƒ‚)NzFaked Space problem)Ú_writestater§   Úwriteré   rê   ÚENOSPC)rV   ÚfhÚdatar+   r+   r/   Úoswrite…  ó   z.FailingMaildirMailboxAppendMessageTask.oswritec                 C   rð   rç   )Ú_renamestater§   Úrenameré   rê   rë   )rV   ÚoldnameÚnewnamer+   r+   r/   Úosrename‹  r÷   z/FailingMaildirMailboxAppendMessageTask.osrenameN)	r\   rq   rr   rè   rñ   rø   rï   rö   rü   r+   r+   r+   r/   ræ   w  s    ræ   c                   @   r‡   )Ú_AppendTestMixinz
    Mixin for L{MaildirMailbox.appendMessage} test cases which defines a helper
    for serially appending multiple messages to a mailbox.
    c                    s:   g ‰‡ ‡‡fdd„}t  |ƒ ¡ ¡ }| ‡fdd„¡ |S )zì
        Deliver the given messages one at a time.  Delivery is serialized to
        guarantee a predictable order in the mailbox (overlapped message deliver
        makes no guarantees about which message which appear first).
        c                  3   s,    ˆD ]} ˆ   | ¡}| ˆj¡ |V  qd S rx   )ÚappendMessager«   Úappend)ÚmrW   ©ÚmboxÚmessagesÚresultsr+   r/   rÿ      s   €
ýz0_AppendTestMixin._appendMessages.<locals>.appendc                    ó   ˆ S rx   r+   ©Úignored)r  r+   r/   Ú<lambda>§  ó    z2_AppendTestMixin._appendMessages.<locals>.<lambda>)r   Ú	cooperateÚwhenDoner«   )rV   r  r  rÿ   rW   r+   r  r/   Ú_appendMessages˜  s
   z _AppendTestMixin._appendMessagesN)r\   rq   rr   rœ   r  r+   r+   r+   r/   rý   ’  s    rý   c                   @   sJ   e Zd ZdZdd„ Zdd„ Zddd„Zeej	d	kd
ƒdd„ ƒZ
dd„ ZdS )ÚMaildirAppendStringTestsúO
    Tests for L{MaildirMailbox.appendMessage} when invoked with a C{str}.
    c                 C   ó   |   ¡ | _tj | j¡ d S rx   ©ÚmktemprW   r   rÒ   ÚinitializeMaildirrb   r+   r+   r/   rz   ±  ó   
zMaildirAppendStringTests.setUpc                 C   s   |  d¡}|  |t¡S )NÚTEST)rþ   ÚassertFailureÚ	Exception©rV   r  r  rW   r+   r+   r/   Ú_appendµ  s   
z MaildirAppendStringTests._appendNc                 C   sz   |dur|   t|jd|jj¡ ||j_|dur&|   t|jd|jj¡ ||j_|dur;|   t|jd|jj¡ ||j_dS dS )aÞ  
        Change the behavior of future C{rename}, C{write}, or C{open} calls made
        by the mailbox C{mbox}.

        @param rename: If not L{None}, a new value for the C{_renamestate}
            attribute of the mailbox's append factory.  The original value will
            be restored at the end of the test.

        @param write: Like C{rename}, but for the C{_writestate} attribute.

        @param open: Like C{rename}, but for the C{_openstate} attribute.
        Nrø   rñ   rè   )Ú
addCleanupÚsetattrÚAppendFactoryrø   rñ   rè   )rV   r  r  rù   rò   r¡   r+   r+   r/   Ú	_setState¹  s,   üüÿüz"MaildirAppendStringTests._setStater&   r(   c                 C   sV   t j | j¡}t|_|  |dd„ tddƒD ƒ¡}| | j	dgd ¡ | | j
|¡ |S )ú–
        L{MaildirMailbox.appendMessage} returns a L{Deferred} which fires when
        the message has been added to the end of the mailbox.
        c                 S   s   g | ]}d | ‘qS )ÚXr+   ©r-   Úir+   r+   r/   r4   å  s    z8MaildirAppendStringTests.test_append.<locals>.<listcomp>rh   é   Nr*   )r   rÒ   ÚMaildirMailboxrW   ræ   r  r  rA   r«   rC   Ú_cbTestAppend)rV   r  rW   r+   r+   r/   Útest_appendÜ  s   z$MaildirAppendStringTests.test_appendc                    s²   |   tˆ  ¡ ƒd¡ |   ‡ fdd„tdƒD ƒttddƒƒ¡ | jdˆ dd |  dˆ ¡}|j| jˆ d	dd
 | | jˆ ¡ |j| jˆ d	dd | | jˆ ¡ |j| jˆ d	d |S )úÛ
        Check that the mailbox has the expected number (ten) of messages in it,
        and that each has the expected contents, and that they are in the same
        order as that in which they were appended.
        r*   c                    ó   g | ]}t ˆ  |¡ ¡ ƒ‘qS r+   ©rD   rÚ   rµ   r  ©r  r+   r/   r4   ò  ó    z:MaildirAppendStringTests._cbTestAppend.<locals>.<listcomp>rh   r!  NF)rù   T)rù   rò   )rò   r¡   )r¡   )rC   rD   rÔ   rA   rE   r  r  r«   r  r+   r(  r/   r#  ê  s    ÿz&MaildirAppendStringTests._cbTestAppend)NNN)r\   rq   rr   rœ   rz   r  r  r   rs   rt   r$  r#  r+   r+   r+   r/   r  «  s    
#
r  c                   @   s8   e Zd ZdZdd„ Zeejdkdƒdd„ ƒZdd	„ Z	d
S )ÚMaildirAppendFileTestsr  c                 C   r  rx   r  rb   r+   r+   r/   rz     r  zMaildirAppendFileTests.setUpr&   r(   c                 C   sx   t j | j¡}g }tddƒD ]}t ¡ }| d| ¡ | dd¡ | 	|¡ |  
|j¡ q|  ||¡}| | j|¡ |S )r  rh   r!  r  r   )r   rÒ   r"  rW   rA   ÚtempfileÚTemporaryFilerò   Úseekrÿ   r  r¥   r  r«   r#  )rV   r  r  r   ÚtemprW   r+   r+   r/   r$  	  s   
z"MaildirAppendFileTests.test_appendc                    s@   |   tˆ  ¡ ƒd¡ |   ‡ fdd„tdƒD ƒttddƒƒ¡ dS )r%  r*   c                    r&  r+   r'  r  r(  r+   r/   r4   $  r)  z8MaildirAppendFileTests._cbTestAppend.<locals>.<listcomp>rh   r!  N)rC   rD   rÔ   rA   rE   )rV   r³   r  r+   r(  r/   r#    s    ÿz$MaildirAppendFileTests._cbTestAppendN)
r\   rq   rr   rœ   rz   r   rs   rt   r$  r#  r+   r+   r+   r/   r*  ÿ  s    
r*  c                   @   sD   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zeej	d	kd
ƒdd„ ƒZ
dS )ÚMaildirTestsc                 C   r  rx   r  rb   r+   r+   r/   rz   *  r  zMaildirTests.setUpc                 C   ó   t  | j¡ d S rx   )ÚshutilÚrmtreerW   rb   r+   r+   r/   r©   .  r{   zMaildirTests.tearDownc                 C   s†  | j }tj |d¡}|  tj |¡otj |¡¡ |  tj tj |d¡¡¡ |  tj tj |d¡¡¡ |  tj tj |d¡¡¡ |  tj tj |d¡¡¡ |  tj tj |d¡¡¡ |  tj tj |d¡¡¡ |  tj tj |d¡¡¡ |  tj tj |d¡¡¡ |  tj tj |d¡¡¡ |  tj tj |d¡¡¡ |  tj tj |d¡¡¡ |  tj tj |d¡¡¡ d S )Nú.TrashÚnewÚcurÚtmp)rW   r§   r±   ÚjoinrN   r~   Úisdir)rV   rW   Útrashr+   r+   r/   ÚtestInitializer1  s    zMaildirTests.testInitializerc                 C   sJ   t  ¡ }| d¡ tj |¡}| ¡ }| d¡ | ¡ }|  ||k ¡ dS )a  
        Each call to L{_MaildirNameGenerator.generate} returns a unique
        string suitable for use as the basename of a new message file.  The
        names are ordered such that those generated earlier sort less than
        those generated later.
        gš™™™™™©?N)r   ÚClockÚadvancer   rÒ   Ú_MaildirNameGeneratorÚgeneraterN   )rV   ÚclockÚ	generatorÚ	firstNameÚ
secondNamer+   r+   r/   Útest_nameGeneratorD  s   

zMaildirTests.test_nameGeneratorr&   r(   c           	   	      sê  t jj‰ tjj‰‡ ‡fdd„dD ƒ}d}|D ]&}tˆ | j|ƒdƒ}| d| ¡ W d  ƒ n1 s3w   Y  |d }qtj 	| j¡}|  
| ¡ ttddƒƒ¡ |  
| d¡d	¡ |  
| d
¡d¡ |  
| d¡ ¡ d¡ |  
| d¡ ¡ d¡ i }tdƒD ]}| |¡}|  ||v ¡ d||< qt j |d
 ¡\}}| d
¡ |  
| d
¡d¡ |  t j ˆ | jdd|ƒ¡¡ |  t j ˆ | j|d
 ƒ¡¡ | ¡  |  
| d
¡d¡ |  t j ˆ | jdd|ƒ¡¡ |  t j ˆ | j|d
 ƒ¡¡ dS )zb
        Exercise the methods of L{IMailbox} as implemented by
        L{MaildirMailbox}.
        c                    s&   g | ]}t d ƒD ]}ˆ |ˆƒ ƒ‘qqS )é   )rA   )r-   r=   r.   ©ÚjÚnr+   r/   r4   ]  s   & z-MaildirTests.test_mailbox.<locals>.<listcomp>)r5  r4  rh   rž   r.   Nr!  r7   rD  r9   ÚxxxxxxxÚxxr*   r   r3  r5  )r§   r±   r7  r   rÒ   Ú_generateMaildirNamer¡   rW   rò   r"  rC   rÔ   rE   rA   rÚ   rµ   rÞ   r°   r–   rà   rN   r~   râ   )	rV   Úmsgsr   r¢   ÚfObjÚmbrW   ÚuÚpr+   rE  r/   Útest_mailboxU  s:   ÿ



"zMaildirTests.test_mailboxN)r\   rq   rr   rz   r©   r:  rC  r   rs   rt   rP  r+   r+   r+   r/   r/  (  s    r/  c                   @   r‡   )ÚAbstractMaildirDomainTestszB
    Tests for L{twisted.mail.maildir.AbstractMaildirDomain}.
    c                 C   s   t tjjtjjƒ dS )zW
        L{maildir.AbstractMaildirDomain} implements L{mail.IAliasableDomain}.
        N)r   r   ÚIAliasableDomainrÒ   ÚAbstractMaildirDomainrb   r+   r+   r/   Útest_interface‡  ó   z)AbstractMaildirDomainTests.test_interfaceN)r\   rq   rr   rœ   rT  r+   r+   r+   r/   rQ    s    rQ  c                   @   sˆ   e Zd ZdZdd„ Zdd„ Zeejdkdƒdd	„ ƒZ	d
d„ Z
eejdkdƒdd„ ƒZeejdkdƒdd„ ƒZeejdkdƒdd„ ƒZdS )ÚMaildirDirdbmDomainTestsz+
    Tests for L{MaildirDirdbmDomain}.
    c                 C   s.   |   ¡ | _tj ¡ | _tj | j| j¡| _dS )zw
        Create a temporary L{MaildirDirdbmDomain} and parent
        L{MailService} before running each test.
        N)r  ÚPr   rŠ   ÚSrÒ   ÚMaildirDirdbmDomainÚDrb   r+   r+   r/   rz   ”  s   
zMaildirDirdbmDomainTests.setUpc                 C   s   t  | j¡ dS )z_
        Remove the temporary C{maildir} directory when the test has
        finished.
        N)r1  r2  rW  rb   r+   r+   r/   r©     s   z!MaildirDirdbmDomainTests.tearDownr&   r(   c              	   C   sv   d}|D ]\}}| j  ||¡ q|D ]&\}}|  || j jv ¡ |  | j j| |¡ |  tj tj | j	|¡¡¡ qdS )z¾
        L{MaildirDirdbmDomain.addUser} accepts a user and password
        argument. It stores those in a C{dbm} dictionary
        attribute and creates a directory for each user.
        ))Úuser1Úpwd1)Úuser2Úpwd2)Úuser3Úpwd3N)
rZ  r„   rN   ÚdbmrC   r§   r±   r~   r7  rW  )rV   ÚtoAddrN  rO  r+   r+   r/   Útest_addUser¤  s    ýz%MaildirDirdbmDomainTests.test_addUserc                 C   sN   | j  ¡ }|  t|ƒd¡ |  tjj |d ¡¡ |  tj	j
|d jv ¡ dS )zŽ
        L{MaildirDirdbmDomain.getCredentialsCheckers} initializes and
        returns one L{ICredentialsChecker} checker by default.
        rh   r   N)rZ  ÚgetCredentialsCheckersrC   rD   rN   r   ÚcheckersÚICredentialsCheckerÚ
providedByÚcredentialsÚIUsernamePasswordÚcredentialInterfaces)rV   Úcredsr+   r+   r/   Útest_credentials´  s   
ÿz)MaildirDirdbmDomainTests.test_credentialsc                 C   sŽ   G dd„ dt ƒ}| j dd¡ |  t| jjdd|¡ | j ddtj¡}|  t	|ƒd¡ |  
|d tju ¡ |  
tj |d ¡¡ |d	 ƒ  dS )
a?  
        L{MaildirDirdbmDomain.requestAvatar} raises L{NotImplementedError}
        unless it is supplied with an L{pop3.IMailbox} interface.
        When called with an L{pop3.IMailbox}, it returns a 3-tuple
        containing L{pop3.IMailbox}, an implementation of that interface
        and a NOOP callable.
        c                   @   s   e Zd ZdS )z?MaildirDirdbmDomainTests.test_requestAvatar.<locals>.ISomethingN)r\   rq   rr   r+   r+   r+   r/   Ú
ISomethingË  s    rm  Úuserrƒ   Nr'   r   rh   r7   )r   rZ  r„   r|   ÚNotImplementedErrorÚrequestAvatarr   ÚIMailboxrC   rD   rN   rg  )rV   rm  Útr+   r+   r/   Útest_requestAvatarÁ  s   
ÿz+MaildirDirdbmDomainTests.test_requestAvatarc                 C   sb   | j  dd¡ | j  ¡ d }tj dd¡}|  tjj|j	|¡ tj dd¡}|  
| 	|¡d¡ dS )a  
        L{DirdbmDatabase.requestAvatarId} raises L{UnauthorizedLogin} if
        supplied with invalid user credentials.
        When called with valid credentials, L{requestAvatarId} returns
        the username associated with the supplied credentials.
        rn  rƒ   r   úwrong passwordN)rZ  r„   rd  r   rh  ÚUsernamePasswordr|   ÚerrorÚUnauthorizedLoginÚrequestAvatarIdrC   )rV   Údatabaserk  r+   r+   r/   Útest_requestAvatarIdÚ  s   z-MaildirDirdbmDomainTests.test_requestAvatarIdc                 C   sz   | j  dd¡ |  | j  d¡tj | j jd¡¡ d| j _|  	| j  d¡d¡ d| j _|  | j  d¡tj | j jd¡¡ dS )a]  
        L{MaildirDirdbmDomain.userDirectory} is supplied with a user name
        and returns the path to that user's maildir subdirectory.
        Calling L{MaildirDirdbmDomain.userDirectory} with a
        non-existent user returns the 'postmaster' directory if there
        is a postmaster or returns L{None} if there is no postmaster.
        rn  rƒ   FÚnouserNTÚ
postmaster)
rZ  r„   rC   ÚuserDirectoryr§   r±   r7  Úrootr|  ÚassertIdenticalrb   r+   r+   r/   Útest_userDirectoryë  s   	ÿÿz+MaildirDirdbmDomainTests.test_userDirectoryN)r\   rq   rr   rœ   rz   r©   r   rs   rt   rc  rl  rs  rz  r€  r+   r+   r+   r/   rV  Ž  s    	


rV  c                   @   s2   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ ZdS )ÚStubAliasableDomainz>
    Minimal testable implementation of IAliasableDomain.
    Nc                 C   ó   t ƒ ‚©z{
        No test coverage for invocations of this method on domain objects,
        so we just won't implement it.
        ©ro  ©rV   rn  Úmemor+   r+   r/   r~     ó   zStubAliasableDomain.existsc                 C   r‚  rƒ  r„  )rV   rn  rƒ   r+   r+   r/   r„     r‡  zStubAliasableDomain.addUserc                 C   s   g S )a!  
        This needs to succeed in order for other tests to complete
        successfully, but we don't actually assert anything about its
        behavior.  Return an empty list.  Sometime later we should return
        something else and assert that a portal got set up properly.
        r+   rb   r+   r+   r/   rd    ó   z*StubAliasableDomain.getCredentialsCheckersc                 C   ó
   || _ dS )zG
        Just record the value so the test can check it later.
        N)Ú
aliasGroup)rV   Úaliasesr+   r+   r/   ÚsetAliasGroup  s   
z!StubAliasableDomain.setAliasGrouprx   )r\   rq   rr   rœ   r~   r„   rd  rŒ  r+   r+   r+   r/   r    s    
	r  c                   @   st   e Zd Zdd„ Zdd„ Zdd„ Zeejdkdƒd	d
„ ƒZ	eejdkdƒdd„ ƒZ
dd„ Zdd„ Zdd„ Zdd„ ZdS )ÚServiceDomainTestsc                 C   sr   t j  ¡ | _t j | jd ¡| _| j| j_d| j_d| j_|  	¡ | _
t j | j| j
¡}| dd¡ | j d|¡ d S )Nr  Úhostnameó   useró   passwordútest.domain)r   rŠ   rX  rŒ   ÚDomainDeliveryBaserZ  r—   ÚprotocolNameÚhostr  ÚtmpdirrÒ   rY  r„   r‹   ©rV   r_   r+   r+   r/   rz   (  s   

zServiceDomainTests.setUpc                 C   r0  rx   ©r1  r2  r•  rb   r+   r+   r/   r©   4  r{   zServiceDomainTests.tearDownc                 C   s4   t ƒ }tƒ }|| j_| j d|¡ |  |j|¡ dS )z‚
        Test that adding an IAliasableDomain to a mail service properly sets
        up alias group references and such.
        úexample.comN)rÇ   r  rX  r‹  r‹   r  rŠ  )rV   r‹  r_   r+   r+   r/   ÚtestAddAliasableDomain7  s
   z)ServiceDomainTests.testAddAliasableDomainr&   r(   c                 C   sZ   | j  dt d¡dg¡}t |¡}tj ¡ }| 	|¡}|  
t| ¡ ƒd¡ |  d|¡ d S )N)Ú
remotehostz123.232.101.234z<someguy@someplace>zuser@host.namerh   ÚReceived)rZ  ÚreceivedHeaderr   ÚAddressÚioÚBytesIOÚemailÚparserÚParserÚparserC   rD   rK   rZ   )rV   Úhdrr¤   ÚemailParserr   r+   r+   r/   ÚtestReceivedHeaderB  s   ý


z%ServiceDomainTests.testReceivedHeaderc                 C   s(   t  ddd d¡}t | jj|¡ | j¡S )Nzuser@test.domainÚheloúwherever@whatever)r   ÚUserr
   ÚmaybeDeferredrZ  Ú
validateTor«   Ú_cbValidateTo©rV   rn  r+   r+   r/   ÚtestValidateToO  s   ÿz!ServiceDomainTests.testValidateToc                 C   s   |   t|ƒ¡ d S rx   )rN   Úcallabler²   r+   r+   r/   r¬  V  r®   z ServiceDomainTests._cbValidateToc                 C   ó*   t  ddd d¡}|  t | jj|¡t j¡S )Nzresu@test.domainr§  r¨  ©r   r©  r  r
   rª  rZ  r«  r…   r­  r+   r+   r/   ÚtestValidateToBadUsernameY  ó   ÿz,ServiceDomainTests.testValidateToBadUsernamec                 C   r°  )Nzuser@domain.testr§  r¨  r±  r­  r+   r+   r/   ÚtestValidateToBadDomain_  r³  z*ServiceDomainTests.testValidateToBadDomainc                 C   sŒ   d}t  d¡}|  | j ||¡|u ¡ d}t  d¡}|  | j ||¡|u ¡ d}t  d¡}|  | j ||¡|u ¡ |  t j| jjd |¡ d S )N)rŽ  r»   z<user@hostname>)rŽ  ú1.2.3.4z<>)r   r  rN   rZ  ÚvalidateFromr|   ÚSMTPBadSender)rV   r§  Úoriginr+   r+   r/   ÚtestValidateFrome  s   


z#ServiceDomainTests.testValidateFromN)r\   rq   rr   rz   r©   r™  r   rs   rt   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eejdkdƒdd„ ƒZd	d
„ Z	eejdkdƒdd„ ƒZ
eejdkdƒdd„ ƒZeejdkdƒdd„ ƒZdd„ Zeejdkdƒdd„ ƒZeejdkdƒdd„ ƒZdS )ÚVirtualPOP3Testsc                 C   s¢   |   ¡ | _tj ¡ | _tj | j| j¡| _| j dd¡ | j 	d| j¡ t
j | j¡}t|j| j ¡ ƒ | | jjd< | jjd< tj ¡ | _| j| j_d| j_d S )Nr  r  r‘  rÆ   z<unit test magic>)r  r•  r   rŠ   rX  rÒ   rY  rZ  r„   r‹   r   ÚportalÚPortalÚmapÚregisterCheckerrd  rÈ   rŒ   ÚVirtualPOP3rW  r—   Úmagic)rV   r»  r+   r+   r/   rz   w  s   

zVirtualPOP3Tests.setUpc                 C   r0  rx   r—  rb   r+   r+   r/   r©   †  r{   zVirtualPOP3Tests.tearDownr&   r(   c                 C   s*   t | jjd ƒ ¡ }| j d|¡ | j¡S )Nrƒ   rn  )r   rW  rÀ  Ú	hexdigestÚauthenticateUserAPOPr«   Ú_cbAuthenticateAPOP©rV   Úrespr+   r+   r/   ÚtestAuthenticateAPOP‰  s   ÿz%VirtualPOP3Tests.testAuthenticateAPOPc                 C   óF   |   t|ƒd¡ |   |d tj¡ |  tj |d ¡¡ |d ƒ  d S ©Nr'   r   rh   r7   ©rC   rD   r   rq  rN   rg  r²   r+   r+   r/   rÃ    ó   z$VirtualPOP3Tests._cbAuthenticateAPOPc                 C   s.   t | jjd ƒ ¡ }|  | j d|¡tjj¡S )Nrƒ   Úresu)	r   rW  rÀ  rÁ  r  rÂ  r   rv  rw  rÄ  r+   r+   r/   Ú!testAuthenticateIncorrectUserAPOP–  s   ÿz2VirtualPOP3Tests.testAuthenticateIncorrectUserAPOPc                 C   s&   t dƒ ¡ }|  | j d|¡tjj¡S )Nzwrong digestrn  )r   rÁ  r  rW  rÂ  r   rv  rw  rÄ  r+   r+   r/   Ú%testAuthenticateIncorrectResponseAPOP  s   ÿz6VirtualPOP3Tests.testAuthenticateIncorrectResponseAPOPc                 C   s   | j  dd¡ | j¡S )Nrn  rƒ   )rW  ÚauthenticateUserPASSr«   Ú_cbAuthenticatePASSrb   r+   r+   r/   ÚtestAuthenticatePASS¤  s   ÿz%VirtualPOP3Tests.testAuthenticatePASSc                 C   rÇ  rÈ  rÉ  r²   r+   r+   r/   rÏ  ª  rÊ  z$VirtualPOP3Tests._cbAuthenticatePASSc                 C   ó   |   | j dd¡tjj¡S )NrË  rƒ   ©r  rW  rÎ  r   rv  rw  rb   r+   r+   r/   ÚtestAuthenticateBadUserPASS°  ó   þz,VirtualPOP3Tests.testAuthenticateBadUserPASSc                 C   rÑ  )Nrn  rt  rÒ  rb   r+   r+   r/   ÚtestAuthenticateBadPasswordPASS·  rÔ  z0VirtualPOP3Tests.testAuthenticateBadPasswordPASSN)r\   rq   rr   rz   r©   r   rs   rt   rÆ  rÃ  rÌ  rÍ  rÐ  rÏ  rÓ  rÕ  r+   r+   r+   r/   rº  u  s"    




rº  c                   @   s   e Zd Zdd„ ZdS )Úemptyc                 C   s   d S rx   r+   rb   r+   r+   r/   Ú__init__À  s   zempty.__init__N)r\   rq   rr   r×  r+   r+   r+   r/   rÖ  ¿  s    rÖ  c                   @   s$   e Zd Zeejdkdƒdd„ ƒZdS )Ú
RelayTestsr&   r(   c                    sô   t j  ¡ }t j |¡}t d¡t ddd¡g}t ddd¡t ddd¡g}|D ]'‰ tƒ }d	|_d
|_	tƒ |_
tƒ |j
_‡ fdd„|j
j_|  t| |¡ƒ¡ q'|D ]&‰ tƒ }d|_tƒ |_
tƒ |j
_‡ fdd„|j
j_d|_	|  tj|j|¡ qQd S )Nz/var/run/mail-relayÚTCPr»   r¼   z192.168.2.1é>   rµ  i—  z	user@hostz	tsoh@resuc                      r  rx   r+   r+   ©Úpeerr+   r/   r  Û  r	  z'RelayTests.testExists.<locals>.<lambda>z
some@placec                      r  rx   r+   r+   rÛ  r+   r/   r  ä  r	  z	who@cares)r   rŠ   ÚrelayÚDomainQueuerr	   ÚUNIXAddressÚIPv4AddressrÖ  ÚorigÚdestr   rš   ÚgetPeerrN   r¯  r~   r|   r   r…   )rV   r—   r_   ÚdoRelayÚ	dontRelayrn  r+   rÛ  r/   r   Æ  s2   
þþ

øzRelayTests.testExistsN)r\   rq   rr   r   rs   rt   r   r+   r+   r+   r/   rØ  Ä  s    rØ  c                   @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚRelayerTestsc              	   C   sØ   |   ¡ | _t | j¡ g | _tdƒD ]J}tj | jd|f ¡}t|d dƒ}t	 
d|f d|f g|¡ W d   ƒ n1 s?w   Y  t|d dƒ}| |¡ | d	d	¡ | j |¡ qtj ¡ | _| j | j¡ d S )
Nr*   úbody-%dú-HÚwbúfrom-%dúto-%dú-Drž   r   )r  r•  r§   ÚmkdirÚmessageFilesrA   r±   r7  r¡   ÚpickleÚdumprò   r-  rÿ   r   rÝ  ÚRelayerMixinÚRÚloadMessages)rV   r   rŸ   r¢   r+   r+   r/   rz   ì  s   
ÿ
zRelayerTests.setUpc                 C   r0  rx   r—  rb   r+   r+   r/   r©   ý  r{   zRelayerTests.tearDownc                 C   sP   t dƒD ]}|  | j ¡ d|f ¡ | j dd d d d ¡ q|  | j ¡ d ¡ d S )Nr*   rê  éú   )rA   rC   rò  ÚgetMailFromÚsentMail©rV   r   r+   r+   r/   ÚtestMailFrom   s   zRelayerTests.testMailFromc                 C   sR   t dƒD ]}|  | j ¡ d|f g¡ | j dd d d d ¡ q|  | j ¡ d ¡ d S )Nr*   rë  rô  )rA   rC   rò  Ú	getMailTorö  r÷  r+   r+   r/   Ú
testMailTo  s   zRelayerTests.testMailToc                 C   sd   t dƒD ]"}tj | jd|f ¡}|  | j ¡  ¡ |¡ | j 	dd d d d ¡ q|  | j ¡ d ¡ d S )Nr*   rç  rô  )
rA   r§   r±   r7  r•  rC   rò  ÚgetMailDatarµ   rö  )rV   r   rŸ   r+   r+   r/   ÚtestMailData  s
   zRelayerTests.testMailDataN)r\   rq   rr   rz   r©   rø  rú  rü  r+   r+   r+   r/   ræ  ê  s    ræ  c                   @   ru   )
ÚManagerc                 C   s   g | _ g | _g | _d S rx   )Úsuccessr   Údonerb   r+   r+   r/   r×    ó   
zManager.__init__c                 C   ó   | j  ||f¡ d S rx   )rþ  rÿ   ©rV   r˜   rj   r+   r+   r/   ÚnotifySuccess  ó   zManager.notifySuccessc                 C   r  rx   )r   rÿ   r  r+   r+   r/   ÚnotifyFailure  r  zManager.notifyFailurec                 C   ó   | j  |¡ d S rx   )rÿ  rÿ   )rV   r˜   r+   r+   r/   Ú
notifyDone   r{   zManager.notifyDoneN)r\   rq   rr   r×  r  r  r  r+   r+   r+   r/   rý    s
    rý  c                   @   ru   )
ÚManagedRelayerTestsc                 C   sd   t ƒ | _ttdddƒƒ| _tƒ | _tj 	| j¡| _
| jd d … | j
_| jd d … | j
_| j| j
_d S )Nr   r6   r7   )rý  ÚmanagerrE   rA   r  rÇ   r˜   r   ÚrelaymanagerÚManagedRelayerMixinrÝ  Únamesrb   r+   r+   r/   rz   &  s   zManagedRelayerTests.setUpc                    óD   ˆ j D ]}ˆ j dd d d d ¡ qˆ  ˆ jj‡ fdd„ˆ j D ƒ¡ d S )Nrô  c                    ó   g | ]}ˆ j |f‘qS r+   ©r˜   ©r-   r   rb   r+   r/   r4   4  r1   z>ManagedRelayerTests.testSuccessfulSentMail.<locals>.<listcomp>)r  rÝ  rö  rC   r	  rþ  r÷  r+   rb   r/   ÚtestSuccessfulSentMail/  ó
   
ÿz*ManagedRelayerTests.testSuccessfulSentMailc                    r  )Ni&  c                    r  r+   r  r  rb   r+   r/   r4   <  r1   z:ManagedRelayerTests.testFailedSentMail.<locals>.<listcomp>)r  rÝ  rö  rC   r	  r   r÷  r+   rb   r/   ÚtestFailedSentMail7  r  z&ManagedRelayerTests.testFailedSentMailc                 C   s,   | j  t tƒ ¡¡ |  | jj| jg¡ d S rx   )	rÝ  r”   r   ÚFailurer  rC   r	  rÿ  r˜   rb   r+   r+   r/   ÚtestConnectionLost?  s   z&ManagedRelayerTests.testConnectionLostN)r\   rq   rr   rz   r  r  r  r+   r+   r+   r/   r  $  s
    	r  c                   @   sl   e Zd Zdd„ Zdd„ Zeejdkdƒdd„ ƒZeejdkdƒd	d
„ ƒZ	eejdkdƒdd„ ƒZ
dd„ ZdS )ÚDirectoryQueueTestsc              	   C   s¤   |   ¡ | _t | j¡ tj | j¡| _d| j_t	dƒD ]/}| j 
¡ \}}| t d|g|¡ W d   ƒ n1 s9w   Y  | d|f ¡ | ¡  q| j ¡  d S )NFé   Úheaders   body: %d)r  r•  r§   rí  r   r
  ÚQueueÚqueueÚnoisyrA   ÚcreateNewMessagerï  rð  r’   rª   ÚreadDirectory)rV   r   ÚhdrFÚmsgFr+   r+   r/   rz   F  s   
ÿ
zDirectoryQueueTests.setUpc                 C   r0  rx   r—  rb   r+   r+   r/   r©   T  r{   zDirectoryQueueTests.tearDownr&   r(   c                 C   s€   |   | j ¡ ¡ |  t| j ¡ ƒd¡ | j ¡ }| j |d ¡ |  t| j ¡ ƒd¡ | j |d ¡ |  t| j ¡ ƒd¡ d S )Nr  r   é   )rN   r  Ú
hasWaitingrC   rD   Ú
getWaitingÚsetRelayingÚ
setWaiting)rV   Úwaitingr+   r+   r/   ÚtestWaitingW  s   
zDirectoryQueueTests.testWaitingc                 C   sš   | j  ¡ D ]}| j  |¡ |  t| j  ¡ ƒdt| j  ¡ ƒ ¡ q|  | j  ¡ ¡ | j  ¡ }| j  |d ¡ |  t| j  ¡ ƒd¡ |  t| j  ¡ ƒd¡ d S )Nr  r   rh   r   )	r  r"  r#  rC   rD   Ú
getRelayedr°   r!  r$  )rV   r   Úrelayedr+   r+   r/   ÚtestRelayingc  s   ÿ
z DirectoryQueueTests.testRelayingc                 C   s~   | j  ¡ d }| j  |¡ | j  |¡ |  t| j  ¡ ƒd¡ |  t| j  ¡ ƒd¡ |  || j  ¡ v ¡ |  || j  ¡ v ¡ d S )Nr   r   )r  r"  r#  rÿ  rC   rD   r'  r°   )rV   Úmsgr+   r+   r/   ÚtestDoner  s   zDirectoryQueueTests.testDonec                 C   sV   g }| j  ¡ D ]}| | j  |¡¡ q| ¡  tdƒD ]}|  | d¡d|g¡ qd S )Nr  r   r  )r  r"  rÿ   ÚgetEnveloperI   rA   rC   Úpop)rV   Ú	envelopesr*  r   r+   r+   r/   ÚtestEnvelope~  s   ÿz DirectoryQueueTests.testEnvelopeN)r\   rq   rr   rz   r©   r   rs   rt   r&  r)  r+  r/  r+   r+   r+   r/   r  D  s    


r  )ÚclientÚcommonÚserverc                   @   ó   e Zd Zdd„ Zddd„ZdS )ÚTestAuthorityc                 C   s   t j | ¡ i | _d S rx   )r1  ÚResolverBaser×  Ú	addressesrb   r+   r+   r/   r×    s   
zTestAuthority.__init__Nc                 C   sv   || j v r0|tjkr0g }| j | D ]}t |tjtjdt d|¡¡}| |¡ qt |g g f¡S t 	t
 t |¡¡¡S )Né<   r   )r6  r   ÚMXr   ÚINr   rÿ   r
   ÚsucceedÚfailr   r  ÚDomainError)rV   rŸ   ÚclsÚtypeÚtimeoutr  r<   r¤  r+   r+   r/   Ú_lookup‘  s   zTestAuthority._lookuprx   )r\   rq   rr   r×  r@  r+   r+   r+   r/   r4  Œ  ó    r4  c                 C   s   t ƒ | _t | jg¡}t |¡}	 tjd|dd| _| j 	¡ j}ztj
||dd| _W n ty9   | j ¡  Y nw nqtjd|fgd| _d S )Nrh   r   r»   ©Ú	interface)Úservers)r4  Úauthr2  ÚDNSServerFactoryr   ÚDNSDatagramProtocolr   Ú	listenTCPÚportÚgetHostÚ	listenUDPÚudpPortr   ÚstopListeningr0  ÚResolverÚresolver)rV   r˜   r   Ú
portNumberr+   r+   r/   ÚsetUpDNS›  s    
ÿÿõrQ  c                 C   sZ   g }|  t | jj¡¡ |  t | jj¡¡ z| jj ¡  W n	 t	y'   Y nw t 
|¡S rx   )rÿ   r
   rª  rI  rM  rL  rO  Ú
_parseCallÚcancelr¦   ÚDeferredList)rV   Údlr+   r+   r/   ÚtearDownDNS®  s   ÿ
rV  c                   @   sð   e Zd ZdZdd„ Zdd„ Zdd„ Zeej	dkd	ƒd
d„ ƒZ
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Zeej	dkd	ƒd*d+„ ƒZd,d-„ Zd.d/„ Zd0d1„ Zd2d3„ Zd4S )5ÚMXTestsz6
    Tests for L{mail.relaymanager.MXCalculator}.
    c                 C   s*   t | ƒ t ¡ | _tj | j| j¡| _d S rx   )	rQ  r   r;  r?  r   r
  ÚMXCalculatorrO  Úmxrb   r+   r+   r/   rz   ¿  s   
zMXTests.setUpc                 C   s   t | ƒS rx   )rV  rb   r+   r+   r/   r©   Ä  s   zMXTests.tearDownc                 C   s   |   tj | j¡jt¡ dS )zQ
        L{MXCalculator}'s default clock is C{twisted.internet.reactor}.
        N)r  r   r
  rX  rO  r?  r   rb   r+   r+   r/   Útest_defaultClockÇ  s   ÿzMXTests.test_defaultClockr&   r(   c                 C   s"   dg| j jd< | j d¡ | j¡S )Núthe.email.test.domainr‘  )rE  r6  rY  ÚgetMXr«   Ú_cbSimpleSuccessrb   r+   r+   r/   ÚtestSimpleSuccessÏ  s   zMXTests.testSimpleSuccessc                 C   s$   |   |jd¡ |   t|jƒd¡ d S )Nr   r[  )rC   Ú
preferencer]   rŸ   ©rV   rY  r+   r+   r/   r]  Ô  s   zMXTests._cbSimpleSuccessc                 C   s   d| j _|  | j  d¡t¡S )NFr‘  )rY  ÚfallbackToDomainr  r\  ÚIOErrorrb   r+   r+   r/   ÚtestSimpleFailureØ  s   zMXTests.testSimpleFailurec                 C   s   |   | j d¡t¡S ©Nr‘  )r  rY  r\  r   rb   r+   r+   r/   ÚtestSimpleFailureWithFallbackÜ  r  z%MXTests.testSimpleFailureWithFallbackc                    sF   G ‡‡fdd„dƒ}|ƒ ˆj _ˆj  ˆ¡}‡ ‡fdd„}| |¡ |S )aZ  
        Issue an MX request for the given domain and arrange for it to be
        responded to with the given records.  Verify that the resulting mail
        exchange is the indicated host.

        @type domain: C{str}
        @type records: C{list} of L{RRHeader}
        @type correctMailExchange: C{str}
        @rtype: L{Deferred}
        c                       ó   e Zd Z‡ ‡fdd„ZdS )z,MXTests._exchangeTest.<locals>.DummyResolverc                    s&   |ˆ krt  ˆg g f¡S t  tˆ ƒ¡S rx   )r
   r:  r;  r   )rV   rŸ   ©r_   Úrecordsr+   r/   ÚlookupMailExchangeì  s   z?MXTests._exchangeTest.<locals>.DummyResolver.lookupMailExchangeN©r\   rq   rr   ri  r+   rg  r+   r/   ÚDummyResolverë  ó    rk  c                    s   ˆ  t| jƒˆ ¡ d S rx   ©rC   r]   rŸ   )Úrecord)ÚcorrectMailExchangerV   r+   r/   ÚgotMailExchangeô  s   z.MXTests._exchangeTest.<locals>.gotMailExchange)rY  rO  r\  r«   )rV   r_   rh  ro  rk  rW   rp  r+   )ro  r_   rh  rV   r/   Ú_exchangeTestß  s   

zMXTests._exchangeTestc                 C   sZ   d}d}d}t |tjtd|ƒdt |tjtd|ƒdt |tjtd|ƒdg}|  |||¡S )zh
        The MX record with the lowest preference is returned by
        L{MXCalculator.getMX}.
        r˜  úgood.example.comúbad.example.comrh   ©rŸ   r>  Úpayloadr   r7   )r   r   ÚTYPErq  ©rV   r_   ÚgoodÚbadrh  r+   r+   r/   Útest_mailExchangePreferenceú  s   ýz#MXTests.test_mailExchangePreferencec                 C   sR   d}d}d}t |tjtd|ƒdt |tjtd|ƒdg}| j |¡ |  |||¡S )z
        L{MXCalculator.getMX} returns the MX record with the lowest preference
        which is not also marked as bad.
        r˜  rr  rs  r   rt  rh   ©r   r   rv  rY  ÚmarkBadrq  rw  r+   r+   r/   Útest_badExchangeExcluded
  s   þz MXTests.test_badExchangeExcludedc                 C   s^   d}d}d}t |tjtd|ƒdt |tjtd|ƒdg}| j |¡ | j |¡ |  |||¡S )zœ
        L{MXCalculator.getMX} returns the MX record with the lowest preference
        if all the MX records in the response have been marked bad.
        r˜  rs  zworse.example.comr   rt  rh   r{  )rV   r_   ry  Úworserh  r+   r+   r/   Útest_fallbackForAllBadExchanges  s   þz'MXTests.test_fallbackForAllBadExchangesc                 C   sb   d}d}d}t |tjtd|ƒdt |tjtd|ƒdg}| j |¡ | j | jj¡ |  |||¡S )z¸
        L{MXCalculator.getMX} returns the MX record with the lowest preference
        if it was last marked bad longer than L{MXCalculator.timeOutBadMX}
        seconds ago.
        r˜  rr  rs  r   rt  rh   )	r   r   rv  rY  r|  r?  r<  ÚtimeOutBadMXrq  ©rV   r_   rx  ÚpreviouslyBadrh  r+   r+   r/   Útest_badExchangeExpires+  s   ÿüzMXTests.test_badExchangeExpiresc                 C   sn   d}d}d}t |tjtd|ƒdt |tjtd|ƒdg}| j |¡ | j |¡ | j | jj¡ |  	|||¡S )z
        L{MXCalculator.getMX} returns the MX record with the lowest preference
        if it was marked good after it was marked bad.
        r˜  rr  rs  r   rt  rh   )
r   r   rv  rY  r|  ÚmarkGoodr?  r<  r€  rq  r  r+   r+   r/   Útest_goodExchangeUsed?  s   ÿüzMXTests.test_goodExchangeUsedc                    sJ   d‰d‰ G ‡ ‡fdd„dƒ}|ƒ | j _| j  ˆ ¡}| | jtˆd¡ |S )zè
        If an MX lookup succeeds but the result set is empty,
        L{MXCalculator.getMX} should try to look up an I{A} record for the
        requested name and call back its returned Deferred with that
        address.
        rµ  zexample.orgc                       s6   e Zd ZdZ” g g g fiZ” ”iZdd„ Zdd„ ZdS )z9MXTests.test_successWithoutResults.<locals>.DummyResolverah  
            Fake resolver which will respond to an MX lookup with an empty
            result set.

            @ivar mx: A dictionary mapping hostnames to three-tuples of
                results to be returned from I{MX} lookups.

            @ivar a: A dictionary mapping hostnames to addresses to be
                returned from I{A} lookups.
            c                 S   ó   t  | j| ¡S rx   )r
   r:  rY  r–  r+   r+   r/   ri  l  r{   zLMXTests.test_successWithoutResults.<locals>.DummyResolver.lookupMailExchangec                 S   r†  rx   )r
   r:  r<   r–  r+   r+   r/   ÚgetHostByNameo  r{   zGMXTests.test_successWithoutResults.<locals>.DummyResolver.getHostByNameN)r\   rq   rr   rœ   rY  r<   ri  r‡  r+   ©r_   Úipr+   r/   rk  ]  s    rk  ©rŸ   ©rY  rO  r\  r«   rC   r   ©rV   rk  rW   r+   rˆ  r/   Útest_successWithoutResultsS  s   
z"MXTests.test_successWithoutResultsc                 C   s<   G dd„ dƒ}|ƒ | j _| j  d¡}| | jtdd¡ |S )a'  
        Test that if the MX record lookup fails, fallback is enabled, and an A
        record is available for the name, then the Deferred returned by
        L{MXCalculator.getMX} ultimately fires with a Record_MX instance which
        gives the address in the A record for the name.
        c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )zAMXTests.test_failureWithSuccessfulFallback.<locals>.DummyResolverzw
            Fake resolver which will fail an MX lookup but then succeed a
            getHostByName call.
            c                 S   s   t  tƒ ¡S rx   )r
   r;  r   r–  r+   r+   r/   ri  …  s   zTMXTests.test_failureWithSuccessfulFallback.<locals>.DummyResolver.lookupMailExchangec                 S   s
   t  d¡S )Nrµ  )r
   r:  r–  r+   r+   r/   r‡  ˆ  ó   
zOMXTests.test_failureWithSuccessfulFallback.<locals>.DummyResolver.getHostByNameN)r\   rq   rr   rœ   ri  r‡  r+   r+   r+   r/   rk    s    rk  r_   rµ  rŠ  r‹  rŒ  r+   r+   r/   Ú"test_failureWithSuccessfulFallbackw  s
   
z*MXTests.test_failureWithSuccessfulFallbackc                    sP   d‰ d‰d‰G ‡ ‡‡fdd„dƒ}|ƒ | j _| j  ˆ ¡}| | jtˆd¡ |S )zÌ
        If an MX lookup returns a single CNAME record as a result, MXCalculator
        will perform an MX lookup for the canonical name indicated and return
        the MX record which results.
        úalias.example.comúcanonical.example.comúmail.example.comc                       s    e Zd ZdZ‡ ‡‡fdd„ZdS )z;MXTests.test_cnameWithoutGlueRecords.<locals>.DummyResolverz¬
            Fake resolver which will return a CNAME for an MX lookup of a name
            which is an alias and an MX for an MX lookup of the canonical name.
            c                    sd   |ˆ krt  t|tjtˆƒdgg g f¡S |ˆkr+t  t|tjtdˆƒdgg g f¡S t  t|ƒ¡S )Nrt  r   )r
   r:  r   r   rv  r   r;  r   r–  ©ÚaliasÚ	canonicalÚexchanger+   r/   ri     s2   ýÿ÷ÿýÿ÷ÿzNMXTests.test_cnameWithoutGlueRecords.<locals>.DummyResolver.lookupMailExchangeN)r\   rq   rr   rœ   ri  r+   r“  r+   r/   rk  š  s    rk  rŠ  r‹  rŒ  r+   r“  r/   Útest_cnameWithoutGlueRecords  s   
&z$MXTests.test_cnameWithoutGlueRecordsc                    sX   G dd„ dƒ}d‰ |ƒ ˆj _ˆj  dˆ ¡}ˆ |tjjj¡ ‡ ‡fdd„}| |¡ |S )zÉ
        If L{MXCalculator.getMX} encounters a CNAME chain which is longer than
        the length specified, the returned L{Deferred} should errback with
        L{CanonicalNameChainTooLong}.
        c                   @   s   e Zd ZdZdZdd„ ZdS )z.MXTests.test_cnameChain.<locals>.DummyResolverz
            Fake resolver which generates a CNAME chain of infinite length in
            response to MX lookups.
            r   c                 S   s<   |  j d7  _ d| j f }t t|tjt|ƒdgg g f¡S )Nrh   úx-%d.example.comrt  )ÚchainCounterr
   r:  r   r   rv  )rV   r_   rŸ   r+   r+   r/   ri  Ô  s   ýÿ÷ÿzAMXTests.test_cnameChain.<locals>.DummyResolver.lookupMailExchangeN)r\   rq   rr   rœ   r™  ri  r+   r+   r+   r/   rk  Ì  s    rk  r'   r’  c                    s:   ˆ  | jd tdˆ d f ƒ¡ ˆ  ˆjjjˆ d ¡ d S )Nr   r˜  rh   )rC   Úargsr   rY  rO  r™  )rv  ©Ú
cnameLimitrV   r+   r/   ÚcbChainTooLongê  s   ÿz/MXTests.test_cnameChain.<locals>.cbChainTooLong)	rY  rO  r\  r  Útwistedr   r
  ÚCanonicalNameChainTooLongr«   )rV   rk  rW   r  r+   r›  r/   Útest_cnameChainÅ  s   

zMXTests.test_cnameChainc                    sV   g ‰d‰ d‰d‰G ‡ ‡‡‡fdd„dƒ}|ƒ | j _| j  ˆ ¡}| | jtˆd¡ |S )zü
        If an MX lookup returns a CNAME and the MX record for the CNAME, the
        L{Deferred} returned by L{MXCalculator.getMX} should be called back
        with the name from the MX record without further lookups being
        attempted.
        r  r‘  r’  c                       s   e Zd Z‡ ‡‡‡fdd„ZdS )z8MXTests.test_cnameWithGlueRecords.<locals>.DummyResolverc              	      sL   |ˆ ksˆrg g g fS t  tˆ tjtˆƒdtˆtjtˆddgg g f¡S )Nrt  rŠ  )r
   r:  r   r   rv  r   r–  ©r”  r•  r–  ÚlookedUpr+   r/   ri     s$   
ýýúòÿzKMXTests.test_cnameWithGlueRecords.<locals>.DummyResolver.lookupMailExchangeNrj  r+   r¡  r+   r/   rk  ÿ  s    rk  rŠ  r‹  rŒ  r+   r¡  r/   Útest_cnameWithGlueRecordsó  s   
z!MXTests.test_cnameWithGlueRecordsc                    sH   d‰ d‰G ‡ ‡fdd„dƒ}|ƒ | j _| j  ˆ ¡}|  |tjjj¡ |S )zç
        If an MX lookup returns two CNAME records which point to each other,
        the loop should be detected and the L{Deferred} returned by
        L{MXCalculator.getMX} should be errbacked with L{CanonicalNameLoop}.
        zcname1.example.comzcname2.example.comc                       rf  )z<MXTests.test_cnameLoopWithGlueRecords.<locals>.DummyResolverc                    s4   t  tˆ tjtˆƒdtˆtjtˆ ƒdgg g f¡S )Nrt  )r
   r:  r   r   rv  r–  ©Ú
firstAliasÚsecondAliasr+   r/   ri  '  s    ýýúòÿzOMXTests.test_cnameLoopWithGlueRecords.<locals>.DummyResolver.lookupMailExchangeNrj  r+   r¤  r+   r/   rk  &  rl  rk  )rY  rO  r\  r  rž  r   r
  ÚCanonicalNameLooprŒ  r+   r¤  r/   Útest_cnameLoopWithGlueRecords  s   
z%MXTests.test_cnameLoopWithGlueRecordsc                 C   s$   g d¢| j jd< | j d¡ | j¡S )N)zmx1.test.domainzmx2.test.domainzmx3.test.domainr‘  )rE  r6  rY  r\  r«   Ú_cbManyRecordsSuccessfulLookuprb   r+   r+   r/   ÚtestManyRecords@  s   ÿzMXTests.testManyRecordsc                 C   sH   |   t|jƒ dd¡d dv ¡ | j t|jƒ¡ | j d¡ | j|¡S )NÚ.rh   r   )Úmx1Úmx2Úmx3r‘  )	rN   r]   rŸ   r–   rY  r|  r\  r«   Ú_cbManyRecordsDifferentResultr`  r+   r+   r/   r©  K  s
    ÿz&MXTests._cbManyRecordsSuccessfulLookupc                 C   sB   |   t|jƒt|jƒ¡ | j t|jƒ¡ | j d¡ | j||¡S rd  )rÝ   r]   rŸ   rY  r|  r\  r«   Ú_cbManyRecordsLastResult)rV   ÚnextMXrY  r+   r+   r/   r¯  R  s
   ÿz%MXTests._cbManyRecordsDifferentResultc                 C   sj   |   t|jƒt|jƒ¡ |   t|jƒt|jƒ¡ | j t|jƒ¡ | j t|jƒ¡ | j d¡ | j|¡S rd  )	rÝ   r]   rŸ   rY  r|  r„  r\  r«   Ú"_cbManyRecordsRepeatSpecificResult)rV   ÚlastMXrY  r±  r+   r+   r/   r°  Z  s   ÿz MXTests._cbManyRecordsLastResultc                 C   s   |   t|jƒt|jƒ¡ d S rx   rm  )rV   ÚagainMXr±  r+   r+   r/   r²  e  s   z*MXTests._cbManyRecordsRepeatSpecificResultN)r\   rq   rr   rœ   rz   r©   rZ  r   rs   rt   r^  r]  rc  re  rq  rz  r}  r  rƒ  r…  r  r  r—  r   r£  r¨  rª  r©  r¯  r°  r²  r+   r+   r+   r/   rW  ¹  s8    
$5.*#

rW  c                   @   s`   e Zd Ze ed¡du rdZdd„ Zdd„ Ze	e
jdkdƒd	d
„ ƒZe	e
jdkdƒdd„ ƒZdS )ÚLiveFireExerciseTestsNz1UDP support is required to determining MX recordsc                 C   s   t | ƒ g d¢| _d S )N)Ú	domainDirÚinsertionDomainÚinsertionQueueÚdestinationDomainÚdestinationQueue)rQ  Útmpdirsrb   r+   r+   r/   rz   n  s   zLiveFireExerciseTests.setUpc                 C   s*   | j D ]}tj |¡rt |¡ qt| ƒS rx   )r»  r§   r±   r~   r1  r2  rV  )rV   rW   r+   r+   r/   r©   x  s
   

€zLiveFireExerciseTests.tearDownr&   r(   c                    sü   t j  ¡ }|j tj ¡ ¡ t j |d¡‰ˆ 	dd¡ | 
dˆ¡ |jd |jd< t|jd jˆ ¡ ƒ | t j |¡¡ | ¡ }tjd|ddˆ_tg d	¢ƒ‰ tƒ ‰t ¡ }‡ fd
d„|_‡fdd„|_t dˆj ¡ j|¡ ‡‡fdd„}ˆ |¡ ˆS )Nr¶  rn  rƒ   r‘  rÆ   r   r»   rB  )ú
HELO mesonzMAIL FROM: <user@hostname>zRCPT TO: <user@test.domain>ÚDATAúThis is the messager«  ÚQUITc                      r  rx   r+   r+   ©r0  r+   r/   r  œ  r	  z9LiveFireExerciseTests.testLocalDelivery.<locals>.<lambda>c                     ó
   ˆ   d ¡S rx   ©Úcallback©rš  ©rÿ  r+   r/   r    ó   
 c                    s>   ˆ   dd tj¡d }| d¡ ¡ }ˆ | d¡d¡ ˆj ¡ S ©Nrn  rh   r   r¾  éÿÿÿÿ)	rp  r   rq  rÚ   rµ   rÝ   ÚfindÚ
smtpServerrM  ©Úignr  r*  )r_   rV   r+   r/   Úfinished   s   
z9LiveFireExerciseTests.testLocalDelivery.<locals>.finished)r   rŠ   Ú
smtpPortalr¾  r   re  ÚAllowAnonymousAccessrÒ   rY  r„   r‹   rÈ   r½  rd  ÚsetQueuerÝ  rÞ  rÁ   r   rH  rÊ  r    r   r   ÚClientFactoryÚclientConnectionLostÚ
connectTCPrJ  rI  r«   )rV   r—   r¢   rÍ  r+   )r0  r_   rÿ  rV   r/   ÚtestLocalDelivery~  s*   
ÿ
z'LiveFireExerciseTests.testLocalDeliveryc                    sÐ  t j  ¡ }|j tj ¡ ¡ t j |d¡‰| 	dˆ¡ t
 d¡ | t j d¡¡ |j t j |¡¡ t j |j¡‰ˆ jd7  _t j ˆd¡‰t j ˆj¡ˆ_dgˆjjd< | ¡ }tjd|dd	ˆ_t j  ¡ }|j tj ¡ ¡ t j |d
¡‰ˆ  dd¡ | 	dˆ¡ t
 d¡ | t j d¡¡ t j ˆd¡‰ˆ !¡  | ¡ }tjd|dd	ˆ_"ˆj" #¡ j$ˆ_%t&g d¢ƒ‰ t'ƒ ‰t( )¡ }‡ fdd„|_(‡fdd„|_*t +dˆj #¡ j$|¡ ‡‡‡‡fdd„}ˆ ,|¡ ˆS )Nr·  zinsertion.domainr¸  )ztest.identity.hostnamerh   r»   zdestination.domainr   rB  r¹  rn  rƒ   rº  )r¼  zMAIL FROM: <user@wherever>z"RCPT TO: <user@destination.domain>r½  r¾  r«  r¿  c                      r  rx   r+   r+   rÀ  r+   r/   r  à  r	  z9LiveFireExerciseTests.testRelayDelivery.<locals>.<lambda>c                     rÁ  rx   rÂ  rÄ  rÅ  r+   r/   r  á  rÆ  c                    s&   ˆ  ¡ }‡ ‡‡fdd„}| |¡ |S )Nc                    sT   ˆ   dd tj¡d }| d¡ ¡ }ˆ | d¡d¡ ˆj ¡  ˆj	 ¡  ˆ 
¡  d S rÇ  )rp  r   rq  rÚ   rµ   rÝ   rÉ  Ú	insServerrM  Ú
destServerÚstopServicerË  )r_   ÚhelperrV   r+   r/   Ú	deliveredé  s   

zLLiveFireExerciseTests.testRelayDelivery.<locals>.finished.<locals>.delivered)Ú
checkStater«   )rÌ  r   rÙ  )r_   rØ  r	  rV   r+   r/   rÍ  ä  s   
	z9LiveFireExerciseTests.testRelayDelivery.<locals>.finished)-r   rŠ   rÎ  r¾  r   re  rÏ  rÒ   rY  r‹   r§   rí  rÐ  r
  r  rT   ÚsetDefaultDomainrÝ  rÞ  ÚSmartHostSMTPRelayingManagerr  ÚfArgsÚRelayStateHelperrX  rO  ÚmxcalcrE  r6  rÁ   r   rH  rÕ  r„   ÚstartServicerÖ  rJ  rI  ÚPORTr    r   r   rÑ  rÒ  rÓ  r«   )rV   ÚinsServr¢   ÚdestServrÍ  r+   )r0  r_   rÿ  rØ  r	  rV   r/   ÚtestRelayDeliveryª  sJ   



ÿ
z'LiveFireExerciseTests.testRelayDelivery)r\   rq   rr   r   ÚIReactorUDPr   Úskiprz   r©   r   rs   rt   rÔ  rä  r+   r+   r+   r/   rµ  i  s    

+rµ  c                   @   ru   )
ÚLineBufferMessagec                 C   s   g | _ d| _d| _d S )NF)ÚlinesÚeomÚlostrb   r+   r+   r/   r×  ú  r   zLineBufferMessage.__init__c                 C   r  rx   )rè  rÿ   )rV   r·   r+   r+   r/   r’   ÿ  r{   zLineBufferMessage.lineReceivedc                 C   s   d| _ t d¡S )NTz
<Whatever>)ré  r
   r:  rb   r+   r+   r/   rª     ó   
zLineBufferMessage.eomReceivedc                 C   s
   d| _ d S )NT)rê  rb   r+   r+   r/   r”     rŽ  z LineBufferMessage.connectionLostN)r\   rq   rr   r×  r’   rª   r”   r+   r+   r+   r/   rç  ù  s
    rç  c                   @   sd   e Zd Zg d¢Zdd„ Zeejdkdƒdd„ ƒZdd	„ Z	d
d„ Z
eejdkdƒdd„ ƒZdd„ ZdS )Ú
AliasTests©z
First linez	Next linerÆ   zAfter a blank linez	Last linec                 C   sx   i }g d¢}|D ]}t j ||dd ¡ q|  |d ddg¡ |  |d dg¡ |  |d d	g¡ |  |d
 g d¢¡ d S )N)zuser:  another@host
znextuser:  |/bin/program
zuser:  me@again
z"moreusers: :/etc/include/filename
z3multiuser: first@host, second@host,last@anotherhostr#   rn  zanother@hostzme@againÚnextuserz|/bin/programÚ	moreusersz:/etc/include/filenameÚ	multiuser)z
first@hostzsecond@hostzlast@anotherhost)r   r”  ÚhandlerC   )rV   r³   rè  Úlr+   r+   r/   Ú
testHandle  s   ÿzAliasTests.testHandler&   r(   c                 C   s
  dt ƒ i}tjj|t t d¡ ¡ ¡d}|  	t
|ƒd¡ |d }t|ƒ}dD ]}|  | |¡d¡ q'|  	t
|ƒd¡ |d	 }t|ƒ}d
D ]}|  | |¡d¡ qE|  	t
|ƒd¡ |d }t|ƒ}|  	| d¡d¡ dD ]}|  | |¡dd| ¡ ql|  	t
|ƒd¡ d S )NrÆ   a…                      # Here's a comment
                       # woop another one
                    testuser:                   address1,address2, address3,
                        continuation@address, |/bin/process/this

                    usertwo:thisaddress,thataddress, lastaddress
                    lastuser:       :/includable, /filename, |/program, address
                    )r¤   r'   Útestuser)Úaddress1Úaddress2Úaddress3zcontinuation@addressz/bin/process/thisrÈ  rD  Úusertwo)ÚthisaddressÚthataddressÚlastaddressÚlastuserz/includable)z	/filenameÚprogramr	   z%s not found)rÇ   r   r”  ÚloadAliasFilerž  rŸ  ÚtextwrapÚdedentÚencoderC   rD   r]   rÝ   rÉ  )rV   rT   r³   ÚgroupÚsr<   r+   r+   r/   ÚtestFileLoader"  s6   
ÿ
õþzAliasTests.testFileLoaderc                 C   sD   t ƒ t ƒ t ƒ f}tj |¡}| jD ]}| |¡ q| ¡  | j|¡S rx   )	rç  r   r”  ÚMultiWrapperrè  r’   rª   r«   Ú_cbMultiWrapper)rV   rK  r*  ÚLr+   r+   r/   ÚtestMultiWrapperQ  s
   
zAliasTests.testMultiWrapperc                 C   s6   |D ]}|   |j¡ |  |j¡ |  | j|j¡ qd S rx   )rN   ré  r°   rê  rC   rè  )rV   r  rK  r   r+   r+   r/   r  Y  s
   ýzAliasTests._cbMultiWrapperc                 C   sH   |   ¡ }tj |d d ¡}| ¡ }| jD ]}| |¡ q| ¡  | j	|¡S rx   )
r  r   r”  Ú	FileAliasÚcreateMessageReceiverrè  r’   rª   r«   Ú_cbTestFileAlias)rV   Útmpfiler<   r   rò  r+   r+   r/   ÚtestFileAlias_  s   
zAliasTests.testFileAliasc                 C   sL   t |ƒ}| ¡ }W d   ƒ n1 sw   Y  |  dd„ |D ƒ| j¡ d S )Nc                 S   ó   g | ]}|d d… ‘qS ©NrÈ  r+   ©r-   r  r+   r+   r/   r4   l  r5   z/AliasTests._cbTestFileAlias.<locals>.<listcomp>©r¡   Ú	readlinesrC   rè  )rV   r  r  r¢   rè  r+   r+   r/   r  i  ó   

ÿzAliasTests._cbTestFileAliasN)r\   rq   rr   rè  ró  r   rs   rt   r  r  r  r  r  r+   r+   r+   r/   rì  
  s    
.
	rì  c                   @   s"   e Zd ZdZdd„ Zddd„ZdS )ÚDummyDomainz/
    Test domain for L{AddressAliasTests}.
    c                 C   s
   || _ d S rx   )r	   )rV   r	   r+   r+   r/   r×  t  rŽ  zDummyDomain.__init__Nc                    s   |rt j d¡‚‡ ‡fdd„S )a
  
        @returns: When a C{memo} is passed in this will raise a
            L{smtp.SMTPBadRcpt} exception, otherwise a boolean
            indicating if the C{user} and string version of
            L{self.address} are equal or not.
        @rtype: C{bool}
        Úhamc                      s   ˆt ˆ jƒkS rx   )r]   r	   r+   r­  r+   r/   r  ‚  s    z$DummyDomain.exists.<locals>.<lambda>)r   r   r…   r…  r+   r­  r/   r~   w  s   zDummyDomain.existsrx   )r\   rq   rr   rœ   r×  r~   r+   r+   r+   r/   r  o  s    r  c                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚAddressAliasTestsz7
    Tests for L{twisted.mail.alias.AddressAlias}.
    c                 C   s:   t j d¡| _| jjt| jƒi}t j | j|| j¡| _dS )z+
        Setup an L{AddressAlias}.
        zfoo@barN)r   r   r  r	   r_   r  r”  ÚAddressAlias)rV   rT   r+   r+   r/   rz   ‹  s   zAddressAliasTests.setUpc                 C   s   |   | j ¡ ¡ dS )z”
        L{createMessageReceiever} calls C{exists()} on the domain object
        which key matches the C{alias} passed to L{AddressAlias}.
        N)rN   r”  r
  rb   r+   r+   r/   Útest_createMessageReceiver“  s   z,AddressAliasTests.test_createMessageReceiverc                 C   s   |   t| jƒd¡ dS )zP
        The string presentation of L{AddressAlias} includes the alias.
        z<Address foo@bar>N)rC   r]   r”  rb   r+   r+   r/   rc   š  rU  zAddressAliasTests.test_strc                 C   s   |   | j | jdi¡d¡ dS )z—
        L{resolve} will look for additional aliases when an C{aliasmap}
        dictionary is passed, and returns L{None} if none were found.
        rÜ   N)rC   r”  Úresolver	   rb   r+   r+   r/   Útest_resolve   s   zAddressAliasTests.test_resolvec                 C   s   |   | j i ¡d¡ dS )z¡
        L{resolve} returns L{None} when the alias could not be found in the
        C{aliasmap} and no L{mail.smtp.User} with this alias exists either.
        N)rC   r”  r  rb   r+   r+   r/   Útest_resolveWithoutAliasmap§  s   z-AddressAliasTests.test_resolveWithoutAliasmapN)	r\   rq   rr   rœ   rz   r  rc   r  r  r+   r+   r+   r/   r  …  s    r  c                   @   s   e Zd ZdgZdS )ÚDummyProcessÚonEndN)r\   rq   rr   Ú	__slots__r+   r+   r+   r/   r  ¯  s    
r  c                   @   r‡   )ÚMockProcessAliaszD
    An alias processor that doesn't actually launch processes.
    c                 C   ó   dS )z(
        Don't spawn a process.
        Nr+   )rV   Úprotorý  r±   r+   r+   r/   ÚspawnProcess¸  r	  zMockProcessAlias.spawnProcessN)r\   rq   rr   rœ   r"  r+   r+   r+   r/   r  ³  s    r  c                   @   s   e Zd ZdZeZdS )ÚMockAliasGroupz3
    An alias group using C{MockProcessAlias}.
    N)r\   rq   rr   rœ   r  ÚprocessAliasFactoryr+   r+   r+   r/   r#  ¾  s    r#  c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚStubProcessz—
    Fake implementation of L{IProcessTransport}.

    @ivar signals: A list of all the signals which have been sent to this fake
        process.
    c                 C   s
   g | _ d S rx   )Úsignalsrb   r+   r+   r/   r×  Î  rŽ  zStubProcess.__init__c                 C   r   )z8
        No-op implementation of disconnection.
        Nr+   rb   r+   r+   r/   ÚloseConnectionÑ  r	  zStubProcess.loseConnectionc                 C   s   | j  |¡ dS )zL
        Record a signal sent to this process for later inspection.
        N)r&  rÿ   )rV   Úsignalr+   r+   r/   ÚsignalProcessÖ  s   zStubProcess.signalProcessN)r\   rq   rr   rœ   r×  r'  r)  r+   r+   r+   r/   r%  Æ  s
    r%  c                   @   s¼   e Zd ZdZe ed¡du rdZg d¢Zdd„ Z	dd„ Z
d	d
„ Zdd„ Zeejdkdƒdd„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zeejdkdƒdd„ ƒZeejdkdƒdd„ ƒZdS )ÚProcessAliasTestsz%
    Tests for alias resolution.
    NzIReactorProcess not supportedrí  c                 C   sB   |d> dB }|   t |¡¡ |  t |¡|¡ |  t |¡¡ |S )a  
        Construct a status from the given exit code.

        @type code: L{int} between 0 and 255 inclusive.
        @param code: The exit status which the code will represent.

        @rtype: L{int}
        @return: A status integer for the given exit code.
        é   r   )rN   r§   Ú	WIFEXITEDrC   ÚWEXITSTATUSr°   ÚWIFSIGNALED)rV   ÚcodeÚstatusr+   r+   r/   Ú
exitStatusè  s
   zProcessAliasTests.exitStatusc                 C   s:   |}|   t |¡¡ |  t |¡|¡ |  t |¡¡ |S )a  
        Construct a status from the given signal.

        @type signal: L{int} between 0 and 255 inclusive.
        @param signal: The signal number which the status will represent.

        @rtype: L{int}
        @return: A status integer for the given signal.
        )rN   r§   r.  rC   ÚWTERMSIGr°   r,  )rV   r(  r0  r+   r+   r/   ÚsignalStatusý  s
   zProcessAliasTests.signalStatusc                 C   s   t j| _dt _dS )zB
        Replace L{smtp.DNSNAME} with a well-known value.
        rÆ   N)r   ÚDNSNAMErb   r+   r+   r/   rz     s   
zProcessAliasTests.setUpc                 C   s   | j t_ dS )z@
        Restore the original value of L{smtp.DNSNAME}.
        N)r4  r   rb   r+   r+   r/   r©     s   zProcessAliasTests.tearDownr&   r(   c                    sn   t ˆ  ¡ ƒ}| d¡ t |jd¡ tj |jdd¡}| 	¡ }ˆ j
D ]}| |¡ q"‡ fdd„}| ¡  |¡S )z¤
        Standard call to C{mail.alias.ProcessAlias}: check that the specified
        script is called, and that the input is correctly transferred to it.
        zX#!/bin/sh
rm -f process.alias.out
while read i; do
    echo $i >> process.alias.out
doneiÀ  Nc                    sL   t dƒ}| ¡ }W d   ƒ n1 sw   Y  ˆ  dd„ |D ƒˆ j¡ d S )Nzprocess.alias.outc                 S   r  r  r+   r  r+   r+   r/   r4   ;  r5   zPProcessAliasTests.test_processAlias.<locals>._cbProcessAlias.<locals>.<listcomp>r  )r  r¢   rè  rb   r+   r/   Ú_cbProcessAlias8  r  z<ProcessAliasTests.test_processAlias.<locals>._cbProcessAlias)r   r  Ú
setContentr§   Úchmodr±   r   r”  ÚProcessAliasr
  rè  r’   rª   r«   )rV   Úshr<   r   rò  r5  r+   rb   r/   Útest_processAlias"  s   ÿ
z#ProcessAliasTests.test_processAliasc                    st   t  ¡ }tƒ ‰tj ¡ ‰ ˆ  ˆ¡ tj ˆ d|¡}| ¡ }| 	|j
¡ ‡ ‡‡fdd„}ˆ |tjj¡ | |¡ |S )a  
        If the alias child process does not exit within a particular period of
        time, the L{Deferred} returned by L{MessageWrapper.eomReceived} should
        fail with L{ProcessAliasTimeout} and send the I{KILL} signal to the
        child process..
        Nc                    s*   ˆ  ˆjdg¡ ˆ  tˆ tj¡ƒ¡ d S )NÚKILL)rC   r&  ÚprocessEndedr   r3  r(  ÚSIGKILLr  ©r!  rV   rš   r+   r/   ÚtimedOutO  s   z<ProcessAliasTests.test_processAliasTimeout.<locals>.timedOut)r   r;  r%  r   r”  ÚProcessAliasProtocolr‘   ÚMessageWrapperrª   r<  ÚcompletionTimeoutr  ÚProcessAliasTimeoutr«   )rV   r   ÚreceiverrW   r?  r+   r>  r/   Útest_processAliasTimeout?  s   


z*ProcessAliasTests.test_processAliasTimeoutc                 C   sN   t ƒ }tj ¡ }| |¡ tj |dd¡}| t t	dƒ¡¡ |  
| ¡ t	¡S )z¿
        If the process associated with an L{mail.alias.MessageWrapper} exits
        before I{eomReceived} is called, the L{Deferred} returned by
        I{eomReceived} should fail.
        Nr   )r%  r   r”  r@  r‘   rA  r<  r   r  r   r  rª   )rV   rš   r   rD  r+   r+   r/   Útest_earlyProcessTerminationY  ó   

z.ProcessAliasTests.test_earlyProcessTerminationc                 C   sN   t ƒ }tj ¡ }| |¡ tj |dd¡}| t t	|ƒ¡¡ |  
| ¡ t	¡S )zÕ
        Verify that if the process associated with an
        L{mail.alias.MessageWrapper} exits with the given status, the
        L{Deferred} returned by I{eomReceived} fails with L{ProcessTerminated}.
        N)r%  r   r”  r@  r‘   rA  r<  r   r  r   r  rª   )rV   r0  rš   r   rD  r+   r+   r/   Ú_terminationTestf  rG  z"ProcessAliasTests._terminationTestc                 C   s   |   |  d¡¡S )z¹
        If the process associated with an L{mail.alias.MessageWrapper} exits
        with a non-zero exit code, the L{Deferred} returned by I{eomReceived}
        should fail.
        rh   )rH  r1  rb   r+   r+   r/   Útest_errorProcessTerminations  s   z.ProcessAliasTests.test_errorProcessTerminationc                 C   s   |   |  tj¡¡S )z¼
        If the process associated with an L{mail.alias.MessageWrapper} exits
        because it received a signal, the L{Deferred} returned by
        I{eomReceived} should fail.
        )rH  r3  r(  ÚSIGHUPrb   r+   r+   r/   Útest_signalProcessTermination{  s   z/ProcessAliasTests.test_signalProcessTerminationc                 C   sx  i }dt |g d¢ƒi}tg d¢|dƒ}tddg|dƒ}tj d|d¡}| |||d	œ¡ | |¡}tt|j	ƒ}| 
¡  tttj d
dd¡tj tƒ d¡tj d¡gƒ}| 
¡  |  ||¡ | |¡}	tt|	j	ƒ}
|
 
¡  tttj ddd¡tj ddd¡gƒ}| 
¡  |  |
|¡ | |¡}tt|j	ƒ}| 
¡  tttj d
dd¡tj tƒ d¡tj d¡gƒ}| 
¡  |  ||¡ dS )aÅ  
        Check that the C{resolve} method of alias processors produce the correct
        set of objects:
            - direct alias with L{mail.alias.AddressAlias} if a simple input is passed
            - aliases in a file with L{mail.alias.FileWrapper} if an input in the format
              '/file' is given
            - aliases resulting of a process call wrapped by L{mail.alias.MessageWrapper}
              if the format is '|process'
        rÆ   )r[  r]  r_  )r[  ú|echoú/fileÚalias1r]  r_  Úalias2Úalias3©rN  rO  rP  r[  NÚechorM  )Ú
TestDomainr#  r   r”  r  rO   r  r½  r]   ÚobjsrI   rA  r  ÚFileWrapperrC   )rV   r‹  r_   ÚA1ÚA2ÚA3Úres1Úr1ÚexpectedÚres2Úr2Úres3Úr3r+   r+   r/   Útest_aliasResolutionƒ  s\   ýÿ

ýþ
þþ

ýþz&ProcessAliasTests.test_aliasResolutionc           
      C   sü   i }dt |g ƒi}tj d|d¡}tj d|d¡}tj d|d¡}| |||dœ¡ |  |d  |¡d¡ |  |d  |¡d¡ |  |d  |¡d¡ tddg|dƒ}||d< | |¡}tt	|j
ƒ}| ¡  tt	tj tƒ d	¡gƒ}	|	 ¡  |  ||	¡ dS )
zN
        Check that a cycle in alias resolution is correctly handled.
        rÆ   rO  rN  rP  rQ  NrL  Úalias4rR  )rS  r   r”  r  rO   rC   r  r#  r½  r]   rT  rI   rA  r  )
rV   r‹  r_   rV  rW  rX  ÚA4ÚresÚrr[  r+   r+   r/   Útest_cyclicAliasÄ  s"   
z"ProcessAliasTests.test_cyclicAlias)r\   rq   rr   rœ   r   ÚIReactorProcessr   ræ  rè  r1  r3  rz   r©   r   rs   rt   r:  rE  rF  rH  rI  rK  r`  re  r+   r+   r+   r/   r*  Ý  s(    

@r*  c                   @   r3  )rS  c                 C   s   || _ || _d S rx   )r‹  Úusers)rV   r‹  rg  r+   r+   r/   r×  à  rë  zTestDomain.__init__Nc                    sn   ˆj j‰ˆ| jv r‡fdd„S z| jˆ }W n ty"   t ˆ¡‚w | | j|¡‰ ˆ r2‡ fdd„S t ˆ¡‚)Nc                      s   t j ˆ d d ¡S rx   )r   r”  r  r+   )rn  r+   r/   r  ç  s    z#TestDomain.exists.<locals>.<lambda>c                      r  rx   r+   r+   )r‹  r+   r/   r  ï  r	  )râ  Úlocalrg  r‹  r¦   r   r…   r  )rV   rn  r†  r<   r+   )r‹  rn  r/   r~   ä  s   

ÿ
zTestDomain.existsrx   )r\   rq   rr   r×  r~   r+   r+   r+   r/   rS  ß  rA  rS  c                   @   s@   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dS )Ú
DummyQueuezÒ
    A fake relay queue to use for testing.

    This queue doesn't keep track of which messages are waiting to be relayed
    or are in the process of being relayed.

    @ivar directory: See L{__init__}.
    c                 C   r‰  )zŽ
        @type directory: L{bytes}
        @param directory: The pathname of the directory holding messages in the
            queue.
        N)Ú	directory)rV   rj  r+   r+   r/   r×  ý  s   
zDummyQueue.__init__c                 C   s8   t j |¡}t  |  |¡d ¡ t  |  |¡d ¡ dS )z‹
        Remove a message from the queue.

        @type message: L{bytes}
        @param message: The base filename of a message.
        rì  rè  N)r§   r±   Úbasenamer¨   ÚgetPath©rV   rj   r+   r+   r/   rÿ  	  s   zDummyQueue.donec                 C   s   t tj | j|d ¡dƒS )zé
        Get the envelope file for a message in the queue.

        @type message: L{bytes}
        @param message: The base filename of a message.

        @rtype: L{file}
        @return: The envelope file for the message.
        rè  Úrb)r¡   r§   r±   r7  rj  rm  r+   r+   r/   ÚgetEnvelopeFile	  s   
zDummyQueue.getEnvelopeFilec                 C   s   t j | j|¡S )zõ
        Return the full base pathname of a message in the queue.

        @type message: L{bytes}
        @param message: The base filename of a message.

        @rtype: L{bytes}
        @return: The full base pathname of the message.
        )r§   r±   r7  rj  rm  r+   r+   r/   rl  	  s   
zDummyQueue.getPathc                 C   sv   t   ¡ › dt| ƒ› }ttj | j|d ¡dƒ}tj | j|d ¡}tj | j|d ¡}t|dƒ}|tj |||¡fS )zß
        Create a new message in the queue.

        @rtype: 2-L{tuple} of (E{1}) L{file}, (E{2}) L{FileMessage}
        @return: The envelope file and a message receiver for a new message in
            the queue.
        Ú_rè  ré  z-Crì  )	ÚtimeÚidr¡   r§   r±   r7  rj  r   r£   )rV   ÚfnameÚ
headerFileÚtempFilenameÚfinalFilenameÚmessageFiler+   r+   r/   r  (	  s   
ÿzDummyQueue.createNewMessagec                 C   r   )z©
        Ignore the request to mark a message as waiting to be relayed.

        @type message: L{bytes}
        @param message: The base filename of a message.
        Nr+   rm  r+   r+   r/   r$  :	  rˆ  zDummyQueue.setWaitingN)
r\   rq   rr   rœ   r×  rÿ  ro  rl  r  r$  r+   r+   r+   r/   ri  ó  s    	ri  c                   @   r‡   )Ú!DummySmartHostSMTPRelayingManagera4  
    A fake smart host to use for testing.

    @type managed: L{dict} of L{bytes} -> L{list} of
        L{list} of L{bytes}
    @ivar managed: A mapping of a string identifying a managed relayer to
        filenames of messages the managed relayer is responsible for.

    @ivar queue: See L{__init__}.
    c                 C   s   i | _ || _dS )z´
        Initialize the minimum necessary members of a smart host.

        @type queue: L{DummyQueue}
        @param queue: A queue that can be used for testing purposes.
        N)Úmanagedr  )rV   r  r+   r+   r/   r×  P	  s   
z*DummySmartHostSMTPRelayingManager.__init__N)r\   rq   rr   rœ   r×  r+   r+   r+   r/   rx  D	  s    rx  c                   @   s°   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zeejdkdƒdd„ ƒZeejdkdƒdd„ ƒZdd„ Zdd„ Zd d!„ Zd"d#„ Zd$S )%Ú_AttemptManagerTestsa  
    Test the behavior of L{_AttemptManager}.

    @type tmpdir: L{bytes}
    @ivar tmpdir: The path to a temporary directory holding the message files.

    @type reactor: L{MemoryReactorClock}
    @ivar reactor: The reactor used for test purposes.

    @type eventLog: L{None} or L{dict} of L{bytes} -> L{object}
    @ivar eventLog: Information about the last informational log message
        generated or none if no log message has been generated.

    @type noisyAttemptMgr: L{_AttemptManager}
    @ivar noisyAttemptMgr: An attempt manager which generates informational
        log messages.

    @type quietAttemptMgr: L{_AttemptManager}
    @ivar quietAttemptMgr: An attempt manager which does not generate
        informational log messages.

    @type noisyMessage: L{bytes}
    @ivar noisyMessage: The full base pathname of the message to be used with
        the noisy attempt manager.

    @type quietMessage: L{bytes}
    @ivar quietMessage: The full base pathname of the message to be used with
        the quiet.
    c                 C   s^  |   ¡ | _t | j¡ tƒ | _d| _t | j	¡ t
tt| jƒƒd| jƒ| _t
tt| jƒƒd| jƒ| _d}d}tj | j|¡| _tj | j|¡| _t| jd dƒ ¡  t| jd dƒ ¡  |g| jjjd< |g| jjjd	< t| jd
 dƒ}t ddg|¡ W d  ƒ n1 s‡w   Y  t| jd
 dƒ}t ddg|¡ W d  ƒ dS 1 s¨w   Y  dS )a  
        Set up a temporary directory for the queue, attempt managers with the
        noisy flag on and off, message files for use with each attempt manager,
        and a reactor.  Also, register to be notified when log messages are
        generated.
        NTFÚnoisyMessageÚquietMessagerì  rž   ÚnoisyRelayerÚquietRelayerrè  ré  zfrom-noisy@domainzto-noisy@domainzfrom-quiet@domainzto-quiet@domain)r  r•  r§   rí  r!   r   ÚeventLogr   ÚaddObserverÚ_logObserverr   rx  ri  ÚnoisyAttemptMgrÚquietAttemptMgrr±   r7  r{  r|  r¡   r¥   r	  ry  rï  rð  )rV   ÚnoisyBaseNameÚquietBaseNameÚenveloper+   r+   r/   rz   {	  s:   
ýýÿ"ÿz_AttemptManagerTests.setUpc                 C   s   t  | j¡ t | j¡ dS )zO
        Unregister for log events and remove the temporary directory.
        N)r   ÚremoveObserverr  r1  r2  r•  rb   r+   r+   r/   r©   ¨	  s   z_AttemptManagerTests.tearDownc                 C   r‰  )zÂ
        A log observer.

        @type eventDict: L{dict} of L{bytes} -> L{object}
        @param eventDict: Information about the last informational log message
            generated.
        N)r  )rV   Ú	eventDictr+   r+   r/   r  ¯	  s   
z!_AttemptManagerTests._logObserverc                 C   s"   t tt| jƒƒƒ}|  |j¡ dS )z
        When an attempt manager is created without the noisy parameter, the
        noisy instance variable should default to true.
        N)r   rx  ri  r•  rN   r  ©rV   Úamr+   r+   r/   Útest_initNoisyDefault¹	  s   z*_AttemptManagerTests.test_initNoisyDefaultc                 C   ó   |   | jj¡ dS )z•
        When an attempt manager is created with the noisy parameter set to
        true, the noisy instance variable should be set to true.
        N)rN   r‚  r  rb   r+   r+   r/   Útest_initNoisyÁ	  ó   z#_AttemptManagerTests.test_initNoisyc                 C   rŒ  )z—
        When an attempt manager is created with the noisy parameter set to
        false, the noisy instance variable should be set to false.
        N)r°   rƒ  r  rb   r+   r+   r/   Útest_initQuietÈ	  rŽ  z#_AttemptManagerTests.test_initQuietc                 C   s$   t tt| jƒƒƒ}|  |jt¡ dS )zŸ
        When an attempt manager is created without the reactor parameter, the
        reactor instance variable should default to the global reactor.
        N)r   rx  ri  r•  rC   r   r‰  r+   r+   r/   Útest_initReactorDefaultÏ	  s   z,_AttemptManagerTests.test_initReactorDefaultc                 C   s   |   | jj| j¡ dS )z“
        When an attempt manager is created with a reactor provided, the
        reactor instance variable should default to that reactor.
        N)rC   r‚  r   rb   r+   r+   r/   Útest_initReactor×	  s   z%_AttemptManagerTests.test_initReactorc                 C   ó    | j  d| j¡ |  | j¡ dS )zw
        For an attempt manager with the noisy flag set, notifySuccess should
        result in a log message.
        r}  N)r‚  r  r{  rN   r  rb   r+   r+   r/   Útest_notifySuccessNoisyÞ	  ó   z,_AttemptManagerTests.test_notifySuccessNoisyc                 C   r’  )z|
        For an attempt manager with the noisy flag not set, notifySuccess
        should result in no log message.
        r~  N)rƒ  r  r|  r°   r  rb   r+   r+   r/   Útest_notifySuccessQuietæ	  r”  z,_AttemptManagerTests.test_notifySuccessQuietr&   r(   c                 C   r’  )zw
        For an attempt manager with the noisy flag set, notifyFailure should
        result in a log message.
        r}  N)r‚  r  r{  rN   r  rb   r+   r+   r/   Útest_notifyFailureNoisyî	  ó   z,_AttemptManagerTests.test_notifyFailureNoisyc                 C   r’  )z|
        For an attempt manager with the noisy flag not set, notifyFailure
        should result in no log message.
        r~  N)rƒ  r  r|  r°   r  rb   r+   r+   r/   Útest_notifyFailureQuiet÷	  r—  z,_AttemptManagerTests.test_notifyFailureQuietc                 C   ó   | j  d¡ |  | j¡ dS )zt
        For an attempt manager with the noisy flag set, notifyDone should
        result in a log message.
        r}  N)r‚  r  rN   r  rb   r+   r+   r/   Útest_notifyDoneNoisy 
  ó   z)_AttemptManagerTests.test_notifyDoneNoisyc                 C   r™  )zy
        For an attempt manager with the noisy flag not set, notifyDone
        should result in no log message.
        r~  N)rƒ  r  r°   r  rb   r+   r+   r/   Útest_notifyDoneQuiet
  r›  z)_AttemptManagerTests.test_notifyDoneQuietc                 C   ó(   | j  d¡ |  | j¡ | j d¡ dS )z|
        For an attempt manager with the noisy flag set, notifyNoConnection
        should result in a log message.
        r}  r7  N)r‚  ÚnotifyNoConnectionrN   r  r   r<  rb   r+   r+   r/   Útest_notifyNoConnectionNoisy
  ó   z1_AttemptManagerTests.test_notifyNoConnectionNoisyc                 C   r  )z
        For an attempt manager with the noisy flag not set, notifyNoConnection
        should result in no log message.
        r~  r7  N)rƒ  rž  r°   r  r   r<  rb   r+   r+   r/   Útest_notifyNoConnectionQuiet
  r   z1_AttemptManagerTests.test_notifyNoConnectionQuietN)r\   rq   rr   rœ   rz   r©   r  r‹  r  r  r  r‘  r“  r•  r   rs   rt   r–  r˜  rš  rœ  rŸ  r¡  r+   r+   r+   r/   rz  [	  s(    -


	rz  )}rœ   Úemail.messager   Úemail.parserrê   rž  r§   rï  r1  r(  rs   r+  rÿ  rq  Úhashlibr   Úunittestr   Úzope.interfacer   r   Úzope.interface.verifyr   Útwisted.cred.checkersrž  Útwisted.cred.credentialsÚtwisted.cred.portalÚtwisted.mail.aliasÚtwisted.mail.mailÚtwisted.mail.maildirÚtwisted.mail.protocolsÚtwisted.mail.relayÚtwisted.mail.relaymanagerr   r   Útwisted.internetr	   r
   r   r   r   r   Útwisted.internet.deferr   Útwisted.internet.errorr   r   r   r   Útwisted.mailr   r   r   Útwisted.namesr   Útwisted.names.dnsr   r   r   Útwisted.names.errorr   Útwisted.pythonr   r   Útwisted.python.filepathr   Útwisted.python.runtimer   Útwisted.test.proto_helpersr    r!   r"   Útwisted.trial.unittestr#   r%   rv   rˆ   r   rº   rÎ   rÒ   Ú _MaildirMailboxAppendMessageTaskræ   rý   r  r*  r/  rQ  rV  rR  r  r  rº  r©  rÖ  rØ  ræ  rý  r  r  r0  r1  r2  r5  r4  rQ  rV  rW  rµ  rç  rì  r  r  r  r”  r8  r  Ú
AliasGroupr#  r%  r*  rS  ri  rx  rz  r+   r+   r+   r/   Ú<module>   sä    h&0M
ÿS(X
s#NI%)D   2 d)  Q