server.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  1. # Copyright 2018-2021 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import hashlib
  15. import json
  16. import logging
  17. import os
  18. import os.path
  19. import sqlite3
  20. import time
  21. import uuid
  22. import warnings
  23. from collections import deque
  24. from io import SEEK_END, BytesIO
  25. from typing import (
  26. Any,
  27. Awaitable,
  28. Callable,
  29. Dict,
  30. Iterable,
  31. List,
  32. MutableMapping,
  33. Optional,
  34. Sequence,
  35. Tuple,
  36. Type,
  37. TypeVar,
  38. Union,
  39. cast,
  40. )
  41. from unittest.mock import Mock
  42. import attr
  43. from typing_extensions import Deque, ParamSpec
  44. from zope.interface import implementer
  45. from twisted.internet import address, threads, udp
  46. from twisted.internet._resolver import SimpleResolverComplexifier
  47. from twisted.internet.defer import Deferred, fail, maybeDeferred, succeed
  48. from twisted.internet.error import DNSLookupError
  49. from twisted.internet.interfaces import (
  50. IAddress,
  51. IConnector,
  52. IConsumer,
  53. IHostnameResolver,
  54. IProducer,
  55. IProtocol,
  56. IPullProducer,
  57. IPushProducer,
  58. IReactorPluggableNameResolver,
  59. IReactorTime,
  60. IResolverSimple,
  61. ITransport,
  62. )
  63. from twisted.internet.protocol import ClientFactory, DatagramProtocol
  64. from twisted.python import threadpool
  65. from twisted.python.failure import Failure
  66. from twisted.test.proto_helpers import AccumulatingProtocol, MemoryReactorClock
  67. from twisted.web.http_headers import Headers
  68. from twisted.web.resource import IResource
  69. from twisted.web.server import Request, Site
  70. from synapse.config.database import DatabaseConnectionConfig
  71. from synapse.config.homeserver import HomeServerConfig
  72. from synapse.events.presence_router import load_legacy_presence_router
  73. from synapse.events.third_party_rules import load_legacy_third_party_event_rules
  74. from synapse.handlers.auth import load_legacy_password_auth_providers
  75. from synapse.http.site import SynapseRequest
  76. from synapse.logging.context import ContextResourceUsage
  77. from synapse.module_api.callbacks.spamchecker_callbacks import load_legacy_spam_checkers
  78. from synapse.server import HomeServer
  79. from synapse.storage import DataStore
  80. from synapse.storage.database import LoggingDatabaseConnection
  81. from synapse.storage.engines import PostgresEngine, create_engine
  82. from synapse.storage.prepare_database import prepare_database
  83. from synapse.types import ISynapseReactor, JsonDict
  84. from synapse.util import Clock
  85. from tests.utils import (
  86. LEAVE_DB,
  87. POSTGRES_BASE_DB,
  88. POSTGRES_HOST,
  89. POSTGRES_PASSWORD,
  90. POSTGRES_PORT,
  91. POSTGRES_USER,
  92. SQLITE_PERSIST_DB,
  93. USE_POSTGRES_FOR_TESTS,
  94. MockClock,
  95. default_config,
  96. )
  97. logger = logging.getLogger(__name__)
  98. R = TypeVar("R")
  99. P = ParamSpec("P")
  100. # the type of thing that can be passed into `make_request` in the headers list
  101. CustomHeaderType = Tuple[Union[str, bytes], Union[str, bytes]]
  102. # A pre-prepared SQLite DB that is used as a template when creating new SQLite
  103. # DB each test run. This dramatically speeds up test set up when using SQLite.
  104. PREPPED_SQLITE_DB_CONN: Optional[LoggingDatabaseConnection] = None
  105. class TimedOutException(Exception):
  106. """
  107. A web query timed out.
  108. """
  109. @implementer(ITransport, IPushProducer, IConsumer)
  110. @attr.s(auto_attribs=True)
  111. class FakeChannel:
  112. """
  113. A fake Twisted Web Channel (the part that interfaces with the
  114. wire).
  115. See twisted.web.http.HTTPChannel.
  116. """
  117. site: Union[Site, "FakeSite"]
  118. _reactor: MemoryReactorClock
  119. result: dict = attr.Factory(dict)
  120. _ip: str = "127.0.0.1"
  121. _producer: Optional[Union[IPullProducer, IPushProducer]] = None
  122. resource_usage: Optional[ContextResourceUsage] = None
  123. _request: Optional[Request] = None
  124. @property
  125. def request(self) -> Request:
  126. assert self._request is not None
  127. return self._request
  128. @request.setter
  129. def request(self, request: Request) -> None:
  130. assert self._request is None
  131. self._request = request
  132. @property
  133. def json_body(self) -> JsonDict:
  134. body = json.loads(self.text_body)
  135. assert isinstance(body, dict)
  136. return body
  137. @property
  138. def json_list(self) -> List[JsonDict]:
  139. body = json.loads(self.text_body)
  140. assert isinstance(body, list)
  141. return body
  142. @property
  143. def text_body(self) -> str:
  144. """The body of the result, utf-8-decoded.
  145. Raises an exception if the request has not yet completed.
  146. """
  147. if not self.is_finished():
  148. raise Exception("Request not yet completed")
  149. return self.result["body"].decode("utf8")
  150. def is_finished(self) -> bool:
  151. """check if the response has been completely received"""
  152. return self.result.get("done", False)
  153. @property
  154. def code(self) -> int:
  155. if not self.result:
  156. raise Exception("No result yet.")
  157. return int(self.result["code"])
  158. @property
  159. def headers(self) -> Headers:
  160. if not self.result:
  161. raise Exception("No result yet.")
  162. h = Headers()
  163. for i in self.result["headers"]:
  164. h.addRawHeader(*i)
  165. return h
  166. def writeHeaders(
  167. self, version: bytes, code: bytes, reason: bytes, headers: Headers
  168. ) -> None:
  169. self.result["version"] = version
  170. self.result["code"] = code
  171. self.result["reason"] = reason
  172. self.result["headers"] = headers
  173. def write(self, data: bytes) -> None:
  174. assert isinstance(data, bytes), "Should be bytes! " + repr(data)
  175. if "body" not in self.result:
  176. self.result["body"] = b""
  177. self.result["body"] += data
  178. def writeSequence(self, data: Iterable[bytes]) -> None:
  179. for x in data:
  180. self.write(x)
  181. def loseConnection(self) -> None:
  182. self.unregisterProducer()
  183. self.transport.loseConnection()
  184. # Type ignore: mypy doesn't like the fact that producer isn't an IProducer.
  185. def registerProducer(self, producer: IProducer, streaming: bool) -> None:
  186. # TODO This should ensure that the IProducer is an IPushProducer or
  187. # IPullProducer, unfortunately twisted.protocols.basic.FileSender does
  188. # implement those, but doesn't declare it.
  189. self._producer = cast(Union[IPushProducer, IPullProducer], producer)
  190. self.producerStreaming = streaming
  191. def _produce() -> None:
  192. if self._producer:
  193. self._producer.resumeProducing()
  194. self._reactor.callLater(0.1, _produce)
  195. if not streaming:
  196. self._reactor.callLater(0.0, _produce)
  197. def unregisterProducer(self) -> None:
  198. if self._producer is None:
  199. return
  200. self._producer = None
  201. def stopProducing(self) -> None:
  202. if self._producer is not None:
  203. self._producer.stopProducing()
  204. def pauseProducing(self) -> None:
  205. raise NotImplementedError()
  206. def resumeProducing(self) -> None:
  207. raise NotImplementedError()
  208. def requestDone(self, _self: Request) -> None:
  209. self.result["done"] = True
  210. if isinstance(_self, SynapseRequest):
  211. assert _self.logcontext is not None
  212. self.resource_usage = _self.logcontext.get_resource_usage()
  213. def getPeer(self) -> IAddress:
  214. # We give an address so that getClientAddress/getClientIP returns a non null entry,
  215. # causing us to record the MAU
  216. return address.IPv4Address("TCP", self._ip, 3423)
  217. def getHost(self) -> IAddress:
  218. # this is called by Request.__init__ to configure Request.host.
  219. return address.IPv4Address("TCP", "127.0.0.1", 8888)
  220. def isSecure(self) -> bool:
  221. return False
  222. @property
  223. def transport(self) -> "FakeChannel":
  224. return self
  225. def await_result(self, timeout_ms: int = 1000) -> None:
  226. """
  227. Wait until the request is finished.
  228. """
  229. end_time = self._reactor.seconds() + timeout_ms / 1000.0
  230. self._reactor.run()
  231. while not self.is_finished():
  232. # If there's a producer, tell it to resume producing so we get content
  233. if self._producer:
  234. self._producer.resumeProducing()
  235. if self._reactor.seconds() > end_time:
  236. raise TimedOutException("Timed out waiting for request to finish.")
  237. self._reactor.advance(0.1)
  238. def extract_cookies(self, cookies: MutableMapping[str, str]) -> None:
  239. """Process the contents of any Set-Cookie headers in the response
  240. Any cookines found are added to the given dict
  241. """
  242. headers = self.headers.getRawHeaders("Set-Cookie")
  243. if not headers:
  244. return
  245. for h in headers:
  246. parts = h.split(";")
  247. k, v = parts[0].split("=", maxsplit=1)
  248. cookies[k] = v
  249. class FakeSite:
  250. """
  251. A fake Twisted Web Site, with mocks of the extra things that
  252. Synapse adds.
  253. """
  254. server_version_string = b"1"
  255. site_tag = "test"
  256. access_logger = logging.getLogger("synapse.access.http.fake")
  257. def __init__(
  258. self,
  259. resource: IResource,
  260. reactor: IReactorTime,
  261. experimental_cors_msc3886: bool = False,
  262. ):
  263. """
  264. Args:
  265. resource: the resource to be used for rendering all requests
  266. """
  267. self._resource = resource
  268. self.reactor = reactor
  269. self.experimental_cors_msc3886 = experimental_cors_msc3886
  270. def getResourceFor(self, request: Request) -> IResource:
  271. return self._resource
  272. def make_request(
  273. reactor: MemoryReactorClock,
  274. site: Union[Site, FakeSite],
  275. method: Union[bytes, str],
  276. path: Union[bytes, str],
  277. content: Union[bytes, str, JsonDict] = b"",
  278. access_token: Optional[str] = None,
  279. request: Type[Request] = SynapseRequest,
  280. shorthand: bool = True,
  281. federation_auth_origin: Optional[bytes] = None,
  282. content_is_form: bool = False,
  283. await_result: bool = True,
  284. custom_headers: Optional[Iterable[CustomHeaderType]] = None,
  285. client_ip: str = "127.0.0.1",
  286. ) -> FakeChannel:
  287. """
  288. Make a web request using the given method, path and content, and render it
  289. Returns the fake Channel object which records the response to the request.
  290. Args:
  291. reactor:
  292. site: The twisted Site to use to render the request
  293. method: The HTTP request method ("verb").
  294. path: The HTTP path, suitably URL encoded (e.g. escaped UTF-8 & spaces and such).
  295. content: The body of the request. JSON-encoded, if a str of bytes.
  296. access_token: The access token to add as authorization for the request.
  297. request: The request class to create.
  298. shorthand: Whether to try and be helpful and prefix the given URL
  299. with the usual REST API path, if it doesn't contain it.
  300. federation_auth_origin: if set to not-None, we will add a fake
  301. Authorization header pretenting to be the given server name.
  302. content_is_form: Whether the content is URL encoded form data. Adds the
  303. 'Content-Type': 'application/x-www-form-urlencoded' header.
  304. await_result: whether to wait for the request to complete rendering. If true,
  305. will pump the reactor until the the renderer tells the channel the request
  306. is finished.
  307. custom_headers: (name, value) pairs to add as request headers
  308. client_ip: The IP to use as the requesting IP. Useful for testing
  309. ratelimiting.
  310. Returns:
  311. channel
  312. """
  313. if not isinstance(method, bytes):
  314. method = method.encode("ascii")
  315. if not isinstance(path, bytes):
  316. path = path.encode("ascii")
  317. # Decorate it to be the full path, if we're using shorthand
  318. if (
  319. shorthand
  320. and not path.startswith(b"/_matrix")
  321. and not path.startswith(b"/_synapse")
  322. ):
  323. if path.startswith(b"/"):
  324. path = path[1:]
  325. path = b"/_matrix/client/r0/" + path
  326. if not path.startswith(b"/"):
  327. path = b"/" + path
  328. if isinstance(content, dict):
  329. content = json.dumps(content).encode("utf8")
  330. if isinstance(content, str):
  331. content = content.encode("utf8")
  332. channel = FakeChannel(site, reactor, ip=client_ip)
  333. req = request(channel, site)
  334. channel.request = req
  335. req.content = BytesIO(content)
  336. # Twisted expects to be at the end of the content when parsing the request.
  337. req.content.seek(0, SEEK_END)
  338. # Old version of Twisted (<20.3.0) have issues with parsing x-www-form-urlencoded
  339. # bodies if the Content-Length header is missing
  340. req.requestHeaders.addRawHeader(
  341. b"Content-Length", str(len(content)).encode("ascii")
  342. )
  343. if access_token:
  344. req.requestHeaders.addRawHeader(
  345. b"Authorization", b"Bearer " + access_token.encode("ascii")
  346. )
  347. if federation_auth_origin is not None:
  348. req.requestHeaders.addRawHeader(
  349. b"Authorization",
  350. b"X-Matrix origin=%s,key=,sig=" % (federation_auth_origin,),
  351. )
  352. if content:
  353. if content_is_form:
  354. req.requestHeaders.addRawHeader(
  355. b"Content-Type", b"application/x-www-form-urlencoded"
  356. )
  357. else:
  358. # Assume the body is JSON
  359. req.requestHeaders.addRawHeader(b"Content-Type", b"application/json")
  360. if custom_headers:
  361. for k, v in custom_headers:
  362. req.requestHeaders.addRawHeader(k, v)
  363. req.parseCookies()
  364. req.requestReceived(method, path, b"1.1")
  365. if await_result:
  366. channel.await_result()
  367. return channel
  368. # ISynapseReactor implies IReactorPluggableNameResolver, but explicitly
  369. # marking this as an implementer of the latter seems to keep mypy-zope happier.
  370. @implementer(IReactorPluggableNameResolver, ISynapseReactor)
  371. class ThreadedMemoryReactorClock(MemoryReactorClock):
  372. """
  373. A MemoryReactorClock that supports callFromThread.
  374. """
  375. def __init__(self) -> None:
  376. self.threadpool = ThreadPool(self)
  377. self._tcp_callbacks: Dict[Tuple[str, int], Callable] = {}
  378. self._udp: List[udp.Port] = []
  379. self.lookups: Dict[str, str] = {}
  380. self._thread_callbacks: Deque[Callable[..., R]] = deque()
  381. lookups = self.lookups
  382. @implementer(IResolverSimple)
  383. class FakeResolver:
  384. def getHostByName(
  385. self, name: str, timeout: Optional[Sequence[int]] = None
  386. ) -> "Deferred[str]":
  387. if name not in lookups:
  388. return fail(DNSLookupError("OH NO: unknown %s" % (name,)))
  389. return succeed(lookups[name])
  390. self.nameResolver = SimpleResolverComplexifier(FakeResolver())
  391. super().__init__()
  392. def installNameResolver(self, resolver: IHostnameResolver) -> IHostnameResolver:
  393. raise NotImplementedError()
  394. def listenUDP(
  395. self,
  396. port: int,
  397. protocol: DatagramProtocol,
  398. interface: str = "",
  399. maxPacketSize: int = 8196,
  400. ) -> udp.Port:
  401. p = udp.Port(port, protocol, interface, maxPacketSize, self)
  402. p.startListening()
  403. self._udp.append(p)
  404. return p
  405. def callFromThread(
  406. self, callable: Callable[..., Any], *args: object, **kwargs: object
  407. ) -> None:
  408. """
  409. Make the callback fire in the next reactor iteration.
  410. """
  411. cb = lambda: callable(*args, **kwargs)
  412. # it's not safe to call callLater() here, so we append the callback to a
  413. # separate queue.
  414. self._thread_callbacks.append(cb)
  415. def callInThread(
  416. self, callable: Callable[..., Any], *args: object, **kwargs: object
  417. ) -> None:
  418. raise NotImplementedError()
  419. def suggestThreadPoolSize(self, size: int) -> None:
  420. raise NotImplementedError()
  421. def getThreadPool(self) -> "threadpool.ThreadPool":
  422. # Cast to match super-class.
  423. return cast(threadpool.ThreadPool, self.threadpool)
  424. def add_tcp_client_callback(
  425. self, host: str, port: int, callback: Callable[[], None]
  426. ) -> None:
  427. """Add a callback that will be invoked when we receive a connection
  428. attempt to the given IP/port using `connectTCP`.
  429. Note that the callback gets run before we return the connection to the
  430. client, which means callbacks cannot block while waiting for writes.
  431. """
  432. self._tcp_callbacks[(host, port)] = callback
  433. def connectTCP(
  434. self,
  435. host: str,
  436. port: int,
  437. factory: ClientFactory,
  438. timeout: float = 30,
  439. bindAddress: Optional[Tuple[str, int]] = None,
  440. ) -> IConnector:
  441. """Fake L{IReactorTCP.connectTCP}."""
  442. conn = super().connectTCP(
  443. host, port, factory, timeout=timeout, bindAddress=None
  444. )
  445. callback = self._tcp_callbacks.get((host, port))
  446. if callback:
  447. callback()
  448. return conn
  449. def advance(self, amount: float) -> None:
  450. # first advance our reactor's time, and run any "callLater" callbacks that
  451. # makes ready
  452. super().advance(amount)
  453. # now run any "callFromThread" callbacks
  454. while True:
  455. try:
  456. callback = self._thread_callbacks.popleft()
  457. except IndexError:
  458. break
  459. callback()
  460. # check for more "callLater" callbacks added by the thread callback
  461. # This isn't required in a regular reactor, but it ends up meaning that
  462. # our database queries can complete in a single call to `advance` [1] which
  463. # simplifies tests.
  464. #
  465. # [1]: we replace the threadpool backing the db connection pool with a
  466. # mock ThreadPool which doesn't really use threads; but we still use
  467. # reactor.callFromThread to feed results back from the db functions to the
  468. # main thread.
  469. super().advance(0)
  470. class ThreadPool:
  471. """
  472. Threadless thread pool.
  473. See twisted.python.threadpool.ThreadPool
  474. """
  475. def __init__(self, reactor: IReactorTime):
  476. self._reactor = reactor
  477. def start(self) -> None:
  478. pass
  479. def stop(self) -> None:
  480. pass
  481. def callInThreadWithCallback(
  482. self,
  483. onResult: Callable[[bool, Union[Failure, R]], None],
  484. function: Callable[P, R],
  485. *args: P.args,
  486. **kwargs: P.kwargs,
  487. ) -> "Deferred[None]":
  488. def _(res: Any) -> None:
  489. if isinstance(res, Failure):
  490. onResult(False, res)
  491. else:
  492. onResult(True, res)
  493. d: "Deferred[None]" = Deferred()
  494. d.addCallback(lambda x: function(*args, **kwargs))
  495. d.addBoth(_)
  496. self._reactor.callLater(0, d.callback, True)
  497. return d
  498. def _make_test_homeserver_synchronous(server: HomeServer) -> None:
  499. """
  500. Make the given test homeserver's database interactions synchronous.
  501. """
  502. clock = server.get_clock()
  503. for database in server.get_datastores().databases:
  504. pool = database._db_pool
  505. def runWithConnection(
  506. func: Callable[..., R], *args: Any, **kwargs: Any
  507. ) -> Awaitable[R]:
  508. return threads.deferToThreadPool(
  509. pool._reactor,
  510. pool.threadpool,
  511. pool._runWithConnection,
  512. func,
  513. *args,
  514. **kwargs,
  515. )
  516. def runInteraction(
  517. desc: str, func: Callable[..., R], *args: Any, **kwargs: Any
  518. ) -> Awaitable[R]:
  519. return threads.deferToThreadPool(
  520. pool._reactor,
  521. pool.threadpool,
  522. pool._runInteraction,
  523. desc,
  524. func,
  525. *args,
  526. **kwargs,
  527. )
  528. pool.runWithConnection = runWithConnection # type: ignore[assignment]
  529. pool.runInteraction = runInteraction # type: ignore[assignment]
  530. # Replace the thread pool with a threadless 'thread' pool
  531. pool.threadpool = ThreadPool(clock._reactor) # type: ignore[assignment]
  532. pool.running = True
  533. # We've just changed the Databases to run DB transactions on the same
  534. # thread, so we need to disable the dedicated thread behaviour.
  535. server.get_datastores().main.USE_DEDICATED_DB_THREADS_FOR_EVENT_FETCHING = False
  536. def get_clock() -> Tuple[ThreadedMemoryReactorClock, Clock]:
  537. clock = ThreadedMemoryReactorClock()
  538. hs_clock = Clock(clock)
  539. return clock, hs_clock
  540. @implementer(ITransport)
  541. @attr.s(cmp=False, auto_attribs=True)
  542. class FakeTransport:
  543. """
  544. A twisted.internet.interfaces.ITransport implementation which sends all its data
  545. straight into an IProtocol object: it exists to connect two IProtocols together.
  546. To use it, instantiate it with the receiving IProtocol, and then pass it to the
  547. sending IProtocol's makeConnection method:
  548. server = HTTPChannel()
  549. client.makeConnection(FakeTransport(server, self.reactor))
  550. If you want bidirectional communication, you'll need two instances.
  551. """
  552. other: IProtocol
  553. """The Protocol object which will receive any data written to this transport.
  554. """
  555. _reactor: IReactorTime
  556. """Test reactor
  557. """
  558. _protocol: Optional[IProtocol] = None
  559. """The Protocol which is producing data for this transport. Optional, but if set
  560. will get called back for connectionLost() notifications etc.
  561. """
  562. _peer_address: IAddress = attr.Factory(
  563. lambda: address.IPv4Address("TCP", "127.0.0.1", 5678)
  564. )
  565. """The value to be returned by getPeer"""
  566. _host_address: IAddress = attr.Factory(
  567. lambda: address.IPv4Address("TCP", "127.0.0.1", 1234)
  568. )
  569. """The value to be returned by getHost"""
  570. disconnecting = False
  571. disconnected = False
  572. connected = True
  573. buffer: bytes = b""
  574. producer: Optional[IPushProducer] = None
  575. autoflush: bool = True
  576. def getPeer(self) -> IAddress:
  577. return self._peer_address
  578. def getHost(self) -> IAddress:
  579. return self._host_address
  580. def loseConnection(self) -> None:
  581. if not self.disconnecting:
  582. logger.info("FakeTransport: loseConnection()")
  583. self.disconnecting = True
  584. if self._protocol:
  585. self._protocol.connectionLost(
  586. Failure(RuntimeError("FakeTransport.loseConnection()"))
  587. )
  588. # if we still have data to write, delay until that is done
  589. if self.buffer:
  590. logger.info(
  591. "FakeTransport: Delaying disconnect until buffer is flushed"
  592. )
  593. else:
  594. self.connected = False
  595. self.disconnected = True
  596. def abortConnection(self) -> None:
  597. logger.info("FakeTransport: abortConnection()")
  598. if not self.disconnecting:
  599. self.disconnecting = True
  600. if self._protocol:
  601. self._protocol.connectionLost(None) # type: ignore[arg-type]
  602. self.disconnected = True
  603. def pauseProducing(self) -> None:
  604. if not self.producer:
  605. return
  606. self.producer.pauseProducing()
  607. def resumeProducing(self) -> None:
  608. if not self.producer:
  609. return
  610. self.producer.resumeProducing()
  611. def unregisterProducer(self) -> None:
  612. if not self.producer:
  613. return
  614. self.producer = None
  615. def registerProducer(self, producer: IPushProducer, streaming: bool) -> None:
  616. self.producer = producer
  617. self.producerStreaming = streaming
  618. def _produce() -> None:
  619. if not self.producer:
  620. # we've been unregistered
  621. return
  622. # some implementations of IProducer (for example, FileSender)
  623. # don't return a deferred.
  624. d = maybeDeferred(self.producer.resumeProducing)
  625. d.addCallback(lambda x: self._reactor.callLater(0.1, _produce))
  626. if not streaming:
  627. self._reactor.callLater(0.0, _produce)
  628. def write(self, byt: bytes) -> None:
  629. if self.disconnecting:
  630. raise Exception("Writing to disconnecting FakeTransport")
  631. self.buffer = self.buffer + byt
  632. # always actually do the write asynchronously. Some protocols (notably the
  633. # TLSMemoryBIOProtocol) get very confused if a read comes back while they are
  634. # still doing a write. Doing a callLater here breaks the cycle.
  635. if self.autoflush:
  636. self._reactor.callLater(0.0, self.flush)
  637. def writeSequence(self, seq: Iterable[bytes]) -> None:
  638. for x in seq:
  639. self.write(x)
  640. def flush(self, maxbytes: Optional[int] = None) -> None:
  641. if not self.buffer:
  642. # nothing to do. Don't write empty buffers: it upsets the
  643. # TLSMemoryBIOProtocol
  644. return
  645. if self.disconnected:
  646. return
  647. if maxbytes is not None:
  648. to_write = self.buffer[:maxbytes]
  649. else:
  650. to_write = self.buffer
  651. logger.info("%s->%s: %s", self._protocol, self.other, to_write)
  652. try:
  653. self.other.dataReceived(to_write)
  654. except Exception as e:
  655. logger.exception("Exception writing to protocol: %s", e)
  656. return
  657. self.buffer = self.buffer[len(to_write) :]
  658. if self.buffer and self.autoflush:
  659. self._reactor.callLater(0.0, self.flush)
  660. if not self.buffer and self.disconnecting:
  661. logger.info("FakeTransport: Buffer now empty, completing disconnect")
  662. self.disconnected = True
  663. def connect_client(
  664. reactor: ThreadedMemoryReactorClock, client_id: int
  665. ) -> Tuple[IProtocol, AccumulatingProtocol]:
  666. """
  667. Connect a client to a fake TCP transport.
  668. Args:
  669. reactor
  670. factory: The connecting factory to build.
  671. """
  672. factory = reactor.tcpClients.pop(client_id)[2]
  673. client = factory.buildProtocol(None)
  674. server = AccumulatingProtocol()
  675. server.makeConnection(FakeTransport(client, reactor))
  676. client.makeConnection(FakeTransport(server, reactor))
  677. return client, server
  678. class TestHomeServer(HomeServer):
  679. DATASTORE_CLASS = DataStore # type: ignore[assignment]
  680. def setup_test_homeserver(
  681. cleanup_func: Callable[[Callable[[], None]], None],
  682. name: str = "test",
  683. config: Optional[HomeServerConfig] = None,
  684. reactor: Optional[ISynapseReactor] = None,
  685. homeserver_to_use: Type[HomeServer] = TestHomeServer,
  686. **kwargs: Any,
  687. ) -> HomeServer:
  688. """
  689. Setup a homeserver suitable for running tests against. Keyword arguments
  690. are passed to the Homeserver constructor.
  691. If no datastore is supplied, one is created and given to the homeserver.
  692. Args:
  693. cleanup_func : The function used to register a cleanup routine for
  694. after the test.
  695. Calling this method directly is deprecated: you should instead derive from
  696. HomeserverTestCase.
  697. """
  698. if reactor is None:
  699. from twisted.internet import reactor as _reactor
  700. reactor = cast(ISynapseReactor, _reactor)
  701. if config is None:
  702. config = default_config(name, parse=True)
  703. config.caches.resize_all_caches()
  704. if "clock" not in kwargs:
  705. kwargs["clock"] = MockClock()
  706. if USE_POSTGRES_FOR_TESTS:
  707. test_db = "synapse_test_%s" % uuid.uuid4().hex
  708. database_config = {
  709. "name": "psycopg2",
  710. "args": {
  711. "database": test_db,
  712. "host": POSTGRES_HOST,
  713. "password": POSTGRES_PASSWORD,
  714. "user": POSTGRES_USER,
  715. "port": POSTGRES_PORT,
  716. "cp_min": 1,
  717. "cp_max": 5,
  718. },
  719. }
  720. else:
  721. if SQLITE_PERSIST_DB:
  722. # The current working directory is in _trial_temp, so this gets created within that directory.
  723. test_db_location = os.path.abspath("test.db")
  724. logger.debug("Will persist db to %s", test_db_location)
  725. # Ensure each test gets a clean database.
  726. try:
  727. os.remove(test_db_location)
  728. except FileNotFoundError:
  729. pass
  730. else:
  731. logger.debug("Removed existing DB at %s", test_db_location)
  732. else:
  733. test_db_location = ":memory:"
  734. database_config = {
  735. "name": "sqlite3",
  736. "args": {"database": test_db_location, "cp_min": 1, "cp_max": 1},
  737. }
  738. # Check if we have set up a DB that we can use as a template.
  739. global PREPPED_SQLITE_DB_CONN
  740. if PREPPED_SQLITE_DB_CONN is None:
  741. temp_engine = create_engine(database_config)
  742. PREPPED_SQLITE_DB_CONN = LoggingDatabaseConnection(
  743. sqlite3.connect(":memory:"), temp_engine, "PREPPED_CONN"
  744. )
  745. database = DatabaseConnectionConfig("master", database_config)
  746. config.database.databases = [database]
  747. prepare_database(
  748. PREPPED_SQLITE_DB_CONN, create_engine(database_config), config
  749. )
  750. database_config["_TEST_PREPPED_CONN"] = PREPPED_SQLITE_DB_CONN
  751. if "db_txn_limit" in kwargs:
  752. database_config["txn_limit"] = kwargs["db_txn_limit"]
  753. database = DatabaseConnectionConfig("master", database_config)
  754. config.database.databases = [database]
  755. db_engine = create_engine(database.config)
  756. # Create the database before we actually try and connect to it, based off
  757. # the template database we generate in setupdb()
  758. if isinstance(db_engine, PostgresEngine):
  759. import psycopg2.extensions
  760. db_conn = db_engine.module.connect(
  761. database=POSTGRES_BASE_DB,
  762. user=POSTGRES_USER,
  763. host=POSTGRES_HOST,
  764. port=POSTGRES_PORT,
  765. password=POSTGRES_PASSWORD,
  766. )
  767. assert isinstance(db_conn, psycopg2.extensions.connection)
  768. db_conn.autocommit = True
  769. cur = db_conn.cursor()
  770. cur.execute("DROP DATABASE IF EXISTS %s;" % (test_db,))
  771. cur.execute(
  772. "CREATE DATABASE %s WITH TEMPLATE %s;" % (test_db, POSTGRES_BASE_DB)
  773. )
  774. cur.close()
  775. db_conn.close()
  776. hs = homeserver_to_use(
  777. name,
  778. config=config,
  779. version_string="Synapse/tests",
  780. reactor=reactor,
  781. )
  782. # Install @cache_in_self attributes
  783. for key, val in kwargs.items():
  784. setattr(hs, "_" + key, val)
  785. # Mock TLS
  786. hs.tls_server_context_factory = Mock()
  787. hs.setup()
  788. if homeserver_to_use == TestHomeServer:
  789. hs.setup_background_tasks()
  790. if isinstance(db_engine, PostgresEngine):
  791. database_pool = hs.get_datastores().databases[0]
  792. # We need to do cleanup on PostgreSQL
  793. def cleanup() -> None:
  794. import psycopg2
  795. import psycopg2.extensions
  796. # Close all the db pools
  797. database_pool._db_pool.close()
  798. dropped = False
  799. # Drop the test database
  800. db_conn = db_engine.module.connect(
  801. database=POSTGRES_BASE_DB,
  802. user=POSTGRES_USER,
  803. host=POSTGRES_HOST,
  804. port=POSTGRES_PORT,
  805. password=POSTGRES_PASSWORD,
  806. )
  807. assert isinstance(db_conn, psycopg2.extensions.connection)
  808. db_conn.autocommit = True
  809. cur = db_conn.cursor()
  810. # Try a few times to drop the DB. Some things may hold on to the
  811. # database for a few more seconds due to flakiness, preventing
  812. # us from dropping it when the test is over. If we can't drop
  813. # it, warn and move on.
  814. for _ in range(5):
  815. try:
  816. cur.execute("DROP DATABASE IF EXISTS %s;" % (test_db,))
  817. db_conn.commit()
  818. dropped = True
  819. except psycopg2.OperationalError as e:
  820. warnings.warn(
  821. "Couldn't drop old db: " + str(e),
  822. category=UserWarning,
  823. stacklevel=2,
  824. )
  825. time.sleep(0.5)
  826. cur.close()
  827. db_conn.close()
  828. if not dropped:
  829. warnings.warn(
  830. "Failed to drop old DB.",
  831. category=UserWarning,
  832. stacklevel=2,
  833. )
  834. if not LEAVE_DB:
  835. # Register the cleanup hook
  836. cleanup_func(cleanup)
  837. # bcrypt is far too slow to be doing in unit tests
  838. # Need to let the HS build an auth handler and then mess with it
  839. # because AuthHandler's constructor requires the HS, so we can't make one
  840. # beforehand and pass it in to the HS's constructor (chicken / egg)
  841. async def hash(p: str) -> str:
  842. return hashlib.md5(p.encode("utf8")).hexdigest()
  843. hs.get_auth_handler().hash = hash # type: ignore[assignment]
  844. async def validate_hash(p: str, h: str) -> bool:
  845. return hashlib.md5(p.encode("utf8")).hexdigest() == h
  846. hs.get_auth_handler().validate_hash = validate_hash # type: ignore[assignment]
  847. # Make the threadpool and database transactions synchronous for testing.
  848. _make_test_homeserver_synchronous(hs)
  849. # Load any configured modules into the homeserver
  850. module_api = hs.get_module_api()
  851. for module, module_config in hs.config.modules.loaded_modules:
  852. module(config=module_config, api=module_api)
  853. load_legacy_spam_checkers(hs)
  854. load_legacy_third_party_event_rules(hs)
  855. load_legacy_presence_router(hs)
  856. load_legacy_password_auth_providers(hs)
  857. return hs