1
0

test_complexity.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. # Copyright 2019 Matrix.org Foundation
  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 unittest.mock import AsyncMock
  15. from synapse.api.errors import Codes, SynapseError
  16. from synapse.rest import admin
  17. from synapse.rest.client import login, room
  18. from synapse.types import JsonDict, UserID, create_requester
  19. from tests import unittest
  20. class RoomComplexityTests(unittest.FederatingHomeserverTestCase):
  21. servlets = [
  22. admin.register_servlets,
  23. room.register_servlets,
  24. login.register_servlets,
  25. ]
  26. def default_config(self) -> JsonDict:
  27. config = super().default_config()
  28. config["limit_remote_rooms"] = {"enabled": True, "complexity": 0.05}
  29. return config
  30. def test_complexity_simple(self) -> None:
  31. u1 = self.register_user("u1", "pass")
  32. u1_token = self.login("u1", "pass")
  33. room_1 = self.helper.create_room_as(u1, tok=u1_token)
  34. self.helper.send_state(
  35. room_1, event_type="m.room.topic", body={"topic": "foo"}, tok=u1_token
  36. )
  37. # Get the room complexity
  38. channel = self.make_signed_federation_request(
  39. "GET", "/_matrix/federation/unstable/rooms/%s/complexity" % (room_1,)
  40. )
  41. self.assertEqual(200, channel.code)
  42. complexity = channel.json_body["v1"]
  43. self.assertTrue(complexity > 0, complexity)
  44. # Artificially raise the complexity
  45. store = self.hs.get_datastores().main
  46. async def get_current_state_event_counts(room_id: str) -> int:
  47. return int(500 * 1.23)
  48. store.get_current_state_event_counts = get_current_state_event_counts # type: ignore[method-assign]
  49. # Get the room complexity again -- make sure it's our artificial value
  50. channel = self.make_signed_federation_request(
  51. "GET", "/_matrix/federation/unstable/rooms/%s/complexity" % (room_1,)
  52. )
  53. self.assertEqual(200, channel.code)
  54. complexity = channel.json_body["v1"]
  55. self.assertEqual(complexity, 1.23)
  56. def test_join_too_large(self) -> None:
  57. u1 = self.register_user("u1", "pass")
  58. handler = self.hs.get_room_member_handler()
  59. fed_transport = self.hs.get_federation_transport_client()
  60. # Mock out some things, because we don't want to test the whole join
  61. fed_transport.client.get_json = AsyncMock(return_value={"v1": 9999}) # type: ignore[method-assign]
  62. handler.federation_handler.do_invite_join = AsyncMock( # type: ignore[method-assign]
  63. return_value=("", 1)
  64. )
  65. d = handler._remote_join(
  66. create_requester(u1),
  67. ["other.example.com"],
  68. "roomid",
  69. UserID.from_string(u1),
  70. {"membership": "join"},
  71. )
  72. self.pump()
  73. # The request failed with a SynapseError saying the resource limit was
  74. # exceeded.
  75. f = self.get_failure(d, SynapseError)
  76. self.assertEqual(f.value.code, 400, f.value)
  77. self.assertEqual(f.value.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
  78. def test_join_too_large_admin(self) -> None:
  79. # Check whether an admin can join if option "admins_can_join" is undefined,
  80. # this option defaults to false, so the join should fail.
  81. u1 = self.register_user("u1", "pass", admin=True)
  82. handler = self.hs.get_room_member_handler()
  83. fed_transport = self.hs.get_federation_transport_client()
  84. # Mock out some things, because we don't want to test the whole join
  85. fed_transport.client.get_json = AsyncMock(return_value={"v1": 9999}) # type: ignore[method-assign]
  86. handler.federation_handler.do_invite_join = AsyncMock( # type: ignore[method-assign]
  87. return_value=("", 1)
  88. )
  89. d = handler._remote_join(
  90. create_requester(u1),
  91. ["other.example.com"],
  92. "roomid",
  93. UserID.from_string(u1),
  94. {"membership": "join"},
  95. )
  96. self.pump()
  97. # The request failed with a SynapseError saying the resource limit was
  98. # exceeded.
  99. f = self.get_failure(d, SynapseError)
  100. self.assertEqual(f.value.code, 400, f.value)
  101. self.assertEqual(f.value.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
  102. def test_join_too_large_once_joined(self) -> None:
  103. u1 = self.register_user("u1", "pass")
  104. u1_token = self.login("u1", "pass")
  105. # Ok, this might seem a bit weird -- I want to test that we actually
  106. # leave the room, but I don't want to simulate two servers. So, we make
  107. # a local room, which we say we're joining remotely, even if there's no
  108. # remote, because we mock that out. Then, we'll leave the (actually
  109. # local) room, which will be propagated over federation in a real
  110. # scenario.
  111. room_1 = self.helper.create_room_as(u1, tok=u1_token)
  112. handler = self.hs.get_room_member_handler()
  113. fed_transport = self.hs.get_federation_transport_client()
  114. # Mock out some things, because we don't want to test the whole join
  115. fed_transport.client.get_json = AsyncMock(return_value=None) # type: ignore[method-assign]
  116. handler.federation_handler.do_invite_join = AsyncMock( # type: ignore[method-assign]
  117. return_value=("", 1)
  118. )
  119. # Artificially raise the complexity
  120. async def get_current_state_event_counts(room_id: str) -> int:
  121. return 600
  122. self.hs.get_datastores().main.get_current_state_event_counts = get_current_state_event_counts # type: ignore[method-assign]
  123. d = handler._remote_join(
  124. create_requester(u1),
  125. ["other.example.com"],
  126. room_1,
  127. UserID.from_string(u1),
  128. {"membership": "join"},
  129. )
  130. self.pump()
  131. # The request failed with a SynapseError saying the resource limit was
  132. # exceeded.
  133. f = self.get_failure(d, SynapseError)
  134. self.assertEqual(f.value.code, 400)
  135. self.assertEqual(f.value.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
  136. class RoomComplexityAdminTests(unittest.FederatingHomeserverTestCase):
  137. # Test the behavior of joining rooms which exceed the complexity if option
  138. # limit_remote_rooms.admins_can_join is True.
  139. servlets = [
  140. admin.register_servlets,
  141. room.register_servlets,
  142. login.register_servlets,
  143. ]
  144. def default_config(self) -> JsonDict:
  145. config = super().default_config()
  146. config["limit_remote_rooms"] = {
  147. "enabled": True,
  148. "complexity": 0.05,
  149. "admins_can_join": True,
  150. }
  151. return config
  152. def test_join_too_large_no_admin(self) -> None:
  153. # A user which is not an admin should not be able to join a remote room
  154. # which is too complex.
  155. u1 = self.register_user("u1", "pass")
  156. handler = self.hs.get_room_member_handler()
  157. fed_transport = self.hs.get_federation_transport_client()
  158. # Mock out some things, because we don't want to test the whole join
  159. fed_transport.client.get_json = AsyncMock(return_value={"v1": 9999}) # type: ignore[method-assign]
  160. handler.federation_handler.do_invite_join = AsyncMock( # type: ignore[method-assign]
  161. return_value=("", 1)
  162. )
  163. d = handler._remote_join(
  164. create_requester(u1),
  165. ["other.example.com"],
  166. "roomid",
  167. UserID.from_string(u1),
  168. {"membership": "join"},
  169. )
  170. self.pump()
  171. # The request failed with a SynapseError saying the resource limit was
  172. # exceeded.
  173. f = self.get_failure(d, SynapseError)
  174. self.assertEqual(f.value.code, 400, f.value)
  175. self.assertEqual(f.value.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
  176. def test_join_too_large_admin(self) -> None:
  177. # An admin should be able to join rooms where a complexity check fails.
  178. u1 = self.register_user("u1", "pass", admin=True)
  179. handler = self.hs.get_room_member_handler()
  180. fed_transport = self.hs.get_federation_transport_client()
  181. # Mock out some things, because we don't want to test the whole join
  182. fed_transport.client.get_json = AsyncMock(return_value={"v1": 9999}) # type: ignore[method-assign]
  183. handler.federation_handler.do_invite_join = AsyncMock( # type: ignore[method-assign]
  184. return_value=("", 1)
  185. )
  186. d = handler._remote_join(
  187. create_requester(u1),
  188. ["other.example.com"],
  189. "roomid",
  190. UserID.from_string(u1),
  191. {"membership": "join"},
  192. )
  193. self.pump()
  194. # The request success since the user is an admin
  195. self.get_success(d)