test_events.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. # Copyright 2020 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, Optional
  15. from twisted.test.proto_helpers import MemoryReactor
  16. from synapse.api.constants import EventTypes, Membership
  17. from synapse.api.room_versions import RoomVersions
  18. from synapse.events import EventBase
  19. from synapse.federation.federation_base import event_from_pdu_json
  20. from synapse.rest import admin
  21. from synapse.rest.client import login, room
  22. from synapse.server import HomeServer
  23. from synapse.types import StateMap
  24. from synapse.util import Clock
  25. from tests.unittest import HomeserverTestCase
  26. class ExtremPruneTestCase(HomeserverTestCase):
  27. servlets = [
  28. admin.register_servlets,
  29. room.register_servlets,
  30. login.register_servlets,
  31. ]
  32. def prepare(
  33. self, reactor: MemoryReactor, clock: Clock, homeserver: HomeServer
  34. ) -> None:
  35. self.state = self.hs.get_state_handler()
  36. persistence = self.hs.get_storage_controllers().persistence
  37. assert persistence is not None
  38. self._persistence = persistence
  39. self._state_storage_controller = self.hs.get_storage_controllers().state
  40. self.store = self.hs.get_datastores().main
  41. self.register_user("user", "pass")
  42. self.token = self.login("user", "pass")
  43. self.room_id = self.helper.create_room_as(
  44. "user", room_version=RoomVersions.V6.identifier, tok=self.token
  45. )
  46. body = self.helper.send(self.room_id, body="Test", tok=self.token)
  47. local_message_event_id = body["event_id"]
  48. # Fudge a remote event and persist it. This will be the extremity before
  49. # the gap.
  50. self.remote_event_1 = event_from_pdu_json(
  51. {
  52. "type": EventTypes.Message,
  53. "state_key": "@user:other",
  54. "content": {},
  55. "room_id": self.room_id,
  56. "sender": "@user:other",
  57. "depth": 5,
  58. "prev_events": [local_message_event_id],
  59. "auth_events": [],
  60. "origin_server_ts": self.clock.time_msec(),
  61. },
  62. RoomVersions.V6,
  63. )
  64. self.persist_event(self.remote_event_1)
  65. # Check that the current extremities is the remote event.
  66. self.assert_extremities([self.remote_event_1.event_id])
  67. def persist_event(
  68. self, event: EventBase, state: Optional[StateMap[str]] = None
  69. ) -> None:
  70. """Persist the event, with optional state"""
  71. context = self.get_success(
  72. self.state.compute_event_context(
  73. event,
  74. state_ids_before_event=state,
  75. partial_state=None if state is None else False,
  76. )
  77. )
  78. self.get_success(self._persistence.persist_event(event, context))
  79. def assert_extremities(self, expected_extremities: List[str]) -> None:
  80. """Assert the current extremities for the room"""
  81. extremities = self.get_success(
  82. self.store.get_prev_events_for_room(self.room_id)
  83. )
  84. self.assertCountEqual(extremities, expected_extremities)
  85. def test_prune_gap(self) -> None:
  86. """Test that we drop extremities after a gap when we see an event from
  87. the same domain.
  88. """
  89. # Fudge a second event which points to an event we don't have. This is a
  90. # state event so that the state changes (otherwise we won't prune the
  91. # extremity as they'll have the same state group).
  92. remote_event_2 = event_from_pdu_json(
  93. {
  94. "type": EventTypes.Member,
  95. "state_key": "@user:other",
  96. "content": {"membership": Membership.JOIN},
  97. "room_id": self.room_id,
  98. "sender": "@user:other",
  99. "depth": 50,
  100. "prev_events": ["$some_unknown_message"],
  101. "auth_events": [],
  102. "origin_server_ts": self.clock.time_msec(),
  103. },
  104. RoomVersions.V6,
  105. )
  106. state_before_gap = self.get_success(
  107. self._state_storage_controller.get_current_state_ids(self.room_id)
  108. )
  109. self.persist_event(remote_event_2, state=state_before_gap)
  110. # Check the new extremity is just the new remote event.
  111. self.assert_extremities([remote_event_2.event_id])
  112. def test_do_not_prune_gap_if_state_different(self) -> None:
  113. """Test that we don't prune extremities after a gap if the resolved
  114. state is different.
  115. """
  116. # Fudge a second event which points to an event we don't have.
  117. remote_event_2 = event_from_pdu_json(
  118. {
  119. "type": EventTypes.Message,
  120. "state_key": "@user:other",
  121. "content": {},
  122. "room_id": self.room_id,
  123. "sender": "@user:other",
  124. "depth": 10,
  125. "prev_events": ["$some_unknown_message"],
  126. "auth_events": [],
  127. "origin_server_ts": self.clock.time_msec(),
  128. },
  129. RoomVersions.V6,
  130. )
  131. # Now we persist it with state with a dropped history visibility
  132. # setting. The state resolution across the old and new event will then
  133. # include it, and so the resolved state won't match the new state.
  134. state_before_gap = dict(
  135. self.get_success(
  136. self._state_storage_controller.get_current_state_ids(self.room_id)
  137. )
  138. )
  139. state_before_gap.pop(("m.room.history_visibility", ""))
  140. context = self.get_success(
  141. self.state.compute_event_context(
  142. remote_event_2,
  143. state_ids_before_event=state_before_gap,
  144. partial_state=False,
  145. )
  146. )
  147. self.get_success(self._persistence.persist_event(remote_event_2, context))
  148. # Check that we haven't dropped the old extremity.
  149. self.assert_extremities([self.remote_event_1.event_id, remote_event_2.event_id])
  150. def test_prune_gap_if_old(self) -> None:
  151. """Test that we drop extremities after a gap when the previous extremity
  152. is "old"
  153. """
  154. # Advance the clock for many days to make the old extremity "old". We
  155. # also set the depth to "lots".
  156. self.reactor.advance(7 * 24 * 60 * 60)
  157. # Fudge a second event which points to an event we don't have. This is a
  158. # state event so that the state changes (otherwise we won't prune the
  159. # extremity as they'll have the same state group).
  160. remote_event_2 = event_from_pdu_json(
  161. {
  162. "type": EventTypes.Member,
  163. "state_key": "@user:other2",
  164. "content": {"membership": Membership.JOIN},
  165. "room_id": self.room_id,
  166. "sender": "@user:other2",
  167. "depth": 10000,
  168. "prev_events": ["$some_unknown_message"],
  169. "auth_events": [],
  170. "origin_server_ts": self.clock.time_msec(),
  171. },
  172. RoomVersions.V6,
  173. )
  174. state_before_gap = self.get_success(
  175. self._state_storage_controller.get_current_state_ids(self.room_id)
  176. )
  177. self.persist_event(remote_event_2, state=state_before_gap)
  178. # Check the new extremity is just the new remote event.
  179. self.assert_extremities([remote_event_2.event_id])
  180. def test_do_not_prune_gap_if_other_server(self) -> None:
  181. """Test that we do not drop extremities after a gap when we see an event
  182. from a different domain.
  183. """
  184. # Fudge a second event which points to an event we don't have. This is a
  185. # state event so that the state changes (otherwise we won't prune the
  186. # extremity as they'll have the same state group).
  187. remote_event_2 = event_from_pdu_json(
  188. {
  189. "type": EventTypes.Member,
  190. "state_key": "@user:other2",
  191. "content": {"membership": Membership.JOIN},
  192. "room_id": self.room_id,
  193. "sender": "@user:other2",
  194. "depth": 10,
  195. "prev_events": ["$some_unknown_message"],
  196. "auth_events": [],
  197. "origin_server_ts": self.clock.time_msec(),
  198. },
  199. RoomVersions.V6,
  200. )
  201. state_before_gap = self.get_success(
  202. self._state_storage_controller.get_current_state_ids(self.room_id)
  203. )
  204. self.persist_event(remote_event_2, state=state_before_gap)
  205. # Check the new extremity is just the new remote event.
  206. self.assert_extremities([self.remote_event_1.event_id, remote_event_2.event_id])
  207. def test_prune_gap_if_dummy_remote(self) -> None:
  208. """Test that we drop extremities after a gap when the previous extremity
  209. is a local dummy event and only points to remote events.
  210. """
  211. body = self.helper.send_event(
  212. self.room_id, type=EventTypes.Dummy, content={}, tok=self.token
  213. )
  214. local_message_event_id = body["event_id"]
  215. self.assert_extremities([local_message_event_id])
  216. # Advance the clock for many days to make the old extremity "old". We
  217. # also set the depth to "lots".
  218. self.reactor.advance(7 * 24 * 60 * 60)
  219. # Fudge a second event which points to an event we don't have. This is a
  220. # state event so that the state changes (otherwise we won't prune the
  221. # extremity as they'll have the same state group).
  222. remote_event_2 = event_from_pdu_json(
  223. {
  224. "type": EventTypes.Member,
  225. "state_key": "@user:other2",
  226. "content": {"membership": Membership.JOIN},
  227. "room_id": self.room_id,
  228. "sender": "@user:other2",
  229. "depth": 10000,
  230. "prev_events": ["$some_unknown_message"],
  231. "auth_events": [],
  232. "origin_server_ts": self.clock.time_msec(),
  233. },
  234. RoomVersions.V6,
  235. )
  236. state_before_gap = self.get_success(
  237. self._state_storage_controller.get_current_state_ids(self.room_id)
  238. )
  239. self.persist_event(remote_event_2, state=state_before_gap)
  240. # Check the new extremity is just the new remote event.
  241. self.assert_extremities([remote_event_2.event_id])
  242. def test_prune_gap_if_dummy_local(self) -> None:
  243. """Test that we don't drop extremities after a gap when the previous
  244. extremity is a local dummy event and points to local events.
  245. """
  246. body = self.helper.send(self.room_id, body="Test", tok=self.token)
  247. body = self.helper.send_event(
  248. self.room_id, type=EventTypes.Dummy, content={}, tok=self.token
  249. )
  250. local_message_event_id = body["event_id"]
  251. self.assert_extremities([local_message_event_id])
  252. # Advance the clock for many days to make the old extremity "old". We
  253. # also set the depth to "lots".
  254. self.reactor.advance(7 * 24 * 60 * 60)
  255. # Fudge a second event which points to an event we don't have. This is a
  256. # state event so that the state changes (otherwise we won't prune the
  257. # extremity as they'll have the same state group).
  258. remote_event_2 = event_from_pdu_json(
  259. {
  260. "type": EventTypes.Member,
  261. "state_key": "@user:other2",
  262. "content": {"membership": Membership.JOIN},
  263. "room_id": self.room_id,
  264. "sender": "@user:other2",
  265. "depth": 10000,
  266. "prev_events": ["$some_unknown_message"],
  267. "auth_events": [],
  268. "origin_server_ts": self.clock.time_msec(),
  269. },
  270. RoomVersions.V6,
  271. )
  272. state_before_gap = self.get_success(
  273. self._state_storage_controller.get_current_state_ids(self.room_id)
  274. )
  275. self.persist_event(remote_event_2, state=state_before_gap)
  276. # Check the new extremity is just the new remote event.
  277. self.assert_extremities([remote_event_2.event_id, local_message_event_id])
  278. def test_do_not_prune_gap_if_not_dummy(self) -> None:
  279. """Test that we do not drop extremities after a gap when the previous extremity
  280. is not a dummy event.
  281. """
  282. body = self.helper.send(self.room_id, body="test", tok=self.token)
  283. local_message_event_id = body["event_id"]
  284. self.assert_extremities([local_message_event_id])
  285. # Fudge a second event which points to an event we don't have. This is a
  286. # state event so that the state changes (otherwise we won't prune the
  287. # extremity as they'll have the same state group).
  288. remote_event_2 = event_from_pdu_json(
  289. {
  290. "type": EventTypes.Member,
  291. "state_key": "@user:other2",
  292. "content": {"membership": Membership.JOIN},
  293. "room_id": self.room_id,
  294. "sender": "@user:other2",
  295. "depth": 10000,
  296. "prev_events": ["$some_unknown_message"],
  297. "auth_events": [],
  298. "origin_server_ts": self.clock.time_msec(),
  299. },
  300. RoomVersions.V6,
  301. )
  302. state_before_gap = self.get_success(
  303. self._state_storage_controller.get_current_state_ids(self.room_id)
  304. )
  305. self.persist_event(remote_event_2, state=state_before_gap)
  306. # Check the new extremity is just the new remote event.
  307. self.assert_extremities([local_message_event_id, remote_event_2.event_id])
  308. class InvalideUsersInRoomCacheTestCase(HomeserverTestCase):
  309. servlets = [
  310. admin.register_servlets,
  311. room.register_servlets,
  312. login.register_servlets,
  313. ]
  314. def prepare(
  315. self, reactor: MemoryReactor, clock: Clock, homeserver: HomeServer
  316. ) -> None:
  317. self.state = self.hs.get_state_handler()
  318. persistence = self.hs.get_storage_controllers().persistence
  319. assert persistence is not None
  320. self._persistence = persistence
  321. self.store = self.hs.get_datastores().main
  322. def test_remote_user_rooms_cache_invalidated(self) -> None:
  323. """Test that if the server leaves a room the `get_rooms_for_user` cache
  324. is invalidated for remote users.
  325. """
  326. # Set up a room with a local and remote user in it.
  327. user_id = self.register_user("user", "pass")
  328. token = self.login("user", "pass")
  329. room_id = self.helper.create_room_as(
  330. "user", room_version=RoomVersions.V6.identifier, tok=token
  331. )
  332. body = self.helper.send(room_id, body="Test", tok=token)
  333. local_message_event_id = body["event_id"]
  334. # Fudge a join event for a remote user.
  335. remote_user = "@user:other"
  336. remote_event_1 = event_from_pdu_json(
  337. {
  338. "type": EventTypes.Member,
  339. "state_key": remote_user,
  340. "content": {"membership": Membership.JOIN},
  341. "room_id": room_id,
  342. "sender": remote_user,
  343. "depth": 5,
  344. "prev_events": [local_message_event_id],
  345. "auth_events": [],
  346. "origin_server_ts": self.clock.time_msec(),
  347. },
  348. RoomVersions.V6,
  349. )
  350. context = self.get_success(self.state.compute_event_context(remote_event_1))
  351. self.get_success(self._persistence.persist_event(remote_event_1, context))
  352. # Call `get_rooms_for_user` to add the remote user to the cache
  353. rooms = self.get_success(self.store.get_rooms_for_user(remote_user))
  354. self.assertEqual(set(rooms), {room_id})
  355. # Now we have the local server leave the room, and check that calling
  356. # `get_user_in_room` for the remote user no longer includes the room.
  357. self.helper.leave(room_id, user_id, tok=token)
  358. rooms = self.get_success(self.store.get_rooms_for_user(remote_user))
  359. self.assertEqual(set(rooms), set())
  360. def test_room_remote_user_cache_invalidated(self) -> None:
  361. """Test that if the server leaves a room the `get_users_in_room` cache
  362. is invalidated for remote users.
  363. """
  364. # Set up a room with a local and remote user in it.
  365. user_id = self.register_user("user", "pass")
  366. token = self.login("user", "pass")
  367. room_id = self.helper.create_room_as(
  368. "user", room_version=RoomVersions.V6.identifier, tok=token
  369. )
  370. body = self.helper.send(room_id, body="Test", tok=token)
  371. local_message_event_id = body["event_id"]
  372. # Fudge a join event for a remote user.
  373. remote_user = "@user:other"
  374. remote_event_1 = event_from_pdu_json(
  375. {
  376. "type": EventTypes.Member,
  377. "state_key": remote_user,
  378. "content": {"membership": Membership.JOIN},
  379. "room_id": room_id,
  380. "sender": remote_user,
  381. "depth": 5,
  382. "prev_events": [local_message_event_id],
  383. "auth_events": [],
  384. "origin_server_ts": self.clock.time_msec(),
  385. },
  386. RoomVersions.V6,
  387. )
  388. context = self.get_success(self.state.compute_event_context(remote_event_1))
  389. self.get_success(self._persistence.persist_event(remote_event_1, context))
  390. # Call `get_users_in_room` to add the remote user to the cache
  391. users = self.get_success(self.store.get_users_in_room(room_id))
  392. self.assertEqual(set(users), {user_id, remote_user})
  393. # Now we have the local server leave the room, and check that calling
  394. # `get_user_in_room` for the remote user no longer includes the room.
  395. self.helper.leave(room_id, user_id, tok=token)
  396. users = self.get_success(self.store.get_users_in_room(room_id))
  397. self.assertEqual(users, [])