test_room_search.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. # Copyright 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. from typing import List, Tuple
  15. from unittest.case import SkipTest
  16. from twisted.test.proto_helpers import MemoryReactor
  17. import synapse.rest.admin
  18. from synapse.api.constants import EventTypes
  19. from synapse.api.errors import StoreError
  20. from synapse.rest.client import login, room
  21. from synapse.server import HomeServer
  22. from synapse.storage.databases.main import DataStore
  23. from synapse.storage.databases.main.search import Phrase, SearchToken, _tokenize_query
  24. from synapse.storage.engines import PostgresEngine
  25. from synapse.storage.engines.sqlite import Sqlite3Engine
  26. from synapse.util import Clock
  27. from tests.unittest import HomeserverTestCase, skip_unless
  28. from tests.utils import USE_POSTGRES_FOR_TESTS
  29. class EventSearchInsertionTest(HomeserverTestCase):
  30. servlets = [
  31. synapse.rest.admin.register_servlets_for_client_rest_resource,
  32. login.register_servlets,
  33. room.register_servlets,
  34. ]
  35. def test_null_byte(self):
  36. """
  37. Postgres/SQLite don't like null bytes going into the search tables. Internally
  38. we replace those with a space.
  39. Ensure this doesn't break anything.
  40. """
  41. # Register a user and create a room, create some messages
  42. self.register_user("alice", "password")
  43. access_token = self.login("alice", "password")
  44. room_id = self.helper.create_room_as("alice", tok=access_token)
  45. # Send messages and ensure they don't cause an internal server
  46. # error
  47. for body in ["hi\u0000bob", "another message", "hi alice"]:
  48. response = self.helper.send(room_id, body, tok=access_token)
  49. self.assertIn("event_id", response)
  50. # Check that search works for the message where the null byte was replaced
  51. store = self.hs.get_datastores().main
  52. result = self.get_success(
  53. store.search_msgs([room_id], "hi bob", ["content.body"])
  54. )
  55. self.assertEqual(result.get("count"), 1)
  56. if isinstance(store.database_engine, PostgresEngine):
  57. self.assertIn("hi", result.get("highlights"))
  58. self.assertIn("bob", result.get("highlights"))
  59. # Check that search works for an unrelated message
  60. result = self.get_success(
  61. store.search_msgs([room_id], "another", ["content.body"])
  62. )
  63. self.assertEqual(result.get("count"), 1)
  64. if isinstance(store.database_engine, PostgresEngine):
  65. self.assertIn("another", result.get("highlights"))
  66. # Check that search works for a search term that overlaps with the message
  67. # containing a null byte and an unrelated message.
  68. result = self.get_success(store.search_msgs([room_id], "hi", ["content.body"]))
  69. self.assertEqual(result.get("count"), 2)
  70. result = self.get_success(
  71. store.search_msgs([room_id], "hi alice", ["content.body"])
  72. )
  73. if isinstance(store.database_engine, PostgresEngine):
  74. self.assertIn("alice", result.get("highlights"))
  75. def test_non_string(self):
  76. """Test that non-string `value`s are not inserted into `event_search`.
  77. This is particularly important when using sqlite, since a sqlite column can hold
  78. both strings and integers. When using Postgres, integers are automatically
  79. converted to strings.
  80. Regression test for #11918.
  81. """
  82. store = self.hs.get_datastores().main
  83. # Register a user and create a room
  84. user_id = self.register_user("alice", "password")
  85. access_token = self.login("alice", "password")
  86. room_id = self.helper.create_room_as("alice", tok=access_token)
  87. room_version = self.get_success(store.get_room_version(room_id))
  88. # Construct a message with a numeric body to be received over federation
  89. # The message can't be sent using the client API, since Synapse's event
  90. # validation will reject it.
  91. prev_event_ids = self.get_success(store.get_prev_events_for_room(room_id))
  92. prev_event = self.get_success(store.get_event(prev_event_ids[0]))
  93. prev_state_map = self.get_success(
  94. self.hs.get_storage_controllers().state.get_state_ids_for_event(
  95. prev_event_ids[0]
  96. )
  97. )
  98. event_dict = {
  99. "type": EventTypes.Message,
  100. "content": {"msgtype": "m.text", "body": 2},
  101. "room_id": room_id,
  102. "sender": user_id,
  103. "depth": prev_event.depth + 1,
  104. "prev_events": prev_event_ids,
  105. "origin_server_ts": self.clock.time_msec(),
  106. }
  107. builder = self.hs.get_event_builder_factory().for_room_version(
  108. room_version, event_dict
  109. )
  110. event = self.get_success(
  111. builder.build(
  112. prev_event_ids=prev_event_ids,
  113. auth_event_ids=self.hs.get_event_auth_handler().compute_auth_events(
  114. builder,
  115. prev_state_map,
  116. for_verification=False,
  117. ),
  118. depth=event_dict["depth"],
  119. )
  120. )
  121. # Receive the event
  122. self.get_success(
  123. self.hs.get_federation_event_handler().on_receive_pdu(
  124. self.hs.hostname, event
  125. )
  126. )
  127. # The event should not have an entry in the `event_search` table
  128. f = self.get_failure(
  129. store.db_pool.simple_select_one_onecol(
  130. "event_search",
  131. {"room_id": room_id, "event_id": event.event_id},
  132. "event_id",
  133. ),
  134. StoreError,
  135. )
  136. self.assertEqual(f.value.code, 404)
  137. @skip_unless(not USE_POSTGRES_FOR_TESTS, "requires sqlite")
  138. def test_sqlite_non_string_deletion_background_update(self):
  139. """Test the background update to delete bad rows from `event_search`."""
  140. store = self.hs.get_datastores().main
  141. # Populate `event_search` with dummy data
  142. self.get_success(
  143. store.db_pool.simple_insert_many(
  144. "event_search",
  145. keys=["event_id", "room_id", "key", "value"],
  146. values=[
  147. ("event1", "room_id", "content.body", "hi"),
  148. ("event2", "room_id", "content.body", "2"),
  149. ("event3", "room_id", "content.body", 3),
  150. ],
  151. desc="populate_event_search",
  152. )
  153. )
  154. # Run the background update
  155. store.db_pool.updates._all_done = False
  156. self.get_success(
  157. store.db_pool.simple_insert(
  158. "background_updates",
  159. {
  160. "update_name": "event_search_sqlite_delete_non_strings",
  161. "progress_json": "{}",
  162. },
  163. )
  164. )
  165. self.wait_for_background_updates()
  166. # The non-string `value`s ought to be gone now.
  167. values = self.get_success(
  168. store.db_pool.simple_select_onecol(
  169. "event_search",
  170. {"room_id": "room_id"},
  171. "value",
  172. ),
  173. )
  174. self.assertCountEqual(values, ["hi", "2"])
  175. class MessageSearchTest(HomeserverTestCase):
  176. """
  177. Check message search.
  178. A powerful way to check the behaviour is to run the following in Postgres >= 11:
  179. # SELECT websearch_to_tsquery('english', <your string>);
  180. The result can be compared to the tokenized version for SQLite and Postgres < 11.
  181. """
  182. servlets = [
  183. synapse.rest.admin.register_servlets_for_client_rest_resource,
  184. login.register_servlets,
  185. room.register_servlets,
  186. ]
  187. PHRASE = "the quick brown fox jumps over the lazy dog"
  188. # Each entry is a search query, followed by a boolean of whether it is in the phrase.
  189. COMMON_CASES = [
  190. ("nope", False),
  191. ("brown", True),
  192. ("quick brown", True),
  193. ("brown quick", True),
  194. ("quick \t brown", True),
  195. ("jump", True),
  196. ("brown nope", False),
  197. ('"brown quick"', False),
  198. ('"jumps over"', True),
  199. ('"quick fox"', False),
  200. ("nope OR doublenope", False),
  201. ("furphy OR fox", True),
  202. ("fox -nope", True),
  203. ("fox -brown", False),
  204. ('"fox" quick', True),
  205. ('"quick brown', True),
  206. ('" quick "', True),
  207. ('" nope"', False),
  208. ]
  209. # TODO Test non-ASCII cases.
  210. # Case that fail on SQLite.
  211. POSTGRES_CASES = [
  212. # SQLite treats NOT as a binary operator.
  213. ("- fox", False),
  214. ("- nope", True),
  215. ('"-fox quick', False),
  216. # PostgreSQL skips stop words.
  217. ('"the quick brown"', True),
  218. ('"over lazy"', True),
  219. ]
  220. def prepare(
  221. self, reactor: MemoryReactor, clock: Clock, homeserver: HomeServer
  222. ) -> None:
  223. # Register a user and create a room, create some messages
  224. self.register_user("alice", "password")
  225. self.access_token = self.login("alice", "password")
  226. self.room_id = self.helper.create_room_as("alice", tok=self.access_token)
  227. # Send the phrase as a message and check it was created
  228. response = self.helper.send(self.room_id, self.PHRASE, tok=self.access_token)
  229. self.assertIn("event_id", response)
  230. # The behaviour of a missing trailing double quote changed in PostgreSQL 14
  231. # from ignoring the initial double quote to treating it as a phrase.
  232. main_store = homeserver.get_datastores().main
  233. found = False
  234. if isinstance(main_store.database_engine, PostgresEngine):
  235. assert main_store.database_engine._version is not None
  236. found = main_store.database_engine._version < 140000
  237. self.COMMON_CASES.append(('"fox quick', found))
  238. def test_tokenize_query(self) -> None:
  239. """Test the custom logic to tokenize a user's query."""
  240. cases = (
  241. ("brown", ["brown"]),
  242. ("quick brown", ["quick", SearchToken.And, "brown"]),
  243. ("quick \t brown", ["quick", SearchToken.And, "brown"]),
  244. ('"brown quick"', [Phrase(["brown", "quick"])]),
  245. ("furphy OR fox", ["furphy", SearchToken.Or, "fox"]),
  246. ("fox -brown", ["fox", SearchToken.Not, "brown"]),
  247. ("- fox", [SearchToken.Not, "fox"]),
  248. ('"fox" quick', [Phrase(["fox"]), SearchToken.And, "quick"]),
  249. # No trailing double quote.
  250. ('"fox quick', [Phrase(["fox", "quick"])]),
  251. ('"-fox quick', [Phrase(["-fox", "quick"])]),
  252. ('" quick "', [Phrase(["quick"])]),
  253. (
  254. 'q"uick brow"n',
  255. [
  256. "q",
  257. SearchToken.And,
  258. Phrase(["uick", "brow"]),
  259. SearchToken.And,
  260. "n",
  261. ],
  262. ),
  263. (
  264. '-"quick brown"',
  265. [SearchToken.Not, Phrase(["quick", "brown"])],
  266. ),
  267. )
  268. for query, expected in cases:
  269. tokenized = _tokenize_query(query)
  270. self.assertEqual(
  271. tokenized, expected, f"{tokenized} != {expected} for {query}"
  272. )
  273. def _check_test_cases(
  274. self, store: DataStore, cases: List[Tuple[str, bool]]
  275. ) -> None:
  276. # Run all the test cases versus search_msgs
  277. for query, expect_to_contain in cases:
  278. result = self.get_success(
  279. store.search_msgs([self.room_id], query, ["content.body"])
  280. )
  281. self.assertEquals(
  282. result["count"],
  283. 1 if expect_to_contain else 0,
  284. f"expected '{query}' to match '{self.PHRASE}'"
  285. if expect_to_contain
  286. else f"'{query}' unexpectedly matched '{self.PHRASE}'",
  287. )
  288. self.assertEquals(
  289. len(result["results"]),
  290. 1 if expect_to_contain else 0,
  291. "results array length should match count",
  292. )
  293. # Run them again versus search_rooms
  294. for query, expect_to_contain in cases:
  295. result = self.get_success(
  296. store.search_rooms([self.room_id], query, ["content.body"], 10)
  297. )
  298. self.assertEquals(
  299. result["count"],
  300. 1 if expect_to_contain else 0,
  301. f"expected '{query}' to match '{self.PHRASE}'"
  302. if expect_to_contain
  303. else f"'{query}' unexpectedly matched '{self.PHRASE}'",
  304. )
  305. self.assertEquals(
  306. len(result["results"]),
  307. 1 if expect_to_contain else 0,
  308. "results array length should match count",
  309. )
  310. def test_postgres_web_search_for_phrase(self):
  311. """
  312. Test searching for phrases using typical web search syntax, as per postgres' websearch_to_tsquery.
  313. This test is skipped unless the postgres instance supports websearch_to_tsquery.
  314. See https://www.postgresql.org/docs/current/textsearch-controls.html
  315. """
  316. store = self.hs.get_datastores().main
  317. if not isinstance(store.database_engine, PostgresEngine):
  318. raise SkipTest("Test only applies when postgres is used as the database")
  319. self._check_test_cases(store, self.COMMON_CASES + self.POSTGRES_CASES)
  320. def test_sqlite_search(self):
  321. """
  322. Test sqlite searching for phrases.
  323. """
  324. store = self.hs.get_datastores().main
  325. if not isinstance(store.database_engine, Sqlite3Engine):
  326. raise SkipTest("Test only applies when sqlite is used as the database")
  327. self._check_test_cases(store, self.COMMON_CASES)