memory_datasrc_unittest.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 <exceptions/exceptions.h>
  15. #include <dns/name.h>
  16. #include <dns/rrclass.h>
  17. #include <dns/rrttl.h>
  18. #include <dns/masterload.h>
  19. #include <datasrc/memory_datasrc.h>
  20. #include <gtest/gtest.h>
  21. using namespace isc::dns;
  22. using namespace isc::datasrc;
  23. namespace {
  24. class MemoryDataSrcTest : public ::testing::Test {
  25. protected:
  26. MemoryDataSrcTest() : rrclass(RRClass::IN())
  27. {}
  28. RRClass rrclass;
  29. MemoryDataSrc memory_datasrc;
  30. };
  31. TEST_F(MemoryDataSrcTest, add_find_Zone) {
  32. // test add zone
  33. // Bogus zone (NULL)
  34. EXPECT_THROW(memory_datasrc.addZone(ZonePtr()), isc::InvalidParameter);
  35. // add zones with different names one by one
  36. EXPECT_EQ(result::SUCCESS, memory_datasrc.addZone(
  37. ZonePtr(new MemoryZone(RRClass::IN(), Name("a")))));
  38. EXPECT_EQ(result::SUCCESS, memory_datasrc.addZone(
  39. ZonePtr(new MemoryZone(RRClass::CH(), Name("b")))));
  40. EXPECT_EQ(result::SUCCESS, memory_datasrc.addZone(
  41. ZonePtr(new MemoryZone(RRClass::IN(), Name("c")))));
  42. // add zones with the same name suffix
  43. EXPECT_EQ(result::SUCCESS, memory_datasrc.addZone(
  44. ZonePtr(new MemoryZone(RRClass::CH(),
  45. Name("x.d.e.f")))));
  46. EXPECT_EQ(result::SUCCESS, memory_datasrc.addZone(
  47. ZonePtr(new MemoryZone(RRClass::CH(),
  48. Name("o.w.y.d.e.f")))));
  49. EXPECT_EQ(result::SUCCESS, memory_datasrc.addZone(
  50. ZonePtr(new MemoryZone(RRClass::CH(),
  51. Name("p.w.y.d.e.f")))));
  52. EXPECT_EQ(result::SUCCESS, memory_datasrc.addZone(
  53. ZonePtr(new MemoryZone(RRClass::IN(),
  54. Name("q.w.y.d.e.f")))));
  55. // add super zone and its subzone
  56. EXPECT_EQ(result::SUCCESS, memory_datasrc.addZone(
  57. ZonePtr(new MemoryZone(RRClass::CH(), Name("g.h")))));
  58. EXPECT_EQ(result::SUCCESS, memory_datasrc.addZone(
  59. ZonePtr(new MemoryZone(RRClass::IN(), Name("i.g.h")))));
  60. EXPECT_EQ(result::SUCCESS, memory_datasrc.addZone(
  61. ZonePtr(new MemoryZone(RRClass::IN(),
  62. Name("z.d.e.f")))));
  63. EXPECT_EQ(result::SUCCESS, memory_datasrc.addZone(
  64. ZonePtr(new MemoryZone(RRClass::IN(),
  65. Name("j.z.d.e.f")))));
  66. // different zone class isn't allowed.
  67. EXPECT_EQ(result::EXIST, memory_datasrc.addZone(
  68. ZonePtr(new MemoryZone(RRClass::CH(),
  69. Name("q.w.y.d.e.f")))));
  70. // names are compared in a case insensitive manner.
  71. EXPECT_EQ(result::EXIST, memory_datasrc.addZone(
  72. ZonePtr(new MemoryZone(RRClass::IN(),
  73. Name("Q.W.Y.d.E.f")))));
  74. // test find zone
  75. EXPECT_EQ(result::SUCCESS, memory_datasrc.findZone(Name("a")).code);
  76. EXPECT_EQ(Name("a"),
  77. memory_datasrc.findZone(Name("a")).zone->getOrigin());
  78. EXPECT_EQ(result::SUCCESS,
  79. memory_datasrc.findZone(Name("j.z.d.e.f")).code);
  80. EXPECT_EQ(Name("j.z.d.e.f"),
  81. memory_datasrc.findZone(Name("j.z.d.e.f")).zone->getOrigin());
  82. // NOTFOUND
  83. EXPECT_EQ(result::NOTFOUND, memory_datasrc.findZone(Name("d.e.f")).code);
  84. EXPECT_EQ(ConstZonePtr(), memory_datasrc.findZone(Name("d.e.f")).zone);
  85. EXPECT_EQ(result::NOTFOUND,
  86. memory_datasrc.findZone(Name("w.y.d.e.f")).code);
  87. EXPECT_EQ(ConstZonePtr(),
  88. memory_datasrc.findZone(Name("w.y.d.e.f")).zone);
  89. // there's no exact match. the result should be the longest match,
  90. // and the code should be PARTIALMATCH.
  91. EXPECT_EQ(result::PARTIALMATCH,
  92. memory_datasrc.findZone(Name("j.g.h")).code);
  93. EXPECT_EQ(Name("g.h"),
  94. memory_datasrc.findZone(Name("g.h")).zone->getOrigin());
  95. EXPECT_EQ(result::PARTIALMATCH,
  96. memory_datasrc.findZone(Name("z.i.g.h")).code);
  97. EXPECT_EQ(Name("i.g.h"),
  98. memory_datasrc.findZone(Name("z.i.g.h")).zone->getOrigin());
  99. }
  100. TEST_F(MemoryDataSrcTest, getZoneCount) {
  101. EXPECT_EQ(0, memory_datasrc.getZoneCount());
  102. memory_datasrc.addZone(
  103. ZonePtr(new MemoryZone(rrclass, Name("example.com"))));
  104. EXPECT_EQ(1, memory_datasrc.getZoneCount());
  105. // duplicate add. counter shouldn't change
  106. memory_datasrc.addZone(
  107. ZonePtr(new MemoryZone(rrclass, Name("example.com"))));
  108. EXPECT_EQ(1, memory_datasrc.getZoneCount());
  109. // add one more
  110. memory_datasrc.addZone(
  111. ZonePtr(new MemoryZone(rrclass, Name("example.org"))));
  112. EXPECT_EQ(2, memory_datasrc.getZoneCount());
  113. }
  114. /// \brief Test fixture for the MemoryZone class
  115. class MemoryZoneTest : public ::testing::Test {
  116. public:
  117. MemoryZoneTest() :
  118. class_(RRClass::IN()),
  119. origin_("example.org"),
  120. ns_name_("ns.example.org"),
  121. zone_(class_, origin_),
  122. rr_out_(new RRset(Name("example.com"), class_, RRType::A(),
  123. RRTTL(300))),
  124. rr_ns_(new RRset(origin_, class_, RRType::NS(), RRTTL(300))),
  125. rr_ns_a_(new RRset(ns_name_, class_, RRType::A(), RRTTL(300))),
  126. rr_ns_aaaa_(new RRset(ns_name_, class_, RRType::AAAA(), RRTTL(300))),
  127. rr_a_(new RRset(origin_, class_, RRType::A(), RRTTL(300)))
  128. {
  129. }
  130. // Some data to test with
  131. RRClass class_;
  132. Name origin_, ns_name_;
  133. // The zone to torture by tests
  134. MemoryZone zone_;
  135. /*
  136. * Some RRsets to put inside the zone.
  137. * They are empty, but the MemoryZone does not have a reason to look
  138. * inside anyway. We will check it finds them and does not change
  139. * the pointer.
  140. */
  141. RRsetPtr
  142. // Out of zone RRset
  143. rr_out_,
  144. // NS of example.org
  145. rr_ns_,
  146. // A of ns.example.org
  147. rr_ns_a_,
  148. // AAAA of ns.example.org
  149. rr_ns_aaaa_,
  150. // A of example.org
  151. rr_a_;
  152. /**
  153. * \brief Test one find query to the zone.
  154. *
  155. * Asks a query to the zone and checks it does not throw and returns
  156. * expected results. It returns nothing, it just signals failures
  157. * to GTEST.
  158. *
  159. * \param name The name to ask for.
  160. * \param rrtype The RRType to ask of.
  161. * \param result The expected code of the result.
  162. * \param check_answer Should a check against equality of the answer be
  163. * done?
  164. * \param answer The expected rrset, if any should be returned.
  165. * \param zone Check different MemoryZone object than zone_ (if NULL,
  166. * uses zone_)
  167. */
  168. void findTest(const Name& name, const RRType& rrtype, Zone::Result result,
  169. bool check_answer = true,
  170. const ConstRRsetPtr& answer = ConstRRsetPtr(), MemoryZone *zone = NULL)
  171. {
  172. if (!zone) {
  173. zone = &zone_;
  174. }
  175. // The whole block is inside, because we need to check the result and
  176. // we can't assign to FindResult
  177. EXPECT_NO_THROW({
  178. Zone::FindResult find_result(zone->find(name, rrtype));
  179. // Check it returns correct answers
  180. EXPECT_EQ(result, find_result.code);
  181. if (check_answer) {
  182. EXPECT_EQ(answer, find_result.rrset);
  183. }
  184. });
  185. }
  186. };
  187. /**
  188. * \brief Test MemoryZone::MemoryZone constructor.
  189. *
  190. * Takes the created zone and checks its properties they are the same
  191. * as passed parameters.
  192. */
  193. TEST_F(MemoryZoneTest, constructor) {
  194. ASSERT_EQ(class_, zone_.getClass());
  195. ASSERT_EQ(origin_, zone_.getOrigin());
  196. }
  197. /**
  198. * \brief Test adding.
  199. *
  200. * We test that it throws at the correct moments and the correct exceptions.
  201. * And we test the return value.
  202. */
  203. TEST_F(MemoryZoneTest, add) {
  204. // This one does not belong to this zone
  205. EXPECT_THROW(zone_.add(rr_out_), MemoryZone::OutOfZone);
  206. // Test null pointer
  207. EXPECT_THROW(zone_.add(ConstRRsetPtr()), MemoryZone::NullRRset);
  208. using namespace result; // Who should write the prefix all the time
  209. // Now put all the data we have there. It should throw nothing
  210. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_ns_)));
  211. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_ns_a_)));
  212. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_ns_aaaa_)));
  213. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_a_)));
  214. // Try putting there something twice, it should be rejected
  215. EXPECT_NO_THROW(EXPECT_EQ(EXIST, zone_.add(rr_ns_)));
  216. EXPECT_NO_THROW(EXPECT_EQ(EXIST, zone_.add(rr_ns_a_)));
  217. }
  218. /**
  219. * \brief Test searching.
  220. *
  221. * Check it finds or does not find correctly and does not throw exceptions.
  222. * \todo This doesn't do any kind of CNAME and so on. If it isn't
  223. * directly there, it just tells it doesn't exist.
  224. */
  225. TEST_F(MemoryZoneTest, find) {
  226. // Fill some data inside
  227. using namespace result; // Who should write the prefix all the time
  228. // Now put all the data we have there. It should throw nothing
  229. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_ns_)));
  230. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_ns_a_)));
  231. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_ns_aaaa_)));
  232. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_.add(rr_a_)));
  233. // These two should be successful
  234. findTest(origin_, RRType::NS(), Zone::SUCCESS, true, rr_ns_);
  235. findTest(ns_name_, RRType::A(), Zone::SUCCESS, true, rr_ns_a_);
  236. // These domain exist but don't have the provided RRType
  237. findTest(origin_, RRType::AAAA(), Zone::NXRRSET);
  238. findTest(ns_name_, RRType::NS(), Zone::NXRRSET);
  239. // These domains don't exist (and one is out of the zone)
  240. findTest(Name("nothere.example.org"), RRType::A(), Zone::NXDOMAIN);
  241. findTest(Name("example.net"), RRType::A(), Zone::NXDOMAIN);
  242. }
  243. TEST_F(MemoryZoneTest, load) {
  244. // Put some data inside the zone
  245. EXPECT_NO_THROW(EXPECT_EQ(result::SUCCESS, zone_.add(rr_ns_)));
  246. // Loading with different origin should fail
  247. EXPECT_THROW(zone_.load(TEST_DATA_DIR "/root.zone"), MasterLoadError);
  248. // See the original data is still there, survived the exception
  249. findTest(origin_, RRType::NS(), Zone::SUCCESS, true, rr_ns_);
  250. // Create correct zone
  251. MemoryZone rootzone(class_, Name("."));
  252. // Try putting something inside
  253. EXPECT_NO_THROW(EXPECT_EQ(result::SUCCESS, rootzone.add(rr_ns_aaaa_)));
  254. // Load the zone. It should overwrite/remove the above RRset
  255. EXPECT_NO_THROW(rootzone.load(TEST_DATA_DIR "/root.zone"));
  256. // Now see there are some rrsets (we don't look inside, though)
  257. findTest(Name("."), RRType::SOA(), Zone::SUCCESS, false, ConstRRsetPtr(),
  258. &rootzone);
  259. findTest(Name("."), RRType::NS(), Zone::SUCCESS, false, ConstRRsetPtr(),
  260. &rootzone);
  261. findTest(Name("a.root-servers.net."), RRType::A(), Zone::SUCCESS, false,
  262. ConstRRsetPtr(), &rootzone);
  263. // But this should no longer be here
  264. findTest(ns_name_, RRType::AAAA(), Zone::NXDOMAIN, true, ConstRRsetPtr(),
  265. &rootzone);
  266. }
  267. }