test_visibility.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 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 logging
  16. from twisted.internet import defer
  17. from twisted.internet.defer import succeed
  18. from synapse.events import FrozenEvent
  19. from synapse.visibility import filter_events_for_server
  20. import tests.unittest
  21. from tests.utils import create_room, setup_test_homeserver
  22. logger = logging.getLogger(__name__)
  23. TEST_ROOM_ID = "!TEST:ROOM"
  24. class FilterEventsForServerTestCase(tests.unittest.TestCase):
  25. @defer.inlineCallbacks
  26. def setUp(self):
  27. self.hs = yield setup_test_homeserver(self.addCleanup)
  28. self.event_creation_handler = self.hs.get_event_creation_handler()
  29. self.event_builder_factory = self.hs.get_event_builder_factory()
  30. self.store = self.hs.get_datastore()
  31. yield create_room(self.hs, TEST_ROOM_ID, "@someone:ROOM")
  32. @defer.inlineCallbacks
  33. def test_filtering(self):
  34. #
  35. # The events to be filtered consist of 10 membership events (it doesn't
  36. # really matter if they are joins or leaves, so let's make them joins).
  37. # One of those membership events is going to be for a user on the
  38. # server we are filtering for (so we can check the filtering is doing
  39. # the right thing).
  40. #
  41. # before we do that, we persist some other events to act as state.
  42. self.inject_visibility("@admin:hs", "joined")
  43. for i in range(0, 10):
  44. yield self.inject_room_member("@resident%i:hs" % i)
  45. events_to_filter = []
  46. for i in range(0, 10):
  47. user = "@user%i:%s" % (i, "test_server" if i == 5 else "other_server")
  48. evt = yield self.inject_room_member(user, extra_content={"a": "b"})
  49. events_to_filter.append(evt)
  50. filtered = yield filter_events_for_server(
  51. self.store, "test_server", events_to_filter
  52. )
  53. # the result should be 5 redacted events, and 5 unredacted events.
  54. for i in range(0, 5):
  55. self.assertEqual(events_to_filter[i].event_id, filtered[i].event_id)
  56. self.assertNotIn("a", filtered[i].content)
  57. for i in range(5, 10):
  58. self.assertEqual(events_to_filter[i].event_id, filtered[i].event_id)
  59. self.assertEqual(filtered[i].content["a"], "b")
  60. @tests.unittest.DEBUG
  61. @defer.inlineCallbacks
  62. def test_erased_user(self):
  63. # 4 message events, from erased and unerased users, with a membership
  64. # change in the middle of them.
  65. events_to_filter = []
  66. evt = yield self.inject_message("@unerased:local_hs")
  67. events_to_filter.append(evt)
  68. evt = yield self.inject_message("@erased:local_hs")
  69. events_to_filter.append(evt)
  70. evt = yield self.inject_room_member("@joiner:remote_hs")
  71. events_to_filter.append(evt)
  72. evt = yield self.inject_message("@unerased:local_hs")
  73. events_to_filter.append(evt)
  74. evt = yield self.inject_message("@erased:local_hs")
  75. events_to_filter.append(evt)
  76. # the erasey user gets erased
  77. yield self.hs.get_datastore().mark_user_erased("@erased:local_hs")
  78. # ... and the filtering happens.
  79. filtered = yield filter_events_for_server(
  80. self.store, "test_server", events_to_filter
  81. )
  82. for i in range(0, len(events_to_filter)):
  83. self.assertEqual(
  84. events_to_filter[i].event_id,
  85. filtered[i].event_id,
  86. "Unexpected event at result position %i" % (i,),
  87. )
  88. for i in (0, 3):
  89. self.assertEqual(
  90. events_to_filter[i].content["body"],
  91. filtered[i].content["body"],
  92. "Unexpected event content at result position %i" % (i,),
  93. )
  94. for i in (1, 4):
  95. self.assertNotIn("body", filtered[i].content)
  96. @defer.inlineCallbacks
  97. def inject_visibility(self, user_id, visibility):
  98. content = {"history_visibility": visibility}
  99. builder = self.event_builder_factory.new(
  100. {
  101. "type": "m.room.history_visibility",
  102. "sender": user_id,
  103. "state_key": "",
  104. "room_id": TEST_ROOM_ID,
  105. "content": content,
  106. }
  107. )
  108. event, context = yield self.event_creation_handler.create_new_client_event(
  109. builder
  110. )
  111. yield self.hs.get_datastore().persist_event(event, context)
  112. defer.returnValue(event)
  113. @defer.inlineCallbacks
  114. def inject_room_member(self, user_id, membership="join", extra_content={}):
  115. content = {"membership": membership}
  116. content.update(extra_content)
  117. builder = self.event_builder_factory.new(
  118. {
  119. "type": "m.room.member",
  120. "sender": user_id,
  121. "state_key": user_id,
  122. "room_id": TEST_ROOM_ID,
  123. "content": content,
  124. }
  125. )
  126. event, context = yield self.event_creation_handler.create_new_client_event(
  127. builder
  128. )
  129. yield self.hs.get_datastore().persist_event(event, context)
  130. defer.returnValue(event)
  131. @defer.inlineCallbacks
  132. def inject_message(self, user_id, content=None):
  133. if content is None:
  134. content = {"body": "testytest"}
  135. builder = self.event_builder_factory.new(
  136. {
  137. "type": "m.room.message",
  138. "sender": user_id,
  139. "room_id": TEST_ROOM_ID,
  140. "content": content,
  141. }
  142. )
  143. event, context = yield self.event_creation_handler.create_new_client_event(
  144. builder
  145. )
  146. yield self.hs.get_datastore().persist_event(event, context)
  147. defer.returnValue(event)
  148. @defer.inlineCallbacks
  149. def test_large_room(self):
  150. # see what happens when we have a large room with hundreds of thousands
  151. # of membership events
  152. # As above, the events to be filtered consist of 10 membership events,
  153. # where one of them is for a user on the server we are filtering for.
  154. import cProfile
  155. import pstats
  156. import time
  157. # we stub out the store, because building up all that state the normal
  158. # way is very slow.
  159. test_store = _TestStore()
  160. # our initial state is 100000 membership events and one
  161. # history_visibility event.
  162. room_state = []
  163. history_visibility_evt = FrozenEvent(
  164. {
  165. "event_id": "$history_vis",
  166. "type": "m.room.history_visibility",
  167. "sender": "@resident_user_0:test.com",
  168. "state_key": "",
  169. "room_id": TEST_ROOM_ID,
  170. "content": {"history_visibility": "joined"},
  171. }
  172. )
  173. room_state.append(history_visibility_evt)
  174. test_store.add_event(history_visibility_evt)
  175. for i in range(0, 100000):
  176. user = "@resident_user_%i:test.com" % (i,)
  177. evt = FrozenEvent(
  178. {
  179. "event_id": "$res_event_%i" % (i,),
  180. "type": "m.room.member",
  181. "state_key": user,
  182. "sender": user,
  183. "room_id": TEST_ROOM_ID,
  184. "content": {"membership": "join", "extra": "zzz,"},
  185. }
  186. )
  187. room_state.append(evt)
  188. test_store.add_event(evt)
  189. events_to_filter = []
  190. for i in range(0, 10):
  191. user = "@user%i:%s" % (i, "test_server" if i == 5 else "other_server")
  192. evt = FrozenEvent(
  193. {
  194. "event_id": "$evt%i" % (i,),
  195. "type": "m.room.member",
  196. "state_key": user,
  197. "sender": user,
  198. "room_id": TEST_ROOM_ID,
  199. "content": {"membership": "join", "extra": "zzz"},
  200. }
  201. )
  202. events_to_filter.append(evt)
  203. room_state.append(evt)
  204. test_store.add_event(evt)
  205. test_store.set_state_ids_for_event(
  206. evt, {(e.type, e.state_key): e.event_id for e in room_state}
  207. )
  208. pr = cProfile.Profile()
  209. pr.enable()
  210. logger.info("Starting filtering")
  211. start = time.time()
  212. filtered = yield filter_events_for_server(
  213. test_store, "test_server", events_to_filter
  214. )
  215. logger.info("Filtering took %f seconds", time.time() - start)
  216. pr.disable()
  217. with open("filter_events_for_server.profile", "w+") as f:
  218. ps = pstats.Stats(pr, stream=f).sort_stats('cumulative')
  219. ps.print_stats()
  220. # the result should be 5 redacted events, and 5 unredacted events.
  221. for i in range(0, 5):
  222. self.assertEqual(events_to_filter[i].event_id, filtered[i].event_id)
  223. self.assertNotIn("extra", filtered[i].content)
  224. for i in range(5, 10):
  225. self.assertEqual(events_to_filter[i].event_id, filtered[i].event_id)
  226. self.assertEqual(filtered[i].content["extra"], "zzz")
  227. test_large_room.skip = "Disabled by default because it's slow"
  228. class _TestStore(object):
  229. """Implements a few methods of the DataStore, so that we can test
  230. filter_events_for_server
  231. """
  232. def __init__(self):
  233. # data for get_events: a map from event_id to event
  234. self.events = {}
  235. # data for get_state_ids_for_events mock: a map from event_id to
  236. # a map from (type_state_key) -> event_id for the state at that
  237. # event
  238. self.state_ids_for_events = {}
  239. def add_event(self, event):
  240. self.events[event.event_id] = event
  241. def set_state_ids_for_event(self, event, state):
  242. self.state_ids_for_events[event.event_id] = state
  243. def get_state_ids_for_events(self, events, types):
  244. res = {}
  245. include_memberships = False
  246. for (type, state_key) in types:
  247. if type == "m.room.history_visibility":
  248. continue
  249. if type != "m.room.member" or state_key is not None:
  250. raise RuntimeError(
  251. "Unimplemented: get_state_ids with type (%s, %s)"
  252. % (type, state_key)
  253. )
  254. include_memberships = True
  255. if include_memberships:
  256. for event_id in events:
  257. res[event_id] = self.state_ids_for_events[event_id]
  258. else:
  259. k = ("m.room.history_visibility", "")
  260. for event_id in events:
  261. hve = self.state_ids_for_events[event_id][k]
  262. res[event_id] = {k: hve}
  263. return succeed(res)
  264. def get_events(self, events):
  265. return succeed({event_id: self.events[event_id] for event_id in events})
  266. def are_users_erased(self, users):
  267. return succeed({u: False for u in users})