a
    )¶(bï7  ã                   @   sÔ   d dl 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 d dlmZ d d	lmZ d
ZG dd„ deƒZdd„ Zdd„ ZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdS )é    N)Úpartial)ÚBytesIO)ÚmainÚprotocol)Úfailure)Ú	iterbytes)Úbanana)ÚStringTransport)ÚTestCasel   ÿÿÿÿ c                   @   s   e Zd Zdd„ ZdS )Ú	MathTestsc                 C   sp   t tddƒƒt tddƒƒ t tddƒƒ dg }|D ]6}tƒ }t ||j¡ | ¡ }t |¡}|  ||¡ q4d S )Nr   éd   iè  iL  i@B i¤B l                )	ÚlistÚranger   r   Zint2b128ÚwriteÚgetvalueZb1282intÚassertEqual)ÚselfZ	funkylistÚiÚxÚvÚy© r   ú>lib/python3.9/site-packages/twisted/spread/test/test_banana.pyÚtest_int2b128   s    ÿþýÿ
zMathTests.test_int2b128N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r      s   r   c                 C   s   |   |¡ dS )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   Zdialectr   r   r   ÚselectDialect#   s    r   c                 C   s0   t ƒ }| ƒ }| |¡ | ¡  | |¡ | ¡ 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	transportr   r   r   r   Úencode2   s    

r#   c                   @   s.   e Zd ZdZejZdd„ Zdd„ Zdd„ Z	dS )	ÚBananaTestBasezn
    The base for test classes. It defines commonly used things and sets up a
    connection for testing.
    c                 C   sN   t ƒ | _|  ¡ | _| j t | j¡¡ t| jdƒ | j| j_	t
t| jƒ| _d S )Ns   none)r   ÚioÚencClassÚencr   r   ZFileWrapperr   Ú	putResultZexpressionReceivedr   r#   ©r   r   r   r   ÚsetUpP   s    

zBananaTestBase.setUpc                 C   s
   || _ dS )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)r   r+   r   r   r   r(   X   s    zBananaTestBase.putResultc                 C   s   | j  t tj¡¡ | ` d S ©N)r'   ZconnectionLostr   ZFailurer   ZCONNECTION_DONEr)   r   r   r   ÚtearDowna   s    zBananaTestBase.tearDownN)
r   r   r   Ú__doc__r   ÚBananar&   r*   r(   r-   r   r   r   r   r$   H   s
   	r$   c                   @   sÈ   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Zd*d+„ Zd,d-„ Zd.d/„ Zd0S )1ÚBananaTestsz
    General banana tests.
    c                 C   s0   | j  d¡ | j  | j ¡ ¡ | jdks,J ‚d S )Ns   hello)r'   r    ÚdataReceivedr%   r   r+   r)   r   r   r   Útest_stringk   s    zBananaTests.test_stringc                 C   s   |   dd¡ dS )z”
        Banana does not support unicode.  ``Banana.sendEncoded`` raises
        ``BananaError`` if called with an instance of ``unicode``.
        Zhellozbuiltins.strN)Ú_unsupportedTypeTestr)   r   r   r   Útest_unsupportedUnicodep   s    z#BananaTests.test_unsupportedUnicodec                 C   s   |   td¡ dS )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.typeN)r3   Útyper)   r   r   r   Útest_unsupportedBuiltinTypew   s    z'BananaTests.test_unsupportedBuiltinTypec                 C   s   |   tƒ td ¡ dS )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)r3   r   r   r)   r   r   r   Útest_unsupportedUserType€   s    z$BananaTests.test_unsupportedUserTypec                 C   s0   |   tj| jj|¡}|  d|› dt|ƒ¡ dS )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.
        zBanana cannot send z objectsN)ÚassertRaisesr   ÚBananaErrorr'   r    ZassertInÚstr)r   r"   ÚnameÚexcr   r   r   r3   ˆ   s    z BananaTests._unsupportedTypeTestc                 C   s>   | j  d¡ | j  | j ¡ ¡ |  | jd¡ |  | jt¡ dS )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    r1   r%   r   r   r+   ZassertIsInstanceÚintr)   r   r   r   Útest_int˜   s    zBananaTests.test_intc                 C   s(   | j j}|d }d| d }|d }|S )Né   é   é   )r'   ÚprefixLimit)r   ÚbytesÚbitsÚlargestÚsmallestr   r   r   Ú_getSmallest£   s
    zBananaTests._getSmallestc                 C   s    |   ¡ }|  tj| jj|¡ dS )zz
        Test that a long above the implementation-specific limit is rejected
        as too large to be encoded.
        N)rG   r8   r   r9   r'   r    )r   rF   r   r   r   Útest_encodeTooLargeLong¯   s    z#BananaTests.test_encodeTooLargeLongc                 C   sj   |   ¡ }| j | jjd ¡ | j |¡ | j ¡ }| j d¡ | j | jjd ¡ |  t	j
| jj|¡ dS )zz
        Test that a long above the implementation specific limit is rejected
        as too large to be decoded.
        r@   r   N)rG   r'   ÚsetPrefixLimitrB   r    r%   r   Útruncater8   r   r9   r1   )r   rF   Úencodedr   r   r   Útest_decodeTooLargeLong·   s    
z#BananaTests.test_decodeTooLargeLongc                 C   s
   |   ¡  S r,   )rG   r)   r   r   r   Ú_getLargestÅ   s    zBananaTests._getLargestc                 C   s    |   ¡ }|  tj| jj|¡ dS )zƒ
        Test that a negative long below the implementation-specific limit is
        rejected as too small to be encoded.
        N)rM   r8   r   r9   r'   r    )r   rE   r   r   r   Útest_encodeTooSmallLongÈ   s    z#BananaTests.test_encodeTooSmallLongc                 C   sj   |   ¡ }| j | jjd ¡ | j |¡ | j ¡ }| j d¡ | j | jjd ¡ |  t	j
| jj|¡ dS )zƒ
        Test that a negative long below the implementation specific limit is
        rejected as too small to be decoded.
        r@   r   N)rM   r'   rI   rB   r    r%   r   rJ   r8   r   r9   r1   )r   rE   rK   r   r   r   Útest_decodeTooSmallLongÐ   s    
z#BananaTests.test_decodeTooSmallLongc                 C   s0   | j  d¡ | j  | j ¡ ¡ |  | jd¡ d S )Ni÷  ©r'   r    r1   r%   r   r   r+   r)   r   r   r   Útest_integerÞ   s    zBananaTests.test_integerc                 C   s0   | j  d¡ | j  | j ¡ ¡ |  | jd¡ d S )Ni	üÿÿrP   r)   r   r   r   Útest_negativeã   s    zBananaTests.test_negativec                 C   s0   | j  d¡ | j  | j ¡ ¡ |  | jd¡ d S )Ng     ¸@rP   r)   r   r   r   Ú
test_floatè   s    zBananaTests.test_floatc              	   C   sX   ddddgddgddd	d
dggdgg g}| j  |¡ | j  | j ¡ ¡ |  | j|¡ d S )NrA   r@   é   é   ç     €>@çš™™™™D@é   ó   sixó   sevenó   eighté	   é
   rP   ©r   Zfoor   r   r   Ú	test_listí   s    ø
zBananaTests.test_listc                 C   sl   ddddgddgddd	d
dggdgt jd t jd t jd g
}| j |¡ |  | j ¡ ¡ |  | j|¡ dS )zj
        Test feeding the data byte per byte to the receiver. Normally
        data is not split.
        rA   r@   rT   rU   rV   rW   rX   rY   rZ   r[   r\   r]   éþÿÿÿN)	ÚsysÚmaxsizer'   r    Úfeedr%   r   r   r+   r^   r   r   r   Útest_partialü   s    õzBananaTests.test_partialc                 C   s   t |ƒD ]}| j |¡ qdS )z‡
        Feed the data byte per byte to the receiver.

        @param data: The bytes to deliver.
        @type data: L{bytes}
        N)r   r'   r1   )r   ÚdataZbyter   r   r   rc     s    zBananaTests.feedc                 C   s   d}|   tj| j|¡ d S )Ns   €©r8   r   r9   rc   ©r   re   r   r   r   Útest_oversizedList  s    zBananaTests.test_oversizedListc                 C   s   d}|   tj| j|¡ d S )Ns   ‚rf   rg   r   r   r   Útest_oversizedString!  s    z BananaTests.test_oversizedStringc                 C   s.   d}z| j  |¡ W n tjy(   Y n0 d S )Ns       €)r'   r1   r   r9   )r   ZcrashStringr   r   r   Útest_crashString&  s
    zBananaTests.test_crashStringc                 C   s0   | j  d¡ | j  | j ¡ ¡ |  | jd¡ d S )Né   €rP   r)   r   r   r   Útest_crashNegativeLong9  s    z"BananaTests.test_crashNegativeLongc                 C   sP  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 ¡d| ¡ dS )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ÿÿÿrk   s   r@   ó   }rA   ó   ~r   ó   s      …ó    ó   rT   ó   s   ƒs       ƒs      †ó   N)r   r#   )r   Z	baseIntInZ	baseNegInZ
baseIntOutZbaseLongIntOutZ
baseNegOutZbaseLongNegOutr   r   r   Útest_sizedIntegerTypesE  s$    z"BananaTests.test_sizedIntegerTypesN)r   r   r   r.   r2   r4   r6   r7   r3   r>   rG   rH   rL   rM   rN   rO   rQ   rR   rS   r_   rd   rc   rh   ri   rj   rl   rt   r   r   r   r   r0   f   s0   	
r0   c                   @   sZ   e Zd ZdZdZeejje fƒej	 Z
edƒej	 Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚDialectTestsz2
    Tests for Banana's handling of dialects.
    s   remote)éz   c                 C   s   |   t| jj| j¡ dS )z|
        If no dialect has been selected and a PB VOCAB item is received,
        L{NotImplementedError} is raised.
        N)r8   ÚNotImplementedErrorr'   r1   ÚlegalPbItemr)   r   r   r   Útest_dialectNotSetl  s    zDialectTests.test_dialectNotSetc                 C   s.   t | jdƒ | j | j¡ |  | j| j¡ dS )zS
        If the PB dialect has been selected, a PB VOCAB item is accepted.
        ó   pbN)r   r'   r1   rx   r   r+   Úvocabr)   r   r   r   Útest_receivePbs  s    zDialectTests.test_receivePbc                 C   s$   t | jdƒ |  t| jj| j¡ dS )zž
        If the PB dialect has been selected and an unrecognized PB VOCAB item
        is received, L{banana.Banana.dataReceived} raises L{KeyError}.
        rz   N)r   r'   r8   ÚKeyErrorr1   ÚillegalPbItemr)   r   r   r   Útest_receiveIllegalPb{  s    z"DialectTests.test_receiveIllegalPbc                 C   s2   t | jdƒ | j | j¡ |  | j| j ¡ ¡ dS )zl
        if pb dialect is selected, the sender must be able to send things in
        that dialect.
        rz   N)r   r'   r    r{   r   rx   r%   r   r)   r   r   r   Útest_sendPbƒ  s    zDialectTests.test_sendPbN)r   r   r   r.   r{   rC   r   r/   ZoutgoingVocabularyZVOCABrx   r~   ry   r|   r   r€   r   r   r   r   ru   c  s   ru   c                   @   s   e Zd ZdZdd„ ZdS )ÚGlobalCoderTestszM
    Tests for the free functions L{banana.encode} and L{banana.decode}.
    c                 C   s0   d}|   tjtj|¡ d}|  t |¡d¡ dS )zJ
        Calls to L{banana.decode} are independent of each other.
        sB   …s   rA   N)r8   r   r9   Údecoder   )r   ZundecodableZ	decodabler   r   r   Útest_statelessDecode’  s    z%GlobalCoderTests.test_statelessDecodeN)r   r   r   r.   rƒ   r   r   r   r   r     s   r   )ra   Ú	functoolsr   r%   r   Ztwisted.internetr   r   Ztwisted.pythonr   Ztwisted.python.compatr   Ztwisted.spreadr   Ztwisted.test.proto_helpersr	   Ztwisted.trial.unittestr
   Z_maxintr   r   r#   r$   r0   ru   r   r   r   r   r   Ú<module>   s"    ~*