U
    c6{iù  ã                   @   sb   d dl mZ d dlmZmZ d dlmZ d dlmZ eej	ƒG dd„ dƒƒZ
G dd„ dejƒZd	S )
é    )Úimplementer)ÚethernetÚraw)Ú
components)Úunittestc                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )Ú
MyProtocolc                 C   s   t |ƒ| _d S )N)ÚlistÚ	expecting)Úselfr	   © r   úC/tmp/pip-unpacked-wheel-hfbvu0bz/twisted/pair/test/test_ethernet.pyÚ__init__   s    zMyProtocol.__init__c                 C   s   dS )z!
        Not implemented
        Nr   )r
   ÚnumÚprotor   r   r   ÚaddProto   s    zMyProtocol.addProtoc           	      C   sR   | j stdƒ‚| j  d¡}tƒ }||||dœ}|||fksNtd |||f¡ƒ‚d S )Nz(Got a packet when not expecting anymore.r   ©ÚpartialÚdestÚsourceÚprotocolzExpected {!r}, got {!r})r	   ÚAssertionErrorÚpopÚlocalsÚformat)	r
   Údatar   r   r   r   ÚexpectZlocalVariablesÚparamsr   r   r   ÚdatagramReceived   s    ü ÿzMyProtocol.datagramReceivedN)Ú__name__Ú
__module__Ú__qualname__r   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 )ÚEthernetTestsc                 C   sR   t  ¡ }tddddddœfgƒ}| d|¡ |jddd� |jrNtd	|j ƒ‚d S )
Nó   foobarr   ó   123456ó   987654é   r   ó   123456987654 foobar©r   ú5Should not expect any more packets, but still want %r©r   ÚEthernetProtocolr   r   r   r	   r   ©r
   r   Úp1r   r   r   ÚtestPacketParsing$   s     üþÿÿÿzEthernetTests.testPacketParsingc                 C   sp   t  ¡ }tddddddœfddd	d
ddœfgƒ}| d|¡ |jddd� |jddd� |jrltd|j ƒ‚d S )Nr"   r   r#   r$   r%   r   ó   quuxé   ó   012345ó   abcdefr&   r'   ó   012345abcdef quuxr(   r)   r+   r   r   r   ÚtestMultiplePackets;   s0    üþ
üþöÿÿz!EthernetTests.testMultiplePacketsc                 C   sŽ   t  ¡ }tddddddœfgƒ}tddddddœfgƒ}| d|¡ | d|¡ |jddd� |jrttd	 |j¡ƒ‚|jrŠtd	 |j¡ƒ‚d S )
Nr"   r   r#   r$   r%   r   r&   r'   z7Should not expect any more packets, but still want {!r})r   r*   r   r   r   r	   r   r   ©r
   r   r,   Úp2r   r   r   ÚtestMultipleSameProtos\   sH    üþÿÿüþÿÿÿÿþÿÿþz$EthernetTests.testMultipleSameProtosc                 C   s<   t  ¡ }tg ƒ}| d|¡ |jddd� |jddd� d S )Ni  r&   r   r'   r2   r/   )r   r*   r   r   r   r+   r   r   r   ÚtestWrongProtoNotSeenŠ   s
    z#EthernetTests.testWrongProtoNotSeenc                 C   sÔ   t  ¡ }tddddddœfddd	d
ddœfgƒ}| d|¡ tddd	d
ddœfddddddœfgƒ}| d|¡ |jddd� |jddd� |jddd� |jddd� |jr¼td|j ƒ‚|jrÐtd|j ƒ‚d S )Nr"   r   r#   r$   r%   r   r.   r/   r0   r1   i  r&   r'   s   012345abcdefquuxs   123456987654foobarr2   r(   r)   r4   r   r   r   ÚtestDemuxing’   s^    üþ
üþöÿüþ
üþöÿÿÿzEthernetTests.testDemuxingc                 C   s<   t  ¡ }z| dd¡ W n tjk
r.   Y n
X tdƒ‚dS )z2Adding a wrong level protocol raises an exception.é*   Z	sillinessú2addProto must raise an exception for bad protocolsN)r   r*   r   r   ZCannotAdaptr   ©r
   Úer   r   r   ÚtestAddingBadProtos_WrongLevelÐ   s    z,EthernetTests.testAddingBadProtos_WrongLevelc              
   C   sZ   t  ¡ }z| dtg ƒ¡ W n0 tk
rL } z|jdkr:n‚ W 5 d}~X Y n
X tdƒ‚dS )z=Adding a protocol with a negative number raises an exception.éÿÿÿÿ)z'Added protocol must be positive or zeroNr:   ©r   r*   r   r   Ú	TypeErrorÚargsr   r;   r   r   r   ÚtestAddingBadProtos_TooSmallÚ   s    
z*EthernetTests.testAddingBadProtos_TooSmallc              
   C   sZ   t  ¡ }z| dtg ƒ¡ W n0 tk
rL } z|jdkr:n‚ W 5 d}~X Y n
X tdƒ‚dS )ú<Adding a protocol with a number >=2**16 raises an exception.i   ©z"Added protocol must fit in 16 bitsNr:   r?   r;   r   r   r   ÚtestAddingBadProtos_TooBigç   s    
z(EthernetTests.testAddingBadProtos_TooBigc              
   C   sZ   t  ¡ }z| dtg ƒ¡ W n0 tk
rL } z|jdkr:n‚ W 5 d}~X Y n
X tdƒ‚dS )rC   i  rD   Nr:   r?   r;   r   r   r   ÚtestAddingBadProtos_TooBig2ô   s    
z)EthernetTests.testAddingBadProtos_TooBig2N)r   r   r    r-   r3   r6   r7   r8   r=   rB   rE   rF   r   r   r   r   r!   #   s   !.>
r!   N)Zzope.interfacer   Ztwisted.pairr   r   Ztwisted.pythonr   Ztwisted.trialr   ZIRawPacketProtocolr   ZTestCaser!   r   r   r   r   Ú<module>   s   