
    \dXD                        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dS )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dS )StringTCPTransportFNc                     | j         S )N)peerselfs    7lib/python3.11/site-packages/twisted/test/test_socks.pygetPeerzStringTCPTransport.getPeer   s
    y    c                 .    t          j        ddd          S )NTCPz2.3.4.5*   )r   IPv4Addressr   s    r   getHostzStringTCPTransport.getHost   s    "5)R888r   c                     d| _         d S )NT)stringTCPTransport_closingr   s    r   loseConnectionz!StringTCPTransport.loseConnection   s    *.'''r   )__name__
__module____qualname__r   r   r   r   r    r   r   r   r      sL        !&D  9 9 9/ / / / /r   r   c                       e Zd ZdZd Zd ZdS )FakeResolverReactorzP
    Bare-bones reactor with deterministic behavior for the resolve method.
    c                     || _         dS )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          $ r: t          j        t          d|                    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6 	 	 	:88??7;S;SS     	s   ! AA%$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dS )SOCKSv4DriverNc                      || }t                      |_        t          j        d||          |j        _        |                                 || _        t          j        |          S )Nr   )	r   	transportr   r   r   connectionMadedriver_outgoingr   r%   )r   hostportklassargsprotos         r   connectClasszSOCKSv4Driver.connectClassD   s]    t,..&25$E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   s8    %,$199D}i.///r   )r   r   r   r1   r;   r7   r=   r   r   r   r-   r-   =   s>        O M$ $ $0 0 0 0 0r   r-   c                   B    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
S )ConnectTestszV
    Tests for SOCKS and SOCKSv4a connect requests using the L{SOCKSv4} protocol.
    c                     t                      | _        t                      | j        _        | j                                         t          ddi          | j        _        d S Ns	   localhost	127.0.0.1r-   sockr   r/   r0   r    reactorr   s    r   setUpzConnectTests.setUp[   N    !OO	022		  """/{0KLL	r   c                 f    | j         j        }|"|                     |j        j        d           d S d S )Nz-Outgoing SOCKS connections need to be closed.)rD   r1   
assertTruer/   r   )r   outgoings     r   tearDownzConnectTests.tearDowna   sF    9,OO"=?      r   c           	         | 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                             d
           | 	                    | j         j        j                                        d
           | j         j                            d           | 	                    | j         j                                        d           | j                             d           d S )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   s   	K1b))y))* 	
 	
 	
 y"((**	!!###&+faR0063CI3N3NN	
 	
 	
 	,GHHHTY6777 		///2<BBDDoVVV 		!..{;;;,2244kBBB	  /////r   c           	         t          j        dddd          t          j        d          z   dz   dz   }t	          |          D ]}| 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           | 
                    | j        j                                        d           | j                            d           dS )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    K1b))y))*  	 m,, 	) 	)DI""4((((y"((**	!!### 	&+faR0063CK3P3PP	
 	
 	
 	,GHHHTY6777 		///2<BBDDoVVV 		!..{;;;,2244kBBB	  /////r   c           	         t          j        dddd          t          j        d          z   dz   dz   }t	          |          D ]}| j                            |           | j        j                                        }| 	                    |t          j        ddd	d          t          j        d
          z              | 
                    | j        j        j                   |                     | j        j                   dS )
        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       K1b))y))*  	 m,, 	) 	)DI""4(((( y"((**&+faQ//&2B92M2MM	
 	
 	

 		+FGGG$)344444r   c           	         d | j         _        | j                             t          j        dddd          t          j        d          z   dz   dz              |                     | j         j        	                                t          j        dd	d
d	          t          j        d          z              | 
                    | j         j        j                   |                     | j         j                   d S )Nc                     dS Nr   r   codeserverr3   users       r   <lambda>z0ConnectTests.test_accessDenied.<locals>.<lambda>       q 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    @@		K1d++z**+ 	
 	
 	
 	I%%''K2q))F,<Y,G,GG	
 	
 	
 		+FGGG$)344444r   c                    | j                             t          j        dddd          t	          j        d          z   dz   dz              | j         j                                         | j                             d           |                     | j         j	        j        
                                d           | j         j	        j                                         | j         j	                            d	           d S 
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    	K1b))y))* 	
 	
 	
 		!!### 		///2<BBDDoVVV 		!+::<<<	!00?????r   c                    | j                             t          j        dddd          t	          j        d          z   dz   dz              | j         j                                         | j                             d           |                     | j         j	        j        
                                d           | j                             d	           d S r   )rD   rX   rY   rZ   r[   r\   r/   r^   r_   r1   r]   rb   r   s    r   test_eofLocalzConnectTests.test_eofLocal   s    	K1b))y))* 	
 	
 	
 		!!### 		///2<BBDDoVVV 		  /////r   N)r   r   r   r+   rF   rK   rd   rm   ru   r   r   r   r   r   r   r?   r?   V   s         M M M  0 0 02,0 ,0 ,0\5 5 5:5 5 5@ @ @"0 0 0 0 0r   r?   c                   B    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
S )	BindTestszS
    Tests for SOCKS and SOCKSv4a bind requests using the L{SOCKSv4} protocol.
    c                     t                      | _        t                      | j        _        | j                                         t          ddi          | j        _        d S rA   rC   r   s    r   rF   zBindTests.setUp  rG   r   c           	      ^   | 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                            d          }|                     |           t                      |_        |                                 | j         j                                        }| j         j                                         | 	                    |t          j        ddd	d          t	          j        d          z              | 
                    | j         j        j                   | j                             d           | 	                    |j                                        d           |                    d           | 	                    | j         j                                        d           | j                             d           d S )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  sf   	K1b))y))* 	
 	
 	
 y"((**	!!###&+faT22V5Ei5P5PP	
 	
 	
 	,GHHHTY4555 9*889JKKX&&&/11!!! y"((**	!!###&+faQ//&2B92M2MM	
 	
 	
 	,GHHH 		///+1133_EEE 	k***,2244kBBB	  /////r   c           	         t          j        dddd          t          j        d          z   dz   dz   }t	          |          D ]}| j                            |           | j        j                                        }| j        j        	                                 | 
                    |t          j        ddd	d
          t          j        d          z              |                     | j        j        j                   |                     | j        j                   | j        j                            d          }|                     |           t!                      |_        |                                 | j        j                                        }| j        j        	                                 | 
                    |t          j        ddd	d          t          j        d          z              |                     | j        j        j        d           | j                            d           | 
                    |j                                        d           |                    d           | 
                    | j        j                                        d           | j                            d           dS )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  s    K1b))y))*  	 m,, 	) 	)DI""4((((y"((**	!!### 	&+faT22V5Ei5P5PP	
 	
 	
 	,GHHHTY4555 9*889LMMX&&&/11!!! y"((**	!!###&+faQ//&2B92M2MM	
 	
 	
 	,GNNN 		{++++1133[AAA 	k***,2244kBBB	  /////r   c           	         t          j        dddd          t          j        d          z   dz   dz   }t	          |          D ]}| j                            |           | j        j                                        }| 	                    |t          j        ddd	d          t          j        d
          z              | 
                    | j        j        j                   |                     | j        j                   dS )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                             t          j        dddd          t          j        d          z   dz   dz              |                     | j         j        	                                t          j        dd	d
d	          t          j        d          z              | 
                    | j         j        j                   |                     | j         j                   d S )Nc                     dS 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    @@		K1d++z**+ 	
 	
 	
 	I%%''K2q))F,<Y,G,GG	
 	
 	
 		+FGGG$)122222r   c           	         | j                             t          j        dddd          t	          j        d          z   dz   dz              | j         j                                        }| j         j                                         | j         j	        
                    d          }|                     |           t                      |_        |                                 | j         j                                        }| j         j                                         |                     |t          j        dd	d
d	          t	          j        d          z              |                     | j         j        j                   | j                             d           |                     |j                                        d           |j                                         |                    d           d S 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   	K1b))y))* 	
 	
 	
 y"((**	!!### 9*889JKKX&&&/11!!! y"((**	!!###&+faQ//&2B92M2MM	
 	
 	
 	,GHHH 		///+1133_EEE 	))+++.....r   c           	         | j                             t          j        dddd          t	          j        d          z   dz   dz              | j         j                                        }| j         j                                         | j         j	        
                    d          }|                     |           t                      |_        |                                 | j         j                                        }| j         j                                         |                     |t          j        dd	d
d	          t	          j        d          z              |                     | j         j        j                   | j                             d           |                     |j                                        d           | j                             d           d S 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   	K1b))y))* 	
 	
 	
 y"((**	!!### 9*889JKKX&&&/11!!! y"((**	!!###&+faQ//&2B92M2MM	
 	
 	
 	,GHHH 		///+1133_EEE 		  /////r   c           	         | j                             t          j        dddd          t	          j        d          z   dz   dz              | j         j                                        }| j         j                                         | j         j	        
                    d          }|                     |           | j         j                                        }| j         j                                         |                     |t          j        dd	d
d	          t	          j        d          z              |                     | j         j        j                   d S )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@   	K1b))y))* 	
 	
 	
 y"((**	!!### 9*889IJJ(### y"((**	!!###&+faQ//&2B92M2MM	
 	
 	
 		+FGGGGGr   N)r   r   r   r+   rF   rd   r   ru   r   r   r   r   r   r   r   r   r      s         M M M%0 %0 %0N90 90 90v5 5 5:3 3 3/ / /@0 0 0>H H H H 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 1 1 1 1 1 # # # # # # + + + + + + & & & & & & " " " " " "/ / / / /6 / / /       60 0 0 0 0EM 0 0 02e0 e0 e0 e0 e08$ e0 e0 e0PtH tH tH tH tH! tH tH tH tH tHr   