U
    n5{iõ.  ã                	   @   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e
e
e
e
e
e
e
dœdd„Ze	dddddfe
e
e
e
e
e
e
dœdd„Ze	ddddfe
e
e
e
e
e
dœ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   | | | | | | 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   úP/tmp/pip-unpacked-wheel-mnhg7r0q/twisted/protocols/haproxy/test/test_v2parser.pyÚ_makeHeaderIPv6   s    #r   ó   s    s       c                 C   s   | | | | | | S )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   s    #r   ó   1s    ØsØ   /home/tests/mysockets/sock                                                                                  /home/tests/mysockets/sock                                                                                  )r   r	   r
   r   r   r   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ddœdd„Zddœdd„Zddœdd	„Zddœd
d„Zddœdd„Zddœdd„Z	ddœdd„Z
ddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœd d!„Zddœd"d#„Zddœd$d%„Zddœd&d'„Zddœd(d)„Zddœd*d+„Zddœd,d-„Zddœd.d/„Zddœd0d1„Zddœd2d3„ZdS )4ÚV2ParserTestsz?
    Test L{twisted.protocols.haproxy.V2Parser} behaviour.
    N)r   c                 C   s   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‡   s    z V2ParserTests.test_happyPathIPv4c                 C   s   t ƒ }|  tj |¡¡ dS )zL
        Test if a well formed IPv6 header is parsed without error.
        N)r   r   r   r   r   r   r   r   r   Útest_happyPathIPv6Ž   s    z V2ParserTests.test_happyPathIPv6c                 C   s   t ƒ }|  tj |¡¡ dS )zL
        Test if a well formed UNIX header is parsed without error.
        N)r   r   r   r   r   r   r   r   r   Útest_happyPathUnix•   s    z V2ParserTests.test_happyPathUnixc                 C   s    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œ   s    
ýz#V2ParserTests.test_invalidSignaturec                 C   s    t dd�}|  ttjj|¡ dS )zF
        Test if an invalid version raises InvalidProxyError.
        r   ©r	   Nr!   r   r   r   r   Útest_invalidVersion§   s    
ýz!V2ParserTests.test_invalidVersionc                 C   s    t dd�}|  ttjj|¡ dS )zF
        Test if an invalid command raises InvalidProxyError.
        ó   #r$   Nr!   r   r   r   r   Útest_invalidCommand²   s    
ýz!V2ParserTests.test_invalidCommandc                 C   s    t dd�}|  ttjj|¡ dS )zE
        Test if an invalid family raises InvalidProxyError.
        ó   @©r
   Nr!   r   r   r   r   Útest_invalidFamily½   s    
ýz V2ParserTests.test_invalidFamilyc                 C   s    t dd�}|  ttjj|¡ dS )zG
        Test if an invalid protocol raises InvalidProxyError.
        ó   $r)   Nr!   r   r   r   r   Útest_invalidProtoÈ   s    
ýzV2ParserTests.test_invalidProtoc                 C   s2   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Ó   s    
z#V2ParserTests.test_localCommandIpv4c                 C   s2   t dd�}tj |¡}|  |j¡ |  |j¡ dS )zU
        Test that local does not return endpoint data for IPv6 connections.
        r-   r$   N©r   r   r   r   r/   r0   r1   r2   r   r   r   Útest_localCommandIpv6Ü   s    
z#V2ParserTests.test_localCommandIpv6c                 C   s2   t dd�}tj |¡}|  |j¡ |  |j¡ dS )zU
        Test that local does not return endpoint data for UNIX connections.
        r-   r$   N©r   r   r   r   r/   r0   r1   r2   r   r   r   Útest_localCommandUnixå   s    
z#V2ParserTests.test_localCommandUnixc                 C   sR   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   r0   ÚassertIsInstancer   ÚIPv4Addressr1   r2   r   r   r   Útest_proxyCommandIpv4î   s    
z#V2ParserTests.test_proxyCommandIpv4c                 C   sR   t dd�}tj |¡}|  |j¡ |  |jtj¡ |  |j	¡ |  |j	tj¡ dS )zM
        Test that proxy returns endpoint data for IPv6 connections.
        r   r$   N)
r   r   r   r   r   r0   r9   r   ÚIPv6Addressr1   r2   r   r   r   Útest_proxyCommandIpv6ù   s    
z#V2ParserTests.test_proxyCommandIpv6c                 C   sR   t dd�}tj |¡}|  |j¡ |  |jtj¡ |  |j	¡ |  |j	tj¡ dS )zM
        Test that proxy returns endpoint data for UNIX connections.
        r   r$   N)
r   r   r   r   r   r0   r9   r   ZUNIXAddressr1   r2   r   r   r   Útest_proxyCommandUnix  s    
z#V2ParserTests.test_proxyCommandUnixc                 C   s2   t dd�}tj |¡}|  |j¡ |  |j¡ dS )úV
        Test that UNSPEC does not return endpoint data for IPv4 connections.
        ó   r)   Nr.   r2   r   r   r   Útest_unspecFamilyIpv4  s    
z#V2ParserTests.test_unspecFamilyIpv4c                 C   s2   t dd�}tj |¡}|  |j¡ |  |j¡ dS )úV
        Test that UNSPEC does not return endpoint data for IPv6 connections.
        r@   r)   Nr5   r2   r   r   r   Útest_unspecFamilyIpv6  s    
z#V2ParserTests.test_unspecFamilyIpv6c                 C   s2   t dd�}tj |¡}|  |j¡ |  |j¡ dS )úV
        Test that UNSPEC does not return endpoint data for UNIX connections.
        r@   r)   Nr7   r2   r   r   r   Útest_unspecFamilyUnix!  s    
z#V2ParserTests.test_unspecFamilyUnixc                 C   s2   t dd�}tj |¡}|  |j¡ |  |j¡ dS )r?   ó   r)   Nr.   r2   r   r   r   Útest_unspecProtoIpv4*  s    
z"V2ParserTests.test_unspecProtoIpv4c                 C   s2   t dd�}tj |¡}|  |j¡ |  |j¡ dS )rB   r-   r)   Nr5   r2   r   r   r   Útest_unspecProtoIpv63  s    
z"V2ParserTests.test_unspecProtoIpv6c                 C   s2   t dd�}tj |¡}|  |j¡ |  |j¡ dS )rD   ó   0r)   Nr7   r2   r   r   r   Útest_unspecProtoUnix<  s    
z"V2ParserTests.test_unspecProtoUnixc                 C   s>   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   Úparserr3   Zoverflowr   r   r   Útest_overflowIpv4E  s    

zV2ParserTests.test_overflowIpv4c                 C   s>   d}t ƒ | }t ¡ }| |¡\}}|  |¡ |  ||¡ dS )zU
        Test that overflow bits are preserved during feed parsing for IPv6.
        rK   N)r   r   r   rL   r   rM   rN   r   r   r   Útest_overflowIpv6P  s    

zV2ParserTests.test_overflowIpv6c                 C   s>   d}t ƒ | }t ¡ }| |¡\}}|  |¡ |  ||¡ dS )zU
        Test that overflow bits are preserved during feed parsing for Unix.
        rK   N)r   r   r   rL   r   rM   rN   r   r   r   Útest_overflowUnix[  s    

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   rL   )r   rO   rP   r   r   r   Útest_segmentTooSmallf  s    ýz"V2ParserTests.test_segmentTooSmall)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r    r#   r%   r'   r*   r,   r4   r6   r8   r;   r=   r>   rA   rC   rE   rG   rH   rJ   rQ   rR   rS   rT   r   r   r   r   r   ‚   s2   									r   N)rX   Ztwisted.internetr   Ztwisted.trialr   Ú r   Ú_exceptionsr   ZV2_SIGNATUREÚbytesr   r   r   ZTestCaser   r   r   r   r   Ú<module>   sb   úù'úù'ûõ&