2018-07-09 02:09:20 -04:00
|
|
|
from mock import Mock
|
|
|
|
|
2019-12-10 12:01:37 -05:00
|
|
|
from twisted.internet.defer import ensureDeferred, maybeDeferred, succeed
|
2018-06-27 06:27:32 -04:00
|
|
|
|
2020-02-07 10:30:04 -05:00
|
|
|
from synapse.events import make_event_from_dict
|
2019-07-03 10:07:04 -04:00
|
|
|
from synapse.logging.context import LoggingContext
|
2018-06-27 06:27:32 -04:00
|
|
|
from synapse.types import Requester, UserID
|
2018-07-09 02:09:20 -04:00
|
|
|
from synapse.util import Clock
|
2020-05-21 11:41:12 -04:00
|
|
|
from synapse.util.retryutils import NotRetryingDestination
|
2018-06-27 06:27:32 -04:00
|
|
|
|
|
|
|
from tests import unittest
|
2018-07-09 02:09:20 -04:00
|
|
|
from tests.server import ThreadedMemoryReactorClock, setup_test_homeserver
|
2018-06-27 06:27:32 -04:00
|
|
|
|
|
|
|
|
2020-05-21 11:41:12 -04:00
|
|
|
class MessageAcceptTests(unittest.HomeserverTestCase):
|
2018-06-27 06:27:32 -04:00
|
|
|
def setUp(self):
|
|
|
|
|
|
|
|
self.http_client = Mock()
|
|
|
|
self.reactor = ThreadedMemoryReactorClock()
|
|
|
|
self.hs_clock = Clock(self.reactor)
|
|
|
|
self.homeserver = setup_test_homeserver(
|
2018-08-13 02:47:46 -04:00
|
|
|
self.addCleanup,
|
|
|
|
http_client=self.http_client,
|
|
|
|
clock=self.hs_clock,
|
|
|
|
reactor=self.reactor,
|
2018-06-27 06:27:32 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
user_id = UserID("us", "test")
|
|
|
|
our_user = Requester(user_id, None, False, None, None)
|
|
|
|
room_creator = self.homeserver.get_room_creation_handler()
|
2020-05-22 09:21:54 -04:00
|
|
|
room_deferred = ensureDeferred(
|
2020-05-01 10:15:36 -04:00
|
|
|
room_creator.create_room(
|
|
|
|
our_user, room_creator.PRESETS_DICT["public_chat"], ratelimit=False
|
|
|
|
)
|
2018-06-27 06:27:32 -04:00
|
|
|
)
|
|
|
|
self.reactor.advance(0.1)
|
2020-05-22 09:21:54 -04:00
|
|
|
self.room_id = self.successResultOf(room_deferred)[0]["room_id"]
|
2018-06-27 06:27:32 -04:00
|
|
|
|
2019-12-06 11:02:50 -05:00
|
|
|
self.store = self.homeserver.get_datastore()
|
|
|
|
|
2018-06-27 06:27:32 -04:00
|
|
|
# Figure out what the most recent event is
|
|
|
|
most_recent = self.successResultOf(
|
2018-06-27 06:32:09 -04:00
|
|
|
maybeDeferred(
|
2019-10-30 09:33:38 -04:00
|
|
|
self.homeserver.get_datastore().get_latest_event_ids_in_room,
|
|
|
|
self.room_id,
|
2018-06-27 06:32:09 -04:00
|
|
|
)
|
2018-06-27 06:27:32 -04:00
|
|
|
)[0]
|
|
|
|
|
2020-02-07 10:30:04 -05:00
|
|
|
join_event = make_event_from_dict(
|
2018-06-27 06:27:32 -04:00
|
|
|
{
|
|
|
|
"room_id": self.room_id,
|
|
|
|
"sender": "@baduser:test.serv",
|
|
|
|
"state_key": "@baduser:test.serv",
|
|
|
|
"event_id": "$join:test.serv",
|
|
|
|
"depth": 1000,
|
|
|
|
"origin_server_ts": 1,
|
|
|
|
"type": "m.room.member",
|
|
|
|
"origin": "test.servx",
|
|
|
|
"content": {"membership": "join"},
|
|
|
|
"auth_events": [],
|
|
|
|
"prev_state": [(most_recent, {})],
|
|
|
|
"prev_events": [(most_recent, {})],
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
self.handler = self.homeserver.get_handlers().federation_handler
|
2019-11-01 12:19:09 -04:00
|
|
|
self.handler.do_auth = lambda origin, event, context, auth_events: succeed(
|
|
|
|
context
|
|
|
|
)
|
2018-06-27 06:27:32 -04:00
|
|
|
self.client = self.homeserver.get_federation_client()
|
|
|
|
self.client._check_sigs_and_hash_and_fetch = lambda dest, pdus, **k: succeed(
|
|
|
|
pdus
|
|
|
|
)
|
|
|
|
|
|
|
|
# Send the join, it should return None (which is not an error)
|
2019-12-10 12:01:37 -05:00
|
|
|
d = ensureDeferred(
|
|
|
|
self.handler.on_receive_pdu(
|
|
|
|
"test.serv", join_event, sent_to_us_directly=True
|
|
|
|
)
|
2018-06-27 06:27:32 -04:00
|
|
|
)
|
|
|
|
self.reactor.advance(1)
|
|
|
|
self.assertEqual(self.successResultOf(d), None)
|
|
|
|
|
|
|
|
# Make sure we actually joined the room
|
|
|
|
self.assertEqual(
|
|
|
|
self.successResultOf(
|
2019-12-06 11:02:50 -05:00
|
|
|
maybeDeferred(self.store.get_latest_event_ids_in_room, self.room_id)
|
2018-06-27 06:27:32 -04:00
|
|
|
)[0],
|
|
|
|
"$join:test.serv",
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_cant_hide_direct_ancestors(self):
|
|
|
|
"""
|
|
|
|
If you send a message, you must be able to provide the direct
|
|
|
|
prev_events that said event references.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def post_json(destination, path, data, headers=None, timeout=0):
|
|
|
|
# If it asks us for new missing events, give them NOTHING
|
|
|
|
if path.startswith("/_matrix/federation/v1/get_missing_events/"):
|
|
|
|
return {"events": []}
|
|
|
|
|
|
|
|
self.http_client.post_json = post_json
|
|
|
|
|
|
|
|
# Figure out what the most recent event is
|
|
|
|
most_recent = self.successResultOf(
|
2019-12-06 11:02:50 -05:00
|
|
|
maybeDeferred(self.store.get_latest_event_ids_in_room, self.room_id)
|
2018-06-27 06:27:32 -04:00
|
|
|
)[0]
|
|
|
|
|
|
|
|
# Now lie about an event
|
2020-02-07 10:30:04 -05:00
|
|
|
lying_event = make_event_from_dict(
|
2018-06-27 06:27:32 -04:00
|
|
|
{
|
|
|
|
"room_id": self.room_id,
|
|
|
|
"sender": "@baduser:test.serv",
|
|
|
|
"event_id": "one:test.serv",
|
|
|
|
"depth": 1000,
|
|
|
|
"origin_server_ts": 1,
|
|
|
|
"type": "m.room.message",
|
|
|
|
"origin": "test.serv",
|
2018-11-02 09:44:12 -04:00
|
|
|
"content": {"body": "hewwo?"},
|
2018-06-27 06:27:32 -04:00
|
|
|
"auth_events": [],
|
|
|
|
"prev_events": [("two:test.serv", {}), (most_recent, {})],
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2018-09-27 06:25:34 -04:00
|
|
|
with LoggingContext(request="lying_event"):
|
2019-12-10 12:01:37 -05:00
|
|
|
d = ensureDeferred(
|
|
|
|
self.handler.on_receive_pdu(
|
|
|
|
"test.serv", lying_event, sent_to_us_directly=True
|
|
|
|
)
|
2018-09-27 06:25:34 -04:00
|
|
|
)
|
2018-06-27 06:27:32 -04:00
|
|
|
|
2018-11-15 17:46:51 -05:00
|
|
|
# Step the reactor, so the database fetches come back
|
|
|
|
self.reactor.advance(1)
|
2018-06-27 06:27:32 -04:00
|
|
|
|
|
|
|
# on_receive_pdu should throw an error
|
|
|
|
failure = self.failureResultOf(d)
|
|
|
|
self.assertEqual(
|
|
|
|
failure.value.args[0],
|
|
|
|
(
|
|
|
|
"ERROR 403: Your server isn't divulging details about prev_events "
|
|
|
|
"referenced in this event."
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
# Make sure the invalid event isn't there
|
2019-12-06 11:02:50 -05:00
|
|
|
extrem = maybeDeferred(self.store.get_latest_event_ids_in_room, self.room_id)
|
2018-06-27 06:27:32 -04:00
|
|
|
self.assertEqual(self.successResultOf(extrem)[0], "$join:test.serv")
|
2020-05-21 11:41:12 -04:00
|
|
|
|
|
|
|
def test_retry_device_list_resync(self):
|
|
|
|
"""Tests that device lists are marked as stale if they couldn't be synced, and
|
|
|
|
that stale device lists are retried periodically.
|
|
|
|
"""
|
|
|
|
remote_user_id = "@john:test_remote"
|
|
|
|
remote_origin = "test_remote"
|
|
|
|
|
|
|
|
# Track the number of attempts to resync the user's device list.
|
|
|
|
self.resync_attempts = 0
|
|
|
|
|
|
|
|
# When this function is called, increment the number of resync attempts (only if
|
|
|
|
# we're querying devices for the right user ID), then raise a
|
|
|
|
# NotRetryingDestination error to fail the resync gracefully.
|
|
|
|
def query_user_devices(destination, user_id):
|
|
|
|
if user_id == remote_user_id:
|
|
|
|
self.resync_attempts += 1
|
|
|
|
|
|
|
|
raise NotRetryingDestination(0, 0, destination)
|
|
|
|
|
|
|
|
# Register the mock on the federation client.
|
|
|
|
federation_client = self.homeserver.get_federation_client()
|
|
|
|
federation_client.query_user_devices = Mock(side_effect=query_user_devices)
|
|
|
|
|
|
|
|
# Register a mock on the store so that the incoming update doesn't fail because
|
|
|
|
# we don't share a room with the user.
|
|
|
|
store = self.homeserver.get_datastore()
|
|
|
|
store.get_rooms_for_user = Mock(return_value=["!someroom:test"])
|
|
|
|
|
|
|
|
# Manually inject a fake device list update. We need this update to include at
|
|
|
|
# least one prev_id so that the user's device list will need to be retried.
|
|
|
|
device_list_updater = self.homeserver.get_device_handler().device_list_updater
|
|
|
|
self.get_success(
|
|
|
|
device_list_updater.incoming_device_list_update(
|
|
|
|
origin=remote_origin,
|
|
|
|
edu_content={
|
|
|
|
"deleted": False,
|
|
|
|
"device_display_name": "Mobile",
|
|
|
|
"device_id": "QBUAZIFURK",
|
|
|
|
"prev_id": [5],
|
|
|
|
"stream_id": 6,
|
|
|
|
"user_id": remote_user_id,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Check that there was one resync attempt.
|
|
|
|
self.assertEqual(self.resync_attempts, 1)
|
|
|
|
|
|
|
|
# Check that the resync attempt failed and caused the user's device list to be
|
|
|
|
# marked as stale.
|
|
|
|
need_resync = self.get_success(
|
|
|
|
store.get_user_ids_requiring_device_list_resync()
|
|
|
|
)
|
|
|
|
self.assertIn(remote_user_id, need_resync)
|
|
|
|
|
|
|
|
# Check that waiting for 30 seconds caused Synapse to retry resyncing the device
|
|
|
|
# list.
|
|
|
|
self.reactor.advance(30)
|
|
|
|
self.assertEqual(self.resync_attempts, 2)
|