o
    ¯bHˆ  ã                   @   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mZm	Z	m
Z
mZ ddlmZ z
ddlZddlZW n ey?   dZdZY nw eZe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$m%Z% ddl&m'Z( e ej)ƒ *¡ Z+G dd„ de%ƒZ,G dd„ de%ƒZ-G dd„ de%ƒZ.ee/eddƒ dƒG dd„ de%ƒƒZ0G dd„ de%ƒZ1G dd„ de%ƒZ2G dd„ de%ƒZ3G dd „ d eƒZ4ee 5e¡ d!ƒG d"d#„ d#e%ƒƒZ6G d$d%„ d%e%ƒZ7G d&d'„ d'e%ƒZ8G d(d)„ d)ej9ƒZ:G d*d+„ d+ej9ƒZ;G d,d-„ d-e:ƒZ<G d.d/„ d/ƒZ=G d0d1„ d1ƒZ>G d2d3„ d3e%ƒZ?ee/ed4dƒ d5ƒG d6d7„ d7e%ƒƒZ@eejA d8ƒG d9d:„ d:e%ƒƒZBG d;d<„ d<e%ƒZCG d=d>„ d>e%ƒZDG d?d@„ d@e%ƒZEG dAdB„ dBe%ƒZFdS )Cz#
Tests for L{twisted.python.util}.
é    N)ÚIterableÚMappingÚMutableMappingÚSequence)ÚskipIf)Úreactor)ÚDeferred)ÚProcessDone)ÚIReactorProcess)ÚProcessProtocol)Úutil)ÚFilePath)ÚMockOS)ÚFailTestÚTestCase)Úsuppressc                   @   sX   e Zd Zdd„ Zdd„ Zdd„ Zeedu dƒd	d
„ ƒZdd„ Z	ee
du dƒdd„ ƒZdS )Ú	UtilTestsc                 C   s"   g d¢}|   t |¡g d¢¡ d S )N)Úaé   Úabr   é   é   r   é   r   r   é   )r   r   r   r   r   r   r   )ÚassertEqualr   Úuniquify)ÚselfÚlistWithDupes© r   ú?/usr/lib/python3/dist-packages/twisted/python/test/test_util.pyÚtestUniq+   s   zUtilTests.testUniqc                 C   s\   |   t ttdd¡¡ |  t ttdd¡¡ zt ttdd¡ W tdƒ‚ ty-   Y d S w )Nr   r   z,util.raises didn't raise when it should have)Ú
assertTruer   ÚraisesÚZeroDivisionErrorÚdivmodÚassertFalseÚ	TypeErrorr   ©r   r   r   r   Ú
testRaises/   s   ýÿzUtilTests.testRaisesc                 C   ó   |   t d¡d¡ dS )ú†
        When L{uidFromString} is called with a base-ten string representation
        of an integer, it returns the integer.
        Ú100éd   N)r   r   ÚuidFromStringr'   r   r   r   Útest_uidFromNumericString:   ó   z#UtilTests.test_uidFromNumericStringNz0Username/UID conversion requires the pwd module.c                 C   ó(   t  t ¡ ¡}|  t |j¡|j¡ dS )r*   N)	ÚpwdÚgetpwuidÚosÚgetuidr   r   r-   Úpw_nameÚpw_uid)r   Úpwentr   r   r   Útest_uidFromUsernameStringA   ó   z$UtilTests.test_uidFromUsernameStringc                 C   r)   )ú†
        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_gidFromNumericStringJ   r/   z#UtilTests.test_gidFromNumericStringz2Group Name/GID conversion requires the grp module.c                 C   r0   )r:   N)	ÚgrpÚgetgrgidr3   Úgetgidr   r   r;   Úgr_nameÚgr_gid)r   Úgrentr   r   r   Útest_gidFromGroupnameStringQ   r9   z%UtilTests.test_gidFromGroupnameString)Ú__name__Ú
__module__Ú__qualname__r    r(   r.   r   r1   r8   r<   r=   rC   r   r   r   r   r   *   s    
r   c                   @   ó   e Zd ZdZdd„ ZdS )ÚNameToLabelTestsz#
    Tests for L{nameToLabel}.
    c                 C   sD   g d¢}|D ]\}}t  |¡}|  ||d|›d|›d|›¡ qdS )zK
        Test the various kinds of inputs L{nameToLabel} supports.
        ))ÚfÚF)ÚfoÚFo)ÚfooÚFoo)ÚfooBarzFoo Bar)Ú	fooBarBazzFoo Bar BazznameToLabel(z) == z != N)r   ÚnameToLabelr   )r   ÚnameDataÚinpÚoutÚgotr   r   r   Útest_nameToLabel`   s
   
"þz!NameToLabelTests.test_nameToLabelN)rD   rE   rF   Ú__doc__rV   r   r   r   r   rH   [   ó    rH   c                   @   rG   )Ú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 ur|tjdƒ‚| | S )Nr   zInterrupted system call!)ÚcallsÚ
exceptionsÚpopÚerrnoÚEINTR)r   ÚbÚexcr'   r   r   rI   }   s
   
z3UntilConcludesTests.test_uninterruptably.<locals>.fNr   r   r   r   é   )r[   rZ   r   r   ÚuntilConcludesÚOSErrorÚIOError)r   rI   r   r'   r   Útest_uninterruptablyu   s   z(UntilConcludesTests.test_uninterruptablyN)rD   rE   rF   rW   re   r   r   r   r   rY   p   rX   rY   r4   zgetuid/setuid not availablec                   @   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 )ÚSwitchUIDTestsz&
    Tests for L{util.switchUID}.
    c                 C   s2   t ƒ | _|  td| j¡ |  td| j¡ g | _d S )Nr3   Ú
initgroups)r   ÚmockosÚpatchr   rg   Ú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)rj   Úappend)r   ÚuidÚgidr   r   r   rg   ›   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©ro   N)Úsetuidro   )r   Ú	switchUIDr   rj   rh   Ú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}.
        ro   NTrp   )r   rr   r   rj   rh   Ú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)
rh   r4   r   rr   r   rj   rs   ÚflushWarningsÚlenÚassertIn)r   rm   Ú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   rw   zbut euid is already %i)
rh   Úgeteuidr   rr   r   rj   ru   rx   ry   rz   )r   Úeuidr{   r   r   r   Útest_currentEUIDÄ   s   

þzSwitchUIDTests.test_currentEUIDN)
rD   rE   rF   rW   rk   rg   rt   rv   r|   r   r   r   r   r   rf      s    		rf   c                   @   ó8   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   rM   ã   ó   zQMergeFunctionMetadataTests.test_mergedFunctionBehavesLikeMergeTarget.<locals>.fooé
   c                    s   |\}}ˆ S r‚   r   )ÚxÚyr   ÚcÚdÚer   r_   )Ú
bar_objectr   r   Úbaræ   s   zQMergeFunctionMetadataTests.test_mergedFunctionBehavesLikeMergeTarget.<locals>.barr   r   )r   r   )ÚquuxN)r…   )Úobjectr   ÚmergeFunctionMetadataÚassertIs©r   rM   rŒ   Úbazr   )r‹   rƒ   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   ó   d S r‚   r   r   r   r   r   rM   ó   r„   z;MergeFunctionMetadataTests.test_moduleIsMerged.<locals>.fooc                   S   r”   r‚   r   r   r   r   r   rŒ   ö   r„   z;MergeFunctionMetadataTests.test_moduleIsMerged.<locals>.barzsomewhere.elseN)rE   r   r   r   r‘   r   r   r   Útest_moduleIsMergedí   s
   z.MergeFunctionMetadataTests.test_moduleIsMergedc                 C   ó0   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   ó   dS )z&
            This is foo.
            Nr   r   r   r   r   rM     ó    z>MergeFunctionMetadataTests.test_docstringIsMerged.<locals>.fooc                   S   r—   )z&
            This is bar.
            Nr   r   r   r   r   rŒ     r˜   z>MergeFunctionMetadataTests.test_docstringIsMerged.<locals>.barN)r   r   r   rW   r‘   r   r   r   Útest_docstringIsMergedþ   s   z1MergeFunctionMetadataTests.test_docstringIsMergedc                 C   r–   )zS
        Merging C{foo} into C{bar} returns a function with C{foo}'s name.
        c                   S   r”   r‚   r   r   r   r   r   rM     r„   z9MergeFunctionMetadataTests.test_nameIsMerged.<locals>.fooc                   S   r”   r‚   r   r   r   r   r   rŒ     r„   z9MergeFunctionMetadataTests.test_nameIsMerged.<locals>.barN)r   r   r   rD   r‘   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   r”   r‚   r   r   r   r   r   rM   $  r„   zGMergeFunctionMetadataTests.test_instanceDictionaryIsMerged.<locals>.foor   r   c                   S   r”   r‚   r   r   r   r   r   rŒ   *  r„   zGMergeFunctionMetadataTests.test_instanceDictionaryIsMerged.<locals>.barr   r   N)r   r_   rˆ   r   r   r   r‘   r   r   r   Útest_instanceDictionaryIsMerged  s   z:MergeFunctionMetadataTests.test_instanceDictionaryIsMergedN)	rD   rE   rF   rW   r“   r•   r™   rš   r›   r   r   r   r   r   Ö   s    r   c                   @   rG   )Ú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©ÚoffendingFunctionsrw   zftwisted.python.util.OrderedDict was deprecated in Twisted 15.5.0: Use collections.OrderedDict instead.Úcategoryr   N)Útwisted.python.utilr   rx   Útest_deprecatedr   ÚDeprecationWarningry   )r   r   r{   r   r   r   r¢   ;  s   
þz OrderedDictTests.test_deprecatedN)rD   rE   rF   rW   r¢   r   r   r   r   rœ   6  rX   rœ   c                   @   r€   )ÚInsensitiveDictTestsz,
    Tests for L{util.InsensitiveDict}.
    c                 C   sL   t  ¡ }|  t|tƒ¡ |  t|tƒ¡ |  t|tƒ¡ |  t|tƒ¡ dS )zN
        L{util.InsensitiveDict} implements L{typing.MutableMapping}.
        N)	r   ÚInsensitiveDictr!   Ú
isinstancer   r   r   r%   r   )r   Údctr   r   r   Útest_abcR  s
   zInsensitiveDictTests.test_abcc                 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	œƒ}|  ||¡ g d
¢}|D ]}|  || ¡ ¡ |  ||| f| 	¡ ¡ qU|  t
|ƒt
|ƒ¡ |d= |d= |  | ¡ dg¡ dS )zr
        L{util.InsensitiveDict} preserves the case of keys if constructed with
        C{preserve=True}.
        rŒ   r   r   ©rN   r   Úfnz©Úpreserverª   rM   rN   )r§   r¥   )rN   rª   r   N)r   r¥   r   ÚcopyÚgetrz   ÚevalÚreprÚkeysÚitemsry   )r   r§   Úresultr±   r†   r   r   r   Útest_preserve\  s,   þþz"InsensitiveDictTests.test_preservec                 C   sŠ   t jddddidœdd}g d¢}|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}.
        rŒ   r   r   r©   r   r«   )rM   rª   r   rM   rª   N)r   r¥   rz   r±   r²   r   ry   )r   r§   r±   r†   r   r   r   Útest_noPreserve{  s   z$InsensitiveDictTests.test_noPreservec                 C   ó:   t jdd}d|d< |  |d d¡ |  | ¡ dg¡ dS )z4
        Unicode keys are case insensitive.
        Fr«   r   rN   ÚFOOrM   N©r   r¥   r   r±   ©r   r‰   r   r   r   Útest_unicodeŠ  ó   z!InsensitiveDictTests.test_unicodec                 C   r¶   )z2
        Bytes keys are case insensitive.
        Fr«   r   s   Foos   FOOs   fooNr¸   r¹   r   r   r   Ú
test_bytes“  r»   zInsensitiveDictTests.test_bytesN)	rD   rE   rF   rW   r¨   r´   rµ   rº   r¼   r   r   r   r   r¤   M  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\n"} 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
)ÚoutputÚ	transportÚwriter'   r   r   r   ÚconnectionMade£  s   z-PasswordTestingProcessProtocol.connectionMadec                 C   s   | j  ||f¡ d S r‚   )r¾   rl   )r   Úfdr¾   r   r   r   ÚchildDataReceived§  s   z0PasswordTestingProcessProtocol.childDataReceivedc                 C   s   | j  || jf¡ d S r‚   )ÚfinishedÚcallbackr¾   )r   Úreasonr   r   r   ÚprocessEndedª  s   z+PasswordTestingProcessProtocol.processEndedN)rD   rE   rF   rW   rÁ   rÃ   rÇ   r   r   r   r   r½     s
    r½   z,Process support required to test getPasswordc                   @   ó   e Zd Zdd„ ZdS )ÚGetPasswordTestsc                    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)Útrapr	   rz   )r³   rÆ   r¾   r'   r   r   ÚprocessFinishedÌ  s   
z4GetPasswordTests.test_stdin.<locals>.processFinished)r½   r   rÄ   r   ÚspawnProcessÚpyExer3   ÚpathsepÚjoinÚsysÚpathÚencodeÚaddCallback)r   ÚprÍ   r   r'   r   Ú
test_stdin³  s   ü
ózGetPasswordTests.test_stdinN)rD   rE   rF   r×   r   r   r   r   rÉ   ®  s    rÉ   c                   @   rÈ   )Ú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.txtrŒ   Úbam)ÚfilesÚdirsÚsearchupwards)r3   ÚmakedirsÚopenÚcloseÚmkdirr   rÝ   rÓ   ÚabspathÚsepr   ÚshutilÚrmtree)r   ÚactualÚexpectedr   r   r   ÚtestSearchupwardsÕ  s&   




ÿ
ÿz$SearchUpwardsTests.testSearchupwardsN)rD   rE   rF   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 )Nr…   r,   ©r…   N©Úiterr   ÚIntervalDifferentialÚranger   Únext©r   r‰   Úir   r   r   ÚtestDefaultì  s   ÿz%IntervalDifferentialTests.testDefaultc                 C   s4   t t dgd¡ƒ}tdƒD ]
}|  t|ƒd¡ qd S )Nra   r…   r,   ©ra   r   rë   rð   r   r   r   Ú
testSingleñ  s   ÿz$IntervalDifferentialTests.testSinglec                 C   sæ   t t ddgd¡ƒ}tdƒD ]b}|  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 )Nra   é   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   r   ÚtestPairö  s   ôz"IntervalDifferentialTests.testPairc                 C   sV  t t g d¢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¡ |  t|ƒd¡ |  t|ƒd¡ |  t|ƒd¡ |  t|ƒd¡ |  t|ƒd¡ |  t|ƒd¡ |  t|ƒd¡ qd S )	N)r   r   ra   r…   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 )Nr…   rê   r   r÷   r   rü   )rì   r   rí   r   rï   Ú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   ra   r…   r÷   rö   rø   rú   ró   rê   )rì   r   rí   r   rï   ÚremoveIntervalÚassertRaisesÚ
ValueErrorr¹   r   r   r   Ú
testRemove+  s   

z$IntervalDifferentialTests.testRemoveN)	rD   rE   rF   rò   rô   rý   rþ   r   r  r   r   r   r   ré   ë  s    ré   c                   @   ó   e Zd ZdZdZdd„ ZdS )ÚRecordz8
    Trivial user of L{FancyEqMixin} used by tests.
    ©r   r_   c                 C   ó   || _ || _d S r‚   r  ©r   r   r_   r   r   r   Ú__init__?  ó   
zRecord.__init__N©rD   rE   rF   rW   ÚcompareAttributesr
  r   r   r   r   r  8  ó    r  c                   @   r  )ÚDifferentRecordzL
    Trivial user of L{FancyEqMixin} which is not related to L{Record}.
    r  c                 C   r  r‚   r  r	  r   r   r   r
  K  r  zDifferentRecord.__init__Nr  r   r   r   r   r  D  r  r  c                   @   ó   e Zd ZdZdS )ÚDerivedRecordz@
    A class with an inheritance relationship to L{Record}.
    N)rD   rE   rF   rW   r   r   r   r   r  P  ó    r  c                   @   ó"   e Zd ZdZdedefdd„ZdS )ÚEqualToEverythingzQ
    A class the instances of which consider themselves equal to everything.
    ÚotherÚreturnc                 C   r—   )NTr   ©r   r  r   r   r   Ú__eq__[  r„   zEqualToEverything.__eq__N©rD   rE   rF   rW   rŽ   Úboolr  r   r   r   r   r  V  ó    r  c                   @   r  )ÚEqualToNothingzN
    A class the instances of which consider themselves equal to nothing.
    r  r  c                 C   r—   )NFr   r  r   r   r   r  d  r„   zEqualToNothing.__eq__Nr  r   r   r   r   r  _  r  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)rD   rE   rF   r   r   r   r   ÚEmptys  s    r  N)r   ÚFancyEqMixinr%   r!   )r   r  Úemptyr   r   r   Útest_identitym  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|  ó   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‘  ó   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˜  r&  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Ÿ  r#  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ª  r#  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µ  ó   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¿  r+  z8EqualityTests.test_rightHandArgumentImplementsUnequalityN)rD   rE   rF   rW   r!  r"  r$  r%  r'  r(  r)  r*  r,  r   r   r   r   r  h  s    

r  r}   zgeteuid/seteuid not availablec                   @   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dS )ÚRunAsEffectiveUserTestsz;
    Test for the L{util.runAsEffectiveUser} function.
    c                 C   sT   t ƒ | _|  td| jj¡ |  td| jj¡ |  td| jj¡ |  td| jj¡ d S )Nr}   ÚgetegidÚseteuidÚsetegid)r   rh   ri   r3   r}   r.  r/  r0  r'   r   r   r   rk   Ð  s
   zRunAsEffectiveUserTests.setUpc                 C   s@   |   ||kp|| jjd k¡ |   ||kp|| jjd k¡ dS )zF
        Check if wanted UID/GID matched start or saved ones.
        éÿÿÿÿN)r!   rh   ru   Ú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   r—   )Nr   r   r   r   r   r   Ú<lambda>ã  r˜   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   )r†   r   r   r   r8  ë  s    z=RunAsEffectiveUserTests.test_takeParameters.<locals>.<lambda>r   r   Nr9  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 r‚   r   )r†   r‡   Úzr   r   r   r8  ó  s    zDRunAsEffectiveUserTests.test_takesKeyworkArguments.<locals>.<lambda>r   r   )r>  r   Nrù   r9  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)	rh   r~   Úegidr   r:  r7  r   ru   r2  )r   r3  r4  r5  r6  ÚexpectedUIDSwitchesÚ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©rC  r'   r   r   r   Ú	test_root  s    z!RunAsEffectiveUserTests.test_rootc              	   C   sh   |   ddddddgg ¡ |   ddddg g ¡ |   ddddg d¢ddg¡ |   ddddg d¢ddg¡ dS )zP
        Check UID/GID switches when current effective UID is non-root.
        r   r   ©r   r   r   r   r   ©r   r   r   r   NrD  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   NrD  r'   r   r   r   Útest_GID&  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g d¢ddg¡ |   ddddg g ¡ |   ddddg d¢g ¡ |   ddddg d¢ddg¡ |   ddddg d¢ddg¡ dS )zT
        Check UID/GID switches when current effective UID/GID is non-root.
        r   r   rF  r   rG  NrD  r'   r   r   r   Útest_UIDGID/  s    z#RunAsEffectiveUserTests.test_UIDGIDN)rD   rE   rF   rW   rk   r7  r<  r=  r?  rC  rE  rH  rI  rJ  r   r   r   r   r-  Ê  s    			r-  z.stdlib support for initgroups is not availablec                   @   s    e Zd ZdZdd„ Zd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 r‚   )rl   )r†   r‡   ©rZ   r   r   ÚmockInitgroupsI  s   z?InitGroupsTests.test_initgroupsInStdlib.<locals>.mockInitgroupsÚ_initgroupsÚ	setgroupsr   N)ri   r   rl   rg   r3   r4   r   r1   r2   r5   r%   )r   rM  ÚsetgroupsCallsr   rL  r   Útest_initgroupsInStdlibB  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   rg   r3   r4   r'   r   r   r   Útest_initgroupsNoneGidT  s   z&InitGroupsTests.test_initgroupsNoneGidN)rD   rE   rF   rW   rQ  rR  r   r   r   r   rK  <  s    rK  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   óT   t  ¡  | j| jgd}|  |d d d¡ |  |d d t¡ |  t|ƒd¡ dS )z6
        L{util.getPluginDirs} is deprecated.
        rž   r   rw   zCtwisted.python.util.getPluginDirs is deprecated since Twisted 12.2.r    r   N)r   ÚgetPluginDirsrx   Útest_getPluginDirsr   r£   ry   ©r   r{   r   r   r   rV  `  ó   ÿ
þz#DeprecationTests.test_getPluginDirsc                 C   rT  )z5
        L{util.addPluginDir} is deprecated.
        rž   r   rw   zBtwisted.python.util.addPluginDir is deprecated since Twisted 12.2.r    r   N)r   ÚaddPluginDirrx   Útest_addPluginDirr   r£   ry   rW  r   r   r   rZ  o  rX  z"DeprecationTests.test_addPluginDirz/twisted.python.util.getPluginDirs is deprecated)r    rw   N)	rD   rE   rF   rW   rV  rZ  ÚSUPPRESSr£   r   r   r   r   r   rS  [  s    þ
ÿrS  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   rx   ©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   ó   g | ]}|d  ‘qS ©rw   r   ©Ú.0rÙ   r   r   r   Ú
<listcomp>ž  ó    zTSuppressedWarningsTests.test_runWithWarningsSuppressedUnfiltered.<locals>.<listcomp>Nra  re  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   r—   )Nr   r   r   r   r   r   r8  ¥  r˜   z:SuppressedWarningsTests.test_passThrough.<locals>.<lambda>r   N)r   rb  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   r”   r‚   r   r   r   r   r   r8  ­  r˜   z<SuppressedWarningsTests.test_noSideEffects.<locals>.<lambda>r`  c                 S   rh  ri  r   rj  r   r   r   rl  ¯  rm  z>SuppressedWarningsTests.test_noSideEffects.<locals>.<listcomp>Nra  re  r   r   r   Útest_noSideEffects§  s   
 z*SuppressedWarningsTests.test_noSideEffectsN)rD   rE   rF   rW   Ústaticmethodr   rb  rg  rn  ro  rp  r   r   r   r   r\  †  s    

	r\  c                   @   r€   )ÚFancyStrMixinTestsz*
    Tests for L{util.FancyStrMixin}.
    c                 C   ó(   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                   @   ó   e Zd ZdZdZdZdS )z6FancyStrMixinTests.test_sequenceOfStrings.<locals>.Foo©ÚfirstÚsecondr   ÚhelloN©rD   rE   rF   ÚshowAttributesrv  rw  r   r   r   r   rN   ½  ó    rN   z<Foo first=1 second='hello'>N©r   ÚFancyStrMixinr   Ústr©r   rN   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>.Foorv  rw  c                 S   s   t | d d d… ƒS )Nr1  )r°   )Úvaluer   r   r   r8  Í  s    z7FancyStrMixinTests.test_formatter.<locals>.Foo.<lambda>rx  ÚworldNry  r   r   r   r   rN   Ì  s    rN   z"<Foo first='hello' second='dlrow'>Nr|  r  r   r   r   Útest_formatterÄ  ó   z!FancyStrMixinTests.test_formatterc                 C   rs  )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                   @   rt  )z-FancyStrMixinTests.test_override.<locals>.Foo)rv  )rw  Ú2ndz%.1fr   gÙÎ÷Sã @Nry  r   r   r   r   rN   Û  r{  rN   z<Foo first=1 2nd=2.1>Nr|  r  r   r   r   Útest_overrideÓ  r„  z FancyStrMixinTests.test_overridec                 C   rs  )z_
        If C{fancybasename} is present, C{__str__} uses it instead of the class name.
        c                   @   r  )z2FancyStrMixinTests.test_fancybasename.<locals>.FooÚBarN)rD   rE   rF   Úfancybasenamer   r   r   r   rN   ç  r  rN   z<Bar>Nr|  r  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                   @   rt  )z)FancyStrMixinTests.test_repr.<locals>.Fooru  r   rx  Nry  r   r   r   r   rN   ñ  r{  rN   N)r   r}  r   r~  r°   )r   rN   Úobjr   r   r   Ú	test_reprì  s   zFancyStrMixinTests.test_reprN)	rD   rE   rF   rW   r€  rƒ  r†  r‰  r‹  r   r   r   r   rr  ²  s    
rr  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   r†   Nr  r  r   r   r   Útest_specificDefaultValue  s   z$PadToTests.test_specificDefaultValuec                 C   s&   t  dddgd¡}|  g d¢|¡ dS )zl
        A list which already has some items has the padding value added after
        those items.
        r   r   r   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_padToSmallerSize  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Ž  ry   r   ©r   r²   r  r   r   r   Útest_alreadyPadded  ó   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Ž  ry   ÚassertIsNotr•  r   r   r   Útest_alreadyPaddedCopies'  r—  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_makeCopy0  s   zPadToTests.test_makeCopyN)rD   rE   rF   rW   r‘  r’  r“  r”  r–  r™  rš  r   r   r   r   rŒ  ú  s    		rŒ  )GrW   r]   Úos.pathr3   rä   rÒ   rc  Útypingr   r   r   r   Úunittestr   r=   Ú_grpr1   Ú_pwdÚImportErrorÚtwisted.internetr   Útwisted.internet.deferr   Útwisted.internet.errorr	   Útwisted.internet.interfacesr
   Útwisted.internet.protocolr   Útwisted.pythonr   Útwisted.python.filepathr   Útwisted.test.test_processr   Útwisted.trial.unittestr   r   Útwisted.trial.utilr   r[  Ú
executableÚ_asBytesPathrÏ   r   rH   rY   Úgetattrrf   r   rœ   r¤   r½   Ú
providedByrÉ   rØ   ré   r  r  r  r  r  r  r  r-  rN  rK  rS  r\  rr  rŒ  r   r   r   r   Ú<module>   st   þ1F`P
þ"M		bq+,H