HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //usr/lib/python3/dist-packages/twisted/protocols/test/__pycache__/test_basic.cpython-38.pyc
U


W[���@szdZddlmZmZddlZddlZddlmZddlm	Z	ddl
mZmZddl
mZddlmZdd	lmZdd
lmZmZddlmZddlmZd
ZGdd�dej�ZGdd�dej�ZGdd�dej�ZGdd�dej �Z!Gdd�dej�Z"Gdd�dej �Z#Gdd�dej �Z$Gdd�d�Z%Gdd�de%ej&�Z'Gd d!�d!�Z(Gd"d#�d#ej e(�Z)Gd$d%�d%e(�Z*Gd&d'�d'e+�Z,Gd(d)�d)e%ej-�Z.Gd*d+�d+ej e*e,�Z/Gd,d-�d-e%ej0�Z1Gd.d/�d/ej e*e,�Z2Gd0d1�d1e1e+�Z3Gd2d3�d3e2�Z4Gd4d5�d5e%ej5�Z6Gd6d7�d7ej e*e,�Z7Gd8d9�d9e+�Z8Gd:d;�d;ej�Z9Gd<d=�d=ej �Z:Gd>d?�d?ej;�Z<Gd@dA�dAej;�Z=dS)Bz,
Test cases for L{twisted.protocols.basic}.
�)�division�absolute_importN)�BytesIO)�verifyObject)�_PY3�	iterbytes)�unittest)�basic)�reflect)�protocol�task)�	IProducer)�
proto_helpersz&All classes are new style on Python 3.c@s,eZdZdZdZdd�Zdd�Zdd�Zd	S)
�FlippingLineTesterzT
    A line receiver that flips between line and raw data modes after one byte.
    �
cCs
g|_dS�N)�lines��self�r�C/usr/lib/python3/dist-packages/twisted/protocols/test/test_basic.py�__init__#szFlippingLineTester.__init__cCs|j�|�|��dS)z&
        Set the mode to raw.
        N)r�append�
setRawMode�r�linerrr�lineReceived'szFlippingLineTester.lineReceivedcCs|�|dd��dS)z,
        Set the mode back to line.
        �N)�setLineMode�r�datarrr�rawDataReceived/sz"FlippingLineTester.rawDataReceivedN)�__name__�
__module__�__qualname__�__doc__�	delimiterrrr!rrrrrs
rc@sBeZdZdZdZdZddd�Zdd�Zd	d
�Zdd�Z	d
d�Z
dS)�
LineTestera�
    A line receiver that parses data received and make actions on some tokens.

    @type delimiter: C{bytes}
    @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.
    r�@NcCs
||_dS)z@
        If given, use a clock to make callLater calls.
        N)�clock)rr)rrrrGszLineTester.__init__cCs
g|_dS�z;
        Create/clean data received on connection.
        N��receivedrrrr�connectionMadeNszLineTester.connectionMadecCs�|j�|�|dkr|��n�|dkr@|��|j�d|j�n�|dkrv|��|��|j�d�|j�d|j�nd|dkr�|��nR|dd�dkr�t|dd��|_	n.|�
d	�r�|j�|d
�n|�
d�r�|j�
�dS)z|
        Receive line and make some action for some tokens: pause, rawpause,
        stop, len, produce, unproduce.
        ��pauser�rawpause�stopN�slen �produceF�	unproduce)r,rr�pauseProducingr)Z	callLater�resumeProducing�
stopProducing�int�length�
startswith�	transportZregisterProducerZunregisterProducerrrrrrUs&



zLineTester.lineReceivedcCsZ|d|j�||jd�}}|jt|�|_|jd||jd<|jdkrV|�|�dS)zj
        Read raw data, until the quantity specified by a previous 'len' line is
        reached.
        N���r)r9�lenr,r)rr �restrrrr!os

zLineTester.rawDataReceivedcCs.t|�|jdkr*|�||jdd��dS)z<
        Adjust line mode when long lines received.
        rN)r=�
MAX_LENGTHrrrrr�lineLengthExceeded{szLineTester.lineLengthExceeded)N)r"r#r$r%r&r?rr-rr!r@rrrrr'7s
r'c@s(eZdZdZdZdZdd�Zdd�ZdS)	�LineOnlyTesterz)
    A buffering line only receiver.
    rr(cCs
g|_dSr*r+rrrrr-�szLineOnlyTester.connectionMadecCs|j�|�dS)z%
        Save received data.
        N�r,rrrrrr�szLineOnlyTester.lineReceivedN)r"r#r$r%r&r?r-rrrrrrA�s
rAc
@s�eZdZdZdZddddddd	d
ddd
ddg
Zdd�ZdZdddgZedgZ	dd�Z
dZdddddgZddddddgZ
dd�ZdZdddgZd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�Zd0d1�Zd2d3�Zd4S)5�LineReceiverTestsz\
    Test L{twisted.protocols.basic.LineReceiver}, using the C{LineTester}
    wrapper.
    s�len 10

0123456789len 5

1234
len 20
foo 123

0123456789
012345678len 0
foo 5

1234567890123456789012345678901234567890123456789012345678901234567890
len 1

aslen 10�
0123456789slen 5s1234
slen 20sfoo 123s0123456789
012345678slen 0sfoo 5r.s67890slen 1�acCs�tdd�D]r}t��}t�}|�t�|��tt|j�|d�D](}|j|||d|�}|�	|�qB|�
|j|j�q
dS)zl
        Test buffering for different packet size, checking received matches
        expected data.
        r�
N)
�ranger�StringIOWithoutClosingr'�makeConnectionr�FileWrapperr=�buffer�dataReceived�assertEqual�outputr,�r�packet_size�t�a�i�srrr�test_buffer�szLineReceiverTests.test_buffers!twiddle1
twiddle2
pause
twiddle3
stwiddle1stwiddle2r/stwiddle3cCs�tdd�D]�}t��}t��}t|�}|�t�|��tt	|j
�|d�D](}|j
|||d|�}|�|�qL|�|j
|j�|�d�|�|j|j�q
dS)zo
        Test pause inside data receiving. It uses fake clock to see if
        pausing/resuming work.
        rrFrN)rGrrHr�Clockr'rIrrJr=�pauseBufrLrM�pauseOutput1r,�advance�pauseOutput2�rrPrQr)rRrSrTrrr�test_pausing�s
zLineReceiverTests.test_pausings/twiddle1
twiddle2
len 5
rawpause
12345twiddle3
r0s12345cCs�tdd�D]�}t��}t��}t|�}|�t�|��tt	|j
�|d�D](}|j
|||d|�}|�|�qL|�|j
|j�|�d�|�|j|j�q
dS)z7
        Test pause inside raw date receiving.
        rrFrN)rGrrHrrVr'rIrrJr=�rawpauseBufrLrM�rawpauseOutput1r,rY�rawpauseOutput2r[rrr�test_rawPausing�s
z!LineReceiverTests.test_rawPausings"twiddle1
twiddle2
stop
more
stuff
r1cCs�tdd�D]r}t��}t�}|�t�|��tt|j�|d�D](}|j|||d|�}|�	|�qB|�
|j|j�q
dS)z-
        Test stop inside producing.
        rrFN)
rGrrHr'rIrrJr=�stop_bufrLrM�stop_outputr,rOrrr�test_stopProducing�sz$LineReceiverTests.test_stopProducingcCsBt�}t��}|�t�|��|�d�|�|jddddg�dS)z6
        Test produce/unproduce in receiving.
        s&produce
hello world
unproduce
goodbye
r3shello worldr4�goodbyeN)	r'rrHrIrrJrLrMr,)rrRrQrrr�test_lineReceiverAsProducers

�z-LineReceiverTests.test_lineReceiverAsProducercCshGdd�dtj�}|�}|�d�|�|jd�|�|jd�|�d�|�|jd�|�|jd�d	S)
z�
        L{LineReceiver.clearLineBuffer} removes all buffered data and returns
        it as a C{bytes} and can be called from beneath C{dataReceived}.
        c@seZdZdd�ZdS)z@LineReceiverTests.test_clearLineBuffer.<locals>.ClearingReceivercSs||_|��|_dSr)rZclearLineBufferr>rrrrrszMLineReceiverTests.test_clearLineBuffer.<locals>.ClearingReceiver.lineReceivedN)r"r#r$rrrrr�ClearingReceiversrfs
foo
bar
baz�foosbar
bazsquux
squuxr.N)r	�LineReceiverrLrMrr>)rrfrrrr�test_clearLineBuffers

z&LineReceiverTests.test_clearLineBuffercCsPt�}t��}|�t�|��t��}|�d|�|�	d|d�
|j��dS)zC
        Test switching modes many times on the same data.
        sx
x�xr.N)rrrHrIrrJ�sys�getrecursionlimitrLrM�joinr)r�protor;�limitrrr�test_stackRecursion'sz%LineReceiverTests.test_stackRecursioncCsBt��}t��}|�|�|�d|jdd�|�|j�dS�zx
        C{LineReceiver} disconnects the transport if it receives a line longer
        than its C{MAX_LENGTH}.
        rjr�
rN�	r	rhr�StringTransportrIrLr?�
assertTrue�
disconnecting�rrnr;rrr�test_maximumLineLength3s

z(LineReceiverTests.test_maximumLineLengthcCs�t�}d|_t��}|�|�d|jd}|�|�|�|jdd��|�|jdd�|�|�|j�|�	t
|j�d�|�	||jd�dS)a~
        C{LineReceiver} doesn't disconnect the transport when it
        receives a finished line as long as its C{MAX_LENGTH}, when
        the second-to-last packet ended with a pattern that could have
        been -- and turns out to have been -- the start of a
        delimiter, and that packet causes the total input to exceed
        C{MAX_LENGTH} + len(delimiter).
        r2rjrNr<r)r'r?rrtrIrLr&�assertFalservrMr=r,)rrnrQrrrr�&test_maximumLineLengthPartialDelimiter?s	

z8LineReceiverTests.test_maximumLineLengthPartialDelimitercCsXt��}d|_t��}|�|�|�d|j|jdt|j�d��|�	|j
�dS)aO
        C{LineReceiver} doesn't disconnect the transport it if
        receives a non-finished line whose length, counting the
        delimiter, is longer than its C{MAX_LENGTH} but shorter than
        its C{MAX_LENGTH} + len(delimiter). (When the first part that
        exceeds the max is the beginning of the delimiter.)
        s
rjNr)r	rhr&rrtrIrLr?r=ryrvrwrrr�(test_notQuiteMaximumLineLengthUnfinishedVs
�z:LineReceiverTests.test_notQuiteMaximumLineLengthUnfinishedcCsFt��}dd�|_t��}|�|�|��|�d�}|�|t	�dS)zf
        C{LineReceiver.dataReceived} forwards errors returned by
        C{rawDataReceived}.
        cSstd�S)NZoops)�RuntimeError�r rrr�<lambda>or.z5LineReceiverTests.test_rawDataError.<locals>.<lambda>sdataN)
r	rhr!rrtrIrrLZassertIsInstancer|)rrnr;Zwhyrrr�test_rawDataErroris


z#LineReceiverTests.test_rawDataErrorcCst��}|�t|jd�dS)z�
        When L{LineReceiver.rawDataReceived} is not overridden in a
        subclass, calling it raises C{NotImplementedError}.
        �fooN)r	rh�assertRaises�NotImplementedErrorr!�rrnrrr�"test_rawDataReceivedNotImplementedwsz4LineReceiverTests.test_rawDataReceivedNotImplementedcCst��}|�t|jd�dS)z�
        When L{LineReceiver.lineReceived} is not overridden in a subclass,
        calling it raises C{NotImplementedError}.
        r�N)r	rhr�r�rr�rrr�test_lineReceivedNotImplemented�sz1LineReceiverTests.test_lineReceivedNotImplementedN)r"r#r$r%rKrNrUrWrXrZr\r]r^r_r`rarbrcrerirprxrzr{rr�r�rrrrrC�sJ�

�
	rCc@s(eZdZdZdd�Zdd�Zdd�ZdS)	�ExcessivelyLargeLineCatcherz�
    Helper for L{LineReceiverLineLengthExceededTests}.

    @ivar longLines: A L{list} of L{bytes} giving the values
        C{lineLengthExceeded} has been called with.
    cCs
g|_dSr)�	longLinesrrrrr-�sz*ExcessivelyLargeLineCatcher.connectionMadecCsdS)z/
        Disregard any received lines.
        Nrrrrrr�sz(ExcessivelyLargeLineCatcher.lineReceivedcCs|j�|�dS)zF
        Record any data that exceeds the line length limits.
        N)r�rrrrrr@�sz.ExcessivelyLargeLineCatcher.lineLengthExceededN)r"r#r$r%r-rr@rrrrr��sr�c@sHeZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dS)�#LineReceiverLineLengthExceededTestszO
    Tests for L{twisted.protocols.basic.LineReceiver.lineLengthExceeded}.
    cCs,t�|_d|j_t��|_|j�|j�dS)N�)r�rnr?rrtr;rIrrrr�setUp�s
z)LineReceiverLineLengthExceededTests.setUpcCs6d|jjdd}|j�|�|�|g|jj�dS)z�
        If more bytes than C{LineReceiver.MAX_LENGTH} arrive containing no line
        delimiter, all of the bytes are passed as a single string to
        L{LineReceiver.lineLengthExceeded}.
        rj�N)rnr?rLrMr��rZ	excessiverrr�test_longUnendedLine�sz8LineReceiverLineLengthExceededTests.test_longUnendedLinecCsBd|jjdd}|j�d|jj|�|�|g|jj�dS)a*
        If L{LineReceiver.dataReceived} is called with bytes representing a
        short line followed by bytes that exceed the length limit without a
        line delimiter, L{LineReceiver.lineLengthExceeded} is called with all
        of the bytes following the short line's delimiter.
        rjr�N)rnr?rLr&rMr�r�rrr�test_longLineAfterShortLine�sz?LineReceiverLineLengthExceededTests.test_longLineAfterShortLinecCsF|jj�d|jjddgd�}|j�|�|�|g|jj�dS)a
        If L{LineReceiver.dataReceived} is called with more than
        C{LineReceiver.MAX_LENGTH} bytes containing a line delimiter somewhere
        not in the first C{MAX_LENGTH} bytes, the entire byte string is passed
        to L{LineReceiver.lineLengthExceeded}.
        rjr�N)rnr&rmr?rLrMr�r�rrr�test_longLineWithDelimiter�s
�z>LineReceiverLineLengthExceededTests.test_longLineWithDelimitercCsBd|jjdd|jjd}|j�|�|�|g|jj�dS)a
        If L{LineReceiver.dataReceived} is called with more than
        C{LineReceiver.MAX_LENGTH} bytes containing multiple line delimiters
        somewhere not in the first C{MAX_LENGTH} bytes, the entire byte string
        is passed to L{LineReceiver.lineLengthExceeded}.
        rjr�N)rnr?r&rLrMr�r�rrr�test_multipleLongLines�s
�z:LineReceiverLineLengthExceededTests.test_multipleLongLinescCsBt��}t��}|�|�|�d|jdd�|�|j�dSrqrsrwrrrrx�s

z:LineReceiverLineLengthExceededTests.test_maximumLineLengthcCsDt��}t��}|�|�|�d|jt|j��|�	|j
�dS)z�
        C{LineReceiver} disconnects the transport it if receives a non-finished
        line longer than its C{MAX_LENGTH}.
        rjN)r	rhrrtrIrLr?r=r&rurvrwrrr�test_maximumLineLengthRemaining�s

zCLineReceiverLineLengthExceededTests.test_maximumLineLengthRemainingN)r"r#r$r%r�r�r�r�r�rxr�rrrrr��s

r�c@s,eZdZdZdZdd�Zdd�Zdd�Zd	S)
�LineOnlyReceiverTestsz@
    Tests for L{twisted.protocols.basic.LineOnlyReceiver}.
    s7foo
    bleakness
    desolation
    plastic forks
    cCsTt��}t�}|�|�t|j�D]}|�|�q"|�|j|j�	d�dd��dS)zW
        Test buffering over line protocol: data received should match buffer.
        rNr<)
rrtrArIrrKrLrMr,�split)rrQrR�crrrrUs
z!LineOnlyReceiverTests.test_buffercCsJt�}t��}|�|�|�d|jt|j�dd�|�|j	�dS)z�
        C{LineOnlyReceiver} disconnects the transport if it receives a
        line longer than its C{MAX_LENGTH} + len(delimiter).
        rjrrrN)
rArrtrIrLr?r=r&rurvrwrrr�!test_greaterThanMaximumLineLengths

���z7LineOnlyReceiverTests.test_greaterThanMaximumLineLengthcCst��}|�t|jd�dS)z�
        When L{LineOnlyReceiver.lineReceived} is not overridden in a subclass,
        calling it raises C{NotImplementedError}.
        r�N)r	�LineOnlyReceiverr�r�rr�rrrr�sz5LineOnlyReceiverTests.test_lineReceivedNotImplementedN)r"r#r$r%rKrUr�r�rrrrr��s

r�c@s,eZdZdd�Zdd�ZdZdZdd�Zd	S)
�	TestMixincCs
g|_dSrr+rrrrr-(szTestMixin.connectionMadecCs|j�|�dSrrB�rrTrrr�stringReceived,szTestMixin.stringReceived�2rcCs
d|_dS)Nr)�closed)r�reasonrrr�connectionLost3szTestMixin.connectionLostN)r"r#r$r-r�r?r�r�rrrrr�&s
r�c@seZdZdd�ZdS)�
TestNetstringcCs|j�|�|j�|�dSr)r,rr;�writer�rrrr�:szTestNetstring.stringReceivedN)r"r#r$r�rrrrr�8sr�c@s$eZdZgZdZdd�Zdd�ZdS)�LPTestCaseMixinNcCst��}|��}|�|�|S)z�
        Return a new instance of C{self.protocol} connected to a new instance
        of L{proto_helpers.StringTransport}.
        )rrtrrI)rrQrRrrr�getProtocolFs
zLPTestCaseMixin.getProtocolcCs>|jD]2}|��}t|�D]}|�|�q|�|jj�qdS)zO
        Assert that illegal strings cause the transport to be closed.
        N)�illegalStringsr�rrLrur;rv�rrT�rr�rrr�test_illegalQs

zLPTestCaseMixin.test_illegal)r"r#r$r�rr�r�rrrrr�@sr�c@s�eZdZdZdddddddgZd	d
ddgZeZd
d�Zdd�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�Zd-d.�Zd/d0�Zd1d2�Zd3d4�Zd5d6�Zd7d8�Zd9S):�NetstringReceiverTestszA
    Tests for L{twisted.protocols.basic.NetstringReceiver}.
    �hello�worldshowsaresyou123s:todaysaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaas9999999999999999999999sabcs4:abcdes751:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab,cCs$t��|_t�|_|j�|j�dSr)rrtr;r��netstringReceiverrIrrrrr�js
zNetstringReceiverTests.setUpcCs�tdd�D]�}t��}t�}d|_|�|�|jD]}|�|�q2|��}tt	|�|d�D]*}||||d|�}|r^|�
|�q^|�|j|j�q
dS)zI
        Strings can be received in chunks of different lengths.
        rrFi�N)
rGrrtr�r?rI�strings�
sendString�valuer=rLrMr,)rrPrQrRrT�outrSrrrrUps

z"NetstringReceiverTests.test_buffercCs"|j�d�|�|jjdg�dS)zE
        Empty netstrings (with length '0') can be received.
        s0:,r.N�r�rLrMr,rrrr�test_receiveEmptyNetstring�sz1NetstringReceiverTests.test_receiveEmptyNetstringcCs"|j�d�|�|jjdg�dS)z;
        One-character netstrings can be received.
        �1:a,rENr�rrrr�test_receiveOneCharacter�sz/NetstringReceiverTests.test_receiveOneCharactercCs"|j�d�|�|jjdg�dS)z;
        Two-character netstrings can be received.
        s2:ab,�abNr�rrrr�test_receiveTwoCharacters�sz0NetstringReceiverTests.test_receiveTwoCharacterscCs"|j�d�|�|jjdg�dS)z�
        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.
        s4:1:a,,r�Nr�rrrr�test_receiveNestedNetstring�sz2NetstringReceiverTests.test_receiveNestedNetstringcCs|j�d�|�|jj�dS)zL
        Netstrings containing more data than expected are refused.
        s2:aaa,N�r�rLrur;rvrrrr�test_moreDataThanSpecified�sz1NetstringReceiverTests.test_moreDataThanSpecifiedcCs|j�d�|�|jj�dS)z�
        Netstrings that should be empty according to their length
        specification are refused if they contain data.
        s0:a,Nr�rrrr�$test_moreDataThanSpecifiedBorderCase�sz;NetstringReceiverTests.test_moreDataThanSpecifiedBorderCasecCs|j�d�|�|jj�dS)z`
        Netstrings without leading digits that specify the length
        are refused.
        s:aaa,Nr�rrrr�test_missingNumber�sz)NetstringReceiverTests.test_missingNumbercCs|j�d�|�|jj�dS)zg
        Netstrings without a colon between length specification and
        data are refused.
        s3aaa,Nr�rrrr�test_missingColon�sz(NetstringReceiverTests.test_missingColoncCs|j�d�|�|jj�dS)zY
        Netstrings that have no leading digits nor a colon are
        refused.
        saaa,Nr�rrrr�test_missingNumberAndColon�sz1NetstringReceiverTests.test_missingNumberAndColoncCs|j�d�|�|jj�dS)zA
        Netstrings consisting only of data are refused.
        saaaNr�rrrr�
test_onlyData�sz$NetstringReceiverTests.test_onlyDatacCs>|j�d�|j�d�|�|jjdg�|�|j���dS)z=
        Netstrings can be received in two portions.
        s4:aasaa,saaaaN)r�rLrMr,ru�_payloadCompleterrrr�test_receiveNetstringPortions_1�sz6NetstringReceiverTests.test_receiveNetstringPortions_1cCs,dD]}|j�|�q|�|jjdg�dS)z�
        Netstrings can be received in more than two portions, even if
        the length specification is split across two portions.
        )�1s0:01234s56789�,rDNr��r�partrrr�test_receiveNetstringPortions_2�sz6NetstringReceiverTests.test_receiveNetstringPortions_2cCs,dD]}|j�|�q|�|jjdg�dS)zE
        Netstrings can be received one character at a time.
        )�2�:rE�br�r�Nr�r�rrr�test_receiveNetstringPortions_3�sz6NetstringReceiverTests.test_receiveNetstringPortions_3cCsR|j�d�|�|j���|�|jjdg�|j�d�|�|jjddg�dS)z�
        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.
        s1:a,1rEs:b,r�N)r�rLrur�rMr,rrrr�test_receiveTwoNetstrings�s
z0NetstringReceiverTests.test_receiveTwoNetstringscCs>|jjd}|j�d�t|�dd|f��|�|jj�dS)zs
        Netstrings with a length specification exceeding the specified
        C{MAX_LENGTH} are refused.
        rr.r�rEN)r�r?rLrm�bytesrur;rv)rZtooLongrrr�test_maxReceiveLimits

�z+NetstringReceiverTests.test_maxReceiveLimitcCs&d|j_|j��|�|jjd�dS)zw
        C{_consumeLength} returns the expected length of the
        netstring, including the trailing comma.
        �12:�
N)r��_remainingData�_consumeLengthrM�_expectedPayloadSizerrrr�test_consumeLengths
z)NetstringReceiverTests.test_consumeLengthcCs.d|j_d|j_|j��|�|jjd�dS)z�
        C{_consumeLength} works as expected if the length specification
        contains the value of C{MAX_LENGTH} (border case).
        r��r�N)r�r�r?r�rMr�rrrr�test_consumeLengthBorderCase1s
z4NetstringReceiverTests.test_consumeLengthBorderCase1cCs&d|j_d|j_|�tj|jj�dS)z�
        C{_consumeLength} raises a L{basic.NetstringParseError} if
        the length specification exceeds the value of C{MAX_LENGTH}
        by 1 (border case).
        r��N�r�r�r?r�r	ZNetstringParseErrorr�rrrr�test_consumeLengthBorderCase2#s
�z4NetstringReceiverTests.test_consumeLengthBorderCase2cCs&d|j_d|j_|�tj|jj�dS)z�
        C{_consumeLength} raises a L{basic.NetstringParseError} if
        the length specification exceeds the value of C{MAX_LENGTH}
        by more than 1.
        s1000:r�Nr�rrrr�test_consumeLengthBorderCase3/s
�z4NetstringReceiverTests.test_consumeLengthBorderCase3cCst��}|�t|jd�dS)z�
        When L{NetstringReceiver.stringReceived} is not overridden in a
        subclass, calling it raises C{NotImplementedError}.
        r�N)r	�NetstringReceiverr�r�r�r�rrr�!test_stringReceivedNotImplemented;sz8NetstringReceiverTests.test_stringReceivedNotImplementedN)r"r#r$r%r�r�r�rr�rUr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrr�]s@��
				

	

r�c@sPeZdZdZdZdZdZdZdd�Zdd�Z	dd�Z
d	d
�Zdd�Zd
d�Z
dS)�IntNTestCaseMixinz4
    TestCase mixin for int-prefixed protocols.
    NcCsR|��}|jD].}tt�|jt|��|�D]}|�|�q,q|�|j	|j�dS)z>
        Test receiving data find the same data send.
        N)
r�r�r�struct�pack�structFormatr=rLrMr,)rr�rTr�rrr�test_receiveOs

zIntNTestCaseMixin.test_receivecCs>|jD]2}|��}t|�D]}|�|�q|�|jg�qdS)zK
        Send partial data, nothing should be definitely received.
        N)�partialStringsr�rrLrMr,r�rrr�test_partialZs

zIntNTestCaseMixin.test_partialcCs6|��}|�d�|�|j��t�|jd�d�dS)z2
        Test sending data over protocol.
        �bbbbbbbbbbbbbbbb�N)r�r�rMr;r�r�r�r��rr�rrr�	test_sendes

�zIntNTestCaseMixin.test_sendcCs@g}|��}|j|_d|_|�t�|jd��|�|dg�dS)z�
        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.
        rFr�N)	r�r�lengthLimitExceededr?rLr�r�r�rM)rr9r�rrr�test_lengthLimitExceededosz*IntNTestCaseMixin.test_lengthLimitExceededcCs8|��}d|_|�t�|jd�d�|�|jg�dS)z�
        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}.
        rFr�sxxxxxxxxxxxN)r�r?rLr�r�r�rMr,r�rrr�test_longStringNotDelivered}s�z-IntNTestCaseMixin.test_longStringNotDeliveredcCst��}|�t|jd�dS)z�
        When L{IntNStringReceiver.stringReceived} is not overridden in a
        subclass, calling it raises C{NotImplementedError}.
        r�N)r	ZIntNStringReceiverr�r�r�r�rrrr��sz3IntNTestCaseMixin.test_stringReceivedNotImplemented)r"r#r$r%rr�r�r�r�r�r�r�r�r�rrrrr�Es

r�c@s8eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�ZdS)
�RecvdAttributeMixinz�
    Mixin defining tests for string receiving protocols with a C{recvd}
    attribute which should be settable by application code, to be combined with
    L{IntNTestCaseMixin} on a L{TestCase} subclass
    cCst�|jt|��|S)zg
        Return C{data} prefixed with message length in C{protocol.structFormat}
        form.
        )r�r�r�r=)rrr rrr�makeMessage�szRecvdAttributeMixin.makeMessagecsdg�|�����fdd�}|�_t��jd�d}t��jd�d}��||�|��|g�dS)z�
        In stringReceived, recvd contains the remaining data that was passed to
        dataReceived that was not part of the current message.
        cs���j�dSr�r�recvd�ZreceivedString�r��resultrrr��szKRecvdAttributeMixin.test_recvdContainsRemainingData.<locals>.stringReceived��aaaaasbbbbN)r�r�r�r�r�rLrM)rr�ZcompleteMessageZincompleteMessagerr�r�test_recvdContainsRemainingData�sz3RecvdAttributeMixin.test_recvdContainsRemainingDatacst|���g�d}|��|�����fdd�}|�_d}d}|��|�}|��|�}��||�|��||g�dS)z�
        In stringReceived, if recvd is changed, messages should be parsed from
        it rather than the input to dataReceived.
        sccccccs�s
��_��|�dSr)r�rr��ZmessageCr�r�rrr��sz=RecvdAttributeMixin.test_recvdChanged.<locals>.stringReceivedr�sbbbbbN)r�r�r�rLrM)rZpayloadCr�ZpayloadAZpayloadBZmessageAZmessageBrr�r�test_recvdChanged�sz%RecvdAttributeMixin.test_recvdChangedcs�|���g}d�|jD] }|�|��|��|���qg����fdd�}|�_��d�|����d�|��|j�|��jd�dS)z�
        Data already parsed by L{IntNStringReceiver.dataReceived} is not
        reparsed if C{stringReceived} consumes some of the
        L{IntNStringReceiver.recvd} buffer.
        scs"��|��jt��d��_dSr)rr�r=r��ZSWITCHrnr�rrr��s
z:RecvdAttributeMixin.test_switching.<locals>.stringReceivedr.�N)	r�r�rr�r�rLrmrMr�)rZmixrTr�rr�r�test_switching�s

z"RecvdAttributeMixin.test_switchingcsp|���d}t|�d�_|��|�}g���fdd�}|�_��|�|��dt|��|��d|�dS)z�
        The L{IntNStringReceiver.recvd} buffer contains all data not yet
        processed by L{IntNStringReceiver.dataReceived} if the
        C{lengthLimitExceeded} event occurs.
        stoo longrcs��|����j�dSrr�)r9�rnr�rrr��s
zPRecvdAttributeMixin.test_recvdInLengthLimitExceeded.<locals>.lengthLimitExceededrN)r�r=r?r�r�rLrM)rZDATA�messager�rr�r�test_recvdInLengthLimitExceeded�s
z3RecvdAttributeMixin.test_recvdInLengthLimitExceededN)	r"r#r$r%r�r�r�r�r�rrrrr��sr�c@seZdZdZdS)�	TestInt32z�
    A L{basic.Int32StringReceiver} storing received strings in an array.

    @ivar received: array holding received strings.
    N�r"r#r$r%rrrrr��sr�c@s4eZdZdZeZddgZdgZdddgZdd	�Z	d
S)�
Int32Testsz/
    Test case for int32-prefixed protocol
    rEr�s
aaaaaas�hello therer.cCsB|��}|�d�|�|j��d�|�d�|�|jdg�dS)z?
        Test specific behavior of the 32-bits length.
        rgsfoosubar�ubarN�r�r�rMr;r�rLr,r�rrr�	test_datas


zInt32Tests.test_dataN)
r"r#r$r%r�rr�r�r�r�rrrrr�s
r�c@seZdZdZdS)�	TestInt16z�
    A L{basic.Int16StringReceiver} storing received strings in an array.

    @ivar received: array holding received strings.
    Nr�rrrrr�sr�c@s<eZdZdZeZddgZdgZdddgZdd	�Z	d
d�Z
dS)
�
Int16Testsz/
    Test case for int16-prefixed protocol
    rEr�saaaaaa�r�r.cCsB|��}|�d�|�|j��d�|�d�|�|jdg�dS)z?
        Test specific behavior of the 16-bits length.
        rgsfoosubarr�Nr�r�rrrr�/s


zInt16Tests.test_datacCs2|��}dd|jdd}|�t|j|�dS�zA
        Send too much data: that should cause an error.
        r�r��rN�r�ZprefixLengthr��AssertionErrorr��rr�ZtooSendrrr�test_tooLongSend:szInt16Tests.test_tooLongSendN)r"r#r$r%r�rr�r�r�r�rrrrrr%s
rc@seZdZdZdS)�NewStyleTestInt16z0
    A new-style class version of TestInt16
    Nr�rrrrrDsrc@seZdZdZereZeZdS)�NewStyleInt16Testszn
    This test case verifies that IntNStringReceiver still works when inherited
    by a new-style class.
    N)	r"r#r$r%r�_PY3NEWSTYLESKIP�skiprrrrrrr	Ksr	c@seZdZdZdS)�TestInt8z�
    A L{basic.Int8StringReceiver} storing received strings in an array.

    @ivar received: array holding received strings.
    Nr�rrrrrWsrc@s<eZdZdZeZddgZdgZdddgZdd	�Z	d
d�Z
dS)
�	Int8Testsz.
    Test case for int8-prefixed protocol
    rEr�saaaaaa�sdzadzr.cCsB|��}|�d�|�|j��d�|�d�|�|jdg�dS)z>
        Test specific behavior of the 8-bits length.
        rgsfoosubarr�Nr�r�rrrr�ks


zInt8Tests.test_datacCs2|��}dd|jdd}|�t|j|�dSrrrrrrrvszInt8Tests.test_tooLongSendN)r"r#r$r%rrr�r�r�r�rrrrrr
`s
r
c@s8eZdZdZdZdZdd�Zdd�Zdd�Zd	d
�Z	dS)�OnlyProducerTransportzm
    Transport which isn't really a transport, just looks like one to
    someone not looking very hard.
    FcCs
g|_dSrr}rrrrr�szOnlyProducerTransport.__init__cCs
d|_dS)NT��pausedrrrrr5�sz$OnlyProducerTransport.pauseProducingcCs
d|_dS)NFrrrrrr6�sz%OnlyProducerTransport.resumeProducingcCs|j�|�dSr)r r)rr�rrrr��szOnlyProducerTransport.writeN)
r"r#r$r%rrvrr5r6r�rrrrr�src@seZdZdZdd�ZdS)�ConsumingProtocolzC
    Protocol that really, really doesn't want any more bytes.
    cCs|j�|�|��dSr)r;r�r5rrrrr�szConsumingProtocol.lineReceivedN)r"r#r$r%rrrrrr�src@seZdZdZdd�ZdS)�
ProducerTestszM
    Tests for L{basic._PausableMixin} and L{basic.LineReceiver.paused}.
    cCs�t�}t�}|�|�|�d�|�|jg�|�|j�|�|j�|�d�|�|jdg�|�|j�|�|j�|�	�|�|jdg�|�|j�|�|j�|�d�|�|jddg�|�|j�|�|j�|�	�|�|jdddg�|�|j�|�|j�|�d�|�|jdddg�|�|j�|�|j�|�	�|�|jddddg�|�|j�|�|j�|�	�|�|jddddg�|�|j�|�|j�d	S)
a?
        When L{basic.LineReceiver} is paused, it doesn't deliver lines to
        L{basic.LineReceiver.lineReceived} and delivers them immediately upon
        being resumed.

        L{ConsumingProtocol} is a L{LineReceiver} that pauses itself after
        every line, and writes that line to its transport.
        shello, sworld
shello, worldshello
world
r�r�s	goodbye
rdN)
rrrIrLrMr ryrrur6)r�prQrrr�test_pauseResume�sR	





�
�zProducerTests.test_pauseResumeN)r"r#r$r%rrrrrr�src@s@eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dS)�FileSenderTestsz(
    Tests for L{basic.FileSender}.
    cCst��}|�tt|��dS)zP
        L{basic.FileSender} implements the L{IPullProducer} interface.
        N)r	�
FileSenderrurr
)r�senderrrr�test_interface�szFileSenderTests.test_interfacecCsBtd�}t��}t��}|�||�|�|j|�|�|j	�dS)z�
        When L{basic.FileSender.beginFileTransfer} is called, it registers
        itself with provided consumer, as a non-streaming producer.
        �Test contentN)
rrrtr	r�beginFileTransferrM�producerryZ	streaming)r�source�consumerrrrr�test_producerRegistered�sz'FileSenderTests.test_producerRegisteredcCsftd�}t��}t��}|�||�}|��|��|�|j�|�	d|�
|��|�	d|���dS)z�
        L{basic.FileSender} sends the content of the given file using a
        C{IConsumer} interface via C{beginFileTransfer}. It returns a
        L{Deferred} which fires with the last byte sent.
        r�tN)rrrtr	rrr6ZassertIsNonerrM�successResultOfr��rrrr�drrr�
test_transferszFileSenderTests.test_transfercCs�td�}t��}t��}d|_|�||�}|��|�d|�	��|��|�d|�	��|��|�d|�	��|��|�d|�
|��|�d|�	��dS)zj
        L{basic.FileSender} reads at most C{CHUNK_SIZE} every time it resumes
        producing.
        rr2sTestsTest conr N)rrrtr	rZ
CHUNK_SIZErr6rMr�r!r"rrr�test_transferMultipleChunkssz+FileSenderTests.test_transferMultipleChunkscCsddd�}td�}t��}t��}|�|||�}|��|��|�d|�|��|�d|�	��dS)z�
        L{basic.FileSender.beginFileTransfer} takes a C{transform} argument
        which allows to manipulate the data on the fly.
        cSs|��Sr)�swapcase)�chunkrrr�	transform<sz=FileSenderTests.test_transferWithTransform.<locals>.transformr�TstEST CONTENTN)
rrrtr	rrr6rMr!r�)rr(rrrr#rrr�test_transferWithTransform6sz*FileSenderTests.test_transferWithTransformcCsVtd�}t��}t��}|�||�}|��|�|�}|�t	�|�
dt|j��dS)z�
        The C{Deferred} returned by L{basic.FileSender.beginFileTransfer} fails
        with an C{Exception} if C{stopProducing} when the transfer is not
        complete.
        rz#Consumer asked us to stop producingN)
rrrtr	rrr7ZfailureResultOfZtrap�	ExceptionrM�strr�)rrrrr#Zfailurerrr�test_abortedTransferKs

�z$FileSenderTests.test_abortedTransferN)
r"r#r$r%rrr$r%r*r-rrrrr�s
rc@s eZdZdZerdZdd�ZdS)�MiceDeprecationTestsz2
    L{twisted.protocols.mice} is deprecated.
    z7twisted.protocols.mice is not being ported to Python 3.cCs:t�d�|��}|�dt|��|�d|dd�dS)zM
        L{twisted.protocols.mice} is deprecated since Twisted 16.0.
        ztwisted.protocols.micerzatwisted.protocols.mice was deprecated in Twisted 16.0.0: There is no replacement for this module.rr�N)r
ZnamedAnyZ
flushWarningsrMr=)rZ
warningsShownrrr�test_MiceDeprecationgs

�z)MiceDeprecationTests.test_MiceDeprecationN)r"r#r$r%rrr/rrrrr._sr.)>r%Z
__future__rrrkr��iorZzope.interface.verifyrZtwisted.python.compatrrZ
twisted.trialrZtwisted.protocolsr	Ztwisted.pythonr
Ztwisted.internetrrZtwisted.internet.interfacesr
Ztwisted.testrr
rhrr'r�rAZSynchronousTestCaserCr�r�r�r�r�r�r�r�r��objectr�ZInt32StringReceiverr�r�ZInt16StringReceiverr�rrr	ZInt8StringReceiverrr
rrrZTestCaserr.rrrr�<module>s^MqU.iOi	�	�	� Ln