Ϫf$dZddlZddlmZddlmZmZddlmZm Z ddl m Z ddl m Z ddlmZmZmZdd lmZdd lmZdd lmZGd d eZGddej2ZGddej6Zy)z- Test cases for L{twisted.names.srvconnect}. N) verifyObject)deferprotocol)DNSLookupErrorServiceNameUnknownError) IConnector) MemoryReactor)clientdns srvconnect) ResolverBase) DNSNameError)unittestceZdZdZddZdZy) FakeResolvera Resolver that only gives out one given result. Either L{results} or L{failure} must be set and will be used for the return value of L{_lookup} @ivar results: List of L{dns.RRHeader} for the desired result. @type results: C{list} @ivar failure: Failure with an exception from L{twisted.names.error}. @type failure: L{Failure} Nc.||_||_g|_yN)resultsfailurelookups)selfrrs D/usr/lib/python3/dist-packages/twisted/names/test/test_srvconnect.py__init__zFakeResolver.__init__$s   c|jj||||f|j"tj|jggfStj |j S)z9 Return the result or failure on lookup. )rappendrrsucceedfailr)rnameclsqtypetimeouts r_lookupzFakeResolver._lookup)sV T3w78 << #==$,,B!78 8::dll+ +r)NN)__name__ __module__ __qualname____doc__rr#rrrrs  ,rrceZdZdZdZdZy) DummyFactoryzL Dummy client factory that stores the reason of connection failure. cd|_yrreasonrs rrzDummyFactory.__init__9s  rc||_yrr,)r connectorr-s rclientConnectionFailedz#DummyFactory.clientConnectionFailed<s  rN)r$r%r&r'rr1r(rrr*r*4srr*cjeZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZdZy)SRVConnectorTestsz/ Tests for L{srvconnect.SRVConnector}. c|jtdtt|_t |_tj|jdd|j |_ g|_ g|_ y)N theResolver xmpp-server example.org) patchr rr reactorr*factoryr SRVConnectorr0 randIntArgsrandIntResultsr.s rsetUpzSRVConnectorTests.setUpEs[ 6=,.9$ #~ #00 LL-   rcr|jj||f|jjdS)a Fake randint. Returns the first element of L{randIntResults} and records the arguments passed to it in L{randIntArgs}. @param min: Lower bound of the random number. @type min: L{int} @param max: Higher bound of the random number. @type max: L{int} @return: Fake random number from L{randIntResults}. @rtype: L{int} r)r<rr=pop)rminmaxs r_randintzSRVConnectorTests._randintOs3 c +""&&q))rc8tt|jy)zF L{srvconnect.SRVConnector} implements L{IConnector}. N)rrr0r.s rtest_interfacez SRVConnectorTests.test_interfacebs Z0rctjddd}tjdtjtjd|gt j _|jj|j|jj|j|jjj!ddd y) zS Test connectTCP gets called with the address from the SRV record. }host.example.org<porttargetttlr7rtyper rMpayloadN)rHrG)r Record_SRVRRHeaderSRVINr r5rr0connect assertIsNoner:r- assertEqualr9 tcpClientsr@rrPs rtest_SRVPresentz!SRVConnectorTests.test_SRVPresenths..d3E2N LL"cff"g & "   $,,--. 00446r:"   $,,--. 00446r:"    ++112IJKrctjdtjtjddgtj _|jj|j|jj|j|jjjdddy)zU Test connectTCP gets called with fallback parameters on bad result. r7rINrNrQr])r rSCNAMErUr r5rr0rVrWr:r-rXr9rYr@r.s rtest_SRVBadResultz#SRVConnectorTests.test_SRVBadResults LL"BPT & "   $,,--.  LL # # ' ' )"1 -/M rctjddd}tjdtjtjd|gt j _|jj|j|jj|jjjt|j|j j"gy)zH Test that connecting fails when no service is present. i.rIrJr7rNN)r rRrSrTrUr r5rr0rVassertIsNotNoner:r-traprrXr9rYrZs rtest_SRVNoServicez#SRVConnectorTests.test_SRVNoServices..d4R@ LL"cff"g & "   T\\001   0 00"5rcgtj_|jj tjj dd}|j d|y)zW The lookup name is a native string from service, protocol and domain. rs_xmpp-server._tcp.example.orgN)r r5rr0rVrrX)rrs rtest_SRVLookupNamez$SRVConnectorTests.test_SRVLookupNamesN&("  !!))"-a0 94@rctj|jdd|j|_|j d|jj y)zq L{srvconnect.SRVConnector} automatically encodes unicode domain using C{idna} encoding. z xmpp-clientuéchec.example.orgsxn--chec-9oa.example.orgN)r r;r9r:r0rXdomainr.s rtest_unicodeDomainz$SRVConnectorTests.test_unicodeDomainsE $00 LL-)A4<<  4dnn6K6KLrctjdddd}tjdddd}||g|j_g|j_|j t d|jddg|_|jj|j|jdd |jj|j|jd d ddg|_|jj|j|jd d |jj|j|jd dy)a pickServer calculates running sum of weights and calls randint. This exercises the server selection algorithm specified in RFC 2782 by preparing fake L{random.randint} results and checking the values it was called with. rchost1.example.orghost2.example.orgrandint r)r)rryrQ)rr{N) r rRr0orderedServersserversr8randomrCr= pickServerrXr<rrecord1record2s rtest_pickServerWeightsz(SRVConnectorTests.test_pickServerWeightss'..R/BC..R/BC)0'(:%!# 69dmm4 "1g !!# ))!,g6 !!# ))!,g6 "1g !!# ))!,g6 !!# ))!,g6rctjdddd}tjdddd}||g|j_g|j_|j t d|jddg|_|jd|jj|jd |jjy ) zR Two records with equal priorities compare on weight (ascending). ryrcrzr{r|r}rrzrcr|rcN r rRr0rrr8rrCr=rXrrs rtest_pickServerSamePrioritiesz/SRVConnectorTests.test_pickServerSamePrioritiess..R/BC..R/BC)0'(:%!# 69dmm4 !f 4dnn6O6O6QR 4dnn6O6O6QRrctjdddd}tjdddd}||g|j_g|j_|j t d|jddg|_|jd|jj|jd |jjy ) zX Two records with differing priorities compare on priority (ascending). ryrrcrzr{r|r}rrNrrs rtest_srvDifferentPrioritiesz-SRVConnectorTests.test_srvDifferentPrioritiess..Q.AB..Q.AB)0'(:%!# 69dmm4 !f 4dnn6O6O6QR 4dnn6O6O6QRrN)r$r%r&r'r>rCrEr[r^r`rerirlrqrtrwrrrr(rrr3r3@sZ!*&1 X    S$ L 6 AM7BS Srr3)r'rzope.interface.verifyrtwisted.internetrrtwisted.internet.errorrrtwisted.internet.interfacesrtwisted.internet.testingr twisted.namesr r r twisted.names.commonr twisted.names.errorr twisted.trialrr ClientFactoryr*TestCaser3r(rrrsa .,J2211-,",<,: 8)) aS))aSr