memory_datasrc_unittest.cc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  2. // Copyright (C) 2011 CZ NIC
  3. //
  4. // Permission to use, copy, modify, and/or distribute this software for any
  5. // purpose with or without fee is hereby granted, provided that the above
  6. // copyright notice and this permission notice appear in all copies.
  7. //
  8. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  9. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  10. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  12. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  13. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  14. // PERFORMANCE OF THIS SOFTWARE.
  15. #include <exceptions/exceptions.h>
  16. #include <dns/name.h>
  17. #include <dns/rdata.h>
  18. #include <dns/rdataclass.h>
  19. #include <dns/rrclass.h>
  20. #include <dns/rrsetlist.h>
  21. #include <dns/rrttl.h>
  22. #include <dns/masterload.h>
  23. #include <datasrc/memory_datasrc.h>
  24. #include <gtest/gtest.h>
  25. using namespace isc::dns;
  26. using namespace isc::dns::rdata;
  27. using namespace isc::datasrc;
  28. namespace {
  29. // Commonly used result codes (Who should write the prefix all the time)
  30. using result::SUCCESS;
  31. using result::EXIST;
  32. class MemoryDataSrcTest : public ::testing::Test {
  33. protected:
  34. MemoryDataSrcTest() : rrclass(RRClass::IN())
  35. {}
  36. RRClass rrclass;
  37. MemoryDataSrc memory_datasrc;
  38. };
  39. TEST_F(MemoryDataSrcTest, add_find_Zone) {
  40. // test add zone
  41. // Bogus zone (NULL)
  42. EXPECT_THROW(memory_datasrc.addZone(ZonePtr()), isc::InvalidParameter);
  43. // add zones with different names one by one
  44. EXPECT_EQ(result::SUCCESS, memory_datasrc.addZone(
  45. ZonePtr(new MemoryZone(RRClass::IN(), Name("a")))));
  46. EXPECT_EQ(result::SUCCESS, memory_datasrc.addZone(
  47. ZonePtr(new MemoryZone(RRClass::CH(), Name("b")))));
  48. EXPECT_EQ(result::SUCCESS, memory_datasrc.addZone(
  49. ZonePtr(new MemoryZone(RRClass::IN(), Name("c")))));
  50. // add zones with the same name suffix
  51. EXPECT_EQ(result::SUCCESS, memory_datasrc.addZone(
  52. ZonePtr(new MemoryZone(RRClass::CH(),
  53. Name("x.d.e.f")))));
  54. EXPECT_EQ(result::SUCCESS, memory_datasrc.addZone(
  55. ZonePtr(new MemoryZone(RRClass::CH(),
  56. Name("o.w.y.d.e.f")))));
  57. EXPECT_EQ(result::SUCCESS, memory_datasrc.addZone(
  58. ZonePtr(new MemoryZone(RRClass::CH(),
  59. Name("p.w.y.d.e.f")))));
  60. EXPECT_EQ(result::SUCCESS, memory_datasrc.addZone(
  61. ZonePtr(new MemoryZone(RRClass::IN(),
  62. Name("q.w.y.d.e.f")))));
  63. // add super zone and its subzone
  64. EXPECT_EQ(result::SUCCESS, memory_datasrc.addZone(
  65. ZonePtr(new MemoryZone(RRClass::CH(), Name("g.h")))));
  66. EXPECT_EQ(result::SUCCESS, memory_datasrc.addZone(
  67. ZonePtr(new MemoryZone(RRClass::IN(), Name("i.g.h")))));
  68. EXPECT_EQ(result::SUCCESS, memory_datasrc.addZone(
  69. ZonePtr(new MemoryZone(RRClass::IN(),
  70. Name("z.d.e.f")))));
  71. EXPECT_EQ(result::SUCCESS, memory_datasrc.addZone(
  72. ZonePtr(new MemoryZone(RRClass::IN(),
  73. Name("j.z.d.e.f")))));
  74. // different zone class isn't allowed.
  75. EXPECT_EQ(result::EXIST, memory_datasrc.addZone(
  76. ZonePtr(new MemoryZone(RRClass::CH(),
  77. Name("q.w.y.d.e.f")))));
  78. // names are compared in a case insensitive manner.
  79. EXPECT_EQ(result::EXIST, memory_datasrc.addZone(
  80. ZonePtr(new MemoryZone(RRClass::IN(),
  81. Name("Q.W.Y.d.E.f")))));
  82. // test find zone
  83. EXPECT_EQ(result::SUCCESS, memory_datasrc.findZone(Name("a")).code);
  84. EXPECT_EQ(Name("a"),
  85. memory_datasrc.findZone(Name("a")).zone->getOrigin());
  86. EXPECT_EQ(result::SUCCESS,
  87. memory_datasrc.findZone(Name("j.z.d.e.f")).code);
  88. EXPECT_EQ(Name("j.z.d.e.f"),
  89. memory_datasrc.findZone(Name("j.z.d.e.f")).zone->getOrigin());
  90. // NOTFOUND
  91. EXPECT_EQ(result::NOTFOUND, memory_datasrc.findZone(Name("d.e.f")).code);
  92. EXPECT_EQ(ConstZonePtr(), memory_datasrc.findZone(Name("d.e.f")).zone);
  93. EXPECT_EQ(result::NOTFOUND,
  94. memory_datasrc.findZone(Name("w.y.d.e.f")).code);
  95. EXPECT_EQ(ConstZonePtr(),
  96. memory_datasrc.findZone(Name("w.y.d.e.f")).zone);
  97. // there's no exact match. the result should be the longest match,
  98. // and the code should be PARTIALMATCH.
  99. EXPECT_EQ(result::PARTIALMATCH,
  100. memory_datasrc.findZone(Name("j.g.h")).code);
  101. EXPECT_EQ(Name("g.h"),
  102. memory_datasrc.findZone(Name("g.h")).zone->getOrigin());
  103. EXPECT_EQ(result::PARTIALMATCH,
  104. memory_datasrc.findZone(Name("z.i.g.h")).code);
  105. EXPECT_EQ(Name("i.g.h"),
  106. memory_datasrc.findZone(Name("z.i.g.h")).zone->getOrigin());
  107. }
  108. TEST_F(MemoryDataSrcTest, getZoneCount) {
  109. EXPECT_EQ(0, memory_datasrc.getZoneCount());
  110. memory_datasrc.addZone(
  111. ZonePtr(new MemoryZone(rrclass, Name("example.com"))));
  112. EXPECT_EQ(1, memory_datasrc.getZoneCount());
  113. // duplicate add. counter shouldn't change
  114. memory_datasrc.addZone(
  115. ZonePtr(new MemoryZone(rrclass, Name("example.com"))));
  116. EXPECT_EQ(1, memory_datasrc.getZoneCount());
  117. // add one more
  118. memory_datasrc.addZone(
  119. ZonePtr(new MemoryZone(rrclass, Name("example.org"))));
  120. EXPECT_EQ(2, memory_datasrc.getZoneCount());
  121. }
  122. /// \brief Test fixture for the MemoryZone class
  123. class MemoryZoneTest : public ::testing::Test {
  124. public:
  125. MemoryZoneTest() :
  126. class_(RRClass::IN()),
  127. origin_("example.org"),
  128. ns_name_("ns.example.org"),
  129. cname_name_("cname.example.org"),
  130. dname_name_("dname.example.org"),
  131. child_ns_name_("child.example.org"),
  132. child_glue_name_("ns.child.example.org"),
  133. grandchild_ns_name_("grand.child.example.org"),
  134. grandchild_glue_name_("ns.grand.child.example.org"),
  135. zone_(class_, origin_),
  136. rr_out_(new RRset(Name("example.com"), class_, RRType::A(),
  137. RRTTL(300))),
  138. rr_ns_(new RRset(origin_, class_, RRType::NS(), RRTTL(300))),
  139. rr_ns_a_(new RRset(ns_name_, class_, RRType::A(), RRTTL(300))),
  140. rr_ns_aaaa_(new RRset(ns_name_, class_, RRType::AAAA(), RRTTL(300))),
  141. rr_a_(new RRset(origin_, class_, RRType::A(), RRTTL(300))),
  142. rr_cname_(new RRset(cname_name_, class_, RRType::CNAME(), RRTTL(300))),
  143. rr_cname_a_(new RRset(cname_name_, class_, RRType::A(), RRTTL(300))),
  144. rr_dname_(new RRset(dname_name_, class_, RRType::DNAME(), RRTTL(300))),
  145. rr_dname_a_(new RRset(dname_name_, class_, RRType::A(),
  146. RRTTL(300))),
  147. rr_dname_ns_(new RRset(dname_name_, class_, RRType::NS(), RRTTL(300))),
  148. rr_dname_apex_(new RRset(origin_, class_, RRType::DNAME(),
  149. RRTTL(300))),
  150. rr_child_ns_(new RRset(child_ns_name_, class_, RRType::NS(),
  151. RRTTL(300))),
  152. rr_child_glue_(new RRset(child_glue_name_, class_, RRType::A(),
  153. RRTTL(300))),
  154. rr_grandchild_ns_(new RRset(grandchild_ns_name_, class_, RRType::NS(),
  155. RRTTL(300))),
  156. rr_grandchild_glue_(new RRset(grandchild_glue_name_, class_,
  157. RRType::AAAA(), RRTTL(300)))
  158. {
  159. }
  160. // Some data to test with
  161. const RRClass class_;
  162. const Name origin_, ns_name_, cname_name_, dname_name_, child_ns_name_,
  163. child_glue_name_, grandchild_ns_name_, grandchild_glue_name_;
  164. // The zone to torture by tests
  165. MemoryZone zone_;
  166. /*
  167. * Some RRsets to put inside the zone.
  168. * They are empty, but the MemoryZone does not have a reason to look
  169. * inside anyway. We will check it finds them and does not change
  170. * the pointer.
  171. */
  172. ConstRRsetPtr
  173. // Out of zone RRset
  174. rr_out_,
  175. // NS of example.org
  176. rr_ns_,
  177. // A of ns.example.org
  178. rr_ns_a_,
  179. // AAAA of ns.example.org
  180. rr_ns_aaaa_,
  181. // A of example.org
  182. rr_a_;
  183. RRsetPtr rr_cname_; // CNAME in example.org (RDATA will be added)
  184. ConstRRsetPtr rr_cname_a_; // for mixed CNAME + A case
  185. RRsetPtr rr_dname_; // DNAME in example.org (RDATA will be added)
  186. ConstRRsetPtr rr_dname_a_; // for mixed DNAME + A case
  187. ConstRRsetPtr rr_dname_ns_; // for mixed DNAME + NS case
  188. ConstRRsetPtr rr_dname_apex_; // for mixed DNAME + NS case in the apex
  189. ConstRRsetPtr rr_child_ns_; // NS of a child domain (for delegation)
  190. ConstRRsetPtr rr_child_glue_; // glue RR of the child domain
  191. ConstRRsetPtr rr_grandchild_ns_; // NS below a zone cut (unusual)
  192. ConstRRsetPtr rr_grandchild_glue_; // glue RR below a deeper zone cut
  193. /**
  194. * \brief Test one find query to the zone.
  195. *
  196. * Asks a query to the zone and checks it does not throw and returns
  197. * expected results. It returns nothing, it just signals failures
  198. * to GTEST.
  199. *
  200. * \param name The name to ask for.
  201. * \param rrtype The RRType to ask of.
  202. * \param result The expected code of the result.
  203. * \param check_answer Should a check against equality of the answer be
  204. * done?
  205. * \param answer The expected rrset, if any should be returned.
  206. * \param zone Check different MemoryZone object than zone_ (if NULL,
  207. * uses zone_)
  208. */
  209. void findTest(const Name& name, const RRType& rrtype, Zone::Result result,
  210. bool check_answer = true,
  211. const ConstRRsetPtr& answer = ConstRRsetPtr(),
  212. RRsetList* target = NULL,
  213. MemoryZone* zone = NULL,
  214. Zone::FindOptions options = Zone::FIND_DEFAULT)
  215. {
  216. if (!zone) {
  217. zone = &zone_;
  218. }
  219. // The whole block is inside, because we need to check the result and
  220. // we can't assign to FindResult
  221. EXPECT_NO_THROW({
  222. Zone::FindResult find_result(zone->find(name, rrtype, target,
  223. options));
  224. // Check it returns correct answers
  225. EXPECT_EQ(result, find_result.code);
  226. if (check_answer) {
  227. EXPECT_EQ(answer, find_result.rrset);
  228. }
  229. });
  230. }
  231. };
  232. /**
  233. * \brief Test MemoryZone::MemoryZone constructor.
  234. *
  235. * Takes the created zone and checks its properties they are the same
  236. * as passed parameters.
  237. */
  238. TEST_F(MemoryZoneTest, constructor) {
  239. ASSERT_EQ(class_, zone_.getClass());
  240. ASSERT_EQ(origin_, zone_.getOrigin());
  241. }
  242. /**
  243. * \brief Test adding.
  244. *
  245. * We test that it throws at the correct moments and the correct exceptions.
  246. * And we test the return value.
  247. */
  248. TEST_F(MemoryZoneTest, add) {
  249. // This one does not belong to this zone
  250. EXPECT_THROW(zone_.add(rr_out_), MemoryZone::OutOfZone);
  251. // Test null pointer
  252. EXPECT_THROW(zone_.add(ConstRRsetPtr()), MemoryZone::NullRRset);
  253. // Now put all the data we have there. It should throw nothing
  254. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_ns_)));
  255. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_ns_a_)));
  256. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_ns_aaaa_)));
  257. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_a_)));
  258. // Try putting there something twice, it should be rejected
  259. EXPECT_NO_THROW(EXPECT_EQ(EXIST, zone_.add(rr_ns_)));
  260. EXPECT_NO_THROW(EXPECT_EQ(EXIST, zone_.add(rr_ns_a_)));
  261. }
  262. TEST_F(MemoryZoneTest, addMultipleCNAMEs) {
  263. rr_cname_->addRdata(generic::CNAME("canonical1.example.org."));
  264. rr_cname_->addRdata(generic::CNAME("canonical2.example.org."));
  265. EXPECT_THROW(zone_.add(rr_cname_), MemoryZone::AddError);
  266. }
  267. TEST_F(MemoryZoneTest, addCNAMEThenOther) {
  268. rr_cname_->addRdata(generic::CNAME("canonical.example.org."));
  269. EXPECT_EQ(SUCCESS, zone_.add(rr_cname_));
  270. EXPECT_THROW(zone_.add(rr_cname_a_), MemoryZone::AddError);
  271. }
  272. TEST_F(MemoryZoneTest, addOtherThenCNAME) {
  273. rr_cname_->addRdata(generic::CNAME("canonical.example.org."));
  274. EXPECT_EQ(SUCCESS, zone_.add(rr_cname_a_));
  275. EXPECT_THROW(zone_.add(rr_cname_), MemoryZone::AddError);
  276. }
  277. TEST_F(MemoryZoneTest, findCNAME) {
  278. // install CNAME RR
  279. rr_cname_->addRdata(generic::CNAME("canonical.example.org."));
  280. EXPECT_EQ(SUCCESS, zone_.add(rr_cname_));
  281. // Find A RR of the same. Should match the CNAME
  282. findTest(cname_name_, RRType::NS(), Zone::CNAME, true, rr_cname_);
  283. // Find the CNAME itself. Should result in normal SUCCESS
  284. findTest(cname_name_, RRType::CNAME(), Zone::SUCCESS, true, rr_cname_);
  285. }
  286. TEST_F(MemoryZoneTest, findCNAMEUnderZoneCut) {
  287. // There's nothing special when we find a CNAME under a zone cut
  288. // (with FIND_GLUE_OK). The behavior is different from BIND 9,
  289. // so we test this case explicitly.
  290. EXPECT_EQ(SUCCESS, zone_.add(rr_child_ns_));
  291. RRsetPtr rr_cname_under_cut_(new RRset(Name("cname.child.example.org"),
  292. class_, RRType::CNAME(),
  293. RRTTL(300)));
  294. EXPECT_EQ(SUCCESS, zone_.add(rr_cname_under_cut_));
  295. findTest(Name("cname.child.example.org"), RRType::AAAA(),
  296. Zone::CNAME, true, rr_cname_under_cut_, NULL, NULL,
  297. Zone::FIND_GLUE_OK);
  298. }
  299. // Two DNAMEs at single domain are disallowed by RFC 2672, section 3)
  300. // Having a CNAME there is disallowed too, but it is tested by
  301. // addOtherThenCNAME and addCNAMEThenOther.
  302. TEST_F(MemoryZoneTest, addMultipleDNAMEs) {
  303. rr_dname_->addRdata(generic::DNAME("dname1.example.org."));
  304. rr_dname_->addRdata(generic::DNAME("dname2.example.org."));
  305. EXPECT_THROW(zone_.add(rr_dname_), MemoryZone::AddError);
  306. }
  307. /*
  308. * These two tests ensure that we can't have DNAME and NS at the same
  309. * node with the exception of the apex of zone (forbidden by RFC 2672)
  310. */
  311. TEST_F(MemoryZoneTest, addDNAMEThenNS) {
  312. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_dname_)));
  313. EXPECT_THROW(zone_.add(rr_dname_ns_), MemoryZone::AddError);
  314. }
  315. TEST_F(MemoryZoneTest, addNSThenDNAME) {
  316. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_dname_ns_)));
  317. EXPECT_THROW(zone_.add(rr_dname_), MemoryZone::AddError);
  318. }
  319. // It is allowed to have NS and DNAME at apex
  320. TEST_F(MemoryZoneTest, DNAMEAndNSAtApex) {
  321. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_dname_apex_)));
  322. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_ns_)));
  323. // The NS should be possible to be found, below should be DNAME, not
  324. // delegation
  325. findTest(origin_, RRType::NS(), Zone::SUCCESS, true, rr_ns_);
  326. findTest(child_ns_name_, RRType::A(), Zone::DNAME, true, rr_dname_apex_);
  327. }
  328. TEST_F(MemoryZoneTest, NSAndDNAMEAtApex) {
  329. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_ns_)));
  330. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_dname_apex_)));
  331. }
  332. // TODO: Test (and implement) adding data under DNAME. That is forbidden by
  333. // 2672 as well.
  334. // Search under a DNAME record. It should return the DNAME
  335. TEST_F(MemoryZoneTest, findBelowDNAME) {
  336. rr_dname_->addRdata(generic::DNAME("target.example.org."));
  337. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_dname_)));
  338. findTest(Name("below.dname.example.org"), RRType::A(), Zone::DNAME, true,
  339. rr_dname_);
  340. }
  341. // Search at the domain with DNAME. It should act as DNAME isn't there, DNAME
  342. // influences only the data below (see RFC 2672, section 3)
  343. TEST_F(MemoryZoneTest, findAtDNAME) {
  344. rr_dname_->addRdata(generic::DNAME("target.example.org."));
  345. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_dname_)));
  346. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_dname_a_)));
  347. findTest(dname_name_, RRType::A(), Zone::SUCCESS, true, rr_dname_a_);
  348. findTest(dname_name_, RRType::DNAME(), Zone::SUCCESS, true, rr_dname_);
  349. findTest(dname_name_, RRType::TXT(), Zone::NXRRSET, true);
  350. }
  351. // Test adding child zones and zone cut handling
  352. TEST_F(MemoryZoneTest, delegationNS) {
  353. // add in-zone data
  354. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_ns_)));
  355. // install a zone cut
  356. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_child_ns_)));
  357. // below the zone cut
  358. findTest(Name("www.child.example.org"), RRType::A(), Zone::DELEGATION,
  359. true, rr_child_ns_);
  360. // at the zone cut
  361. findTest(Name("child.example.org"), RRType::A(), Zone::DELEGATION,
  362. true, rr_child_ns_);
  363. findTest(Name("child.example.org"), RRType::NS(), Zone::DELEGATION,
  364. true, rr_child_ns_);
  365. // finding NS for the apex (origin) node. This must not be confused
  366. // with delegation due to the existence of an NS RR.
  367. findTest(origin_, RRType::NS(), Zone::SUCCESS, true, rr_ns_);
  368. // unusual case of "nested delegation": the highest cut should be used.
  369. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_grandchild_ns_)));
  370. findTest(Name("www.grand.child.example.org"), RRType::A(),
  371. Zone::DELEGATION, true, rr_child_ns_); // note: !rr_grandchild_ns_
  372. }
  373. TEST_F(MemoryZoneTest, findAny) {
  374. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_a_)));
  375. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_ns_)));
  376. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_child_glue_)));
  377. // origin
  378. RRsetList origin_rrsets;
  379. findTest(origin_, RRType::ANY(), Zone::SUCCESS, true,
  380. ConstRRsetPtr(), &origin_rrsets);
  381. EXPECT_EQ(2, origin_rrsets.size());
  382. EXPECT_EQ(rr_a_, origin_rrsets.findRRset(RRType::A(), RRClass::IN()));
  383. EXPECT_EQ(rr_ns_, origin_rrsets.findRRset(RRType::NS(), RRClass::IN()));
  384. // out zone name
  385. RRsetList out_rrsets;
  386. findTest(Name("example.com"), RRType::ANY(), Zone::NXDOMAIN, true,
  387. ConstRRsetPtr(), &out_rrsets);
  388. EXPECT_EQ(0, out_rrsets.size());
  389. RRsetList glue_child_rrsets;
  390. findTest(child_glue_name_, RRType::ANY(), Zone::SUCCESS, true,
  391. ConstRRsetPtr(), &glue_child_rrsets);
  392. EXPECT_EQ(rr_child_glue_, glue_child_rrsets.findRRset(RRType::A(),
  393. RRClass::IN()));
  394. EXPECT_EQ(1, glue_child_rrsets.size());
  395. // TODO: test NXRRSET case after rbtree non-terminal logic has
  396. // been implemented
  397. // add zone cut
  398. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_child_ns_)));
  399. // zone cut
  400. RRsetList child_rrsets;
  401. findTest(child_ns_name_, RRType::ANY(), Zone::DELEGATION, true,
  402. rr_child_ns_, &child_rrsets);
  403. EXPECT_EQ(0, child_rrsets.size());
  404. // glue for this zone cut
  405. RRsetList new_glue_child_rrsets;
  406. findTest(child_glue_name_, RRType::ANY(), Zone::DELEGATION, true,
  407. rr_child_ns_, &new_glue_child_rrsets);
  408. EXPECT_EQ(0, new_glue_child_rrsets.size());
  409. }
  410. TEST_F(MemoryZoneTest, glue) {
  411. // install zone data:
  412. // a zone cut
  413. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_child_ns_)));
  414. // glue for this cut
  415. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_child_glue_)));
  416. // a nested zone cut (unusual)
  417. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_grandchild_ns_)));
  418. // glue under the deeper zone cut
  419. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_grandchild_glue_)));
  420. // by default glue is hidden due to the zone cut
  421. findTest(child_glue_name_, RRType::A(), Zone::DELEGATION, true,
  422. rr_child_ns_);
  423. // If we do it in the "glue OK" mode, we should find the exact match.
  424. findTest(child_glue_name_, RRType::A(), Zone::SUCCESS, true,
  425. rr_child_glue_, NULL, NULL, Zone::FIND_GLUE_OK);
  426. // glue OK + NXRRSET case
  427. findTest(child_glue_name_, RRType::AAAA(), Zone::NXRRSET, true,
  428. ConstRRsetPtr(), NULL, NULL, Zone::FIND_GLUE_OK);
  429. // glue OK + NXDOMAIN case
  430. findTest(Name("www.child.example.org"), RRType::A(), Zone::DELEGATION,
  431. true, rr_child_ns_, NULL, NULL, Zone::FIND_GLUE_OK);
  432. // TODO:
  433. // glue name would match a wildcard under a zone cut: wildcard match
  434. // shouldn't happen under a cut and result must be PARTIALMATCH
  435. // (This case cannot be tested yet)
  436. // nested cut case. The glue should be found.
  437. findTest(grandchild_glue_name_, RRType::AAAA(), Zone::SUCCESS,
  438. true, rr_grandchild_glue_, NULL, NULL, Zone::FIND_GLUE_OK);
  439. // A non-existent name in nested cut. This should result in delegation
  440. // at the highest zone cut.
  441. findTest(Name("www.grand.child.example.org"), RRType::TXT(),
  442. Zone::DELEGATION, true, rr_child_ns_, NULL, NULL,
  443. Zone::FIND_GLUE_OK);
  444. }
  445. /**
  446. * \brief Test searching.
  447. *
  448. * Check it finds or does not find correctly and does not throw exceptions.
  449. * \todo This doesn't do any kind of CNAME and so on. If it isn't
  450. * directly there, it just tells it doesn't exist.
  451. */
  452. TEST_F(MemoryZoneTest, find) {
  453. // Fill some data inside
  454. // Now put all the data we have there. It should throw nothing
  455. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_ns_)));
  456. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_ns_a_)));
  457. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_ns_aaaa_)));
  458. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_a_)));
  459. // These two should be successful
  460. findTest(origin_, RRType::NS(), Zone::SUCCESS, true, rr_ns_);
  461. findTest(ns_name_, RRType::A(), Zone::SUCCESS, true, rr_ns_a_);
  462. // These domain exist but don't have the provided RRType
  463. findTest(origin_, RRType::AAAA(), Zone::NXRRSET);
  464. findTest(ns_name_, RRType::NS(), Zone::NXRRSET);
  465. // These domains don't exist (and one is out of the zone)
  466. findTest(Name("nothere.example.org"), RRType::A(), Zone::NXDOMAIN);
  467. findTest(Name("example.net"), RRType::A(), Zone::NXDOMAIN);
  468. }
  469. TEST_F(MemoryZoneTest, load) {
  470. // Put some data inside the zone
  471. EXPECT_NO_THROW(EXPECT_EQ(result::SUCCESS, zone_.add(rr_ns_)));
  472. // Loading with different origin should fail
  473. EXPECT_THROW(zone_.load(TEST_DATA_DIR "/root.zone"), MasterLoadError);
  474. // See the original data is still there, survived the exception
  475. findTest(origin_, RRType::NS(), Zone::SUCCESS, true, rr_ns_);
  476. // Create correct zone
  477. MemoryZone rootzone(class_, Name("."));
  478. // Try putting something inside
  479. EXPECT_NO_THROW(EXPECT_EQ(result::SUCCESS, rootzone.add(rr_ns_aaaa_)));
  480. // Load the zone. It should overwrite/remove the above RRset
  481. EXPECT_NO_THROW(rootzone.load(TEST_DATA_DIR "/root.zone"));
  482. // Now see there are some rrsets (we don't look inside, though)
  483. findTest(Name("."), RRType::SOA(), Zone::SUCCESS, false, ConstRRsetPtr(),
  484. NULL, &rootzone);
  485. findTest(Name("."), RRType::NS(), Zone::SUCCESS, false, ConstRRsetPtr(),
  486. NULL, &rootzone);
  487. findTest(Name("a.root-servers.net."), RRType::A(), Zone::SUCCESS, false,
  488. ConstRRsetPtr(), NULL, &rootzone);
  489. // But this should no longer be here
  490. findTest(ns_name_, RRType::AAAA(), Zone::NXDOMAIN, true, ConstRRsetPtr(),
  491. NULL, &rootzone);
  492. // Try loading zone that is wrong in a different way
  493. EXPECT_THROW(zone_.load(TEST_DATA_DIR "/duplicate_rrset.zone"),
  494. MasterLoadError);
  495. }
  496. TEST_F(MemoryZoneTest, swap) {
  497. // build one zone with some data
  498. MemoryZone zone1(class_, origin_);
  499. EXPECT_EQ(result::SUCCESS, zone1.add(rr_ns_));
  500. EXPECT_EQ(result::SUCCESS, zone1.add(rr_ns_aaaa_));
  501. // build another zone of a different RR class with some other data
  502. const Name other_origin("version.bind");
  503. ASSERT_NE(origin_, other_origin); // make sure these two are different
  504. MemoryZone zone2(RRClass::CH(), other_origin);
  505. EXPECT_EQ(result::SUCCESS,
  506. zone2.add(RRsetPtr(new RRset(Name("version.bind"),
  507. RRClass::CH(), RRType::TXT(),
  508. RRTTL(0)))));
  509. zone1.swap(zone2);
  510. EXPECT_EQ(other_origin, zone1.getOrigin());
  511. EXPECT_EQ(origin_, zone2.getOrigin());
  512. EXPECT_EQ(RRClass::CH(), zone1.getClass());
  513. EXPECT_EQ(RRClass::IN(), zone2.getClass());
  514. // make sure the zone data is swapped, too
  515. findTest(origin_, RRType::NS(), Zone::NXDOMAIN, false, ConstRRsetPtr(),
  516. NULL, &zone1);
  517. findTest(other_origin, RRType::TXT(), Zone::SUCCESS, false,
  518. ConstRRsetPtr(), NULL, &zone1);
  519. findTest(origin_, RRType::NS(), Zone::SUCCESS, false, ConstRRsetPtr(),
  520. NULL, &zone2);
  521. findTest(other_origin, RRType::TXT(), Zone::NXDOMAIN, false,
  522. ConstRRsetPtr(), NULL, &zone2);
  523. }
  524. TEST_F(MemoryZoneTest, getFileName) {
  525. // for an empty zone the file name should also be empty.
  526. EXPECT_TRUE(zone_.getFileName().empty());
  527. // if loading a zone fails the file name shouldn't be set.
  528. EXPECT_THROW(zone_.load(TEST_DATA_DIR "/root.zone"), MasterLoadError);
  529. EXPECT_TRUE(zone_.getFileName().empty());
  530. // after a successful load, the specified file name should be set
  531. MemoryZone rootzone(class_, Name("."));
  532. EXPECT_NO_THROW(rootzone.load(TEST_DATA_DIR "/root.zone"));
  533. EXPECT_EQ(TEST_DATA_DIR "/root.zone", rootzone.getFileName());
  534. // overriding load, which will fail
  535. EXPECT_THROW(rootzone.load(TEST_DATA_DIR "/duplicate_rrset.zone"),
  536. MasterLoadError);
  537. // the file name should be intact.
  538. EXPECT_EQ(TEST_DATA_DIR "/root.zone", rootzone.getFileName());
  539. // After swap, file names should also be swapped.
  540. zone_.swap(rootzone);
  541. EXPECT_EQ(TEST_DATA_DIR "/root.zone", zone_.getFileName());
  542. EXPECT_TRUE(rootzone.getFileName().empty());
  543. }
  544. }