
    YfVH                     6   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
mZmZmZ ddlmZmZmZmZmZmZ ddlmZmZ dd	lmZmZ dd
lmZ ddlmZ  G d de          Z G d de          Z  G d de          Z! G d de          Z" G d de          Z#dS )z)
Tests for L{twisted.internet.protocol}.
    )BytesIO)implementer)verifyObject)CancelledError)	IConsumerILoggingContext	IProtocolIProtocolFactory)ClientCreatorConsumerToProtocolAdapterFactoryFileWrapperProtocolProtocolToConsumerAdapter)MemoryReactorClockStringTransport)LogLevelglobalLogPublisher)Failure)TestCasec                   r    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd ZdS )ClientCreatorTestsz?
    Tests for L{twisted.internet.protocol.ClientCreator}.
    c                      G d dt                     }t                      }t          ||          } |||          }|                    d          }|                     ||           dS )a  
        Helper for implementing a test to verify that one of the I{connect}
        methods of L{ClientCreator} passes the right arguments to the right
        reactor method.

        @param check: A function which will be invoked with a reactor and a
            L{ClientCreator} instance and which should call one of the
            L{ClientCreator}'s I{connect} methods and assert that all of its
            arguments except for the factory are passed on as expected to the
            reactor.  The factory should be returned.
        c                       e Zd ZdS ):ClientCreatorTests._basicConnectTest.<locals>.SomeProtocolN__name__
__module____qualname__     U/var/www/html/env/lib/python3.11/site-packages/twisted/internet/test/test_protocol.pySomeProtocolr   5           Dr!   r#   N)r   r   r   buildProtocolassertIsInstance)selfcheckr#   reactorccfactoryprotocols          r"   _basicConnectTestz$ClientCreatorTests._basicConnectTest(   s    	 	 	 	 	8 	 	 	 %&&7L11%$$((..h55555r!   c                 <      fd}                      |           dS )z
        L{ClientCreator.connectTCP} calls C{reactor.connectTCP} with the host
        and port information passed to it, and with a factory which will
        construct the protocol passed to L{ClientCreator.__init__}.
        c                 &   |                     dddd           | j                                        \  }}}}}                    |d                               |d                               |d                               |d           |S )Nexample.com    )z1.2.3.4i&  )
connectTCP
tcpClientspopassertEqual)r)   r*   hostportr+   timeoutbindAddressr'   s          r"   r(   z1ClientCreatorTests.test_connectTCP.<locals>.checkE   s    MM-t5FGGG8?8J8N8N8P8P5D$+T=111T4(((Wd+++[*;<<<Nr!   Nr-   r'   r(   s   ` r"   test_connectTCPz"ClientCreatorTests.test_connectTCP>   s8    	 	 	 	 	 	u%%%%%r!   c                 <      fd}                      |           dS )z
        L{ClientCreator.connectUNIX} calls C{reactor.connectUNIX} with the
        filename passed to it, and with a factory which will construct the
        protocol passed to L{ClientCreator.__init__}.
        c                     |                     ddd           | j                                        \  }}}}                    |d                               |d                               |           |S )N/foo/bar{   T)connectUNIXunixClientsr5   r6   
assertTrue)r)   r*   addressr+   r9   checkPIDr'   s         r"   r(   z2ClientCreatorTests.test_connectUNIX.<locals>.checkW   sy    NN:sD111292E2I2I2K2K/GWgxWj111Wc***OOH%%%Nr!   Nr;   r<   s   ` r"   test_connectUNIXz#ClientCreatorTests.test_connectUNIXP   s8    	 	 	 	 	 	u%%%%%r!   c                 <      fd}                      |           dS )z
        L{ClientCreator.connectSSL} calls C{reactor.connectSSL} with the host,
        port, and context factory passed to it, and with a factory which will
        construct the protocol passed to L{ClientCreator.__init__}.
        c                 r   t                      }|                    dd|dd           | j                                        \  }}}}}}	                    |d           	                    |d           	                    ||           	                    |d           	                    |d           |S )Nr0   r1   r2   )z4.3.2.1i.  )object
connectSSL
sslClientsr5   r6   assertIs)
r)   r*   expectedContextFactoryr7   r8   r+   contextFactoryr9   r:   r'   s
            r"   r(   z1ClientCreatorTests.test_connectSSL.<locals>.checkh   s    %+XX"MMt%;TCT   "&&((T=111T4(((MM.*@AAAWd+++[*;<<<Nr!   Nr;   r<   s   ` r"   test_connectSSLz"ClientCreatorTests.test_connectSSLa   s8    	 	 	 	 	( 	u%%%%%r!   c                 V   t                      }t          |t                    } ||          }|j                                        }|                     |j                   |                                 |                     |j                   | 	                    |t                    S )a  
        Helper for implementing a test to verify that cancellation of the
        L{Deferred} returned by one of L{ClientCreator}'s I{connect} methods is
        implemented to cancel the underlying connector.

        @param connect: A function which will be invoked with a L{ClientCreator}
            instance as an argument and which should call one its I{connect}
            methods and return the result.

        @return: A L{Deferred} which fires when the test is complete or fails if
            there is a problem.
        )r   r   r   
connectorsr5   assertFalse_disconnectedcancelrD   assertFailurer   r'   connectr)   r*   d	connectors         r"   _cancelConnectTestz%ClientCreatorTests._cancelConnectTest~   s     %&&7H--GBKK&**,,	0111	


	/000!!!^444r!   c                 2    d }|                      |          S )
        The L{Deferred} returned by L{ClientCreator.connectTCP} can be cancelled
        to abort the connection attempt before it completes.
        c                 .    |                      dd          S Nr0   r1   )r3   r*   s    r"   rX   z9ClientCreatorTests.test_cancelConnectTCP.<locals>.connect   s    ==555r!   r[   r'   rX   s     r"   test_cancelConnectTCPz(ClientCreatorTests.test_cancelConnectTCP   s'    	6 	6 	6 &&w///r!   c                 2    d }|                      |          S )r]   c                 ,    |                      d          S Nr@   )rB   r`   s    r"   rX   z:ClientCreatorTests.test_cancelConnectUNIX.<locals>.connect   s    >>*---r!   ra   rb   s     r"   test_cancelConnectUNIXz)ClientCreatorTests.test_cancelConnectUNIX   s'    	. 	. 	. &&w///r!   c                 2    d }|                      |          S )r]   c                 H    |                      ddt                                S r_   )rK   rJ   r`   s    r"   rX   z9ClientCreatorTests.test_cancelConnectSSL.<locals>.connect   s    ==fhh???r!   ra   rb   s     r"   test_cancelConnectSSLz(ClientCreatorTests.test_cancelConnectSSL   s*    	@ 	@ 	@ &&w///r!   c                    t                      }t          |t                    } |||          }|j                                        }|                     t          |                                          d           |                                 |                     |                                g            | 	                    |j
                   |                     |t                    S )z
        Like L{_cancelConnectTest}, but for the case where the L{Deferred} is
        cancelled after the connection is set up but before it is fired with the
        resulting protocol instance.
           )r   r   r   rR   r5   r6   lengetDelayedCallsrU   rD   rT   rV   r   rW   s         r"   _cancelConnectTimeoutTestz,ClientCreatorTests._cancelConnectTimeoutTest   s     %&&7H--GGR  &**,,	 	W446677;;; 	



0022B777
 		/000!!!^444r!   c                 2    d }|                      |          S )a  
        L{ClientCreator.connectTCP} inserts a very short delayed call between
        the time the connection is established and the time the L{Deferred}
        returned from one of its connect methods actually fires.  If the
        L{Deferred} is cancelled in this interval, the established connection is
        closed, the timeout is cancelled, and the L{Deferred} fails with
        L{CancelledError}.
        c                     |                     dd          }| j                                        \  }}}}}|                    d           }t	                      }	|                    |	           |S r_   )r3   r4   r5   r%   r   makeConnection)
r)   r*   rY   r7   r8   r+   r9   r:   r,   	transports
             r"   rX   z@ClientCreatorTests.test_cancelConnectTCPTimeout.<locals>.connect   sk    mT22A8?8J8N8N8P8P5D$+,,T22H'))I##I...Hr!   ro   rb   s     r"   test_cancelConnectTCPTimeoutz/ClientCreatorTests.test_cancelConnectTCPTimeout   '    	 	 	 --g666r!   c                 2    d }|                      |          S )a  
        L{ClientCreator.connectUNIX} inserts a very short delayed call between
        the time the connection is established and the time the L{Deferred}
        returned from one of its connect methods actually fires.  If the
        L{Deferred} is cancelled in this interval, the established connection is
        closed, the timeout is cancelled, and the L{Deferred} fails with
        L{CancelledError}.
        c                     |                     d          }| j                                        \  }}}}|                    d           }t	                      }|                    |           |S rf   )rB   rC   r5   r%   r   rr   )	r)   r*   rY   rE   r+   r9   r:   r,   rs   s	            r"   rX   zAClientCreatorTests.test_cancelConnectUNIXTimeout.<locals>.connect   sg    z**A5<5H5L5L5N5N2GWg{,,T22H'))I##I...Hr!   rt   rb   s     r"   test_cancelConnectUNIXTimeoutz0ClientCreatorTests.test_cancelConnectUNIXTimeout   rv   r!   c                 2    d }|                      |          S )a  
        L{ClientCreator.connectSSL} inserts a very short delayed call between
        the time the connection is established and the time the L{Deferred}
        returned from one of its connect methods actually fires.  If the
        L{Deferred} is cancelled in this interval, the established connection is
        closed, the timeout is cancelled, and the L{Deferred} fails with
        L{CancelledError}.
        c                     |                     ddt                                }| j                                        \  }}}}}}|                    d           }	t                      }
|	                    |
           |S r_   )rK   rJ   rL   r5   r%   r   rr   )r)   r*   rY   r7   r8   r+   rO   r9   bindADdressr,   rs   s              r"   rX   z@ClientCreatorTests.test_cancelConnectSSLTimeout.<locals>.connect  s}    mT688<<A "&&((,,T22H'))I##I...Hr!   rt   rb   s     r"   test_cancelConnectSSLTimeoutz/ClientCreatorTests.test_cancelConnectSSLTimeout   s'    	 	 	 --g666r!   c                    t                      }t          |t                    } |||          \  }}|j                                        }|                    |t          t          d                               |                     t          |
                                          d           |                                 |                     |
                                g            |                     |t                    S )z
        Like L{_cancelConnectTest}, but for the case where the L{Deferred} is
        cancelled after the connection attempt has failed but before it is fired
        with the resulting failure.
        zSimulated failurerl   )r   r   r   rR   r5   clientConnectionFailedr   	Exceptionr6   rm   rn   rU   rV   r   )r'   rX   r)   r*   rY   r+   rZ   s          r"   _cancelConnectFailedTimeoutTestz2ClientCreatorTests._cancelConnectFailedTimeoutTest  s     %&&7H--WWb))
7&**,,	&&wy)<==>>	
 	
 	
 	W446677;;; 	



0022B777!!!^444r!   c                 2    d }|                      |          S )zz
        Similar to L{test_cancelConnectTCPTimeout}, but for the case where the
        connection attempt fails.
        c                 t    |                     dd          }| j                                        \  }}}}}||fS r_   )r3   r4   r5   )r)   r*   rY   r7   r8   r+   r9   r:   s           r"   rX   zFClientCreatorTests.test_cancelConnectTCPFailedTimeout.<locals>.connect2  s>    mT22A8?8J8N8N8P8P5D$+g:r!   r   rb   s     r"   "test_cancelConnectTCPFailedTimeoutz5ClientCreatorTests.test_cancelConnectTCPFailedTimeout,  '    	 	 	
 33G<<<r!   c                 2    d }|                      |          S )z{
        Similar to L{test_cancelConnectUNIXTimeout}, but for the case where the
        connection attempt fails.
        c                 p    |                     d          }| j                                        \  }}}}||fS rf   )rB   rC   r5   )r)   r*   rY   rE   r+   r9   r:   s          r"   rX   zGClientCreatorTests.test_cancelConnectUNIXFailedTimeout.<locals>.connect?  s:    z**A5<5H5L5L5N5N2GWg{g:r!   r   rb   s     r"   #test_cancelConnectUNIXFailedTimeoutz6ClientCreatorTests.test_cancelConnectUNIXFailedTimeout9  r   r!   c                 2    d }|                      |          S )zz
        Similar to L{test_cancelConnectSSLTimeout}, but for the case where the
        connection attempt fails.
        c                     |                     ddt                                }| j                                        \  }}}}}}||fS r_   )rK   rJ   rL   r5   )	r)   r*   rY   r7   r8   r+   rO   r9   r|   s	            r"   rX   zFClientCreatorTests.test_cancelConnectSSLFailedTimeout.<locals>.connectL  sP    mT688<<A "&&((g:r!   r   rb   s     r"   "test_cancelConnectSSLFailedTimeoutz5ClientCreatorTests.test_cancelConnectSSLFailedTimeoutF  s'    
	 
	 
	 33G<<<r!   N)r   r   r   __doc__r-   r=   rG   rP   r[   rc   rg   rj   ro   ru   ry   r}   r   r   r   r   r    r!   r"   r   r   #   s        6 6 6,& & &$& & &"& & &:5 5 5,	0 	0 	0	0 	0 	0	0 	0 	05 5 567 7 7(7 7 7(7 7 765 5 52= = == = == = = = =r!   r   c                   $    e Zd ZdZd Zd Zd ZdS )ProtocolTestsz:
    Tests for L{twisted.internet.protocol.Protocol}.
    c                     t                      }|                     t          t          |                     |                     t          t          |                     dS )zT
        L{Protocol} instances provide L{IProtocol} and L{ILoggingContext}.
        N)r   rD   r   r	   r   )r'   protos     r"   test_interfaceszProtocolTests.test_interfaces`  sK     

Y66777_e<<=====r!   c                      G d dt                     }|                     d |                                                       dS )zJ
        L{Protocol.logPrefix} returns the protocol class's name.
        c                       e Zd ZdS )/ProtocolTests.test_logPrefix.<locals>.SomeThingNr   r    r!   r"   	SomeThingr   m  r$   r!   r   N)r   r6   	logPrefix)r'   r   s     r"   test_logPrefixzProtocolTests.test_logPrefixh  s\    
	 	 	 	 	 	 	 	 	iikk&;&;&=&=>>>>>r!   c                     g  G fddt                     }t                      } |            }|                    |           |                     |g           dS )zz
        L{Protocol.makeConnection} sets the given transport on itself, and
        then calls C{connectionMade}.
        c                       e Zd Z fdZdS )7ProtocolTests.test_makeConnection.<locals>.SomeProtocolc                 <                         | j                   d S N)appendrs   )r'   results    r"   connectionMadezFProtocolTests.test_makeConnection.<locals>.SomeProtocol.connectionMadez  s    dn-----r!   N)r   r   r   r   r   s   r"   r#   r   y  s.        . . . . . . .r!   r#   N)r   rJ   rr   r6   )r'   r#   rs   r,   r   s       @r"   test_makeConnectionz!ProtocolTests.test_makeConnectionr  s    
 	. 	. 	. 	. 	. 	. 	.8 	. 	. 	. HH	<>>	***)-----r!   N)r   r   r   r   r   r   r   r    r!   r"   r   r   [  sK         > > >? ? ?. . . . .r!   r   c                   6    e Zd ZdZd Zd Zd Zd Zd Zd Z	dS )	FactoryTestsz(
    Tests for L{protocol.Factory}.
    c                     t                      }|                     t          t          |                     |                     t          t          |                     dS )zg
        L{Factory} instances provide both L{IProtocolFactory} and
        L{ILoggingContext}.
        N)r   rD   r   r
   r   )r'   r+   s     r"   r   zFactoryTests.test_interfaces  sL    
 ))%5w??@@@_g>>?????r!   c                      G d dt                     }|                     d |                                                       dS )zM
        L{Factory.logPrefix} returns the name of the factory class.
        c                       e Zd ZdS )6FactoryTests.test_logPrefix.<locals>.SomeKindOfFactoryNr   r    r!   r"   SomeKindOfFactoryr     r$   r!   r   N)r   r6   r   )r'   r   s     r"   r   zFactoryTests.test_logPrefix  sa    
	 	 	 	 	 	 	 	 	,.?.?.A.A.K.K.M.MNNNNNr!   c                      G d dt                     }t                      }||_        |                    d          }|                     ||           |                     |j        |           dS )z
        L{Factory.buildProtocol} by default constructs a protocol by calling
        its C{protocol} attribute, and attaches the factory to the result.
        c                       e Zd ZdS )<FactoryTests.test_defaultBuildProtocol.<locals>.SomeProtocolNr   r    r!   r"   r#   r     r$   r!   r#   N)r   r   r,   r%   r&   rM   r+   )r'   r#   fr,   s       r"   test_defaultBuildProtocolz&FactoryTests.test_defaultBuildProtocol  s    	 	 	 	 	8 	 	 	 II!
??4((h555h&*****r!   c                     G d dt                     }|                    t          ddd          }|                     |j        t                     |                     |j        d           |                     |j        ddi           d	S )
z
        L{Factory.forProtocol} constructs a Factory, passing along any
        additional arguments, and sets its C{protocol} attribute to the given
        Protocol subclass.
        c                       e Zd Zd ZdS )7FactoryTests.test_forProtocol.<locals>.ArgTakingFactoryc                 $    ||c| _         | _        d S r   )argskwargs)r'   r   r   s      r"   __init__z@FactoryTests.test_forProtocol.<locals>.ArgTakingFactory.__init__  s    )-v&	4;;;r!   N)r   r   r   r   r    r!   r"   ArgTakingFactoryr     s#        6 6 6 6 6r!   r   rl         )foo)rl   r   r   N)r   forProtocolr   r6   r,   r   r   )r'   r   r+   s      r"   test_forProtocolzFactoryTests.test_forProtocol  s    	6 	6 	6 	6 	6w 	6 	6 	6 #..xA2.FF)8444v...%55555r!   c                    g t          j        j                   |                     fd           t	                      }|                                 |                     d         d         |           |                     d         d         t          j	                   |                     d         d         d           dS )z
        L{Factory.doStart} logs that it is starting a factory, followed by
        the L{repr} of the L{Factory} instance that is being started.
        c                  6    t          j         j                  S r   r   removeObserverr   eventss   r"   <lambda>z;FactoryTests.test_doStartLoggingStatement.<locals>.<lambda>       2 A&- P P r!   r   r+   	log_level
log_formatzStarting factory {factory!r}N)
r   addObserverr   
addCleanupr   doStartrM   r6   r   info)r'   r   r   s     @r"   test_doStartLoggingStatementz)FactoryTests.test_doStartLoggingStatement  s    
 &v}555PPPPQQQII			fQi	*A...;/???<02PQQQQQr!   c                    g t          j        j                   |                     fd            G d dt                    } |            }|                                 |                     d         d         |           |                     d         d         t          j	                   |                     d         d         d           d	S )
z
        L{Factory.doStop} logs that it is stopping a factory, followed by
        the L{repr} of the L{Factory} instance that is being stopped.
        c                  6    t          j         j                  S r   r   r   s   r"   r   z:FactoryTests.test_doStopLoggingStatement.<locals>.<lambda>  r   r!   c                       e Zd ZdZdS );FactoryTests.test_doStopLoggingStatement.<locals>.MyFactoryrl   N)r   r   r   numPortsr    r!   r"   	MyFactoryr     s        HHHr!   r   r   r+   r   r   zStopping factory {factory!r}N)
r   r   r   r   r   doStoprM   r6   r   r   )r'   r   r   r   s      @r"   test_doStopLoggingStatementz(FactoryTests.test_doStopLoggingStatement  s    
 &v}555PPPPQQQ	 	 	 	 	 	 	 	 IKK	


fQi	*A...;/???<02PQQQQQr!   N)
r   r   r   r   r   r   r   r   r   r   r    r!   r"   r   r     s         @ @ @O O O+ + +6 6 6 R R R R R R R Rr!   r   c                       e Zd ZdZd Zd ZdS )AdapterTestszR
    Tests for L{ProtocolToConsumerAdapter} and L{ConsumerToProtocolAdapter}.
    c                     g }t                      }|j        |_        t          |          }|                    d           |                     |dg           |                     |t                     dS )z}
        L{IProtocol} providers can be adapted to L{IConsumer} providers using
        L{ProtocolToConsumerAdapter}.
           helloN)r   r   dataReceivedr   writer6   r&   r   )r'   r   pconsumers       r"   test_protocolToConsumerz$AdapterTests.test_protocolToConsumer  sm    
 JJQ<<x   (,,,h(ABBBBBr!   c                    g t          t                     G fdd                      } |            }t          |          }|                    d           |                     dg           |                     |t                     dS )z}
        L{IConsumer} providers can be adapted to L{IProtocol} providers using
        L{ProtocolToConsumerAdapter}.
        c                       e Zd Z fdZdS )6AdapterTests.test_consumerToProtocol.<locals>.Consumerc                 2                         |           d S r   )r   )r'   rY   r   s     r"   r   z<AdapterTests.test_consumerToProtocol.<locals>.Consumer.write  s    a     r!   N)r   r   r   r   r   s   r"   Consumerr     s.        ! ! ! ! ! ! !r!   r   r   N)r   r   r	   r   r6   r&   r   )r'   r   cr,   r   s       @r"   test_consumerToProtocolz$AdapterTests.test_consumerToProtocol  s    
 	Y			! 	! 	! 	! 	! 	! 	! 	! 	! 
 		! HJJQ<<h'''(,,,h(ABBBBBr!   N)r   r   r   r   r   r   r    r!   r"   r   r     sD         C C CC C C C Cr!   r   c                       e Zd ZdZd Zd ZdS )FileWrapperTestsz2
    L{twisted.internet.protocol.FileWrapper}
    c                 z   t          t                                }|                    d           |                     |j                                        d           t          t                                }|                    d           |                     |j                                        d           dS )z@
        L{twisted.internet.protocol.FileWrapper.write}
           test1stuffN)r   r   r   r6   filegetvalueassertNotEqualr'   wrappers     r"   
test_writezFileWrapperTests.test_write  s     gii((h..00(;;;gii(( 	gGL1133W=====r!   c                 @   t          t                                }|                    ddg           |                     |j                                        d           t          t                                }|                     t          |j        ddg           dS )zH
        L{twisted.internet.protocol.FileWrapper.writeSequence}
        r   s   test2s
   test1test2test3test4N)r   r   writeSequencer6   r   r   assertRaises	TypeErrorr   s     r"   test_writeSequencez#FileWrapperTests.test_writeSequence  s     gii((x2333..00-@@@gii(()W%:Wg<NOOOOOr!   N)r   r   r   r   r   r   r    r!   r"   r   r     sA         > > >
P 
P 
P 
P 
Pr!   r   N)$r   ior   zope.interfacer   zope.interface.verifyr   twisted.internet.deferr   twisted.internet.interfacesr   r   r	   r
   twisted.internet.protocolr   r   r   r   r   r   twisted.internet.testingr   r   twisted.loggerr   r   twisted.python.failurer   twisted.trial.unittestr   r   r   r   r   r   r    r!   r"   <module>r      sF   
       & & & & & & . . . . . . 1 1 1 1 1 1                           I H H H H H H H 7 7 7 7 7 7 7 7 * * * * * * + + + + + +u= u= u= u= u= u= u= u=p	%. %. %. %. %.H %. %. %.PXR XR XR XR XR8 XR XR XRv"C "C "C "C "C8 "C "C "CJP P P P Px P P P P Pr!   