o
    ¯bõ.  ã                   @   sä   d Z ddlmZ ddlmZ ddlmZ ddlmZ dZ	e	ddd	d
dfde
de
de
de
de
de
de
fdd„Ze	dddddfde
de
de
de
de
de
de
fdd„Ze	ddddfde
de
de
de
de
de
fdd„ZG dd „ d ejƒZd!S )"z7
Test cases for L{twisted.protocols.haproxy.V2Parser}.
é    )Úaddress)Úunittesté   )Ú	_v2parser)ÚInvalidProxyHeaders   

 
QUIT
ó   !s    $s                                  s   �"¸ÚsigÚverComÚfamProtoÚ
addrLengthÚaddrsÚportsÚreturnc                 C   ó   | | | | | | S )aV  
    Construct a version 2 IPv6 header with custom bytes.

    @param sig: The protocol signature; defaults to valid L{V2_SIGNATURE}.
    @type sig: L{bytes}

    @param verCom: Protocol version and command.  Defaults to V2 PROXY.
    @type verCom: L{bytes}

    @param famProto: Address family and protocol.  Defaults to AF_INET6/STREAM.
    @type famProto: L{bytes}

    @param addrLength: Network-endian byte length of payload.  Defaults to
        description of default addrs/ports.
    @type addrLength: L{bytes}

    @param addrs: Address payload.  Defaults to C{::1} for source and
        destination.
    @type addrs: L{bytes}

    @param ports: Source and destination ports.  Defaults to 8080 for source
        8888 for destination.
    @type ports: L{bytes}

    @return: A packet with header, addresses, and ports.
    @rtype: L{bytes}
    © ©r   r	   r
   r   r   r   r   r   úN/usr/lib/python3/dist-packages/twisted/protocols/haproxy/test/test_v2parser.pyÚ_makeHeaderIPv6   ó   #r   ó   s    s       c                 C   r   )aX  
    Construct a version 2 IPv4 header with custom bytes.

    @param sig: The protocol signature; defaults to valid L{V2_SIGNATURE}.
    @type sig: L{bytes}

    @param verCom: Protocol version and command.  Defaults to V2 PROXY.
    @type verCom: L{bytes}

    @param famProto: Address family and protocol.  Defaults to AF_INET/STREAM.
    @type famProto: L{bytes}

    @param addrLength: Network-endian byte length of payload.  Defaults to
        description of default addrs/ports.
    @type addrLength: L{bytes}

    @param addrs: Address payload.  Defaults to 127.0.0.1 for source and
        destination.
    @type addrs: L{bytes}

    @param ports: Source and destination ports.  Defaults to 8080 for source
        8888 for destination.
    @type ports: L{bytes}

    @return: A packet with header, addresses, and ports.
    @rtype: L{bytes}
    r   r   r   r   r   Ú_makeHeaderIPv46   r   r   ó   1s    ØsØ   /home/tests/mysockets/sock                                                                                  /home/tests/mysockets/sock                                                                                  c                 C   s   | | | | | S )að  
    Construct a version 2 IPv4 header with custom bytes.

    @param sig: The protocol signature; defaults to valid L{V2_SIGNATURE}.
    @type sig: L{bytes}

    @param verCom: Protocol version and command.  Defaults to V2 PROXY.
    @type verCom: L{bytes}

    @param famProto: Address family and protocol.  Defaults to AF_UNIX/STREAM.
    @type famProto: L{bytes}

    @param addrLength: Network-endian byte length of payload.  Defaults to 108
        bytes for 2 null terminated paths.
    @type addrLength: L{bytes}

    @param addrs: Address payload.  Defaults to C{/home/tests/mysockets/sock}
        for source and destination paths.
    @type addrs: L{bytes}

    @return: A packet with header, addresses, and8 ports.
    @rtype: L{bytes}
    r   )r   r	   r
   r   r   r   r   r   Ú_makeHeaderUnix\   s   #r   c                   @   s   e Zd ZdZd4dd„Zd4dd„Zd4dd	„Zd4d
d„Zd4dd„Zd4dd„Z	d4dd„Z
d4dd„Zd4dd„Zd4dd„Zd4dd„Zd4dd„Zd4dd„Zd4dd„Zd4d d!„Zd4d"d#„Zd4d$d%„Zd4d&d'„Zd4d(d)„Zd4d*d+„Zd4d,d-„Zd4d.d/„Zd4d0d1„Zd4d2d3„ZdS )5ÚV2ParserTestsz?
    Test L{twisted.protocols.haproxy.V2Parser} behaviour.
    r   Nc                 C   ó   t ƒ }|  tj |¡¡ dS )zL
        Test if a well formed IPv4 header is parsed without error.
        N)r   Ú
assertTruer   ÚV2ParserÚparse©ÚselfÚheaderr   r   r   Útest_happyPathIPv4‡   ó   z V2ParserTests.test_happyPathIPv4c                 C   r   )zL
        Test if a well formed IPv6 header is parsed without error.
        N)r   r   r   r   r   r   r   r   r   Útest_happyPathIPv6Ž   r"   z V2ParserTests.test_happyPathIPv6c                 C   r   )zL
        Test if a well formed UNIX header is parsed without error.
        N)r   r   r   r   r   r   r   r   r   Útest_happyPathUnix•   r"   z V2ParserTests.test_happyPathUnixc                 C   ó    t dd�}|  ttjj|¡ dS )zN
        Test if an invalid signature block raises InvalidProxyError.
        s               )r   N©r   ÚassertRaisesr   r   r   r   r   r   r   r   Útest_invalidSignatureœ   ó   
ýz#V2ParserTests.test_invalidSignaturec                 C   r%   )zF
        Test if an invalid version raises InvalidProxyError.
        r   ©r	   Nr&   r   r   r   r   Útest_invalidVersion§   r)   z!V2ParserTests.test_invalidVersionc                 C   r%   )zF
        Test if an invalid command raises InvalidProxyError.
        ó   #r*   Nr&   r   r   r   r   Útest_invalidCommand²   r)   z!V2ParserTests.test_invalidCommandc                 C   r%   )zE
        Test if an invalid family raises InvalidProxyError.
        ó   @©r
   Nr&   r   r   r   r   Útest_invalidFamily½   r)   z V2ParserTests.test_invalidFamilyc                 C   r%   )zG
        Test if an invalid protocol raises InvalidProxyError.
        ó   $r/   Nr&   r   r   r   r   Útest_invalidProtoÈ   r)   zV2ParserTests.test_invalidProtoc                 C   ó2   t dd�}tj |¡}|  |j¡ |  |j¡ dS )zU
        Test that local does not return endpoint data for IPv4 connections.
        ó    r*   N©r   r   r   r   ÚassertFalseÚsourceÚdestination©r   r    Úinfor   r   r   Útest_localCommandIpv4Ó   ó   
z#V2ParserTests.test_localCommandIpv4c                 C   r3   )zU
        Test that local does not return endpoint data for IPv6 connections.
        r4   r*   N©r   r   r   r   r6   r7   r8   r9   r   r   r   Útest_localCommandIpv6Ü   r<   z#V2ParserTests.test_localCommandIpv6c                 C   r3   )zU
        Test that local does not return endpoint data for UNIX connections.
        r4   r*   N©r   r   r   r   r6   r7   r8   r9   r   r   r   Útest_localCommandUnixå   r<   z#V2ParserTests.test_localCommandUnixc                 C   óR   t dd�}tj |¡}|  |j¡ |  |jtj¡ |  |j	¡ |  |j	tj¡ dS )zM
        Test that proxy returns endpoint data for IPv4 connections.
        r   r*   N)
r   r   r   r   r   r7   ÚassertIsInstancer   ÚIPv4Addressr8   r9   r   r   r   Útest_proxyCommandIpv4î   ó   
z#V2ParserTests.test_proxyCommandIpv4c                 C   rA   )zM
        Test that proxy returns endpoint data for IPv6 connections.
        r   r*   N)
r   r   r   r   r   r7   rB   r   ÚIPv6Addressr8   r9   r   r   r   Útest_proxyCommandIpv6ù   rE   z#V2ParserTests.test_proxyCommandIpv6c                 C   rA   )zM
        Test that proxy returns endpoint data for UNIX connections.
        r   r*   N)
r   r   r   r   r   r7   rB   r   ÚUNIXAddressr8   r9   r   r   r   Útest_proxyCommandUnix  rE   z#V2ParserTests.test_proxyCommandUnixc                 C   r3   )úV
        Test that UNSPEC does not return endpoint data for IPv4 connections.
        ó   r/   Nr5   r9   r   r   r   Útest_unspecFamilyIpv4  r<   z#V2ParserTests.test_unspecFamilyIpv4c                 C   r3   )úV
        Test that UNSPEC does not return endpoint data for IPv6 connections.
        rK   r/   Nr=   r9   r   r   r   Útest_unspecFamilyIpv6  r<   z#V2ParserTests.test_unspecFamilyIpv6c                 C   r3   )úV
        Test that UNSPEC does not return endpoint data for UNIX connections.
        rK   r/   Nr?   r9   r   r   r   Útest_unspecFamilyUnix!  r<   z#V2ParserTests.test_unspecFamilyUnixc                 C   r3   )rJ   ó   r/   Nr5   r9   r   r   r   Útest_unspecProtoIpv4*  r<   z"V2ParserTests.test_unspecProtoIpv4c                 C   r3   )rM   r4   r/   Nr=   r9   r   r   r   Útest_unspecProtoIpv63  r<   z"V2ParserTests.test_unspecProtoIpv6c                 C   r3   )rO   ó   0r/   Nr?   r9   r   r   r   Útest_unspecProtoUnix<  r<   z"V2ParserTests.test_unspecProtoUnixc                 C   ó>   d}t ƒ | }t ¡ }| |¡\}}|  |¡ |  ||¡ dS )zU
        Test that overflow bits are preserved during feed parsing for IPv4.
        ó   TEST DATA

TEST DATAN)r   r   r   Úfeedr   ÚassertEqual©r   Ú	testValuer    Úparserr:   Úoverflowr   r   r   Útest_overflowIpv4E  ó   

zV2ParserTests.test_overflowIpv4c                 C   rV   )zU
        Test that overflow bits are preserved during feed parsing for IPv6.
        rW   N)r   r   r   rX   r   rY   rZ   r   r   r   Útest_overflowIpv6P  r_   zV2ParserTests.test_overflowIpv6c                 C   rV   )zU
        Test that overflow bits are preserved during feed parsing for Unix.
        rW   N)r   r   r   rX   r   rY   rZ   r   r   r   Útest_overflowUnix[  r_   zV2ParserTests.test_overflowUnixc                 C   s    d}t  ¡ }|  t|j|¡ dS )zK
        Test that an initial payload of less than 16 bytes fails.
        s   NEEDMOREDATAN)r   r   r'   r   rX   )r   r[   r\   r   r   r   Útest_segmentTooSmallf  s   ýz"V2ParserTests.test_segmentTooSmall)r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r!   r#   r$   r(   r+   r-   r0   r2   r;   r>   r@   rD   rG   rI   rL   rN   rP   rR   rS   rU   r^   r`   ra   rb   r   r   r   r   r   ‚   s4    









	
	
	



	
	
	
	
	
	

r   N)rf   Útwisted.internetr   Útwisted.trialr   Ú r   Ú_exceptionsr   ÚV2_SIGNATUREÚbytesr   r   r   ÚTestCaser   r   r   r   r   Ú<module>   s†   úÿþýüûú
ù'úÿþýüûú
ù'ûÿþýüû
õ&