HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //usr/lib/python3/dist-packages/twisted/internet/test/__pycache__/test_resolver.cpython-38.pyc
U


W[ON�@s�dZddlmZmZeZddlmZddlm	Z	m
Z
mZmZm
Z
mZmZmZmZddlmZmZddlmZddlmZddlmZmZmZmZdd	lmZ dd
l!m"Z"ddl#m$Z$m%Z%m&Z&ddl'm(Z(m)Z)dd
l*m+Z+m,Z,m-Z-ddl.m/Z/ddl0m1Z1ddl2m3Z3Gdd�de"e4�Z5dd�Z6dd�Z7Gdd�de4�Z8ee�Gdd�de4��Z9Gdd�de �Z:Gdd�de �Z;ee�Gdd �d e4��Z<Gd!d"�d"e e4�Z=Gd#d$�d$e3e4�Z>Gd%d&�d&e e4�Z?d'S)(zh
Tests for implementations of L{IHostnameResolver} and their interactions with
reactor implementations.
�)�division�absolute_import)�defaultdict)	�getaddrinfo�gaierror�
EAI_NONAME�AF_INET�AF_INET6�	AF_UNSPEC�SOCK_STREAM�
SOCK_DGRAM�IPPROTO_TCP)�local�Lock)�implementer)�verifyObject)�IResolutionReceiver�IResolverSimple�IReactorPluggableNameResolver�IHostnameResolver)�SynchronousTestCase)�
ThreadPool)�createMemoryWorker�Team�
LockWorker)�IPv4Address�IPv6Address)�GAIResolver�SimpleResolverComplexifier�ComplexResolverSimplifier)�Deferred)�DNSLookupError)�ReactorBasec@seZdZdZdd�ZdS)�DeterministicThreadPoolz6
    Create a deterministic L{ThreadPool} object.
    cCs"d|_d|_d|_g|_||_dS)zE
        Create a L{DeterministicThreadPool} from a L{Team}.
        �N)�min�max�nameZthreadsZ_team)�selfZteam�r)�E/usr/lib/python3/dist-packages/twisted/internet/test/test_resolver.py�__init__2s
z DeterministicThreadPool.__init__N)�__name__�
__module__�__qualname__�__doc__r+r)r)r)r*r#.sr#cs4t�\�}tttt�t���fdd�dd���|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.
    cs�S�Nr)r)�Zworkerr)r*�<lambda>H�z#deterministicPool.<locals>.<lambda>cSsdSr0r)r)r)r)r*r2Hr3)rr#rrrr)�doerr)r1r*�deterministicPool>s

��r5cs(t�\�}G�fdd�dt�}|�|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.
    cseZdZ�fdd�ZdS)z(deterministicReactorThreads.<locals>.CFTcs�����fdd��dS)Ncs
����Sr0r)r)��a�f�kr)r*r2Yr3zIdeterministicReactorThreads.<locals>.CFT.callFromThread.<locals>.<lambda>)Zdo)r(r8r7r9r1r6r*�callFromThreadXsz7deterministicReactorThreads.<locals>.CFT.callFromThreadN)r,r-r.r:r)r1r)r*�CFTWsr;)r�object)r4r;r)r1r*�deterministicReactorThreadsNs
r=c@s4eZdZdZdd�Zddd�Zeeedfdd	�Z	d
S)�FakeAddrInfoGetterz/
    Test object implementing getaddrinfo.
    cCsg|_tt�|_dS)z1
        Create a L{FakeAddrInfoGetter}.
        N)�callsr�list�results�r(r)r)r*r+cszFakeAddrInfoGetter.__init__rcCs8|j�||||||f�|j|}|r*|Sttd��dS)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 knownN)r?�appendrArr)r(�host�port�family�socktype�proto�flagsrAr)r)r*rks
�zFakeAddrInfoGetter.getaddrinfor3cCs|j|�|||||f�dS)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)rArC)r(rDZsockaddrrFrGrHZ	canonnamer)r)r*�addResultForHost�s
�z#FakeAddrInfoGetter.addResultForHostN)rrrr)
r,r-r.r/r+rrrr
rJr)r)r)r*r>^s
�r>c@s8eZdZdZdZdZdd�Zdd�Zdd�Zd	d
�Z	dS)�ResultHolderzI
    A resolution receiver which holds onto the results it received.
    FcCs
||_dS)z>
        Create a L{ResultHolder} with a L{UnitTest}.
        N)Z	_testCase)r(ZtestCaser)r)r*r+�szResultHolder.__init__cCsd|_||_g|_dS)zg
        Hostname resolution began.

        @param hostResolution: see L{IResolutionReceiver}
        TN)�_started�_resolution�
_addresses)r(ZhostResolutionr)r)r*�resolutionBegan�szResultHolder.resolutionBegancCs|j�|�dS)z^
        An address was resolved.

        @param address: see L{IResolutionReceiver}
        N)rNrC)r(Zaddressr)r)r*�addressResolved�szResultHolder.addressResolvedcCs
d|_dS)z2
        Hostname resolution is complete.
        TN)�_endedrBr)r)r*�resolutionComplete�szResultHolder.resolutionCompleteN)
r,r-r.r/rLrQr+rOrPrRr)r)r)r*rK�s	rKc@seZdZdZdd�ZdS)�HelperTestsz?
    Tests for error cases of helpers used in this module.
    cCsDt�\|_|_dd�}|j�|�|��|�t|�t��d�dS)zq
        L{DeterministicThreadPool} will log any exceptions that its "thread"
        workers encounter.
        cSsddS)Nr$rr)r)r)r)r*�divideByZero�sz9HelperTests.test_logErrorsInThreads.<locals>.divideByZeror$N)r5�pool�doThreadWorkZcallInThread�assertEqual�len�flushLoggedErrors�ZeroDivisionError)r(rTr)r)r*�test_logErrorsInThreads�s
z#HelperTests.test_logErrorsInThreadsN)r,r-r.r/r[r)r)r)r*rS�srSc@s`eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dS)�HostnameResolutionTestsz(
    Tests for hostname resolution.
    csDt�\�_�_t�\�_�_t��_t�j�fdd��jj	��_
dS)z*
        Set up a L{GAIResolver}.
        cs�jSr0�rUr)rBr)r*r2�r3z/HostnameResolutionTests.setUp.<locals>.<lambda>N)r5rUrVr=�reactor�
doReactorWorkr>�getterrr�resolverrBr)rBr*�setUp�s�zHostnameResolutionTests.setUpcCs�t|�}|j�dd�|j�|d�}|�|j|�|�|jd�|�|j	d�|�
�|��|�|j	d�|�|jt
ddd�g�dS)	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�TCPrdrN)rKr`rJra�resolveHostName�assertIsrMrWrLrQrVr_rNr�r(�receiver�
resolutionr)r)r*�test_resolveOneHost�s��z+HostnameResolutionTests.test_resolveOneHostc	Cs�t|�}d}d}|jjddd||ftd�|j�|d�}|�|j|�|�|j	d�|�|j
d�|��|��|�|j
d�|�|j
td	dd||�g�d
S)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$�rc�::1r)rFTFreN)rKr`rJr	rarfrgrMrWrLrQrVr_rNr)r(ri�flowInfo�scopeIDrjr)r)r*�test_resolveOneIPv6Hosts&
���z/HostnameResolutionTests.test_resolveOneIPv6HostcCsbt|�}|j�|d�}|�|j|�|��|��|�|jd�|�|j	d�|�|j
g�dS)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.
        rcTN)rKrarfrgrMrVr_rWrLrQrNrhr)r)r*�
test_gaierror%s�z%HostnameResolutionTests.test_gaierrorcCs`t|�}|jj|d|d�}|�|j|�|��|��|jjd\}}}}}	}
|�	||�dS)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
        rc)ZaddressTypesrN)
rKrarfrgrMrVr_r`r?rW)r(Z	addrTypesZ
expectedAFrirjrDrErFrGrHrIr)r)r*�_resolveOnlyTest7s	�z(HostnameResolutionTests._resolveOnlyTestcCs|�tgt�dS)z�
        When passed an C{addressTypes} parameter containing only
        L{IPv4Address}, L{GAIResolver} will pass C{AF_INET} to C{getaddrinfo}.
        N)rrrrrBr)r)r*�test_resolveOnlyIPv4Ksz,HostnameResolutionTests.test_resolveOnlyIPv4cCs|�tgt�dS)z�
        When passed an C{addressTypes} parameter containing only
        L{IPv6Address}, L{GAIResolver} will pass C{AF_INET6} to C{getaddrinfo}.
        N)rrrr	rBr)r)r*�test_resolveOnlyIPv6Ssz,HostnameResolutionTests.test_resolveOnlyIPv6cCs |�ttgt�|�dt�dS)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)rrrrr
rBr)r)r*�test_resolveBoth[sz(HostnameResolutionTests.test_resolveBothc
Cs�t|�}|jj|ddd�t|�}|jj|ddd�|��|��|��|��|jjd\}}}}}}|jjd\}}}}	}}|�|t�|�|	t	�dS)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.comre)ZtransportSemantics�UDPrr$N)
rKrarfrVr_r`r?rWrr)
r(riZ	receiver2rDrErFZ	socktypeTrHrIZ	socktypeUr)r)r*�#test_transportSemanticsToSocketTypees 
�
�z;HostnameResolutionTests.test_transportSemanticsToSocketTypec	Cs�t|�}d}d}ttfD]4}|jjddd||ft|d�|jjddt|d�q|j�|d�|�	�|�
�|j\}}}}|�|j
d�|�|j
d�|�|j
d	�|�|j
d	�d
S)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$rlrvrmr)rFrG)z	127.0.0.3rrerwN)rKrrr`rJr	rrarfrVr_rNrW�type)	r(rirnrorGZstream4Zstream6Zdgram4Zdgram6r)r)r*�test_socketTypeToAddressType{s0
��z4HostnameResolutionTests.test_socketTypeToAddressTypeN)r,r-r.r/rbrkrprqrrrsrtrurxrzr)r)r)r*r\�s
r\c@s"eZdZdZdd�Zddd�ZdS)	�SillyResolverSimplez6
    Trivial implementation of L{IResolverSimple}
    cCs
g|_dS)zd
        Create a L{SillyResolverSimple} with a queue of requests it is working
        on.
        N)�	_requestsrBr)r)r*r+�szSillyResolverSimple.__init__r)cCs|j�t��|jdS)z�
        Implement L{IResolverSimple.getHostByName}.

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

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

        @return: see L{IResolverSimple.getHostByName}.
        ���)r|rCr )r(r'Ztimeoutr)r)r*�
getHostByName�s
z!SillyResolverSimple.getHostByNameN)r))r,r-r.r/r+r~r)r)r)r*r{�sr{c@s8eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�ZdS)
�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}.
    cCs�t�}t|�}t|�}|�|jd�|�|d�|�|jd�|�|jd�|�|jg�|jd�	d�|�|jt
ddd�g�|�|jd�dS)z�
        L{SimpleResolverComplexifier} translates C{resolveHostName} into
        L{IResolutionReceiver.addressResolved}.
        FrvTr�192.168.1.1reN)r{rrKrWrLrfrQrNr|�callbackr�r(Zsimple�complexrir)r)r*�test_success�s�z%LegacyCompatibilityTests.test_successcCs�t�}t|�}t|�}|�|jd�|�|d�|�|jd�|�|jd�|�|jg�|jd�	t
d��|�|jd�|�|jg�dS)z�
        L{SimpleResolverComplexifier} translates a known error result from
        L{IResolverSimple.resolveHostName} into an empty result.
        FrvTrZnopeN)r{rrKrWrLrfrQrNr|�errbackr!r�r)r)r*�test_failure�sz%LegacyCompatibilityTests.test_failurecCs�t�}t|�}t|�}|�|jd�|�|d�|�|jd�|�|jd�|�|jg�|jd�	t
d��|�t|�t
��d�|�|jd�|�|jg�dS)z�
        L{SimpleResolverComplexifier} translates an unknown error result from
        L{IResolverSimple.resolveHostName} into an empty result and a logged
        error.
        FrvTrZzowr$N)
r{rrKrWrLrfrQrNr|r�rZrXrYr�r)r)r*�
test_error�sz#LegacyCompatibilityTests.test_errorcs�t�\�_�_t�\�_�_t��_t�j�fdd��jj	��_
t�j
�}�j�dd�|�
d�}|�
d�}����������������|�jt�����|�d�dS)z�
        L{ComplexResolverSimplifier} translates an L{IHostnameResolver} into an
        L{IResolverSimple} for applications that still expect the old
        interfaces to be in place.
        cs�jSr0r]r)rBr)r*r2�r3z:LegacyCompatibilityTests.test_simplifier.<locals>.<lambda>rv)�192.168.3.4��znx.example.comr�N)r5rUrVr=r^r_r>r`rrrarrJr~rWZfailureResultOfryr!ZsuccessResultOf)r(ZsimpleResolverZsuccessZfailurer)rBr*�test_simplifier�s �


z(LegacyCompatibilityTests.test_simplifiercCs�t�}t|�}t|�}|�|dd�|�|jd�|�|jd�|�|jg�|jd�	d�|�|jt
ddd�g�|�|jd�dS)	z�
        L{SimpleResolverComplexifier} preserves the C{port} argument passed to
        C{resolveHostName} in its returned addresses.
        rvr�TFrr�reN)r{rrKrfrWrLrQrNr|r�rr�r)r)r*�test_portNumbers�z(LegacyCompatibilityTests.test_portNumberN)	r,r-r.r/r�r�r�r�r�r)r)r)r*r�src@seZdZdZdd�ZdS)�JustEnoughReactorzT
    Just enough subclass implementation to be a valid L{ReactorBase} subclass.
    cCsdS)z
        Do nothing.
        Nr)rBr)r)r*�installWaker!szJustEnoughReactor.installWakerN)r,r-r.r/r�r)r)r)r*r�sr�c@s(eZdZdZdd�Zdd�Zdd�ZdS)	�ReactorInstallationTestsz�
    Tests for installing old and new resolvers onto a L{ReactorBase} (from
    which all of Twisted's reactor implementations derive).
    cCs,t�}tt|�tt|j�tt|j�dS)z�
        L{ReactorBase} (and its subclasses) provide both
        L{IReactorPluggableNameResolver} and L{IReactorPluggableResolver}.
        N)r�rrrrar�nameResolver�r(r^r)r)r*�test_interfaceCompliance.s
z1ReactorInstallationTests.test_interfaceCompliancecCsXt�}|�|jt�|�|jjt�|�|jt�|�|jj	|�|�|jj
|j�dS)zD
        L{ReactorBase} defaults to using a L{GAIResolver}.
        N)r��assertIsInstancer�rrgZ_getaddrinforrarZ_reactorZ
_nameResolverr�r)r)r*�test_defaultToGAIResolver9sz2ReactorInstallationTests.test_defaultToGAIResolvercCs>t�}t�}tt|�|��|�|jt�|�|jj	|�dS)zS
        L{ReactorBase} will wrap an L{IResolverSimple} in a complexifier.
        N)
r�r{rrZinstallResolverr�r�rrgZ_simpleResolver)r(r^�itr)r)r*�test_installingOldStyleResolverEs
z8ReactorInstallationTests.test_installingOldStyleResolverN)r,r-r.r/r�r�r�r)r)r)r*r�(sr�N)@r/Z
__future__rrryZ
__metaclass__�collectionsrZsocketrrrrr	r
rrr
Z	threadingrrZzope.interfacerZzope.interface.verifyrZtwisted.internet.interfacesrrrrZtwisted.trial.unittestrZUnitTestZtwisted.python.threadpoolrZtwisted._threadsrrrZtwisted.internet.addressrrZtwisted.internet._resolverrrrZtwisted.internet.deferr Ztwisted.internet.errorr!Ztwisted.internet.baser"r<r#r5r=r>rKrSr\r{rr�r�r)r)r)r*�<module>s<,L*1i