test_srv_resolver.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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
  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. self.assertNoResult(resolve_d)
  42. # should have reset to the sentinel context
  43. self.assertIs(LoggingContext.current_context(), LoggingContext.sentinel)
  44. result = yield resolve_d
  45. # should have restored our context
  46. self.assertIs(LoggingContext.current_context(), ctx)
  47. return result
  48. test_d = do_lookup()
  49. self.assertNoResult(test_d)
  50. dns_client_mock.lookupService.assert_called_once_with(service_name)
  51. result_deferred.callback(([answer_srv], None, None))
  52. servers = self.successResultOf(test_d)
  53. self.assertEquals(len(servers), 1)
  54. self.assertEquals(servers, cache[service_name])
  55. self.assertEquals(servers[0].host, host_name)
  56. @defer.inlineCallbacks
  57. def test_from_cache_expired_and_dns_fail(self):
  58. dns_client_mock = Mock()
  59. dns_client_mock.lookupService.return_value = defer.fail(error.DNSServerError())
  60. service_name = b"test_service.example.com"
  61. entry = Mock(spec_set=["expires", "priority", "weight"])
  62. entry.expires = 0
  63. entry.priority = 0
  64. entry.weight = 0
  65. cache = {service_name: [entry]}
  66. resolver = SrvResolver(dns_client=dns_client_mock, cache=cache)
  67. servers = yield resolver.resolve_service(service_name)
  68. dns_client_mock.lookupService.assert_called_once_with(service_name)
  69. self.assertEquals(len(servers), 1)
  70. self.assertEquals(servers, cache[service_name])
  71. @defer.inlineCallbacks
  72. def test_from_cache(self):
  73. clock = MockClock()
  74. dns_client_mock = Mock(spec_set=["lookupService"])
  75. dns_client_mock.lookupService = Mock(spec_set=[])
  76. service_name = b"test_service.example.com"
  77. entry = Mock(spec_set=["expires", "priority", "weight"])
  78. entry.expires = 999999999
  79. entry.priority = 0
  80. entry.weight = 0
  81. cache = {service_name: [entry]}
  82. resolver = SrvResolver(
  83. dns_client=dns_client_mock, cache=cache, get_time=clock.time
  84. )
  85. servers = yield resolver.resolve_service(service_name)
  86. self.assertFalse(dns_client_mock.lookupService.called)
  87. self.assertEquals(len(servers), 1)
  88. self.assertEquals(servers, cache[service_name])
  89. @defer.inlineCallbacks
  90. def test_empty_cache(self):
  91. dns_client_mock = Mock()
  92. dns_client_mock.lookupService.return_value = defer.fail(error.DNSServerError())
  93. service_name = b"test_service.example.com"
  94. cache = {}
  95. resolver = SrvResolver(dns_client=dns_client_mock, cache=cache)
  96. with self.assertRaises(error.DNSServerError):
  97. yield resolver.resolve_service(service_name)
  98. @defer.inlineCallbacks
  99. def test_name_error(self):
  100. dns_client_mock = Mock()
  101. dns_client_mock.lookupService.return_value = defer.fail(error.DNSNameError())
  102. service_name = b"test_service.example.com"
  103. cache = {}
  104. resolver = SrvResolver(dns_client=dns_client_mock, cache=cache)
  105. servers = yield resolver.resolve_service(service_name)
  106. self.assertEquals(len(servers), 0)
  107. self.assertEquals(len(cache), 0)
  108. def test_disabled_service(self):
  109. """
  110. test the behaviour when there is a single record which is ".".
  111. """
  112. service_name = b"test_service.example.com"
  113. lookup_deferred = Deferred()
  114. dns_client_mock = Mock()
  115. dns_client_mock.lookupService.return_value = lookup_deferred
  116. cache = {}
  117. resolver = SrvResolver(dns_client=dns_client_mock, cache=cache)
  118. resolve_d = resolver.resolve_service(service_name)
  119. self.assertNoResult(resolve_d)
  120. # returning a single "." should make the lookup fail with a ConenctError
  121. lookup_deferred.callback(
  122. (
  123. [dns.RRHeader(type=dns.SRV, payload=dns.Record_SRV(target=b"."))],
  124. None,
  125. None,
  126. )
  127. )
  128. self.failureResultOf(resolve_d, ConnectError)
  129. def test_non_srv_answer(self):
  130. """
  131. test the behaviour when the dns server gives us a spurious non-SRV response
  132. """
  133. service_name = b"test_service.example.com"
  134. lookup_deferred = Deferred()
  135. dns_client_mock = Mock()
  136. dns_client_mock.lookupService.return_value = lookup_deferred
  137. cache = {}
  138. resolver = SrvResolver(dns_client=dns_client_mock, cache=cache)
  139. resolve_d = resolver.resolve_service(service_name)
  140. self.assertNoResult(resolve_d)
  141. lookup_deferred.callback(
  142. (
  143. [
  144. dns.RRHeader(type=dns.A, payload=dns.Record_A()),
  145. dns.RRHeader(type=dns.SRV, payload=dns.Record_SRV(target=b"host")),
  146. ],
  147. None,
  148. None,
  149. )
  150. )
  151. servers = self.successResultOf(resolve_d)
  152. self.assertEquals(len(servers), 1)
  153. self.assertEquals(servers, cache[service_name])
  154. self.assertEquals(servers[0].host, b"host")