1
0

utils.py 18 KB

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