Your IP : 216.73.216.213


Current Path : /opt/alt/python37/lib64/python3.7/site-packages/twisted/test/__pycache__/
Upload File :
Current File : //opt/alt/python37/lib64/python3.7/site-packages/twisted/test/__pycache__/test_socks.cpython-37.pyc

B

;��]JE�@s�dZddlZddlZddlmZmZddlmZddlm	Z	ddl
mZddlm
Z
ddlmZGd	d
�d
e
j�ZGdd�d�ZGd
d�dej�ZGdd�dej�ZGdd�dej�ZdS)z_
Tests for L{twisted.protocol.socks}, an implementation of the SOCKSv4 and
SOCKSv4a protocols.
�N)�defer�address)�DNSLookupError)�	iterbytes)�socks)�
proto_helpers)�unittestc@s,eZdZdZdZdd�Zdd�Zdd�ZdS)	�StringTCPTransportFNcCs|jS)N)�peer)�self�r�J/opt/alt/python37/lib64/python3.7/site-packages/twisted/test/test_socks.py�getPeerszStringTCPTransport.getPeercCst�ddd�S)N�TCPz2.3.4.5�*)r�IPv4Address)rrrr
�getHostszStringTCPTransport.getHostcCs
d|_dS)NT)�stringTCPTransport_closing)rrrr
�loseConnectionsz!StringTCPTransport.loseConnection)�__name__�
__module__�__qualname__rr
rrrrrrr
r	s
r	c@s eZdZdZdd�Zdd�ZdS)�FakeResolverReactorzP
    Bare-bones reactor with deterministic behavior for the resolve method.
    cCs
||_dS)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)rrrrr
�__init__(szFakeResolverReactor.__init__c
Cs>yt�|j|�Stk
r8t�td|�d���SXdS)zQ
        Resolve a hostname by looking it up in the C{names} dictionary.
        z"FakeResolverReactor couldn't find zutf-8N)r�succeedr�KeyErrorZfailr�decode)r�hostnamerrr
�resolve1szFakeResolverReactor.resolveN)rrr�__doc__rrrrrr
r$s	rc@s$eZdZdZdZdd�Zdd�ZdS)�
SOCKSv4DriverNcGs:||�}t�|_t�d||�|j_|��||_t�|�S)Nr)	r	�	transportrrr
�connectionMade�driver_outgoingrr)r�host�port�klass�args�protorrr
�connectClassEszSOCKSv4Driver.connectClasscGs(||�}||_|dkrd}t�d|f�S)Nri�z6.7.8.9)�
driver_listenrr)rr&r'r(�factoryrrr
�listenClassOs
zSOCKSv4Driver.listenClass)rrrr$r+r*r-rrrr
r!>s
r!c@sPeZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�ZdS)�ConnectTestszV
    Tests for SOCKS and SOCKSv4a connect requests using the L{SOCKSv4} protocol.
    cCs0t�|_t�|j_|j��tddi�|j_dS)Ns	localhostz	127.0.0.1)r!�sockr	r"r#r�reactor)rrrr
�setUp]s

zConnectTests.setUpcCs$|jj}|dk	r |�|jjd�dS)Nz-Outgoing SOCKS connections need to be closed.)r/r$�
assertTruer"r)rZoutgoingrrr
�tearDownds
zConnectTests.tearDownc	Cs�|j�t�dddd�t�d�dd�|jj��}|jj��|�	|t�ddd	d�t�d��|�
|jjj�|�|jj
�|j�d
�|�	|jj
j��d
�|jj
�d�|�	|jj��d�|j�d�dS)
Nz!BBH���"z1.2.3.4sfooBAR�r�Zshello, worldshi therezfake reason)r/�dataReceived�struct�pack�socket�	inet_atonr"�value�clear�assertEqual�assertFalser�assertIsNotNoner$�connectionLost)r�sentrrr
�test_simpleks$zConnectTests.test_simplec	Cs�t�dddd�t�d�dd}xt|�D]}|j�|�q,W|jj��}|jj�	�|�
|t�ddd	d�t�d
��|�|jjj�|�
|jj�|j�d�|�
|jjj��d�|jj�d�|�
|jj��d�|j�d
�dS)au
        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}
        z!BBHr4r5r6z0.0.0.1sfooBAZs
localhostrr8z	127.0.0.1shello, worldshi therezfake reasonN)r:r;r<r=rr/r9r"r>r?r@rArrBr$rC)r�
clientRequest�byterDrrr
� test_socks4aSuccessfulResolution�s "z-ConnectTests.test_socks4aSuccessfulResolutionc	Cs�t�dddd�t�d�dd}xt|�D]}|j�|�q,W|jj��}|�	|t�ddd	d�t�d
��|�
|jjj�|�|jj
�dS)z�
        Failed hostname resolution on a SOCKSv4a packet results in a 91 error
        response and the connection getting closed.
        z!BBHr4r5r6z0.0.0.1sfooBAZsfailinghostr�[z0.0.0.0N)r:r;r<r=rr/r9r"r>r@r2r�assertIsNoner$)rrFrGrDrrr
�test_socks4aFailedResolution�s
"z)ConnectTests.test_socks4aFailedResolutionc	Cs�dd�|j_|j�t�dddd�t�d�dd	�|�|jj�	�t�dd
dd
�t�d��|�
|jjj�|�|jj
�dS)
NcSsdS)Nrr)�code�serverr&�userrrr
�<lambda>��z0ConnectTests.test_accessDenied.<locals>.<lambda>z!BBHr4r5i�z10.2.3.4sfooBARr7rrIz0.0.0.0)r/�	authorizer9r:r;r<r=r@r"r>r2rrJr$)rrrr
�test_accessDenied�s$zConnectTests.test_accessDeniedcCsx|j�t�dddd�t�d�dd�|jj��|j�d�|�|jj	j�
�d�|jj	j��|jj	�d	�dS)
Nz!BBHr4r5r6z1.2.3.4sfooBARr7shello, worldzfake reason)
r/r9r:r;r<r=r"r?r@r$r>rrC)rrrr
�test_eofRemote�s$zConnectTests.test_eofRemotecCsh|j�t�dddd�t�d�dd�|jj��|j�d�|�|jj	j�
�d�|j�d	�dS)
Nz!BBHr4r5r6z1.2.3.4sfooBARr7shello, worldzfake reason)r/r9r:r;r<r=r"r?r@r$r>rC)rrrr
�
test_eofLocal�s$zConnectTests.test_eofLocalN)rrrr r1r3rErHrKrRrSrTrrrr
r.Ys/r.c@sPeZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�ZdS)�	BindTestszS
    Tests for SOCKS and SOCKSv4a bind requests using the L{SOCKSv4} protocol.
    cCs0t�|_t�|j_|j��tddi�|j_dS)Ns	localhostz	127.0.0.1)r!r/r	r"r#rr0)rrrr
r1s

zBindTests.setUpc	Cs@|j�t�dddd�t�d�dd�|jj��}|jj��|�	|t�ddd	d
�t�d��|�
|jjj�|�|jj
�|jj
�d�}|�|�t�|_|��|jj��}|jj��|�	|t�ddd	d�t�d
��|�
|jjj�|j�d�|�	|j��d�|�d�|�	|jj��d�|j�d�dS)Nz!BBHr4�r6z1.2.3.4sfooBARr7rr8i�z6.7.8.9)z1.2.3.4i�z0.0.0.0shello, worldshi therezfake reason)r/r9r:r;r<r=r"r>r?r@rArrBr+�
buildProtocolr	r#rC)rrD�incomingrrr
rEs2$

zBindTests.test_simplec	CsXt�dddd�t�d�dd}xt|�D]}|j�|�q,W|jj��}|jj�	�|�
|t�ddd	d
�t�d��|�|jjj�|�
|jj�|jj�d�}|�
|�t�|_|��|jj��}|jj�	�|�
|t�ddd	d�t�d
��|�|jjjd�|j�d�|�
|j��d�|�d�|�
|jj��d�|j�d�dS)au
        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}
        z!BBHr4rVr6z0.0.0.1sfooBAZs
localhostrr8i�z6.7.8.9)z	127.0.0.1i�z0.0.0.0Nshi therezfake reason)r:r;r<r=rr/r9r"r>r?r@rArrBr+rWr	r#ZassertIsNotrC)rrFrGrDrXrrr
�test_socks4a<s4"

zBindTests.test_socks4ac	Cs�t�dddd�t�d�dd}xt|�D]}|j�|�q,W|jj��}|�	|t�ddd	d�t�d
��|�
|jjj�|�|jj
�dS)z�
        Failed hostname resolution on a SOCKSv4a packet results in a 91 error
        response and the connection getting closed.
        z!BBHr4rVr6z0.0.0.1sfooBAZsfailinghostrrIz0.0.0.0N)r:r;r<r=rr/r9r"r>r@r2rrJr$)rrFrGrDrrr
rKxs
"z&BindTests.test_socks4aFailedResolutionc	Cs�dd�|j_|j�t�dddd�t�d�dd	�|�|jj�	�t�dd
dd
�t�d��|�
|jjj�|�|jj
�dS)
NcSsdS)Nrr)rLrMr&rNrrr
rO�rPz-BindTests.test_accessDenied.<locals>.<lambda>z!BBHr4rVi�z10.2.3.4sfooBARr7rrIz0.0.0.0)r/rQr9r:r;r<r=r@r"r>r2rrJr+)rrrr
rR�s$zBindTests.test_accessDeniedc	Cs�|j�t�dddd�t�d�dd�|jj��}|jj��|jj	�
d�}|�|�t�|_|�
�|jj��}|jj��|�|t�dd	d
d	�t�d��|�|jjj�|j�d�|�|j��d�|j��|�d
�dS)Nz!BBHr4rVr6z1.2.3.4sfooBARr7)z1.2.3.4i�rr8z0.0.0.0shello, worldzfake reason)r/r9r:r;r<r=r"r>r?r+rWrBr	r#r@rArrrC)rrDrXrrr
rS�s&$

zBindTests.test_eofRemotec	Cs�|j�t�dddd�t�d�dd�|jj��}|jj��|jj	�
d�}|�|�t�|_|�
�|jj��}|jj��|�|t�dd	d
d	�t�d��|�|jjj�|j�d�|�|j��d�|j�d
�dS)Nz!BBHr4rVr6z1.2.3.4sfooBARr7)z1.2.3.4i�rr8z0.0.0.0shello, worldzfake reason)r/r9r:r;r<r=r"r>r?r+rWrBr	r#r@rArrC)rrDrXrrr
rT�s$$
zBindTests.test_eofLocalc	Cs�|j�t�dddd�t�d�dd�|jj��}|jj��|jj	�
d�}|�|�|jj��}|jj��|�|t�dd	d
d	�t�d��|�
|jjj�dS)Nz!BBHr4rVr6z1.2.3.4sfooBARr7)z1.6.6.6i�rrIz0.0.0.0)r/r9r:r;r<r=r"r>r?r+rWrJr@r2r)rrDrXrrr
�test_badSource�s$
zBindTests.test_badSourceN)rrrr r1rErYrKrRrSrTrZrrrr
rUs
(<! rU)r r:r<Ztwisted.internetrrZtwisted.internet.errorrZtwisted.python.compatrZtwisted.protocolsrZtwisted.testrZ
twisted.trialrZStringTransportr	rZSOCKSv4r!ZTestCaser.rUrrrr
�<module>s+