File: //usr/lib/python3/dist-packages/twisted/internet/test/__pycache__/test_endpoints.cpython-38.pyc
U
��W[�D � @ s\ d Z ddlmZmZ ddlmZ ddlmZmZm Z m
Z
mZ ddlm
Z
ddlmZ ddlmZmZmZ ddlmZ dd lmZmZ dd
lmZ ddlmZ ddlmZmZ dd
lm Z ddl!m"Z" ddl#m$Z$m%Z%m&Z&m'Z'm(Z( ddl#m)Z)m*Z*m+Z+ ddl,m-Z-m.Z.m/Z/ ddl,m0Z0m1Z1 ddl2m3Z3 ddl4m5Z5m6Z6m7Z7 ddl8m9Z9m:Z:m;Z; ddl<m=Z= ddl>m?Z? ddl@mAZAmBZB ddlCmDZD ddlEmFZF ddlGmHZH ddlImJZJ ddlKmLZL ddlMmNZN ddlOmPZPmQZQ dd lRmSZSmTZT dd!lUmVZV dd"lWmXZX dd#l4mYZY dd$l4mZZZ dd%l[m\Z\ dd&l]m^Z^ eLd'�j_�`d(�ZaeLd'�j_�`d)�ZbeLd'�j_�`d*�ZceLed�j_�`d+�Zeee�fd,�Zge'�heaji�Zje'�hebji�Zke'�hecji�Zle'�heeji�Zme'�hegji�Znzpdd-lompZp dd.lqmrZrmsZsmtZtmuZumvZv dd/lwmxZx dd0lymzZzm{Z{m|Z|m}Z} es�~ea�� �Z�er�~ea�� �Z�d1Z�W n e�k
�r4 d2Z�Y nX G d3d4� d4e:�Z�ee%j��G d5d6� d6e���Z�ee%j��G d7d8� d8e���Z�ee%j��G d9d:� d:e���Z�G d;d<� d<e9�Z�G d=d>� d>e9�Z�G d?d@� d@ej��Z�G dAdB� dBe��Z�G dCdD� dDe��Z�G dEdF� dFe�e��Z�G dGdH� dHe;�Z�G dIdJ� dJe��Z�G dKdL� dLej��Z�G dMdN� dNe(j:�Z�ee%j��G dOdP� dPe e���Z�ee%j5e�� ee%j6e�� ee%j�e�� ee%j��G dQdR� dRe���Z�G dSdT� dTej��Z�G dUdV� dVej��Z�G dWdX� dXej��Z�G dYdZ� dZe�ej��Z�G d[d\� d\e�ej��Z�G d]d^� d^e�ej��Z�G d_d`� d`ee?�Z�d�dcdd�Z�G dedf� dfej��Z�G dgdh� dhej��Z�G didj� dje��Z�G dkdl� dle�ej��Z�G dmdn� dne�ej��Z�G dodp� dpe�ej��Z�G dqdr� dre�ej��Z�G dsdt� dtej��Z�G dudv� dvej��Z�G dwdx� dxej��Z�G dydz� dzej��Z�G d{d|� d|e�ej��Z�G d}d~� d~e�ej��Z�G dd�� d�ej��Z�G d�d�� d�ej��Z�d�d�d��Z�G d�d�� d�ej��Z�G d�d�� d�ej��Z�G d�d�� d�e�ej��Z�G d�d�� d�ej��Z�G d�d�� d�ej��Z�G d�d�� d�ej��Z�G d�d�� d�ej��Z�G d�d�� d�eQj�e��Z�G d�d�� d�eQj�e��Z�G d�d�� d�ePj�e��Z�G d�d�� d�e��Z�G d�d�� d�ej��Z�d�d�� Z�G d�d�� d�ej��Z�d�d�� Z�G d�d�� d�ej��Z�dbS )�z�
Test the C{I...Endpoint} implementations that wrap the L{IReactorTCP},
L{IReactorSSL}, and L{IReactorUNIX} interfaces found in
L{twisted.internet.endpoints}.
� )�division�absolute_import)�EPERM)�AF_INET�AF_INET6�SOCK_STREAM�IPPROTO_TCP�gaierror)� normalize)�FunctionType)�implementer�
providedBy�provider)�InterfaceClass)�verifyObject�verifyClass)�unittest)�MemoryReactorClock)�RaisingMemoryReactor�StringTransport)� StringTransportWithDisconnection)�plugins)�error�
interfaces�defer� endpoints�protocol)�reactor�threads�stdio)�IPv4Address�IPv6Address�UNIXAddress)�_ProcessAddress�HostnameAddress)�StandardErrorBehavior)� IConsumer�
IPushProducer�
ITransport)�
ClientFactory�Protocol�Factory)�PipeAddress)�Clock)�ILogObserver�globalLogPublisher)�
getPlugins)�log)�Failure)�FilePath)� getModule)� ListenFDs)�basic�policies)�connectedServerAndClient�connectableEndpoint)�ConnectingCancelledError)�nativeString)�IHostnameResolver)�IReactorPluggableNameResolver)�proxyForInterface)�
isIPv6Addressztwisted.testz
server.pemzkey.pem.no_trailing_newlinezcert.pem.no_trailing_newlineZfake_CAsz chain.pem)�makeCertificate)�PrivateCertificate�Certificate�CertificateOptions�KeyPair�DiffieHellmanParameters��TLSMemoryBIOFactory)�ContextType�
SSLv23_METHOD�TLSv1_METHOD�OP_NO_SSLv3Fz.OpenSSL is required to construct SSL Endpointsc @ s8 e Zd ZdZdd� Zdd� Zdd� Zdd � Zd
d� ZdS )
�TestProtocolzx
Protocol whose only function is to callback deferreds on the
factory when it is connected or disconnected.
c C s g | _ g | _d| _d S �Nr )�data�connectionsLost�connectionMadeCalls��self� rS �F/usr/lib/python3/dist-packages/twisted/internet/test/test_endpoints.py�__init__\ s zTestProtocol.__init__c C s dS )N�A Test ProtocolrS rQ rS rS rT � logPrefixb s zTestProtocol.logPrefixc C s | j d7 _ d S �N� )rP rQ rS rS rT �connectionMadef s zTestProtocol.connectionMadec C s | j �|� d S �N)rN �append�rR rN rS rS rT �dataReceivedj s zTestProtocol.dataReceivedc C s | j �|� d S r[ )rO r\ �rR �reasonrS rS rT �connectionLostn s zTestProtocol.connectionLostN) �__name__�
__module__�__qualname__�__doc__rU rW rZ r^ ra rS rS rS rT rL V s rL c @ s( e Zd ZdZdd� Zdd� Zdd� ZdS ) �TestHalfCloseableProtocolab
A Protocol that implements L{IHalfCloseableProtocol} and records whether
its C{readConnectionLost} and {writeConnectionLost} methods are called.
@ivar readLost: A C{bool} indicating whether C{readConnectionLost} has been
called.
@ivar writeLost: A C{bool} indicating whether C{writeConnectionLost} has
been called.
c C s t �| � d| _d| _d S )NF)rL rU �readLost� writeLostrQ rS rS rT rU � s
z"TestHalfCloseableProtocol.__init__c C s
d| _ d S �NT)rg rQ rS rS rT �readConnectionLost� s z,TestHalfCloseableProtocol.readConnectionLostc C s
d| _ d S ri )rh rQ rS rS rT �writeConnectionLost� s z-TestHalfCloseableProtocol.writeConnectionLostN)rb rc rd re rU rj rk rS rS rS rT rf s s rf c @ s e Zd ZdZdd� Zdd� ZdS )�"TestFileDescriptorReceiverProtocola
A Protocol that implements L{IFileDescriptorReceiver} and records how its
C{fileDescriptorReceived} method is called.
@ivar receivedDescriptors: A C{list} containing all of the file descriptors
passed to C{fileDescriptorReceived} calls made on this instance.
c C s t �| � g | _d S r[ )rL rZ �receivedDescriptorsrQ rS rS rT rZ � s
z1TestFileDescriptorReceiverProtocol.connectionMadec C s | j �|� d S r[ )rm r\ )rR Z
descriptorrS rS rT �fileDescriptorReceived� s z9TestFileDescriptorReceiverProtocol.fileDescriptorReceivedN)rb rc rd re rZ rn rS rS rS rT rl � s rl c @ s e Zd ZdZdd� Zdd� ZdS )�TestHandshakeListenera
A Protocol that implements L{IHandshakeListener} and records the
number of times its C{handshakeCompleted} method has been called.
@ivar handshakeCompletedCalls: The number of times
C{handshakeCompleted}
@type handshakeCompletedCalls: L{int}
c C s t �| � d| _d S rM )rL rU �handshakeCompletedCallsrQ rS rS rT rU � s
zTestHandshakeListener.__init__c C s | j d7 _ dS )zk
Called when a TLS handshake has completed. Implemented per
L{IHandshakeListener}
rY N)rp rQ rS rS rT �handshakeCompleted� s z(TestHandshakeListener.handshakeCompletedN)rb rc rd re rU rq rS rS rS rT ro � s ro c @ s e Zd ZdZeZdS )�TestFactoryz�
Simple factory to be used both when connecting and listening. It contains
two deferreds which are called back when my protocol connects and
disconnects.
N)rb rc rd re rL r rS rS rS rT rr � s rr c @ s e Zd ZdZdd� ZdS )�NoneFactoryzC
A one off factory whose C{buildProtocol} returns L{None}.
c C s d S r[ rS �rR �addrrS rS rT �
buildProtocol� s zNoneFactory.buildProtocolN�rb rc rd re rv rS rS rS rT rs � s rs c @ s� e Zd ZdZdd� Zdd� Zdd� Zdd � Zd
d� Zdd
� Z dd� Z
dd� Zdd� Zdd� Z
dd� Zdd� Zdd� Zdd� Zdd� Zd d!� Zd"d#� Zd$d%� Zd&d'� Zd(d)� Zd*d+� Zd,S )-�WrappingFactoryTestszS
Test the behaviour of our ugly implementation detail C{_WrappingFactory}.
c C s* t � }t�|�}|�� | �d|j� dS )z�
L{_WrappingFactory.doStart} passes through to the wrapped factory's
C{doStart} method, allowing application-specific setup and logging.
rY N)r) r �_WrappingFactoryZdoStart�assertEqual�numPorts�rR �factory�wfrS rS rT �test_doStart� s
z!WrappingFactoryTests.test_doStartc C s0 t � }d|_t�|�}|�� | �d|j� dS )z�
L{_WrappingFactory.doStop} passes through to the wrapped factory's
C{doStop} method, allowing application-specific cleanup and logging.
� � N)r) r{ r ry ZdoStoprz r| rS rS rT �test_doStop� s
z WrappingFactoryTests.test_doStopc sJ G dd� dt �}t�|� �}|�d� � �|jt�}|�� fdd�� |S )z�
An exception raised in C{buildProtocol} of our wrappedFactory
results in our C{onConnection} errback being fired.
c @ s e Zd ZdZdd� ZdS )zCWrappingFactoryTests.test_failedBuildProtocol.<locals>.BogusFactoryzZ
A one off factory whose C{buildProtocol} raises an C{Exception}.
c S s t d��d S )N�My protocol is poorly defined.)�
ValueErrorrt rS rS rT rv � s zQWrappingFactoryTests.test_failedBuildProtocol.<locals>.BogusFactory.buildProtocolNrw rS rS rS rT �BogusFactory� s r� Nc s � � | jd�S )N)r� )rz �args)�erQ rS rT �<lambda>� s �z?WrappingFactoryTests.test_failedBuildProtocol.<locals>.<lambda>)r) r ry rv �
assertFailure�
_onConnectionr� �addCallback)rR r� r~ �drS rQ rT �test_failedBuildProtocol� s
z-WrappingFactoryTests.test_failedBuildProtocolc C s* t �t� �}|�d� | �|jtj� dS )z�
If the wrapped factory's C{buildProtocol} returns L{None} the
C{onConnection} errback fires with L{error.NoProtocol}.
N)r ry rs rv �failureResultOfr� r �
NoProtocol�rR ZwrappingFactoryrS rS rT �test_buildNoneProtocol s
z+WrappingFactoryTests.test_buildNoneProtocolc C s0 t �t� �}|j�dd� � | �|�d�� dS )z�
If the wrapped factory's C{buildProtocol} returns L{None} then
L{endpoints._WrappingFactory.buildProtocol} returns L{None}.
c S s d S r[ rS �r` rS rS rT r� � zDWrappingFactoryTests.test_buildProtocolReturnsNone.<locals>.<lambda>N)r ry rs r� �
addErrback�assertIsNonerv r� rS rS rT �test_buildProtocolReturnsNone
s z2WrappingFactoryTests.test_buildProtocolReturnsNonec C s* t �t� �}|�d�}| �|�� d� dS )z�
If the wrapped protocol provides L{ILoggingContext}, whatever is
returned from the wrapped C{logPrefix} method is returned from
L{_WrappingProtocol.logPrefix}.
NrV )r ry rr rv rz rW )rR r~ �wprS rS rT �test_logPrefixPassthrough s
z.WrappingFactoryTests.test_logPrefixPassthroughc C sD G dd� dt �}t� }||_t�|�}|�d�}| �|�� d� dS )z�
If the wrapped protocol does not provide L{ILoggingContext}, the
wrapped protocol's class name is returned from
L{_WrappingProtocol.logPrefix}.
c @ s e Zd ZdS )z>WrappingFactoryTests.test_logPrefixDefault.<locals>.NoProtocolN�rb rc rd rS rS rS rT r� * s r� N)�objectrr r r ry rv rz rW )rR r� r} r~ r� rS rS rT �test_logPrefixDefault$ s
z*WrappingFactoryTests.test_logPrefixDefaultc C s^ t �t� �}|�d�}|�d� |�d� | �|jjdg� |�d� | �|jjddg� dS )z�
The wrapped C{Protocol}'s C{dataReceived} will get called when our
C{_WrappingProtocol}'s C{dataReceived} gets called.
Ns foos bar) r ry rr rv �makeConnectionr^ rz �_wrappedProtocolrN )rR r~ �prS rS rT � test_wrappedProtocolDataReceived3 s
z5WrappingFactoryTests.test_wrappedProtocolDataReceivedc C sH t �t� �}|�d�}t� }|�|� | �|j|� | �|jj|� dS )zo
Our transport is properly hooked up to the wrappedProtocol when a
connection is made.
N) r ry rr rv r� r� rz � transportr� )rR r~ r� ZdummyTransportrS rS rT �test_wrappedProtocolTransportC s
z2WrappingFactoryTests.test_wrappedProtocolTransportc C s: t � }t�|�}|�d�}|�d� | �|jjdg� dS )z�
Our wrappedProtocol's connectionLost method is called when
L{_WrappingProtocol.connectionLost} is called.
N�fail)rr r ry rv ra rz r� rO )rR Ztfr~ r� rS rS rT �"test_wrappedProtocolConnectionLostT s
z7WrappingFactoryTests.test_wrappedProtocolConnectionLostc sV t �t� �}ttjdd��}|�d|� g � � fdd�}|j�|� | � � |g� dS )z�
Calls to L{_WrappingFactory.clientConnectionLost} should errback the
L{_WrappingFactory._onConnection} L{Deferred}
r� ��stringNc s � � | � d S r[ �r\ ��f��errorsrS rT �gotErrorn s zBWrappingFactoryTests.test_clientConnectionFailed.<locals>.gotError)
r ry rr r2 r �ConnectError�clientConnectionFailedr� r� rz )rR r~ ZexpectedFailurer� rS r� rT �test_clientConnectionFailedb s z0WrappingFactoryTests.test_clientConnectionFailedc C s>