U
     T‚g  ã                   @   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   ú;/tmp/pip-unpacked-wheel-1_7di8ic/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   ÚsetUp8   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   ÚtearDown@   s    zReadWriteTests.tearDownc                 C   s   t  | j|¡S )z)
        Write data to the pipe.
        )r   Z	writeToFDr   )r   Údr   r   r   ÚwriteM   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   Ú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 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   ÚorigZwrittenÚresultZ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)   ZassertIsNone)r   r&   r3   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   r9   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.
        r8   N)r   r	   r   r)   r"   r   r9   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   ÚerrnoÚEAGAIN©ÚfdÚdataÚerrr   r   r   ÚeagainWrite°   s    z4ReadWriteTests.test_writeErrors.<locals>.eagainWriter8   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
oldOsWriterD   rE   r   r   r   Útest_writeErrorsª   s    zReadWriteTests.test_writeErrorsN)r   r   r   r   r   r    r"   r(   r-   r5   r6   r7   r:   r;   r<   rF   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   s~   t  ¡ }|dkrhz&t  tjtjd| j| ¡ f g¡ W qz tk
rd   dd l}| 	¡  t  
d¡ Y qzX ntt j|dƒd S d S )Nr   z-cé   r0   )r   ÚforkÚexecvÚsysÚ
executableÚprogramÚfilenoÚBaseExceptionÚ	tracebackÚ	print_excÚ_exitr   Úwaitpid)r   ÚfObjÚpidrP   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   Ú_setCloseOnExecrN   rV   r   r   Ú	WIFEXITEDr)   ÚWEXITSTATUS©r   rT   Ú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.
        rW   é   N)rX   rY   r   rZ   rN   Z_unsetCloseOnExecrV   r   r   r[   r)   r\   r]   r   r   r   Útest_unsetCloseOnExecø   s    
z&CloseOnExecTests.test_unsetCloseOnExecN)r   r   r   r   rM   rV   r_   ra   r   r   r   r   rG   Ç   s
   rG   )r   r>   r   rK   r   ÚImportErrorÚskipZtwisted.internetr   Ztwisted.python.utilr   Ztwisted.trialr   ZSynchronousTestCaser   r   rG   r   r   r   r   Ú<module>   s   
 