o
    Óó·i¦  ã                   @   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   úaC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-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   Z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   Z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   Z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.
        N)r   ÚProxyClientFactoryÚbuildProtocolÚprotoInstance©r   ÚaddrÚprotor   r   r   r.   K   s   z(TestableProxyClientFactory.buildProtocolN©r'   r(   r)   r*   r.   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>N)r   ÚProxyFactoryr.   r,   r7   ZclientProtocolFactoryr/   r0   r   r8   r   r.   `   s
   z"TestableProxyFactory.buildProtocolNr3   r   r   r   r   r4   T   s    r4   c                   @   r   )ÚPortforwardingTestsz
    Test port forwarding.
    c                 C   s   t  ¡ | _t ¡ | _g | _d S r6   )r   r   ÚserverProtocolr   ÚProtocolÚclientProtocolÚ	openPortsr8   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   ZmaybeDeferredÚstopListening)Ú.0Úpr   r   r   Ú
<listcomp>Š   s    z0PortforwardingTests.tearDown.<locals>.<listcomp>)ÚproxyServerFactoryr/   Ú	transportZloseConnectionÚAttributeErrorr7   r?   r=   r   ZgatherResultsr@   )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                      r5   r6   )r=   r   r8   r   r   r9   ’   r:   z6PortforwardingTests.test_portforward.<locals>.<lambda>r   ú	127.0.0.1)Z	interfaceiè  c                    sB   ˆ  t| ƒ¡ tˆƒˆkrˆ d ˆ¡dˆ ¡ ˆ  d ¡ d S 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 )NrL   )r?   rG   Úwriter   )rT   r   r   r   ÚtestConnectionMade©   s   z@PortforwardingTests.test_portforward.<locals>.testConnectionMadec                      r5   r6   )r?   r   r8   r   r   r9   ¯   r:   N)r   ZServerFactoryr   Z	listenTCPr@   Úappendr4   ZgetHostÚportrF   r   ZDeferredr?   r   ZconnectionMadeZClientFactoryZ
connectTCP)r   ZrealServerFactoryZrealServerPortZproxyServerPortrU   rW   ÚclientFactoryr   rR   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.
        ZTCPrK   r   é   N)r   ÚIPv4Addressr   r;   r.   r	   ZMemoryReactorr   r   r   r   rN   Z
tcpClientsZassertIsInstancer-   ZassertIsZproducerÚ
assertTrueZ	streaming)r   r1   Úserverr   ZserverTransportÚhostrY   rZ   ÚtimeoutÚ_ÚclientZclientTransportr   r   r   Útest_registerProducersµ   s    


z*PortforwardingTests.test_registerProducersN)r'   r(   r)   r*   rA   rJ   r[   rd   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.
        ZfooN)r	   r   ÚassertRaisesÚ	TypeErrorrV   )r   Úsr   r   r   Útest_noUnicodeÞ   s   z#StringTransportTests.test_noUnicodeN)r'   r(   r)   r*   ri   r   r   r   r   re   Ù   s    re   N)r*   Ztwisted.internetr   r   r   r   Ztwisted.protocolsr   r   Ztwisted.python.compatr   Ztwisted.testr	   Ztwisted.trialr
   ZTestCaser   r-   r,   r;   r4   r<   re   r   r   r   r   Ú<module>   s   4l