o
    ¯bJ  ã                   @   sR   d 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
 G dd„ dejƒZd	S )
z7
Test cases for L{twisted.protocols.haproxy.V1Parser}.
é    )Úaddress)Úunittesté   )Ú	_v1parser)ÚInvalidNetworkProtocolÚInvalidProxyHeaderÚMissingAddressDatac                   @   st   e Zd ZdZddd„Zddd„Zddd	„Zdd
d„Zddd„Zddd„Z	ddd„Z
ddd„Zddd„Zddd„ZdS )ÚV1ParserTestsz?
    Test L{twisted.protocols.haproxy.V1Parser} behaviour.
    ÚreturnNc                 C   ó   |   ttjjd¡ dS )zT
        Test that an exception is raised when the PROXY header is missing.
        s	   NOTPROXY N)ÚassertRaisesr   r   ÚV1ParserÚparse©Úself© r   úN/usr/lib/python3/dist-packages/twisted/protocols/haproxy/test/test_v1parser.pyÚtest_missingPROXYHeaderValue   ó
   ýz*V1ParserTests.test_missingPROXYHeaderValuec                 C   r   )zX
        Test that an exception is raised when the proto is not TCP or UNKNOWN.
        s   PROXY WUTPROTO N)r   r   r   r   r   r   r   r   r   Útest_invalidNetworkProtocol   r   z)V1ParserTests.test_invalidNetworkProtocolc                 C   r   )zU
        Test that an exception is raised when the proto has no source data.
        s   PROXY TCP4 N©r   r   r   r   r   r   r   r   r   Útest_missingSourceData'   r   z$V1ParserTests.test_missingSourceDatac                 C   r   )zU
        Test that an exception is raised when the proto has no destination.
        s   PROXY TCP4 127.0.0.1 8080 8888Nr   r   r   r   r   Útest_missingDestData1   r   z"V1ParserTests.test_missingDestDatac                 C   ó„   t j d¡}|  |jtj¡ t|jtjƒsJ ‚t|jtjƒs J ‚|  	|jj
d¡ |  	|jjd¡ |  	|jj
d¡ |  	|jjd¡ dS )zE
        Test that parsing is successful for a PROXY header.
        s(   PROXY TCP4 127.0.0.1 127.0.0.1 8080 8888ú	127.0.0.1é�  é¸"  N)r   r   r   ÚassertIsInstanceÚsourcer   ÚIPv4AddressÚ
isinstanceÚdestinationÚassertEqualÚhostÚport©r   Úinfor   r   r   Útest_fullParsingSuccess;   ó   ÿz%V1ParserTests.test_fullParsingSuccessc                 C   r   )zK
        Test that parsing is successful for an IPv6 PROXY header.
        s   PROXY TCP6 ::1 ::1 8080 8888z::1r   r   N)r   r   r   r   r   r   ÚIPv6Addressr    r!   r"   r#   r$   r%   r   r   r   Útest_fullParsingSuccess_IPv6J   r(   z*V1ParserTests.test_fullParsingSuccess_IPv6c                 C   s(   t j d¡}|  |j¡ |  |j¡ dS )zM
        Test that parsing is successful for a UNKNOWN PROXY header.
        s$   PROXY UNKNOWN anything could go hereN)r   r   r   ÚassertIsNoner   r!   r%   r   r   r   Útest_fullParsingSuccess_UNKNOWNY   s
   ÿz-V1ParserTests.test_fullParsingSuccess_UNKNOWNc                 C   sô   t  ¡ }| d¡\}}|  |¡ |  |¡ | d¡\}}|  |¡ |  |¡ | d¡\}}|  |¡ |dus8J ‚|dus>J ‚|  |jtj¡ t|jtjƒsOJ ‚t|j	tjƒsXJ ‚|  
|jjd¡ |  
|jjd¡ |  
|j	jd¡ |  
|j	jd¡ dS )zE
        Test that parsing happens when fed a complete line.
        ó   PROXY TCP4 127.0.0.1 127.0.0.1 ó	   8080 8888s   
Nr   r   r   )r   r   ÚfeedÚassertFalser   r   r   r   r    r!   r"   r#   r$   ©r   Úparserr&   Ú	remainingr   r   r   Útest_feedParsingc   s$   




zV1ParserTests.test_feedParsingc                 C   s`   t  ¡ }| d¡\}}|  |¡ |  |¡ | d¡\}}|  |¡ |  |¡ |  t|jd¡ dS )zN
        Test that parsing fails if no newline is found in 108 bytes.
        r-   r.   sd                                                                                                       N)r   r   r/   r0   r   r   r1   r   r   r   Útest_feedParsingTooLongz   s   



ýz%V1ParserTests.test_feedParsingTooLongc                 C   s<   t  ¡ }| d¡\}}|  |¡ |  |d¡ |  |j¡ dS )zH
        Test that parsing leaves overflow bytes in the buffer.
        s:   PROXY TCP4 127.0.0.1 127.0.0.1 8080 8888
HTTP/1.1 GET /
s   HTTP/1.1 GET /
N)r   r   r/   Ú
assertTruer"   r0   Úbufferr1   r   r   r   Útest_feedParsingOverflow‹   s   ÿ
z&V1ParserTests.test_feedParsingOverflow)r
   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r'   r*   r,   r4   r5   r8   r   r   r   r   r	      s    













r	   N)r<   Útwisted.internetr   Útwisted.trialr   Ú r   Ú_exceptionsr   r   r   ÚTestCaser	   r   r   r   r   Ú<module>   s   