test_federation_server.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. @unittest.DEBUG
  20. class ServerACLsTestCase(unittest.TestCase):
  21. def test_blacklisted_server(self):
  22. e = _create_acl_event({"allow": ["*"], "deny": ["evil.com"]})
  23. logging.info("ACL event: %s", e.content)
  24. self.assertFalse(server_matches_acl_event("evil.com", e))
  25. self.assertFalse(server_matches_acl_event("EVIL.COM", e))
  26. self.assertTrue(server_matches_acl_event("evil.com.au", e))
  27. self.assertTrue(server_matches_acl_event("honestly.not.evil.com", e))
  28. def test_block_ip_literals(self):
  29. e = _create_acl_event({"allow_ip_literals": False, "allow": ["*"]})
  30. logging.info("ACL event: %s", e.content)
  31. self.assertFalse(server_matches_acl_event("1.2.3.4", e))
  32. self.assertTrue(server_matches_acl_event("1a.2.3.4", e))
  33. self.assertFalse(server_matches_acl_event("[1:2::]", e))
  34. self.assertTrue(server_matches_acl_event("1:2:3:4", e))
  35. def _create_acl_event(content):
  36. return FrozenEvent(
  37. {
  38. "room_id": "!a:b",
  39. "event_id": "$a:b",
  40. "type": "m.room.server_acls",
  41. "sender": "@a:b",
  42. "content": content,
  43. }
  44. )