
    Yf7                        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e          Zd Zd Z G d de          Z G d de          Z G d de          Z G d de          ZdS )    N)partial)BytesIO)mainprotocol)StringTransport)failure)	iterbytes)banana)TestCasel    c                       e Zd Zd ZdS )	MathTestsc                    t          t          dd                    t          t          dd                    z   t          t          dd                    z   dgz   }|D ]h}t                      }t          j        ||j                   |                                }t          j        |          }|                     ||           id S )Nr   d   i  iL  i@B iB l                )	listranger   r
   int2b128writegetvalueb1282intassertEqual)self	funkylistixvys         Q/var/www/html/env/lib/python3.11/site-packages/twisted/spread/test/test_banana.pytest_int2b128zMathTests.test_int2b128   s    q#5t$$%%&5'**++, j 	  	# 	#A		AOAqw'''

A""AQ""""	# 	#    N)__name__
__module____qualname__r    r   r   r   r      s#        # # # # #r   r   c                 &     | j         |           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   dialects     r   selectDialectr'   #   s     HG$$$$$r   c                     t                      } |             } |j        |           |                                  |j        |           |                                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
   s       r   encoder0   2   s`      !!I]__FF)$$$OOFs??r   c                   2    e Zd ZdZej        Zd Zd Zd Z	dS )BananaTestBasezn
    The base for test classes. It defines commonly used things and sets up a
    connection for testing.
    c                 J   t                      | _        |                                 | _        | j                            t          j        | j                             t          | j        d           | j        | j        _	        t          t          | j                  | _        d S )Ns   none)r   ioencClassencr)   r   FileWrapperr'   	putResultexpressionReceivedr   r0   r   s    r   setUpzBananaTestBase.setUpP   st    ))==?? 4TW = =>>>dh(((&*n#fdm44r   c                     || _         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=   s     r   r8   zBananaTestBase.putResultX   s     r   c                 v    | j                             t          j        t          j                             | ` d S N)r6   connectionLostr   Failurer   CONNECTION_DONEr:   s    r   tearDownzBananaTestBase.tearDowna   s.    0D E EFFFHHHr   N)
r    r!   r"   __doc__r
   Bananar5   r;   r8   rC   r#   r   r   r2   r2   H   sT         
 }H5 5 5      r   r2   c                       e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd ZdS )BananaTestsz
    General banana tests.
    c                     | j                             d           | j                             | j                                                   | j        dk    sJ d S )Ns   hello)r6   r+   dataReceivedr4   r   r=   r:   s    r   test_stringzBananaTests.test_stringk   sU    X&&&dg..00111{h&&&&&&r   c                 2    |                      dd           dS )z
        Banana does not support unicode.  ``Banana.sendEncoded`` raises
        ``BananaError`` if called with an instance of ``unicode``.
        hellozbuiltins.strN)_unsupportedTypeTestr:   s    r   test_unsupportedUnicodez#BananaTests.test_unsupportedUnicodep   s     
 	!!'>:::::r   c                 <    |                      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)rM   typer:   s    r   test_unsupportedBuiltinTypez'BananaTests.test_unsupportedBuiltinTypew   s      	!!$88888r   c                 Z    |                      t                      t          dz              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)rM   r   r    r:   s    r   test_unsupportedUserTypez$BananaTests.test_unsupportedUserType   s)     	!!)++x,/FGGGGGr   c                     |                      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
   BananaErrorr6   r+   assertInstr)r   r.   nameexcs       r   rM   z BananaTests._unsupportedTypeTest   sO      2DH4H#NN:D:::CHHEEEEEr   c                    | 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)	r6   r+   rI   r4   r   r   r=   assertIsInstanceintr:   s    r   test_intzBananaTests.test_int   st     	U###dg..00111e,,,dk3/////r   c                 B    | j         j        }|dz  }d|z  dz
  }|dz   }|S )N         )r6   prefixLimit)r   bytesbitslargestsmallests        r   _getSmallestzBananaTests._getSmallest   s2    $ qyT'A+Q;r   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)rh   rU   r
   rV   r6   r+   )r   rg   s     r   test_encodeTooLargeLongz#BananaTests.test_encodeTooLargeLong   s;    
 $$&&&,dh.BHMMMMMr   c                    |                                  }| j                            | j        j        dz             | j                            |           | j                                        }| j                            d           | j                            | j        j        dz             |                     t          j
        | j        j        |           dS )zz
        Test that a long above the implementation specific limit is rejected
        as too large to be decoded.
        ra   r   N)rh   r6   setPrefixLimitrc   r+   r4   r   truncaterU   r
   rV   rI   )r   rg   encodeds      r   test_decodeTooLargeLongz#BananaTests.test_decodeTooLargeLong   s    
 $$&& 4q 8999X&&&'""$$ 4 9:::&,dh.CWMMMMMr   c                 ,    |                                   S r?   )rh   r:   s    r   _getLargestzBananaTests._getLargest   s    !!####r   c                     |                                  }|                     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)rq   rU   r
   rV   r6   r+   )r   rf   s     r   test_encodeTooSmallLongz#BananaTests.test_encodeTooSmallLong   s;    
 ""$$&,dh.BGLLLLLr   c                    |                                  }| j                            | j        j        dz             | j                            |           | j                                        }| j                            d           | j                            | j        j        dz             |                     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.
        ra   r   N)rq   r6   rl   rc   r+   r4   r   rm   rU   r
   rV   rI   )r   rf   rn   s      r   test_decodeTooSmallLongz#BananaTests.test_decodeTooSmallLong   s    
 ""$$ 4q 8999W%%%'""$$ 4 9:::&,dh.CWMMMMMr   c                     | j                             d           | j                             | j                                                   |                     | j        d           d S )Ni  r6   r+   rI   r4   r   r   r=   r:   s    r   test_integerzBananaTests.test_integer   sZ    T"""dg..00111d+++++r   c                     | j                             d           | j                             | j                                                   |                     | j        d           d S )Ni	rw   r:   s    r   test_negativezBananaTests.test_negative   sZ    U###dg..00111e,,,,,r   c                     | j                             d           | j                             | j                                                   |                     | j        d           d S )Ng     @rw   r:   s    r   
test_floatzBananaTests.test_float   sZ    V$$$dg..00111f-----r   c           	          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 )Nrb   ra              >@皙D@      six   seven   eight	   
   rw   r   foos     r   	test_listzBananaTests.test_list   s    F4LX!}-D	
 	S!!!dg..00111c*****r   c                 B   ddddgddgddd	d
dggdgt           j        dz  t           j        dz  t           j        dz  g
}| j                            |           |                     | j                                                   |                     | j        |           dS )zj
        Test feeding the data byte per byte to the receiver. Normally
        data is not split.
        rb   ra   r~   r   r   r   r   r   r   r   r   r   N)	sysmaxsizer6   r+   feedr4   r   r   r=   r   s     r   test_partialzBananaTests.test_partial   s     F4LX!}-DK!OK!OK"
 	S!!!		$'""$$%%%c*****r   c                 ^    t          |          D ]}| j                            |           dS )z
        Feed the data byte per byte to the receiver.

        @param data: The bytes to deliver.
        @type data: L{bytes}
        N)r	   r6   rI   )r   databytes      r   r   zBananaTests.feed  s<     dOO 	( 	(DH!!$''''	( 	(r   c                 V    d}|                      t          j        | j        |           d S )Ns   rU   r
   rV   r   r   r   s     r   test_oversizedListzBananaTests.test_oversizedList  *    *&,di>>>>>r   c                 V    d}|                      t          j        | j        |           d S )Ns   r   r   s     r   test_oversizedStringz BananaTests.test_oversizedString!  r   r   c                 l    d}	 | j                             |           d S # t          j        $ r Y d S w xY w)Ns       )r6   rI   r
   rV   )r   crashStrings     r   test_crashStringzBananaTests.test_crashString&  sM    1	H!!+.....! 	 	 	DD	s     33c                     | j                             d           | j                             | j                                                   |                     | j        d           d S )N   rw   r:   s    r   test_crashNegativeLongz"BananaTests.test_crashNegativeLong9  s\     	[)))dg..00111k22222r   c                    d}d}d}|                      |                     |dz
            d|z              |                      |                     |dz
            d|z              |                      |                     |dz
            d	|z              d
}|                      |                     |dz             d|z              |                      |                     |dz             d|z              |                      |                     |dz             d|z              d}|                      |                     |dz             d|z              |                      |                     |dz             d	|z              |                      |                     |dz             d           d}|                      |                     |dz
            d|z              |                      |                     |dz
            d|z              |                      |                     |dz
            d|z              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   ra      }rb      ~r      s             r~      s   s       s         N)r   r0   )r   	baseIntIn	baseNegIn
baseIntOutbaseLongIntOut
baseNegOutbaseLongNegOuts          r   test_sizedIntegerTypesz"BananaTests.test_sizedIntegerTypesE  s(     		,
Y]33Wz5IJJJY]33Wz5IJJJY]33Wz5IJJJ0Y]33W~5MNNNY]33W~5MNNNY]33W~5MNNN,
Y]33Wz5IJJJY]33Wz5IJJJY]335PQQQ0Y]33W~5MNNNY]33W~5MNNNY]33W~5MNNNNNr   N)r    r!   r"   rD   rJ   rN   rQ   rS   rM   r^   rh   rj   ro   rq   rs   ru   rx   rz   r|   r   r   r   r   r   r   r   r   r#   r   r   rG   rG   f   s        ' ' '
; ; ;9 9 9H H HF F F 	0 	0 	0
 
 
N N NN N N$ $ $M M MN N N, , ,
- - -
. . .
+ + ++ + +,( ( (? ? ?
? ? ?
  &
3 
3 
3O O O O Or   rG   c                       e Zd ZdZdZ eej        j        e         f          ej	        z   Z
 ed          ej	        z   Zd Zd Zd Zd ZdS )	DialectTestsz2
    Tests for Banana's handling of dialects.
    s   remote)z   c                 \    |                      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)rU   NotImplementedErrorr6   rI   legalPbItemr:   s    r   test_dialectNotSetzDialectTests.test_dialectNotSetl  s+    
 	-tx/DdFVWWWWWr   c                     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'   r6   rI   r   r   r=   vocabr:   s    r   test_receivePbzDialectTests.test_receivePbs  sO     	dh&&&d.///dj11111r   c                     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'   r6   rU   KeyErrorrI   illegalPbItemr:   s    r   test_receiveIllegalPbz"DialectTests.test_receiveIllegalPb{  s<    
 	dh&&&(DH$94;MNNNNNr   c                     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'   r6   r+   r   r   r   r4   r   r:   s    r   test_sendPbzDialectTests.test_sendPb  s[    
 	dh&&&TZ((()47+;+;+=+=>>>>>r   N)r    r!   r"   rD   r   rd   r
   rE   outgoingVocabularyVOCABr   r   r   r   r   r   r#   r   r   r   r   c  s          E%9%@BCCflRKE&MMFL0MX X X2 2 2O O O? ? ? ? ?r   r   c                       e Zd ZdZd ZdS )GlobalCoderTestszM
    Tests for the free functions L{banana.encode} and L{banana.decode}.
    c                     d}|                      t          j        t          j        |           d}|                     t          j        |          d           dS )zJ
        Calls to L{banana.decode} are independent of each other.
        sB   s   rb   N)rU   r
   rV   decoder   )r   undecodable	decodables      r   test_statelessDecodez%GlobalCoderTests.test_statelessDecode  sS    
 -&,fm[III  	y11155555r   N)r    r!   r"   rD   r   r#   r   r   r   r     s-         6 6 6 6 6r   r   )r   	functoolsr   r4   r   twisted.internetr   r   twisted.internet.testingr   twisted.pythonr   twisted.python.compatr	   twisted.spreadr
   twisted.trial.unittestr   _maxintr   r'   r0   r2   rG   r   r   r#   r   r   <module>r      s  
 


             + + + + + + + + 4 4 4 4 4 4 " " " " " " + + + + + + ! ! ! ! ! ! + + + + + +
# # # # # # # # % % %  ,    X   <zO zO zO zO zO. zO zO zOz'? '? '? '? '?> '? '? '?T6 6 6 6 6x 6 6 6 6 6r   