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/spread/test/__pycache__/test_banana.cpython-38.pyc
U


W[`<�@sddlmZmZddlZddlmZddlmZddlm	Z	ddl
mZddlm
Z
ddlmZmZmZmZdd	lmZmZdd
lmZer�dZnddlmZGd
d�de	j�Zdd�Zdd�ZGdd�de	j�ZGdd�de�Z Gdd�de�Z!Gdd�de	j�Z"dS)�)�absolute_import�divisionN)�partial)�BytesIO)�unittest)�banana)�failure)�long�	iterbytes�	_bytesChr�_PY3)�protocol�main)�StringTransportl����)�maxintc@seZdZdd�ZdS)�	MathTestscCspttdd��ttdd��ttdd��dg}|D]6}t�}t�||j�|��}t�|�}|�||�q4dS)Nr�di�iLi@Bi�Bl)	�list�rangerrZint2b128�write�getvalueZb1282int�assertEqual)�selfZ	funkylist�i�x�v�y�r�A/usr/lib/python3/dist-packages/twisted/spread/test/test_banana.py�
test_int2b128s��
zMathTests.test_int2b128N)�__name__�
__module__�__qualname__rrrrrrsrcCs|�|�dS)z�
    Dictate a Banana dialect to use.

    @param protocol: A L{banana.Banana} instance which has not yet had a
        dialect negotiated.

    @param dialect: A L{bytes} instance naming a Banana dialect to select.
    N)Z_selectDialect)r
Zdialectrrr�
selectDialect$sr#cCs0t�}|�}|�|�|��|�|�|��S)a�
    Banana encode an object using L{banana.Banana.sendEncoded}.

    @param bananaFactory: A no-argument callable which will return a new,
        unconnected protocol instance to use to do the encoding (this should
        most likely be a L{banana.Banana} instance).

    @param obj: The object to encode.
    @type obj: Any type supported by Banana.

    @return: A L{bytes} instance giving the encoded form of C{obj}.
    )r�makeConnection�clear�sendEncoded�value)Z
bananaFactory�objZ	transportrrrr�encode4s


r)c@s.eZdZdZejZdd�Zdd�Zdd�Z	dS)	�BananaTestBasezn
    The base for test classes. It defines commonly used things and sets up a
    connection for testing.
    cCsNt�|_|��|_|j�t�|j��t|jd�|j|j_	t
t|j�|_dS)Nsnone)r�io�encClass�encr$r
ZFileWrapperr#�	putResultZexpressionReceivedrr)�rrrr�setUpRs

zBananaTestBase.setUpcCs
||_dS)z�
        Store an expression received by C{self.enc}.

        @param result: The object that was received.
        @type result: Any type supported by Banana.
        N)�result)rr1rrrr.[szBananaTestBase.putResultcCs|j�t�tj��|`dS�N)r-ZconnectionLostrZFailurerZCONNECTION_DONEr/rrr�tearDowneszBananaTestBase.tearDownN)
r r!r"�__doc__r�Bananar,r0r.r3rrrrr*Ks
	
r*c@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
erNde
_dd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�Zd-d.�Zd/d0�Zd1d2�Zd3S)4�BananaTestsz
    General banana tests.
    cCs0|j�d�|j�|j���|jdks,t�dS)Nshello)r-r&�dataReceivedr+rr1�AssertionErrorr/rrr�test_stringpszBananaTests.test_stringcCs"tr|�dd�n|�dd�dS)z�
        Banana does not support unicode.  ``Banana.sendEncoded`` raises
        ``BananaError`` if called with an instance of ``unicode``.
        Zhellozbuiltins.strz__builtin__.unicodeN)r�_unsupportedTypeTestr/rrr�test_unsupportedUnicodevsz#BananaTests.test_unsupportedUnicodecCs"tr|�td�n|�td�dS)z�
        Banana does not support arbitrary builtin types like L{type}.
        L{banana.Banana.sendEncoded} raises L{banana.BananaError} if called
        with an instance of L{type}.
        z
builtins.typez__builtin__.typeN)rr:�typer/rrr�test_unsupportedBuiltinType�sz'BananaTests.test_unsupportedBuiltinTypecCs|�t�td�dS)z�
        Banana does not support arbitrary user-defined types (such as those
        defined with the ``class`` statement).  ``Banana.sendEncoded`` raises
        ``BananaError`` if called with an instance of such a type.
        z
.MathTestsN)r:rr r/rrr�test_unsupportedUserType�sz$BananaTests.test_unsupportedUserTypecCs.|�tj|jj|�}|�d�|�t|��dS)a�
        Assert that L{banana.Banana.sendEncoded} raises L{banana.BananaError}
        if called with the given object.

        @param obj: Some object that Banana does not support.
        @param name: The name of the type of the object.

        @raise: The failure exception is raised if L{Banana.sendEncoded} does
            not raise L{banana.BananaError} or if the message associated with the
            exception is not formatted to include the type of the unsupported
            object.
        zBanana cannot send {0} objectsN)�assertRaisesr�BananaErrorr-r&ZassertIn�format�str)rr(�name�excrrrr:�s
z BananaTests._unsupportedTypeTestcCsPdtd�fD]>}|j�|�|j�|j���|�|jd�|�|jt	�qdS)z�
        A positive integer less than 2 ** 32 should round-trip through
        banana without changing value and should come out represented
        as an C{int} (regardless of the type which was encoded).
        i�'N)
r	r-r&r7r+rrr1�assertIsInstance�int)rr'rrr�test_int�s
zBananaTests.test_intcCs�dD]�}dD]x}d||}||dfD]X}|j�|�|��|�|j|�|tksd|tdkrt|�|jt�q*|�|jt�q*qqdS)a
        Integers greater than 2 ** 32 and less than -2 ** 32 should
        round-trip through banana without changing value and should
        come out represented as C{int} instances if the value fits
        into that type on the receiving platform.
        )� �@��)r��rLN)	r-r7r)rr1�_maxintrEr	rF)rZexp�add�m�nrrr�test_largeLong�szBananaTests.test_largeLongz@Python 3 has unified int/long into an int type of unlimited sizecCs(|jj}|d}d|d}|d}|S)N�rMrL)r-�prefixLimit)r�bytes�bits�largest�smallestrrr�_getSmallest�s
zBananaTests._getSmallestcCs |��}|�tj|jj|�dS)zz
        Test that a long above the implementation-specific limit is rejected
        as too large to be encoded.
        N)rYr?rr@r-r&)rrXrrr�test_encodeTooLargeLong�sz#BananaTests.test_encodeTooLargeLongcCsj|��}|j�|jjd�|j�|�|j��}|j�d�|j�|jjd�|�t	j
|jj|�dS)zz
        Test that a long above the implementation specific limit is rejected
        as too large to be decoded.
        rMrN)rYr-�setPrefixLimitrTr&r+r�truncater?rr@r7)rrX�encodedrrr�test_decodeTooLargeLong�s
z#BananaTests.test_decodeTooLargeLongcCs
|��Sr2)rYr/rrr�_getLargest�szBananaTests._getLargestcCs |��}|�tj|jj|�dS)z�
        Test that a negative long below the implementation-specific limit is
        rejected as too small to be encoded.
        N)r_r?rr@r-r&)rrWrrr�test_encodeTooSmallLong�sz#BananaTests.test_encodeTooSmallLongcCsj|��}|j�|jjd�|j�|�|j��}|j�d�|j�|jjd�|�t	j
|jj|�dS)z�
        Test that a negative long below the implementation specific limit is
        rejected as too small to be decoded.
        rMrN)r_r-r[rTr&r+rr\r?rr@r7)rrWr]rrr�test_decodeTooSmallLong�s
z#BananaTests.test_decodeTooSmallLongcCs0|j�d�|j�|j���|�|jd�dS)Ni��r-r&r7r+rrr1r/rrr�test_integer
szBananaTests.test_integercCs0|j�d�|j�|j���|�|jd�dS)Ni	���rbr/rrr�
test_negativeszBananaTests.test_negativecCs0|j�d�|j�|j���|�|jd�dS)Ng��@rbr/rrr�
test_floatszBananaTests.test_floatc	CsXddddgddgddd	d
dggdggg}|j�|�|j�|j���|�|j|�dS)
NrLrM����>@皙���D@��six�seven�eight�	�
rb�rZfoorrr�	test_lists�zBananaTests.test_listcCslddddgddgddd	d
dggdgtjdtjdtjd
g
}|j�|�|�|j���|�|j|�dS)zj
        Test feeding the data byte per byte to the receiver. Normally
        data is not split.
        rLrMrfrgrhrirjrkrlrmrnro���N)	�sys�maxsizer-r&�feedr+rrr1rprrr�test_partial's�zBananaTests.test_partialcCst|�D]}|j�|�qdS)z�
        Feed the data byte per byte to the receiver.

        @param data: The bytes to deliver.
        @type data: L{bytes}
        N)r
r-r7)r�dataZbyterrrru5szBananaTests.feedcCsd}|�tj|j|�dS)Ns��r?rr@ru�rrwrrr�test_oversizedList@szBananaTests.test_oversizedListcCsd}|�tj|j|�dS)Ns�rxryrrr�test_oversizedStringFsz BananaTests.test_oversizedStringcCs0d}z|j�|�Wntjk
r*YnXdS)Ns�)r-r7rr@)rZcrashStringrrr�test_crashStringLs
zBananaTests.test_crashStringcCs0|j�d�|j�|j���|�|jd�dS)N��rbr/rrr�test_crashNegativeLong_sz"BananaTests.test_crashNegativeLongcCsPd}d}d}|�|�|d�d|�|�|�|d�d|�|�|�|d�d	|�d
}|�|�|d�d|�|�|�|d�d|�|�|�|d
�d|�d}|�|�|d�d|�|�|�|d�d	|�|�|�|d�d�d}|�|�|d�d|�|�|�|d�d|�|�|�|d
�d|�dS)z�
        Test that integers below the maximum C{INT} token size cutoff are
        serialized as C{INT} or C{NEG} and that larger integers are
        serialized as C{LONGINT} or C{LONGNEG}.
        i���r}s�rM�}rL�~r�s���rf�s�s�s��N)rr))rZ	baseIntInZ	baseNegInZ
baseIntOutZbaseLongIntOutZ
baseNegOutZbaseLongNegOutrrr�test_sizedIntegerTypesls$z"BananaTests.test_sizedIntegerTypesN)r r!r"r4r9r;r=r>r:rGrRr�skiprYrZr^r_r`rarcrdrerqrvrurzr{r|r~r�rrrrr6ks8
	
�
		
r6c@sXeZdZdZdZeejje�ej	Z
ed�ej	Zdd�Zdd�Z
dd	�Zd
d�ZdS)
�DialectTestsz2
    Tests for Banana's handling of dialects.
    sremote�zcCs|�t|jj|j�dS)z|
        If no dialect has been selected and a PB VOCAB item is received,
        L{NotImplementedError} is raised.
        N)r?�NotImplementedErrorr-r7�legalPbItemr/rrr�test_dialectNotSet�s
�zDialectTests.test_dialectNotSetcCs.t|jd�|j�|j�|�|j|j�dS)zS
        If the PB dialect has been selected, a PB VOCAB item is accepted.
        �pbN)r#r-r7r�rr1�vocabr/rrr�test_receivePb�szDialectTests.test_receivePbcCs$t|jd�|�t|jj|j�dS)z�
        If the PB dialect has been selected and an unrecognized PB VOCAB item
        is received, L{banana.Banana.dataReceived} raises L{KeyError}.
        r�N)r#r-r?�KeyErrorr7�
illegalPbItemr/rrr�test_receiveIllegalPb�sz"DialectTests.test_receiveIllegalPbcCs2t|jd�|j�|j�|�|j|j���dS)zl
        if pb dialect is selected, the sender must be able to send things in
        that dialect.
        r�N)r#r-r&r�rr�r+rr/rrr�test_sendPb�szDialectTests.test_sendPbN)r r!r"r4r��chrrr5ZoutgoingVocabularyZVOCABr�r�r�r�r�r�rrrrr��s
		r�c@seZdZdZdd�ZdS)�GlobalCoderTestszM
    Tests for the free functions L{banana.encode} and L{banana.decode}.
    cCs0d}|�tjtj|�d}|�t�|�d�dS)zJ
        Calls to L{banana.decode} are independent of each other.
        sB�s�rLN)r?rr@�decoder)rZundecodableZ	decodablerrr�test_statelessDecode�sz%GlobalCoderTests.test_statelessDecodeN)r r!r"r4r�rrrrr��sr�)#Z
__future__rrrs�	functoolsrr+rZ
twisted.trialrZtwisted.spreadrZtwisted.pythonrZtwisted.python.compatr	r
rr�rZtwisted.internetr
rZtwisted.test.proto_helpersrrNrZTestCaserr#r)r*r6r�r�rrrr�<module>s*
 "/