
    Yfi2                         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dS )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dS )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                  }                     t          d ||                     d _        d _        d _	         fd}                     t          d|           dS )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      Q/var/www/html/env/lib/python3.11/site-packages/twisted/mail/test/test_mailmail.pyr   z$OptionsTests.setUp.<locals>.sendmail3   sI    DI"DLDJ=
*       r   )
r   outtwisted.loggerr   r   patchr	   r   r   r   )r   r   r   logObserverr   s   `    r    setUpzOptionsTests.setUp#   s    
 ::>>>>>>>>))$(33

8VVV[%A%A%ABBB	

	 
	 
	 
	 
	 	

8Z22222r!   c                     |                      t          dt          d                     |                     t          t
          g           }|                     |j        d           dS )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   sY     	

3*S!T!TUUU
L"==#@AAAAAr!   c                     |                      t          t          dg          }|                     |j        d           dS )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<    
 
L5'BB#;<<<<<r!   c                     |                      t          t          dg          }|                     |j        d           dS )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   <     
L5'BB#;<<<<<r!   c                     |                      t          t          dg          }|                     |j        d           |                      t          t          dg          }|                     |j        d           dS )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\   sn     
L4&AA#;<<<
L5'BB#;<<<<<r!   c                     |                      t          t          dg          }|                     |j        d           dS )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                 8   t                      }|                     t          d|           |                     t          t
          d          }|                     |j        d           |                                }|                     |dt           d           dS )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   s    
 jj

3#&&&**:|[QQ,d333||~~?G???@@@@@r!   c                     t          d          }|                     t          d|           t          d          }|                     |j                   dS )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}   sL     

3'''  %%%%%r!   c                     t          d          }|                     t          d|           t          d          }|                     |j                   dS )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   sN     

3'''  &&&&&r!   c                     t          d          }|                     t          d|           t          d          }|                     t          |j                  d           dS )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   sa    
  
 
 	

3'''QTA&&&&&r!   c                     t          d          }|                     t          d|           t          g d          }|                     |j        d           dS )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U

3'''IIIJJ#EFFFFFr!   c                     t          d          }|                     t          d|           t          g d          }|                     |j        d           dS )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   sc    
 7
 
 	

3'''KKKLL#=>>>>>r!   win32zImailmail.run() does not work on win32 due to lack of support for getuid()c                    d}t          d          }|                     t          d|           |                     t          d|           t          j                     |                     | j        j        t          j                               |                     | j        j	        dg           | 
                    | j        j                   |                     t          j                   dS )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.ABBB+E*FGGG0111(/*****r!   c                    t          d          }|                     t          d|           |                                 }t	          j                    }t	          j                    }t          |d          5 }|                    d	                    ||                     ddd           n# 1 swxY w Y   |                     t          d|           d}|                     t          d|           t          j                     |                     | j                                        d	           |                     | j                                        d
           |                     | j                                        d           dS )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GG/ 06veU/C/C  	 	 	 	 	 	 	 	 	 	 	 	 	 	 	2 	

8[(333G

3%%%HF	
 	
 	
 	HF	
 	
 	
 	H!Q	
 	
 	
 	
 	
s   6*B,,B03B0c                     ddl m} |                                 }t          |d          5 }|                    |           ddd           n# 1 swxY w Y    ||          S )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  s     	=<<<<<;;==(C   	AGGFOOO	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 z(###s   AAAc                    |                      d          }|                     |j        d           |                      d          }|                     |j        d           |                      d          }|                     |j        d           |                     |j        d           |                      d          }|                     d|j                   |                     |j        d         d	d
g           |                      d          }|                     |j        dg           |                      d          }|                     |j        ddg           |                      d          }|                     |j        ddg           |                     |j        ddg           |                      d          }|                     |j        dg           |                      d          }|                     |j	        dg           |                      d          }|                     |j        ddg           |                     |j	        ddg           dS )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  s   
 ''
 

 	);777''
 

 	666''
 
 	);777666''
 
 	&"3444*73j*5MNNN''
 
 	)B4000''
 
 	2r(333''
 
 	)B84442r(333''
 
 	)B4000''
 
 	2$///''
 
 	)B84442r(33333r!   c                       fd}                      t          d|           t          j                    }d|_        t          t                                }t          j        ||           dS )z
        L{twisted.mail.scripts.mailmail.senderror} sends mail back to the
        sender if an error occurs while sending mail to the recipient.
        c                                          |d                               |dg                                |                                d           t                      S )Nzpostmaster@
testsender
ValueError)rn   r,   r@   r   )r   r   	recipientr   r   s       r    r   z-OptionsTests.test_senderror.<locals>.sendmail}  sY    V]333Y777T]]__l;;;::r!   r   r   N)r$   r   r	   Optionsr   r   r   	senderror)r   r   optsfails   `   r    test_senderrorzOptionsTests.test_senderrorw  ss    	 	 	 	 	 	

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         "MOOM3 3 3<B B B= = == = =	= 	= 	== = =A A A& & &' ' '' ' ' G G G? ? ? VV + +	 +$ VV 4
 4
	 4
l$ $ $0W4 W4 W4r' ' ' ' '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      s'  
 
 
			 



             % % % % % % + + + + + + 2 2 2 2 2 2       ) ) ) ) ) ) 6 6 6 6 6 6 * * * * * * / / / / / / + + + + + +m' m' m' m' m'8 m' m' m' m' m'r!   