test_events.py 18 KB

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