File: //usr/lib/python3/dist-packages/twisted/internet/test/__pycache__/test_tcp.cpython-310.pyc
o
�b}� �
@ s� d Z ddlZddlZddlZddlZddlZddlmZ ddlm Z m
Z
mZmZm
Z
mZ ddlmZ ddlmZmZ ddlmZmZ ddlZddlmZmZ dd lmZmZmZmZm Z m!Z! dd
l"m#Z#m$Z$ ddl%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z. ddl/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9 dd
l:m;Z;m<Z<m=Z= ddl>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZE ddlFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZO ddlPmQZQmRZRmSZS ddlTmUZU ddlVmWZW ddlXmYZY ddlZm[Z[ ddl\m]Z]m^Z^ ddl_m`Z`maZambZbmcZc ddldmeZemfZfmgZg zddlhmiZi W n ej�y dZkY n w ddllmmZm dZkdZnze�ejoejp�Znen�qd� W n er�yN Zs z
dZtdes� �ZuW Y dZs[sn dZs[sww dZtdZuendu�r\en�v� e[�w� �rmddlxmyZy eyjzZ{dZ|nzdd lxm}Z} W n ej�y� d!d"� Z{Y nw e}j~Z{dZ|d#d$� Zd%d&� Z�G d'd(� d(�Z�G d)d*� d*eg�Z�G d+d,� d,e<�Z�ee5�G d-d.� d.��Z�ee5e�� G d/d0� d0eg�Z�G d1d2� d2eg�Z�G d3d4� d4eJ�Z�G d5d6� d6e��Z�ee8�G d7d8� d8��Z�G d9d:� d:eQeIeM�Z�G d;d<� d<e��Z�eeteu�G d=d>� d>e���Z�G d?d@� d@eQ�Z�G dAdB� dBe��Z�eeteu�G dCdD� dDe���Z�dEdF� Z�G dGdH� dHe�Z�ee��ejnddI�G dJdK� dK���Z�ee|dL�G dMdN� dNef��Z�dOdP� Z�ee|dL�G dQdR� dRef��Z�G dSdT� dTeK�Z�G dUdV� dV�Z�G dWdX� dXe��Z�G dYdZ� dZe��Z�G d[d\� d\�Z�G d]d^� d^eQe�e�e��Z�G d_d`� d`eQe�e�e��Z�G dadb� dbe<�Z�dcdd� Z�dedf� Z�dgdh� Z�G didj� djeQ�Z�G dkdl� dl�Z�G dmdn� dn�Z�G dodp� dpe�e�eQ�Z�G dqdr� dre�e�eQ�Z�e�� ��e���� � e�� ��e���� � e�� ��e���� � e�� ��e���� � e�� ��e���� � e�� ��e���� � e�� ��e���� � e�� ��e���� � e�� ��e���� � G dsdt� dteH�Z�G dudv� dveH�Z�G dwdx� dxeH�Z�G dydz� dze��Z�G d{d|� d|eH�Z�G d}d~� d~eH�Z�G dd�� d�eH�Z�G d�d�� d�e��Z�G d�d�� d�e��Z�G d�d�� d�e��Z�G d�d�� d�e��Z�G d�d�� d�eH�Z�G d�d�� d�eH�Z�G d�d�� d�e��Z�G d�d�� d�e��Z�G d�d�� d�e��Z�G d�d�� d�e��Z�G d�d�� d��Z�G d�d�� d�eQe��Z�e�� ��e���� � eeteu�G d�d�� d�eg��Z�G d�d�� d�ef�Z�ee|dL�G d�d�� d�ef��Z�G d�d�� d�ef�Z�dS )�zU
Tests for implementations of L{IReactorTCP} and the TCP parts of
L{IReactorSocket}.
� N��wraps)�Callable�ClassVar�List�Optional�Sequence�Type)�skipIf)� Interface�implementer)�verifyClass�verifyObject)�IPv4Address�IPv6Address)�Deferred�DeferredList�fail�
gatherResults�
maybeDeferred�succeed)�TCP4ClientEndpoint�TCP4ServerEndpoint) �ConnectBindError�ConnectionAborted�ConnectionClosed�ConnectionDone�ConnectionLost�ConnectionRefusedError�DNSLookupError�
NoProtocol� UserError)
�
IConnector�IHalfCloseableProtocol�ILoggingContext�
IPullProducer�
IPushProducer�
IReactorFDSet�IReactorSocket�IReactorTCP�IResolverSimple�
ITLSTransport)�
ClientFactory�Protocol�
ServerFactory)�
Connection�Server�_BuffersLogs�_FileDescriptorReservation�_IFileDescriptorReservation�_NullFileDescriptorReservation�_resolveIPv6) �BrokenContextFactory�ConnectableProtocol�ConnectionTestsMixin�EndpointCreator�LogObserverMixin�Stop�StreamClientTestsMixin�findFreePort�runProtocolsWithReactor)�ReactorBuilder�needsRunningReactor�stopOnError)�Logger)�log)�Failure)�platform)�
MemoryReactor�StringTransport)�ClientStartStopFactory�ClosingFactory�MyClientFactory�MyServerFactory)�SkipTest�SynchronousTestCase�TestCase)�SSLF)�ClientContextFactoryT)�::1r zIPv6 not available. � )�_win32ifaces)�_posixifacesc C s g S �N� rV rV rV �@/usr/lib/python3/dist-packages/twisted/internet/test/test_tcp.py�<lambda>� � rX c C s t � } | r | d S td��)al
Find and return a configured link local IPv6 address including a scope
identifier using the % separation syntax. If the system has no link local
IPv6 addresses, raise L{SkipTest} instead.
@raise SkipTest: if no link local address can be found or if the
C{netifaces} module is not available.
@return: a C{str} giving the address
r z#Link local IPv6 address unavailable)�getLinkLocalIPv6AddressesrL )� addressesrV rV rW �getLinkLocalIPv6Address� s r\ c C sD |\}}d|v sd|v rt �||�d d }n||f}| �|� dS )a7
Connect a socket to the given destination.
@param client: A C{socket.socket}.
@param destination: A tuple of (host, port). The host is a C{str}, the
port a C{int}. If the C{host} is an IPv6 IP, the address is resolved
using C{getaddrinfo} and the first version found is used.
�%�:r � N)�socket�getaddrinfo�connect)�client�destination�host�port�addressrV rV rW rb � s
rb c @ �P e Zd ZdZdd� Zdd� Zdd� Zdd � Zd
d� Zdd
� Z dd� Z
dd� ZdS )�
FakeSocketz�
A fake for L{socket.socket} objects.
@ivar data: A C{str} giving the data which will be returned from
L{FakeSocket.recv}.
@ivar sendBuffer: A C{list} of the objects passed to L{FakeSocket.send}.
c C s || _ g | _d S rU )�data�
sendBuffer��selfrj rV rV rW �__init__� s
zFakeSocket.__init__c C �
|| _ d S rU )�blocking)rm rp rV rV rW �setblocking� �
zFakeSocket.setblockingc C s | j S rU �rj )rm �sizerV rV rW �recv� s zFakeSocket.recvc C s | j �|� t|�S )z�
I{Send} all of C{bytes} by accumulating it into C{self.sendBuffer}.
@return: The length of C{bytes}, indicating all the data has been
accepted.
)rk �append�len�rm �bytesrV rV rW �send� s zFakeSocket.sendc C � dS )z�
Shutdown is not implemented. The method is provided since real sockets
have it and some code expects it. No behavior of L{FakeSocket} is
affected by a call to it.
NrV )rm �howrV rV rW �shutdown� rY zFakeSocket.shutdownc C r{ )z�
Close is not implemented. The method is provided since real sockets
have it and some code expects it. No behavior of L{FakeSocket} is
affected by a call to it.
NrV �rm rV rV rW �close� rY zFakeSocket.closec G r{ )z�
Setsockopt is not implemented. The method is provided since
real sockets have it and some code expects it. No behavior of
L{FakeSocket} is affected by a call to it.
NrV )rm �argsrV rV rW �
setsockopt� rY zFakeSocket.setsockoptc C r{ )z�
Return a fake file descriptor. If actually used, this will have no
connection to this L{FakeSocket} and will probably cause surprising
results.
� rV r~ rV rV rW �fileno� s zFakeSocket.filenoN)�__name__�
__module__�__qualname__�__doc__rn rq ru rz r} r r� r� rV rV rV rW ri � s
ri c @ �( e Zd ZdZdd� Zdd� Zdd� ZdS ) �FakeSocketTestszT
Test that the FakeSocket can be used by the doRead method of L{Connection}
c C s$ t d�}|�d� | �|jd� d S )N� someDatar )ri rq �assertEqualrp �rm �sktrV rV rW �
test_blocking� s
zFakeSocketTests.test_blockingc C s t d�}| �|�d�d� d S )Nr� �
)ri r� ru r� rV rV rW � test_recv� s zFakeSocketTests.test_recvc C s2 t d�}|�d�}| �|d� | �|jdg� dS )z�
L{FakeSocket.send} accepts the entire string passed to it, adds it to
its send buffer, and returns its length.
� s foo� N)ri rz r� rk )rm r� �countrV rV rW � test_send� s
zFakeSocketTests.test_sendN)r� r� r� r� r� r� r� rV rV rV rW r� � s
r� c @ � e Zd ZdZdd� ZdS )�FakeProtocolzL
An L{IProtocol} that returns a value from its dataReceived method.
c C r{ )zq
Return something other than L{None} to trigger a deprecation warning for
that behavior.
rV rV rl rV rV rW �dataReceived s zFakeProtocol.dataReceivedN�r� r� r� r� r� rV rV rV rW r� � r� c @ rh )�_FakeFDSetReactora�
An in-memory implementation of L{IReactorFDSet}, which records the current
sets of active L{IReadDescriptor} and L{IWriteDescriptor}s.
@ivar _readers: The set of L{IReadDescriptor}s active on this
L{_FakeFDSetReactor}
@type _readers: L{set}
@ivar _writers: The set of L{IWriteDescriptor}s active on this
L{_FakeFDSetReactor}
@ivar _writers: L{set}
c C s t � | _t � | _d S rU )�set�_readers�_writersr~ rV rV rW rn � z_FakeFDSetReactor.__init__c C � | j �|� d S rU )r� �add�rm �readerrV rV rW � addReader � z_FakeFDSetReactor.addReaderc C � || j v r
| j �|� d S d S rU )r� �remover� rV rV rW �removeReader# �
�z_FakeFDSetReactor.removeReaderc C r� rU )r� r� �rm �writerrV rV rW � addWriter' r� z_FakeFDSetReactor.addWriterc C r� rU )r� r� r� rV rV rW �removeWriter* r� z_FakeFDSetReactor.removeWriterc C s | � � | �� }| �� |S rU )�
getReaders�
getWritersrn �rm �resultrV rV rW � removeAll. s z_FakeFDSetReactor.removeAllc C �
t | j�S rU )�listr� r~ rV rV rW r� 3 rr z_FakeFDSetReactor.getReadersc C r� rU )r� r� r~ rV rV rW r� 6 rr z_FakeFDSetReactor.getWritersN)r� r� r� r� rn r� r� r� r� r� r� r� rV rV rV rW r�
s
r� c @ s8 e Zd ZdZdd� Zdd� Zdd� Zdd � Zd
d� ZdS )
�TCPServerTestsz<
Whitebox tests for L{twisted.internet.tcp.Server}.
c C sH t � | _G dd� d�}td�| _t� | _t| j| jd|� d | j�| _d S )Nc @ � e Zd ZdZdS )z&TCPServerTests.setUp.<locals>.FakePortr� N)r� r� r� �_realPortNumberrV rV rV rW �FakePortE � r� r� )rR r )r� �reactorri r� r- �protocolr0 �server)rm r� rV rV rW �setUpB s
�zTCPServerTests.setUpc C s4 | j �ttd��� | j �d� | �| jjg � dS )zq
L{Server.write} discards bytes passed to it if called after it has lost
its connection.
�Simulated lost connection� hello worldN)r� �connectionLostrD � Exception�writer� r� rk r~ rV rV rW �test_writeAfterDisconnectN s z(TCPServerTests.test_writeAfterDisconnectc C � d| j _| �� dS )z�
L{Server.write} discards bytes passed to it if called after it has lost
its connection when the connection had started TLS.
TN)r� �TLSr� r~ rV rV rW �!test_writeAfterDisconnectAfterTLSW � z0TCPServerTests.test_writeAfterDisconnectAfterTLSc C s6 | j �ttd��� | j �dg� | �| jjg � dS )zy
L{Server.writeSequence} discards bytes passed to it if called after it
has lost its connection.
r� r� N)r� r� rD r� �
writeSequencer� r� rk r~ rV rV rW �!test_writeSequenceAfterDisconnect_ s z0TCPServerTests.test_writeSequenceAfterDisconnectc C r� )z�
L{Server.writeSequence} discards bytes passed to it if called after it
has lost its connection when the connection had started TLS.
TN)r� r� r� r~ rV rV rW �)test_writeSequenceAfterDisconnectAfterTLSh r� z8TCPServerTests.test_writeSequenceAfterDisconnectAfterTLSN) r� r� r� r� r� r� r� r� r� rV rV rV rW r� = s r� c @ s4 e Zd ZdZdd� Zdd� Zee d�dd� �Zd S )
�TCPConnectionTestsz@
Whitebox tests for L{twisted.internet.tcp.Connection}.
c C sj t d�}t� }t||�}|�� | �tjg�}| �|d d t� | �|d d d� | �t|�d� dS )z�
When an L{IProtocol} implementation that returns a value from its
C{dataReceived} method, a deprecated warning is emitted.
r� r �category�messagez�Returning a value other than None from twisted.internet.test.test_tcp.FakeProtocol.dataReceived is deprecated since Twisted 11.0.0.r� N) ri r� r/ �doRead�
flushWarningsr� r� �DeprecationWarningrw )rm r� r� �conn�warningsrV rV rW �test_doReadWarningIsRaisedv s
�z-TCPConnectionTests.test_doReadWarningIsRaisedc C s( t d�}t� }t||�}| �|j� dS )z
The C{TLS} attribute of a L{Connection} instance is C{False} before
L{Connection.startTLS} is called.
r� N)ri r� r/ �assertFalser� �rm r� r� r� rV rV rW �test_noTLSBeforeStartTLS� s
z+TCPConnectionTests.test_noTLSBeforeStartTLSzNo SSL support availablec C sB t d�}t� }t||t� d�}d|_|�t� d� | �|j� dS )z}
The C{TLS} attribute of a L{Connection} instance is C{True} after
L{Connection.startTLS} is called.
r� �r� TN) ri r� r/ r� �_tlsClientDefault�startTLSrP �
assertTruer� r� rV rV rW �test_tlsAfterStartTLS� s z(TCPConnectionTests.test_tlsAfterStartTLSN) r� r� r� r� r� r� r
�useSSLr� rV rV rV rW r� q s
r� c @ s$ e Zd ZdZdZdd� Zdd� ZdS )�
TCPCreatorzO
Create IPv4 TCP endpoints for L{runProtocolsWithReactor}-based tests.
� 127.0.0.1c C s t |d| jd�S )z4
Create a server-side TCP endpoint.
r �� interface)r r� )rm r� rV rV rW r� � � zTCPCreator.serverc C s t || j|j�S )z�
Create a client end point that will connect to the given address.
@type serverAddress: L{IPv4Address}
)r r� rf )rm r� �
serverAddressrV rV rW rc � � zTCPCreator.clientN)r� r� r� r� r� r� rc rV rV rV rW r� � s
r� c @ r� )�TCP6CreatoraV
Create IPv6 TCP endpoints for
C{ReactorBuilder.runProtocolsWithReactor}-based tests.
The endpoint types in question here are still the TCP4 variety, since
these simply pass through IPv6 address literals to the reactor, and we are
only testing address literals, not name resolution (as name resolution has
not yet been implemented). See http://twistedmatrix.com/trac/ticket/4470
for more specific information about new endpoint classes. The naming is
slightly misleading, but presumably if you're passing an IPv6 literal, you
know what you're asking for.
c C � t � | _d S rU �r\ r� r~ rV rV rW rn � � zTCP6Creator.__init__N)r� r� r� r� rn rV rV rV rW r� � s
r� c @ � e Zd ZdZdd� Zdd� ZdS )�FakeResolverzR
A resolver implementation based on a C{dict} mapping names to addresses.
c C ro rU )�names)rm r� rV rV rW rn � rr zFakeResolver.__init__c C s4 zt | j| �W S ty ttd| �� Y S w )z�
Return the address mapped to C{name} if it exists, or raise a
C{DNSLookupError}.
@param name: The name to resolve.
@param timeout: The lookup timeout, ignore here.
zFakeResolver couldn't find )r r� �KeyErrorr r )rm �name�timeoutrV rV rW �
getHostByName� s
�zFakeResolver.getHostByNameN)r� r� r� r� rn r� rV rV rV rW r� � s r� c @ sZ e Zd ZdZefZdZedd� �Zedd� �Z dd� Z
d d
� Zdd� Zd
d� Z
dd� ZdS )�TCPClientTestsBasea�
Base class for builders defining tests related to
L{IReactorTCP.connectTCP}. Classes which uses this in must provide all of
the documented instance variables in order to specify how the test works.
These are documented as instance variables rather than declared as methods
due to some peculiar inheritance ordering concerns, but they are
effectively abstract methods.
@ivar endpoints: A client/server endpoint creator appropriate to the
address family being tested.
@type endpoints: L{twisted.internet.test.connectionmixins.EndpointCreator}
@ivar interface: An IP address literal to locally bind a socket to as well
as to connect to. This can be any valid interface for the local host.
@type interface: C{str}
@ivar port: An unused local listening port to listen on and connect to.
This will be used in conjunction with the C{interface}. (Depending on
what they're testing, some tests will locate their own port with
L{findFreePort} instead.)
@type port: C{int}
@ivar family: an address family constant, such as L{socket.AF_INET},
L{socket.AF_INET6}, or L{socket.AF_UNIX}, which indicates the address
family of the transport type under test.
@type family: C{int}
@ivar addressClass: the L{twisted.internet.interfaces.IAddress} implementor
associated with the transport type under test. Must also be a
3-argument callable which produces an instance of same.
@type addressClass: C{type}
@ivar fakeDomainName: A fake domain name to use, to simulate hostname
resolution and to distinguish between hostnames and IP addresses where
necessary.
@type fakeDomainName: C{str}
Nc C s( | j dur| j �� jS t| j| j�d S )z�
Return the port number to connect to, using C{self._port} set up by
C{listen} if available.
@return: The port number to connect to.
@rtype: C{int}
Nr� )�_port�getHostrf r= r� �familyr~ rV rV rW rf s
zTCPClientTestsBase.portc C s | j jS )zK
Return the interface attribute from the endpoints object.
)� endpointsr� r~ rV rV rW r� s zTCPClientTestsBase.interfacec C s |j d|| jd�| _| jS )z�
Start a TCP server with the given C{factory}.
@param reactor: The reactor to create the TCP port in.
@param factory: The server factory.
@return: A TCP port instance.
r r� )� listenTCPr� r� �rm r� �factoryrV rV rW �listen s
zTCPClientTestsBase.listenc C s |� | j| j|�S )z�
Start a TCP client with the given C{factory}.
@param reactor: The reactor to create the connection in.
@param factory: The client factory.
@return: A TCP connector instance.
)�
connectTCPr� rf r� rV rV rW rb - s
zTCPClientTestsBase.connectc
s� t � ���� �t� }�|_t��� G dd� dt�� �j����|�}� ��fdd�}|� |�}�fdd�}dd� }|�
||� �fd d
�}|� |�}|�tj
� �fdd�} |� | � ���� d
S )zr
When the factory's C{buildProtocol} returns L{None} the connection is
gracefully closed.
c @ � e Zd Zdd� ZdS )zETCPClientTestsBase.test_buildProtocolReturnsNone.<locals>.NoneFactoryc S � d S rU rV �rm rg rV rV rW �
buildProtocolG � zSTCPClientTestsBase.test_buildProtocolReturnsNone.<locals>.NoneFactory.buildProtocolN�r� r� r� r� rV rV rV rW �NoneFactoryF � r� c s"