U
    ª€ljHˆ  ã                   @   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k
r€   dZdZY n
X 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kdƒd	d
„ ƒZdd„ Z	ee
dkdƒdd„ ƒZ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)ÚselfZlistWithDupes© r   úA/tmp/pip-unpacked-wheel-aghyhbke/twisted/python/test/test_util.pyÚtestUniq+   s    zUtilTests.testUniqc                 C   sb   |   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   ©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.
        Ú100éd   N)r   r   ÚuidFromStringr$   r   r   r   Útest_uidFromNumericString:   s    z#UtilTests.test_uidFromNumericStringNz0Username/UID conversion requires the pwd module.c                 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_uidFromUsernameStringA   s    z$UtilTests.test_uidFromUsernameStringc                 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_gidFromNumericStringJ   s    z#UtilTests.test_gidFromNumericStringz2Group Name/GID conversion requires the grp module.c                 C   s(   t  t ¡ ¡}|  t |j¡|j¡ dS )r3   N)	ÚgrpÚgetgrgidr-   Úgetgidr   r   r4   Úgr_nameÚgr_gid)r   Zgrentr   r   r   Útest_gidFromGroupnameStringQ   s    z%UtilTests.test_gidFromGroupnameString)Ú__name__Ú
__module__Ú__qualname__r   r%   r*   r   r+   r2   r5   r6   r;   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   sJ   dddddg}|D ]2\}}t  |¡}|  ||d|›d|›d|›�¡ qd	S )
zK
        Test the various kinds of inputs L{nameToLabel} supports.
        )ÚfÚF)ÚfoZFo)ÚfooÚFoo)ZfooBarzFoo Bar)Z	fooBarBazzFoo Bar BazznameToLabel(z) == z != N)r   ZnameToLabelr   )r   ZnameDataÚinpÚoutÚgotr   r   r   Útest_nameToLabel`   s    û
z!NameToLabelTests.test_nameToLabelN)r<   r=   r>   Ú__doc__rH   r   r   r   r   r?   [   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   é   )rL   rK   r   r   ZuntilConcludesÚOSErrorÚIOError)r   r@   r   r$   r   Útest_uninterruptablyu   s    z(UntilConcludesTests.test_uninterruptablyN)r<   r=   r>   rI   rT   r   r   r   r   rJ   p   s   rJ   r.   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 )Nr-   Ú
initgroups)r   ÚmockosÚpatchr   rV   Ú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)rY   Úappend)r   ÚuidÚgidr   r   r   rV   ›   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©r^   N)Úsetuidr^   )r   Ú	switchUIDr   rY   rW   Ú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}.
        r^   NTr_   )r   ra   r   rY   rW   Ú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)
rW   r.   r   ra   r   rY   rb   ÚflushWarningsÚlenÚassertIn)r   r\   Ú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   rf   zbut euid is already %i)
rW   Úgeteuidr   ra   r   rY   rd   rg   rh   ri   )r   Úeuidrj   r   r   r   Útest_currentEUIDÄ   s    

þzSwitchUIDTests.test_currentEUIDN)
r<   r=   r>   rI   rZ   rV   rc   re   rk   rn   r   r   r   r   rU   �   s   		rU   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   rC   ã   s    zQMergeFunctionMetadataTests.test_mergedFunctionBehavesLikeMergeTarget.<locals>.fooé
   c                    s   |\}}ˆ S rp   r   )ÚxÚyr   ÚcÚdÚer   rO   )Ú
bar_objectr   r   Úbaræ   s    zQMergeFunctionMetadataTests.test_mergedFunctionBehavesLikeMergeTarget.<locals>.barr   r   )r   r   )ZquuxN)rr   )Úobjectr   ÚmergeFunctionMetadataZassertIs©r   rC   ry   Zbazr   )rx   rq   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 rp   r   r   r   r   r   rC   ó   s    z;MergeFunctionMetadataTests.test_moduleIsMerged.<locals>.fooc                   S   s   d S rp   r   r   r   r   r   ry   ö   s    z;MergeFunctionMetadataTests.test_moduleIsMerged.<locals>.barzsomewhere.elseN)r=   r   r{   r   r|   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   rC     s    z>MergeFunctionMetadataTests.test_docstringIsMerged.<locals>.fooc                   S   s   dS )z&
            This is bar.
            Nr   r   r   r   r   ry     s    z>MergeFunctionMetadataTests.test_docstringIsMerged.<locals>.barN)r   r{   r   rI   r|   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 rp   r   r   r   r   r   rC     s    z9MergeFunctionMetadataTests.test_nameIsMerged.<locals>.fooc                   S   s   d S rp   r   r   r   r   r   ry     s    z9MergeFunctionMetadataTests.test_nameIsMerged.<locals>.barN)r   r{   r   r<   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   s   d S rp   r   r   r   r   r   rC   $  s    zGMergeFunctionMetadataTests.test_instanceDictionaryIsMerged.<locals>.foor   r   c                   S   s   d S rp   r   r   r   r   r   ry   *  s    zGMergeFunctionMetadataTests.test_instanceDictionaryIsMerged.<locals>.barr   r   N)r   rO   ru   r   r{   r   r|   r   r   r   Útest_instanceDictionaryIsMerged  s    z:MergeFunctionMetadataTests.test_instanceDictionaryIsMergedN)	r<   r=   r>   rI   r}   r~   r   r€   r�   r   r   r   r   ro   Ö   s   ro   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offendingFunctionsrf   zftwisted.python.util.OrderedDict was deprecated in Twisted 15.5.0: Use collections.OrderedDict instead.Úcategoryr   N)Ztwisted.python.utilrƒ   rg   Útest_deprecatedr   ÚDeprecationWarningrh   )r   rƒ   rj   r   r   r   r†   ;  s    
þz OrderedDictTests.test_deprecatedN)r<   r=   r>   rI   r†   r   r   r   r   r‚   6  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 )Ú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	œƒ}|  ||¡ 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}.
        ry   r   r   ©rD   r   Úfnz©ÚpreserverŽ   rC   rD   )r‹   r‰   N)r   r‰   r   ÚcopyÚgetri   ÚevalÚreprÚkeysÚitemsrh   )r   r‹   Úresultr•   rs   r   r   r   Útest_preserve\  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}.
        ry   r   r   r�   r   r�   rC   rŽ   N)r   r‰   ri   r•   r–   r   rh   )r   r‹   r•   rs   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   rD   ZFOOrC   N©r   r‰   r   r•   ©r   rv   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)	r<   r=   r>   rI   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 rp   )rŸ   r[   )r   ÚfdrŸ   r   r   r   ÚchildDataReceived§  s    z0PasswordTestingProcessProtocol.childDataReceivedc                 C   s   | j  || jf¡ d S rp   )ÚfinishedÚcallbackrŸ   )r   Úreasonr   r   r   ÚprocessEndedª  s    z+PasswordTestingProcessProtocol.processEndedN)r<   r=   r>   rI   r¢   r¤   r¨   r   r   r   r   rž   �  s   rž   z,Process support required to test getPasswordc                   @   s   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)Ztrapr	   ri   )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)r<   r=   r>   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.txtry   Zbam)ÚfilesÚdirsÚsearchupwards)r-   ÚmakedirsÚopenÚcloseÚmkdirr   r¹   r±   ÚabspathÚsepr   ÚshutilÚrmtree)r   ÚactualÚexpectedr   r   r   ÚtestSearchupwardsÕ  s.    




  ÿ
  ÿz$SearchUpwardsTests.testSearchupwardsN)r<   r=   r>   rÄ   r   r   r   r   rµ   Ô  s   rµ   c                   @   s<   e Zd Zdd„ Zdd„ Zdd„ Zd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 )Nrr   r(   ©rr   N©Úiterr   ÚIntervalDifferentialÚranger   Únext©r   rv   Úir   r   r   ÚtestDefaultì  s    z%IntervalDifferentialTests.testDefaultc                 C   s4   t t dgd¡ƒ}tdƒD ]}|  t|ƒd¡ qd S )NrQ   rr   r(   ©rQ   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 )NrQ   é   rr   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   rQ   rr   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 )Nrr   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   rQ   rr   rÓ   rÒ   rÔ   rÕ   rÏ   rÆ   )rÈ   r   rÉ   r   rË   ZremoveIntervalÚassertRaisesÚ
ValueErrorr›   r   r   r   Ú
testRemove+  s    

z$IntervalDifferentialTests.testRemoveN)	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Zdd„ ZdS )ÚRecordz8
    Trivial user of L{FancyEqMixin} used by tests.
    ©r   rO   c                 C   s   || _ || _d S rp   rß   ©r   r   rO   r   r   r   Ú__init__?  s    zRecord.__init__N©r<   r=   r>   rI   ZcompareAttributesrá   r   r   r   r   rÞ   8  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 rp   rß   rà   r   r   r   rá   K  s    zDifferentRecord.__init__Nrâ   r   r   r   r   rã   D  s   rã   c                   @   s   e Zd ZdZdS )ÚDerivedRecordz@
    A class with an inheritance relationship to L{Record}.
    N)r<   r=   r>   rI   r   r   r   r   rä   P  s   rä   c                   @   s    e Zd ZdZeedœdd„ZdS )ÚEqualToEverythingzQ
    A class the instances of which consider themselves equal to everything.
    ©ÚotherÚreturnc                 C   s   dS )NTr   ©r   rç   r   r   r   Ú__eq__[  s    zEqualToEverything.__eq__N©r<   r=   r>   rI   rz   Úboolrê   r   r   r   r   rå   V  s   rå   c                   @   s    e Zd ZdZeedœdd„ZdS )ÚEqualToNothingzN
    A class the instances of which consider themselves equal to nothing.
    ræ   c                 C   s   dS )NFr   ré   r   r   r   rê   d  s    zEqualToNothing.__eq__Nrë   r   r   r   r   rí   _  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)r<   r=   r>   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|  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)r<   r=   r>   rI   rò   ró   rô   rõ   rö   r÷   rø   rù   rú   r   r   r   r   rî   h  s   

rî   rl   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 )Nrl   ÚgetegidÚseteuidÚsetegid)r   rW   rX   r-   rl   rü   rý   rþ   r$   r   r   r   rZ   Ð  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   rW   rd   Ú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   )rs   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 rp   r   )rs   rt   Ú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)	rW   rm   Zegidr   r	  r  r   rd   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_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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_UIDGIDN)r<   r=   r>   rI   rZ   r  r  r  r  r  r  r  r  r  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 rp   )r[   )rs   rt   ©rK   r   r   ÚmockInitgroupsI  s    z?InitGroupsTests.test_initgroupsInStdlib.<locals>.mockInitgroupsÚ_initgroupsÚ	setgroupsr   N)rX   r   r[   rV   r-   r.   r   r+   r,   r/   r"   )r   r  ZsetgroupsCallsr   r  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   rV   r-   r.   r$   r   r   r   Útest_initgroupsNoneGidT  s    z&InitGroupsTests.test_initgroupsNoneGidN)r<   r=   r>   rI   r  r  r   r   r   r   r  <  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   rf   zCtwisted.python.util.getPluginDirs is deprecated since Twisted 12.2.r…   r   N)r   ZgetPluginDirsrg   Útest_getPluginDirsr   r‡   rh   ©r   rj   r   r   r   r  `  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   rf   zBtwisted.python.util.addPluginDir is deprecated since Twisted 12.2.r…   r   N)r   ZaddPluginDirrg   Útest_addPluginDirr   r‡   rh   r  r   r   r   r  o  s    ÿ
þz"DeprecationTests.test_addPluginDirz/twisted.python.util.getPluginDirs is deprecated)r…   rf   N)	r<   r=   r>   rI   r  r  ÚSUPPRESSr‡   r   r   r   r   r   r  [  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   rg   ©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 ©rf   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 rp   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   r0  ¯  s     z>SuppressedWarningsTests.test_noSideEffects.<locals>.<listcomp>Nr&  r*  r   r   r   Útest_noSideEffects§  s    
z*SuppressedWarningsTests.test_noSideEffectsN)r<   r=   r>   rI   Ústaticmethodr   r'  r,  r1  r2  r3  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©r<   r=   r>   ZshowAttributesr7  r8  r   r   r   r   rD   ½  s   rD   z<Foo first=1 second='hello'>N©r   ÚFancyStrMixinr   Ústr©r   rD   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>.Foor7  r8  c                 S   s   t | d d d… ƒS )Nrÿ   )r”   )Úvaluer   r   r   r  Í  r  z7FancyStrMixinTests.test_formatter.<locals>.Foo.<lambda>r9  ZworldNr:  r   r   r   r   rD   Ì  s   rD   z"<Foo first='hello' second='dlrow'>Nr;  r>  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)r7  )r8  Z2ndz%.1fr   gÙÎ÷Sã @Nr:  r   r   r   r   rD   Û  s   rD   z<Foo first=1 2nd=2.1>Nr;  r>  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>.FooÚBarN)r<   r=   r>   Zfancybasenamer   r   r   r   rD   ç  s   rD   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                   @   s   e Zd ZdZdZdZdS )z)FancyStrMixinTests.test_repr.<locals>.Foor6  r   r9  Nr:  r   r   r   r   rD   ñ  s   rD   N)r   r<  r   r=  r”   )r   rD   Úobjr   r   r   Ú	test_reprì  s    zFancyStrMixinTests.test_reprN)	r<   r=   r>   rI   r?  rA  rB  rD  rF  r   r   r   r   r5  ²  s   
r5  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   rs   NrH  rJ  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  NrH  rJ  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   rI  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   rI  rh   r   ©r   r–   rK  r   r   r   Útest_alreadyPadded  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   rI  rh   ZassertIsNotrP  r   r   r   Útest_alreadyPaddedCopies'  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   NrH  )r   r–   r   r   r   Útest_makeCopy0  s    zPadToTests.test_makeCopyN)r<   r=   r>   rI   rL  rM  rN  rO  rQ  rR  rS  r   r   r   r   rG  ú  s   		rG  )GrI   rN   Úos.pathr-   rÀ   r°   r(  Útypingr   r   r   r   Zunittestr   r6   Z_grpr+   Ú_pwdÚImportErrorZtwisted.internetr   Ztwisted.internet.deferr   Ztwisted.internet.errorr	   Ztwisted.internet.interfacesr
   Ztwisted.internet.protocolr   Ztwisted.pythonr   Ztwisted.python.filepathr   Ztwisted.test.test_processr   Ztwisted.trial.unittestr   r   Ztwisted.trial.utilr   r   Ú
executableZ_asBytesPathr­   r   r?   rJ   ÚgetattrrU   ro   r‚   rˆ   rž   Z
providedByr©   rµ   rÅ   rð   rÞ   rã   rä   rå   rí   rî   rû   r  r  r  r!  r5  rG  r   r   r   r   Ú<module>   sp   
1F`P
þ"M		bq+,H