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/names/test/__pycache__/test_rootresolve.cpython-38.pyc
U


W[+d�
@s�dZddlmZddlmZddlmZddlmZddl	m
Z
mZddlm
Z
mZmZmZddlmZdd	lmZmZdd
lmZddlmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&ddl'm(Z(m)Z)dd
l*m+Z+dd�Z,dd�Z-Gdd�de�Z.Gdd�de/�Z0dd�Z1Gdd�de�Z2ddddddd d!d"d#d$d%d&g
Z3ee�Gd'd(�d(e4��Z5eee5�Gd)d*�d*e
�Z6Gd+d,�d,�Z7ej8e9d-d.�Z:d/S)0z.
Test cases for Twisted.names' root resolver.
�)�implementer)�verifyClass)�msg)�util)�SynchronousTestCase�TestCase)�Deferred�succeed�
gatherResults�TimeoutError)�IResolverSimple)�client�root)�Resolver)�IN�HS�A�NS�CNAME�OK�ENAME�Record_CNAME�Name�Query�Message�RRHeader�Record_A�	Record_NS)�DNSNameError�
ResolverError)�
MemoryReactorcCs|\}}}|djS)z�
    From the result of a L{Deferred} returned by L{IResolver.lookupAddress},
    return the payload of the first record in the answer section.
    r��payload��resultsZansZauth�add�r&�E/usr/lib/python3/dist-packages/twisted/names/test/test_rootresolve.py�
getOnePayloads
r(cCst|���S)z�
    From the result of a L{Deferred} returned by L{IResolver.lookupAddress},
    return the first IPv4 address from the answer section.
    )r(Z
dottedQuad�r$r&r&r'�
getOneAddress#sr*c@s�eZdZdZdd�Zdd�Zdd�Zgggefdd	�Zd&dd�Z	d
d�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%S)'�RootResolverTestsz3
    Tests for L{twisted.names.root.Resolver}.
    cCs�t�}tg|d�}|�tdtt�dgd|�}|j��\}}|j\\}}t	�}	|	�
|�|�|	jtdtt�g�|�|	j
g�|�|	jg�|�|	jg�g}
|�|
j�|�|
g�|	jdd�=d|	_|	j
�tdtd�d��|j�|	��d�|
d	S)
aK
        Invoke L{Resolver._query} and verify that it sends the correct DNS
        query.  Deliver a canned response to the query and return whatever the
        L{Deferred} returned by L{Resolver._query} fires with.

        @param filter: The value to pass for the C{filter} parameter to
            L{Resolver._query}.
        )�reactor�foo.example.com)�1.1.2.3i)�N��	5.8.13.21r!r)r r�_queryrrrZudpPorts�popitemZ_sentPacketsrZfromStr�assertEqual�queries�answers�	authority�
additional�addCallback�append�answerrrZ	_protocolZdatagramReceivedZtoStr)�self�filterr,�resolver�dZ
portNumberZ	transportZpacketZaddress�message�responser&r&r'�
_queryTest0s<	
�
��zRootResolverTests._queryTestc	CsJ|�d�\}}}|�|tdtddd�d�g�|�|g�|�|g�dS)a)
        L{Resolver._query} accepts a L{Query} instance and an address, issues
        the query, and returns a L{Deferred} which fires with the response to
        the query.  If a true value is passed for the C{filter} parameter, the
        result is a three-tuple of lists of records.
        Tr-r1r�Zttlr!N)rBr4rr)r<r;r7r8r&r&r'�test_filteredQuery\s�z$RootResolverTests.test_filteredQueryc	Csd|�d�}|�|t�|�|jg�|�|jtdtddd�d�g�|�|jg�|�|j	g�dS)z�
        Similar to L{test_filteredQuery}, but for the case where a false value
        is passed for the C{filter} parameter.  In this case, the result is a
        L{Message} instance.
        Fr-r1rrCr!N)
rB�assertIsInstancerr4r5r6rrr7r8)r<r@r&r&r'�test_unfilteredQueryks
�z&RootResolverTests.test_unfilteredQuerycCsHt|d�}|j|f|j|f|j|ffD]\}}|�dd�|D��q&|S)a�
        Create a L{Message} suitable for use as a response to a query.

        @param answers: A C{list} of two-tuples giving data for the answers
            section of the message.  The first element of each tuple is a name
            for the L{RRHeader}.  The second element is the payload.
        @param authority: A C{list} like C{answers}, but for the authority
            section of the response.
        @param additional: A C{list} like C{answers}, but for the
            additional section of the response.
        @param rCode: The response code the message will be created with.

        @return: A new L{Message} initialized with the given values.
        )�rCodec	Ss*g|]"\}}t||jt|dt�|d��qS)�CLASSr!)rZTYPE�getattrr)�.0�name�recordr&r&r'�
<listcomp>�s��z.RootResolverTests._respond.<locals>.<listcomp>)rr6r7r8�extend)r<r6r7r8rGrAZsection�datar&r&r'�_respond{s
�
�
zRootResolverTests._respond�
cs(dg}t||�}��fdd�}||_|S)a�
        Create and return a new L{root.Resolver} modified to resolve queries
        against the record data represented by C{servers}.

        @param serverResponses: A mapping from dns server addresses to
            mappings.  The inner mappings are from query two-tuples (name,
            type) to dictionaries suitable for use as **arguments to
            L{_respond}.  See that method for details.
        r.c	sjtd|j|f�|D]N}z�|}Wntk
r>YqYnX||jj|jf}t�jf|��SdS)NzQuery for QNAME %s at %r)rrK�KeyError�typer	rP)�queryZserverAddressesZtimeoutr=ZaddrZserverZrecords�r<�serverResponsesr&r'rT�s
z-RootResolverTests._getResolver.<locals>.query)rr2)r<rVZmaximumQueries�rootsr>rTr&rUr'�_getResolver�s


	zRootResolverTests._getResolvercCsrdtfdtd�fgdtd�fgd�idtfddtd�fgiid�}|�|�}|�d�}|�t�|�|jd�|S)a
        L{root.Resolver.lookupAddress} looks up the I{A} records for the
        specified hostname by first querying one of the root servers the
        resolver was created with and then following the authority delegations
        until a result is received.
        r-�ns1.example.com�34.55.89.144�r7r8r6�10.0.0.1)�r.�5)rZr^�rrrrX�
lookupAddressr9r*r4�r<�serversr>r?r&r&r'�test_lookupAddress�s 	�����


z$RootResolverTests.test_lookupAddresscCs�td�}t|_dtfd|fgdtd�fgdtd�fgd�idtfddtd�fgiid�}|�|�}|�d�}|�t�|�|j	td��|S)	z�
        If a response includes a record with a class different from the one
        in the query, it is ignored and lookup continues until a record with
        the right class is found.
        r\r-rY�10.0.0.2)r6r7r8r6�10.0.0.3)r]�rdr^)
rrrHrrrXr`r9r(r4)r<ZbadClassrbr>r?r&r&r'�test_lookupChecksClass�s&�����


z(RootResolverTests.test_lookupChecksClasscCs|dtfddtd�fgidtfddtd�fgiidtfddtd�fgiid�}|�|�}|�d�}|�t�|�|jd�|S)z�
        If an intermediate response includes no glue records for the
        authorities, separate queries are made to find those addresses.
        r-r7sns1.example.orgr6r\rd)r])r\r^r_rar&r&r'�test_missingGlue�s(���
���


z"RootResolverTests.test_missingGluecCs4ddtfdtiii}|�|�}|�d�}|�|t�S)z�
        If a name is missing, L{Resolver.lookupAddress} returns a L{Deferred}
        which fails with L{DNSNameError}.
        r]r-rG)rrrXr`�
assertFailurerrar&r&r'�test_missingNames���

z"RootResolverTests.test_missingNamecCs0ddtfiii}|�|�}|�d�}|�|t�S)z�
        If a query is responded to with no answers or nameserver records, the
        L{Deferred} returned by L{Resolver.lookupAddress} fires with
        L{ResolverError}.
        r]�example.com)rrXr`rirrar&r&r'�test_answerlesss��

z!RootResolverTests.test_answerlesscCsJddtfddtd�fgidtfdtiii}|�|�}|�d�}|�|t�S)z�
        If there is an error resolving the nameserver in a delegation response,
        the L{Deferred} returned by L{Resolver.lookupAddress} fires with that
        error.
        r]rkr7rYrG)rrrrXr`rirrar&r&r'�test_delegationLookupError%s����


z,RootResolverTests.test_delegationLookupErrorcCsFddtfddtd�fgidtfiii}|�|�}|�d�}|�|t�S)z�
        If there are no records in the response to a lookup of a delegation
        nameserver, the L{Deferred} returned by L{Resolver.lookupAddress} fires
        with L{ResolverError}.
        r]rkr7rY)rrrXr`rirrar&r&r'�test_delegationLookupEmpty:s���	

z,RootResolverTests.test_delegationLookupEmptycCsfddtfdtidtfddtd�fgiii}|�|�}|�d�}dd�}|�|�|�|jtd��|S)z�
        L{Resolver.lookupNameservers} is like L{Resolver.lookupAddress}, except
        it queries for I{NS} records instead of I{A} records.
        r]rkrGr6rYcSs|\}}}|djjS�Nr)r"rKr#r&r&r'�
getOneName_s
z<RootResolverTests.test_lookupNameservers.<locals>.getOneName)	rrrrrXZlookupNameserversr9r4r)r<rbr>r?rpr&r&r'�test_lookupNameserversNs"����



z(RootResolverTests.test_lookupNameserversc	Cs|ddtfddtd�fdtd�fgiii}|�|�}|�d�}|�dd��|�|jtdttd�d�tdttd�d�g�|S)	z�
        If a I{CNAME} record is encountered as the answer to a query for
        another record type, that record is returned as the answer.
        r]rkr6�example.netz10.0.0.7cSs|dSror&r)r&r&r'�<lambda>v�z<RootResolverTests.test_returnCanonicalName.<locals>.<lambda>r!�	rrrrXr`r9r4rrrar&r&r'�test_returnCanonicalNamegs&

����

��z*RootResolverTests.test_returnCanonicalNamec	Cs�ddtfddtd�fgidtfddtd�fgiii}|�|�}|�d�}|�dd��|�|jtdttd�d�tdttd�d�g�|S)	z�
        If no record of the requested type is included in a response, but a
        I{CNAME} record for the query name is included, queries are made to
        resolve the value of the I{CNAME}.
        r]rkr6rrz10.0.0.5cSs|dSror&r)r&r&r'rs�rtz<RootResolverTests.test_followCanonicalName.<locals>.<lambda>r!rurar&r&r'�test_followCanonicalName~s*����


��z*RootResolverTests.test_followCanonicalNamecCsHddtfddtd�fdtd�fgiii}|�|�}|�d�}|�|t�S)z�
        If there is a cycle between I{CNAME} records in a response, this is
        detected and the L{Deferred} returned by the lookup method fails
        with L{ResolverError}.
        r]rkr6rr)rrrXr`rirrar&r&r'�test_detectCanonicalNameLoop�s

����

z.RootResolverTests.test_detectCanonicalNameLoopcCs�dtfddtd�fgidtfddtd�fgiidtfdtd�fgdtd�fgd�idtfddtd	�fgiid
�}|�|d�}|�|�d�t�}|�|d�}|�d�}|�t�|�|j	td	��t
||g�S)
z�
        L{Resolver.lookupAddress} won't issue more queries following
        delegations than the limit passed to its initializer.
        rkr7rYr6rdsns2.example.comrer[z10.0.0.4)r]rf)rer^��)rrrrXrir`rr9r(r4r
)r<rbZfailerZfailDZ	succeederZsucceedDr&r&r'�test_boundedQueries�s<	��������!�

z%RootResolverTests.test_boundedQueriesN)rQ)�__name__�
__module__�__qualname__�__doc__rBrDrFrrPrXrcrgrhrjrlrmrnrqrvrwrxr{r&r&r&r'r+,s$,
r+c@seZdZdZdd�ZdS)�ResolverFactoryArgumentszf
    Raised by L{raisingResolverFactory} with the *args and **kwargs passed to
    that function.
    cCs||_||_dS)z�
        Store the supplied args and kwargs as attributes.

        @param args: Positional arguments.
        @param kwargs: Keyword arguments.
        N��args�kwargs)r<r�r�r&r&r'�__init__�sz!ResolverFactoryArguments.__init__N)r|r}r~rr�r&r&r&r'r��sr�cOst||��dS)a=
    Raise a L{ResolverFactoryArguments} exception containing the
    positional and keyword arguments passed to resolverFactory.

    @param args: A L{list} of all the positional arguments supplied by
        the caller.

    @param kwargs: A L{list} of all the keyword arguments supplied by
        the caller.
    N)r�r�r&r&r'�raisingResolverFactory�sr�c@s(eZdZdZdd�Zdd�Zdd�ZdS)	� RootResolverResolverFactoryTestsz6
    Tests for L{root.Resolver._resolverFactory}.
    cCs tdgtd�}|�|jt�dS)z�
        L{root.Resolver.__init__} accepts a C{resolverFactory}
        argument and assigns it to C{self._resolverFactory}.
        N)�hints�resolverFactory)rr��assertIs�_resolverFactory�r<�rr&r&r'�#test_resolverFactoryArgumentPresentszDRootResolverResolverFactoryTests.test_resolverFactoryArgumentPresentcCs tdgd�}|�|jtj�dS)z�
        L{root.Resolver.__init__} sets L{client.Resolver} as the
        C{_resolverFactory} if a C{resolverFactory} argument is not
        supplied.
        N)r�)rr�r�r
r�r&r&r'�"test_resolverFactoryArgumentAbsentszCRootResolverResolverFactoryTests.test_resolverFactoryArgumentAbsentcCsJt�}tdgt|d�}|�t|jd�}|�d|dgd�f|j|jf�dS)zy
        L{root.Resolver._resolverFactory} is supplied with C{reactor} and
        C{servers} keyword arguments.
        �192.0.2.101)r�r�r,zexample.comr&)r�r^)r,rbN)	�objectrr�ZassertRaisesr�r`r4r�r�)r<ZdummyReactorr��er&r&r'�)test_resolverFactoryOnlyExpectedArgumentss��
�zJRootResolverResolverFactoryTests.test_resolverFactoryOnlyExpectedArgumentsN)r|r}r~rr�r�r�r&r&r&r'r��s	
r�za.root-servers.netzb.root-servers.netzc.root-servers.netzd.root-servers.netze.root-servers.netzf.root-servers.netzg.root-servers.netzh.root-servers.netzi.root-servers.netzj.root-servers.netzk.root-servers.netzl.root-servers.netzm.root-servers.netc@s eZdZdZdd�Zdd�ZdS)�StubResolverz�
    An L{IResolverSimple} implementer which traces all getHostByName
    calls and their deferred results. The deferred results can be
    accessed and fired synchronously.
    cCsg|_g|_dS)z�
        @type calls: L{list} of L{tuple} containing C{args} and
            C{kwargs} supplied to C{getHostByName} calls.
        @type pendingResults: L{list} of L{Deferred} returned by
            C{getHostByName}.
        N)�calls�pendingResults)r<r&r&r'r�AszStubResolver.__init__cOs&|j�||f�t�}|j�|�|S)al
        A fake implementation of L{IResolverSimple.getHostByName}

        @param args: A L{list} of all the positional arguments supplied by
           the caller.

        @param kwargs: A L{list} of all the keyword arguments supplied by
           the caller.

        @return: A L{Deferred} which may be fired later from the test
            fixture.
        )r�r:rr�)r<r�r�r?r&r&r'�
getHostByNameLs
zStubResolver.getHostByNameN)r|r}r~rr�r�r&r&r&r'r�:sr�c@sHeZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dS)�BootstrapTestsz%
    Tests for L{root.bootstrap}
    cCst�t��}|�|tj�dS)zl
        L{root.bootstrap} returns an object which is initially a
        L{root.DeferredResolver}.
        N)r�	bootstrapr�rEZDeferredResolver)r<�deferredResolverr&r&r'�test_returnsDeferredResolverhsz+BootstrapTests.test_returnsDeferredResolvercCs,t�}t�|�|�|jdd�tD��dS)z�
        The L{IResolverSimple} supplied to L{root.bootstrap} is used to lookup
        the IP addresses of the 13 root name servers.
        cSsg|]}|fif�qSr&r&)rJ�sr&r&r'rMzsz=BootstrapTests.test_resolves13RootServers.<locals>.<listcomp>N)r�rr�r4r��ROOT_SERVERS)r<�stubResolverr&r&r'�test_resolves13RootServersqs
�z)BootstrapTests.test_resolves13RootServerscCs6t�}t�|�}|jD]}|�d�q|�|t�dS)z�
        The L{root.DeferredResolver} initially returned by L{root.bootstrap}
        becomes a L{root.Resolver} when the supplied resolver has successfully
        looked up all root hints.
        r�N)r�rr�r��callbackrEr�r<r�r�r?r&r&r'�test_becomesResolver}s


z#BootstrapTests.test_becomesResolvercCs>t�}t�|�}|jD]}|�d�q|�|jdgd�dS)z�
        The L{root.Resolver} which eventually replaces L{root.DeferredResolver}
        is supplied with the IP addresses of the 13 root servers.
        r��
N)r�rr�r�r�r4r�r�r&r&r'�test_resolverReceivesRootHints�s


z-BootstrapTests.test_resolverReceivesRootHintscs^t�}t�|��t|j�}t|�}|D]}|�d�q&|�t����fdd�}|�	|�dS)z�
        The L{root.Resolver} is eventually created, even if some of the root
        hint lookups fail. Only the working root hint IP addresses are supplied
        to the L{root.Resolver}.
        r�cs���jdgd�dS)Nr���r4r���res�r�r<r&r'�
checkHints�szFBootstrapTests.test_continuesWhenSomeRootHintsFail.<locals>.checkHintsN)
r�rr��iterr��nextr��errbackr�addBoth�r<r�r$Zd1r?r�r&r�r'�#test_continuesWhenSomeRootHintsFail�s

z2BootstrapTests.test_continuesWhenSomeRootHintsFailcsnt�}t�|��t|j�}t|�}|D]}|�t��q&|�t����fdd�}|�|���	�j
t�dS)z�
        The L{root.Resolver} is eventually created, even if all of the root hint
        lookups fail. Pending and new lookups will then fail with
        AttributeError.
        cs���jg�dS�Nr�r�r�r&r'r��szEBootstrapTests.test_continuesWhenAllRootHintsFail.<locals>.checkHintsN)r�rr�r�r�r�r�rr�Z
addCleanupZflushLoggedErrorsr�r&r�r'�"test_continuesWhenAllRootHintsFail�s


z1BootstrapTests.test_continuesWhenAllRootHintsFailcCs<t�}tj|td�}|jD]}|�d�q|�|jt�dS)z�
        L{root.bootstrap} accepts a C{resolverFactory} argument which is passed
        as an argument to L{root.Resolver} when it has successfully looked up
        root hints.
        )r�r�N)r�rr�r�r�r�r�r�r�r&r&r'�test_passesResolverFactory�s�
�z)BootstrapTests.test_passesResolverFactoryN)r|r}r~rr�r�r�r�r�r�r�r&r&r&r'r�ds	
r�c@seZdZdZdd�ZdS)�StubDNSDatagramProtocolz�
    A do-nothing stand-in for L{DNSDatagramProtocol} which can be used to avoid
    network traffic in tests where that kind of thing doesn't matter.
    cOst�Sr�)r)r<�a�kwr&r&r'rT�szStubDNSDatagramProtocol.queryN)r|r}r~rrTr&r&r&r'r��sr�zbtwisted.names.root.retry is deprecated since Twisted 10.0.  Use a Resolver object for retry logic.)�categoryr@N);rZzope.interfacerZzope.interface.verifyrZtwisted.python.logrZ
twisted.trialrZtwisted.trial.unittestrrZtwisted.internet.deferrr	r
rZtwisted.internet.interfacesrZ
twisted.namesr
rZtwisted.names.rootrZtwisted.names.dnsrrrrrrrrrrrrrrZtwisted.names.errorrrZtwisted.names.test.test_utilr r(r*r+�	Exceptionr�r�r�r�r�r�r�r��suppress�DeprecationWarningZ_retrySuppressionr&r&r&r'�<module>sX@		5,�%
l
�