test_federation.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. from mock import Mock
  2. from twisted.internet.defer import ensureDeferred, maybeDeferred, succeed
  3. from synapse.events import FrozenEvent
  4. from synapse.logging.context import LoggingContext
  5. from synapse.types import Requester, UserID
  6. from synapse.util import Clock
  7. from tests import unittest
  8. from tests.server import ThreadedMemoryReactorClock, setup_test_homeserver
  9. class MessageAcceptTests(unittest.TestCase):
  10. def setUp(self):
  11. self.http_client = Mock()
  12. self.reactor = ThreadedMemoryReactorClock()
  13. self.hs_clock = Clock(self.reactor)
  14. self.homeserver = setup_test_homeserver(
  15. self.addCleanup,
  16. http_client=self.http_client,
  17. clock=self.hs_clock,
  18. reactor=self.reactor,
  19. )
  20. user_id = UserID("us", "test")
  21. our_user = Requester(user_id, None, False, None, None)
  22. room_creator = self.homeserver.get_room_creation_handler()
  23. room = room_creator.create_room(
  24. our_user, room_creator.PRESETS_DICT["public_chat"], ratelimit=False
  25. )
  26. self.reactor.advance(0.1)
  27. self.room_id = self.successResultOf(room)["room_id"]
  28. self.store = self.homeserver.get_datastore()
  29. # Figure out what the most recent event is
  30. most_recent = self.successResultOf(
  31. maybeDeferred(
  32. self.homeserver.get_datastore().get_latest_event_ids_in_room,
  33. self.room_id,
  34. )
  35. )[0]
  36. join_event = FrozenEvent(
  37. {
  38. "room_id": self.room_id,
  39. "sender": "@baduser:test.serv",
  40. "state_key": "@baduser:test.serv",
  41. "event_id": "$join:test.serv",
  42. "depth": 1000,
  43. "origin_server_ts": 1,
  44. "type": "m.room.member",
  45. "origin": "test.servx",
  46. "content": {"membership": "join"},
  47. "auth_events": [],
  48. "prev_state": [(most_recent, {})],
  49. "prev_events": [(most_recent, {})],
  50. }
  51. )
  52. self.handler = self.homeserver.get_handlers().federation_handler
  53. self.handler.do_auth = lambda origin, event, context, auth_events: succeed(
  54. context
  55. )
  56. self.client = self.homeserver.get_federation_client()
  57. self.client._check_sigs_and_hash_and_fetch = lambda dest, pdus, **k: succeed(
  58. pdus
  59. )
  60. # Send the join, it should return None (which is not an error)
  61. d = ensureDeferred(
  62. self.handler.on_receive_pdu(
  63. "test.serv", join_event, sent_to_us_directly=True
  64. )
  65. )
  66. self.reactor.advance(1)
  67. self.assertEqual(self.successResultOf(d), None)
  68. # Make sure we actually joined the room
  69. self.assertEqual(
  70. self.successResultOf(
  71. maybeDeferred(self.store.get_latest_event_ids_in_room, self.room_id)
  72. )[0],
  73. "$join:test.serv",
  74. )
  75. def test_cant_hide_direct_ancestors(self):
  76. """
  77. If you send a message, you must be able to provide the direct
  78. prev_events that said event references.
  79. """
  80. def post_json(destination, path, data, headers=None, timeout=0):
  81. # If it asks us for new missing events, give them NOTHING
  82. if path.startswith("/_matrix/federation/v1/get_missing_events/"):
  83. return {"events": []}
  84. self.http_client.post_json = post_json
  85. # Figure out what the most recent event is
  86. most_recent = self.successResultOf(
  87. maybeDeferred(self.store.get_latest_event_ids_in_room, self.room_id)
  88. )[0]
  89. # Now lie about an event
  90. lying_event = FrozenEvent(
  91. {
  92. "room_id": self.room_id,
  93. "sender": "@baduser:test.serv",
  94. "event_id": "one:test.serv",
  95. "depth": 1000,
  96. "origin_server_ts": 1,
  97. "type": "m.room.message",
  98. "origin": "test.serv",
  99. "content": {"body": "hewwo?"},
  100. "auth_events": [],
  101. "prev_events": [("two:test.serv", {}), (most_recent, {})],
  102. }
  103. )
  104. with LoggingContext(request="lying_event"):
  105. d = ensureDeferred(
  106. self.handler.on_receive_pdu(
  107. "test.serv", lying_event, sent_to_us_directly=True
  108. )
  109. )
  110. # Step the reactor, so the database fetches come back
  111. self.reactor.advance(1)
  112. # on_receive_pdu should throw an error
  113. failure = self.failureResultOf(d)
  114. self.assertEqual(
  115. failure.value.args[0],
  116. (
  117. "ERROR 403: Your server isn't divulging details about prev_events "
  118. "referenced in this event."
  119. ),
  120. )
  121. # Make sure the invalid event isn't there
  122. extrem = maybeDeferred(self.store.get_latest_event_ids_in_room, self.room_id)
  123. self.assertEqual(self.successResultOf(extrem)[0], "$join:test.serv")