o
    b^                     @   s  d Z ddlZddlZddlZddlmZmZ ddlmZ ddl	m
Z
 ddlmZmZmZ ddlmZ ddlmZmZmZmZmZ dd	lmZ dd
lmZmZmZmZmZmZ ddl m!Z! ddl"m#Z#m$Z$ ddl%m&Z& ddl'm(Z( ddl)m*Z*m+Z+m,Z, ddl-m.Z. dd Z/G dd dej0Z1ejdddddddddZ2ejdddd d!d"d#d$dZ3ejd%d&d'd(dd)dd*Z4e1de2fe2ed+e5d,e6d-e6d.d/e7d0d1ej8d2d3d4e9d5e:d6e;d7e<d8d9e=dd:d;e=d<d=d>e=d?d@dAe<dBe>dCej?dDdEdFej@ddGdHejAdIdJdKeBdLejCdMeDdd0dNdOdPeEdQgeFdRdSdTdUgedVedWgedXedYge9dgedZgd[d\ZGe1de3fd]eHd^e3gid\ZIe1d_e4fd_e4ejdZd`daej5dbdcdaej5dddedaejFdRdSdTdUdfdagid\ZJG dgdh dhe.jKZLG didj dje.jKZMG dkdl dle.jKZNG dmdn dne.jKZOG dodp dpe.jKZPG dqdr dre.jKZQG dsdt dte.jKZRG dudv dve.jKZSG dwdx dxe.jKZTdyZUG dzd{ d{e.jKZVd|ZWG d}d~ d~e.jKZXdS )z
Test cases for twisted.names.
    N)partialreduce)BytesIO)pack)defererrorreactor)succeed)	authorityclientcommondnsserver)Resolver)SOAMessageQueryRecord_A
Record_SOARRHeader)DomainError)SecondaryAuthoritySecondaryAuthorityService)nativeString)FilePath)MemoryReactorClockStringTransportwaitUntilAllDisconnected)unittestc                 C   s   dd | d D S )Nc                 S   s   g | ]}|j qS  payload.0rr   r   ?/usr/lib/python3/dist-packages/twisted/names/test/test_names.py
<listcomp>"       zjustPayload.<locals>.<listcomp>r   r   resultsr   r   r%   justPayload!   s   r*   c                   @      e Zd Zdd ZdS )NoFileAuthorityc                 C   s   t j|  ||| _| _d S N)r   ResolverBase__init__soarecords)selfr0   r1   r   r   r%   r/   &   s   zNoFileAuthority.__init__N)__name__
__module____qualname__r/   r   r   r   r%   r,   %       r,      test-domain.comzroot.test-domain.comd     i  H?&      mnamernameserialrefreshminimumexpireretryttls   93.84.28.in-addr.arpax   i1  i~  Ϊ       my-domain.coms   postmaster.test-domain.com   i90  i?B )r>   r?   r@   rA   rB   rC   rD   s	   127.0.0.1s   39.28.189.39#   v=spf1 mx/30 mx:example.org/30 -all   v=spf1 +mx a: colo   .example.com/28 -all not valid
   host.test-domain.com   Linux   A Fast One, Dontcha know)oscpus   canonical.name.coms   mailbox.test-domain.coms   mail.group.someplace   A First piece of Text   a SecoNd pieces
   ABCD::4321       s	   0:0069::0s   some.network.tld   s$   0:5634:1294:AFCB:56AC:48EF:34C3:01FFs   tra.la.la.net+   Some more text, haha!  Yes.     Still here?s   mail.redirect.or.whatevers
   r mail boxs
   e mail box)rmailbxemailbxs   afsdb.test-domain.com)subtypehostnames   whatever.i.dunnos   some.more.text)mboxtxts   12.54.78.12       u   sip+E2U!   !^.*$!sip:information@domain.tld!s'   AF43:5634:1294:AFCB:56AC:48EF:34C3:01FF  ?  骪  s   some.other.place.fool   123.242.1.5   0.255.0.255s   255.255.255.254s   0.0.0.0s   1.2.3.4)r7   s   http.tcp.test-domain.com   host.test-domain.coms   host-two.test-domain.coms   cname.test-domain.coms   anothertest-domain.comr0   r1   s   123.93.84.28.in-addr.arpas   test.host-reverse.lookup.coms   my-domain.com1SrE   s
   ns1.domains   2Ms
   ns2.domain3H4Dc                   @   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<S )=ServerDNSTestsz/
    Test cases for DNS server and client.
    c                 C   s   t jtttgdd| _t| j}	 tj	d| jdd}| 
|j | j}z
tj||dd}W n
 tjy:   Y nw | 
|j nq|| _|| _tjd|fgd| _d S )N   )verboser<   r   	127.0.0.1)	interfaceservers)r   DNSServerFactorytest_domain_comreverse_domainmy_domain_comfactoryr   DNSDatagramProtocolr   	listenTCP
addCleanupstopListeninggetHostport	listenUDPr   CannotListenErrorlistenerTCPlistenerUDPr   r   resolver)r2   pr   r   r   r   r   r%   setUp   s&   

zServerDNSTests.setUpc                 C   s6   | j jdd D ]}|j  qtt| j jdd S )zu
        Clean up any server connections associated with the
        L{DNSServerFactory} created in L{setUp}
        N)r{   connections	transportloseConnectionr   r   )r2   connr   r   r%   tearDown   s   zServerDNSTests.tearDownc                    s    fdd}| | |S )aD  
        Assert that the DNS response C{querying} will eventually fire with
        contains exactly a certain collection of records.

        @param querying: A L{Deferred} returned from one of the DNS client
            I{lookup} methods.

        @param expectedRecords: A L{list} of L{IRecord} providers which must be
            in the response or the test will be failed.

        @return: A L{Deferred} that fires when the assertion has been made.  It
            fires with a success result if the assertion succeeds and with a
            L{Failure} if it fails.
        c                    s    t | }t t| d S r-   )r*   assertEqualset)responsereceivedRecordsexpectedRecordsr2   r   r%   checkResults   s   z.ServerDNSTests.namesTest.<locals>.checkResults)addCallback)r2   queryingr   r   r   r   r%   	namesTest   s   
zServerDNSTests.namesTestc                 C       |  | jdtjdddgS )z"Test simple DNS 'A' record queriestest-domain.comrs   r:   rm   r   r   lookupAddressr   r   r2   r   r   r%   test_addressRecord1      
z"ServerDNSTests.test_addressRecord1c                 C   ,   |  | jdtjdddtjdddgS )z1Test DNS 'A' record queries with multiple answersrP   z123.242.1.5r:   rm   z0.255.0.255r   r   r   r   r%   test_addressRecord2      
z"ServerDNSTests.test_addressRecord2c                 C   r   )z+Test DNS 'A' record queries with edge caseszhost-two.test-domain.comz255.255.255.254r:   rm   0.0.0.0r   r   r   r   r%   test_addressRecord3   r   z"ServerDNSTests.test_addressRecord3c                 C   s   |  | jdtgS )zTest DNS 'SOA' record queriesr   )r   r   lookupAuthority
soa_recordr   r   r   r%   test_authority   s   zServerDNSTests.test_authorityc              	   C   s:   |  | jdtjddddtjdddtjdddgS )z
        The DNS client can issue an MX query and receive a response including
        an MX record as well as any A record hints.
        r7   rO   rj   r:   rm   rh   ri   )r   r   lookupMailExchanger   	Record_MXr   r   r   r   r%   test_mailExchangeRecord   s   
z&ServerDNSTests.test_mailExchangeRecordc                 C   r   )zTest DNS 'NS' record queriesr   z39.28.189.39r:   rm   )r   r   lookupNameserversr   	Record_NSr   r   r   r%   test_nameserver  r   zServerDNSTests.test_nameserverc                 C   "   |  | jdtjddddgS )zTest DNS 'HINFO' record queriesr   rQ   rR   r:   )rS   rT   rE   )r   r   lookupHostInfor   Record_HINFOr   r   r   r%   
test_HINFO     
zServerDNSTests.test_HINFOc                 C   r   )zTest DNS 'PTR' record queriesz123.93.84.28.in-addr.arpaztest.host-reverse.lookup.comrG   rm   )r   r   lookupPointerr   
Record_PTRr   r   r   r%   test_PTR   r   zServerDNSTests.test_PTRc                 C   r   )zTest DNS 'CNAME' record queriesr   zcanonical.name.comr:   rm   )r   r   lookupCanonicalNamer   Record_CNAMEr   r   r   r%   
test_CNAME'  r   zServerDNSTests.test_CNAMEc                 C   r   )zTest DNS 'MB' record queriesr   zmailbox.test-domain.comr:   rm   )r   r   lookupMailBoxr   	Record_MBr   r   r   r%   test_MB.  r   zServerDNSTests.test_MBc                 C   r   )zTest DNS 'MG' record queriesr   zmail.group.someplacer:   rm   )r   r   lookupMailGroupr   	Record_MGr   r   r   r%   test_MG5  r   zServerDNSTests.test_MGc                 C   r   )zTest DNS 'MR' record queriesr   zmail.redirect.or.whateverr:   rm   )r   r   lookupMailRenamer   	Record_MRr   r   r   r%   test_MR<  r   zServerDNSTests.test_MRc                 C   r   )zTest DNS 'MINFO' record queriesr   z
r mail boxz
e mail boxr:   )r[   r\   rE   )r   r   lookupMailboxInfor   Record_MINFOr   r   r   r%   
test_MINFOC  r   zServerDNSTests.test_MINFOc              
   C   s&   |  | jdtjddddddgS )zTest DNS 'SRV' record querieszhttp.tcp.test-domain.comre   rf   rg   some.other.place.foolr:   rm   )r   r   lookupServicer   
Record_SRVr   r   r   r%   test_SRVN  s   
zServerDNSTests.test_SRVc                 C   r   )zTest DNS 'AFSDB' record queriesr   r<   zafsdb.test-domain.comr:   )r]   r^   rE   )r   r   lookupAFSDatabaser   Record_AFSDBr   r   r   r%   
test_AFSDBU  r   zServerDNSTests.test_AFSDBc                 C   r   )zTest DNS 'RP' record queriesr   zwhatever.i.dunnozsome.more.textr:   )r_   r`   rE   )r   r   lookupResponsibilityr   	Record_RPr   r   r   r%   test_RP`  r   zServerDNSTests.test_RPc                 C   s.   |  | jdtjddddtjdddgS )zTest DNS 'TXT' record queriesr   rU   rV   r:   rm   rZ   )r   r   
lookupTextr   
Record_TXTr   r   r   r%   test_TXTk  s   
zServerDNSTests.test_TXTc              	   C   s.   |  | jdtjdddtjddddgS )zH
        L{DNSServerFactory} can serve I{SPF} resource records.
        r   rL   r:   rm   rM   rN   )r   r   lookupSenderPolicyr   
Record_SPFr   r   r   r%   test_spfy  s   
zServerDNSTests.test_spfc              	   C   s&   |  | jdtjdtjdddgS )zTest DNS 'WKS' record queriesr   z12.54.78.12ra   r:   rm   )r   r   lookupWellKnownServicesr   
Record_WKSsocketIPPROTO_TCPr   r   r   r%   test_WKS  s   
zServerDNSTests.test_WKSc                 C   s^   t  t}tj|_| | jd|tjdddtj	dddtj	dddtj
d	d
ddddgS )NrJ   1.2.3.4rl   rm   z
ns1.domain2Mz
ns2.domainrn   re   rf   rg   r   ro   )copymy_soarC   rE   r   r   lookupAllRecordsr   r   r   r   )r2   
result_soar   r   r%   test_someRecordsWithTTLs  s   

z'ServerDNSTests.test_someRecordsWithTTLsc                 C   r   )z%Test DNS 'AAAA' record queries (IPv6)r   z'AF43:5634:1294:AFCB:56AC:48EF:34C3:01FFr:   rm   )r   r   lookupIPV6Addressr   Record_AAAAr   r   r   r%   	test_AAAA  r   zServerDNSTests.test_AAAAc                 C   sD   |  | jdtjdddddtjddd	ddtjd
ddddgS )z#Test DNS 'A6' record queries (IPv6)r   r   z
ABCD::4321 r:   rm   rX   z	0:0069::0zsome.network.tldrY   z$0:5634:1294:AFCB:56AC:48EF:34C3:01FFztra.la.la.net)r   r   lookupAddress6r   	Record_A6r   r   r   r%   test_A6  s   
zServerDNSTests.test_A6c                 C   sZ   t j}dd ttjtj D }|D ]
}|jdu r||_q| 	| j
ddd |S )z9
        Test DNS 'AXFR' queries (Zone transfer)
        c                 S   s   g | ]}t  |qS r   )r   r"   r   r   r%   r&     s    
z4ServerDNSTests.test_zoneTransfer.<locals>.<listcomp>Nr   c                 S   s   | d d d fS )Nr   r   )r$   r   r   r%   <lambda>  r'   z2ServerDNSTests.test_zoneTransfer.<locals>.<lambda>)r   rC   r   operatoraddrx   r1   valuesrE   r   r   
lookupZoner   )r2   default_ttlr)   r$   r   r   r%   test_zoneTransfer  s   
z ServerDNSTests.test_zoneTransferc                 C   s    t dgd}| |dtjS )zu
        A failed AXFR TCP connection errbacks the L{Deferred} returned
        from L{Resolver.lookupZone}.
        )znameserver.invalid5   ru   zimpossible.invalid)r   assertFailurer   r   DNSLookupError)r2   r   r   r   r%    test_zoneTransferConnectionFails  s   z/ServerDNSTests.test_zoneTransferConnectionFailsc                 C   r   )z6Tests that unrelated zones don't mess with each other.zanothertest-domain.comr   r:   rm   r   r   r   r   r%   test_similarZonesDontInterfere  r   z-ServerDNSTests.test_similarZonesDontInterferec                 C   s(   |  | jdtjdddddddgS )	z2
        Test DNS 'NAPTR' record queries.
        r   r8   rO   rb   rc   rd   r:   rm   )r   r   lookupNamingAuthorityPointerr   Record_NAPTRr   r   r   r%   
test_NAPTR  s   
zServerDNSTests.test_NAPTRN)!r3   r4   r5   __doc__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%   rp      s>    


rp   c                   @   r+   )HelperTestsc                 C   s@   |   }t|}tdD ]}t|}| ||k  |}qd S )N   )mktempr
   	getSerialrange
assertTrue)r2   faibr   r   r%   test_serialGenerator  s   

z HelperTests.test_serialGeneratorN)r3   r4   r5   r  r   r   r   r%   r     r6   r   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )	AXFRTestsc                 C   s   d | _ t | _| j| j td| j| _t	j
dt	jt	jddt	jddddddd	d
dd| _| jt	j
dt	jt	jddt	jddddt	j
dt	jt	jddt	jdddddt	j
dt	jt	jddt	jdddd| jg| _d S )N	fooby.comiQ Fzhooj.fooby.comr8      i,  i  i  iX  )r>   r?   r@   rA   rD   rC   rB   rE   nametypeclsrE   authr!     ns.twistedmatrix.comr  rE   rO   zmail.mv3d.com)
preferenceexchangerE   z64.123.27.105)addressrE   )r)   r   Deferreddr   _gotResultsr   AXFRController
controllerr   r   r   INr   r0   NSr   MXr   Ar   r1   r   r   r   r%   r     s`   

zAXFRTests.setUpc                 C   s   t jdddddddddd	S )N  r<   r   	idansweropCoderecDesrecAvr
  rCodetruncmaxSize)r   r   r   r   r   r%   _makeMessage2  s   zAXFRTests._makeMessagec                 C   sH   |   }tdtjtjg|_| j|_| j	|d  | 
| j| j d S )Nr  )r$  r   r   AXFRr  queriesr1   answersr  messageReceivedr   r)   )r2   mr   r   r%   test_bindAndTNamesStyle@  s
   z!AXFRTests.test_bindAndTNamesStylec                 C   s
   || _ d S r-   r(   )r2   resultr   r   r%   r  H  s   
zAXFRTests._gotResultsc                 C   sT   | j d d  }|r |  }g |_|dg|_| j|d  |s	| | j| j  d S Nr   )	r1   r$  r&  popr'  r  r(  r   r)   )r2   r1   r)  r   r   r%   test_DJBStyleK  s   zAXFRTests.test_DJBStyleN)r3   r4   r5   r   r$  r*  r  r.  r   r   r   r%   r    s    5r  c                   @   s   e Zd Zdd Zdd ZdS )ResolvConfHandlingTestsc                 C   s2   |   }tj|d}| |jdg |j  d S )Nresolvrs   r   )r   r   r   r   
dynServers
_parseCallcancelr2   
resolvConfr$   r   r   r%   test_missingW  s   z$ResolvConfHandlingTests.test_missingc                 C   s@   |   }t|d  tj|d}| |jdg |j  d S )Nwr0  r2  )	r   opencloser   r   r   r3  r4  r5  r6  r   r   r%   
test_empty]  s
   z"ResolvConfHandlingTests.test_emptyN)r3   r4   r5   r8  r<  r   r   r   r%   r/  V  s    r/  c                   @   sH   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S )AuthorityTestszs
    Tests for the basic response record selection code in L{FileAuthority}
    (independent of its fileness).
    c                 C   s4   t }d|jd  }| ||}| |jt dS )aJ  
        L{FileAuthority} lookup methods errback with L{DomainError} if
        the requested C{name} shares a common suffix with its zone but
        is not actually a descendant of its zone, in terms of its
        sequence of DNS name labels. eg www.the-example.com has
        nothing to do with the zone example.com.
           nonexistent.prefix-r   N)rx   r0   failureResultOfr   assertIsInstancevaluer   )r2   
testDomaintestDomainNamer   r   r   r%   'test_domainErrorForNameWithCommonSuffixk  s   z6AuthorityTests.test_domainErrorForNameWithCommonSuffixc              
   C   s   t tjjtftjjttdgid}| |tjj\}}}| |g  | |tj	tjjtj
tjtddg | |g  dS )a!  
        If a L{FileAuthority} has a zone which includes an I{NS} record for a
        particular name and that authority is asked for another record for the
        same name which does not exist, the I{NS} record is not included in the
        authority section of the response.
        r   rk   TrE   r!   r
  N)r,   r   r>   r  r   r   successResultOfr   r   r   TYPErC   )r2   r
   r  
additionalr   r   r%   test_recordMissingx  s0   
	
z!AuthorityTests.test_recordMissingc                 C   sJ   t }d|jd  }ttjd }| |t||d}| |j	t
 dS )z
        Requesting a record of unknown type where no records exist for the name
        in question results in L{DomainError}.
        r>  r   r<   r  r  N)rx   r0   maxr   typeToMethodr?  queryr   r@  rA  r   )r2   rB  rC  unknownTyper   r   r   r%   test_unknownTypeNXDOMAIN  s   z'AuthorityTests.test_unknownTypeNXDOMAINc                 C   s<   t tjd }| ttd|d\}}}| |g  dS )z
        Requesting a record of unknown type where other records exist for the
        name in question results in an empty answer set.
        r<   rJ   rJ  N)rK  r   rL  rF  rz   rM  r   r   )r2   rN  r  r
   rH  r   r   r%   test_unknownTypeMissing  s
   
z&AuthorityTests.test_unknownTypeMissingc              
   C   s   dt jj }td}tt jjt f||gid}t|||}| |\}}}| |g  | |tj	|tj
t j|ddg | |g  dS )z
        Create an authority and make a request against it.  Then verify that the
        result is a referral, including no records in the answers or additional
        sections, but with an I{NS} record in the authority section.
        s   example.r   rk   FrE  N)r   r>   r  r   r   r,   getattrrF  r   r   r  rC   )r2   method	subdomain
nameserverr
   r  r  rH  r   r   r%   _referralTest  s0   

zAuthorityTests._referralTestc                 C      |  d dS )a  
        When an I{NS} record is found for a child zone, it is included in the
        authority section of the response. It is marked as non-authoritative if
        the authority is not also authoritative for the child zone (RFC 2181,
        section 6.1).
        r   NrU  r   r   r   r%   test_referral  s   zAuthorityTests.test_referralc                 C   rV  )zT
        A referral is also generated for a request of type C{ALL_RECORDS}.
        r   NrW  r   r   r   r%   test_allRecordsReferral  s   z&AuthorityTests.test_allRecordsReferralN)r3   r4   r5   r   rD  rI  rO  rP  rU  rX  rY  r   r   r   r%   r=  e  s    "!	r=  c                   @   s   e Zd ZdZedZe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"S )#AdditionalProcessingTestszz
    Tests for L{FileAuthority}'s additional processing for those record types
    which require it (MX, CNAME, etc).
    s   10.0.0.1s   f080::1c                 C   s8   t |jj|f|jj||g||id}t||tjjS )a  
        Perform a DNS lookup against a L{FileAuthority} configured with records
        as defined by C{makeRecord} and C{addresses}.

        @param method: The name of the lookup method to use; for example,
            C{"lookupNameservers"}.
        @type method: L{str}

        @param soa: A L{Record_SOA} for the zone for which the L{FileAuthority}
            is authoritative.

        @param makeRecord: A one-argument callable which accepts a name and
            returns an L{IRecord} provider.  L{FileAuthority} is constructed
            with this record.  The L{FileAuthority} is queried for a record of
            the resulting type with the given name.

        @param target: The extra name which the record returned by
            C{makeRecord} will be pointed at; this is the name which might
            require extra processing by the server so that all the available,
            useful information is returned.  For example, this is the target of
            a CNAME record or the mail exchange host pointed to by an MX record.
        @type target: L{bytes}

        @param addresses: A L{list} of records giving addresses of C{target}.

        @return: A L{Deferred} that fires with the result of the resolver
            method give by C{method}.
        rk   )r,   r>   r  rQ  r   )r2   rR  r0   
makeRecordtarget	addressesr
   r   r   r%   _lookupSomeRecords  s   
z,AdditionalProcessingTests._lookupSomeRecordsc                 C   s(   dd }|  t||dt||d dS )a  
        Assert that the L{RRHeader} instances given by C{expected} and
        C{computed} carry all the same information but without requiring the
        records appear in the same order.

        @param expected: A L{list} of L{RRHeader} instances giving the expected
            records.

        @param computed: A L{list} of L{RRHeader} instances giving the records
            computed by the scenario under test.

        @raise self.failureException: If the two collections of records
            disagree.
        c                 S   s   | j S r-   )r  )rrr   r   r%   r   $  s    z>AdditionalProcessingTests.assertRecordsMatch.<locals>.<lambda>)keyN)r   sorted)r2   expectedcomputedr`  r   r   r%   assertRecordsMatch  s    z,AdditionalProcessingTests.assertRecordsMatchc                    sL   dt jj  | |t | |}| |\}}}|  fdd|D | dS )aT  
        Verify that certain address records are included in the I{additional}
        section of a response generated by L{FileAuthority}.

        @param method: See L{_lookupSomeRecords}

        @param makeRecord: See L{_lookupSomeRecords}

        @param addresses: A L{list} of L{IRecord} providers which the
            I{additional} section of the response is required to match
            (ignoring order).

        @raise self.failureException: If the I{additional} section of the
            response consists of different records than those given by
            C{addresses}.
        s   mail.c              	      $   g | ]}t j |jtj|d dqS TrE  r   r   rG  r   rC   r#   r  r\  r   r%   r&   =      z=AdditionalProcessingTests._additionalTest.<locals>.<listcomp>N)r   r>   r  r^  rF  rd  )r2   rR  r[  r]  r  r  r
   rH  r   ri  r%   _additionalTest'  s   

z)AdditionalProcessingTests._additionalTestc                 C   s   |  dttjd| dS )z
        Verify that a response to an MX query has certain records in the
        I{additional} section.

        @param addresses: See C{_additionalTest}
        r   rO   N)rk  r   r   r   r2   r]  r   r   r%   _additionalMXTestJ  s   z+AdditionalProcessingTests._additionalMXTestc                 C      |  | jg dS )z
        If the name of the MX response has A records, they are included in the
        additional section of the response.
        N)rm  _Ar   r   r   r%   test_mailExchangeAdditionalAU     z6AdditionalProcessingTests.test_mailExchangeAdditionalAc                 C   rn  )z
        If the name of the MX response has AAAA records, they are included in
        the additional section of the response.
        N)rm  _AAAAr   r   r   r%   test_mailExchangeAdditionalAAAA\  rq  z9AdditionalProcessingTests.test_mailExchangeAdditionalAAAAc                 C      |  | j| jg dS )z
        If the name of the MX response has both A and AAAA records, they are
        all included in the additional section of the response.
        N)rm  ro  rr  r   r   r   r%   test_mailExchangeAdditionalBothc     z9AdditionalProcessingTests.test_mailExchangeAdditionalBothc                 C   s   |  dtj| dS )z
        Verify that a response to an NS query has certain records in the
        I{additional} section.

        @param addresses: See C{_additionalTest}
        r   N)rk  r   r   rl  r   r   r%   _additionalNSTestj  s   z+AdditionalProcessingTests._additionalNSTestc                 C   rn  )z
        If the name of the NS response has A records, they are included in the
        additional section of the response.
        N)rw  ro  r   r   r   r%   test_nameserverAdditionalAs  rq  z4AdditionalProcessingTests.test_nameserverAdditionalAc                 C   rn  )z
        If the name of the NS response has AAAA records, they are included in
        the additional section of the response.
        N)rw  rr  r   r   r   r%   test_nameserverAdditionalAAAAz  rq  z7AdditionalProcessingTests.test_nameserverAdditionalAAAAc                 C   rt  )z
        If the name of the NS response has both A and AAAA records, they are
        all included in the additional section of the response.
        N)rw  ro  rr  r   r   r   r%   test_nameserverAdditionalBoth  rv  z7AdditionalProcessingTests.test_nameserverAdditionalBothc                    sv   dt jj  | dt tj |}| |\}}}tjt jjtjt j	t dd}| 
 fdd|D |g | dS )z
        Verify that a response to a CNAME query has certain records in the
        I{answer} section.

        @param addresses: See C{_additionalTest}
        s   www.r   TrE  c              	      re  rf  rg  rh  ri  r   r%   r&     rj  z>AdditionalProcessingTests._answerCNAMETest.<locals>.<listcomp>N)r   r>   r  r^  r   r   rF  r   CNAMErC   rd  )r2   r]  r  r  r
   rH  aliasr   ri  r%   _answerCNAMETest  s(   

z*AdditionalProcessingTests._answerCNAMETestc                 C   rn  )z
        If the name of the CNAME response has A records, they are included in
        the answer section of the response.
        N)r}  ro  r   r   r   r%   test_canonicalNameAnswerA  rq  z3AdditionalProcessingTests.test_canonicalNameAnswerAc                 C   rn  )z
        If the name of the CNAME response has AAAA records, they are included
        in the answer section of the response.
        N)r}  rr  r   r   r   r%   test_canonicalNameAnswerAAAA  rq  z6AdditionalProcessingTests.test_canonicalNameAnswerAAAAc                 C   rt  )z
        If the name of the CNAME response has both A and AAAA records, they are
        all included in the answer section of the response.
        N)r}  ro  rr  r   r   r   r%   test_canonicalNameAnswerBoth  rv  z6AdditionalProcessingTests.test_canonicalNameAnswerBothN)r3   r4   r5   r   r   r   ro  r   rr  r^  rd  rk  rm  rp  rs  ru  rw  rx  ry  rz  r}  r~  r  r  r   r   r   r%   rZ    s&    

&#	#rZ  c                   @   r+   )NoInitialResponseTestsc                    s    fdd}dd }t dgd}||_g  | tjd| tjdtjtjd	d
tjdd	ddg}tj	dddddddddd	}||_
 | | |dtjS )z
        If a request returns a L{dns.NS} response, but we can't connect to the
        given server, the request fails with the error returned at connection.
        c                    s   t  dS r,  )r	   r-  )r2   argsmessagesr   r%   rM    s   z3NoInitialResponseTests.test_noAnswer.<locals>.queryc                 _   s   t tdS )NzCouldn't connect)r   failr   gaierror)r2   r  kwargsr   r   r%   queryProtocol     z;NoInitialResponseTests.test_noAnswer.<locals>.queryProtocol)r   r   ru   rM  z	fooba.comr  Fr  r  r  r  r<   r   r  r  )r   _querypatchr   r|   r   r  r  r   r   r'  appendr   getHostByNamer   r  )r2   rM  r  r   r1   r)  r   r  r%   test_noAnswer  s:   

z$NoInitialResponseTests.test_noAnswerN)r3   r4   r5   r  r   r   r   r%   r    r6   r  c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	SecondaryAuthorityServiceTestsz
    Tests for L{SecondaryAuthorityService}, a service which keeps one or more
    authorities up to date by doing zone transfers from a master.
    c                 C   s   d}t |ddg}| |j| | |jd | |jd j| | |jd jd | |jd jd | |jd j| | |jd jd | |jd jd dS )	a  
        L{SecondaryAuthorityService} can be constructed with a C{str} giving a
        master server address and several domains, causing the creation of a
        secondary authority for each domain and that master server address and
        the default DNS port.
        192.168.1.2   example.comzexample.orgr   r   r<   s   example.orgN)r   r   primary_portdomainsdomainr2   r  servicer   r   r%   test_constructAuthorityFromHost  s   z>SecondaryAuthorityServiceTests.test_constructAuthorityFromHostc                 C   s   d}d}t ||fddg}| |j| | |jd | |jd j| | |jd j| | |jd jd | |jd j| | |jd j| | |jd jd dS )	a\  
        L{SecondaryAuthorityService.fromServerAddressAndDomains} constructs a
        new L{SecondaryAuthorityService} from a C{str} giving a master server
        address and DNS port and several domains, causing the creation of a secondary
        authority for each domain and that master server address and the given
        DNS port.
        192.168.1.3i  zexample.net   example.edur      example.netr<   N)r   fromServerAddressAndDomainsr   r  r  r  r  )r2   r  r   r  r   r   r%   &test_constructAuthorityFromHostAndPort  s   zESecondaryAuthorityServiceTests.test_constructAuthorityFromHostAndPortc                 C   sx   d}t | ddg}| |j| | |jd j| | |jd jd | |jd j| | |jd jd dS )aQ  
        L{SecondaryAuthorityService.fromServerAddressAndDomains} constructs a
        new L{SecondaryAuthorityService} from a C{bytes} giving a master server
        address and several domains, causing the creation of a secondary
        authority for each domain and that master server address and the given
        DNS port.
        r  r  zexample.edur   r<   r  N)r   encoder   r  r  r  r  r   r   r%    test_constructAuthorityFromBytes#  s   z?SecondaryAuthorityServiceTests.test_constructAuthorityFromBytesN)r3   r4   r5   r   r  r  r  r   r   r   r%   r    s
    r  c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )SecondaryAuthorityTestsz
    L{twisted.names.secondary.SecondaryAuthority} correctly constructs objects
    with a specified IP address and optionally specified DNS port.
    c                 C   s8   t dd}| |jd | |jd | |jd dS )zp
        When constructed using L{SecondaryAuthority.__init__}, the default port
        of 53 is used.
        192.168.1.1
inside.comr   
   inside.comN)r   r   r  r  r  r2   	secondaryr   r   r%   test_defaultPort?  s   
z(SecondaryAuthorityTests.test_defaultPortc                 C   s:   t dd}| |jd | |jd | |jd dS )z~
        When constructed using L{SecondaryAuthority.fromServerAddressAndDomain},
        the specified port is used.
        )r    r  r  r  r  N)r   fromServerAddressAndDomainr   r  r  r  r  r   r   r%   test_explicitPortI  s   z)SecondaryAuthorityTests.test_explicitPortc                 C   s   t dd}t  |_}|  |jd\}}}}}| |d | |d |||f}t	 }	|
|	 t }
|
t|	 dd  | tdtjtjg|
j dS )z
        An attempt is made to transfer the zone for the domain the
        L{SecondaryAuthority} was constructed with from the server address it
        was constructed with when L{SecondaryAuthority.transfer} is called.
        r  r9   example.comr   r  r9   rq   N)r   r  r   _reactortransfer
tcpClientsr-  r   buildProtocolr   makeConnectionr   decoder   rA  r   r   r%  r  r&  )r2   r  r   hostr   r{   timeoutbindAddressprotor   msgr   r   r%   test_transferU  s   
"z%SecondaryAuthorityTests.test_transferc              
   C   s.  t dd}t  |_}|  |jd\}}}}}|||f}t }	|	|	 t
ddd}
|
t|	 dd  tdd	d
dddddd}tddd}t
|
jddd}|jtdt|dtd|dtdt|dg | }|tdt||  | |d}| td|ddgg g f| dS )z
        L{SecondaryAuthority.lookupAddress} returns a L{Deferred} that fires
        with the I{A} records the authority has cached from the primary.
        r  r  r   r<   )r  r
  rq   Ns   ns1.example.comzadmin.example.comi@ i  i  i   i%  i.  r=   s   192.168.1.2rm   )r  r  r
  )r  r!   r    z!Hr  T)r!   r
  )r   r  r   r  r  r  r-  r  r   r  r   r  r   rA  r   r   r  r'  extendr   r   toStrdataReceivedr   lenrF  r   r   )r2   r  r   r  r   r{   r  r  r  r   rM  r0   r   r  datar+  r   r   r%   test_lookupAddresss  sF   


z*SecondaryAuthorityTests.test_lookupAddressN)r3   r4   r5   r   r  r  r  r  r   r   r   r%   r  9  s    
r  sY  $ORIGIN example.com.
$TTL    1w
example.com. IN SOA dns.example.com (
            2013120201 ; serial number of this zone file
            1d         ; slave refresh
            2h         ; slave retry time in case of a problem
            4w         ; slave expiration time
            1h         ; maximum caching time in case of failed lookups
            )

; A comment.
@                  IN AAAA 2001:db8:10::1
example.com.       IN A 10.0.0.1
no-in.example.com. A 10.0.0.2  ; technically wrong but used to work
not-fqdn           IN MX 10 mx.example.com
www                IN CNAME example.comc                   @   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S )BindAuthorityTestsz=
    Tests for L{twisted.names.authority.BindAuthority}.
    c                 C   s(   t |  d}|| t|jS )z
        Create a new L{twisted.names.authority.BindAuthority} from C{s}.

        @param s: A string with BIND zone data.
        @type s: bytes

        @return: a new bind authority
        @rtype: L{twisted.names.authority.BindAuthority}
        ascii)r   r   r  
setContentr
   BindAuthoritypath)r2   sfpr   r   r%   loadBindString  s   

z!BindAuthorityTests.loadBindStringc                 C      |  t| _d S r-   )r  sampleBindZoner
  r   r   r   r%   r     r  zBindAuthorityTests.setUpc                 C   s8   | j j D ]}| j j| D ]
}| d|jk qqdS )zG
        Loads the default $TTL and applies it to all records.
        :	 N)r
  r1   keysr   rE   )r2   domrecr   r   r%   test_ttl  s
   zBindAuthorityTests.test_ttlc                 C   s$   |  d| jj | d| jj dS )z,
        Loads the default $ORIGIN.
        s   example.com.   not-fqdn.example.comN)r   r
  originassertInr1   r   r   r   r%   test_originFromFile  s   z&BindAuthorityTests.test_originFromFilec                 C   sB   dD ]\}}|  | j|\\}\ \ | t|d|j qdS )'
        A records are loaded.
        )r  z10.0.0.1)   no-in.example.comz10.0.0.2r  NrF  r
  r   r   r   r   r!   r2   r  ipr_  r   r   r%   test_aRecords  s   z BindAuthorityTests.test_aRecordsc                 C   s4   |  | jd\\}\ \ | tdd|j dS )*
        AAAA records are loaded.
        r  2001:db8:10::1r  NrF  r
  r   r   r   r   r!   r2   r_  r   r   r%   test_aaaaRecords  s   
z#BindAuthorityTests.test_aaaaRecordsc                 C   s8   |  | jd\\}\ \ | tjdddd|j dS )(
        MX records are loaded.
        r  rO   zmx.example.comr  )r  r  rE   NrF  r
  r   r   r   r   r!   r  r   r   r%   test_mxRecords  s   
z!BindAuthorityTests.test_mxRecordsc                 C   s<   |  | jd\}\ \ |d }| tjddd|j dS )+
        CNAME records are loaded.
           www.example.comr   r  r  r  NrF  r
  r   r   r   r   r!   r2   r'  r_  r   r   r%   test_cnameRecords  s   

z$BindAuthorityTests.test_cnameRecordsc                 C   sL   |  t}| d W d   n1 sw   Y  | d|jjd  dS )zO
        loadBindString raises NotImplementedError on invalid records.
        s   example.com. IN LOL 192.168.0.1NzRecord type 'LOL' not supportedr   )assertRaisesNotImplementedErrorr  r   	exceptionr  )r2   er   r   r%   test_invalidRecordClass%  s   z*BindAuthorityTests.test_invalidRecordClassc              	   C   sb   dD ],}|  t}| |d  W d   n1 sw   Y  | t|d |jjd  qdS )zC
        $INCLUDE and $GENERATE raise NotImplementedError.
        )s   $INCLUDEs	   $GENERATEs    doesNotMatterNs    directive not implementedr   )r  r  r  r   r   r  r  )r2   	directiver  r   r   r%   test_invalidDirectives-  s   

z)BindAuthorityTests.test_invalidDirectivesN)r3   r4   r5   r   r  r   r  r  r  r  r  r  r  r  r   r   r   r%   r    s    r  a  zone = [
    SOA(
        # For whom we are the authority
        'example.com',

        # This nameserver's name
        mname = "dns.example.com",

        # Mailbox of individual who handles this
        rname = "root.example.com",

        # Unique serial identifying this SOA data
        serial = 86400,

        # Time interval before zone should be refreshed
        refresh = "2H",

        # Interval before failed refresh should be retried
        retry = "1H",

        # Upper limit on time interval before expiry
        expire = "1H",

        # Minimum TTL
        minimum = "3H"

    ),

    AAAA('example.com', '2001:db8:10::1'),
    A('example.com', '10.0.0.1'),
    NS('example.com', 'dns.example.com'),
    A('no-in.example.com', '10.0.0.2'),
    PTR('2.0.0.10.in-addr.arpa', 'no-in.example.com'),

    CNAME('www.example.com', 'example.com'),
    CNAME('ftp.example.com', 'example.com'),

    MX('not-fqdn.example.com', 10, 'mail.example.com'),
]
c                   @   sP   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S )PySourceAuthorityTestszA
    Tests for L{twisted.names.authority.PySourceAuthority}.
    c                 C   sN   t |  }t|jd}|| W d   n1 sw   Y  t|jS )a  
        Create a new L{twisted.names.authority.PySourceAuthority} from C{s}.

        @param s: A string with BIND zone data in a Python source file.
        @type s: L{str}

        @return: a new bind authority
        @rtype: L{twisted.names.authority.PySourceAuthority}
        r9  N)r   r   r:  r  writer
   PySourceAuthority)r2   r  r  r   r   r   r%   loadPySourceStringj  s
   
z)PySourceAuthorityTests.loadPySourceStringc                 C   r  r-   )r  samplePySourcer
  r   r   r   r%   r   z  r  zPySourceAuthorityTests.setUpc                 C   s@   dD ]\}}|  | j|\\}\ \ | t||j qdS )r  r  Nr  r  r   r   r%   r  }  s   z$PySourceAuthorityTests.test_aRecordsc                 C   s2   |  | jd\\}\ \ | td|j dS )r  r  r  Nr  r  r   r   r%   r    s   
z'PySourceAuthorityTests.test_aaaaRecordsc                 C   s6   |  | jd\\}\ \ | tjddd|j dS )r  r  rO   zmail.example.com)r  r  Nr  r  r   r   r%   r    s   
z%PySourceAuthorityTests.test_mxRecordsc                 C   :   |  | jd\}\ \ |d }| tjdd|j dS )r  r  r   r  r  Nr  r  r   r   r%   r       

z(PySourceAuthorityTests.test_cnameRecordsc                 C   r  )z)
        PTR records are loaded.
        s   2.0.0.10.in-addr.arpar   r  r  N)rF  r
  r   r   r   r   r!   r  r   r   r%   r     r  zPySourceAuthorityTests.test_PTRc                 C   s   d}|  t| j| dS )z1
        Input file has no zone variable
        znothing = []N)r  
ValueErrorr  )r2   badPySourcer   r   r%   test_badInputNoZone  s   z*PySourceAuthorityTests.test_badInputNoZoneN)r3   r4   r5   r   r  r   r  r  r  r  r   r  r   r   r   r%   r  e  s    r  )Yr   r   r   r   	functoolsr   r   ior   structr   twisted.internetr   r   r   twisted.internet.deferr	   twisted.namesr
   r   r   r   r   twisted.names.clientr   twisted.names.dnsr   r   r   r   r   r   twisted.names.errorr   twisted.names.secondaryr   r   twisted.python.compatr   twisted.python.filepathr   twisted.test.proto_helpersr   r   r   twisted.trialr   r*   FileAuthorityr,   r   reverse_soar   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rx   r   ry   rz   TestCaserp   r   r  r/  r=  rZ  r  r  r  r  r  r  r  r   r   r   r%   <module>   s    




	

3  \
Z~ _0Hm +