2018-07-09 02:09:20 -04:00
|
|
|
import json
|
2018-06-27 05:37:24 -04:00
|
|
|
from io import BytesIO
|
|
|
|
|
|
|
|
from six import text_type
|
|
|
|
|
2018-07-09 02:09:20 -04:00
|
|
|
import attr
|
2018-09-13 10:15:51 -04:00
|
|
|
from zope.interface import implementer
|
2018-07-09 02:09:20 -04:00
|
|
|
|
2018-09-13 10:15:51 -04:00
|
|
|
from twisted.internet import address, threads, udp
|
|
|
|
from twisted.internet._resolver import HostResolution
|
|
|
|
from twisted.internet.address import IPv4Address
|
2018-06-27 05:37:24 -04:00
|
|
|
from twisted.internet.defer import Deferred
|
2018-09-13 10:15:51 -04:00
|
|
|
from twisted.internet.error import DNSLookupError
|
|
|
|
from twisted.internet.interfaces import IReactorPluggableNameResolver
|
2018-07-09 02:09:20 -04:00
|
|
|
from twisted.python.failure import Failure
|
2018-06-27 05:37:24 -04:00
|
|
|
from twisted.test.proto_helpers import MemoryReactorClock
|
|
|
|
|
|
|
|
from synapse.http.site import SynapseRequest
|
2018-08-08 22:22:01 -04:00
|
|
|
from synapse.util import Clock
|
2018-07-09 02:09:20 -04:00
|
|
|
|
2018-06-27 05:37:24 -04:00
|
|
|
from tests.utils import setup_test_homeserver as _sth
|
|
|
|
|
|
|
|
|
|
|
|
@attr.s
|
|
|
|
class FakeChannel(object):
|
|
|
|
"""
|
|
|
|
A fake Twisted Web Channel (the part that interfaces with the
|
|
|
|
wire).
|
|
|
|
"""
|
|
|
|
|
2018-07-09 19:11:39 -04:00
|
|
|
result = attr.ib(default=attr.Factory(dict))
|
2018-08-15 09:43:41 -04:00
|
|
|
_producer = None
|
2018-06-27 05:37:24 -04:00
|
|
|
|
|
|
|
@property
|
|
|
|
def json_body(self):
|
|
|
|
if not self.result:
|
|
|
|
raise Exception("No result yet.")
|
2018-08-08 22:22:01 -04:00
|
|
|
return json.loads(self.result["body"].decode('utf8'))
|
|
|
|
|
|
|
|
@property
|
|
|
|
def code(self):
|
|
|
|
if not self.result:
|
|
|
|
raise Exception("No result yet.")
|
|
|
|
return int(self.result["code"])
|
2018-06-27 05:37:24 -04:00
|
|
|
|
|
|
|
def writeHeaders(self, version, code, reason, headers):
|
|
|
|
self.result["version"] = version
|
|
|
|
self.result["code"] = code
|
|
|
|
self.result["reason"] = reason
|
|
|
|
self.result["headers"] = headers
|
|
|
|
|
|
|
|
def write(self, content):
|
|
|
|
if "body" not in self.result:
|
|
|
|
self.result["body"] = b""
|
|
|
|
|
|
|
|
self.result["body"] += content
|
|
|
|
|
2018-08-15 09:43:41 -04:00
|
|
|
def registerProducer(self, producer, streaming):
|
|
|
|
self._producer = producer
|
|
|
|
|
|
|
|
def unregisterProducer(self):
|
|
|
|
if self._producer is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
self._producer = None
|
|
|
|
|
2018-06-27 05:37:24 -04:00
|
|
|
def requestDone(self, _self):
|
|
|
|
self.result["done"] = True
|
|
|
|
|
|
|
|
def getPeer(self):
|
2018-08-23 13:33:04 -04:00
|
|
|
# We give an address so that getClientIP returns a non null entry,
|
|
|
|
# causing us to record the MAU
|
2018-09-06 12:58:18 -04:00
|
|
|
return address.IPv4Address("TCP", "127.0.0.1", 3423)
|
2018-06-27 05:37:24 -04:00
|
|
|
|
|
|
|
def getHost(self):
|
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def transport(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
|
|
class FakeSite:
|
|
|
|
"""
|
|
|
|
A fake Twisted Web Site, with mocks of the extra things that
|
|
|
|
Synapse adds.
|
|
|
|
"""
|
|
|
|
|
|
|
|
server_version_string = b"1"
|
|
|
|
site_tag = "test"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def access_logger(self):
|
|
|
|
class FakeLogger:
|
|
|
|
def info(self, *args, **kwargs):
|
|
|
|
pass
|
|
|
|
|
|
|
|
return FakeLogger()
|
|
|
|
|
|
|
|
|
2018-09-20 06:14:34 -04:00
|
|
|
def make_request(method, path, content=b"", access_token=None, request=SynapseRequest):
|
2018-06-27 05:37:24 -04:00
|
|
|
"""
|
|
|
|
Make a web request using the given method and path, feed it the
|
|
|
|
content, and return the Request and the Channel underneath.
|
|
|
|
"""
|
2018-08-08 22:22:01 -04:00
|
|
|
if not isinstance(method, bytes):
|
|
|
|
method = method.encode('ascii')
|
|
|
|
|
|
|
|
if not isinstance(path, bytes):
|
|
|
|
path = path.encode('ascii')
|
2018-06-27 05:37:24 -04:00
|
|
|
|
2018-07-17 06:43:18 -04:00
|
|
|
# Decorate it to be the full path
|
|
|
|
if not path.startswith(b"/_matrix"):
|
|
|
|
path = b"/_matrix/client/r0/" + path
|
2018-08-08 22:22:01 -04:00
|
|
|
path = path.replace(b"//", b"/")
|
2018-07-17 06:43:18 -04:00
|
|
|
|
2018-06-27 05:37:24 -04:00
|
|
|
if isinstance(content, text_type):
|
|
|
|
content = content.encode('utf8')
|
|
|
|
|
|
|
|
site = FakeSite()
|
|
|
|
channel = FakeChannel()
|
|
|
|
|
2018-09-20 06:14:34 -04:00
|
|
|
req = request(site, channel)
|
2018-06-27 05:37:24 -04:00
|
|
|
req.process = lambda: b""
|
|
|
|
req.content = BytesIO(content)
|
2018-08-23 13:33:04 -04:00
|
|
|
|
|
|
|
if access_token:
|
|
|
|
req.requestHeaders.addRawHeader(b"Authorization", b"Bearer " + access_token)
|
|
|
|
|
2018-09-20 06:14:34 -04:00
|
|
|
if content:
|
|
|
|
req.requestHeaders.addRawHeader(b"Content-Type", b"application/json")
|
|
|
|
|
2018-06-27 05:37:24 -04:00
|
|
|
req.requestReceived(method, path, b"1.1")
|
|
|
|
|
|
|
|
return req, channel
|
|
|
|
|
|
|
|
|
2018-08-15 09:43:41 -04:00
|
|
|
def wait_until_result(clock, request, timeout=100):
|
2018-06-27 05:37:24 -04:00
|
|
|
"""
|
2018-08-15 09:43:41 -04:00
|
|
|
Wait until the request is finished.
|
2018-06-27 05:37:24 -04:00
|
|
|
"""
|
|
|
|
clock.run()
|
|
|
|
x = 0
|
|
|
|
|
2018-08-15 09:43:41 -04:00
|
|
|
while not request.finished:
|
|
|
|
|
|
|
|
# If there's a producer, tell it to resume producing so we get content
|
|
|
|
if request._channel._producer:
|
|
|
|
request._channel._producer.resumeProducing()
|
|
|
|
|
2018-06-27 05:37:24 -04:00
|
|
|
x += 1
|
|
|
|
|
|
|
|
if x > timeout:
|
|
|
|
raise Exception("Timed out waiting for request to finish.")
|
|
|
|
|
|
|
|
clock.advance(0.1)
|
|
|
|
|
|
|
|
|
2018-07-17 06:43:18 -04:00
|
|
|
def render(request, resource, clock):
|
|
|
|
request.render(resource)
|
2018-08-15 09:43:41 -04:00
|
|
|
wait_until_result(clock, request)
|
2018-07-17 06:43:18 -04:00
|
|
|
|
|
|
|
|
2018-09-13 10:15:51 -04:00
|
|
|
@implementer(IReactorPluggableNameResolver)
|
2018-06-27 05:37:24 -04:00
|
|
|
class ThreadedMemoryReactorClock(MemoryReactorClock):
|
|
|
|
"""
|
|
|
|
A MemoryReactorClock that supports callFromThread.
|
|
|
|
"""
|
2018-08-10 09:54:09 -04:00
|
|
|
|
2018-09-13 10:15:51 -04:00
|
|
|
def __init__(self):
|
|
|
|
self._udp = []
|
|
|
|
self.lookups = {}
|
|
|
|
|
|
|
|
class Resolver(object):
|
|
|
|
def resolveHostName(
|
|
|
|
_self,
|
|
|
|
resolutionReceiver,
|
|
|
|
hostName,
|
|
|
|
portNumber=0,
|
|
|
|
addressTypes=None,
|
|
|
|
transportSemantics='TCP',
|
|
|
|
):
|
|
|
|
|
|
|
|
resolution = HostResolution(hostName)
|
|
|
|
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__()
|
|
|
|
|
|
|
|
def listenUDP(self, port, protocol, interface='', maxPacketSize=8196):
|
|
|
|
p = udp.Port(port, protocol, interface, maxPacketSize, self)
|
|
|
|
p.startListening()
|
|
|
|
self._udp.append(p)
|
|
|
|
return p
|
|
|
|
|
2018-06-27 05:37:24 -04:00
|
|
|
def callFromThread(self, callback, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Make the callback fire in the next reactor iteration.
|
|
|
|
"""
|
|
|
|
d = Deferred()
|
|
|
|
d.addCallback(lambda x: callback(*args, **kwargs))
|
|
|
|
self.callLater(0, d.callback, True)
|
|
|
|
return d
|
|
|
|
|
|
|
|
|
2018-08-13 02:47:46 -04:00
|
|
|
def setup_test_homeserver(cleanup_func, *args, **kwargs):
|
2018-06-27 05:37:24 -04:00
|
|
|
"""
|
|
|
|
Set up a synchronous test server, driven by the reactor used by
|
|
|
|
the homeserver.
|
|
|
|
"""
|
2018-08-13 02:47:46 -04:00
|
|
|
d = _sth(cleanup_func, *args, **kwargs).result
|
|
|
|
|
|
|
|
if isinstance(d, Failure):
|
|
|
|
d.raiseException()
|
2018-06-27 05:37:24 -04:00
|
|
|
|
|
|
|
# Make the thread pool synchronous.
|
|
|
|
clock = d.get_clock()
|
|
|
|
pool = d.get_db_pool()
|
|
|
|
|
|
|
|
def runWithConnection(func, *args, **kwargs):
|
|
|
|
return threads.deferToThreadPool(
|
|
|
|
pool._reactor,
|
|
|
|
pool.threadpool,
|
|
|
|
pool._runWithConnection,
|
|
|
|
func,
|
|
|
|
*args,
|
|
|
|
**kwargs
|
|
|
|
)
|
|
|
|
|
|
|
|
def runInteraction(interaction, *args, **kwargs):
|
|
|
|
return threads.deferToThreadPool(
|
|
|
|
pool._reactor,
|
|
|
|
pool.threadpool,
|
|
|
|
pool._runInteraction,
|
|
|
|
interaction,
|
|
|
|
*args,
|
|
|
|
**kwargs
|
|
|
|
)
|
|
|
|
|
|
|
|
pool.runWithConnection = runWithConnection
|
|
|
|
pool.runInteraction = runInteraction
|
|
|
|
|
|
|
|
class ThreadPool:
|
|
|
|
"""
|
|
|
|
Threadless thread pool.
|
|
|
|
"""
|
2018-08-10 09:54:09 -04:00
|
|
|
|
2018-06-27 05:37:24 -04:00
|
|
|
def start(self):
|
|
|
|
pass
|
|
|
|
|
2018-08-13 02:47:46 -04:00
|
|
|
def stop(self):
|
|
|
|
pass
|
|
|
|
|
2018-06-27 05:37:24 -04:00
|
|
|
def callInThreadWithCallback(self, onResult, function, *args, **kwargs):
|
|
|
|
def _(res):
|
|
|
|
if isinstance(res, Failure):
|
|
|
|
onResult(False, res)
|
|
|
|
else:
|
|
|
|
onResult(True, res)
|
|
|
|
|
|
|
|
d = Deferred()
|
|
|
|
d.addCallback(lambda x: function(*args, **kwargs))
|
|
|
|
d.addBoth(_)
|
|
|
|
clock._reactor.callLater(0, d.callback, True)
|
|
|
|
return d
|
|
|
|
|
|
|
|
clock.threadpool = ThreadPool()
|
|
|
|
pool.threadpool = ThreadPool()
|
2018-09-03 12:21:48 -04:00
|
|
|
pool.running = True
|
2018-06-27 05:37:24 -04:00
|
|
|
return d
|
2018-08-08 22:22:01 -04:00
|
|
|
|
|
|
|
|
|
|
|
def get_clock():
|
|
|
|
clock = ThreadedMemoryReactorClock()
|
|
|
|
hs_clock = Clock(clock)
|
|
|
|
return (clock, hs_clock)
|
2018-09-18 13:17:15 -04:00
|
|
|
|
|
|
|
|
|
|
|
@attr.s
|
|
|
|
class FakeTransport(object):
|
|
|
|
"""
|
|
|
|
A twisted.internet.interfaces.ITransport implementation which sends all its data
|
|
|
|
straight into an IProtocol object: it exists to connect two IProtocols together.
|
|
|
|
|
|
|
|
To use it, instantiate it with the receiving IProtocol, and then pass it to the
|
|
|
|
sending IProtocol's makeConnection method:
|
|
|
|
|
|
|
|
server = HTTPChannel()
|
|
|
|
client.makeConnection(FakeTransport(server, self.reactor))
|
|
|
|
|
|
|
|
If you want bidirectional communication, you'll need two instances.
|
|
|
|
"""
|
|
|
|
|
|
|
|
other = attr.ib()
|
|
|
|
"""The Protocol object which will receive any data written to this transport.
|
|
|
|
|
|
|
|
:type: twisted.internet.interfaces.IProtocol
|
|
|
|
"""
|
|
|
|
|
|
|
|
_reactor = attr.ib()
|
|
|
|
"""Test reactor
|
|
|
|
|
|
|
|
:type: twisted.internet.interfaces.IReactorTime
|
|
|
|
"""
|
|
|
|
|
|
|
|
disconnecting = False
|
|
|
|
buffer = attr.ib(default=b'')
|
|
|
|
producer = attr.ib(default=None)
|
|
|
|
|
|
|
|
def getPeer(self):
|
|
|
|
return None
|
|
|
|
|
|
|
|
def getHost(self):
|
|
|
|
return None
|
|
|
|
|
|
|
|
def loseConnection(self):
|
|
|
|
self.disconnecting = True
|
|
|
|
|
|
|
|
def abortConnection(self):
|
|
|
|
self.disconnecting = True
|
|
|
|
|
|
|
|
def pauseProducing(self):
|
|
|
|
self.producer.pauseProducing()
|
|
|
|
|
|
|
|
def unregisterProducer(self):
|
|
|
|
if not self.producer:
|
|
|
|
return
|
|
|
|
|
|
|
|
self.producer = None
|
|
|
|
|
|
|
|
def registerProducer(self, producer, streaming):
|
|
|
|
self.producer = producer
|
|
|
|
self.producerStreaming = streaming
|
|
|
|
|
|
|
|
def _produce():
|
|
|
|
d = self.producer.resumeProducing()
|
|
|
|
d.addCallback(lambda x: self._reactor.callLater(0.1, _produce))
|
|
|
|
|
|
|
|
if not streaming:
|
|
|
|
self._reactor.callLater(0.0, _produce)
|
|
|
|
|
|
|
|
def write(self, byt):
|
|
|
|
self.buffer = self.buffer + byt
|
|
|
|
|
|
|
|
def _write():
|
|
|
|
if getattr(self.other, "transport") is not None:
|
|
|
|
self.other.dataReceived(self.buffer)
|
|
|
|
self.buffer = b""
|
|
|
|
return
|
|
|
|
|
|
|
|
self._reactor.callLater(0.0, _write)
|
|
|
|
|
|
|
|
_write()
|
|
|
|
|
|
|
|
def writeSequence(self, seq):
|
|
|
|
for x in seq:
|
|
|
|
self.write(x)
|