datasrc_unittest.cc 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  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 <stdint.h>
  15. #include <iostream>
  16. #include <vector>
  17. #include <string>
  18. #include <gtest/gtest.h>
  19. #include <util/buffer.h>
  20. #include <dns/message.h>
  21. #include <dns/messagerenderer.h>
  22. #include <dns/question.h>
  23. #include <dns/opcode.h>
  24. #include <dns/rcode.h>
  25. #include <dns/rdata.h>
  26. #include <dns/rdataclass.h>
  27. #include <dns/rrclass.h>
  28. #include <dns/rrttl.h>
  29. #include <dns/rrtype.h>
  30. #include <cc/data.h>
  31. #include <datasrc/query.h>
  32. #include <datasrc/sqlite3_datasrc.h>
  33. #include <datasrc/static_datasrc.h>
  34. #include <testutils/dnsmessage_test.h>
  35. #include <dns/tests/unittest_util.h>
  36. #include <datasrc/tests/test_datasrc.h>
  37. using isc::UnitTestUtil;
  38. using namespace std;
  39. using namespace isc::util;
  40. using namespace isc::dns;
  41. using namespace isc::dns::rdata;
  42. using namespace isc::datasrc;
  43. using namespace isc::data;
  44. using namespace isc::testutils;
  45. namespace {
  46. ConstElementPtr SQLITE_DBFILE_EXAMPLE = Element::fromJSON(
  47. "{ \"database_file\": \"" TEST_DATA_DIR "/example.org.sqlite3\"}");
  48. class DataSrcTest : public ::testing::Test {
  49. protected:
  50. DataSrcTest() : obuffer(0), renderer(obuffer), msg(Message::PARSE),
  51. opcodeval(Opcode::QUERY().getCode()), qid(0)
  52. {
  53. DataSrcPtr sql3_source = DataSrcPtr(new Sqlite3DataSrc);
  54. sql3_source->init(SQLITE_DBFILE_EXAMPLE);
  55. DataSrcPtr test_source = DataSrcPtr(new TestDataSrc);
  56. test_source->init();
  57. DataSrcPtr static_source = DataSrcPtr(new StaticDataSrc);
  58. meta_source.addDataSrc(test_source);
  59. meta_source.addDataSrc(sql3_source);
  60. meta_source.addDataSrc(static_source);
  61. }
  62. void QueryCommon(const RRClass& qclass);
  63. void createAndProcessQuery(const Name& qname, const RRClass& qclass,
  64. const RRType& qtype, bool need_dnssec);
  65. HotCache cache;
  66. MetaDataSrc meta_source;
  67. OutputBuffer obuffer;
  68. MessageRenderer renderer;
  69. Message msg;
  70. const uint16_t opcodeval;
  71. qid_t qid;
  72. };
  73. void
  74. performQuery(DataSrc& data_source, HotCache& cache, Message& message,
  75. bool need_dnssec = true)
  76. {
  77. message.setHeaderFlag(Message::HEADERFLAG_AA);
  78. message.setRcode(Rcode::NOERROR());
  79. Query q(message, cache, need_dnssec);
  80. data_source.doQuery(q);
  81. }
  82. void
  83. DataSrcTest::createAndProcessQuery(const Name& qname, const RRClass& qclass,
  84. const RRType& qtype,
  85. bool need_dnssec = true)
  86. {
  87. msg.makeResponse();
  88. msg.setOpcode(Opcode::QUERY());
  89. msg.addQuestion(Question(qname, qclass, qtype));
  90. msg.setHeaderFlag(Message::HEADERFLAG_RD);
  91. qid = msg.getQid();
  92. performQuery(meta_source, cache, msg, need_dnssec);
  93. }
  94. void
  95. DataSrcTest::QueryCommon(const RRClass& qclass) {
  96. createAndProcessQuery(Name("www.example.com"), qclass, RRType::A());
  97. headerCheck(msg, qid, Rcode::NOERROR(), opcodeval,
  98. QR_FLAG | AA_FLAG | RD_FLAG, 1, 2, 4, 6);
  99. RRsetIterator rit = msg.beginSection(Message::SECTION_ANSWER);
  100. RRsetPtr rrset = *rit;
  101. EXPECT_EQ(Name("www.example.com"), rrset->getName());
  102. EXPECT_EQ(RRType::A(), rrset->getType());
  103. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  104. RdataIteratorPtr it = rrset->getRdataIterator();
  105. EXPECT_EQ("192.0.2.1", it->getCurrent().toText());
  106. it->next();
  107. EXPECT_TRUE(it->isLast());
  108. // XXX: also check ANSWER RRSIG
  109. rit = msg.beginSection(Message::SECTION_AUTHORITY);
  110. rrset = *rit;
  111. EXPECT_EQ(Name("example.com"), rrset->getName());
  112. EXPECT_EQ(RRType::NS(), rrset->getType());
  113. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  114. it = rrset->getRdataIterator();
  115. EXPECT_EQ("dns01.example.com.", it->getCurrent().toText());
  116. it->next();
  117. EXPECT_EQ("dns02.example.com.", it->getCurrent().toText());
  118. it->next();
  119. EXPECT_EQ("dns03.example.com.", it->getCurrent().toText());
  120. it->next();
  121. EXPECT_TRUE(it->isLast());
  122. rit = msg.beginSection(Message::SECTION_ADDITIONAL);
  123. rrset = *rit;
  124. EXPECT_EQ(Name("dns01.example.com"), rrset->getName());
  125. EXPECT_EQ(RRType::A(), rrset->getType());
  126. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  127. it = rrset->getRdataIterator();
  128. EXPECT_EQ("192.0.2.1", it->getCurrent().toText());
  129. it->next();
  130. EXPECT_TRUE(it->isLast());
  131. }
  132. TEST_F(DataSrcTest, Query) {
  133. QueryCommon(RRClass::IN());
  134. }
  135. // Query class doesn't match any of the data source classes. The result
  136. // should be the same as "NxZone".
  137. TEST_F(DataSrcTest, QueryClassMismatch) {
  138. createAndProcessQuery(Name("www.example.com"), RRClass::CH(), RRType::A());
  139. headerCheck(msg, qid, Rcode::REFUSED(), opcodeval, QR_FLAG | RD_FLAG,
  140. 1, 0, 0, 0);
  141. }
  142. // Query class of any should match the first data source.
  143. TEST_F(DataSrcTest, QueryClassAny) {
  144. QueryCommon(RRClass::ANY());
  145. }
  146. TEST_F(DataSrcTest, queryClassAnyNegative) {
  147. // There was a bug where Class ANY query triggered a crash due to NULL
  148. // pointer dereference. This test checks that condition.
  149. // NXDOMAIN case
  150. createAndProcessQuery(Name("notexistent.example.com"), RRClass::ANY(),
  151. RRType::A());
  152. headerCheck(msg, qid, Rcode::NXDOMAIN(), opcodeval,
  153. QR_FLAG | AA_FLAG | RD_FLAG, 1, 0, 6, 0);
  154. // NXRRSET case
  155. msg.clear(Message::PARSE);
  156. createAndProcessQuery(Name("www.example.com"), RRClass::ANY(),
  157. RRType::TXT());
  158. headerCheck(msg, qid, Rcode::NOERROR(), opcodeval,
  159. QR_FLAG | AA_FLAG | RD_FLAG, 1, 0, 4, 0);
  160. }
  161. TEST_F(DataSrcTest, queryClassAnyDNAME) {
  162. // Class ANY query that would match a DNAME. Everything including the
  163. // synthesized CNAME should be the same as the response to class IN query.
  164. createAndProcessQuery(Name("www.dname.example.com"), RRClass::ANY(),
  165. RRType::A(), false);
  166. headerCheck(msg, qid, Rcode::NOERROR(), opcodeval,
  167. QR_FLAG | AA_FLAG | RD_FLAG, 1, 3, 3, 3);
  168. rrsetsCheck("dname.example.com. 3600 IN DNAME sql1.example.com.\n"
  169. "www.dname.example.com. 3600 IN CNAME www.sql1.example.com.\n"
  170. "www.sql1.example.com. 3600 IN A 192.0.2.2\n",
  171. msg.beginSection(Message::SECTION_ANSWER),
  172. msg.endSection(Message::SECTION_ANSWER));
  173. // Also check the case of explicit DNAME query.
  174. msg.clear(Message::PARSE);
  175. createAndProcessQuery(Name("dname.example.com"), RRClass::ANY(),
  176. RRType::DNAME(), false);
  177. headerCheck(msg, qid, Rcode::NOERROR(), opcodeval,
  178. QR_FLAG | AA_FLAG | RD_FLAG, 1, 1, 3, 3);
  179. rrsetsCheck("dname.example.com. 3600 IN DNAME sql1.example.com.\n",
  180. msg.beginSection(Message::SECTION_ANSWER),
  181. msg.endSection(Message::SECTION_ANSWER));
  182. }
  183. TEST_F(DataSrcTest, queryClassAnyCNAME) {
  184. // Similar test for CNAME
  185. createAndProcessQuery(Name("foo.example.com"), RRClass::ANY(),
  186. RRType::A(), false);
  187. headerCheck(msg, qid, Rcode::NOERROR(), opcodeval,
  188. QR_FLAG | AA_FLAG | RD_FLAG, 1, 1, 0, 0);
  189. rrsetsCheck("foo.example.com. 3600 IN CNAME cnametest.example.net.\n",
  190. msg.beginSection(Message::SECTION_ANSWER),
  191. msg.endSection(Message::SECTION_ANSWER));
  192. }
  193. TEST_F(DataSrcTest, NSQuery) {
  194. createAndProcessQuery(Name("example.com"), RRClass::IN(),
  195. RRType::NS());
  196. headerCheck(msg, qid, Rcode::NOERROR(), opcodeval,
  197. QR_FLAG | AA_FLAG | RD_FLAG, 1, 4, 0, 6);
  198. RRsetIterator rit = msg.beginSection(Message::SECTION_ANSWER);
  199. RRsetPtr rrset = *rit;
  200. EXPECT_EQ(Name("example.com"), rrset->getName());
  201. EXPECT_EQ(RRType::NS(), rrset->getType());
  202. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  203. RdataIteratorPtr it = rrset->getRdataIterator();
  204. EXPECT_EQ("dns01.example.com.", it->getCurrent().toText());
  205. it->next();
  206. EXPECT_EQ("dns02.example.com.", it->getCurrent().toText());
  207. it->next();
  208. EXPECT_EQ("dns03.example.com.", it->getCurrent().toText());
  209. it->next();
  210. EXPECT_TRUE(it->isLast());
  211. }
  212. // Make sure two successive queries have the same result
  213. TEST_F(DataSrcTest, DuplicateQuery) {
  214. createAndProcessQuery(Name("example.com"), RRClass::IN(),
  215. RRType::NS());
  216. headerCheck(msg, qid, Rcode::NOERROR(), opcodeval,
  217. QR_FLAG | AA_FLAG | RD_FLAG, 1, 4, 0, 6);
  218. RRsetIterator rit = msg.beginSection(Message::SECTION_ANSWER);
  219. RRsetPtr rrset = *rit;
  220. EXPECT_EQ(Name("example.com"), rrset->getName());
  221. EXPECT_EQ(RRType::NS(), rrset->getType());
  222. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  223. RdataIteratorPtr it = rrset->getRdataIterator();
  224. EXPECT_EQ("dns01.example.com.", it->getCurrent().toText());
  225. it->next();
  226. EXPECT_EQ("dns02.example.com.", it->getCurrent().toText());
  227. it->next();
  228. EXPECT_EQ("dns03.example.com.", it->getCurrent().toText());
  229. it->next();
  230. EXPECT_TRUE(it->isLast());
  231. msg.clear(Message::PARSE);
  232. createAndProcessQuery(Name("example.com"), RRClass::IN(),
  233. RRType::NS());
  234. headerCheck(msg, qid, Rcode::NOERROR(), opcodeval,
  235. QR_FLAG | AA_FLAG | RD_FLAG, 1, 4, 0, 6);
  236. rit = msg.beginSection(Message::SECTION_ANSWER);
  237. rrset = *rit;
  238. EXPECT_EQ(Name("example.com"), rrset->getName());
  239. EXPECT_EQ(RRType::NS(), rrset->getType());
  240. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  241. it = rrset->getRdataIterator();
  242. EXPECT_EQ("dns01.example.com.", it->getCurrent().toText());
  243. it->next();
  244. EXPECT_EQ("dns02.example.com.", it->getCurrent().toText());
  245. it->next();
  246. EXPECT_EQ("dns03.example.com.", it->getCurrent().toText());
  247. it->next();
  248. EXPECT_TRUE(it->isLast());
  249. }
  250. TEST_F(DataSrcTest, DNSKEYQuery) {
  251. createAndProcessQuery(Name("example.com"), RRClass::IN(),
  252. RRType::DNSKEY());
  253. headerCheck(msg, qid, Rcode::NOERROR(), opcodeval,
  254. QR_FLAG | AA_FLAG | RD_FLAG, 1, 4, 4, 6);
  255. RRsetIterator rit = msg.beginSection(Message::SECTION_ANSWER);
  256. RRsetPtr rrset = *rit;
  257. EXPECT_EQ(Name("example.com"), rrset->getName());
  258. EXPECT_EQ(RRType::DNSKEY(), rrset->getType());
  259. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  260. }
  261. // Repeat the previous query to check that cache is working correctly.
  262. // We query for a record at a zone cut to ensure the REFERRAL flag doesn't
  263. // cause incorrect behavior.
  264. TEST_F(DataSrcTest, DNSKEYDuplicateQuery) {
  265. createAndProcessQuery(Name("example.com"), RRClass::IN(),
  266. RRType::DNSKEY());
  267. headerCheck(msg, qid, Rcode::NOERROR(), opcodeval,
  268. QR_FLAG | AA_FLAG | RD_FLAG, 1, 4, 4, 6);
  269. RRsetIterator rit = msg.beginSection(Message::SECTION_ANSWER);
  270. RRsetPtr rrset = *rit;
  271. EXPECT_EQ(Name("example.com"), rrset->getName());
  272. EXPECT_EQ(RRType::DNSKEY(), rrset->getType());
  273. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  274. msg.clear(Message::PARSE);
  275. createAndProcessQuery(Name("example.com"), RRClass::IN(),
  276. RRType::DNSKEY());
  277. rit = msg.beginSection(Message::SECTION_ANSWER);
  278. rrset = *rit;
  279. EXPECT_EQ(Name("example.com"), rrset->getName());
  280. EXPECT_EQ(RRType::DNSKEY(), rrset->getType());
  281. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  282. }
  283. TEST_F(DataSrcTest, NxRRset) {
  284. createAndProcessQuery(Name("example.com"), RRClass::IN(),
  285. RRType::PTR());
  286. headerCheck(msg, qid, Rcode::NOERROR(), opcodeval,
  287. QR_FLAG | AA_FLAG | RD_FLAG, 1, 0, 4, 0);
  288. RRsetIterator rit = msg.beginSection(Message::SECTION_AUTHORITY);
  289. RRsetPtr rrset = *rit;
  290. EXPECT_EQ(Name("example.com"), rrset->getName());
  291. EXPECT_EQ(RRType::SOA(), rrset->getType());
  292. }
  293. TEST_F(DataSrcTest, Nxdomain) {
  294. createAndProcessQuery(Name("glork.example.com"), RRClass::IN(),
  295. RRType::A());
  296. headerCheck(msg, qid, Rcode::NXDOMAIN(), opcodeval,
  297. QR_FLAG | AA_FLAG | RD_FLAG, 1, 0, 6, 0);
  298. RRsetIterator rit = msg.beginSection(Message::SECTION_AUTHORITY);
  299. RRsetPtr rrset = *rit;
  300. EXPECT_EQ(Name("example.com"), rrset->getName());
  301. EXPECT_EQ(RRType::SOA(), rrset->getType());
  302. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  303. // XXX: check for other authority section answers
  304. }
  305. TEST_F(DataSrcTest, NxdomainAfterSOAQuery) {
  306. // There was a bug where once SOA RR is stored in the hot spot cache
  307. // subsequent negative search fails due to "missing SOA". This test
  308. // checks that situation.
  309. // First, run the scenario with disabling the cache.
  310. cache.setEnabled(false);
  311. createAndProcessQuery(Name("example.com"), RRClass::IN(),
  312. RRType::SOA());
  313. msg.clear(Message::PARSE);
  314. createAndProcessQuery(Name("notexistent.example.com"), RRClass::IN(),
  315. RRType::A());
  316. {
  317. SCOPED_TRACE("NXDOMAIN after SOA, without hot spot cache");
  318. headerCheck(msg, qid, Rcode::NXDOMAIN(), opcodeval,
  319. QR_FLAG | AA_FLAG | RD_FLAG, 1, 0, 6, 0);
  320. }
  321. // Then enable the cache and perform the same queries. This should
  322. // produce the same result.
  323. cache.setEnabled(true);
  324. msg.clear(Message::PARSE);
  325. createAndProcessQuery(Name("example.com"), RRClass::IN(),
  326. RRType::SOA());
  327. msg.clear(Message::PARSE);
  328. createAndProcessQuery(Name("notexistent.example.com"), RRClass::IN(),
  329. RRType::A());
  330. {
  331. SCOPED_TRACE("NXDOMAIN after SOA, without hot spot cache");
  332. headerCheck(msg, qid, Rcode::NXDOMAIN(), opcodeval,
  333. QR_FLAG | AA_FLAG | RD_FLAG, 1, 0, 6, 0);
  334. }
  335. }
  336. TEST_F(DataSrcTest, NxZone) {
  337. createAndProcessQuery(Name("spork.example"), RRClass::IN(),
  338. RRType::A());
  339. headerCheck(msg, qid, Rcode::REFUSED(), opcodeval,
  340. QR_FLAG | RD_FLAG, 1, 0, 0, 0);
  341. EXPECT_EQ(Rcode::REFUSED(), msg.getRcode());
  342. EXPECT_TRUE(msg.getHeaderFlag(Message::HEADERFLAG_QR));
  343. EXPECT_FALSE(msg.getHeaderFlag(Message::HEADERFLAG_AA));
  344. EXPECT_TRUE(msg.getHeaderFlag(Message::HEADERFLAG_RD));
  345. }
  346. TEST_F(DataSrcTest, Wildcard) {
  347. createAndProcessQuery(Name("www.wild.example.com"), RRClass::IN(),
  348. RRType::A());
  349. headerCheck(msg, qid, Rcode::NOERROR(), opcodeval,
  350. QR_FLAG | AA_FLAG | RD_FLAG, 1, 2, 6, 6);
  351. RRsetIterator rit = msg.beginSection(Message::SECTION_ANSWER);
  352. RRsetPtr rrset = *rit;
  353. EXPECT_EQ(Name("www.wild.example.com"), rrset->getName());
  354. EXPECT_EQ(RRType::A(), rrset->getType());
  355. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  356. RdataIteratorPtr it = rrset->getRdataIterator();
  357. EXPECT_EQ("192.0.2.2", it->getCurrent().toText());
  358. it->next();
  359. EXPECT_TRUE(it->isLast());
  360. rit = msg.beginSection(Message::SECTION_AUTHORITY);
  361. rrset = *rit;
  362. EXPECT_EQ(Name("*.wild.example.com"), rrset->getName());
  363. EXPECT_EQ(RRType::NSEC(), rrset->getType());
  364. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  365. ++rit;
  366. ++rit;
  367. rrset = *rit;
  368. EXPECT_EQ(Name("example.com"), rrset->getName());
  369. EXPECT_EQ(RRType::NS(), rrset->getType());
  370. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  371. it = rrset->getRdataIterator();
  372. EXPECT_EQ("dns01.example.com.", it->getCurrent().toText());
  373. it->next();
  374. EXPECT_EQ("dns02.example.com.", it->getCurrent().toText());
  375. it->next();
  376. EXPECT_EQ("dns03.example.com.", it->getCurrent().toText());
  377. it->next();
  378. EXPECT_TRUE(it->isLast());
  379. rit = msg.beginSection(Message::SECTION_ADDITIONAL);
  380. rrset = *rit;
  381. EXPECT_EQ(Name("dns01.example.com"), rrset->getName());
  382. EXPECT_EQ(RRType::A(), rrset->getType());
  383. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  384. it = rrset->getRdataIterator();
  385. EXPECT_EQ("192.0.2.1", it->getCurrent().toText());
  386. it->next();
  387. EXPECT_TRUE(it->isLast());
  388. }
  389. TEST_F(DataSrcTest, WildcardNodata) {
  390. // Check that a query for a data type not covered by the wildcard
  391. // returns NOERROR
  392. createAndProcessQuery(Name("www.wild.example.com"), RRClass::IN(),
  393. RRType::AAAA());
  394. headerCheck(msg, qid, Rcode::NOERROR(), opcodeval,
  395. QR_FLAG | AA_FLAG | RD_FLAG, 1, 0, 2, 0);
  396. }
  397. TEST_F(DataSrcTest, DISABLED_WildcardAgainstMultiLabel) {
  398. // this qname shouldn't match *.wild.com.com (because * can only match
  399. // a single label), and it should result in NXDOMAIN.
  400. createAndProcessQuery(Name("www.xxx.wild.example.com"), RRClass::IN(),
  401. RRType::A());
  402. headerCheck(msg, qid, Rcode::NXDOMAIN(), opcodeval,
  403. QR_FLAG | AA_FLAG | RD_FLAG, 1, 0, 1, 0);
  404. }
  405. TEST_F(DataSrcTest, WildcardCname) {
  406. // Check that wildcard answers containing CNAMES are followed
  407. // correctly. It should result in the same response for both
  408. // class IN and ANY queries.
  409. const RRClass classes[2] = { RRClass::IN(), RRClass::ANY() };
  410. for (int i = 0; i < sizeof(classes) / sizeof(classes[0]); ++i) {
  411. SCOPED_TRACE("Wildcard + CNAME test for class " + classes[i].toText());
  412. msg.clear(Message::PARSE);
  413. createAndProcessQuery(Name("www.wild2.example.com"), classes[i],
  414. RRType::A(), false);
  415. headerCheck(msg, qid, Rcode::NOERROR(), opcodeval,
  416. QR_FLAG | AA_FLAG | RD_FLAG, 1, 2, 3, 3);
  417. rrsetsCheck("www.wild2.example.com. 3600 IN CNAME www.example.com\n"
  418. "www.example.com. 3600 IN A 192.0.2.1\n",
  419. msg.beginSection(Message::SECTION_ANSWER),
  420. msg.endSection(Message::SECTION_ANSWER));
  421. rrsetsCheck("example.com. 3600 IN NS dns01.example.com.\n"
  422. "example.com. 3600 IN NS dns02.example.com.\n"
  423. "example.com. 3600 IN NS dns03.example.com.",
  424. msg.beginSection(Message::SECTION_AUTHORITY),
  425. msg.endSection(Message::SECTION_AUTHORITY));
  426. rrsetsCheck("dns01.example.com. 3600 IN A 192.0.2.1\n"
  427. "dns02.example.com. 3600 IN A 192.0.2.2\n"
  428. "dns03.example.com. 3600 IN A 192.0.2.3",
  429. msg.beginSection(Message::SECTION_ADDITIONAL),
  430. msg.endSection(Message::SECTION_ADDITIONAL));
  431. }
  432. }
  433. TEST_F(DataSrcTest, WildcardCnameNodata) {
  434. // A wildcard containing a CNAME whose target does not include
  435. // data of this type.
  436. createAndProcessQuery(Name("www.wild2.example.com"), RRClass::IN(),
  437. RRType::AAAA());
  438. headerCheck(msg, qid, Rcode::NOERROR(), opcodeval,
  439. QR_FLAG | AA_FLAG | RD_FLAG, 1, 2, 4, 0);
  440. RRsetIterator rit = msg.beginSection(Message::SECTION_ANSWER);
  441. RRsetPtr rrset = *rit;
  442. EXPECT_EQ(Name("www.wild2.example.com"), rrset->getName());
  443. EXPECT_EQ(RRType::CNAME(), rrset->getType());
  444. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  445. RdataIteratorPtr it = rrset->getRdataIterator();
  446. EXPECT_EQ("www.example.com.", it->getCurrent().toText());
  447. it->next();
  448. EXPECT_TRUE(it->isLast());
  449. rit = msg.beginSection(Message::SECTION_AUTHORITY);
  450. rrset = *rit;
  451. EXPECT_EQ(Name("*.wild2.example.com"), rrset->getName());
  452. EXPECT_EQ(RRType::NSEC(), rrset->getType());
  453. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  454. ++rit;
  455. ++rit;
  456. rrset = *rit;
  457. EXPECT_EQ(Name("www.example.com"), rrset->getName());
  458. EXPECT_EQ(RRType::NSEC(), rrset->getType());
  459. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  460. }
  461. TEST_F(DataSrcTest, WildcardCnameNxdomain) {
  462. // A wildcard containing a CNAME whose target does not exist
  463. createAndProcessQuery(Name("www.wild3.example.com"), RRClass::IN(),
  464. RRType::A());
  465. headerCheck(msg, qid, Rcode::NOERROR(), opcodeval,
  466. QR_FLAG | AA_FLAG | RD_FLAG, 1, 2, 6, 0);
  467. RRsetIterator rit = msg.beginSection(Message::SECTION_ANSWER);
  468. RRsetPtr rrset = *rit;
  469. EXPECT_EQ(Name("www.wild3.example.com"), rrset->getName());
  470. EXPECT_EQ(RRType::CNAME(), rrset->getType());
  471. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  472. RdataIteratorPtr it = rrset->getRdataIterator();
  473. EXPECT_EQ("spork.example.com.", it->getCurrent().toText());
  474. it->next();
  475. EXPECT_TRUE(it->isLast());
  476. rit = msg.beginSection(Message::SECTION_AUTHORITY);
  477. rrset = *rit;
  478. EXPECT_EQ(Name("*.wild3.example.com"), rrset->getName());
  479. EXPECT_EQ(RRType::NSEC(), rrset->getType());
  480. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  481. ++rit;
  482. ++rit;
  483. rrset = *rit;
  484. EXPECT_EQ(Name("foo.example.com"), rrset->getName());
  485. EXPECT_EQ(RRType::NSEC(), rrset->getType());
  486. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  487. ++rit;
  488. ++rit;
  489. rrset = *rit;
  490. EXPECT_EQ(Name("example.com"), rrset->getName());
  491. EXPECT_EQ(RRType::NSEC(), rrset->getType());
  492. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  493. }
  494. TEST_F(DataSrcTest, AuthDelegation) {
  495. createAndProcessQuery(Name("www.sql1.example.com"), RRClass::IN(),
  496. RRType::A());
  497. headerCheck(msg, qid, Rcode::NOERROR(), opcodeval,
  498. QR_FLAG | AA_FLAG | RD_FLAG, 1, 2, 4, 6);
  499. RRsetIterator rit = msg.beginSection(Message::SECTION_ANSWER);
  500. RRsetPtr rrset = *rit;
  501. EXPECT_EQ(Name("www.sql1.example.com"), rrset->getName());
  502. EXPECT_EQ(RRType::A(), rrset->getType());
  503. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  504. RdataIteratorPtr it = rrset->getRdataIterator();
  505. EXPECT_EQ("192.0.2.2", it->getCurrent().toText());
  506. it->next();
  507. EXPECT_TRUE(it->isLast());
  508. rit = msg.beginSection(Message::SECTION_AUTHORITY);
  509. rrset = *rit;
  510. EXPECT_EQ(Name("sql1.example.com"), rrset->getName());
  511. EXPECT_EQ(RRType::NS(), rrset->getType());
  512. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  513. it = rrset->getRdataIterator();
  514. EXPECT_EQ("dns01.example.com.", it->getCurrent().toText());
  515. it->next();
  516. EXPECT_EQ("dns02.example.com.", it->getCurrent().toText());
  517. it->next();
  518. EXPECT_EQ("dns03.example.com.", it->getCurrent().toText());
  519. it->next();
  520. EXPECT_TRUE(it->isLast());
  521. rit = msg.beginSection(Message::SECTION_ADDITIONAL);
  522. rrset = *rit;
  523. EXPECT_EQ(Name("dns01.example.com"), rrset->getName());
  524. EXPECT_EQ(RRType::A(), rrset->getType());
  525. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  526. it = rrset->getRdataIterator();
  527. EXPECT_EQ("192.0.2.1", it->getCurrent().toText());
  528. it->next();
  529. EXPECT_TRUE(it->isLast());
  530. }
  531. TEST_F(DataSrcTest, Dname) {
  532. createAndProcessQuery(Name("www.dname.example.com"), RRClass::IN(),
  533. RRType::A());
  534. headerCheck(msg, qid, Rcode::NOERROR(), opcodeval,
  535. QR_FLAG | AA_FLAG | RD_FLAG, 1, 5, 4, 6);
  536. RRsetIterator rit = msg.beginSection(Message::SECTION_ANSWER);
  537. RRsetPtr rrset = *rit;
  538. EXPECT_EQ(Name("dname.example.com"), rrset->getName());
  539. EXPECT_EQ(RRType::DNAME(), rrset->getType());
  540. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  541. RdataIteratorPtr it = rrset->getRdataIterator();
  542. EXPECT_EQ("sql1.example.com.", it->getCurrent().toText());
  543. it->next();
  544. EXPECT_TRUE(it->isLast());
  545. // XXX: check CNAME and A record too
  546. rit = msg.beginSection(Message::SECTION_AUTHORITY);
  547. rrset = *rit;
  548. EXPECT_EQ(Name("sql1.example.com"), rrset->getName());
  549. EXPECT_EQ(RRType::NS(), rrset->getType());
  550. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  551. it = rrset->getRdataIterator();
  552. EXPECT_EQ("dns01.example.com.", it->getCurrent().toText());
  553. it->next();
  554. EXPECT_EQ("dns02.example.com.", it->getCurrent().toText());
  555. it->next();
  556. EXPECT_EQ("dns03.example.com.", it->getCurrent().toText());
  557. it->next();
  558. EXPECT_TRUE(it->isLast());
  559. rit = msg.beginSection(Message::SECTION_ADDITIONAL);
  560. rrset = *rit;
  561. EXPECT_EQ(Name("dns01.example.com"), rrset->getName());
  562. EXPECT_EQ(RRType::A(), rrset->getType());
  563. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  564. it = rrset->getRdataIterator();
  565. EXPECT_EQ("192.0.2.1", it->getCurrent().toText());
  566. it->next();
  567. EXPECT_TRUE(it->isLast());
  568. }
  569. TEST_F(DataSrcTest, DnameExact) {
  570. // The example.org test zone has a DNAME RR for dname2.foo.example.org.
  571. // A query for that name with a different RR type than DNAME shouldn't
  572. // confuse delegation processing.
  573. createAndProcessQuery(Name("dname2.foo.example.org"), RRClass::IN(),
  574. RRType::A());
  575. headerCheck(msg, qid, Rcode::NOERROR(), opcodeval,
  576. QR_FLAG | AA_FLAG | RD_FLAG, 1, 0, 1, 0);
  577. }
  578. TEST_F(DataSrcTest, Cname) {
  579. createAndProcessQuery(Name("foo.example.com"), RRClass::IN(),
  580. RRType::A());
  581. headerCheck(msg, qid, Rcode::NOERROR(), opcodeval,
  582. QR_FLAG | AA_FLAG | RD_FLAG, 1, 2, 0, 0);
  583. RRsetIterator rit = msg.beginSection(Message::SECTION_ANSWER);
  584. RRsetPtr rrset = *rit;
  585. EXPECT_EQ(Name("foo.example.com"), rrset->getName());
  586. EXPECT_EQ(RRType::CNAME(), rrset->getType());
  587. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  588. RdataIteratorPtr it = rrset->getRdataIterator();
  589. EXPECT_EQ("cnametest.example.net.", it->getCurrent().toText());
  590. it->next();
  591. EXPECT_TRUE(it->isLast());
  592. }
  593. TEST_F(DataSrcTest, CnameInt) {
  594. createAndProcessQuery(Name("cname-int.example.com"), RRClass::IN(),
  595. RRType::A());
  596. headerCheck(msg, qid, Rcode::NOERROR(), opcodeval,
  597. QR_FLAG | AA_FLAG | RD_FLAG, 1, 4, 4, 6);
  598. RRsetIterator rit = msg.beginSection(Message::SECTION_ANSWER);
  599. RRsetPtr rrset = *rit;
  600. EXPECT_EQ(Name("cname-int.example.com"), rrset->getName());
  601. EXPECT_EQ(RRType::CNAME(), rrset->getType());
  602. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  603. RdataIteratorPtr it = rrset->getRdataIterator();
  604. EXPECT_EQ("www.example.com.", it->getCurrent().toText());
  605. it->next();
  606. EXPECT_TRUE(it->isLast());
  607. // XXX: check a record as well
  608. rit = msg.beginSection(Message::SECTION_AUTHORITY);
  609. rrset = *rit;
  610. EXPECT_EQ(Name("example.com"), rrset->getName());
  611. EXPECT_EQ(RRType::NS(), rrset->getType());
  612. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  613. }
  614. TEST_F(DataSrcTest, CnameExt) {
  615. createAndProcessQuery(Name("cname-ext.example.com"), RRClass::IN(),
  616. RRType::A());
  617. headerCheck(msg, qid, Rcode::NOERROR(), opcodeval,
  618. QR_FLAG | AA_FLAG | RD_FLAG, 1, 4, 4, 6);
  619. RRsetIterator rit = msg.beginSection(Message::SECTION_ANSWER);
  620. RRsetPtr rrset = *rit;
  621. EXPECT_EQ(Name("cname-ext.example.com"), rrset->getName());
  622. EXPECT_EQ(RRType::CNAME(), rrset->getType());
  623. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  624. RdataIteratorPtr it = rrset->getRdataIterator();
  625. EXPECT_EQ("www.sql1.example.com.", it->getCurrent().toText());
  626. it->next();
  627. EXPECT_TRUE(it->isLast());
  628. rit = msg.beginSection(Message::SECTION_AUTHORITY);
  629. rrset = *rit;
  630. EXPECT_EQ(Name("sql1.example.com"), rrset->getName());
  631. EXPECT_EQ(RRType::NS(), rrset->getType());
  632. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  633. }
  634. TEST_F(DataSrcTest, Delegation) {
  635. createAndProcessQuery(Name("www.subzone.example.com"), RRClass::IN(),
  636. RRType::A());
  637. headerCheck(msg, qid, Rcode::NOERROR(), opcodeval,
  638. QR_FLAG | RD_FLAG, 1, 0, 5, 2);
  639. RRsetIterator rit = msg.beginSection(Message::SECTION_AUTHORITY);
  640. RRsetPtr rrset = *rit;
  641. EXPECT_EQ(Name("subzone.example.com."), rrset->getName());
  642. EXPECT_EQ(RRType::NS(), rrset->getType());
  643. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  644. RdataIteratorPtr it = rrset->getRdataIterator();
  645. EXPECT_EQ("ns1.subzone.example.com.", it->getCurrent().toText());
  646. it->next();
  647. EXPECT_FALSE(it->isLast());
  648. rit = msg.beginSection(Message::SECTION_ADDITIONAL);
  649. rrset = *rit;
  650. EXPECT_EQ(Name("ns1.subzone.example.com"), rrset->getName());
  651. EXPECT_EQ(RRType::A(), rrset->getType());
  652. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  653. it = rrset->getRdataIterator();
  654. EXPECT_EQ("192.0.2.1", it->getCurrent().toText());
  655. it->next();
  656. EXPECT_TRUE(it->isLast());
  657. }
  658. TEST_F(DataSrcTest, NSDelegation) {
  659. createAndProcessQuery(Name("subzone.example.com"), RRClass::IN(),
  660. RRType::NS());
  661. headerCheck(msg, qid, Rcode::NOERROR(), opcodeval,
  662. QR_FLAG | RD_FLAG, 1, 0, 5, 2);
  663. RRsetIterator rit = msg.beginSection(Message::SECTION_AUTHORITY);
  664. RRsetPtr rrset = *rit;
  665. EXPECT_EQ(Name("subzone.example.com."), rrset->getName());
  666. EXPECT_EQ(RRType::NS(), rrset->getType());
  667. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  668. RdataIteratorPtr it = rrset->getRdataIterator();
  669. EXPECT_EQ("ns1.subzone.example.com.", it->getCurrent().toText());
  670. it->next();
  671. EXPECT_FALSE(it->isLast());
  672. rit = msg.beginSection(Message::SECTION_ADDITIONAL);
  673. rrset = *rit;
  674. EXPECT_EQ(Name("ns1.subzone.example.com"), rrset->getName());
  675. EXPECT_EQ(RRType::A(), rrset->getType());
  676. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  677. it = rrset->getRdataIterator();
  678. EXPECT_EQ("192.0.2.1", it->getCurrent().toText());
  679. it->next();
  680. EXPECT_TRUE(it->isLast());
  681. }
  682. TEST_F(DataSrcTest, ANYZonecut) {
  683. // An ANY query at a zone cut should behave the same as any other
  684. // delegation
  685. createAndProcessQuery(Name("subzone.example.com"), RRClass::IN(),
  686. RRType::ANY());
  687. }
  688. TEST_F(DataSrcTest, NSECZonecut) {
  689. createAndProcessQuery(Name("subzone.example.com"), RRClass::IN(),
  690. RRType::NSEC());
  691. headerCheck(msg, qid, Rcode::NOERROR(), opcodeval,
  692. QR_FLAG | AA_FLAG | RD_FLAG, 1, 2, 4, 6);
  693. RRsetIterator rit = msg.beginSection(Message::SECTION_ANSWER);
  694. RRsetPtr rrset = *rit;
  695. EXPECT_EQ(Name("subzone.example.com."), rrset->getName());
  696. EXPECT_EQ(RRType::NSEC(), rrset->getType());
  697. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  698. rit = msg.beginSection(Message::SECTION_AUTHORITY);
  699. rrset = *rit;
  700. EXPECT_EQ(Name("example.com"), rrset->getName());
  701. EXPECT_EQ(RRType::NS(), rrset->getType());
  702. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  703. RdataIteratorPtr it = rrset->getRdataIterator();
  704. EXPECT_EQ("dns01.example.com.", it->getCurrent().toText());
  705. it->next();
  706. EXPECT_EQ("dns02.example.com.", it->getCurrent().toText());
  707. it->next();
  708. EXPECT_EQ("dns03.example.com.", it->getCurrent().toText());
  709. it->next();
  710. EXPECT_TRUE(it->isLast());
  711. }
  712. TEST_F(DataSrcTest, DNAMEZonecut) {
  713. createAndProcessQuery(Name("subzone.example.com"), RRClass::IN(),
  714. RRType::DNAME());
  715. headerCheck(msg, qid, Rcode::NOERROR(), opcodeval,
  716. QR_FLAG | RD_FLAG, 1, 0, 5, 2);
  717. RRsetIterator rit = msg.beginSection(Message::SECTION_AUTHORITY);
  718. RRsetPtr rrset = *rit;
  719. EXPECT_EQ(Name("subzone.example.com."), rrset->getName());
  720. EXPECT_EQ(RRType::NS(), rrset->getType());
  721. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  722. RdataIteratorPtr it = rrset->getRdataIterator();
  723. EXPECT_EQ("ns1.subzone.example.com.", it->getCurrent().toText());
  724. it->next();
  725. EXPECT_FALSE(it->isLast());
  726. rit = msg.beginSection(Message::SECTION_ADDITIONAL);
  727. rrset = *rit;
  728. EXPECT_EQ(Name("ns1.subzone.example.com"), rrset->getName());
  729. EXPECT_EQ(RRType::A(), rrset->getType());
  730. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  731. it = rrset->getRdataIterator();
  732. EXPECT_EQ("192.0.2.1", it->getCurrent().toText());
  733. it->next();
  734. EXPECT_TRUE(it->isLast());
  735. }
  736. TEST_F(DataSrcTest, DS) {
  737. createAndProcessQuery(Name("subzone.example.com"), RRClass::IN(),
  738. RRType::DS());
  739. headerCheck(msg, qid, Rcode::NOERROR(), opcodeval,
  740. QR_FLAG | AA_FLAG | RD_FLAG, 1, 3, 4, 6);
  741. RRsetIterator rit = msg.beginSection(Message::SECTION_ANSWER);
  742. RRsetPtr rrset = *rit;
  743. EXPECT_EQ(Name("subzone.example.com."), rrset->getName());
  744. EXPECT_EQ(RRType::DS(), rrset->getType());
  745. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  746. rit = msg.beginSection(Message::SECTION_AUTHORITY);
  747. rrset = *rit;
  748. EXPECT_EQ(Name("example.com"), rrset->getName());
  749. EXPECT_EQ(RRType::NS(), rrset->getType());
  750. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  751. RdataIteratorPtr it = rrset->getRdataIterator();
  752. EXPECT_EQ("dns01.example.com.", it->getCurrent().toText());
  753. it->next();
  754. EXPECT_EQ("dns02.example.com.", it->getCurrent().toText());
  755. it->next();
  756. EXPECT_EQ("dns03.example.com.", it->getCurrent().toText());
  757. it->next();
  758. EXPECT_TRUE(it->isLast());
  759. }
  760. TEST_F(DataSrcTest, CNAMELoop) {
  761. createAndProcessQuery(Name("one.loop.example"), RRClass::IN(),
  762. RRType::A());
  763. EXPECT_EQ(Rcode::NOERROR(), msg.getRcode());
  764. // one.loop.example points to two.loop.example, which points back
  765. // to one.loop.example, so there should be exactly two CNAME records
  766. // in the answer.
  767. EXPECT_EQ(2, msg.getRRCount(Message::SECTION_ANSWER));
  768. }
  769. // NSEC query for the name of a zone cut for non-secure delegation.
  770. // Should return normal referral.
  771. TEST_F(DataSrcTest, NSECZonecutOfNonsecureZone) {
  772. createAndProcessQuery(Name("sub.example.org"), RRClass::IN(),
  773. RRType::NSEC());
  774. headerCheck(msg, qid, Rcode::NOERROR(), opcodeval,
  775. QR_FLAG | RD_FLAG, 1, 0, 1, 1);
  776. RRsetIterator rit = msg.beginSection(Message::SECTION_AUTHORITY);
  777. ConstRRsetPtr rrset = *rit;
  778. EXPECT_EQ(Name("sub.example.org."), rrset->getName());
  779. EXPECT_EQ(RRType::NS(), rrset->getType());
  780. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  781. RdataIteratorPtr it = rrset->getRdataIterator();
  782. EXPECT_EQ(createRdata(RRType::NS(), RRClass::IN(),
  783. "ns.sub.example.org.")->toText(),
  784. it->getCurrent().toText());
  785. it->next();
  786. EXPECT_TRUE(it->isLast());
  787. rit = msg.beginSection(Message::SECTION_ADDITIONAL);
  788. rrset = *rit;
  789. EXPECT_EQ(Name("ns.sub.example.org."), rrset->getName());
  790. EXPECT_EQ(RRType::A(), rrset->getType());
  791. EXPECT_EQ(RRClass::IN(), rrset->getClass());
  792. it = rrset->getRdataIterator();
  793. EXPECT_EQ(createRdata(RRType::A(), RRClass::IN(), "192.0.2.101")->toText(),
  794. it->getCurrent().toText());
  795. it->next();
  796. EXPECT_TRUE(it->isLast());
  797. }
  798. // Test sending a DS query to root (nonsense, but it should survive)
  799. TEST_F(DataSrcTest, RootDSQuery1) {
  800. EXPECT_NO_THROW(createAndProcessQuery(Name("."), RRClass::IN(),
  801. RRType::DS()));
  802. headerCheck(msg, qid, Rcode::REFUSED(), opcodeval,
  803. QR_FLAG | RD_FLAG, 1, 0, 0, 0);
  804. }
  805. // The same, but when we have the root zone
  806. // (which triggers rfc4035 section 3.1.4.1)
  807. TEST_F(DataSrcTest, RootDSQuery2) {
  808. // The message
  809. msg.makeResponse();
  810. msg.setOpcode(Opcode::QUERY());
  811. msg.addQuestion(Question(Name("."), RRClass::IN(), RRType::DS()));
  812. msg.setHeaderFlag(Message::HEADERFLAG_RD);
  813. // Prepare the source
  814. DataSrcPtr sql3_source = DataSrcPtr(new Sqlite3DataSrc);
  815. ConstElementPtr sqlite_root = Element::fromJSON(
  816. "{ \"database_file\": \"" TEST_DATA_DIR "/test-root.sqlite3\"}");
  817. EXPECT_NO_THROW(sql3_source->init(sqlite_root));
  818. // Make the query
  819. EXPECT_NO_THROW(performQuery(*sql3_source, cache, msg));
  820. headerCheck(msg, qid, Rcode::NOERROR(), opcodeval,
  821. QR_FLAG | AA_FLAG | RD_FLAG, 1, 0, 1, 0);
  822. }
  823. TEST_F(DataSrcTest, DSQueryFromCache) {
  824. // explicitly enable hot spot cache
  825. cache.setEnabled(true);
  826. // The first query will create a negative cache for example.org/CNAME
  827. createAndProcessQuery(Name("example.org"), RRClass::IN(), RRType::SOA());
  828. // the cached CNAME shouldn't confuse subsequent query.
  829. // there may be several different possible cases that could trigger a bug,
  830. // but DS query is the only known example.
  831. msg.clear(Message::PARSE);
  832. createAndProcessQuery(Name("example.org"), RRClass::IN(), RRType::DS());
  833. // returning refused is probably a bad behavior, but it's a different
  834. // issue -- see Trac Ticket #306.
  835. headerCheck(msg, qid, Rcode::REFUSED(), opcodeval,
  836. QR_FLAG | RD_FLAG, 1, 0, 0, 0);
  837. }
  838. // Non-existent name in the "static" data source. The purpose of this test
  839. // is to check a corner case behavior when atypical RRClass (CH in this case)
  840. // is specified.
  841. TEST_F(DataSrcTest, StaticNxDomain) {
  842. createAndProcessQuery(Name("www.version.bind"), RRClass::CH(),
  843. RRType::TXT());
  844. headerCheck(msg, qid, Rcode::NXDOMAIN(), opcodeval,
  845. QR_FLAG | AA_FLAG | RD_FLAG, 1, 0, 1, 0);
  846. RRsetIterator rit = msg.beginSection(Message::SECTION_AUTHORITY);
  847. RRsetPtr rrset = *rit;
  848. EXPECT_EQ(Name("version.bind"), rrset->getName());
  849. EXPECT_EQ(RRType::SOA(), rrset->getType());
  850. EXPECT_EQ(RRClass::CH(), rrset->getClass());
  851. }
  852. TEST_F(DataSrcTest, Nsec3Hash) {
  853. vector<uint8_t> salt;
  854. salt.push_back(0xfe);
  855. salt.push_back(0xed);
  856. salt.push_back(0xab);
  857. salt.push_back(0xee);
  858. Nsec3Param nsec3(1, 0, 10, salt);
  859. EXPECT_EQ("VIR9KJAPN2FHRLS6EP0JBQ89MBLUE296", nsec3.getHash(Name("test1")));
  860. EXPECT_EQ("FHA27EURONFH5640SFJQ8MJAKMCVB7UJ", nsec3.getHash(Name("test2")));
  861. EXPECT_EQ("A4M93LR7A60IDDQMO6TCVUPCC60CU38A", nsec3.getHash(Name("test3")));
  862. }
  863. TEST_F(DataSrcTest, AddRemoveDataSrc) {
  864. MetaDataSrc ds;
  865. ConstDataSrcPtr tsp = ConstDataSrcPtr(new TestDataSrc);
  866. EXPECT_EQ(0, ds.dataSrcCount());
  867. ds.addDataSrc(tsp);
  868. EXPECT_EQ(1, ds.dataSrcCount());
  869. ds.removeDataSrc(tsp);
  870. EXPECT_EQ(0, ds.dataSrcCount());
  871. }
  872. TEST_F(DataSrcTest, noNSZone) {
  873. EXPECT_THROW(createAndProcessQuery(Name("www.nons.example"),
  874. RRClass::IN(), RRType::A()),
  875. DataSourceError);
  876. }
  877. TEST_F(DataSrcTest, noNSButDnameZone) {
  878. EXPECT_THROW(createAndProcessQuery(Name("www.nons-dname.example"),
  879. RRClass::IN(), RRType::A()),
  880. DataSourceError);
  881. }
  882. TEST_F(DataSrcTest, noSOAZone) {
  883. EXPECT_THROW(createAndProcessQuery(Name("notexist.nosoa.example"),
  884. RRClass::IN(), RRType::A()),
  885. DataSourceError);
  886. }
  887. TEST_F(DataSrcTest, apexCNAMEZone) {
  888. // The query name doesn't exist in the best matching zone,
  889. // and there's a CNAME at the apex (which is bogus), so query handling
  890. // will fail due to missing SOA.
  891. EXPECT_THROW(createAndProcessQuery(Name("notexist.apexcname.example"),
  892. RRClass::IN(), RRType::A()),
  893. DataSourceError);
  894. }
  895. TEST_F(DataSrcTest, incompleteGlue) {
  896. // One of the NS names belong to a different zone (which is still
  897. // authoritative), and the glue is missing in that zone. We should
  898. // still return the existent glue.
  899. // (nons.example is also broken in that it doesn't have apex NS, but
  900. // that doesn't matter for this test)
  901. createAndProcessQuery(Name("www.incompletechild.nons.example"),
  902. RRClass::IN(), RRType::A());
  903. headerCheck(msg, qid, Rcode::NOERROR(), opcodeval,
  904. QR_FLAG | RD_FLAG, 1, 0, 2, 1);
  905. rrsetsCheck("incompletechild.nons.example. 3600 IN NS ns.incompletechild.nons.example.\n"
  906. "incompletechild.nons.example. 3600 IN NS nx.nosoa.example.",
  907. msg.beginSection(Message::SECTION_AUTHORITY),
  908. msg.endSection(Message::SECTION_AUTHORITY));
  909. rrsetsCheck("ns.incompletechild.nons.example. 3600 IN A 192.0.2.1",
  910. msg.beginSection(Message::SECTION_ADDITIONAL),
  911. msg.endSection(Message::SECTION_ADDITIONAL));
  912. }
  913. // currently fails
  914. TEST_F(DataSrcTest, DISABLED_synthesizedCnameTooLong) {
  915. // qname has the possible max length (255 octets). it matches a DNAME,
  916. // and the synthesized CNAME would exceed the valid length.
  917. createAndProcessQuery(
  918. Name("0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde."
  919. "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde."
  920. "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde."
  921. "0123456789abcdef0123456789abcdef0123456789a.dname.example.org."),
  922. RRClass::IN(), RRType::A());
  923. }
  924. TEST_F(DataSrcTest, cacheDataInNonexistentZone) {
  925. // This test emulates the situation where an RRset in some zone of some
  926. // data source is cached and then the zone is removed from the data source.
  927. // When there is such a substantial inconsistency between the cache and
  928. // the real data source, we should honor the latter. More important,
  929. // the inconsistency shouldn't cause any disruption such as a crash.
  930. const Name qname("nosuchzone.example");
  931. RRsetPtr rrset(new RRset(qname, RRClass::IN(), RRType::A(), RRTTL(0)));
  932. cache.addPositive(rrset, DataSrc::REFERRAL);
  933. createAndProcessQuery(qname, RRClass::IN(), RRType::A(), false);
  934. headerCheck(msg, qid, Rcode::REFUSED(), opcodeval, QR_FLAG | RD_FLAG,
  935. 1, 0, 0, 0);
  936. }
  937. // Tests of the DataSrcMatch class start here
  938. class DataSrcMatchTest : public ::testing::Test {
  939. protected:
  940. DataSrcMatchTest() {
  941. datasrc1.init();
  942. }
  943. // test data source serves example.com/IN.
  944. TestDataSrc datasrc1;
  945. // this data source is dummy. Its content doesn't matter in the tests.
  946. TestDataSrc datasrc2;
  947. };
  948. TEST_F(DataSrcMatchTest, match) {
  949. DataSrcMatch match(Name("very.very.long.example.com"), RRClass::IN());
  950. datasrc1.findClosestEnclosure(match);
  951. EXPECT_EQ(Name("example.com"), *match.getEnclosingZone());
  952. EXPECT_EQ(&datasrc1, match.getDataSource());
  953. }
  954. TEST_F(DataSrcMatchTest, matchWithWrongClass) {
  955. DataSrcMatch match(Name("very.very.long.example.com"), RRClass::CH());
  956. datasrc1.findClosestEnclosure(match);
  957. // XXX: some deviant compilers seem to fail to recognize a NULL as a
  958. // pointer type. This explicit cast works around such compilers.
  959. EXPECT_EQ(static_cast<void*>(NULL), match.getEnclosingZone());
  960. EXPECT_EQ(static_cast<void*>(NULL), match.getDataSource());
  961. }
  962. TEST_F(DataSrcMatchTest, matchWithAnyClass) {
  963. DataSrcMatch match(Name("very.very.long.example.com"), RRClass::ANY());
  964. datasrc1.findClosestEnclosure(match);
  965. EXPECT_EQ(Name("example.com"), *match.getEnclosingZone());
  966. EXPECT_EQ(&datasrc1, match.getDataSource());
  967. }
  968. TEST_F(DataSrcMatchTest, updateWithWrongClass) {
  969. DataSrcMatch match(Name("www.example.com"), RRClass::CH());
  970. EXPECT_EQ(RRClass::IN(), datasrc2.getClass());
  971. match.update(datasrc2, Name("com"));
  972. EXPECT_EQ(static_cast<void*>(NULL), match.getEnclosingZone());
  973. EXPECT_EQ(static_cast<void*>(NULL), match.getDataSource());
  974. EXPECT_EQ(RRClass::IN(), datasrc1.getClass());
  975. match.update(datasrc1, Name("example.com"));
  976. EXPECT_EQ(static_cast<void*>(NULL), match.getEnclosingZone());
  977. EXPECT_EQ(static_cast<void*>(NULL), match.getDataSource());
  978. }
  979. TEST_F(DataSrcMatchTest, updateAgainstAnyClass) {
  980. DataSrcMatch match(Name("www.example.com"), RRClass::ANY());
  981. match.update(datasrc2, Name("com"));
  982. EXPECT_EQ(Name("com"), *match.getEnclosingZone());
  983. EXPECT_EQ(&datasrc2, match.getDataSource());
  984. // the given class for search is ANY, so update should be okay.
  985. EXPECT_EQ(RRClass::IN(), datasrc1.getClass());
  986. match.update(datasrc1, Name("example.com"));
  987. EXPECT_EQ(Name("example.com"), *match.getEnclosingZone());
  988. EXPECT_EQ(&datasrc1, match.getDataSource());
  989. }
  990. TEST_F(DataSrcMatchTest, updateWithNoMatch) {
  991. DataSrcMatch match(Name("www.example.com"), RRClass::IN());
  992. match.update(datasrc1, Name("com"));
  993. EXPECT_EQ(Name("com"), *match.getEnclosingZone());
  994. EXPECT_EQ(&datasrc1, match.getDataSource());
  995. // An attempt of update with a name that doesn't match. This attempt
  996. // should be ignored.
  997. match.update(datasrc2, Name("example.org"));
  998. EXPECT_EQ(Name("com"), *match.getEnclosingZone());
  999. EXPECT_EQ(&datasrc1, match.getDataSource());
  1000. }
  1001. TEST_F(DataSrcMatchTest, initialUpdateWithNoMatch) {
  1002. DataSrcMatch match(Name("www.example.com"), RRClass::IN());
  1003. // An initial attempt of update with a name that doesn't match.
  1004. // Should be ignored.
  1005. match.update(datasrc1, Name("example.org"));
  1006. EXPECT_EQ(static_cast<void*>(NULL), match.getEnclosingZone());
  1007. EXPECT_EQ(static_cast<void*>(NULL), match.getDataSource());
  1008. }
  1009. TEST_F(DataSrcMatchTest, updateWithShorterMatch) {
  1010. DataSrcMatch match(Name("www.example.com"), RRClass::IN());
  1011. match.update(datasrc1, Name("example.com"));
  1012. EXPECT_EQ(Name("example.com"), *match.getEnclosingZone());
  1013. EXPECT_EQ(&datasrc1, match.getDataSource());
  1014. // An attempt of update with a name that gives a shorter match.
  1015. // This attempt should be ignored.
  1016. match.update(datasrc2, Name("com"));
  1017. EXPECT_EQ(Name("example.com"), *match.getEnclosingZone());
  1018. EXPECT_EQ(&datasrc1, match.getDataSource());
  1019. }
  1020. }