memory_datasrc_unittest.cc 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586
  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 <boost/bind.hpp>
  17. #include <boost/foreach.hpp>
  18. #include <exceptions/exceptions.h>
  19. #include <dns/masterload.h>
  20. #include <dns/name.h>
  21. #include <dns/rdata.h>
  22. #include <dns/rdataclass.h>
  23. #include <dns/rrclass.h>
  24. #include <dns/rrsetlist.h>
  25. #include <dns/rrttl.h>
  26. #include <dns/masterload.h>
  27. #include <datasrc/memory_datasrc.h>
  28. #include <datasrc/data_source.h>
  29. #include <datasrc/iterator.h>
  30. #include <testutils/dnsmessage_test.h>
  31. #include <gtest/gtest.h>
  32. using namespace std;
  33. using namespace isc::dns;
  34. using namespace isc::dns::rdata;
  35. using namespace isc::datasrc;
  36. using namespace isc::testutils;
  37. namespace {
  38. // Commonly used result codes (Who should write the prefix all the time)
  39. using result::SUCCESS;
  40. using result::EXIST;
  41. class InMemoryClientTest : public ::testing::Test {
  42. protected:
  43. InMemoryClientTest() : rrclass(RRClass::IN())
  44. {}
  45. RRClass rrclass;
  46. InMemoryClient memory_client;
  47. };
  48. TEST_F(InMemoryClientTest, add_find_Zone) {
  49. // test add zone
  50. // Bogus zone (NULL)
  51. EXPECT_THROW(memory_client.addZone(ZoneFinderPtr()),
  52. isc::InvalidParameter);
  53. // add zones with different names one by one
  54. EXPECT_EQ(result::SUCCESS, memory_client.addZone(
  55. ZoneFinderPtr(new InMemoryZoneFinder(RRClass::IN(),
  56. Name("a")))));
  57. EXPECT_EQ(result::SUCCESS, memory_client.addZone(
  58. ZoneFinderPtr(new InMemoryZoneFinder(RRClass::CH(),
  59. Name("b")))));
  60. EXPECT_EQ(result::SUCCESS, memory_client.addZone(
  61. ZoneFinderPtr(new InMemoryZoneFinder(RRClass::IN(),
  62. Name("c")))));
  63. // add zones with the same name suffix
  64. EXPECT_EQ(result::SUCCESS, memory_client.addZone(
  65. ZoneFinderPtr(new InMemoryZoneFinder(RRClass::CH(),
  66. Name("x.d.e.f")))));
  67. EXPECT_EQ(result::SUCCESS, memory_client.addZone(
  68. ZoneFinderPtr(new InMemoryZoneFinder(RRClass::CH(),
  69. Name("o.w.y.d.e.f")))));
  70. EXPECT_EQ(result::SUCCESS, memory_client.addZone(
  71. ZoneFinderPtr(new InMemoryZoneFinder(RRClass::CH(),
  72. Name("p.w.y.d.e.f")))));
  73. EXPECT_EQ(result::SUCCESS, memory_client.addZone(
  74. ZoneFinderPtr(new InMemoryZoneFinder(RRClass::IN(),
  75. Name("q.w.y.d.e.f")))));
  76. // add super zone and its subzone
  77. EXPECT_EQ(result::SUCCESS, memory_client.addZone(
  78. ZoneFinderPtr(new InMemoryZoneFinder(RRClass::CH(),
  79. Name("g.h")))));
  80. EXPECT_EQ(result::SUCCESS, memory_client.addZone(
  81. ZoneFinderPtr(new InMemoryZoneFinder(RRClass::IN(),
  82. Name("i.g.h")))));
  83. EXPECT_EQ(result::SUCCESS, memory_client.addZone(
  84. ZoneFinderPtr(new InMemoryZoneFinder(RRClass::IN(),
  85. Name("z.d.e.f")))));
  86. EXPECT_EQ(result::SUCCESS, memory_client.addZone(
  87. ZoneFinderPtr(new InMemoryZoneFinder(RRClass::IN(),
  88. Name("j.z.d.e.f")))));
  89. // different zone class isn't allowed.
  90. EXPECT_EQ(result::EXIST, memory_client.addZone(
  91. ZoneFinderPtr(new InMemoryZoneFinder(RRClass::CH(),
  92. Name("q.w.y.d.e.f")))));
  93. // names are compared in a case insensitive manner.
  94. EXPECT_EQ(result::EXIST, memory_client.addZone(
  95. ZoneFinderPtr(new InMemoryZoneFinder(RRClass::IN(),
  96. Name("Q.W.Y.d.E.f")))));
  97. // test find zone
  98. EXPECT_EQ(result::SUCCESS, memory_client.findZone(Name("a")).code);
  99. EXPECT_EQ(Name("a"),
  100. memory_client.findZone(Name("a")).zone_finder->getOrigin());
  101. EXPECT_EQ(result::SUCCESS,
  102. memory_client.findZone(Name("j.z.d.e.f")).code);
  103. EXPECT_EQ(Name("j.z.d.e.f"),
  104. memory_client.findZone(Name("j.z.d.e.f")).zone_finder->
  105. getOrigin());
  106. // NOTFOUND
  107. EXPECT_EQ(result::NOTFOUND, memory_client.findZone(Name("d.e.f")).code);
  108. EXPECT_EQ(ConstZoneFinderPtr(),
  109. memory_client.findZone(Name("d.e.f")).zone_finder);
  110. EXPECT_EQ(result::NOTFOUND,
  111. memory_client.findZone(Name("w.y.d.e.f")).code);
  112. EXPECT_EQ(ConstZoneFinderPtr(),
  113. memory_client.findZone(Name("w.y.d.e.f")).zone_finder);
  114. // there's no exact match. the result should be the longest match,
  115. // and the code should be PARTIALMATCH.
  116. EXPECT_EQ(result::PARTIALMATCH,
  117. memory_client.findZone(Name("j.g.h")).code);
  118. EXPECT_EQ(Name("g.h"),
  119. memory_client.findZone(Name("g.h")).zone_finder->getOrigin());
  120. EXPECT_EQ(result::PARTIALMATCH,
  121. memory_client.findZone(Name("z.i.g.h")).code);
  122. EXPECT_EQ(Name("i.g.h"),
  123. memory_client.findZone(Name("z.i.g.h")).zone_finder->
  124. getOrigin());
  125. }
  126. TEST_F(InMemoryClientTest, iterator) {
  127. // Just some preparations of data
  128. boost::shared_ptr<InMemoryZoneFinder>
  129. zone(new InMemoryZoneFinder(RRClass::IN(), Name("a")));
  130. RRsetPtr aRRsetA(new RRset(Name("a"), RRClass::IN(), RRType::A(),
  131. RRTTL(300)));
  132. aRRsetA->addRdata(rdata::in::A("192.0.2.1"));
  133. RRsetPtr aRRsetAAAA(new RRset(Name("a"), RRClass::IN(), RRType::AAAA(),
  134. RRTTL(300)));
  135. aRRsetAAAA->addRdata(rdata::in::AAAA("2001:db8::1"));
  136. aRRsetAAAA->addRdata(rdata::in::AAAA("2001:db8::2"));
  137. RRsetPtr subRRsetA(new RRset(Name("sub.x.a"), RRClass::IN(), RRType::A(),
  138. RRTTL(300)));
  139. subRRsetA->addRdata(rdata::in::A("192.0.2.2"));
  140. EXPECT_EQ(result::SUCCESS, memory_client.addZone(zone));
  141. // First, the zone is not there, so it should throw
  142. EXPECT_THROW(memory_client.getIterator(Name("b")), DataSourceError);
  143. // This zone is not there either, even when there's a zone containing this
  144. EXPECT_THROW(memory_client.getIterator(Name("x.a")), DataSourceError);
  145. // Now, an empty zone
  146. ZoneIteratorPtr iterator(memory_client.getIterator(Name("a")));
  147. EXPECT_EQ(ConstRRsetPtr(), iterator->getNextRRset());
  148. // It throws Unexpected when we are past the end
  149. EXPECT_THROW(iterator->getNextRRset(), isc::Unexpected);
  150. EXPECT_EQ(result::SUCCESS, zone->add(aRRsetA));
  151. EXPECT_EQ(result::SUCCESS, zone->add(aRRsetAAAA));
  152. EXPECT_EQ(result::SUCCESS, zone->add(subRRsetA));
  153. // Check it with full zone, one by one.
  154. // It should be in ascending order in case of InMemory data source
  155. // (isn't guaranteed in general)
  156. iterator = memory_client.getIterator(Name("a"));
  157. EXPECT_EQ(aRRsetA, iterator->getNextRRset());
  158. EXPECT_EQ(aRRsetAAAA, iterator->getNextRRset());
  159. EXPECT_EQ(subRRsetA, iterator->getNextRRset());
  160. EXPECT_EQ(ConstRRsetPtr(), iterator->getNextRRset());
  161. }
  162. TEST_F(InMemoryClientTest, iterator_separate_rrs) {
  163. // Exactly the same tests as for iterator, but now with separate_rrs = true
  164. // For the one that returns actual data, the AAAA should now be split up
  165. boost::shared_ptr<InMemoryZoneFinder>
  166. zone(new InMemoryZoneFinder(RRClass::IN(), Name("a")));
  167. RRsetPtr aRRsetA(new RRset(Name("a"), RRClass::IN(), RRType::A(),
  168. RRTTL(300)));
  169. aRRsetA->addRdata(rdata::in::A("192.0.2.1"));
  170. RRsetPtr aRRsetAAAA(new RRset(Name("a"), RRClass::IN(), RRType::AAAA(),
  171. RRTTL(300)));
  172. aRRsetAAAA->addRdata(rdata::in::AAAA("2001:db8::1"));
  173. aRRsetAAAA->addRdata(rdata::in::AAAA("2001:db8::2"));
  174. RRsetPtr aRRsetAAAA_r1(new RRset(Name("a"), RRClass::IN(), RRType::AAAA(),
  175. RRTTL(300)));
  176. aRRsetAAAA_r1->addRdata(rdata::in::AAAA("2001:db8::1"));
  177. RRsetPtr aRRsetAAAA_r2(new RRset(Name("a"), RRClass::IN(), RRType::AAAA(),
  178. RRTTL(300)));
  179. aRRsetAAAA_r2->addRdata(rdata::in::AAAA("2001:db8::2"));
  180. RRsetPtr subRRsetA(new RRset(Name("sub.x.a"), RRClass::IN(), RRType::A(),
  181. RRTTL(300)));
  182. subRRsetA->addRdata(rdata::in::A("192.0.2.2"));
  183. EXPECT_EQ(result::SUCCESS, memory_client.addZone(zone));
  184. // First, the zone is not there, so it should throw
  185. EXPECT_THROW(memory_client.getIterator(Name("b"), true), DataSourceError);
  186. // This zone is not there either, even when there's a zone containing this
  187. EXPECT_THROW(memory_client.getIterator(Name("x.a")), DataSourceError);
  188. // Now, an empty zone
  189. ZoneIteratorPtr iterator(memory_client.getIterator(Name("a"), true));
  190. EXPECT_EQ(ConstRRsetPtr(), iterator->getNextRRset());
  191. // It throws Unexpected when we are past the end
  192. EXPECT_THROW(iterator->getNextRRset(), isc::Unexpected);
  193. ASSERT_EQ(result::SUCCESS, zone->add(aRRsetA));
  194. ASSERT_EQ(result::SUCCESS, zone->add(aRRsetAAAA));
  195. ASSERT_EQ(result::SUCCESS, zone->add(subRRsetA));
  196. // Check it with full zone, one by one.
  197. // It should be in ascending order in case of InMemory data source
  198. // (isn't guaranteed in general)
  199. iterator = memory_client.getIterator(Name("a"), true);
  200. EXPECT_EQ(aRRsetA->toText(), iterator->getNextRRset()->toText());
  201. EXPECT_EQ(aRRsetAAAA_r1->toText(), iterator->getNextRRset()->toText());
  202. EXPECT_EQ(aRRsetAAAA_r2->toText(), iterator->getNextRRset()->toText());
  203. EXPECT_EQ(subRRsetA->toText(), iterator->getNextRRset()->toText());
  204. EXPECT_EQ(ConstRRsetPtr(), iterator->getNextRRset());
  205. }
  206. TEST_F(InMemoryClientTest, getZoneCount) {
  207. EXPECT_EQ(0, memory_client.getZoneCount());
  208. memory_client.addZone(
  209. ZoneFinderPtr(new InMemoryZoneFinder(rrclass,
  210. Name("example.com"))));
  211. EXPECT_EQ(1, memory_client.getZoneCount());
  212. // duplicate add. counter shouldn't change
  213. memory_client.addZone(
  214. ZoneFinderPtr(new InMemoryZoneFinder(rrclass,
  215. Name("example.com"))));
  216. EXPECT_EQ(1, memory_client.getZoneCount());
  217. // add one more
  218. memory_client.addZone(
  219. ZoneFinderPtr(new InMemoryZoneFinder(rrclass,
  220. Name("example.org"))));
  221. EXPECT_EQ(2, memory_client.getZoneCount());
  222. }
  223. TEST_F(InMemoryClientTest, startUpdateZone) {
  224. EXPECT_THROW(memory_client.getUpdater(Name("example.org"), false),
  225. isc::NotImplemented);
  226. }
  227. // Commonly used RRSIG data
  228. const char* const rrsig_a_txt =
  229. "example.org. 300 IN RRSIG A 5 3 3600 20000101000000 20000201000000 12345 "
  230. "example.org. FAKEFAKEFAKE\n";
  231. const char* const rrsig_ns_txt =
  232. "example.org. 300 IN RRSIG NS 5 3 3600 20000101000000 20000201000000 "
  233. "54321 example.org. FAKEFAKEFAKEFAKE\n";
  234. // This RRset has two RRSIGs
  235. const char* const rrsig_aaaa_txt =
  236. "ns.example.org. 300 IN RRSIG AAAA 5 3 3600 20000101000000 20000201000000 "
  237. "12345 example.org. FAKEFAKEFAKE\n"
  238. "ns.example.org. 300 IN RRSIG AAAA 5 3 3600 20000101000000 20000201000000 "
  239. "54321 example.org. FAKEFAKEFAKEFAKE\n";
  240. // A helper callback of masterLoad() used in InMemoryZoneFinderTest.
  241. void
  242. setRRset(RRsetPtr rrset, vector<RRsetPtr*>::iterator& it) {
  243. *(*it) = rrset;
  244. ++it;
  245. }
  246. /// \brief Test fixture for the InMemoryZoneFinder class
  247. class InMemoryZoneFinderTest : public ::testing::Test {
  248. // A straightforward pair of textual RR(set) and a RRsetPtr variable
  249. // to store the RRset. Used to build test data below.
  250. struct RRsetData {
  251. const char* const text; // textual representation of an RRset
  252. RRsetPtr* rrset;
  253. };
  254. public:
  255. InMemoryZoneFinderTest() :
  256. class_(RRClass::IN()),
  257. origin_("example.org"),
  258. zone_finder_(class_, origin_)
  259. {
  260. // Build test RRsets. Below, we construct an RRset for
  261. // each textual RR(s) of zone_data, and assign it to the corresponding
  262. // rr_xxx.
  263. const RRsetData zone_data[] = {
  264. {"example.org. 300 IN NS ns.example.org.", &rr_ns_},
  265. {"example.org. 300 IN A 192.0.2.1", &rr_a_},
  266. {"ns.example.org. 300 IN A 192.0.2.2", &rr_ns_a_},
  267. {"ns.example.org. 300 IN AAAA 2001:db8::2", &rr_ns_aaaa_},
  268. {"cname.example.org. 300 IN CNAME canonical.example.org",
  269. &rr_cname_},
  270. {"cname.example.org. 300 IN A 192.0.2.3", &rr_cname_a_},
  271. {"dname.example.org. 300 IN DNAME target.example.org.",
  272. &rr_dname_},
  273. {"dname.example.org. 300 IN A 192.0.2.39", &rr_dname_a_},
  274. {"dname.example.org. 300 IN NS ns.dname.example.org.",
  275. &rr_dname_ns_},
  276. {"example.org. 300 IN DNAME example.com.", &rr_dname_apex_},
  277. {"child.example.org. 300 IN NS ns.child.example.org.",
  278. &rr_child_ns_},
  279. {"child.example.org. 300 IN DS 12345 5 1 DEADBEEF",
  280. &rr_child_ds_},
  281. {"ns.child.example.org. 300 IN A 192.0.2.153",
  282. &rr_child_glue_},
  283. {"grand.child.example.org. 300 IN NS ns.grand.child.example.org.",
  284. &rr_grandchild_ns_},
  285. {"ns.grand.child.example.org. 300 IN AAAA 2001:db8::253",
  286. &rr_grandchild_glue_},
  287. {"dname.child.example.org. 300 IN DNAME example.com.",
  288. &rr_child_dname_},
  289. {"example.com. 300 IN A 192.0.2.10", &rr_out_},
  290. {"*.wild.example.org. 300 IN A 192.0.2.1", &rr_wild_},
  291. {"foo.wild.example.org. 300 IN A 192.0.2.3", &rr_under_wild_},
  292. {"wild.*.foo.example.org. 300 IN A 192.0.2.1", &rr_emptywild_},
  293. {"wild.*.foo.*.bar.example.org. 300 IN A 192.0.2.1",
  294. &rr_nested_emptywild_},
  295. {"*.nswild.example.org. 300 IN NS nswild.example.", &rr_nswild_},
  296. {"*.dnamewild.example.org. 300 IN DNAME dnamewild.example.",
  297. &rr_dnamewild_},
  298. {"*.child.example.org. 300 IN A 192.0.2.1", &rr_child_wild_},
  299. {"bar.foo.wild.example.org. 300 IN A 192.0.2.2", &rr_not_wild_},
  300. {"baz.foo.wild.example.org. 300 IN A 192.0.2.3",
  301. &rr_not_wild_another_},
  302. {NULL, NULL}
  303. };
  304. stringstream zone_data_stream;
  305. vector<RRsetPtr*> rrsets;
  306. for (unsigned int i = 0; zone_data[i].text != NULL; ++i) {
  307. zone_data_stream << zone_data[i].text << "\n";
  308. rrsets.push_back(zone_data[i].rrset);
  309. }
  310. masterLoad(zone_data_stream, Name::ROOT_NAME(), class_,
  311. boost::bind(setRRset, _1, rrsets.begin()));
  312. }
  313. // Some data to test with
  314. const RRClass class_;
  315. const Name origin_;
  316. // The zone finder to torture by tests
  317. InMemoryZoneFinder zone_finder_;
  318. // Placeholder for storing RRsets to be checked with rrsetsCheck()
  319. vector<ConstRRsetPtr> actual_rrsets_;
  320. /*
  321. * Some RRsets to put inside the zone.
  322. */
  323. RRsetPtr
  324. // Out of zone RRset
  325. rr_out_,
  326. // NS of example.org
  327. rr_ns_,
  328. // A of ns.example.org
  329. rr_ns_a_,
  330. // AAAA of ns.example.org
  331. rr_ns_aaaa_,
  332. // A of example.org
  333. rr_a_;
  334. RRsetPtr rr_cname_; // CNAME in example.org (RDATA will be added)
  335. RRsetPtr rr_cname_a_; // for mixed CNAME + A case
  336. RRsetPtr rr_dname_; // DNAME in example.org (RDATA will be added)
  337. RRsetPtr rr_dname_a_; // for mixed DNAME + A case
  338. RRsetPtr rr_dname_ns_; // for mixed DNAME + NS case
  339. RRsetPtr rr_dname_apex_; // for mixed DNAME + NS case in the apex
  340. RRsetPtr rr_child_ns_; // NS of a child domain (for delegation)
  341. RRsetPtr rr_child_ds_; // DS of a child domain (for delegation, auth data)
  342. RRsetPtr rr_child_glue_; // glue RR of the child domain
  343. RRsetPtr rr_grandchild_ns_; // NS below a zone cut (unusual)
  344. RRsetPtr rr_grandchild_glue_; // glue RR below a deeper zone cut
  345. RRsetPtr rr_child_dname_; // A DNAME under NS
  346. RRsetPtr rr_wild_;
  347. RRsetPtr rr_emptywild_;
  348. RRsetPtr rr_nested_emptywild_;
  349. RRsetPtr rr_nswild_, rr_dnamewild_;
  350. RRsetPtr rr_child_wild_;
  351. RRsetPtr rr_under_wild_;
  352. RRsetPtr rr_not_wild_;
  353. RRsetPtr rr_not_wild_another_;
  354. /**
  355. * \brief Test one find query to the zone finder.
  356. *
  357. * Asks a query to the zone finder and checks it does not throw and returns
  358. * expected results. It returns nothing, it just signals failures
  359. * to GTEST.
  360. *
  361. * \param name The name to ask for.
  362. * \param rrtype The RRType to ask of.
  363. * \param result The expected code of the result.
  364. * \param check_answer Should a check against equality of the answer be
  365. * done?
  366. * \param answer The expected rrset, if any should be returned.
  367. * \param zone_finder Check different InMemoryZoneFinder object than
  368. * zone_finder_ (if NULL, uses zone_finder_)
  369. * \param check_wild_answer Checks that the answer has the same RRs, type
  370. * class and TTL as the eqxpected answer and that the name corresponds
  371. * to the one searched. It is meant for checking answers for wildcard
  372. * queries.
  373. */
  374. void findTest(const Name& name, const RRType& rrtype,
  375. ZoneFinder::Result result,
  376. bool check_answer = true,
  377. const ConstRRsetPtr& answer = ConstRRsetPtr(),
  378. InMemoryZoneFinder* zone_finder = NULL,
  379. ZoneFinder::FindOptions options = ZoneFinder::FIND_DEFAULT,
  380. bool check_wild_answer = false)
  381. {
  382. if (zone_finder == NULL) {
  383. zone_finder = &zone_finder_;
  384. }
  385. // The whole block is inside, because we need to check the result and
  386. // we can't assign to FindResult
  387. EXPECT_NO_THROW({
  388. ZoneFinder::FindResult find_result(zone_finder->find(
  389. name, rrtype,
  390. options));
  391. // Check it returns correct answers
  392. EXPECT_EQ(result, find_result.code);
  393. if (check_answer) {
  394. EXPECT_EQ(answer, find_result.rrset);
  395. } else if (check_wild_answer) {
  396. ASSERT_NE(ConstRRsetPtr(), answer) <<
  397. "Wrong test, don't check for wild names if you expect "
  398. "empty answer";
  399. ASSERT_NE(ConstRRsetPtr(), find_result.rrset) <<
  400. "No answer found";
  401. RdataIteratorPtr expectedIt(answer->getRdataIterator());
  402. RdataIteratorPtr actualIt(
  403. find_result.rrset->getRdataIterator());
  404. while (!expectedIt->isLast() && !actualIt->isLast()) {
  405. EXPECT_EQ(0, expectedIt->getCurrent().compare(
  406. actualIt->getCurrent())) << "The RRs differ ('" <<
  407. expectedIt->getCurrent().toText() << "', '" <<
  408. actualIt->getCurrent().toText() << "')";
  409. expectedIt->next();
  410. actualIt->next();
  411. }
  412. EXPECT_TRUE(expectedIt->isLast()) <<
  413. "Result has less RRs than expected";
  414. EXPECT_TRUE(actualIt->isLast()) <<
  415. "Result has more RRs than expected";
  416. EXPECT_EQ(answer->getClass(),
  417. find_result.rrset->getClass());
  418. EXPECT_EQ(answer->getType(),
  419. find_result.rrset->getType());
  420. EXPECT_EQ(answer->getTTL(),
  421. find_result.rrset->getTTL());
  422. EXPECT_EQ(name, find_result.rrset->getName());
  423. }
  424. });
  425. }
  426. /**
  427. * \brief Calls the findAll on the finder and checks the result.
  428. */
  429. std::vector<ConstRRsetPtr> findAllTest(const Name& name,
  430. ZoneFinder::Result result,
  431. size_t expected_size,
  432. InMemoryZoneFinder* finder = NULL,
  433. const ConstRRsetPtr &rrset_result =
  434. ConstRRsetPtr(),
  435. ZoneFinder::FindOptions options =
  436. ZoneFinder::FIND_DEFAULT)
  437. {
  438. if (finder == NULL) {
  439. finder = &zone_finder_;
  440. }
  441. std::vector<ConstRRsetPtr> target;
  442. ZoneFinder::FindResult findResult(finder->findAll(name, target,
  443. options));
  444. EXPECT_EQ(result, findResult.code);
  445. EXPECT_EQ(rrset_result, findResult.rrset);
  446. BOOST_FOREACH(const ConstRRsetPtr& rrset, target) {
  447. EXPECT_EQ(name, rrset->getName());
  448. }
  449. EXPECT_EQ(expected_size, target.size());
  450. return (target);
  451. }
  452. // Internal part of the cancelWildcard test that is multiple times
  453. void doCancelWildcardTest();
  454. ConstRRsetPtr textToRRset(const string& text_rrset,
  455. const RRClass& rrclass = RRClass::IN()) const
  456. {
  457. stringstream ss(text_rrset);
  458. RRsetPtr rrset;
  459. vector<RRsetPtr*> rrsets;
  460. rrsets.push_back(&rrset);
  461. masterLoad(ss, Name::ROOT_NAME(), rrclass,
  462. boost::bind(setRRset, _1, rrsets.begin()));
  463. return (rrset);
  464. }
  465. };
  466. /**
  467. * \brief Check that findPreviousName throws as it should now.
  468. */
  469. TEST_F(InMemoryZoneFinderTest, findPreviousName) {
  470. EXPECT_THROW(zone_finder_.findPreviousName(Name("www.example.org")),
  471. isc::NotImplemented);
  472. }
  473. /**
  474. * \brief Test InMemoryZoneFinder::InMemoryZoneFinder constructor.
  475. *
  476. * Takes the created zone finder and checks its properties they are the same
  477. * as passed parameters.
  478. */
  479. TEST_F(InMemoryZoneFinderTest, constructor) {
  480. ASSERT_EQ(class_, zone_finder_.getClass());
  481. ASSERT_EQ(origin_, zone_finder_.getOrigin());
  482. }
  483. /**
  484. * \brief Test adding.
  485. *
  486. * We test that it throws at the correct moments and the correct exceptions.
  487. * And we test the return value.
  488. */
  489. TEST_F(InMemoryZoneFinderTest, add) {
  490. // This one does not belong to this zone
  491. EXPECT_THROW(zone_finder_.add(rr_out_), InMemoryZoneFinder::OutOfZone);
  492. // Test null pointer
  493. EXPECT_THROW(zone_finder_.add(ConstRRsetPtr()),
  494. InMemoryZoneFinder::NullRRset);
  495. // Now put all the data we have there. It should throw nothing
  496. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_finder_.add(rr_ns_)));
  497. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_finder_.add(rr_ns_a_)));
  498. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_finder_.add(rr_ns_aaaa_)));
  499. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_finder_.add(rr_a_)));
  500. // Try putting there something twice, it should be rejected
  501. EXPECT_NO_THROW(EXPECT_EQ(EXIST, zone_finder_.add(rr_ns_)));
  502. EXPECT_NO_THROW(EXPECT_EQ(EXIST, zone_finder_.add(rr_ns_a_)));
  503. }
  504. TEST_F(InMemoryZoneFinderTest, addMultipleCNAMEs) {
  505. rr_cname_->addRdata(generic::CNAME("canonical2.example.org."));
  506. EXPECT_THROW(zone_finder_.add(rr_cname_), InMemoryZoneFinder::AddError);
  507. }
  508. TEST_F(InMemoryZoneFinderTest, addCNAMEThenOther) {
  509. EXPECT_EQ(SUCCESS, zone_finder_.add(rr_cname_));
  510. EXPECT_THROW(zone_finder_.add(rr_cname_a_), InMemoryZoneFinder::AddError);
  511. }
  512. TEST_F(InMemoryZoneFinderTest, addOtherThenCNAME) {
  513. EXPECT_EQ(SUCCESS, zone_finder_.add(rr_cname_a_));
  514. EXPECT_THROW(zone_finder_.add(rr_cname_), InMemoryZoneFinder::AddError);
  515. }
  516. TEST_F(InMemoryZoneFinderTest, addCNAMEAndDNSSECRecords) {
  517. // CNAME and RRSIG can coexist
  518. EXPECT_EQ(SUCCESS, zone_finder_.add(rr_cname_));
  519. EXPECT_EQ(SUCCESS, zone_finder_.add(
  520. textToRRset("cname.example.org. 300 IN RRSIG CNAME 5 3 "
  521. "3600 20000101000000 20000201000000 12345 "
  522. "example.org. FAKEFAKEFAKE")));
  523. // Same for NSEC
  524. EXPECT_EQ(SUCCESS, zone_finder_.add(
  525. textToRRset("cname.example.org. 300 IN NSEC "
  526. "dname.example.org. CNAME RRSIG NSEC")));
  527. // Same as above, but adding NSEC first.
  528. EXPECT_EQ(SUCCESS, zone_finder_.add(
  529. textToRRset("cname2.example.org. 300 IN NSEC "
  530. "dname.example.org. CNAME RRSIG NSEC")));
  531. EXPECT_EQ(SUCCESS, zone_finder_.add(
  532. textToRRset("cname2.example.org. 300 IN CNAME c.example.")));
  533. // If there's another type of RRset with NSEC, it should still fail.
  534. EXPECT_EQ(SUCCESS, zone_finder_.add(
  535. textToRRset("cname3.example.org. 300 IN A 192.0.2.1")));
  536. EXPECT_EQ(SUCCESS, zone_finder_.add(
  537. textToRRset("cname3.example.org. 300 IN NSEC "
  538. "dname.example.org. CNAME RRSIG NSEC")));
  539. EXPECT_THROW(zone_finder_.add(textToRRset("cname3.example.org. 300 "
  540. "IN CNAME c.example.")),
  541. InMemoryZoneFinder::AddError);
  542. }
  543. TEST_F(InMemoryZoneFinderTest, findCNAME) {
  544. // install CNAME RR
  545. EXPECT_EQ(SUCCESS, zone_finder_.add(rr_cname_));
  546. // Find A RR of the same. Should match the CNAME
  547. findTest(rr_cname_->getName(), RRType::NS(), ZoneFinder::CNAME, true,
  548. rr_cname_);
  549. // Find the CNAME itself. Should result in normal SUCCESS
  550. findTest(rr_cname_->getName(), RRType::CNAME(), ZoneFinder::SUCCESS, true,
  551. rr_cname_);
  552. }
  553. TEST_F(InMemoryZoneFinderTest, findCNAMEUnderZoneCut) {
  554. // There's nothing special when we find a CNAME under a zone cut
  555. // (with FIND_GLUE_OK). The behavior is different from BIND 9,
  556. // so we test this case explicitly.
  557. EXPECT_EQ(SUCCESS, zone_finder_.add(rr_child_ns_));
  558. ConstRRsetPtr rr_cname_under_cut_ = textToRRset(
  559. "cname.child.example.org. 300 IN CNAME target.child.example.org.");
  560. EXPECT_EQ(SUCCESS, zone_finder_.add(rr_cname_under_cut_));
  561. findTest(Name("cname.child.example.org"), RRType::AAAA(),
  562. ZoneFinder::CNAME, true, rr_cname_under_cut_, NULL,
  563. ZoneFinder::FIND_GLUE_OK);
  564. }
  565. // Two DNAMEs at single domain are disallowed by RFC 2672, section 3)
  566. // Having a CNAME there is disallowed too, but it is tested by
  567. // addOtherThenCNAME and addCNAMEThenOther.
  568. TEST_F(InMemoryZoneFinderTest, addMultipleDNAMEs) {
  569. rr_dname_->addRdata(generic::DNAME("target2.example.org."));
  570. EXPECT_THROW(zone_finder_.add(rr_dname_), InMemoryZoneFinder::AddError);
  571. }
  572. /*
  573. * These two tests ensure that we can't have DNAME and NS at the same
  574. * node with the exception of the apex of zone (forbidden by RFC 2672)
  575. */
  576. TEST_F(InMemoryZoneFinderTest, addDNAMEThenNS) {
  577. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_finder_.add(rr_dname_)));
  578. EXPECT_THROW(zone_finder_.add(rr_dname_ns_), InMemoryZoneFinder::AddError);
  579. }
  580. TEST_F(InMemoryZoneFinderTest, addNSThenDNAME) {
  581. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_finder_.add(rr_dname_ns_)));
  582. EXPECT_THROW(zone_finder_.add(rr_dname_), InMemoryZoneFinder::AddError);
  583. }
  584. // It is allowed to have NS and DNAME at apex
  585. TEST_F(InMemoryZoneFinderTest, DNAMEAndNSAtApex) {
  586. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_finder_.add(rr_dname_apex_)));
  587. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_finder_.add(rr_ns_)));
  588. // The NS should be possible to be found, below should be DNAME, not
  589. // delegation
  590. findTest(origin_, RRType::NS(), ZoneFinder::SUCCESS, true, rr_ns_);
  591. findTest(rr_child_ns_->getName(), RRType::A(), ZoneFinder::DNAME, true,
  592. rr_dname_apex_);
  593. }
  594. TEST_F(InMemoryZoneFinderTest, NSAndDNAMEAtApex) {
  595. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_finder_.add(rr_ns_)));
  596. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_finder_.add(rr_dname_apex_)));
  597. }
  598. // TODO: Test (and implement) adding data under DNAME. That is forbidden by
  599. // 2672 as well.
  600. // Search under a DNAME record. It should return the DNAME
  601. TEST_F(InMemoryZoneFinderTest, findBelowDNAME) {
  602. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_finder_.add(rr_dname_)));
  603. findTest(Name("below.dname.example.org"), RRType::A(), ZoneFinder::DNAME,
  604. true, rr_dname_);
  605. }
  606. // Search at the domain with DNAME. It should act as DNAME isn't there, DNAME
  607. // influences only the data below (see RFC 2672, section 3)
  608. TEST_F(InMemoryZoneFinderTest, findAtDNAME) {
  609. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_finder_.add(rr_dname_)));
  610. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_finder_.add(rr_dname_a_)));
  611. const Name dname_name(rr_dname_->getName());
  612. findTest(dname_name, RRType::A(), ZoneFinder::SUCCESS, true, rr_dname_a_);
  613. findTest(dname_name, RRType::DNAME(), ZoneFinder::SUCCESS, true,
  614. rr_dname_);
  615. findTest(dname_name, RRType::TXT(), ZoneFinder::NXRRSET, true);
  616. }
  617. // Try searching something that is both under NS and DNAME, without and with
  618. // GLUE_OK mode (it should stop at the NS and DNAME respectively).
  619. TEST_F(InMemoryZoneFinderTest, DNAMEUnderNS) {
  620. zone_finder_.add(rr_child_ns_);
  621. zone_finder_.add(rr_child_dname_);
  622. Name lowName("below.dname.child.example.org.");
  623. findTest(lowName, RRType::A(), ZoneFinder::DELEGATION, true, rr_child_ns_);
  624. findTest(lowName, RRType::A(), ZoneFinder::DNAME, true, rr_child_dname_,
  625. NULL, ZoneFinder::FIND_GLUE_OK);
  626. }
  627. // Test adding child zones and zone cut handling
  628. TEST_F(InMemoryZoneFinderTest, delegationNS) {
  629. // add in-zone data
  630. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_finder_.add(rr_ns_)));
  631. // install a zone cut
  632. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_finder_.add(rr_child_ns_)));
  633. // below the zone cut
  634. findTest(Name("www.child.example.org"), RRType::A(),
  635. ZoneFinder::DELEGATION, true, rr_child_ns_);
  636. // at the zone cut
  637. findTest(Name("child.example.org"), RRType::A(), ZoneFinder::DELEGATION,
  638. true, rr_child_ns_);
  639. findTest(Name("child.example.org"), RRType::NS(), ZoneFinder::DELEGATION,
  640. true, rr_child_ns_);
  641. // finding NS for the apex (origin) node. This must not be confused
  642. // with delegation due to the existence of an NS RR.
  643. findTest(origin_, RRType::NS(), ZoneFinder::SUCCESS, true, rr_ns_);
  644. // unusual case of "nested delegation": the highest cut should be used.
  645. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_finder_.add(rr_grandchild_ns_)));
  646. findTest(Name("www.grand.child.example.org"), RRType::A(),
  647. // note: !rr_grandchild_ns_
  648. ZoneFinder::DELEGATION, true, rr_child_ns_);
  649. }
  650. TEST_F(InMemoryZoneFinderTest, delegationWithDS) {
  651. // Similar setup to the previous one, but with DS RR at the delegation
  652. // point.
  653. EXPECT_EQ(SUCCESS, zone_finder_.add(rr_ns_));
  654. EXPECT_EQ(SUCCESS, zone_finder_.add(rr_child_ns_));
  655. EXPECT_EQ(SUCCESS, zone_finder_.add(rr_child_ds_));
  656. // Normal types of query should result in delegation, but DS query
  657. // should be considered in-zone.
  658. findTest(Name("child.example.org"), RRType::A(), ZoneFinder::DELEGATION,
  659. true, rr_child_ns_);
  660. findTest(Name("child.example.org"), RRType::DS(), ZoneFinder::SUCCESS,
  661. true, rr_child_ds_);
  662. // There's nothing special for DS query at the zone apex. It would
  663. // normally result in NXRRSET.
  664. findTest(Name("example.org"), RRType::DS(), ZoneFinder::NXRRSET,
  665. true, ConstRRsetPtr());
  666. }
  667. TEST_F(InMemoryZoneFinderTest, findAny) {
  668. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_finder_.add(rr_a_)));
  669. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_finder_.add(rr_ns_)));
  670. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_finder_.add(rr_child_glue_)));
  671. // origin
  672. std::vector<ConstRRsetPtr> rrsets(findAllTest(origin_, ZoneFinder::SUCCESS,
  673. 2));
  674. EXPECT_FALSE(rrsets.end() == std::find(rrsets.begin(), rrsets.end(),
  675. rr_a_));
  676. EXPECT_FALSE(rrsets.end() == std::find(rrsets.begin(), rrsets.end(),
  677. rr_ns_));
  678. // out zone name
  679. findAllTest(Name("example.com"), ZoneFinder::NXDOMAIN, 0);
  680. rrsets = findAllTest(rr_child_glue_->getName(), ZoneFinder::SUCCESS, 1);
  681. EXPECT_FALSE(rrsets.end() == std::find(rrsets.begin(), rrsets.end(),
  682. rr_child_glue_));
  683. // TODO: test NXRRSET case after rbtree non-terminal logic has
  684. // been implemented
  685. // add zone cut
  686. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_finder_.add(rr_child_ns_)));
  687. // zone cut
  688. findAllTest(rr_child_ns_->getName(), ZoneFinder::DELEGATION, 0, NULL,
  689. rr_child_ns_);
  690. // glue for this zone cut
  691. findAllTest(rr_child_glue_->getName(),ZoneFinder::DELEGATION, 0, NULL,
  692. rr_child_ns_);
  693. }
  694. TEST_F(InMemoryZoneFinderTest, glue) {
  695. // install zone data:
  696. // a zone cut
  697. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_finder_.add(rr_child_ns_)));
  698. // glue for this cut
  699. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_finder_.add(rr_child_glue_)));
  700. // a nested zone cut (unusual)
  701. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_finder_.add(rr_grandchild_ns_)));
  702. // glue under the deeper zone cut
  703. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_finder_.add(rr_grandchild_glue_)));
  704. // by default glue is hidden due to the zone cut
  705. findTest(rr_child_glue_->getName(), RRType::A(), ZoneFinder::DELEGATION,
  706. true, rr_child_ns_);
  707. // If we do it in the "glue OK" mode, we should find the exact match.
  708. findTest(rr_child_glue_->getName(), RRType::A(), ZoneFinder::SUCCESS, true,
  709. rr_child_glue_, NULL, ZoneFinder::FIND_GLUE_OK);
  710. // glue OK + NXRRSET case
  711. findTest(rr_child_glue_->getName(), RRType::AAAA(), ZoneFinder::NXRRSET,
  712. true, ConstRRsetPtr(), NULL, ZoneFinder::FIND_GLUE_OK);
  713. // glue OK + NXDOMAIN case
  714. findTest(Name("www.child.example.org"), RRType::A(),
  715. ZoneFinder::DELEGATION, true, rr_child_ns_, NULL,
  716. ZoneFinder::FIND_GLUE_OK);
  717. // nested cut case. The glue should be found.
  718. findTest(rr_grandchild_glue_->getName(), RRType::AAAA(),
  719. ZoneFinder::SUCCESS,
  720. true, rr_grandchild_glue_, NULL, ZoneFinder::FIND_GLUE_OK);
  721. // A non-existent name in nested cut. This should result in delegation
  722. // at the highest zone cut.
  723. findTest(Name("www.grand.child.example.org"), RRType::TXT(),
  724. ZoneFinder::DELEGATION, true, rr_child_ns_, NULL,
  725. ZoneFinder::FIND_GLUE_OK);
  726. }
  727. /**
  728. * \brief Test searching.
  729. *
  730. * Check it finds or does not find correctly and does not throw exceptions.
  731. * \todo This doesn't do any kind of CNAME and so on. If it isn't
  732. * directly there, it just tells it doesn't exist.
  733. */
  734. TEST_F(InMemoryZoneFinderTest, find) {
  735. // Fill some data inside
  736. // Now put all the data we have there. It should throw nothing
  737. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_finder_.add(rr_ns_)));
  738. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_finder_.add(rr_ns_a_)));
  739. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_finder_.add(rr_ns_aaaa_)));
  740. EXPECT_NO_THROW(EXPECT_EQ(SUCCESS, zone_finder_.add(rr_a_)));
  741. // These two should be successful
  742. findTest(origin_, RRType::NS(), ZoneFinder::SUCCESS, true, rr_ns_);
  743. findTest(rr_ns_a_->getName(), RRType::A(), ZoneFinder::SUCCESS, true,
  744. rr_ns_a_);
  745. // These domain exist but don't have the provided RRType
  746. findTest(origin_, RRType::AAAA(), ZoneFinder::NXRRSET);
  747. findTest(rr_ns_a_->getName(), RRType::NS(), ZoneFinder::NXRRSET);
  748. // These domains don't exist (and one is out of the zone)
  749. findTest(Name("nothere.example.org"), RRType::A(), ZoneFinder::NXDOMAIN);
  750. findTest(Name("example.net"), RRType::A(), ZoneFinder::NXDOMAIN);
  751. }
  752. TEST_F(InMemoryZoneFinderTest, emptyNode) {
  753. /*
  754. * The backend RBTree for this test should look like as follows:
  755. * example.org
  756. * |
  757. * baz (empty; easy case)
  758. * / | \
  759. * bar | x.foo ('foo' part is empty; a bit trickier)
  760. * bbb
  761. * /
  762. * aaa
  763. */
  764. // Construct the test zone
  765. const char* const names[] = {
  766. "bar.example.org.", "x.foo.example.org.", "aaa.baz.example.org.",
  767. "bbb.baz.example.org.", NULL};
  768. for (int i = 0; names[i] != NULL; ++i) {
  769. ConstRRsetPtr rrset = textToRRset(string(names[i]) +
  770. " 300 IN A 192.0.2.1");
  771. EXPECT_EQ(SUCCESS, zone_finder_.add(rrset));
  772. }
  773. // empty node matching, easy case: the node for 'baz' exists with
  774. // no data.
  775. findTest(Name("baz.example.org"), RRType::A(), ZoneFinder::NXRRSET);
  776. // empty node matching, a trickier case: the node for 'foo' is part of
  777. // "x.foo", which should be considered an empty node.
  778. findTest(Name("foo.example.org"), RRType::A(), ZoneFinder::NXRRSET);
  779. // "org" is contained in "example.org", but it shouldn't be treated as
  780. // NXRRSET because it's out of zone.
  781. // Note: basically we don't expect such a query to be performed (the common
  782. // operation is to identify the best matching zone first then perform
  783. // search it), but we shouldn't be confused even in the unexpected case.
  784. findTest(Name("org"), RRType::A(), ZoneFinder::NXDOMAIN);
  785. }
  786. TEST_F(InMemoryZoneFinderTest, load) {
  787. // Put some data inside the zone
  788. EXPECT_NO_THROW(EXPECT_EQ(result::SUCCESS, zone_finder_.add(rr_ns_)));
  789. // Loading with different origin should fail
  790. EXPECT_THROW(zone_finder_.load(TEST_DATA_DIR "/root.zone"),
  791. MasterLoadError);
  792. // See the original data is still there, survived the exception
  793. findTest(origin_, RRType::NS(), ZoneFinder::SUCCESS, true, rr_ns_);
  794. // Create correct zone
  795. InMemoryZoneFinder rootzone(class_, Name("."));
  796. // Try putting something inside
  797. EXPECT_NO_THROW(EXPECT_EQ(result::SUCCESS, rootzone.add(rr_ns_aaaa_)));
  798. // Load the zone. It should overwrite/remove the above RRset
  799. EXPECT_NO_THROW(rootzone.load(TEST_DATA_DIR "/root.zone"));
  800. // Now see there are some rrsets (we don't look inside, though)
  801. findTest(Name("."), RRType::SOA(), ZoneFinder::SUCCESS, false,
  802. ConstRRsetPtr(), &rootzone);
  803. findTest(Name("."), RRType::NS(), ZoneFinder::SUCCESS, false,
  804. ConstRRsetPtr(), &rootzone);
  805. findTest(Name("a.root-servers.net."), RRType::A(), ZoneFinder::SUCCESS,
  806. false, ConstRRsetPtr(), &rootzone);
  807. // But this should no longer be here
  808. findTest(rr_ns_a_->getName(), RRType::AAAA(), ZoneFinder::NXDOMAIN, true,
  809. ConstRRsetPtr(), &rootzone);
  810. // Try loading zone that is wrong in a different way
  811. EXPECT_THROW(zone_finder_.load(TEST_DATA_DIR "/duplicate_rrset.zone"),
  812. MasterLoadError);
  813. }
  814. /*
  815. * Test that puts a (simple) wildcard into the zone and checks we can
  816. * correctly find the data.
  817. */
  818. TEST_F(InMemoryZoneFinderTest, wildcard) {
  819. /*
  820. * example.org.
  821. * |
  822. * wild (not *.wild, should have wild mark)
  823. * |
  824. * *
  825. */
  826. EXPECT_EQ(SUCCESS, zone_finder_.add(rr_wild_));
  827. // Search at the parent. The parent will not have the A, but it will
  828. // be in the wildcard (so check the wildcard isn't matched at the parent)
  829. {
  830. SCOPED_TRACE("Search at parrent");
  831. findTest(Name("wild.example.org"), RRType::A(), ZoneFinder::NXRRSET);
  832. }
  833. // Search the original name of wildcard
  834. {
  835. SCOPED_TRACE("Search directly at *");
  836. findTest(Name("*.wild.example.org"), RRType::A(), ZoneFinder::SUCCESS,
  837. true, rr_wild_);
  838. }
  839. // Search "created" name.
  840. {
  841. SCOPED_TRACE("Search at created child");
  842. findTest(Name("a.wild.example.org"), RRType::A(), ZoneFinder::SUCCESS,
  843. false, rr_wild_, NULL, ZoneFinder::FIND_DEFAULT, true);
  844. }
  845. // Search another created name, this time little bit lower
  846. {
  847. SCOPED_TRACE("Search at created grand-child");
  848. findTest(Name("a.b.wild.example.org"), RRType::A(),
  849. ZoneFinder::SUCCESS, false, rr_wild_, NULL,
  850. ZoneFinder::FIND_DEFAULT, true);
  851. }
  852. EXPECT_EQ(SUCCESS, zone_finder_.add(rr_under_wild_));
  853. {
  854. SCOPED_TRACE("Search under non-wildcard");
  855. findTest(Name("bar.foo.wild.example.org"), RRType::A(),
  856. ZoneFinder::NXDOMAIN);
  857. }
  858. }
  859. /*
  860. * Test that we don't match a wildcard if we get under delegation.
  861. * By 4.3.3 of RFC1034:
  862. * "Wildcard RRs do not apply:
  863. * - When the query is in another zone. That is, delegation cancels
  864. * the wildcard defaults."
  865. */
  866. TEST_F(InMemoryZoneFinderTest, delegatedWildcard) {
  867. EXPECT_EQ(SUCCESS, zone_finder_.add(rr_child_wild_));
  868. EXPECT_EQ(SUCCESS, zone_finder_.add(rr_child_ns_));
  869. {
  870. SCOPED_TRACE("Looking under delegation point");
  871. findTest(Name("a.child.example.org"), RRType::A(),
  872. ZoneFinder::DELEGATION, true, rr_child_ns_);
  873. }
  874. {
  875. SCOPED_TRACE("Looking under delegation point in GLUE_OK mode");
  876. findTest(Name("a.child.example.org"), RRType::A(),
  877. ZoneFinder::DELEGATION, true, rr_child_ns_, NULL,
  878. ZoneFinder::FIND_GLUE_OK);
  879. }
  880. }
  881. // Tests combination of wildcard and ANY.
  882. TEST_F(InMemoryZoneFinderTest, anyWildcard) {
  883. EXPECT_EQ(SUCCESS, zone_finder_.add(rr_wild_));
  884. // First try directly the name (normal match)
  885. {
  886. SCOPED_TRACE("Asking direcly for *");
  887. const std::vector<ConstRRsetPtr>
  888. target(findAllTest(Name("*.wild.example.org"), ZoneFinder::SUCCESS,
  889. 1));
  890. ASSERT_EQ(1, target.size());
  891. EXPECT_EQ(RRType::A(), (*target.begin())->getType());
  892. EXPECT_EQ(Name("*.wild.example.org"), (*target.begin())->getName());
  893. }
  894. // Then a wildcard match
  895. {
  896. SCOPED_TRACE("Asking in the wild way");
  897. const std::vector<ConstRRsetPtr>
  898. target(findAllTest(Name("a.wild.example.org"), ZoneFinder::SUCCESS,
  899. 1));
  900. EXPECT_EQ(RRType::A(), (*target.begin())->getType());
  901. EXPECT_EQ(Name("a.wild.example.org"), (*target.begin())->getName());
  902. }
  903. }
  904. // Test there's nothing in the wildcard in the middle if we load
  905. // wild.*.foo.example.org.
  906. TEST_F(InMemoryZoneFinderTest, emptyWildcard) {
  907. /*
  908. * example.org.
  909. * foo
  910. * *
  911. * wild
  912. */
  913. EXPECT_EQ(SUCCESS, zone_finder_.add(rr_emptywild_));
  914. {
  915. SCOPED_TRACE("Asking for the original record under wildcard");
  916. findTest(Name("wild.*.foo.example.org"), RRType::A(),
  917. ZoneFinder::SUCCESS, true, rr_emptywild_);
  918. }
  919. {
  920. SCOPED_TRACE("Asking for A record");
  921. findTest(Name("a.foo.example.org"), RRType::A(), ZoneFinder::NXRRSET);
  922. findTest(Name("*.foo.example.org"), RRType::A(), ZoneFinder::NXRRSET);
  923. findTest(Name("foo.example.org"), RRType::A(), ZoneFinder::NXRRSET);
  924. }
  925. {
  926. SCOPED_TRACE("Asking for ANY record");
  927. findAllTest(Name("*.foo.example.org"), ZoneFinder::NXRRSET, 0);
  928. findAllTest(Name("a.foo.example.org"), ZoneFinder::NXRRSET, 0);
  929. }
  930. {
  931. SCOPED_TRACE("Asking on the non-terminal");
  932. findTest(Name("wild.bar.foo.example.org"), RRType::A(),
  933. ZoneFinder::NXRRSET);
  934. }
  935. }
  936. // Same as emptyWildcard, but with multiple * in the path.
  937. TEST_F(InMemoryZoneFinderTest, nestedEmptyWildcard) {
  938. EXPECT_EQ(SUCCESS, zone_finder_.add(rr_nested_emptywild_));
  939. {
  940. SCOPED_TRACE("Asking for the original record under wildcards");
  941. findTest(Name("wild.*.foo.*.bar.example.org"), RRType::A(),
  942. ZoneFinder::SUCCESS, true, rr_nested_emptywild_);
  943. }
  944. {
  945. SCOPED_TRACE("Matching wildcard against empty nonterminal");
  946. const char* names[] = {
  947. "baz.foo.*.bar.example.org",
  948. "baz.foo.baz.bar.example.org",
  949. "*.foo.baz.bar.example.org",
  950. NULL
  951. };
  952. for (const char** name = names; *name != NULL; ++ name) {
  953. SCOPED_TRACE(string("Node ") + *name);
  954. findTest(Name(*name), RRType::A(), ZoneFinder::NXRRSET);
  955. }
  956. }
  957. // Domains to test
  958. const char* names[] = {
  959. "*.foo.*.bar.example.org",
  960. "foo.*.bar.example.org",
  961. "*.bar.example.org",
  962. "bar.example.org",
  963. NULL
  964. };
  965. {
  966. SCOPED_TRACE("Asking directly for A on parent nodes");
  967. for (const char** name = names; *name != NULL; ++ name) {
  968. SCOPED_TRACE(string("Node ") + *name);
  969. findTest(Name(*name), RRType::A(), ZoneFinder::NXRRSET);
  970. }
  971. }
  972. {
  973. SCOPED_TRACE("Asking for ANY on parent nodes");
  974. for (const char** name = names; *name != NULL; ++ name) {
  975. SCOPED_TRACE(string("Node ") + *name);
  976. findAllTest(Name(*name), ZoneFinder::NXRRSET, 0);
  977. }
  978. }
  979. }
  980. // We run this part twice from the below test, in two slightly different
  981. // situations
  982. void
  983. InMemoryZoneFinderTest::doCancelWildcardTest() {
  984. // These should be canceled
  985. {
  986. SCOPED_TRACE("Canceled under foo.wild.example.org");
  987. findTest(Name("aaa.foo.wild.example.org"), RRType::A(),
  988. ZoneFinder::NXDOMAIN);
  989. findTest(Name("zzz.foo.wild.example.org"), RRType::A(),
  990. ZoneFinder::NXDOMAIN);
  991. }
  992. // This is existing, non-wildcard domain, shouldn't wildcard at all
  993. {
  994. SCOPED_TRACE("Existing domain under foo.wild.example.org");
  995. findTest(Name("bar.foo.wild.example.org"), RRType::A(),
  996. ZoneFinder::SUCCESS, true, rr_not_wild_);
  997. }
  998. // These should be caught by the wildcard
  999. {
  1000. SCOPED_TRACE("Neighbor wildcards to foo.wild.example.org");
  1001. const char* names[] = {
  1002. "aaa.bbb.wild.example.org",
  1003. "aaa.zzz.wild.example.org",
  1004. "zzz.wild.example.org",
  1005. NULL
  1006. };
  1007. for (const char** name = names; *name != NULL; ++ name) {
  1008. SCOPED_TRACE(string("Node ") + *name);
  1009. findTest(Name(*name), RRType::A(), ZoneFinder::SUCCESS, false,
  1010. rr_wild_, NULL, ZoneFinder::FIND_DEFAULT, true);
  1011. }
  1012. }
  1013. // This shouldn't be wildcarded, it's an existing domain
  1014. {
  1015. SCOPED_TRACE("The foo.wild.example.org itself");
  1016. findTest(Name("foo.wild.example.org"), RRType::A(),
  1017. ZoneFinder::NXRRSET);
  1018. }
  1019. }
  1020. /*
  1021. * This tests that if there's a name between the wildcard domain and the
  1022. * searched one, it will not trigger wildcard, for example, if we have
  1023. * *.wild.example.org and bar.foo.wild.example.org, then we know
  1024. * foo.wild.example.org exists and is not wildcard. Therefore, search for
  1025. * aaa.foo.wild.example.org should return NXDOMAIN.
  1026. *
  1027. * Tests few cases "around" the canceled wildcard match, to see something that
  1028. * shouldn't be canceled isn't.
  1029. */
  1030. TEST_F(InMemoryZoneFinderTest, cancelWildcard) {
  1031. EXPECT_EQ(SUCCESS, zone_finder_.add(rr_wild_));
  1032. EXPECT_EQ(SUCCESS, zone_finder_.add(rr_not_wild_));
  1033. {
  1034. SCOPED_TRACE("Runnig with single entry under foo.wild.example.org");
  1035. doCancelWildcardTest();
  1036. }
  1037. // Try putting another one under foo.wild....
  1038. // The result should be the same but it will be done in another way in the
  1039. // code, because the foo.wild.example.org will exist in the tree.
  1040. EXPECT_EQ(SUCCESS, zone_finder_.add(rr_not_wild_another_));
  1041. {
  1042. SCOPED_TRACE("Runnig with two entries under foo.wild.example.org");
  1043. doCancelWildcardTest();
  1044. }
  1045. }
  1046. TEST_F(InMemoryZoneFinderTest, loadBadWildcard) {
  1047. // We reject loading the zone if it contains a wildcard name for
  1048. // NS or DNAME.
  1049. EXPECT_THROW(zone_finder_.add(rr_nswild_), InMemoryZoneFinder::AddError);
  1050. EXPECT_THROW(zone_finder_.add(rr_dnamewild_),
  1051. InMemoryZoneFinder::AddError);
  1052. }
  1053. TEST_F(InMemoryZoneFinderTest, swap) {
  1054. // build one zone finder with some data
  1055. InMemoryZoneFinder finder1(class_, origin_);
  1056. EXPECT_EQ(result::SUCCESS, finder1.add(rr_ns_));
  1057. EXPECT_EQ(result::SUCCESS, finder1.add(rr_ns_aaaa_));
  1058. // build another zone finder of a different RR class with some other data
  1059. const Name other_origin("version.bind");
  1060. ASSERT_NE(origin_, other_origin); // make sure these two are different
  1061. InMemoryZoneFinder finder2(RRClass::CH(), other_origin);
  1062. EXPECT_EQ(result::SUCCESS,
  1063. finder2.add(textToRRset("version.bind. 0 CH TXT \"test\"",
  1064. RRClass::CH())));
  1065. finder1.swap(finder2);
  1066. EXPECT_EQ(other_origin, finder1.getOrigin());
  1067. EXPECT_EQ(origin_, finder2.getOrigin());
  1068. EXPECT_EQ(RRClass::CH(), finder1.getClass());
  1069. EXPECT_EQ(RRClass::IN(), finder2.getClass());
  1070. // make sure the zone data is swapped, too
  1071. findTest(origin_, RRType::NS(), ZoneFinder::NXDOMAIN, false,
  1072. ConstRRsetPtr(), &finder1);
  1073. findTest(other_origin, RRType::TXT(), ZoneFinder::SUCCESS, false,
  1074. ConstRRsetPtr(), &finder1);
  1075. findTest(origin_, RRType::NS(), ZoneFinder::SUCCESS, false,
  1076. ConstRRsetPtr(), &finder2);
  1077. findTest(other_origin, RRType::TXT(), ZoneFinder::NXDOMAIN, false,
  1078. ConstRRsetPtr(), &finder2);
  1079. }
  1080. TEST_F(InMemoryZoneFinderTest, getFileName) {
  1081. // for an empty zone the file name should also be empty.
  1082. EXPECT_TRUE(zone_finder_.getFileName().empty());
  1083. // if loading a zone fails the file name shouldn't be set.
  1084. EXPECT_THROW(zone_finder_.load(TEST_DATA_DIR "/root.zone"),
  1085. MasterLoadError);
  1086. EXPECT_TRUE(zone_finder_.getFileName().empty());
  1087. // after a successful load, the specified file name should be set
  1088. InMemoryZoneFinder rootzone(class_, Name("."));
  1089. EXPECT_NO_THROW(rootzone.load(TEST_DATA_DIR "/root.zone"));
  1090. EXPECT_EQ(TEST_DATA_DIR "/root.zone", rootzone.getFileName());
  1091. // overriding load, which will fail
  1092. EXPECT_THROW(rootzone.load(TEST_DATA_DIR "/duplicate_rrset.zone"),
  1093. MasterLoadError);
  1094. // the file name should be intact.
  1095. EXPECT_EQ(TEST_DATA_DIR "/root.zone", rootzone.getFileName());
  1096. // After swap, file names should also be swapped.
  1097. zone_finder_.swap(rootzone);
  1098. EXPECT_EQ(TEST_DATA_DIR "/root.zone", zone_finder_.getFileName());
  1099. EXPECT_TRUE(rootzone.getFileName().empty());
  1100. }
  1101. TEST_F(InMemoryZoneFinderTest, addRRsig) {
  1102. // A simple valid case: adding an RRset to be signed followed by an RRSIG
  1103. // that covers the first RRset
  1104. zone_finder_.add(rr_a_);
  1105. zone_finder_.add(textToRRset(rrsig_a_txt));
  1106. ZoneFinder::FindResult result = zone_finder_.find(origin_, RRType::A(),
  1107. ZoneFinder::FIND_DNSSEC);
  1108. EXPECT_EQ(ZoneFinder::SUCCESS, result.code);
  1109. ASSERT_TRUE(result.rrset);
  1110. ASSERT_TRUE(result.rrset->getRRsig());
  1111. actual_rrsets_.push_back(result.rrset->getRRsig());
  1112. rrsetsCheck(rrsig_a_txt, actual_rrsets_.begin(), actual_rrsets_.end());
  1113. // Confirm a separate RRISG for a different type can be added
  1114. actual_rrsets_.clear();
  1115. zone_finder_.add(rr_ns_);
  1116. zone_finder_.add(textToRRset(rrsig_ns_txt));
  1117. ZoneFinder::FindResult result2 =
  1118. zone_finder_.find(origin_, RRType::NS(), ZoneFinder::FIND_DNSSEC);
  1119. EXPECT_EQ(ZoneFinder::SUCCESS, result2.code);
  1120. ASSERT_TRUE(result2.rrset);
  1121. ASSERT_TRUE(result2.rrset->getRRsig());
  1122. actual_rrsets_.push_back(result2.rrset->getRRsig());
  1123. rrsetsCheck(rrsig_ns_txt, actual_rrsets_.begin(), actual_rrsets_.end());
  1124. // Check a case with multiple RRSIGs
  1125. actual_rrsets_.clear();
  1126. zone_finder_.add(rr_ns_aaaa_);
  1127. zone_finder_.add(textToRRset(rrsig_aaaa_txt));
  1128. ZoneFinder::FindResult result3 =
  1129. zone_finder_.find(Name("ns.example.org"), RRType::AAAA(),
  1130. ZoneFinder::FIND_DNSSEC);
  1131. EXPECT_EQ(ZoneFinder::SUCCESS, result3.code);
  1132. ASSERT_TRUE(result3.rrset);
  1133. ASSERT_TRUE(result3.rrset->getRRsig());
  1134. actual_rrsets_.push_back(result3.rrset->getRRsig());
  1135. rrsetsCheck(rrsig_aaaa_txt, actual_rrsets_.begin(), actual_rrsets_.end());
  1136. }
  1137. TEST_F(InMemoryZoneFinderTest, addRRsigWithoutCovered) {
  1138. // The current implementation rejects attempts of adding RRSIG without
  1139. // covered RRsets already in the zone.
  1140. // Name doesn't exist
  1141. EXPECT_THROW(zone_finder_.add(
  1142. textToRRset("notexist.example.org. 300 IN RRSIG A 5 3 "
  1143. "3600 20000101000000 20000201000000 12345 "
  1144. "example.org. FAKEFAKEFAKE\n")),
  1145. InMemoryZoneFinder::AddError);
  1146. // Name exists, but is empty.
  1147. zone_finder_.add(rr_emptywild_);
  1148. EXPECT_THROW(zone_finder_.add(
  1149. textToRRset("foo.example.org. 300 IN RRSIG A 5 3 "
  1150. "3600 20000101000000 20000201000000 12345 "
  1151. "example.org. FAKEFAKEFAKE\n")),
  1152. InMemoryZoneFinder::AddError);
  1153. // Add RRSIG RRset without covered RR
  1154. zone_finder_.add(rr_a_);
  1155. EXPECT_THROW(zone_finder_.add(textToRRset(rrsig_ns_txt)),
  1156. InMemoryZoneFinder::AddError);
  1157. }
  1158. TEST_F(InMemoryZoneFinderTest, addbadRRsig) {
  1159. // Tests with other types of bogus input
  1160. // Empty RRSIG RRset.
  1161. EXPECT_THROW(zone_finder_.add(RRsetPtr(new RRset(origin_, class_,
  1162. RRType::RRSIG(),
  1163. RRTTL(300)))),
  1164. InMemoryZoneFinder::AddError);
  1165. // RRSIG with mixed covered types
  1166. zone_finder_.add(rr_a_); // make sure the covered name exists
  1167. // textToRRset() doesn't work as intended for this pathological case,
  1168. // so we need to construct the RRset by hand.
  1169. RRsetPtr rrset(new RRset(origin_, class_, RRType::RRSIG(), RRTTL(300)));
  1170. rrset->addRdata(generic::RRSIG("A 5 3 3600 20000101000000 20000201000000 "
  1171. "12345 example.org. FAKEFAKEFAKE"));
  1172. rrset->addRdata(generic::RRSIG("NS 5 3 3600 20000101000000 20000201000000 "
  1173. "54321 example.org. FAKEFAKEFAKEFAKE"));
  1174. EXPECT_THROW(zone_finder_.add(rrset), InMemoryZoneFinder::AddError);
  1175. // An attempt of overriding an existing RRSIG. The current implementation
  1176. // prohibits that.
  1177. zone_finder_.add(textToRRset(rrsig_a_txt));
  1178. EXPECT_THROW(zone_finder_.add(textToRRset(rrsig_a_txt)),
  1179. InMemoryZoneFinder::AddError);
  1180. }
  1181. //
  1182. // (Faked) NSEC3 hash data. Arbitrarily borrowed from RFC515 examples.
  1183. //
  1184. // Commonly used NSEC3 suffix. It's incorrect to use it for all NSEC3s, but
  1185. // doesn't matter for the purpose of our tests.
  1186. const char* const nsec3_common = " 300 IN NSEC3 1 1 12 aabbccdd "
  1187. "2T7B4G4VSA5SMI47K61MV5BV1A22BOJR A RRSIG";
  1188. // Likewise, common RRSIG suffix for NSEC3s.
  1189. const char* const nsec3_rrsig_common = " 300 IN RRSIG NSEC3 5 3 3600 "
  1190. "20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE";
  1191. // For apex (example.org)
  1192. const char* const apex_hash = "0P9MHAVEQVM6T7VBL5LOP2U3T2RP3TOM";
  1193. const char* const apex_hash_lower = "0p9mhaveqvm6t7vbl5lop2u3t2rp3tom";
  1194. // For ns1.example.org
  1195. const char* const ns1_hash = "2T7B4G4VSA5SMI47K61MV5BV1A22BOJR";
  1196. // For x.y.w.example.org (lower-cased)
  1197. const char* const xrw_hash = "2vptu5timamqttgl4luu9kg21e0aor3s";
  1198. void
  1199. nsec3Check(bool expected_matched, const string& expected_rrsets_txt,
  1200. const ZoneFinder::FindNSEC3Result& result,
  1201. bool expected_sig = false)
  1202. {
  1203. vector<ConstRRsetPtr> actual_rrsets;
  1204. EXPECT_EQ(expected_matched, result.matched);
  1205. ASSERT_TRUE(result.closest_proof);
  1206. if (expected_sig) {
  1207. ASSERT_TRUE(result.closest_proof->getRRsig());
  1208. }
  1209. actual_rrsets.push_back(result.closest_proof);
  1210. if (expected_sig) {
  1211. actual_rrsets.push_back(result.closest_proof->getRRsig());
  1212. }
  1213. rrsetsCheck(expected_rrsets_txt, actual_rrsets.begin(),
  1214. actual_rrsets.end());
  1215. }
  1216. // In the following tests we use a temporary faked version of findNSEC3
  1217. // as the real version isn't implemented yet (it's a task for #1577).
  1218. // When #1577 is done the tests should be updated using the real version.
  1219. // If we can use fake hash calculator (see #1575), we should be able to
  1220. // just replace findNSEC3Tmp with findNSEC3.
  1221. TEST_F(InMemoryZoneFinderTest, addNSEC3) {
  1222. const string nsec3_text = string(apex_hash) + ".example.org." +
  1223. string(nsec3_common);
  1224. // This name shouldn't be found in the normal domain tree.
  1225. EXPECT_EQ(result::SUCCESS, zone_finder_.add(textToRRset(nsec3_text)));
  1226. EXPECT_EQ(ZoneFinder::NXDOMAIN,
  1227. zone_finder_.find(Name(string(apex_hash) + ".example.org"),
  1228. RRType::NSEC3()).code);
  1229. // Dedicated NSEC3 find should be able to find it.
  1230. nsec3Check(true, nsec3_text,
  1231. zone_finder_.findNSEC3Tmp(Name("example.org"), false));
  1232. // This implementation rejects duplicate/update add of the same hash name
  1233. EXPECT_EQ(result::EXIST,
  1234. zone_finder_.add(textToRRset(
  1235. string(apex_hash) + ".example.org." +
  1236. string(nsec3_common) + " AAAA")));
  1237. // The original NSEC3 should be intact
  1238. nsec3Check(true, nsec3_text,
  1239. zone_finder_.findNSEC3Tmp(Name("example.org"), false));
  1240. // NSEC3-like name but of ordinary RR type should go to normal tree.
  1241. const string nonsec3_text = string(apex_hash) + ".example.org. " +
  1242. "300 IN A 192.0.2.1";
  1243. EXPECT_EQ(result::SUCCESS, zone_finder_.add(textToRRset(nonsec3_text)));
  1244. EXPECT_EQ(ZoneFinder::SUCCESS,
  1245. zone_finder_.find(Name(string(apex_hash) + ".example.org"),
  1246. RRType::A()).code);
  1247. }
  1248. TEST_F(InMemoryZoneFinderTest, addNSEC3Lower) {
  1249. // Similar to the previous case, but NSEC3 owner name is lower-cased.
  1250. const string nsec3_text = string(apex_hash_lower) + ".example.org." +
  1251. string(nsec3_common);
  1252. EXPECT_EQ(result::SUCCESS, zone_finder_.add(textToRRset(nsec3_text)));
  1253. nsec3Check(true, nsec3_text,
  1254. zone_finder_.findNSEC3Tmp(Name("example.org"), false));
  1255. }
  1256. TEST_F(InMemoryZoneFinderTest, addNSEC3Ordering) {
  1257. // Check that the internal storage ensures comparison based on the NSEC3
  1258. // semantics, regardless of the add order or the letter-case of hash.
  1259. // Adding "0P..", "2v..", then "2T..".
  1260. const string smallest = string(apex_hash) + ".example.org." +
  1261. string(nsec3_common);
  1262. const string middle = string(ns1_hash) + ".example.org." +
  1263. string(nsec3_common);
  1264. const string largest = string(xrw_hash) + ".example.org." +
  1265. string(nsec3_common);
  1266. zone_finder_.add(textToRRset(smallest));
  1267. zone_finder_.add(textToRRset(largest));
  1268. zone_finder_.add(textToRRset(middle));
  1269. // Then look for NSEC3 that covers a name whose hash is "2S.."
  1270. // The covering NSEC3 should be "0P.."
  1271. nsec3Check(false, smallest,
  1272. zone_finder_.findNSEC3Tmp(Name("www.example.org"), false));
  1273. // Look for NSEC3 that covers names whose hash are "Q0.." and "0A.."
  1274. // The covering NSEC3 should be "2v.." in both cases
  1275. nsec3Check(false, largest,
  1276. zone_finder_.findNSEC3Tmp(Name("xxx.example.org"), false));
  1277. nsec3Check(false, largest,
  1278. zone_finder_.findNSEC3Tmp(Name("yyy.example.org"), false));
  1279. }
  1280. TEST_F(InMemoryZoneFinderTest, badNSEC3Name) {
  1281. // Our implementation refuses to load NSEC3 at a wildcard name
  1282. EXPECT_THROW(zone_finder_.add(textToRRset("*.example.org." +
  1283. string(nsec3_common))),
  1284. InMemoryZoneFinder::AddError);
  1285. // Likewise, if the owner name of NSEC3 has too many labels, it's refused.
  1286. EXPECT_THROW(zone_finder_.add(textToRRset("a." + string(apex_hash) +
  1287. ".example.org." +
  1288. string(nsec3_common))),
  1289. InMemoryZoneFinder::AddError);
  1290. }
  1291. TEST_F(InMemoryZoneFinderTest, addMultiNSEC3) {
  1292. // In this current implementation multiple NSEC3 RDATA isn't supported.
  1293. RRsetPtr nsec3(new RRset(Name(string(apex_hash) + ".example.org"),
  1294. RRClass::IN(), RRType::NSEC3(), RRTTL(300)));
  1295. nsec3->addRdata(
  1296. generic::NSEC3("1 0 12 aabbccdd 2T7B4G4VSA5SMI47K61MV5BV1A22BOJR A"));
  1297. nsec3->addRdata(
  1298. generic::NSEC3("1 1 1 ddccbbaa 2T7B4G4VSA5SMI47K61MV5BV1A22BOJR A"));
  1299. EXPECT_THROW(zone_finder_.add(nsec3), InMemoryZoneFinder::AddError);
  1300. }
  1301. TEST_F(InMemoryZoneFinderTest, addNSEC3WithRRSIG) {
  1302. // Adding NSEC3 and its RRSIG
  1303. const string nsec3_text = string(apex_hash) + ".example.org." +
  1304. string(nsec3_common);
  1305. EXPECT_EQ(result::SUCCESS, zone_finder_.add(textToRRset(nsec3_text)));
  1306. const string nsec3_rrsig_text = string(apex_hash) + ".example.org." +
  1307. string(nsec3_rrsig_common);
  1308. EXPECT_EQ(result::SUCCESS, zone_finder_.add(textToRRset(nsec3_rrsig_text)));
  1309. // Then look for it. The NSEC3 should have the RRSIG that was just added.
  1310. nsec3Check(true, nsec3_text + "\n" + nsec3_rrsig_text,
  1311. zone_finder_.findNSEC3Tmp(Name("example.org"), false), true);
  1312. // Duplicate add of RRSIG for the same NSEC3 is prohibited.
  1313. EXPECT_THROW(zone_finder_.add(textToRRset(nsec3_rrsig_text)),
  1314. InMemoryZoneFinder::AddError);
  1315. // Same check using the lower-cased name. This also confirms matching
  1316. // is case-insensitive.
  1317. EXPECT_THROW(zone_finder_.add(textToRRset(string(apex_hash_lower) +
  1318. ".example.org."
  1319. + string(nsec3_rrsig_common))),
  1320. InMemoryZoneFinder::AddError);
  1321. }
  1322. TEST_F(InMemoryZoneFinderTest, badRRsigForNSEC3) {
  1323. // adding RRSIG for NSEC3 even before adding any NSEC3 (internally,
  1324. // a space for NSEC3 namespace isn't yet allocated)
  1325. EXPECT_THROW(zone_finder_.add(textToRRset(string(apex_hash) +
  1326. ".example.org." +
  1327. string(nsec3_rrsig_common))),
  1328. InMemoryZoneFinder::AddError);
  1329. // Add an NSEC3
  1330. EXPECT_EQ(result::SUCCESS, zone_finder_.add(
  1331. textToRRset(string(apex_hash) + ".example.org." +
  1332. string(nsec3_common))));
  1333. // Then add an NSEC3 for a non existent NSEC3. It should fail in the
  1334. // current implementation.
  1335. EXPECT_THROW(zone_finder_.add(textToRRset(string(ns1_hash) +
  1336. ".example.org." +
  1337. string(nsec3_rrsig_common))),
  1338. InMemoryZoneFinder::AddError);
  1339. }
  1340. TEST_F(InMemoryZoneFinderTest, paramConsistencyWithNSEC3PARAM) {
  1341. // First, add an NSEC3PARAM RR
  1342. EXPECT_EQ(result::SUCCESS,
  1343. zone_finder_.add(textToRRset("example.org. 300 IN NSEC3PARAM "
  1344. "1 0 12 aabbccdd")));
  1345. // Adding an NSEC3 that has matching parameters is okay.
  1346. EXPECT_EQ(result::SUCCESS, zone_finder_.add(
  1347. textToRRset(string(apex_hash) + ".example.org." +
  1348. string(nsec3_common))));
  1349. // NSEC3 with inconsistent parameter will be rejected
  1350. EXPECT_THROW(zone_finder_.add(
  1351. textToRRset("a.example.org. 300 IN NSEC3 1 0 1 aabbccdd "
  1352. "2T7B4G4VSA5SMI47K61MV5BV1A22BOJR A RRSIG")),
  1353. InMemoryZoneFinder::AddError);
  1354. }
  1355. TEST_F(InMemoryZoneFinderTest, paramConsistencyWithNSEC3) {
  1356. // Add an NSEC3 without adding NSEC3PARAM
  1357. EXPECT_EQ(result::SUCCESS, zone_finder_.add(
  1358. textToRRset(string(apex_hash) + ".example.org." +
  1359. string(nsec3_common))));
  1360. // Adding an NSEC3 with inconsistent parameter will be rejected at this pt.
  1361. EXPECT_THROW(zone_finder_.add(
  1362. textToRRset("a.example.org. 300 IN NSEC3 1 0 1 aabbccdd "
  1363. "2T7B4G4VSA5SMI47K61MV5BV1A22BOJR A RRSIG")),
  1364. InMemoryZoneFinder::AddError);
  1365. // Likewise, NSEC3PARAM with inconsistent parameter will be rejected.
  1366. EXPECT_THROW(zone_finder_.add(textToRRset("example.org. 300 IN NSEC3PARAM "
  1367. "1 0 1 aabbccdd")),
  1368. InMemoryZoneFinder::AddError);
  1369. }
  1370. TEST_F(InMemoryZoneFinderTest, multiNSEC3PARAM) {
  1371. // In this current implementation multiple NSEC3PARAM isn't supported.
  1372. RRsetPtr nsec3param(new RRset(Name("example.org"), RRClass::IN(),
  1373. RRType::NSEC3PARAM(), RRTTL(300)));
  1374. nsec3param->addRdata(generic::NSEC3PARAM("1 0 12 aabbccdd"));
  1375. nsec3param->addRdata(generic::NSEC3PARAM("1 1 1 ddccbbaa"));
  1376. EXPECT_THROW(zone_finder_.add(nsec3param), InMemoryZoneFinder::AddError);
  1377. }
  1378. TEST_F(InMemoryZoneFinderTest, nonOriginNSEC3PARAM) {
  1379. // This is a normal NSEC3PARAM at the zone origin
  1380. EXPECT_EQ(result::SUCCESS,
  1381. zone_finder_.add(textToRRset("example.org. 300 IN NSEC3PARAM "
  1382. "1 0 12 aabbccdd")));
  1383. // Add another (with different param) at a non origin node. This is
  1384. // awkward, but the implementation accepts it as an ordinary RR.
  1385. EXPECT_EQ(result::SUCCESS,
  1386. zone_finder_.add(textToRRset("a.example.org. 300 IN NSEC3PARAM "
  1387. "1 1 1 aabbccdd")));
  1388. }
  1389. TEST_F(InMemoryZoneFinderTest, loadNSEC3Zone) {
  1390. // Check if it can load validly NSEC3-signed zone. At this moment
  1391. // it's sufficient to see it doesn't crash
  1392. zone_finder_.load(TEST_DATA_DIR "/example.org.nsec3-signed");
  1393. // Reload the zone with a version that doesn't have NSEC3PARAM.
  1394. // This is an abnormal case, but the implementation accepts it.
  1395. zone_finder_.load(TEST_DATA_DIR "/example.org.nsec3-signed-noparam");
  1396. }
  1397. }