
    Vh.                         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Ze	dddddfde
de
de
de
de
de
de
fdZe	ddddfde
de
de
de
de
de
fdZ G d dej                        Zy)z7
Test cases for L{twisted.protocols.haproxy.V2Parser}.
    )address)unittest   )	_v2parser)InvalidProxyHeaders   

 
QUIT
   !s    $s                                  s   "sigverComfamProto
addrLengthaddrsportsreturnc                 $    | |z   |z   |z   |z   |z   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   s         \/home/dcms/DCMS/lib/python3.12/site-packages/twisted/protocols/haproxy/test/test_v2parser.py_makeHeaderIPv6r      #    F <("Z/%7%??       s    s       c                 $    | |z   |z   |z   |z   |z   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   s         r   _makeHeaderIPv4r   6   r   r      1s    s   /home/tests/mysockets/sock                                                                                  /home/tests/mysockets/sock                                                                                  c                     | |z   |z   |z   |z   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   s        r   _makeHeaderUnixr   \   s    F <("Z/%77r   c                       e Zd 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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dZddZddZddZddZy)V2ParserTestsz?
    Test L{twisted.protocols.haproxy.V2Parser} behaviour.
    Nc                 t    t               }| j                  t        j                  j	                  |             y)zL
        Test if a well formed IPv4 header is parsed without error.
        N)r   
assertTruer   V2Parserparseselfheaders     r   test_happyPathIPv4z V2ParserTests.test_happyPathIPv4   )     !"	**0089r   c                 t    t               }| j                  t        j                  j	                  |             y)zL
        Test if a well formed IPv6 header is parsed without error.
        N)r   r    r   r!   r"   r#   s     r   test_happyPathIPv6z V2ParserTests.test_happyPathIPv6   r'   r   c                 t    t               }| j                  t        j                  j	                  |             y)zL
        Test if a well formed UNIX header is parsed without error.
        N)r   r    r   r!   r"   r#   s     r   test_happyPathUnixz V2ParserTests.test_happyPathUnix   r'   r   c                 z    t        d      }| j                  t        t        j                  j
                  |       y)zN
        Test if an invalid signature block raises InvalidProxyError.
        s               )r	   Nr   assertRaisesr   r   r!   r"   r#   s     r   test_invalidSignaturez#V2ParserTests.test_invalidSignature   s1     !\2$$	
r   c                 z    t        d      }| j                  t        t        j                  j
                  |       y)zF
        Test if an invalid version raises InvalidProxyError.
        r   r
   Nr-   r#   s     r   test_invalidVersionz!V2ParserTests.test_invalidVersion   1     !0$$	
r   c                 z    t        d      }| j                  t        t        j                  j
                  |       y)zF
        Test if an invalid command raises InvalidProxyError.
           #r1   Nr-   r#   s     r   test_invalidCommandz!V2ParserTests.test_invalidCommand   r3   r   c                 z    t        d      }| j                  t        t        j                  j
                  |       y)zE
        Test if an invalid family raises InvalidProxyError.
           @r   Nr-   r#   s     r   test_invalidFamilyz V2ParserTests.test_invalidFamily   1     !'2$$	
r   c                 z    t        d      }| j                  t        t        j                  j
                  |       y)zG
        Test if an invalid protocol raises InvalidProxyError.
           $r9   Nr-   r#   s     r   test_invalidProtozV2ParserTests.test_invalidProto   r;   r   c                     t        d      }t        j                  j                  |      }| j	                  |j
                         | j	                  |j                         y)zU
        Test that local does not return endpoint data for IPv4 connections.
            r1   Nr   r   r!   r"   assertFalsesourcedestinationr$   r%   infos      r   test_localCommandIpv4z#V2ParserTests.test_localCommandIpv4   J     !0!!''/%))*r   c                     t        d      }t        j                  j                  |      }| j	                  |j
                         | j	                  |j                         y)zU
        Test that local does not return endpoint data for IPv6 connections.
        r@   r1   Nr   r   r!   r"   rB   rC   rD   rE   s      r   test_localCommandIpv6z#V2ParserTests.test_localCommandIpv6   rH   r   c                     t        d      }t        j                  j                  |      }| j	                  |j
                         | j	                  |j                         y)zU
        Test that local does not return endpoint data for UNIX connections.
        r@   r1   Nr   r   r!   r"   rB   rC   rD   rE   s      r   test_localCommandUnixz#V2ParserTests.test_localCommandUnix   rH   r   c                 n   t        d      }t        j                  j                  |      }| j	                  |j
                         | j                  |j
                  t        j                         | j	                  |j                         | j                  |j                  t        j                         y)zM
        Test that proxy returns endpoint data for IPv4 connections.
        r   r1   N)
r   r   r!   r"   r    rC   assertIsInstancer   IPv4AddressrD   rE   s      r   test_proxyCommandIpv4z#V2ParserTests.test_proxyCommandIpv4   |     !0!!''/$dkk7+>+>?(()d..0C0CDr   c                 n   t        d      }t        j                  j                  |      }| j	                  |j
                         | j                  |j
                  t        j                         | j	                  |j                         | j                  |j                  t        j                         y)zM
        Test that proxy returns endpoint data for IPv6 connections.
        r   r1   N)
r   r   r!   r"   r    rC   rP   r   IPv6AddressrD   rE   s      r   test_proxyCommandIpv6z#V2ParserTests.test_proxyCommandIpv6   rS   r   c                 n   t        d      }t        j                  j                  |      }| j	                  |j
                         | j                  |j
                  t        j                         | j	                  |j                         | j                  |j                  t        j                         y)zM
        Test that proxy returns endpoint data for UNIX connections.
        r   r1   N)
r   r   r!   r"   r    rC   rP   r   UNIXAddressrD   rE   s      r   test_proxyCommandUnixz#V2ParserTests.test_proxyCommandUnix  rS   r   c                     t        d      }t        j                  j                  |      }| j	                  |j
                         | j	                  |j                         y)V
        Test that UNSPEC does not return endpoint data for IPv4 connections.
           r9   NrA   rE   s      r   test_unspecFamilyIpv4z#V2ParserTests.test_unspecFamilyIpv4  J     !'2!!''/%))*r   c                     t        d      }t        j                  j                  |      }| j	                  |j
                         | j	                  |j                         y)V
        Test that UNSPEC does not return endpoint data for IPv6 connections.
        r\   r9   NrJ   rE   s      r   test_unspecFamilyIpv6z#V2ParserTests.test_unspecFamilyIpv6  r^   r   c                     t        d      }t        j                  j                  |      }| j	                  |j
                         | j	                  |j                         y)V
        Test that UNSPEC does not return endpoint data for UNIX connections.
        r\   r9   NrM   rE   s      r   test_unspecFamilyUnixz#V2ParserTests.test_unspecFamilyUnix!  r^   r   c                     t        d      }t        j                  j                  |      }| j	                  |j
                         | j	                  |j                         y)r[      r9   NrA   rE   s      r   test_unspecProtoIpv4z"V2ParserTests.test_unspecProtoIpv4*  r^   r   c                     t        d      }t        j                  j                  |      }| j	                  |j
                         | j	                  |j                         y)r`   r@   r9   NrJ   rE   s      r   test_unspecProtoIpv6z"V2ParserTests.test_unspecProtoIpv63  r^   r   c                     t        d      }t        j                  j                  |      }| j	                  |j
                         | j	                  |j                         y)rc      0r9   NrM   rE   s      r   test_unspecProtoUnixz"V2ParserTests.test_unspecProtoUnix<  r^   r   c                     d}t               |z   }t        j                         }|j                  |      \  }}| j	                  |       | j                  ||       y)zU
        Test that overflow bits are preserved during feed parsing for IPv4.
           TEST DATA

TEST DATAN)r   r   r!   feedr    assertEqualr$   	testValuer%   parserrF   overflows         r   test_overflowIpv4zV2ParserTests.test_overflowIpv4E  R     2	 "Y.##%V,h9-r   c                     d}t               |z   }t        j                         }|j                  |      \  }}| j	                  |       | j                  ||       y)zU
        Test that overflow bits are preserved during feed parsing for IPv6.
        rn   N)r   r   r!   ro   r    rp   rq   s         r   test_overflowIpv6zV2ParserTests.test_overflowIpv6P  rv   r   c                     d}t               |z   }t        j                         }|j                  |      \  }}| j	                  |       | j                  ||       y)zU
        Test that overflow bits are preserved during feed parsing for Unix.
        rn   N)r   r   r!   ro   r    rp   rq   s         r   test_overflowUnixzV2ParserTests.test_overflowUnix[  rv   r   c                 r    d}t        j                         }| j                  t        |j                  |       y)zK
        Test that an initial payload of less than 16 bytes fails.
        s   NEEDMOREDATAN)r   r!   r.   r   ro   )r$   rr   rs   s      r   test_segmentTooSmallz"V2ParserTests.test_segmentTooSmallf  s2     $	##%KK	
r   )r   N)__name__
__module____qualname____doc__r&   r)   r+   r/   r2   r6   r:   r>   rG   rK   rN   rR   rV   rY   r]   ra   rd   rg   ri   rl   ru   rx   rz   r|   r   r   r   r   r      s    :::	
	
	
	
	
+++	E	E	E++++++	.	.	.

r   r   N)r   twisted.internetr   twisted.trialr    r   _exceptionsr   V2_SIGNATUREbytesr   r   r   TestCaser   r   r   r   <module>r      sJ   % "  ,B #1&#@	#@#@ #@ 	#@
 #@ #@ #@N #6&#@	#@#@ #@ 	#@
 #@ #@ #@N ##8	#8#8 #8 	#8
 #8 #8Ln
H%% n
r   