
    Vh                     &   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y)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y)	WireTestsz
    Test wire protocols.
    c                 `   t        j                         }t        j                         }|j	                  |       |j                  d       |j                  d       |j                  d       |j                  d       |j                  d       | j                  |j                         d       y)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      K/home/dcms/DCMS/lib/python3.12/site-packages/twisted/test/test_protocols.py	test_echozWireTests.test_echo   s~     ))+IIK		x 	x 	v	v	v$:;    c                     t        j                         }t        j                         }|j	                  |       | j                  |j                         d       y)z)
        Test wire.Who protocol.
        s   root
N)r
   r   r   Whor   r   r   r   s      r   test_whozWireTests.test_who"   sA     ))+HHJ	K0r    c                     t        j                         }t        j                         }|j	                  |       | j                  |j                         d       y)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+   sB     ))+IIK	$PQr    c                 `   t        j                         }t        j                         }|j	                  |       |j                  d       |j                  d       |j                  d       |j                  d       |j                  d       | j                  |j                         d       y)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}     ))+LLN		x 	x 	v	v	vC(r    N)__name__
__module____qualname____doc__r   r#   r&   r)    r    r   r   r      s    <1R)r    r   c                       e Zd ZdZd Zy)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                  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"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y)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                  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.H r    )r   ProxyFactoryr3   r0   r>   clientProtocolFactoryr4   r5   s   `  r   r3   z"TestableProxyFactory.buildProtocol`   s=    
 ((66tTB%?%A"&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y)PortforwardingTestsz
    Test port forwarding.
    c                 v    t        j                         | _        t        j                         | _        g | _        y r=   )r   r   serverProtocolr   ProtocolclientProtocol	openPortsr?   s    r   setUpzPortforwardingTests.setUpr   s(    "iik&//1r    c                    	 | j                   j                  j                  j                          	 | j                   j
                  j                  }|j                  j                          	 | j                  j                  j                          	 | j                  j                  j                          t        j                  | j                  D cg c]!  }t        j                  |j                        # c}      S # t        $ r Y w xY w# t        $ r Y w xY w# t        $ r Y w xY w# t        $ r Y w xY wc c}w r=   )proxyServerFactoryr4   	transportloseConnectionAttributeErrorr>   rH   rF   r   gatherResultsrI   maybeDeferredstopListening)r   pips      r   tearDownzPortforwardingTests.tearDownw   s   	##11;;JJL	((>>LLBLL'')	))88:	))88: "";?>>JaU  1J
 	
  		
  		  		  		 KsL   .C? :D ,$D $D, &D;?	D
D	DD	D)(D),	D87D8c                    	 t        j                         } fd|_         t        j                  d|d      } j                  j                  |       t        d|j                         j                         _	        t        j                  d j                  d      } j                  j                  |       dg 	t        j                         	 fd}| j                  _         fd}| j                  _        t        j                         } fd|_         t        j                   d|j                         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                     j                  t        |              t              k\  r6j                  dj	                        dz         j                  d        y y )Nr       x)extendr	   lenr   joincallback)datadnBytesreceivedr   s    r   testDataReceivedz>PortforwardingTests.test_portforward.<locals>.testDataReceived   sM    OOIdO,8}&  (!3TF]C

4  'r    c                  V    j                   j                  j                  d z         y )Nr[   )rH   rM   write)rb   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portrL   r   DeferredrH   r   connectionMadeClientFactory
connectTCP)
r   realServerFactoryrealServerPortproxyServerPortrd   rg   clientFactoryra   rb   rc   s
   `      @@@r   test_portforwardz$PortforwardingTests.test_portforward   s    %224%@" **1.?;Wn-"6//166#
 "++t&&+
 	o.NN	! ,<(	? .@* ..0!<;(?(?(A(F(FVr    c                    t        j                  ddd      }t        j                  dd      j	                  |      }t        j                         }||_        t        j                         }|j                  |       | j                  t        |j                        d       |j                  d   \  }}}}}	| j                  |t        j                         |j	                  |      }
t        j                         }|
j                  |       | j                  |j                   |       | j                  |j                   |       | j#                  |j$                         | j#                  |j$                         y)zm
        The proxy client registers itself as a producer of the proxy server and
        vice versa.
        TCPrX   r      N)r   IPv4Addressr   rA   r3   r
   MemoryReactorr   r   r   r   r]   
tcpClientsassertIsInstancer2   assertIsproducer
assertTrue	streaming)r   r6   serverr   serverTransporthostrm   ru   timeout_clientclientTransports               r   test_registerProducersz*PortforwardingTests.test_registerProducers   s4    ""5+q9))+q9GGM  --/  (779o. 	W//0!4070B0B10E-dM7Am[-K-KL ,,T2'779o. 	o..@o..@112112r    N)r*   r+   r,   r-   rJ   rU   rv   r   r.   r    r   rD   rD   m   s    

,&P!3r    rD   c                       e Zd ZdZd Zy)StringTransportTestszA
    Test L{proto_helpers.StringTransport} helper behaviour.
    c                 n    t        j                         }| j                  t        |j                  d       y)zY
        Test that L{proto_helpers.StringTransport} doesn't accept unicode data.
        fooN)r
   r   assertRaises	TypeErrorrf   )r   ss     r   test_noUnicodez#StringTransportTests.test_noUnicode   s(     ))+)QWWe4r    N)r*   r+   r,   r-   r   r.   r    r   r   r      s    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      s|    ? > / + & "1)!! 1)h!?!? ";33 2i3(++ i3X
58,, 
5r    