o
    bǢ                     @   s   d Z ddlmZ ddlmZ ddlmZ ddlmZm	Z	m
Z
mZ ddlmZmZ ddlmZ G dd	 d	eZd
d ZG dd dejZG dd dejZG dd dZG dd dZG dd dZG dd dZdd ZG dd dejZdS )z)
Test cases for L{twisted.names.server}.
    )verifyClass)defer)IProtocolFactory)dnserrorresolveserver)failurelog)unittestc                   @      e Zd ZdZdd ZdS )RaisedArgumentszD
    An exception containing the arguments raised by L{raiser}.
    c                 C   s   || _ || _d S Nargskwargsselfr   r    r   @/usr/lib/python3/dist-packages/twisted/names/test/test_server.py__init__   s   
zRaisedArguments.__init__N)__name__
__module____qualname____doc__r   r   r   r   r   r          r   c                  O   s
   t | |)z
    Raise a L{RaisedArguments} exception containing the supplied arguments.

    Used as a fake when testing the call signatures of  methods and functions.
    )r   r   r   r   r   raiser   s   
r   c                   @   s    e Zd ZdZdd Zdd ZdS )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.
    c                 C      dS )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addressr   r   r   
allowQuery.   s   z%NoResponseDNSServerFactory.allowQueryc                 C   r   )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   )r   r    r   r!   r   r   r   	sendReply;       z$NoResponseDNSServerFactory.sendReplyN)r   r   r   r   r"   r#   r   r   r   r   r   $   s    	r   c                   @   (   e Zd ZdZG dd deZdd ZdS )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                   @      e Zd ZdZdS )z+RaisingDNSServerFactory.AllowQueryArgumentsG
        Contains positional and keyword arguments in C{args}.
        Nr   r   r   r   r   r   r   r   AllowQueryArgumentsN       r*   c                 O      |  ||)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}
        )r*   r   r   r   r   r"   S   s   z"RaisingDNSServerFactory.allowQueryN)r   r   r   r   	Exceptionr*   r"   r   r   r   r   r&   E   s    r&   c                   @   r%   )RaisingProtocolzm
    A partial fake L{IProtocol} whose methods raise an exception containing the
    supplied arguments.
    c                   @   r'   )z%RaisingProtocol.WriteMessageArgumentsr(   Nr)   r   r   r   r   WriteMessageArgumentsh   r+   r/   c                 O   r,   z
        Raises the supplied arguments.

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

        @param kwargs: Keyword args
        @type kwargs: L{dict}
        )r/   r   r   r   r   writeMessagem      
zRaisingProtocol.writeMessageN)r   r   r   r   r-   r/   r1   r   r   r   r   r.   b       r.   c                   @   r   )NoopProtocolzT
    A partial fake L{dns.DNSProtocolMixin} with a noop L{writeMessage} method.
    c                 O   r   )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}
        Nr   r   r   r   r   r1      r$   zNoopProtocol.writeMessageN)r   r   r   r   r1   r   r   r   r   r4   z   r   r4   c                   @   r%   )RaisingResolverzm
    A partial fake L{IResolver} whose methods raise an exception containing the
    supplied arguments.
    c                   @   r'   )zRaisingResolver.QueryArgumentsr(   Nr)   r   r   r   r   QueryArguments   r+   r6   c                 O   r,   r0   )r6   r   r   r   r   query   r2   zRaisingResolver.queryN)r   r   r   r   r-   r6   r7   r   r   r   r   r5      r3   r5   c                   @   r%   )RaisingCachez}
    A partial fake L{twisted.names.cache.Cache} whose methods raise an exception
    containing the supplied arguments.
    c                   @   r'   )z!RaisingCache.CacheResultArgumentsr(   Nr)   r   r   r   r   CacheResultArguments   r+   r9   c                 O   r,   r0   )r9   r   r   r   r   cacheResult   r2   zRaisingCache.cacheResultN)r   r   r   r   r-   r9   r:   r   r   r   r   r8      r3   r8   c                 O   sH   g }t |j | t j|j ||i | | dd |D | dS )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}
    c                 S   s   g | ]}|d  d qS )r   r   r   ).0mr   r   r   
<listcomp>   s    z$assertLogMessage.<locals>.<listcomp>N)r
   addObserverappend
addCleanupremoveObserverassertEqual)testCaseexpectedMessagescallabler   r   loggedMessagesr   r   r   assertLogMessage   s
   rG   c                   @   s  e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd: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 ZBdd ZCdS )DNSServerFactoryTestsz/
    Tests for L{server.DNSServerFactory}.
    c                 C   s   |  t jtj dS )zd
        L{server.DNSServerFactory.resolver} is a L{resolve.ResolverChain}
        instance
        N)assertIsInstancer   DNSServerFactoryresolverr   ResolverChainr   r   r   r   test_resolverType      z'DNSServerFactoryTests.test_resolverTypec                 C   s   |  t jjg  dS )zn
        L{server.DNSServerFactory.resolver} is an empty L{resolve.ResolverChain}
        by default.
        NrB   r   rJ   rK   	resolversrM   r   r   r   test_resolverDefaultEmpty   rO   z/DNSServerFactoryTests.test_resolverDefaultEmptyc                 C   &   t  }| tj|gdjj|g dS )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objectrB   r   rJ   rK   rQ   r   dummyResolverr   r   r   test_authorities   
   z&DNSServerFactoryTests.test_authoritiesc                 C   rS   )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}.
        cachesNrU   rW   r   r   r   test_caches   rZ   z!DNSServerFactoryTests.test_cachesc                 C   rS   )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}.
        clientsNrU   rW   r   r   r   test_clients  rZ   z"DNSServerFactoryTests.test_clientsc                 C   sV   G dd d}G dd d}G dd d}|  tj|g|g|gdjj|||g dS )	zz
        L{server.DNSServerFactory.resolver} contains an ordered list of
        authorities, caches and clients.
        c                   @      e Zd ZdS )z@DNSServerFactoryTests.test_resolverOrder.<locals>.DummyAuthorityNr   r   r   r   r   r   r   DummyAuthority      rc   c                   @   ra   )z<DNSServerFactoryTests.test_resolverOrder.<locals>.DummyCacheNrb   r   r   r   r   
DummyCache  rd   re   c                   @   ra   )z=DNSServerFactoryTests.test_resolverOrder.<locals>.DummyClientNrb   r   r   r   r   DummyClient  rd   rf   )rT   r\   r_   NrP   )r   rc   re   rf   r   r   r   test_resolverOrder  s   z(DNSServerFactoryTests.test_resolverOrderc                 C      |  t j dS )zI
        L{server.DNSServerFactory.cache} is L{None} by default.
        N)assertIsNoner   rJ   cacherM   r   r   r   test_cacheDefault(     z'DNSServerFactoryTests.test_cacheDefaultc                 C   s&   t  }| tjt  |gdj| dS )z
        L{server.DNSServerFactory.__init__} assigns the last object in the
        C{caches} list to L{server.DNSServerFactory.cache}.
        r[   N)rV   rB   r   rJ   rj   rW   r   r   r   test_cacheOverride.  s
   z(DNSServerFactoryTests.test_cacheOverridec                 C   rh   )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   rJ   
canRecurserM   r   r   r   test_canRecurseDefault9  s   z,DNSServerFactoryTests.test_canRecurseDefaultc                 C   s   |  tjdgdjd dS )z~
        L{server.DNSServerFactory.__init__} sets C{canRecurse} to L{True} if it
        is supplied with C{clients}.
        Nr^   T)rB   r   rJ   ro   rM   r   r   r   test_canRecurseOverrideA  s   z-DNSServerFactoryTests.test_canRecurseOverridec                 C   rh   )zJ
        L{server.DNSServerFactory.verbose} defaults to L{False}.
        N)rn   r   rJ   verboserM   r   r   r   test_verboseDefaultH  rl   z)DNSServerFactoryTests.test_verboseDefaultc                 C   s   |  tjddj dS )z
        L{server.DNSServerFactory.__init__} accepts a C{verbose} argument which
        overrides L{server.DNSServerFactory.verbose}.
        Trr   N)
assertTruer   rJ   rr   rM   r   r   r   test_verboseOverrideN  rO   z*DNSServerFactoryTests.test_verboseOverridec                 C   s   |  tttj dS )zL
        L{server.DNSServerFactory} implements L{IProtocolFactory}.
        N)ru   r   r   r   rJ   rM   r   r   r   test_interfaceU     z$DNSServerFactoryTests.test_interfacec                 C   s   |  tjjtj dS )zU
        L{server.DNSServerFactory.protocol} defaults to L{dns.DNSProtocol}.
        N)assertIsr   rJ   r    r   DNSProtocolrM   r   r   r   test_defaultProtocol[  rx   z*DNSServerFactoryTests.test_defaultProtocolc                    s\   G dd d}|   fdd}t  }||_|jdd}|  |fi f||j|jf dS )z
        L{server.DNSServerFactory.buildProtocol} builds a protocol by calling
        L{server.DNSServerFactory.protocol} with its self as a positional
        argument.
        c                   @   s   e Zd ZdZdZdZdS )zNDNSServerFactoryTests.test_buildProtocolProtocolOverride.<locals>.FakeProtocolN)r   r   r   factoryr   r   r   r   r   r   FakeProtocolh  s    r}   c                     s   |  _ | _ S r   r   r   stubProtocolr   r   fakeProtocolFactoryo  s   zUDNSServerFactoryTests.test_buildProtocolProtocolOverride.<locals>.fakeProtocolFactoryN)addr)r   rJ   r    buildProtocolrB   r   r   )r   r}   r   fpr   r~   r   "test_buildProtocolProtocolOverridea  s   "z8DNSServerFactoryTests.test_buildProtocolProtocolOverridec                 C   s   t  }t| g |jd dS )zm
        L{server.DNSServerFactory._verboseLog} does not log messages unless
        C{verbose > 0}.
        Foo BarNr   rJ   rG   _verboseLogr   r   r   r   r   test_verboseLogQuietz  s   z*DNSServerFactoryTests.test_verboseLogQuietc                 C   s"   t jdd}t| dg|jd dS )zZ
        L{server.DNSServerFactory._verboseLog} logs a message if C{verbose > 0}.
           rt   r   Nr   r   r   r   r   test_verboseLogVerbose  s   z,DNSServerFactoryTests.test_verboseLogVerbosec                 C   s.   t  }tdd}t| dg|j|ddd dS )z
        L{server.DNSServerFactory.messageReceived} logs about an empty query if
        the message had no queries and C{verbose} is C{>0}.
        r   rt   z$Empty query from ('192.0.2.100', 53)Nz192.0.2.1005   r   protor!   )r   Messager   rG   messageReceivedr   r<   r   r   r   r   "test_messageReceivedLoggingNoQuery  s   

z8DNSServerFactoryTests.test_messageReceivedLoggingNoQueryc                 C   N   t  }|jdt jd |jdt jd tdd}t| dg|j|ddd dS )	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typer   rt   z&MX AAAA query from ('192.0.2.100', 53)Nr   r   r   r   addQueryMXAAAAr   rG   r   r   r   r   r   test_messageReceivedLogging1  s   

z2DNSServerFactoryTests.test_messageReceivedLogging1c                 C   r   )	z
        L{server.DNSServerFactory.messageReceived} logs the repr of all queries
        in the message if C{verbose} is set to C{2}.
        r   r      rt   zT<Query example.com MX IN> <Query example.com AAAA IN> query from ('192.0.2.100', 53)Nr   r   r   r   r   r   r   test_messageReceivedLogging2  s   

z2DNSServerFactoryTests.test_messageReceivedLogging2c                    sN   t  }t }t  | tjd fdd |j|ddd | |j	  dS )zv
        L{server.DNSServerFactory.messageReceived} assigns a unix timestamp to
        the received message.
        timec                          S r   r   r   tr   r   <lambda>  r$   zEDNSServerFactoryTests.test_messageReceivedTimestamp.<locals>.<lambda>Nr   )
r   r   r   rV   patchr   r   r   rB   timeReceivedr   r   r   r   test_messageReceivedTimestamp  s   z3DNSServerFactoryTests.test_messageReceivedTimestampc                 C   s^   t  }t }t }t }| jtj|j|||d}|j\}}| ||||f | |i  dS )z
        L{server.DNSServerFactory.messageReceived} passes all messages to
        L{server.DNSServerFactory.allowQuery} along with the receiving protocol
        and origin address.
        r   N)	r   r   rV   r&   assertRaisesr*   r   r   rB   )r   r   dummyProtocoldummyAddressr   er   r   r   r   r   test_messageReceivedAllowQuery  s   
z4DNSServerFactoryTests.test_messageReceivedAllowQueryc                    sj   G dd dt  G  fdddtj}| }| j |jt ddd}|j\\}}}}| |j	tj
 dS )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                   @   ra   )zFDNSServerFactoryTests.test_allowQueryFalse.<locals>.SendReplyExceptionNrb   r   r   r   r   SendReplyException  rd   r   c                       s    e Zd Zdd Z fddZdS )zKDNSServerFactoryTests.test_allowQueryFalse.<locals>.RaisingDNSServerFactoryc                 _   r   )NFr   r   r   r   r   r"        zVDNSServerFactoryTests.test_allowQueryFalse.<locals>.RaisingDNSServerFactory.allowQueryc                    s
    ||r   r   r   r   r   r   r#     s   
zUDNSServerFactoryTests.test_allowQueryFalse.<locals>.RaisingDNSServerFactory.sendReplyN)r   r   r   r"   r#   r   r   r   r   r&     s    r&   Nr   )r-   r   rJ   r   r   r   r   r   rB   rCodeEREFUSED)r   r&   r   r   r   r   r!   r   r   r   r   test_allowQueryFalse  s   z*DNSServerFactoryTests.test_allowQueryFalsec                    sX   dg|_ g   fdd}t }td}t||| ||| |  ||dfg dS )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}
        Nc                    s     | ||f d S r   r?   r   r    r!   receivedMessagesr   r   fakeHandler  s   z?DNSServerFactoryTests._messageReceivedTest.<locals>.fakeHandler)queriesr4   r   rJ   setattrr   rB   )r   
methodNamer   r   r    r|   r   r   r   _messageReceivedTest  s   
z*DNSServerFactoryTests._messageReceivedTestc                 C      |  dtjtjd dS )z
        L{DNSServerFactory.messageReceived} passes messages with an opcode of
        C{OP_QUERY} on to L{DNSServerFactory.handleQuery}.
        handleQueryopCodeN)r   r   r   OP_QUERYrM   r   r   r   test_queryMessageReceived     z/DNSServerFactoryTests.test_queryMessageReceivedc                 C   r   )z
        L{DNSServerFactory.messageReceived} passes messages with an opcode of
        C{OP_INVERSE} on to L{DNSServerFactory.handleInverseQuery}.
        handleInverseQueryr   N)r   r   r   
OP_INVERSErM   r   r   r    test_inverseQueryMessageReceived&  s   z6DNSServerFactoryTests.test_inverseQueryMessageReceivedc                 C   r   )z
        L{DNSServerFactory.messageReceived} passes messages with an opcode of
        C{OP_STATUS} on to L{DNSServerFactory.handleStatus}.
        handleStatusr   N)r   r   r   	OP_STATUSrM   r   r   r   test_statusMessageReceived/  r   z0DNSServerFactoryTests.test_statusMessageReceivedc                 C   r   )z
        L{DNSServerFactory.messageReceived} passes messages with an opcode of
        C{OP_NOTIFY} on to L{DNSServerFactory.handleNotify}.
        handleNotifyr   N)r   r   r   	OP_NOTIFYrM   r   r   r   test_notifyMessageReceived6  r   z0DNSServerFactoryTests.test_notifyMessageReceivedc                 C   r   )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   r   r   	OP_UPDATErM   r   r   r   test_updateMessageReceived=  s   z0DNSServerFactoryTests.test_updateMessageReceivedc                 C   s   t  t  }}t }|| | |j|g || | |j||g || | |j|g || | |jg  dS )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)rV   r   rJ   connectionMaderB   connectionsconnectionLost)r   protoAprotoBr|   r   r   r   test_connectionTrackingF  s   



z-DNSServerFactoryTests.test_connectionTrackingc                 C   sh   t  }|d |d t }t |_| jtj|j	|t
 dd}|j\\}}| ||jd  dS )z
        L{server.DNSServerFactory.handleQuery} takes the first query from the
        supplied message and dispatches it to
        L{server.DNSServerFactory.resolver.query}.
           one.example.coms   two.example.comNr   r   )r   r   r   r   rJ   r5   rK   r   r6   r   r4   r   rB   r   )r   r<   r   r   r7   r   r   r   r   test_handleQueryW  s   

z&DNSServerFactoryTests.test_handleQueryc                    s   t  }t  G  fddd}| |_g fdd}||_t }|d t	 }t
 }|j|||d t
 } | | ||||fi fg dS )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.
        c                          e Zd Z fddZdS )zDDNSServerFactoryTests.test_handleQueryCallback.<locals>.FakeResolverc                    r   r   r   r   dr   r   r7   z  r   zJDNSServerFactoryTests.test_handleQueryCallback.<locals>.FakeResolver.queryNr   r   r   r7   r   r   r   r   FakeResolvery      r   c                          | |f d S r   r   r   )gotResolverResponseArgsr   r   fakeGotResolverResponse     zODNSServerFactoryTests.test_handleQueryCallback.<locals>.fakeGotResolverResponser   r   N)r   rJ   r   DeferredrK   gotResolverResponser   r   r   r4   rV   r   callbackrB   )r   r   r   r   r<   r   r   dummyResponser   )r   r   r   test_handleQueryCallbackm  s$   

z.DNSServerFactoryTests.test_handleQueryCallbackc                    s   t  }t  G  fddd}| |_g fdd}||_t }|d t	 }t
 }|j|||d tt } | | ||||fi fg dS )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.
        c                       r   )zCDNSServerFactoryTests.test_handleQueryErrback.<locals>.FakeResolverc                    r   r   r   r   r   r   r   r7     r   zIDNSServerFactoryTests.test_handleQueryErrback.<locals>.FakeResolver.queryNr   r   r   r   r   r     r   r   c                     r   r   r   r   )gotResolverErrorArgsr   r   fakeGotResolverError  r   zKDNSServerFactoryTests.test_handleQueryErrback.<locals>.fakeGotResolverErrorr   r   N)r   rJ   r   r   rK   gotResolverErrorr   r   r   r4   rV   r   r	   Failurer-   errbackrB   )r   r   r   r   r<   r   r   stubFailurer   )r   r   r   test_handleQueryErrback  s"   

z-DNSServerFactoryTests.test_handleQueryErrbackc                 C   st   t  }g }g }g }| jtj|j|||ft t dd}|j\\}}| 	|j
| | 	|j| | 	|j| dS )z
        L{server.DNSServerFactory.gotResolverResponse} accepts a tuple of
        resource record lists and triggers a response message containing those
        resource record lists.
        Nr    r   r!   )r   rJ   r   r.   r/   r   r   r   r   ry   answers	authority
additional)r   r   r   r   r   r   r   r   r   r   r   test_gotResolverResponse  s    z.DNSServerFactoryTests.test_gotResolverResponsec              
   C   sd   t  }t|_t }d|_| jt|jg g g fd|dd}| 	dt
|tjg g g df|j|jf dS )z
        L{server.DNSServerFactory.gotResolverResponse} calls
        L{server.DNSServerFactory._responseFromMessage} to generate a response.
        r   Nr   r   )r   r   r   r   r   )r   r   _responseFromMessager   r   r   r   r   r   rB   dictOKr   r   r   r|   requestr   r   r   r   0test_gotResolverResponseCallsResponseFromMessage  s0   

zFDNSServerFactoryTests.test_gotResolverResponseCallsResponseFromMessagec                 C   s4   t  }tjddd}|j|df}| || dS )z
        L{server.DNSServerFactory._responseFromMessage} generates a response
        message which is a copy of the request message.
        F)answerrecAvr   N)r   rJ   r   r   r   assertIsNotr   r|   r   responser   r   r   "test_responseFromMessageNewMessage  s   z8DNSServerFactoryTests.test_responseFromMessageNewMessagec                 C   sT   t  }d|_|jtjddd}d|_|jtjddd}| d|j|jf dS )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   rJ   ro   r   r   r   rB   r   r   r|   	response1	response2r   r   r   *test_responseFromMessageRecursionAvailable   s   z@DNSServerFactoryTests.test_responseFromMessageRecursionAvailablec                 C   6   t  }t }d|_|j|d}| |j|j dS )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   rJ   r   r   r   r   rB   r   r   r   r   $test_responseFromMessageTimeReceived  
   z:DNSServerFactoryTests.test_responseFromMessageTimeReceivedc                 C   r  )z
        L{server.DNSServerFactory._responseFromMessage} generates a response
        message whose C{maxSize} attribute has the same value as that found
        on the request.
        r   r   N)r   rJ   r   r   maxSizer   rB   r   r   r   r   test_responseFromMessageMaxSize  r  z5DNSServerFactoryTests.test_responseFromMessageMaxSizec                 C   s   |  tjtjj dS )z{
        L{server.DNSServerFactory} has a C{_messageFactory} attribute which is
        L{dns.Message} by default.
        N)ry   r   r   r   rJ   _messageFactoryrM   r   r   r   test_messageFactory'     z)DNSServerFactoryTests.test_messageFactoryc              
   C   sd   t  }| tdt t }| jt|j|tj	d}| 
dt|j|tj	|jddf|j|jf dS )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   r   r   r   F)responseConstructorr   r   r   authN)r   rJ   r   r   r   r   r   r   r   r   rB   r   r
  ro   r   r   r   r   r   r   +test_responseFromMessageCallsMessageFactory.  s$   

zADNSServerFactoryTests.test_responseFromMessageCallsMessageFactoryc                 C   sX   t  }|jt tjddgd}|jt tjddgd}| d|j|jf dS )z
        L{server.DNSServerFactory._responseFromMessage} marks the response
        message as authoritative if any of the answer records are authoritative.
        T)r  )r   r   Fr   N)r   rJ   r   r   r   RRHeaderrB   r  r  r   r   r   ,test_responseFromMessageAuthoritativeMessageK  s   
zBDNSServerFactoryTests.test_responseFromMessageAuthoritativeMessagec              	   C   sR   t dd}t g}t g}t g}t| dg|j|||ft t dd dS )z
        L{server.DNSServerFactory.gotResolverResponse} logs the total number of
        records in the response if C{verbose > 0}.
        r   rt   zLookup found 3 recordsNr   )r   r   r  rG   r   r4   r   )r   r   r   r   r   r   r   r   test_gotResolverResponseLogging\  s   




z5DNSServerFactoryTests.test_gotResolverResponseLoggingc                 C   s   t t gd}t }|d t g}g }g }| jtj|j|||ft	 |dd}|j
\\}\}}	}
}| |jjd | || | |	| | |
| dS )z
        L{server.DNSServerFactory.gotResolverResponse} caches the response if at
        least one cache was provided in the constructor.
        r[   s   example.comNr   )r   r8   r   r   r   r  r   r9   r   r4   r   rB   r   ry   )r   r   r<   expectedAnswersexpectedAuthorityexpectedAdditionalr   r7   r   r   r   r   r   r   r   test_gotResolverResponseCachingp  s&   

z5DNSServerFactoryTests.test_gotResolverResponseCachingc                 C   sb   t  }t|_t }d|_| jt|jt	
t d|dd}| dt|tjdf|j|jf dS )z
        L{server.DNSServerFactory.gotResolverError} calls
        L{server.DNSServerFactory._responseFromMessage} to generate a response.
        r   Nr   r   r  )r   r   r   r   r   r   r   r   r   r	   r   r   DomainErrorrB   r   ENAMEr   r   r   r   r   r   -test_gotResolverErrorCallsResponseFromMessage  s   zCDNSServerFactoryTests.test_gotResolverErrorCallsResponseFromMessagec                 C   sL   t  }| jtj|jt|t t	 dd}|j
\\}}| |j| dS )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   rJ   r   r.   r/   r   r	   r   r   r   r   rB   r   )r   responseErrorexpectedMessageCoder   r   r   r   r   r   r   _assertMessageRcodeForError  s   z1DNSServerFactoryTests._assertMessageRcodeForErrorc                 C      |  t tj dS )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  r   r  r   r  rM   r   r   r    test_gotResolverErrorDomainError  r  z6DNSServerFactoryTests.test_gotResolverErrorDomainErrorc                 C   r  )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  r   AuthoritativeDomainErrorr   r  rM   r   r   r   -test_gotResolverErrorAuthoritativeDomainError  s   zCDNSServerFactoryTests.test_gotResolverErrorAuthoritativeDomainErrorc                 C   s.   |  t tj | t}| t|d dS )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.
        r   N)r  KeyErrorr   ESERVERflushLoggedErrorsrB   len)r   r   r   r   r   test_gotResolverErrorOtherError  s   
z5DNSServerFactoryTests.test_gotResolverErrorOtherErrorc              	   C   s8   t dd}t| dg|jtt t t	 dd dS )zZ
        L{server.DNSServerFactory.gotResolver} logs a message if C{verbose > 0}.
        r   rt   zLookup failedNr   )
r   rG   r   r	   r   r   r  r4   r   r   r   r   r   r   test_gotResolverErrorLogging  s   

z2DNSServerFactoryTests.test_gotResolverErrorLoggingc                    s   t  }g   fdd|_tjddd}t t g|_t t g|_t t g|_|j	t
t d|dd | tjdddg  dS )	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.
        c                    
     |S r   r   r    r   r!   	responsesr   r   r         zUDNSServerFactoryTests.test_gotResolverErrorResetsResponseAttributes.<locals>.<lambda>TauthenticDatacheckingDisabledNr      r   r   )r   rJ   r#   r   r   rV   r   r   r   r   r	   r   r   r  rB   r   r|   r   r   r*  r   -test_gotResolverErrorResetsResponseAttributes  s   zCDNSServerFactoryTests.test_gotResolverErrorResetsResponseAttributesc                    s   t  }g   fdd|_tjddd}t t g|_t t g|_t t g|_|j	g g g fd|dd | 
tjdddg  dS )	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.
        c                    r(  r   r   r)  r*  r   r   r   	  r,  zXDNSServerFactoryTests.test_gotResolverResponseResetsResponseAttributes.<locals>.<lambda>Tr-  Nr   r   r1  )r   rJ   r#   r   r   rV   r   r   r   r   rB   r2  r   r*  r   0test_gotResolverResponseResetsResponseAttributes   s   zFDNSServerFactoryTests.test_gotResolverResponseResetsResponseAttributesc                 C   sZ   t  }t }t }| jtj|jt ||d}|j	\}}| 
|||f | 
|i  dS )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)r   r   rV   r   rJ   r   r.   r/   r#   r   rB   )r   r<   r   r   r   r   r   r   r   r   test_sendReplyWithAddress  s   
z/DNSServerFactoryTests.test_sendReplyWithAddressc                 C   sR   t  }t }| jtj|jt |dd}|j\}}| 	||f | 	|i  dS )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   )
r   r   r   rJ   r   r.   r/   r#   r   rB   )r   r<   r   r   r   r   r   r   r   test_sendReplyWithoutAddress+  s   
z2DNSServerFactoryTests.test_sendReplyWithoutAddressc                 C   sN   |  tjddd  t }d|_tjdd}t| ddg|jt	 |d	d
 d	S )z
        If L{server.DNSServerFactory.sendReply} logs a "no answers" message if
        the supplied message has no answers.
        r   c                   S   r   NiQ r   r   r   r   r   r   C  r$   zFDNSServerFactoryTests.test_sendReplyLoggingNoAnswers.<locals>.<lambda>Q r   rt   zReplying with no answers Processed query in 1.000 secondsNr   )
r   r   r   r   r   r   rJ   rG   r#   r4   r   r   r   r   test_sendReplyLoggingNoAnswers>  s   
z4DNSServerFactoryTests.test_sendReplyLoggingNoAnswersc                 C   s   |  tjddd  t }|jtjtdd |j	tjtdd |j
tjtdd d|_tjdd}t| g d	|jt |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.
        r   c                   S   r   r7  r   r   r   r   r   r   V  r$   zHDNSServerFactoryTests.test_sendReplyLoggingWithAnswers.<locals>.<lambda>z	127.0.0.1)payloadr8  r   rt   )z*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>r9  Nr   )r   r   r   r   r   r   r?   r  Record_Ar   r   r   rJ   rG   r#   r4   r   r   r   r    test_sendReplyLoggingWithAnswersP  s   
z6DNSServerFactoryTests.test_sendReplyLoggingWithAnswersc                 C   F   t  }| jtj|jt t dd}|j\\}}| 	|j
tj dS )z
        L{server.DNSServerFactory.handleInverseQuery} triggers the sending of a
        response message with C{rCode} set to L{dns.ENOTIMP}.
        Nr   )r   rJ   r   r.   r/   r   r   r   r   rB   r   ENOTIMPr   r   r   r   r   r   r   r   test_handleInverseQueryk     z-DNSServerFactoryTests.test_handleInverseQueryc                 C   ,   t dd}t| dg|jt t dd dS )zz
        L{server.DNSServerFactory.handleInverseQuery} logs the message origin
        address if C{verbose > 0}.
        r   rt   zInverse query from ('::1', 53)z::1r   r   N)r   rG   r   r   r   r4   r   r   r   r   test_handleInverseQueryLogging|     

z4DNSServerFactoryTests.test_handleInverseQueryLoggingc                 C   r>  )z
        L{server.DNSServerFactory.handleStatus} triggers the sending of a
        response message with C{rCode} set to L{dns.ENOTIMP}.
        Nr   )r   rJ   r   r.   r/   r   r   r   r   rB   r   r?  r@  r   r   r   test_handleStatus  rB  z'DNSServerFactoryTests.test_handleStatusc                 C   rC  )zt
        L{server.DNSServerFactory.handleStatus} logs the message origin address
        if C{verbose > 0}.
        r   rt   zStatus request from ('::1', 53)rD  r   N)r   rG   r   r   r   r4   r   r   r   r   test_handleStatusLogging  rF  z.DNSServerFactoryTests.test_handleStatusLoggingc                 C   r>  )z
        L{server.DNSServerFactory.handleNotify} triggers the sending of a
        response message with C{rCode} set to L{dns.ENOTIMP}.
        Nr   )r   rJ   r   r.   r/   r   r   r   r   rB   r   r?  r@  r   r   r   test_handleNotify  rB  z'DNSServerFactoryTests.test_handleNotifyc                 C   rC  )zt
        L{server.DNSServerFactory.handleNotify} logs the message origin address
        if C{verbose > 0}.
        r   rt   zNotify message from ('::1', 53)rD  r   N)r   rG   r   r   r   r4   r   r   r   r   test_handleNotifyLogging  rF  z.DNSServerFactoryTests.test_handleNotifyLoggingc                 C   r>  )z
        L{server.DNSServerFactory.handleOther} triggers the sending of a
        response message with C{rCode} set to L{dns.ENOTIMP}.
        Nr   )r   rJ   r   r.   r/   r   r   r   r   rB   r   r?  r@  r   r   r   test_handleOther  rB  z&DNSServerFactoryTests.test_handleOtherc                 C   rC  )zs
        L{server.DNSServerFactory.handleOther} logs the message origin address
        if C{verbose > 0}.
        r   rt   z$Unknown op code (0) from ('::1', 53)rD  r   N)r   rG   r   r   r   r4   r   r   r   r   test_handleOtherLogging  rF  z-DNSServerFactoryTests.test_handleOtherLoggingN)Dr   r   r   r   rN   rR   rY   r]   r`   rg   rk   rm   rp   rq   rs   rv   rw   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'  r3  r4  r5  r6  r:  r=  rA  rE  rG  rH  rI  rJ  rK  rL  r   r   r   r   rH      s    		('!
rH   N)r   zope.interface.verifyr   twisted.internetr   twisted.internet.interfacesr   twisted.namesr   r   r   r   twisted.pythonr	   r
   twisted.trialr   r-   r   r   rJ   r   r&   r.   r4   r5   r8   rG   TestCaserH   r   r   r   r   <module>   s"   
	! 