query_unittest.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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 <sstream>
  15. #include <vector>
  16. #include <map>
  17. #include <boost/bind.hpp>
  18. #include <dns/masterload.h>
  19. #include <dns/message.h>
  20. #include <dns/name.h>
  21. #include <dns/opcode.h>
  22. #include <dns/rcode.h>
  23. #include <dns/rrttl.h>
  24. #include <dns/rrtype.h>
  25. #include <dns/rdataclass.h>
  26. #include <datasrc/memory_datasrc.h>
  27. #include <auth/query.h>
  28. #include <testutils/dnsmessage_test.h>
  29. #include <gtest/gtest.h>
  30. using namespace std;
  31. using namespace isc::dns;
  32. using namespace isc::dns::rdata;
  33. using namespace isc::datasrc;
  34. using namespace isc::auth;
  35. using namespace isc::testutils;
  36. namespace {
  37. // This is the content of the mock zone (see below).
  38. // It's a sequence of textual RRs that is supposed to be parsed by
  39. // dns::masterLoad(). Some of the RRs are also used as the expected
  40. // data in specific tests, in which case they are referenced via specific
  41. // local variables (such as soa_txt).
  42. const char* const soa_txt = "example.com. 3600 IN SOA . . 0 0 0 0 0\n";
  43. const char* const zone_ns_txt =
  44. "example.com. 3600 IN NS glue.delegation.example.com.\n"
  45. "example.com. 3600 IN NS noglue.example.com.\n"
  46. "example.com. 3600 IN NS example.net.\n";
  47. const char* const ns_addrs_txt =
  48. "glue.delegation.example.com. 3600 IN A 192.0.2.153\n"
  49. "glue.delegation.example.com. 3600 IN AAAA 2001:db8::53\n"
  50. "noglue.example.com. 3600 IN A 192.0.2.53\n";
  51. const char* const delegation_txt =
  52. "delegation.example.com. 3600 IN NS glue.delegation.example.com.\n"
  53. "delegation.example.com. 3600 IN NS noglue.example.com.\n"
  54. "delegation.example.com. 3600 IN NS cname.example.com.\n"
  55. "delegation.example.com. 3600 IN NS example.org.\n";
  56. const char* const mx_txt =
  57. "mx.example.com. 3600 IN MX 10 www.example.com.\n"
  58. "mx.example.com. 3600 IN MX 20 mailer.example.org.\n"
  59. "mx.example.com. 3600 IN MX 30 mx.delegation.example.com.\n";
  60. const char* const www_a_txt = "www.example.com. 3600 IN A 192.0.2.80\n";
  61. // The rest of data won't be referenced from the test cases.
  62. const char* const other_zone_rrs =
  63. "cname.example.com. 3600 IN CNAME www.example.com.\n"
  64. "cnamemailer.example.com. 3600 IN CNAME www.example.com.\n"
  65. "cnamemx.example.com. 3600 IN MX 10 cnamemailer.example.com.\n"
  66. "mx.delegation.example.com. 3600 IN A 192.0.2.100\n";
  67. // This is a mock Zone class for testing.
  68. // It is a derived class of Zone for the convenient of tests.
  69. // Its find() method emulates the common behavior of protocol compliant
  70. // zone classes, but simplifies some minor cases and also supports broken
  71. // behavior.
  72. // For simplicity, most names are assumed to be "in zone"; there's only
  73. // one zone cut at the point of name "delegation.example.com".
  74. // It doesn't handle empty non terminal nodes (if we need to test such cases
  75. // find() should have specialized code for it).
  76. class MockZone : public Zone {
  77. public:
  78. MockZone() :
  79. origin_(Name("example.com")),
  80. delegation_name_("delegation.example.com"),
  81. has_SOA_(true),
  82. has_apex_NS_(true),
  83. rrclass_(RRClass::IN())
  84. {
  85. stringstream zone_stream;
  86. zone_stream << soa_txt << zone_ns_txt << ns_addrs_txt <<
  87. delegation_txt << mx_txt << www_a_txt << other_zone_rrs;
  88. masterLoad(zone_stream, origin_, rrclass_,
  89. boost::bind(&MockZone::loadRRset, this, _1));
  90. }
  91. virtual const isc::dns::Name& getOrigin() const { return (origin_); }
  92. virtual const isc::dns::RRClass& getClass() const { return (rrclass_); }
  93. virtual FindResult find(const isc::dns::Name& name,
  94. const isc::dns::RRType& type,
  95. const FindOptions options = FIND_DEFAULT) const;
  96. // If false is passed, it makes the zone broken as if it didn't have the
  97. // SOA.
  98. void setSOAFlag(bool on) { has_SOA_ = on; }
  99. // If false is passed, it makes the zone broken as if it didn't have
  100. // the apex NS.
  101. void setApexNSFlag(bool on) { has_apex_NS_ = on; }
  102. private:
  103. typedef map<RRType, ConstRRsetPtr> RRsetStore;
  104. typedef map<Name, RRsetStore> Domains;
  105. Domains domains_;
  106. void loadRRset(ConstRRsetPtr rrset) {
  107. domains_[rrset->getName()][rrset->getType()] = rrset;
  108. if (rrset->getName() == delegation_name_ &&
  109. rrset->getType() == RRType::NS()) {
  110. delegation_rrset = rrset;
  111. }
  112. }
  113. const Name origin_;
  114. const Name delegation_name_;
  115. bool has_SOA_;
  116. bool has_apex_NS_;
  117. ConstRRsetPtr delegation_rrset;
  118. const RRClass rrclass_;
  119. };
  120. Zone::FindResult
  121. MockZone::find(const Name& name, const RRType& type,
  122. const FindOptions options) const
  123. {
  124. // Emulating a broken zone: mandatory apex RRs are missing if specifically
  125. // configured so (which are rare cases).
  126. if (name == origin_ && type == RRType::SOA() && !has_SOA_) {
  127. return (FindResult(NXDOMAIN, RRsetPtr()));
  128. } else if (name == origin_ && type == RRType::NS() && !has_apex_NS_) {
  129. return (FindResult(NXDOMAIN, RRsetPtr()));
  130. }
  131. // Special case for names on or under a zone cut
  132. if ((options & FIND_GLUE_OK) == 0 &&
  133. (name == delegation_name_ ||
  134. name.compare(delegation_name_).getRelation() ==
  135. NameComparisonResult::SUBDOMAIN)) {
  136. return (FindResult(DELEGATION, delegation_rrset));
  137. }
  138. // normal cases. names are searched for only per exact-match basis
  139. // for simplicity.
  140. const Domains::const_iterator found_domain = domains_.find(name);
  141. if (found_domain != domains_.end()) {
  142. // First, try exact match.
  143. RRsetStore::const_iterator found_rrset =
  144. found_domain->second.find(type);
  145. if (found_rrset != found_domain->second.end()) {
  146. return (FindResult(SUCCESS, found_rrset->second));
  147. }
  148. // If not found but the qtype is ANY, return the first RRset
  149. if (type == RRType::ANY()) {
  150. return (FindResult(SUCCESS, found_domain->second.begin()->second));
  151. }
  152. // Otherwise, if this domain name has CNAME, return it.
  153. found_rrset = found_domain->second.find(RRType::CNAME());
  154. if (found_rrset != found_domain->second.end()) {
  155. return (FindResult(CNAME, found_rrset->second));
  156. }
  157. // Otherwise it's NXRRSET case.
  158. return (FindResult(NXRRSET, RRsetPtr()));
  159. }
  160. // query name isn't found in our domains. returns NXDOMAIN.
  161. return (FindResult(NXDOMAIN, RRsetPtr()));
  162. }
  163. class QueryTest : public ::testing::Test {
  164. protected:
  165. QueryTest() :
  166. qname(Name("www.example.com")), qclass(RRClass::IN()),
  167. qtype(RRType::A()), response(Message::RENDER),
  168. qid(response.getQid()), query_code(Opcode::QUERY().getCode())
  169. {
  170. response.setRcode(Rcode::NOERROR());
  171. response.setOpcode(Opcode::QUERY());
  172. // create and add a matching zone.
  173. mock_zone = new MockZone();
  174. memory_datasrc.addZone(ZonePtr(mock_zone));
  175. }
  176. MockZone* mock_zone;
  177. MemoryDataSrc memory_datasrc;
  178. const Name qname;
  179. const RRClass qclass;
  180. const RRType qtype;
  181. Message response;
  182. const qid_t qid;
  183. const uint16_t query_code;
  184. stringstream expected_answer;
  185. stringstream expected_authority;
  186. stringstream expected_additional;
  187. };
  188. TEST_F(QueryTest, noZone) {
  189. // There's no zone in the memory datasource. So the response should have
  190. // REFUSED.
  191. MemoryDataSrc empty_memory_datasrc;
  192. Query nozone_query(empty_memory_datasrc, qname, qtype, response);
  193. EXPECT_NO_THROW(nozone_query.process());
  194. EXPECT_EQ(Rcode::REFUSED(), response.getRcode());
  195. }
  196. TEST_F(QueryTest, exactMatch) {
  197. Query query(memory_datasrc, qname, qtype, response);
  198. EXPECT_NO_THROW(query.process());
  199. // find match rrset
  200. headerCheck(response, qid, Rcode::NOERROR(), query_code,
  201. AA_FLAG, 0, 1, 3, 3);
  202. expected_answer << www_a_txt;
  203. rrsetsCheck(expected_answer,
  204. response.beginSection(Message::SECTION_ANSWER),
  205. response.endSection(Message::SECTION_ANSWER));
  206. expected_authority << zone_ns_txt;
  207. rrsetsCheck(expected_authority,
  208. response.beginSection(Message::SECTION_AUTHORITY),
  209. response.endSection(Message::SECTION_AUTHORITY));
  210. expected_additional << ns_addrs_txt;
  211. rrsetsCheck(expected_additional,
  212. response.beginSection(Message::SECTION_ADDITIONAL),
  213. response.endSection(Message::SECTION_ADDITIONAL));
  214. }
  215. TEST_F(QueryTest, exactAddrMatch) {
  216. // find match rrset, omit additional data which has already been provided
  217. // in the answer section from the additional.
  218. EXPECT_NO_THROW(Query(memory_datasrc, Name("noglue.example.com"), qtype,
  219. response).process());
  220. headerCheck(response, qid, Rcode::NOERROR(), query_code,
  221. AA_FLAG, 0, 1, 3, 2);
  222. expected_answer << "noglue.example.com. 3600 IN A 192.0.2.53\n";
  223. rrsetsCheck(expected_answer,
  224. response.beginSection(Message::SECTION_ANSWER),
  225. response.endSection(Message::SECTION_ANSWER));
  226. expected_authority << zone_ns_txt;
  227. rrsetsCheck(expected_authority,
  228. response.beginSection(Message::SECTION_AUTHORITY),
  229. response.endSection(Message::SECTION_AUTHORITY));
  230. expected_additional <<
  231. "glue.delegation.example.com. 3600 IN A 192.0.2.153\n"
  232. "glue.delegation.example.com. 3600 IN AAAA 2001:db8::53\n";
  233. rrsetsCheck(expected_additional,
  234. response.beginSection(Message::SECTION_ADDITIONAL),
  235. response.endSection(Message::SECTION_ADDITIONAL));
  236. }
  237. TEST_F(QueryTest, apexNSMatch) {
  238. // find match rrset, omit authority data which has already been provided
  239. // in the answer section from the authority section.
  240. EXPECT_NO_THROW(Query(memory_datasrc, Name("example.com"), RRType::NS(),
  241. response).process());
  242. headerCheck(response, qid, Rcode::NOERROR(), query_code,
  243. AA_FLAG, 0, 3, 0, 3);
  244. expected_answer << zone_ns_txt;
  245. rrsetsCheck(expected_answer,
  246. response.beginSection(Message::SECTION_ANSWER),
  247. response.endSection(Message::SECTION_ANSWER));
  248. expected_additional << ns_addrs_txt;
  249. rrsetsCheck(expected_additional,
  250. response.beginSection(Message::SECTION_ADDITIONAL),
  251. response.endSection(Message::SECTION_ADDITIONAL));
  252. }
  253. TEST_F(QueryTest, exactAnyMatch) {
  254. // find match rrset, omit additional data which has already been provided
  255. // in the answer section from the additional.
  256. EXPECT_NO_THROW(Query(memory_datasrc, Name("noglue.example.com"),
  257. RRType::ANY(), response).process());
  258. headerCheck(response, qid, Rcode::NOERROR(), query_code,
  259. AA_FLAG, 0, 1, 3, 2);
  260. expected_answer << "noglue.example.com. 3600 IN A 192.0.2.53\n";
  261. rrsetsCheck(expected_answer,
  262. response.beginSection(Message::SECTION_ANSWER),
  263. response.endSection(Message::SECTION_ANSWER));
  264. expected_authority << zone_ns_txt;
  265. rrsetsCheck(expected_authority,
  266. response.beginSection(Message::SECTION_AUTHORITY),
  267. response.endSection(Message::SECTION_AUTHORITY));
  268. expected_additional <<
  269. "glue.delegation.example.com. 3600 IN A 192.0.2.153\n"
  270. "glue.delegation.example.com. 3600 IN AAAA 2001:db8::53\n";
  271. rrsetsCheck(expected_additional,
  272. response.beginSection(Message::SECTION_ADDITIONAL),
  273. response.endSection(Message::SECTION_ADDITIONAL));
  274. }
  275. // This tests that when we need to look up Zone's apex NS records for
  276. // authoritative answer, and there is no apex NS records. It should
  277. // throw in that case.
  278. TEST_F(QueryTest, noApexNS) {
  279. // Disable apex NS record
  280. mock_zone->setApexNSFlag(false);
  281. EXPECT_THROW(Query(memory_datasrc, Name("noglue.example.com"), qtype,
  282. response).process(), Query::NoApexNS);
  283. // We don't look into the response, as it threw
  284. }
  285. TEST_F(QueryTest, delegation) {
  286. EXPECT_NO_THROW(Query(memory_datasrc, Name("delegation.example.com"),
  287. qtype, response).process());
  288. headerCheck(response, qid, Rcode::NOERROR(), query_code,
  289. 0, 0, 0, 4, 3);
  290. expected_authority << delegation_txt;
  291. rrsetsCheck(expected_authority,
  292. response.beginSection(Message::SECTION_AUTHORITY),
  293. response.endSection(Message::SECTION_AUTHORITY));
  294. expected_additional << ns_addrs_txt;
  295. rrsetsCheck(expected_additional,
  296. response.beginSection(Message::SECTION_ADDITIONAL),
  297. response.endSection(Message::SECTION_ADDITIONAL));
  298. }
  299. TEST_F(QueryTest, nxdomain) {
  300. EXPECT_NO_THROW(Query(memory_datasrc, Name("nxdomain.example.com"), qtype,
  301. response).process());
  302. headerCheck(response, qid, Rcode::NXDOMAIN(), query_code,
  303. AA_FLAG, 0, 0, 1, 0);
  304. expected_authority << soa_txt;
  305. rrsetsCheck(expected_authority,
  306. response.beginSection(Message::SECTION_AUTHORITY),
  307. response.endSection(Message::SECTION_AUTHORITY),
  308. Name("example.com"));
  309. }
  310. TEST_F(QueryTest, nxrrset) {
  311. EXPECT_NO_THROW(Query(memory_datasrc, Name("www.example.com"),
  312. RRType::TXT(), response).process());
  313. headerCheck(response, qid, Rcode::NOERROR(), query_code,
  314. AA_FLAG, 0, 0, 1, 0);
  315. expected_authority << soa_txt;
  316. rrsetsCheck(expected_authority,
  317. response.beginSection(Message::SECTION_AUTHORITY),
  318. response.endSection(Message::SECTION_AUTHORITY),
  319. Name("example.com"));
  320. }
  321. /*
  322. * This tests that when there's no SOA and we need a negative answer. It should
  323. * throw in that case.
  324. */
  325. TEST_F(QueryTest, noSOA) {
  326. // disable zone's SOA RR.
  327. mock_zone->setSOAFlag(false);
  328. // The NX Domain
  329. EXPECT_THROW(Query(memory_datasrc, Name("nxdomain.example.com"),
  330. qtype, response).process(), Query::NoSOA);
  331. // Of course, we don't look into the response, as it throwed
  332. // NXRRSET
  333. EXPECT_THROW(Query(memory_datasrc, Name("nxrrset.example.com"),
  334. qtype, response).process(), Query::NoSOA);
  335. }
  336. TEST_F(QueryTest, noMatchZone) {
  337. // there's a zone in the memory datasource but it doesn't match the qname.
  338. // should result in REFUSED.
  339. Query(memory_datasrc, Name("example.org"), qtype, response).process();;
  340. EXPECT_EQ(Rcode::REFUSED(), response.getRcode());
  341. }
  342. /*
  343. * Test MX additional processing.
  344. *
  345. * The MX RRset has two RRs, one pointing to a known domain with
  346. * A record, other to unknown out of zone one.
  347. */
  348. TEST_F(QueryTest, MX) {
  349. Query(memory_datasrc, Name("mx.example.com"), RRType::MX(),
  350. response).process();
  351. headerCheck(response, qid, Rcode::NOERROR(), query_code,
  352. AA_FLAG, 0, 3, 3, 4);
  353. expected_answer << mx_txt;
  354. rrsetsCheck(expected_answer,
  355. response.beginSection(Message::SECTION_ANSWER),
  356. response.endSection(Message::SECTION_ANSWER));
  357. expected_additional << ns_addrs_txt;
  358. expected_additional << www_a_txt;
  359. rrsetsCheck(expected_additional,
  360. response.beginSection(Message::SECTION_ADDITIONAL),
  361. response.endSection(Message::SECTION_ADDITIONAL));
  362. }
  363. /*
  364. * Test when we ask for MX whose exchange is an alias (CNAME in this case).
  365. *
  366. * This should not trigger the additional processing for the exchange.
  367. */
  368. TEST_F(QueryTest, MXAlias) {
  369. Query(memory_datasrc, Name("cnamemx.example.com"), RRType::MX(),
  370. response).process();
  371. EXPECT_EQ(Rcode::NOERROR(), response.getRcode());
  372. // there shouldn't be no additional RRs for the exchanges (we have 3
  373. // RRs for the NS)
  374. headerCheck(response, qid, Rcode::NOERROR(), query_code,
  375. AA_FLAG, 0, 1, 3, 3);
  376. expected_additional << ns_addrs_txt;
  377. rrsetsCheck(expected_additional,
  378. response.beginSection(Message::SECTION_ADDITIONAL),
  379. response.endSection(Message::SECTION_ADDITIONAL));
  380. }
  381. }