U
    
W[`<  ã                   @   s  d dl mZmZ d dl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 dlmZmZmZmZ d d	lmZmZ d d
lmZ er’dZnd dlmZ G dd„ de	jƒZdd„ Zdd„ ZG dd„ de	jƒZG dd„ deƒZ G dd„ deƒZ!G dd„ de	jƒZ"dS )é    )Úabsolute_importÚdivisionN)Úpartial)ÚBytesIO)Úunittest)Úbanana)Úfailure)ÚlongÚ	iterbytesÚ	_bytesChrÚ_PY3)ÚprotocolÚmain)ÚStringTransportl   ÿÿÿÿ )Úmaxintc                   @   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   ú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)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   Úencode4   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   ÚsetUpR   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   r1   r   r   r   r.   [   s    zBananaTestBase.putResultc                 C   s   | j  t tj¡¡ | ` d S ©N)r-   ZconnectionLostr   ZFailurer   ZCONNECTION_DONEr/   r   r   r   ÚtearDowne   s    zBananaTestBase.tearDownN)
r    r!   r"   Ú__doc__r   ÚBananar,   r0   r.   r3   r   r   r   r   r*   K   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
erNde
_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/d0„ Zd1d2„ Zd3S )4ÚBananaTestsz
    General banana tests.
    c                 C   s0   | j  d¡ | j  | j ¡ ¡ | jdks,t‚d S )Ns   hello)r-   r&   ÚdataReceivedr+   r   r1   ÚAssertionErrorr/   r   r   r   Útest_stringp   s    zBananaTests.test_stringc                 C   s"   t r|  dd¡ n|  dd¡ dS )z”
        Banana does not support unicode.  ``Banana.sendEncoded`` raises
        ``BananaError`` if called with an instance of ``unicode``.
        Zhellozbuiltins.strz__builtin__.unicodeN)r   Ú_unsupportedTypeTestr/   r   r   r   Útest_unsupportedUnicodev   s    z#BananaTests.test_unsupportedUnicodec                 C   s"   t r|  td¡ n|  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.typez__builtin__.typeN)r   r:   Útyper/   r   r   r   Útest_unsupportedBuiltinType   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)r:   r   r    r/   r   r   r   Útest_unsupportedUserTypeŽ   s    z$BananaTests.test_unsupportedUserTypec                 C   s.   |   tj| jj|¡}|  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 {0} objectsN)ÚassertRaisesr   ÚBananaErrorr-   r&   ZassertInÚformatÚstr)r   r(   ÚnameÚexcr   r   r   r:   —   s    z BananaTests._unsupportedTypeTestc                 C   sP   dt dƒfD ]>}| j |¡ | j | j ¡ ¡ |  | jd¡ |  | jt	¡ q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&   r7   r+   r   r   r1   ÚassertIsInstanceÚint)r   r'   r   r   r   Útest_int¨   s
    zBananaTests.test_intc                 C   sŒ   dD ]‚}dD ]x}d| | }|| d fD ]X}| j  |  |¡¡ |  | j|¡ |tksd|t d k rt|  | jt¡ q*|  | jt¡ q*qqdS )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   é   é   rL   N)	r-   r7   r)   r   r1   Ú_maxintrE   r	   rF   )r   ZexpÚaddÚmÚnr   r   r   Útest_largeLongµ   s    zBananaTests.test_largeLongz@Python 3 has unified int/long into an int type of unlimited sizec                 C   s(   | j j}|d }d| d }|d }|S )Né   rM   rL   )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)rY   r?   r   r@   r-   r&   )r   rX   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.
        rM   r   N)rY   r-   ÚsetPrefixLimitrT   r&   r+   r   Útruncater?   r   r@   r7   )r   rX   Úencodedr   r   r   Útest_decodeTooLargeLongâ   s    
z#BananaTests.test_decodeTooLargeLongc                 C   s
   |   ¡  S r2   )rY   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)r_   r?   r   r@   r-   r&   )r   rW   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.
        rM   r   N)r_   r-   r[   rT   r&   r+   r   r\   r?   r   r@   r7   )r   rW   r]   r   r   r   Útest_decodeTooSmallLongþ   s    
z#BananaTests.test_decodeTooSmallLongc                 C   s0   | j  d¡ | j  | j ¡ ¡ |  | jd¡ d S )Ni÷  ©r-   r&   r7   r+   r   r   r1   r/   r   r   r   Útest_integer  s    zBananaTests.test_integerc                 C   s0   | j  d¡ | j  | j ¡ ¡ |  | jd¡ d S )Ni	üÿÿrb   r/   r   r   r   Útest_negative  s    zBananaTests.test_negativec                 C   s0   | j  d¡ | j  | j ¡ ¡ |  | jd¡ d S )Ng     ¸@rb   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 )NrL   rM   é   é   ç     €>@çš™™™™D@é   ó   sixó   sevenó   eighté	   é
   rb   ©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.
        rL   rM   rf   rg   rh   ri   rj   rk   rl   rm   rn   ro   éþÿÿÿN)	ÚsysÚmaxsizer-   r&   Úfeedr+   r   r   r1   rp   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-   r7   )r   ÚdataZbyter   r   r   ru   5  s    zBananaTests.feedc                 C   s   d}|   tj| j|¡ d S )Ns   €©r?   r   r@   ru   ©r   rw   r   r   r   Útest_oversizedList@  s    zBananaTests.test_oversizedListc                 C   s   d}|   tj| j|¡ d S )Ns   ‚rx   ry   r   r   r   Útest_oversizedStringF  s    z BananaTests.test_oversizedStringc                 C   s0   d}z| j  |¡ W n tjk
r*   Y nX d S )Ns       €)r-   r7   r   r@   )r   ZcrashStringr   r   r   Útest_crashStringL  s
    zBananaTests.test_crashStringc                 C   s0   | j  d¡ | j  | j ¡ ¡ |  | jd¡ d S )Né   €rb   r/   r   r   r   Útest_crashNegativeLong_  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   rM   ó   }rL   ó   ~r   ó   s      …ó    ó   rf   ó   s   ƒs       ƒs      †ó   N)r   r)   )r   Z	baseIntInZ	baseNegInZ
baseIntOutZbaseLongIntOutZ
baseNegOutZbaseLongNegOutr   r   r   Útest_sizedIntegerTypesl  s$    z"BananaTests.test_sizedIntegerTypesN)r    r!   r"   r4   r9   r;   r=   r>   r:   rG   rR   r   ÚskiprY   rZ   r^   r_   r`   ra   rc   rd   re   rq   rv   ru   rz   r{   r|   r~   r†   r   r   r   r   r6   k   s8   	ÿ		r6   c                   @   sX   e Zd ZdZdZeejje ƒ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)r?   ÚNotImplementedErrorr-   r7   ÚlegalPbItemr/   r   r   r   Útest_dialectNotSet“  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-   r7   r‹   r   r1   Úvocabr/   r   r   r   Útest_receivePb  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-   r?   ÚKeyErrorr7   Ú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.
        r   N)r#   r-   r&   rŽ   r   r‹   r+   r   r/   r   r   r   Útest_sendPb¯  s    zDialectTests.test_sendPbN)r    r!   r"   r4   rŽ   Úchrr   r5   ZoutgoingVocabularyZVOCABr‹   r‘   rŒ   r   r’   r“   r   r   r   r   rˆ   ‹  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   rL   N)r?   r   r@   Údecoder   )r   ZundecodableZ	decodabler   r   r   Útest_statelessDecode¾  s    z%GlobalCoderTests.test_statelessDecodeN)r    r!   r"   r4   r—   r   r   r   r   r•   º  s   r•   )#Z
__future__r   r   rs   Ú	functoolsr   r+   r   Ztwisted.trialr   Ztwisted.spreadr   Ztwisted.pythonr   Ztwisted.python.compatr	   r
   r   r”   r   Ztwisted.internetr   r   Ztwisted.test.proto_helpersr   rN   r   ZTestCaser   r#   r)   r*   r6   rˆ   r•   r   r   r   r   Ú<module>   s*      "/