test_srv_resolver.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. # Copyright 2014-2016 OpenMarket Ltd
  2. # Copyright 2019 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. from unittest.mock import Mock
  16. from twisted.internet import defer
  17. from twisted.internet.defer import Deferred
  18. from twisted.internet.error import ConnectError
  19. from twisted.names import dns, error
  20. from synapse.http.federation.srv_resolver import SrvResolver
  21. from synapse.logging.context import LoggingContext, current_context
  22. from tests import unittest
  23. from tests.utils import MockClock
  24. class SrvResolverTestCase(unittest.TestCase):
  25. def test_resolve(self):
  26. dns_client_mock = Mock()
  27. service_name = b"test_service.example.com"
  28. host_name = b"example.com"
  29. answer_srv = dns.RRHeader(
  30. type=dns.SRV, payload=dns.Record_SRV(target=host_name)
  31. )
  32. result_deferred = Deferred()
  33. dns_client_mock.lookupService.return_value = result_deferred
  34. cache = {}
  35. resolver = SrvResolver(dns_client=dns_client_mock, cache=cache)
  36. @defer.inlineCallbacks
  37. def do_lookup():
  38. with LoggingContext("one") as ctx:
  39. resolve_d = resolver.resolve_service(service_name)
  40. result = yield defer.ensureDeferred(resolve_d)
  41. # should have restored our context
  42. self.assertIs(current_context(), ctx)
  43. return result
  44. test_d = do_lookup()
  45. self.assertNoResult(test_d)
  46. dns_client_mock.lookupService.assert_called_once_with(service_name)
  47. result_deferred.callback(([answer_srv], None, None))
  48. servers = self.successResultOf(test_d)
  49. self.assertEqual(len(servers), 1)
  50. self.assertEqual(servers, cache[service_name])
  51. self.assertEqual(servers[0].host, host_name)
  52. @defer.inlineCallbacks
  53. def test_from_cache_expired_and_dns_fail(self):
  54. dns_client_mock = Mock()
  55. dns_client_mock.lookupService.return_value = defer.fail(error.DNSServerError())
  56. service_name = b"test_service.example.com"
  57. entry = Mock(spec_set=["expires", "priority", "weight"])
  58. entry.expires = 0
  59. entry.priority = 0
  60. entry.weight = 0
  61. cache = {service_name: [entry]}
  62. resolver = SrvResolver(dns_client=dns_client_mock, cache=cache)
  63. servers = yield defer.ensureDeferred(resolver.resolve_service(service_name))
  64. dns_client_mock.lookupService.assert_called_once_with(service_name)
  65. self.assertEqual(len(servers), 1)
  66. self.assertEqual(servers, cache[service_name])
  67. @defer.inlineCallbacks
  68. def test_from_cache(self):
  69. clock = MockClock()
  70. dns_client_mock = Mock(spec_set=["lookupService"])
  71. dns_client_mock.lookupService = Mock(spec_set=[])
  72. service_name = b"test_service.example.com"
  73. entry = Mock(spec_set=["expires", "priority", "weight"])
  74. entry.expires = 999999999
  75. entry.priority = 0
  76. entry.weight = 0
  77. cache = {service_name: [entry]}
  78. resolver = SrvResolver(
  79. dns_client=dns_client_mock, cache=cache, get_time=clock.time
  80. )
  81. servers = yield defer.ensureDeferred(resolver.resolve_service(service_name))
  82. self.assertFalse(dns_client_mock.lookupService.called)
  83. self.assertEqual(len(servers), 1)
  84. self.assertEqual(servers, cache[service_name])
  85. @defer.inlineCallbacks
  86. def test_empty_cache(self):
  87. dns_client_mock = Mock()
  88. dns_client_mock.lookupService.return_value = defer.fail(error.DNSServerError())
  89. service_name = b"test_service.example.com"
  90. cache = {}
  91. resolver = SrvResolver(dns_client=dns_client_mock, cache=cache)
  92. with self.assertRaises(error.DNSServerError):
  93. yield defer.ensureDeferred(resolver.resolve_service(service_name))
  94. @defer.inlineCallbacks
  95. def test_name_error(self):
  96. dns_client_mock = Mock()
  97. dns_client_mock.lookupService.return_value = defer.fail(error.DNSNameError())
  98. service_name = b"test_service.example.com"
  99. cache = {}
  100. resolver = SrvResolver(dns_client=dns_client_mock, cache=cache)
  101. servers = yield defer.ensureDeferred(resolver.resolve_service(service_name))
  102. self.assertEqual(len(servers), 0)
  103. self.assertEqual(len(cache), 0)
  104. def test_disabled_service(self):
  105. """
  106. test the behaviour when there is a single record which is ".".
  107. """
  108. service_name = b"test_service.example.com"
  109. lookup_deferred = Deferred()
  110. dns_client_mock = Mock()
  111. dns_client_mock.lookupService.return_value = lookup_deferred
  112. cache = {}
  113. resolver = SrvResolver(dns_client=dns_client_mock, cache=cache)
  114. # Old versions of Twisted don't have an ensureDeferred in failureResultOf.
  115. resolve_d = defer.ensureDeferred(resolver.resolve_service(service_name))
  116. # returning a single "." should make the lookup fail with a ConenctError
  117. lookup_deferred.callback(
  118. (
  119. [dns.RRHeader(type=dns.SRV, payload=dns.Record_SRV(target=b"."))],
  120. None,
  121. None,
  122. )
  123. )
  124. self.failureResultOf(resolve_d, ConnectError)
  125. def test_non_srv_answer(self):
  126. """
  127. test the behaviour when the dns server gives us a spurious non-SRV response
  128. """
  129. service_name = b"test_service.example.com"
  130. lookup_deferred = Deferred()
  131. dns_client_mock = Mock()
  132. dns_client_mock.lookupService.return_value = lookup_deferred
  133. cache = {}
  134. resolver = SrvResolver(dns_client=dns_client_mock, cache=cache)
  135. # Old versions of Twisted don't have an ensureDeferred in successResultOf.
  136. resolve_d = defer.ensureDeferred(resolver.resolve_service(service_name))
  137. lookup_deferred.callback(
  138. (
  139. [
  140. dns.RRHeader(type=dns.A, payload=dns.Record_A()),
  141. dns.RRHeader(type=dns.SRV, payload=dns.Record_SRV(target=b"host")),
  142. ],
  143. None,
  144. None,
  145. )
  146. )
  147. servers = self.successResultOf(resolve_d)
  148. self.assertEqual(len(servers), 1)
  149. self.assertEqual(servers, cache[service_name])
  150. self.assertEqual(servers[0].host, b"host")