o
    b7                     @   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 ]}t }t||j | }t|}| || qd S )Nr   d   i  iL  i@B iB l                )	listranger   r   int2b128writegetvalueb1282intassertEqual)self	funkylistixvy r   A/usr/lib/python3/dist-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)_selectDialect)r   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)bananaFactoryobj	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   FileWrapperr"   	putResult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   r4   r   r   r   r0   X   s   
zBananaTestBase.putResultc                 C   s   | j ttj | ` d S N)r.   connectionLostr   Failurer   CONNECTION_DONEr2   r   r   r   tearDowna   s   zBananaTestBase.tearDownN)
r   r   r   __doc__r   Bananar-   r3   r0   r9   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   r4   r2   r   r   r   test_stringk      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``.
        hellozbuiltins.strN)_unsupportedTypeTestr2   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)rA   typer2   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)rA   r   r   r2   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%   assertInstr)r   r(   nameexcr   r   r   rA      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%   r=   r,   r   r   r4   assertIsInstanceintr2   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       |   }| 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)rW   rF   r   rG   r.   r%   )r   rV   r   r   r   test_encodeTooLargeLong      z#BananaTests.test_encodeTooLargeLongc                 C   j   |   }| 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.
        rP   r   N)rW   r.   setPrefixLimitrR   r%   r,   r   truncaterF   r   rG   r=   )r   rV   encodedr   r   r   test_decodeTooLargeLong      
z#BananaTests.test_decodeTooLargeLongc                 C   s
   |    S r5   )rW   r2   r   r   r   _getLargest   s   
zBananaTests._getLargestc                 C   rX   )z
        Test that a negative long below the implementation-specific limit is
        rejected as too small to be encoded.
        N)ra   rF   r   rG   r.   r%   )r   rU   r   r   r   test_encodeTooSmallLong   rZ   z#BananaTests.test_encodeTooSmallLongc                 C   r[   )z
        Test that a negative long below the implementation specific limit is
        rejected as too small to be decoded.
        rP   r   N)ra   r.   r\   rR   r%   r,   r   r]   rF   r   rG   r=   )r   rU   r^   r   r   r   test_decodeTooSmallLong   r`   z#BananaTests.test_decodeTooSmallLongc                 C   0   | j d | j | j  | | jd d S )Ni  r.   r%   r=   r,   r   r   r4   r2   r   r   r   test_integer   r?   zBananaTests.test_integerc                 C   rd   )Ni	re   r2   r   r   r   test_negative   r?   zBananaTests.test_negativec                 C   rd   )Ng     @re   r2   r   r   r   
test_float   r?   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 )NrQ   rP              >@皙D@      six   seven   eight	   
   re   r   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.
        rQ   rP   ri   rj   rk   rl   rm   rn   ro   rp   rq   rr   N)	sysmaxsizer.   r%   feedr,   r   r   r4   rs   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.   r=   )r   databyter   r   r   ry     s   zBananaTests.feedc                 C      d}|  tj| j| d S )Ns   rF   r   rG   ry   r   r{   r   r   r   test_oversizedList     zBananaTests.test_oversizedListc                 C   r}   )Ns   r~   r   r   r   r   test_oversizedString!  r   z BananaTests.test_oversizedStringc                 C   s.   d}z	| j | W d S  tjy   Y d S w )Ns       )r.   r=   r   rG   )r   crashStringr   r   r   test_crashString&  s   zBananaTests.test_crashStringc                 C   rd   )N   re   r2   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r   s   rP      }rQ      ~r      s             ri      s   s       s         N)r   r*   )r   	baseIntIn	baseNegIn
baseIntOutbaseLongIntOut
baseNegOutbaseLongNegOutr   r   r   test_sizedIntegerTypesE  s$   z"BananaTests.test_sizedIntegerTypesN)r   r   r   r:   r>   rB   rD   rE   rA   rN   rW   rY   r_   ra   rb   rc   rf   rg   rh   ru   rz   ry   r   r   r   r   r   r   r   r   r   r<   f   s2    	
r<   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)rF   NotImplementedErrorr.   r=   legalPbItemr2   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.   r=   r   r   r4   vocabr2   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}.
        r   N)r"   r.   rF   KeyErrorr=   illegalPbItemr2   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.
        r   N)r"   r.   r%   r   r   r   r,   r   r2   r   r   r   test_sendPb  s   zDialectTests.test_sendPbN)r   r   r   r:   r   rS   r   r;   outgoingVocabularyVOCABr   r   r   r   r   r   r   r   r   r   r   c  s    r   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   rQ   N)rF   r   rG   decoder   )r   undecodable	decodabler   r   r   test_statelessDecode  s   z%GlobalCoderTests.test_statelessDecodeN)r   r   r   r:   r   r   r   r   r   r     s    r   )rw   	functoolsr   r,   r   twisted.internetr   r   twisted.pythonr   twisted.python.compatr   twisted.spreadr   twisted.test.proto_helpersr	   twisted.trial.unittestr
   _maxintr   r"   r*   r+   r<   r   r   r   r   r   r   <module>   s$    ~*