ó
[³XMc           @   sõ   d  Z  d d l Z d d l Z d d l m Z d d l m Z d d l m Z m	 Z	 m
 Z
 d d l m Z d d l m Z d e j f d	 „  ƒ  YZ d
 d d „  ƒ  YZ d e j f d „  ƒ  YZ d e j f d „  ƒ  YZ d e j f d „  ƒ  YZ d S(   s_   
Tests for L{twisted.protocol.socks}, an implementation of the SOCKSv4 and
SOCKSv4a protocols.
iÿÿÿÿN(   t   unittest(   t   proto_helpers(   t   defert   addresst   reactor(   t   DNSLookupError(   t   sockst   StringTCPTransportc           B   s/   e  Z e Z d Z d  „  Z d „  Z d „  Z RS(   c         C   s   |  j  S(   N(   t   peer(   t   self(    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_socks.pyt   getPeer   s    c         C   s   t  j d d d ƒ S(   Nt   TCPs   2.3.4.5i*   (   R   t   IPv4Address(   R	   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_socks.pyt   getHost   s    c         C   s   t  |  _ d  S(   N(   t   Truet   stringTCPTransport_closing(   R	   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_socks.pyt   loseConnection   s    N(	   t   __name__t
   __module__t   FalseR   t   NoneR   R
   R   R   (    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_socks.pyR      s
   		t   FakeResolverReactorc           B   s    e  Z d  Z d „  Z d „  Z RS(   sP   
    Bare-bones reactor with deterministic behavior for the resolve method.
    c         C   s   | |  _  d S(   sÃ   
        @type names: C{dict} containing C{str} keys and C{str} values.
        @param names: A hostname to IP address mapping. The IP addresses are
            stringified dotted quads.
        N(   t   names(   R	   R   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_socks.pyt   __init__%   s    c         C   sD   y t  j |  j | ƒ SWn% t k
 r? t  j t d | ƒ ƒ SXd S(   sQ   
        Resolve a hostname by looking it up in the C{names} dictionary.
        s"   FakeResolverReactor couldn't find N(   R   t   succeedR   t   KeyErrort   failR   (   R	   t   hostname(    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_socks.pyt   resolve.   s
    (   R   R   t   __doc__R   R   (    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_socks.pyR   !   s   		t   SOCKSv4Driverc           B   s&   e  Z d Z d Z d  „  Z d „  Z RS(   c         G   sS   | | Œ  } t  ƒ  | _ t j d | | ƒ | j _ | j ƒ  | |  _ t j | ƒ S(   NR   (	   R   t	   transportR   R   R   t   connectionMadet   driver_outgoingR   R   (   R	   t   hostt   portt   klasst   argst   proto(    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_socks.pyt   connectClassA   s    
	c         G   s=   | | Œ  } | |  _  | d k r* d } n  t j d | f ƒ S(   Ni    iÒ  s   6.7.8.9(   t   driver_listenR   R   (   R	   R#   R$   R%   t   factory(    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_socks.pyt   listenClassJ   s
    		N(   R   R   R   R!   R(   R'   R*   (    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_socks.pyR   :   s   		t   Connectc           B   sV   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(	   sV   
    Tests for SOCKS and SOCKSv4a connect requests using the L{SOCKSv4} protocol.
    c         C   sE   t  ƒ  |  _ t ƒ  |  j _ |  j j ƒ  t i d d 6ƒ |  j _ d  S(   Ns	   127.0.0.1t	   localhost(   R   t   sockR   R   R    R   R   (   R	   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_socks.pyt   setUpX   s    c         C   s5   |  j  j } | d  k	 r1 |  j | j j d ƒ n  d  S(   Ns-   Outgoing SOCKS connections need to be closed.(   R-   R!   R   t   assert_R   R   (   R	   t   outgoing(    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_socks.pyt   tearDown_   s    c         C   s*  |  j  j t j d d d d ƒ t j d ƒ d d ƒ |  j  j j ƒ  } |  j  j j ƒ  |  j	 | t j d d d	 d ƒ t j d ƒ ƒ |  j
 |  j  j j ƒ |  j
 |  j  j d  k	 ƒ |  j  j d
 ƒ |  j	 |  j  j j j ƒ  d
 ƒ |  j  j j d ƒ |  j	 |  j  j j ƒ  d ƒ |  j  j d ƒ d  S(   Ns   !BBHi   i   i"   s   1.2.3.4t   fooBARt    i    iZ   s   hello, worlds   hi theres   fake reason(   R-   t   dataReceivedt   structt   packt   sockett	   inet_atonR   t   valuet   cleart   assertEqualR/   R   R!   R   t   connectionLost(   R	   t   sent(    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_socks.pyt   test_simplef   s    	.	c         C   s=  t  j d d d d ƒ t j d ƒ d d } x | D] } |  j j | ƒ q4 W|  j j j ƒ  } |  j j j ƒ  |  j	 | t  j d d d	 d ƒ t j d
 ƒ ƒ |  j
 |  j j j ƒ |  j |  j j d ƒ |  j j d ƒ |  j	 |  j j j j ƒ  d ƒ |  j j j d ƒ |  j	 |  j j j ƒ  d ƒ |  j j d ƒ d S(   su  
        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}
        s   !BBHi   i   i"   s   0.0.0.1t   fooBAZ t
   localhost i    iZ   s	   127.0.0.1s   hello, worlds   hi theres   fake reasonN(   R5   R6   R7   R8   R-   R4   R   R9   R:   t   assertEqualst   assertFalseR   t   assertNotIdenticalR!   R   R<   (   R	   t   clientRequestt   byteR=   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_socks.pyt    test_socks4aSuccessfulResolution€   s     -&c         C   s¿   t  j d d d d ƒ t j d ƒ d d } x | D] } |  j j | ƒ q4 W|  j j j ƒ  } |  j | t  j d d d	 d ƒ t j d
 ƒ ƒ |  j	 |  j j j
 ƒ |  j |  j j d ƒ d S(   s‹   
        Failed hostname resolution on a SOCKSv4a packet results in a 91 error
        response and the connection getting closed.
        s   !BBHi   i   i"   s   0.0.0.1R?   t   failinghost i    i[   s   0.0.0.0N(   R5   R6   R7   R8   R-   R4   R   R9   RA   t
   assertTrueR   t   assertIdenticalR!   R   (   R	   RD   RE   R=   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_socks.pyt   test_socks4aFailedResolution¯   s    
-&c         C   s±   d „  |  j  _ |  j  j t j d d d d ƒ t j d ƒ d d ƒ |  j |  j  j j	 ƒ  t j d d	 d
 d	 ƒ t j d ƒ ƒ |  j
 |  j  j j ƒ |  j |  j  j d  ƒ d  S(   Nc         S   s   d S(   Ni    (    (   t   codet   serverR#   t   user(    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_socks.pyt   <lambda>Í   s    s   !BBHi   i   i’  s   10.2.3.4R2   R3   i    i[   s   0.0.0.0(   R-   t	   authorizeR4   R5   R6   R7   R8   R;   R   R9   R/   R   RI   R!   R   (   R	   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_socks.pyt   test_accessDeniedÌ   s    	.c         C   s²   |  j  j t j d d d d ƒ t j d ƒ d d ƒ |  j  j j ƒ  } |  j  j j ƒ  |  j  j d ƒ |  j	 |  j  j
 j j ƒ  d ƒ |  j  j
 j j ƒ  |  j  j
 j d	 ƒ d  S(
   Ns   !BBHi   i   i"   s   1.2.3.4R2   R3   s   hello, worlds   fake reason(   R-   R4   R5   R6   R7   R8   R   R9   R:   R;   R!   R   R<   (   R	   R=   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_socks.pyt   test_eofRemoteÚ   s    	.c         C   sœ   |  j  j t j d d d d ƒ t j d ƒ d d ƒ |  j  j j ƒ  } |  j  j j ƒ  |  j  j d ƒ |  j	 |  j  j
 j j ƒ  d ƒ |  j  j d	 ƒ d  S(
   Ns   !BBHi   i   i"   s   1.2.3.4R2   R3   s   hello, worlds   fake reason(   R-   R4   R5   R6   R7   R8   R   R9   R:   R;   R!   R<   (   R	   R=   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_socks.pyt   test_eofLocalí   s    	.(   R   R   R   R.   R1   R>   RF   RJ   RP   RQ   RR   (    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_socks.pyR+   T   s   				/			t   Bindc           B   sV   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(	   sS   
    Tests for SOCKS and SOCKSv4a bind requests using the L{SOCKSv4} protocol.
    c         C   sE   t  ƒ  |  _ t ƒ  |  j _ |  j j ƒ  t i d d 6ƒ |  j _ d  S(   Ns	   127.0.0.1R,   (   R   R-   R   R   R    R   R   (   R	   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_socks.pyR.     s    c         C   sÁ  |  j  j t j d d d d ƒ t j d ƒ d d ƒ |  j  j j ƒ  } |  j  j j ƒ  |  j	 | t j d d d	 d
 ƒ t j d ƒ ƒ |  j
 |  j  j j ƒ |  j
 |  j  j d  k	 ƒ |  j  j j d ƒ } |  j | d  ƒ t ƒ  | _ | j ƒ  |  j  j j ƒ  } |  j  j j ƒ  |  j	 | t j d d d	 d ƒ t j d ƒ ƒ |  j
 |  j  j j ƒ |  j  j d ƒ |  j	 | j j ƒ  d ƒ | j d ƒ |  j	 |  j  j j ƒ  d ƒ |  j  j d ƒ d  S(   Ns   !BBHi   i   i"   s   1.2.3.4R2   R3   i    iZ   iÒ  s   6.7.8.9iá  s   0.0.0.0s   hello, worlds   hi theres   fake reason(   s   1.2.3.4iá  (   R-   R4   R5   R6   R7   R8   R   R9   R:   R;   R/   R   R(   R   t   buildProtocolRC   R   R    R<   (   R	   R=   t   incoming(    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_socks.pyR>     s2    	.	
	c         C   sÖ  t  j d d d d ƒ t j d ƒ d d } x | D] } |  j j | ƒ q4 W|  j j j ƒ  } |  j j j ƒ  |  j	 | t  j d d d	 d
 ƒ t j d ƒ ƒ |  j
 |  j j j ƒ |  j |  j j d ƒ |  j j j d ƒ } |  j | d ƒ t ƒ  | _ | j ƒ  |  j j j ƒ  } |  j j j ƒ  |  j | t  j d d d	 d ƒ t j d ƒ ƒ |  j |  j j j d ƒ |  j j d ƒ |  j	 | j j ƒ  d ƒ | j d ƒ |  j |  j j j ƒ  d ƒ |  j j d ƒ d S(   su  
        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}
        s   !BBHi   i   i"   s   0.0.0.1R?   R@   i    iZ   iÒ  s   6.7.8.9s	   127.0.0.1iá  s   0.0.0.0s   hi theres   fake reasonN(   s	   127.0.0.1iá  (   R5   R6   R7   R8   R-   R4   R   R9   R:   RA   RB   R   RC   R(   R   RT   R   R    R;   R<   (   R	   RD   RE   R=   RU   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_socks.pyt   test_socks4a9  s4    -&
	c         C   s¿   t  j d d d d ƒ t j d ƒ d d } x | D] } |  j j | ƒ q4 W|  j j j ƒ  } |  j | t  j d d d	 d ƒ t j d
 ƒ ƒ |  j	 |  j j j
 ƒ |  j |  j j d ƒ d S(   s‹   
        Failed hostname resolution on a SOCKSv4a packet results in a 91 error
        response and the connection getting closed.
        s   !BBHi   i   i"   s   0.0.0.1R?   RG   i    i[   s   0.0.0.0N(   R5   R6   R7   R8   R-   R4   R   R9   RA   RH   R   RI   R!   R   (   R	   RD   RE   R=   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_socks.pyRJ   u  s    
-&c         C   s±   d „  |  j  _ |  j  j t j d d d d ƒ t j d ƒ d d ƒ |  j |  j  j j	 ƒ  t j d d	 d
 d	 ƒ t j d ƒ ƒ |  j
 |  j  j j ƒ |  j |  j  j d  ƒ d  S(   Nc         S   s   d S(   Ni    (    (   RK   RL   R#   RM   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_socks.pyRN   “  s    s   !BBHi   i   i’  s   10.2.3.4R2   R3   i    i[   s   0.0.0.0(   R-   RO   R4   R5   R6   R7   R8   R;   R   R9   R/   R   RI   R(   R   (   R	   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_socks.pyRP   ’  s    	.c         C   sC  |  j  j t j d d d d ƒ t j d ƒ d d ƒ |  j  j j ƒ  } |  j  j j ƒ  |  j  j	 j
 d ƒ } |  j | d  ƒ t ƒ  | _ | j ƒ  |  j  j j ƒ  } |  j  j j ƒ  |  j | t j d d	 d
 d	 ƒ t j d ƒ ƒ |  j |  j  j j ƒ |  j  j d ƒ |  j | j j ƒ  d ƒ | j j ƒ  | j d ƒ d  S(   Ns   !BBHi   i   i"   s   1.2.3.4R2   R3   iá  i    iZ   s   0.0.0.0s   hello, worlds   fake reason(   s   1.2.3.4iá  (   R-   R4   R5   R6   R7   R8   R   R9   R:   R(   RT   RC   R   R   R    R;   R/   R   R   R<   (   R	   R=   RU   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_socks.pyRQ   Ÿ  s&    	.
	c         C   s9  |  j  j t j d d d d ƒ t j d ƒ d d ƒ |  j  j j ƒ  } |  j  j j ƒ  |  j  j	 j
 d ƒ } |  j | d  ƒ t ƒ  | _ | j ƒ  |  j  j j ƒ  } |  j  j j ƒ  |  j | t j d d	 d
 d	 ƒ t j d ƒ ƒ |  j |  j  j j ƒ |  j  j d ƒ |  j | j j ƒ  d ƒ |  j  j d ƒ d  S(   Ns   !BBHi   i   i"   s   1.2.3.4R2   R3   iá  i    iZ   s   0.0.0.0s   hello, worlds   fake reason(   s   1.2.3.4iá  (   R-   R4   R5   R6   R7   R8   R   R9   R:   R(   RT   RC   R   R   R    R;   R/   R   R<   (   R	   R=   RU   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_socks.pyRR   ¿  s$    	.
	c         C   sé   |  j  j t j d d d d ƒ t j d ƒ d d ƒ |  j  j j ƒ  } |  j  j j ƒ  |  j  j	 j
 d ƒ } |  j | d  ƒ |  j  j j ƒ  } |  j  j j ƒ  |  j | t j d d
 d d
 ƒ t j d ƒ ƒ |  j |  j  j j ƒ d  S(   Ns   !BBHi   i   i"   s   1.2.3.4R2   R3   s   1.6.6.6iš  i    i[   s   0.0.0.0(   s   1.6.6.6iš  (   R-   R4   R5   R6   R7   R8   R   R9   R:   R(   RT   RI   R   R;   R/   R   (   R	   R=   RU   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_socks.pyt   test_badSourceÞ  s    	.	(   R   R   R   R.   R>   RV   RJ   RP   RQ   RR   RW   (    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_socks.pyRS      s   		(	<			 	(    (   R   R5   R7   t   twisted.trialR    t   twisted.testR   t   twisted.internetR   R   R   t   twisted.internet.errorR   t   twisted.protocolsR   t   StringTransportR   R   t   SOCKSv4R   t   TestCaseR+   RS   (    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/test/test_socks.pyt   <module>   s   ¬