U
    
W[X                     @   s  d Z ddlmZmZ ddlZddlZddl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
lmZmZmZmZ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& ddl'm(Z(m)Z) ddl*m+Z+ ddl,m-Z- ddl.m/Z/m0Z0m1Z1 dd Z2G dd dej3Z4ej!dddddddddZ5ej!ddd d!d"d#d$d%dZ6ej!d&d'd(d)dd*dd+Z7e4de5fe5e d,e8d-e9d.e9d/d0e:d1d2ej;d3d4d5e<d6e=d7e>d8e?d9d:e@dd;d<e@d=d>d?e@d@dAdBe?dCeAdDejBdEdFdGejCddHdIejDdJdKdLeEdMejFdNeGdd1dOdPdQeHdRgeIdSdTdUdVge dWe dXge dYe dZge<dge d[gd\d]ZJe4de6fd^eKd_e6gid]ZLe4d`e7fd`e7ej d[dadbej8dcdddbej8dedfdbejIdSdTdUdVdgdbgid]ZMG dhdi diejNZOG djdk dkejNZPG dldm dmejNZQG dndo doejNZRG dpdq dqejNZSG drds dsejNZTG dtdu duejNZUG dvdw dwejNZVG dxdy dyejNZWdzZXG d{d| d|ejNZYdS )}z
Test cases for twisted.names.
    )absolute_importdivisionN)BytesIO)partialreduce)pack)unittest)reactordefererror)succeed)clientservercommon	authoritydns)SOAMessageRRHeaderRecord_A
Record_SOAQuery)DomainError)Resolver)SecondaryAuthorityServiceSecondaryAuthority)nativeString)FilePath)StringTransportMemoryReactorClockwaitUntilAllDisconnectedc                 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>$   s     zjustPayload.<locals>.<listcomp>r   r!   resultsr!   r!   r'   justPayload#   s    r+   c                   @   s   e Zd Zdd ZdS )NoFileAuthorityc                 C   s   t j|  || | _| _d S N)r   ZResolverBase__init__soarecords)selfr/   r0   r!   r!   r'   r.   '   s    zNoFileAuthority.__init__N)__name__
__module____qualname__r.   r!   r!   r!   r'   r,   &   s   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>   r?   r@   rA   rB   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)r5   s   http.tcp.test-domain.com   host.test-domain.coms   host-two.test-domain.coms   cname.test-domain.coms   anothertest-domain.comr/   r0   s   123.93.84.28.in-addr.arpas   test.host-reverse.lookup.coms   my-domain.com1SrC   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k
rt   Y q"X | 
|j qq"|| _|| _tjd|fgd| _d S )N   )verboser   	127.0.0.1)Z	interfaceZservers)r   ZDNSServerFactorytest_domain_comreverse_domainmy_domain_comfactoryr   DNSDatagramProtocolr	   Z	listenTCPZ
addCleanupZstopListeningZgetHostportZ	listenUDPr   ZCannotListenErrorlistenerTCPlistenerUDPr   r   resolver)r1   pry   rx   rz   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)rv   Zconnections	transportZloseConnectionr    r	   )r1   Z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)ZresponseZreceivedRecordsexpectedRecordsr1   r!   r'   checkResults   s    z.ServerDNSTests.namesTest.<locals>.checkResults)addCallback)r1   Zqueryingr   r   r!   r   r'   	namesTest   s    
zServerDNSTests.namesTestc                 C   s    |  | jdtjdddgS )z"Test simple DNS 'A' record queriestest-domain.comrq   r8   rk   r   r{   lookupAddressr   r   r1   r!   r!   r'   test_addressRecord1   s    
z"ServerDNSTests.test_addressRecord1c                 C   s,   |  | jdtjdddtjdddgS )z1Test DNS 'A' record queries with multiple answersrN   z123.242.1.5r8   rk   z0.255.0.255r   r   r!   r!   r'   test_addressRecord2   s    
z"ServerDNSTests.test_addressRecord2c                 C   s,   |  | jdtjdddtjdddgS )z+Test DNS 'A' record queries with edge caseszhost-two.test-domain.comz255.255.255.254r8   rk   0.0.0.0r   r   r!   r!   r'   test_addressRecord3   s    
z"ServerDNSTests.test_addressRecord3c                 C   s   |  | jdtgS )zTest DNS 'SOA' record queriesr   )r   r{   Z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.
        r5   rM   rh   r8   rk   rf   rg   )r   r{   lookupMailExchanger   	Record_MXr   r   r!   r!   r'   test_mailExchangeRecord  s    
z&ServerDNSTests.test_mailExchangeRecordc                 C   s    |  | jdtjdddgS )zTest DNS 'NS' record queriesr   z39.28.189.39r8   rk   )r   r{   lookupNameserversr   	Record_NSr   r!   r!   r'   test_nameserver  s    
zServerDNSTests.test_nameserverc                 C   s"   |  | jdtjddddgS )zTest DNS 'HINFO' record queriesr   rO   rP   r8   )rQ   rR   rC   )r   r{   ZlookupHostInfor   Record_HINFOr   r!   r!   r'   
test_HINFO  s    
zServerDNSTests.test_HINFOc                 C   s    |  | jdtjdddgS )zTest DNS 'PTR' record queriesz123.93.84.28.in-addr.arpaztest.host-reverse.lookup.comrE   rk   )r   r{   ZlookupPointerr   
Record_PTRr   r!   r!   r'   test_PTR"  s    
zServerDNSTests.test_PTRc                 C   s    |  | jdtjdddgS )zTest DNS 'CNAME' record queriesr   zcanonical.name.comr8   rk   )r   r{   lookupCanonicalNamer   Record_CNAMEr   r!   r!   r'   
test_CNAME*  s    
zServerDNSTests.test_CNAMEc                 C   s    |  | jdtjdddgS )zTest DNS 'MB' record queriesr   zmailbox.test-domain.comr8   rk   )r   r{   ZlookupMailBoxr   	Record_MBr   r!   r!   r'   test_MB1  s    
zServerDNSTests.test_MBc                 C   s    |  | jdtjdddgS )zTest DNS 'MG' record queriesr   zmail.group.someplacer8   rk   )r   r{   ZlookupMailGroupr   	Record_MGr   r!   r!   r'   test_MG9  s    
zServerDNSTests.test_MGc                 C   s    |  | jdtjdddgS )zTest DNS 'MR' record queriesr   zmail.redirect.or.whateverr8   rk   )r   r{   ZlookupMailRenamer   	Record_MRr   r!   r!   r'   test_MRA  s    
zServerDNSTests.test_MRc                 C   s"   |  | jdtjddddgS )zTest DNS 'MINFO' record queriesr   z
r mail boxz
e mail boxr8   )rY   rZ   rC   )r   r{   ZlookupMailboxInfor   Record_MINFOr   r!   r!   r'   
test_MINFOI  s    
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.comrc   rd   re   some.other.place.foolr8   rk   )r   r{   ZlookupServicer   
Record_SRVr   r!   r!   r'   test_SRVQ  s    
zServerDNSTests.test_SRVc                 C   s"   |  | jdtjddddgS )zTest DNS 'AFSDB' record queriesr   r:   zafsdb.test-domain.comr8   )r[   r\   rC   )r   r{   ZlookupAFSDatabaser   Record_AFSDBr   r!   r!   r'   
test_AFSDBX  s    
zServerDNSTests.test_AFSDBc                 C   s"   |  | jdtjddddgS )zTest DNS 'RP' record queriesr   zwhatever.i.dunnozsome.more.textr8   )r]   r^   rC   )r   r{   ZlookupResponsibilityr   	Record_RPr   r!   r!   r'   test_RP`  s    
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   rS   rT   r8   rk   rX   )r   r{   Z
lookupTextr   
Record_TXTr   r!   r!   r'   test_TXTh  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   rJ   r8   rk   rK   rL   )r   r{   ZlookupSenderPolicyr   
Record_SPFr   r!   r!   r'   test_spfs  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.12r_   r8   rk   )r   r{   Z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 )NrH   1.2.3.4rj   rk   z
ns1.domainZ2Mz
ns2.domainrl   rc   rd   re   r   rm   )copymy_soarA   rC   r   r{   lookupAllRecordsr   r   r   r   )r1   Z
result_soar!   r!   r'   test_someRecordsWithTTLs  s    

z'ServerDNSTests.test_someRecordsWithTTLsc                 C   s    |  | jdtjdddgS )z%Test DNS 'AAAA' record queries (IPv6)r   z'AF43:5634:1294:AFCB:56AC:48EF:34C3:01FFr8   rk   )r   r{   lookupIPV6Addressr   Record_AAAAr   r!   r!   r'   	test_AAAA  s    
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 r8   rk   rV   z	0:0069::0zsome.network.tldrW   z$0:5634:1294:AFCB:56AC:48EF:34C3:01FFztra.la.la.net)r   r{   Z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k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>  rU   z2ServerDNSTests.test_zoneTransfer.<locals>.<lambda>)r   rA   r   operatoraddrs   r0   valuesrC   r   r{   
lookupZoner   )r1   Z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   rr   zimpossible.invalid)r   assertFailurer   r   ZDNSLookupError)r1   r{   r!   r!   r'    test_zoneTransferConnectionFails  s    z/ServerDNSTests.test_zoneTransferConnectionFailsc                 C   s    |  | jdtjdddgS )z6Tests that unrelated zones don't mess with each other.zanothertest-domain.comr   r8   rk   r   r   r!   r!   r'   test_similarZonesDontInterfere  s    
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   r6   rM   r`   ra   rb   r8   rk   )r   r{   ZlookupNamingAuthorityPointerr   Record_NAPTRr   r!   r!   r'   
test_NAPTR  s    
zServerDNSTests.test_NAPTRN)!r2   r3   r4   __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'   rn      s<   		

rn   c                   @   s   e Zd Zdd ZdS )HelperTestsc                 C   s@   |   }t|}tdD ] }t|}| ||k  |}qd S )N   )mktempr   Z	getSerialrange
assertTrue)r1   faibr!   r!   r'   test_serialGenerator  s    

z HelperTests.test_serialGeneratorN)r2   r3   r4   r   r!   r!   r!   r'   r     s   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.comr6      i,  i  i  iX  )r<   r=   r>   r?   rB   rA   r@   rC   nametypeclsrC   authr#     ns.twistedmatrix.comr   rC   rM   zmail.mv3d.com)
preferenceZexchangerC   z64.123.27.105)addressrC   )r*   r
   ZDeferreddr   _gotResultsr   ZAXFRController
controllerr   r   r   INr   r/   NSr   ZMXr   Ar   r0   r   r!   r!   r'   r}     s6    
zAXFRTests.setUpc                 C   s   t jdddddddddd	S )N  r:   r   	idanswerZopCodeZrecDesZrecAvr   ZrCodeZtruncZmaxSize)r   r   r   r!   r!   r'   _makeMessage  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   queriesr0   answersr   messageReceivedr   r*   )r1   mr!   r!   r'   test_bindAndTNamesStyle  s
    z!AXFRTests.test_bindAndTNamesStylec                 C   s
   || _ d S r-   r)   )r1   resultr!   r!   r'   r     s    zAXFRTests._gotResultsc                 C   sR   | j d d  }|r>|  }g |_|dg|_| j|d  q| | j| j  d S Nr   )	r0   r   r   popr   r   r   r   r*   )r1   r0   r   r!   r!   r'   test_DJBStyle
  s    zAXFRTests.test_DJBStyleN)r2   r3   r4   r}   r   r   r   r   r!   r!   r!   r'   r     s
   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Zresolvrq   r   )r   r   r   r   
dynServers
_parseCallcancelr1   Z
resolvConfr&   r!   r!   r'   test_missing  s    z$ResolvConfHandlingTests.test_missingc                 C   s@   |   }t|d  tj|d}| |jdg |j  d S )Nwr   r   )	r   opencloser   r   r   r   r   r   r   r!   r!   r'   
test_empty  s
    z"ResolvConfHandlingTests.test_emptyN)r2   r3   r4   r  r  r!   r!   r!   r'   r     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)rs   r/   failureResultOfr   assertIsInstancevaluer   )r1   
testDomaintestDomainNamer   r!   r!   r'   'test_domainErrorForNameWithCommonSuffix,  s    z6AuthorityTests.test_domainErrorForNameWithCommonSuffixc              
   C   s   t ttjtfttjttdgid}|ttj}g }||j |d \}}}| 	|g  | 	|tj
tt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   ri   r   TrC   r#   r   N)r,   strr   r<   r   r   r   r   appendr   r   TYPErA   )r1   r   r   r   r   
additionalr!   r!   r'   test_recordMissing:  s2      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)rs   r/   maxr   typeToMethodr  queryr   r	  r
  r   )r1   r  r  unknownTyper   r!   r!   r'   test_unknownTypeNXDOMAINV  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:   rH   r  N)r  r   r  successResultOfru   r  r   r   )r1   r  r   r   r  r!   r!   r'   test_unknownTypeMissingc  s    

z&AuthorityTests.test_unknownTypeMissingc              
   C   s   dt tj }td}tt t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.
        zexample.r   ri   Fr  N)r  r   r<   r   r   r,   getattrr  r   r   r   rA   )r1   methodZ	subdomainZ
nameserverr   r   r   r  r!   r!   r'   _referralTesto  s.    
    zAuthorityTests._referralTestc                 C   s   |  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r  r   r!   r!   r'   test_referral  s    zAuthorityTests.test_referralc                 C   s   |  d dS )zT
        A referral is also generated for a request of type C{ALL_RECORDS}.
        r   Nr  r   r!   r!   r'   test_allRecordsReferral  s    z&AuthorityTests.test_allRecordsReferralN)r2   r3   r4   r   r  r  r  r  r  r   r!  r!   r!   r!   r'   r  &  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}.
        ri   )r,   r<   r   r  r   )r1   r  r/   
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     rU   z>AdditionalProcessingTests.assertRecordsMatch.<locals>.<lambda>)keyN)r   sorted)r1   ZexpectedZ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              	      s$   g | ]}t j |jtj|d dqS Tr  r   r   r  r   rA   r%   r   r$  r!   r'   r(     s      z=AdditionalProcessingTests._additionalTest.<locals>.<listcomp>N)r   r<   r   r&  r  r*  )r1   r  r#  r%  r   r   r   r  r!   r.  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   rM   N)r/  r   r   r   r1   r%  r!   r!   r'   _additionalMXTest  s
     
 z+AdditionalProcessingTests._additionalMXTestc                 C   s   |  | 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)r1  _Ar   r!   r!   r'   test_mailExchangeAdditionalA  s    z6AdditionalProcessingTests.test_mailExchangeAdditionalAc                 C   s   |  | jg dS )z
        If the name of the MX response has AAAA records, they are included in
        the additional section of the response.
        N)r1  _AAAAr   r!   r!   r'   test_mailExchangeAdditionalAAAA  s    z9AdditionalProcessingTests.test_mailExchangeAdditionalAAAAc                 C   s   |  | 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)r1  r2  r4  r   r!   r!   r'   test_mailExchangeAdditionalBoth  s    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)r/  r   r   r0  r!   r!   r'   _additionalNSTest  s
      z+AdditionalProcessingTests._additionalNSTestc                 C   s   |  | jg dS )z
        If the name of the NS response has A records, they are included in the
        additional section of the response.
        N)r7  r2  r   r!   r!   r'   test_nameserverAdditionalA*  s    z4AdditionalProcessingTests.test_nameserverAdditionalAc                 C   s   |  | jg dS )z
        If the name of the NS response has AAAA records, they are included in
        the additional section of the response.
        N)r7  r4  r   r!   r!   r'   test_nameserverAdditionalAAAA2  s    z7AdditionalProcessingTests.test_nameserverAdditionalAAAAc                 C   s   |  | j| jg dS )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)r7  r2  r4  r   r!   r!   r'   test_nameserverAdditionalBoth:  s    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   Tr  c              	      s$   g | ]}t j |jtj|d dqS r+  r,  r-  r.  r!   r'   r(   S  s      z>AdditionalProcessingTests._answerCNAMETest.<locals>.<listcomp>N)r   r<   r   r&  r   r   r  r   ZCNAMErA   r*  )r1   r%  r   r   r   r  aliasr!   r.  r'   _answerCNAMETestB  s0          
z*AdditionalProcessingTests._answerCNAMETestc                 C   s   |  | jg dS )z
        If the name of the CNAME response has A records, they are included in
        the answer section of the response.
        N)r<  r2  r   r!   r!   r'   test_canonicalNameAnswerAZ  s    z3AdditionalProcessingTests.test_canonicalNameAnswerAc                 C   s   |  | jg dS )z
        If the name of the CNAME response has AAAA records, they are included
        in the answer section of the response.
        N)r<  r4  r   r!   r!   r'   test_canonicalNameAnswerAAAAb  s    z6AdditionalProcessingTests.test_canonicalNameAnswerAAAAc                 C   s   |  | j| jg dS )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<  r2  r4  r   r!   r!   r'   test_canonicalNameAnswerBothj  s    z6AdditionalProcessingTests.test_canonicalNameAnswerBothN)r2   r3   r4   r   r   r   r2  r   r4  r&  r*  r/  r1  r3  r5  r6  r7  r8  r9  r:  r<  r=  r>  r?  r!   r!   r!   r'   r"    s$   

'r"  c                   @   s   e Zd Zdd ZdS )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   )r1   argsZmessagesr!   r'   r  {  s    z3NoInitialResponseTests.test_noAnswer.<locals>.queryc                 _   s   t tdS )NzCouldn't connect)r
   Zfailr   gaierror)r1   rA  kwargsr!   r!   r'   queryProtocol  s    z;NoInitialResponseTests.test_noAnswer.<locals>.queryProtocol)r   r   rr   r  z	fooba.comr   Fr   r   r   r   r:   r   r   r   )r   Z_queryZpatchr   rw   r   r   r   r   r   r   r  r   ZgetHostByNamer   rC  )r1   r  rE  r{   r0   r   r!   rB  r'   test_noAnsweru  s0      
 z$NoInitialResponseTests.test_noAnswerN)r2   r3   r4   rF  r!   r!   r!   r'   r@  s  s   r@  c                   @   s    e Zd Z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:   N)r   r   primary_portdomainsdomain)r1   rJ  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.
        z192.168.1.3i  zexample.netzexample.edur   r:   N)r   ZfromServerAddressAndDomainsr   rJ  rK  rL  rM  )r1   rJ  rx   rN  r!   r!   r'   &test_constructAuthorityFromHostAndPort  s     zESecondaryAuthorityServiceTests.test_constructAuthorityFromHostAndPortN)r2   r3   r4   r   rO  rP  r!   r!   r!   r'   rG    s   rG  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   N)r   r   rJ  rK  rM  r1   	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.
        )rR    rS  rR  rW  N)r   fromServerAddressAndDomainr   rJ  rK  rM  rT  r!   r!   r'   test_explicitPort  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.
        rH  r7   rI  r   rH  r7   ro   N)r   rX  r   _reactortransfer
tcpClientsr   r   buildProtocolr   makeConnectionr   decoder   r
  r   r   r   r   r   )r1   rU  r	   hostrx   rv   timeoutbindAddressprotor~   msgr!   r!   r'   test_transfer  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.
        rZ     example.comr   r:   )r   r   ro   Ns   ns1.example.comzadmin.example.comi@ i  i  i   i%  i.  r;   s   192.168.1.2rk   )r   r   r   )r   r#   r"   z!HrI  T)r#   r   )r   rX  r   r[  r\  r]  r   r^  r   r_  r   r`  r   r
  r   r   r   r   extendr   r   ZtoStrZdataReceivedr   lenr  r   r   )r1   rU  r	   ra  rx   rv   rb  rc  rd  r~   r  r/   r   r   datar   r!   r!   r'   test_lookupAddress	  sN     


  z*SecondaryAuthorityTests.test_lookupAddressN)r2   r3   r4   r   rV  rY  rf  rk  r!   r!   r!   r'   rQ    s
   rQ  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   encodeZ
setContentr   ZBindAuthoritypath)r1   sfpr!   r!   r'   loadBindStringQ  s    

z!BindAuthorityTests.loadBindStringc                 C   s   |  t| _d S r-   )rr  sampleBindZoner   r   r!   r!   r'   r}   a  s    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   r0   keysr   rC   )r1   domZrecr!   r!   r'   test_ttle  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   originZassertInr0   r   r!   r!   r'   test_originFromFilep  s      z&BindAuthorityTests.test_originFromFilec                 C   sB   dD ]8\}}|  | j|d d }| t|d|j qdS )z'
        A records are loaded.
        ))rg  z10.0.0.1)s   no-in.example.comz10.0.0.2r   rt  N)r  r   r   r   r   r   r#   )r1   rv  Zipr'  r!   r!   r'   test_aRecords|  s    
z BindAuthorityTests.test_aRecordsc                 C   s4   |  | jdd d }| tdd|j dS )z*
        AAAA records are loaded.
        rg  r   z2001:db8:10::1rt  N)r  r   r   r   r   r   r#   r1   r'  r!   r!   r'   test_aaaaRecords  s    
z#BindAuthorityTests.test_aaaaRecordsc                 C   s8   |  | jdd d }| tjdddd|j dS )z(
        MX records are loaded.
        rx  r   rM   zmx.example.comrt  )r   r   rC   N)r  r   r   r   r   r   r#   r|  r!   r!   r'   test_mxRecords  s    
  z!BindAuthorityTests.test_mxRecordsc                 C   s6   |  | jdd d }| tjddd|j dS )z+
        CNAME records are loaded.
        s   www.example.comr   rI  rt  r   N)r  r   r   r   r   r   r#   r|  r!   r!   r'   test_cnameRecords  s    
 z$BindAuthorityTests.test_cnameRecordsc              	   C   s8   |  t}| d W 5 Q R X | d|jjd  dS )zO
        loadBindString raises NotImplementedError on invalid records.
        s   example.com. IN LOL 192.168.0.1zRecord type 'LOL' not supportedr   N)assertRaisesNotImplementedErrorrr  r   	exceptionrA  )r1   er!   r!   r'   test_invalidRecordClass  s     
z*BindAuthorityTests.test_invalidRecordClassc              
   C   sN   dD ]D}|  t}| |d  W 5 Q R X | t|d |jjd  qdS )zC
        $INCLUDE and $GENERATE raise NotImplementedError.
        )s   $INCLUDEs	   $GENERATEs    doesNotMatters    directive not implementedr   N)r  r  rr  r   r   r  rA  )r1   Z	directiver  r!   r!   r'   test_invalidDirectives  s    

z)BindAuthorityTests.test_invalidDirectivesN)r2   r3   r4   r   rr  r}   rw  rz  r{  r}  r~  r  r  r  r!   r!   r!   r'   rl  M  s   rl  )Zr   Z
__future__r   r   r   r   r   ior   	functoolsr   r   Zstructr   Ztwisted.trialr   Ztwisted.internetr	   r
   r   Ztwisted.internet.deferr   Ztwisted.namesr   r   r   r   r   Ztwisted.names.dnsr   r   r   r   r   r   Ztwisted.names.errorr   Ztwisted.names.clientr   Ztwisted.names.secondaryr   r   Ztwisted.python.compatr   Ztwisted.python.filepathr   Ztwisted.test.proto_helpersr   r   r    r+   ZFileAuthorityr,   r   Zreverse_soar   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rs   r   rt   ru   ZTestCasern   r   r   r   r  r"  r@  rG  rQ  rs  rl  r!   r!   r!   r'   <module>   s   



		5  9
9s [%5l