query_unittest.cc 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680
  1. // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <sstream>
  15. #include <vector>
  16. #include <map>
  17. #include <boost/bind.hpp>
  18. #include <boost/scoped_ptr.hpp>
  19. #include <exceptions/exceptions.h>
  20. #include <dns/masterload.h>
  21. #include <dns/message.h>
  22. #include <dns/name.h>
  23. #include <dns/opcode.h>
  24. #include <dns/rcode.h>
  25. #include <dns/rrttl.h>
  26. #include <dns/rrtype.h>
  27. #include <dns/rdataclass.h>
  28. #include <datasrc/memory_datasrc.h>
  29. #include <auth/query.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::auth;
  37. using namespace isc::testutils;
  38. namespace {
  39. // This is the content of the mock zone (see below).
  40. // It's a sequence of textual RRs that is supposed to be parsed by
  41. // dns::masterLoad(). Some of the RRs are also used as the expected
  42. // data in specific tests, in which case they are referenced via specific
  43. // local variables (such as soa_txt).
  44. const char* const soa_txt = "example.com. 3600 IN SOA . . 0 0 0 0 0\n";
  45. const char* const zone_ns_txt =
  46. "example.com. 3600 IN NS glue.delegation.example.com.\n"
  47. "example.com. 3600 IN NS noglue.example.com.\n"
  48. "example.com. 3600 IN NS example.net.\n";
  49. const char* const ns_addrs_txt =
  50. "glue.delegation.example.com. 3600 IN A 192.0.2.153\n"
  51. "glue.delegation.example.com. 3600 IN AAAA 2001:db8::53\n"
  52. "noglue.example.com. 3600 IN A 192.0.2.53\n";
  53. const char* const delegation_txt =
  54. "delegation.example.com. 3600 IN NS glue.delegation.example.com.\n"
  55. "delegation.example.com. 3600 IN NS noglue.example.com.\n"
  56. "delegation.example.com. 3600 IN NS cname.example.com.\n"
  57. "delegation.example.com. 3600 IN NS example.org.\n";
  58. const char* const mx_txt =
  59. "mx.example.com. 3600 IN MX 10 www.example.com.\n"
  60. "mx.example.com. 3600 IN MX 20 mailer.example.org.\n"
  61. "mx.example.com. 3600 IN MX 30 mx.delegation.example.com.\n";
  62. const char* const www_a_txt = "www.example.com. 3600 IN A 192.0.2.80\n";
  63. const char* const cname_txt =
  64. "cname.example.com. 3600 IN CNAME www.example.com.\n";
  65. const char* const cname_nxdom_txt =
  66. "cnamenxdom.example.com. 3600 IN CNAME nxdomain.example.com.\n";
  67. // CNAME Leading out of zone
  68. const char* const cname_out_txt =
  69. "cnameout.example.com. 3600 IN CNAME www.example.org.\n";
  70. // The DNAME to do tests against
  71. const char* const dname_txt =
  72. "dname.example.com. 3600 IN DNAME "
  73. "somethinglong.dnametarget.example.com.\n";
  74. // Some data at the dname node (allowed by RFC 2672)
  75. const char* const dname_a_txt =
  76. "dname.example.com. 3600 IN A 192.0.2.5\n";
  77. // This is not inside the zone, this is created at runtime
  78. const char* const synthetized_cname_txt =
  79. "www.dname.example.com. 3600 IN CNAME "
  80. "www.somethinglong.dnametarget.example.com.\n";
  81. // The rest of data won't be referenced from the test cases.
  82. const char* const other_zone_rrs =
  83. "cnamemailer.example.com. 3600 IN CNAME www.example.com.\n"
  84. "cnamemx.example.com. 3600 IN MX 10 cnamemailer.example.com.\n"
  85. "mx.delegation.example.com. 3600 IN A 192.0.2.100\n";
  86. // Wildcards
  87. const char* const wild_txt = "*.wild.example.com. 3600 IN A 192.0.2.7\n";
  88. const char* const nsec_wild_txt =
  89. "*.wild.example.com. 3600 IN NSEC www.example.com. A NSEC RRSIG\n";
  90. const char* const cnamewild_txt =
  91. "*.cnamewild.example.com. 3600 IN CNAME www.example.org.\n";
  92. const char* const nsec_cnamewild_txt = "*.cnamewild.example.com. "
  93. "3600 IN NSEC delegation.example.com. CNAME NSEC RRSIG\n";
  94. // Wildcard_nxrrset
  95. const char* const wild_txt_nxrrset =
  96. "*.uwild.example.com. 3600 IN A 192.0.2.9\n";
  97. const char* const nsec_wild_txt_nxrrset =
  98. "*.uwild.example.com. 3600 IN NSEC www.uwild.example.com. A NSEC RRSIG\n";
  99. const char* const wild_txt_next =
  100. "www.uwild.example.com. 3600 IN A 192.0.2.11\n";
  101. const char* const nsec_wild_txt_next =
  102. "www.uwild.example.com. 3600 IN NSEC *.wild.example.com. A NSEC RRSIG\n";
  103. // Wildcard empty
  104. const char* const empty_txt = "b.*.t.example.com. 3600 IN A 192.0.2.13\n";
  105. const char* const nsec_empty_txt =
  106. "b.*.t.example.com. 3600 IN NSEC *.uwild.example.com. A NSEC RRSIG\n";
  107. const char* const empty_prev_txt = "t.example.com. 3600 IN A 192.0.2.15\n";
  108. const char* const nsec_empty_prev_txt =
  109. "t.example.com. 3600 IN NSEC b.*.t.example.com. A NSEC RRSIG\n";
  110. // Used in NXDOMAIN proof test. We are going to test some unusual case where
  111. // the best possible wildcard is below the "next domain" of the NSEC RR that
  112. // proves the NXDOMAIN, i.e.,
  113. // mx.example.com. (exist)
  114. // (.no.example.com. (qname, NXDOMAIN)
  115. // ).no.example.com. (exist)
  116. // *.no.example.com. (best possible wildcard, not exist)
  117. const char* const no_txt =
  118. ").no.example.com. 3600 IN AAAA 2001:db8::53\n";
  119. // NSEC records.
  120. const char* const nsec_apex_txt =
  121. "example.com. 3600 IN NSEC cname.example.com. NS SOA NSEC RRSIG\n";
  122. const char* const nsec_mx_txt =
  123. "mx.example.com. 3600 IN NSEC ).no.example.com. MX NSEC RRSIG\n";
  124. const char* const nsec_no_txt =
  125. ").no.example.com. 3600 IN NSEC nz.no.example.com. AAAA NSEC RRSIG\n";
  126. // We'll also test the case where a single NSEC proves both NXDOMAIN and the
  127. // non existence of wildcard. The following records will be used for that
  128. // test.
  129. // ).no.example.com. (exist, whose NSEC proves everything)
  130. // *.no.example.com. (best possible wildcard, not exist)
  131. // nx.no.example.com. (NXDOMAIN)
  132. // nz.no.example.com. (exist)
  133. const char* const nz_txt =
  134. "nz.no.example.com. 3600 IN AAAA 2001:db8::5300\n";
  135. const char* const nsec_nz_txt =
  136. "nz.no.example.com. 3600 IN NSEC noglue.example.com. AAAA NSEC RRSIG\n";
  137. const char* const nsec_nxdomain_txt =
  138. "noglue.example.com. 3600 IN NSEC nonsec.example.com. A\n";
  139. // NSEC for the normal NXRRSET case
  140. const char* const nsec_www_txt =
  141. "www.example.com. 3600 IN NSEC example.com. A NSEC RRSIG\n";
  142. // Authoritative data without NSEC
  143. const char* const nonsec_a_txt = "nonsec.example.com. 3600 IN A 192.0.2.0\n";
  144. // NSEC3 RRs. You may also need to add mapping to MockZoneFinder::hash_map_.
  145. const char* const nsec3_apex_txt =
  146. "0p9mhaveqvm6t7vbl5lop2u3t2rp3tom.example.com. 3600 IN NSEC3 1 1 12 "
  147. "aabbccdd 2t7b4g4vsa5smi47k61mv5bv1a22bojr NS SOA NSEC3PARAM RRSIG\n";
  148. const char* const nsec3_www_txt =
  149. "q04jkcevqvmu85r014c7dkba38o0ji5r.example.com. 3600 IN NSEC3 1 1 12 "
  150. "aabbccdd r53bq7cc2uvmubfu5ocmm6pers9tk9en A RRSIG\n";
  151. // (Secure) delegation data; Delegation with DS record
  152. const char* const signed_delegation_txt =
  153. "signed-delegation.example.com. 3600 IN NS ns.example.net.\n";
  154. const char* const signed_delegation_ds_txt =
  155. "signed-delegation.example.com. 3600 IN DS 12345 8 2 "
  156. "764501411DE58E8618945054A3F620B36202E115D015A7773F4B78E0F952CECA\n";
  157. // (Secure) delegation data; Delegation without DS record (and NSEC denying
  158. // its existence.
  159. const char* const unsigned_delegation_txt =
  160. "unsigned-delegation.example.com. 3600 IN NS ns.example.net.\n";
  161. const char* const unsigned_delegation_nsec_txt =
  162. "unsigned-delegation.example.com. 3600 IN NSEC "
  163. "*.uwild.example.com. NS RRSIG NSEC\n";
  164. // (Secure) delegation data; Delegation where the DS lookup will raise an
  165. // exception.
  166. const char* const bad_delegation_txt =
  167. "bad-delegation.example.com. 3600 IN NS ns.example.net.\n";
  168. // A helper function that generates a textual representation of RRSIG RDATA
  169. // for the given covered type. The resulting RRSIG may not necessarily make
  170. // sense in terms of the DNSSEC protocol, but for our testing purposes it's
  171. // okay.
  172. string
  173. getCommonRRSIGText(const string& type) {
  174. return (type +
  175. string(" 5 3 3600 20000101000000 20000201000000 12345 "
  176. "example.com. FAKEFAKEFAKE"));
  177. }
  178. // This is a mock Zone Finder class for testing.
  179. // It is a derived class of ZoneFinder for the convenient of tests.
  180. // Its find() method emulates the common behavior of protocol compliant
  181. // ZoneFinder classes, but simplifies some minor cases and also supports broken
  182. // behavior.
  183. // For simplicity, most names are assumed to be "in zone"; there's only
  184. // one zone cut at the point of name "delegation.example.com".
  185. // Another special name is "dname.example.com". Query names under this name
  186. // will result in DNAME.
  187. // This mock zone doesn't handle empty non terminal nodes (if we need to test
  188. // such cases find() should have specialized code for it).
  189. class MockZoneFinder : public ZoneFinder {
  190. public:
  191. MockZoneFinder() :
  192. origin_(Name("example.com")),
  193. delegation_name_("delegation.example.com"),
  194. signed_delegation_name_("signed-delegation.example.com"),
  195. bad_signed_delegation_name_("bad-delegation.example.com"),
  196. unsigned_delegation_name_("unsigned-delegation.example.com"),
  197. dname_name_("dname.example.com"),
  198. has_SOA_(true),
  199. has_apex_NS_(true),
  200. rrclass_(RRClass::IN()),
  201. include_rrsig_anyway_(false),
  202. use_nsec3_(false),
  203. nsec_name_(origin_)
  204. {
  205. stringstream zone_stream;
  206. zone_stream << soa_txt << zone_ns_txt << ns_addrs_txt <<
  207. delegation_txt << mx_txt << www_a_txt << cname_txt <<
  208. cname_nxdom_txt << cname_out_txt << dname_txt <<
  209. dname_a_txt << other_zone_rrs << no_txt << nz_txt <<
  210. nsec_apex_txt << nsec_mx_txt << nsec_no_txt << nsec_nz_txt <<
  211. nsec_nxdomain_txt << nsec_www_txt << nonsec_a_txt <<
  212. wild_txt << nsec_wild_txt << cnamewild_txt << nsec_cnamewild_txt <<
  213. wild_txt_nxrrset << nsec_wild_txt_nxrrset << wild_txt_next <<
  214. nsec_wild_txt_next << empty_txt << nsec_empty_txt <<
  215. empty_prev_txt << nsec_empty_prev_txt <<
  216. nsec3_apex_txt << nsec3_www_txt <<
  217. signed_delegation_txt << signed_delegation_ds_txt <<
  218. unsigned_delegation_txt << unsigned_delegation_nsec_txt <<
  219. bad_delegation_txt;
  220. masterLoad(zone_stream, origin_, rrclass_,
  221. boost::bind(&MockZoneFinder::loadRRset, this, _1));
  222. empty_nsec_rrset_ = ConstRRsetPtr(new RRset(Name::ROOT_NAME(),
  223. RRClass::IN(),
  224. RRType::NSEC(),
  225. RRTTL(3600)));
  226. // (Faked) NSEC3 hash map. For convenience we use hardcoded built-in
  227. // map instead of calculating and using actual hash.
  228. // The used hash values are borrowed from RFC5155 examples.
  229. hash_map_[Name("example.com")] = "0p9mhaveqvm6t7vbl5lop2u3t2rp3tom";
  230. hash_map_[Name("nxdomain.example.com")] =
  231. "v644ebqk9bibcna874givr6joj62mlhv";
  232. hash_map_[Name("nx.domain.example.com")] =
  233. "v644ebqk9bibcna874givr6joj62mlhv";
  234. hash_map_[Name("domain.example.com")] =
  235. "v644ebqk9bibcna874givr6joj62mlhv";
  236. hash_map_[Name("nxdomain2.example.com")] =
  237. "q00jkcevqvmu85r014c7dkba38o0ji5r";
  238. hash_map_[Name("nxdomain3.example.com")] =
  239. "009mhaveqvm6t7vbl5lop2u3t2rp3tom";
  240. }
  241. virtual isc::dns::Name getOrigin() const { return (origin_); }
  242. virtual isc::dns::RRClass getClass() const { return (rrclass_); }
  243. virtual FindResult find(const isc::dns::Name& name,
  244. const isc::dns::RRType& type,
  245. const FindOptions options = FIND_DEFAULT);
  246. virtual FindResult findAll(const isc::dns::Name& name,
  247. std::vector<ConstRRsetPtr>& target,
  248. const FindOptions options = FIND_DEFAULT);
  249. virtual ZoneFinder::FindNSEC3Result
  250. findNSEC3(const Name& name, bool recursive);
  251. // If false is passed, it makes the zone broken as if it didn't have the
  252. // SOA.
  253. void setSOAFlag(bool on) { has_SOA_ = on; }
  254. // If false is passed, it makes the zone broken as if it didn't have
  255. // the apex NS.
  256. void setApexNSFlag(bool on) { has_apex_NS_ = on; }
  257. // Turn this on if you want it to return RRSIGs regardless of FIND_GLUE_OK
  258. void setIncludeRRSIGAnyway(bool on) { include_rrsig_anyway_ = on; }
  259. // Once called, this "faked" result will be returned when NSEC is expected
  260. // for the specified query name.
  261. void setNSECResult(const Name& nsec_name, Result code,
  262. ConstRRsetPtr rrset)
  263. {
  264. nsec_name_ = nsec_name;
  265. nsec_result_.reset(new ZoneFinder::FindResult(code, rrset));
  266. }
  267. // If true is passed return an empty NSEC3 RRset for some negative
  268. // answers when DNSSEC is required.
  269. void setNSEC3Flag(bool on) { use_nsec3_ = on; }
  270. Name findPreviousName(const Name&) const {
  271. isc_throw(isc::NotImplemented, "Mock doesn't support previous name");
  272. }
  273. public:
  274. // We allow the tests to use these for convenience
  275. ConstRRsetPtr delegation_rrset_;
  276. ConstRRsetPtr signed_delegation_rrset_;
  277. ConstRRsetPtr signed_delegation_ds_rrset_;
  278. ConstRRsetPtr bad_signed_delegation_rrset_;
  279. ConstRRsetPtr unsigned_delegation_rrset_;
  280. ConstRRsetPtr empty_nsec_rrset_;
  281. private:
  282. typedef map<RRType, ConstRRsetPtr> RRsetStore;
  283. typedef map<Name, RRsetStore> Domains;
  284. Domains domains_;
  285. Domains nsec3_domains_;
  286. void loadRRset(RRsetPtr rrset) {
  287. if (rrset->getType() == RRType::NSEC3()) {
  288. // NSEC3 should go to the dedicated table
  289. nsec3_domains_[rrset->getName()][rrset->getType()] = rrset;
  290. // By nature it should have RRSIG. (We may want to selectively
  291. // omit this to test pathological cases).
  292. rrset->addRRsig(RdataPtr(new generic::RRSIG(
  293. getCommonRRSIGText(rrset->getType().
  294. toText()))));
  295. return;
  296. }
  297. domains_[rrset->getName()][rrset->getType()] = rrset;
  298. if (rrset->getName() == delegation_name_ &&
  299. rrset->getType() == RRType::NS()) {
  300. delegation_rrset_ = rrset;
  301. } else if (rrset->getName() == signed_delegation_name_ &&
  302. rrset->getType() == RRType::NS()) {
  303. signed_delegation_rrset_ = rrset;
  304. } else if (rrset->getName() == bad_signed_delegation_name_ &&
  305. rrset->getType() == RRType::NS()) {
  306. bad_signed_delegation_rrset_ = rrset;
  307. } else if (rrset->getName() == unsigned_delegation_name_ &&
  308. rrset->getType() == RRType::NS()) {
  309. unsigned_delegation_rrset_ = rrset;
  310. } else if (rrset->getName() == signed_delegation_name_ &&
  311. rrset->getType() == RRType::DS()) {
  312. signed_delegation_ds_rrset_ = rrset;
  313. // Like NSEC(3), by nature it should have an RRSIG.
  314. rrset->addRRsig(RdataPtr(new generic::RRSIG(
  315. getCommonRRSIGText(rrset->getType().
  316. toText()))));
  317. } else if (rrset->getName() == dname_name_ &&
  318. rrset->getType() == RRType::DNAME()) {
  319. dname_rrset_ = rrset;
  320. // Add some signatures
  321. } else if (rrset->getName() == Name("example.com.") &&
  322. rrset->getType() == RRType::NS()) {
  323. // For NS, we only have RRSIG for the origin name.
  324. rrset->addRRsig(RdataPtr(new generic::RRSIG(
  325. getCommonRRSIGText("NS"))));
  326. } else {
  327. // For others generate RRSIG unconditionally. Technically this
  328. // is wrong because we shouldn't have it for names under a zone
  329. // cut. But in our tests that doesn't matter, so we add them
  330. // just for simplicity.
  331. rrset->addRRsig(RdataPtr(new generic::RRSIG(
  332. getCommonRRSIGText(rrset->getType().
  333. toText()))));
  334. }
  335. }
  336. const Name origin_;
  337. // Names where we delegate somewhere else
  338. const Name delegation_name_;
  339. const Name signed_delegation_name_;
  340. const Name bad_signed_delegation_name_;
  341. const Name unsigned_delegation_name_;
  342. const Name dname_name_;
  343. bool has_SOA_;
  344. bool has_apex_NS_;
  345. ConstRRsetPtr dname_rrset_;
  346. const RRClass rrclass_;
  347. bool include_rrsig_anyway_;
  348. bool use_nsec3_;
  349. // The following two will be used for faked NSEC cases
  350. Name nsec_name_;
  351. boost::scoped_ptr<ZoneFinder::FindResult> nsec_result_;
  352. map<Name, string> hash_map_;
  353. };
  354. // A helper function that generates a new RRset based on "wild_rrset",
  355. // replacing its owner name with 'real_name'.
  356. ConstRRsetPtr
  357. substituteWild(const RRset& wild_rrset, const Name& real_name) {
  358. RRsetPtr rrset(new RRset(real_name, wild_rrset.getClass(),
  359. wild_rrset.getType(), wild_rrset.getTTL()));
  360. // For simplicity we only consider the case with one RDATA (for now)
  361. rrset->addRdata(wild_rrset.getRdataIterator()->getCurrent());
  362. ConstRRsetPtr wild_sig = wild_rrset.getRRsig();
  363. if (wild_sig) {
  364. RRsetPtr sig(new RRset(real_name, wild_sig->getClass(),
  365. wild_sig->getType(), wild_sig->getTTL()));
  366. sig->addRdata(wild_sig->getRdataIterator()->getCurrent());
  367. rrset->addRRsig(sig);
  368. }
  369. return (rrset);
  370. }
  371. ZoneFinder::FindResult
  372. MockZoneFinder::findAll(const Name& name, std::vector<ConstRRsetPtr>& target,
  373. const FindOptions options)
  374. {
  375. ZoneFinder::FindResult result(find(name, RRType::ANY(), options));
  376. if (result.code == NXRRSET) {
  377. const Domains::const_iterator found_domain = domains_.find(name);
  378. if (!found_domain->second.empty()) {
  379. for (RRsetStore::const_iterator found_rrset =
  380. found_domain->second.begin();
  381. found_rrset != found_domain->second.end(); ++found_rrset) {
  382. // Insert RRs under the domain name into target
  383. target.push_back(found_rrset->second);
  384. }
  385. return (FindResult(SUCCESS, RRsetPtr()));
  386. }
  387. }
  388. return (result);
  389. }
  390. ZoneFinder::FindNSEC3Result
  391. MockZoneFinder::findNSEC3(const Name& name, bool recursive) {
  392. ConstRRsetPtr covering_proof;
  393. const int labels = name.getLabelCount();
  394. // For brevity, we assume several things below: maps should have an
  395. // expected entry when operator[] is used; maps are not empty.
  396. for (int i = 0; i < labels; ++i) {
  397. const string hlabel = hash_map_[name.split(i, labels - i)];
  398. const Name hname = Name(hlabel + ".example.com");
  399. // We don't use const_iterator so that we can use operator[] below
  400. Domains::iterator found_domain = nsec3_domains_.lower_bound(hname);
  401. // If the given hash is larger than the largest stored hash or
  402. // the first label doesn't match the target, identify the "previous"
  403. // hash value and remember it as the candidate next closer proof.
  404. if (found_domain == nsec3_domains_.end() ||
  405. found_domain->first.split(0, 1).toText(true) != hlabel) {
  406. // If the given hash is larger or smaller than everything,
  407. // the covering proof is the NSEC3 that has the largest hash.
  408. if (found_domain == nsec3_domains_.end() ||
  409. found_domain == nsec3_domains_.begin()) {
  410. covering_proof =
  411. nsec3_domains_.rbegin()->second[RRType::NSEC3()];
  412. } else {
  413. // Otherwise, H(found_domain-1) < given_hash < H(found_domain)
  414. // The covering proof is the first one.
  415. covering_proof = (--found_domain)->second[RRType::NSEC3()];
  416. }
  417. if (!recursive) { // in non recursive mode, we are done.
  418. return (ZoneFinder::FindNSEC3Result(false,
  419. name.getLabelCount(),
  420. covering_proof,
  421. ConstRRsetPtr()));
  422. }
  423. } else { // exact match
  424. return (ZoneFinder::FindNSEC3Result(
  425. true, name.getLabelCount() - i,
  426. found_domain->second[RRType::NSEC3()],
  427. covering_proof));
  428. }
  429. }
  430. isc_throw(isc::Unexpected, "findNSEC3() isn't expected to fail");
  431. }
  432. ZoneFinder::FindResult
  433. MockZoneFinder::find(const Name& name, const RRType& type,
  434. const FindOptions options)
  435. {
  436. // Emulating a broken zone: mandatory apex RRs are missing if specifically
  437. // configured so (which are rare cases).
  438. if (name == origin_ && type == RRType::SOA() && !has_SOA_) {
  439. return (FindResult(NXDOMAIN, RRsetPtr()));
  440. } else if (name == origin_ && type == RRType::NS() && !has_apex_NS_) {
  441. return (FindResult(NXDOMAIN, RRsetPtr()));
  442. }
  443. // Special case for names on or under a zone cut
  444. if ((options & FIND_GLUE_OK) == 0 &&
  445. (name == delegation_name_ ||
  446. name.compare(delegation_name_).getRelation() ==
  447. NameComparisonResult::SUBDOMAIN)) {
  448. return (FindResult(DELEGATION, delegation_rrset_));
  449. // And under DNAME
  450. } else if (name.compare(dname_name_).getRelation() ==
  451. NameComparisonResult::SUBDOMAIN) {
  452. if (type != RRType::DS()) {
  453. return (FindResult(DNAME, dname_rrset_));
  454. }
  455. } else if (name == signed_delegation_name_ ||
  456. name.compare(signed_delegation_name_).getRelation() ==
  457. NameComparisonResult::SUBDOMAIN) {
  458. if (type != RRType::DS()) {
  459. return (FindResult(DELEGATION, signed_delegation_rrset_));
  460. } else {
  461. return (FindResult(SUCCESS, signed_delegation_ds_rrset_));
  462. }
  463. } else if (name == unsigned_delegation_name_ ||
  464. name.compare(unsigned_delegation_name_).getRelation() ==
  465. NameComparisonResult::SUBDOMAIN) {
  466. if (type != RRType::DS()) {
  467. return (FindResult(DELEGATION, unsigned_delegation_rrset_));
  468. }
  469. } else if (name == bad_signed_delegation_name_ ||
  470. name.compare(bad_signed_delegation_name_).getRelation() ==
  471. NameComparisonResult::SUBDOMAIN) {
  472. if (type != RRType::DS()) {
  473. return (FindResult(DELEGATION, bad_signed_delegation_rrset_));
  474. } else {
  475. return (FindResult(NXDOMAIN, RRsetPtr()));
  476. }
  477. }
  478. // normal cases. names are searched for only per exact-match basis
  479. // for simplicity.
  480. const Domains::const_iterator found_domain = domains_.find(name);
  481. if (found_domain != domains_.end()) {
  482. // First, try exact match.
  483. RRsetStore::const_iterator found_rrset =
  484. found_domain->second.find(type);
  485. if (found_rrset != found_domain->second.end()) {
  486. ConstRRsetPtr rrset;
  487. // Strip whatever signature there is in case DNSSEC is not required
  488. // Just to make sure the Query asks for it when it is needed
  489. if ((options & ZoneFinder::FIND_DNSSEC) != 0 ||
  490. include_rrsig_anyway_ ||
  491. !found_rrset->second->getRRsig()) {
  492. rrset = found_rrset->second;
  493. } else {
  494. RRsetPtr noconst(new RRset(found_rrset->second->getName(),
  495. found_rrset->second->getClass(),
  496. found_rrset->second->getType(),
  497. found_rrset->second->getTTL()));
  498. for (RdataIteratorPtr
  499. i(found_rrset->second->getRdataIterator());
  500. !i->isLast(); i->next()) {
  501. noconst->addRdata(i->getCurrent());
  502. }
  503. rrset = noconst;
  504. }
  505. return (FindResult(SUCCESS, rrset));
  506. }
  507. // Otherwise, if this domain name has CNAME, return it.
  508. found_rrset = found_domain->second.find(RRType::CNAME());
  509. if (found_rrset != found_domain->second.end()) {
  510. return (FindResult(CNAME, found_rrset->second));
  511. }
  512. // Otherwise it's NXRRSET case.
  513. if ((options & FIND_DNSSEC) != 0) {
  514. if (use_nsec3_) {
  515. return (FindResult(NXRRSET, RRsetPtr(), RESULT_NSEC3_SIGNED));
  516. }
  517. found_rrset = found_domain->second.find(RRType::NSEC());
  518. if (found_rrset != found_domain->second.end()) {
  519. return (FindResult(NXRRSET, found_rrset->second,
  520. RESULT_NSEC_SIGNED));
  521. }
  522. }
  523. return (FindResult(NXRRSET, RRsetPtr(), RESULT_NSEC_SIGNED));
  524. }
  525. // query name isn't found in our domains.
  526. // We first check if the query name is an empty non terminal name
  527. // of the zone by naive linear search.
  528. Domains::const_iterator domain;
  529. for (domain = domains_.begin(); domain != domains_.end(); ++domain) {
  530. if (name.compare((*domain).first).getRelation() ==
  531. NameComparisonResult::SUPERDOMAIN) {
  532. break;
  533. }
  534. }
  535. if (domain != domains_.end()) {
  536. // The query name is in an empty non terminal node followed by 'domain'
  537. // (for simplicity we ignore the pathological case of 'domain' is
  538. // the origin of the zone)
  539. --domain; // reset domain to the "previous name"
  540. if ((options & FIND_DNSSEC) != 0) {
  541. if (use_nsec3_) {
  542. return (FindResult(NXRRSET, RRsetPtr(), RESULT_NSEC3_SIGNED));
  543. }
  544. RRsetStore::const_iterator found_rrset =
  545. (*domain).second.find(RRType::NSEC());
  546. if (found_rrset != (*domain).second.end()) {
  547. return (FindResult(NXRRSET, found_rrset->second,
  548. RESULT_NSEC_SIGNED));
  549. }
  550. }
  551. return (FindResult(NXRRSET, RRsetPtr()));
  552. }
  553. // Another possibility is wildcard. For simplicity we only check
  554. // hardcoded specific cases, ignoring other details such as canceling
  555. // due to the existence of closer name.
  556. if ((options & NO_WILDCARD) == 0) {
  557. const Name wild_suffix(name.split(1));
  558. // Unit Tests use those domains for Wildcard test.
  559. if (name.equals(Name("www.wild.example.com"))||
  560. name.equals(Name("www1.uwild.example.com"))||
  561. name.equals(Name("a.t.example.com"))) {
  562. if (name.compare(wild_suffix).getRelation() ==
  563. NameComparisonResult::SUBDOMAIN) {
  564. domain = domains_.find(Name("*").concatenate(wild_suffix));
  565. // Matched the QNAME
  566. if (domain != domains_.end()) {
  567. RRsetStore::const_iterator found_rrset =
  568. domain->second.find(type);
  569. // Matched the QTYPE
  570. if(found_rrset != domain->second.end()) {
  571. return (FindResult(SUCCESS,
  572. substituteWild(
  573. *found_rrset->second, name),
  574. RESULT_WILDCARD |
  575. (use_nsec3_ ?
  576. RESULT_NSEC3_SIGNED :
  577. RESULT_NSEC_SIGNED)));
  578. } else {
  579. // No matched QTYPE, this case is for WILDCARD_NXRRSET
  580. if (use_nsec3_) {
  581. return (FindResult(NXRRSET, RRsetPtr(),
  582. RESULT_WILDCARD |
  583. RESULT_NSEC3_SIGNED));
  584. }
  585. const Name new_name =
  586. Name("*").concatenate(wild_suffix);
  587. found_rrset = domain->second.find(RRType::NSEC());
  588. assert(found_rrset != domain->second.end());
  589. return (FindResult(NXRRSET,
  590. substituteWild(
  591. *found_rrset->second,
  592. new_name),
  593. RESULT_WILDCARD |
  594. RESULT_NSEC_SIGNED));
  595. }
  596. } else {
  597. // This is empty non terminal name case on wildcard.
  598. const Name empty_name = Name("*").concatenate(wild_suffix);
  599. if (use_nsec3_) {
  600. return (FindResult(NXRRSET, RRsetPtr(),
  601. RESULT_WILDCARD |
  602. RESULT_NSEC3_SIGNED));
  603. }
  604. for (Domains::reverse_iterator it = domains_.rbegin();
  605. it != domains_.rend();
  606. ++it) {
  607. RRsetStore::const_iterator nsec_it;
  608. if ((*it).first < empty_name &&
  609. (nsec_it = (*it).second.find(RRType::NSEC()))
  610. != (*it).second.end()) {
  611. return (FindResult(NXRRSET, (*nsec_it).second,
  612. RESULT_WILDCARD |
  613. RESULT_NSEC_SIGNED));
  614. }
  615. }
  616. }
  617. return (FindResult(NXRRSET, RRsetPtr(), RESULT_WILDCARD));
  618. }
  619. }
  620. const Name cnamewild_suffix("cnamewild.example.com");
  621. if (name.compare(cnamewild_suffix).getRelation() ==
  622. NameComparisonResult::SUBDOMAIN) {
  623. domain = domains_.find(Name("*").concatenate(cnamewild_suffix));
  624. assert(domain != domains_.end());
  625. RRsetStore::const_iterator found_rrset =
  626. domain->second.find(RRType::CNAME());
  627. assert(found_rrset != domain->second.end());
  628. return (FindResult(CNAME,
  629. substituteWild(*found_rrset->second, name),
  630. RESULT_WILDCARD |
  631. (use_nsec3_ ? RESULT_NSEC3_SIGNED :
  632. RESULT_NSEC_SIGNED)));
  633. }
  634. }
  635. // This is an NXDOMAIN case.
  636. // If we need DNSSEC proof, find the "previous name" that has an NSEC RR
  637. // and return NXDOMAIN with the found NSEC. Otherwise, just return the
  638. // NXDOMAIN code and NULL. If DNSSEC proof is requested but no NSEC is
  639. // found, we return NULL, too. (For simplicity under the test conditions
  640. // we don't care about pathological cases such as the name is "smaller"
  641. // than the origin)
  642. if ((options & FIND_DNSSEC) != 0) {
  643. if (use_nsec3_) {
  644. return (FindResult(NXDOMAIN, RRsetPtr(), RESULT_NSEC3_SIGNED));
  645. }
  646. // Emulate a broken DataSourceClient for some special names.
  647. if (nsec_result_ && nsec_name_ == name) {
  648. return (*nsec_result_);
  649. }
  650. // Normal case
  651. // XXX: some older g++ complains about operator!= if we use
  652. // const_reverse_iterator
  653. for (Domains::reverse_iterator it = domains_.rbegin();
  654. it != domains_.rend();
  655. ++it) {
  656. RRsetStore::const_iterator nsec_it;
  657. if ((*it).first < name &&
  658. (nsec_it = (*it).second.find(RRType::NSEC()))
  659. != (*it).second.end()) {
  660. return (FindResult(NXDOMAIN, (*nsec_it).second,
  661. RESULT_NSEC_SIGNED));
  662. }
  663. }
  664. }
  665. return (FindResult(NXDOMAIN, RRsetPtr()));
  666. }
  667. class QueryTest : public ::testing::Test {
  668. protected:
  669. QueryTest() :
  670. qname(Name("www.example.com")), qclass(RRClass::IN()),
  671. qtype(RRType::A()), response(Message::RENDER),
  672. qid(response.getQid()), query_code(Opcode::QUERY().getCode())
  673. {
  674. response.setRcode(Rcode::NOERROR());
  675. response.setOpcode(Opcode::QUERY());
  676. // create and add a matching zone.
  677. mock_finder = new MockZoneFinder();
  678. memory_client.addZone(ZoneFinderPtr(mock_finder));
  679. }
  680. MockZoneFinder* mock_finder;
  681. // We use InMemoryClient here. We could have some kind of mock client
  682. // here, but historically, the Query supported only InMemoryClient
  683. // (originally named MemoryDataSrc) and was tested with it, so we keep
  684. // it like this for now.
  685. InMemoryClient memory_client;
  686. const Name qname;
  687. const RRClass qclass;
  688. const RRType qtype;
  689. Message response;
  690. const qid_t qid;
  691. const uint16_t query_code;
  692. };
  693. // A wrapper to check resulting response message commonly used in
  694. // tests below.
  695. // check_origin needs to be specified only when the authority section has
  696. // an SOA RR. The interface is not generic enough but should be okay
  697. // for our test cases in practice.
  698. void
  699. responseCheck(Message& response, const isc::dns::Rcode& rcode,
  700. unsigned int flags, const unsigned int ancount,
  701. const unsigned int nscount, const unsigned int arcount,
  702. const char* const expected_answer,
  703. const char* const expected_authority,
  704. const char* const expected_additional,
  705. const Name& check_origin = Name::ROOT_NAME())
  706. {
  707. // In our test cases QID, Opcode, and QDCOUNT should be constant, so
  708. // we don't bother the test cases specifying these values.
  709. headerCheck(response, response.getQid(), rcode, Opcode::QUERY().getCode(),
  710. flags, 0, ancount, nscount, arcount);
  711. if (expected_answer != NULL) {
  712. rrsetsCheck(expected_answer,
  713. response.beginSection(Message::SECTION_ANSWER),
  714. response.endSection(Message::SECTION_ANSWER),
  715. check_origin);
  716. }
  717. if (expected_authority != NULL) {
  718. rrsetsCheck(expected_authority,
  719. response.beginSection(Message::SECTION_AUTHORITY),
  720. response.endSection(Message::SECTION_AUTHORITY),
  721. check_origin);
  722. }
  723. if (expected_additional != NULL) {
  724. rrsetsCheck(expected_additional,
  725. response.beginSection(Message::SECTION_ADDITIONAL),
  726. response.endSection(Message::SECTION_ADDITIONAL));
  727. }
  728. }
  729. TEST_F(QueryTest, noZone) {
  730. // There's no zone in the memory datasource. So the response should have
  731. // REFUSED.
  732. InMemoryClient empty_memory_client;
  733. Query nozone_query(empty_memory_client, qname, qtype, response);
  734. EXPECT_NO_THROW(nozone_query.process());
  735. EXPECT_EQ(Rcode::REFUSED(), response.getRcode());
  736. }
  737. TEST_F(QueryTest, exactMatch) {
  738. Query query(memory_client, qname, qtype, response);
  739. EXPECT_NO_THROW(query.process());
  740. // find match rrset
  741. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 3, 3,
  742. www_a_txt, zone_ns_txt, ns_addrs_txt);
  743. }
  744. TEST_F(QueryTest, exactMatchIgnoreSIG) {
  745. // Check that we do not include the RRSIG when not requested even when
  746. // we receive it from the data source.
  747. mock_finder->setIncludeRRSIGAnyway(true);
  748. Query query(memory_client, qname, qtype, response);
  749. EXPECT_NO_THROW(query.process());
  750. // find match rrset
  751. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 3, 3,
  752. www_a_txt, zone_ns_txt, ns_addrs_txt);
  753. }
  754. TEST_F(QueryTest, dnssecPositive) {
  755. // Just like exactMatch, but the signatures should be included as well
  756. Query query(memory_client, qname, qtype, response, true);
  757. EXPECT_NO_THROW(query.process());
  758. // find match rrset
  759. // We can't let responseCheck to check the additional section as well,
  760. // it gets confused by the two RRs for glue.delegation.../RRSIG due
  761. // to it's design and fixing it would be hard. Therefore we simply
  762. // check manually this one time.
  763. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 2, 4, 6,
  764. (www_a_txt + std::string("www.example.com. 3600 IN RRSIG "
  765. "A 5 3 3600 20000101000000 "
  766. "20000201000000 12345 example.com. "
  767. "FAKEFAKEFAKE\n")).c_str(),
  768. (zone_ns_txt + std::string("example.com. 3600 IN RRSIG NS 5 "
  769. "3 3600 20000101000000 "
  770. "20000201000000 12345 "
  771. "example.com. FAKEFAKEFAKE\n")).
  772. c_str(), NULL);
  773. RRsetIterator iterator(response.beginSection(Message::SECTION_ADDITIONAL));
  774. const char* additional[] = {
  775. "glue.delegation.example.com. 3600 IN A 192.0.2.153\n",
  776. "glue.delegation.example.com. 3600 IN RRSIG A 5 3 3600 20000101000000 "
  777. "20000201000000 12345 example.com. FAKEFAKEFAKE\n",
  778. "glue.delegation.example.com. 3600 IN AAAA 2001:db8::53\n",
  779. "glue.delegation.example.com. 3600 IN RRSIG AAAA 5 3 3600 "
  780. "20000101000000 20000201000000 12345 example.com. FAKEFAKEFAKE\n",
  781. "noglue.example.com. 3600 IN A 192.0.2.53\n",
  782. "noglue.example.com. 3600 IN RRSIG A 5 3 3600 20000101000000 "
  783. "20000201000000 12345 example.com. FAKEFAKEFAKE\n",
  784. NULL
  785. };
  786. for (const char** rr(additional); *rr != NULL; ++ rr) {
  787. ASSERT_FALSE(iterator ==
  788. response.endSection(Message::SECTION_ADDITIONAL));
  789. EXPECT_EQ(*rr, (*iterator)->toText());
  790. iterator ++;
  791. }
  792. EXPECT_TRUE(iterator == response.endSection(Message::SECTION_ADDITIONAL));
  793. }
  794. TEST_F(QueryTest, exactAddrMatch) {
  795. // find match rrset, omit additional data which has already been provided
  796. // in the answer section from the additional.
  797. EXPECT_NO_THROW(Query(memory_client, Name("noglue.example.com"), qtype,
  798. response).process());
  799. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 3, 2,
  800. "noglue.example.com. 3600 IN A 192.0.2.53\n", zone_ns_txt,
  801. "glue.delegation.example.com. 3600 IN A 192.0.2.153\n"
  802. "glue.delegation.example.com. 3600 IN AAAA 2001:db8::53\n");
  803. }
  804. TEST_F(QueryTest, apexNSMatch) {
  805. // find match rrset, omit authority data which has already been provided
  806. // in the answer section from the authority section.
  807. EXPECT_NO_THROW(Query(memory_client, Name("example.com"), RRType::NS(),
  808. response).process());
  809. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 3, 0, 3,
  810. zone_ns_txt, NULL, ns_addrs_txt);
  811. }
  812. // test type any query logic
  813. TEST_F(QueryTest, exactAnyMatch) {
  814. // find match rrset, omit additional data which has already been provided
  815. // in the answer section from the additional.
  816. EXPECT_NO_THROW(Query(memory_client, Name("noglue.example.com"),
  817. RRType::ANY(), response).process());
  818. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 2, 3, 2,
  819. (string("noglue.example.com. 3600 IN A 192.0.2.53\n") +
  820. string(nsec_nxdomain_txt)).c_str(),
  821. zone_ns_txt,
  822. "glue.delegation.example.com. 3600 IN A 192.0.2.153\n"
  823. "glue.delegation.example.com. 3600 IN AAAA 2001:db8::53\n");
  824. }
  825. TEST_F(QueryTest, apexAnyMatch) {
  826. // find match rrset, omit additional data which has already been provided
  827. // in the answer section from the additional.
  828. EXPECT_NO_THROW(Query(memory_client, Name("example.com"),
  829. RRType::ANY(), response).process());
  830. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 5, 0, 3,
  831. (string(soa_txt) + string(zone_ns_txt) +
  832. string(nsec_apex_txt)).c_str(),
  833. NULL, ns_addrs_txt, mock_finder->getOrigin());
  834. }
  835. TEST_F(QueryTest, mxANYMatch) {
  836. EXPECT_NO_THROW(Query(memory_client, Name("mx.example.com"),
  837. RRType::ANY(), response).process());
  838. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 4, 3, 4,
  839. (string(mx_txt) + string(nsec_mx_txt)).c_str(), zone_ns_txt,
  840. (string(ns_addrs_txt) + string(www_a_txt)).c_str());
  841. }
  842. TEST_F(QueryTest, glueANYMatch) {
  843. EXPECT_NO_THROW(Query(memory_client, Name("delegation.example.com"),
  844. RRType::ANY(), response).process());
  845. responseCheck(response, Rcode::NOERROR(), 0, 0, 4, 3,
  846. NULL, delegation_txt, ns_addrs_txt);
  847. }
  848. TEST_F(QueryTest, nodomainANY) {
  849. EXPECT_NO_THROW(Query(memory_client, Name("nxdomain.example.com"),
  850. RRType::ANY(), response).process());
  851. responseCheck(response, Rcode::NXDOMAIN(), AA_FLAG, 0, 1, 0,
  852. NULL, soa_txt, NULL, mock_finder->getOrigin());
  853. }
  854. // This tests that when we need to look up Zone's apex NS records for
  855. // authoritative answer, and there is no apex NS records. It should
  856. // throw in that case.
  857. TEST_F(QueryTest, noApexNS) {
  858. // Disable apex NS record
  859. mock_finder->setApexNSFlag(false);
  860. EXPECT_THROW(Query(memory_client, Name("noglue.example.com"), qtype,
  861. response).process(), Query::NoApexNS);
  862. // We don't look into the response, as it threw
  863. }
  864. TEST_F(QueryTest, delegation) {
  865. EXPECT_NO_THROW(Query(memory_client, Name("delegation.example.com"),
  866. qtype, response).process());
  867. responseCheck(response, Rcode::NOERROR(), 0, 0, 4, 3,
  868. NULL, delegation_txt, ns_addrs_txt);
  869. }
  870. TEST_F(QueryTest, secureDelegation) {
  871. EXPECT_NO_THROW(Query(memory_client,
  872. Name("foo.signed-delegation.example.com"),
  873. qtype, response, true).process());
  874. // Should now contain RRSIG and DS record as well.
  875. responseCheck(response, Rcode::NOERROR(), 0, 0, 3, 0,
  876. NULL,
  877. (string(signed_delegation_txt) +
  878. string(signed_delegation_ds_txt) +
  879. string("signed-delegation.example.com. 3600 IN RRSIG ") +
  880. getCommonRRSIGText("DS")).c_str(),
  881. NULL);
  882. }
  883. TEST_F(QueryTest, secureUnsignedDelegation) {
  884. EXPECT_NO_THROW(Query(memory_client,
  885. Name("foo.unsigned-delegation.example.com"),
  886. qtype, response, true).process());
  887. // Should now contain RRSIG and NSEC record as well.
  888. responseCheck(response, Rcode::NOERROR(), 0, 0, 3, 0,
  889. NULL,
  890. (string(unsigned_delegation_txt) +
  891. string(unsigned_delegation_nsec_txt) +
  892. string("unsigned-delegation.example.com. 3600 IN RRSIG ") +
  893. getCommonRRSIGText("NSEC")).c_str(),
  894. NULL);
  895. }
  896. TEST_F(QueryTest, badSecureDelegation) {
  897. // Test whether exception is raised if DS query at delegation results in
  898. // something different than SUCCESS or NXRRSET
  899. EXPECT_THROW(Query(memory_client, Name("bad-delegation.example.com"),
  900. qtype, response, true).process(), Query::BadDS);
  901. // But only if DNSSEC is requested (it shouldn't even try to look for
  902. // the DS otherwise)
  903. EXPECT_NO_THROW(Query(memory_client, Name("bad-delegation.example.com"),
  904. qtype, response).process());
  905. }
  906. TEST_F(QueryTest, nxdomain) {
  907. EXPECT_NO_THROW(Query(memory_client, Name("nxdomain.example.com"), qtype,
  908. response).process());
  909. responseCheck(response, Rcode::NXDOMAIN(), AA_FLAG, 0, 1, 0,
  910. NULL, soa_txt, NULL, mock_finder->getOrigin());
  911. }
  912. TEST_F(QueryTest, nxdomainWithNSEC) {
  913. // NXDOMAIN with DNSSEC proof. We should have SOA, NSEC that proves
  914. // NXDOMAIN and NSEC that proves nonexistence of matching wildcard,
  915. // as well as their RRSIGs.
  916. EXPECT_NO_THROW(Query(memory_client, Name("nxdomain.example.com"), qtype,
  917. response, true).process());
  918. responseCheck(response, Rcode::NXDOMAIN(), AA_FLAG, 0, 6, 0,
  919. NULL, (string(soa_txt) +
  920. string("example.com. 3600 IN RRSIG ") +
  921. getCommonRRSIGText("SOA") + "\n" +
  922. string(nsec_nxdomain_txt) + "\n" +
  923. string("noglue.example.com. 3600 IN RRSIG ") +
  924. getCommonRRSIGText("NSEC") + "\n" +
  925. string(nsec_apex_txt) + "\n" +
  926. string("example.com. 3600 IN RRSIG ") +
  927. getCommonRRSIGText("NSEC")).c_str(),
  928. NULL, mock_finder->getOrigin());
  929. }
  930. TEST_F(QueryTest, nxdomainWithNSEC2) {
  931. // See comments about no_txt. In this case the best possible wildcard
  932. // is derived from the next domain of the NSEC that proves NXDOMAIN, and
  933. // the NSEC to provide the non existence of wildcard is different from
  934. // the first NSEC.
  935. Query(memory_client, Name("(.no.example.com"), qtype,
  936. response, true).process();
  937. responseCheck(response, Rcode::NXDOMAIN(), AA_FLAG, 0, 6, 0,
  938. NULL, (string(soa_txt) +
  939. string("example.com. 3600 IN RRSIG ") +
  940. getCommonRRSIGText("SOA") + "\n" +
  941. string(nsec_mx_txt) + "\n" +
  942. string("mx.example.com. 3600 IN RRSIG ") +
  943. getCommonRRSIGText("NSEC") + "\n" +
  944. string(nsec_no_txt) + "\n" +
  945. string(").no.example.com. 3600 IN RRSIG ") +
  946. getCommonRRSIGText("NSEC")).c_str(),
  947. NULL, mock_finder->getOrigin());
  948. }
  949. TEST_F(QueryTest, nxdomainWithNSECDuplicate) {
  950. // See comments about nz_txt. In this case we only need one NSEC,
  951. // which proves both NXDOMAIN and the non existence of wildcard.
  952. Query(memory_client, Name("nx.no.example.com"), qtype,
  953. response, true).process();
  954. responseCheck(response, Rcode::NXDOMAIN(), AA_FLAG, 0, 4, 0,
  955. NULL, (string(soa_txt) +
  956. string("example.com. 3600 IN RRSIG ") +
  957. getCommonRRSIGText("SOA") + "\n" +
  958. string(nsec_no_txt) + "\n" +
  959. string(").no.example.com. 3600 IN RRSIG ") +
  960. getCommonRRSIGText("NSEC")).c_str(),
  961. NULL, mock_finder->getOrigin());
  962. }
  963. TEST_F(QueryTest, nxdomainBadNSEC1) {
  964. // ZoneFinder::find() returns NXDOMAIN with non NSEC RR.
  965. mock_finder->setNSECResult(Name("badnsec.example.com"),
  966. ZoneFinder::NXDOMAIN,
  967. mock_finder->delegation_rrset_);
  968. EXPECT_THROW(Query(memory_client, Name("badnsec.example.com"), qtype,
  969. response, true).process(),
  970. std::bad_cast);
  971. }
  972. TEST_F(QueryTest, nxdomainBadNSEC2) {
  973. // ZoneFinder::find() returns NXDOMAIN with an empty NSEC RR.
  974. mock_finder->setNSECResult(Name("emptynsec.example.com"),
  975. ZoneFinder::NXDOMAIN,
  976. mock_finder->empty_nsec_rrset_);
  977. EXPECT_THROW(Query(memory_client, Name("emptynsec.example.com"), qtype,
  978. response, true).process(),
  979. Query::BadNSEC);
  980. }
  981. TEST_F(QueryTest, nxdomainBadNSEC3) {
  982. // "no-wildcard proof" returns SUCCESS. it should be NXDOMAIN.
  983. mock_finder->setNSECResult(Name("*.example.com"),
  984. ZoneFinder::SUCCESS,
  985. mock_finder->delegation_rrset_);
  986. EXPECT_THROW(Query(memory_client, Name("nxdomain.example.com"), qtype,
  987. response, true).process(),
  988. Query::BadNSEC);
  989. }
  990. TEST_F(QueryTest, nxdomainBadNSEC4) {
  991. // "no-wildcard proof" doesn't return RRset.
  992. mock_finder->setNSECResult(Name("*.example.com"),
  993. ZoneFinder::NXDOMAIN, ConstRRsetPtr());
  994. EXPECT_THROW(Query(memory_client, Name("nxdomain.example.com"), qtype,
  995. response, true).process(),
  996. Query::BadNSEC);
  997. }
  998. TEST_F(QueryTest, nxdomainBadNSEC5) {
  999. // "no-wildcard proof" returns non NSEC.
  1000. mock_finder->setNSECResult(Name("*.example.com"),
  1001. ZoneFinder::NXDOMAIN,
  1002. mock_finder->delegation_rrset_);
  1003. // This is a bit odd, but we'll simply include the returned RRset.
  1004. Query(memory_client, Name("nxdomain.example.com"), qtype,
  1005. response, true).process();
  1006. responseCheck(response, Rcode::NXDOMAIN(), AA_FLAG, 0, 8, 0,
  1007. NULL, (string(soa_txt) +
  1008. string("example.com. 3600 IN RRSIG ") +
  1009. getCommonRRSIGText("SOA") + "\n" +
  1010. string(nsec_nxdomain_txt) + "\n" +
  1011. string("noglue.example.com. 3600 IN RRSIG ") +
  1012. getCommonRRSIGText("NSEC") + "\n" +
  1013. delegation_txt).c_str(),
  1014. NULL, mock_finder->getOrigin());
  1015. }
  1016. TEST_F(QueryTest, nxdomainBadNSEC6) {
  1017. // "no-wildcard proof" returns empty NSEC.
  1018. mock_finder->setNSECResult(Name("*.example.com"),
  1019. ZoneFinder::NXDOMAIN,
  1020. mock_finder->empty_nsec_rrset_);
  1021. EXPECT_THROW(Query(memory_client, Name("nxdomain.example.com"), qtype,
  1022. response, true).process(),
  1023. Query::BadNSEC);
  1024. }
  1025. TEST_F(QueryTest, nxrrset) {
  1026. EXPECT_NO_THROW(Query(memory_client, Name("www.example.com"),
  1027. RRType::TXT(), response).process());
  1028. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 0, 1, 0,
  1029. NULL, soa_txt, NULL, mock_finder->getOrigin());
  1030. }
  1031. TEST_F(QueryTest, nxrrsetWithNSEC) {
  1032. // NXRRSET with DNSSEC proof. We should have SOA, NSEC that proves the
  1033. // NXRRSET and their RRSIGs.
  1034. Query(memory_client, Name("www.example.com"), RRType::TXT(), response,
  1035. true).process();
  1036. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 0, 4, 0, NULL,
  1037. (string(soa_txt) + string("example.com. 3600 IN RRSIG ") +
  1038. getCommonRRSIGText("SOA") + "\n" +
  1039. string(nsec_www_txt) + "\n" +
  1040. string("www.example.com. 3600 IN RRSIG ") +
  1041. getCommonRRSIGText("NSEC")).c_str(),
  1042. NULL, mock_finder->getOrigin());
  1043. }
  1044. TEST_F(QueryTest, emptyNameWithNSEC) {
  1045. // Empty non terminal with DNSSEC proof. This is one of the cases of
  1046. // Section 3.1.3.2 of RFC4035.
  1047. // mx.example.com. NSEC ).no.example.com. proves no.example.com. is a
  1048. // non empty terminal node. Note that it also implicitly proves there
  1049. // should be no closer wildcard match (because the empty name is an
  1050. // exact match), so we only need one NSEC.
  1051. // From the point of the Query::process(), this is actually no different
  1052. // from the other NXRRSET case, but we check that explicitly just in case.
  1053. Query(memory_client, Name("no.example.com"), RRType::A(), response,
  1054. true).process();
  1055. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 0, 4, 0, NULL,
  1056. (string(soa_txt) + string("example.com. 3600 IN RRSIG ") +
  1057. getCommonRRSIGText("SOA") + "\n" +
  1058. string(nsec_mx_txt) + "\n" +
  1059. string("mx.example.com. 3600 IN RRSIG ") +
  1060. getCommonRRSIGText("NSEC")).c_str(),
  1061. NULL, mock_finder->getOrigin());
  1062. }
  1063. TEST_F(QueryTest, nxrrsetWithoutNSEC) {
  1064. // NXRRSET with DNSSEC proof requested, but there's no NSEC at that node.
  1065. // This is an unexpected event (if the zone is supposed to be properly
  1066. // signed with NSECs), but we accept and ignore the oddity.
  1067. Query(memory_client, Name("nonsec.example.com"), RRType::TXT(), response,
  1068. true).process();
  1069. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 0, 2, 0, NULL,
  1070. (string(soa_txt) + string("example.com. 3600 IN RRSIG ") +
  1071. getCommonRRSIGText("SOA") + "\n").c_str(),
  1072. NULL, mock_finder->getOrigin());
  1073. }
  1074. TEST_F(QueryTest, wildcardNSEC) {
  1075. // The qname matches *.wild.example.com. The response should contain
  1076. // an NSEC that proves the non existence of a closer name.
  1077. Query(memory_client, Name("www.wild.example.com"), RRType::A(), response,
  1078. true).process();
  1079. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 2, 6, 6,
  1080. (string(wild_txt).replace(0, 1, "www") +
  1081. string("www.wild.example.com. 3600 IN RRSIG ") +
  1082. getCommonRRSIGText("A") + "\n").c_str(),
  1083. (zone_ns_txt + string("example.com. 3600 IN RRSIG NS 5 "
  1084. "3 3600 20000101000000 "
  1085. "20000201000000 12345 "
  1086. "example.com. FAKEFAKEFAKE\n") +
  1087. string(nsec_wild_txt) +
  1088. string("*.wild.example.com. 3600 IN RRSIG ") +
  1089. getCommonRRSIGText("NSEC") + "\n").c_str(),
  1090. NULL, // we are not interested in additionals in this test
  1091. mock_finder->getOrigin());
  1092. }
  1093. TEST_F(QueryTest, CNAMEwildNSEC) {
  1094. // Similar to the previous case, but the matching wildcard record is
  1095. // CNAME.
  1096. Query(memory_client, Name("www.cnamewild.example.com"), RRType::A(),
  1097. response, true).process();
  1098. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 2, 2, 0,
  1099. (string(cnamewild_txt).replace(0, 1, "www") +
  1100. string("www.cnamewild.example.com. 3600 IN RRSIG ") +
  1101. getCommonRRSIGText("CNAME") + "\n").c_str(),
  1102. (string(nsec_cnamewild_txt) +
  1103. string("*.cnamewild.example.com. 3600 IN RRSIG ") +
  1104. getCommonRRSIGText("NSEC") + "\n").c_str(),
  1105. NULL, // we are not interested in additionals in this test
  1106. mock_finder->getOrigin());
  1107. }
  1108. TEST_F(QueryTest, badWildcardProof1) {
  1109. // Unexpected case in wildcard proof: ZoneFinder::find() returns SUCCESS
  1110. // when NXDOMAIN is expected.
  1111. mock_finder->setNSECResult(Name("www.wild.example.com"),
  1112. ZoneFinder::SUCCESS,
  1113. mock_finder->delegation_rrset_);
  1114. EXPECT_THROW(Query(memory_client, Name("www.wild.example.com"),
  1115. RRType::A(), response, true).process(),
  1116. Query::BadNSEC);
  1117. }
  1118. TEST_F(QueryTest, badWildcardProof2) {
  1119. // "wildcard proof" doesn't return RRset.
  1120. mock_finder->setNSECResult(Name("www.wild.example.com"),
  1121. ZoneFinder::NXDOMAIN, ConstRRsetPtr());
  1122. EXPECT_THROW(Query(memory_client, Name("www.wild.example.com"),
  1123. RRType::A(), response, true).process(),
  1124. Query::BadNSEC);
  1125. }
  1126. TEST_F(QueryTest, badWildcardProof3) {
  1127. // "wildcard proof" returns empty NSEC.
  1128. mock_finder->setNSECResult(Name("www.wild.example.com"),
  1129. ZoneFinder::NXDOMAIN,
  1130. mock_finder->empty_nsec_rrset_);
  1131. EXPECT_THROW(Query(memory_client, Name("www.wild.example.com"),
  1132. RRType::A(), response, true).process(),
  1133. Query::BadNSEC);
  1134. }
  1135. TEST_F(QueryTest, wildcardNxrrsetWithDuplicateNSEC) {
  1136. // WILDCARD_NXRRSET with DNSSEC proof. We should have SOA, NSEC that proves the
  1137. // NXRRSET and their RRSIGs. In this case we only need one NSEC,
  1138. // which proves both NXDOMAIN and the non existence RRSETs of wildcard.
  1139. Query(memory_client, Name("www.wild.example.com"), RRType::TXT(), response,
  1140. true).process();
  1141. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 0, 4, 0, NULL,
  1142. (string(soa_txt) + string("example.com. 3600 IN RRSIG ") +
  1143. getCommonRRSIGText("SOA") + "\n" +
  1144. string(nsec_wild_txt) +
  1145. string("*.wild.example.com. 3600 IN RRSIG ") +
  1146. getCommonRRSIGText("NSEC")+"\n").c_str(),
  1147. NULL, mock_finder->getOrigin());
  1148. }
  1149. TEST_F(QueryTest, wildcardNxrrsetWithNSEC) {
  1150. // WILDCARD_NXRRSET with DNSSEC proof. We should have SOA, NSEC that proves the
  1151. // NXRRSET and their RRSIGs. In this case we need two NSEC RRs,
  1152. // one proves NXDOMAIN and the other proves non existence RRSETs of wildcard.
  1153. Query(memory_client, Name("www1.uwild.example.com"), RRType::TXT(), response,
  1154. true).process();
  1155. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 0, 6, 0, NULL,
  1156. (string(soa_txt) + string("example.com. 3600 IN RRSIG ") +
  1157. getCommonRRSIGText("SOA") + "\n" +
  1158. string(nsec_wild_txt_nxrrset) +
  1159. string("*.uwild.example.com. 3600 IN RRSIG ") +
  1160. getCommonRRSIGText("NSEC")+"\n" +
  1161. string(nsec_wild_txt_next) +
  1162. string("www.uwild.example.com. 3600 IN RRSIG ") +
  1163. getCommonRRSIGText("NSEC") + "\n").c_str(),
  1164. NULL, mock_finder->getOrigin());
  1165. }
  1166. TEST_F(QueryTest, wildcardEmptyWithNSEC) {
  1167. // WILDCARD_EMPTY with DNSSEC proof. We should have SOA, NSEC that proves the
  1168. // NXDOMAIN and their RRSIGs. In this case we need two NSEC RRs,
  1169. // one proves NXDOMAIN and the other proves non existence wildcard.
  1170. Query(memory_client, Name("a.t.example.com"), RRType::A(), response,
  1171. true).process();
  1172. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 0, 6, 0, NULL,
  1173. (string(soa_txt) + string("example.com. 3600 IN RRSIG ") +
  1174. getCommonRRSIGText("SOA") + "\n" +
  1175. string(nsec_empty_prev_txt) +
  1176. string("t.example.com. 3600 IN RRSIG ") +
  1177. getCommonRRSIGText("NSEC")+"\n" +
  1178. string(nsec_empty_txt) +
  1179. string("b.*.t.example.com. 3600 IN RRSIG ") +
  1180. getCommonRRSIGText("NSEC")+"\n").c_str(),
  1181. NULL, mock_finder->getOrigin());
  1182. }
  1183. /*
  1184. * This tests that when there's no SOA and we need a negative answer. It should
  1185. * throw in that case.
  1186. */
  1187. TEST_F(QueryTest, noSOA) {
  1188. // disable zone's SOA RR.
  1189. mock_finder->setSOAFlag(false);
  1190. // The NX Domain
  1191. EXPECT_THROW(Query(memory_client, Name("nxdomain.example.com"),
  1192. qtype, response).process(), Query::NoSOA);
  1193. // Of course, we don't look into the response, as it throwed
  1194. // NXRRSET
  1195. EXPECT_THROW(Query(memory_client, Name("nxrrset.example.com"),
  1196. qtype, response).process(), Query::NoSOA);
  1197. }
  1198. TEST_F(QueryTest, noMatchZone) {
  1199. // there's a zone in the memory datasource but it doesn't match the qname.
  1200. // should result in REFUSED.
  1201. Query(memory_client, Name("example.org"), qtype, response).process();
  1202. EXPECT_EQ(Rcode::REFUSED(), response.getRcode());
  1203. }
  1204. /*
  1205. * Test MX additional processing.
  1206. *
  1207. * The MX RRset has two RRs, one pointing to a known domain with
  1208. * A record, other to unknown out of zone one.
  1209. */
  1210. TEST_F(QueryTest, MX) {
  1211. Query(memory_client, Name("mx.example.com"), RRType::MX(),
  1212. response).process();
  1213. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 3, 3, 4,
  1214. mx_txt, NULL,
  1215. (string(ns_addrs_txt) + string(www_a_txt)).c_str());
  1216. }
  1217. /*
  1218. * Test when we ask for MX whose exchange is an alias (CNAME in this case).
  1219. *
  1220. * This should not trigger the additional processing for the exchange.
  1221. */
  1222. TEST_F(QueryTest, MXAlias) {
  1223. Query(memory_client, Name("cnamemx.example.com"), RRType::MX(),
  1224. response).process();
  1225. // there shouldn't be no additional RRs for the exchanges (we have 3
  1226. // RRs for the NS). The normal MX case is tested separately so we don't
  1227. // bother to examine the answer (and authority) sections.
  1228. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 3, 3,
  1229. NULL, NULL, ns_addrs_txt);
  1230. }
  1231. /*
  1232. * Tests encountering a cname.
  1233. *
  1234. * There are tests leading to successful answers, NXRRSET, NXDOMAIN and
  1235. * out of the zone.
  1236. *
  1237. * TODO: We currently don't do chaining, so only the CNAME itself should be
  1238. * returned.
  1239. */
  1240. TEST_F(QueryTest, CNAME) {
  1241. Query(memory_client, Name("cname.example.com"), RRType::A(),
  1242. response).process();
  1243. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 0, 0,
  1244. cname_txt, NULL, NULL);
  1245. }
  1246. TEST_F(QueryTest, explicitCNAME) {
  1247. // same owner name as the CNAME test but explicitly query for CNAME RR.
  1248. // expect the same response as we don't provide a full chain yet.
  1249. Query(memory_client, Name("cname.example.com"), RRType::CNAME(),
  1250. response).process();
  1251. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 3, 3,
  1252. cname_txt, zone_ns_txt, ns_addrs_txt);
  1253. }
  1254. TEST_F(QueryTest, CNAME_NX_RRSET) {
  1255. // Leads to www.example.com, it doesn't have TXT
  1256. // note: with chaining, what should be expected is not trivial:
  1257. // BIND 9 returns the CNAME in answer and SOA in authority, no additional.
  1258. // NSD returns the CNAME, NS in authority, A/AAAA for NS in additional.
  1259. Query(memory_client, Name("cname.example.com"), RRType::TXT(),
  1260. response).process();
  1261. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 0, 0,
  1262. cname_txt, NULL, NULL);
  1263. }
  1264. TEST_F(QueryTest, explicitCNAME_NX_RRSET) {
  1265. // same owner name as the NXRRSET test but explicitly query for CNAME RR.
  1266. Query(memory_client, Name("cname.example.com"), RRType::CNAME(),
  1267. response).process();
  1268. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 3, 3,
  1269. cname_txt, zone_ns_txt, ns_addrs_txt);
  1270. }
  1271. TEST_F(QueryTest, CNAME_NX_DOMAIN) {
  1272. // Leads to nxdomain.example.com
  1273. // note: with chaining, what should be expected is not trivial:
  1274. // BIND 9 returns the CNAME in answer and SOA in authority, no additional,
  1275. // RCODE being NXDOMAIN.
  1276. // NSD returns the CNAME, NS in authority, A/AAAA for NS in additional,
  1277. // RCODE being NOERROR.
  1278. Query(memory_client, Name("cnamenxdom.example.com"), RRType::A(),
  1279. response).process();
  1280. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 0, 0,
  1281. cname_nxdom_txt, NULL, NULL);
  1282. }
  1283. TEST_F(QueryTest, explicitCNAME_NX_DOMAIN) {
  1284. // same owner name as the NXDOMAIN test but explicitly query for CNAME RR.
  1285. Query(memory_client, Name("cnamenxdom.example.com"), RRType::CNAME(),
  1286. response).process();
  1287. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 3, 3,
  1288. cname_nxdom_txt, zone_ns_txt, ns_addrs_txt);
  1289. }
  1290. TEST_F(QueryTest, CNAME_OUT) {
  1291. /*
  1292. * This leads out of zone. This should have only the CNAME even
  1293. * when we do chaining.
  1294. *
  1295. * TODO: We should be able to have two zones in the mock data source.
  1296. * Then the same test should be done with .org included there and
  1297. * see what it does (depends on what we want to do)
  1298. */
  1299. Query(memory_client, Name("cnameout.example.com"), RRType::A(),
  1300. response).process();
  1301. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 0, 0,
  1302. cname_out_txt, NULL, NULL);
  1303. }
  1304. TEST_F(QueryTest, explicitCNAME_OUT) {
  1305. // same owner name as the OUT test but explicitly query for CNAME RR.
  1306. Query(memory_client, Name("cnameout.example.com"), RRType::CNAME(),
  1307. response).process();
  1308. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 3, 3,
  1309. cname_out_txt, zone_ns_txt, ns_addrs_txt);
  1310. }
  1311. /*
  1312. * Test a query under a domain with DNAME. We should get a synthetized CNAME
  1313. * as well as the DNAME.
  1314. *
  1315. * TODO: Once we have CNAME chaining, check it works with synthetized CNAMEs
  1316. * as well. This includes tests pointing inside the zone, outside the zone,
  1317. * pointing to NXRRSET and NXDOMAIN cases (similarly as with CNAME).
  1318. */
  1319. TEST_F(QueryTest, DNAME) {
  1320. Query(memory_client, Name("www.dname.example.com"), RRType::A(),
  1321. response).process();
  1322. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 2, 0, 0,
  1323. (string(dname_txt) + synthetized_cname_txt).c_str(),
  1324. NULL, NULL);
  1325. }
  1326. /*
  1327. * Ask an ANY query below a DNAME. Should return the DNAME and synthetized
  1328. * CNAME.
  1329. *
  1330. * ANY is handled specially sometimes. We check it is not the case with
  1331. * DNAME.
  1332. */
  1333. TEST_F(QueryTest, DNAME_ANY) {
  1334. Query(memory_client, Name("www.dname.example.com"), RRType::ANY(),
  1335. response).process();
  1336. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 2, 0, 0,
  1337. (string(dname_txt) + synthetized_cname_txt).c_str(), NULL, NULL);
  1338. }
  1339. // Test when we ask for DNAME explicitly, it does no synthetizing.
  1340. TEST_F(QueryTest, explicitDNAME) {
  1341. Query(memory_client, Name("dname.example.com"), RRType::DNAME(),
  1342. response).process();
  1343. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 3, 3,
  1344. dname_txt, zone_ns_txt, ns_addrs_txt);
  1345. }
  1346. /*
  1347. * Request a RRset at the domain with DNAME. It should not synthetize
  1348. * the CNAME, it should return the RRset.
  1349. */
  1350. TEST_F(QueryTest, DNAME_A) {
  1351. Query(memory_client, Name("dname.example.com"), RRType::A(),
  1352. response).process();
  1353. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 3, 3,
  1354. dname_a_txt, zone_ns_txt, ns_addrs_txt);
  1355. }
  1356. /*
  1357. * Request a RRset at the domain with DNAME that is not there (NXRRSET).
  1358. * It should not synthetize the CNAME.
  1359. */
  1360. TEST_F(QueryTest, DNAME_NX_RRSET) {
  1361. EXPECT_NO_THROW(Query(memory_client, Name("dname.example.com"),
  1362. RRType::TXT(), response).process());
  1363. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 0, 1, 0,
  1364. NULL, soa_txt, NULL, mock_finder->getOrigin());
  1365. }
  1366. /*
  1367. * Constructing the CNAME will result in a name that is too long. This,
  1368. * however, should not throw (and crash the server), but respond with
  1369. * YXDOMAIN.
  1370. */
  1371. TEST_F(QueryTest, LongDNAME) {
  1372. // A name that is as long as it can be
  1373. Name longname(
  1374. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  1375. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  1376. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  1377. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  1378. "dname.example.com.");
  1379. EXPECT_NO_THROW(Query(memory_client, longname, RRType::A(),
  1380. response).process());
  1381. responseCheck(response, Rcode::YXDOMAIN(), AA_FLAG, 1, 0, 0,
  1382. dname_txt, NULL, NULL);
  1383. }
  1384. /*
  1385. * Constructing the CNAME will result in a name of maximal length.
  1386. * This tests that we don't reject valid one by some kind of off by
  1387. * one mistake.
  1388. */
  1389. TEST_F(QueryTest, MaxLenDNAME) {
  1390. Name longname(
  1391. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  1392. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  1393. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  1394. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  1395. "dname.example.com.");
  1396. EXPECT_NO_THROW(Query(memory_client, longname, RRType::A(),
  1397. response).process());
  1398. // Check the answer is OK
  1399. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 2, 0, 0,
  1400. NULL, NULL, NULL);
  1401. // Check that the CNAME has the maximal length.
  1402. bool ok(false);
  1403. for (RRsetIterator i(response.beginSection(Message::SECTION_ANSWER));
  1404. i != response.endSection(Message::SECTION_ANSWER); ++ i) {
  1405. if ((*i)->getType() == RRType::CNAME()) {
  1406. ok = true;
  1407. RdataIteratorPtr ci((*i)->getRdataIterator());
  1408. ASSERT_FALSE(ci->isLast()) << "The CNAME is empty";
  1409. /*
  1410. * Does anybody have a clue why, if the Name::MAX_WIRE is put
  1411. * directly inside ASSERT_EQ, it fails to link and complains
  1412. * it is unresolved external?
  1413. */
  1414. const size_t max_len(Name::MAX_WIRE);
  1415. ASSERT_EQ(max_len, dynamic_cast<const rdata::generic::CNAME&>(
  1416. ci->getCurrent()).getCname().getLength());
  1417. }
  1418. }
  1419. EXPECT_TRUE(ok) << "The synthetized CNAME not found";
  1420. }
  1421. // Test for this test module itself
  1422. void
  1423. nsec3Check(bool expected_matched, uint8_t expected_labels,
  1424. const string& expected_rrsets_txt,
  1425. const ZoneFinder::FindNSEC3Result& result)
  1426. {
  1427. vector<ConstRRsetPtr> actual_rrsets;
  1428. EXPECT_EQ(expected_matched, result.matched);
  1429. // Convert to int so the error messages would be more readable:
  1430. EXPECT_EQ(static_cast<int>(expected_labels),
  1431. static_cast<int>(result.closest_labels));
  1432. if (result.closest_proof) {
  1433. actual_rrsets.push_back(result.closest_proof);
  1434. }
  1435. if (result.next_proof) {
  1436. actual_rrsets.push_back(result.next_proof);
  1437. }
  1438. rrsetsCheck(expected_rrsets_txt, actual_rrsets.begin(),
  1439. actual_rrsets.end());
  1440. }
  1441. TEST_F(QueryTest, findNSEC3) {
  1442. // In all test cases in the recursive mode, the closest encloser is the
  1443. // apex, and result's closest_labels should be the number of apex labels.
  1444. // (In non recursive mode closest_labels should be the # labels of the
  1445. // query name)
  1446. const uint8_t expected_closest_labels =
  1447. Name("example.com").getLabelCount();
  1448. // Apex name. It should have a matching NSEC3
  1449. nsec3Check(true, expected_closest_labels, nsec3_apex_txt,
  1450. mock_finder->findNSEC3(Name("example.com"), false));
  1451. // Recursive mode doesn't change the result in this case.
  1452. nsec3Check(true, expected_closest_labels, nsec3_apex_txt,
  1453. mock_finder->findNSEC3(Name("example.com"), true));
  1454. // Non existent name. Disabling recursion, a covering NSEC3 should be
  1455. // returned.
  1456. nsec3Check(false, 4, nsec3_www_txt,
  1457. mock_finder->findNSEC3(Name("nxdomain.example.com"), false));
  1458. // Non existent name. The closest provable encloser is the apex,
  1459. // and next closer is the query name.
  1460. nsec3Check(true, expected_closest_labels,
  1461. string(nsec3_apex_txt) + string(nsec3_www_txt),
  1462. mock_finder->findNSEC3(Name("nxdomain.example.com"), true));
  1463. // Similar to the previous case, but next closer name is different
  1464. // (is the parent) of the non existent name.
  1465. nsec3Check(true, expected_closest_labels,
  1466. string(nsec3_apex_txt) + string(nsec3_www_txt),
  1467. mock_finder->findNSEC3(Name("nx.domain.example.com"), true));
  1468. // In the rest of test we check hash comparison for wrap around cases.
  1469. nsec3Check(false, 4, nsec3_apex_txt,
  1470. mock_finder->findNSEC3(Name("nxdomain2.example.com"), false));
  1471. nsec3Check(false, 4, nsec3_www_txt,
  1472. mock_finder->findNSEC3(Name("nxdomain3.example.com"), false));
  1473. }
  1474. // The following are tentative tests until we really add tests for the
  1475. // query logic for these cases. At that point it's probably better to
  1476. // clean them up.
  1477. TEST_F(QueryTest, nxdomainWithNSEC3) {
  1478. mock_finder->setNSEC3Flag(true);
  1479. ZoneFinder::FindResult result = mock_finder->find(
  1480. Name("nxdomain.example.com"), RRType::A(), ZoneFinder::FIND_DNSSEC);
  1481. EXPECT_EQ(ZoneFinder::NXDOMAIN, result.code);
  1482. EXPECT_FALSE(result.rrset);
  1483. EXPECT_TRUE(result.isNSEC3Signed());
  1484. EXPECT_FALSE(result.isWildcard());
  1485. }
  1486. TEST_F(QueryTest, nxrrsetWithNSEC3) {
  1487. mock_finder->setNSEC3Flag(true);
  1488. ZoneFinder::FindResult result = mock_finder->find(
  1489. Name("www.example.com"), RRType::TXT(), ZoneFinder::FIND_DNSSEC);
  1490. EXPECT_EQ(ZoneFinder::NXRRSET, result.code);
  1491. EXPECT_FALSE(result.rrset);
  1492. EXPECT_TRUE(result.isNSEC3Signed());
  1493. EXPECT_FALSE(result.isWildcard());
  1494. }
  1495. TEST_F(QueryTest, emptyNameWithNSEC3) {
  1496. mock_finder->setNSEC3Flag(true);
  1497. ZoneFinder::FindResult result = mock_finder->find(
  1498. Name("no.example.com"), RRType::A(), ZoneFinder::FIND_DNSSEC);
  1499. EXPECT_EQ(ZoneFinder::NXRRSET, result.code);
  1500. EXPECT_FALSE(result.rrset);
  1501. EXPECT_TRUE(result.isNSEC3Signed());
  1502. EXPECT_FALSE(result.isWildcard());
  1503. }
  1504. TEST_F(QueryTest, wildcardNxrrsetWithNSEC3) {
  1505. mock_finder->setNSEC3Flag(true);
  1506. ZoneFinder::FindResult result = mock_finder->find(
  1507. Name("www1.uwild.example.com"), RRType::TXT(),
  1508. ZoneFinder::FIND_DNSSEC);
  1509. EXPECT_EQ(ZoneFinder::NXRRSET, result.code);
  1510. EXPECT_FALSE(result.rrset);
  1511. EXPECT_TRUE(result.isNSEC3Signed());
  1512. EXPECT_TRUE(result.isWildcard());
  1513. }
  1514. TEST_F(QueryTest, wildcardEmptyWithNSEC3) {
  1515. mock_finder->setNSEC3Flag(true);
  1516. ZoneFinder::FindResult result = mock_finder->find(
  1517. Name("a.t.example.com"), RRType::A(), ZoneFinder::FIND_DNSSEC);
  1518. EXPECT_EQ(ZoneFinder::NXRRSET, result.code);
  1519. EXPECT_TRUE(result.isNSEC3Signed());
  1520. EXPECT_TRUE(result.isWildcard());
  1521. }
  1522. }