Ϫf (dZddlmZddlmZddlmZddlmZm Z m Z m Z m Z m Z mZmZddlmZmZmZddlmZddlmZGd d eZGd d ZGd deeZGddeeZGddeeZy)zC Tests for the I{hosts(5)}-based resolver, L{twisted.names.hosts}. ) annotations)Protocol) gatherResults)AAAAINA DomainErrorQueryRecord_A Record_AAAARRHeader)Resolver searchFileForsearchFileForAll)FilePath)SynchronousTestCaseceZdZddZy)_SupportsMktempcy)Nselfs ?/usr/lib/python3/dist-packages/twisted/names/test/test_hosts.pymktempz_SupportsMktemp.mktemps N)returnstr)__name__ __module__ __qualname__rrrrrrs rrceZdZddZy)GoodTempPathMixincRt|jjdS)Nzutf-8)rrencoders rpathzGoodTempPathMixin.path!s ,,W566rN)rrrzFilePath[bytes])rrr r%rrrr"r" s7rr"c0eZdZdZddZddZddZddZy)SearchHostsFileTestsz Tests for L{searchFileFor}, a helper which finds the first address for a particular hostname in a I{hosts(5)}-style file. c|j}|jd|jdt|jdy)zr If there is an IPv4 address for the hostname passed to L{searchFileFor}, it is returned. 10.2.3.4 foo.example.com z10.2.3.4foo.example.comNr% setContent assertEqualrrhostss rtest_findAddressz%SearchHostsFileTests.test_findAddress+s:   67 ]5::?Q%RSrc|j}|jd|jt|jdy)z~ If there is no address information for the hostname passed to L{searchFileFor}, L{None} is returned. r)sbar.example.comN)r%r, assertIsNonerr.s rtest_notFoundAddressz)SearchHostsFileTests.test_notFoundAddress4s8   67 - 4FGHrc|j}|jd|jdt|jdy)zS The first address associated with the given hostname is returned. sV::1 foo.example.com 10.1.2.3 foo.example.com fe80::21b:fcff:feee:5a1d foo.example.com ::1r*Nr+r.s rtest_firstAddressz&SearchHostsFileTests.test_firstAddress=s@   :  ejj:L MNrc|j}|jd|jt|jdd|jt|jdd|jt|jdd|jt|jdd|jt|jddy ) z For a host with a canonical name and one or more aliases, L{searchFileFor} can find an address given any of the names. sY127.0.1.1 helmut.example.org helmut # a comment ::1 localhost ip6-localhost ip6-loopback shelmutz 127.0.1.1shelmut.example.orgs ip6-localhostr5s ip6-loopbacks localhostNr+r.s rtest_searchFileForAliasesz.SearchHostsFileTests.test_searchFileForAliasesIs    : uzz9={K uzz3HI;W uzz3CDeL uzz?CUK uzz<@%HrNrNone)rrr __doc__r0r3r6r8rrrr'r'%s TI OIrr'c0eZdZdZddZddZddZddZy)SearchHostsFileForAllTestsz Tests for L{searchFileForAll}, a helper which finds all addresses for a particular hostname in a I{hosts(5)}-style file. c|j}|jd|jgdt|dy)zt L{searchFileForAll} returns a list of all addresses associated with the name passed to it. sc127.0.0.1 foobar.example.com 127.0.0.2 foobar.example.com ::1 foobar.example.com ) 127.0.0.1z 127.0.0.2r5sfoobar.example.comNr%r,r-rr.s rtest_allAddressesz,SearchHostsFileForAllTests.test_allAddressesas@    2  - U$9 : rc|j}|jd|jdgt|dy)zL L{searchFileForAll} searches for names case-insensitively. s!127.0.0.1 foobar.EXAMPLE.com r?sFOOBAR.example.comNr@r.s rtest_caseInsensitivelyz1SearchHostsFileForAllTests.test_caseInsensitivelyqs9  >? +(8@U(VWrcX|jgt|jdy)z If there is an error reading the contents of the hosts file, L{searchFileForAll} returns an empty list. s example.comN)r-rr%rs rtest_readErrorz)SearchHostsFileForAllTests.test_readErrorys" -diik>JKrc|j}|jd|jddgt|dy)zw L{searchFileForAll} ignores any malformed IP addresses associated with the name passed to it. s[127.0.0.1 miser.example.org miser not-an-ip miser not-ascii miser # miser miser ::1 miserr?r5smiserNr@r.s rtest_malformedIPz+SearchHostsFileForAllTests.test_malformedIPsD       % UH - rNr9)rrr r;rArCrErGrrrr=r=[s  XL rr=cxeZdZdZddZddZddZddZddZddZ ddZ dd Z dd Z dd Z dd Zdd ZddZy) HostsTestszJ Tests for the I{hosts(5)}-based L{twisted.names.hosts.Resolver}. c|j}|jdd|_t|j|j|_y)Ns  1.1.1.1 EXAMPLE EXAMPLE.EXAMPLETHING ::2 mixed 1.1.1.2 MIXED ::1 ip6thingy 1.1.1.3 multiple 1.1.1.4 multiple ::3 ip6-multiple ::4 ip6-multiple not-an-ip malformed malformed # malformed 1.1.1.5 malformed ::5 malformed ih)r%r,ttlrresolver)rfs rsetUpzHostsTests.setUps= IIK   " 2 rcPt}|jd|jy)z The default hosts file used by L{Resolver} is I{/etc/hosts} if no value is given for the C{file} initializer parameter. s /etc/hostsN)rr-file)rrLs rtest_defaultPathzHostsTests.test_defaultPaths :  6rcgd}|Dcgc]:\}}|jj|j|j|<}}}|j t |ycc}}w)z L{hosts.Resolver.getHostByName} returns a L{Deferred} which fires with a string giving the address of the queried name as found in the resolver's hosts file. ))EXAMPLE1.1.1.1)sEXAMPLE.EXAMPLETHINGrT)sMIXED1.1.1.2N)rL getHostByName addCallbackr-successResultOfr)rdatanipdss rtest_getHostByNamezHostsTests.test_getHostByNamesh   2 MM ' ' * 6 6t7G7G L   ]2./  s?A'c X|jjd}|j|\}}}|jt dt t |jtd|jt dt t |jtd|jf|y)z L{hosts.Resolver.lookupAddress} returns a L{Deferred} which fires with A records from the hosts file. smultiplez1.1.1.3z1.1.1.4N rL lookupAddressrXr-r rrrKr rdanswers authority additionals rtest_lookupAddresszHostsTests.test_lookupAddresss MM ' ' 4)-)=)=a)@&J aTXXx 4887TUaTXXx 4887TU    rc X|jjd}|j|\}}}|jt dt t |jtd|jt dt t |jtd|jf|y)z L{hosts.Resolver.lookupIPV6Address} returns a L{Deferred} which fires with AAAA records from the hosts file. s ip6-multiplez::3z::4N rLlookupIPV6AddressrXr-r rrrKr ras rtest_lookupIPV6Addressz!HostsTests.test_lookupIPV6Addresss MM + +O <)-)=)=a)@&J #T2txxUDHH9U#T2txxUDHH9U    rc |jjd}|j|\}}}|jt dt t |jtd|jf|y)z L{hosts.Resolver.lookupAllRecords} returns a L{Deferred} which fires with A records from the hosts file. smixedrUN) rLlookupAllRecordsrXr-r rrrKr ras rtest_lookupAllRecordsz HostsTests.test_lookupAllRecordssb MM * *8 4)-)=)=a)@&J  h2txx)TXX1N O Q  rcb|j|jjdty)z| L{hosts.Resolver} fails with L{NotImplementedError} for L{IResolver} methods it doesn't implement. rSN)failureResultOfrLlookupMailExchangeNotImplementedErrorrs rtest_notImplementedzHostsTests.test_notImplementeds'  MM , ,Z 8:M rc|jjtd}|j|\\}}}|j |j j dy)NrSrT)rLqueryr rXr-payload dottedQuadrrbanswerrdres r test_queryzHostsTests.test_querysQ MM  j 1 2*.*>*>q*A')Z 224i@rcb|j|jjdty)z L{hosts.Resolver.lookupAddress} returns a L{Deferred} which fires with L{dns.DomainError} if the name passed in has no addresses in the hosts file. foueoaN)rorLr`r rs rtest_lookupAddressNotFoundz%HostsTests.test_lookupAddressNotFounds# T]]88C[Qrcb|j|jjdty)zj Like L{test_lookupAddressNotFound}, but for L{hosts.Resolver.lookupIPV6Address}. r{N)rorLrir rs rtest_lookupIPV6AddressNotFoundz)HostsTests.test_lookupIPV6AddressNotFound s# T]]<*>q*A')Z  \1b$((HY4Q R  rc |jjd}|j|\\}}}|jt dt t |jtd|j|y)zk Like L{test_lookupAddressMalformed}, but for L{hosts.Resolver.lookupIPV6Address}. rz::5Nrhrws rtest_lookupIPV6Malformedz#HostsTests.test_lookupIPV6Malformed'sa MM + +L 9*.*>*>q*A')Z  \4TXX{5$((7S T  rNr9)rrr r;rNrQr]rfrjrmrrryr|r~rrrrrrrIrIsL3,70"   &   A RVU    rrIN)r; __future__rtypingrtwisted.internet.deferrtwisted.names.dnsrrrr r r r r twisted.names.hostsrrrtwisted.python.filepathrtwisted.trial.unittestrrr"r'r=rIrrrrs#0   JI,6 h 77 3I.0A3Il6 !46G6 r] $&7] r