resolver_unittest.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <string>
  15. #include <exceptions/exceptions.h>
  16. #include <dns/name.h>
  17. #include <cc/data.h>
  18. #include <resolver/resolver.h>
  19. #include <dns/tests/unittest_util.h>
  20. #include <testutils/dnsmessage_test.h>
  21. #include <testutils/srv_test.h>
  22. using namespace std;
  23. using namespace isc::dns;
  24. using namespace isc::data;
  25. using namespace isc::testutils;
  26. using isc::UnitTestUtil;
  27. namespace {
  28. const char* const TEST_PORT = "53535";
  29. class ResolverTest : public SrvTestBase{
  30. protected:
  31. ResolverTest() : server() {
  32. // By default queries from the "default remote address" will be
  33. // rejected, so we'll need to add an explicit ACL entry to allow that.
  34. server.setConfigured();
  35. server.updateConfig(Element::fromJSON(
  36. "{ \"query_acl\": "
  37. " [ {\"action\": \"ACCEPT\","
  38. " \"from\": \"" +
  39. string(DEFAULT_REMOTE_ADDRESS) +
  40. "\"} ] }"));
  41. }
  42. virtual void processMessage() {
  43. server.processMessage(*io_message,
  44. parse_message,
  45. response_message,
  46. response_obuffer,
  47. &dnsserv);
  48. }
  49. Resolver server;
  50. };
  51. // Unsupported requests. Should result in NOTIMP.
  52. TEST_F(ResolverTest, unsupportedRequest) {
  53. unsupportedRequest();
  54. }
  55. // Multiple questions. Should result in FORMERR.
  56. TEST_F(ResolverTest, multiQuestion) {
  57. multiQuestion();
  58. }
  59. // Incoming data doesn't even contain the complete header. Must be silently
  60. // dropped.
  61. TEST_F(ResolverTest, shortMessage) {
  62. shortMessage();
  63. }
  64. // Response messages. Must be silently dropped, whether it's a valid response
  65. // or malformed or could otherwise cause a protocol error.
  66. TEST_F(ResolverTest, response) {
  67. response();
  68. }
  69. // Query with a broken question
  70. TEST_F(ResolverTest, shortQuestion) {
  71. shortQuestion();
  72. }
  73. // Query with a broken answer section
  74. TEST_F(ResolverTest, shortAnswer) {
  75. shortAnswer();
  76. }
  77. // Query with unsupported version of EDNS.
  78. TEST_F(ResolverTest, ednsBadVers) {
  79. ednsBadVers();
  80. }
  81. TEST_F(ResolverTest, AXFROverUDP) {
  82. axfrOverUDP();
  83. }
  84. TEST_F(ResolverTest, AXFRFail) {
  85. UnitTestUtil::createRequestMessage(request_message, opcode, default_qid,
  86. Name("example.com"), RRClass::IN(),
  87. RRType::AXFR());
  88. createRequestPacket(request_message, IPPROTO_TCP);
  89. // AXFR is not implemented and should always send NOTIMP.
  90. server.processMessage(*io_message,
  91. parse_message,
  92. response_message,
  93. response_obuffer,
  94. &dnsserv);
  95. EXPECT_TRUE(dnsserv.hasAnswer());
  96. headerCheck(*parse_message, default_qid, Rcode::NOTIMP(), opcode.getCode(),
  97. QR_FLAG, 1, 0, 0, 0);
  98. }
  99. TEST_F(ResolverTest, IXFRFail) {
  100. UnitTestUtil::createRequestMessage(request_message, opcode, default_qid,
  101. Name("example.com"), RRClass::IN(),
  102. RRType::IXFR());
  103. createRequestPacket(request_message, IPPROTO_TCP);
  104. // IXFR is not implemented and should always send NOTIMP.
  105. server.processMessage(*io_message,
  106. parse_message,
  107. response_message,
  108. response_obuffer,
  109. &dnsserv);
  110. EXPECT_TRUE(dnsserv.hasAnswer());
  111. // the second check is what we'll need in the end (with the values
  112. // from the first one), but right now the first one is for what
  113. // will actually be returned to the client
  114. headerCheck(*parse_message, default_qid, Rcode::NOTIMP(), opcode.getCode(),
  115. QR_FLAG, 1, 0, 0, 0);
  116. headerCheck(*response_message, default_qid, Rcode::NOTIMP(), opcode.getCode(),
  117. 0, 0, 0, 0, 0);
  118. }
  119. TEST_F(ResolverTest, notifyFail) {
  120. // Notify should always return NOTAUTH
  121. request_message.clear(Message::RENDER);
  122. request_message.setOpcode(Opcode::NOTIFY());
  123. request_message.setRcode(Rcode::NOERROR());
  124. request_message.setHeaderFlag(Message::HEADERFLAG_AA);
  125. request_message.setQid(default_qid);
  126. request_message.setHeaderFlag(Message::HEADERFLAG_AA);
  127. createRequestPacket(request_message, IPPROTO_UDP);
  128. server.processMessage(*io_message,
  129. parse_message,
  130. response_message,
  131. response_obuffer,
  132. &dnsserv);
  133. EXPECT_TRUE(dnsserv.hasAnswer());
  134. headerCheck(*parse_message, default_qid, Rcode::NOTAUTH(),
  135. Opcode::NOTIFY().getCode(), QR_FLAG, 0, 0, 0, 0);
  136. }
  137. TEST_F(ResolverTest, setQueryACL) {
  138. // valid cases are tested through other tests. We only explicitly check
  139. // an invalid case: passing a NULL shared pointer.
  140. EXPECT_THROW(server.setQueryACL(
  141. boost::shared_ptr<const Resolver::QueryACL>()),
  142. isc::InvalidParameter);
  143. }
  144. TEST_F(ResolverTest, queryACL) {
  145. // The "ACCEPT" cases are covered in other tests. Here we explicitly
  146. // test "REJECT" and "DROP" cases.
  147. // Clear the existing ACL, reverting to the "default reject" rule.
  148. // AXFR over UDP. This would otherwise result in FORMERR.
  149. server.updateConfig(Element::fromJSON("{ \"query_acl\": [] }"));
  150. UnitTestUtil::createRequestMessage(request_message, opcode, default_qid,
  151. Name("example.com"), RRClass::IN(),
  152. RRType::AXFR());
  153. createRequestPacket(request_message, IPPROTO_UDP);
  154. server.processMessage(*io_message, parse_message, response_message,
  155. response_obuffer, &dnsserv);
  156. EXPECT_TRUE(dnsserv.hasAnswer());
  157. headerCheck(*parse_message, default_qid, Rcode::REFUSED(),
  158. Opcode::QUERY().getCode(), QR_FLAG, 1, 0, 0, 0);
  159. // Same query, but with an explicit "DROP" ACL entry. There should be
  160. // no response.
  161. server.updateConfig(Element::fromJSON("{ \"query_acl\": "
  162. " [ {\"action\": \"DROP\","
  163. " \"from\": \"" +
  164. string(DEFAULT_REMOTE_ADDRESS) +
  165. "\"} ] }"));
  166. parse_message->clear(Message::PARSE);
  167. response_message->clear(Message::RENDER);
  168. response_obuffer->clear();
  169. server.processMessage(*io_message, parse_message, response_message,
  170. response_obuffer, &dnsserv);
  171. EXPECT_FALSE(dnsserv.hasAnswer());
  172. }
  173. }