ó
[³XMc           @   s¸   d  Z  d d l m Z d d l m Z d d l m Z m Z d d l m	 Z	 d d l
 m Z d d l m Z m Z d d d	 „  ƒ  YZ d
 e e	 f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   s)   
Tests for L{twisted.internet.protocol}.
iÿÿÿÿ(   t   Failure(   t   CancelledError(   t   Protocolt   ClientCreator(   t   Clock(   t   TestCase(   t   MemoryReactort   StringTransportt   MemoryConnectorc           B   s   e  Z e Z d  „  Z RS(   c         C   s   t  |  _ d  S(   N(   t   Truet   _disconnected(   t   self(    (    sG   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_protocol.pyt
   disconnect   s    (   t   __name__t
   __module__t   FalseR
   R   (    (    (    sG   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_protocol.pyR      s   t"   MemoryReactorWithConnectorsAndTimec           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   ss   
    An extension of L{MemoryReactor} which returns L{IConnector}
    providers from its C{connectTCP} method.
    c         C   s'   t  j |  ƒ t j |  ƒ g  |  _ d  S(   N(   R   t   __init__R   t
   connectors(   R   (    (    sG   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_protocol.pyR      s    c         O   s0   t  j |  | | Ž t ƒ  } |  j j | ƒ | S(   N(   R   t
   connectTCPR   R   t   append(   R   t   at   kwt	   connector(    (    sG   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_protocol.pyR   $   s    	c         O   s0   t  j |  | | Ž t ƒ  } |  j j | ƒ | S(   N(   R   t   connectUNIXR   R   R   (   R   R   R   R   (    (    sG   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_protocol.pyR   +   s    	c         O   s0   t  j |  | | Ž t ƒ  } |  j j | ƒ | S(   N(   R   t
   connectSSLR   R   R   (   R   R   R   R   (    (    sG   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_protocol.pyR   2   s    	(   R   R   t   __doc__R   R   R   R   (    (    (    sG   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_protocol.pyR      s
   			t   ClientCreatorTestsc           B   sž   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 d „  Z d „  Z d „  Z d „  Z RS(   s?   
    Tests for L{twisted.internet.protocol.ClientCreator}.
    c         C   s`   d t  f d „  ƒ  Y} t ƒ  } t | | ƒ } | | | ƒ } | j d ƒ } |  j | | ƒ d S(   s  
        Helper for implementing a test to verify that one of the I{connect}
        methods of L{ClientCreator} passes the right arguments to the right
        reactor method.

        @param check: A function which will be invoked with a reactor and a
            L{ClientCreator} instance and which should call one of the
            L{ClientCreator}'s I{connect} methods and assert that all of its
            arguments except for the factory are passed on as expected to the
            reactor.  The factory should be returned.
        t   SomeProtocolc           B   s   e  Z RS(    (   R   R   (    (    (    sG   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_protocol.pyR   J   s   N(   R   R   R   t   buildProtocolt   Nonet   assertIsInstance(   R   t   checkR   t   reactort   cct   factoryt   protocol(    (    sG   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_protocol.pyt   _basicConnectTest>   s    	c            s    ‡  f d †  } ˆ  j  | ƒ d S(   sä   
        L{ClientCreator.connectTCP} calls C{reactor.connectTCP} with the host
        and port information passed to it, and with a factory which will
        construct the protocol passed to L{ClientCreator.__init__}.
        c            sx   | j  d d d d ƒ |  j j ƒ  \ } } } } } ˆ  j | d ƒ ˆ  j | d ƒ ˆ  j | d ƒ ˆ  j | d ƒ | S(   Ns   example.comiÒ  iá  s   1.2.3.4i”&  (   s   1.2.3.4i”&  (   s   1.2.3.4i”&  (   R   t
   tcpClientst   popt   assertEquals(   R!   R"   t   hostt   portR#   t   timeoutt   bindAddress(   R   (    sG   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_protocol.pyR    Z   s    N(   R%   (   R   R    (    (   R   sG   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_protocol.pyt   test_connectTCPT   s    c            s    ‡  f d †  } ˆ  j  | ƒ d S(   sÕ   
        L{ClientCreator.connectUNIX} calls C{reactor.connectUNIX} with the
        filename passed to it, and with a factory which will construct the
        protocol passed to L{ClientCreator.__init__}.
        c            sb   | j  d d t ƒ |  j j ƒ  \ } } } } ˆ  j | d ƒ ˆ  j | d ƒ ˆ  j | t ƒ | S(   Ns   /foo/bari{   (   R   R	   t   unixClientsR'   R(   (   R!   R"   t   addressR#   R+   t   checkPID(   R   (    sG   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_protocol.pyR    k   s    N(   R%   (   R   R    (    (   R   sG   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_protocol.pyt   test_connectUNIXe   s    c            s    ‡  f d †  } ˆ  j  | ƒ d S(   sê   
        L{ClientCreator.connectSSL} calls C{reactor.connectSSL} with the host,
        port, and context factory passed to it, and with a factory which will
        construct the protocol passed to L{ClientCreator.__init__}.
        c   	         s—   t  ƒ  } | j d d | d d ƒ |  j j ƒ  \ } } } } } } ˆ  j | d ƒ ˆ  j | d ƒ ˆ  j | | ƒ ˆ  j | d ƒ ˆ  j | d ƒ | S(   Ns   example.comiÒ  iá  s   4.3.2.1i.  (   s   4.3.2.1i.  (   s   4.3.2.1i.  (   t   objectR   t
   sslClientsR'   R(   t   assertIdentical(	   R!   R"   t   expectedContextFactoryR)   R*   R#   t   contextFactoryR+   R,   (   R   (    sG   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_protocol.pyR    {   s    	!N(   R%   (   R   R    (    (   R   sG   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_protocol.pyt   test_connectSSLu   s    
c         C   sm   t  ƒ  } t | t ƒ } | | ƒ } | j j ƒ  } |  j | j ƒ | j ƒ  |  j | j ƒ |  j	 | t
 ƒ S(   s  
        Helper for implementing a test to verify that cancellation of the
        L{Deferred} returned by one of L{ClientCreator}'s I{connect} methods is
        implemented to cancel the underlying connector.

        @param connect: A function which will be invoked with a L{ClientCreator}
            instance as an argument and which should call one its I{connect}
            methods and return the result.

        @return: A L{Deferred} which fires when the test is complete or fails if
            there is a problem.
        (   R   R   R   R   R'   t   assertFalseR
   t   cancelt
   assertTruet   assertFailureR   (   R   t   connectR!   R"   t   dR   (    (    sG   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_protocol.pyt   _cancelConnectTestˆ   s    	
c         C   s   d „  } |  j  | ƒ S(   s—   
        The L{Deferred} returned by L{ClientCreator.connectTCP} can be cancelled
        to abort the connection attempt before it completes.
        c         S   s   |  j  d d ƒ S(   Ns   example.comiÒ  (   R   (   R"   (    (    sG   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_protocol.pyR<   ¤   s    (   R>   (   R   R<   (    (    sG   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_protocol.pyt   test_cancelConnectTCPŸ   s    	c         C   s   d „  } |  j  | ƒ S(   s—   
        The L{Deferred} returned by L{ClientCreator.connectTCP} can be cancelled
        to abort the connection attempt before it completes.
        c         S   s   |  j  d ƒ S(   Ns   /foo/bar(   R   (   R"   (    (    sG   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_protocol.pyR<   ®   s    (   R>   (   R   R<   (    (    sG   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_protocol.pyt   test_cancelConnectUNIX©   s    	c         C   s   d „  } |  j  | ƒ S(   s—   
        The L{Deferred} returned by L{ClientCreator.connectTCP} can be cancelled
        to abort the connection attempt before it completes.
        c         S   s   |  j  d d t ƒ  ƒ S(   Ns   example.comiÒ  (   R   R2   (   R"   (    (    sG   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_protocol.pyR<   ¸   s    (   R>   (   R   R<   (    (    sG   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_protocol.pyt   test_cancelConnectSSL³   s    	c         C   s’   t  ƒ  } t | t ƒ } | | | ƒ } | j j ƒ  } |  j t | j ƒ  ƒ d ƒ | j ƒ  |  j | j ƒ  g  ƒ |  j	 | j
 ƒ |  j | t ƒ S(   sÍ   
        Like L{_cancelConnectTest}, but for the case where the L{Deferred} is
        cancelled after the connection is set up but before it is fired with the
        resulting protocol instance.
        i   (   R   R   R   R   R'   R(   t   lent   getDelayedCallsR9   R:   R
   R;   R   (   R   R<   R!   R"   R=   R   (    (    sG   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_protocol.pyt   _cancelConnectTimeoutTest½   s    	
c         C   s   d „  } |  j  | ƒ S(   s¡  
        L{ClientCreator.connectTCP} inserts a very short delayed call between
        the time the connection is established and the time the L{Deferred}
        returned from one of its connect methods actually fires.  If the
        L{Deferred} is cancelled in this interval, the established connection is
        closed, the timeout is cancelled, and the L{Deferred} fails with
        L{CancelledError}.
        c   
      S   sY   | j  d d ƒ } |  j j ƒ  \ } } } } } | j d  ƒ } t ƒ  }	 | j |	 ƒ | S(   Ns   example.comiÒ  (   R   R&   R'   R   R   R   t   makeConnection(
   R!   R"   R=   R)   R*   R#   R+   R,   R$   t	   transport(    (    sG   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_protocol.pyR<   â   s    	(   RD   (   R   R<   (    (    sG   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_protocol.pyt   test_cancelConnectTCPTimeoutÙ   s    		c         C   s   d „  } |  j  | ƒ S(   s¢  
        L{ClientCreator.connectUNIX} inserts a very short delayed call between
        the time the connection is established and the time the L{Deferred}
        returned from one of its connect methods actually fires.  If the
        L{Deferred} is cancelled in this interval, the established connection is
        closed, the timeout is cancelled, and the L{Deferred} fails with
        L{CancelledError}.
        c   	      S   sS   | j  d ƒ } |  j j ƒ  \ } } } } | j d  ƒ } t ƒ  } | j | ƒ | S(   Ns   /foo/bar(   R   R.   R'   R   R   R   RE   (	   R!   R"   R=   R/   R#   R+   R,   R$   RF   (    (    sG   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_protocol.pyR<   õ   s    	(   RD   (   R   R<   (    (    sG   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_protocol.pyt   test_cancelConnectUNIXTimeoutì   s    		c         C   s   d „  } |  j  | ƒ S(   s¡  
        L{ClientCreator.connectSSL} inserts a very short delayed call between
        the time the connection is established and the time the L{Deferred}
        returned from one of its connect methods actually fires.  If the
        L{Deferred} is cancelled in this interval, the established connection is
        closed, the timeout is cancelled, and the L{Deferred} fails with
        L{CancelledError}.
        c         S   sb   | j  d d t ƒ  ƒ } |  j j ƒ  \ } } } } } } | j d  ƒ }	 t ƒ  }
 |	 j |
 ƒ | S(   Ns   example.comiÒ  (   R   R2   R3   R'   R   R   R   RE   (   R!   R"   R=   R)   R*   R#   R6   R+   t   bindADdressR$   RF   (    (    sG   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_protocol.pyR<     s    !	(   RD   (   R   R<   (    (    sG   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_protocol.pyt   test_cancelConnectSSLTimeoutÿ   s    		c         C   s¤   t  ƒ  } t | t ƒ } | | | ƒ \ } } | j j ƒ  } | j | t t d ƒ ƒ ƒ |  j t	 | j
 ƒ  ƒ d ƒ | j ƒ  |  j | j
 ƒ  g  ƒ |  j | t ƒ S(   sÌ   
        Like L{_cancelConnectTest}, but for the case where the L{Deferred} is
        cancelled after the connection attempt has failed but before it is fired
        with the resulting failure.
        s   Simulated failurei   (   R   R   R   R   R'   t   clientConnectionFailedR    t	   ExceptionR(   RB   RC   R9   R;   R   (   R   R<   R!   R"   R=   R#   R   (    (    sG   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_protocol.pyt   _cancelConnectFailedTimeoutTest  s    	
c         C   s   d „  } |  j  | ƒ S(   sz   
        Similar to L{test_cancelConnectTCPTimeout}, but for the case where the
        connection attempt fails.
        c         S   s:   | j  d d ƒ } |  j j ƒ  \ } } } } } | | f S(   Ns   example.comiÒ  (   R   R&   R'   (   R!   R"   R=   R)   R*   R#   R+   R,   (    (    sG   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_protocol.pyR<   0  s    (   RM   (   R   R<   (    (    sG   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_protocol.pyt"   test_cancelConnectTCPFailedTimeout+  s    	c         C   s   d „  } |  j  | ƒ S(   s{   
        Similar to L{test_cancelConnectUNIXTimeout}, but for the case where the
        connection attempt fails.
        c         S   s4   | j  d ƒ } |  j j ƒ  \ } } } } | | f S(   Ns   /foo/bar(   R   R.   R'   (   R!   R"   R=   R/   R#   R+   R,   (    (    sG   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_protocol.pyR<   <  s    (   RM   (   R   R<   (    (    sG   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_protocol.pyt#   test_cancelConnectUNIXFailedTimeout7  s    	c         C   s   d „  } |  j  | ƒ S(   sz   
        Similar to L{test_cancelConnectSSLTimeout}, but for the case where the
        connection attempt fails.
        c   	      S   sC   | j  d d t ƒ  ƒ } |  j j ƒ  \ } } } } } } | | f S(   Ns   example.comiÒ  (   R   R2   R3   R'   (	   R!   R"   R=   R)   R*   R#   R6   R+   RI   (    (    sG   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_protocol.pyR<   H  s    !(   RM   (   R   R<   (    (    sG   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_protocol.pyt"   test_cancelConnectSSLFailedTimeoutC  s    	(   R   R   R   R%   R-   R1   R7   R>   R?   R@   RA   RD   RG   RH   RJ   RM   RN   RO   RP   (    (    (    sG   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_protocol.pyR   :   s"   						
	
	
							N(    (   R   t   twisted.python.failureR    t   twisted.internet.deferR   t   twisted.internet.protocolR   R   t   twisted.internet.taskR   t   twisted.trial.unittestR   t   twisted.test.proto_helpersR   R   R   R   R   (    (    (    sG   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_protocol.pyt   <module>   s   !