test_complexity.py 5.7 KB

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