o
    ¯b¦  ã                   @   s®   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„ dejƒZG d	d
„ d
ejƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZdS )z+
Test cases for twisted.protocols package.
é    )ÚaddressÚdeferÚprotocolÚreactor)ÚportforwardÚwire)Ú	iterbytes)Úproto_helpers)Úunittestc                   @   ó0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )Ú	WireTestsz
    Test wire protocols.
    c                 C   ó`   t  ¡ }t ¡ }| |¡ | 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Úa© r   ú=/usr/lib/python3/dist-packages/twisted/test/test_protocols.pyÚ	test_echo   ó   





zWireTests.test_echoc                 C   ó.   t  ¡ }t ¡ }| |¡ |  | ¡ d¡ dS )z)
        Test wire.Who protocol.
        s   root
N)r	   r   r   ÚWhor   r   r   r   r   r   r   Útest_who"   ó   
zWireTests.test_whoc                 C   r!   )z*
        Test wire.QOTD protocol.
        s'   An apple a day keeps the doctor away.
N)r	   r   r   ÚQOTDr   r   r   r   r   r   r   Ú	test_QOTD+   r$   zWireTests.test_QOTDc                 C   r   )z-
        Test wire.Discard protocol.
        r   r   r   r   r   ó    N)r	   r   r   ÚDiscardr   r   r   r   r   r   r   r   Útest_discard4   r    zWireTests.test_discardN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r#   r&   r)   r   r   r   r   r      s    		r   c                   @   ó   e Zd ZdZd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                 C   s   t j | |¡}|| _|S )zE
        Create the protocol instance and keeps track of it.
        )r   ÚProxyClientFactoryÚbuildProtocolÚprotoInstance©r   ÚaddrÚprotor   r   r   r1   K   s   z(TestableProxyClientFactory.buildProtocolN©r*   r+   r,   r-   r1   r   r   r   r   r/   C   s    r/   c                   @   r.   )Ú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                    s.   t j ˆ |¡}tƒ ˆ _‡ fdd„|_|ˆ _|S )z‡
        Create the protocol instance, keeps track of it, and makes it use
        C{clientFactoryInstance} as client factory.
        c                      ó   ˆ j S ©N)ÚclientFactoryInstancer   ©r   r   r   Ú<lambda>h   ó    z4TestableProxyFactory.buildProtocol.<locals>.<lambda>)r   ÚProxyFactoryr1   r/   r:   ÚclientProtocolFactoryr2   r3   r   r;   r   r1   `   s
   z"TestableProxyFactory.buildProtocolNr6   r   r   r   r   r7   T   s    r7   c                   @   r   )ÚPortforwardingTestsz
    Test port forwarding.
    c                 C   s   t  ¡ | _t ¡ | _g | _d S r9   )r   r   ÚserverProtocolr   ÚProtocolÚclientProtocolÚ	openPortsr;   r   r   r   ÚsetUpr   s   


zPortforwardingTests.setUpc                 C   s°   z	| j jj ¡  W n	 ty   Y nw z| j jj}|j ¡  W n	 ty(   Y nw z| jj ¡  W n	 ty:   Y nw z| jj ¡  W n	 tyL   Y nw t 	dd„ | j
D ƒ¡S )Nc                 S   s   g | ]}t  |j¡‘qS r   )r   ÚmaybeDeferredÚstopListening)Ú.0Úpr   r   r   Ú
<listcomp>Š   s    z0PortforwardingTests.tearDown.<locals>.<listcomp>)ÚproxyServerFactoryr2   Ú	transportÚloseConnectionÚAttributeErrorr:   rC   rA   r   ÚgatherResultsrD   )r   Úpir   r   r   ÚtearDownw   s0   ÿ
ÿÿÿÿzPortforwardingTests.tearDownc                    sÐ   t  ¡ }‡fdd„|_ tjd|dd�}ˆj |¡ td| ¡ jƒˆ_	tjdˆj	dd�}ˆj |¡ d‰g ‰t
 ¡ ‰ ‡ ‡‡‡fdd„}|ˆj_‡‡fd	d
„}|ˆj_t  ¡ }‡fdd„|_ t d| ¡ j|¡ ˆ S )z=
        Test port forwarding through Echo protocol.
        c                      r8   r9   )rA   r   r;   r   r   r<   ’   r=   z6PortforwardingTests.test_portforward.<locals>.<lambda>r   ú	127.0.0.1)Ú	interfaceiè  c                    sB   ˆ  t| ƒ¡ tˆƒˆkrˆ d ˆ¡dˆ ¡ ˆ  d ¡ d S d S )Nr'   ó   x)Úextendr   Úlenr   ÚjoinÚcallback)Údata©ÚdÚnBytesÚreceivedr   r   r   ÚtestDataReceived¡   s
   þz>PortforwardingTests.test_portforward.<locals>.testDataReceivedc                      s   ˆj j dˆ  ¡ d S )NrT   )rC   rL   Úwriter   )r\   r   r   r   ÚtestConnectionMade©   s   z@PortforwardingTests.test_portforward.<locals>.testConnectionMadec                      r8   r9   )rC   r   r;   r   r   r<   ¯   r=   )r   ÚServerFactoryr   Ú	listenTCPrD   Úappendr7   ÚgetHostÚportrK   r   ÚDeferredrC   r   ÚconnectionMadeÚClientFactoryÚ
connectTCP)r   ÚrealServerFactoryÚrealServerPortÚproxyServerPortr^   r`   ÚclientFactoryr   rZ   r   Útest_portforward�   s,   
ÿÿz$PortforwardingTests.test_portforwardc                 C   sÈ   t  ddd¡}t dd¡ |¡}t ¡ }||_t ¡ }| 	|¡ |  
t|jƒd¡ |jd \}}}}}	|  |tj¡ | |¡}
t ¡ }|
 	|¡ |  |j|¡ |  |j|¡ |  |j¡ |  |j¡ dS )zm
        The proxy client registers itself as a producer of the proxy server and
        vice versa.
        ÚTCPrR   r   é   N)r   ÚIPv4Addressr   r>   r1   r	   ÚMemoryReactorr   r   r   r   rV   Ú
tcpClientsÚassertIsInstancer0   ÚassertIsÚproducerÚ
assertTrueÚ	streaming)r   r4   Úserverr   ÚserverTransportÚhostre   rm   ÚtimeoutÚ_ÚclientÚclientTransportr   r   r   Útest_registerProducersµ   s    


z*PortforwardingTests.test_registerProducersN)r*   r+   r,   r-   rE   rQ   rn   r€   r   r   r   r   r@   m   s    (r@   c                   @   r.   )ÚStringTransportTestszA
    Test L{proto_helpers.StringTransport} helper behaviour.
    c                 C   s   t  ¡ }|  t|jd¡ dS )zY
        Test that L{proto_helpers.StringTransport} doesn't accept unicode data.
        ÚfooN)r	   r   ÚassertRaisesÚ	TypeErrorr_   )r   Úsr   r   r   Útest_noUnicodeÞ   s   z#StringTransportTests.test_noUnicodeN)r*   r+   r,   r-   r†   r   r   r   r   r�   Ù   s    r�   N)r-   Útwisted.internetr   r   r   r   Útwisted.protocolsr   r   Útwisted.python.compatr   Útwisted.testr	   Útwisted.trialr
   ÚTestCaser   r0   r/   r>   r7   r@   r�   r   r   r   r   Ú<module>   s   4l