
[XMc           @   s   d  Z  d d l Z d d l Z d d l Z y d d l Z Wn e k
 rS d Z n Xd d l m Z d d l	 m
 Z
 d d l m Z d e j f d     YZ d	 e j f d
     YZ d S(   s&   
Tests for L{twisted.internet.fdesc}.
iNs   not supported on this platform(   t   fdesc(   t   untilConcludes(   t   unittestt   ReadWriteTestCasec           B   sz   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d	   Z d
   Z d   Z d   Z RS(   s6   
    Tests for fdesc.readFromFD, fdesc.writeToFD.
    c         C   s<   t  j   \ |  _ |  _ t j |  j  t j |  j  d S(   sJ   
        Create two non-blocking pipes that can be used in tests.
        N(   t   ost   pipet   rt   wR    t   setNonBlocking(   t   self(    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_fdesc.pyt   setUp   s    c         C   sT   y t  j |  j  Wn t k
 r' n Xy t  j |  j  Wn t k
 rO n Xd S(   s   
        Close pipes.
        N(   R   t   closeR   t   OSErrorR   (   R	   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_fdesc.pyt   tearDown$   s    c         C   s   t  j |  j |  S(   s)   
        Write data to the pipe.
        (   R    t	   writeToFDR   (   R	   t   d(    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_fdesc.pyt   write2   s    c         C   sG   g  } t  j |  j | j  } | d k r? | r8 | d Sd Sn | Sd S(   s*   
        Read data from the pipe.
        i    t    N(   R    t
   readFromFDR   t   appendt   None(   R	   t   lt   res(    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_fdesc.pyt   read9   s    c         C   s\   |  j  d  } |  j | d k  |  j   } |  j t |  |  |  j d |  |  d S(   s   
        Test that the number of bytes L{fdesc.writeToFD} reports as written
        with its return value are seen by L{fdesc.readFromFD}.
        t   helloi    N(   R   t
   failUnlessR   t   assertEqualst   len(   R	   t   nt   s(    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_fdesc.pyt   test_writeAndReadH   s
    c         C   s   d d } |  j  |  } |  j | d k  g  } d } d } xM | | k  sY | d k  r | j |  j    | t | d  7} | d 7} qA Wd j |  } |  j t |  |  |  j | |  |  d S(	   s}   
        Similar to L{test_writeAndRead}, but use a much larger string to verify
        the behavior for that case.
        t
   0123456879i'  i    i2   ii   R   N(   R   R   R   R   R   t   joinR   (   R	   t   origt   writtent   resultt   resultlengtht   i(    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_fdesc.pyt   test_writeAndReadLargeT   s    
c         C   sB   g  } t  j |  j | j  } |  j | g   |  j | d  d S(   s   
        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   R   (   R	   R   R#   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_fdesc.pyt   test_readFromEmptyi   s    c         C   s-   t  j |  j  |  j |  j   t j  d S(   s   
        Test that using L{fdesc.readFromFD} on a cleanly closed file descriptor
        returns a connection done indicator.
        N(   R   R   R   R   R   R    t   CONNECTION_DONE(   R	   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_fdesc.pyt   test_readFromCleanCloset   s    c         C   s0   t  j |  j  |  j |  j d  t j  d S(   s   
        Verify that writing with L{fdesc.writeToFD} when the read end is closed
        results in a connection lost indicator.
        R   N(   R   R   R   R   R   R    t   CONNECTION_LOST(   R	   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_fdesc.pyt   test_writeToClosed}   s    c         C   s-   t  j |  j  |  j |  j   t j  d S(   s   
        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    R*   (   R	   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_fdesc.pyt   test_readFromInvalid   s    c         C   s0   t  j |  j  |  j |  j d  t j  d S(   s   
        Verify that writing with L{fdesc.writeToFD} when the write end is
        closed results in a connection lost indicator.
        R   N(   R   R   R   R   R   R    R*   (   R	   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_fdesc.pyt   test_writeToInvalid   s    c         C   s   t  j } d   } | t  _ z |  j |  j d  d  Wd | t  _ Xd   } | t  _ z |  j |  j d  d  Wd | t  _ Xd S(   s8   
        Test error path for L{fdesc.writeTod}.
        c         S   s   t    } t j | _ |  d  S(   N(   R   t   errnot   EAGAIN(   t   fdt   datat   err(    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_fdesc.pyt   eagainWrite   s    	R   i    Nc         S   s   t    } t j | _ |  d  S(   N(   R   R.   t   EINTR(   R0   R1   R2   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_fdesc.pyt
   eintrWrite   s    	(   R   R   R   (   R	   t
   oldOsWriteR3   R5   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_fdesc.pyt   test_writeErrors   s    			
		(   t   __name__t
   __module__t   __doc__R
   R   R   R   R   R&   R'   R)   R+   R,   R-   R7   (    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_fdesc.pyR      s   																t   CloseOnExecTestsc           B   s/   e  Z d  Z d Z d   Z d   Z d   Z RS(   sL   
    Tests for L{fdesc._setCloseOnExec} and L{fdesc._unsetCloseOnExec}.
    s   
import os, errno
try:
    os.write(%d, 'lul')
except OSError, e:
    if e.errno == errno.EBADF:
        os._exit(0)
    os._exit(5)
except:
    os._exit(10)
else:
    os._exit(20)
c         C   s   t  j   } | d k r{ y3 t  j t j t j d |  j | j   f g  Wq d d  l } | j   t  j	 d  q Xn t
 t  j | d  d Sd  S(   Ni    s   -cii   i   (   R   t   forkt   execvt   syst
   executablet   programt   filenot	   tracebackt	   print_exct   _exitR   t   waitpid(   R	   t   fObjt   pidRB   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_fdesc.pyt   _execWithFileDescriptor   s    3
c         C   sj   t  |  j   d  } t j | j    |  j |  } |  j t j |   |  j	 t j
 |  d  d S(   s   
        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.
        R   i    N(   t   filet   mktempR    t   _setCloseOnExecRA   RH   t
   assertTrueR   t	   WIFEXITEDt   assertEqualt   WEXITSTATUS(   R	   RF   t   status(    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_fdesc.pyt   test_setCloseOnExec   s
    c         C   s}   t  |  j   d  } t j | j    t j | j    |  j |  } |  j t j	 |   |  j
 t j |  d  d S(   s   
        A file descriptor passed to L{fdesc._unsetCloseOnExec} is inherited by
        a new process image created with one of the exec family of functions.
        R   i   N(   RI   RJ   R    RK   RA   t   _unsetCloseOnExecRH   RL   R   RM   RN   RO   (   R	   RF   RP   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_fdesc.pyt   test_unsetCloseOnExec   s    (   R8   R9   R:   R@   RH   RQ   RS   (    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_fdesc.pyR;      s
   		(   R:   R   R>   R.   t   fcntlt   ImportErrort   skipt   twisted.internetR    t   twisted.python.utilR   t   twisted.trialR   t   TestCaseR   R;   (    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_fdesc.pyt   <module>   s   
