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   
'
'
&