ó
[³XMc           @   si  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
 m Z m Z m Z d d l m Z d d l m Z d d l m Z m Z d e	 j f d „  ƒ  YZ d	 e j f d
 „  ƒ  YZ d e	 j f d „  ƒ  YZ d e	 j f d „  ƒ  YZ d e j f d „  ƒ  YZ e j e d ƒ sGd e _  n  e j! e d ƒ sed e _  n  d S(   sK   
Tests for implementations of L{IReactorUNIX} and L{IReactorUNIXDatagram}.
iÿÿÿÿN(   t
   interfacest   reactort   protocolt   errort   addresst   defert   utils(   t   lockfile(   t   unittest(   t   MyServerFactoryt   MyClientFactoryt   FailedConnectionClientFactoryc           B   s   e  Z d  „  Z d „  Z RS(   c         C   s   | |  _  d  S(   N(   t   onFail(   t   selfR   (    (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyt   __init__   s    c         C   s   |  j  j | ƒ d  S(   N(   R   t   errback(   R   t	   connectort   reason(    (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyt   clientConnectionFailed   s    (   t   __name__t
   __module__R   R   (    (    (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyR      s   	t   UnixSocketTestCasec           B   sh   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z RS(   s   
    Test unix sockets.
    c            s¾   ˆ  j  ƒ  } ˆ  j  ƒ  ‰ t ƒ  ‰ t j ƒ  } ˆ _ t j | ˆ ƒ } ˆ  j | j ƒ t	 j	 t	 j
 t	 j ƒ } ˆ  j | j ƒ | j ˆ ƒ | j | ƒ ‡  ‡ ‡ f d †  } | j | ƒ | S(   sè   
        The address passed to the server factory's C{buildProtocol} method and
        the address returned by the connected protocol's transport's C{getPeer}
        method match the address the client socket is bound to.
        c            sB   t  j ˆ ƒ } ˆ  j ˆ j | g ƒ ˆ  j |  j j ƒ  | ƒ d  S(   N(   R   t   UNIXAddresst   assertEqualt   peerAddressest	   transportt   getPeer(   t   protot   expected(   R   t   peernamet   serverFactory(    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyt
   cbConnMade/   s    (   t   mktempR	   R   t   Deferredt   protocolConnectionMadeR   t
   listenUNIXt
   addCleanupt   stopListeningt   sockett   AF_UNIXt   SOCK_STREAMt   closet   bindt   connectt   addCallback(   R   t   filenamet   connMadet   unixPortt
   unixSocketR   (    (   R   R   R   s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyt   test_peerBind   s    	c            s·   ˆ  j  ƒ  ‰ t ƒ  } t j ƒ  } | | _ t j ˆ | ƒ } ˆ  j | j ƒ t	 ƒ  ‰ t j ƒ  } | ˆ _ t j
 ˆ ˆ ƒ } t j | | g ƒ } ‡  ‡ ‡ f d †  } | j | ƒ | S(   sŠ   
        L{IReactorUNIX.connectUNIX} can be used to connect a client to a server
        started with L{IReactorUNIX.listenUNIX}.
        c            sI   |  \ } } ˆ  j  ˆ j t j ˆ ƒ g ƒ | j j ƒ  | j j ƒ  d  S(   N(   R   R   R   R   R   t   loseConnection(   t   .0t   serverProtocolt   clientProtocol(   R   R-   t   clientFactory(    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyt   allConnectedG   s
    	(   R    R	   R   R!   R"   R   R#   R$   R%   R
   t   connectUNIXt   gatherResultsR,   (   R   R   t   serverConnMadeR/   t   clientConnMadet   ct   dR7   (    (   R   R-   R6   s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyt   test_dumber7   s    					c            sï   ˆ  j  ƒ  ‰ t ƒ  } t j ƒ  } | | _ t j ˆ | d t ƒ‰ ˆ  j t	 j
 ˆ d ƒ ƒ t ƒ  ‰ t j ƒ  } | ˆ _ t j ˆ ˆ d d ƒ} t j | | g ƒ } ‡ ‡  ‡ ‡ f d †  } | j | ƒ ‡  ‡ f d †  } | j | ƒ | S(   sà   
        A lockfile is created and locked when L{IReactorUNIX.listenUNIX} is
        called and released when the Deferred returned by the L{IListeningPort}
        provider's C{stopListening} method is called back.
        t   wantPIDs   .lockt   checkPIDi   c            sO   |  \ } } ˆ j  ˆ j t j ˆ ƒ g ƒ | j j ƒ  | j j ƒ  ˆ  j ƒ  S(   N(   R   R   R   R   R   R2   R%   (   R3   R4   t   clientProto(   R/   R   R-   R6   (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyt
   _portStuffj   s    	c            s!   ˆ  j  t j ˆ d ƒ d ƒ d  S(   Ns   .lockt   locked(   t   failIfR   t   isLocked(   t   ignored(   R   R-   (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyt   _checkv   s    (   R    R	   R   R!   R"   R   R#   t   Truet
   assertTrueR   RE   R
   R8   R9   R,   (   R   R   R:   R;   R<   R=   RB   RG   (    (   R   R-   R6   R/   s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyt   test_pidFileT   s     				
c            st   |  j  ƒ  ‰  t ƒ  ‰ t j ˆ  ˆ d t ƒ} |  j t j t j ˆ  ˆ d t ƒ‡  ‡ f d †  } | j ƒ  j	 | ƒ S(   s›   
        L{IReactorUNIX.listenUNIX} raises L{error.CannotListenError} if passed
        the name of a file on which a server is already listening.
        R?   c            s"   t  j ˆ  ˆ d t ƒ} | j ƒ  S(   NR?   (   R   R#   RH   R%   (   t   ignR/   (   R-   R   (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyt   stoppedListening‰   s    (
   R    R	   R   R#   RH   t   assertRaisesR   t   CannotListenErrorR%   R,   (   R   R/   RL   (    (   R-   R   s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyt   test_socketLocking|   s    	c         C   sp   |  j  ƒ  |  _ d |  j f } i t j j t j ƒ d 6} t j t j	 d d | f d | ƒ} | j
 | ƒ | S(   Nsn   from twisted.internet import protocol, reactor
reactor.listenUNIX(%r, protocol.ServerFactory(), wantPID=True)
t
   PYTHONPATHs   -us   -ct   env(   R    R-   t   ost   pathsept   joint   syst   pathR   t   getProcessValuet
   executableR,   (   R   t   callbackt   sourceRQ   R=   (    (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyt   _uncleanSocketTest   s    $c            s   ‡  f d †  } ˆ  j  | ƒ S(   s"  
        If passed C{True} for the C{wantPID} parameter, a server can be started
        listening with L{IReactorUNIX.listenUNIX} when passed the name of a
        file on which a previous server which has not exited cleanly has been
        listening using the C{wantPID} option.
        c            s(   t  j ˆ  j t ƒ  d t ƒ} | j ƒ  S(   NR?   (   R   R#   R-   R	   RH   R%   (   RK   t   p(   R   (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyt   ranStupidChild¢   s    (   R[   (   R   R]   (    (   R   s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyt   test_uncleanServerSocketLocking›   s    c            s   ‡  f d †  } ˆ  j  | ƒ S(   s¶   
        If passed C{True} for the C{checkPID} parameter, a client connection
        attempt made with L{IReactorUNIX.connectUNIX} fails with
        L{error.BadFileError}.
        c            sF   t  j ƒ  } t | ƒ } t j ˆ  j | d t ƒ} ˆ  j | t j	 ƒ S(   NR@   (
   R   R!   R   R   R8   R-   RH   t   assertFailureR   t   BadFileError(   RK   R=   t   fR<   (   R   (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyR]   ¯   s    (   R[   (   R   R]   (    (   R   s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyt   test_connectToUncleanServer©   s    c            s’   ˆ  j  ƒ  } t j | | ƒ ‰ d ˆ | f } ˆ  j t ˆ ƒ | ƒ ˆ  j t ˆ ƒ | ƒ t j ˆ j ƒ } ‡  ‡ ‡ f d †  } | j	 | ƒ | S(   s~   
        Test the C{__str__} and C{__repr__} implementations of a UNIX port when
        used with the given factory.
        s
   <%s on %r>c            s=   d ˆ f } ˆ  j  t ˆ ƒ | ƒ ˆ  j  t ˆ ƒ | ƒ d  S(   Ns   <%s (not listening)>(   R   t   reprt   str(   RK   t   unconnectedString(   R   t   factoryNameR/   (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyRL   Ä   s    (
   R    R   R#   R   Rc   Rd   R   t   maybeDeferredR%   R,   (   R   R   Rf   R-   t   connectedStringR=   RL   (    (   R   Rf   R/   s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyt	   _reprTest·   s    c         C   s9   d d d „  ƒ  Y} |  j  | t j ƒ |  j | ƒ  d ƒ S(   s  
        The two string representations of the L{IListeningPort} returned by
        L{IReactorUNIX.listenUNIX} contains the name of the classic factory
        class being used and the filename on which the port is listening or
        indicates that the port is not listening.
        t   ClassicFactoryc           B   s   e  Z d  „  Z d „  Z RS(   c         S   s   d  S(   N(    (   R   (    (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyt   doStartÔ   s    c         S   s   d  S(   N(    (   R   (    (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyt   doStop×   s    (   R   R   Rk   Rl   (    (    (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyRj   Ó   s   	s%   twisted.test.test_unix.ClassicFactory(    (   t   assertIsInstancet   typest	   ClassTypeRi   (   R   Rj   (    (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyt   test_reprWithClassicFactoryÌ   s    c         C   s9   d t  f d „  ƒ  Y} |  j | t ƒ |  j | ƒ  d ƒ S(   s!  
        The two string representations of the L{IListeningPort} returned by
        L{IReactorUNIX.listenUNIX} contains the name of the new-style factory
        class being used and the filename on which the port is listening or
        indicates that the port is not listening.
        t   NewStyleFactoryc           B   s   e  Z d  „  Z d „  Z RS(   c         S   s   d  S(   N(    (   R   (    (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyRk   é   s    c         S   s   d  S(   N(    (   R   (    (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyRl   ì   s    (   R   R   Rk   Rl   (    (    (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyRq   è   s   	s&   twisted.test.test_unix.NewStyleFactory(   t   objectRm   t   typeRi   (   R   Rq   (    (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyt   test_reprWithNewStyleFactoryá   s    (   R   R   t   __doc__R1   R>   RJ   RO   R[   R^   Rb   Ri   Rp   Rt   (    (    (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyR      s   			(						t   ClientProtoc           B   s<   e  Z e Z Z d Z d  „  Z d „  Z d „  Z	 d „  Z
 RS(   c         C   s"   t  j ƒ  |  _ t  j ƒ  |  _ d  S(   N(   R   R!   t   deferredStartedt   deferredGotBack(   R   (    (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyR   û   s    c         C   s   t  |  _ d  S(   N(   RH   t   stopped(   R   (    (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyt   stopProtocolÿ   s    c         C   s   t  |  _ |  j j d  ƒ d  S(   N(   RH   t   startedRw   RY   t   None(   R   (    (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyt   startProtocol  s    	c         C   s   | |  _  |  j j d  ƒ d  S(   N(   t   gotbackRx   RY   R|   (   R   t   data(    (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyt   datagramReceived  s    	N(   R   R   t   FalseR{   Ry   R|   R~   R   Rz   R}   R€   (    (    (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyRv   ÷   s   
			t   ServerProtoc           B   s@   e  Z e Z Z d Z Z d  „  Z d „  Z	 d „  Z
 d „  Z RS(   c         C   s"   t  j ƒ  |  _ t  j ƒ  |  _ d  S(   N(   R   R!   Rw   t   deferredGotWhat(   R   (    (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyR     s    c         C   s   t  |  _ d  S(   N(   RH   Ry   (   R   (    (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyRz     s    c         C   s   t  |  _ |  j j d  ƒ d  S(   N(   RH   R{   Rw   RY   R|   (   R   (    (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyR}     s    	c         C   s9   | |  _  |  j j d | ƒ | |  _ |  j j d  ƒ d  S(   Ns   hi back(   t   gotfromR   t   writet   gotwhatRƒ   RY   R|   (   R   R   t   addr(    (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyR€     s    		N(   R   R   R   R{   Ry   R|   R†   R„   R   Rz   R}   R€   (    (    (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyR‚   
  s   

			t   DatagramUnixSocketTestCasec           B   s;   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s%   
    Test datagram UNIX sockets.
    c            s×   ˆ j  ƒ  ‰  ˆ j  ƒ  } t ƒ  ‰ t ƒ  ‰ t j | ˆ ƒ } ˆ j | j ƒ t j | ˆ d ˆ  ƒ} ˆ j | j ƒ t j	 ˆ j
 ˆ j
 g ƒ } ‡ ‡ f d †  } ‡ ‡ ‡ ‡  f d †  } | j | ƒ | j | ƒ | S(   sf   
        Test that a datagram can be sent to and received by a server and vice
        versa.
        t   bindAddressc            s)   ˆ  j  j d ƒ t j ˆ j ˆ  j g ƒ S(   Nt   hi(   R   R…   R   R9   Rƒ   Rx   (   RF   (   t   cpt   sp(    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyR…   4  s    c            s=   ˆ j  d ˆ j ƒ ˆ j  ˆ ˆ j ƒ ˆ j  d ˆ  j ƒ d  S(   NRŠ   s   hi back(   t   failUnlessEqualR†   R„   R~   (   RF   (   R‹   R   RŒ   t
   clientaddr(    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyt   _cbTestExchange9  s    (   R    R‚   Rv   R   t   listenUNIXDatagramR$   R%   t   connectUNIXDatagramR   R9   Rw   R,   (   R   t
   serveraddrt   sR<   R=   R…   R   (    (   RŽ   R   RŒ   R‹   s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyt   test_exchange%  s    		c         C   s^   |  j  ƒ  } t ƒ  } t j | | ƒ } |  j t j t j | | ƒ | j ƒ  t j	 | ƒ d S(   sž   
        L{IReactorUNIXDatagram.listenUNIXDatagram} raises
        L{error.CannotListenError} if the unix socket specified is already in
        use.
        N(
   R    R‚   R   R   t   failUnlessRaisesR   RN   R%   RR   t   unlink(   R   R‡   R\   R“   (    (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyt   test_cannotListenC  s    	
c            s’   ˆ  j  ƒ  } t j | | ƒ ‰ d ˆ | f } ˆ  j t ˆ ƒ | ƒ ˆ  j t ˆ ƒ | ƒ t j ˆ j ƒ } ‡  ‡ ‡ f d †  } | j	 | ƒ | S(   sˆ   
        Test the C{__str__} and C{__repr__} implementations of a UNIX datagram
        port when used with the given protocol.
        s
   <%s on %r>c            s=   d ˆ f } ˆ  j  t ˆ ƒ | ƒ ˆ  j  t ˆ ƒ | ƒ d  S(   Ns   <%s (not listening)>(   R   Rc   Rd   (   RK   Re   (   R   t   protocolNameR/   (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyRL   _  s    (
   R    R   R   R   Rc   Rd   R   Rg   R%   R,   (   R   t   serverProtoR˜   R-   Rh   t   stopDeferredRL   (    (   R   R/   R˜   s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyRi   R  s    c         C   s9   d d d „  ƒ  Y} |  j  | t j ƒ |  j | ƒ  d ƒ S(   s0  
        The two string representations of the L{IListeningPort} returned by
        L{IReactorUNIXDatagram.listenUNIXDatagram} contains the name of the
        classic protocol class being used and the filename on which the port is
        listening or indicates that the port is not listening.
        t   ClassicProtocolc           B   s   e  Z d  „  Z d „  Z RS(   c         S   s   d  S(   N(    (   R   R   (    (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyt   makeConnectiono  s    c         S   s   d  S(   N(    (   R   (    (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyRl   r  s    (   R   R   Rœ   Rl   (    (    (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyR›   n  s   	s&   twisted.test.test_unix.ClassicProtocol(    (   Rm   Rn   Ro   Ri   (   R   R›   (    (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyt   test_reprWithClassicProtocolg  s    c         C   s9   d t  f d „  ƒ  Y} |  j | t ƒ |  j | ƒ  d ƒ S(   s2  
        The two string representations of the L{IListeningPort} returned by
        L{IReactorUNIXDatagram.listenUNIXDatagram} contains the name of the
        new-style protocol class being used and the filename on which the port
        is listening or indicates that the port is not listening.
        t   NewStyleProtocolc           B   s   e  Z d  „  Z d „  Z RS(   c         S   s   d  S(   N(    (   R   R   (    (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyRœ   „  s    c         S   s   d  S(   N(    (   R   (    (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyRl   ‡  s    (   R   R   Rœ   Rl   (    (    (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyRž   ƒ  s   	s'   twisted.test.test_unix.NewStyleProtocol(   Rr   Rm   Rs   Ri   (   R   Rž   (    (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyt   test_reprWithNewStyleProtocol|  s    (   R   R   Ru   R”   R—   Ri   R   RŸ   (    (    (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyRˆ   !  s   				s1   This reactor does not support UNIX domain socketss3   This reactor does not support UNIX datagram sockets("   Ru   t   statRR   RU   Rn   R&   t   twisted.internetR    R   R   R   R   R   R   t   twisted.pythonR   t   twisted.trialR   t   twisted.test.test_tcpR	   R
   t   ClientFactoryR   t   TestCaseR   t   ConnectedDatagramProtocolRv   t   DatagramProtocolR‚   Rˆ   t   IReactorUNIXR|   t   skipt   IReactorUNIXDatagram(    (    (    s:   /usr/lib/python2.7/dist-packages/twisted/test/test_unix.pyt   <module>   s   04	Üq