test_srv_resolver.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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"])
  62. entry.expires = 0
  63. cache = {service_name: [entry]}
  64. resolver = SrvResolver(dns_client=dns_client_mock, cache=cache)
  65. servers = yield resolver.resolve_service(service_name)
  66. dns_client_mock.lookupService.assert_called_once_with(service_name)
  67. self.assertEquals(len(servers), 1)
  68. self.assertEquals(servers, cache[service_name])
  69. @defer.inlineCallbacks
  70. def test_from_cache(self):
  71. clock = MockClock()
  72. dns_client_mock = Mock(spec_set=["lookupService"])
  73. dns_client_mock.lookupService = Mock(spec_set=[])
  74. service_name = b"test_service.example.com"
  75. entry = Mock(spec_set=["expires"])
  76. entry.expires = 999999999
  77. cache = {service_name: [entry]}
  78. resolver = SrvResolver(
  79. dns_client=dns_client_mock, cache=cache, get_time=clock.time
  80. )
  81. servers = yield resolver.resolve_service(service_name)
  82. self.assertFalse(dns_client_mock.lookupService.called)
  83. self.assertEquals(len(servers), 1)
  84. self.assertEquals(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 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 resolver.resolve_service(service_name)
  102. self.assertEquals(len(servers), 0)
  103. self.assertEquals(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. resolve_d = resolver.resolve_service(service_name)
  115. self.assertNoResult(resolve_d)
  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. resolve_d = resolver.resolve_service(service_name)
  136. self.assertNoResult(resolve_d)
  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.assertEquals(len(servers), 1)
  149. self.assertEquals(servers, cache[service_name])
  150. self.assertEquals(servers[0].host, b"host")