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 isc::acl::dns::RequestACL;
  26. using namespace isc::testutils;
  27. using isc::UnitTestUtil;
  28. namespace {
  29. const char* const TEST_PORT = "53535";
  30. class ResolverTest : public SrvTestBase{
  31. protected:
  32. ResolverTest() : server() {
  33. // By default queries from the "default remote address" will be
  34. // rejected, so we'll need to add an explicit ACL entry to allow that.
  35. server.setConfigured();
  36. server.updateConfig(Element::fromJSON(
  37. "{ \"query_acl\": "
  38. " [ {\"action\": \"ACCEPT\","
  39. " \"from\": \"" +
  40. string(DEFAULT_REMOTE_ADDRESS) +
  41. "\"} ] }"));
  42. }
  43. virtual void processMessage() {
  44. server.processMessage(*io_message,
  45. parse_message,
  46. response_message,
  47. response_obuffer,
  48. &dnsserv);
  49. }
  50. Resolver server;
  51. };
  52. // Unsupported requests. Should result in NOTIMP.
  53. TEST_F(ResolverTest, unsupportedRequest) {
  54. unsupportedRequest();
  55. }
  56. // Multiple questions. Should result in FORMERR.
  57. TEST_F(ResolverTest, multiQuestion) {
  58. multiQuestion();
  59. }
  60. // Incoming data doesn't even contain the complete header. Must be silently
  61. // dropped.
  62. TEST_F(ResolverTest, shortMessage) {
  63. shortMessage();
  64. }
  65. // Response messages. Must be silently dropped, whether it's a valid response
  66. // or malformed or could otherwise cause a protocol error.
  67. TEST_F(ResolverTest, response) {
  68. response();
  69. }
  70. // Query with a broken question
  71. TEST_F(ResolverTest, shortQuestion) {
  72. shortQuestion();
  73. }
  74. // Query with a broken answer section
  75. TEST_F(ResolverTest, shortAnswer) {
  76. shortAnswer();
  77. }
  78. // Query with unsupported version of EDNS.
  79. TEST_F(ResolverTest, ednsBadVers) {
  80. ednsBadVers();
  81. }
  82. TEST_F(ResolverTest, AXFROverUDP) {
  83. axfrOverUDP();
  84. }
  85. TEST_F(ResolverTest, AXFRFail) {
  86. UnitTestUtil::createRequestMessage(request_message, opcode, default_qid,
  87. Name("example.com"), RRClass::IN(),
  88. RRType::AXFR());
  89. createRequestPacket(request_message, IPPROTO_TCP);
  90. // AXFR is not implemented and should always send NOTIMP.
  91. server.processMessage(*io_message,
  92. parse_message,
  93. response_message,
  94. response_obuffer,
  95. &dnsserv);
  96. EXPECT_TRUE(dnsserv.hasAnswer());
  97. headerCheck(*parse_message, default_qid, Rcode::NOTIMP(), opcode.getCode(),
  98. QR_FLAG, 1, 0, 0, 0);
  99. }
  100. TEST_F(ResolverTest, IXFRFail) {
  101. UnitTestUtil::createRequestMessage(request_message, opcode, default_qid,
  102. Name("example.com"), RRClass::IN(),
  103. RRType::IXFR());
  104. createRequestPacket(request_message, IPPROTO_TCP);
  105. // IXFR is not implemented and should always send NOTIMP.
  106. server.processMessage(*io_message,
  107. parse_message,
  108. response_message,
  109. response_obuffer,
  110. &dnsserv);
  111. EXPECT_TRUE(dnsserv.hasAnswer());
  112. // the second check is what we'll need in the end (with the values
  113. // from the first one), but right now the first one is for what
  114. // will actually be returned to the client
  115. headerCheck(*parse_message, default_qid, Rcode::NOTIMP(), opcode.getCode(),
  116. QR_FLAG, 1, 0, 0, 0);
  117. headerCheck(*response_message, default_qid, Rcode::NOTIMP(), opcode.getCode(),
  118. 0, 0, 0, 0, 0);
  119. }
  120. TEST_F(ResolverTest, notifyFail) {
  121. // Notify should always return NOTAUTH
  122. request_message.clear(Message::RENDER);
  123. request_message.setOpcode(Opcode::NOTIFY());
  124. request_message.setRcode(Rcode::NOERROR());
  125. request_message.setHeaderFlag(Message::HEADERFLAG_AA);
  126. request_message.setQid(default_qid);
  127. request_message.setHeaderFlag(Message::HEADERFLAG_AA);
  128. createRequestPacket(request_message, IPPROTO_UDP);
  129. server.processMessage(*io_message,
  130. parse_message,
  131. response_message,
  132. response_obuffer,
  133. &dnsserv);
  134. EXPECT_TRUE(dnsserv.hasAnswer());
  135. headerCheck(*parse_message, default_qid, Rcode::NOTAUTH(),
  136. Opcode::NOTIFY().getCode(), QR_FLAG, 0, 0, 0, 0);
  137. }
  138. TEST_F(ResolverTest, setQueryACL) {
  139. // valid cases are tested through other tests. We only explicitly check
  140. // an invalid case: passing a NULL shared pointer.
  141. EXPECT_THROW(server.setQueryACL(boost::shared_ptr<const RequestACL>()),
  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. }