
[XMc           @   s  d  Z  d d l Z d d l m Z d d l m Z m Z m Z d d l m	 Z	 m
 Z
 m Z m Z m Z d d l 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 d f  d     YZ d e e j f d     YZ d f  d     YZ d e j e f d     YZ d e f d     YZ d e e j f d     YZ d e j e f d     YZ  d e e j! f d      YZ" d! e j e f d"     YZ# d# e e j$ f d$     YZ% d% e j e f d&     YZ& d' e' 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 d0     YZ. d1 e j f d2     YZ/ d3 e j f d4     YZ0 d S(5   s+   
Test cases for twisted.protocols package.
iN(   t   unittest(   t   basict   wiret   portforward(   t   reactort   protocolt   defert   taskt   error(   t   proto_helperst
   LineTesterc           B   sJ   e  Z d  Z d Z d Z d d  Z d   Z d   Z d   Z	 d   Z
 RS(	   s  
    A line receiver that parses data received and make actions on some tokens.

    @type delimiter: C{str}
    @ivar delimiter: character used between received lines.
    @type MAX_LENGTH: C{int}
    @ivar MAX_LENGTH: size of a line when C{lineLengthExceeded} will be called.
    @type clock: L{twisted.internet.task.Clock}
    @ivar clock: clock simulating reactor callLater. Pass it to constructor if
        you want to use the pause/rawpause functionalities.
    s   
i@   c         C   s   | |  _  d S(   s@   
        If given, use a clock to make callLater calls.
        N(   t   clock(   t   selfR   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   __init__    s    c         C   s   g  |  _  d S(   s;   
        Create/clean data received on connection.
        N(   t   received(   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   connectionMade'   s    c         C   s(  |  j  j |  | d k r) |  j   n | d k rX |  j   |  j j d |  j  n | d k r |  j   |  j   |  j  j d  |  j j d |  j  n | d k r |  j   nj | d  d k r t | d  |  _	 nD | j
 d  r|  j j |  t  n | j
 d	  r$|  j j   n  d
 S(   s|   
        Receive line and make some action for some tokens: pause, rawpause,
        stop, len, produce, unproduce.
        t    t   pausei    t   rawpauset   stopi   s   len t   producet	   unproduceN(   R   t   appendt
   setRawModet   pauseProducingR   t	   callLatert   resumeProducingt   stopProducingt   intt   lengtht
   startswitht	   transportt   registerProducert   Falset   unregisterProducer(   R   t   line(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   lineReceived.   s&    


c         C   sl   | |  j   | |  j  } } |  j  t |  |  _  |  j d | |  j d <|  j  d k rh |  j |  n  d S(   sj   
        Read raw data, until the quantity specified by a previous 'len' line is
        reached.
        ii    N(   R   t   lenR   t   setLineMode(   R   t   datat   rest(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   rawDataReceivedH   s
    c         C   s8   t  |  |  j d k r4 |  j | |  j d  n  d S(   s<   
        Adjust line mode when long lines received.
        i   N(   R%   t
   MAX_LENGTHR&   (   R   R#   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   lineLengthExceededT   s    N(   t   __name__t
   __module__t   __doc__t	   delimiterR*   t   NoneR   R   R$   R)   R+   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyR
      s   			t   LineOnlyTesterc           B   s,   e  Z d  Z d Z d Z d   Z d   Z RS(   s)   
    A buffering line only receiver.
    s   
i@   c         C   s   g  |  _  d S(   s;   
        Create/clean data received on connection.
        N(   R   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyR   d   s    c         C   s   |  j  j |  d S(   s%   
        Save received data.
        N(   R   R   (   R   R#   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyR$   k   s    (   R,   R-   R.   R/   R*   R   R$   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyR1   ]   s
   	t   WireTestCasec           B   s2   e  Z d  Z d   Z d   Z d   Z d   Z RS(   s   
    Test wire protocols.
    c         C   s   t  j   } t j   } | j |  | j d  | j d  | j d  | j d  | j d  |  j | j   d  d S(   sT   
        Test wire.Echo protocol: send some data and check it send it back.
        t   hellot   worldt   howt   aret   yout   helloworldhowareyouN(   R	   t   StringTransportR   t   Echot   makeConnectiont   dataReceivedt   assertEqualst   value(   R   t   tt   a(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt	   test_echox   s    c         C   s?   t  j   } t j   } | j |  |  j | j   d  d S(   s)   
        Test wire.Who protocol.
        s   root
N(   R	   R9   R   t   WhoR;   R=   R>   (   R   R?   R@   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   test_who   s    c         C   s?   t  j   } t j   } | j |  |  j | j   d  d S(   s*   
        Test wire.QOTD protocol.
        s'   An apple a day keeps the doctor away.
N(   R	   R9   R   t   QOTDR;   R=   R>   (   R   R?   R@   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt	   test_QOTD   s
    c         C   s   t  j   } t j   } | j |  | j d  | j d  | j d  | j d  | j d  |  j | j   d  d S(   s-   
        Test wire.Discard protocol.
        R3   R4   R5   R6   R7   R   N(   R	   R9   R   t   DiscardR;   R<   t   assertEqualR>   (   R   R?   R@   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   test_discard   s    (   R,   R-   R.   RA   RC   RE   RH   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyR2   s   s
   		
	t   LineReceiverTestCasec           B   s   e  Z d  Z d Z d d d d d d d d	 d
 d d d d g Z d   Z d Z d d d g Z e d g Z d   Z	 d Z
 d d d d d g Z d d d d d d g Z d   Z d Z d d d g Z d   Z d   Z d   Z RS(   s=   
    Test LineReceiver, using the C{LineTester} wrapper.
    s   len 10

0123456789len 5

1234
len 20
foo 123

0123456789
012345678len 0
foo 5

1234567890123456789012345678901234567890123456789012345678901234567890
len 1

as   len 10t
   0123456789s   len 5s   1234
s   len 20s   foo 123s   0123456789
012345678s   len 0s   foo 5R   t   67890s   len 1R@   c         C   s   x t  d d  D] } t j   } t   } | j t j |   xN t  t |  j  | d  D]/ } |  j | | | d | !} | j	 |  q_ W|  j
 |  j | j  q Wd S(   sl   
        Test buffering for different packet size, checking received matches
        expected data.
        i   i
   N(   t   rangeR	   t   StringIOWithoutClosingR
   R;   R   t   FileWrapperR%   t   bufferR<   t   failUnlessEqualt   outputR   (   R   t   packet_sizeR?   R@   t   it   s(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt
   testBuffer   s    	$s!   twiddle1
twiddle2
pause
twiddle3
t   twiddle1t   twiddle2R   t   twiddle3c         C   s   x t  d d  D] } t j   } t j   } t |  } | j t j |   xN t  t	 |  j
  | d  D]/ } |  j
 | | | d | !} | j |  qn W|  j |  j | j  | j d  |  j |  j | j  q Wd S(   so   
        Test pause inside data receiving. It uses fake clock to see if
        pausing/resuming work.
        i   i
   i    N(   RL   R	   RM   R   t   ClockR
   R;   R   RN   R%   t	   pause_bufR<   R=   t   pause_output1R   t   advancet   pause_output2(   R   RR   R?   R   R@   RS   RT   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   test_pausing   s    $s/   twiddle1
twiddle2
len 5
rawpause
12345twiddle3
R   t   12345c         C   s   x t  d d  D] } t j   } t j   } t |  } | j t j |   xN t  t	 |  j
  | d  D]/ } |  j
 | | | d | !} | j |  qn W|  j |  j | j  | j d  |  j |  j | j  q Wd S(   s7   
        Test pause inside raw date receiving.
        i   i
   i    N(   RL   R	   RM   R   RY   R
   R;   R   RN   R%   t   rawpause_bufR<   R=   t   rawpause_output1R   R\   t   rawpause_output2(   R   RR   R?   R   R@   RS   RT   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   test_rawPausing   s    $s"   twiddle1
twiddle2
stop
more
stuff
R   c         C   s   x t  d d  D] } t j   } t   } | j t j |   xN t  t |  j  | d  D]/ } |  j | | | d | !} | j	 |  q_ W|  j
 |  j | j  q Wd S(   s-   
        Test stop inside producing.
        i   i
   N(   RL   R	   RM   R
   R;   R   RN   R%   t   stop_bufR<   R=   t   stop_outputR   (   R   RR   R?   R@   RS   RT   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   test_stopProducing  s    	$c         C   s[   t    } t j   } | j t j |   | j d  |  j | j d d d d g  d S(   s6   
        Test produce/unproduce in receiving.
        s&   produce
hello world
unproduce
goodbye
R   s   hello worldR   t   goodbyeN(	   R
   R	   RM   R;   R   RN   R<   R=   R   (   R   R@   R?   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   test_lineReceiverAsProducer  s    	c         C   s   d t  j f d     Y} |   } | j d  |  j | j d  |  j | j d  | j d  |  j | j d  |  j | j d  d	 S(
   s   
        L{LineReceiver.clearLineBuffer} removes all buffered data and returns
        it as a C{str} and can be called from beneath C{dataReceived}.
        t   ClearingReceiverc           B   s   e  Z d    Z RS(   c         S   s   | |  _  |  j   |  _ d  S(   N(   R#   t   clearLineBufferR(   (   R   R#   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyR$   (  s    	(   R,   R-   R$   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyRi   '  s   s   foo
bar
bazt   foos   bar
bazs   quux
t   quuxR   N(   R   t   LineReceiverR<   RG   R#   R(   (   R   Ri   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   test_clearLineBuffer"  s    	(   R,   R-   R.   RO   RQ   RU   RZ   R[   R]   R^   R`   Ra   Rb   Rc   Rd   Re   Rf   Rh   Rn   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyRI      s(   							t   LineOnlyReceiverTestCasec           B   s&   e  Z d  Z d Z d   Z d   Z RS(   s#   
    Test line only receiveer.
    s7   foo
    bleakness
    desolation
    plastic forks
    c         C   sj   t  j   } t   } | j |  x |  j D] } | j |  q, W|  j | j |  j j d  d   d S(   sW   
        Test buffering over line protocol: data received should match buffer.
        s   
iN(	   R	   R9   R1   R;   RO   R<   R=   R   t   split(   R   R?   R@   t   c(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   test_bufferC  s    	c         C   sL   t  j   } t   } | j |  | j d d  } |  j | t j  d S(   sO   
        Test sending a line too long: it should close the connection.
        t   xi   N(   R	   R9   R1   R;   R<   t   assertIsInstanceR   t   ConnectionLost(   R   R?   R@   t   res(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   test_lineTooLongO  s
    	(   R,   R-   R.   RO   Rr   Rw   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyRo   9  s   	t	   TestMixinc           B   s/   e  Z d    Z d   Z d Z d Z d   Z RS(   c         C   s   g  |  _  d  S(   N(   R   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyR   ]  s    c         C   s   |  j  j |  d  S(   N(   R   R   (   R   RT   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   stringReceiveda  s    i2   i    c         C   s   d |  _  d  S(   Ni   (   t   closed(   R   t   reason(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   connectionLosth  s    (   R,   R-   R   Ry   R*   Rz   R|   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyRx   [  s
   		t   TestNetstringc           B   s   e  Z d    Z RS(   c         C   s$   |  j  j |  |  j j |  d  S(   N(   R   R   R   t   write(   R   RT   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyRy   o  s    (   R,   R-   Ry   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyR}   m  s   t   LPTestCaseMixinc           B   s&   e  Z g  Z d Z d    Z d   Z RS(   c         C   s)   t  j   } |  j   } | j |  | S(   s   
        Return a new instance of C{self.protocol} connected to a new instance
        of L{proto_helpers.StringTransport}.
        (   R	   R9   R   R;   (   R   R?   R@   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   getProtocol{  s    c         C   sU   xN |  j  D]C } |  j   } x | D] } | j |  q# W|  j | j j  q
 Wd S(   sO   
        Assert that illegal strings cause the transport to be closed.
        N(   t   illegalStringsR   R<   t
   assertTrueR   t   disconnecting(   R   RT   t   rRq   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   test_illegal  s
    N(   R,   R-   R   R0   R   R   R   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyR   u  s   	t   NetstringReceiverTestCasec           B   s  e  Z d  d d d d d d d g Z d d	 d
 d g Z 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 d   Z d   Z d   Z d    Z d!   Z d"   Z RS(#   R3   R4   R5   R6   t   you123s   :todayR@   i  t   9999999999999999999999t   abcs   4:abcdes7   51:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab,c         C   s2   t  j   |  _ t   |  _ |  j j |  j  d  S(   N(   R	   R9   R   R}   t   netstringReceiverR;   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   setUp  s    c         C   s   x t  d d  D] } t j   } t   } d | _ | j |  x |  j D] } | j |  qK W| j   } xQ t  t	 |  | d  D]5 } | | | | d | !} | r | j
 |  q q W|  j | j |  j  q Wd S(   sI   
        Strings can be received in chunks of different lengths.
        i   i
   i  N(   RL   R	   R9   R}   R*   R;   t   stringst
   sendStringR>   R%   R<   R=   R   (   R   RR   R?   R@   RT   t   outRS   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyRr     s    		!c   	      C   sR  g  i d d 6d d 6d d d g d d g } t    } t j   } d | _ | j |  x | D] } | j |  | j   } | j   | | j d   } | | j d  d d !} |  j	 t
 |  t t |    |  j	 | t |   q^ W|  j d |  j g  } |  j t |  d  |  j | d d d  |  j | d d t  d S(   s   
        L{basic.NetstringReceiver.sendString} will send objects that are not
        strings by sending their string representation according to str().
        R@   i   t   bi   Rq   i  i   t   finet   ands   you?id   t   :it   offendingFunctionsi   i    t   messagesa   Data passed to sendString() must be a string. Non-string support is deprecated since Twisted 10.0t   categoryN(   i   R   R   s   you?(   R}   R	   R9   R*   R;   R   R>   t   cleart   findR=   R   R%   t   strt   flushWarningst   test_sendNonStringsRG   t   DeprecationWarning(	   R   t
   nonStringsR@   R?   RT   R   R   R'   t   warnings(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyR     s.    #			
"	c         C   s-   |  j  j d  |  j |  j  j d g  d S(   sE   
        Empty netstrings (with length '0') can be received.
        s   0:,R   N(   R   R<   R=   R   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   test_receiveEmptyNetstring  s    c         C   s-   |  j  j d  |  j |  j  j d g  d S(   s;   
        One-character netstrings can be received.
        s   1:a,R@   N(   R   R<   R=   R   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   test_receiveOneCharacter  s    c         C   s-   |  j  j d  |  j |  j  j d g  d S(   s;   
        Two-character netstrings can be received.
        s   2:ab,t   abN(   R   R<   R=   R   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   test_receiveTwoCharacters  s    c         C   s-   |  j  j d  |  j |  j  j d g  d S(   s   
        Netstrings with embedded netstrings. This test makes sure that
        the parser does not become confused about the ',' and ':'
        characters appearing inside the data portion of the netstring.
        s   4:1:a,,s   1:a,N(   R   R<   R=   R   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   test_receiveNestedNetstring  s    c         C   s'   |  j  j d  |  j |  j j  d S(   sL   
        Netstrings containing more data than expected are refused.
        s   2:aaa,N(   R   R<   R   R   R   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   test_moreDataThanSpecified  s    c         C   s'   |  j  j d  |  j |  j j  d S(   s   
        Netstrings that should be empty according to their length
        specification are refused if they contain data.
        s   0:a,N(   R   R<   R   R   R   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt$   test_moreDataThanSpecifiedBorderCase  s    c         C   s'   |  j  j d  |  j |  j j  d S(   s`   
        Netstrings without leading digits that specify the length
        are refused.
        s   :aaa,N(   R   R<   R   R   R   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   test_missingNumber  s    c         C   s'   |  j  j d  |  j |  j j  d S(   sg   
        Netstrings without a colon between length specification and
        data are refused.
        s   3aaa,N(   R   R<   R   R   R   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   test_missingColon  s    c         C   s'   |  j  j d  |  j |  j j  d S(   sY   
        Netstrings that have no leading digits nor a colon are
        refused.
        s   aaa,N(   R   R<   R   R   R   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   test_missingNumberAndColon  s    c         C   s'   |  j  j d  |  j |  j j  d S(   sA   
        Netstrings consisting only of data are refused.
        t   aaaN(   R   R<   R   R   R   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   test_onlyData#  s    c         C   sS   |  j  j d  |  j  j d  |  j |  j  j d g  |  j |  j  j    d S(   s=   
        Netstrings can be received in two portions.
        s   4:aas   aa,t   aaaaN(   R   R<   R=   R   R   t   _payloadComplete(   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   test_receiveNetstringPortions_1+  s    c         C   sJ   x* d d d d g D] } |  j  j |  q W|  j |  j  j d g  d S(   s   
        Netstrings can be received in more than two portions, even if
        the length specification is split across two portions.
        t   1s   0:01234t   56789t   ,RJ   N(   R   R<   R=   R   (   R   t   part(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   test_receiveNetstringPortions_25  s    c         C   s>   x d D] } |  j  j |  q W|  j |  j  j d g  d S(   sE   
        Netstrings can be received one character at a time.
        s   2:ab,R   N(   R   R<   R=   R   (   R   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   test_receiveNetstringPortions_3?  s    c         C   so   |  j  j d  |  j |  j  j    |  j |  j  j d g  |  j  j d  |  j |  j  j d d g  d S(   s   
        A stream of two netstrings can be received in two portions,
        where the first portion contains the complete first netstring
        and the length specification of the second netstring.
        s   1:a,1R@   s   :b,R   N(   R   R<   R   R   R=   R   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   test_receiveTwoNetstringsH  s
    c         C   sE   |  j  j d } |  j  j d | d | f  |  j |  j j  d S(   ss   
        Netstrings with a length specification exceeding the specified
        C{MAX_LENGTH} are refused.
        i   s   %s:%sR@   N(   R   R*   R<   R   R   R   (   R   t   tooLong(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   test_maxReceiveLimitU  s    c         C   s3   d |  j  _ |  j  j   |  j |  j  j d  d S(   sw   
        C{_consumeLength} returns the expected length of the
        netstring, including the trailing comma.
        s   12:i   N(   R   t   _remainingDatat   _consumeLengthR=   t   _expectedPayloadSize(   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   test_consumeLength`  s    c         C   s?   d |  j  _ d |  j  _ |  j  j   |  j |  j  j d  d S(   s   
        C{_consumeLength} works as expected if the length specification
        contains the value of C{MAX_LENGTH} (border case).
        s   12:i   i   N(   R   R   R*   R   R=   R   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   test_consumeLengthBorderCase1j  s    c         C   s5   d |  j  _ d |  j  _ |  j t j |  j  j  d S(   s   
        C{_consumeLength} raises a L{basic.NetstringParseError} if
        the length specification exceeds the value of C{MAX_LENGTH}
        by 1 (border case).
        s   12:i   N(   R   R   R*   t   assertRaisesR   t   NetstringParseErrorR   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   test_consumeLengthBorderCase2u  s    c         C   s5   d |  j  _ d |  j  _ |  j t j |  j  j  d S(   s   
        C{_consumeLength} raises a L{basic.NetstringParseError} if
        the length specification exceeds the value of C{MAX_LENGTH}
        by more than 1.
        s   1000:i   N(   R   R   R*   R   R   R   R   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   test_consumeLengthBorderCase3  s    c         C   s   t  j t  j t  j t  j f |  j d |  j g  } |  j t |  d  x" | D] } |  j | d t	  qQ W|  j | d d d  |  j | d d d  |  j | d	 d d
  |  j | d d d  d S(   s   
        Accessing one of the old module attributes used by the
        NetstringReceiver parser emits a deprecation warning.
        R   i   R   i    R   sk   twisted.protocols.basic.LENGTH was deprecated in Twisted 10.2.0: NetstringReceiver parser state is private.i   si   twisted.protocols.basic.DATA was deprecated in Twisted 10.2.0: NetstringReceiver parser state is private.i   sj   twisted.protocols.basic.COMMA was deprecated in Twisted 10.2.0: NetstringReceiver parser state is private.i   sk   twisted.protocols.basic.NUMBER was deprecated in Twisted 10.2.0: NetstringReceiver parser state is private.N(
   R   t   LENGTHt   DATAt   COMMAt   NUMBERR   t   test_deprecatedModuleAttributesR=   R%   R   (   R   R   t   warning(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyR     s$    	(   R,   R-   R   R   R}   R   R   Rr   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyR     s6   					 				
											
	
					
			t   IntNTestCaseMixinc           B   sS   e  Z d  Z d Z d Z d Z d Z d   Z d   Z	 d   Z
 d   Z d   Z RS(   s4   
    TestCase mixin for int-prefixed protocols.
    c         C   sq   |  j    } xH |  j D]= } x4 t j | j t |   | D] } | j |  q< Wq W|  j | j |  j  d S(   s>   
        Test receiving data find the same data send.
        N(	   R   R   t   structt   packt   structFormatR%   R<   R=   R   (   R   R   RT   Rq   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   test_receive  s
    &c         C   sU   xN |  j  D]C } |  j   } x | D] } | j |  q# W|  j | j g   q
 Wd S(   sK   
        Send partial data, nothing should be definitely received.
        N(   t   partialStringsR   R<   R=   R   (   R   RT   R   Rq   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   test_partial  s
    c         C   sI   |  j    } | j d  |  j | j j   t j | j d  d  d S(   s2   
        Test sending data over protocol.
        R   i   Nt   bbbbbbbbbbbbbbbbR   (   R   R   R=   R   R>   R   R   R   (   R   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt	   test_send  s    c         C   sZ   g  } |  j    } | j | _ d | _ | j t j | j d   |  j | d g  d S(   s   
        When a length prefix is received which is greater than the protocol's
        C{MAX_LENGTH} attribute, the C{lengthLimitExceeded} method is called
        with the received length prefix.
        i
   i   N(	   R   R   t   lengthLimitExceededR*   R<   R   R   R   RG   (   R   R   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   test_lengthLimitExceeded  s    	c         C   sL   |  j    } d | _ | j t j | j d  d  |  j | j g   d S(   s   
        If a length prefix for a string longer than C{MAX_LENGTH} is delivered
        to C{dataReceived} at the same time as the entire string, the string is
        not passed to C{stringReceived}.
        i
   i   Rs   Nt   xxxxxxxxxxx(   R   R*   R<   R   R   R   RG   R   (   R   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   test_longStringNotDelivered  s
    	N(   R,   R-   R.   R0   R   R   R   R   R   R   R   R   R   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyR     s   			
	t	   TestInt32c           B   s   e  Z d  Z RS(   s   
    A L{basic.Int32StringReceiver} storing received strings in an array.

    @ivar received: array holding received strings.
    (   R,   R-   R.   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyR     s   t   Int32TestCasec           B   sA   e  Z d  Z e Z d d	 g Z d g Z d d d g Z d   Z RS(
   s/   
    Test case for int32-prefixed protocol
    R@   R   i   s
      aaaaaat      s   hello thereR   c         C   sY   |  j    } | j d  |  j | j j   d  | j d  |  j | j d g  d S(   s?   
        Test specific behavior of the 32-bits length.
        Rk   t      foot      ubart   ubarN(   R   R   R=   R   R>   R<   R   (   R   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt	   test_data  s
    R   (	   R,   R-   R.   R   R   R   R   R   R   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyR     s   	t	   TestInt16c           B   s   e  Z d  Z RS(   s   
    A L{basic.Int16StringReceiver} storing received strings in an array.

    @ivar received: array holding received strings.
    (   R,   R-   R.   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyR     s   t   Int16TestCasec           B   sJ   e  Z d  Z e Z d d
 g Z d g Z d d d g Z d   Z d	   Z	 RS(   s/   
    Test case for int16-prefixed protocol
    R@   R   i   s    aaaaaat    s   hello thereR   c         C   sY   |  j    } | j d  |  j | j j   d  | j d  |  j | j d g  d S(   s?   
        Test specific behavior of the 16-bits length.
        Rk   t    foot    ubarR   N(   R   R   R=   R   R>   R<   R   (   R   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyR   "  s
    c         C   s?   |  j    } d d | j d d } |  j t | j |  d S(   sA   
        Send too much data: that should cause an error.
        R   i   i   i   N(   R   t   prefixLengthR   t   AssertionErrorR   (   R   R   t   tooSend(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   test_tooLongSend-  s    R   (
   R,   R-   R.   R   R   R   R   R   R   R   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyR     s   		t   TestInt8c           B   s   e  Z d  Z RS(   s   
    A L{basic.Int8StringReceiver} storing received strings in an array.

    @ivar received: array holding received strings.
    (   R,   R-   R.   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyR   7  s   t   Int8TestCasec           B   sJ   e  Z d  Z e Z d d
 g Z d g Z d d d g Z d   Z d	   Z	 RS(   s.   
    Test case for int8-prefixed protocol
    R@   R   i   t     aaaaaas   t   dzadzR   c         C   sY   |  j    } | j d  |  j | j j   d  | j d  |  j | j d g  d S(   s>   
        Test specific behavior of the 8-bits length.
        Rk   s   foos   ubarR   N(   R   R   R=   R   R>   R<   R   (   R   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyR   J  s
    c         C   s?   |  j    } d d | j d d } |  j t | j |  d S(   sA   
        Send too much data: that should cause an error.
        R   i   i   i   N(   R   R   R   R   R   (   R   R   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyR   U  s    R   (
   R,   R-   R.   R   R   R   R   R   R   R   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyR   @  s   		t   OnlyProducerTransportc           B   s8   e  Z e Z e Z d    Z d   Z d   Z d   Z RS(   c         C   s   g  |  _  d  S(   N(   R'   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyR   f  s    c         C   s   t  |  _ d  S(   N(   t   Truet   paused(   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyR   j  s    c         C   s   t  |  _ d  S(   N(   R!   R   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyR   n  s    c         C   s   |  j  j |  d  S(   N(   R'   R   (   R   t   bytes(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyR~   r  s    (	   R,   R-   R!   R   R   R   R   R   R~   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyR   _  s   			t   ConsumingProtocolc           B   s   e  Z d    Z RS(   c         C   s   |  j  j |  |  j   d  S(   N(   R   R~   R   (   R   R#   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyR$   z  s    (   R,   R-   R$   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyR   w  s   t   ProducerTestCasec           B   s   e  Z d    Z RS(   c         C   s  t    } t   } | j |  | j d  |  j | j  |  j | j  |  j | j  | j d  |  j | j d g  |  j | j  |  j | j  | j	   |  j | j  |  j | j  | j d  |  j | j d d g  |  j | j  |  j | j  | j	   | j d  |  j | j d d d g  |  j | j  |  j | j  | j	   |  j | j d d d d g  |  j | j  |  j | j  | j	   |  j | j d d d d g  |  j | j  |  j | j  d  S(	   Ns   hello, s   world
s   hello, worlds   hello
world
R3   s	   goodbye
R4   Rg   (
   R   R   R;   R<   t   failIfR'   R   R=   t
   failUnlessR   (   R   t   pR?   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   testPauseResume  s>    		



(   R,   R-   R   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyR     s   t   TestableProxyClientFactoryc           B   s   e  Z d  Z d   Z RS(   s   
    Test proxy client factory that keeps the last created protocol instance.

    @ivar protoInstance: the last instance of the protocol.
    @type protoInstance: L{portforward.ProxyClient}
    c         C   s"   t  j j |  |  } | |  _ | S(   sE   
        Create the protocol instance and keeps track of it.
        (   R   t   ProxyClientFactoryt   buildProtocolt   protoInstance(   R   t   addrt   proto(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyR     s    	(   R,   R-   R.   R   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyR     s   t   TestableProxyFactoryc           B   s   e  Z d  Z d   Z RS(   sl  
    Test proxy factory that keeps the last created protocol instance.

    @ivar protoInstance: the last instance of the protocol.
    @type protoInstance: L{portforward.ProxyServer}

    @ivar clientFactoryInstance: client factory used by C{protoInstance} to
        create forward connections.
    @type clientFactoryInstance: L{TestableProxyClientFactory}
    c            s@   t  j j   |  } t     _   f d   | _ |   _ | S(   s   
        Create the protocol instance, keeps track of it, and makes it use
        C{clientFactoryInstance} as client factory.
        c              s     j  S(   N(   t   clientFactoryInstance(    (   R   (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   <lambda>  s    (   R   t   ProxyFactoryR   R   R   t   clientProtocolFactoryR   (   R   R   R   (    (   R   s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyR     s
    	(   R,   R-   R.   R   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyR     s   
t   Portforwardingc           B   s)   e  Z d  Z d   Z d   Z d   Z RS(   s   
    Test port forwarding.
    c         C   s+   t  j   |  _ t j   |  _ g  |  _ d  S(   N(   R   R:   t   serverProtocolR   t   Protocolt   clientProtocolt	   openPorts(   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyR     s    c         C   s   y |  j  j j j   Wn t k
 r* n Xy  |  j  j j } | j j   Wn t k
 r^ n Xy |  j j j   Wn t k
 r n Xy |  j j j   Wn t k
 r n Xt j	 g  |  j
 D] } t j | j  ^ q  S(   N(   t   proxyServerFactoryR   R   t   loseConnectiont   AttributeErrorR   R   R   R   t   gatherResultsR   t   maybeDeferredt   stopListening(   R   t   piR   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   tearDown  s&    c            s$  t  j   }  f d   | _  t j d | d d }  j j |  t d | j   j   _	 t j d  j	 d d }  j j |  d  g    t
 j         f d   } |  j _   f d   } |  j _ t  j   }  f d   | _  t j d | j   j |   S(	   s=   
        Test port forwarding through Echo protocol.
        c              s     j  S(   N(   R   (    (   R   (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyR     s    i    t	   interfaces	   127.0.0.1i  c            sP     j  |   t     k rL  j d j    d    j d   n  d  S(   NR   Rs   (   t   extendR%   R=   t   joint   callbackR0   (   R'   (   R   t   dR   t   nBytes(    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   testDataReceived  s    c              s     j  j j d   d  S(   NRs   (   R   R   R~   (    (   R   R  (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   testConnectionMade  s    c              s     j  S(   N(   R   (    (   R   (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyR      s    (   R   t   ServerFactoryR   t	   listenTCPR   R   R   t   getHostt   portR   R   t   DeferredR   R<   R   t   ClientFactoryt
   connectTCP(   R   t   realServerFactoryt   realServerPortt   proxyServerPortR  R  t   clientFactory(    (   R   R   R  R  s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   test_portforward  s,    		(   R,   R-   R.   R   R  R  (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyR     s   		t   StringTransportTestCasec           B   s   e  Z d  Z d   Z RS(   sA   
    Test L{proto_helpers.StringTransport} helper behaviour.
    c         C   s&   t  j   } |  j t | j d  d S(   sY   
        Test that L{proto_helpers.StringTransport} doesn't accept unicode data.
        u   fooN(   R	   R9   R   t	   TypeErrorR~   (   R   RT   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   test_noUnicode.  s    (   R,   R-   R.   R  (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyR  )  s   (1   R.   R   t   twisted.trialR    t   twisted.protocolsR   R   R   t   twisted.internetR   R   R   R   R   t   twisted.testR	   Rm   R
   t   LineOnlyReceiverR1   t   TestCaseR2   RI   Ro   Rx   t   NetstringReceiverR}   R   R   R   t   Int32StringReceiverR   R   t   Int16StringReceiverR   R   t   Int8StringReceiverR   R   t   objectR   R   R   R   R   R   R   R   R  (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_protocols.pyt   <module>   s:   (M9" F				2K