2014-09-03 10:29:13 -06:00
|
|
|
# Copyright 2014 OpenMarket Ltd
|
2014-08-12 20:32:18 -06:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2014-08-12 08:10:52 -06:00
|
|
|
# trial imports
|
|
|
|
from twisted.internet import defer
|
2014-09-12 11:24:53 -06:00
|
|
|
from tests import unittest
|
2014-08-12 08:10:52 -06:00
|
|
|
|
|
|
|
# python imports
|
2014-09-15 08:05:12 -06:00
|
|
|
from mock import Mock, ANY
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2014-09-24 10:25:41 -06:00
|
|
|
from ..utils import MockHttpResource, MockClock, MockKey
|
2014-08-12 08:10:52 -06:00
|
|
|
|
|
|
|
from synapse.server import HomeServer
|
|
|
|
from synapse.federation import initialize_http_replication
|
|
|
|
from synapse.federation.units import Pdu
|
|
|
|
from synapse.storage.pdu import PduTuple, PduEntry
|
|
|
|
|
|
|
|
|
|
|
|
def make_pdu(prev_pdus=[], **kwargs):
|
|
|
|
"""Provide some default fields for making a PduTuple."""
|
|
|
|
pdu_fields = {
|
|
|
|
"is_state": False,
|
|
|
|
"unrecognized_keys": [],
|
|
|
|
"outlier": False,
|
|
|
|
"have_processed": True,
|
|
|
|
"state_key": None,
|
|
|
|
"power_level": None,
|
|
|
|
"prev_state_id": None,
|
|
|
|
"prev_state_origin": None,
|
|
|
|
}
|
|
|
|
pdu_fields.update(kwargs)
|
|
|
|
|
|
|
|
return PduTuple(PduEntry(**pdu_fields), prev_pdus)
|
|
|
|
|
|
|
|
|
|
|
|
class FederationTestCase(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2014-08-18 07:03:07 -06:00
|
|
|
self.mock_resource = MockHttpResource()
|
2014-08-12 08:10:52 -06:00
|
|
|
self.mock_http_client = Mock(spec=[
|
2014-08-13 09:55:53 -06:00
|
|
|
"get_json",
|
2014-08-12 08:10:52 -06:00
|
|
|
"put_json",
|
|
|
|
])
|
|
|
|
self.mock_persistence = Mock(spec=[
|
|
|
|
"get_current_state_for_context",
|
|
|
|
"get_pdu",
|
2014-08-26 11:01:36 -06:00
|
|
|
"persist_event",
|
2014-08-12 08:10:52 -06:00
|
|
|
"update_min_depth_for_context",
|
|
|
|
"prep_send_transaction",
|
|
|
|
"delivered_txn",
|
|
|
|
"get_received_txn_response",
|
|
|
|
"set_received_txn_response",
|
|
|
|
])
|
|
|
|
self.mock_persistence.get_received_txn_response.return_value = (
|
|
|
|
defer.succeed(None)
|
|
|
|
)
|
2014-09-24 10:25:41 -06:00
|
|
|
self.mock_config = Mock()
|
|
|
|
self.mock_config.signing_key = [MockKey()]
|
2014-08-12 08:10:52 -06:00
|
|
|
self.clock = MockClock()
|
|
|
|
hs = HomeServer("test",
|
2014-08-18 07:03:07 -06:00
|
|
|
resource_for_federation=self.mock_resource,
|
2014-08-12 08:10:52 -06:00
|
|
|
http_client=self.mock_http_client,
|
|
|
|
db_pool=None,
|
|
|
|
datastore=self.mock_persistence,
|
|
|
|
clock=self.clock,
|
2014-09-24 10:25:41 -06:00
|
|
|
config=self.mock_config,
|
2014-09-30 08:15:10 -06:00
|
|
|
keyring=Mock(),
|
2014-08-12 08:10:52 -06:00
|
|
|
)
|
|
|
|
self.federation = initialize_http_replication(hs)
|
|
|
|
self.distributor = hs.get_distributor()
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_get_state(self):
|
|
|
|
self.mock_persistence.get_current_state_for_context.return_value = (
|
|
|
|
defer.succeed([])
|
|
|
|
)
|
|
|
|
|
|
|
|
# Empty context initially
|
2014-08-18 07:03:07 -06:00
|
|
|
(code, response) = yield self.mock_resource.trigger("GET",
|
2014-08-31 07:51:37 -06:00
|
|
|
"/_matrix/federation/v1/state/my-context/", None)
|
2014-08-12 08:10:52 -06:00
|
|
|
self.assertEquals(200, code)
|
|
|
|
self.assertFalse(response["pdus"])
|
|
|
|
|
|
|
|
# Now lets give the context some state
|
|
|
|
self.mock_persistence.get_current_state_for_context.return_value = (
|
|
|
|
defer.succeed([
|
|
|
|
make_pdu(
|
|
|
|
pdu_id="the-pdu-id",
|
|
|
|
origin="red",
|
|
|
|
context="my-context",
|
|
|
|
pdu_type="m.topic",
|
2014-10-17 10:31:48 -06:00
|
|
|
ts=123456789000,
|
2014-08-12 08:10:52 -06:00
|
|
|
depth=1,
|
|
|
|
is_state=True,
|
|
|
|
content_json='{"topic":"The topic"}',
|
|
|
|
state_key="",
|
|
|
|
power_level=1000,
|
|
|
|
prev_state_id="last-pdu-id",
|
|
|
|
prev_state_origin="blue",
|
|
|
|
),
|
|
|
|
])
|
|
|
|
)
|
|
|
|
|
2014-08-18 07:03:07 -06:00
|
|
|
(code, response) = yield self.mock_resource.trigger("GET",
|
2014-08-31 07:51:37 -06:00
|
|
|
"/_matrix/federation/v1/state/my-context/", None)
|
2014-08-12 08:10:52 -06:00
|
|
|
self.assertEquals(200, code)
|
|
|
|
self.assertEquals(1, len(response["pdus"]))
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_get_pdu(self):
|
|
|
|
self.mock_persistence.get_pdu.return_value = (
|
|
|
|
defer.succeed(None)
|
|
|
|
)
|
|
|
|
|
2014-08-18 07:03:07 -06:00
|
|
|
(code, response) = yield self.mock_resource.trigger("GET",
|
2014-08-31 07:51:37 -06:00
|
|
|
"/_matrix/federation/v1/pdu/red/abc123def456/", None)
|
2014-08-12 08:10:52 -06:00
|
|
|
self.assertEquals(404, code)
|
|
|
|
|
|
|
|
# Now insert such a PDU
|
|
|
|
self.mock_persistence.get_pdu.return_value = (
|
|
|
|
defer.succeed(
|
|
|
|
make_pdu(
|
|
|
|
pdu_id="abc123def456",
|
|
|
|
origin="red",
|
|
|
|
context="my-context",
|
|
|
|
pdu_type="m.text",
|
2014-10-17 10:31:48 -06:00
|
|
|
ts=123456789001,
|
2014-08-12 08:10:52 -06:00
|
|
|
depth=1,
|
|
|
|
content_json='{"text":"Here is the message"}',
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2014-08-18 07:03:07 -06:00
|
|
|
(code, response) = yield self.mock_resource.trigger("GET",
|
2014-08-31 07:51:37 -06:00
|
|
|
"/_matrix/federation/v1/pdu/red/abc123def456/", None)
|
2014-08-12 08:10:52 -06:00
|
|
|
self.assertEquals(200, code)
|
|
|
|
self.assertEquals(1, len(response["pdus"]))
|
|
|
|
self.assertEquals("m.text", response["pdus"][0]["pdu_type"])
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_send_pdu(self):
|
|
|
|
self.mock_http_client.put_json.return_value = defer.succeed(
|
|
|
|
(200, "OK")
|
|
|
|
)
|
|
|
|
|
|
|
|
pdu = Pdu(
|
|
|
|
pdu_id="abc123def456",
|
|
|
|
origin="red",
|
|
|
|
destinations=["remote"],
|
|
|
|
context="my-context",
|
2014-10-17 10:12:25 -06:00
|
|
|
origin_server_ts=123456789002,
|
2014-08-12 08:10:52 -06:00
|
|
|
pdu_type="m.test",
|
|
|
|
content={"testing": "content here"},
|
|
|
|
depth=1,
|
|
|
|
)
|
|
|
|
|
|
|
|
yield self.federation.send_pdu(pdu)
|
|
|
|
|
|
|
|
self.mock_http_client.put_json.assert_called_with(
|
|
|
|
"remote",
|
2014-08-31 07:51:37 -06:00
|
|
|
path="/_matrix/federation/v1/send/1000000/",
|
2014-08-12 08:10:52 -06:00
|
|
|
data={
|
2014-10-17 10:12:25 -06:00
|
|
|
"origin_server_ts": 1000000,
|
2014-08-12 08:10:52 -06:00
|
|
|
"origin": "test",
|
|
|
|
"pdus": [
|
|
|
|
{
|
|
|
|
"origin": "red",
|
|
|
|
"pdu_id": "abc123def456",
|
|
|
|
"prev_pdus": [],
|
2014-10-17 10:12:25 -06:00
|
|
|
"origin_server_ts": 123456789002,
|
2014-08-12 08:10:52 -06:00
|
|
|
"context": "my-context",
|
|
|
|
"pdu_type": "m.test",
|
|
|
|
"is_state": False,
|
|
|
|
"content": {"testing": "content here"},
|
|
|
|
"depth": 1,
|
|
|
|
},
|
|
|
|
]
|
2014-09-15 08:05:12 -06:00
|
|
|
},
|
2014-10-13 04:49:40 -06:00
|
|
|
json_data_callback=ANY,
|
2014-08-12 08:10:52 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_send_edu(self):
|
|
|
|
self.mock_http_client.put_json.return_value = defer.succeed(
|
|
|
|
(200, "OK")
|
|
|
|
)
|
|
|
|
|
|
|
|
yield self.federation.send_edu(
|
|
|
|
destination="remote",
|
|
|
|
edu_type="m.test",
|
|
|
|
content={"testing": "content here"},
|
|
|
|
)
|
|
|
|
|
|
|
|
# MockClock ensures we can guess these timestamps
|
|
|
|
self.mock_http_client.put_json.assert_called_with(
|
|
|
|
"remote",
|
2014-08-31 07:51:37 -06:00
|
|
|
path="/_matrix/federation/v1/send/1000000/",
|
2014-08-12 08:10:52 -06:00
|
|
|
data={
|
|
|
|
"origin": "test",
|
2014-10-17 10:12:25 -06:00
|
|
|
"origin_server_ts": 1000000,
|
2014-08-12 08:10:52 -06:00
|
|
|
"pdus": [],
|
|
|
|
"edus": [
|
|
|
|
{
|
2014-10-14 09:44:27 -06:00
|
|
|
# TODO: SYN-103: Remove "origin" and "destination"
|
2014-08-12 08:10:52 -06:00
|
|
|
"origin": "test",
|
|
|
|
"destination": "remote",
|
|
|
|
"edu_type": "m.test",
|
|
|
|
"content": {"testing": "content here"},
|
|
|
|
}
|
|
|
|
],
|
2014-09-15 08:05:12 -06:00
|
|
|
},
|
2014-10-13 04:49:40 -06:00
|
|
|
json_data_callback=ANY,
|
2014-09-15 08:05:12 -06:00
|
|
|
)
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2014-10-13 07:37:46 -06:00
|
|
|
|
2014-08-12 08:10:52 -06:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_recv_edu(self):
|
|
|
|
recv_observer = Mock()
|
|
|
|
recv_observer.return_value = defer.succeed(())
|
|
|
|
|
|
|
|
self.federation.register_edu_handler("m.test", recv_observer)
|
|
|
|
|
2014-08-18 07:03:07 -06:00
|
|
|
yield self.mock_resource.trigger("PUT",
|
2014-08-31 07:51:37 -06:00
|
|
|
"/_matrix/federation/v1/send/1001000/",
|
2014-08-12 08:10:52 -06:00
|
|
|
"""{
|
|
|
|
"origin": "remote",
|
2014-10-17 10:12:25 -06:00
|
|
|
"origin_server_ts": 1001000,
|
2014-08-12 08:10:52 -06:00
|
|
|
"pdus": [],
|
|
|
|
"edus": [
|
|
|
|
{
|
|
|
|
"origin": "remote",
|
|
|
|
"destination": "test",
|
|
|
|
"edu_type": "m.test",
|
|
|
|
"content": {"testing": "reply here"}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}""")
|
|
|
|
|
|
|
|
recv_observer.assert_called_with(
|
|
|
|
"remote", {"testing": "reply here"}
|
|
|
|
)
|
2014-08-13 09:55:53 -06:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_send_query(self):
|
|
|
|
self.mock_http_client.get_json.return_value = defer.succeed(
|
|
|
|
{"your": "response"}
|
|
|
|
)
|
|
|
|
|
|
|
|
response = yield self.federation.make_query(
|
|
|
|
destination="remote",
|
|
|
|
query_type="a-question",
|
2014-10-02 07:09:15 -06:00
|
|
|
args={"one": "1", "two": "2"},
|
2014-08-13 09:55:53 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEquals({"your": "response"}, response)
|
|
|
|
|
|
|
|
self.mock_http_client.get_json.assert_called_with(
|
|
|
|
destination="remote",
|
2014-08-31 07:51:37 -06:00
|
|
|
path="/_matrix/federation/v1/query/a-question",
|
2014-10-02 07:09:15 -06:00
|
|
|
args={"one": "1", "two": "2"},
|
|
|
|
retry_on_dns_fail=True,
|
2014-08-13 09:55:53 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_recv_query(self):
|
|
|
|
recv_handler = Mock()
|
|
|
|
recv_handler.return_value = defer.succeed({"another": "response"})
|
|
|
|
|
|
|
|
self.federation.register_query_handler("a-question", recv_handler)
|
|
|
|
|
2014-08-18 07:03:07 -06:00
|
|
|
code, response = yield self.mock_resource.trigger("GET",
|
2014-08-31 07:51:37 -06:00
|
|
|
"/_matrix/federation/v1/query/a-question?three=3&four=4", None)
|
2014-08-13 09:55:53 -06:00
|
|
|
|
|
|
|
self.assertEquals(200, code)
|
|
|
|
self.assertEquals({"another": "response"}, response)
|
|
|
|
|
|
|
|
recv_handler.assert_called_with(
|
|
|
|
{"three": "3", "four": "4"}
|
|
|
|
)
|