test_room_search.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. import synapse.rest.admin
  15. from synapse.rest.client import login, room
  16. from synapse.storage.engines import PostgresEngine
  17. from tests.unittest import HomeserverTestCase
  18. class NullByteInsertionTest(HomeserverTestCase):
  19. servlets = [
  20. synapse.rest.admin.register_servlets_for_client_rest_resource,
  21. login.register_servlets,
  22. room.register_servlets,
  23. ]
  24. def test_null_byte(self):
  25. """
  26. Postgres/SQLite don't like null bytes going into the search tables. Internally
  27. we replace those with a space.
  28. Ensure this doesn't break anything.
  29. """
  30. # Register a user and create a room, create some messages
  31. self.register_user("alice", "password")
  32. access_token = self.login("alice", "password")
  33. room_id = self.helper.create_room_as("alice", tok=access_token)
  34. # Send messages and ensure they don't cause an internal server
  35. # error
  36. for body in ["hi\u0000bob", "another message", "hi alice"]:
  37. response = self.helper.send(room_id, body, tok=access_token)
  38. self.assertIn("event_id", response)
  39. # Check that search works for the message where the null byte was replaced
  40. store = self.hs.get_datastore()
  41. result = self.get_success(
  42. store.search_msgs([room_id], "hi bob", ["content.body"])
  43. )
  44. self.assertEquals(result.get("count"), 1)
  45. if isinstance(store.database_engine, PostgresEngine):
  46. self.assertIn("hi", result.get("highlights"))
  47. self.assertIn("bob", result.get("highlights"))
  48. # Check that search works for an unrelated message
  49. result = self.get_success(
  50. store.search_msgs([room_id], "another", ["content.body"])
  51. )
  52. self.assertEquals(result.get("count"), 1)
  53. if isinstance(store.database_engine, PostgresEngine):
  54. self.assertIn("another", result.get("highlights"))
  55. # Check that search works for a search term that overlaps with the message
  56. # containing a null byte and an unrelated message.
  57. result = self.get_success(store.search_msgs([room_id], "hi", ["content.body"]))
  58. self.assertEquals(result.get("count"), 2)
  59. result = self.get_success(
  60. store.search_msgs([room_id], "hi alice", ["content.body"])
  61. )
  62. if isinstance(store.database_engine, PostgresEngine):
  63. self.assertIn("alice", result.get("highlights"))