o
    ¯bç(  ã                   @   sL   d dl mZ d dlmZ d dlmZ G dd„ dejƒZG dd„ dejƒZ	dS )	é    )Úprotocol)Úrawudp)Úunittestc                   @   s   e Zd Zdd„ Zdd„ ZdS )Ú
MyProtocolc                 C   s   t |ƒ| _d S )N)ÚlistÚ	expecting)Úselfr   © r	   ú?/usr/lib/python3/dist-packages/twisted/pair/test/test_rawudp.pyÚ__init__   s   zMyProtocol.__init__c                 C   sx   |\}}| j sJ dƒ‚| j  d¡\}}}||ks J d ||¡ƒ‚||ks,J d ||¡ƒ‚||ks:J d||||f ƒ‚d S )Nz(Got a packet when not expecting anymore.r   zExpected data {!r}, got {!r}zExpected host {!r}, got {!r}z&Expected port %d=0x%04x, got %d=0x%04x)r   ÚpopÚformat)r   ÚdataÚpeerÚhostÚportÚ
expectDataÚ
expectHostÚ
expectPortr	   r	   r
   ÚdatagramReceived   s   ÿÿüzMyProtocol.datagramReceivedN)Ú__name__Ú
__module__Ú__qualname__r   r   r	   r	   r	   r
   r      s    r   c                   @   sT   e 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S )ÚRawUDPTestsc                 C   s\   t  ¡ }tdgƒ}| d|¡ |jddddddddddddddd� |jr,J d|j ƒ‚d S ©	N©ó   foobaró   testHosti¢C  éð  ó   C¢ð Þ­foobarr   ó   dummyr   ©ÚpartialÚdestÚsourcer   ÚversionÚihlÚtosÚtot_lenÚfragment_idÚfragment_offsetÚdont_fragmentÚmore_fragmentsÚttlú5Should not expect any more packets, but still want %r©r   ÚRawUDPProtocolr   ÚaddProtor   r   ©r   ÚprotoÚp1r	   r	   r
   ÚtestPacketParsing#   s2   ÿÿîÿzRawUDPTests.testPacketParsingc                 C   s„   t  ¡ }tddgƒ}| d|¡ |jddddddddddddddd� |jd	ddd
ddddddddddd� |jr@J d|j ƒ‚d S )Nr   ©ó   quuxó	   otherHostiþ3  r   r   r   r    r   r!   ó   3þð Þ­quuxr8   r.   r/   r2   r	   r	   r
   ÚtestMultiplePacketsE   sT   þÿîîÿzRawUDPTests.testMultiplePacketsc                 C   s†   t  ¡ }tdgƒ}tdgƒ}| d|¡ | d|¡ |jddddddddddddddd� |jr7J d|j ƒ‚|jrAJ d|j ƒ‚d S r   r/   ©r   r3   r4   Úp2r	   r	   r
   ÚtestMultipleSameProtos{   sB   ÿÿÿÿîÿÿz"RawUDPTests.testMultipleSameProtosc                 C   sF   t  ¡ }tg ƒ}| d|¡ |jddddddddddddddd� d S )Né   r   r   r    r   r!   )r   r0   r   r1   r   r2   r	   r	   r
   ÚtestWrongProtoNotSeen¨   s&   
îz!RawUDPTests.testWrongProtoNotSeenc                 C   sü   t  ¡ }tddgƒ}| d|¡ tddgƒ}| d|¡ |jddd	d
d	d	d	d	d	d	d	d	d	d	d� |jddd	dd	d	d	d	d	d	d	d	d	d	d� |jddd	d
d	d	d	d	d	d	d	d	d	d	d� |jddd	dd	d	d	d	d	d	d	d	d	d	d� |jrrJ d|j ƒ‚|jr|J d|j ƒ‚d S )Nr   r6   r   )r7   r8   i¤  )r   r   i£  iP°  s   ¤°P Þ­quuxr   r    r8   r!   r   r   r9   s   £°P Þ­foobarr.   r/   r;   r	   r	   r
   ÚtestDemuxingÂ   s¦   þÿþÿîîîîÿÿzRawUDPTests.testDemuxingc              
   C   sT   t  ¡ }z| dd¡ W tdƒ‚ ty) } z|jdkrn‚ W Y d}~dS d}~ww )z2Adding a wrong level protocol raises an exception.é*   Ú	silliness)z6Added protocol must be an instance of DatagramProtocolNú2addProto must raise an exception for bad protocols)r   r0   r1   Ú	TypeErrorÚargsÚAssertionError©r   Úer	   r	   r
   ÚtestAddingBadProtos_WrongLevel,  s   ú
þ€þz*RawUDPTests.testAddingBadProtos_WrongLevelc              
   C   óX   t  ¡ }z| dt ¡ ¡ W tdƒ‚ ty+ } z|jdkrn‚ W Y d}~dS d}~ww )z=Adding a protocol with a negative number raises an exception.éÿÿÿÿ)z'Added protocol must be positive or zeroNrC   ©r   r0   r1   r   ÚDatagramProtocolrD   rE   rF   rG   r	   r	   r
   ÚtestAddingBadProtos_TooSmall9  ó   ú
þ€þz(RawUDPTests.testAddingBadProtos_TooSmallc              
   C   rJ   )ú<Adding a protocol with a number >=2**16 raises an exception.i   ©z"Added protocol must fit in 16 bitsNrC   rL   rG   r	   r	   r
   ÚtestAddingBadProtos_TooBigF  rO   z&RawUDPTests.testAddingBadProtos_TooBigc              
   C   rJ   )rP   i  rQ   NrC   rL   rG   r	   r	   r
   ÚtestAddingBadProtos_TooBig2S  rO   z'RawUDPTests.testAddingBadProtos_TooBig2N)r   r   r   r5   r:   r=   r?   r@   rI   rN   rR   rS   r	   r	   r	   r
   r   "   s    "6-jr   N)
Útwisted.internetr   Útwisted.pairr   Útwisted.trialr   rM   r   ÚTestCaser   r	   r	   r	   r
   Ú<module>   s
   