Use SimpleResolverComplexifier in tests (#4497)

two reasons for this. One, it saves a bunch of boilerplate. Two, it squashes
unicode to IDNA-in-a-`str` (even on python 3) in a way that it turns out we
rely on to give consistent behaviour between python 2 and 3.
pull/4507/head
Richard van der Hoff 2019-01-29 09:38:29 +00:00 committed by Amber Brown
parent f1a04462eb
commit f2b553d656
3 changed files with 15 additions and 30 deletions

1
changelog.d/4497.feature Normal file
View File

@ -0,0 +1 @@
Implement MSC1708 (.well-known routing for server-server federation)

View File

@ -377,9 +377,8 @@ class MatrixFederationAgentTests(TestCase):
self.mock_resolver.resolve_service.side_effect = lambda _: [] self.mock_resolver.resolve_service.side_effect = lambda _: []
# hostnameendpoint does the lookup on the unicode value (getaddrinfo encodes # the resolver is always called with the IDNA hostname as a native string.
# it back to idna) self.reactor.lookups["xn--bcher-kva.com"] = "1.2.3.4"
self.reactor.lookups[u"bücher.com"] = "1.2.3.4"
# this is idna for bücher.com # this is idna for bücher.com
test_d = self._make_get_request(b"matrix://xn--bcher-kva.com/foo/bar") test_d = self._make_get_request(b"matrix://xn--bcher-kva.com/foo/bar")
@ -424,7 +423,7 @@ class MatrixFederationAgentTests(TestCase):
self.mock_resolver.resolve_service.side_effect = lambda _: [ self.mock_resolver.resolve_service.side_effect = lambda _: [
Server(host=b"xn--trget-3qa.com", port=8443) # târget.com Server(host=b"xn--trget-3qa.com", port=8443) # târget.com
] ]
self.reactor.lookups[u"târget.com"] = "1.2.3.4" self.reactor.lookups["xn--trget-3qa.com"] = "1.2.3.4"
test_d = self._make_get_request(b"matrix://xn--bcher-kva.com/foo/bar") test_d = self._make_get_request(b"matrix://xn--bcher-kva.com/foo/bar")

View File

@ -8,11 +8,10 @@ import attr
from zope.interface import implementer from zope.interface import implementer
from twisted.internet import address, threads, udp from twisted.internet import address, threads, udp
from twisted.internet._resolver import HostResolution from twisted.internet._resolver import SimpleResolverComplexifier
from twisted.internet.address import IPv4Address from twisted.internet.defer import Deferred, fail, succeed
from twisted.internet.defer import Deferred
from twisted.internet.error import DNSLookupError from twisted.internet.error import DNSLookupError
from twisted.internet.interfaces import IReactorPluggableNameResolver from twisted.internet.interfaces import IReactorPluggableNameResolver, IResolverSimple
from twisted.python.failure import Failure from twisted.python.failure import Failure
from twisted.test.proto_helpers import MemoryReactorClock from twisted.test.proto_helpers import MemoryReactorClock
from twisted.web.http import unquote from twisted.web.http import unquote
@ -227,30 +226,16 @@ class ThreadedMemoryReactorClock(MemoryReactorClock):
def __init__(self): def __init__(self):
self._udp = [] self._udp = []
self.lookups = {} lookups = self.lookups = {}
class Resolver(object): @implementer(IResolverSimple)
def resolveHostName( class FakeResolver(object):
_self, def getHostByName(self, name, timeout=None):
resolutionReceiver, if name not in lookups:
hostName, return fail(DNSLookupError("OH NO: unknown %s" % (name, )))
portNumber=0, return succeed(lookups[name])
addressTypes=None,
transportSemantics='TCP',
):
resolution = HostResolution(hostName) self.nameResolver = SimpleResolverComplexifier(FakeResolver())
resolutionReceiver.resolutionBegan(resolution)
if hostName not in self.lookups:
raise DNSLookupError("OH NO")
resolutionReceiver.addressResolved(
IPv4Address('TCP', self.lookups[hostName], portNumber)
)
resolutionReceiver.resolutionComplete()
return resolution
self.nameResolver = Resolver()
super(ThreadedMemoryReactorClock, self).__init__() super(ThreadedMemoryReactorClock, self).__init__()
def listenUDP(self, port, protocol, interface='', maxPacketSize=8196): def listenUDP(self, port, protocol, interface='', maxPacketSize=8196):