1
0

test_federation_server.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 New Vector Ltd
  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. import logging
  16. from synapse.events import FrozenEvent
  17. from synapse.federation.federation_server import server_matches_acl_event
  18. from tests import unittest
  19. class ServerACLsTestCase(unittest.TestCase):
  20. def test_blacklisted_server(self):
  21. e = _create_acl_event({"allow": ["*"], "deny": ["evil.com"]})
  22. logging.info("ACL event: %s", e.content)
  23. self.assertFalse(server_matches_acl_event("evil.com", e))
  24. self.assertFalse(server_matches_acl_event("EVIL.COM", e))
  25. self.assertTrue(server_matches_acl_event("evil.com.au", e))
  26. self.assertTrue(server_matches_acl_event("honestly.not.evil.com", e))
  27. def test_block_ip_literals(self):
  28. e = _create_acl_event({"allow_ip_literals": False, "allow": ["*"]})
  29. logging.info("ACL event: %s", e.content)
  30. self.assertFalse(server_matches_acl_event("1.2.3.4", e))
  31. self.assertTrue(server_matches_acl_event("1a.2.3.4", e))
  32. self.assertFalse(server_matches_acl_event("[1:2::]", e))
  33. self.assertTrue(server_matches_acl_event("1:2:3:4", e))
  34. def _create_acl_event(content):
  35. return FrozenEvent(
  36. {
  37. "room_id": "!a:b",
  38. "event_id": "$a:b",
  39. "type": "m.room.server_acls",
  40. "sender": "@a:b",
  41. "content": content,
  42. }
  43. )