zone_finder_unittest.cc 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615
  1. // Copyright (C) 2012 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 "memory_segment_test.h"
  15. #include "zone_table_segment_test.h"
  16. // NOTE: this faked_nsec3 inclusion (and all related code below)
  17. // was ported during #2109 for the convenience of implementing #2218
  18. // In #2218 the NSEC3 test code in this file is expected to be finalized.
  19. // In #2219 the original is expected to be removed, and this file should
  20. // probably be moved here (and any leftover code not handled in #2218 should
  21. // be cleaned up)
  22. #include "../../tests/faked_nsec3.h"
  23. #include <datasrc/memory/zone_finder.h>
  24. #include <datasrc/memory/zone_data_updater.h>
  25. #include <datasrc/memory/rdata_serialization.h>
  26. #include <datasrc/memory/zone_table_segment.h>
  27. #include <datasrc/memory/memory_client.h>
  28. #include <datasrc/data_source.h>
  29. #include <datasrc/client.h>
  30. #include <testutils/dnsmessage_test.h>
  31. #include <boost/foreach.hpp>
  32. #include <gtest/gtest.h>
  33. #include <string>
  34. using namespace std;
  35. using namespace isc::dns;
  36. using namespace isc::dns::rdata;
  37. using namespace isc::datasrc;
  38. using namespace isc::testutils;
  39. using boost::shared_ptr;
  40. using namespace isc::datasrc::test;
  41. using namespace isc::datasrc::memory::test;
  42. using namespace isc::datasrc::memory;
  43. namespace {
  44. // Commonly used result codes (Who should write the prefix all the time)
  45. using result::SUCCESS;
  46. using result::EXIST;
  47. /// \brief expensive rrset converter
  48. ///
  49. /// converts any specialized rrset (which may not have implemented some
  50. /// methods for efficiency) into a 'full' RRsetPtr, for easy use in test
  51. /// checks.
  52. ///
  53. /// Done very inefficiently through text representation, speed should not
  54. /// be a concern here.
  55. ConstRRsetPtr
  56. convertRRset(ConstRRsetPtr src) {
  57. return (textToRRset(src->toText()));
  58. }
  59. /// \brief Test fixture for the InMemoryZoneFinder class
  60. class InMemoryZoneFinderTest : public ::testing::Test {
  61. // A straightforward pair of textual RR(set) and a RRsetPtr variable
  62. // to store the RRset. Used to build test data below.
  63. struct RRsetData {
  64. const char* const text; // textual representation of an RRset
  65. RRsetPtr* rrset;
  66. };
  67. protected:
  68. // The following sub tests are shared by multiple test cases, changing
  69. // the zone's DNSSEC status (unsigned, NSEC-signed or NSEC3-signed).
  70. // expected_flags is set to either RESULT_NSEC_SIGNED or
  71. // RESULT_NSEC3_SIGNED when it's NSEC/NSEC3 signed respectively and
  72. // find() is expected to set the corresponding flags.
  73. // find_options should be set to FIND_DNSSEC for NSEC-signed case when
  74. // NSEC is expected to be returned.
  75. void findCheck(ZoneFinder::FindResultFlags expected_flags =
  76. ZoneFinder::RESULT_DEFAULT,
  77. ZoneFinder::FindOptions find_options =
  78. ZoneFinder::FIND_DEFAULT);
  79. void emptyNodeCheck(ZoneFinder::FindResultFlags expected_flags =
  80. ZoneFinder::RESULT_DEFAULT);
  81. void wildcardCheck(ZoneFinder::FindResultFlags expected_flags =
  82. ZoneFinder::RESULT_DEFAULT,
  83. ZoneFinder::FindOptions find_options =
  84. ZoneFinder::FIND_DEFAULT);
  85. void doCancelWildcardCheck(ZoneFinder::FindResultFlags expected_flags =
  86. ZoneFinder::RESULT_DEFAULT,
  87. ZoneFinder::FindOptions find_options =
  88. ZoneFinder::FIND_DEFAULT);
  89. void anyWildcardCheck(ZoneFinder::FindResultFlags expected_flags =
  90. ZoneFinder::RESULT_DEFAULT);
  91. void emptyWildcardCheck(ZoneFinder::FindResultFlags expected_flags =
  92. ZoneFinder::RESULT_DEFAULT);
  93. void findNSECENTCheck(const Name& ent_name,
  94. ConstRRsetPtr expected_nsec,
  95. ZoneFinder::FindResultFlags expected_flags =
  96. ZoneFinder::RESULT_DEFAULT);
  97. public:
  98. InMemoryZoneFinderTest() :
  99. class_(RRClass::IN()),
  100. origin_("example.org"),
  101. zone_data_(ZoneData::create(mem_sgmt_, origin_)),
  102. zone_finder_(*zone_data_, class_),
  103. updater_(mem_sgmt_, class_, origin_, *zone_data_)
  104. {
  105. // Build test RRsets. Below, we construct an RRset for
  106. // each textual RR(s) of zone_data, and assign it to the corresponding
  107. // rr_xxx.
  108. // Note that this contains an out-of-zone RR, and due to the
  109. // validation check of masterLoad() used below, we cannot add SOA.
  110. const RRsetData zone_data[] = {
  111. {"example.org. 300 IN NS ns.example.org.", &rr_ns_},
  112. {"example.org. 300 IN A 192.0.2.1", &rr_a_},
  113. {"ns.example.org. 300 IN A 192.0.2.2", &rr_ns_a_},
  114. // This one will place rr_ns_a_ at a zone cut, making it a glue:
  115. {"ns.example.org. 300 IN NS 192.0.2.2", &rr_ns_ns_},
  116. {"ns.example.org. 300 IN AAAA 2001:db8::2", &rr_ns_aaaa_},
  117. {"cname.example.org. 300 IN CNAME canonical.example.org",
  118. &rr_cname_},
  119. {"cname.example.org. 300 IN A 192.0.2.3", &rr_cname_a_},
  120. {"dname.example.org. 300 IN DNAME target.example.org.",
  121. &rr_dname_},
  122. {"dname.example.org. 300 IN A 192.0.2.39", &rr_dname_a_},
  123. {"dname.example.org. 300 IN NS ns.dname.example.org.",
  124. &rr_dname_ns_},
  125. {"example.org. 300 IN DNAME example.com.", &rr_dname_apex_},
  126. {"child.example.org. 300 IN NS ns.child.example.org.",
  127. &rr_child_ns_},
  128. {"child.example.org. 300 IN DS 12345 5 1 DEADBEEF",
  129. &rr_child_ds_},
  130. {"ns.child.example.org. 300 IN A 192.0.2.153",
  131. &rr_child_glue_},
  132. {"grand.child.example.org. 300 IN NS ns.grand.child.example.org.",
  133. &rr_grandchild_ns_},
  134. {"ns.grand.child.example.org. 300 IN AAAA 2001:db8::253",
  135. &rr_grandchild_glue_},
  136. {"dname.child.example.org. 300 IN DNAME example.com.",
  137. &rr_child_dname_},
  138. {"example.com. 300 IN A 192.0.2.10", &rr_out_},
  139. {"*.wild.example.org. 300 IN A 192.0.2.1", &rr_wild_},
  140. {"*.cnamewild.example.org. 300 IN CNAME canonial.example.org.",
  141. &rr_cnamewild_},
  142. {"foo.wild.example.org. 300 IN A 192.0.2.3", &rr_under_wild_},
  143. {"wild.*.foo.example.org. 300 IN A 192.0.2.1", &rr_emptywild_},
  144. {"wild.*.foo.*.bar.example.org. 300 IN A 192.0.2.1",
  145. &rr_nested_emptywild_},
  146. {"*.nswild.example.org. 300 IN NS nswild.example.", &rr_nswild_},
  147. {"*.dnamewild.example.org. 300 IN DNAME dnamewild.example.",
  148. &rr_dnamewild_},
  149. {"*.child.example.org. 300 IN A 192.0.2.1", &rr_child_wild_},
  150. {"bar.foo.wild.example.org. 300 IN A 192.0.2.2", &rr_not_wild_},
  151. {"baz.foo.wild.example.org. 300 IN A 192.0.2.3",
  152. &rr_not_wild_another_},
  153. {"0P9MHAVEQVM6T7VBL5LOP2U3T2RP3TOM.example.org. 300 IN "
  154. "NSEC3 1 1 12 aabbccdd 2T7B4G4VSA5SMI47K61MV5BV1A22BOJR A RRSIG",
  155. &rr_nsec3_},
  156. {"example.org. 300 IN NSEC wild.*.foo.example.org. "
  157. "NS SOA RRSIG NSEC DNSKEY", &rr_nsec_},
  158. // Together with the apex NSEC, these next NSECs make a complete
  159. // chain in the case of the zone for the emptyNonterminal tests
  160. // (We may want to clean up this generator code and/or masterLoad
  161. // so that we can prepare conflicting datasets better)
  162. {"wild.*.foo.example.org. 3600 IN NSEC ns.example.org. "
  163. "A RRSIG NSEC", &rr_ent_nsec2_},
  164. {"ns.example.org. 3600 IN NSEC foo.wild.example.org. A RRSIG NSEC",
  165. &rr_ent_nsec3_},
  166. {"foo.wild.example.org. 3600 IN NSEC example.org. A RRSIG NSEC",
  167. &rr_ent_nsec4_},
  168. // And these are NSECs used in different tests
  169. {"ns.example.org. 300 IN NSEC *.nswild.example.org. A AAAA NSEC",
  170. &rr_ns_nsec_},
  171. {"*.wild.example.org. 300 IN NSEC foo.wild.example.org. A NSEC",
  172. &rr_wild_nsec_},
  173. {NULL, NULL}
  174. };
  175. for (unsigned int i = 0; zone_data[i].text != NULL; ++i) {
  176. *zone_data[i].rrset = textToRRset(zone_data[i].text);
  177. }
  178. }
  179. ~InMemoryZoneFinderTest() {
  180. // Make sure we reset the hash creator to the default
  181. setNSEC3HashCreator(NULL);
  182. ZoneData::destroy(mem_sgmt_, zone_data_, RRClass::IN());
  183. }
  184. void addToZoneData(const ConstRRsetPtr rrset) {
  185. updater_.add(rrset, rrset->getRRsig());
  186. }
  187. // Some data to test with
  188. const RRClass class_;
  189. const Name origin_;
  190. // The zone finder to torture by tests
  191. MemorySegmentTest mem_sgmt_;
  192. memory::ZoneData* zone_data_;
  193. memory::InMemoryZoneFinder zone_finder_;
  194. ZoneDataUpdater updater_;
  195. // Placeholder for storing RRsets to be checked with rrsetsCheck()
  196. vector<ConstRRsetPtr> actual_rrsets_;
  197. /*
  198. * Some RRsets to put inside the zone.
  199. */
  200. RRsetPtr
  201. // Out of zone RRset
  202. rr_out_,
  203. // NS of example.org
  204. rr_ns_,
  205. // A of ns.example.org
  206. rr_ns_a_,
  207. // AAAA of ns.example.org
  208. rr_ns_aaaa_,
  209. // A of example.org
  210. rr_a_;
  211. RRsetPtr rr_ns_ns_; // used to make rr_ns_a_ a glue.
  212. RRsetPtr rr_cname_; // CNAME in example.org (RDATA will be added)
  213. RRsetPtr rr_cname_a_; // for mixed CNAME + A case
  214. RRsetPtr rr_dname_; // DNAME in example.org (RDATA will be added)
  215. RRsetPtr rr_dname_a_; // for mixed DNAME + A case
  216. RRsetPtr rr_dname_ns_; // for mixed DNAME + NS case
  217. RRsetPtr rr_dname_apex_; // for mixed DNAME + NS case in the apex
  218. RRsetPtr rr_child_ns_; // NS of a child domain (for delegation)
  219. RRsetPtr rr_child_ds_; // DS of a child domain (for delegation, auth data)
  220. RRsetPtr rr_child_glue_; // glue RR of the child domain
  221. RRsetPtr rr_grandchild_ns_; // NS below a zone cut (unusual)
  222. RRsetPtr rr_grandchild_glue_; // glue RR below a deeper zone cut
  223. RRsetPtr rr_child_dname_; // A DNAME under NS
  224. RRsetPtr rr_wild_; // Wildcard record
  225. RRsetPtr rr_cnamewild_; // CNAME at a wildcard
  226. RRsetPtr rr_emptywild_;
  227. RRsetPtr rr_nested_emptywild_;
  228. RRsetPtr rr_nswild_, rr_dnamewild_;
  229. RRsetPtr rr_child_wild_;
  230. RRsetPtr rr_under_wild_;
  231. RRsetPtr rr_not_wild_;
  232. RRsetPtr rr_not_wild_another_;
  233. RRsetPtr rr_nsec3_;
  234. RRsetPtr rr_nsec_;
  235. RRsetPtr rr_ent_nsec2_;
  236. RRsetPtr rr_ent_nsec3_;
  237. RRsetPtr rr_ent_nsec4_;
  238. RRsetPtr rr_ns_nsec_;
  239. RRsetPtr rr_wild_nsec_;
  240. /**
  241. * \brief Test one find query to the zone finder.
  242. *
  243. * Asks a query to the zone finder and checks it does not throw and returns
  244. * expected results. It returns nothing, it just signals failures
  245. * to GTEST.
  246. *
  247. * \param name The name to ask for.
  248. * \param rrtype The RRType to ask of.
  249. * \param result The expected code of the result.
  250. * \param check_answer Should a check against equality of the answer be
  251. * done?
  252. * \param answer The expected rrset, if any should be returned.
  253. * \param expected_flags The expected result flags returned via find().
  254. * These can be tested using isWildcard() etc.
  255. * \param zone_finder Check different InMemoryZoneFinder object than
  256. * zone_finder_ (if NULL, uses zone_finder_)
  257. * \param check_wild_answer Checks that the answer has the same RRs, type
  258. * class and TTL as the eqxpected answer and that the name corresponds
  259. * to the one searched. It is meant for checking answers for wildcard
  260. * queries.
  261. */
  262. void findTest(const Name& name, const RRType& rrtype,
  263. ZoneFinder::Result result,
  264. bool check_answer = true,
  265. const ConstRRsetPtr& answer = ConstRRsetPtr(),
  266. ZoneFinder::FindResultFlags expected_flags =
  267. ZoneFinder::RESULT_DEFAULT,
  268. memory::InMemoryZoneFinder* zone_finder = NULL,
  269. ZoneFinder::FindOptions options = ZoneFinder::FIND_DEFAULT,
  270. bool check_wild_answer = false)
  271. {
  272. SCOPED_TRACE("findTest for " + name.toText() + "/" + rrtype.toText());
  273. if (zone_finder == NULL) {
  274. zone_finder = &zone_finder_;
  275. }
  276. const ConstRRsetPtr answer_sig = answer ? answer->getRRsig() :
  277. RRsetPtr(); // note we use the same type as of retval of getRRsig()
  278. // The whole block is inside, because we need to check the result and
  279. // we can't assign to FindResult
  280. EXPECT_NO_THROW({
  281. ZoneFinderContextPtr find_result(zone_finder->find(
  282. name, rrtype, options));
  283. // Check it returns correct answers
  284. EXPECT_EQ(result, find_result->code);
  285. EXPECT_EQ((expected_flags & ZoneFinder::RESULT_WILDCARD) != 0,
  286. find_result->isWildcard());
  287. EXPECT_EQ((expected_flags & ZoneFinder::RESULT_NSEC_SIGNED)
  288. != 0, find_result->isNSECSigned());
  289. EXPECT_EQ((expected_flags & ZoneFinder::RESULT_NSEC3_SIGNED)
  290. != 0, find_result->isNSEC3Signed());
  291. if (check_answer) {
  292. if (!answer) {
  293. ASSERT_FALSE(find_result->rrset);
  294. } else {
  295. ASSERT_TRUE(find_result->rrset);
  296. ConstRRsetPtr result_rrset(
  297. convertRRset(find_result->rrset));
  298. rrsetCheck(answer, result_rrset);
  299. if (answer_sig &&
  300. (options & ZoneFinder::FIND_DNSSEC) != 0) {
  301. ASSERT_TRUE(result_rrset->getRRsig());
  302. rrsetCheck(answer_sig, result_rrset->getRRsig());
  303. } else {
  304. EXPECT_FALSE(result_rrset->getRRsig());
  305. }
  306. }
  307. } else if (check_wild_answer) {
  308. ASSERT_NE(ConstRRsetPtr(), answer) <<
  309. "Wrong test, don't check for wild names if you expect "
  310. "empty answer";
  311. ASSERT_NE(ConstRRsetPtr(), find_result->rrset) <<
  312. "No answer found";
  313. // Build the expected answer using the given name and
  314. // other parameter of the base wildcard RRset.
  315. RRsetPtr wildanswer(new RRset(name, answer->getClass(),
  316. answer->getType(),
  317. answer->getTTL()));
  318. RdataIteratorPtr expectedIt(answer->getRdataIterator());
  319. for (; !expectedIt->isLast(); expectedIt->next()) {
  320. wildanswer->addRdata(expectedIt->getCurrent());
  321. }
  322. ConstRRsetPtr result_rrset(
  323. convertRRset(find_result->rrset));
  324. rrsetCheck(wildanswer, result_rrset);
  325. // Same for the RRSIG, if any.
  326. if (answer_sig) {
  327. ASSERT_TRUE(result_rrset->getRRsig());
  328. RRsetPtr wildsig(new RRset(name,
  329. answer_sig->getClass(),
  330. RRType::RRSIG(),
  331. answer_sig->getTTL()));
  332. RdataIteratorPtr expectedIt(
  333. answer_sig->getRdataIterator());
  334. for (; !expectedIt->isLast(); expectedIt->next()) {
  335. wildsig->addRdata(expectedIt->getCurrent());
  336. }
  337. rrsetCheck(wildsig, result_rrset->getRRsig());
  338. }
  339. }
  340. });
  341. }
  342. /**
  343. * \brief Calls the findAll on the finder and checks the result.
  344. */
  345. void findAllTest(const Name& name, ZoneFinder::Result result,
  346. const vector<ConstRRsetPtr>& expected_rrsets,
  347. ZoneFinder::FindResultFlags expected_flags =
  348. ZoneFinder::RESULT_DEFAULT,
  349. memory::InMemoryZoneFinder* finder = NULL,
  350. const ConstRRsetPtr &rrset_result = ConstRRsetPtr(),
  351. ZoneFinder::FindOptions options =
  352. ZoneFinder::FIND_DEFAULT)
  353. {
  354. if (finder == NULL) {
  355. finder = &zone_finder_;
  356. }
  357. std::vector<ConstRRsetPtr> target;
  358. ZoneFinderContextPtr find_result(finder->findAll(name, target,
  359. options));
  360. EXPECT_EQ(result, find_result->code);
  361. if (!rrset_result) {
  362. EXPECT_FALSE(find_result->rrset);
  363. } else {
  364. ASSERT_TRUE(find_result->rrset);
  365. rrsetCheck(rrset_result, convertRRset(find_result->rrset));
  366. }
  367. EXPECT_EQ((expected_flags & ZoneFinder::RESULT_WILDCARD) != 0,
  368. find_result->isWildcard());
  369. EXPECT_EQ((expected_flags & ZoneFinder::RESULT_NSEC_SIGNED)
  370. != 0, find_result->isNSECSigned());
  371. EXPECT_EQ((expected_flags & ZoneFinder::RESULT_NSEC3_SIGNED)
  372. != 0, find_result->isNSEC3Signed());
  373. // Convert all rrsets to 'full' ones before checking
  374. std::vector<ConstRRsetPtr> converted_rrsets;
  375. BOOST_FOREACH(ConstRRsetPtr cur_rrset, target) {
  376. converted_rrsets.push_back(convertRRset(cur_rrset));
  377. }
  378. rrsetsCheck(expected_rrsets.begin(), expected_rrsets.end(),
  379. converted_rrsets.begin(), converted_rrsets.end());
  380. }
  381. };
  382. /**
  383. * \brief Test InMemoryZoneFinder::InMemoryZoneFinder constructor.
  384. *
  385. * Takes the created zone finder and checks its properties they are the same
  386. * as passed parameters.
  387. */
  388. TEST_F(InMemoryZoneFinderTest, constructor) {
  389. ASSERT_EQ(origin_, zone_finder_.getOrigin());
  390. // Some unusual (abnormal case): if we add a super domain name of the
  391. // zone somehow, the label of the origin node won't be absolute.
  392. // getOrigin() should still be the correct one.
  393. ZoneNode *node;
  394. zone_data_->insertName(mem_sgmt_, Name("org"), &node);
  395. ASSERT_EQ(origin_, zone_finder_.getOrigin());
  396. }
  397. TEST_F(InMemoryZoneFinderTest, findCNAME) {
  398. // install CNAME RR
  399. addToZoneData(rr_cname_);
  400. // Find A RR of the same. Should match the CNAME
  401. findTest(rr_cname_->getName(), RRType::NS(), ZoneFinder::CNAME, true,
  402. rr_cname_);
  403. // Find the CNAME itself. Should result in normal SUCCESS
  404. findTest(rr_cname_->getName(), RRType::CNAME(), ZoneFinder::SUCCESS, true,
  405. rr_cname_);
  406. }
  407. TEST_F(InMemoryZoneFinderTest, findCNAMEUnderZoneCut) {
  408. // There's nothing special when we find a CNAME under a zone cut
  409. // (with FIND_GLUE_OK). The behavior is different from BIND 9,
  410. // so we test this case explicitly.
  411. addToZoneData(rr_child_ns_);
  412. ConstRRsetPtr rr_cname_under_cut_ = textToRRset(
  413. "cname.child.example.org. 300 IN CNAME target.child.example.org.");
  414. addToZoneData(rr_cname_under_cut_);
  415. findTest(Name("cname.child.example.org"), RRType::AAAA(),
  416. ZoneFinder::CNAME, true, rr_cname_under_cut_,
  417. ZoneFinder::RESULT_DEFAULT, NULL, ZoneFinder::FIND_GLUE_OK);
  418. }
  419. // Search under a DNAME record. It should return the DNAME
  420. TEST_F(InMemoryZoneFinderTest, findBelowDNAME) {
  421. EXPECT_NO_THROW(addToZoneData(rr_dname_));
  422. findTest(Name("below.dname.example.org"), RRType::A(), ZoneFinder::DNAME,
  423. true, rr_dname_);
  424. }
  425. // Search at the domain with DNAME. It should act as DNAME isn't there, DNAME
  426. // influences only the data below (see RFC 2672, section 3)
  427. TEST_F(InMemoryZoneFinderTest, findAtDNAME) {
  428. EXPECT_NO_THROW(addToZoneData(rr_dname_));
  429. EXPECT_NO_THROW(addToZoneData(rr_dname_a_));
  430. const Name dname_name(rr_dname_->getName());
  431. findTest(dname_name, RRType::A(), ZoneFinder::SUCCESS, true, rr_dname_a_);
  432. findTest(dname_name, RRType::DNAME(), ZoneFinder::SUCCESS, true,
  433. rr_dname_);
  434. findTest(dname_name, RRType::TXT(), ZoneFinder::NXRRSET, true);
  435. }
  436. // Try searching something that is both under NS and DNAME, without and with
  437. // GLUE_OK mode (it should stop at the NS and DNAME respectively).
  438. TEST_F(InMemoryZoneFinderTest, DNAMEUnderNS) {
  439. addToZoneData(rr_child_ns_);
  440. addToZoneData(rr_child_dname_);
  441. Name lowName("below.dname.child.example.org.");
  442. findTest(lowName, RRType::A(), ZoneFinder::DELEGATION, true, rr_child_ns_);
  443. findTest(lowName, RRType::A(), ZoneFinder::DNAME, true, rr_child_dname_,
  444. ZoneFinder::RESULT_DEFAULT, NULL, ZoneFinder::FIND_GLUE_OK);
  445. }
  446. // Test adding child zones and zone cut handling
  447. TEST_F(InMemoryZoneFinderTest, delegationNS) {
  448. // add in-zone data
  449. EXPECT_NO_THROW(addToZoneData(rr_ns_));
  450. // install a zone cut
  451. EXPECT_NO_THROW(addToZoneData(rr_child_ns_));
  452. // below the zone cut
  453. findTest(Name("www.child.example.org"), RRType::A(),
  454. ZoneFinder::DELEGATION, true, rr_child_ns_);
  455. // at the zone cut
  456. findTest(Name("child.example.org"), RRType::A(), ZoneFinder::DELEGATION,
  457. true, rr_child_ns_);
  458. findTest(Name("child.example.org"), RRType::NS(), ZoneFinder::DELEGATION,
  459. true, rr_child_ns_);
  460. // finding NS for the apex (origin) node. This must not be confused
  461. // with delegation due to the existence of an NS RR.
  462. findTest(origin_, RRType::NS(), ZoneFinder::SUCCESS, true, rr_ns_);
  463. // unusual case of "nested delegation": the highest cut should be used.
  464. EXPECT_NO_THROW(addToZoneData(rr_grandchild_ns_));
  465. findTest(Name("www.grand.child.example.org"), RRType::A(),
  466. // note: !rr_grandchild_ns_
  467. ZoneFinder::DELEGATION, true, rr_child_ns_);
  468. }
  469. TEST_F(InMemoryZoneFinderTest, delegationWithDS) {
  470. // Similar setup to the previous one, but with DS RR at the delegation
  471. // point.
  472. addToZoneData(rr_ns_);
  473. addToZoneData(rr_child_ns_);
  474. addToZoneData(rr_child_ds_);
  475. // Normal types of query should result in delegation, but DS query
  476. // should be considered in-zone (but only exactly at the delegation point).
  477. findTest(Name("child.example.org"), RRType::A(), ZoneFinder::DELEGATION,
  478. true, rr_child_ns_);
  479. findTest(Name("child.example.org"), RRType::DS(), ZoneFinder::SUCCESS,
  480. true, rr_child_ds_);
  481. findTest(Name("grand.child.example.org"), RRType::DS(),
  482. ZoneFinder::DELEGATION, true, rr_child_ns_);
  483. // There's nothing special for DS query at the zone apex. It would
  484. // normally result in NXRRSET.
  485. findTest(Name("example.org"), RRType::DS(), ZoneFinder::NXRRSET,
  486. true, ConstRRsetPtr());
  487. }
  488. TEST_F(InMemoryZoneFinderTest, findAny) {
  489. EXPECT_NO_THROW(addToZoneData(rr_a_));
  490. EXPECT_NO_THROW(addToZoneData(rr_ns_));
  491. EXPECT_NO_THROW(addToZoneData(rr_child_glue_));
  492. vector<ConstRRsetPtr> expected_sets;
  493. // origin
  494. expected_sets.push_back(rr_a_);
  495. expected_sets.push_back(rr_ns_);
  496. findAllTest(origin_, ZoneFinder::SUCCESS, expected_sets);
  497. // out zone name
  498. EXPECT_THROW(findAllTest(Name("example.com"), ZoneFinder::NXDOMAIN,
  499. vector<ConstRRsetPtr>()),
  500. OutOfZone);
  501. expected_sets.clear();
  502. expected_sets.push_back(rr_child_glue_);
  503. findAllTest(rr_child_glue_->getName(), ZoneFinder::SUCCESS, expected_sets);
  504. // add zone cut
  505. EXPECT_NO_THROW(addToZoneData(rr_child_ns_));
  506. // zone cut
  507. findAllTest(rr_child_ns_->getName(), ZoneFinder::DELEGATION,
  508. vector<ConstRRsetPtr>(), ZoneFinder::RESULT_DEFAULT,
  509. NULL, rr_child_ns_);
  510. // glue for this zone cut
  511. findAllTest(rr_child_glue_->getName(),ZoneFinder::DELEGATION,
  512. vector<ConstRRsetPtr>(), ZoneFinder::RESULT_DEFAULT,
  513. NULL, rr_child_ns_);
  514. }
  515. TEST_F(InMemoryZoneFinderTest, glue) {
  516. // install zone data:
  517. // a zone cut
  518. EXPECT_NO_THROW(addToZoneData(rr_child_ns_));
  519. // glue for this cut
  520. EXPECT_NO_THROW(addToZoneData(rr_child_glue_));
  521. // a nested zone cut (unusual)
  522. EXPECT_NO_THROW(addToZoneData(rr_grandchild_ns_));
  523. // glue under the deeper zone cut
  524. EXPECT_NO_THROW(addToZoneData(rr_grandchild_glue_));
  525. // glue 'at the' zone cut
  526. EXPECT_NO_THROW(addToZoneData(rr_ns_a_));
  527. EXPECT_NO_THROW(addToZoneData(rr_ns_ns_));
  528. // by default glue is hidden due to the zone cut
  529. findTest(rr_child_glue_->getName(), RRType::A(), ZoneFinder::DELEGATION,
  530. true, rr_child_ns_);
  531. // If we do it in the "glue OK" mode, we should find the exact match.
  532. findTest(rr_child_glue_->getName(), RRType::A(), ZoneFinder::SUCCESS, true,
  533. rr_child_glue_, ZoneFinder::RESULT_DEFAULT, NULL,
  534. ZoneFinder::FIND_GLUE_OK);
  535. // glue OK + NXRRSET case
  536. findTest(rr_child_glue_->getName(), RRType::AAAA(), ZoneFinder::NXRRSET,
  537. true, ConstRRsetPtr(), ZoneFinder::RESULT_DEFAULT, NULL,
  538. ZoneFinder::FIND_GLUE_OK);
  539. // glue OK + NXDOMAIN case
  540. findTest(Name("www.child.example.org"), RRType::A(),
  541. ZoneFinder::DELEGATION, true, rr_child_ns_,
  542. ZoneFinder::RESULT_DEFAULT, NULL, ZoneFinder::FIND_GLUE_OK);
  543. // nested cut case. The glue should be found.
  544. findTest(rr_grandchild_glue_->getName(), RRType::AAAA(),
  545. ZoneFinder::SUCCESS,
  546. true, rr_grandchild_glue_, ZoneFinder::RESULT_DEFAULT, NULL,
  547. ZoneFinder::FIND_GLUE_OK);
  548. // A non-existent name in nested cut. This should result in delegation
  549. // at the highest zone cut.
  550. findTest(Name("www.grand.child.example.org"), RRType::TXT(),
  551. ZoneFinder::DELEGATION, true, rr_child_ns_,
  552. ZoneFinder::RESULT_DEFAULT, NULL, ZoneFinder::FIND_GLUE_OK);
  553. // Glue at a zone cut
  554. findTest(Name("ns.example.org"), RRType::A(),
  555. ZoneFinder::DELEGATION, true, rr_ns_ns_);
  556. findTest(Name("ns.example.org"), RRType::A(), ZoneFinder::SUCCESS,
  557. true, rr_ns_a_, ZoneFinder::RESULT_DEFAULT,
  558. NULL, ZoneFinder::FIND_GLUE_OK);
  559. }
  560. /**
  561. * \brief Test searching.
  562. *
  563. * Check it finds or does not find correctly and does not throw exceptions.
  564. */
  565. void
  566. InMemoryZoneFinderTest::findCheck(ZoneFinder::FindResultFlags expected_flags,
  567. ZoneFinder::FindOptions find_options)
  568. {
  569. // Fill some data inside
  570. // Now put all the data we have there. It should throw nothing
  571. rr_a_->addRRsig(createRdata(RRType::RRSIG(), RRClass::IN(),
  572. "A 5 3 3600 20120814220826 20120715220826 "
  573. "1234 example.com. FAKE"));
  574. EXPECT_NO_THROW(addToZoneData(rr_ns_));
  575. EXPECT_NO_THROW(addToZoneData(rr_ns_a_));
  576. EXPECT_NO_THROW(addToZoneData(rr_ns_aaaa_));
  577. EXPECT_NO_THROW(addToZoneData(rr_a_));
  578. if ((expected_flags & ZoneFinder::RESULT_NSEC3_SIGNED) != 0) {
  579. addToZoneData(rr_nsec3_);
  580. zone_data_->setSigned(true);
  581. }
  582. if ((expected_flags & ZoneFinder::RESULT_NSEC_SIGNED) != 0) {
  583. addToZoneData(rr_nsec_);
  584. zone_data_->setSigned(true);
  585. }
  586. // These two should be successful
  587. findTest(origin_, RRType::NS(), ZoneFinder::SUCCESS, true, rr_ns_);
  588. findTest(rr_ns_a_->getName(), RRType::A(), ZoneFinder::SUCCESS, true,
  589. rr_ns_a_);
  590. // Similar test for a signed RRset. We should see the RRSIG iff
  591. // FIND_DNSSEC option is specified.
  592. findTest(rr_a_->getName(), RRType::A(), ZoneFinder::SUCCESS, true, rr_a_);
  593. findTest(rr_a_->getName(), RRType::A(), ZoneFinder::SUCCESS, true,
  594. rr_a_, ZoneFinder::RESULT_DEFAULT, NULL, ZoneFinder::FIND_DNSSEC);
  595. // These domains don't exist. (and one is out of the zone). In an
  596. // NSEC-signed zone with DNSSEC records requested, it should return the
  597. // covering NSEC for the query name (the actual NSEC in the test data may
  598. // not really "cover" it, but for the purpose of this test it's okay).
  599. ConstRRsetPtr expected_nsec; // by default it's NULL
  600. if ((expected_flags & ZoneFinder::RESULT_NSEC_SIGNED) != 0 &&
  601. (find_options & ZoneFinder::FIND_DNSSEC) != 0) {
  602. expected_nsec = rr_nsec_;
  603. }
  604. // There's no other name between this one and the origin, so when NSEC
  605. // is to be returned it should be the origin NSEC.
  606. findTest(Name("nothere.example.org"), RRType::A(),
  607. ZoneFinder::NXDOMAIN, true, expected_nsec, expected_flags,
  608. NULL, find_options);
  609. // The previous name in the zone is "ns.example.org", but it doesn't
  610. // have an NSEC. It should be skipped and the origin NSEC will be
  611. // returned as the "closest NSEC".
  612. findTest(Name("nxdomain.example.org"), RRType::A(),
  613. ZoneFinder::NXDOMAIN, true, expected_nsec, expected_flags,
  614. NULL, find_options);
  615. EXPECT_THROW(zone_finder_.find(Name("example.net"), RRType::A()),
  616. OutOfZone);
  617. // These domain exist but don't have the provided RRType. For the latter
  618. // one we now add its NSEC (which was delayed so that it wouldn't break
  619. // other cases above).
  620. findTest(origin_, RRType::AAAA(), ZoneFinder::NXRRSET, true,
  621. expected_nsec, expected_flags, NULL, find_options);
  622. if ((expected_flags & ZoneFinder::RESULT_NSEC_SIGNED) != 0) {
  623. addToZoneData(rr_ns_nsec_);
  624. zone_data_->setSigned(true);
  625. if ((find_options & ZoneFinder::FIND_DNSSEC) != 0) {
  626. expected_nsec = rr_ns_nsec_;
  627. }
  628. }
  629. findTest(rr_ns_a_->getName(), RRType::NS(), ZoneFinder::NXRRSET, true,
  630. expected_nsec, expected_flags, NULL, find_options);
  631. }
  632. TEST_F(InMemoryZoneFinderTest, find) {
  633. findCheck();
  634. }
  635. TEST_F(InMemoryZoneFinderTest, findNSEC3Signed) {
  636. findCheck(ZoneFinder::RESULT_NSEC3_SIGNED);
  637. }
  638. TEST_F(InMemoryZoneFinderTest, findNSEC3SignedWithDNSSEC) {
  639. // For NSEC3-signed zones, specifying the DNSSEC option shouldn't change
  640. // anything (the NSEC3_SIGNED flag is always set, and no records are
  641. // returned for negative cases regardless).
  642. findCheck(ZoneFinder::RESULT_NSEC3_SIGNED, ZoneFinder::FIND_DNSSEC);
  643. }
  644. TEST_F(InMemoryZoneFinderTest, findNSECSigned) {
  645. // NSEC-signed zone, without requesting DNSSEC (no NSEC should be provided)
  646. findCheck(ZoneFinder::RESULT_NSEC_SIGNED);
  647. }
  648. // Generalized test for Empty Nonterminal (ENT) cases with NSEC
  649. void
  650. InMemoryZoneFinderTest::findNSECENTCheck(const Name& ent_name,
  651. ConstRRsetPtr expected_nsec,
  652. ZoneFinder::FindResultFlags expected_flags)
  653. {
  654. addToZoneData(rr_emptywild_);
  655. addToZoneData(rr_under_wild_);
  656. // Sanity check: Should result in NXRRSET
  657. findTest(ent_name, RRType::A(), ZoneFinder::NXRRSET, true,
  658. ConstRRsetPtr(), expected_flags);
  659. // Sanity check: No NSEC added yet
  660. findTest(ent_name, RRType::A(), ZoneFinder::NXRRSET, true,
  661. ConstRRsetPtr(), expected_flags,
  662. NULL, ZoneFinder::FIND_DNSSEC);
  663. // Now add the NSEC rrs making it a 'complete' zone (in terms of NSEC,
  664. // there are no sigs)
  665. addToZoneData(rr_nsec_);
  666. addToZoneData(rr_ent_nsec2_);
  667. addToZoneData(rr_ent_nsec3_);
  668. addToZoneData(rr_ent_nsec4_);
  669. zone_data_->setSigned(true);
  670. // Should result in NXRRSET, and RESULT_NSEC_SIGNED
  671. findTest(ent_name, RRType::A(), ZoneFinder::NXRRSET, true,
  672. ConstRRsetPtr(),
  673. expected_flags | ZoneFinder::RESULT_NSEC_SIGNED);
  674. // And check for the NSEC if DNSSEC_OK
  675. findTest(ent_name, RRType::A(), ZoneFinder::NXRRSET, true,
  676. expected_nsec, expected_flags | ZoneFinder::RESULT_NSEC_SIGNED,
  677. NULL, ZoneFinder::FIND_DNSSEC);
  678. }
  679. TEST_F(InMemoryZoneFinderTest,findNSECEmptyNonterminal) {
  680. // Non-wildcard case
  681. findNSECENTCheck(Name("wild.example.org"), rr_ent_nsec3_);
  682. }
  683. TEST_F(InMemoryZoneFinderTest, findNSECEmptyNonterminalWildcard) {
  684. // Wildcard case, above actual wildcard
  685. findNSECENTCheck(Name("foo.example.org"), rr_nsec_);
  686. }
  687. TEST_F(InMemoryZoneFinderTest, findNSECEmptyNonterminalAtWildcard) {
  688. // Wildcard case, at actual wildcard
  689. findNSECENTCheck(Name("bar.foo.example.org"), rr_nsec_,
  690. ZoneFinder::RESULT_WILDCARD);
  691. }
  692. TEST_F(InMemoryZoneFinderTest, findNSECSignedWithDNSSEC) {
  693. // NSEC-signed zone, requesting DNSSEC (NSEC should be provided)
  694. findCheck(ZoneFinder::RESULT_NSEC_SIGNED, ZoneFinder::FIND_DNSSEC);
  695. }
  696. void
  697. InMemoryZoneFinderTest::emptyNodeCheck(
  698. ZoneFinder::FindResultFlags expected_flags)
  699. {
  700. /*
  701. * The backend ZoneTree for this test should look like as follows:
  702. * example.org
  703. * |
  704. * baz (empty; easy case)
  705. * / | \
  706. * bar | x.foo ('foo' part is empty; a bit trickier)
  707. * bbb
  708. * /
  709. * aaa
  710. */
  711. // Construct the test zone
  712. const char* const names[] = {
  713. "bar.example.org.", "x.foo.example.org.", "aaa.baz.example.org.",
  714. "bbb.baz.example.org.", NULL};
  715. for (int i = 0; names[i] != NULL; ++i) {
  716. ConstRRsetPtr rrset = textToRRset(string(names[i]) +
  717. " 300 IN A 192.0.2.1");
  718. addToZoneData(rrset);
  719. }
  720. if ((expected_flags & ZoneFinder::RESULT_NSEC3_SIGNED) != 0) {
  721. addToZoneData(rr_nsec3_);
  722. zone_data_->setSigned(true);
  723. }
  724. if ((expected_flags & ZoneFinder::RESULT_NSEC_SIGNED) != 0) {
  725. addToZoneData(rr_nsec_);
  726. zone_data_->setSigned(true);
  727. }
  728. // empty node matching, easy case: the node for 'baz' exists with
  729. // no data.
  730. findTest(Name("baz.example.org"), RRType::A(), ZoneFinder::NXRRSET, true,
  731. ConstRRsetPtr(), expected_flags);
  732. // empty node matching, a trickier case: the node for 'foo' is part of
  733. // "x.foo", which should be considered an empty node.
  734. findTest(Name("foo.example.org"), RRType::A(), ZoneFinder::NXRRSET, true,
  735. ConstRRsetPtr(), expected_flags);
  736. // "org" is contained in "example.org", but it shouldn't be treated as
  737. // NXRRSET because it's out of zone.
  738. // Note: basically we don't expect such a query to be performed (the common
  739. // operation is to identify the best matching zone first then perform
  740. // search it), but we shouldn't be confused even in the unexpected case.
  741. EXPECT_THROW(zone_finder_.find(Name("org"), RRType::A()), OutOfZone);
  742. }
  743. TEST_F(InMemoryZoneFinderTest, emptyNode) {
  744. emptyNodeCheck();
  745. }
  746. TEST_F(InMemoryZoneFinderTest, emptyNodeNSEC3) {
  747. emptyNodeCheck(ZoneFinder::RESULT_NSEC3_SIGNED);
  748. }
  749. TEST_F(InMemoryZoneFinderTest, emptyNodeNSEC) {
  750. emptyNodeCheck(ZoneFinder::RESULT_NSEC_SIGNED);
  751. }
  752. /*
  753. * Test that puts a (simple) wildcard into the zone and checks we can
  754. * correctly find the data.
  755. */
  756. void
  757. InMemoryZoneFinderTest::wildcardCheck(
  758. ZoneFinder::FindResultFlags expected_flags,
  759. ZoneFinder::FindOptions find_options)
  760. {
  761. /*
  762. * example.org.
  763. * |
  764. * [cname]wild (not *.wild, should have wild mark)
  765. * |
  766. * *
  767. */
  768. // If the zone is "signed" (detecting it by the NSEC/NSEC3 signed flags),
  769. // add RRSIGs to the records.
  770. if ((expected_flags & ZoneFinder::RESULT_NSEC_SIGNED) != 0 ||
  771. (expected_flags & ZoneFinder::RESULT_NSEC3_SIGNED) != 0) {
  772. // Convenience shortcut. The RDATA is not really validatable, but
  773. // it doesn't matter for our tests.
  774. const char* const rrsig_common = "5 3 3600 "
  775. "20000101000000 20000201000000 12345 example.org. FAKEFAKEFAKE";
  776. find_options = find_options | ZoneFinder::FIND_DNSSEC;
  777. rr_wild_->addRRsig(textToRRset("*.wild.example.org. 300 IN RRSIG A " +
  778. string(rrsig_common)));
  779. rr_cnamewild_->addRRsig(textToRRset("*.cnamewild.example.org. 300 IN "
  780. "RRSIG CNAME " +
  781. string(rrsig_common)));
  782. }
  783. addToZoneData(rr_wild_);
  784. addToZoneData(rr_cnamewild_);
  785. // If the zone is expected to be "signed" with NSEC3, add an NSEC3.
  786. // (the content of the NSEC3 shouldn't matter)
  787. if ((expected_flags & ZoneFinder::RESULT_NSEC3_SIGNED) != 0) {
  788. addToZoneData(rr_nsec3_);
  789. }
  790. if ((expected_flags & ZoneFinder::RESULT_NSEC_SIGNED) != 0) {
  791. addToZoneData(rr_nsec_);
  792. }
  793. // Search at the parent. The parent will not have the A, but it will
  794. // be in the wildcard (so check the wildcard isn't matched at the parent)
  795. {
  796. SCOPED_TRACE("Search at parent");
  797. if ((expected_flags & ZoneFinder::RESULT_NSEC_SIGNED) != 0) {
  798. findTest(Name("wild.example.org"), RRType::A(),
  799. ZoneFinder::NXRRSET, true, rr_nsec_, expected_flags,
  800. NULL, find_options);
  801. } else {
  802. findTest(Name("wild.example.org"), RRType::A(),
  803. ZoneFinder::NXRRSET, true, ConstRRsetPtr(),
  804. expected_flags, NULL, find_options);
  805. }
  806. }
  807. // For the test setup of "NSEC-signed" zone, we might expect it will
  808. // be returned with a negative result, either because wildcard match is
  809. // disabled by the search option or because wildcard match is canceled
  810. // per protocol.
  811. ConstRRsetPtr expected_nsec; // by default it's NULL
  812. if ((expected_flags & ZoneFinder::RESULT_NSEC_SIGNED) != 0 &&
  813. (find_options & ZoneFinder::FIND_DNSSEC) != 0) {
  814. expected_nsec = rr_nsec_;
  815. }
  816. // Explicitly converting the following to const pointers; some compilers
  817. // would complain about mixed use of const and non const in ?: below.
  818. const ConstRRsetPtr rr_wild = rr_wild_;
  819. const ConstRRsetPtr rr_cnamewild = rr_cnamewild_;
  820. // Search the original name of wildcard
  821. {
  822. SCOPED_TRACE("Search directly at *");
  823. findTest(Name("*.wild.example.org"), RRType::A(), ZoneFinder::SUCCESS,
  824. true, rr_wild_, ZoneFinder::RESULT_DEFAULT, NULL,
  825. find_options);
  826. }
  827. // Below some of the test cases will normally result in a wildcard match;
  828. // if NO_WILDCARD is specified, it should result in NXDOMAIN instead,
  829. // and, when available and requested, the covering NSEC will be returned.
  830. // The following are shortcut parameters to unify these cases.
  831. const bool wild_ok = ((find_options & ZoneFinder::NO_WILDCARD) == 0);
  832. const ZoneFinder::FindResultFlags wild_expected_flags =
  833. wild_ok ? (ZoneFinder::RESULT_WILDCARD | expected_flags) :
  834. expected_flags;
  835. // Search "created" name.
  836. {
  837. SCOPED_TRACE("Search at created child");
  838. findTest(Name("a.wild.example.org"), RRType::A(),
  839. wild_ok ? ZoneFinder::SUCCESS : ZoneFinder::NXDOMAIN, false,
  840. wild_ok ? rr_wild : expected_nsec,
  841. wild_expected_flags, NULL, find_options, wild_ok);
  842. }
  843. // Search name that has CNAME.
  844. {
  845. SCOPED_TRACE("Matching CNAME");
  846. findTest(Name("a.cnamewild.example.org"), RRType::A(),
  847. wild_ok ? ZoneFinder::CNAME : ZoneFinder::NXDOMAIN, false,
  848. wild_ok ? rr_cnamewild : expected_nsec,
  849. wild_expected_flags, NULL, find_options, wild_ok);
  850. }
  851. // Search another created name, this time little bit lower
  852. {
  853. SCOPED_TRACE("Search at created grand-child");
  854. findTest(Name("a.b.wild.example.org"), RRType::A(),
  855. wild_ok ? ZoneFinder::SUCCESS : ZoneFinder::NXDOMAIN, false,
  856. wild_ok ? rr_wild : expected_nsec,
  857. wild_expected_flags, NULL, find_options, wild_ok);
  858. }
  859. addToZoneData(rr_under_wild_);
  860. {
  861. SCOPED_TRACE("Search under non-wildcard");
  862. findTest(Name("bar.foo.wild.example.org"), RRType::A(),
  863. ZoneFinder::NXDOMAIN, true, expected_nsec, expected_flags,
  864. NULL, find_options);
  865. }
  866. // Wildcard match, but no data. We add the additional NSEC at the wildcard
  867. // at this point so that it wouldn't break other tests above. Note also
  868. // that in the NO_WILDCARD case the resulting NSEC is the same. Ideally
  869. // we could use a more tricky setup so we can distinguish these cases,
  870. // but for this purpose it's not bad; what we'd like to test here is that
  871. // wildcard substitution doesn't happen for either case, and the
  872. // NO_WILDCARD effect itself can be checked by the result code (NXDOMAIN).
  873. ConstRRsetPtr expected_wild_nsec; // by default it's NULL
  874. if ((expected_flags & ZoneFinder::RESULT_NSEC_SIGNED) != 0) {
  875. addToZoneData(rr_wild_nsec_);
  876. expected_wild_nsec = rr_wild_nsec_;
  877. }
  878. {
  879. SCOPED_TRACE("Search at wildcard, no data");
  880. findTest(Name("a.wild.example.org"), RRType::AAAA(),
  881. wild_ok ? ZoneFinder::NXRRSET : ZoneFinder::NXDOMAIN, true,
  882. wild_ok ? expected_wild_nsec : expected_wild_nsec,
  883. wild_expected_flags, NULL, find_options);
  884. }
  885. }
  886. TEST_F(InMemoryZoneFinderTest, wildcard) {
  887. // Normal case
  888. wildcardCheck();
  889. }
  890. TEST_F(InMemoryZoneFinderTest, wildcardDisabledWithNSEC) {
  891. // Wildcard is disabled. In practice, this is used as part of query
  892. // processing for an NSEC-signed zone, so we test that case specifically.
  893. wildcardCheck(ZoneFinder::RESULT_NSEC_SIGNED, ZoneFinder::NO_WILDCARD);
  894. }
  895. TEST_F(InMemoryZoneFinderTest, wildcardDisabledWithoutNSEC) {
  896. // Similar to the previous once, but check the behavior for a non signed
  897. // zone just in case.
  898. wildcardCheck(ZoneFinder::RESULT_DEFAULT, ZoneFinder::NO_WILDCARD);
  899. }
  900. /*
  901. * Test that we don't match a wildcard if we get under delegation.
  902. * By 4.3.3 of RFC1034:
  903. * "Wildcard RRs do not apply:
  904. * - When the query is in another zone. That is, delegation cancels
  905. * the wildcard defaults."
  906. */
  907. TEST_F(InMemoryZoneFinderTest, delegatedWildcard) {
  908. addToZoneData(rr_child_wild_);
  909. addToZoneData(rr_child_ns_);
  910. {
  911. SCOPED_TRACE("Looking under delegation point");
  912. findTest(Name("a.child.example.org"), RRType::A(),
  913. ZoneFinder::DELEGATION, true, rr_child_ns_);
  914. }
  915. {
  916. SCOPED_TRACE("Looking under delegation point in GLUE_OK mode");
  917. findTest(Name("a.child.example.org"), RRType::A(),
  918. ZoneFinder::DELEGATION, true, rr_child_ns_,
  919. ZoneFinder::RESULT_DEFAULT, NULL, ZoneFinder::FIND_GLUE_OK);
  920. }
  921. }
  922. // Tests combination of wildcard and ANY.
  923. void
  924. InMemoryZoneFinderTest::anyWildcardCheck(
  925. ZoneFinder::FindResultFlags expected_flags)
  926. {
  927. addToZoneData(rr_wild_);
  928. if ((expected_flags & ZoneFinder::RESULT_NSEC3_SIGNED) != 0) {
  929. addToZoneData(rr_nsec3_);
  930. }
  931. if ((expected_flags & ZoneFinder::RESULT_NSEC_SIGNED) != 0) {
  932. addToZoneData(rr_nsec_);
  933. }
  934. vector<ConstRRsetPtr> expected_sets;
  935. // First try directly the name (normal match)
  936. {
  937. SCOPED_TRACE("Asking directly for *");
  938. expected_sets.push_back(rr_wild_);
  939. findAllTest(Name("*.wild.example.org"), ZoneFinder::SUCCESS,
  940. expected_sets);
  941. }
  942. // Then a wildcard match
  943. {
  944. SCOPED_TRACE("Asking in the wild way");
  945. expected_sets.clear();
  946. RRsetPtr expected(new RRset(Name("a.wild.example.org"),
  947. rr_wild_->getClass(), rr_wild_->getType(),
  948. rr_wild_->getTTL()));
  949. expected->addRdata(rr_wild_->getRdataIterator()->getCurrent());
  950. expected_sets.push_back(expected);
  951. findAllTest(Name("a.wild.example.org"), ZoneFinder::SUCCESS,
  952. expected_sets,
  953. ZoneFinder::RESULT_WILDCARD | expected_flags);
  954. }
  955. }
  956. TEST_F(InMemoryZoneFinderTest, anyWildcard) {
  957. anyWildcardCheck();
  958. }
  959. TEST_F(InMemoryZoneFinderTest, anyWildcardNSEC3) {
  960. anyWildcardCheck(ZoneFinder::RESULT_NSEC3_SIGNED);
  961. }
  962. TEST_F(InMemoryZoneFinderTest, anyWildcardNSEC) {
  963. anyWildcardCheck(ZoneFinder::RESULT_NSEC_SIGNED);
  964. }
  965. // Test there's nothing in the wildcard in the middle if we load
  966. // wild.*.foo.example.org.
  967. void
  968. InMemoryZoneFinderTest::emptyWildcardCheck(
  969. ZoneFinder::FindResultFlags expected_flags)
  970. {
  971. /*
  972. * example.org.
  973. * foo
  974. * *
  975. * wild
  976. */
  977. addToZoneData(rr_emptywild_);
  978. if ((expected_flags & ZoneFinder::RESULT_NSEC3_SIGNED) != 0) {
  979. addToZoneData(rr_nsec3_);
  980. }
  981. if ((expected_flags & ZoneFinder::RESULT_NSEC_SIGNED) != 0) {
  982. addToZoneData(rr_nsec_);
  983. }
  984. {
  985. SCOPED_TRACE("Asking for the original record under wildcard");
  986. findTest(Name("wild.*.foo.example.org"), RRType::A(),
  987. ZoneFinder::SUCCESS, true, rr_emptywild_);
  988. }
  989. {
  990. SCOPED_TRACE("Asking for A record");
  991. findTest(Name("a.foo.example.org"), RRType::A(), ZoneFinder::NXRRSET,
  992. true, ConstRRsetPtr(),
  993. ZoneFinder::RESULT_WILDCARD | expected_flags);
  994. findTest(Name("*.foo.example.org"), RRType::A(), ZoneFinder::NXRRSET,
  995. true, ConstRRsetPtr(), expected_flags);
  996. findTest(Name("foo.example.org"), RRType::A(), ZoneFinder::NXRRSET,
  997. true, ConstRRsetPtr(), expected_flags);
  998. }
  999. {
  1000. SCOPED_TRACE("Asking for ANY record");
  1001. findAllTest(Name("*.foo.example.org"), ZoneFinder::NXRRSET,
  1002. vector<ConstRRsetPtr>(), expected_flags);
  1003. findAllTest(Name("a.foo.example.org"), ZoneFinder::NXRRSET,
  1004. vector<ConstRRsetPtr>(),
  1005. ZoneFinder::RESULT_WILDCARD | expected_flags);
  1006. }
  1007. {
  1008. SCOPED_TRACE("Asking on the non-terminal");
  1009. findTest(Name("wild.bar.foo.example.org"), RRType::A(),
  1010. ZoneFinder::NXRRSET, true, ConstRRsetPtr(),
  1011. ZoneFinder::RESULT_WILDCARD | expected_flags);
  1012. }
  1013. }
  1014. TEST_F(InMemoryZoneFinderTest, emptyWildcard) {
  1015. emptyWildcardCheck();
  1016. }
  1017. TEST_F(InMemoryZoneFinderTest, emptyWildcardNSEC3) {
  1018. emptyWildcardCheck(ZoneFinder::RESULT_NSEC3_SIGNED);
  1019. }
  1020. TEST_F(InMemoryZoneFinderTest, emptyWildcardNSEC) {
  1021. emptyWildcardCheck(ZoneFinder::RESULT_NSEC_SIGNED);
  1022. }
  1023. // Same as emptyWildcard, but with multiple * in the path.
  1024. TEST_F(InMemoryZoneFinderTest, nestedEmptyWildcard) {
  1025. addToZoneData(rr_nested_emptywild_);
  1026. {
  1027. SCOPED_TRACE("Asking for the original record under wildcards");
  1028. findTest(Name("wild.*.foo.*.bar.example.org"), RRType::A(),
  1029. ZoneFinder::SUCCESS, true, rr_nested_emptywild_);
  1030. }
  1031. {
  1032. SCOPED_TRACE("Matching wildcard against empty nonterminal");
  1033. const char* names[] = {
  1034. "baz.foo.*.bar.example.org",
  1035. "baz.foo.baz.bar.example.org",
  1036. "*.foo.baz.bar.example.org",
  1037. NULL
  1038. };
  1039. for (const char** name = names; *name != NULL; ++ name) {
  1040. SCOPED_TRACE(string("Node ") + *name);
  1041. findTest(Name(*name), RRType::A(), ZoneFinder::NXRRSET, true,
  1042. ConstRRsetPtr(), ZoneFinder::RESULT_WILDCARD);
  1043. }
  1044. }
  1045. // Domains to test
  1046. const char* names[] = {
  1047. "*.foo.*.bar.example.org",
  1048. "foo.*.bar.example.org",
  1049. "*.bar.example.org",
  1050. "bar.example.org",
  1051. NULL
  1052. };
  1053. {
  1054. SCOPED_TRACE("Asking directly for A on parent nodes");
  1055. for (const char** name = names; *name != NULL; ++ name) {
  1056. SCOPED_TRACE(string("Node ") + *name);
  1057. findTest(Name(*name), RRType::A(), ZoneFinder::NXRRSET);
  1058. }
  1059. }
  1060. {
  1061. SCOPED_TRACE("Asking for ANY on parent nodes");
  1062. for (const char** name = names; *name != NULL; ++ name) {
  1063. SCOPED_TRACE(string("Node ") + *name);
  1064. findAllTest(Name(*name), ZoneFinder::NXRRSET,
  1065. vector<ConstRRsetPtr>());
  1066. }
  1067. }
  1068. }
  1069. // We run this part twice from the below test, in two slightly different
  1070. // situations
  1071. void
  1072. InMemoryZoneFinderTest::doCancelWildcardCheck(
  1073. ZoneFinder::FindResultFlags expected_flags,
  1074. ZoneFinder::FindOptions find_options)
  1075. {
  1076. // These should be canceled
  1077. {
  1078. SCOPED_TRACE("Canceled under foo.wild.example.org");
  1079. // For an NSEC-signed zone with DNSSEC requested, the covering NSEC
  1080. // should be returned. The expected NSEC is actually just the only
  1081. // NSEC in the test data, but in this context it doesn't matter;
  1082. // it's sufficient just to check any NSEC is returned (or not).
  1083. ConstRRsetPtr expected_nsec; // by default it's NULL
  1084. if ((expected_flags & ZoneFinder::RESULT_NSEC_SIGNED) != 0 &&
  1085. (find_options & ZoneFinder::FIND_DNSSEC)) {
  1086. expected_nsec = rr_nsec_;
  1087. }
  1088. findTest(Name("aaa.foo.wild.example.org"), RRType::A(),
  1089. ZoneFinder::NXDOMAIN, true, expected_nsec, expected_flags,
  1090. NULL, find_options);
  1091. findTest(Name("zzz.foo.wild.example.org"), RRType::A(),
  1092. ZoneFinder::NXDOMAIN, true, expected_nsec, expected_flags,
  1093. NULL, find_options);
  1094. }
  1095. // This is existing, non-wildcard domain, shouldn't wildcard at all
  1096. {
  1097. SCOPED_TRACE("Existing domain under foo.wild.example.org");
  1098. findTest(Name("bar.foo.wild.example.org"), RRType::A(),
  1099. ZoneFinder::SUCCESS, true, rr_not_wild_);
  1100. }
  1101. // These should be caught by the wildcard
  1102. {
  1103. SCOPED_TRACE("Neighbor wildcards to foo.wild.example.org");
  1104. const char* names[] = {
  1105. "aaa.bbb.wild.example.org",
  1106. "aaa.zzz.wild.example.org",
  1107. "zzz.wild.example.org",
  1108. NULL
  1109. };
  1110. for (const char** name = names; *name != NULL; ++ name) {
  1111. SCOPED_TRACE(string("Node ") + *name);
  1112. findTest(Name(*name), RRType::A(), ZoneFinder::SUCCESS, false,
  1113. rr_wild_,
  1114. ZoneFinder::RESULT_WILDCARD | expected_flags, NULL,
  1115. ZoneFinder::FIND_DEFAULT, true);
  1116. }
  1117. }
  1118. // This shouldn't be wildcarded, it's an existing domain
  1119. {
  1120. SCOPED_TRACE("The foo.wild.example.org itself");
  1121. findTest(Name("foo.wild.example.org"), RRType::A(),
  1122. ZoneFinder::NXRRSET, true, ConstRRsetPtr(), expected_flags);
  1123. }
  1124. }
  1125. /*
  1126. * This tests that if there's a name between the wildcard domain and the
  1127. * searched one, it will not trigger wildcard, for example, if we have
  1128. * *.wild.example.org and bar.foo.wild.example.org, then we know
  1129. * foo.wild.example.org exists and is not wildcard. Therefore, search for
  1130. * aaa.foo.wild.example.org should return NXDOMAIN.
  1131. *
  1132. * Tests few cases "around" the canceled wildcard match, to see something that
  1133. * shouldn't be canceled isn't.
  1134. */
  1135. TEST_F(InMemoryZoneFinderTest, cancelWildcard) {
  1136. addToZoneData(rr_wild_);
  1137. addToZoneData(rr_not_wild_);
  1138. {
  1139. SCOPED_TRACE("Runnig with single entry under foo.wild.example.org");
  1140. doCancelWildcardCheck();
  1141. }
  1142. // Try putting another one under foo.wild....
  1143. // The result should be the same but it will be done in another way in the
  1144. // code, because the foo.wild.example.org will exist in the tree.
  1145. addToZoneData(rr_not_wild_another_);
  1146. {
  1147. SCOPED_TRACE("Runnig with two entries under foo.wild.example.org");
  1148. doCancelWildcardCheck();
  1149. }
  1150. }
  1151. // Same tests as cancelWildcard for NSEC3-signed zone
  1152. TEST_F(InMemoryZoneFinderTest, cancelWildcardNSEC3) {
  1153. addToZoneData(rr_wild_);
  1154. addToZoneData(rr_not_wild_);
  1155. addToZoneData(rr_nsec3_);
  1156. {
  1157. SCOPED_TRACE("Runnig with single entry under foo.wild.example.org");
  1158. doCancelWildcardCheck(ZoneFinder::RESULT_NSEC3_SIGNED);
  1159. }
  1160. addToZoneData(rr_not_wild_another_);
  1161. {
  1162. SCOPED_TRACE("Runnig with two entries under foo.wild.example.org");
  1163. doCancelWildcardCheck(ZoneFinder::RESULT_NSEC3_SIGNED);
  1164. }
  1165. }
  1166. // Same tests as cancelWildcard for NSEC-signed zone. Check both cases with
  1167. // or without FIND_DNSSEC option. NSEC should be returned only when the option
  1168. // is given.
  1169. TEST_F(InMemoryZoneFinderTest, cancelWildcardNSEC) {
  1170. addToZoneData(rr_wild_);
  1171. addToZoneData(rr_not_wild_);
  1172. addToZoneData(rr_nsec_);
  1173. {
  1174. SCOPED_TRACE("Runnig with single entry under foo.wild.example.org");
  1175. doCancelWildcardCheck(ZoneFinder::RESULT_NSEC_SIGNED,
  1176. ZoneFinder::FIND_DNSSEC);
  1177. doCancelWildcardCheck(ZoneFinder::RESULT_NSEC_SIGNED);
  1178. }
  1179. addToZoneData(rr_not_wild_another_);
  1180. {
  1181. SCOPED_TRACE("Runnig with two entries under foo.wild.example.org");
  1182. doCancelWildcardCheck(ZoneFinder::RESULT_NSEC_SIGNED,
  1183. ZoneFinder::FIND_DNSSEC);
  1184. doCancelWildcardCheck(ZoneFinder::RESULT_NSEC_SIGNED);
  1185. }
  1186. }
  1187. TEST_F(InMemoryZoneFinderTest, findNSEC3ForBadZone) {
  1188. // Set up the faked hash calculator.
  1189. const TestNSEC3HashCreator creator;
  1190. setNSEC3HashCreator(&creator);
  1191. // If the zone has nothing about NSEC3 (neither NSEC3 or NSEC3PARAM),
  1192. // findNSEC3() should be rejected.
  1193. EXPECT_THROW(zone_finder_.findNSEC3(Name("www.example.org"), true),
  1194. DataSourceError);
  1195. // Only having NSEC3PARAM isn't enough
  1196. addToZoneData(textToRRset("example.org. 300 IN NSEC3PARAM "
  1197. "1 0 12 aabbccdd"));
  1198. EXPECT_THROW(zone_finder_.findNSEC3(Name("www.example.org"), true),
  1199. DataSourceError);
  1200. // Unless NSEC3 for apex is added the result in the recursive mode
  1201. // is guaranteed.
  1202. const string ns1_nsec3_text = string(ns1_hash) + ".example.org." +
  1203. string(nsec3_common);
  1204. addToZoneData(textToRRset(ns1_nsec3_text));
  1205. EXPECT_THROW(zone_finder_.findNSEC3(Name("www.example.org"), true),
  1206. DataSourceError);
  1207. }
  1208. TEST_F(InMemoryZoneFinderTest, findOrphanRRSIG) {
  1209. // Make the zone "NSEC signed"
  1210. addToZoneData(rr_nsec_);
  1211. const ZoneFinder::FindResultFlags expected_flags =
  1212. ZoneFinder::RESULT_NSEC_SIGNED;
  1213. // Add A for ns.example.org, and RRSIG-only covering TXT for the same name.
  1214. // query for the TXT should result in NXRRSET.
  1215. addToZoneData(rr_ns_a_);
  1216. updater_.add(ConstRRsetPtr(),
  1217. textToRRset(
  1218. "ns.example.org. 300 IN RRSIG TXT 5 3 300 20120814220826 "
  1219. "20120715220826 1234 example.com. FAKE"));
  1220. findTest(Name("ns.example.org"), RRType::TXT(),
  1221. ZoneFinder::NXRRSET, true, ConstRRsetPtr(), expected_flags);
  1222. // Add RRSIG-only covering NSEC. This shouldn't be returned when NSEC is
  1223. // requested, whether it's for NXRRSET or NXDOMAIN
  1224. updater_.add(ConstRRsetPtr(),
  1225. textToRRset(
  1226. "ns.example.org. 300 IN RRSIG NSEC 5 3 300 "
  1227. "20120814220826 20120715220826 1234 example.com. FAKE"));
  1228. // The added RRSIG for NSEC could be used for NXRRSET but shouldn't
  1229. findTest(Name("ns.example.org"), RRType::TXT(),
  1230. ZoneFinder::NXRRSET, true, ConstRRsetPtr(),
  1231. expected_flags, NULL, ZoneFinder::FIND_DNSSEC);
  1232. // The added RRSIG for NSEC could be used for NXDOMAIN but shouldn't
  1233. findTest(Name("nz.example.org"), RRType::A(),
  1234. ZoneFinder::NXDOMAIN, true, rr_nsec_,
  1235. expected_flags, NULL, ZoneFinder::FIND_DNSSEC);
  1236. // RRSIG-only CNAME shouldn't be accidentally confused with real CNAME.
  1237. updater_.add(ConstRRsetPtr(),
  1238. textToRRset(
  1239. "nocname.example.org. 300 IN RRSIG CNAME 5 3 300 "
  1240. "20120814220826 20120715220826 1234 example.com. FAKE"));
  1241. findTest(Name("nocname.example.org"), RRType::A(),
  1242. ZoneFinder::NXRRSET, true, ConstRRsetPtr(), expected_flags);
  1243. // RRSIG-only for NS wouldn't invoke delegation anyway, but we check this
  1244. // case explicitly.
  1245. updater_.add(ConstRRsetPtr(),
  1246. textToRRset(
  1247. "nodelegation.example.org. 300 IN RRSIG NS 5 3 300 "
  1248. "20120814220826 20120715220826 1234 example.com. FAKE"));
  1249. findTest(Name("nodelegation.example.org"), RRType::A(),
  1250. ZoneFinder::NXRRSET, true, ConstRRsetPtr(), expected_flags);
  1251. findTest(Name("www.nodelegation.example.org"), RRType::A(),
  1252. ZoneFinder::NXDOMAIN, true, ConstRRsetPtr(), expected_flags);
  1253. // Same for RRSIG-only for DNAME
  1254. updater_.add(ConstRRsetPtr(),
  1255. textToRRset(
  1256. "nodname.example.org. 300 IN RRSIG DNAME 5 3 300 "
  1257. "20120814220826 20120715220826 1234 example.com. FAKE"));
  1258. findTest(Name("www.nodname.example.org"), RRType::A(),
  1259. ZoneFinder::NXDOMAIN, true, ConstRRsetPtr(), expected_flags);
  1260. // If we have a delegation NS at this node, it will be a bit trickier,
  1261. // because the zonecut processing actually takes place at the node.
  1262. // But the RRSIG-only for DNAME shouldn't confuse the process and the NS
  1263. // should win.
  1264. ConstRRsetPtr ns_rrset =
  1265. textToRRset("nodname.example.org. 300 IN NS ns.nodname.example.org.");
  1266. addToZoneData(ns_rrset);
  1267. findTest(Name("www.nodname.example.org"), RRType::A(),
  1268. ZoneFinder::DELEGATION, true, ns_rrset);
  1269. }
  1270. // \brief testcase for #2504 (Problem in inmem NSEC denial of existence
  1271. // handling)
  1272. TEST_F(InMemoryZoneFinderTest, NSECNonExistentTest) {
  1273. shared_ptr<ZoneTableSegment> ztable_segment(
  1274. new ZoneTableSegmentTest(class_, mem_sgmt_));
  1275. InMemoryClient client(ztable_segment, class_);
  1276. Name name("example.com.");
  1277. client.load(name, TEST_DATA_DIR "/2504-test.zone");
  1278. DataSourceClient::FindResult result(client.findZone(name));
  1279. // Check for a non-existing name
  1280. Name search_name("nonexist.example.com.");
  1281. ZoneFinderContextPtr find_result(
  1282. result.zone_finder->find(search_name,
  1283. RRType::A(), ZoneFinder::FIND_DNSSEC));
  1284. // We don't find the domain, but find() must complete (not throw or
  1285. // assert).
  1286. EXPECT_EQ(ZoneFinder::NXDOMAIN, find_result->code);
  1287. }
  1288. /// \brief NSEC3 specific tests fixture for the InMemoryZoneFinder class
  1289. class InMemoryZoneFinderNSEC3Test : public InMemoryZoneFinderTest {
  1290. public:
  1291. InMemoryZoneFinderNSEC3Test() {
  1292. // Set up the faked hash calculator.
  1293. setNSEC3HashCreator(&creator_);
  1294. // Add a few NSEC3 records:
  1295. // apex (example.org.): hash=0P..
  1296. // ns1.example.org: hash=2T..
  1297. // w.example.org: hash=01..
  1298. // zzz.example.org: hash=R5..
  1299. const string apex_nsec3_text = string(apex_hash) + ".example.org." +
  1300. string(nsec3_common);
  1301. addToZoneData(textToRRset(apex_nsec3_text));
  1302. const string ns1_nsec3_text = string(ns1_hash) + ".example.org." +
  1303. string(nsec3_common);
  1304. addToZoneData(textToRRset(ns1_nsec3_text));
  1305. const string w_nsec3_text = string(w_hash) + ".example.org." +
  1306. string(nsec3_common);
  1307. addToZoneData(textToRRset(w_nsec3_text));
  1308. const string zzz_nsec3_text = string(zzz_hash) + ".example.org." +
  1309. string(nsec3_common);
  1310. addToZoneData(textToRRset(zzz_nsec3_text));
  1311. }
  1312. private:
  1313. const TestNSEC3HashCreator creator_;
  1314. };
  1315. TEST_F(InMemoryZoneFinderNSEC3Test, findNSEC3) {
  1316. performNSEC3Test(zone_finder_);
  1317. }
  1318. struct TestData {
  1319. // String for the name passed to findNSEC3() (concatenated with
  1320. // "example.org.")
  1321. const char* const name;
  1322. // Should recursive findNSEC3() be performed?
  1323. const bool recursive;
  1324. // The following are members of the FindNSEC3Result returned by
  1325. // findNSEC3(). The proofs are given as char*, which are converted
  1326. // to Name objects and checked against getName() on the returned
  1327. // ConstRRsetPtr. If any of these is NULL, then it's expected that
  1328. // ConstRRsetPtr() will be returned.
  1329. const bool matched;
  1330. const uint8_t closest_labels;
  1331. const char* const closest_proof;
  1332. const char* const next_proof;
  1333. };
  1334. const TestData nsec3_data[] = {
  1335. // ==== These are non-recursive tests.
  1336. {"n0", false, false, 4, "R53BQ7CC2UVMUBFU5OCMM6PERS9TK9EN", NULL},
  1337. {"n1", false, true, 4, "01UDEMVP1J2F7EG6JEBPS17VP3N8I58H", NULL},
  1338. {"n2", false, false, 4, "01UDEMVP1J2F7EG6JEBPS17VP3N8I58H", NULL},
  1339. {"n3", false, true, 4, "0P9MHAVEQVM6T7VBL5LOP2U3T2RP3TOM", NULL},
  1340. {"n4", false, false, 4, "0P9MHAVEQVM6T7VBL5LOP2U3T2RP3TOM", NULL},
  1341. {"n5", false, true, 4, "2T7B4G4VSA5SMI47K61MV5BV1A22BOJR", NULL},
  1342. {"n6", false, false, 4, "2T7B4G4VSA5SMI47K61MV5BV1A22BOJR", NULL},
  1343. {"n7", false, true, 4, "R53BQ7CC2UVMUBFU5OCMM6PERS9TK9EN", NULL},
  1344. {"n8", false, false, 4, "R53BQ7CC2UVMUBFU5OCMM6PERS9TK9EN", NULL},
  1345. // ==== These are recursive tests.
  1346. {"n0", true, true, 3, "0P9MHAVEQVM6T7VBL5LOP2U3T2RP3TOM",
  1347. "R53BQ7CC2UVMUBFU5OCMM6PERS9TK9EN"},
  1348. {"n1", true, true, 4, "01UDEMVP1J2F7EG6JEBPS17VP3N8I58H", NULL},
  1349. {"n2", true, true, 3, "0P9MHAVEQVM6T7VBL5LOP2U3T2RP3TOM",
  1350. "01UDEMVP1J2F7EG6JEBPS17VP3N8I58H"},
  1351. {"n3", true, true, 4, "0P9MHAVEQVM6T7VBL5LOP2U3T2RP3TOM", NULL},
  1352. {"n4", true, true, 3, "0P9MHAVEQVM6T7VBL5LOP2U3T2RP3TOM",
  1353. "0P9MHAVEQVM6T7VBL5LOP2U3T2RP3TOM"},
  1354. {"n5", true, true, 4, "2T7B4G4VSA5SMI47K61MV5BV1A22BOJR", NULL},
  1355. {"n6", true, true, 3, "0P9MHAVEQVM6T7VBL5LOP2U3T2RP3TOM",
  1356. "2T7B4G4VSA5SMI47K61MV5BV1A22BOJR"},
  1357. {"n7", true, true, 4, "R53BQ7CC2UVMUBFU5OCMM6PERS9TK9EN", NULL},
  1358. {"n8", true, true, 3, "0P9MHAVEQVM6T7VBL5LOP2U3T2RP3TOM",
  1359. "R53BQ7CC2UVMUBFU5OCMM6PERS9TK9EN"}
  1360. };
  1361. const size_t data_count(sizeof(nsec3_data) / sizeof(*nsec3_data));
  1362. TEST_F(InMemoryZoneFinderNSEC3Test, findNSEC3Walk) {
  1363. // This test basically uses nsec3_data[] declared above along with
  1364. // the fake hash setup to walk the NSEC3 tree. The names and fake
  1365. // hash calculation is specially setup so that the tree search
  1366. // terminates at specific locations in the tree. We findNSEC3() on
  1367. // each of the nsec3_data[], which is setup such that the hash
  1368. // results in the search terminating on either side of each node of
  1369. // the NSEC3 tree. This way, we check what result is returned in
  1370. // every search termination case in the NSEC3 tree.
  1371. const Name origin("example.org");
  1372. for (size_t i = 0; i < data_count; ++i) {
  1373. const Name name = Name(nsec3_data[i].name).concatenate(origin);
  1374. SCOPED_TRACE(name.toText() + (nsec3_data[i].recursive ?
  1375. ", recursive" :
  1376. ", non-recursive"));
  1377. const ZoneFinder::FindNSEC3Result result =
  1378. zone_finder_.findNSEC3(name, nsec3_data[i].recursive);
  1379. EXPECT_EQ(nsec3_data[i].matched, result.matched);
  1380. EXPECT_EQ(nsec3_data[i].closest_labels, result.closest_labels);
  1381. if (nsec3_data[i].closest_proof != NULL) {
  1382. ASSERT_TRUE(result.closest_proof);
  1383. EXPECT_EQ(Name(nsec3_data[i].closest_proof).concatenate(origin),
  1384. result.closest_proof->getName());
  1385. } else {
  1386. EXPECT_FALSE(result.closest_proof);
  1387. }
  1388. if (nsec3_data[i].next_proof != NULL) {
  1389. ASSERT_TRUE(result.next_proof);
  1390. EXPECT_EQ(Name(nsec3_data[i].next_proof).concatenate(origin),
  1391. result.next_proof->getName());
  1392. } else {
  1393. EXPECT_FALSE(result.next_proof);
  1394. }
  1395. }
  1396. }
  1397. TEST_F(InMemoryZoneFinderNSEC3Test, RRSIGOnly) {
  1398. // add an RRSIG-only NSEC3 to the NSEC3 space, and try to find it; it
  1399. // should result in an exception.
  1400. const string n8_hash = "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ";
  1401. updater_.add(ConstRRsetPtr(),
  1402. textToRRset(
  1403. n8_hash + ".example.org. 300 IN RRSIG NSEC3 5 3 300 "
  1404. "20120814220826 20120715220826 1234 example.com. FAKE"));
  1405. EXPECT_THROW(zone_finder_.findNSEC3(Name("n8.example.org"), false),
  1406. DataSourceError);
  1407. }
  1408. // \brief testcase for #2503 (Problem in inmem NSEC3 denial of existence
  1409. // handling)
  1410. TEST_F(InMemoryZoneFinderNSEC3Test, findNSEC3MissingOrigin) {
  1411. // Set back the default hash calculator.
  1412. DefaultNSEC3HashCreator creator;
  1413. setNSEC3HashCreator(&creator);
  1414. shared_ptr<ZoneTableSegment> ztable_segment(
  1415. new ZoneTableSegmentTest(class_, mem_sgmt_));
  1416. InMemoryClient client(ztable_segment, class_);
  1417. Name name("example.com.");
  1418. client.load(name, TEST_DATA_DIR "/2503-test.zone");
  1419. DataSourceClient::FindResult result(client.findZone(name));
  1420. // Check for a non-existing name
  1421. Name search_name("nonexist.example.com.");
  1422. ZoneFinder::FindNSEC3Result find_result(
  1423. result.zone_finder->findNSEC3(search_name, true));
  1424. // findNSEC3() must have completed (not throw or assert). Because
  1425. // the find was recursive, it always must find something and return
  1426. // true.
  1427. EXPECT_TRUE(find_result.matched);
  1428. }
  1429. }