U
    
W[l                     @   s  d Z ddlmZmZ ddlZddlZddlZddlZddl	Z	zddl
Z
ddlZW n ek
rl   d Z
ZY nX 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! eej"# Z$G dd dej%Z&G dd dej%Z'G dd dej%Z(G dd dej%Z)G dd dej%Z*G dd dej%Z+G dd dej%Z,G dd deZ-G dd dej%Z.G d d! d!ej%Z/G d"d# d#ej%Z0G d$d% d%ej1Z2G d&d' d'ej1Z3G d(d) d)e2Z4G d*d+ d+e5Z6G d,d- d-e5Z7G d.d/ d/ej%Z8G d0d1 d1ej%Z9G d2d3 d3ej%Z:G d4d5 d5ej%Z;G d6d7 d7ej%Z<G d8d9 d9ej%Z=G d:d; d;ej%Z>G d<d= d=ej%Z?dS )>z#
Tests for L{twisted.python.util}.
    )divisionabsolute_importN)unittest)suppress)util)FilePath)reactor)IReactorProcess)ProcessProtocol)Deferred)ProcessDone)MockOSc                   @   sX   e Zd Zdd Zdd Zdd Zdd Zed	kr6d
e_dd Z	dd Z
ed	krTde
_d	S )	UtilTestsc                 C   s>   dddddddddddg}|  t|dddddddg d S )Na   ab            )assertEqualr   Zuniquify)selfl r   ?/usr/lib/python3/dist-packages/twisted/python/test/test_util.pytestUniq(   s    zUtilTests.testUniqc                 C   sd   |  tttdd | tttdd ztttdd W n tk
rT   Y nX tdd S )Nr   r   z,util.raises didn't raise when it should have)	
assertTruer   ZraisesZeroDivisionErrordivmodassertFalse	TypeErrorr   ZFailTestr   r   r   r   
testRaises,   s    zUtilTests.testRaisesc                 C   s   |  tdd dS )
        When L{uidFromString} is called with a base-ten string representation
        of an integer, it returns the integer.
        100d   N)r   r   uidFromStringr!   r   r   r   test_uidFromNumericString8   s    z#UtilTests.test_uidFromNumericStringc                 C   s(   t t }| t|j|j dS )r#   N)	pwdgetpwuidosgetuidr   r   r&   pw_namepw_uid)r   pwentr   r   r   test_uidFromUsernameString@   s    z$UtilTests.test_uidFromUsernameStringNz0Username/UID conversion requires the pwd module.c                 C   s   |  tdd dS )
        When L{gidFromString} is called with a base-ten string representation
        of an integer, it returns the integer.
        r$   r%   N)r   r   gidFromStringr!   r   r   r   test_gidFromNumericStringL   s    z#UtilTests.test_gidFromNumericStringc                 C   s(   t t }| t|j|j dS )r0   N)	grpZgetgrgidr*   getgidr   r   r1   Zgr_nameZgr_gid)r   Zgrentr   r   r   test_gidFromGroupnameStringT   s    z%UtilTests.test_gidFromGroupnameStringz2Group Name/GID conversion requires the grp module.)__name__
__module____qualname__r   r"   r'   r/   r(   skipr2   r5   r3   r   r   r   r   r   &   s   r   c                   @   s   e Zd ZdZdd ZdS )NameToLabelTestsz#
    Tests for L{nameToLabel}.
    c              	   C   sB   dddddg}|D ]*\}}t |}| ||d|||f  qdS )zK
        Test the various kinds of inputs L{nameToLabel} supports.
        )fF)ZfoZFo)fooFoo)ZfooBarzFoo Bar)Z	fooBarBazzFoo Bar BazznameToLabel(%r) == %r != %rN)r   ZnameToLabelr   )r   ZnameDataZinpoutZgotr   r   r   test_nameToLabelf   s    
 z!NameToLabelTests.test_nameToLabelN)r6   r7   r8   __doc__r@   r   r   r   r   r:   a   s   r:   c                   @   s   e Zd ZdZdd ZdS )UntilConcludesTestsz:
    Tests for L{untilConcludes}, an C{EINTR} helper.
    c                    sx    fdd}dg _ d _ t|ddd   jd dttg _ d _ t|ddd   jd dS )	a  
        L{untilConcludes} calls the function passed to it until the function
        does not raise either L{OSError} or L{IOError} with C{errno} of
        C{EINTR}.  It otherwise completes with the same result as the function
        passed to it.
        c                    s4     j d7  _  j }|d k	r,|tjd| | S )Nr   zInterrupted system call!)calls
exceptionspoperrnoZEINTR)r   bexcr!   r   r   r;      s
    
z3UntilConcludesTests.test_uninterruptably.<locals>.fNr   r   r   r      )rD   rC   r   r   ZuntilConcludesOSErrorIOError)r   r;   r   r!   r   test_uninterruptably}   s    z(UntilConcludesTests.test_uninterruptablyN)r6   r7   r8   rA   rL   r   r   r   r   rB   y   s   rB   c                   @   sT   e Zd ZdZeedddkr dZdd Zdd Zd	d
 Z	dd Z
dd Zdd ZdS )SwitchUIDTestsz&
    Tests for L{util.switchUID}.
    r+   Nzgetuid/setuid not availablec                 C   s2   t  | _| td| j | td| j g | _d S )Nr*   
initgroups)r   mockospatchr   rN   initgroupsCallsr!   r   r   r   setUp   s    zSwitchUIDTests.setUpc                 C   s   | j ||f dS )zK
        Save L{util.initgroups} calls in C{self.initgroupsCalls}.
        N)rQ   append)r   uidgidr   r   r   rN      s    zSwitchUIDTests.initgroupsc                 C   s2   t dd | | jdg | | jjdg dS )zn
        L{util.switchUID} calls L{util.initgroups} and then C{os.setuid} with
        the given uid.
        .  NrV   N)setuidrV   )r   	switchUIDr   rQ   rO   actionsr!   r   r   r   test_uid   s    zSwitchUIDTests.test_uidc                 C   s4   t ddd | | jdg | | jjdg dS )z
        L{util.switchUID} calls L{util.initgroups} and then C{os.seteuid} with
        the given uid if the C{euid} parameter is set to C{True}.
        rV   NTrW   )r   rY   r   rQ   rO   seteuidCallsr!   r   r   r   	test_euid   s    zSwitchUIDTests.test_euidc                 C   s   | j  }t|d | | jg  | | j jg  | tjg}| t|d | 	d| |d d  | 	d| |d d  dS )z
        If the current uid is the same as the uid passed to L{util.switchUID},
        then initgroups does not get called, but a warning is issued.
        Nr   z&tried to drop privileges and setuid %ir   messagezbut uid is already %i)
rO   r+   r   rY   r   rQ   rZ   flushWarningslenassertIn)r   rT   currentWarningsr   r   r   test_currentUID   s    


 
zSwitchUIDTests.test_currentUIDc                 C   s   | j  }t|dd | | jg  | | j jg  | tjg}| t|d | 	d| |d d  | 	d| |d d  dS )z
        If the current euid is the same as the euid passed to L{util.switchUID},
        then initgroups does not get called, but a warning is issued.
        NTr   z'tried to drop privileges and seteuid %ir   r^   zbut euid is already %i)
rO   geteuidr   rY   r   rQ   r\   r_   r`   ra   )r   euidrb   r   r   r   test_currentEUID   s    


 
zSwitchUIDTests.test_currentEUID)r6   r7   r8   rA   getattrr*   r9   rR   rN   r[   r]   rc   rf   r   r   r   r   rM      s   

rM   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 )MergeFunctionMetadataTestsz-
    Tests for L{mergeFunctionMetadata}.
    c                    sN   t  t   fdd}d fdd	}t||}| |ddddd	  d
S )zt
        After merging C{foo}'s data into C{bar}, the returned function behaves
        as if it is C{bar}.
        c                      s    S Nr   r   )
foo_objectr   r   r=      s    zQMergeFunctionMetadataTests.test_mergedFunctionBehavesLikeMergeTarget.<locals>.foo
   c                    s   |\}} S ri   r   )xyr   cder   rG   )
bar_objectr   r   bar   s    zQMergeFunctionMetadataTests.test_mergedFunctionBehavesLikeMergeTarget.<locals>.barr   r   )r   r   )ZquuxN)rk   )objectr   mergeFunctionMetadataZassertIsr   r=   rr   bazr   )rq   rj   r   )test_mergedFunctionBehavesLikeMergeTarget   s    zDMergeFunctionMetadataTests.test_mergedFunctionBehavesLikeMergeTargetc                 C   s6   dd }dd }d|_ t||}| |j |j  dS )zd
        Merging C{foo} into C{bar} returns a function with C{foo}'s
        C{__module__}.
        c                   S   s   d S ri   r   r   r   r   r   r=     s    z;MergeFunctionMetadataTests.test_moduleIsMerged.<locals>.fooc                   S   s   d S ri   r   r   r   r   r   rr     s    z;MergeFunctionMetadataTests.test_moduleIsMerged.<locals>.barzsomewhere.elseN)r7   r   rt   r   ru   r   r   r   test_moduleIsMerged   s
    z.MergeFunctionMetadataTests.test_moduleIsMergedc                 C   s0   dd }dd }t ||}| |j|j dS )zX
        Merging C{foo} into C{bar} returns a function with C{foo}'s docstring.
        c                   S   s   dS )z&
            This is foo.
            Nr   r   r   r   r   r=     s    z>MergeFunctionMetadataTests.test_docstringIsMerged.<locals>.fooc                   S   s   dS )z&
            This is bar.
            Nr   r   r   r   r   rr     s    z>MergeFunctionMetadataTests.test_docstringIsMerged.<locals>.barN)r   rt   r   rA   ru   r   r   r   test_docstringIsMerged  s    z1MergeFunctionMetadataTests.test_docstringIsMergedc                 C   s0   dd }dd }t ||}| |j|j dS )zS
        Merging C{foo} into C{bar} returns a function with C{foo}'s name.
        c                   S   s   d S ri   r   r   r   r   r   r=   %  s    z9MergeFunctionMetadataTests.test_nameIsMerged.<locals>.fooc                   S   s   d S ri   r   r   r   r   r   rr   (  s    z9MergeFunctionMetadataTests.test_nameIsMerged.<locals>.barN)r   rt   r   r6   ru   r   r   r   test_nameIsMerged   s    z,MergeFunctionMetadataTests.test_nameIsMergedc                 C   sh   dd }d|_ d|_dd }d|_d|_t||}| |j |j  | |j|j | |j|j d	S )
zv
        Merging C{foo} into C{bar} returns a function with C{bar}'s
        dictionary, updated by C{foo}'s.
        c                   S   s   d S ri   r   r   r   r   r   r=   5  s    zGMergeFunctionMetadataTests.test_instanceDictionaryIsMerged.<locals>.foor   r   c                   S   s   d S ri   r   r   r   r   r   rr   :  s    zGMergeFunctionMetadataTests.test_instanceDictionaryIsMerged.<locals>.barr   r   N)r   rG   rn   r   rt   r   ru   r   r   r   test_instanceDictionaryIsMerged/  s    z:MergeFunctionMetadataTests.test_instanceDictionaryIsMergedN)	r6   r7   r8   rA   rw   rx   ry   rz   r{   r   r   r   r   rh      s   rh   c                   @   s   e Zd ZdZdd ZdS )OrderedDictTestsz(
    Tests for L{util.OrderedDict}.
    c                 C   s\   ddl m} | | j| jgd}| |d d d | |d d t | t|d dS )	z4
        L{util.OrderedDict} is deprecated.
        r   )OrderedDictZoffendingFunctionsr^   zftwisted.python.util.OrderedDict was deprecated in Twisted 15.5.0: Use collections.OrderedDict instead.categoryr   N)Ztwisted.python.utilr}   r_   test_deprecatedr   DeprecationWarningr`   )r   r}   rb   r   r   r   r   J  s    
z OrderedDictTests.test_deprecatedN)r6   r7   r8   rA   r   r   r   r   r   r|   F  s   r|   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )InsensitiveDictTestsz,
    Tests for L{util.InsensitiveDict}.
    c                 C   s  t jddddiddd}| |d ddi | |d d | | | | |d |d | d| | d| tt||t jd	}| || dddg}|D ],}| ||  | ||| f|	  q| t
|t
| |d= |d= | | dg d
S )zr
        L{util.InsensitiveDict} preserves the case of keys if constructed with
        C{preserve=True}.
        rr   r   r   r>   r   fnzZpreserver   r=   r>   )dctInsensitiveDictN)r   r   r   copygetra   evalreprkeysitemsr`   )r   r   resultr   rl   r   r   r   test_preservea  s(    
z"InsensitiveDictTests.test_preservec                 C   s   t jddddiddd}dddg}|D ],}| ||  | ||| f|  q(| t|t| |d= |d= | | dg d	S )
z|
        L{util.InsensitiveDict} does not preserves the case of keys if
        constructed with C{preserve=False}.
        rr   r   r   r   r   r   r=   r   N)r   r   ra   r   r   r   r`   )r   r   r   rl   r   r   r   test_noPreserve~  s    
z$InsensitiveDictTests.test_noPreservec                 C   s:   t jdd}d|d< | |d d | | dg dS )z4
        Unicode keys are case insensitive.
        Fr   r   r>   ZFOOr=   Nr   r   r   r   r   ro   r   r   r   test_unicode  s    z!InsensitiveDictTests.test_unicodec                 C   s:   t jdd}d|d< | |d d | | dg dS )z2
        Bytes keys are case insensitive.
        Fr   r   s   Foos   FOOs   fooNr   r   r   r   r   
test_bytes  s    zInsensitiveDictTests.test_bytesN)r6   r7   r8   rA   r   r   r   r   r   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 )	PasswordTestingProcessProtocolz
    Write the string C{"secret
"} to a subprocess and then collect all of
    its output and fire a Deferred with it when the process ends.
    c                 C   s   g | _ | jd d S )Ns   secret
)outputZ	transportwriter!   r   r   r   connectionMade  s    z-PasswordTestingProcessProtocol.connectionMadec                 C   s   | j ||f d S ri   )r   rS   )r   fdr   r   r   r   childDataReceived  s    z0PasswordTestingProcessProtocol.childDataReceivedc                 C   s   | j || jf d S ri   )finishedcallbackr   )r   reasonr   r   r   processEnded  s    z+PasswordTestingProcessProtocol.processEndedN)r6   r7   r8   rA   r   r   r   r   r   r   r   r     s   r   c                   @   s"   e Zd ZeesdZdd ZdS )GetPasswordTestsz,Process support required to test getPasswordc                    sR   t  }t |_tj|ttddgdtjt	j
did  fdd}|j|S )a.  
        Making sure getPassword accepts a password from standard input by
        running a child process which uses getPassword to read in a string
        which it then writes it out again.  Write a string to the child
        process and then read one and make sure it is the right string.
        s   -csj   import sys
from twisted.python.util import getPassword
sys.stdout.write(getPassword())
sys.stdout.flush()
s
   PYTHONPATHutf8)envc                    s"   | \}}| t  d| d S )N)r   s   secret)Ztrapr   ra   )r   r   r   r!   r   r   processFinished  s    
z4GetPasswordTests.test_stdin.<locals>.processFinished)r   r   r   r   ZspawnProcesspyExer*   pathsepjoinsyspathencodeZaddCallback)r   pr   r   r!   r   
test_stdin  s     
zGetPasswordTests.test_stdinN)r6   r7   r8   r	   Z
providedByr   r9   r   r   r   r   r   r     s   
r   c                   @   s   e Zd Zdd ZdS )SearchUpwardsTestsc                 C   s   t d tdd  tdd  tdd  t d t d t d t d	 tjdd
gddgd}t jdt j	 }| 
|| td tjdd
gddgd}d }| 
|| d S )Nzsearchupwards/a/b/czsearchupwards/foo.txtwzsearchupwards/a/foo.txtzsearchupwards/a/b/c/foo.txtzsearchupwards/barzsearchupwards/bamzsearchupwards/a/barzsearchupwards/a/b/bamzfoo.txtrr   Zbam)filesdirssearchupwards)r*   makedirsopenclosemkdirr   r   r   abspathsepr   shutilZrmtree)r   ZactualZexpectedr   r   r   testSearchupwards  s*    





z$SearchUpwardsTests.testSearchupwardsN)r6   r7   r8   r   r   r   r   r   r     s   r   c                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )IntervalDifferentialTestsc                 C   s2   t tg d}tdD ]}| t|d qd S )Nrk   r%   rk   Niterr   IntervalDifferentialranger   nextr   ro   ir   r   r   testDefault  s    z%IntervalDifferentialTests.testDefaultc                 C   s4   t tdgd}tdD ]}| t|d qd S )NrI   rk   r%   rI   r   r   r   r   r   r   
testSingle  s    z$IntervalDifferentialTests.testSinglec                 C   s   t tddgd}tdD ]}| t|d | t|d | t|d | t|d | t|d	 | t|d | t|d
 | t|d | t|d | t|d | t|d | t|d qd S )NrI      rk   r%   r   r   r   r   r   )r   r   r   r   )r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   testPair  s    z"IntervalDifferentialTests.testPairc                 C   sZ  t tdddgd}tdD ]4}| t|d | t|d | t|d | t|d | t|d	 | t|d | t|d | t|d | t|d
 | t|d | t|d | t|d | t|d | t|d	 | t|d | t|d | t|d | t|d | t|d
 qd S )Nr   r   rI   rk   r%   r   r   )r   r   r   )r   r   r   r   r   r   r   
testTriple  s*    z$IntervalDifferentialTests.testTriplec                 C   s   t tg d}| t|d |d | t|d | t|d |d | t|d | t|d | t|d | t|d | t|d | t|d d S )Nrk   r   r   r   r   r   )r   r   r   r   r   ZaddIntervalr   r   r   r   
testInsert  s    

z$IntervalDifferentialTests.testInsertc                 C   s   t tddgd}| t|d | t|d | t|d |d | t|d | t|d |d | t|d	 | t|jd d S )
Nr   rI   rk   r   r   r   r   r   r   )r   r   r   r   r   ZremoveIntervalassertRaises
ValueErrorr   r   r   r   
testRemove+  s    

z$IntervalDifferentialTests.testRemoveN)	r6   r7   r8   r   r   r   r   r   r   r   r   r   r   r     s   r   c                   @   s   e Zd ZdZdZdd ZdS )Recordz8
    Trivial user of L{FancyEqMixin} used by tests.
    r   rG   c                 C   s   || _ || _d S ri   r   r   r   rG   r   r   r   __init__?  s    zRecord.__init__Nr6   r7   r8   rA   ZcompareAttributesr   r   r   r   r   r   9  s   r   c                   @   s   e Zd ZdZdZdd ZdS )DifferentRecordzL
    Trivial user of L{FancyEqMixin} which is not related to L{Record}.
    r   c                 C   s   || _ || _d S ri   r   r   r   r   r   r   K  s    zDifferentRecord.__init__Nr   r   r   r   r   r   E  s   r   c                   @   s   e Zd ZdZdS )DerivedRecordz@
    A class with an inheritance relationship to L{Record}.
    N)r6   r7   r8   rA   r   r   r   r   r   Q  s   r   c                   @   s    e Zd ZdZdd Zdd ZdS )EqualToEverythingzQ
    A class the instances of which consider themselves equal to everything.
    c                 C   s   dS NTr   r   otherr   r   r   __eq__\  s    zEqualToEverything.__eq__c                 C   s   dS NFr   r   r   r   r   __ne__`  s    zEqualToEverything.__ne__Nr6   r7   r8   rA   r   r   r   r   r   r   r   X  s   r   c                   @   s    e Zd ZdZdd Zdd ZdS )EqualToNothingzN
    A class the instances of which consider themselves equal to nothing.
    c                 C   s   dS r   r   r   r   r   r   r   i  s    zEqualToNothing.__eq__c                 C   s   dS r   r   r   r   r   r   r   m  s    zEqualToNothing.__ne__Nr   r   r   r   r   r   e  s   r   c                   @   sX   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S )EqualityTestsz$
    Tests for L{FancyEqMixin}.
    c                 C   s\   G dd dt j}| | | k | | | k | }| ||k | ||k dS )z
        Instances of a class which mixes in L{FancyEqMixin} but which
        defines no comparison attributes compare by identity.
        c                   @   s   e Zd ZdS )z*EqualityTests.test_identity.<locals>.EmptyN)r6   r7   r8   r   r   r   r   Empty{  s   r   N)r   FancyEqMixinr   r   )r   r   emptyr   r   r   test_identityv  s    zEqualityTests.test_identityc                 C   sl   |  tddtddk | tddtddk | tddtddk | tddtddk dS )z
        Instances of a class which mixes in L{FancyEqMixin} should compare
        equal if all of their attributes compare equal.  They should not
        compare equal if any of their attributes do not compare equal.
        r   r   r   r   N)r   r   r   r!   r   r   r   test_equality  s    zEqualityTests.test_equalityc                 C   sl   |  tddtddk | tddtddk | tddtddk | tddtddk dS )z
        Inequality between instances of a particular L{record} should be
        defined as the negation of equality.
        r   r   r   r   N)r   r   r   r!   r   r   r   test_unequality  s    zEqualityTests.test_unequalityc                 C   s   |  tddtddk dS )zo
        Instances of different classes which mix in L{FancyEqMixin} should not
        compare equal.
        r   r   N)r   r   r   r!   r   r   r   test_differentClassesEquality  s    z+EqualityTests.test_differentClassesEqualityc                 C   s   |  tddtddk dS )zm
        Instances of different classes which mix in L{FancyEqMixin} should
        compare unequal.
        r   r   N)r   r   r   r!   r   r   r   test_differentClassesInequality  s    z-EqualityTests.test_differentClassesInequalityc                 C   sl   |  tddtddk | tddtddk | tddtddk | tddtddk dS )z
        An instance of a class which derives from a class which mixes in
        L{FancyEqMixin} should compare equal to an instance of the base class
        if and only if all of their attributes compare equal.
        r   r   r   r   N)r   r   r   r   r!   r   r   r   test_inheritedClassesEquality  s    z+EqualityTests.test_inheritedClassesEqualityc                 C   sl   |  tddtddk | tddtddk | tddtddk | tddtddk dS )z
        An instance of a class which derives from a class which mixes in
        L{FancyEqMixin} should compare unequal to an instance of the base
        class if any of their attributes compare unequal.
        r   r   r   r   N)r   r   r   r   r!   r   r   r   test_inheritedClassesInequality  s    z-EqualityTests.test_inheritedClassesInequalityc                 C   s0   |  tddt k | tddt k dS )z
        The right-hand argument to the equality operator is given a chance
        to determine the result of the operation if it is of a type
        unrelated to the L{FancyEqMixin}-based instance on the left-hand
        side.
        r   r   N)r   r   r   r   r   r!   r   r   r   (test_rightHandArgumentImplementsEquality  s    z6EqualityTests.test_rightHandArgumentImplementsEqualityc                 C   s0   |  tddt k | tddt k dS )z
        The right-hand argument to the non-equality operator is given a
        chance to determine the result of the operation if it is of a type
        unrelated to the L{FancyEqMixin}-based instance on the left-hand
        side.
        r   r   N)r   r   r   r   r   r!   r   r   r   *test_rightHandArgumentImplementsUnequality  s    z8EqualityTests.test_rightHandArgumentImplementsUnequalityN)r6   r7   r8   rA   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  s   r   c                   @   st   e Zd ZdZeedddkr dZdd Zdd Zd	d
 Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd ZdS )RunAsEffectiveUserTestsz;
    Test for the L{util.runAsEffectiveUser} function.
    rd   Nzgeteuid/seteuid not availablec                 C   sT   t  | _| td| jj | td| jj | td| jj | td| jj d S )Nrd   getegidseteuidsetegid)r   rO   rP   r*   rd   r   r   r   r!   r   r   r   rR     s
    zRunAsEffectiveUserTests.setUpc                 C   s@   |  ||kp|| jjd k |  ||kp8|| jjd k dS )zF
        Check if wanted UID/GID matched start or saved ones.
        N)r   rO   r\   setegidCalls)r   startUIDstartGIDwantUIDwantGIDr   r   r   _securedFunction  s    z(RunAsEffectiveUserTests._securedFunctionc                 C   s"   t dddd }| |d dS )zo
        L{util.runAsEffectiveUser} forwards the result obtained by calling the
        given function
        r   c                   S   s   dS )Nr   r   r   r   r   r   <lambda>      z<RunAsEffectiveUserTests.test_forwardResult.<locals>.<lambda>r   Nr   runAsEffectiveUserr   r   r   r   r   r   test_forwardResult  s    z*RunAsEffectiveUserTests.test_forwardResultc                 C   s$   t dddd d}| |d dS )ze
        L{util.runAsEffectiveUser} pass the given parameters to the given
        function.
        r   c                 S   s   d|  S )Nr   r   )rl   r   r   r   r     r   z=RunAsEffectiveUserTests.test_takeParameters.<locals>.<lambda>r   r   Nr   r   r   r   r   test_takeParameters  s    z+RunAsEffectiveUserTests.test_takeParametersc                 C   s*   t jddd
ddddd}| |d d	S )zg
        L{util.runAsEffectiveUser} pass the keyword parameters to the given
        function.
        r   r   c                 S   s   | | | S ri   r   )rl   rm   zr   r   r   r     r   zDRunAsEffectiveUserTests.test_takesKeyworkArguments.<locals>.<lambda>r   r   )r   r   N)r   r   r   r   r   r   r   test_takesKeyworkArguments  s    z2RunAsEffectiveUserTests.test_takesKeyworkArgumentsc              	   C   s\   || j _|| j _t||| j|||| | | j j| | | j j| g | j _g | j _dS )z
        Helper method checking the calls to C{os.seteuid} and C{os.setegid}
        made by L{util.runAsEffectiveUser}, when switching from startUID to
        wantUID and from startGID to wantGID.
        N)	rO   re   Zegidr   r   r   r   r\   r   )r   r   r   r   r   ZexpectedUIDSwitchesZexpectedGIDSwitchesr   r   r   _testUIDGIDSwitch  s         z)RunAsEffectiveUserTests._testUIDGIDSwitchc              	   C   sd   |  ddddg g  |  ddddddgg  |  ddddg ddg |  ddddddgddg dS )zL
        Check UID/GID switches when current effective UID is root.
        r   r   Nr  r!   r   r   r   	test_root"  s    z!RunAsEffectiveUserTests.test_rootc              
   C   sp   |  ddddddgg  |  ddddg g  |  ddddddddgddg |  ddddddddgddg dS )zP
        Check UID/GID switches when current effective UID is non-root.
        r   r   r   Nr  r!   r   r   r   test_UID,  s     z RunAsEffectiveUserTests.test_UIDc              	   C   sd   |  ddddg ddg |  ddddg g  |  ddddddgg  |  ddddddgddg dS )zP
        Check UID/GID switches when current effective GID is non-root.
        r   r   r   Nr  r!   r   r   r   test_GID6  s    z RunAsEffectiveUserTests.test_GIDc              
   C   s   |  ddddddgddg |  ddddddgg  |  ddddddddgddg |  ddddg g  |  ddddddddgg  |  ddddddddgddg |  ddddddddgddg dS )zT
        Check UID/GID switches when current effective UID/GID is non-root.
        r   r   r   Nr  r!   r   r   r   test_UIDGID@  s      z#RunAsEffectiveUserTests.test_UIDGID)r6   r7   r8   rA   rg   r*   r9   rR   r   r   r   r  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 Zejdkr*dZdS )InitGroupsTestsz'
    Tests for L{util.initgroups}.
    c                    sn   g   fdd}|  td| g }|  td|j tt d |  tt j	dfg | 
| dS )zc
        Calling L{util.initgroups} will call the underlying stdlib
        implmentation.
        c                    s     | |f d S ri   )rS   )rl   rm   rC   r   r   mockInitgroupsY  s    z?InitGroupsTests.test_initgroupsInStdlib.<locals>.mockInitgroups_initgroups	setgroupsr   N)rP   r   rS   rN   r*   r+   r   r(   r)   r,   r   )r   r
  ZsetgroupsCallsr   r	  r   test_initgroupsInStdlibR  s    z'InitGroupsTests.test_initgroupsInStdlibc                 C   s   |  ttjt d dS )zN
        Calling L{util.initgroups} with None for gid gives an error.
        N)r   r    r   rN   r*   r+   r!   r   r   r   test_initgroupsNoneGide  s    z&InitGroupsTests.test_initgroupsNoneGidNz.stdlib support for initgroups is not available)	r6   r7   r8   rA   r  r  r   r  r9   r   r   r   r   r  N  s
   
r  c                   @   s0   e Zd ZdZdd Zdd Zeeddge_dS )	DeprecationTestsz;
    Tests for deprecations in C{twisted.python.util}.
    c                 C   sT   t   | j| jgd}| |d d d | |d d t | t|d dS )z6
        L{util.getPluginDirs} is deprecated.
        r~   r   r^   zCtwisted.python.util.getPluginDirs is deprecated since Twisted 12.2.r   r   N)r   ZgetPluginDirsr_   test_getPluginDirsr   r   r`   r   rb   r   r   r   r  t  s    
z#DeprecationTests.test_getPluginDirsc                 C   sT   t   | j| jgd}| |d d d | |d d t | t|d dS )z5
        L{util.addPluginDir} is deprecated.
        r~   r   r^   zBtwisted.python.util.addPluginDir is deprecated since Twisted 12.2.r   r   N)r   ZaddPluginDirr_   test_addPluginDirr   r   r`   r  r   r   r   r    s    
z"DeprecationTests.test_addPluginDirz/twisted.python.util.getPluginDirs is deprecated)r   r^   N)	r6   r7   r8   rA   r  r  SUPPRESSr   r   r   r   r   r   r  p  s   r  c                   @   s:   e Zd ZdZeejZdd Zdd Zdd Z	dd	 Z
d
S )SuppressedWarningsTestsz6
    Tests for L{util.runWithWarningsSuppressed}.
    c                 C   sD   di fdi fg}|  |tjd |  |tjd | g |   dS )z
        Warnings from the function called by C{runWithWarningsSuppressed} are
        suppressed if they match the passed in filter.
        ignorez.*foo.*r  z.*bar.*
ignore fooz
ignore barNrunWithWarningsSuppressedwarningswarnr   r_   r   filtersr   r   r   &test_runWithWarningsSuppressedFiltered  s    z>SuppressedWarningsTests.test_runWithWarningsSuppressedFilteredc                 C   s@   di fdi fg}|  |tjd | dgdd |  D  dS )z
        Warnings from the function called by C{runWithWarningsSuppressed} are
        not suppressed if they do not match the passed in filter.
        r  r  zdon't ignorec                 S   s   g | ]}|d  qS r^   r   .0r   r   r   r   
<listcomp>  s     zTSuppressedWarningsTests.test_runWithWarningsSuppressedUnfiltered.<locals>.<listcomp>Nr  r  r   r   r   (test_runWithWarningsSuppressedUnfiltered  s     z@SuppressedWarningsTests.test_runWithWarningsSuppressedUnfilteredc                 C   s   |  | g dd d dS )zd
        C{runWithWarningsSuppressed} returns the result of the function it
        called.
        c                   S   s   dS )Nr   r   r   r   r   r   r     r   z:SuppressedWarningsTests.test_passThrough.<locals>.<lambda>r   N)r   r  r!   r   r   r   test_passThrough  s    z(SuppressedWarningsTests.test_passThroughc                 C   sJ   di fdi fg}|  |dd  td | dgdd |  D  dS )	zk
        Once C{runWithWarningsSuppressed} has returned, it no longer
        suppresses warnings.
        r  r  c                   S   s   d S ri   r   r   r   r   r   r     r   z<SuppressedWarningsTests.test_noSideEffects.<locals>.<lambda>r  c                 S   s   g | ]}|d  qS r   r   r!  r   r   r   r#    s     z>SuppressedWarningsTests.test_noSideEffects.<locals>.<listcomp>Nr  r  r   r   r   test_noSideEffects  s    
 z*SuppressedWarningsTests.test_noSideEffectsN)r6   r7   r8   rA   staticmethodr   r  r  r$  r%  r&  r   r   r   r   r    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 )FancyStrMixinTestsz*
    Tests for L{util.FancyStrMixin}.
    c                 C   s(   G dd dt j}| t| d dS )z
        If C{showAttributes} is set to a sequence of strings, C{__str__}
        renders using those by looking them up as attributes on the object.
        c                   @   s   e Zd ZdZdZdZdS )z6FancyStrMixinTests.test_sequenceOfStrings.<locals>.Foofirstsecondr   helloNr6   r7   r8   ZshowAttributesr*  r+  r   r   r   r   r>     s   r>   z<Foo first=1 second='hello'>Nr   FancyStrMixinr   strr   r>   r   r   r   test_sequenceOfStrings  s    z)FancyStrMixinTests.test_sequenceOfStringsc                 C   s(   G dd dt j}| dt|  dS )a  
        If C{showAttributes} has an item that is a 2-tuple, C{__str__} renders
        the first item in the tuple as a key and the result of calling the
        second item with the value of the attribute named by the first item as
        the value.
        c                   @   s$   e Zd Zdddd ffZdZdZdS )z.FancyStrMixinTests.test_formatter.<locals>.Foor*  r+  c                 S   s   t | d d d S )Nr   )r   )valuer   r   r   r     r   z7FancyStrMixinTests.test_formatter.<locals>.Foo.<lambda>r,  ZworldNr-  r   r   r   r   r>     s
   
r>   z"<Foo first='hello' second='dlrow'>Nr.  r1  r   r   r   test_formatter  s    z!FancyStrMixinTests.test_formatterc                 C   s(   G dd dt j}| t| d dS )a&  
        If C{showAttributes} has an item that is a 3-tuple, C{__str__} renders
        the second item in the tuple as a key, and the contents of the
        attribute named in the first item are rendered as the value. The value
        is formatted using the third item in the tuple.
        c                   @   s   e Zd ZdZdZdZdS )z-FancyStrMixinTests.test_override.<locals>.Foo)r*  )r+  Z2ndz%.1fr   gS @Nr-  r   r   r   r   r>     s   r>   z<Foo first=1 2nd=2.1>Nr.  r1  r   r   r   test_override  s    z FancyStrMixinTests.test_overridec                 C   s(   G dd dt j}| t| d dS )z_
        If C{fancybasename} is present, C{__str__} uses it instead of the class name.
        c                   @   s   e Zd ZdZdS )z2FancyStrMixinTests.test_fancybasename.<locals>.FooZBarN)r6   r7   r8   Zfancybasenamer   r   r   r   r>     s   r>   z<Bar>Nr.  r1  r   r   r   test_fancybasename  s    z%FancyStrMixinTests.test_fancybasenamec                 C   s0   G dd dt j}| }| t|t| dS )zE
        C{__repr__} outputs the same content as C{__str__}.
        c                   @   s   e Zd ZdZdZdZdS )z)FancyStrMixinTests.test_repr.<locals>.Foor)  r   r,  Nr-  r   r   r   r   r>     s   r>   N)r   r/  r   r0  r   )r   r>   objr   r   r   	test_repr  s    zFancyStrMixinTests.test_reprN)	r6   r7   r8   rA   r2  r4  r5  r6  r8  r   r   r   r   r(    s   	r(  c                   @   sH   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S )
PadToTestsz"
    Tests for L{util.padTo}.
    c                 C   s"   t dg }| dgd | dS )ze
        L{None} values can be added to a list to cause it to have a certain
        length.
        r   Nr   padTor   r   paddedr   r   r   test_default  s    zPadToTests.test_defaultc                 C   s$   t dg d}| dgd | dS )zg
        A specific value can be added to a list to cause it to have a certain
        length.
        r   rl   Nr:  r<  r   r   r   test_specificDefaultValue  s    z$PadToTests.test_specificDefaultValuec                 C   s(   t dddgd}| dddg| dS )zl
        A list which already has some items has the padding value added after
        those items.
        r   r   r   r   Nr:  r<  r   r   r   test_padNonEmptyList'  s    zPadToTests.test_padNonEmptyListc                 C   s   |  ttjdddg dS )z{
        L{util.padTo} can't pad a list if the size requested is smaller than
        the size of the list to pad.
        r   r   N)r   r   r   r;  r!   r   r   r   test_padToSmallerSize0  s    z PadToTests.test_padToSmallerSizec                 C   s(   ddg}t t||}| || dS )z
        If the list is already the length indicated by the padding argument
        then a list with the same value is returned.
        r   r   N)r   r;  r`   r   r   r   r=  r   r   r   test_alreadyPadded8  s    zPadToTests.test_alreadyPaddedc                 C   s(   ddg}t t||}| || dS )z
        If the list is already the length indicated by the padding argument
        then the return value is a copy of the input.
        r   r   N)r   r;  r`   ZassertIsNotrB  r   r   r   test_alreadyPaddedCopiesB  s    z#PadToTests.test_alreadyPaddedCopiesc                 C   s    g }t d| | g | dS )zO
        L{util.padTo} doesn't modify the input list but makes a copy.
        r   Nr:  )r   r   r   r   r   test_makeCopyL  s    zPadToTests.test_makeCopyN)r6   r7   r8   rA   r>  r?  r@  rA  rC  rD  rE  r   r   r   r   r9    s   			

r9  c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )ReplaceIfTestsz'
    Tests for L{util._replaceIf}.
    c                 C   sF   t ddd dd }| | d | |jd | |jd d	S )
zl
        L{util._replaceIf} swaps out the body of a function if the conditional
        is C{True}.
        Tc                   S   s   dS Nhir   r   r   r   r   r   `  r   z4ReplaceIfTests.test_replacesIfTrue.<locals>.<lambda>c                   S   s   dS Nbyer   r   r   r   r   test`  s    z0ReplaceIfTests.test_replacesIfTrue.<locals>.testrH  rK  ztwisted.python.test.test_utilN)r   
_replaceIfr   r6   r7   r   rK  r   r   r   test_replacesIfTrue[  s
    
z"ReplaceIfTests.test_replacesIfTruec                 C   s*   t ddd dd }| | d dS )zt
        L{util._replaceIf} keeps the original body of the function if the
        conditional is C{False}.
        Fc                   S   s   dS rG  r   r   r   r   r   r   n  r   z2ReplaceIfTests.test_keepsIfFalse.<locals>.<lambda>c                   S   s   dS rI  r   r   r   r   r   rK  n  s    z.ReplaceIfTests.test_keepsIfFalse.<locals>.testrJ  Nr   rL  r   rM  r   r   r   test_keepsIfFalsei  s    
z ReplaceIfTests.test_keepsIfFalsec                 C   sJ   t ddd t ddd t ddd dd }| | d	 d
S )z
        In the case that multiple conditions are true, the first one
        (to the reader) is chosen by L{util._replaceIf}
        Tc                   S   s   dS rG  r   r   r   r   r   r   z  r   z5ReplaceIfTests.test_multipleReplace.<locals>.<lambda>Fc                   S   s   dS )Nrr   r   r   r   r   r   r   {  r   c                   S   s   dS )Nrv   r   r   r   r   r   r   |  r   c                   S   s   dS rI  r   r   r   r   r   rK  z  s    z1ReplaceIfTests.test_multipleReplace.<locals>.testrH  NrO  rM  r   r   r   test_multipleReplaceu  s
    z#ReplaceIfTests.test_multipleReplacec              	   C   sB   |  t}tdddd }W 5 Q R X | |jjd d dS )zM
        L{util._replaceIf}'s condition argument only accepts bools.
        rH  Ztherec                   S   s   dS )z5
                Some test function.
                Nr   r   r   r   r   rK    s    z+ReplaceIfTests.test_boolsOnly.<locals>.testr   z:condition argument to _replaceIf requires a bool, not 'hi'N)r   r   r   rL  r   Z	exceptionargs)r   rp   rK  r   r   r   test_boolsOnly  s    
zReplaceIfTests.test_boolsOnlyN)r6   r7   r8   rA   rN  rP  rQ  rS  r   r   r   r   rF  V  s
   rF  )@rA   Z
__future__r   r   rF   os.pathr*   r   r   r  r(   r3   ImportErrorZtwisted.trialr   Ztwisted.trial.utilr   r  Ztwisted.pythonr   Ztwisted.python.filepathr   Ztwisted.internetr   Ztwisted.internet.interfacesr	   Ztwisted.internet.protocolr
   Ztwisted.internet.deferr   Ztwisted.internet.errorr   Ztwisted.test.test_processr   
executableZ_asBytesPathr   ZTestCaser   r:   rB   rM   rh   r|   r   r   r   r   r   r   r   r   r   rs   r   r   r   r   r  r  r  r(  r9  rF  r   r   r   r   <module>   sZ   ;NaG Nis"'4EF