%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_resolver.cpython-312.pyc

�

Ϫ�f�L����dZddlmZddlmZmZmZmZmZm	Z	m
Z
mZmZddl
mZmZddlmZddlmZddlmZmZmZddlmZmZmZdd	lmZmZdd
lm Z m!Z!ddl"m#Z#ddl$m%Z%dd
l&m'Z'm(Z(m)Z)m*Z*ddl+m,Z,ddl-m.Z/Gd�de,�Z0d�Z1d�Z2Gd�d�Z3ee)�Gd�d��Z4Gd�de/�Z5Gd�de/�Z6ee*�Gd�d��Z7Gd�de/�Z8Gd �d!e!�Z9Gd"�d#e/�Z:y$)%zh
Tests for implementations of L{IHostnameResolver} and their interactions with
reactor implementations.
�)�defaultdict)	�AF_INET�AF_INET6�	AF_UNSPEC�
EAI_NONAME�IPPROTO_TCP�
SOCK_DGRAM�SOCK_STREAM�gaierror�getaddrinfo)�Lock�local)�implementer)�verifyObject)�
LockWorker�Team�createMemoryWorker)�ComplexResolverSimplifier�GAIResolver�SimpleResolverComplexifier)�IPv4Address�IPv6Address)�PluggableResolverMixin�ReactorBase)�Deferred)�DNSLookupError)�IHostnameResolver�IReactorPluggableNameResolver�IResolutionReceiver�IResolverSimple)�
ThreadPool)�SynchronousTestCasec��eZdZdZd�Zy)�DeterministicThreadPoolz6
    Create a deterministic L{ThreadPool} object.
    c�J�d|_d|_d|_g|_||_y)zE
        Create a L{DeterministicThreadPool} from a L{Team}.
        �N)�min�max�name�threads�_team)�self�teams  �E/usr/lib/python3/dist-packages/twisted/internet/test/test_resolver.py�__init__z DeterministicThreadPool.__init__4s'����������	������
�N)�__name__�
__module__�__qualname__�__doc__r/�r0r.r$r$/s���r0r$c	���t�\�}tttt	�t���fd�d���|fS)z�
    Create a deterministic threadpool.

    @return: 2-tuple of L{ThreadPool}, 0-argument C{work} callable; when
        C{work} is called, do the work.
    c����S�Nr5��workers�r.�<lambda>z#deterministicPool.<locals>.<lambda>Is���v�r0c��yr8r5r5r0r.r;z#deterministicPool.<locals>.<lambda>I��r0)rr$rrr
r)�doerr:s @r.�deterministicPoolr??s?���&�'�L�F�D����D�F�E�G�,�~��M�	
�	
�	�r0c�H��t�\�}G�fd�d�}|�|fS)z�
    Create a deterministic L{IReactorThreads}

    @return: a 2-tuple consisting of an L{IReactorThreads}-like object and a
        0-argument callable that will perform one unit of work invoked via that
        object's C{callFromThread} method.
    c���eZdZ�fd�Zy)�(deterministicReactorThreads.<locals>.CFTc�8������j���fd��y)Nc�����i���Sr8r5)�a�f�ks���r.r;zIdeterministicReactorThreads.<locals>.CFT.callFromThread.<locals>.<lambda>[s���a��j�a�j�r0)�do)r,rFrErGr:s ```�r.�callFromThreadz7deterministicReactorThreads.<locals>.CFT.callFromThreadZs����I�I�(�)r0N)r1r2r3rIr9s�r.�CFTrBYs���	*r0rJ)r)r>rJr:s  @r.�deterministicReactorThreadsrKOs)���&�'�L�F�D�*�*��5�$�;�r0c�.�eZdZdZd�Zdd�Zeeedfd�Z	y)�FakeAddrInfoGetterz/
    Test object implementing getaddrinfo.
    c�:�g|_tt�|_y)z1
        Create a L{FakeAddrInfoGetter}.
        N)�callsr�list�results�r,s r.r/zFakeAddrInfoGetter.__init__es����
�"�4�(��r0c��|jj||||||f�|j|}|r|Sttd��)a�
        Mock for L{socket.getaddrinfo}.

        @param host: see L{socket.getaddrinfo}

        @param port: see L{socket.getaddrinfo}

        @param family: see L{socket.getaddrinfo}

        @param socktype: see L{socket.getaddrinfo}

        @param proto: see L{socket.getaddrinfo}

        @param flags: see L{socket.getaddrinfo}

        @return: L{socket.getaddrinfo}
        z,nodename nor servname provided, or not known)rO�appendrQrr)r,�host�port�family�socktype�proto�flagsrQs        r.rzFakeAddrInfoGetter.getaddrinfolsG��$	
�
�
���4��v�x���F�G��,�,�t�$����N��:�'U�V�Vr0r0c�J�|j|j|||||f�y)a�
        Add a result for a given hostname.  When this hostname is resolved, the
        result will be a L{list} of all results C{addResultForHost} has been
        called with using that hostname so far.

        @param host: The hostname to give this result for.  This will be the
            next result from L{FakeAddrInfoGetter.getaddrinfo} when passed this
            host.

        @type canonname: native L{str}

        @param sockaddr: The resulting socket address; should be a 2-tuple for
            IPv4 or a 4-tuple for IPv6.

        @param family: An C{AF_*} constant that will be returned from
            C{getaddrinfo}.

        @param socktype: A C{SOCK_*} constant that will be returned from
            C{getaddrinfo}.

        @param proto: An C{IPPROTO_*} constant that will be returned from
            C{getaddrinfo}.

        @param canonname: A canonical name that will be returned from
            C{getaddrinfo}.
        @type canonname: native L{str}
        N)rQrT)r,rU�sockaddrrWrXrY�	canonnames       r.�addResultForHostz#FakeAddrInfoGetter.addResultForHost�s'��H	
���T��!�!�6�8�U�I�x�"P�Qr0N)rrrr)
r1r2r3r4r/rrr
rr^r5r0r.rMrM`s&���)�W�:����$Rr0rMc�0�eZdZdZdZdZd�Zd�Zd�Zd�Z	y)�ResultHolderzI
    A resolution receiver which holds onto the results it received.
    Fc��||_y)z>
        Create a L{ResultHolder} with a L{UnitTest}.
        N)�	_testCase)r,�testCases  r.r/zResultHolder.__init__�s��"��r0c�.�d|_||_g|_y)zg
        Hostname resolution began.

        @param hostResolution: see L{IResolutionReceiver}
        TN)�_started�_resolution�
_addresses)r,�hostResolutions  r.�resolutionBeganzResultHolder.resolutionBegan�s����
�)�����r0c�:�|jj|�y)z^
        An address was resolved.

        @param address: see L{IResolutionReceiver}
        N)rgrT)r,�addresss  r.�addressResolvedzResultHolder.addressResolved�s��	
�����w�'r0c��d|_y)z2
        Hostname resolution is complete.
        TN)�_endedrRs r.�resolutionCompletezResultHolder.resolutionComplete�s����r0N)
r1r2r3r4rernr/rirlror5r0r.r`r`�s'����H�
�F�"��(�r0r`c��eZdZdZd�Zy)�HelperTestsz?
    Tests for error cases of helpers used in this module.
    c���t�\|_|_d�}|jj|�|j�|j	t|j
t��d�y)zq
        L{DeterministicThreadPool} will log any exceptions that its "thread"
        workers encounter.
        c��ddzS)Nr&rr5r5r0r.�divideByZeroz9HelperTests.test_logErrorsInThreads.<locals>.divideByZero�s���q�5�Lr0r&N)r?�pool�doThreadWork�callInThread�assertEqual�len�flushLoggedErrors�ZeroDivisionError)r,rts  r.�test_logErrorsInThreadsz#HelperTests.test_logErrorsInThreads�s\��
(9�':�$��	�4�$�	�	
�	�	���|�,���������T�3�3�4E�F�G��Kr0N)r1r2r3r4r|r5r0r.rqrq�s
���Lr0rqc�L�eZdZdZd�Zd�Zd�Zd�Zd�Zd�Z	d�Z
d	�Zd
�Zd�Z
y)
�HostnameResolutionTestsz(
    Tests for hostname resolution.
    c����t�\�_�_t�\�_�_t
��_t�j�fd��jj��_
y)z*
        Set up a L{GAIResolver}.
        c����jSr8�rurRs�r.r;z/HostnameResolutionTests.setUp.<locals>.<lambda>�����$�)�)�r0N)r?rurvrK�reactor�
doReactorWorkrM�getterrr�resolverrRs`r.�setUpzHostnameResolutionTests.setUp�sU���(9�':�$��	�4�$�+F�+H�(���d�(�(�*���#��L�L�+�T�[�[�-D�-D�
��
r0c��t|�}|jjdd�|jj	|d�}|j|j|�|j|jd�|j|jd�|j�|j�|j|jd�|j|jtddd�g�y)	z�
        Resolving an individual hostname that results in one address from
        getaddrinfo results in a single call each to C{resolutionBegan},
        C{addressResolved}, and C{resolutionComplete}.
        �sample.example.com)�4.3.2.1rTF�TCPr�rN)r`r�r^r��resolveHostName�assertIsrfrxrernrvr�rgr�r,�receiver�
resolutions   r.�test_resolveOneHostz+HostnameResolutionTests.test_resolveOneHost�s��� ��%�����$�$�%9�>�J��]�]�2�2�8�=Q�R�
��
�
�h�*�*�J�7�����*�*�D�1�������%�0���������������$�/�����,�,�{�5�)�Q�/O�.P�Qr0c
��t|�}d}d}|jjddd||ft��|jj|d�}|j
|j|�|j|jd�|j|jd�|j�|j�|j|jd�|j|jtd	dd||�g�y
)a
        Resolving an individual hostname that results in one address from
        getaddrinfo results in a single call each to C{resolutionBegan},
        C{addressResolved}, and C{resolutionComplete}; C{addressResolved} will
        receive an L{IPv6Address}.
        r&�r��::1r)rWTFr�N)r`r�r^rr�r�r�rfrxrernrvr�rgr)r,r��flowInfo�scopeIDr�s     r.�test_resolveOneIPv6Hostz/HostnameResolutionTests.test_resolveOneIPv6Host	s��� ��%���������$�$� �5�!�X�w�"?��	%�	
��]�]�2�2�8�=Q�R�
��
�
�h�*�*�J�7�����*�*�D�1�������%�0���������������$�/�������+�e�U�A�x��"Q�!R�	
r0c�r�t|�}|jj|d�}|j|j|�|j�|j
�|j|jd�|j|jd�|j|jg�y)a

        Resolving a hostname that results in C{getaddrinfo} raising a
        L{gaierror} will result in the L{IResolutionReceiver} receiving a call
        to C{resolutionComplete} with no C{addressResolved} calls in between;
        no failure is logged.
        r�TN)r`r�r�r�rfrvr�rxrernrgr�s   r.�
test_gaierrorz%HostnameResolutionTests.test_gaierror!s��� ��%���]�]�2�2�8�=Q�R�
��
�
�h�*�*�J�7�������������*�*�D�1�������$�/�����,�,�b�1r0c�2�t|�}|jj|d|��}|j|j|�|j�|j
�|jjd\}}}}}	}
|j||�y)z�
        Verify that the given set of address types results in the given C{AF_}
        constant being passed to C{getaddrinfo}.

        @param addrTypes: iterable of L{IAddress} implementers

        @param expectedAF: an C{AF_*} constant
        r�)�addressTypesrN)
r`r�r�r�rfrvr�r�rOrx)r,�	addrTypes�
expectedAFr�r�rUrVrWrXrYrZs           r.�_resolveOnlyTestz(HostnameResolutionTests._resolveOnlyTest1s��� ��%���]�]�2�2��*��3�
�
�	
�
�
�h�*�*�J�7���������59�[�[�5F�5F�q�5I�2��d�F�H�e�U������,r0c�:�|jtgt�y)z�
        When passed an C{addressTypes} parameter containing only
        L{IPv4Address}, L{GAIResolver} will pass C{AF_INET} to C{getaddrinfo}.
        N)r�rrrRs r.�test_resolveOnlyIPv4z,HostnameResolutionTests.test_resolveOnlyIPv4Ds��
	
���{�m�W�5r0c�:�|jtgt�y)z�
        When passed an C{addressTypes} parameter containing only
        L{IPv6Address}, L{GAIResolver} will pass C{AF_INET6} to C{getaddrinfo}.
        N)r�rrrRs r.�test_resolveOnlyIPv6z,HostnameResolutionTests.test_resolveOnlyIPv6Ks��
	
���{�m�X�6r0c�p�|jttgt�|jdt�y)z�
        When passed an C{addressTypes} parameter containing both L{IPv4Address}
        and L{IPv6Address} (or the default of C{None}, which carries the same
        meaning), L{GAIResolver} will pass C{AF_UNSPEC} to C{getaddrinfo}.
        N)r�rrrrRs r.�test_resolveBothz(HostnameResolutionTests.test_resolveBothRs)��	
���{�K�8�)�D����d�I�.r0c��t|�}|jj|dd��t|�}|jj|dd��|j�|j	�|j�|j	�|j
jd\}}}}}}|j
jd\}}}}	}}|j|t�|j|	t�y)z�
        When passed a C{transportSemantics} paramter, C{'TCP'} (the value
        present in L{IPv4Address.type} to indicate a stream transport) maps to
        C{SOCK_STREAM} and C{'UDP'} maps to C{SOCK_DGRAM}.
        �example.comr�)�transportSemantics�UDPrr&N)
r`r�r�rvr�r�rOrxr
r	)
r,r��	receiver2rUrVrW�	socktypeTrYrZ�	socktypeUs
          r.�#test_transportSemanticsToSocketTypez;HostnameResolutionTests.test_transportSemanticsToSocketType[s��� ��%���
�
�%�%�h�
�RW�%�X� ��&�	��
�
�%�%��}��	&�	
�	
����������������6:�k�k�6G�6G��6J�3��d�F�I�u�e�6:�k�k�6G�6G��6J�3��d�F�I�u�e�����K�0�����J�/r0c�R�t|�}d}d}ttfD]L}|jj	ddd||ft
|��|jj	ddt|���N|jj|d�|j�|j�|j\}}}}|j|jd�|j|jd�|j|jd	�|j|jd	�y
)z�
        When L{GAIResolver} receives a C{SOCK_DGRAM} result from
        C{getaddrinfo}, it returns a C{'TCP'} L{IPv4Address} or L{IPv6Address};
        if it receives C{SOCK_STREAM} then it returns a C{'UDP'} type of same.
        r&r�r�r�r)rWrX)z	127.0.0.3rr�r�N)r`r
r	r�r^rrr�r�rvr�rgrx�type)	r,r�r�r�rX�stream4�stream6�dgram4�dgram6s	         r.�test_socketTypeToAddressTypez4HostnameResolutionTests.test_socketTypeToAddressTypeps
�� ��%������#�Z�/�		�H��K�K�(�(����8�W�-��!�	
)�
�
�K�K�(�(��/��(�
)�
�		�	
�
�
�%�%�h�
�>���������+3�+>�+>�(���&�&�������u�-�������u�-�������e�,�������e�,r0N)r1r2r3r4r�r�r�r�r�r�r�r�r�r�r5r0r.r~r~�s:���	
�R�"
�02� -�&6�7�/�0�*-r0r~c��eZdZdZd�Zdd�Zy)�SillyResolverSimplez6
    Trivial implementation of L{IResolverSimple}
    c��g|_y)zd
        Create a L{SillyResolverSimple} with a queue of requests it is working
        on.
        N)�	_requestsrRs r.r/zSillyResolverSimple.__init__�s��
��r0c�f�|jjt��|jdS)z�
        Implement L{IResolverSimple.getHostByName}.

        @param name: see L{IResolverSimple.getHostByName}.

        @param timeout: see L{IResolverSimple.getHostByName}.

        @return: see L{IResolverSimple.getHostByName}.
        ���)r�rTr)r,r)�timeouts   r.�
getHostByNamez!SillyResolverSimple.getHostByName�s'��	
�����h�j�)��~�~�b�!�!r0N)r5)r1r2r3r4r/r�r5r0r.r�r��s����"r0r�c�.�eZdZdZd�Zd�Zd�Zd�Zd�Zy)�LegacyCompatibilityTestsz�
    Older applications may supply an object to the reactor via
    C{installResolver} that only provides L{IResolverSimple}.
    L{SimpleResolverComplexifier} is a wrapper for an L{IResolverSimple}.
    c��t�}t|�}t|�}|j|jd�|j|d�|j|jd�|j|jd�|j|jg�|jdjd�|j|jtddd�g�|j|jd�y)z�
        L{SimpleResolverComplexifier} translates C{resolveHostName} into
        L{IResolutionReceiver.addressResolved}.
        Fr�Tr�192.168.1.1r�N)r�rr`rxrer�rnrgr��callbackr�r,�simple�complexr�s    r.�test_successz%LegacyCompatibilityTests.test_success�s���
%�&��,�V�4����%������*�*�E�2�����-�8�����*�*�D�1�������%�0�����,�,�b�1������$�$�]�3�����,�,�{�5�-�QR�/S�.T�U�������$�/r0c��t�}t|�}t|�}|j|jd�|j|d�|j|jd�|j|jd�|j|jg�|jdjtd��|j|jd�|j|jg�y)z�
        L{SimpleResolverComplexifier} translates a known error result from
        L{IResolverSimple.resolveHostName} into an empty result.
        Fr�Tr�nopeN)r�rr`rxrer�rnrgr��errbackrr�s    r.�test_failurez%LegacyCompatibilityTests.test_failure�s���
%�&��,�V�4����%������*�*�E�2�����-�8�����*�*�D�1�������%�0�����,�,�b�1������#�#�N�6�$:�;�������$�/�����,�,�b�1r0c�b�t�}t|�}t|�}|j|jd�|j|d�|j|jd�|j|jd�|j|jg�|jdjtd��|jt|jt��d�|j|jd�|j|jg�y)z�
        L{SimpleResolverComplexifier} translates an unknown error result from
        L{IResolverSimple.resolveHostName} into an empty result and a logged
        error.
        Fr�Tr�zowr&N)
r�rr`rxrer�rnrgr�r�r{ryrzr�s    r.�
test_errorz#LegacyCompatibilityTests.test_error�s���%�&��,�V�4����%������*�*�E�2�����-�8�����*�*�D�1�������%�0�����,�,�b�1������#�#�$5�e�$<�=�����T�3�3�4E�F�G��K�������$�/�����,�,�b�1r0c���t�\�_�_t�\�_�_t
��_t�j�fd��jj��_
t�j�}�jjdd�|jd�}|jd�}�j��j��j��j��j�j|�j t"��j�j%|�d�y)z�
        L{ComplexResolverSimplifier} translates an L{IHostnameResolver} into an
        L{IResolverSimple} for applications that still expect the old
        interfaces to be in place.
        c����jSr8r�rRs�r.r;z:LegacyCompatibilityTests.test_simplifier.<locals>.<lambda>�r�r0r�)�192.168.3.4��znx.example.comr�N)r?rurvrKr�r�rMr�rrr�rr^r�rx�failureResultOfr�r�successResultOf)r,�simpleResolver�success�failures`   r.�test_simplifierz(LegacyCompatibilityTests.test_simplifier�s���(9�':�$��	�4�$�+F�+H�(���d�(�(�*���#��L�L�+�T�[�[�-D�-D�
��
�3�4�=�=�A�����$�$�]�4I�J� �.�.�}�=�� �.�.�/?�@����������������������-�-�g�6�;�;�^�L�����-�-�g�6�
�Fr0c���t�}t|�}t|�}|j|dd�|j	|j
d�|j	|jd�|j	|jg�|jdjd�|j	|jtddd�g�|j	|jd�y)	z�
        L{SimpleResolverComplexifier} preserves the C{port} argument passed to
        C{resolveHostName} in its returned addresses.
        r�r�TFrr�r�N)r�rr`r�rxrernrgr�r�rr�s    r.�test_portNumberz(LegacyCompatibilityTests.test_portNumber�s���
%�&��,�V�4����%������-��>�����*�*�D�1�������%�0�����,�,�b�1������$�$�]�3�����,�,�{�5�-�QU�/V�.W�X�������$�/r0N)	r1r2r3r4r�r�r�r�r�r5r0r.r�r��s!���0�"2�"2�&G�.0r0r�c��eZdZdZd�Zy)�JustEnoughReactorzT
    Just enough subclass implementation to be a valid L{ReactorBase} subclass.
    c��y)z
        Do nothing.
        Nr5rRs r.�installWakerzJustEnoughReactor.installWakerr=r0N)r1r2r3r4r�r5r0r.r�r�s���r0r�c�"�eZdZdZd�Zd�Zd�Zy)�ReactorInstallationTestsz�
    Tests for installing old and new resolvers onto a
    L{PluggableResolverMixin} and L{ReactorBase} (from which all of Twisted's
    reactor implementations derive).
    c��t�}tt|�tt|j�tt
|j�y)z�
        L{PluggableResolverMixin} (and its subclasses) implement both
        L{IReactorPluggableNameResolver} and L{IReactorPluggableResolver}.
        N)rrrr r�r�nameResolver�r,r�s  r.�test_interfaceCompliancez1ReactorInstallationTests.test_interfaceCompliances8��
)�*���2�G�<��_�g�&6�&6�7��&��(<�(<�=r0c��t�}t�}tt|j	|��|j|jt�|j|jj|�y)zf
        L{PluggableResolverMixin} will wrap an L{IResolverSimple} in a
        complexifier.
        N)
rr�rr �installResolver�assertIsInstancer�rr��_simpleResolver)r,r��its   r.�test_installingOldStyleResolverz8ReactorInstallationTests.test_installingOldStyleResolver(sZ��
)�*��
 �
"���_�g�&=�&=�b�&A�B����g�2�2�4N�O��
�
�g�*�*�:�:�B�?r0c��t�}|j|jt�|j	|jj
t�|j|jt�|j	|jj|�|j	|jj|j�y)zD
        L{ReactorBase} defaults to using a L{GAIResolver}.
        N)r�r�r�rr��_getaddrinforr�r�_reactor�
_nameResolverr�s  r.�test_defaultToGAIResolverz2ReactorInstallationTests.test_defaultToGAIResolver3s���$�%�����g�2�2�K�@��
�
�g�*�*�7�7��E����g�.�.�0I�J��
�
�g�*�*�3�3�W�=��
�
�g�&�&�4�4�g�6J�6J�Kr0N)r1r2r3r4r�r�r�r5r0r.r�r�s���>�	@�	Lr0r�N);r4�collectionsr�socketrrrrrr	r
rr�	threadingr
r�zope.interfacer�zope.interface.verifyr�twisted._threadsrrr�twisted.internet._resolverrrr�twisted.internet.addressrr�twisted.internet.baserr�twisted.internet.deferr�twisted.internet.errorr�twisted.internet.interfacesrrrr �twisted.python.threadpoolr!�twisted.trial.unittestr"�UnitTestr$r?rKrMr`rqr~r�r�r�r�r5r0r.�<module>r�s���$�
�
�
�"�&�.�A�A���
>�E�+�1���1�B�
�j�
� 
� �"IR�IR�X
�
 �!�$�$�"�$�NL�(�L�(b-�h�b-�J
�_��"�"��"�4a0�x�a0�H���%L�x�%Lr0

Zerion Mini Shell 1.0