%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /usr/lib/python3/dist-packages/twisted/internet/test/__pycache__/
Upload File :
Create Path :
Current File : //usr/lib/python3/dist-packages/twisted/internet/test/__pycache__/test_udp.cpython-312.pyc

�

Ϫ�f�A����dZddlZddlmZddlmZddlmZmZddl	m
Z
mZddlm
Z
mZddlmZmZmZmZdd	lmZdd
lmZmZddlmZddlmZdd
lmZmZddl m!Z!m"Z"ddl#m$Z$d�Z%e%�Z&d�Z'Gd�de�Z(Gd�d�Z)Gd�dee)e(�Z*Gd�dee)e(�Z+e,�j[e*j]��e,�j[e+j]��y)zU
Tests for implementations of L{IReactorUDP} and the UDP parts of
L{IReactorSocket}.
�N)�implementer)�verifyObject)�defer�error)�IPv4Address�IPv6Address)�Deferred�
maybeDeferred)�IListeningPort�ILoggingContext�IReactorSocket�IReactorUDP)�DatagramProtocol)�LogObserverMixin�findFreePort)�ReactorBuilder)�context)�ILogContext�err)�
GoodClient�Server)�SkipTestc��d}d}	tjtj�}|jd�d}|r|j	�|S#t$rY�wxYw)z4Returns True if the system can bind an IPv6 address.NF)�::1rT)�socket�AF_INET6�bind�OSError�close)�sock�has_ipv6s  �@/usr/lib/python3/dist-packages/twisted/internet/test/test_udp.py�	_has_ipv6r#!s[���D��H�
��}�}�V�_�_�-���	�	�*������
�
���O���
��
�s�6A�	A�Ac� �tsd|_|S)Nz.Does not work on systems without IPv6 support.)�HAS_IPV6�skip)�fs r"�skipWithoutIPv6r(5s���A����H�c�"�eZdZdZd�Zd�Zd�Zy)�DatagramTransportTestsMixinzP
    Mixin defining tests which apply to any port/datagram based transport.
    c�&�|j�}|j�}tt�Gd�dt��}|�}|j||�}d|j
�jfz}|j|f|dd�y)zu
        When a port starts, a message including a description of the associated
        protocol is logged.
        c��eZdZd�Zy)�QDatagramTransportTestsMixin.test_startedListeningLogMessage.<locals>.SomeProtocolc��y)NzCrazy Protocol���selfs r"�	logPrefixz[DatagramTransportTestsMixin.test_startedListeningLogMessage.<locals>.SomeProtocol.logPrefixJs��'r)N)�__name__�
__module__�__qualname__r3r0r)r"�SomeProtocolr.Hs��
(r)r7zCrazy Protocol starting on %dr�messageN)	�observe�buildReactorrrr�getListeningPort�getHost�port�assertEqual)r2�loggedMessages�reactorr7�protocol�p�expectedMessages       r"�test_startedListeningLogMessagez;DatagramTransportTestsMixin.test_startedListeningLogMessage@s���
������#�#�%��	�_�	%�	(�+�	(�
&�	(� �>���!�!�'�8�4��9�Q�Y�Y�[�=M�=M�<O�O�����/�+�^�A�->�y�-I�Jr)c�J�����|j��|j��|j�t���d�j	�j
�d�}�fd�����fd�}�j
|�|j��|j|f�dd�y)z�
        When a connection is lost a message is logged containing an
        address identifying the port and the fact that it was closed.
        z
(UDP Port z Closed)c�&���j�y�N��stop��ignoredr@s �r"�stopReactorzNDatagramTransportTestsMixin.test_connectionLostLogMessage.<locals>.stopReactor]s����L�L�Nr)c�X���dd�=t�j�j��yrG)r
�
stopListening�addCallback)r?rBrLs���r"�doStopListeningzRDatagramTransportTestsMixin.test_connectionLostLogMessage.<locals>.doStopListening`s"����q�!��!�/�/�*�6�6�{�Cr)rr8N)	r9r:r;rr<r=�callWhenRunning�
runReactorr>)r2rCrPr?rBr@rLs   @@@@r"�test_connectionLostLogMessagez9DatagramTransportTestsMixin.test_connectionLostLogMessageSs����
������#�#�%���!�!�'�+;�+=�>��&�q�y�y�{�'7�'7�&8��A��	�	D�	����0����� ����/�+�^�A�->�y�-I�Jr)c�>��G�fd�dt�}|j��|�}|j�|�|j��|j	|j
�|j	|j�|j|j�y)z�
        L{DatagramProtocol.stopProtocol} is called asynchronously (ie, not
        re-entrantly) when C{stopListening} is used to stop the datagram
        transport.
        c�.��eZdZdZdZdZdZd�Z�fd�Zy)�VDatagramTransportTestsMixin.test_stopProtocolScheduling.<locals>.DisconnectingProtocolFc�b�d|_d|_|jj�d|_y)NTF)�started�inStartProtocol�	transportrNr1s r"�
startProtocolzdDatagramTransportTestsMixin.test_stopProtocolScheduling.<locals>.DisconnectingProtocol.startProtocolvs)��#���'+��$����,�,�.�',��$r)c�V��d|_|j|_�j�y)NT)�stoppedrY�stoppedInStartrI�r2r@s �r"�stopProtocolzcDatagramTransportTestsMixin.test_stopProtocolScheduling.<locals>.DisconnectingProtocol.stopProtocol|s!���#���&*�&:�&:��#����r)N)	r4r5r6rXr]rYr^r[r`)r@s�r"�DisconnectingProtocolrVps!����G��G�#�O�"�N�
-�
r)raN)	rr:r;rR�
assertTruerXr]�assertFalser^)r2rarAr@s   @r"�test_stopProtocolSchedulingz7DatagramTransportTestsMixin.test_stopProtocolSchedulingis|���	�$4�	�"�#�#�%��(�*�����g�x�0����� �����(�(�)�����(�(�)�����0�0�1r)N)r4r5r6�__doc__rDrSrdr0r)r"r+r+;s���K�&K�,2r)r+c��eZdZdZd�Zd�Zed��Zd�Zd�Z	d�Z
d�Zd	�Zed
��Z
ed��Zd�Zed
��Zed��Zd�Zd�Zy)�UDPPortTestsMixinzY
    Tests for L{IReactorUDP.listenUDP} and
    L{IReactorSocket.adoptDatagramPort}.
    c��|j�}|j|t��}|jt	t
|��y)zY
        L{IReactorUDP.listenUDP} returns an object providing L{IListeningPort}.
        N)r:r;rrbrr�r2r@r=s   r"�test_interfacez UDPPortTestsMixin.test_interface�s;���#�#�%���$�$�W�.>�.@�A������^�T�:�;r)c���ttj��\}}|j�}|j	|t�||��}|j
|j�td||��y)z�
        L{IListeningPort.getHost} returns an L{IPv4Address} giving a
        dotted-quad of the IPv4 address the port is listening on as well as
        the port number.
        )�type)r=�	interface�UDPN)	rr�
SOCK_DGRAMr:r;rr>r<r)r2�host�
portNumberr@r=s     r"�test_getHostzUDPPortTestsMixin.test_getHost�sj��(�V�->�->�?���j��#�#�%���$�$��%�'�j�D�%�
��	
��������U�D�*�)M�Nr)c���|j�}|j|t�d��}|j�}|j	|j
d�|j
|t�y)zr
        L{IListeningPort.getHost} returns an L{IPv6Address} when listening on
        an IPv6 interface.
        r�rmN)r:r;rr<r>rp�assertIsInstancer)r2r@r=�addrs    r"�test_getHostIPv6z"UDPPortTestsMixin.test_getHostIPv6�s\���#�#�%���$�$�W�.>�.@�E�$�R���|�|�~��������E�*����d�K�0r)c��|j�}|jtj|jt�dd��y)z�
        An L{InvalidAddressError} is raised when trying to listen on an address
        that isn't a valid IPv4 or IPv6 address.
        rzexample.comrtN)r:�assertRaisesr�InvalidAddressError�	listenUDPrr_s  r"�test_invalidInterfacez'UDPPortTestsMixin.test_invalidInterface�sC��
�#�#�%������%�%������
�#�	�	
r)c����Gd�dt�}�j��|d�}|j}�j�|�}|j	�}�fd�}|j|�|j
t�|j�fd��|jdd|jf��j��y)	z�
        Datagram transports implement L{ILoggingContext.logPrefix} to return a
        message reflecting the protocol they are running.
        c��eZdZd�Zd�Zd�Zy)�IUDPPortTestsMixin.test_logPrefix.<locals>.CustomLogPrefixDatagramProtocolc�0�||_t�|_yrG)�_prefixr	�system)r2�prefixs  r"�__init__zRUDPPortTestsMixin.test_logPrefix.<locals>.CustomLogPrefixDatagramProtocol.__init__�s��%���&�j��r)c��|jSrG)r�r1s r"r3zSUDPPortTestsMixin.test_logPrefix.<locals>.CustomLogPrefixDatagramProtocol.logPrefix�s���|�|�#r)c��|j�?|j}d|_|jtjt�d�yy)Nr�)r��callbackr�getr)r2�bytesrvr�s    r"�datagramReceivedzZUDPPortTestsMixin.test_logPrefix.<locals>.CustomLogPrefixDatagramProtocol.datagramReceived�s;���;�;�*�!�[�[�F�"&�D�K��O�O�G�K�K��$<�X�$F�G�+r)N)r4r5r6r�r3r�r0r)r"�CustomLogPrefixDatagramProtocolr�s��
)�
$�
Hr)r�zCustom Datagramsc�*���jd|�y)NzCustom Datagrams (UDP))r>)r�r2s �r"�	gotSystemz3UDPPortTestsMixin.test_logPrefix.<locals>.gotSystem�s������5�v�>r)c�$���j�SrGrHrJs �r"�<lambda>z2UDPPortTestsMixin.test_logPrefix.<locals>.<lambda>�s
���g�l�l�n�r)s
some bytes�	127.0.0.1N)rr:r�r;r<rO�
addErrbackr�writer=rR)r2r�rA�dr=�addressr�r@s`      @r"�test_logPrefixz UDPPortTestsMixin.test_logPrefix�s����	H�.>�	H��#�#�%��2�3E�F���O�O���$�$�W�h�7���,�,�.��	?�	
�
�
�i� �	���S��	�
�
�4�5��
�
�=�;����"=�>����� r)c�����Gd�dt�}�j��|�}|j}�j�|�}|j	�}d���fd�}|j
|�|jt�|j
�fd��|j�d|jf��j��y)zH
        Write a sequence of L{bytes} to a L{DatagramProtocol}.
        c��eZdZd�Zd�Zy)�DUDPPortTestsMixin.test_writeSequence.<locals>.SimpleDatagramProtocolc�"�t�|_yrG)r	rr1s r"r�zMUDPPortTestsMixin.test_writeSequence.<locals>.SimpleDatagramProtocol.__init__�s��%�Z��
r)c�:�|jj|�yrG)rr�)r2�datarvs   r"r�zUUDPPortTestsMixin.test_writeSequence.<locals>.SimpleDatagramProtocol.datagramReceived�s���
�
�#�#�D�)r)N)r4r5r6r�r�r0r)r"�SimpleDatagramProtocolr��s��
(�
*r)r�)ssomesbytesstoswritec�H���jdj��|�y)Nr))r>�join)r��dataToWriter2s ��r"�gotDataz5UDPPortTestsMixin.test_writeSequence.<locals>.gotData�s������S�X�X�k�2�D�9r)c�$���j�SrGrHrJs �r"r�z6UDPPortTestsMixin.test_writeSequence.<locals>.<lambda>�s
���'�,�,�.�r)r�N)rr:rr;r<rOr�r�
writeSequencer=rR)	r2r�rArr=r�r�r�r@s	`      @@r"�test_writeSequencez$UDPPortTestsMixin.test_writeSequence�s����
	*�%5�	*��#�#�%��)�+�������$�$�W�h�7���,�,�.��:��	:�	����'�"�����������8�9����;��g�l�l�(C�D����� r)c���|j�}|j|t��}|jt	|j�j�t	|��y)zQ
        C{str()} on the listening port object includes the port number.
        N)r:r;r�assertIn�strr<r=ris   r"�test_strzUDPPortTestsMixin.test_strsK���#�#�%���$�$�W�.>�.@�A���
�
�c�$�,�,�.�-�-�.��D�	�:r)c���|j�}|j|t��}|jt	|j�j�t|��y)zR
        C{repr()} on the listening port object includes the port number.
        N)r:r;rr��reprr<r=r�ris   r"�	test_reprzUDPPortTestsMixin.test_reprsK���#�#�%���$�$�W�.>�.@�A���
�
�d�4�<�<�>�.�.�/��T��;r)c���	�
�|j��	t��
tj�x}�
_|j�	�
d��t
��tj�x}�_|j�	�d���jj�}��
fd�}�	fd�}tj||g�}|j|�|j|�|jt�|j�	��
jd}|j|d|j |j"ff�y)zS
        Writing to an IPv6 UDP socket on the loopback interface succeeds.
        rrtc����jjdd�jj�jf�t	j
�x}�_|S)��
            Send a datagram from the client once it's started.

            @param ignored: a list of C{[None, None]}, which is ignored
            @returns: a deferred which fires when the server has received a
                datagram.
            �spamr)rZr�r<r=rr	�packetReceived�rK�serverReceived�client�servers  ��r"�cbClientStartedzDUDPPortTestsMixin.test_writeToIPv6Interface.<locals>.cbClientStartedsO���
���"�"�7�U�F�4D�4D�4L�4L�4N�4S�4S�,T�U�5:�^�^�5E�E�N�V�2�!�!r)c�&���j�y�z�
            Stop the reactor after a datagram is received.

            @param ignored: L{None}, which is ignored
            @returns: L{None}
            NrHrJs �r"�cbServerReceivedzEUDPPortTestsMixin.test_writeToIPv6Interface.<locals>.cbServerReceived+s���
�L�L�Nr)rr�N�r:rrr	�startedDeferredr;rrZr<�
gatherResultsrOr�rrR�packetsr>rpr=�r2�
serverStarted�
clientStarted�cAddrr�r�r��packetr�r@r�s        @@@r"�test_writeToIPv6Interfacez+UDPPortTestsMixin.test_writeToIPv6Interfaces	���
�#�#�%�����16���1A�A�
��.����g�v���?����16���1A�A�
��.����g�v���?�� � �(�(�*��
	"�	�
����
� >�?��	�
�
�o�&�	�
�
�&�'�	���S������ �����"������'�E�J�J��
�
�+C�!D�Er)c���	�
�|j��	t��
tj�x}�
_|j�	�
d��t
��tj�x}�_|j�	�d���jj�}��
fd�}�	fd�}tj||g�}|j|�|j|�|jt�|j�	��
jd}|j|d|j |j"ff�y)z�
        An IPv6 address can be passed as the C{interface} argument to
        L{listenUDP}. The resulting Port accepts IPv6 datagrams.
        rrtc�����jjd�jj�j��jj	d�tj�x}�_|S)r�rr�)rZ�connectr<r=r�rr	r�r�s  ��r"r�zMUDPPortTestsMixin.test_connectedWriteToIPv6Interface.<locals>.cbClientStartedMs^���
���$�$�U�F�,<�,<�,D�,D�,F�,K�,K�L����"�"�7�+�5:�^�^�5E�E�N�V�2�!�!r)c�&���j�yr�rHrJs �r"r�zNUDPPortTestsMixin.test_connectedWriteToIPv6Interface.<locals>.cbServerReceived[s���
�L�L�Nr)rr�Nr�r�s        @@@r"�"test_connectedWriteToIPv6Interfacez4UDPPortTestsMixin.test_connectedWriteToIPv6Interface=s	����#�#�%�����16���1A�A�
��.����g�v���?����16���1A�A�
��.����g�v���?�� � �(�(�*��	"�	�
����
� >�?��	�
�
�o�&�	�
�
�&�'�	���S������ �����"������'�E�J�J��
�
�+C�!D�Er)c��|j�}|j|t��}|jtj
|jdd�y)zn
        Writing to a hostname instead of an IP address will raise an
        L{InvalidAddressError}.
        �spam)�example.invalid�N�r:r;rryrrzr�ris   r"�/test_writingToHostnameRaisesInvalidAddressErrorzAUDPPortTestsMixin.test_writingToHostnameRaisesInvalidAddressErrornsJ��
�#�#�%���$�$�W�.>�.@�A������%�%�t�z�z�6�;Q�	
r)c��|j�}|j|t�d��}|jtj
|jdd�y)�l
        Writing to an IPv6 address on an IPv4 socket will raise an
        L{InvalidAddressError}.
        r�rtr�)rr�Nr�ris   r"�1test_writingToIPv6OnIPv4RaisesInvalidAddressErrorzCUDPPortTestsMixin.test_writingToIPv6OnIPv4RaisesInvalidAddressErrorysK���#�#�%���$�$�W�.>�.@�K�$�X�����%�3�3�T�Z�Z���Tr)c��|j�}|j|t�d��}|jtj
|jdd�y)r�rrtr�)r�r�Nr�ris   r"�1test_writingToIPv4OnIPv6RaisesInvalidAddressErrorzCUDPPortTestsMixin.test_writingToIPv4OnIPv6RaisesInvalidAddressError�sO���#�#�%���$�$�W�.>�.@�E�$�R������%�%�t�z�z�6�;K�	
r)c��|j�}|j|t��}|jtj
|jdd�y)zq
        Connecting to a hostname instead of an IP address will raise an
        L{InvalidAddressError}.
        r�r�N)r:r;rryrrzr�ris   r"�2test_connectingToHostnameRaisesInvalidAddressErrorzDUDPPortTestsMixin.test_connectingToHostnameRaisesInvalidAddressError�sH��
�#�#�%���$�$�W�.>�.@�A�����%�3�3�T�\�\�CT�VW�Xr)c��|j�}|j|t��}|jd�|j	|j��y)zk
        L{IListeningPort.setBroadcastAllowed} sets broadcast to be allowed
        on the socket.
        TN)r:r;r�setBroadcastAllowedrb�getBroadcastAllowedris   r"�test_allowBroadcastz%UDPPortTestsMixin.test_allowBroadcast�sK��
�#�#�%���$�$�W�.>�.@�A��� � ��&�����0�0�2�3r)N)r4r5r6rerjrrr(rwr|r�r�r�r�r�r�r�r�r�r�r�r0r)r"rgrg�s����
<�O��	1��	1�
�"!�H!�8;�<��*F��*F�X�.F��.F�`	
��U��U��	
��	
�Y�4r)rgc� �eZdZdZefZ	dd�Zy)�UDPServerTestsBuilderzM
    Run L{UDPPortTestsMixin} tests using newly created UDP
    sockets.
    c�,�|j||||��S)aB
        Get a UDP port from a reactor.

        @param reactor: A reactor used to build the returned
            L{IListeningPort} provider.
        @type reactor: L{twisted.internet.interfaces.IReactorUDP}

        @see: L{twisted.internet.IReactorUDP.listenUDP} for other
            argument and return types.
        )rm�
maxPacketSize)r{)r2r@rAr=rmr�s      r"r;z&UDPServerTestsBuilder.getListeningPort�s%��� � ��(�i�}�!�
�	
r)N�r�i )r4r5r6rer�requiredInterfacesr;r0r)r"r�r��s���
&���FJ�
r)r�c� �eZdZdZefZ	dd�Zy)�UDPFDServerTestsBuilderzC
    Run L{UDPPortTestsMixin} tests using adopted UDP sockets.
    c�<�tj|�r�d|vr-tj}tj||�dd}ntj
}||f}tj|tj�}|j|�|jd�	|j|j�|j||�|j�|j�Std��#|j�|j�wxYw)a�
        Get a UDP port from a reactor, wrapping an already-initialized file
        descriptor.

        @param reactor: A reactor used to build the returned
            L{IListeningPort} provider.
        @type reactor: L{twisted.internet.interfaces.IReactorSocket}

        @param port: A port number to which the adopted socket will be
            bound.
        @type port: C{int}

        @param interface: The local IPv4 or IPv6 address to which the
            adopted socket will be bound.  defaults to '', ie all IPv4
            addresses.
        @type interface: C{str}

        @see: L{twisted.internet.IReactorSocket.adoptDatagramPort} for other
            argument and return types.
        �:r�Fz'Reactor does not provide IReactorSocket)r
�
providedByrr�getaddrinfo�AF_INETror�setblocking�adoptDatagramPort�fileno�familyrr)	r2r@rAr=rmr��domainr��portSocks	         r"r;z(UDPFDServerTestsBuilder.getListeningPort�s���.�$�$�W�-��i������ �,�,�Y��=�a�@��C������$�d�+���}�}�V�V�->�->�?�H��M�M�'�"�� � ��'�

!��0�0��O�O�%�x����-�����!���� ��D�E�E�����!���� �s�"+C9�9"DNr�)r4r5r6rer
r�r;r0r)r"r�r��s���)�*��FJ�-Fr)r�)/rer�zope.interfacer�zope.interface.verifyr�twisted.internetrr�twisted.internet.addressrr�twisted.internet.deferr	r
�twisted.internet.interfacesrrr
r�twisted.internet.protocolr�&twisted.internet.test.connectionmixinsrr�#twisted.internet.test.reactormixinsr�twisted.pythonr�twisted.python.logrr�twisted.test.test_udprr�twisted.trial.unittestrr#r%r(r+rgr�r��globals�update�makeTestCaseClassesr0r)r"�<module>r�s�����&�.�)�=�:���7�Q�>�"�/�4�+��"�;��
�M2�"2�M2�`U4�U4�p
��%�'B�
�86F��%�'B�6F�r�	���&�:�:�<�=��	���(�<�<�>�?r)

Zerion Mini Shell 1.0