utils.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 OpenMarket Ltd
  3. # Copyright 2018-2019 New Vector Ltd
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import atexit
  17. import hashlib
  18. import os
  19. import time
  20. import uuid
  21. import warnings
  22. from inspect import getcallargs
  23. from mock import Mock, patch
  24. from six.moves.urllib import parse as urlparse
  25. from twisted.internet import defer, reactor
  26. from synapse.api.constants import EventTypes
  27. from synapse.api.errors import CodeMessageException, cs_error
  28. from synapse.api.room_versions import RoomVersions
  29. from synapse.config.homeserver import HomeServerConfig
  30. from synapse.config.server import DEFAULT_ROOM_VERSION
  31. from synapse.federation.transport import server as federation_server
  32. from synapse.http.server import HttpServer
  33. from synapse.logging.context import LoggingContext
  34. from synapse.server import HomeServer
  35. from synapse.storage import DataStore
  36. from synapse.storage.engines import PostgresEngine, create_engine
  37. from synapse.storage.prepare_database import (
  38. _get_or_create_schema_state,
  39. _setup_new_database,
  40. prepare_database,
  41. )
  42. from synapse.util.ratelimitutils import FederationRateLimiter
  43. # set this to True to run the tests against postgres instead of sqlite.
  44. #
  45. # When running under postgres, we first create a base database with the name
  46. # POSTGRES_BASE_DB and update it to the current schema. Then, for each test case, we
  47. # create another unique database, using the base database as a template.
  48. USE_POSTGRES_FOR_TESTS = os.environ.get("SYNAPSE_POSTGRES", False)
  49. LEAVE_DB = os.environ.get("SYNAPSE_LEAVE_DB", False)
  50. POSTGRES_USER = os.environ.get("SYNAPSE_POSTGRES_USER", None)
  51. POSTGRES_HOST = os.environ.get("SYNAPSE_POSTGRES_HOST", None)
  52. POSTGRES_PASSWORD = os.environ.get("SYNAPSE_POSTGRES_PASSWORD", None)
  53. POSTGRES_BASE_DB = "_synapse_unit_tests_base_%s" % (os.getpid(),)
  54. # the dbname we will connect to in order to create the base database.
  55. POSTGRES_DBNAME_FOR_INITIAL_CREATE = "postgres"
  56. def setupdb():
  57. # If we're using PostgreSQL, set up the db once
  58. if USE_POSTGRES_FOR_TESTS:
  59. # create a PostgresEngine
  60. db_engine = create_engine({"name": "psycopg2", "args": {}})
  61. # connect to postgres to create the base database.
  62. db_conn = db_engine.module.connect(
  63. user=POSTGRES_USER,
  64. host=POSTGRES_HOST,
  65. password=POSTGRES_PASSWORD,
  66. dbname=POSTGRES_DBNAME_FOR_INITIAL_CREATE,
  67. )
  68. db_conn.autocommit = True
  69. cur = db_conn.cursor()
  70. cur.execute("DROP DATABASE IF EXISTS %s;" % (POSTGRES_BASE_DB,))
  71. cur.execute("CREATE DATABASE %s;" % (POSTGRES_BASE_DB,))
  72. cur.close()
  73. db_conn.close()
  74. # Set up in the db
  75. db_conn = db_engine.module.connect(
  76. database=POSTGRES_BASE_DB,
  77. user=POSTGRES_USER,
  78. host=POSTGRES_HOST,
  79. password=POSTGRES_PASSWORD,
  80. )
  81. cur = db_conn.cursor()
  82. _get_or_create_schema_state(cur, db_engine)
  83. _setup_new_database(cur, db_engine)
  84. db_conn.commit()
  85. cur.close()
  86. db_conn.close()
  87. def _cleanup():
  88. db_conn = db_engine.module.connect(
  89. user=POSTGRES_USER,
  90. host=POSTGRES_HOST,
  91. password=POSTGRES_PASSWORD,
  92. dbname=POSTGRES_DBNAME_FOR_INITIAL_CREATE,
  93. )
  94. db_conn.autocommit = True
  95. cur = db_conn.cursor()
  96. cur.execute("DROP DATABASE IF EXISTS %s;" % (POSTGRES_BASE_DB,))
  97. cur.close()
  98. db_conn.close()
  99. atexit.register(_cleanup)
  100. def default_config(name, parse=False):
  101. """
  102. Create a reasonable test config.
  103. """
  104. config_dict = {
  105. "server_name": name,
  106. "media_store_path": "media",
  107. "uploads_path": "uploads",
  108. # the test signing key is just an arbitrary ed25519 key to keep the config
  109. # parser happy
  110. "signing_key": "ed25519 a_lPym qvioDNmfExFBRPgdTU+wtFYKq4JfwFRv7sYVgWvmgJg",
  111. "event_cache_size": 1,
  112. "enable_registration": True,
  113. "enable_registration_captcha": False,
  114. "macaroon_secret_key": "not even a little secret",
  115. "expire_access_token": False,
  116. "trusted_third_party_id_servers": [],
  117. "room_invite_state_types": [],
  118. "password_providers": [],
  119. "worker_replication_url": "",
  120. "worker_app": None,
  121. "block_non_admin_invites": False,
  122. "federation_domain_whitelist": None,
  123. "filter_timeline_limit": 5000,
  124. "user_directory_search_all_users": False,
  125. "user_consent_server_notice_content": None,
  126. "block_events_without_consent_error": None,
  127. "user_consent_at_registration": False,
  128. "user_consent_policy_name": "Privacy Policy",
  129. "media_storage_providers": [],
  130. "autocreate_auto_join_rooms": True,
  131. "auto_join_rooms": [],
  132. "limit_usage_by_mau": False,
  133. "hs_disabled": False,
  134. "hs_disabled_message": "",
  135. "hs_disabled_limit_type": "",
  136. "max_mau_value": 50,
  137. "mau_trial_days": 0,
  138. "mau_stats_only": False,
  139. "mau_limits_reserved_threepids": [],
  140. "admin_contact": None,
  141. "rc_federation": {
  142. "reject_limit": 10,
  143. "sleep_limit": 10,
  144. "sleep_delay": 10,
  145. "concurrent": 10,
  146. },
  147. "rc_message": {"per_second": 10000, "burst_count": 10000},
  148. "rc_registration": {"per_second": 10000, "burst_count": 10000},
  149. "rc_login": {
  150. "address": {"per_second": 10000, "burst_count": 10000},
  151. "account": {"per_second": 10000, "burst_count": 10000},
  152. "failed_attempts": {"per_second": 10000, "burst_count": 10000},
  153. },
  154. "saml2_enabled": False,
  155. "public_baseurl": None,
  156. "default_identity_server": None,
  157. "key_refresh_interval": 24 * 60 * 60 * 1000,
  158. "old_signing_keys": {},
  159. "tls_fingerprints": [],
  160. "use_frozen_dicts": False,
  161. # We need a sane default_room_version, otherwise attempts to create
  162. # rooms will fail.
  163. "default_room_version": DEFAULT_ROOM_VERSION,
  164. # disable user directory updates, because they get done in the
  165. # background, which upsets the test runner.
  166. "update_user_directory": False,
  167. }
  168. if parse:
  169. config = HomeServerConfig()
  170. config.parse_config_dict(config_dict, "", "")
  171. return config
  172. return config_dict
  173. class TestHomeServer(HomeServer):
  174. DATASTORE_CLASS = DataStore
  175. @defer.inlineCallbacks
  176. def setup_test_homeserver(
  177. cleanup_func,
  178. name="test",
  179. datastore=None,
  180. config=None,
  181. reactor=None,
  182. homeserverToUse=TestHomeServer,
  183. **kargs
  184. ):
  185. """
  186. Setup a homeserver suitable for running tests against. Keyword arguments
  187. are passed to the Homeserver constructor.
  188. If no datastore is supplied, one is created and given to the homeserver.
  189. Args:
  190. cleanup_func : The function used to register a cleanup routine for
  191. after the test.
  192. Calling this method directly is deprecated: you should instead derive from
  193. HomeserverTestCase.
  194. """
  195. if reactor is None:
  196. from twisted.internet import reactor
  197. if config is None:
  198. config = default_config(name, parse=True)
  199. config.ldap_enabled = False
  200. if "clock" not in kargs:
  201. kargs["clock"] = MockClock()
  202. if USE_POSTGRES_FOR_TESTS:
  203. test_db = "synapse_test_%s" % uuid.uuid4().hex
  204. config.database_config = {
  205. "name": "psycopg2",
  206. "args": {
  207. "database": test_db,
  208. "host": POSTGRES_HOST,
  209. "password": POSTGRES_PASSWORD,
  210. "user": POSTGRES_USER,
  211. "cp_min": 1,
  212. "cp_max": 5,
  213. },
  214. }
  215. else:
  216. config.database_config = {
  217. "name": "sqlite3",
  218. "args": {"database": ":memory:", "cp_min": 1, "cp_max": 1},
  219. }
  220. db_engine = create_engine(config.database_config)
  221. # Create the database before we actually try and connect to it, based off
  222. # the template database we generate in setupdb()
  223. if datastore is None and isinstance(db_engine, PostgresEngine):
  224. db_conn = db_engine.module.connect(
  225. database=POSTGRES_BASE_DB,
  226. user=POSTGRES_USER,
  227. host=POSTGRES_HOST,
  228. password=POSTGRES_PASSWORD,
  229. )
  230. db_conn.autocommit = True
  231. cur = db_conn.cursor()
  232. cur.execute("DROP DATABASE IF EXISTS %s;" % (test_db,))
  233. cur.execute(
  234. "CREATE DATABASE %s WITH TEMPLATE %s;" % (test_db, POSTGRES_BASE_DB)
  235. )
  236. cur.close()
  237. db_conn.close()
  238. # we need to configure the connection pool to run the on_new_connection
  239. # function, so that we can test code that uses custom sqlite functions
  240. # (like rank).
  241. config.database_config["args"]["cp_openfun"] = db_engine.on_new_connection
  242. if datastore is None:
  243. hs = homeserverToUse(
  244. name,
  245. config=config,
  246. db_config=config.database_config,
  247. version_string="Synapse/tests",
  248. database_engine=db_engine,
  249. tls_server_context_factory=Mock(),
  250. tls_client_options_factory=Mock(),
  251. reactor=reactor,
  252. **kargs
  253. )
  254. # Prepare the DB on SQLite -- PostgreSQL is a copy of an already up to
  255. # date db
  256. if not isinstance(db_engine, PostgresEngine):
  257. db_conn = hs.get_db_conn()
  258. yield prepare_database(db_conn, db_engine, config)
  259. db_conn.commit()
  260. db_conn.close()
  261. else:
  262. # We need to do cleanup on PostgreSQL
  263. def cleanup():
  264. import psycopg2
  265. # Close all the db pools
  266. hs.get_db_pool().close()
  267. dropped = False
  268. # Drop the test database
  269. db_conn = db_engine.module.connect(
  270. database=POSTGRES_BASE_DB,
  271. user=POSTGRES_USER,
  272. host=POSTGRES_HOST,
  273. password=POSTGRES_PASSWORD,
  274. )
  275. db_conn.autocommit = True
  276. cur = db_conn.cursor()
  277. # Try a few times to drop the DB. Some things may hold on to the
  278. # database for a few more seconds due to flakiness, preventing
  279. # us from dropping it when the test is over. If we can't drop
  280. # it, warn and move on.
  281. for x in range(5):
  282. try:
  283. cur.execute("DROP DATABASE IF EXISTS %s;" % (test_db,))
  284. db_conn.commit()
  285. dropped = True
  286. except psycopg2.OperationalError as e:
  287. warnings.warn(
  288. "Couldn't drop old db: " + str(e), category=UserWarning
  289. )
  290. time.sleep(0.5)
  291. cur.close()
  292. db_conn.close()
  293. if not dropped:
  294. warnings.warn("Failed to drop old DB.", category=UserWarning)
  295. if not LEAVE_DB:
  296. # Register the cleanup hook
  297. cleanup_func(cleanup)
  298. hs.setup()
  299. if homeserverToUse.__name__ == "TestHomeServer":
  300. hs.setup_master()
  301. else:
  302. hs = homeserverToUse(
  303. name,
  304. db_pool=None,
  305. datastore=datastore,
  306. config=config,
  307. version_string="Synapse/tests",
  308. database_engine=db_engine,
  309. tls_server_context_factory=Mock(),
  310. tls_client_options_factory=Mock(),
  311. reactor=reactor,
  312. **kargs
  313. )
  314. # bcrypt is far too slow to be doing in unit tests
  315. # Need to let the HS build an auth handler and then mess with it
  316. # because AuthHandler's constructor requires the HS, so we can't make one
  317. # beforehand and pass it in to the HS's constructor (chicken / egg)
  318. hs.get_auth_handler().hash = lambda p: hashlib.md5(p.encode("utf8")).hexdigest()
  319. hs.get_auth_handler().validate_hash = (
  320. lambda p, h: hashlib.md5(p.encode("utf8")).hexdigest() == h
  321. )
  322. fed = kargs.get("resource_for_federation", None)
  323. if fed:
  324. register_federation_servlets(hs, fed)
  325. defer.returnValue(hs)
  326. def register_federation_servlets(hs, resource):
  327. federation_server.register_servlets(
  328. hs,
  329. resource=resource,
  330. authenticator=federation_server.Authenticator(hs),
  331. ratelimiter=FederationRateLimiter(
  332. hs.get_clock(), config=hs.config.rc_federation
  333. ),
  334. )
  335. def get_mock_call_args(pattern_func, mock_func):
  336. """ Return the arguments the mock function was called with interpreted
  337. by the pattern functions argument list.
  338. """
  339. invoked_args, invoked_kargs = mock_func.call_args
  340. return getcallargs(pattern_func, *invoked_args, **invoked_kargs)
  341. def mock_getRawHeaders(headers=None):
  342. headers = headers if headers is not None else {}
  343. def getRawHeaders(name, default=None):
  344. return headers.get(name, default)
  345. return getRawHeaders
  346. # This is a mock /resource/ not an entire server
  347. class MockHttpResource(HttpServer):
  348. def __init__(self, prefix=""):
  349. self.callbacks = [] # 3-tuple of method/pattern/function
  350. self.prefix = prefix
  351. def trigger_get(self, path):
  352. return self.trigger(b"GET", path, None)
  353. @patch("twisted.web.http.Request")
  354. @defer.inlineCallbacks
  355. def trigger(
  356. self, http_method, path, content, mock_request, federation_auth_origin=None
  357. ):
  358. """ Fire an HTTP event.
  359. Args:
  360. http_method : The HTTP method
  361. path : The HTTP path
  362. content : The HTTP body
  363. mock_request : Mocked request to pass to the event so it can get
  364. content.
  365. federation_auth_origin (bytes|None): domain to authenticate as, for federation
  366. Returns:
  367. A tuple of (code, response)
  368. Raises:
  369. KeyError If no event is found which will handle the path.
  370. """
  371. path = self.prefix + path
  372. # annoyingly we return a twisted http request which has chained calls
  373. # to get at the http content, hence mock it here.
  374. mock_content = Mock()
  375. config = {"read.return_value": content}
  376. mock_content.configure_mock(**config)
  377. mock_request.content = mock_content
  378. mock_request.method = http_method.encode("ascii")
  379. mock_request.uri = path.encode("ascii")
  380. mock_request.getClientIP.return_value = "-"
  381. headers = {}
  382. if federation_auth_origin is not None:
  383. headers[b"Authorization"] = [
  384. b"X-Matrix origin=%s,key=,sig=" % (federation_auth_origin,)
  385. ]
  386. mock_request.requestHeaders.getRawHeaders = mock_getRawHeaders(headers)
  387. # return the right path if the event requires it
  388. mock_request.path = path
  389. # add in query params to the right place
  390. try:
  391. mock_request.args = urlparse.parse_qs(path.split("?")[1])
  392. mock_request.path = path.split("?")[0]
  393. path = mock_request.path
  394. except Exception:
  395. pass
  396. if isinstance(path, bytes):
  397. path = path.decode("utf8")
  398. for (method, pattern, func) in self.callbacks:
  399. if http_method != method:
  400. continue
  401. matcher = pattern.match(path)
  402. if matcher:
  403. try:
  404. args = [urlparse.unquote(u) for u in matcher.groups()]
  405. (code, response) = yield func(mock_request, *args)
  406. defer.returnValue((code, response))
  407. except CodeMessageException as e:
  408. defer.returnValue((e.code, cs_error(e.msg, code=e.errcode)))
  409. raise KeyError("No event can handle %s" % path)
  410. def register_paths(self, method, path_patterns, callback):
  411. for path_pattern in path_patterns:
  412. self.callbacks.append((method, path_pattern, callback))
  413. class MockKey(object):
  414. alg = "mock_alg"
  415. version = "mock_version"
  416. signature = b"\x9a\x87$"
  417. @property
  418. def verify_key(self):
  419. return self
  420. def sign(self, message):
  421. return self
  422. def verify(self, message, sig):
  423. assert sig == b"\x9a\x87$"
  424. def encode(self):
  425. return b"<fake_encoded_key>"
  426. class MockClock(object):
  427. now = 1000
  428. def __init__(self):
  429. # list of lists of [absolute_time, callback, expired] in no particular
  430. # order
  431. self.timers = []
  432. self.loopers = []
  433. def time(self):
  434. return self.now
  435. def time_msec(self):
  436. return self.time() * 1000
  437. def call_later(self, delay, callback, *args, **kwargs):
  438. current_context = LoggingContext.current_context()
  439. def wrapped_callback():
  440. LoggingContext.thread_local.current_context = current_context
  441. callback(*args, **kwargs)
  442. t = [self.now + delay, wrapped_callback, False]
  443. self.timers.append(t)
  444. return t
  445. def looping_call(self, function, interval):
  446. self.loopers.append([function, interval / 1000.0, self.now])
  447. def cancel_call_later(self, timer, ignore_errs=False):
  448. if timer[2]:
  449. if not ignore_errs:
  450. raise Exception("Cannot cancel an expired timer")
  451. timer[2] = True
  452. self.timers = [t for t in self.timers if t != timer]
  453. # For unit testing
  454. def advance_time(self, secs):
  455. self.now += secs
  456. timers = self.timers
  457. self.timers = []
  458. for t in timers:
  459. time, callback, expired = t
  460. if expired:
  461. raise Exception("Timer already expired")
  462. if self.now >= time:
  463. t[2] = True
  464. callback()
  465. else:
  466. self.timers.append(t)
  467. for looped in self.loopers:
  468. func, interval, last = looped
  469. if last + interval < self.now:
  470. func()
  471. looped[2] = self.now
  472. def advance_time_msec(self, ms):
  473. self.advance_time(ms / 1000.0)
  474. def time_bound_deferred(self, d, *args, **kwargs):
  475. # We don't bother timing things out for now.
  476. return d
  477. def _format_call(args, kwargs):
  478. return ", ".join(
  479. ["%r" % (a) for a in args] + ["%s=%r" % (k, v) for k, v in kwargs.items()]
  480. )
  481. class DeferredMockCallable(object):
  482. """A callable instance that stores a set of pending call expectations and
  483. return values for them. It allows a unit test to assert that the given set
  484. of function calls are eventually made, by awaiting on them to be called.
  485. """
  486. def __init__(self):
  487. self.expectations = []
  488. self.calls = []
  489. def __call__(self, *args, **kwargs):
  490. self.calls.append((args, kwargs))
  491. if not self.expectations:
  492. raise ValueError(
  493. "%r has no pending calls to handle call(%s)"
  494. % (self, _format_call(args, kwargs))
  495. )
  496. for (call, result, d) in self.expectations:
  497. if args == call[1] and kwargs == call[2]:
  498. d.callback(None)
  499. return result
  500. failure = AssertionError(
  501. "Was not expecting call(%s)" % (_format_call(args, kwargs))
  502. )
  503. for _, _, d in self.expectations:
  504. try:
  505. d.errback(failure)
  506. except Exception:
  507. pass
  508. raise failure
  509. def expect_call_and_return(self, call, result):
  510. self.expectations.append((call, result, defer.Deferred()))
  511. @defer.inlineCallbacks
  512. def await_calls(self, timeout=1000):
  513. deferred = defer.DeferredList(
  514. [d for _, _, d in self.expectations], fireOnOneErrback=True
  515. )
  516. timer = reactor.callLater(
  517. timeout / 1000,
  518. deferred.errback,
  519. AssertionError(
  520. "%d pending calls left: %s"
  521. % (
  522. len([e for e in self.expectations if not e[2].called]),
  523. [e for e in self.expectations if not e[2].called],
  524. )
  525. ),
  526. )
  527. yield deferred
  528. timer.cancel()
  529. self.calls = []
  530. def assert_had_no_calls(self):
  531. if self.calls:
  532. calls = self.calls
  533. self.calls = []
  534. raise AssertionError(
  535. "Expected not to received any calls, got:\n"
  536. + "\n".join(["call(%s)" % _format_call(c[0], c[1]) for c in calls])
  537. )
  538. @defer.inlineCallbacks
  539. def create_room(hs, room_id, creator_id):
  540. """Creates and persist a creation event for the given room
  541. Args:
  542. hs
  543. room_id (str)
  544. creator_id (str)
  545. """
  546. store = hs.get_datastore()
  547. event_builder_factory = hs.get_event_builder_factory()
  548. event_creation_handler = hs.get_event_creation_handler()
  549. builder = event_builder_factory.for_room_version(
  550. RoomVersions.V1,
  551. {
  552. "type": EventTypes.Create,
  553. "state_key": "",
  554. "sender": creator_id,
  555. "room_id": room_id,
  556. "content": {},
  557. },
  558. )
  559. event, context = yield event_creation_handler.create_new_client_event(builder)
  560. yield store.persist_event(event, context)