test_srv_resolver.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 OpenMarket Ltd
  3. # Copyright 2019 New Vector Ltd
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. from mock import Mock
  17. from twisted.internet import defer
  18. from twisted.internet.defer import Deferred
  19. from twisted.internet.error import ConnectError
  20. from twisted.names import dns, error
  21. from synapse.http.federation.srv_resolver import SrvResolver
  22. from synapse.logging.context import LoggingContext, current_context
  23. from tests import unittest
  24. from tests.utils import MockClock
  25. class SrvResolverTestCase(unittest.TestCase):
  26. def test_resolve(self):
  27. dns_client_mock = Mock()
  28. service_name = b"test_service.example.com"
  29. host_name = b"example.com"
  30. answer_srv = dns.RRHeader(
  31. type=dns.SRV, payload=dns.Record_SRV(target=host_name)
  32. )
  33. result_deferred = Deferred()
  34. dns_client_mock.lookupService.return_value = result_deferred
  35. cache = {}
  36. resolver = SrvResolver(dns_client=dns_client_mock, cache=cache)
  37. @defer.inlineCallbacks
  38. def do_lookup():
  39. with LoggingContext("one") as ctx:
  40. resolve_d = resolver.resolve_service(service_name)
  41. result = yield defer.ensureDeferred(resolve_d)
  42. # should have restored our context
  43. self.assertIs(current_context(), ctx)
  44. return result
  45. test_d = do_lookup()
  46. self.assertNoResult(test_d)
  47. dns_client_mock.lookupService.assert_called_once_with(service_name)
  48. result_deferred.callback(([answer_srv], None, None))
  49. servers = self.successResultOf(test_d)
  50. self.assertEquals(len(servers), 1)
  51. self.assertEquals(servers, cache[service_name])
  52. self.assertEquals(servers[0].host, host_name)
  53. @defer.inlineCallbacks
  54. def test_from_cache_expired_and_dns_fail(self):
  55. dns_client_mock = Mock()
  56. dns_client_mock.lookupService.return_value = defer.fail(error.DNSServerError())
  57. service_name = b"test_service.example.com"
  58. entry = Mock(spec_set=["expires", "priority", "weight"])
  59. entry.expires = 0
  60. entry.priority = 0
  61. entry.weight = 0
  62. cache = {service_name: [entry]}
  63. resolver = SrvResolver(dns_client=dns_client_mock, cache=cache)
  64. servers = yield defer.ensureDeferred(resolver.resolve_service(service_name))
  65. dns_client_mock.lookupService.assert_called_once_with(service_name)
  66. self.assertEquals(len(servers), 1)
  67. self.assertEquals(servers, cache[service_name])
  68. @defer.inlineCallbacks
  69. def test_from_cache(self):
  70. clock = MockClock()
  71. dns_client_mock = Mock(spec_set=["lookupService"])
  72. dns_client_mock.lookupService = Mock(spec_set=[])
  73. service_name = b"test_service.example.com"
  74. entry = Mock(spec_set=["expires", "priority", "weight"])
  75. entry.expires = 999999999
  76. entry.priority = 0
  77. entry.weight = 0
  78. cache = {service_name: [entry]}
  79. resolver = SrvResolver(
  80. dns_client=dns_client_mock, cache=cache, get_time=clock.time
  81. )
  82. servers = yield defer.ensureDeferred(resolver.resolve_service(service_name))
  83. self.assertFalse(dns_client_mock.lookupService.called)
  84. self.assertEquals(len(servers), 1)
  85. self.assertEquals(servers, cache[service_name])
  86. @defer.inlineCallbacks
  87. def test_empty_cache(self):
  88. dns_client_mock = Mock()
  89. dns_client_mock.lookupService.return_value = defer.fail(error.DNSServerError())
  90. service_name = b"test_service.example.com"
  91. cache = {}
  92. resolver = SrvResolver(dns_client=dns_client_mock, cache=cache)
  93. with self.assertRaises(error.DNSServerError):
  94. yield defer.ensureDeferred(resolver.resolve_service(service_name))
  95. @defer.inlineCallbacks
  96. def test_name_error(self):
  97. dns_client_mock = Mock()
  98. dns_client_mock.lookupService.return_value = defer.fail(error.DNSNameError())
  99. service_name = b"test_service.example.com"
  100. cache = {}
  101. resolver = SrvResolver(dns_client=dns_client_mock, cache=cache)
  102. servers = yield defer.ensureDeferred(resolver.resolve_service(service_name))
  103. self.assertEquals(len(servers), 0)
  104. self.assertEquals(len(cache), 0)
  105. def test_disabled_service(self):
  106. """
  107. test the behaviour when there is a single record which is ".".
  108. """
  109. service_name = b"test_service.example.com"
  110. lookup_deferred = Deferred()
  111. dns_client_mock = Mock()
  112. dns_client_mock.lookupService.return_value = lookup_deferred
  113. cache = {}
  114. resolver = SrvResolver(dns_client=dns_client_mock, cache=cache)
  115. # Old versions of Twisted don't have an ensureDeferred in failureResultOf.
  116. resolve_d = defer.ensureDeferred(resolver.resolve_service(service_name))
  117. # returning a single "." should make the lookup fail with a ConenctError
  118. lookup_deferred.callback(
  119. (
  120. [dns.RRHeader(type=dns.SRV, payload=dns.Record_SRV(target=b"."))],
  121. None,
  122. None,
  123. )
  124. )
  125. self.failureResultOf(resolve_d, ConnectError)
  126. def test_non_srv_answer(self):
  127. """
  128. test the behaviour when the dns server gives us a spurious non-SRV response
  129. """
  130. service_name = b"test_service.example.com"
  131. lookup_deferred = Deferred()
  132. dns_client_mock = Mock()
  133. dns_client_mock.lookupService.return_value = lookup_deferred
  134. cache = {}
  135. resolver = SrvResolver(dns_client=dns_client_mock, cache=cache)
  136. # Old versions of Twisted don't have an ensureDeferred in successResultOf.
  137. resolve_d = defer.ensureDeferred(resolver.resolve_service(service_name))
  138. lookup_deferred.callback(
  139. (
  140. [
  141. dns.RRHeader(type=dns.A, payload=dns.Record_A()),
  142. dns.RRHeader(type=dns.SRV, payload=dns.Record_SRV(target=b"host")),
  143. ],
  144. None,
  145. None,
  146. )
  147. )
  148. servers = self.successResultOf(resolve_d)
  149. self.assertEquals(len(servers), 1)
  150. self.assertEquals(servers, cache[service_name])
  151. self.assertEquals(servers[0].host, b"host")