test_complexity.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 Matrix.org Foundation
  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. from mock import Mock
  16. from twisted.internet import defer
  17. from synapse.api.errors import Codes, SynapseError
  18. from synapse.config.ratelimiting import FederationRateLimitConfig
  19. from synapse.federation.transport import server
  20. from synapse.rest import admin
  21. from synapse.rest.client.v1 import login, room
  22. from synapse.types import UserID
  23. from synapse.util.ratelimitutils import FederationRateLimiter
  24. from tests import unittest
  25. class RoomComplexityTests(unittest.HomeserverTestCase):
  26. servlets = [
  27. admin.register_servlets,
  28. room.register_servlets,
  29. login.register_servlets,
  30. ]
  31. def default_config(self, name="test"):
  32. config = super().default_config(name=name)
  33. config["limit_large_remote_room_joins"] = True
  34. config["limit_large_remote_room_complexity"] = 0.05
  35. return config
  36. def prepare(self, reactor, clock, homeserver):
  37. class Authenticator(object):
  38. def authenticate_request(self, request, content):
  39. return defer.succeed("otherserver.nottld")
  40. ratelimiter = FederationRateLimiter(
  41. clock,
  42. FederationRateLimitConfig(
  43. window_size=1,
  44. sleep_limit=1,
  45. sleep_msec=1,
  46. reject_limit=1000,
  47. concurrent_requests=1000,
  48. ),
  49. )
  50. server.register_servlets(
  51. homeserver, self.resource, Authenticator(), ratelimiter
  52. )
  53. def test_complexity_simple(self):
  54. u1 = self.register_user("u1", "pass")
  55. u1_token = self.login("u1", "pass")
  56. room_1 = self.helper.create_room_as(u1, tok=u1_token)
  57. self.helper.send_state(
  58. room_1, event_type="m.room.topic", body={"topic": "foo"}, tok=u1_token
  59. )
  60. # Get the room complexity
  61. request, channel = self.make_request(
  62. "GET", "/_matrix/federation/unstable/rooms/%s/complexity" % (room_1,)
  63. )
  64. self.render(request)
  65. self.assertEquals(200, channel.code)
  66. complexity = channel.json_body["v1"]
  67. self.assertTrue(complexity > 0, complexity)
  68. # Artificially raise the complexity
  69. store = self.hs.get_datastore()
  70. store.get_current_state_event_counts = lambda x: defer.succeed(500 * 1.23)
  71. # Get the room complexity again -- make sure it's our artificial value
  72. request, channel = self.make_request(
  73. "GET", "/_matrix/federation/unstable/rooms/%s/complexity" % (room_1,)
  74. )
  75. self.render(request)
  76. self.assertEquals(200, channel.code)
  77. complexity = channel.json_body["v1"]
  78. self.assertEqual(complexity, 1.23)
  79. def test_join_too_large(self):
  80. u1 = self.register_user("u1", "pass")
  81. handler = self.hs.get_room_member_handler()
  82. fed_transport = self.hs.get_federation_transport_client()
  83. # Mock out some things, because we don't want to test the whole join
  84. fed_transport.client.get_json = Mock(return_value=defer.succeed({"v1": 9999}))
  85. handler.federation_handler.do_invite_join = Mock(return_value=defer.succeed(1))
  86. d = handler._remote_join(
  87. None,
  88. ["otherserver.example"],
  89. "roomid",
  90. UserID.from_string(u1),
  91. {"membership": "join"},
  92. )
  93. self.pump()
  94. # The request failed with a SynapseError saying the resource limit was
  95. # exceeded.
  96. f = self.get_failure(d, SynapseError)
  97. self.assertEqual(f.value.code, 400, f.value)
  98. self.assertEqual(f.value.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
  99. def test_join_too_large_once_joined(self):
  100. u1 = self.register_user("u1", "pass")
  101. u1_token = self.login("u1", "pass")
  102. # Ok, this might seem a bit weird -- I want to test that we actually
  103. # leave the room, but I don't want to simulate two servers. So, we make
  104. # a local room, which we say we're joining remotely, even if there's no
  105. # remote, because we mock that out. Then, we'll leave the (actually
  106. # local) room, which will be propagated over federation in a real
  107. # scenario.
  108. room_1 = self.helper.create_room_as(u1, tok=u1_token)
  109. handler = self.hs.get_room_member_handler()
  110. fed_transport = self.hs.get_federation_transport_client()
  111. # Mock out some things, because we don't want to test the whole join
  112. fed_transport.client.get_json = Mock(return_value=defer.succeed(None))
  113. handler.federation_handler.do_invite_join = Mock(return_value=defer.succeed(1))
  114. # Artificially raise the complexity
  115. self.hs.get_datastore().get_current_state_event_counts = lambda x: defer.succeed(
  116. 600
  117. )
  118. d = handler._remote_join(
  119. None,
  120. ["otherserver.example"],
  121. room_1,
  122. UserID.from_string(u1),
  123. {"membership": "join"},
  124. )
  125. self.pump()
  126. # The request failed with a SynapseError saying the resource limit was
  127. # exceeded.
  128. f = self.get_failure(d, SynapseError)
  129. self.assertEqual(f.value.code, 400)
  130. self.assertEqual(f.value.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)