Ë
    oji2  ã                   ó²   — d Z 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	m
Z
 ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ  G d„ de«      Zy)zi
Tests for L{twisted.mail.scripts.mailmail}, the implementation of the
command line program I{mailmail}.
é    N)ÚStringIO)ÚskipIf)Úversion)ÚDeferred)ÚMemoryReactor)Úsmtp)Úmailmail)ÚparseOptions)ÚFailure)ÚplatformType)ÚTestCasec                   óÀ   — e Zd ZdZ e«       Zd„ Zd„ Zd„ Zd„ Z	d„ Z
d„ Zd„ Zd	„ Zd
„ Zd„ Zd„ Zd„ Z eedk(  d«      d„ «       Z eedk(  d«      d„ «       Zd„ Zd„ Zd„ Zy)ÚOptionsTestsz¸
    Tests for L{parseOptions} which parses command line arguments and reads
    message text from stdin to produce an L{Options} instance which can be
    used to send a message.
    c                 óö   ‡ — t        «       ‰ _        ddlm}m}  |‰ j                  «      }‰ j                  t        d ||¬«      «       d‰ _        d‰ _        d‰ _	        ˆ fd„}‰ j                  t        d|«       y)zz
        Override some things in mailmail, so that we capture C{stdout},
        and do not call L{reactor.stop}.
        r   )ÚLoggerÚtextFileLogObserverÚ_log)ÚobserverNc                 ó²   •— | ‰_         |‰_        |‰_        t        j                  | |j
                  |j                  |j                  ‰j                  ¬«      S )N)Úreactor)	ÚhostÚoptionsÚidentr   ÚsendmailÚsenderÚtoÚbodyÚmemoryReactor)r   r   r   Úselfs      €úA/usr/lib/python3/dist-packages/twisted/mail/test/test_mailmail.pyr   z$OptionsTests.setUp.<locals>.sendmail3   sJ   ø€ ØˆDŒIØ"ˆDŒLØˆDŒJÜ—=‘=ØØ—‘Ø—
‘
Ø—‘Ø×*Ñ*ôð ó    r   )
r   ÚoutÚtwisted.loggerr   r   Úpatchr	   r   r   r   )r   r   r   ÚlogObserverr   s   `    r    ÚsetUpzOptionsTests.setUp#   s`   ø€ ô
 “:ˆŒç>á)¨$¯(©(Ó3ˆØ�
‰
”8˜V¡V°[Ô%AÔBØˆŒ	ØˆŒØˆŒ
ô
	ð 	�
‰
”8˜Z¨Õ2r!   c                 ó²   — | j                  t        dt        d«      «       | j                  t        t
        g «      }| j                  |j                  d«       y)zÒ
        If no recipients are given in the argument list and there is no
        recipient header in the message text, L{parseOptions} raises
        L{SystemExit} with a string describing the problem.
        ÚstdinzSubject: foo

Hello, goodbye.
)zNo recipients specified.N)r$   Úsysr   ÚassertRaisesÚ
SystemExitr
   ÚassertEqualÚargs©r   Úexcs     r    Útest_unspecifiedRecipientsz'OptionsTests.test_unspecifiedRecipientsA   sC   € ð 	�
‰
”3˜¤Ð*SÓ!TÔUØ×Ñ¤
¬L¸"Ó=ˆØ×Ñ˜Ÿ™Ð#@ÕAr!   c                 ót   — | j                  t        t        dg«      }| j                  |j                  d«       y)z™
        The I{-bp} option for listing queue information is unsupported and
        if it is passed to L{parseOptions}, L{SystemExit} is raised.
        z-bp©zUnsupported option.N©r*   r+   r
   r,   r-   r.   s     r    Útest_listQueueInformationz&OptionsTests.test_listQueueInformationK   s/   € ð
 ×Ñ¤
¬L¸5¸'ÓBˆØ×Ñ˜Ÿ™Ð#;Õ<r!   c                 ót   — | j                  t        t        dg«      }| j                  |j                  d«       y)z´
        The I{-bs} option for using stdin and stdout as the SMTP transport
        is unsupported and if it is passed to L{parseOptions}, L{SystemExit}
        is raised.
        z-bsr2   Nr3   r.   s     r    Útest_stdioTransportz OptionsTests.test_stdioTransportS   ó/   € ð ×Ñ¤
¬L¸5¸'ÓBˆØ×Ñ˜Ÿ™Ð#;Õ<r!   c                 óä   — | j                  t        t        dg«      }| j                  |j                  d«       | j                  t        t        dg«      }| j                  |j                  d«       y)z»
        The I{-i} and I{-oi} options for ignoring C{"."} by itself on a line
        are unsupported and if either is passed to L{parseOptions},
        L{SystemExit} is raised.
        z-ir2   z-oiNr3   r.   s     r    Útest_ignoreFullStopz OptionsTests.test_ignoreFullStop\   sZ   € ð ×Ñ¤
¬L¸4¸&ÓAˆØ×Ñ˜Ÿ™Ð#;Ô<Ø×Ñ¤
¬L¸5¸'ÓBˆØ×Ñ˜Ÿ™Ð#;Õ<r!   c                 ót   — | j                  t        t        dg«      }| j                  |j                  d«       y)z¿
        The I{-om} option for copying the sender if they appear in an alias
        expansion is unsupported and if it is passed to L{parseOptions},
        L{SystemExit} is raised.
        z-omr2   Nr3   r.   s     r    Útest_copyAliasedSenderz#OptionsTests.test_copyAliasedSenderg   r7   r!   c                 ó  — t        «       }| j                  t        d|«       | j                  t        t
        d«      }| j                  |j                  d«       |j                  «       }| j                  |dt        › d�«       y)z~
        The I{--version} option displays the version and raises
        L{SystemExit} with L{None} as the exit code.
        Ústdoutz	--versionNzmailmail version: ú
)
r   r$   r)   r*   r+   r
   r,   ÚcodeÚgetvaluer   )r   r"   ÚsystemExitCodeÚdatas       r    Útest_versionzOptionsTests.test_versionp   sj   € ô
 ‹jˆØ�
‰
”3˜ #Ô&Ø×*Ñ*¬:´|À[ÓQˆà×Ñ˜×,Ñ,¨dÔ3Ø�|‰|‹~ˆØ×Ñ˜Ð!3´G°9¸BÐ?Õ@r!   c                 ó”   — t        d«      }| j                  t        d|«       t        d«      }| j	                  |j
                  «       y)zA
        The I{-odb} flag specifies background delivery.
        r>   r(   z-odbN)r   r$   r)   r
   Ú
assertTrueÚ
background©r   r(   Úos      r    Útest_backgroundDeliveryz$OptionsTests.test_backgroundDelivery}   s7   € ô ˜“ˆØ�
‰
”3˜ Ô'Ü˜Ó ˆØ�‰˜Ÿ™Õ%r!   c                 ó”   — t        d«      }| j                  t        d|«       t        d«      }| j	                  |j
                  «       y)zB
        The I{-odf} flags specifies foreground delivery.
        r>   r(   z-odfN)r   r$   r)   r
   ÚassertFalserF   rG   s      r    Útest_foregroundDeliveryz$OptionsTests.test_foregroundDelivery†   s9   € ô ˜“ˆØ�
‰
”3˜ Ô'Ü˜Ó ˆØ×Ñ˜Ÿ™Õ&r!   c                 ó¨   — t        d«      }| j                  t        d|«       t        d«      }| j	                  t        |j                  «      d«       y)zd
        The I{-t} flags specifies that recipients should be obtained
        from headers.
        zTo: Curly <invaliduser2@example.com>
Cc: Larry <invaliduser1@example.com>
Bcc: Moe <invaliduser3@example.com>

Oh, a wise guy?
r(   ú-té   N)r   r$   r)   r
   r,   Úlenr   rG   s      r    Útest_recipientsFromHeadersz'OptionsTests.test_recipientsFromHeaders�   sG   € ô
 ð ó
ˆð 	�
‰
”3˜ Ô'Ü˜ÓˆØ×Ñœ˜QŸT™T› AÕ&r!   c                 óš   — t        d«      }| j                  t        d|«       t        g d¢«      }| j	                  |j
                  d«       y)zw
        When a message has no I{From:} header, a I{From:} value can be
        specified with the I{-F} flag.
        z3To: invaliduser2@example.com
Subject: A wise guy?

r(   )ú-Fú Larry <invaliduser1@example.com>rN   rT   N©r   r$   r)   r
   r,   r   rG   s      r    Útest_setFromzOptionsTests.test_setFromŸ   s?   € ô
 ÐTÓUˆØ�
‰
”3˜ Ô'ÜÒIÓJˆØ×Ñ˜Ÿ™Ð#EÕFr!   c                 óš   — t        d«      }| j                  t        d|«       t        g d¢«      }| j	                  |j
                  d«       y)z‘
        The I{-F} flag specifies the From: value.  However, I{-F} flag is
        overriden by the value of From: in the e-mail header.
        zLTo: Curly <invaliduser4@example.com>
From: Shemp <invaliduser4@example.com>
r(   )rS   z"Groucho <invaliduser5@example.com>rN   zinvaliduser4@example.comNrU   rG   s      r    Ú!test_overrideFromFlagByFromHeaderz.OptionsTests.test_overrideFromFlagByFromHeader©   sE   € ô
 ð7ó
ˆð 	�
‰
”3˜ Ô'ÜÒKÓLˆØ×Ñ˜Ÿ™Ð#=Õ>r!   Úwin32zImailmail.run() does not work on win32 due to lack of support for getuid()c                 óè  — d}t        d«      }| j                  t        d|«       | j                  t        d|«       t        j                  «        | j                  | j                  j                  t        j                  «       «       | j                  | j                  j                  dg«       | j                  | j                  j                  «       | j                  t        j                  «       y)z½
        Call L{mailmail.run}, and specify I{-oep} to print errors
        to stderr.  The sender, to, and printErrors options should be
        set and there should be no failure.
        ©ztest_mailmail.pyúinvaliduser2@example.comz-oepr>   Úargvr(   r\   N)r   r$   r)   r	   Úrunr,   r   r   Úgetloginr   rE   ÚprintErrorsÚassertIsNoneÚfailed)r   r]   r(   s      r    Útest_runErrorsToStderrz#OptionsTests.test_runErrorsToStderr¶   s£   € ð HˆÜ˜“ˆØ�
‰
”3˜ Ô%Ø�
‰
”3˜ Ô'Ü�‰ŒØ×Ñ˜Ÿ™×,Ñ,¬h×.?Ñ.?Ó.AÔBØ×Ñ˜Ÿ™Ÿ™Ð+EÐ*FÔGà�‰˜Ÿ™×0Ñ0Ô1à×Ñœ(Ÿ/™/Õ*r!   c                 óÀ  — t        d«      }| j                  t        d|«       | j                  «       }t	        j
                  «       }t	        j                  «       }t        |d«      5 }|j                  dj                  ||«      «       ddd«       | j                  t        d|«       d}| j                  t        d|«       t        j                  «        | j                  | j                  j                  «       d	«       | j                  | j                  j                  «       d
«       | j                  | j                  j                  «       d«       y# 1 sw Y   ŒÌxY w)z…
        Error messages for illegal UID value, illegal GID value, and illegal
        identity entry will be sent to stderr.
        r>   r(   Úwa  [useraccess]
allow=invaliduser2,invaliduser1
deny=invaliduser3,invaliduser4,{}
order=allow,deny
[groupaccess]
allow=invalidgid1,invalidgid2
deny=invalidgid1,invalidgid2,{}
order=deny,allow
[identity]
localhost=funny
[addresses]
smarthost=localhost
default_domain=example.com
NÚ	LOCAL_CFGr[   r]   z3Illegal UID in \[useraccess\] section: invaliduser1z3Illegal GID in \[groupaccess\] section: invalidgid1z,Illegal entry in \[identity\] section: funny)r   r$   r)   ÚmktempÚosÚgetuidÚgetgidÚopenÚwriteÚformatr	   r^   ÚassertRegexr"   r@   )r   r(   ÚfilenameÚmyUidÚmyGidÚfr]   s          r    Útest_readInvalidConfigz#OptionsTests.test_readInvalidConfigÌ   s  € ô ˜“ˆØ�
‰
”3˜ Ô'à—;‘;“=ˆÜ—	‘	“ˆÜ—	‘	“ˆä�(˜CÓ ð 	 Aà�G‰Gð/÷ 06©v°e¸UÓ/Cô÷	ð2 	�
‰
”8˜[¨(Ô3àGˆØ�
‰
”3˜ Ô%Ü�‰ŒØ×ÑØ�H‰H×ÑÓØFô	
ð 	×ÑØ�H‰H×ÑÓØFô	
ð 	×ÑØ�H‰H×ÑÓÐ!Qõ	
÷M	ð 	ús   Á'"EÅEc                 ó¢   — ddl m} | j                  «       }t        |d«      5 }|j	                  |«       ddd«        ||«      S # 1 sw Y   ŒxY w)aª  
        Read a mailmail configuration file.

        The mailmail script checks the twisted.mail.scripts.mailmail.GLOBAL_CFG
        variable and then the twisted.mail.scripts.mailmail.LOCAL_CFG
        variable for the path to its  config file.

        @param config: path to config file
        @type config: L{str}

        @return: A parsed config.
        @rtype: L{twisted.mail.scripts.mailmail.Configuration}
        r   )Ú
loadConfigre   N)Útwisted.mail.scripts.mailmailru   rg   rk   rl   )r   Úconfigru   ro   rr   s        r    ÚgetConfigFromFilezOptionsTests.getConfigFromFile  sK   € õ 	=à—;‘;“=ˆä�(˜CÓ ð 	 AØ�G‰G�FŒO÷	ñ ˜(Ó#Ð#÷	ð 	ús   £AÁAc                 óŒ  — | j                  d«      }| j                  |j                  d«       | j                  d«      }| j                  |j                  d«       | j                  d«      }| j                  |j                  d«       | j                  |j                  d«       | j                  d«      }| j	                  d|j
                  «       | j                  |j
                  d   d	d
g«       | j                  d«      }| j                  |j                  dg«       | j                  d«      }| j                  |j                  ddg«       | j                  d«      }| j                  |j                  ddg«       | j                  |j                  ddg«       | j                  d«      }| j                  |j                  dg«       | j                  d«      }| j                  |j                  dg«       | j                  d«      }| j                  |j                  ddg«       | j                  |j                  ddg«       y)zj
        L{twisted.mail.scripts.mailmail.loadConfig}
        parses the config file for mailmail.
        z 
[addresses]
smarthost=localhostÚ	localhostz'
[addresses]
default_domain=example.comzexample.comz;
[addresses]
smarthost=localhost
default_domain=example.comz1
[identity]
host1=invalid
host2=username:passwordÚhost1Úhost2ÚusernameÚpasswordz+
[useraccess]
allow=invalid1,35
order=allowé#   z#
[useraccess]
deny=35,36
order=denyé$   z/
[useraccess]
allow=35,36
deny=37,38
order=denyé%   é&   z(
[groupaccess]
allow=gid1,41
order=allowé)   z!
[groupaccess]
deny=41
order=denyz6
[groupaccess]
allow=41,42
deny=43,44
order=allow,denyé*   é+   é,   N)
rx   r,   Ú	smarthostÚdomainÚassertNotInÚ
identitiesÚ	allowUIDsÚdenyUIDsÚ	allowGIDsÚdenyGIDs)r   rw   s     r    Útest_loadConfigzOptionsTests.test_loadConfig  s6  € ð
 ×'Ñ'ðó
ˆð
 	×Ñ˜×)Ñ)¨;Ô7à×'Ñ'ðó
ˆð
 	×Ñ˜Ÿ™¨Ô6à×'Ñ'ðó
ˆð 	×Ñ˜×)Ñ)¨;Ô7Ø×Ñ˜Ÿ™¨Ô6à×'Ñ'ðó
ˆð 	×Ñ˜ &×"3Ñ"3Ô4Ø×Ñ˜×*Ñ*¨7Ñ3°jÀ*Ð5MÔNà×'Ñ'ðó
ˆð 	×Ñ˜×)Ñ)¨B¨4Ô0à×'Ñ'ðó
ˆð 	×Ñ˜Ÿ™¨2¨r¨(Ô3à×'Ñ'ðó
ˆð 	×Ñ˜×)Ñ)¨B°¨8Ô4Ø×Ñ˜Ÿ™¨2¨r¨(Ô3à×'Ñ'ðó
ˆð 	×Ñ˜×)Ñ)¨B¨4Ô0à×'Ñ'ðó
ˆð 	×Ñ˜Ÿ™¨2¨$Ô/à×'Ñ'ðó
ˆð 	×Ñ˜×)Ñ)¨B°¨8Ô4Ø×Ñ˜Ÿ™¨2¨r¨(Õ3r!   c                 óÆ   ‡ — ˆ fd„}‰ j                  t        d|«       t        j                  «       }d|_        t        t        «       «      }t        j                  ||«       y)zš
        L{twisted.mail.scripts.mailmail.senderror} sends mail back to the
        sender if an error occurs while sending mail to the recipient.
        c                 ó¢   •— ‰j                  |d«       ‰j                  |dg«       ‰j                  |j                  «       d«       t        «       S )Nzpostmaster@Ú
testsenderÚ
ValueError)rn   r,   r@   r   )r   r   Ú	recipientr   r   s       €r    r   z-OptionsTests.test_senderror.<locals>.sendmail}  sC   ø€ Ø×Ñ˜V ]Ô3Ø×Ñ˜Y¨¨Ô7Ø×Ñ˜TŸ]™]›_¨lÔ;Ü“:Ðr!   r   r’   N)r$   r   r	   ÚOptionsr   r   r“   Ú	senderror)r   r   ÚoptsÚfails   `   r    Útest_senderrorzOptionsTests.test_senderrorw  sK   ø€ ô	ð 	�
‰
”4˜ XÔ.Ü×ÑÓ!ˆØ"ˆŒÜ”z“|Ó$ˆÜ×Ñ˜4 Õ&r!   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r&   r0   r4   r6   r9   r;   rC   rI   rL   rQ   rV   rX   r   r   rc   rs   rx   r�   r™   © r!   r    r   r      s©   „ ññ "“O€Mò3ò<Bò=ò=ò	=ò=òAò&ò'ò'ò Gò?ñ Ø˜ÑØVóñ+ó	ð+ñ$ Ø˜ÑØVóñ4
ó	ð4
òl$ò0W4ór'r!   r   )r�   rh   r)   Úior   Úunittestr   Útwisted.copyrightr   Útwisted.internet.deferr   Útwisted.internet.testingr   Útwisted.mailr   Útwisted.mail.scriptsr	   rv   r
   Útwisted.python.failurer   Útwisted.python.runtimer   Útwisted.trial.unittestr   r   rž   r!   r    ú<module>r©      sA   ðñ
ó
 
Û 
Ý Ý å %Ý +Ý 2Ý Ý )Ý 6Ý *Ý /Ý +ôm'�8õ m'r!   