Ϫf6dZddlmZddlmZddlmZddlmZm Z ddl m Z ddl mZdd l mZGd d e ZGd d ej ZGddej Zy)z> Test cases for L{twisted.protocols.haproxy.HAProxyProtocol}. )Optional)mock)address)FactoryProtocol) StringTransportWithDisconnection)unittest)HAProxyWrappingFactoryc(eZdZdZddZdeddfdZy)StaticProtocolz6 Protocol stand-in that maintains test state. returnNc<d|_d|_d|_d|_y)NF)source destinationdata disconnected)selfs M/usr/lib/python3/dist-packages/twisted/protocols/haproxy/test/test_wrapper.py__init__zStaticProtocol.__init__s 26 7; !rrc|jsJ|jj|_|jj|_|xj |z c_y)N) transportgetPeerrgetHostrr)rrs r dataReceivedzStaticProtocol.dataReceivedsE~~~nn,,. >>113 T rrN)__name__ __module__ __qualname____doc__rbytesrrrr r s" 4rr cPeZdZdZd dZd dZd dZd dZd dZd dZ d d Z d d Z y) HAProxyWrappingFactoryV1Testsz] Test L{twisted.protocols.haproxy.HAProxyWrappingFactory} with v1 PROXY headers. Nc0ttjt}|j t j ddd}t}||_|j||jd|j|jy)J Test if invalid headers result in connectionLost events. TCP 127.1.1.1sNOTPROXY anything can go here N r r forProtocolr buildProtocolr IPv4AddressrprotocolmakeConnectionr assertFalse connectedrfactoryprotors rtest_invalidHeaderDisconnectsz;HAProxyWrappingFactoryV1Tests.test_invalidHeaderDisconnects)s~))<)<^)LM%%   {D 9 56 "  Y' ?@ ,,-rcRttjt}|j t j ddd}t}||_|j||jd|jd|j|jy)r'r(r)r*sPROXY TCP4 1.1.1.1 s2.2.2.2 8080 Nr+r3s r$test_invalidPartialHeaderDisconnectszBHAProxyWrappingFactoryV1Tests.test_invalidPartialHeaderDisconnects7s))<)<^)LM%%   {D 9 56 "  Y' 45 ./ ,,-rcttjt}|j t j ddd}ttjjtjj}|j||j|jtjj|j|jtjjy)z Before any data is received the HAProxy protocol will return the same peer and host as the IP connection. r( 127.0.0.1r*) hostAddress peerAddressN)r rr,r r-rr.rrsentinel host_address peer_addressr0 assertEqualrrr3s r test_preDataReceived_getPeerHostz>HAProxyWrappingFactoryV1Tests.test_preDataReceived_getPeerHostFs ))<)<^)LM%%   {D 9 5 22 22  Y' $--*D*DE $--*D*DErc,ttjt}|j t j ddd}t}|j||jd|j|jjd|j|jjd|j|jjjjd|j|jjjjd|j|j!jd|j|j!jd|j|jjj!jd|j|jjj!jdy) P Test if IPv4 headers result in the correct host and peer data. r(r:r*s&PROXY TCP4 1.1.1.1 2.2.2.2 8080 8888 z1.1.1.1z2.2.2.2"N)r rr,r r-rr.rr0rr@rhostportwrappedProtocolrrr3s r(test_validIPv4HeaderResolves_getPeerHostzFHAProxyWrappingFactoryV1Tests.test_validIPv4HeaderResolves_getPeerHostWs))<)<^)LM%%   {D 9 56  Y' FG --y9 --t4   ! ! + + 3 3 5 : :     ! ! + + 3 3 5 : :   --y9 --t4   ! ! + + 3 3 5 : :     ! ! + + 3 3 5 : :  rc,ttjt}|j t j ddd}t}|j||jd|j|jjd|j|jjd|j|jjjjd|j|jjjjd|j|j!jd|j|j!jd|j|jjj!jd|j|jjj!jdy)P Test if IPv6 headers result in the correct host and peer data. r(::1r*sPROXY TCP6 ::1 ::2 8080 8888 z::2rDN)r rr,r r-r IPv6Addressrr0rr@rrErFrGrrr3s r(test_validIPv6HeaderResolves_getPeerHostzFHAProxyWrappingFactoryV1Tests.test_validIPv6HeaderResolves_getPeerHostws))<)<^)LM%%   ud 3 56  Y' >? --u5 --t4   ! ! + + 3 3 5 : :     ! ! + + 3 3 5 : :   --u5 --t4   ! ! + + 3 3 5 : :     ! ! + + 3 3 5 : :  rc8ttjt}|j t j ddd}t}|j||jd|j|jjdy)N Test if non-header bytes are passed to the wrapped protocol. r(rKr*s,PROXY TCP6 ::1 ::2 8080 8888 HTTP/1.1 / GETHTTP/1.1 / GETN r rr,r r-rrLrr0rr@rGrr3s r'test_overflowBytesSentToWrappedProtocolzEHAProxyWrappingFactoryV1Tests.test_overflowBytesSentToWrappedProtocols))<)<^)LM%%   ud 3 56  Y' LM ..335FGrcZttjt}|j t j ddd}t}|j||jd|jd|j|jjdy)K Test if header streaming passes extra data appropriately. r(rKr*PROXY TCP6 ::1 ::2 8080 8888 HTTP/1.1 / GETrPNrQr3s r-test_overflowBytesSentToWrappedProtocolChunkszKHAProxyWrappingFactoryV1Tests.test_overflowBytesSentToWrappedProtocolChunkss))<)<^)LM%%   ud 3 56  Y' 12 9: ..335FGrc|ttjt}|j t j ddd}t}|j||jd|jd|jd|j|jjdy) zT Test if wrapper writes all data to wrapped protocol after parsing. r(rKr*rUrVs sHTTP/1.1 / GET NrQr3s r,test_overflowBytesSentToWrappedProtocolAfterzJHAProxyWrappingFactoryV1Tests.test_overflowBytesSentToWrappedProtocolAfters))<)<^)LM%%   ud 3 56  Y' 12 9: ;' ..335NOrr) rrr r!r6r8rArHrMrRrWrYr#rrr%r%#s5 . .F" @ @ H H Prr%c\eZdZdZdZdZdZdezezZd dZd dZ d d Z d d Z d d Z d d Z y)HAProxyWrappingFactoryV2Testsz] Test L{twisted.protocols.haproxy.HAProxyWrappingFactory} with v2 PROXY headers. s QUIT ! "s4 QUIT !!$"sl/home/tests/mysockets/socks QUIT !1NcPttjt}|j t j ddd}t}||_|j||jd|jddz|j|jy)r'r(rKr*N)r rr,r r-rrLrr/r0r IPV4HEADERr1r2r3s rr6z;HAProxyWrappingFactoryV2Tests.test_invalidHeaderDisconnectss))<)<^)LM%%   ud 3 56 "  Y' 7T__QR%889 ,,-rc@ttjt}|j t j ddd}t}|j||j|j|j|jjd|j|jjd|j|jj jjd|j|jj jjd|j|j#jd|j|j#jd|j|jj j#jd|j|jj j#jdy)rCr(r:r*rDN)r rr,r r-rr.rr0rr_r@rrErFrGrrr3s rrHzFHAProxyWrappingFactoryV2Tests.test_validIPv4HeaderResolves_getPeerHosts))<)<^)LM%%   {D 9 56  Y' 4??+ --{; --t4   ! ! + + 3 3 5 : :     ! ! + + 3 3 5 : :   --{; --t4   ! ! + + 3 3 5 : :     ! ! + + 3 3 5 : :  rc@ttjt}|j t j ddd}t}|j||j|j|j|jjd|j|jjd|j|jj jjd|j|jj jjd|j|j#jd|j|j#jd|j|jj j#jd|j|jj j#jdy)rJr(rKr*z0:0:0:0:0:0:0:1rDN)r rr,r r-rr.rr0r IPV6HEADERr@rrErFrGrrr3s rrMzFHAProxyWrappingFactoryV2Tests.test_validIPv6HeaderResolves_getPeerHost(s))<)<^)LM%%   ud 3 56  Y' 4??+ --/@A --t4   ! ! + + 3 3 5 : :     ! ! + + 3 3 5 : :   --/@A --t4   ! ! + + 3 3 5 : :     ! ! + + 3 3 5 : :  rcttjt}|j t j d}t}|j||j|j|j|jjd|j|jjjjd|j|j!jd|j|jjj!jdy)zP Test if UNIX headers result in the correct host and peer data. s/home/test/sockets/server.socks/home/tests/mysockets/sockN)r rr,r r-r UNIXAddressrr0r UNIXHEADERr@rnamerGrrr3s r(test_validUNIXHeaderResolves_getPeerHostzFHAProxyWrappingFactoryV2Tests.test_validUNIXHeaderResolves_getPeerHostHs))<)<^)LM%%    A B 56  Y' 4??+ --/LM   ! ! + + 3 3 5 : : )  --/LM   ! ! + + 3 3 5 : : ) rcRttjt}|j t j ddd}t}|j||j|jdz|j|jjdy)rOr(rKr*rPNr rr,r r-rrLrr0rrbr@rGrr3s rrRzEHAProxyWrappingFactoryV2Tests.test_overflowBytesSentToWrappedProtocol^s))<)<^)LM%%   ud 3 56  Y' 4??->>? ..335FGrcttjt}|j t j ddd}t}|j||j|jdd|j|jdddz|j|jjdy)rTr(rKr*NrPrir3s rrWzKHAProxyWrappingFactoryV2Tests.test_overflowBytesSentToWrappedProtocolChunksks))<)<^)LM%%   ud 3 56  Y' 4??3B/0 4??23/2CCD ..335FGrr)rrr r!r_rb _SOCK_PATHrer6rHrMrgrRrWr#rrr[r[sf  " E      . @ @ , H Hrr[N)r!typingrr rtwisted.internetrtwisted.internet.protocolrrtwisted.internet.testingr twisted.trial_wrapperr r TestCaser%r[r#rrrtsZ$7E"-X$\PH$5$5\P~uHH$5$5uHr