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_server.cpython-38.pyc
U


W[��@sdZddlmZmZddlmZddlmZddlm	Z	ddl
mZmZm
Z
mZddlmZmZddlmZGd	d
�d
e�Zdd�ZGd
d�dej�ZGdd�dej�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�Zdd�ZGdd�dej �Z!dS)z)
Test cases for L{twisted.names.server}.
�)�division�absolute_import)�verifyClass)�defer)�IProtocolFactory)�dns�error�resolve�server)�failure�log)�unittestc@seZdZdZdd�ZdS)�RaisedArgumentszD
    An exception containing the arguments raised by L{raiser}.
    cCs||_||_dS�N��args�kwargs��selfrr�r�@/usr/lib/python3/dist-packages/twisted/names/test/test_server.py�__init__szRaisedArguments.__init__N)�__name__�
__module__�__qualname__�__doc__rrrrrrsrcOst||��dS)z�
    Raise a L{RaisedArguments} exception containing the supplied arguments.

    Used as a fake when testing the call signatures of  methods and functions.
    N)rrrrr�raisersrc@s eZdZdZdd�Zdd�ZdS)�NoResponseDNSServerFactorya
    A L{server.DNSServerFactory} subclass which does not attempt to reply to any
    received messages.

    Used for testing logged messages in C{messageReceived} without having to
    fake or patch the preceding code which attempts to deliver a response
    message.
    cCsdS)a
        Deny all queries.

        @param message: See L{server.DNSServerFactory.allowQuery}
        @param protocol: See L{server.DNSServerFactory.allowQuery}
        @param address: See L{server.DNSServerFactory.allowQuery}

        @return: L{False}
        @rtype: L{bool}
        Fr)r�message�protocol�addressrrr�
allowQuery0sz%NoResponseDNSServerFactory.allowQuerycCsdS)z�
        A noop send reply.

        @param protocol: See L{server.DNSServerFactory.sendReply}
        @param message: See L{server.DNSServerFactory.sendReply}
        @param address: See L{server.DNSServerFactory.sendReply}
        Nr)rrrr rrr�	sendReply>sz$NoResponseDNSServerFactory.sendReplyN)rrrrr!r"rrrrr'src@s(eZdZdZGdd�de�Zdd�ZdS)�RaisingDNSServerFactoryz�
    A L{server.DNSServerFactory} subclass whose methods raise an exception
    containing the supplied arguments.

    Used for stopping L{messageReceived} and testing the arguments supplied to
    L{allowQuery}.
    c@seZdZdZdS)z+RaisingDNSServerFactory.AllowQueryArguments�G
        Contains positional and keyword arguments in C{args}.
        N�rrrrrrrr�AllowQueryArgumentsRsr&cOs|�||��dS)aC
        Raise the arguments supplied to L{allowQuery}.

        @param args: Positional arguments which will be recorded in the raised
            exception.
        @type args: L{tuple}

        @param kwargs: Keyword args which will be recorded in the raised
            exception.
        @type kwargs: L{dict}
        N)r&rrrrr!Wsz"RaisingDNSServerFactory.allowQueryN)rrrr�	Exceptionr&r!rrrrr#Isr#c@s(eZdZdZGdd�de�Zdd�ZdS)�RaisingProtocolzm
    A partial fake L{IProtocol} whose methods raise an exception containing the
    supplied arguments.
    c@seZdZdZdS)z%RaisingProtocol.WriteMessageArgumentsr$Nr%rrrr�WriteMessageArgumentslsr)cOs|�||��dS�z�
        Raises the supplied arguments.

        @param args: Positional arguments
        @type args: L{tuple}

        @param kwargs: Keyword args
        @type kwargs: L{dict}
        N)r)rrrr�writeMessageqs
zRaisingProtocol.writeMessageN)rrrrr'r)r+rrrrr(gsr(c@seZdZdZdd�ZdS)�NoopProtocolzT
    A partial fake L{dns.DNSProtocolMixin} with a noop L{writeMessage} method.
    cOsdS)z�
        A noop version of L{dns.DNSProtocolMixin.writeMessage}.

        @param args: Positional arguments
        @type args: L{tuple}

        @param kwargs: Keyword args
        @type kwargs: L{dict}
        Nrrrrrr+�szNoopProtocol.writeMessageN)rrrrr+rrrrr,sr,c@s(eZdZdZGdd�de�Zdd�ZdS)�RaisingResolverzm
    A partial fake L{IResolver} whose methods raise an exception containing the
    supplied arguments.
    c@seZdZdZdS)zRaisingResolver.QueryArgumentsr$Nr%rrrr�QueryArguments�sr.cOs|�||��dSr*)r.rrrr�query�s
zRaisingResolver.queryN)rrrrr'r.r/rrrrr-�sr-c@s(eZdZdZGdd�de�Zdd�ZdS)�RaisingCachez}
    A partial fake L{twisted.names.cache.Cache} whose methods raise an exception
    containing the supplied arguments.
    c@seZdZdZdS)z!RaisingCache.CacheResultArgumentsr$Nr%rrrr�CacheResultArguments�sr1cOs|�||��dSr*)r1rrrr�cacheResult�s
zRaisingCache.cacheResultN)rrrrr'r1r2rrrrr0�sr0cOsDg}t�|j�|�tj|j�|||�|�dd�|D�|�dS)a
    Assert that the callable logs the expected messages when called.

    XXX: Put this somewhere where it can be re-used elsewhere. See #6677.

    @param testCase: The test case controlling the test which triggers the
        logged messages and on which assertions will be called.
    @type testCase: L{unittest.SynchronousTestCase}

    @param expectedMessages: A L{list} of the expected log messages
    @type expectedMessages: L{list}

    @param callable: The function which is expected to produce the
        C{expectedMessages} when called.
    @type callable: L{callable}

    @param args: Positional arguments to be passed to C{callable}.
    @type args: L{list}

    @param kwargs: Keyword arguments to be passed to C{callable}.
    @type kwargs: L{dict}
    cSsg|]}|dd�qS)rrr)�.0�mrrr�
<listcomp>�sz$assertLogMessage.<locals>.<listcomp>N)rZaddObserver�appendZ
addCleanupZremoveObserver�assertEqual)ZtestCaseZexpectedMessages�callablerrZloggedMessagesrrr�assertLogMessage�s
�r9c@seZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�Zd0d1�Zd2d3�Zd4d5�Zd6d7�Zd8d9�Zd:d;�Z d<d=�Z!d>d?�Z"d@dA�Z#dBdC�Z$dDdE�Z%dFdG�Z&dHdI�Z'dJdK�Z(dLdM�Z)dNdO�Z*dPdQ�Z+dRdS�Z,dTdU�Z-dVdW�Z.dXdY�Z/dZd[�Z0d\d]�Z1d^d_�Z2d`da�Z3dbdc�Z4ddde�Z5dfdg�Z6dhdi�Z7djdk�Z8dldm�Z9dndo�Z:dpdq�Z;drds�Z<dtdu�Z=dvdw�Z>dxdy�Z?dzd{�Z@d|d}�ZAd~d�ZBd�d��ZCd�S)��DNSServerFactoryTestsz/
    Tests for L{server.DNSServerFactory}.
    cCs|�t��jtj�dS)zd
        L{server.DNSServerFactory.resolver} is a L{resolve.ResolverChain}
        instance
        N)ZassertIsInstancer
�DNSServerFactory�resolverr	Z
ResolverChain�rrrr�test_resolverType�s�z'DNSServerFactoryTests.test_resolverTypecCs|�t��jjg�dS)zn
        L{server.DNSServerFactory.resolver} is an empty L{resolve.ResolverChain}
        by default.
        N)r7r
r;r<�	resolversr=rrr�test_resolverDefaultEmpty�s
�z/DNSServerFactoryTests.test_resolverDefaultEmptycCs&t�}|�tj|gd�jj|g�dS)z�
        L{server.DNSServerFactory.__init__} accepts an C{authorities}
        argument. The value of this argument is a list and is used to extend the
        C{resolver} L{resolve.ResolverChain}.
        )�authoritiesN��objectr7r
r;r<r?�rZ
dummyResolverrrr�test_authorities�s��z&DNSServerFactoryTests.test_authoritiescCs&t�}|�tj|gd�jj|g�dS)z�
        L{server.DNSServerFactory.__init__} accepts a C{caches} argument. The
        value of this argument is a list and is used to extend the C{resolver}
        L{resolve.ResolverChain}.
        ��cachesNrBrDrrr�test_caches
s��z!DNSServerFactoryTests.test_cachescCs&t�}|�tj|gd�jj|g�dS)z�
        L{server.DNSServerFactory.__init__} accepts a C{clients} argument. The
        value of this argument is a list and is used to extend the C{resolver}
        L{resolve.ResolverChain}.
        ��clientsNrBrDrrr�test_clientss��z"DNSServerFactoryTests.test_clientscCs\Gdd�dt�}Gdd�dt�}Gdd�dt�}|�tj|g|g|gd�jj|||g�dS)	zz
        L{server.DNSServerFactory.resolver} contains an ordered list of
        authorities, caches and clients.
        c@seZdZdS)z@DNSServerFactoryTests.test_resolverOrder.<locals>.DummyAuthorityN�rrrrrrr�DummyAuthority*srMc@seZdZdS)z<DNSServerFactoryTests.test_resolverOrder.<locals>.DummyCacheNrLrrrr�
DummyCache-srNc@seZdZdS)z=DNSServerFactoryTests.test_resolverOrder.<locals>.DummyClientNrLrrrr�DummyClient0srO)rArGrJNrB)rrMrNrOrrr�test_resolverOrder$s��z(DNSServerFactoryTests.test_resolverOrdercCs|�t��j�dS)zI
        L{server.DNSServerFactory.cache} is L{None} by default.
        N)ZassertIsNoner
r;�cacher=rrr�test_cacheDefault;sz'DNSServerFactoryTests.test_cacheDefaultcCs&t�}|�tjt�|gd�j|�dS)z�
        L{server.DNSServerFactory.__init__} assigns the last object in the
        C{caches} list to L{server.DNSServerFactory.cache}.
        rFN)rCr7r
r;rQrDrrr�test_cacheOverrideBs
�z(DNSServerFactoryTests.test_cacheOverridecCs|�t��j�dS)z�
        L{server.DNSServerFactory.canRecurse} is a flag indicating that this
        server is capable of performing recursive DNS lookups. It defaults to
        L{False}.
        N)�assertFalser
r;�
canRecurser=rrr�test_canRecurseDefaultMsz,DNSServerFactoryTests.test_canRecurseDefaultcCs|�tjdgd�jd�dS)z~
        L{server.DNSServerFactory.__init__} sets C{canRecurse} to L{True} if it
        is supplied with C{clients}.
        NrIT)r7r
r;rUr=rrr�test_canRecurseOverrideVs�z-DNSServerFactoryTests.test_canRecurseOverridecCs|�t��j�dS)zJ
        L{server.DNSServerFactory.verbose} defaults to L{False}.
        N)rTr
r;�verboser=rrr�test_verboseDefault_sz)DNSServerFactoryTests.test_verboseDefaultcCs|�tjdd�j�dS)z�
        L{server.DNSServerFactory.__init__} accepts a C{verbose} argument which
        overrides L{server.DNSServerFactory.verbose}.
        T�rXN)�
assertTruer
r;rXr=rrr�test_verboseOverridefsz*DNSServerFactoryTests.test_verboseOverridecCs|�tttj��dS)zL
        L{server.DNSServerFactory} implements L{IProtocolFactory}.
        N)r[rrr
r;r=rrr�test_interfacensz$DNSServerFactoryTests.test_interfacecCs|�tjjtj�dS)zU
        L{server.DNSServerFactory.protocol} defaults to L{dns.DNSProtocol}.
        N)�assertIsr
r;rrZDNSProtocolr=rrr�test_defaultProtocolusz*DNSServerFactoryTests.test_defaultProtocolcs^Gdd�dt�}|���fdd�}t��}||_|jdd�}|��|fif||j|jf�dS)z�
        L{server.DNSServerFactory.buildProtocol} builds a protocol by calling
        L{server.DNSServerFactory.protocol} with its self as a positional
        argument.
        c@seZdZdZdZdZdS)zNDNSServerFactoryTests.test_buildProtocolProtocolOverride.<locals>.FakeProtocolN)rrr�factoryrrrrrr�FakeProtocol�sracs|�_|�_�Srrr��stubProtocolrr�fakeProtocolFactory�szUDNSServerFactoryTests.test_buildProtocolProtocolOverride.<locals>.fakeProtocolFactoryN)Zaddr)rCr
r;rZ
buildProtocolr7rr)rrard�f�prrbr�"test_buildProtocolProtocolOverride|s
�z8DNSServerFactoryTests.test_buildProtocolProtocolOverridecCst��}t|g|jd�dS)zm
        L{server.DNSServerFactory._verboseLog} does not log messages unless
        C{verbose > 0}.
        �Foo BarN�r
r;r9Z_verboseLog�rrerrr�test_verboseLogQuiet�s�z*DNSServerFactoryTests.test_verboseLogQuietcCs"tjdd�}t|dg|jd�dS)zZ
        L{server.DNSServerFactory._verboseLog} logs a message if C{verbose > 0}.
        �rZrhNrirjrrr�test_verboseLogVerbose�s�z,DNSServerFactoryTests.test_verboseLogVerbosecCs.t��}tdd�}t|dg|j|ddd�dS)z�
        L{server.DNSServerFactory.messageReceived} logs about an empty query if
        the message had no queries and C{verbose} is C{>0}.
        rlrZz$Empty query from ('192.0.2.100', 53)N�z192.0.2.100�5�r�protor )r�Messagerr9�messageReceived�rr4rerrr�"test_messageReceivedLoggingNoQuery�s
�z8DNSServerFactoryTests.test_messageReceivedLoggingNoQuerycCsNt��}|jdtjd�|jdtjd�tdd�}t|dg|j|ddd�dS)	z�
        L{server.DNSServerFactory.messageReceived} logs the query types of all
        queries in the message if C{verbose} is set to C{1}.
        �example.com��name�typerlrZz&MX AAAA query from ('192.0.2.100', 53)Nrnrp�rrr�addQueryZMXZAAAArr9rsrtrrr�test_messageReceivedLogging1�s
�z2DNSServerFactoryTests.test_messageReceivedLogging1cCsNt��}|jdtjd�|jdtjd�tdd�}t|dg|j|ddd�dS)	z�
        L{server.DNSServerFactory.messageReceived} logs the repr of all queries
        in the message if C{verbose} is set to C{2}.
        rvrw�rZzT<Query example.com MX IN> <Query example.com AAAA IN> query from ('192.0.2.100', 53)Nrnrprzrtrrr�test_messageReceivedLogging2�s
�z2DNSServerFactoryTests.test_messageReceivedLogging2csNt��}t�}t��|�tjd�fdd��|j|ddd�|�|j	��dS)zv
        L{server.DNSServerFactory.messageReceived} assigns a unix timestamp to
        the received message.
        �timecs�Srrr��trr�<lambda>��zEDNSServerFactoryTests.test_messageReceivedTimestamp.<locals>.<lambda>Nrp)
rrrrrC�patchr
rrsr7�timeReceivedrtrr�r�test_messageReceivedTimestamp�sz3DNSServerFactoryTests.test_messageReceivedTimestampcCs^t��}t�}t�}t�}|jtj|j|||d�}|j\}}|�||||f�|�|i�dS)z�
        L{server.DNSServerFactory.messageReceived} passes all messages to
        L{server.DNSServerFactory.allowQuery} along with the receiving protocol
        and origin address.
        rpN)	rrrrCr#�assertRaisesr&rsrr7)rrZ
dummyProtocol�dummyAddressre�errrrr�test_messageReceivedAllowQuery�s�
z4DNSServerFactoryTests.test_messageReceivedAllowQuerycsjGdd�dt��G�fdd�dtj�}|�}|j�|jt��ddd�}|j\\}}}}|�|j	tj
�dS)z�
        If C{allowQuery} returns C{False},
        L{server.DNSServerFactory.messageReceived} calls L{server.sendReply}
        with a message whose C{rCode} is L{dns.EREFUSED}.
        c@seZdZdS)zFDNSServerFactoryTests.test_allowQueryFalse.<locals>.SendReplyExceptionNrLrrrr�SendReplyException
sr�cs eZdZdd�Z�fdd�ZdS)zKDNSServerFactoryTests.test_allowQueryFalse.<locals>.RaisingDNSServerFactoryc_sdS)NFrrrrrr!szVDNSServerFactoryTests.test_allowQueryFalse.<locals>.RaisingDNSServerFactory.allowQuerycs�||��dSrrr�r�rrr"szUDNSServerFactoryTests.test_allowQueryFalse.<locals>.RaisingDNSServerFactory.sendReplyN)rrrr!r"rr�rrr#sr#Nrp)r'r
r;r�rsrrrrr7�rCodeZEREFUSED)rr#rer�rqrr rrr�r�test_allowQueryFalses�z*DNSServerFactoryTests.test_allowQueryFalsecsXdg|_g��fdd�}t�}t�d�}t|||�|�||�|��||dfg�dS)a�
        Assert that the named method is called with the given message when it is
        passed to L{DNSServerFactory.messageReceived}.

        @param methodName: The name of the method which is expected to be
            called.
        @type methodName: L{str}

        @param message: The message which is expected to be passed to the
            C{methodName} method.
        @type message: L{dns.Message}
        Ncs��|||f�dSr�r6�rrr �ZreceivedMessagesrr�fakeHandler3sz?DNSServerFactoryTests._messageReceivedTest.<locals>.fakeHandler)�queriesr,r
r;�setattrrsr7)rZ
methodNamerr�rr`rr�r�_messageReceivedTest!s
z*DNSServerFactoryTests._messageReceivedTestcCs|�dtjtjd��dS)z�
        L{DNSServerFactory.messageReceived} passes messages with an opcode of
        C{OP_QUERY} on to L{DNSServerFactory.handleQuery}.
        �handleQuery�ZopCodeN)r�rrrZOP_QUERYr=rrr�test_queryMessageReceived=s�z/DNSServerFactoryTests.test_queryMessageReceivedcCs|�dtjtjd��dS)z�
        L{DNSServerFactory.messageReceived} passes messages with an opcode of
        C{OP_INVERSE} on to L{DNSServerFactory.handleInverseQuery}.
        �handleInverseQueryr�N)r�rrrZ
OP_INVERSEr=rrr� test_inverseQueryMessageReceivedFs�z6DNSServerFactoryTests.test_inverseQueryMessageReceivedcCs|�dtjtjd��dS)z�
        L{DNSServerFactory.messageReceived} passes messages with an opcode of
        C{OP_STATUS} on to L{DNSServerFactory.handleStatus}.
        �handleStatusr�N)r�rrrZ	OP_STATUSr=rrr�test_statusMessageReceivedOs�z0DNSServerFactoryTests.test_statusMessageReceivedcCs|�dtjtjd��dS)z�
        L{DNSServerFactory.messageReceived} passes messages with an opcode of
        C{OP_NOTIFY} on to L{DNSServerFactory.handleNotify}.
        �handleNotifyr�N)r�rrrZ	OP_NOTIFYr=rrr�test_notifyMessageReceivedXs�z0DNSServerFactoryTests.test_notifyMessageReceivedcCs|�dtjtjd��dS)z�
        L{DNSServerFactory.messageReceived} passes messages with an opcode of
        C{OP_UPDATE} on to L{DNSServerFactory.handleOther}.

        This may change if the implementation ever covers update messages.
        �handleOtherr�N)r�rrrZ	OP_UPDATEr=rrr�test_updateMessageReceivedas�z0DNSServerFactoryTests.test_updateMessageReceivedcCs�t�t�}}t��}|�|�|�|j|g�|�|�|�|j||g�|�|�|�|j|g�|�|�|�|jg�dS)z�
        The C{connectionMade} and C{connectionLost} methods of
        L{DNSServerFactory} cooperate to keep track of all L{DNSProtocol}
        objects created by a factory which are connected.
        N)rCr
r;ZconnectionMader7ZconnectionsZconnectionLost)rZprotoAZprotoBr`rrr�test_connectionTrackingls



z-DNSServerFactoryTests.test_connectionTrackingcCsht��}|�d�|�d�t��}t�|_|jtj|j	|t
�dd�}|j\\}}|�||j
d�dS)z�
        L{server.DNSServerFactory.handleQuery} takes the first query from the
        supplied message and dispatches it to
        L{server.DNSServerFactory.resolver.query}.
        �one.example.comstwo.example.comNr�r)rrrr{r
r;r-r<r�r.r�r,rr7r�)rr4rer�r/rrrr�test_handleQuery~s

�z&DNSServerFactoryTests.test_handleQuerycs�t��}t���G�fdd�dt�}|�|_g��fdd�}||_t��}|�	d�t
�}t�}|j|||d�t�}��|�|�
�||||fifg�dS)aC
        L{server.DNSServerFactory.handleQuery} adds
        L{server.DNSServerFactory.resolver.gotResolverResponse} as a callback to
        the deferred returned by L{server.DNSServerFactory.resolver.query}. It
        is called with the query response, the original protocol, message and
        origin address.
        cseZdZ�fdd�ZdS)zDDNSServerFactoryTests.test_handleQueryCallback.<locals>.FakeResolvercs�Srrr��drrr/�szJDNSServerFactoryTests.test_handleQueryCallback.<locals>.FakeResolver.queryN�rrrr/rr�rr�FakeResolver�sr�cs��||f�dSrr�r)�gotResolverResponseArgsrr�fakeGotResolverResponse�szODNSServerFactoryTests.test_handleQueryCallback.<locals>.fakeGotResolverResponser�r�N)r
r;r�DeferredrCr<�gotResolverResponserrrr{r,r��callbackr7)rrer�r�r4rcr�Z
dummyResponser)r�r�r�test_handleQueryCallback�s$

�z.DNSServerFactoryTests.test_handleQueryCallbackcs�t��}t���G�fdd�dt�}|�|_g��fdd�}||_t��}|�	d�t
�}t�}|j|||d�t�
t��}��|�|��||||fifg�dS)a?
        L{server.DNSServerFactory.handleQuery} adds
        L{server.DNSServerFactory.resolver.gotResolverError} as an errback to
        the deferred returned by L{server.DNSServerFactory.resolver.query}. It
        is called with the query failure, the original protocol, message and
        origin address.
        cseZdZ�fdd�ZdS)zCDNSServerFactoryTests.test_handleQueryErrback.<locals>.FakeResolvercs�Srrrr�rrr/�szIDNSServerFactoryTests.test_handleQueryErrback.<locals>.FakeResolver.queryNr�rr�rrr��sr�cs��||f�dSrr�r)�gotResolverErrorArgsrr�fakeGotResolverError�szKDNSServerFactoryTests.test_handleQueryErrback.<locals>.fakeGotResolverErrorr�r�N)r
r;rr�rCr<�gotResolverErrorrrrr{r,r�r�Failurer'Zerrbackr7)rrer�r�r4rcr�ZstubFailurer)r�r�r�test_handleQueryErrback�s$

�z-DNSServerFactoryTests.test_handleQueryErrbackcCstt��}g}g}g}|jtj|j|||ft�t��dd�}|j\\}}|�	|j
|�|�	|j|�|�	|j|�dS)z�
        L{server.DNSServerFactory.gotResolverResponse} accepts a tuple of
        resource record lists and triggers a response message containing those
        resource record lists.
        N�rrr )
r
r;r�r(r)r�rrrrr^�answers�	authority�
additional)rrer�r�r�r�rrrrr�test_gotResolverResponse�s �z.DNSServerFactoryTests.test_gotResolverResponsec
Csdt�}t|_t��}d|_|jt|jgggfd|dd�}|�	dt
|tjgggd�f|j|j
f�dS)z�
        L{server.DNSServerFactory.gotResolverResponse} calls
        L{server.DNSServerFactory._responseFromMessage} to generate a response.
        rlNr�r)rr�r�r�r�)rr�_responseFromMessagerrrr�r�rr�r7�dict�OKrr�rr`�requestr�rrr�0test_gotResolverResponseCallsResponseFromMessage�s(�
�
�zFDNSServerFactoryTests.test_gotResolverResponseCallsResponseFromMessagecCs4t��}tjddd�}|j|d�f}|�||�dS)z�
        L{server.DNSServerFactory._responseFromMessage} generates a response
        message which is a copy of the request message.
        F)�answer�recAv�rN)r
r;rrrr�ZassertIsNot�rr`r��responserrr�"test_responseFromMessageNewMessagesz8DNSServerFactoryTests.test_responseFromMessageNewMessagecCsTt��}d|_|jtjdd�d�}d|_|jtjdd�d�}|�d|j|jf�dS)z�
        L{server.DNSServerFactory._responseFromMessage} generates a response
        message whose C{recAV} attribute is L{True} if
        L{server.DNSServerFactory.canRecurse} is L{True}.
        TF)r�r��TFN)r
r;rUr�rrrr7r��rr`Z	response1Z	response2rrr�*test_responseFromMessageRecursionAvailables
�
�
�z@DNSServerFactoryTests.test_responseFromMessageRecursionAvailablecCs6t��}t��}d|_|j|d�}|�|j|j�dS)z�
        L{server.DNSServerFactory._responseFromMessage} generates a response
        message whose C{timeReceived} attribute has the same value as that found
        on the request.
        i�r�N)r
r;rrrr�r�r7r�rrr�$test_responseFromMessageTimeReceived&s
z:DNSServerFactoryTests.test_responseFromMessageTimeReceivedcCs6t��}t��}d|_|j|d�}|�|j|j�dS)z�
        L{server.DNSServerFactory._responseFromMessage} generates a response
        message whose C{maxSize} attribute has the same value as that found
        on the request.
        rr�N)r
r;rrrZmaxSizer�r7r�rrr�test_responseFromMessageMaxSize4s
z5DNSServerFactoryTests.test_responseFromMessageMaxSizecCs|�tjtjj�dS)z{
        L{server.DNSServerFactory} has a C{_messageFactory} attribute which is
        L{dns.Message} by default.
        N)r^rrrr
r;�_messageFactoryr=rrr�test_messageFactoryBsz)DNSServerFactoryTests.test_messageFactoryc
Csdt��}|�tdt�t��}|jt|j|tj	d�}|�
dt|j|tj	|j
dd�f|j|jf�dS)a4
        L{server.DNSServerFactory._responseFromMessage} calls
        C{dns._responseFromMessage} to generate a response
        message from the request message. It supplies the request message and
        other keyword arguments which should be passed to the response message
        initialiser.
        r��rr�rF)ZresponseConstructorrr�r��authN)r
r;r�rrrrr�rr�r�r7r�r�rUrrr�rrr�+test_responseFromMessageCallsMessageFactoryJs$��
�zADNSServerFactoryTests.test_responseFromMessageCallsMessageFactorycCsXt��}|jt��tjdd�gd�}|jt��tjdd�gd�}|�d|j|jf�dS)z�
        L{server.DNSServerFactory._responseFromMessage} marks the response
        message as authoritative if any of the answer records are authoritative.
        T)r�)rr�Fr�N)r
r;r�rrr�RRHeaderr7r�r�rrr�,test_responseFromMessageAuthoritativeMessagecs��
�zBDNSServerFactoryTests.test_responseFromMessageAuthoritativeMessagec	CsRtdd�}t��g}t��g}t��g}t|dg|j|||ft�t��dd�dS)z�
        L{server.DNSServerFactory.gotResolverResponse} logs the total number of
        records in the response if C{verbose > 0}.
        rlrZzLookup found 3 recordsNr�)rrr�r9r�r,rr)rrer�r�r�rrr�test_gotResolverResponseLoggingss



�z5DNSServerFactoryTests.test_gotResolverResponseLoggingcCs�tt�gd�}t��}|�d�t��g}g}g}|jtj|j|||ft	�|dd�}|j
\\}\}}	}
}|�|jjd�|�
||�|�
|	|�|�
|
|�dS)z�
        L{server.DNSServerFactory.gotResolverResponse} caches the response if at
        least one cache was provided in the constructor.
        rFsexample.comNr�)rr0rrrr{r�r�r1r�r,rr7rxr^)rrer4ZexpectedAnswersZexpectedAuthorityZexpectedAdditionalr�r/r�r�r�rrrr�test_gotResolverResponseCaching�s&

�z5DNSServerFactoryTests.test_gotResolverResponseCachingcCsbt�}t|_t��}d|_|jt|jt	�
t���d|dd�}|�
dt|tjd�f|j|jf�dS)z�
        L{server.DNSServerFactory.gotResolverError} calls
        L{server.DNSServerFactory._responseFromMessage} to generate a response.
        rlNr�rr�)rrr�rrrr�r�rr�rr�r�DomainErrorr7r��ENAMErrr�rrr�-test_gotResolverErrorCallsResponseFromMessage�s �
�zCDNSServerFactoryTests.test_gotResolverErrorCallsResponseFromMessagecCsLt��}|jtj|jt�|�t�t�	�dd�}|j
\\}}|�|j|�dS)a{
        L{server.DNSServerFactory.gotResolver} accepts a L{failure.Failure} and
        triggers a response message whose rCode corresponds to the DNS error
        contained in the C{Failure}.

        @param responseError: The L{Exception} instance which is expected to
            trigger C{expectedMessageCode} when it is supplied to
            C{gotResolverError}
        @type responseError: L{Exception}

        @param expectedMessageCode: The C{rCode} which is expected in the
            message returned by C{gotResolverError} in response to
            C{responseError}.
        @type expectedMessageCode: L{int}
        Nr�)
r
r;r�r(r)r�rr�rrrrr7r�)rZ
responseErrorZexpectedMessageCoderer�rrrrr�_assertMessageRcodeForError�s�z1DNSServerFactoryTests._assertMessageRcodeForErrorcCs|�t��tj�dS)z�
        L{server.DNSServerFactory.gotResolver} triggers a response message with
        an C{rCode} of L{dns.ENAME} if supplied with a L{error.DomainError}.
        N)r�rr�rr�r=rrr� test_gotResolverErrorDomainError�sz6DNSServerFactoryTests.test_gotResolverErrorDomainErrorcCs|�t��tj�dS)z�
        L{server.DNSServerFactory.gotResolver} triggers a response message with
        an C{rCode} of L{dns.ENAME} if supplied with a
        L{error.AuthoritativeDomainError}.
        N)r�rZAuthoritativeDomainErrorrr�r=rrr�-test_gotResolverErrorAuthoritativeDomainError�s�zCDNSServerFactoryTests.test_gotResolverErrorAuthoritativeDomainErrorcCs.|�t�tj�|�t�}|�t|�d�dS)z�
        L{server.DNSServerFactory.gotResolver} triggers a response message with
        an C{rCode} of L{dns.ESERVER} if supplied with another type of error and
        logs the error.
        rlN)r��KeyErrorrZESERVERZflushLoggedErrorsr7�len)rr�rrr�test_gotResolverErrorOtherError�s
z5DNSServerFactoryTests.test_gotResolverErrorOtherErrorc	Cs8tdd�}t|dg|jt�t���t�t�	�dd�dS)zZ
        L{server.DNSServerFactory.gotResolver} logs a message if C{verbose > 0}.
        rlrZz
Lookup failedNr�)
rr9r�rr�rr�r,rrrrjrrr�test_gotResolverErrorLogging�s
�z2DNSServerFactoryTests.test_gotResolverErrorLoggingcs�t��}g��fdd�|_tjddd�}t�t�g|_t�t�g|_t�t�g|_|j	t
�t�
��d|dd�|�tjddd�g��dS)	z�
        L{server.DNSServerFactory.gotResolverError} does not allow request
        attributes to leak into the response ie it sends a response with AD, CD
        set to 0 and empty response record sections.
        cs
��|�Srr��rr�r �Z	responsesrrr�r�zUDNSServerFactoryTests.test_gotResolverErrorResetsResponseAttributes.<locals>.<lambda>T�Z
authenticDataZcheckingDisabledNr���r�r�)r
r;r"rrrrCr�r�r�r�rr�rr�r7�rr`r�rr�r�-test_gotResolverErrorResetsResponseAttributes�s
��zCDNSServerFactoryTests.test_gotResolverErrorResetsResponseAttributescs�t��}g��fdd�|_tjddd�}t�t�g|_t�t�g|_t�t�g|_|j	gggfd|dd�|�
tjddd�g��dS)	a
        L{server.DNSServerFactory.gotResolverResponse} does not allow request
        attributes to leak into the response ie it sends a response with AD, CD
        set to 0 and none of the records in the request answer sections are
        copied to the response.
        cs
��|�Srr�r�r�rrr�r�zXDNSServerFactoryTests.test_gotResolverResponseResetsResponseAttributes.<locals>.<lambda>Tr�Nr�rr�)r
r;r"rrrrCr�r�r�r�r7r�rr�r�0test_gotResolverResponseResetsResponseAttributess
��zFDNSServerFactoryTests.test_gotResolverResponseResetsResponseAttributescCsZt��}t�}t��}|jtj|jt�||d�}|j	\}}|�
|||f�|�
|i�dS)z�
        If L{server.DNSServerFactory.sendReply} is supplied with a protocol
        *and* an address tuple it will supply that address to
        C{protocol.writeMessage}.
        r�N)rrrrCr
r;r�r(r)r"rr7)rr4r�rer�rrrrr�test_sendReplyWithAddress+s�
z/DNSServerFactoryTests.test_sendReplyWithAddresscCsRt��}t��}|jtj|jt�|dd�}|j\}}|�	||f�|�	|i�dS)z�
        If L{server.DNSServerFactory.sendReply} is supplied with a protocol but
        no address tuple it will supply only a message to
        C{protocol.writeMessage}.
        Nr�)
rrrr
r;r�r(r)r"rr7)rr4rer�rrrrr�test_sendReplyWithoutAddress?s�
z2DNSServerFactoryTests.test_sendReplyWithoutAddresscCsN|�tjddd��t��}d|_tjdd�}t|ddg|jt	�|d	d
�d	S)z�
        If L{server.DNSServerFactory.sendReply} logs a "no answers" message if
        the supplied message has no answers.
        rcSsdS�Ni�Qrrrrrr�Wr�zFDNSServerFactoryTests.test_sendReplyLoggingNoAnswers.<locals>.<lambda>�Qr}rZzReplying with no answers� Processed query in 1.000 secondsNr�)
r�r
rrrrr�r;r9r"r,rtrrr�test_sendReplyLoggingNoAnswersRs�z4DNSServerFactoryTests.test_sendReplyLoggingNoAnswerscCs�|�tjddd��t��}|j�tjt�d�d��|j	�tjt�d�d��|j
�tjt�d�d��d|_tjdd�}t
|d	d
ddg|jt�|d
d�d
S)z�
        If L{server.DNSServerFactory.sendReply} logs a message for answers,
        authority, additional if the supplied a message has records in any of
        those sections.
        rcSsdSr�rrrrrr�jr�zHDNSServerFactoryTests.test_sendReplyLoggingWithAnswers.<locals>.<lambda>z	127.0.0.1)Zpayloadr�r}rZz*Answers are <A address=127.0.0.1 ttl=None>z+Authority is <A address=127.0.0.1 ttl=None>z,Additional is <A address=127.0.0.1 ttl=None>r�Nr�)r�r
rrrrr�r6r�ZRecord_Ar�r�r�r;r9r"r,rtrrr� test_sendReplyLoggingWithAnswersds&��z6DNSServerFactoryTests.test_sendReplyLoggingWithAnswerscCsFt��}|jtj|jt��t�dd�}|j\\}}|�	|j
tj�dS)z�
        L{server.DNSServerFactory.handleInverseQuery} triggers the sending of a
        response message with C{rCode} set to L{dns.ENOTIMP}.
        Nr�)r
r;r�r(r)r�rrrrr7r��ENOTIMP�rrer�rrrrr�test_handleInverseQuery}s�z-DNSServerFactoryTests.test_handleInverseQuerycCs,tdd�}t|dg|jt��t�dd�dS)zz
        L{server.DNSServerFactory.handleInverseQuery} logs the message origin
        address if C{verbose > 0}.
        rlrZzInverse query from ('::1', 53)�z::1ror�N)rr9r�rrrr,rjrrr�test_handleInverseQueryLogging�s
�z4DNSServerFactoryTests.test_handleInverseQueryLoggingcCsFt��}|jtj|jt��t�dd�}|j\\}}|�	|j
tj�dS)z�
        L{server.DNSServerFactory.handleStatus} triggers the sending of a
        response message with C{rCode} set to L{dns.ENOTIMP}.
        Nr�)r
r;r�r(r)r�rrrrr7r�r�r�rrr�test_handleStatus�s�z'DNSServerFactoryTests.test_handleStatuscCs,tdd�}t|dg|jt��t�dd�dS)zt
        L{server.DNSServerFactory.handleStatus} logs the message origin address
        if C{verbose > 0}.
        rlrZzStatus request from ('::1', 53)r�r�N)rr9r�rrrr,rjrrr�test_handleStatusLogging�s
�z.DNSServerFactoryTests.test_handleStatusLoggingcCsFt��}|jtj|jt��t�dd�}|j\\}}|�	|j
tj�dS)z�
        L{server.DNSServerFactory.handleNotify} triggers the sending of a
        response message with C{rCode} set to L{dns.ENOTIMP}.
        Nr�)r
r;r�r(r)r�rrrrr7r�r�r�rrr�test_handleNotify�s�z'DNSServerFactoryTests.test_handleNotifycCs,tdd�}t|dg|jt��t�dd�dS)zt
        L{server.DNSServerFactory.handleNotify} logs the message origin address
        if C{verbose > 0}.
        rlrZzNotify message from ('::1', 53)r�r�N)rr9r�rrrr,rjrrr�test_handleNotifyLogging�s
�z.DNSServerFactoryTests.test_handleNotifyLoggingcCsFt��}|jtj|jt��t�dd�}|j\\}}|�	|j
tj�dS)z�
        L{server.DNSServerFactory.handleOther} triggers the sending of a
        response message with C{rCode} set to L{dns.ENOTIMP}.
        Nr�)r
r;r�r(r)r�rrrrr7r�r�r�rrr�test_handleOther�s�z&DNSServerFactoryTests.test_handleOthercCs,tdd�}t|dg|jt��t�dd�dS)zs
        L{server.DNSServerFactory.handleOther} logs the message origin address
        if C{verbose > 0}.
        rlrZz$Unknown op code (0) from ('::1', 53)r�r�N)rr9r�rrrr,rjrrr�test_handleOtherLogging�s
�z-DNSServerFactoryTests.test_handleOtherLoggingN)Drrrrr>r@rErHrKrPrRrSrVrWrYr\r]r_rgrkrmrur|r~r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrr:�s�




		
				$$

r:N)"rZ
__future__rrZzope.interface.verifyrZtwisted.internetrZtwisted.internet.interfacesrZ
twisted.namesrrr	r
Ztwisted.pythonrrZ
twisted.trialr
r'rrr;rr#rCr(r,r-r0r9ZTestCaser:rrrr�<module>s"

"#