
    VhȢ                     P   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	e      Zd
 Z G d dej(                        Z G d dej(                        Z G d d      Z G d d      Z G d d      Z G d d      Zd Z G d dej8                        Zy)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 Zy)RaisedArgumentszD
    An exception containing the arguments raised by L{raiser}.
    c                      || _         || _        y Nargskwargsselfr   r   s      N/home/dcms/DCMS/lib/python3.12/site-packages/twisted/names/test/test_server.py__init__zRaisedArguments.__init__   s    	    N)__name__
__module____qualname____doc__r    r   r   r   r      s    r   r   c                      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   s     r   raiserr      s     $
''r   c                       e Zd ZdZd Zd Zy)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                      y)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addresss       r   
allowQueryz%NoResponseDNSServerFactory.allowQuery.   s     r   c                      y)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%   s       r   	sendReplyz$NoResponseDNSServerFactory.sendReply;       r   N)r   r   r   r   r&   r(   r   r   r   r!   r!   $   s    r   r!   c                   ,    e Zd ZdZ G d de      Zd Zy)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y)+RaisingDNSServerFactory.AllowQueryArgumentsG
        Contains positional and keyword arguments in C{args}.
        Nr   r   r   r   r   r   r   AllowQueryArgumentsr-   N       	r   r0   c                 &    | j                  ||      )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}
        )r0   r   s      r   r&   z"RaisingDNSServerFactory.allowQueryS   s     &&tV44r   N)r   r   r   r   	Exceptionr0   r&   r   r   r   r+   r+   E   s    i 
5r   r+   c                   ,    e Zd ZdZ G d de      Zd Zy)RaisingProtocolzm
    A partial fake L{IProtocol} whose methods raise an exception containing the
    supplied arguments.
    c                       e Zd ZdZy)%RaisingProtocol.WriteMessageArgumentsr.   Nr/   r   r   r   WriteMessageArgumentsr7   h   r1   r   r8   c                 &    | j                  ||      z
        Raises the supplied arguments.

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

        @param kwargs: Keyword args
        @type kwargs: L{dict}
        )r8   r   s      r   writeMessagezRaisingProtocol.writeMessagem   s     ((v66r   N)r   r   r   r   r3   r8   r;   r   r   r   r5   r5   b   s    
	 

7r   r5   c                       e Zd ZdZd Zy)NoopProtocolzT
    A partial fake L{dns.DNSProtocolMixin} with a noop L{writeMessage} method.
    c                      y)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   s      r   r;   zNoopProtocol.writeMessage   r)   r   N)r   r   r   r   r;   r   r   r   r=   r=   z   s    	r   r=   c                   ,    e Zd ZdZ G d de      Zd Zy)RaisingResolverzm
    A partial fake L{IResolver} whose methods raise an exception containing the
    supplied arguments.
    c                       e Zd ZdZy)RaisingResolver.QueryArgumentsr.   Nr/   r   r   r   QueryArgumentsrB      r1   r   rC   c                 &    | j                  ||      r:   )rC   r   s      r   queryzRaisingResolver.query   s     !!$//r   N)r   r   r   r   r3   rC   rE   r   r   r   r@   r@      s    
 

0r   r@   c                   ,    e Zd ZdZ G d de      Zd Zy)RaisingCachez}
    A partial fake L{twisted.names.cache.Cache} whose methods raise an exception
    containing the supplied arguments.
    c                       e Zd ZdZy)!RaisingCache.CacheResultArgumentsr.   Nr/   r   r   r   CacheResultArgumentsrI      r1   r   rJ   c                 &    | j                  ||      r:   )rJ   r   s      r   cacheResultzRaisingCache.cacheResult   s     ''f55r   N)r   r   r   r   r3   rJ   rL   r   r   r   rG   rG      s    
y 

6r   rG   c                     g }t        j                  |j                         | j                  t         j                  |j                          ||i | | j                  |D cg c]
  }|d   d    c}|       yc c}w )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}
    r#   r   N)r   addObserverappend
addCleanupremoveObserverassertEqual)testCaseexpectedMessagescallabler   r   loggedMessagesms          r   assertLogMessagerX      sk    . NOON))***N,A,ABdf>Ba!I,q/BDTUBs   #A;c                      e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d  Z"d! Z#d" Z$d# Z%d$ Z&d% Z'd& Z(d' Z)d( Z*d) Z+d* Z,d+ Z-d, Z.d- Z/d. Z0d/ Z1d0 Z2d1 Z3d2 Z4d3 Z5d4 Z6d5 Z7d6 Z8d7 Z9d8 Z:d9 Z;d: Z<d; Z=d< Z>d= Z?d> Z@d? ZAd@ ZBdA ZCyB)CDNSServerFactoryTestsz/
    Tests for L{server.DNSServerFactory}.
    c                 |    | j                  t        j                         j                  t        j
                         y)zd
        L{server.DNSServerFactory.resolver} is a L{resolve.ResolverChain}
        instance
        N)assertIsInstancer	   DNSServerFactoryresolverr   ResolverChainr   s    r   test_resolverTypez'DNSServerFactoryTests.test_resolverType   s)    
 	f557@@'BWBWXr   c                 t    | j                  t        j                         j                  j                  g        y)zn
        L{server.DNSServerFactory.resolver} is an empty L{resolve.ResolverChain}
        by default.
        NrR   r	   r]   r^   	resolversr`   s    r   test_resolverDefaultEmptyz/DNSServerFactoryTests.test_resolverDefaultEmpty   s)    
 	002;;EErJr   c                     t               }| j                  t        j                  |g      j                  j
                  |g       y)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objectrR   r	   r]   r^   rd   r   dummyResolvers     r   test_authoritiesz&DNSServerFactoryTests.test_authorities   s:     ##@IISSO	
r   c                     t               }| j                  t        j                  |g      j                  j
                  |g       y)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}.
        cachesNrh   rj   s     r   test_cachesz!DNSServerFactoryTests.test_caches   s:     ##M?;DDNNO	
r   c                     t               }| j                  t        j                  |g      j                  j
                  |g       y)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}.
        clientsNrh   rj   s     r   test_clientsz"DNSServerFactoryTests.test_clients  s:     ##]O<EEOOO	
r   c                      G d d      } G d d      } G d d      }| j                  t        j                  |g|g|g      j                  j                  |||g       y)	zz
        L{server.DNSServerFactory.resolver} contains an ordered list of
        authorities, caches and clients.
        c                       e Zd Zy)@DNSServerFactoryTests.test_resolverOrder.<locals>.DummyAuthorityNr   r   r   r   r   r   DummyAuthorityrw         r   ry   c                       e Zd Zy)<DNSServerFactoryTests.test_resolverOrder.<locals>.DummyCacheNrx   r   r   r   
DummyCacher|     rz   r   r}   c                       e Zd Zy)=DNSServerFactoryTests.test_resolverOrder.<locals>.DummyClientNrx   r   r   r   DummyClientr     rz   r   r   )rg   ro   rs   Nrc   )r   ry   r}   r   s       r   test_resolverOrderz(DNSServerFactoryTests.test_resolverOrder  s_    	 		 		 	 	##+,j\K=hyyZ5		
r   c                 ^    | j                  t        j                         j                         y)zI
        L{server.DNSServerFactory.cache} is L{None} by default.
        N)assertIsNoner	   r]   cacher`   s    r   test_cacheDefaultz'DNSServerFactoryTests.test_cacheDefault)  s!     	&11399:r   c                     t               }| j                  t        j                  t               |g      j                  |       y)z
        L{server.DNSServerFactory.__init__} assigns the last object in the
        C{caches} list to L{server.DNSServerFactory.cache}.
        rn   N)ri   rR   r	   r]   r   rj   s     r   test_cacheOverridez(DNSServerFactoryTests.test_cacheOverride/  s7    
 ##FHm+DEKK	
r   c                 ^    | j                  t        j                         j                         y)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`   s    r   test_canRecurseDefaultz,DNSServerFactoryTests.test_canRecurseDefault:  s!     	002==>r   c                 f    | j                  t        j                  dg      j                  d       y)z~
        L{server.DNSServerFactory.__init__} sets C{canRecurse} to L{True} if it
        is supplied with C{clients}.
        Nrr   T)rR   r	   r]   r   r`   s    r   test_canRecurseOverridez-DNSServerFactoryTests.test_canRecurseOverrideB  s'    
 	00$@KKTRr   c                 ^    | j                  t        j                         j                         y)zJ
        L{server.DNSServerFactory.verbose} defaults to L{False}.
        N)r   r	   r]   verboser`   s    r   test_verboseDefaultz)DNSServerFactoryTests.test_verboseDefaultI  s!     	002::;r   c                 b    | j                  t        j                  d      j                         y)z
        L{server.DNSServerFactory.__init__} accepts a C{verbose} argument which
        overrides L{server.DNSServerFactory.verbose}.
        Tr   N)
assertTruer	   r]   r   r`   s    r   test_verboseOverridez*DNSServerFactoryTests.test_verboseOverrideO  s!    
 	//=EEFr   c                 ^    | j                  t        t        t        j                               y)zL
        L{server.DNSServerFactory} implements L{IProtocolFactory}.
        N)r   r   r   r	   r]   r`   s    r   test_interfacez$DNSServerFactoryTests.test_interfaceV  s     	$4f6M6MNOr   c                 t    | j                  t        j                  j                  t        j
                         y)zU
        L{server.DNSServerFactory.protocol} defaults to L{dns.DNSProtocol}.
        N)assertIsr	   r]   r$   r   DNSProtocolr`   s    r   test_defaultProtocolz*DNSServerFactoryTests.test_defaultProtocol\  s"     	f--66Hr   c                      G d d      } |       fd}t        j                         }||_        |j                  d      }| j	                  |fi f||j
                  |j                  f       y)z
        L{server.DNSServerFactory.buildProtocol} builds a protocol by calling
        L{server.DNSServerFactory.protocol} with its self as a positional
        argument.
        c                       e Zd ZdZdZdZy)NDNSServerFactoryTests.test_buildProtocolProtocolOverride.<locals>.FakeProtocolN)r   r   r   factoryr   r   r   r   r   FakeProtocolr   i  s    GDFr   r   c                  $    | _         |_        S r   r   )r   r   stubProtocols     r   fakeProtocolFactoryzUDNSServerFactoryTests.test_buildProtocolProtocolOverride.<locals>.fakeProtocolFactoryp  s     $L"(Lr   N)addr)r	   r]   r$   buildProtocolrR   r   r   )r   r   r   fpr   s        @r   "test_buildProtocolProtocolOverridez8DNSServerFactoryTests.test_buildProtocolProtocolOverrideb  sj    	 	
 $~	 
 ##%(
OOO&,b1Aqvvqxx3HIr   c                 \    t        j                         }t        | g |j                  d       y)zm
        L{server.DNSServerFactory._verboseLog} does not log messages unless
        C{verbose > 0}.
        Foo BarNr	   r]   rX   _verboseLogr   r   s     r   test_verboseLogQuietz*DNSServerFactoryTests.test_verboseLogQuiet{  s$    
 ##%r1==)<r   c                 b    t        j                  d      }t        | dg|j                  d       y)zZ
        L{server.DNSServerFactory._verboseLog} logs a message if C{verbose > 0}.
           r   r   Nr   r   s     r   test_verboseLogVerbosez,DNSServerFactoryTests.test_verboseLogVerbose  s(     ##A.	{AMM9Er   c                 |    t        j                         }t        d      }t        | dg|j                  |dd       y)z
        L{server.DNSServerFactory.messageReceived} logs about an empty query if
        the message had no queries and C{verbose} is C{>0}.
        r   r   z$Empty query from ('192.0.2.100', 53)Nz192.0.2.1005   r#   protor%   )r   Messager!   rX   messageReceivedr   rW   r   s      r   "test_messageReceivedLoggingNoQueryz8DNSServerFactoryTests.test_messageReceivedLoggingNoQuery  s<    
 KKM&q134'	
r   c                     t        j                         }|j                  dt         j                         |j                  dt         j                         t        d      }t        | dg|j                  |dd       y)	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   r   z&MX AAAA query from ('192.0.2.100', 53)Nr   r   r   r   addQueryMXAAAAr!   rX   r   r   s      r   test_messageReceivedLogging1z2DNSServerFactoryTests.test_messageReceivedLogging1  sd    
 KKM	

CFF
3	

CHH
5&q156'	
r   c                     t        j                         }|j                  dt         j                         |j                  dt         j                         t        d      }t        | dg|j                  |dd       y)	z
        L{server.DNSServerFactory.messageReceived} logs the repr of all queries
        in the message if C{verbose} is set to C{2}.
        r   r      r   zT<Query example.com MX IN> <Query example.com AAAA IN> query from ('192.0.2.100', 53)Nr   r   r   r   s      r   test_messageReceivedLogging2z2DNSServerFactoryTests.test_messageReceivedLogging2  sk    
 KKM	

CFF
3	

CHH
5&q1M '
	
r   c                     t        j                         }t               }t               | j	                  t
        j                  dfd       |j                  |dd       | j                  |j                         y)zv
        L{server.DNSServerFactory.messageReceived} assigns a unix timestamp to
        the received message.
        timec                       S r   r   )ts   r   <lambda>zEDNSServerFactoryTests.test_messageReceivedTimestamp.<locals>.<lambda>  s     r   Nr   )
r   r   r!   ri   patchr	   r   r   rR   timeReceived)r   rW   r   r   s      @r   test_messageReceivedTimestampz3DNSServerFactoryTests.test_messageReceivedTimestamp  s\    
 KKM&(H

6;;	2	!4>+r   c                 0   t        j                         }t               }t               }t               }| j	                  t        j
                  |j                  |||      }|j                  \  }}| j                  ||||f       | j                  |i        y)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   ri   r+   assertRaisesr0   r   r   rR   )r   r#   dummyProtocoldummyAddressr   er   r   s           r   test_messageReceivedAllowQueryz4DNSServerFactoryTests.test_messageReceivedAllowQuery  s     ++-x#%#77   
 vvfEF$r   c                 F    G d dt                G fddt        j                        } |       }| j                  |j                  t        j                         dd      }|j                  \  \  }}}}| j                  |j                  t
        j                         y)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                       e Zd Zy)FDNSServerFactoryTests.test_allowQueryFalse.<locals>.SendReplyExceptionNrx   r   r   r   SendReplyExceptionr     rz   r   r   c                       e Zd Zd Z fdZy)KDNSServerFactoryTests.test_allowQueryFalse.<locals>.RaisingDNSServerFactoryc                      y)NFr   r   s      r   r&   zVDNSServerFactoryTests.test_allowQueryFalse.<locals>.RaisingDNSServerFactory.allowQuery  s    r   c                      ||      r   r   )r   r   r   r   s      r   r(   zUDNSServerFactoryTests.test_allowQueryFalse.<locals>.RaisingDNSServerFactory.sendReply  s    (v66r   N)r   r   r   r&   r(   )r   s   r   r+   r     s    7r   r+   Nr   )r3   r	   r]   r   r   r   r   r   rR   rCodeEREFUSED)	r   r+   r   r   r   r#   r%   r   r   s	           @r   test_allowQueryFalsez*DNSServerFactoryTests.test_allowQueryFalse  s    	 		7f&=&= 	7 $%KKM  
 -.FF)!65r   c                     dg|_         g fd}t               }t        j                  d      }t	        |||       |j                  ||       | j                  ||dfg       y)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                 .    j                  | ||f       y r   rO   )r#   r$   r%   receivedMessagess      r   fakeHandlerz?DNSServerFactoryTests._messageReceivedTest.<locals>.fakeHandler  s    ##Wh$@Ar   )queriesr=   r	   r]   setattrr   rR   )r   
methodNamer#   r   r$   r   r   s         @r   _messageReceivedTestz*DNSServerFactoryTests._messageReceivedTest  sj      &	B  >))$/[12)Wh,E+FGr   c                 l    | j                  dt        j                  t        j                               y)z
        L{DNSServerFactory.messageReceived} passes messages with an opcode of
        C{OP_QUERY} on to L{DNSServerFactory.handleQuery}.
        handleQueryopCodeN)r   r   r   OP_QUERYr`   s    r   test_queryMessageReceivedz/DNSServerFactoryTests.test_queryMessageReceived   s!    
 	!!-CLL1QRr   c                 l    | j                  dt        j                  t        j                               y)z
        L{DNSServerFactory.messageReceived} passes messages with an opcode of
        C{OP_INVERSE} on to L{DNSServerFactory.handleInverseQuery}.
        handleInverseQueryr   N)r   r   r   
OP_INVERSEr`   s    r    test_inverseQueryMessageReceivedz6DNSServerFactoryTests.test_inverseQueryMessageReceived'  s$    
 	!! #++S^^"D	
r   c                 l    | j                  dt        j                  t        j                               y)z
        L{DNSServerFactory.messageReceived} passes messages with an opcode of
        C{OP_STATUS} on to L{DNSServerFactory.handleStatus}.
        handleStatusr   N)r   r   r   	OP_STATUSr`   s    r   test_statusMessageReceivedz0DNSServerFactoryTests.test_statusMessageReceived0  !    
 	!!.#++S]]2STr   c                 l    | j                  dt        j                  t        j                               y)z
        L{DNSServerFactory.messageReceived} passes messages with an opcode of
        C{OP_NOTIFY} on to L{DNSServerFactory.handleNotify}.
        handleNotifyr   N)r   r   r   	OP_NOTIFYr`   s    r   test_notifyMessageReceivedz0DNSServerFactoryTests.test_notifyMessageReceived7  r   r   c                 l    | j                  dt        j                  t        j                               y)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_UPDATEr`   s    r   test_updateMessageReceivedz0DNSServerFactoryTests.test_updateMessageReceived>  s!     	!!-CMM1RSr   c                    t               t               }}t        j                         }|j                  |       | j	                  |j
                  |g       |j                  |       | j	                  |j
                  ||g       |j                  |       | j	                  |j
                  |g       |j                  |       | j	                  |j
                  g        y)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)ri   r	   r]   connectionMaderR   connectionsconnectionLost)r   protoAprotoBr   s       r   test_connectionTrackingz-DNSServerFactoryTests.test_connectionTrackingG  s      68))+v&,,vh7v&,,vv.>?v&,,vh7v&,,b1r   c                    t        j                         }|j                  d       |j                  d       t        j                         }t               |_        | j                  t
        j                  |j                  |t               d      }|j                  \  \  }}| j                  ||j                  d          y)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.comNr#   r$   r%   r   )r   r   r   r	   r]   r@   r^   r   rC   r   r=   r   rR   r   )r   rW   r   r   rE   r   s         r   test_handleQueryz&DNSServerFactoryTests.test_handleQueryX  s     KKM	

%&	

%&##%$&
**MM!^  
 66&		!-r   c                   	 t        j                         }t        j                          G fdd      } |       |_        g 		fd}||_        t        j                         }|j                  d       t               }t               }|j                  |||       t               }j                  |       | j                  	||||fi fg       y)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Zy)DDNSServerFactoryTests.test_handleQueryCallback.<locals>.FakeResolverc                     S r   r   r   r   r   ds      r   rE   zJDNSServerFactoryTests.test_handleQueryCallback.<locals>.FakeResolver.query{      r   Nr   r   r   rE   r  s   r   FakeResolverr  z      r   r  c                  ,    j                  | |f       y r   r   )r   r   gotResolverResponseArgss     r   fakeGotResolverResponsezODNSServerFactoryTests.test_handleQueryCallback.<locals>.fakeGotResolverResponse  s    #**D&>:r   r  r	  N)r	   r]   r   Deferredr^   gotResolverResponser   r   r   r=   ri   r   callbackrR   )
r   r   r  r  rW   r   r   dummyResponser  r  s
           @@r   test_handleQueryCallbackz.DNSServerFactoryTests.test_handleQueryCallbackn  s     ##%NN	 	 "^
"$	; !8KKM	

%&#~x	a,M	

=!#lA|<bAB	
r   c                   	 t        j                         }t        j                          G fdd      } |       |_        g 		fd}||_        t        j                         }|j                  d       t               }t               }|j                  |||       t        j                  t                     }j                  |       | j!                  	||||fi fg       y)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                       e Zd Z fdZy)CDNSServerFactoryTests.test_handleQueryErrback.<locals>.FakeResolverc                     S r   r   r  s      r   rE   zIDNSServerFactoryTests.test_handleQueryErrback.<locals>.FakeResolver.query  r  r   Nr  r  s   r   r  r     r  r   r  c                  ,    j                  | |f       y r   r   )r   r   gotResolverErrorArgss     r   fakeGotResolverErrorzKDNSServerFactoryTests.test_handleQueryErrback.<locals>.fakeGotResolverError  s     ''v7r   r  r	  N)r	   r]   r   r  r^   gotResolverErrorr   r   r   r=   ri   r   r
   Failurer3   errbackrR   )
r   r   r  r$  rW   r   r   stubFailurer  r#  s
           @@r   test_handleQueryErrbackz-DNSServerFactoryTests.test_handleQueryErrback  s     ##%NN	 	 "^
!	8 2KKM	

%&#~x	a,Mooik2			+ [,<$PRT#U"V	
r   c                    t        j                         }g }g }g }| j                  t        j                  |j
                  |||ft               t        j                         d      }|j                  \  \  }}| j                  |j                  |       | j                  |j                  |       | j                  |j                  |       y)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	   r]   r   r5   r8   r  r   r   r   r   answers	authority
additional)r   r   r,  r-  r.  r   r#   r   s           r   test_gotResolverResponsez.DNSServerFactoryTests.test_gotResolverResponse  s     ##%	
11!!i,$&KKM  
 VV
Fgoow/g''3g((*5r   c           
      @   t               }t        |_        t        j                         }d|_        | j                  t        |j                  g g g fd|d      }| j                  dt        |t        j                  g g g       f|j                  |j                  f       y)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  rR   dictOKr   r   r   r   requestr   s       r   0test_gotResolverResponseCallsResponseFromMessagezFDNSServerFactoryTests.test_gotResolverResponseCallsResponseFromMessage  s    
 -.'-$++- ''RL  
 	#&& !	 VVQXX	
r   c                     t        j                         }t        j                  dd      }|j	                  |      f}| j                  ||       y)z
        L{server.DNSServerFactory._responseFromMessage} generates a response
        message which is a copy of the request message.
        F)answerrecAvr#   N)r	   r]   r   r   r1  assertIsNotr   r   r5  responses       r   "test_responseFromMessageNewMessagez8DNSServerFactoryTests.test_responseFromMessageNewMessage  sH    
 ))+++U%8000AC(+r   c                 0   t        j                         }d|_        |j                  t	        j
                  d            }d|_        |j                  t	        j
                  d            }| j                  d|j                  |j                  f       y)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)r9  r:  TFN)r	   r]   r   r1  r   r   rR   r9  r   r   	response1	response2s       r   *test_responseFromMessageRecursionAvailablez@DNSServerFactoryTests.test_responseFromMessageRecursionAvailable  sw     ))+!0059Q0R	"0049P0Q	)//(JKr   c                     t        j                         }t        j                         }d|_        |j                  |      }| j                  |j                  |j                         y)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]   r   r   r   r1  rR   r<  s       r   $test_responseFromMessageTimeReceivedz:DNSServerFactoryTests.test_responseFromMessageTimeReceived  sT     ))+++-#///@--x/D/DEr   c                     t        j                         }t        j                         }d|_        |j                  |      }| j                  |j                  |j                         y)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	   r]   r   r   maxSizer1  rR   r<  s       r   test_responseFromMessageMaxSizez5DNSServerFactoryTests.test_responseFromMessageMaxSize  sQ     ))+++-///@(*:*:;r   c                 t    | j                  t        j                  t        j                  j
                         y)z{
        L{server.DNSServerFactory} has a C{_messageFactory} attribute which is
        L{dns.Message} by default.
        N)r   r   r   r	   r]   _messageFactoryr`   s    r   test_messageFactoryz)DNSServerFactoryTests.test_messageFactory(  s"    
 	ckk6#:#:#J#JKr   c           
         t        j                         }| j                  t        dt               t        j
                         }| j                  t        |j                  |t        j                        }| j                  dt        |j                  |t        j                  |j                  d      f|j                  |j                  f       y)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.
        r1  r#   r   r   F)responseConstructorr#   r   r9  authN)r	   r]   r   r   r   r   r   r   r1  r3  rR   r2  rK  r   r   r   r4  s       r   +test_responseFromMessageCallsMessageFactoryzADNSServerFactoryTests.test_responseFromMessageCallsMessageFactory/  s     ))+

3.7++-W997RURXRX  
 	(/(?(?#&&!,,	 VVQXX	
r   c                 d   t        j                         }|j                  t        j                         t        j
                  d      g      }|j                  t        j                         t        j
                  d      g      }| j                  d|j                  |j                  f       y)z
        L{server.DNSServerFactory._responseFromMessage} marks the response
        message as authoritative if any of the answer records are authoritative.
        T)rP  )r#   r,  Fr@  N)r	   r]   r1  r   r   RRHeaderrR   rP  rA  s       r   ,test_responseFromMessageAuthoritativeMessagezBDNSServerFactoryTests.test_responseFromMessageAuthoritativeMessageL  s    
 ))+00KKMCLLd,C+D 1 
	 00KKMCLLe,D+E 1 
	 	^^Y^^,	
r   c           	         t        d      }t        j                         g}t        j                         g}t        j                         g}t        | dg|j                  |||ft               t        j                         d       y)z
        L{server.DNSServerFactory.gotResolverResponse} logs the total number of
        records in the response if C{verbose > 0}.
        r   r   zLookup found 3 recordsNr+  )r!   r   rS  rX   r  r=   r   )r   r   r,  r-  r.  s        r   test_gotResolverResponseLoggingz5DNSServerFactoryTests.test_gotResolverResponseLogging]  sm    
 'q1<<>"\\^$	lln%
%&!!i,!^KKM	
r   c                    t        t               g      }t        j                         }|j	                  d       t        j
                         g}g }g }| j                  t        j                  |j                  |||ft               |d      }|j                  \  \  }\  }}	}
}| j                  |j                  j                  d       | j                  ||       | j                  |	|       | j                  |
|       y)z
        L{server.DNSServerFactory.gotResolverResponse} caches the response if at
        least one cache was provided in the constructor.
        rn   s   example.comNr+  )r!   rG   r   r   r   rS  r   rJ   r  r=   r   rR   r   r   )r   r   rW   expectedAnswersexpectedAuthorityexpectedAdditionalr   rE   r,  r-  r.  r   s               r   test_gotResolverResponseCachingz5DNSServerFactoryTests.test_gotResolverResponseCachingq  s    
 '|~.>?KKM	

>"<<>*--!!/1CD!^  
 =>FF910)Z6.9g/i!23j"45r   c                 ~   t               }t        |_        t        j                         }d|_        | j                  t        |j                  t        j                  t        j                               d|d      }| j                  dt        |t        j                        f|j                   |j"                  f       y)z
        L{server.DNSServerFactory.gotResolverError} calls
        L{server.DNSServerFactory._responseFromMessage} to generate a response.
        r   Nr+  r   rN  )r!   r   r1  r   r   r   r   r   r%  r
   r&  r   DomainErrorrR   r2  ENAMEr   r   r4  s       r   -test_gotResolverErrorCallsResponseFromMessagezCDNSServerFactoryTests.test_gotResolverErrorCallsResponseFromMessage  s    
 -.'-$++- $$OOE--/0  
 	gSYY78166188:L	
r   c                 >   t        j                         }| j                  t        j                  |j
                  t        j                  |      t               t        j                         d      }|j                  \  \  }}| j                  |j                  |       y)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   r5   r8   r%  r
   r&  r   r   r   rR   r   )r   responseErrorexpectedMessageCoder   r   r#   r   s          r   _assertMessageRcodeForErrorz1DNSServerFactoryTests._assertMessageRcodeForError  s~      ##%11OOM*$&KKM  
 VV
F(;<r   c                 h    | j                  t        j                         t        j                         y)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)rc  r   r]  r   r^  r`   s    r    test_gotResolverErrorDomainErrorz6DNSServerFactoryTests.test_gotResolverErrorDomainError  s!    
 	(():):)<ciiHr   c                 h    | j                  t        j                         t        j                         y)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)rc  r   AuthoritativeDomainErrorr   r^  r`   s    r   -test_gotResolverErrorAuthoritativeDomainErrorzCDNSServerFactoryTests.test_gotResolverErrorAuthoritativeDomainError  s!     	(()G)G)I399Ur   c                     | j                  t               t        j                         | j	                  t              }| j                  t        |      d       y)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)rc  KeyErrorr   ESERVERflushLoggedErrorsrR   len)r   r   s     r   test_gotResolverErrorOtherErrorz5DNSServerFactoryTests.test_gotResolverErrorOtherError  s>     	((S[[A""8,Q#r   c           	          t        d      }t        | dg|j                  t        j                  t        j                               t               t        j                         d       y)zZ
        L{server.DNSServerFactory.gotResolver} logs a message if C{verbose > 0}.
        r   r   zLookup failedNr+  )
r!   rX   r%  r
   r&  r   r]  r=   r   r   r   s     r   test_gotResolverErrorLoggingz2DNSServerFactoryTests.test_gotResolverErrorLogging  sN     'q1OOE--/0!^KKM	
r   c                    t        j                         }g fd|_        t        j                  dd      }t               t               g|_        t               t               g|_        t               t               g|_        |j                  t        j                  t        j                               d|d       | j                  t        j                  dd      g       y)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                 &    j                  |      S r   r   r$   r=  r%   	responsess      r   r   zUDNSServerFactoryTests.test_gotResolverErrorResetsResponseAttributes.<locals>.<lambda>      	@P@PA
 r   TauthenticDatacheckingDisabledNr+     r   r8  )r	   r]   r(   r   r   ri   r,  r-  r.  r%  r
   r&  r   r]  rR   r   r   r5  rt  s      @r   -test_gotResolverErrorResetsResponseAttributeszCDNSServerFactoryTests.test_gotResolverErrorResetsResponseAttributes  s     ))+	
 ++D4H!8VX.#Xvx0$h1  OOE--/0	 	! 	
 	#++Ad;<iHr   c                    t        j                         }g fd|_        t        j                  dd      }t               t               g|_        t               t               g|_        t               t               g|_        |j                  g g g fd|d       | j                  t        j                  dd      g       y)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                 &    j                  |      S r   r   rs  s      r   r   zXDNSServerFactoryTests.test_gotResolverResponseResetsResponseAttributes.<locals>.<lambda>
  ru  r   Trv  Nr+  r   rz  )r	   r]   r(   r   r   ri   r,  r-  r.  r  rR   r{  s      @r   0test_gotResolverResponseResetsResponseAttributeszFDNSServerFactoryTests.test_gotResolverResponseResetsResponseAttributes  s     ))+	
 ++D4H!8VX.#Xvx0$h1##RL4$ 	$ 	
 	#++Ad;<iHr   c                 >   t        j                         }t               }t        j                         }| j                  t        j                  |j                  t               ||      }|j                  \  }}| j                  |||f       | j                  |i        y)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   ri   r	   r]   r   r5   r8   r(   r   rR   )r   rW   r   r   r   r   r   s          r   test_sendReplyWithAddressz/DNSServerFactoryTests.test_sendReplyWithAddress  s     KKMx##%11KK$&   
 vvf<01$r   c                 (   t        j                         }t        j                         }| j	                  t
        j                  |j                  t               |d      }|j                  \  }}| j                  ||f       | j                  |i        y)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	   r]   r   r5   r8   r(   r   rR   )r   rW   r   r   r   r   s         r   test_sendReplyWithoutAddressz2DNSServerFactoryTests.test_sendReplyWithoutAddress,  s     KKM##%11KK$&  
 vvft$$r   c                     | j                  t        j                  dd        t        j                         }d|_        t        j                  d      }t        | ddg|j                  t               |d	       y)
z
        If L{server.DNSServerFactory.sendReply} logs a "no answers" message if
        the supplied message has no answers.
        r   c                       yNiQ r   r   r   r   r   zFDNSServerFactoryTests.test_sendReplyLoggingNoAnswers.<locals>.<lambda>D  r)   r   Q r   r   zReplying with no answers Processed query in 1.000 secondsNr+  )
r   r	   r   r   r   r   r]   rX   r(   r=   r   s      r   test_sendReplyLoggingNoAnswersz4DNSServerFactoryTests.test_sendReplyLoggingNoAnswers?  s`    
 	

6;;6KKM##A.')KLKK!^	
r   c                    | j                  t        j                  dd        t        j                         }|j
                  j                  t        j                  t        j                  d                   |j                  j                  t        j                  t        j                  d                   |j                  j                  t        j                  t        j                  d                   d|_        t        j                  d      }t        | g d|j                  t               |d	
       y	)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                       yr  r   r   r   r   r   zHDNSServerFactoryTests.test_sendReplyLoggingWithAnswers.<locals>.<lambda>W  r)   r   z	127.0.0.1)payloadr  r   r   )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>r  Nr+  )r   r	   r   r   r   r,  rO   rS  Record_Ar-  r.  r   r]   rX   r(   r=   r   s      r    test_sendReplyLoggingWithAnswersz6DNSServerFactoryTests.test_sendReplyLoggingWithAnswersQ  s     	

6;;6KKM			cll;.GHI	3<<[0IJK	CLLk1JKL##A. KK!^	
r   c                 2   t        j                         }| j                  t        j                  |j
                  t        j                         t               d      }|j                  \  \  }}| j                  |j                  t        j                         y)z
        L{server.DNSServerFactory.handleInverseQuery} triggers the sending of a
        response message with C{rCode} set to L{dns.ENOTIMP}.
        Nr	  )r	   r]   r   r5   r8   r   r   r   r   rR   r   ENOTIMPr   r   r   r#   r   s        r   test_handleInverseQueryz-DNSServerFactoryTests.test_handleInverseQueryl  su    
 ##%11  KKM$&  
 VV
F4r   c                     t        d      }t        | dg|j                  t        j                         t               d       y)zz
        L{server.DNSServerFactory.handleInverseQuery} logs the message origin
        address if C{verbose > 0}.
        r   r   zInverse query from ('::1', 53)z::1r   r	  N)r!   rX   r   r   r   r=   r   s     r   test_handleInverseQueryLoggingz4DNSServerFactoryTests.test_handleInverseQueryLogging}  s9    
 'q1-.  KKM!^	
r   c                 2   t        j                         }| j                  t        j                  |j
                  t        j                         t               d      }|j                  \  \  }}| j                  |j                  t        j                         y)z
        L{server.DNSServerFactory.handleStatus} triggers the sending of a
        response message with C{rCode} set to L{dns.ENOTIMP}.
        Nr	  )r	   r]   r   r5   r8   r   r   r   r   rR   r   r  r  s        r   test_handleStatusz'DNSServerFactoryTests.test_handleStatus  s    
 ##%11NNKKM$&  
 VV
F4r   c                     t        d      }t        | dg|j                  t        j                         t               d       y)zt
        L{server.DNSServerFactory.handleStatus} logs the message origin address
        if C{verbose > 0}.
        r   r   zStatus request from ('::1', 53)r  r	  N)r!   rX   r   r   r   r=   r   s     r   test_handleStatusLoggingz.DNSServerFactoryTests.test_handleStatusLogging  7    
 'q1./NNKKM!^	
r   c                 2   t        j                         }| j                  t        j                  |j
                  t        j                         t               d      }|j                  \  \  }}| j                  |j                  t        j                         y)z
        L{server.DNSServerFactory.handleNotify} triggers the sending of a
        response message with C{rCode} set to L{dns.ENOTIMP}.
        Nr	  )r	   r]   r   r5   r8   r   r   r   r   rR   r   r  r  s        r   test_handleNotifyz'DNSServerFactoryTests.test_handleNotify  r  r   c                     t        d      }t        | dg|j                  t        j                         t               d       y)zt
        L{server.DNSServerFactory.handleNotify} logs the message origin address
        if C{verbose > 0}.
        r   r   zNotify message from ('::1', 53)r  r	  N)r!   rX   r   r   r   r=   r   s     r   test_handleNotifyLoggingz.DNSServerFactoryTests.test_handleNotifyLogging  r  r   c                 2   t        j                         }| j                  t        j                  |j
                  t        j                         t               d      }|j                  \  \  }}| j                  |j                  t        j                         y)z
        L{server.DNSServerFactory.handleOther} triggers the sending of a
        response message with C{rCode} set to L{dns.ENOTIMP}.
        Nr	  )r	   r]   r   r5   r8   r   r   r   r   rR   r   r  r  s        r   test_handleOtherz&DNSServerFactoryTests.test_handleOther  ss    
 ##%11MMKKM$&  
 VV
F4r   c                     t        d      }t        | dg|j                  t        j                         t               d       y)zs
        L{server.DNSServerFactory.handleOther} logs the message origin address
        if C{verbose > 0}.
        r   r   z$Unknown op code (0) from ('::1', 53)r  r	  N)r!   rX   r   r   r   r=   r   s     r   test_handleOtherLoggingz-DNSServerFactoryTests.test_handleOtherLogging  s7    
 'q134MMKKM!^	
r   N)Dr   r   r   r   ra   re   rl   rp   rt   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/  r6  r>  rD  rF  rI  rL  rQ  rT  rV  r[  r_  rc  re  rh  rn  rp  r|  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r   r   rZ   rZ      s^   YK






.;	
?S<GPIJ2=F
"
&
,,%,6:H8S
UUT2".,&
P%
N60
B	,LF<L
:
"
(68
.=:IV$
I0I.%(%&
$
65"
5"
5"
5"
r   rZ   N)r   zope.interface.verifyr   twisted.internetr   twisted.internet.interfacesr   twisted.namesr   r   r   r	   twisted.pythonr
   r   twisted.trialr   r3   r   r   r]   r!   r+   r5   r=   r@   rG   rX   TestCaserZ   r   r   r   <module>r     s    . " 8 5 5 ' "i (!8!8 B5f55 5:7 70 "0 006 60V@O
H-- O
r   