o
    ¯b  ã                   @   sž   d Z ddlZddlZddlZzddlZW n ey   dZY nw 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   Ú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   ÚsetUp8   s   zReadWriteTests.setUpc                 C   sL   zt  | j¡ W n	 ty   Y nw z	t  | j¡ W dS  ty%   Y dS w )z
        Close pipes.
        N)r   r	   r   ÚOSErrorr   r   r   r   r   ÚtearDown@   s   ÿÿzReadWriteTests.tearDownc                 C   s   t  | j|¡S )z)
        Write data to the pipe.
        )r   Ú	writeToFDr   )r   Údr   r   r   ÚwriteM   s   zReadWriteTests.writec                 C   s0   g }t  | j|j¡}|du r|r|d S dS |S )z*
        Read data from the pipe.
        Nr   ó    )r   Ú
readFromFDr   Úappend)r   ÚlÚresr   r   r   ÚreadS   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_writeAndReada   s
   
z ReadWriteTests.test_writeAndReadc                 C   s¤   dd }|   |¡}|  |dk¡ g }d}d}||k s|dk r9| |  ¡ ¡ |t|d ƒ7 }|d7 }||k s|dk s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   ÚorigÚwrittenÚresultÚresultlengthÚir   r   r   Útest_writeAndReadLargel   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+   ÚassertIsNone)r   r(   r6   r   r   r   Útest_readFromEmpty€   s   z!ReadWriteTests.test_readFromEmptyc                 C   ó"   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   ÚCONNECTION_DONEr   r   r   r   Útest_readFromCleanCloseŠ   ó   z&ReadWriteTests.test_readFromCleanClosec                 C   ó$   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’   ó   z!ReadWriteTests.test_writeToClosedc                 C   r<   )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   rB   r   r   r   r   Útest_readFromInvalidš   r?   z#ReadWriteTests.test_readFromInvalidc                 C   r@   )zŠ
        Verify that writing with L{fdesc.writeToFD} when the write end is
        closed results in a connection lost indicator.
        rA   N)r   r	   r   r+   r$   r   rB   r   r   r   r   Útest_writeToInvalid¢   rD   z"ReadWriteTests.test_writeToInvalidc                 C   sp   t j}dd„ }|t _z|  |  d¡d¡ W |t _n|t _w dd„ }|t _z|  |  d¡d¡ W |t _dS |t _w )z8
        Test error path for L{fdesc.writeTod}.
        c                 S   ó   t ƒ }tj|_|‚©N)r    ÚerrnoÚEAGAIN©ÚfdÚdataÚerrr   r   r   ÚeagainWrite°   ó   z4ReadWriteTests.test_writeErrors.<locals>.eagainWriterA   r   c                 S   rG   rH   )r    rI   ÚEINTRrK   r   r   r   Ú
eintrWrite»   rP   z3ReadWriteTests.test_writeErrors.<locals>.eintrWriteN)r   r$   r+   )r   Ú
oldOsWriterO   rR   r   r   r   Útest_writeErrorsª   s   zReadWriteTests.test_writeErrorsN)r   r   r   r   r   r!   r$   r*   r/   r9   r;   r>   rC   rE   rF   rT   r   r   r   r   r   3   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 BaseException:
    os._exit(10)
else:
    os._exit(20)
c                 C   sz   t  ¡ }|dkr4zt  tjtjd| j| ¡ f g¡ W d S  ty3   dd l}| 	¡  t  
d¡ Y d S w tt j|dƒd S )Nr   z-cé   r2   )r   ÚforkÚexecvÚsysÚ
executableÚprogramÚfilenoÚBaseExceptionÚ	tracebackÚ	print_excÚ_exitr   Úwaitpid)r   ÚfObjÚpidr^   r   r   r   Ú_execWithFileDescriptorÚ   s   
þüz(CloseOnExecTests._execWithFileDescriptorc                 C   sn   t |  ¡ dƒ&}t | ¡ ¡ |  |¡}|  t |¡¡ |  	t 
|¡d¡ W d  ƒ dS 1 s0w   Y  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   Ú_setCloseOnExecr\   rd   r   r   Ú	WIFEXITEDr+   ÚWEXITSTATUS©r   rb   Ústatusr   r   r   Útest_setCloseOnExecì   s   
"üz$CloseOnExecTests.test_setCloseOnExecc                 C   s|   t |  ¡ dƒ-}t | ¡ ¡ t | ¡ ¡ |  |¡}|  t 	|¡¡ |  
t |¡d¡ W d  ƒ dS 1 s7w   Y  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.
        re   é   N)rf   rg   r   rh   r\   Ú_unsetCloseOnExecrd   r   r   ri   r+   rj   rk   r   r   r   Útest_unsetCloseOnExecø   s   
"ûz&CloseOnExecTests.test_unsetCloseOnExecN)r   r   r   r   r[   rd   rm   rp   r   r   r   r   rU   Ç   s    rU   )r   rI   r   rY   r   ÚImportErrorÚskipÚtwisted.internetr   Útwisted.python.utilr   Útwisted.trialr   ÚSynchronousTestCaser   r   rU   r   r   r   r   Ú<module>   s    ÿ 