
    VhXD                         d Z ddlZddlZddlmZmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZ  G d	 d
ej                         Z G d d      Z G d de	j&                        Z G d dej*                        Z G d dej*                        Zy)z_
Tests for L{twisted.protocol.socks}, an implementation of the SOCKSv4 and
SOCKSv4a protocols.
    N)addressdefer)DNSLookupError)socks)	iterbytes)proto_helpers)unittestc                   &    e Zd ZdZdZd Zd Zd Zy)StringTCPTransportFNc                     | j                   S )N)peerselfs    G/home/dcms/DCMS/lib/python3.12/site-packages/twisted/test/test_socks.pygetPeerzStringTCPTransport.getPeer   s    yy    c                 0    t        j                  ddd      S )NTCPz2.3.4.5*   )r   IPv4Addressr   s    r   getHostzStringTCPTransport.getHost   s    ""5)R88r   c                     d| _         y )NT)stringTCPTransport_closingr   s    r   loseConnectionz!StringTCPTransport.loseConnection   s
    *.'r   )__name__
__module____qualname__r   r   r   r   r    r   r   r   r      s    !&D9/r   r   c                       e Zd ZdZd Zd Zy)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)r   r"   s     r   __init__zFakeResolverReactor.__init__'   s     
r   c           	          	 t        j                  | j                  |         S # t        $ r3 t        j                  t        d|j                  d      z               cY S w xY w)zQ
        Resolve a hostname by looking it up in the C{names} dictionary.
        z"FakeResolverReactor couldn't find zutf-8)r   succeedr"   KeyErrorfailr   decode)r   hostnames     r   resolvezFakeResolverReactor.resolve/   sX    	==H!566 	::88??7;SS 	s   !$ 9A A N)r   r   r   __doc__r#   r*   r   r   r   r    r    "   s    r   r    c                        e Zd ZdZdZd Zd Zy)SOCKSv4DriverNc                      || }t               |_        t        j                  d||      |j                  _        |j                          || _        t        j                  |      S )Nr   )	r   	transportr   r   r   connectionMadedriver_outgoingr   r%   )r   hostportklassargsprotos         r   connectClasszSOCKSv4Driver.connectClassD   sS    t,.&225$E$}}U##r   c                 V     || }|| _         |dk(  rd}t        j                  d|f      S )Nr     6.7.8.9)driver_listenr   r%   )r   r3   r4   r5   factorys        r   listenClasszSOCKSv4Driver.listenClassM   s3    ,$19D}}i.//r   )r   r   r   r1   r;   r7   r=   r   r   r   r-   r-   =   s    O M$0r   r-   c                   @    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zy
)ConnectTestszV
    Tests for SOCKS and SOCKSv4a connect requests using the L{SOCKSv4} protocol.
    c                     t               | _        t               | j                  _        | j                  j	                          t        ddi      | j                  _        y Ns	   localhost	127.0.0.1r-   sockr   r/   r0   r    reactorr   s    r   setUpzConnectTests.setUp[   @    !O	02				  "/{0KL		r   c                     | j                   j                  }|'| j                  |j                  j                  d       y y )Nz-Outgoing SOCKS connections need to be closed.)rD   r1   
assertTruer/   r   )r   outgoings     r   tearDownzConnectTests.tearDowna   s:    99,,OO""==?  r   c           	         | j                   j                  t        j                  dddd      t	        j
                  d      z   dz   dz          | j                   j                  j                         }| j                   j                  j                          | j                  |t        j                  ddd	d      t	        j
                  d      z          | j                  | j                   j                  j                         | j                  | j                   j                         | j                   j                  d
       | j                  | j                   j                  j                  j                         d
       | j                   j                  j                  d       | j                  | j                   j                  j                         d       | j                   j                  d       y )N!BBH      "   1.2.3.4   fooBAR    r   Z      hello, world   hi therefake reason)rD   dataReceivedstructpacksocket	inet_atonr/   valueclearassertEqualassertFalser   assertIsNotNoner1   connectionLost)r   sents     r   test_simplezConnectTests.test_simplei   sq   		KK1b)y)* 	
 yy""((*		!!#&++faR063C3CI3NN	
 	,,GGHTYY667 			/22<<BBDoV 			!!..{;,,224kB		  /r   c           	         t        j                  dddd      t        j                  d      z   dz   dz   }t	        |      D ]  }| j
                  j                  |        | j
                  j                  j                         }| j
                  j                  j                          | j                  |t        j                  ddd	d      t        j                  d
      z          | j                  | j
                  j                  j                         | j                  | j
                  j                         | j
                  j                  d       | j                  | j
                  j                  j                  j                         d       | j
                  j                  j                  d       | j                  | j
                  j                  j                         d       | j
                  j                  d       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}
        rM   rN   rO   rP   0.0.0.1   fooBAZ 
   localhost r   rT   rB   rU   rV   rW   N)rY   rZ   r[   r\   r   rD   rX   r/   r]   r^   r_   r`   r   ra   r1   rb   r   clientRequestbyterc   s       r    test_socks4aSuccessfulResolutionz-ConnectTests.test_socks4aSuccessfulResolution   s    KK1b)y)*  	 m, 	)DII""4(	) yy""((*		!!# 	&++faR063C3CK3PP	
 	,,GGHTYY667 			/22<<BBDoV 			!!..{;,,224kB		  /r   c           	      .   t        j                  dddd      t        j                  d      z   dz   dz   }t	        |      D ]  }| j
                  j                  |        | j
                  j                  j                         }| j                  |t        j                  ddd	d      t        j                  d
      z          | j                  | j
                  j                  j                         | j                  | j
                  j                         y)
        Failed hostname resolution on a SOCKSv4a packet results in a 91 error
        response and the connection getting closed.
        rM   rN   rO   rP   rg   rh      failinghost r   [   0.0.0.0NrY   rZ   r[   r\   r   rD   rX   r/   r]   r_   rI   r   assertIsNoner1   rj   s       r   test_socks4aFailedResolutionz)ConnectTests.test_socks4aFailedResolution        KK1b)y)*  	 m, 	)DII""4(	) yy""((*&++faQ/&2B2B92MM	

 			++FFG$))334r   c           	      *   d | j                   _        | j                   j                  t        j                  dddd      t        j                  d      z   dz   dz          | j                  | j                   j                  j                         t        j                  dd	d
d	      t        j                  d      z          | j                  | j                   j                  j                         | j                  | j                   j                         y )Nc                      yNr   r   codeserverr3   users       r   <lambda>z0ConnectTests.test_accessDenied.<locals>.<lambda>       r   rM   rN   rO     10.2.3.4rR   rS   r   rq   rr   )rD   	authorizerX   rY   rZ   r[   r\   r_   r/   r]   rI   r   rt   r1   r   s    r   test_accessDeniedzConnectTests.test_accessDenied   s    @				KK1d+z*+ 	
 	II%%'KK2q)F,<,<Y,GG	
 			++FFG$))334r   c                 >   | j                   j                  t        j                  dddd      t	        j
                  d      z   dz   dz          | j                   j                  j                          | j                   j                  d       | j                  | j                   j                  j                  j                         d       | j                   j                  j                  j                          | j                   j                  j                  d	       y 
NrM   rN   rO   rP   rQ   rR   rS   rU   rW   )rD   rX   rY   rZ   r[   r\   r/   r^   r_   r1   r]   r   rb   r   s    r   test_eofRemotezConnectTests.test_eofRemote   s    		KK1b)y)* 	
 			!!# 			/22<<BBDoV 			!!++::<		!!00?r   c                    | j                   j                  t        j                  dddd      t	        j
                  d      z   dz   dz          | j                   j                  j                          | j                   j                  d       | j                  | j                   j                  j                  j                         d       | j                   j                  d	       y r   )rD   rX   rY   rZ   r[   r\   r/   r^   r_   r1   r]   rb   r   s    r   test_eofLocalzConnectTests.test_eofLocal   s    		KK1b)y)* 	
 			!!# 			/22<<BBDoV 			  /r   N)r   r   r   r+   rF   rK   rd   rm   ru   r   r   r   r   r   r   r?   r?   V   s2    M02,0\5:5@"0r   r?   c                   @    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zy
)	BindTestszS
    Tests for SOCKS and SOCKSv4a bind requests using the L{SOCKSv4} protocol.
    c                     t               | _        t               | j                  _        | j                  j	                          t        ddi      | j                  _        y rA   rC   r   s    r   rF   zBindTests.setUp  rG   r   c           	         | j                   j                  t        j                  dddd      t	        j
                  d      z   dz   dz          | j                   j                  j                         }| j                   j                  j                          | j                  |t        j                  ddd	d
      t	        j
                  d      z          | j                  | j                   j                  j                         | j                  | j                   j                         | j                   j                  j                  d      }| j                  |       t               |_        |j!                          | j                   j                  j                         }| j                   j                  j                          | j                  |t        j                  ddd	d      t	        j
                  d      z          | j                  | j                   j                  j                         | j                   j                  d       | j                  |j                  j                         d       |j                  d       | j                  | j                   j                  j                         d       | j                   j#                  d       y )NrM   rN      rP   rQ   rR   rS   r   rT   r9   r:   rQ     rr   rU   rV   rW   )rD   rX   rY   rZ   r[   r\   r/   r]   r^   r_   r`   r   ra   r;   buildProtocolr   r0   rb   r   rc   incomings      r   rd   zBindTests.test_simple  s!   		KK1b)y)* 	
 yy""((*		!!#&++faT2V5E5Ei5PP	
 	,,GGHTYY445 99**889JKX&/1! yy""((*		!!#&++faQ/&2B2B92MM	
 	,,GGH 			/++113_E 	k*,,224kB		  /r   c           	         t        j                  dddd      t        j                  d      z   dz   dz   }t	        |      D ]  }| j
                  j                  |        | j
                  j                  j                         }| j
                  j                  j                          | j                  |t        j                  ddd	d
      t        j                  d      z          | j                  | j
                  j                  j                         | j                  | j
                  j                         | j
                  j                  j                  d      }| j                  |       t!               |_        |j#                          | j
                  j                  j                         }| j
                  j                  j                          | j                  |t        j                  ddd	d      t        j                  d      z          | j%                  | j
                  j                  j                  d       | j
                  j                  d       | j                  |j                  j                         d       |j                  d       | j                  | j
                  j                  j                         d       | j
                  j'                  d       y)rf   rM   rN   r   rP   rg   rh   ri   r   rT   r9   r:   )rB   r   rr   NrV   rW   )rY   rZ   r[   r\   r   rD   rX   r/   r]   r^   r_   r`   r   ra   r;   r   r   r0   assertIsNotrb   )r   rk   rl   rc   r   s        r   test_socks4azBindTests.test_socks4a7  sB    KK1b)y)*  	 m, 	)DII""4(	) yy""((*		!!# 	&++faT2V5E5Ei5PP	
 	,,GGHTYY445 99**889LMX&/1! yy""((*		!!#&++faQ/&2B2B92MM	
 	,,GGN 			{+++113[A 	k*,,224kB		  /r   c           	      .   t        j                  dddd      t        j                  d      z   dz   dz   }t	        |      D ]  }| j
                  j                  |        | j
                  j                  j                         }| j                  |t        j                  ddd	d      t        j                  d
      z          | j                  | j
                  j                  j                         | j                  | j
                  j                         y)ro   rM   rN   r   rP   rg   rh   rp   r   rq   rr   Nrs   rj   s       r   ru   z&BindTests.test_socks4aFailedResolutionr  rv   r   c           	      *   d | j                   _        | j                   j                  t        j                  dddd      t        j                  d      z   dz   dz          | j                  | j                   j                  j                         t        j                  dd	d
d	      t        j                  d      z          | j                  | j                   j                  j                         | j                  | j                   j                         y )Nc                      yry   r   rz   s       r   r~   z-BindTests.test_accessDenied.<locals>.<lambda>  r   r   rM   rN   r   r   r   rR   rS   r   rq   rr   )rD   r   rX   rY   rZ   r[   r\   r_   r/   r]   rI   r   rt   r;   r   s    r   r   zBindTests.test_accessDenied  s    @				KK1d+z*+ 	
 	II%%'KK2q)F,<,<Y,GG	
 			++FFG$))112r   c           	      "   | j                   j                  t        j                  dddd      t	        j
                  d      z   dz   dz          | j                   j                  j                         }| j                   j                  j                          | j                   j                  j                  d      }| j                  |       t               |_        |j                          | j                   j                  j                         }| j                   j                  j                          | j                  |t        j                  dd	d
d	      t	        j
                  d      z          | j                  | j                   j                  j                          | j                   j                  d       | j                  |j                  j                         d       |j                  j#                          |j%                  d       y NrM   rN   r   rP   rQ   rR   rS   r   r   rT   rr   rU   rW   )rD   rX   rY   rZ   r[   r\   r/   r]   r^   r;   r   ra   r   r0   r_   r`   r   r   rb   r   s      r   r   zBindTests.test_eofRemote  s   		KK1b)y)* 	
 yy""((*		!!# 99**889JKX&/1! yy""((*		!!#&++faQ/&2B2B92MM	
 	,,GGH 			/++113_E 	))+.r   c           	         | j                   j                  t        j                  dddd      t	        j
                  d      z   dz   dz          | j                   j                  j                         }| j                   j                  j                          | j                   j                  j                  d      }| j                  |       t               |_        |j                          | j                   j                  j                         }| j                   j                  j                          | j                  |t        j                  dd	d
d	      t	        j
                  d      z          | j                  | j                   j                  j                          | j                   j                  d       | j                  |j                  j                         d       | j                   j#                  d       y r   )rD   rX   rY   rZ   r[   r\   r/   r]   r^   r;   r   ra   r   r0   r_   r`   r   rb   r   s      r   r   zBindTests.test_eofLocal  s   		KK1b)y)* 	
 yy""((*		!!# 99**889JKX&/1! yy""((*		!!#&++faQ/&2B2B92MM	
 	,,GGH 			/++113_E 			  /r   c           	         | j                   j                  t        j                  dddd      t	        j
                  d      z   dz   dz          | j                   j                  j                         }| j                   j                  j                          | j                   j                  j                  d      }| j                  |       | j                   j                  j                         }| j                   j                  j                          | j                  |t        j                  dd	d
d	      t	        j
                  d      z          | j                  | j                   j                  j                         y )NrM   rN   r   rP   rQ   rR   rS   )z1.6.6.6i  r   rq   rr   )rD   rX   rY   rZ   r[   r\   r/   r]   r^   r;   r   rt   r_   rI   r   r   s      r   test_badSourcezBindTests.test_badSource  s&   		KK1b)y)* 	
 yy""((*		!!# 99**889IJ(# yy""((*		!!#&++faQ/&2B2B92MM	
 			++FFGr   N)r   r   r   r+   rF   rd   r   ru   r   r   r   r   r   r   r   r   r      s4    M%0N90v5:3/@0>Hr   r   )r+   r[   rY   twisted.internetr   r   twisted.internet.errorr   twisted.protocolsr   twisted.python.compatr   twisted.testr   twisted.trialr	   StringTransportr   r    SOCKSv4r-   TestCaser?   r   r   r   r   <module>r      s{   
   + 1 # + & "/66 / 60EMM 02e08$$ e0PtH!! tHr   