
    \d                        d Z ddlmZmZmZmZ ddlmZ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
ej                  Z G d dej                  Z G d dej                  Z G d dej                  ZdS )z+
Test cases for twisted.protocols package.
    )addressdeferprotocolreactor)portforwardwire)	iterbytes)proto_helpers)unittestc                   *    e Zd ZdZd Zd Zd Zd ZdS )	WireTestsz
    Test wire protocols.
    c                    t          j                    }t          j                    }|                    |           |                    d           |                    d           |                    d           |                    d           |                    d           |                     |                                d           dS )zT
        Test wire.Echo protocol: send some data and check it send it back.
           hello   world   how   are   yous   helloworldhowareyouN)r
   StringTransportr   EchomakeConnectiondataReceivedassertEqualvalueselftas      ;lib/python3.11/site-packages/twisted/test/test_protocols.py	test_echozWireTests.test_echo   s     )++IKK		x   	x   	v	v	v$:;;;;;    c                     t          j                    }t          j                    }|                    |           |                     |                                d           dS )z)
        Test wire.Who protocol.
        s   root
N)r
   r   r   Whor   r   r   r   s      r   test_whozWireTests.test_who"   sU     )++HJJ	K00000r    c                     t          j                    }t          j                    }|                    |           |                     |                                d           dS )z*
        Test wire.QOTD protocol.
        s'   An apple a day keeps the doctor away.
N)r
   r   r   QOTDr   r   r   r   s      r   	test_QOTDzWireTests.test_QOTD+   sV     )++IKK	$PQQQQQr    c                    t          j                    }t          j                    }|                    |           |                    d           |                    d           |                    d           |                    d           |                    d           |                     |                                d           dS )z-
        Test wire.Discard protocol.
        r   r   r   r   r   r    N)r
   r   r   Discardr   r   r   r   r   s      r   test_discardzWireTests.test_discard4   s     )++LNN		x   	x   	v	v	vC(((((r    N)__name__
__module____qualname____doc__r   r#   r&   r)    r    r   r   r      s]         < < <1 1 1R R R) ) ) ) )r    r   c                       e Zd ZdZd ZdS )TestableProxyClientFactoryz
    Test proxy client factory that keeps the last created protocol instance.

    @ivar protoInstance: the last instance of the protocol.
    @type protoInstance: L{portforward.ProxyClient}
    c                 T    t           j                            | |          }|| _        |S )zE
        Create the protocol instance and keeps track of it.
        )r   ProxyClientFactorybuildProtocolprotoInstancer   addrprotos      r   r3   z(TestableProxyClientFactory.buildProtocolK   s)     .<<T4HH"r    Nr*   r+   r,   r-   r3   r.   r    r   r0   r0   C   s-             r    r0   c                       e Zd ZdZd ZdS )TestableProxyFactoryal  
    Test proxy factory that keeps the last created protocol instance.

    @ivar protoInstance: the last instance of the protocol.
    @type protoInstance: L{portforward.ProxyServer}

    @ivar clientFactoryInstance: client factory used by C{protoInstance} to
        create forward connections.
    @type clientFactoryInstance: L{TestableProxyClientFactory}
    c                      t           j                             |          }t                       _         fd|_        | _        |S )z
        Create the protocol instance, keeps track of it, and makes it use
        C{clientFactoryInstance} as client factory.
        c                       j         S N)clientFactoryInstancer   s   r   <lambda>z4TestableProxyFactory.buildProtocol.<locals>.<lambda>h   s
    d.H r    )r   ProxyFactoryr3   r0   r>   clientProtocolFactoryr4   r5   s   `  r   r3   z"TestableProxyFactory.buildProtocol`   sI    
 (66tTBB%?%A%A"&H&H&H&H#"r    Nr8   r.   r    r   r:   r:   T   s-        	 	
 
 
 
 
r    r:   c                   *    e Zd ZdZd Zd Zd Zd ZdS )PortforwardingTestsz
    Test port forwarding.
    c                 t    t          j                    | _        t          j                    | _        g | _        d S r=   )r   r   serverProtocolr   ProtocolclientProtocol	openPortsr?   s    r   setUpzPortforwardingTests.setUpr   s,    "ikk&/11r    c                    	 | j         j        j                                         n# t          $ r Y nw xY w	 | j         j        j        }|j                                         n# t          $ r Y nw xY w	 | j        j                                         n# t          $ r Y nw xY w	 | j        j                                         n# t          $ r Y nw xY wt          j	        d | j
        D                       S )Nc                 @    g | ]}t          j        |j                  S r.   )r   maybeDeferredstopListening).0ps     r   
<listcomp>z0PortforwardingTests.tearDown.<locals>.<listcomp>   s%    JJJaU 11JJJr    )proxyServerFactoryr4   	transportloseConnectionAttributeErrorr>   rH   rF   r   gatherResultsrI   )r   pis     r   tearDownzPortforwardingTests.tearDownw   s7   	#1;JJLLLL 	 	 	D		(>LBL'')))) 	 	 	D		)88:::: 	 	 	D		)88:::: 	 	 	D	"JJ4>JJJ
 
 	
sA   #& 
33*A" "
A/.A/3B 
BB#C 
CCc                 |   	 t          j                    } fd|_         t          j        d|d          } j                            |           t          d|                                j                   _	        t          j        d j	        d          } j                            |           dg 	t          j                    	 fd}| j        _         fd}| j        _        t          j                    } fd|_         t          j        d|                                j        |           S )	z=
        Test port forwarding through Echo protocol.
        c                       j         S r=   )rF   r?   s   r   r@   z6PortforwardingTests.test_portforward.<locals>.<lambda>   s
    T-@ r    r   	127.0.0.1)	interfacei  c                                          t          |                      t                    k    rC                    d                              dz                                 d            d S d S )Nr       x)extendr	   lenr   joincallback)datadnBytesreceivedr   s    r   testDataReceivedz>PortforwardingTests.test_portforward.<locals>.testDataReceived   sr    OOIdOO,,,8}}&&  (!3!3TF]CCC

4      '&r    c                  L    j         j                            d z             d S )Nr^   )rH   rS   write)re   r   s   r   testConnectionMadez@PortforwardingTests.test_portforward.<locals>.testConnectionMade   s'    )//v>>>>>r    c                       j         S r=   )rH   r?   s   r   r@   z6PortforwardingTests.test_portforward.<locals>.<lambda>   s
    )< r    )r   ServerFactoryr   	listenTCPrI   appendr:   getHostportrR   r   DeferredrH   r   connectionMadeClientFactory
connectTCP)
r   realServerFactoryrealServerPortproxyServerPortrg   rj   clientFactoryrd   re   rf   s
   `      @@@r   test_portforwardz$PortforwardingTests.test_portforward   sx    %244%@%@%@%@" *1.?;WWWn---"6//116#
 #
 "+t&+
 
 
 	o...N	! 	! 	! 	! 	! 	! 	! 	! ,<(	? 	? 	? 	? 	? 	? .@* .00!<!<!<!<;(?(?(A(A(FVVVr    c                 
   t          j        ddd          }t          j        dd                              |          }t          j                    }||_        t          j                    }|	                    |           | 
                    t          |j                  d           |j        d         \  }}}}}	|                     |t          j                   |                    |          }
t          j                    }|
	                    |           |                     |j        |           |                     |j        |           |                     |j                   |                     |j                   dS )zm
        The proxy client registers itself as a producer of the proxy server and
        vice versa.
        TCPr[   r      N)r   IPv4Addressr   rA   r3   r
   MemoryReactorr   r   r   r   r`   
tcpClientsassertIsInstancer2   assertIsproducer
assertTrue	streaming)r   r6   serverr   serverTransporthostrp   rx   timeout_clientclientTransports               r   test_registerProducersz*PortforwardingTests.test_registerProducers   s^    "5+q99)+q99GGMM  -//  (799o... 	W/00!444070B10E-dM7Am[-KLLL ,,T22'799o... 	o.@@@o.@@@1222122222r    N)r*   r+   r,   r-   rJ   rX   ry   r   r.   r    r   rD   rD   m   s[           

 
 
,& & &P!3 !3 !3 !3 !3r    rD   c                       e Zd ZdZd ZdS )StringTransportTestszA
    Test L{proto_helpers.StringTransport} helper behaviour.
    c                 n    t          j                    }|                     t          |j        d           dS )zY
        Test that L{proto_helpers.StringTransport} doesn't accept unicode data.
        fooN)r
   r   assertRaises	TypeErrorri   )r   ss     r   test_noUnicodez#StringTransportTests.test_noUnicode   s2     )++)QWe44444r    N)r*   r+   r,   r-   r   r.   r    r   r   r      s-         5 5 5 5 5r    r   N)r-   twisted.internetr   r   r   r   twisted.protocolsr   r   twisted.python.compatr	   twisted.testr
   twisted.trialr   TestCaser   r2   r0   rA   r:   rD   r   r.   r    r   <module>r      sx    ? > > > > > > > > > > > / / / / / / / / + + + + + + & & & & & & " " " " " "1) 1) 1) 1) 1)! 1) 1) 1)h    !?   "    ;3   2i3 i3 i3 i3 i3(+ i3 i3 i3X
5 
5 
5 
5 
58, 
5 
5 
5 
5 
5r    