ϪfXD dZddlZddlZddlmZmZddlmZddlm Z ddl m Z ddl m Z ddlmZGd d e j ZGd d ZGd de j&ZGddej*ZGddej*Zy)z_ Tests for L{twisted.protocol.socks}, an implementation of the SOCKSv4 and SOCKSv4a protocols. N)addressdefer)DNSLookupError)socks) iterbytes) proto_helpers)unittestc&eZdZdZdZdZdZdZy)StringTCPTransportFNc|jS)N)peerselfs 9/usr/lib/python3/dist-packages/twisted/test/test_socks.pygetPeerzStringTCPTransport.getPeers yyc0tjdddS)NTCPz2.3.4.5*)r IPv4Addressrs rgetHostzStringTCPTransport.getHosts""5)R88rcd|_y)NT)stringTCPTransport_closingrs rloseConnectionz!StringTCPTransport.loseConnections *.'r)__name__ __module__ __qualname__rr rrrrrr r s!& D9/rr ceZdZdZdZdZy)FakeResolverReactorzP Bare-bones reactor with deterministic behavior for the resolve method. c||_y)z @type names: L{dict} containing L{str} keys and L{str} values. @param names: A hostname to IP address mapping. The IP addresses are stringified dotted quads. N)names)rr"s r__init__zFakeResolverReactor.__init__'s  rc  tj|j|S#t$r3tjt d|j dzcYSwxYw)zQ Resolve a hostname by looking it up in the C{names} dictionary. z"FakeResolverReactor couldn't find zutf-8)rsucceedr"KeyErrorfailrdecode)rhostnames rresolvezFakeResolverReactor.resolve/sX ==H!56 6 ::88??7;SS  s!$9A A N)rrr__doc__r#r*rrrr r "s rr c eZdZdZdZdZdZy) SOCKSv4DriverNc||}t|_tjd|||j_|j ||_tj|S)Nr) r transportrrr connectionMadedriver_outgoingrr%)rhostportklassargsprotos r connectClasszSOCKSv4Driver.connectClassDsSt ,.&225$E $}}U##rcV||}||_|dk(rd}tjd|fS)Nr6.7.8.9) driver_listenrr%)rr3r4r5factorys r listenClasszSOCKSv4Driver.listenClassMs3,$ 19D}}i.//r)rrrr1r;r7r=rrrr-r-=sOM$0rr-c@eZdZdZdZdZdZdZdZdZ dZ d Z y ) ConnectTestszV Tests for SOCKS and SOCKSv4a connect requests using the L{SOCKSv4} protocol. ct|_t|j_|jj t ddi|j_yNs localhost 127.0.0.1r-sockr r/r0r reactorrs rsetUpzConnectTests.setUp[@!O 02    "/{0KL rc|jj}|'|j|jjdyy)Nz-Outgoing SOCKS connections need to be closed.)rDr1 assertTruer/r)routgoings rtearDownzConnectTests.tearDownas:99,,   OO""==?  rc |jjtjddddt j dzdzdz|jj j}|jj j|j|tjddd dt j dz|j|jj j|j|jj|jjd |j|jjj jd |jjjd |j|jj jd |jjd y) N!BBH"1.2.3.4fooBARrZ hello, worldhi there fake reason)rD dataReceivedstructpacksocket inet_atonr/valueclear assertEqual assertFalserassertIsNotNoner1connectionLost)rsents r test_simplezConnectTests.test_simpleisq  KK1b )y) *   yy""((* !!#  &++faR063C3CI3NN  ,,GGH TYY667 / 22<<BBDoV !!..{; ,,224kB   /rc tjddddtjdzdzdz}t |D]}|j j ||j jj}|j jj|j|tjddd dtjd z|j|j jj|j|j j|j j d |j|j jjjd |j jj d |j|j jjd |j jd y)u If the destination IP address has zeros for the first three octets and non-zero for the fourth octet, the client is attempting a v4a connection. A hostname is specified after the user ID string and the server connects to the address that hostname resolves to. @see: U{http://en.wikipedia.org/wiki/SOCKS#SOCKS_4a_protocol} rMrNrOrP0.0.0.1fooBAZ localhostrrTrBrUrVrWN)rYrZr[r\rrDrXr/r]r^r_r`rrar1rbr clientRequestbytercs r test_socks4aSuccessfulResolutionz-ConnectTests.test_socks4aSuccessfulResolutions KK1b )y) *   m, )D II " "4 ( )yy""((* !!#  &++faR063C3CK3PP  ,,GGH TYY667 / 22<<BBDoV !!..{; ,,224kB   /rc .tjddddtjdzdzdz}t |D]}|j j ||j jj}|j|tjddd dtjd z|j|j jj|j|j jy ) Failed hostname resolution on a SOCKSv4a packet results in a 91 error response and the connection getting closed. rMrNrOrPrgrh failinghostr[0.0.0.0NrYrZr[r\rrDrXr/r]r_rIr assertIsNoner1rjs rtest_socks4aFailedResolutionz)ConnectTests.test_socks4aFailedResolution KK1b )y) *   m, )D II " "4 ( )yy""((*  &++faQ/&2B2B92MM  ++FFG $))334rc *d|j_|jjtjddddt j dzdzdz|j|jjjtjdd d d t j d z|j|jjj|j|jjy) NcyNrrcodeserverr3users rz0ConnectTests.test_accessDenied..rrMrNrO10.2.3.4rRrSrrqrr)rD authorizerXrYrZr[r\r_r/r]rIrrtr1rs rtest_accessDeniedzConnectTests.test_accessDenieds@   KK1d +z* +    II   % % ' KK2q )F,<,|jjtjddddt j dzdzdz|jj j|jjd|j|jjj jd|jjj j|jjjd y NrMrNrOrPrQrRrSrUrW) rDrXrYrZr[r\r/r^r_r1r]rrbrs rtest_eofRemotezConnectTests.test_eofRemotes  KK1b )y) *   !!# / 22<<BBDoV !!++::< !!00?rc|jjtjddddt j dzdzdz|jj j|jjd|j|jjj jd|jjd yr) rDrXrYrZr[r\r/r^r_r1r]rbrs r test_eofLocalzConnectTests.test_eofLocals  KK1b )y) *   !!# / 22<<BBDoV   /rN) rrrr+rFrKrdrmrurrrrrrr?r?Vs2M 02,0\5: 5@"0rr?c@eZdZdZdZdZdZdZdZdZ dZ d Z y ) BindTestszS Tests for SOCKS and SOCKSv4a bind requests using the L{SOCKSv4} protocol. ct|_t|j_|jj t ddi|j_yrArCrs rrFzBindTests.setUprGrc |jjtjddddt j dzdzdz|jj j}|jj j|j|tjddd d t j d z|j|jj j|j|jj|jjjd }|j|t|_|j!|jj j}|jj j|j|tjddd dt j d z|j|jj j|jjd|j|j jd|jd|j|jj jd|jj#dy)NrMrNrPrQrRrSrrTr9r:rQrrrUrVrW)rDrXrYrZr[r\r/r]r^r_r`rrar; buildProtocolr r0rbrrcincomings rrdzBindTests.test_simples!  KK1b )y) *   yy""((* !!#  &++faT2V5E5Ei5PP  ,,GGH TYY44599**889JK X&/1!yy""((* !!#  &++faQ/&2B2B92MM  ,,GGH / ++113_E k* ,,224kB   /rc tjddddtjdzdzdz}t |D]}|j j ||j jj}|j jj|j|tjddd d tjd z|j|j jj|j|j j|j jjd }|j|t!|_|j#|j jj}|j jj|j|tjddd dtjd z|j%|j jjd|j j d|j|jjd|j d|j|j jjd|j j'dy)rfrMrNrrPrgrhrirrTr9r:)rBrrrNrVrW)rYrZr[r\rrDrXr/r]r^r_r`rrar;rr r0 assertIsNotrb)rrkrlrcrs r test_socks4azBindTests.test_socks4a7sB KK1b )y) *   m, )D II " "4 ( )yy""((* !!#  &++faT2V5E5Ei5PP  ,,GGH TYY44599**889LM X&/1!yy""((* !!#  &++faQ/&2B2B92MM  ,,GGN {+ ++113[A k* ,,224kB   /rc .tjddddtjdzdzdz}t |D]}|j j ||j jj}|j|tjddd dtjd z|j|j jj|j|j jy ) rorMrNrrPrgrhrprrqrrNrsrjs rruz&BindTests.test_socks4aFailedResolutionrrvrc *d|j_|jjtjddddt j dzdzdz|j|jjjtjdd d d t j d z|j|jjj|j|jjy) Ncyryrrzs rr~z-BindTests.test_accessDenied..rrrMrNrrrrRrSrrqrr)rDrrXrYrZr[r\r_r/r]rIrrtr;rs rrzBindTests.test_accessDenieds@   KK1d +z* +    II   % % ' KK2q )F,<,Hrr)r+r[rYtwisted.internetrrtwisted.internet.errorrtwisted.protocolsrtwisted.python.compatr twisted.testr twisted.trialr StringTransportr r SOCKSv4r-TestCaser?rrrrrs{  +1#+&" /66 /60EMM02e08$$e0PtH!!tHr