U
    ¹êW[Ë  ã                   @   s    d Z ddlZddlZddlZzddlZW n ek
r@   dZY nX ddlmZ ddl	m
Z
 ddlmZ G dd„ dejƒZG d	d
„ d
ejƒZG dd„ dejƒZdS )z&
Tests for L{twisted.internet.fdesc}.
é    Nznot supported on this platform)Úfdesc)ÚuntilConcludes)Úunittestc                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚNonBlockingTestszE
    Tests for L{fdesc.setNonBlocking} and L{fdesc.setBlocking}.
    c                 C   sj   t  ¡ \}}|  t j|¡ |  t j|¡ |  t |tj¡t j@ ¡ t 	|¡ |  
t |tj¡t j@ ¡ dS )zR
        L{fdesc.setNonBlocking} sets a file description to non-blocking.
        N)ÚosÚpipeÚ
addCleanupÚcloseÚassertFalseÚfcntlÚF_GETFLÚ
O_NONBLOCKr   ÚsetNonBlockingÚ
assertTrue©ÚselfÚrÚw© r   ú9/usr/lib/python3/dist-packages/twisted/test/test_fdesc.pyÚtest_setNonBlocking   s    
z$NonBlockingTests.test_setNonBlockingc                 C   sZ   t  ¡ \}}|  t j|¡ |  t j|¡ t |¡ t |¡ |  t |tj	¡t j
@ ¡ dS )zK
        L{fdesc.setBlocking} sets a file description to blocking.
        N)r   r   r   r	   r   r   ZsetBlockingr
   r   r   r   r   r   r   r   Útest_setBlocking(   s    

z!NonBlockingTests.test_setBlockingN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r      s   r   c                   @   sp   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d„ Zdd„ ZdS )ÚReadWriteTestsz<
    Tests for L{fdesc.readFromFD}, L{fdesc.writeToFD}.
    c                 C   s,   t  ¡ \| _| _t | j¡ t | j¡ dS )zG
        Create a non-blocking pipe that can be used in tests.
        N)r   r   r   r   r   r   ©r   r   r   r   ÚsetUp:   s    zReadWriteTests.setUpc                 C   sP   zt  | j¡ W n tk
r$   Y nX zt  | j¡ W n tk
rJ   Y nX dS )z
        Close pipes.
        N)r   r	   r   ÚOSErrorr   r   r   r   r   ÚtearDownC   s    zReadWriteTests.tearDownc                 C   s   t  | j|¡S )z)
        Write data to the pipe.
        )r   Z	writeToFDr   )r   Údr   r   r   ÚwriteQ   s    zReadWriteTests.writec                 C   s6   g }t  | j|j¡}|dkr.|r(|d S dS n|S dS )z*
        Read data from the pipe.
        Nr   ó    )r   Ú
readFromFDr   Úappend)r   ÚlÚresr   r   r   ÚreadX   s    zReadWriteTests.readc                 C   sH   |   d¡}|  |dk¡ |  ¡ }|  t|ƒ|¡ |  dd|… |¡ dS )z”
        Test that the number of bytes L{fdesc.writeToFD} reports as written
        with its return value are seen by L{fdesc.readFromFD}.
        s   hellor   N)r"   r   r(   ÚassertEqualÚlen)r   ÚnÚsr   r   r   Útest_writeAndReadg   s
    
z ReadWriteTests.test_writeAndReadc                 C   s–   dd }|   |¡}|  |dk¡ g }d}d}||k s<|dk rd| |  ¡ ¡ |t|d ƒ7 }|d7 }q,d |¡}|  t|ƒ|¡ |  |d|… |¡ dS )	z}
        Similar to L{test_writeAndRead}, but use a much larger string to verify
        the behavior for that case.
        s
   0123456879i'  r   é2   éÿÿÿÿé   r#   N)r"   r   r%   r(   r*   Újoinr)   )r   ZorigZwrittenÚresultZresultlengthÚir   r   r   Útest_writeAndReadLarges   s    


z%ReadWriteTests.test_writeAndReadLargec                 C   s.   g }t  | j|j¡}|  |g ¡ |  |¡ dS )z¨
        Verify that reading from a file descriptor with no data does not raise
        an exception and does not result in the callback function being called.
        N)r   r$   r   r%   r)   ZassertIsNone)r   r&   r2   r   r   r   Útest_readFromEmptyˆ   s    z!ReadWriteTests.test_readFromEmptyc                 C   s"   t  | j¡ |  |  ¡ tj¡ dS )z†
        Test that using L{fdesc.readFromFD} on a cleanly closed file descriptor
        returns a connection done indicator.
        N)r   r	   r   r)   r(   r   ZCONNECTION_DONEr   r   r   r   Útest_readFromCleanClose“   s    z&ReadWriteTests.test_readFromCleanClosec                 C   s$   t  | j¡ |  |  d¡tj¡ dS )z‰
        Verify that writing with L{fdesc.writeToFD} when the read end is closed
        results in a connection lost indicator.
        ó   sN)r   r	   r   r)   r"   r   ÚCONNECTION_LOSTr   r   r   r   Útest_writeToClosedœ   s    z!ReadWriteTests.test_writeToClosedc                 C   s"   t  | j¡ |  |  ¡ tj¡ dS )zŠ
        Verify that reading with L{fdesc.readFromFD} when the read end is
        closed results in a connection lost indicator.
        N)r   r	   r   r)   r(   r   r8   r   r   r   r   Útest_readFromInvalid¥   s    z#ReadWriteTests.test_readFromInvalidc                 C   s$   t  | j¡ |  |  d¡tj¡ dS )zŠ
        Verify that writing with L{fdesc.writeToFD} when the write end is
        closed results in a connection lost indicator.
        r7   N)r   r	   r   r)   r"   r   r8   r   r   r   r   Útest_writeToInvalid®   s    z"ReadWriteTests.test_writeToInvalidc                 C   sf   t j}dd„ }|t _z|  |  d¡d¡ W 5 |t _X dd„ }|t _z|  |  d¡d¡ W 5 |t _X dS )z8
        Test error path for L{fdesc.writeTod}.
        c                 S   s   t ƒ }tj|_|‚d S ©N)r   ÚerrnoZEAGAIN©ÚfdÚdataÚerrr   r   r   ÚeagainWrite¼   s    z4ReadWriteTests.test_writeErrors.<locals>.eagainWriter7   r   c                 S   s   t ƒ }tj|_|‚d S r<   )r   r=   ZEINTRr>   r   r   r   Ú
eintrWriteÆ   s    z3ReadWriteTests.test_writeErrors.<locals>.eintrWriteN)r   r"   r)   )r   Z
oldOsWriterB   rC   r   r   r   Útest_writeErrors·   s    zReadWriteTests.test_writeErrorsN)r   r   r   r   r   r    r"   r(   r-   r4   r5   r6   r9   r:   r;   rD   r   r   r   r   r   5   s   					r   c                   @   s,   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	S )
ÚCloseOnExecTestszL
    Tests for L{fdesc._setCloseOnExec} and L{fdesc._unsetCloseOnExec}.
    z¸
import os, errno
try:
    os.write(%d, b'lul')
except OSError as e:
    if e.errno == errno.EBADF:
        os._exit(0)
    os._exit(5)
except:
    os._exit(10)
else:
    os._exit(20)
c                 C   sv   t  ¡ }|dkr`z&t  tjtjd| j| ¡ f g¡ W qr   dd l}| ¡  t  	d¡ Y qrX nt
t j|dƒd S d S )Nr   z-cé   r0   )r   ÚforkÚexecvÚsysÚ
executableÚprogramÚfilenoÚ	tracebackÚ	print_excÚ_exitr   Úwaitpid)r   ÚfObjÚpidrM   r   r   r   Ú_execWithFileDescriptorä   s    &z(CloseOnExecTests._execWithFileDescriptorc              	   C   sX   t |  ¡ dƒ@}t | ¡ ¡ |  |¡}|  t |¡¡ |  	t 
|¡d¡ W 5 Q R X dS )z°
        A file descriptor passed to L{fdesc._setCloseOnExec} is not inherited
        by a new process image created with one of the exec family of
        functions.
        Úwbr   N)ÚopenÚmktempr   Ú_setCloseOnExecrL   rS   r   r   Ú	WIFEXITEDr)   ÚWEXITSTATUS©r   rQ   Zstatusr   r   r   Útest_setCloseOnExecó   s
    
z$CloseOnExecTests.test_setCloseOnExecc              	   C   sf   t |  ¡ dƒN}t | ¡ ¡ t | ¡ ¡ |  |¡}|  t 	|¡¡ |  
t |¡d¡ W 5 Q R X dS )z¦
        A file descriptor passed to L{fdesc._unsetCloseOnExec} is inherited by
        a new process image created with one of the exec family of functions.
        rT   é   N)rU   rV   r   rW   rL   Z_unsetCloseOnExecrS   r   r   rX   r)   rY   rZ   r   r   r   Útest_unsetCloseOnExec   s    
z&CloseOnExecTests.test_unsetCloseOnExecN)r   r   r   r   rK   rS   r[   r]   r   r   r   r   rE   Ò   s
   rE   )r   r   rI   r=   r   ÚImportErrorÚskipZtwisted.internetr   Ztwisted.python.utilr   Ztwisted.trialr   ZSynchronousTestCaser   r   rE   r   r   r   r   Ú<module>   s   
 