U
    
W[  ã                   @   s®   d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	m
Z
mZ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.
é    )Úunittest)ÚwireÚportforward)Ú	iterbytes)ÚreactorÚdeferÚaddressÚprotocol)Úproto_helpersc                   @   s0   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   s`   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   s    





zWireTests.test_echoc                 C   s.   t  ¡ }t ¡ }| |¡ |  | ¡ d¡ dS )z)
        Test wire.Who protocol.
        s   root
N)r
   r   r   ZWhor   r   r   r   r   r   r   Útest_who#   s    
zWireTests.test_whoc                 C   s.   t  ¡ }t ¡ }| |¡ |  | ¡ d¡ dS )z*
        Test wire.QOTD protocol.
        s'   An apple a day keeps the doctor away.
N)r
   r   r   ZQOTDr   r   r   r   r   r   r   Ú	test_QOTD-   s    

ÿzWireTests.test_QOTDc                 C   s`   t  ¡ }t ¡ }| |¡ | d¡ | d¡ | d¡ | d¡ | d¡ |  | ¡ d¡ dS )z-
        Test wire.Discard protocol.
        r   r   r   r   r   ó    N)r
   r   r   ZDiscardr   r   r   r   r   r   r   r   Útest_discard8   s    





zWireTests.test_discardN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r!   r   r   r   r   r      s
   
r   c                   @   s   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   r(   P   s    z(TestableProxyClientFactory.buildProtocolN©r"   r#   r$   r%   r(   r   r   r   r   r&   H   s   r&   c                   @   s   e Zd ZdZd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                    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                      s   ˆ j S ©N)ÚclientFactoryInstancer   ©r   r   r   Ú<lambda>n   r    z4TestableProxyFactory.buildProtocol.<locals>.<lambda>)r   ÚProxyFactoryr(   r&   r0   ZclientProtocolFactoryr)   r*   r   r1   r   r(   f   s
    z"TestableProxyFactory.buildProtocolNr-   r   r   r   r   r.   Z   s   r.   c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚPortforwardingTestsz
    Test port forwarding.
    c                 C   s   t  ¡ | _t ¡ | _g | _d S r/   )r   r   ÚserverProtocolr	   ZProtocolÚclientProtocolÚ	openPortsr1   r   r   r   ÚsetUpy   s    

zPortforwardingTests.setUpc                 C   s¸   z| j jj ¡  W n tk
r&   Y nX z| j jj}|j ¡  W n tk
rT   Y nX z| jj ¡  W n tk
rz   Y nX z| jj ¡  W n tk
r    Y nX t 	dd„ | j
D ƒ¡S )Nc                 S   s   g | ]}t  |j¡‘qS r   )r   ZmaybeDeferredZstopListening)Ú.0Úpr   r   r   Ú
<listcomp>’   s     z0PortforwardingTests.tearDown.<locals>.<listcomp>)ÚproxyServerFactoryr)   Ú	transportZloseConnectionÚAttributeErrorr0   r6   r5   r   ZgatherResultsr7   )r   Zpir   r   r   ÚtearDown   s(    
ÿ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                      s   ˆ j S r/   )r5   r   r1   r   r   r2   š   r    z6PortforwardingTests.test_portforward.<locals>.<lambda>r   ú	127.0.0.1)Z	interfaceiè  c                    s>   ˆ  t| ƒ¡ tˆƒˆkr:ˆ d ˆ¡dˆ ¡ ˆ  d ¡ d S )Nr    ó   x)Úextendr   Úlenr   ÚjoinÚcallback)Údata©ÚdÚnBytesZreceivedr   r   r   ÚtestDataReceived¨   s    z>PortforwardingTests.test_portforward.<locals>.testDataReceivedc                      s   ˆj j dˆ  ¡ d S )NrA   )r6   r=   Úwriter   )rI   r   r   r   ÚtestConnectionMade°   s    z@PortforwardingTests.test_portforward.<locals>.testConnectionMadec                      s   ˆ j S r/   )r6   r   r1   r   r   r2   ¶   r    )r	   ZServerFactoryr   Z	listenTCPr7   Úappendr.   ZgetHostÚportr<   r   ZDeferredr6   r   ZconnectionMadeZClientFactoryZ
connectTCP)r   ZrealServerFactoryZrealServerPortZproxyServerPortrJ   rL   ÚclientFactoryr   rG   r   Útest_portforward•   s8    ÿÿ
ÿ  ÿ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.
        ZTCPr@   r   é   N)r   ZIPv4Addressr   r3   r(   r
   ZMemoryReactorr   r   r   r   rC   Z
tcpClientsZassertIsInstancer'   ZassertIsZproducerZ
assertTrueZ	streaming)r   r+   Zserverr   ZserverTransportZhostrN   rO   ZtimeoutÚ_ZclientZclientTransportr   r   r   Útest_registerProducers¾   s     


z*PortforwardingTests.test_registerProducersN)r"   r#   r$   r%   r8   r?   rP   rS   r   r   r   r   r4   t   s
   )r4   c                   @   s   e Zd ZdZdd„ ZdS )Ú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.
        ZfooN)r
   r   ZassertRaisesÚ	TypeErrorrK   )r   Úsr   r   r   Útest_noUnicodeè   s    z#StringTransportTests.test_noUnicodeN)r"   r#   r$   r%   rW   r   r   r   r   rT   ã   s   rT   N)r%   Ztwisted.trialr   Ztwisted.protocolsr   r   Ztwisted.python.compatr   Ztwisted.internetr   r   r   r	   Ztwisted.testr
   ZTestCaser   r'   r&   r3   r.   r4   rT   r   r   r   r   Ú<module>   s   9o