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