query_unittest.cc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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. const char* const cname_txt =
  62. "cname.example.com. 3600 IN CNAME www.example.com.\n";
  63. const char* const cname_nxdom_txt =
  64. "cnamenxdom.example.com. 3600 IN CNAME nxdomain.example.com.\n";
  65. // CNAME Leading out of zone
  66. const char* const cname_out_txt =
  67. "cnameout.example.com. 3600 IN CNAME www.example.org.\n";
  68. // The DNAME to do tests against
  69. const char* const dname_txt =
  70. "dname.example.com. 3600 IN DNAME "
  71. "somethinglong.dnametarget.example.com.\n";
  72. // Some data at the dname node (allowed by RFC 2672)
  73. const char* const dname_a_txt =
  74. "dname.example.com. 3600 IN A 192.0.2.5\n";
  75. // This is not inside the zone, this is created at runtime
  76. const char* const synthetized_cname_txt =
  77. "www.dname.example.com. 3600 IN CNAME "
  78. "www.somethinglong.dnametarget.example.com.\n";
  79. // The rest of data won't be referenced from the test cases.
  80. const char* const other_zone_rrs =
  81. "cnamemailer.example.com. 3600 IN CNAME www.example.com.\n"
  82. "cnamemx.example.com. 3600 IN MX 10 cnamemailer.example.com.\n"
  83. "mx.delegation.example.com. 3600 IN A 192.0.2.100\n";
  84. // This is a mock Zone class for testing.
  85. // It is a derived class of Zone for the convenient of tests.
  86. // Its find() method emulates the common behavior of protocol compliant
  87. // zone classes, but simplifies some minor cases and also supports broken
  88. // behavior.
  89. // For simplicity, most names are assumed to be "in zone"; there's only
  90. // one zone cut at the point of name "delegation.example.com".
  91. // Another special name is "dname.example.com". Query names under this name
  92. // will result in DNAME.
  93. // This mock zone doesn't handle empty non terminal nodes (if we need to test
  94. // such cases find() should have specialized code for it).
  95. class MockZone : public Zone {
  96. public:
  97. MockZone() :
  98. origin_(Name("example.com")),
  99. delegation_name_("delegation.example.com"),
  100. dname_name_("dname.example.com"),
  101. has_SOA_(true),
  102. has_apex_NS_(true),
  103. rrclass_(RRClass::IN())
  104. {
  105. stringstream zone_stream;
  106. zone_stream << soa_txt << zone_ns_txt << ns_addrs_txt <<
  107. delegation_txt << mx_txt << www_a_txt << cname_txt <<
  108. cname_nxdom_txt << cname_out_txt << dname_txt << dname_a_txt <<
  109. other_zone_rrs;
  110. masterLoad(zone_stream, origin_, rrclass_,
  111. boost::bind(&MockZone::loadRRset, this, _1));
  112. }
  113. virtual const isc::dns::Name& getOrigin() const { return (origin_); }
  114. virtual const isc::dns::RRClass& getClass() const { return (rrclass_); }
  115. virtual FindResult find(const isc::dns::Name& name,
  116. const isc::dns::RRType& type,
  117. RRsetList* target = NULL,
  118. const FindOptions options = FIND_DEFAULT) const;
  119. // If false is passed, it makes the zone broken as if it didn't have the
  120. // SOA.
  121. void setSOAFlag(bool on) { has_SOA_ = on; }
  122. // If false is passed, it makes the zone broken as if it didn't have
  123. // the apex NS.
  124. void setApexNSFlag(bool on) { has_apex_NS_ = on; }
  125. private:
  126. typedef map<RRType, ConstRRsetPtr> RRsetStore;
  127. typedef map<Name, RRsetStore> Domains;
  128. Domains domains_;
  129. void loadRRset(ConstRRsetPtr rrset) {
  130. domains_[rrset->getName()][rrset->getType()] = rrset;
  131. if (rrset->getName() == delegation_name_ &&
  132. rrset->getType() == RRType::NS()) {
  133. delegation_rrset_ = rrset;
  134. } else if (rrset->getName() == dname_name_ &&
  135. rrset->getType() == RRType::DNAME()) {
  136. dname_rrset_ = rrset;
  137. }
  138. }
  139. const Name origin_;
  140. // Names where we delegate somewhere else
  141. const Name delegation_name_;
  142. const Name dname_name_;
  143. bool has_SOA_;
  144. bool has_apex_NS_;
  145. ConstRRsetPtr delegation_rrset_;
  146. ConstRRsetPtr dname_rrset_;
  147. const RRClass rrclass_;
  148. };
  149. Zone::FindResult
  150. MockZone::find(const Name& name, const RRType& type,
  151. RRsetList* target, const FindOptions options) const
  152. {
  153. // Emulating a broken zone: mandatory apex RRs are missing if specifically
  154. // configured so (which are rare cases).
  155. if (name == origin_ && type == RRType::SOA() && !has_SOA_) {
  156. return (FindResult(NXDOMAIN, RRsetPtr()));
  157. } else if (name == origin_ && type == RRType::NS() && !has_apex_NS_) {
  158. return (FindResult(NXDOMAIN, RRsetPtr()));
  159. }
  160. // Special case for names on or under a zone cut
  161. if ((options & FIND_GLUE_OK) == 0 &&
  162. (name == delegation_name_ ||
  163. name.compare(delegation_name_).getRelation() ==
  164. NameComparisonResult::SUBDOMAIN)) {
  165. return (FindResult(DELEGATION, delegation_rrset_));
  166. // And under DNAME
  167. } else if (name.compare(dname_name_).getRelation() ==
  168. NameComparisonResult::SUBDOMAIN) {
  169. return (FindResult(DNAME, dname_rrset_));
  170. }
  171. // normal cases. names are searched for only per exact-match basis
  172. // for simplicity.
  173. const Domains::const_iterator found_domain = domains_.find(name);
  174. if (found_domain != domains_.end()) {
  175. // First, try exact match.
  176. RRsetStore::const_iterator found_rrset =
  177. found_domain->second.find(type);
  178. if (found_rrset != found_domain->second.end()) {
  179. return (FindResult(SUCCESS, found_rrset->second));
  180. }
  181. // If not found but we have a target, fill it with all RRsets here
  182. if (!found_domain->second.empty() && target != NULL) {
  183. for (found_rrset = found_domain->second.begin();
  184. found_rrset != found_domain->second.end(); found_rrset++) {
  185. // Insert RRs under the domain name into target
  186. target->addRRset(
  187. boost::const_pointer_cast<RRset>(found_rrset->second));
  188. }
  189. return (FindResult(SUCCESS, found_domain->second.begin()->second));
  190. }
  191. // Otherwise, if this domain name has CNAME, return it.
  192. found_rrset = found_domain->second.find(RRType::CNAME());
  193. if (found_rrset != found_domain->second.end()) {
  194. return (FindResult(CNAME, found_rrset->second));
  195. }
  196. // Otherwise it's NXRRSET case.
  197. return (FindResult(NXRRSET, RRsetPtr()));
  198. }
  199. // query name isn't found in our domains. returns NXDOMAIN.
  200. return (FindResult(NXDOMAIN, RRsetPtr()));
  201. }
  202. class QueryTest : public ::testing::Test {
  203. protected:
  204. QueryTest() :
  205. qname(Name("www.example.com")), qclass(RRClass::IN()),
  206. qtype(RRType::A()), response(Message::RENDER),
  207. qid(response.getQid()), query_code(Opcode::QUERY().getCode())
  208. {
  209. response.setRcode(Rcode::NOERROR());
  210. response.setOpcode(Opcode::QUERY());
  211. // create and add a matching zone.
  212. mock_zone = new MockZone();
  213. memory_datasrc.addZone(ZonePtr(mock_zone));
  214. }
  215. MockZone* mock_zone;
  216. MemoryDataSrc memory_datasrc;
  217. const Name qname;
  218. const RRClass qclass;
  219. const RRType qtype;
  220. Message response;
  221. const qid_t qid;
  222. const uint16_t query_code;
  223. };
  224. // A wrapper to check resulting response message commonly used in
  225. // tests below.
  226. // check_origin needs to be specified only when the authority section has
  227. // an SOA RR. The interface is not generic enough but should be okay
  228. // for our test cases in practice.
  229. void
  230. responseCheck(Message& response, const isc::dns::Rcode& rcode,
  231. unsigned int flags, const unsigned int ancount,
  232. const unsigned int nscount, const unsigned int arcount,
  233. const char* const expected_answer,
  234. const char* const expected_authority,
  235. const char* const expected_additional,
  236. const Name& check_origin = Name::ROOT_NAME())
  237. {
  238. // In our test cases QID, Opcode, and QDCOUNT should be constant, so
  239. // we don't bother the test cases specifying these values.
  240. headerCheck(response, response.getQid(), rcode, Opcode::QUERY().getCode(),
  241. flags, 0, ancount, nscount, arcount);
  242. if (expected_answer != NULL) {
  243. rrsetsCheck(expected_answer,
  244. response.beginSection(Message::SECTION_ANSWER),
  245. response.endSection(Message::SECTION_ANSWER),
  246. check_origin);
  247. }
  248. if (expected_authority != NULL) {
  249. rrsetsCheck(expected_authority,
  250. response.beginSection(Message::SECTION_AUTHORITY),
  251. response.endSection(Message::SECTION_AUTHORITY),
  252. check_origin);
  253. }
  254. if (expected_additional != NULL) {
  255. rrsetsCheck(expected_additional,
  256. response.beginSection(Message::SECTION_ADDITIONAL),
  257. response.endSection(Message::SECTION_ADDITIONAL));
  258. }
  259. }
  260. TEST_F(QueryTest, noZone) {
  261. // There's no zone in the memory datasource. So the response should have
  262. // REFUSED.
  263. MemoryDataSrc empty_memory_datasrc;
  264. Query nozone_query(empty_memory_datasrc, qname, qtype, response);
  265. EXPECT_NO_THROW(nozone_query.process());
  266. EXPECT_EQ(Rcode::REFUSED(), response.getRcode());
  267. }
  268. TEST_F(QueryTest, exactMatch) {
  269. Query query(memory_datasrc, qname, qtype, response);
  270. EXPECT_NO_THROW(query.process());
  271. // find match rrset
  272. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 3, 3,
  273. www_a_txt, zone_ns_txt, ns_addrs_txt);
  274. }
  275. TEST_F(QueryTest, exactAddrMatch) {
  276. // find match rrset, omit additional data which has already been provided
  277. // in the answer section from the additional.
  278. EXPECT_NO_THROW(Query(memory_datasrc, Name("noglue.example.com"), qtype,
  279. response).process());
  280. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 3, 2,
  281. "noglue.example.com. 3600 IN A 192.0.2.53\n", zone_ns_txt,
  282. "glue.delegation.example.com. 3600 IN A 192.0.2.153\n"
  283. "glue.delegation.example.com. 3600 IN AAAA 2001:db8::53\n");
  284. }
  285. TEST_F(QueryTest, apexNSMatch) {
  286. // find match rrset, omit authority data which has already been provided
  287. // in the answer section from the authority section.
  288. EXPECT_NO_THROW(Query(memory_datasrc, Name("example.com"), RRType::NS(),
  289. response).process());
  290. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 3, 0, 3,
  291. zone_ns_txt, NULL, ns_addrs_txt);
  292. }
  293. // test type any query logic
  294. TEST_F(QueryTest, exactAnyMatch) {
  295. // find match rrset, omit additional data which has already been provided
  296. // in the answer section from the additional.
  297. EXPECT_NO_THROW(Query(memory_datasrc, Name("noglue.example.com"),
  298. RRType::ANY(), response).process());
  299. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 3, 2,
  300. "noglue.example.com. 3600 IN A 192.0.2.53\n",
  301. zone_ns_txt,
  302. "glue.delegation.example.com. 3600 IN A 192.0.2.153\n"
  303. "glue.delegation.example.com. 3600 IN AAAA 2001:db8::53\n");
  304. }
  305. TEST_F(QueryTest, apexAnyMatch) {
  306. // find match rrset, omit additional data which has already been provided
  307. // in the answer section from the additional.
  308. EXPECT_NO_THROW(Query(memory_datasrc, Name("example.com"),
  309. RRType::ANY(), response).process());
  310. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 4, 0, 0,
  311. "example.com. 3600 IN SOA . . 0 0 0 0 0\n"
  312. "example.com. 3600 IN NS glue.delegation.example.com.\n"
  313. "example.com. 3600 IN NS noglue.example.com.\n"
  314. "example.com. 3600 IN NS example.net.\n",
  315. NULL, NULL, mock_zone->getOrigin());
  316. }
  317. TEST_F(QueryTest, glueANYMatch) {
  318. EXPECT_NO_THROW(Query(memory_datasrc, Name("delegation.example.com"),
  319. RRType::ANY(), response).process());
  320. responseCheck(response, Rcode::NOERROR(), 0, 0, 4, 3,
  321. NULL, delegation_txt, ns_addrs_txt);
  322. }
  323. TEST_F(QueryTest, nodomainANY) {
  324. EXPECT_NO_THROW(Query(memory_datasrc, Name("nxdomain.example.com"),
  325. RRType::ANY(), response).process());
  326. responseCheck(response, Rcode::NXDOMAIN(), AA_FLAG, 0, 1, 0,
  327. NULL, soa_txt, NULL, mock_zone->getOrigin());
  328. }
  329. // This tests that when we need to look up Zone's apex NS records for
  330. // authoritative answer, and there is no apex NS records. It should
  331. // throw in that case.
  332. TEST_F(QueryTest, noApexNS) {
  333. // Disable apex NS record
  334. mock_zone->setApexNSFlag(false);
  335. EXPECT_THROW(Query(memory_datasrc, Name("noglue.example.com"), qtype,
  336. response).process(), Query::NoApexNS);
  337. // We don't look into the response, as it threw
  338. }
  339. TEST_F(QueryTest, delegation) {
  340. EXPECT_NO_THROW(Query(memory_datasrc, Name("delegation.example.com"),
  341. qtype, response).process());
  342. responseCheck(response, Rcode::NOERROR(), 0, 0, 4, 3,
  343. NULL, delegation_txt, ns_addrs_txt);
  344. }
  345. TEST_F(QueryTest, nxdomain) {
  346. EXPECT_NO_THROW(Query(memory_datasrc, Name("nxdomain.example.com"), qtype,
  347. response).process());
  348. responseCheck(response, Rcode::NXDOMAIN(), AA_FLAG, 0, 1, 0,
  349. NULL, soa_txt, NULL, mock_zone->getOrigin());
  350. }
  351. TEST_F(QueryTest, nxrrset) {
  352. EXPECT_NO_THROW(Query(memory_datasrc, Name("www.example.com"),
  353. RRType::TXT(), response).process());
  354. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 0, 1, 0,
  355. NULL, soa_txt, NULL, mock_zone->getOrigin());
  356. }
  357. /*
  358. * This tests that when there's no SOA and we need a negative answer. It should
  359. * throw in that case.
  360. */
  361. TEST_F(QueryTest, noSOA) {
  362. // disable zone's SOA RR.
  363. mock_zone->setSOAFlag(false);
  364. // The NX Domain
  365. EXPECT_THROW(Query(memory_datasrc, Name("nxdomain.example.com"),
  366. qtype, response).process(), Query::NoSOA);
  367. // Of course, we don't look into the response, as it throwed
  368. // NXRRSET
  369. EXPECT_THROW(Query(memory_datasrc, Name("nxrrset.example.com"),
  370. qtype, response).process(), Query::NoSOA);
  371. }
  372. TEST_F(QueryTest, noMatchZone) {
  373. // there's a zone in the memory datasource but it doesn't match the qname.
  374. // should result in REFUSED.
  375. Query(memory_datasrc, Name("example.org"), qtype, response).process();
  376. EXPECT_EQ(Rcode::REFUSED(), response.getRcode());
  377. }
  378. /*
  379. * Test MX additional processing.
  380. *
  381. * The MX RRset has two RRs, one pointing to a known domain with
  382. * A record, other to unknown out of zone one.
  383. */
  384. TEST_F(QueryTest, MX) {
  385. Query(memory_datasrc, Name("mx.example.com"), RRType::MX(),
  386. response).process();
  387. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 3, 3, 4,
  388. mx_txt, NULL,
  389. (string(ns_addrs_txt) + string(www_a_txt)).c_str());
  390. }
  391. /*
  392. * Test when we ask for MX whose exchange is an alias (CNAME in this case).
  393. *
  394. * This should not trigger the additional processing for the exchange.
  395. */
  396. TEST_F(QueryTest, MXAlias) {
  397. Query(memory_datasrc, Name("cnamemx.example.com"), RRType::MX(),
  398. response).process();
  399. // there shouldn't be no additional RRs for the exchanges (we have 3
  400. // RRs for the NS). The normal MX case is tested separately so we don't
  401. // bother to examine the answer (and authority) sections.
  402. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 3, 3,
  403. NULL, NULL, ns_addrs_txt);
  404. }
  405. /*
  406. * Tests encountering a cname.
  407. *
  408. * There are tests leading to successful answers, NXRRSET, NXDOMAIN and
  409. * out of the zone.
  410. *
  411. * TODO: We currently don't do chaining, so only the CNAME itself should be
  412. * returned.
  413. */
  414. TEST_F(QueryTest, CNAME) {
  415. Query(memory_datasrc, Name("cname.example.com"), RRType::A(),
  416. response).process();
  417. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 0, 0,
  418. cname_txt, NULL, NULL);
  419. }
  420. TEST_F(QueryTest, explicitCNAME) {
  421. // same owner name as the CNAME test but explicitly query for CNAME RR.
  422. // expect the same response as we don't provide a full chain yet.
  423. Query(memory_datasrc, Name("cname.example.com"), RRType::CNAME(),
  424. response).process();
  425. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 3, 3,
  426. cname_txt, zone_ns_txt, ns_addrs_txt);
  427. }
  428. TEST_F(QueryTest, CNAME_NX_RRSET) {
  429. // Leads to www.example.com, it doesn't have TXT
  430. // note: with chaining, what should be expected is not trivial:
  431. // BIND 9 returns the CNAME in answer and SOA in authority, no additional.
  432. // NSD returns the CNAME, NS in authority, A/AAAA for NS in additional.
  433. Query(memory_datasrc, Name("cname.example.com"), RRType::TXT(),
  434. response).process();
  435. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 0, 0,
  436. cname_txt, NULL, NULL);
  437. }
  438. TEST_F(QueryTest, explicitCNAME_NX_RRSET) {
  439. // same owner name as the NXRRSET test but explicitly query for CNAME RR.
  440. Query(memory_datasrc, Name("cname.example.com"), RRType::CNAME(),
  441. response).process();
  442. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 3, 3,
  443. cname_txt, zone_ns_txt, ns_addrs_txt);
  444. }
  445. TEST_F(QueryTest, CNAME_NX_DOMAIN) {
  446. // Leads to nxdomain.example.com
  447. // note: with chaining, what should be expected is not trivial:
  448. // BIND 9 returns the CNAME in answer and SOA in authority, no additional,
  449. // RCODE being NXDOMAIN.
  450. // NSD returns the CNAME, NS in authority, A/AAAA for NS in additional,
  451. // RCODE being NOERROR.
  452. Query(memory_datasrc, Name("cnamenxdom.example.com"), RRType::A(),
  453. response).process();
  454. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 0, 0,
  455. cname_nxdom_txt, NULL, NULL);
  456. }
  457. TEST_F(QueryTest, explicitCNAME_NX_DOMAIN) {
  458. // same owner name as the NXDOMAIN test but explicitly query for CNAME RR.
  459. Query(memory_datasrc, Name("cnamenxdom.example.com"), RRType::CNAME(),
  460. response).process();
  461. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 3, 3,
  462. cname_nxdom_txt, zone_ns_txt, ns_addrs_txt);
  463. }
  464. TEST_F(QueryTest, CNAME_OUT) {
  465. /*
  466. * This leads out of zone. This should have only the CNAME even
  467. * when we do chaining.
  468. *
  469. * TODO: We should be able to have two zones in the mock data source.
  470. * Then the same test should be done with .org included there and
  471. * see what it does (depends on what we want to do)
  472. */
  473. Query(memory_datasrc, Name("cnameout.example.com"), RRType::A(),
  474. response).process();
  475. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 0, 0,
  476. cname_out_txt, NULL, NULL);
  477. }
  478. TEST_F(QueryTest, explicitCNAME_OUT) {
  479. // same owner name as the OUT test but explicitly query for CNAME RR.
  480. Query(memory_datasrc, Name("cnameout.example.com"), RRType::CNAME(),
  481. response).process();
  482. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 3, 3,
  483. cname_out_txt, zone_ns_txt, ns_addrs_txt);
  484. }
  485. /*
  486. * Test a query under a domain with DNAME. We should get a synthetized CNAME
  487. * as well as the DNAME.
  488. *
  489. * TODO: Once we have CNAME chaining, check it works with synthetized CNAMEs
  490. * as well. This includes tests pointing inside the zone, outside the zone,
  491. * pointing to NXRRSET and NXDOMAIN cases (similarly as with CNAME).
  492. */
  493. TEST_F(QueryTest, DNAME) {
  494. Query(memory_datasrc, Name("www.dname.example.com"), RRType::A(),
  495. response).process();
  496. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 2, 0, 0,
  497. (string(dname_txt) + synthetized_cname_txt).c_str(),
  498. NULL, NULL);
  499. }
  500. /*
  501. * Ask an ANY query below a DNAME. Should return the DNAME and synthetized
  502. * CNAME.
  503. *
  504. * ANY is handled specially sometimes. We check it is not the case with
  505. * DNAME.
  506. */
  507. TEST_F(QueryTest, DNAME_ANY) {
  508. Query(memory_datasrc, Name("www.dname.example.com"), RRType::ANY(),
  509. response).process();
  510. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 2, 0, 0,
  511. (string(dname_txt) + synthetized_cname_txt).c_str(), NULL, NULL);
  512. }
  513. // Test when we ask for DNAME explicitly, it does no synthetizing.
  514. TEST_F(QueryTest, explicitDNAME) {
  515. Query(memory_datasrc, Name("dname.example.com"), RRType::DNAME(),
  516. response).process();
  517. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 3, 3,
  518. dname_txt, zone_ns_txt, ns_addrs_txt);
  519. }
  520. /*
  521. * Request a RRset at the domain with DNAME. It should not synthetize
  522. * the CNAME, it should return the RRset.
  523. */
  524. TEST_F(QueryTest, DNAME_A) {
  525. Query(memory_datasrc, Name("dname.example.com"), RRType::A(),
  526. response).process();
  527. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 3, 3,
  528. dname_a_txt, zone_ns_txt, ns_addrs_txt);
  529. }
  530. /*
  531. * Request a RRset at the domain with DNAME that is not there (NXRRSET).
  532. * It should not synthetize the CNAME.
  533. */
  534. TEST_F(QueryTest, DNAME_NX_RRSET) {
  535. EXPECT_NO_THROW(Query(memory_datasrc, Name("dname.example.com"),
  536. RRType::TXT(), response).process());
  537. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 0, 1, 0,
  538. NULL, soa_txt, NULL, mock_zone->getOrigin());
  539. }
  540. /*
  541. * Constructing the CNAME will result in a name that is too long. This,
  542. * however, should not throw (and crash the server), but respond with
  543. * YXDOMAIN.
  544. */
  545. TEST_F(QueryTest, LongDNAME) {
  546. // A name that is as long as it can be
  547. Name longname(
  548. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  549. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  550. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  551. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  552. "dname.example.com.");
  553. EXPECT_NO_THROW(Query(memory_datasrc, longname, RRType::A(),
  554. response).process());
  555. responseCheck(response, Rcode::YXDOMAIN(), AA_FLAG, 1, 0, 0,
  556. dname_txt, NULL, NULL);
  557. }
  558. }