query_unittest.cc 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  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 <dns/masterload.h>
  20. #include <dns/message.h>
  21. #include <dns/name.h>
  22. #include <dns/opcode.h>
  23. #include <dns/rcode.h>
  24. #include <dns/rrttl.h>
  25. #include <dns/rrtype.h>
  26. #include <dns/rdataclass.h>
  27. #include <datasrc/memory_datasrc.h>
  28. #include <auth/query.h>
  29. #include <testutils/dnsmessage_test.h>
  30. #include <gtest/gtest.h>
  31. using namespace std;
  32. using namespace isc::dns;
  33. using namespace isc::dns::rdata;
  34. using namespace isc::datasrc;
  35. using namespace isc::auth;
  36. using namespace isc::testutils;
  37. namespace {
  38. // This is the content of the mock zone (see below).
  39. // It's a sequence of textual RRs that is supposed to be parsed by
  40. // dns::masterLoad(). Some of the RRs are also used as the expected
  41. // data in specific tests, in which case they are referenced via specific
  42. // local variables (such as soa_txt).
  43. const char* const soa_txt = "example.com. 3600 IN SOA . . 0 0 0 0 0\n";
  44. const char* const zone_ns_txt =
  45. "example.com. 3600 IN NS glue.delegation.example.com.\n"
  46. "example.com. 3600 IN NS noglue.example.com.\n"
  47. "example.com. 3600 IN NS example.net.\n";
  48. const char* const ns_addrs_txt =
  49. "glue.delegation.example.com. 3600 IN A 192.0.2.153\n"
  50. "glue.delegation.example.com. 3600 IN AAAA 2001:db8::53\n"
  51. "noglue.example.com. 3600 IN A 192.0.2.53\n";
  52. const char* const delegation_txt =
  53. "delegation.example.com. 3600 IN NS glue.delegation.example.com.\n"
  54. "delegation.example.com. 3600 IN NS noglue.example.com.\n"
  55. "delegation.example.com. 3600 IN NS cname.example.com.\n"
  56. "delegation.example.com. 3600 IN NS example.org.\n";
  57. const char* const mx_txt =
  58. "mx.example.com. 3600 IN MX 10 www.example.com.\n"
  59. "mx.example.com. 3600 IN MX 20 mailer.example.org.\n"
  60. "mx.example.com. 3600 IN MX 30 mx.delegation.example.com.\n";
  61. const char* const www_a_txt = "www.example.com. 3600 IN A 192.0.2.80\n";
  62. const char* const cname_txt =
  63. "cname.example.com. 3600 IN CNAME www.example.com.\n";
  64. const char* const cname_nxdom_txt =
  65. "cnamenxdom.example.com. 3600 IN CNAME nxdomain.example.com.\n";
  66. // CNAME Leading out of zone
  67. const char* const cname_out_txt =
  68. "cnameout.example.com. 3600 IN CNAME www.example.org.\n";
  69. // The DNAME to do tests against
  70. const char* const dname_txt =
  71. "dname.example.com. 3600 IN DNAME "
  72. "somethinglong.dnametarget.example.com.\n";
  73. // Some data at the dname node (allowed by RFC 2672)
  74. const char* const dname_a_txt =
  75. "dname.example.com. 3600 IN A 192.0.2.5\n";
  76. // This is not inside the zone, this is created at runtime
  77. const char* const synthetized_cname_txt =
  78. "www.dname.example.com. 3600 IN CNAME "
  79. "www.somethinglong.dnametarget.example.com.\n";
  80. // The rest of data won't be referenced from the test cases.
  81. const char* const other_zone_rrs =
  82. "cnamemailer.example.com. 3600 IN CNAME www.example.com.\n"
  83. "cnamemx.example.com. 3600 IN MX 10 cnamemailer.example.com.\n"
  84. "mx.delegation.example.com. 3600 IN A 192.0.2.100\n";
  85. // Wildcards
  86. const char* const wild_txt = "*.wild.example.com. 3600 IN A 192.0.2.7\n";
  87. const char* const nsec_wild_txt =
  88. "*.wild.example.com. 3600 IN NSEC www.example.com. A NSEC RRSIG\n";
  89. const char* const cnamewild_txt =
  90. "*.cnamewild.example.com. 3600 IN CNAME www.example.org.\n";
  91. const char* const nsec_cnamewild_txt = "*.cnamewild.example.com. "
  92. "3600 IN NSEC delegation.example.com. CNAME NSEC RRSIG\n";
  93. // Used in NXDOMAIN proof test. We are going to test some unusual case where
  94. // the best possible wildcard is below the "next domain" of the NSEC RR that
  95. // proves the NXDOMAIN, i.e.,
  96. // mx.example.com. (exist)
  97. // (.no.example.com. (qname, NXDOMAIN)
  98. // ).no.example.com. (exist)
  99. // *.no.example.com. (best possible wildcard, not exist)
  100. const char* const no_txt =
  101. ").no.example.com. 3600 IN AAAA 2001:db8::53\n";
  102. // NSEC records.
  103. const char* const nsec_apex_txt =
  104. "example.com. 3600 IN NSEC cname.example.com. NS SOA NSEC RRSIG\n";
  105. const char* const nsec_mx_txt =
  106. "mx.example.com. 3600 IN NSEC ).no.example.com. MX NSEC RRSIG\n";
  107. const char* const nsec_no_txt =
  108. ").no.example.com. 3600 IN NSEC nz.no.example.com. AAAA NSEC RRSIG\n";
  109. // We'll also test the case where a single NSEC proves both NXDOMAIN and the
  110. // non existence of wildcard. The following records will be used for that
  111. // test.
  112. // ).no.example.com. (exist, whose NSEC proves everything)
  113. // *.no.example.com. (best possible wildcard, not exist)
  114. // nx.no.example.com. (NXDOMAIN)
  115. // nz.no.example.com. (exist)
  116. const char* const nz_txt =
  117. "nz.no.example.com. 3600 IN AAAA 2001:db8::5300\n";
  118. const char* const nsec_nz_txt =
  119. "nz.no.example.com. 3600 IN NSEC noglue.example.com. AAAA NSEC RRSIG\n";
  120. const char* const nsec_nxdomain_txt =
  121. "noglue.example.com. 3600 IN NSEC nonsec.example.com. A\n";
  122. // NSEC for the normal NXRRSET case
  123. const char* const nsec_www_txt =
  124. "www.example.com. 3600 IN NSEC example.com. A NSEC RRSIG\n";
  125. // Authoritative data without NSEC
  126. const char* const nonsec_a_txt = "nonsec.example.com. 3600 IN A 192.0.2.0\n";
  127. // A helper function that generates a textual representation of RRSIG RDATA
  128. // for the given covered type. The resulting RRSIG may not necessarily make
  129. // sense in terms of the DNSSEC protocol, but for our testing purposes it's
  130. // okay.
  131. string
  132. getCommonRRSIGText(const string& type) {
  133. return (type +
  134. string(" 5 3 3600 20000101000000 20000201000000 12345 "
  135. "example.com. FAKEFAKEFAKE"));
  136. }
  137. // This is a mock Zone Finder class for testing.
  138. // It is a derived class of ZoneFinder for the convenient of tests.
  139. // Its find() method emulates the common behavior of protocol compliant
  140. // ZoneFinder classes, but simplifies some minor cases and also supports broken
  141. // behavior.
  142. // For simplicity, most names are assumed to be "in zone"; there's only
  143. // one zone cut at the point of name "delegation.example.com".
  144. // Another special name is "dname.example.com". Query names under this name
  145. // will result in DNAME.
  146. // This mock zone doesn't handle empty non terminal nodes (if we need to test
  147. // such cases find() should have specialized code for it).
  148. class MockZoneFinder : public ZoneFinder {
  149. public:
  150. MockZoneFinder() :
  151. origin_(Name("example.com")),
  152. delegation_name_("delegation.example.com"),
  153. dname_name_("dname.example.com"),
  154. has_SOA_(true),
  155. has_apex_NS_(true),
  156. rrclass_(RRClass::IN()),
  157. include_rrsig_anyway_(false),
  158. nsec_name_(origin_)
  159. {
  160. stringstream zone_stream;
  161. zone_stream << soa_txt << zone_ns_txt << ns_addrs_txt <<
  162. delegation_txt << mx_txt << www_a_txt << cname_txt <<
  163. cname_nxdom_txt << cname_out_txt << dname_txt << dname_a_txt <<
  164. other_zone_rrs << no_txt << nz_txt <<
  165. nsec_apex_txt << nsec_mx_txt << nsec_no_txt << nsec_nz_txt <<
  166. nsec_nxdomain_txt << nsec_www_txt << nonsec_a_txt <<
  167. wild_txt << nsec_wild_txt << cnamewild_txt << nsec_cnamewild_txt;
  168. masterLoad(zone_stream, origin_, rrclass_,
  169. boost::bind(&MockZoneFinder::loadRRset, this, _1));
  170. empty_nsec_rrset_ = ConstRRsetPtr(new RRset(Name::ROOT_NAME(),
  171. RRClass::IN(),
  172. RRType::NSEC(),
  173. RRTTL(3600)));
  174. }
  175. virtual isc::dns::Name getOrigin() const { return (origin_); }
  176. virtual isc::dns::RRClass getClass() const { return (rrclass_); }
  177. virtual FindResult find(const isc::dns::Name& name,
  178. const isc::dns::RRType& type,
  179. RRsetList* target = NULL,
  180. const FindOptions options = FIND_DEFAULT);
  181. // If false is passed, it makes the zone broken as if it didn't have the
  182. // SOA.
  183. void setSOAFlag(bool on) { has_SOA_ = on; }
  184. // If false is passed, it makes the zone broken as if it didn't have
  185. // the apex NS.
  186. void setApexNSFlag(bool on) { has_apex_NS_ = on; }
  187. // Turn this on if you want it to return RRSIGs regardless of FIND_GLUE_OK
  188. void setIncludeRRSIGAnyway(bool on) { include_rrsig_anyway_ = on; }
  189. // Once called, this "faked" result will be returned when NSEC is expected
  190. // for the specified query name.
  191. void setNSECResult(const Name& nsec_name, Result code,
  192. ConstRRsetPtr rrset)
  193. {
  194. nsec_name_ = nsec_name;
  195. nsec_result_.reset(new ZoneFinder::FindResult(code, rrset));
  196. }
  197. Name findPreviousName(const Name&) const {
  198. isc_throw(isc::NotImplemented, "Mock doesn't support previous name");
  199. }
  200. public:
  201. // We allow the tests to use these for convenience
  202. ConstRRsetPtr delegation_rrset_;
  203. ConstRRsetPtr empty_nsec_rrset_;
  204. private:
  205. typedef map<RRType, ConstRRsetPtr> RRsetStore;
  206. typedef map<Name, RRsetStore> Domains;
  207. Domains domains_;
  208. void loadRRset(RRsetPtr rrset) {
  209. domains_[rrset->getName()][rrset->getType()] = rrset;
  210. if (rrset->getName() == delegation_name_ &&
  211. rrset->getType() == RRType::NS()) {
  212. delegation_rrset_ = rrset;
  213. } else if (rrset->getName() == dname_name_ &&
  214. rrset->getType() == RRType::DNAME()) {
  215. dname_rrset_ = rrset;
  216. // Add some signatures
  217. } else if (rrset->getName() == Name("example.com.") &&
  218. rrset->getType() == RRType::NS()) {
  219. // For NS, we only have RRSIG for the origin name.
  220. rrset->addRRsig(RdataPtr(new generic::RRSIG(
  221. getCommonRRSIGText("NS"))));
  222. } else {
  223. // For others generate RRSIG unconditionally. Technically this
  224. // is wrong because we shouldn't have it for names under a zone
  225. // cut. But in our tests that doesn't matter, so we add them
  226. // just for simplicity.
  227. rrset->addRRsig(RdataPtr(new generic::RRSIG(
  228. getCommonRRSIGText(rrset->getType().
  229. toText()))));
  230. }
  231. }
  232. const Name origin_;
  233. // Names where we delegate somewhere else
  234. const Name delegation_name_;
  235. const Name dname_name_;
  236. bool has_SOA_;
  237. bool has_apex_NS_;
  238. ConstRRsetPtr dname_rrset_;
  239. const RRClass rrclass_;
  240. bool include_rrsig_anyway_;
  241. // The following two will be used for faked NSEC cases
  242. Name nsec_name_;
  243. boost::scoped_ptr<ZoneFinder::FindResult> nsec_result_;
  244. };
  245. // A helper function that generates a new RRset based on "wild_rrset",
  246. // replacing its owner name with 'real_name'.
  247. ConstRRsetPtr
  248. substituteWild(const RRset& wild_rrset, const Name& real_name) {
  249. RRsetPtr rrset(new RRset(real_name, wild_rrset.getClass(),
  250. wild_rrset.getType(), wild_rrset.getTTL()));
  251. // For simplicity we only consider the case with one RDATA (for now)
  252. rrset->addRdata(wild_rrset.getRdataIterator()->getCurrent());
  253. ConstRRsetPtr wild_sig = wild_rrset.getRRsig();
  254. if (wild_sig) {
  255. RRsetPtr sig(new RRset(real_name, wild_sig->getClass(),
  256. wild_sig->getType(), wild_sig->getTTL()));
  257. sig->addRdata(wild_sig->getRdataIterator()->getCurrent());
  258. rrset->addRRsig(sig);
  259. }
  260. return (rrset);
  261. }
  262. ZoneFinder::FindResult
  263. MockZoneFinder::find(const Name& name, const RRType& type,
  264. RRsetList* target, const FindOptions options)
  265. {
  266. // Emulating a broken zone: mandatory apex RRs are missing if specifically
  267. // configured so (which are rare cases).
  268. if (name == origin_ && type == RRType::SOA() && !has_SOA_) {
  269. return (FindResult(NXDOMAIN, RRsetPtr()));
  270. } else if (name == origin_ && type == RRType::NS() && !has_apex_NS_) {
  271. return (FindResult(NXDOMAIN, RRsetPtr()));
  272. }
  273. // Special case for names on or under a zone cut
  274. if ((options & FIND_GLUE_OK) == 0 &&
  275. (name == delegation_name_ ||
  276. name.compare(delegation_name_).getRelation() ==
  277. NameComparisonResult::SUBDOMAIN)) {
  278. return (FindResult(DELEGATION, delegation_rrset_));
  279. // And under DNAME
  280. } else if (name.compare(dname_name_).getRelation() ==
  281. NameComparisonResult::SUBDOMAIN) {
  282. return (FindResult(DNAME, dname_rrset_));
  283. }
  284. // normal cases. names are searched for only per exact-match basis
  285. // for simplicity.
  286. const Domains::const_iterator found_domain = domains_.find(name);
  287. if (found_domain != domains_.end()) {
  288. // First, try exact match.
  289. RRsetStore::const_iterator found_rrset =
  290. found_domain->second.find(type);
  291. if (found_rrset != found_domain->second.end()) {
  292. ConstRRsetPtr rrset;
  293. // Strip whatever signature there is in case DNSSEC is not required
  294. // Just to make sure the Query asks for it when it is needed
  295. if (options & ZoneFinder::FIND_DNSSEC ||
  296. include_rrsig_anyway_ ||
  297. !found_rrset->second->getRRsig()) {
  298. rrset = found_rrset->second;
  299. } else {
  300. RRsetPtr noconst(new RRset(found_rrset->second->getName(),
  301. found_rrset->second->getClass(),
  302. found_rrset->second->getType(),
  303. found_rrset->second->getTTL()));
  304. for (RdataIteratorPtr
  305. i(found_rrset->second->getRdataIterator());
  306. !i->isLast(); i->next()) {
  307. noconst->addRdata(i->getCurrent());
  308. }
  309. rrset = noconst;
  310. }
  311. return (FindResult(SUCCESS, rrset));
  312. }
  313. // If not found but we have a target, fill it with all RRsets here
  314. if (!found_domain->second.empty() && target != NULL) {
  315. for (found_rrset = found_domain->second.begin();
  316. found_rrset != found_domain->second.end(); ++found_rrset) {
  317. // Insert RRs under the domain name into target
  318. target->addRRset(
  319. boost::const_pointer_cast<RRset>(found_rrset->second));
  320. }
  321. return (FindResult(SUCCESS, found_domain->second.begin()->second));
  322. }
  323. // Otherwise, if this domain name has CNAME, return it.
  324. found_rrset = found_domain->second.find(RRType::CNAME());
  325. if (found_rrset != found_domain->second.end()) {
  326. return (FindResult(CNAME, found_rrset->second));
  327. }
  328. // Otherwise it's NXRRSET case.
  329. if ((options & FIND_DNSSEC) != 0) {
  330. found_rrset = found_domain->second.find(RRType::NSEC());
  331. if (found_rrset != found_domain->second.end()) {
  332. return (FindResult(NXRRSET, found_rrset->second));
  333. }
  334. }
  335. return (FindResult(NXRRSET, RRsetPtr()));
  336. }
  337. // query name isn't found in our domains.
  338. // We first check if the query name is an empty non terminal name
  339. // of the zone by naive linear search.
  340. Domains::const_iterator domain;
  341. for (domain = domains_.begin(); domain != domains_.end(); ++domain) {
  342. if (name.compare((*domain).first).getRelation() ==
  343. NameComparisonResult::SUPERDOMAIN) {
  344. break;
  345. }
  346. }
  347. if (domain != domains_.end()) {
  348. // The query name is in an empty non terminal node followed by 'domain'
  349. // (for simplicity we ignore the pathological case of 'domain' is
  350. // the origin of the zone)
  351. --domain; // reset domain to the "previous name"
  352. if ((options & FIND_DNSSEC) != 0) {
  353. RRsetStore::const_iterator found_rrset =
  354. (*domain).second.find(RRType::NSEC());
  355. if (found_rrset != (*domain).second.end()) {
  356. return (FindResult(NXRRSET, found_rrset->second));
  357. }
  358. }
  359. return (FindResult(NXRRSET, RRsetPtr()));
  360. }
  361. // Another possibility is wildcard. For simplicity we only check
  362. // hardcoded specific cases, ignoring other details such as canceling
  363. // due to the existence of closer name.
  364. if ((options & NO_WILDCARD) == 0) {
  365. const Name wild_suffix("wild.example.com");
  366. if (name.compare(wild_suffix).getRelation() ==
  367. NameComparisonResult::SUBDOMAIN) {
  368. domain = domains_.find(Name("*").concatenate(wild_suffix));
  369. assert(domain != domains_.end());
  370. RRsetStore::const_iterator found_rrset = domain->second.find(type);
  371. assert(found_rrset != domain->second.end());
  372. return (FindResult(WILDCARD,
  373. substituteWild(*found_rrset->second, name)));
  374. }
  375. const Name cnamewild_suffix("cnamewild.example.com");
  376. if (name.compare(cnamewild_suffix).getRelation() ==
  377. NameComparisonResult::SUBDOMAIN) {
  378. domain = domains_.find(Name("*").concatenate(cnamewild_suffix));
  379. assert(domain != domains_.end());
  380. RRsetStore::const_iterator found_rrset =
  381. domain->second.find(RRType::CNAME());
  382. assert(found_rrset != domain->second.end());
  383. return (FindResult(WILDCARD_CNAME,
  384. substituteWild(*found_rrset->second, name)));
  385. }
  386. }
  387. // This is an NXDOMAIN case.
  388. // If we need DNSSEC proof, find the "previous name" that has an NSEC RR
  389. // and return NXDOMAIN with the found NSEC. Otherwise, just return the
  390. // NXDOMAIN code and NULL. If DNSSEC proof is requested but no NSEC is
  391. // found, we return NULL, too. (For simplicity under the test conditions
  392. // we don't care about pathological cases such as the name is "smaller"
  393. // than the origin)
  394. if ((options & FIND_DNSSEC) != 0) {
  395. // Emulate a broken DataSourceClient for some special names.
  396. if (nsec_result_ && nsec_name_ == name) {
  397. return (*nsec_result_);
  398. }
  399. // Normal case
  400. // XXX: some older g++ complains about operator!= if we use
  401. // const_reverse_iterator
  402. for (Domains::reverse_iterator it = domains_.rbegin();
  403. it != domains_.rend();
  404. ++it) {
  405. RRsetStore::const_iterator nsec_it;
  406. if ((*it).first < name &&
  407. (nsec_it = (*it).second.find(RRType::NSEC()))
  408. != (*it).second.end()) {
  409. return (FindResult(NXDOMAIN, (*nsec_it).second));
  410. }
  411. }
  412. }
  413. return (FindResult(NXDOMAIN, RRsetPtr()));
  414. }
  415. class QueryTest : public ::testing::Test {
  416. protected:
  417. QueryTest() :
  418. qname(Name("www.example.com")), qclass(RRClass::IN()),
  419. qtype(RRType::A()), response(Message::RENDER),
  420. qid(response.getQid()), query_code(Opcode::QUERY().getCode())
  421. {
  422. response.setRcode(Rcode::NOERROR());
  423. response.setOpcode(Opcode::QUERY());
  424. // create and add a matching zone.
  425. mock_finder = new MockZoneFinder();
  426. memory_client.addZone(ZoneFinderPtr(mock_finder));
  427. }
  428. MockZoneFinder* mock_finder;
  429. // We use InMemoryClient here. We could have some kind of mock client
  430. // here, but historically, the Query supported only InMemoryClient
  431. // (originally named MemoryDataSrc) and was tested with it, so we keep
  432. // it like this for now.
  433. InMemoryClient memory_client;
  434. const Name qname;
  435. const RRClass qclass;
  436. const RRType qtype;
  437. Message response;
  438. const qid_t qid;
  439. const uint16_t query_code;
  440. };
  441. // A wrapper to check resulting response message commonly used in
  442. // tests below.
  443. // check_origin needs to be specified only when the authority section has
  444. // an SOA RR. The interface is not generic enough but should be okay
  445. // for our test cases in practice.
  446. void
  447. responseCheck(Message& response, const isc::dns::Rcode& rcode,
  448. unsigned int flags, const unsigned int ancount,
  449. const unsigned int nscount, const unsigned int arcount,
  450. const char* const expected_answer,
  451. const char* const expected_authority,
  452. const char* const expected_additional,
  453. const Name& check_origin = Name::ROOT_NAME())
  454. {
  455. // In our test cases QID, Opcode, and QDCOUNT should be constant, so
  456. // we don't bother the test cases specifying these values.
  457. headerCheck(response, response.getQid(), rcode, Opcode::QUERY().getCode(),
  458. flags, 0, ancount, nscount, arcount);
  459. if (expected_answer != NULL) {
  460. rrsetsCheck(expected_answer,
  461. response.beginSection(Message::SECTION_ANSWER),
  462. response.endSection(Message::SECTION_ANSWER),
  463. check_origin);
  464. }
  465. if (expected_authority != NULL) {
  466. rrsetsCheck(expected_authority,
  467. response.beginSection(Message::SECTION_AUTHORITY),
  468. response.endSection(Message::SECTION_AUTHORITY),
  469. check_origin);
  470. }
  471. if (expected_additional != NULL) {
  472. rrsetsCheck(expected_additional,
  473. response.beginSection(Message::SECTION_ADDITIONAL),
  474. response.endSection(Message::SECTION_ADDITIONAL));
  475. }
  476. }
  477. TEST_F(QueryTest, noZone) {
  478. // There's no zone in the memory datasource. So the response should have
  479. // REFUSED.
  480. InMemoryClient empty_memory_client;
  481. Query nozone_query(empty_memory_client, qname, qtype, response);
  482. EXPECT_NO_THROW(nozone_query.process());
  483. EXPECT_EQ(Rcode::REFUSED(), response.getRcode());
  484. }
  485. TEST_F(QueryTest, exactMatch) {
  486. Query query(memory_client, qname, qtype, response);
  487. EXPECT_NO_THROW(query.process());
  488. // find match rrset
  489. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 3, 3,
  490. www_a_txt, zone_ns_txt, ns_addrs_txt);
  491. }
  492. TEST_F(QueryTest, exactMatchIgnoreSIG) {
  493. // Check that we do not include the RRSIG when not requested even when
  494. // we receive it from the data source.
  495. mock_finder->setIncludeRRSIGAnyway(true);
  496. Query query(memory_client, qname, qtype, response);
  497. EXPECT_NO_THROW(query.process());
  498. // find match rrset
  499. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 3, 3,
  500. www_a_txt, zone_ns_txt, ns_addrs_txt);
  501. }
  502. TEST_F(QueryTest, dnssecPositive) {
  503. // Just like exactMatch, but the signatures should be included as well
  504. Query query(memory_client, qname, qtype, response, true);
  505. EXPECT_NO_THROW(query.process());
  506. // find match rrset
  507. // We can't let responseCheck to check the additional section as well,
  508. // it gets confused by the two RRs for glue.delegation.../RRSIG due
  509. // to it's design and fixing it would be hard. Therefore we simply
  510. // check manually this one time.
  511. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 2, 4, 6,
  512. (www_a_txt + std::string("www.example.com. 3600 IN RRSIG "
  513. "A 5 3 3600 20000101000000 "
  514. "20000201000000 12345 example.com. "
  515. "FAKEFAKEFAKE\n")).c_str(),
  516. (zone_ns_txt + std::string("example.com. 3600 IN RRSIG NS 5 "
  517. "3 3600 20000101000000 "
  518. "20000201000000 12345 "
  519. "example.com. FAKEFAKEFAKE\n")).
  520. c_str(), NULL);
  521. RRsetIterator iterator(response.beginSection(Message::SECTION_ADDITIONAL));
  522. const char* additional[] = {
  523. "glue.delegation.example.com. 3600 IN A 192.0.2.153\n",
  524. "glue.delegation.example.com. 3600 IN RRSIG A 5 3 3600 20000101000000 "
  525. "20000201000000 12345 example.com. FAKEFAKEFAKE\n",
  526. "glue.delegation.example.com. 3600 IN AAAA 2001:db8::53\n",
  527. "glue.delegation.example.com. 3600 IN RRSIG AAAA 5 3 3600 "
  528. "20000101000000 20000201000000 12345 example.com. FAKEFAKEFAKE\n",
  529. "noglue.example.com. 3600 IN A 192.0.2.53\n",
  530. "noglue.example.com. 3600 IN RRSIG A 5 3 3600 20000101000000 "
  531. "20000201000000 12345 example.com. FAKEFAKEFAKE\n",
  532. NULL
  533. };
  534. for (const char** rr(additional); *rr != NULL; ++ rr) {
  535. ASSERT_FALSE(iterator ==
  536. response.endSection(Message::SECTION_ADDITIONAL));
  537. EXPECT_EQ(*rr, (*iterator)->toText());
  538. iterator ++;
  539. }
  540. EXPECT_TRUE(iterator == response.endSection(Message::SECTION_ADDITIONAL));
  541. }
  542. TEST_F(QueryTest, exactAddrMatch) {
  543. // find match rrset, omit additional data which has already been provided
  544. // in the answer section from the additional.
  545. EXPECT_NO_THROW(Query(memory_client, Name("noglue.example.com"), qtype,
  546. response).process());
  547. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 3, 2,
  548. "noglue.example.com. 3600 IN A 192.0.2.53\n", zone_ns_txt,
  549. "glue.delegation.example.com. 3600 IN A 192.0.2.153\n"
  550. "glue.delegation.example.com. 3600 IN AAAA 2001:db8::53\n");
  551. }
  552. TEST_F(QueryTest, apexNSMatch) {
  553. // find match rrset, omit authority data which has already been provided
  554. // in the answer section from the authority section.
  555. EXPECT_NO_THROW(Query(memory_client, Name("example.com"), RRType::NS(),
  556. response).process());
  557. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 3, 0, 3,
  558. zone_ns_txt, NULL, ns_addrs_txt);
  559. }
  560. // test type any query logic
  561. TEST_F(QueryTest, exactAnyMatch) {
  562. // find match rrset, omit additional data which has already been provided
  563. // in the answer section from the additional.
  564. EXPECT_NO_THROW(Query(memory_client, Name("noglue.example.com"),
  565. RRType::ANY(), response).process());
  566. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 2, 3, 2,
  567. (string("noglue.example.com. 3600 IN A 192.0.2.53\n") +
  568. string(nsec_nxdomain_txt)).c_str(),
  569. zone_ns_txt,
  570. "glue.delegation.example.com. 3600 IN A 192.0.2.153\n"
  571. "glue.delegation.example.com. 3600 IN AAAA 2001:db8::53\n");
  572. }
  573. TEST_F(QueryTest, apexAnyMatch) {
  574. // find match rrset, omit additional data which has already been provided
  575. // in the answer section from the additional.
  576. EXPECT_NO_THROW(Query(memory_client, Name("example.com"),
  577. RRType::ANY(), response).process());
  578. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 5, 0, 3,
  579. (string(soa_txt) + string(zone_ns_txt) +
  580. string(nsec_apex_txt)).c_str(),
  581. NULL, ns_addrs_txt, mock_finder->getOrigin());
  582. }
  583. TEST_F(QueryTest, mxANYMatch) {
  584. EXPECT_NO_THROW(Query(memory_client, Name("mx.example.com"),
  585. RRType::ANY(), response).process());
  586. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 4, 3, 4,
  587. (string(mx_txt) + string(nsec_mx_txt)).c_str(), zone_ns_txt,
  588. (string(ns_addrs_txt) + string(www_a_txt)).c_str());
  589. }
  590. TEST_F(QueryTest, glueANYMatch) {
  591. EXPECT_NO_THROW(Query(memory_client, Name("delegation.example.com"),
  592. RRType::ANY(), response).process());
  593. responseCheck(response, Rcode::NOERROR(), 0, 0, 4, 3,
  594. NULL, delegation_txt, ns_addrs_txt);
  595. }
  596. TEST_F(QueryTest, nodomainANY) {
  597. EXPECT_NO_THROW(Query(memory_client, Name("nxdomain.example.com"),
  598. RRType::ANY(), response).process());
  599. responseCheck(response, Rcode::NXDOMAIN(), AA_FLAG, 0, 1, 0,
  600. NULL, soa_txt, NULL, mock_finder->getOrigin());
  601. }
  602. // This tests that when we need to look up Zone's apex NS records for
  603. // authoritative answer, and there is no apex NS records. It should
  604. // throw in that case.
  605. TEST_F(QueryTest, noApexNS) {
  606. // Disable apex NS record
  607. mock_finder->setApexNSFlag(false);
  608. EXPECT_THROW(Query(memory_client, Name("noglue.example.com"), qtype,
  609. response).process(), Query::NoApexNS);
  610. // We don't look into the response, as it threw
  611. }
  612. TEST_F(QueryTest, delegation) {
  613. EXPECT_NO_THROW(Query(memory_client, Name("delegation.example.com"),
  614. qtype, response).process());
  615. responseCheck(response, Rcode::NOERROR(), 0, 0, 4, 3,
  616. NULL, delegation_txt, ns_addrs_txt);
  617. }
  618. TEST_F(QueryTest, nxdomain) {
  619. EXPECT_NO_THROW(Query(memory_client, Name("nxdomain.example.com"), qtype,
  620. response).process());
  621. responseCheck(response, Rcode::NXDOMAIN(), AA_FLAG, 0, 1, 0,
  622. NULL, soa_txt, NULL, mock_finder->getOrigin());
  623. }
  624. TEST_F(QueryTest, nxdomainWithNSEC) {
  625. // NXDOMAIN with DNSSEC proof. We should have SOA, NSEC that proves
  626. // NXDOMAIN and NSEC that proves nonexistence of matching wildcard,
  627. // as well as their RRSIGs.
  628. EXPECT_NO_THROW(Query(memory_client, Name("nxdomain.example.com"), qtype,
  629. response, true).process());
  630. responseCheck(response, Rcode::NXDOMAIN(), AA_FLAG, 0, 6, 0,
  631. NULL, (string(soa_txt) +
  632. string("example.com. 3600 IN RRSIG ") +
  633. getCommonRRSIGText("SOA") + "\n" +
  634. string(nsec_nxdomain_txt) + "\n" +
  635. string("noglue.example.com. 3600 IN RRSIG ") +
  636. getCommonRRSIGText("NSEC") + "\n" +
  637. string(nsec_apex_txt) + "\n" +
  638. string("example.com. 3600 IN RRSIG ") +
  639. getCommonRRSIGText("NSEC")).c_str(),
  640. NULL, mock_finder->getOrigin());
  641. }
  642. TEST_F(QueryTest, nxdomainWithNSEC2) {
  643. // See comments about no_txt. In this case the best possible wildcard
  644. // is derived from the next domain of the NSEC that proves NXDOMAIN, and
  645. // the NSEC to provide the non existence of wildcard is different from
  646. // the first NSEC.
  647. Query(memory_client, Name("(.no.example.com"), qtype,
  648. response, true).process();
  649. responseCheck(response, Rcode::NXDOMAIN(), AA_FLAG, 0, 6, 0,
  650. NULL, (string(soa_txt) +
  651. string("example.com. 3600 IN RRSIG ") +
  652. getCommonRRSIGText("SOA") + "\n" +
  653. string(nsec_mx_txt) + "\n" +
  654. string("mx.example.com. 3600 IN RRSIG ") +
  655. getCommonRRSIGText("NSEC") + "\n" +
  656. string(nsec_no_txt) + "\n" +
  657. string(").no.example.com. 3600 IN RRSIG ") +
  658. getCommonRRSIGText("NSEC")).c_str(),
  659. NULL, mock_finder->getOrigin());
  660. }
  661. TEST_F(QueryTest, nxdomainWithNSECDuplicate) {
  662. // See comments about nz_txt. In this case we only need one NSEC,
  663. // which proves both NXDOMAIN and the non existence of wildcard.
  664. Query(memory_client, Name("nx.no.example.com"), qtype,
  665. response, true).process();
  666. responseCheck(response, Rcode::NXDOMAIN(), AA_FLAG, 0, 4, 0,
  667. NULL, (string(soa_txt) +
  668. string("example.com. 3600 IN RRSIG ") +
  669. getCommonRRSIGText("SOA") + "\n" +
  670. string(nsec_no_txt) + "\n" +
  671. string(").no.example.com. 3600 IN RRSIG ") +
  672. getCommonRRSIGText("NSEC")).c_str(),
  673. NULL, mock_finder->getOrigin());
  674. }
  675. TEST_F(QueryTest, nxdomainBadNSEC1) {
  676. // ZoneFinder::find() returns NXDOMAIN with non NSEC RR.
  677. mock_finder->setNSECResult(Name("badnsec.example.com"),
  678. ZoneFinder::NXDOMAIN,
  679. mock_finder->delegation_rrset_);
  680. EXPECT_THROW(Query(memory_client, Name("badnsec.example.com"), qtype,
  681. response, true).process(),
  682. std::bad_cast);
  683. }
  684. TEST_F(QueryTest, nxdomainBadNSEC2) {
  685. // ZoneFinder::find() returns NXDOMAIN with an empty NSEC RR.
  686. mock_finder->setNSECResult(Name("emptynsec.example.com"),
  687. ZoneFinder::NXDOMAIN,
  688. mock_finder->empty_nsec_rrset_);
  689. EXPECT_THROW(Query(memory_client, Name("emptynsec.example.com"), qtype,
  690. response, true).process(),
  691. Query::BadNSEC);
  692. }
  693. TEST_F(QueryTest, nxdomainBadNSEC3) {
  694. // "no-wildcard proof" returns SUCCESS. it should be NXDOMAIN.
  695. mock_finder->setNSECResult(Name("*.example.com"),
  696. ZoneFinder::SUCCESS,
  697. mock_finder->delegation_rrset_);
  698. EXPECT_THROW(Query(memory_client, Name("nxdomain.example.com"), qtype,
  699. response, true).process(),
  700. Query::BadNSEC);
  701. }
  702. TEST_F(QueryTest, nxdomainBadNSEC4) {
  703. // "no-wildcard proof" doesn't return RRset.
  704. mock_finder->setNSECResult(Name("*.example.com"),
  705. ZoneFinder::NXDOMAIN, ConstRRsetPtr());
  706. EXPECT_THROW(Query(memory_client, Name("nxdomain.example.com"), qtype,
  707. response, true).process(),
  708. Query::BadNSEC);
  709. }
  710. TEST_F(QueryTest, nxdomainBadNSEC5) {
  711. // "no-wildcard proof" returns non NSEC.
  712. mock_finder->setNSECResult(Name("*.example.com"),
  713. ZoneFinder::NXDOMAIN,
  714. mock_finder->delegation_rrset_);
  715. // This is a bit odd, but we'll simply include the returned RRset.
  716. Query(memory_client, Name("nxdomain.example.com"), qtype,
  717. response, true).process();
  718. responseCheck(response, Rcode::NXDOMAIN(), AA_FLAG, 0, 8, 0,
  719. NULL, (string(soa_txt) +
  720. string("example.com. 3600 IN RRSIG ") +
  721. getCommonRRSIGText("SOA") + "\n" +
  722. string(nsec_nxdomain_txt) + "\n" +
  723. string("noglue.example.com. 3600 IN RRSIG ") +
  724. getCommonRRSIGText("NSEC") + "\n" +
  725. delegation_txt).c_str(),
  726. NULL, mock_finder->getOrigin());
  727. }
  728. TEST_F(QueryTest, nxdomainBadNSEC6) {
  729. // "no-wildcard proof" returns empty NSEC.
  730. mock_finder->setNSECResult(Name("*.example.com"),
  731. ZoneFinder::NXDOMAIN,
  732. mock_finder->empty_nsec_rrset_);
  733. EXPECT_THROW(Query(memory_client, Name("nxdomain.example.com"), qtype,
  734. response, true).process(),
  735. Query::BadNSEC);
  736. }
  737. TEST_F(QueryTest, nxrrset) {
  738. EXPECT_NO_THROW(Query(memory_client, Name("www.example.com"),
  739. RRType::TXT(), response).process());
  740. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 0, 1, 0,
  741. NULL, soa_txt, NULL, mock_finder->getOrigin());
  742. }
  743. TEST_F(QueryTest, nxrrsetWithNSEC) {
  744. // NXRRSET with DNSSEC proof. We should have SOA, NSEC that proves the
  745. // NXRRSET and their RRSIGs.
  746. Query(memory_client, Name("www.example.com"), RRType::TXT(), response,
  747. true).process();
  748. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 0, 4, 0, NULL,
  749. (string(soa_txt) + string("example.com. 3600 IN RRSIG ") +
  750. getCommonRRSIGText("SOA") + "\n" +
  751. string(nsec_www_txt) + "\n" +
  752. string("www.example.com. 3600 IN RRSIG ") +
  753. getCommonRRSIGText("NSEC")).c_str(),
  754. NULL, mock_finder->getOrigin());
  755. }
  756. TEST_F(QueryTest, emptyNameWithNSEC) {
  757. // Empty non terminal with DNSSEC proof. This is one of the cases of
  758. // Section 3.1.3.2 of RFC4035.
  759. // mx.example.com. NSEC ).no.example.com. proves no.example.com. is a
  760. // non empty terminal node. Note that it also implicitly proves there
  761. // should be no closer wildcard match (because the empty name is an
  762. // exact match), so we only need one NSEC.
  763. // From the point of the Query::process(), this is actually no different
  764. // from the other NXRRSET case, but we check that explicitly just in case.
  765. Query(memory_client, Name("no.example.com"), RRType::A(), response,
  766. true).process();
  767. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 0, 4, 0, NULL,
  768. (string(soa_txt) + string("example.com. 3600 IN RRSIG ") +
  769. getCommonRRSIGText("SOA") + "\n" +
  770. string(nsec_mx_txt) + "\n" +
  771. string("mx.example.com. 3600 IN RRSIG ") +
  772. getCommonRRSIGText("NSEC")).c_str(),
  773. NULL, mock_finder->getOrigin());
  774. }
  775. TEST_F(QueryTest, nxrrsetWithoutNSEC) {
  776. // NXRRSET with DNSSEC proof requested, but there's no NSEC at that node.
  777. // This is an unexpected event (if the zone is supposed to be properly
  778. // signed with NSECs), but we accept and ignore the oddity.
  779. Query(memory_client, Name("nonsec.example.com"), RRType::TXT(), response,
  780. true).process();
  781. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 0, 2, 0, NULL,
  782. (string(soa_txt) + string("example.com. 3600 IN RRSIG ") +
  783. getCommonRRSIGText("SOA") + "\n").c_str(),
  784. NULL, mock_finder->getOrigin());
  785. }
  786. TEST_F(QueryTest, wildcardNSEC) {
  787. // The qname matches *.wild.example.com. The response should contain
  788. // an NSEC that proves the non existence of a closer name.
  789. Query(memory_client, Name("www.wild.example.com"), RRType::A(), response,
  790. true).process();
  791. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 2, 6, 6,
  792. (string(wild_txt).replace(0, 1, "www") +
  793. string("www.wild.example.com. 3600 IN RRSIG ") +
  794. getCommonRRSIGText("A") + "\n").c_str(),
  795. (zone_ns_txt + string("example.com. 3600 IN RRSIG NS 5 "
  796. "3 3600 20000101000000 "
  797. "20000201000000 12345 "
  798. "example.com. FAKEFAKEFAKE\n") +
  799. string(nsec_wild_txt) +
  800. string("*.wild.example.com. 3600 IN RRSIG ") +
  801. getCommonRRSIGText("NSEC") + "\n").c_str(),
  802. NULL, // we are not interested in additionals in this test
  803. mock_finder->getOrigin());
  804. }
  805. TEST_F(QueryTest, CNAMEwildNSEC) {
  806. // Similar to the previous case, but the matching wildcard record is
  807. // CNAME.
  808. Query(memory_client, Name("www.cnamewild.example.com"), RRType::A(),
  809. response, true).process();
  810. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 2, 2, 0,
  811. (string(cnamewild_txt).replace(0, 1, "www") +
  812. string("www.cnamewild.example.com. 3600 IN RRSIG ") +
  813. getCommonRRSIGText("CNAME") + "\n").c_str(),
  814. (string(nsec_cnamewild_txt) +
  815. string("*.cnamewild.example.com. 3600 IN RRSIG ") +
  816. getCommonRRSIGText("NSEC") + "\n").c_str(),
  817. NULL, // we are not interested in additionals in this test
  818. mock_finder->getOrigin());
  819. }
  820. TEST_F(QueryTest, badWildcardProof1) {
  821. // Unexpected case in wildcard proof: ZoneFinder::find() returns SUCCESS
  822. // when NXDOMAIN is expected.
  823. mock_finder->setNSECResult(Name("www.wild.example.com"),
  824. ZoneFinder::SUCCESS,
  825. mock_finder->delegation_rrset_);
  826. EXPECT_THROW(Query(memory_client, Name("www.wild.example.com"),
  827. RRType::A(), response, true).process(),
  828. Query::BadNSEC);
  829. }
  830. TEST_F(QueryTest, badWildcardProof2) {
  831. // "wildcard proof" doesn't return RRset.
  832. mock_finder->setNSECResult(Name("www.wild.example.com"),
  833. ZoneFinder::NXDOMAIN, ConstRRsetPtr());
  834. EXPECT_THROW(Query(memory_client, Name("www.wild.example.com"),
  835. RRType::A(), response, true).process(),
  836. Query::BadNSEC);
  837. }
  838. TEST_F(QueryTest, badWildcardProof3) {
  839. // "wildcard proof" returns empty NSEC.
  840. mock_finder->setNSECResult(Name("www.wild.example.com"),
  841. ZoneFinder::NXDOMAIN,
  842. mock_finder->empty_nsec_rrset_);
  843. EXPECT_THROW(Query(memory_client, Name("www.wild.example.com"),
  844. RRType::A(), response, true).process(),
  845. Query::BadNSEC);
  846. }
  847. TEST_F(QueryTest, wildcardNxrrsetWithNSEC1) {
  848. // NXRRSET with DNSSEC proof. We should have SOA, NSEC that proves the
  849. // NXRRSET and their RRSIGs.
  850. Query(memory_client, Name("www.wild.example.com"), RRType::TXT(), response,
  851. true).process();
  852. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 0, 4, 0, NULL,
  853. (string(soa_txt) + string("example.com. 3600 IN RRSIG ") +
  854. getCommonRRSIGText("SOA") + "\n" +
  855. string(nsec_wild_txt) +
  856. string("*.wild.example.com. 3600 IN RRSIG ") +
  857. getCommonRRSIGText("NSEC")+"\n").c_str(),
  858. NULL, mock_finder->getOrigin());
  859. }
  860. /*
  861. * This tests that when there's no SOA and we need a negative answer. It should
  862. * throw in that case.
  863. */
  864. TEST_F(QueryTest, noSOA) {
  865. // disable zone's SOA RR.
  866. mock_finder->setSOAFlag(false);
  867. // The NX Domain
  868. EXPECT_THROW(Query(memory_client, Name("nxdomain.example.com"),
  869. qtype, response).process(), Query::NoSOA);
  870. // Of course, we don't look into the response, as it throwed
  871. // NXRRSET
  872. EXPECT_THROW(Query(memory_client, Name("nxrrset.example.com"),
  873. qtype, response).process(), Query::NoSOA);
  874. }
  875. TEST_F(QueryTest, noMatchZone) {
  876. // there's a zone in the memory datasource but it doesn't match the qname.
  877. // should result in REFUSED.
  878. Query(memory_client, Name("example.org"), qtype, response).process();
  879. EXPECT_EQ(Rcode::REFUSED(), response.getRcode());
  880. }
  881. /*
  882. * Test MX additional processing.
  883. *
  884. * The MX RRset has two RRs, one pointing to a known domain with
  885. * A record, other to unknown out of zone one.
  886. */
  887. TEST_F(QueryTest, MX) {
  888. Query(memory_client, Name("mx.example.com"), RRType::MX(),
  889. response).process();
  890. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 3, 3, 4,
  891. mx_txt, NULL,
  892. (string(ns_addrs_txt) + string(www_a_txt)).c_str());
  893. }
  894. /*
  895. * Test when we ask for MX whose exchange is an alias (CNAME in this case).
  896. *
  897. * This should not trigger the additional processing for the exchange.
  898. */
  899. TEST_F(QueryTest, MXAlias) {
  900. Query(memory_client, Name("cnamemx.example.com"), RRType::MX(),
  901. response).process();
  902. // there shouldn't be no additional RRs for the exchanges (we have 3
  903. // RRs for the NS). The normal MX case is tested separately so we don't
  904. // bother to examine the answer (and authority) sections.
  905. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 3, 3,
  906. NULL, NULL, ns_addrs_txt);
  907. }
  908. /*
  909. * Tests encountering a cname.
  910. *
  911. * There are tests leading to successful answers, NXRRSET, NXDOMAIN and
  912. * out of the zone.
  913. *
  914. * TODO: We currently don't do chaining, so only the CNAME itself should be
  915. * returned.
  916. */
  917. TEST_F(QueryTest, CNAME) {
  918. Query(memory_client, Name("cname.example.com"), RRType::A(),
  919. response).process();
  920. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 0, 0,
  921. cname_txt, NULL, NULL);
  922. }
  923. TEST_F(QueryTest, explicitCNAME) {
  924. // same owner name as the CNAME test but explicitly query for CNAME RR.
  925. // expect the same response as we don't provide a full chain yet.
  926. Query(memory_client, Name("cname.example.com"), RRType::CNAME(),
  927. response).process();
  928. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 3, 3,
  929. cname_txt, zone_ns_txt, ns_addrs_txt);
  930. }
  931. TEST_F(QueryTest, CNAME_NX_RRSET) {
  932. // Leads to www.example.com, it doesn't have TXT
  933. // note: with chaining, what should be expected is not trivial:
  934. // BIND 9 returns the CNAME in answer and SOA in authority, no additional.
  935. // NSD returns the CNAME, NS in authority, A/AAAA for NS in additional.
  936. Query(memory_client, Name("cname.example.com"), RRType::TXT(),
  937. response).process();
  938. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 0, 0,
  939. cname_txt, NULL, NULL);
  940. }
  941. TEST_F(QueryTest, explicitCNAME_NX_RRSET) {
  942. // same owner name as the NXRRSET test but explicitly query for CNAME RR.
  943. Query(memory_client, Name("cname.example.com"), RRType::CNAME(),
  944. response).process();
  945. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 3, 3,
  946. cname_txt, zone_ns_txt, ns_addrs_txt);
  947. }
  948. TEST_F(QueryTest, CNAME_NX_DOMAIN) {
  949. // Leads to nxdomain.example.com
  950. // note: with chaining, what should be expected is not trivial:
  951. // BIND 9 returns the CNAME in answer and SOA in authority, no additional,
  952. // RCODE being NXDOMAIN.
  953. // NSD returns the CNAME, NS in authority, A/AAAA for NS in additional,
  954. // RCODE being NOERROR.
  955. Query(memory_client, Name("cnamenxdom.example.com"), RRType::A(),
  956. response).process();
  957. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 0, 0,
  958. cname_nxdom_txt, NULL, NULL);
  959. }
  960. TEST_F(QueryTest, explicitCNAME_NX_DOMAIN) {
  961. // same owner name as the NXDOMAIN test but explicitly query for CNAME RR.
  962. Query(memory_client, Name("cnamenxdom.example.com"), RRType::CNAME(),
  963. response).process();
  964. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 3, 3,
  965. cname_nxdom_txt, zone_ns_txt, ns_addrs_txt);
  966. }
  967. TEST_F(QueryTest, CNAME_OUT) {
  968. /*
  969. * This leads out of zone. This should have only the CNAME even
  970. * when we do chaining.
  971. *
  972. * TODO: We should be able to have two zones in the mock data source.
  973. * Then the same test should be done with .org included there and
  974. * see what it does (depends on what we want to do)
  975. */
  976. Query(memory_client, Name("cnameout.example.com"), RRType::A(),
  977. response).process();
  978. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 0, 0,
  979. cname_out_txt, NULL, NULL);
  980. }
  981. TEST_F(QueryTest, explicitCNAME_OUT) {
  982. // same owner name as the OUT test but explicitly query for CNAME RR.
  983. Query(memory_client, Name("cnameout.example.com"), RRType::CNAME(),
  984. response).process();
  985. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 3, 3,
  986. cname_out_txt, zone_ns_txt, ns_addrs_txt);
  987. }
  988. /*
  989. * Test a query under a domain with DNAME. We should get a synthetized CNAME
  990. * as well as the DNAME.
  991. *
  992. * TODO: Once we have CNAME chaining, check it works with synthetized CNAMEs
  993. * as well. This includes tests pointing inside the zone, outside the zone,
  994. * pointing to NXRRSET and NXDOMAIN cases (similarly as with CNAME).
  995. */
  996. TEST_F(QueryTest, DNAME) {
  997. Query(memory_client, Name("www.dname.example.com"), RRType::A(),
  998. response).process();
  999. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 2, 0, 0,
  1000. (string(dname_txt) + synthetized_cname_txt).c_str(),
  1001. NULL, NULL);
  1002. }
  1003. /*
  1004. * Ask an ANY query below a DNAME. Should return the DNAME and synthetized
  1005. * CNAME.
  1006. *
  1007. * ANY is handled specially sometimes. We check it is not the case with
  1008. * DNAME.
  1009. */
  1010. TEST_F(QueryTest, DNAME_ANY) {
  1011. Query(memory_client, Name("www.dname.example.com"), RRType::ANY(),
  1012. response).process();
  1013. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 2, 0, 0,
  1014. (string(dname_txt) + synthetized_cname_txt).c_str(), NULL, NULL);
  1015. }
  1016. // Test when we ask for DNAME explicitly, it does no synthetizing.
  1017. TEST_F(QueryTest, explicitDNAME) {
  1018. Query(memory_client, Name("dname.example.com"), RRType::DNAME(),
  1019. response).process();
  1020. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 3, 3,
  1021. dname_txt, zone_ns_txt, ns_addrs_txt);
  1022. }
  1023. /*
  1024. * Request a RRset at the domain with DNAME. It should not synthetize
  1025. * the CNAME, it should return the RRset.
  1026. */
  1027. TEST_F(QueryTest, DNAME_A) {
  1028. Query(memory_client, Name("dname.example.com"), RRType::A(),
  1029. response).process();
  1030. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 1, 3, 3,
  1031. dname_a_txt, zone_ns_txt, ns_addrs_txt);
  1032. }
  1033. /*
  1034. * Request a RRset at the domain with DNAME that is not there (NXRRSET).
  1035. * It should not synthetize the CNAME.
  1036. */
  1037. TEST_F(QueryTest, DNAME_NX_RRSET) {
  1038. EXPECT_NO_THROW(Query(memory_client, Name("dname.example.com"),
  1039. RRType::TXT(), response).process());
  1040. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 0, 1, 0,
  1041. NULL, soa_txt, NULL, mock_finder->getOrigin());
  1042. }
  1043. /*
  1044. * Constructing the CNAME will result in a name that is too long. This,
  1045. * however, should not throw (and crash the server), but respond with
  1046. * YXDOMAIN.
  1047. */
  1048. TEST_F(QueryTest, LongDNAME) {
  1049. // A name that is as long as it can be
  1050. Name longname(
  1051. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  1052. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  1053. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  1054. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  1055. "dname.example.com.");
  1056. EXPECT_NO_THROW(Query(memory_client, longname, RRType::A(),
  1057. response).process());
  1058. responseCheck(response, Rcode::YXDOMAIN(), AA_FLAG, 1, 0, 0,
  1059. dname_txt, NULL, NULL);
  1060. }
  1061. /*
  1062. * Constructing the CNAME will result in a name of maximal length.
  1063. * This tests that we don't reject valid one by some kind of off by
  1064. * one mistake.
  1065. */
  1066. TEST_F(QueryTest, MaxLenDNAME) {
  1067. Name longname(
  1068. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  1069. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  1070. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  1071. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
  1072. "dname.example.com.");
  1073. EXPECT_NO_THROW(Query(memory_client, longname, RRType::A(),
  1074. response).process());
  1075. // Check the answer is OK
  1076. responseCheck(response, Rcode::NOERROR(), AA_FLAG, 2, 0, 0,
  1077. NULL, NULL, NULL);
  1078. // Check that the CNAME has the maximal length.
  1079. bool ok(false);
  1080. for (RRsetIterator i(response.beginSection(Message::SECTION_ANSWER));
  1081. i != response.endSection(Message::SECTION_ANSWER); ++ i) {
  1082. if ((*i)->getType() == RRType::CNAME()) {
  1083. ok = true;
  1084. RdataIteratorPtr ci((*i)->getRdataIterator());
  1085. ASSERT_FALSE(ci->isLast()) << "The CNAME is empty";
  1086. /*
  1087. * Does anybody have a clue why, if the Name::MAX_WIRE is put
  1088. * directly inside ASSERT_EQ, it fails to link and complains
  1089. * it is unresolved external?
  1090. */
  1091. const size_t max_len(Name::MAX_WIRE);
  1092. ASSERT_EQ(max_len, dynamic_cast<const rdata::generic::CNAME&>(
  1093. ci->getCurrent()).getCname().getLength());
  1094. }
  1095. }
  1096. EXPECT_TRUE(ok) << "The synthetized CNAME not found";
  1097. }
  1098. }