data_source_sqlite3_unittest.cc 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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. // $Id$
  15. #include <stdint.h>
  16. #include <algorithm>
  17. #include <string>
  18. #include <vector>
  19. #include <gtest/gtest.h>
  20. #include <dns/name.h>
  21. #include <dns/message.h>
  22. #include <dns/rdata.h>
  23. #include <dns/rrclass.h>
  24. #include <dns/rrtype.h>
  25. #include <dns/rdataclass.h>
  26. #include <dns/rrsetlist.h>
  27. #include "query.h"
  28. #include "data_source.h"
  29. #include "data_source_sqlite3.h"
  30. using namespace std;
  31. using namespace isc::dns;
  32. using namespace isc::dns::rdata;
  33. using namespace isc::auth;
  34. namespace {
  35. static const char* SQLITE_DBFILE_EXAMPLE = "testdata/test.sqlite3";
  36. static const char* SQLITE_DBFILE_EXAMPLE2 = "testdata/test2.sqlite3";
  37. static const string sigdata_common(" 20100322084538 20100220084538 "
  38. "33495 example.com. FAKEFAKEFAKEFAKE");
  39. static const string dnskey1_data(" AwEAAcOUBllYc1hf7ND9uDy+Yz1BF3sI0m4q"
  40. "NGV7WcTD0WEiuV7IjXgHE36fCmS9QsUxSSOV"
  41. "o1I/FMxI2PJVqTYHkXFBS7AzLGsQYMU7UjBZ"
  42. "SotBJ6Imt5pXMu+lEDNy8TOUzG3xm7g0qcbW"
  43. "YF6qCEfvZoBtAqi5Rk7Mlrqs8agxYyMx");
  44. static const string dnskey2_data(" AwEAAe5WFbxdCPq2jZrZhlMj7oJdff3W7syJ"
  45. "tbvzg62tRx0gkoCDoBI9DPjlOQG0UAbj+xUV"
  46. "4HQZJStJaZ+fHU5AwVNT+bBZdtV+NujSikhd"
  47. "THb4FYLg2b3Cx9NyJvAVukHp/91HnWuG4T36"
  48. "CzAFrfPwsHIrBz9BsaIQ21VRkcmj7DswfI/i"
  49. "DGd8j6bqiODyNZYQ+ZrLmF0KIJ2yPN3iO6Zq"
  50. "23TaOrVTjB7d1a/h31ODfiHAxFHrkY3t3D5J"
  51. "R9Nsl/7fdRmSznwtcSDgLXBoFEYmw6p86Acv"
  52. "RyoYNcL1SXjaKVLG5jyU3UR+LcGZT5t/0xGf"
  53. "oIK/aKwENrsjcKZZj660b1M=");
  54. static const Name zone_name("example.com");
  55. static const Name nomatch_name("example.org");
  56. static const Name child_name("sql1.example.com");
  57. static const Name www_name("www.example.com");
  58. static const Name www_upper_name("WWW.EXAMPLE.COM");
  59. typedef enum {
  60. NORMAL,
  61. EXACT,
  62. ADDRESS,
  63. REFERRAL
  64. } FindMode;
  65. class Sqlite3DataSourceTest : public ::testing::Test {
  66. protected:
  67. Sqlite3DataSourceTest() : message(Message::PARSE),
  68. query(NULL), rrclass(RRClass::IN()),
  69. rrtype(RRType::A()), rrttl(RRTTL(3600)),
  70. find_flags(0), rrset_matched(0), rrset_count(0)
  71. {
  72. data_source.init(SQLITE_DBFILE_EXAMPLE);
  73. // the data source will ignore the message, and the encapsulating
  74. // query object so the content doesn't matter.
  75. // (it's a bad practice, but is a different issue)
  76. message.addQuestion(Question(Name("example.org"), rrclass, rrtype));
  77. query = new Query(message, true);
  78. common_a_data.push_back("192.0.2.1");
  79. common_sig_data.push_back("A 5 3 3600" + sigdata_common);
  80. common_aaaa_data.push_back("2001:db8::1234");
  81. common_aaaa_sig_data.push_back("AAAA 5 3 3600" + sigdata_common);
  82. www_nsec_data.push_back("example.com. A RRSIG NSEC");
  83. www_nsec_sig_data.push_back("NSEC 5 3 7200" + sigdata_common);
  84. mix_a_data.push_back("192.0.2.1");
  85. mix_a_data.push_back("192.0.2.2");
  86. mix_aaaa_data.push_back("2001:db8::1");
  87. mix_aaaa_data.push_back("2001:db8::2");
  88. apex_soa_data.push_back("master.example.com. admin.example.com. "
  89. "1234 3600 1800 2419200 7200");
  90. apex_soa_sig_data.push_back("SOA 5 2 3600" + sigdata_common);
  91. apex_ns_data.push_back("dns01.example.com.");
  92. apex_ns_data.push_back("dns02.example.com.");
  93. apex_ns_data.push_back("dns03.example.com.");
  94. apex_ns_sig_data.push_back("NS 5 2 3600" + sigdata_common);
  95. apex_mx_data.push_back("10 mail.example.com.");
  96. apex_mx_data.push_back("20 mail.subzone.example.com.");
  97. apex_mx_sig_data.push_back("MX 5 2 3600" + sigdata_common);
  98. apex_nsec_data.push_back("cname-ext.example.com. "
  99. "NS SOA MX RRSIG NSEC DNSKEY");
  100. apex_nsec_sig_data.push_back("NSEC 5 2 7200" + sigdata_common);
  101. apex_dnskey_data.push_back("256 3 5" + dnskey1_data);
  102. apex_dnskey_data.push_back("257 3 5" + dnskey2_data);
  103. // this one is special (using different key):
  104. apex_dnskey_sig_data.push_back("DNSKEY 5 2 3600 20100322084538 "
  105. "20100220084538 4456 example.com. "
  106. "FAKEFAKEFAKEFAKE");
  107. apex_dnskey_sig_data.push_back("DNSKEY 5 2 3600" + sigdata_common);
  108. wild_a_data.push_back("192.0.2.255");
  109. dname_data.push_back("sql1.example.com.");
  110. dname_sig_data.push_back("DNAME 5 3 3600" + sigdata_common);
  111. cname_data.push_back("cnametest.example.org.");
  112. cname_sig_data.push_back("CNAME 5 3 3600" + sigdata_common);
  113. cname_nsec_data.push_back("mail.example.com. CNAME RRSIG NSEC");
  114. cname_nsec_sig_data.push_back("NSEC 5 3 7200" + sigdata_common);
  115. delegation_ns_data.push_back("ns1.subzone.example.com.");
  116. delegation_ns_data.push_back("ns2.subzone.example.com.");
  117. delegation_ds_data.push_back("33313 5 1 "
  118. "FDD7A2C11AA7F55D50FBF9B7EDDA2322C541A8DE");
  119. delegation_ds_data.push_back("33313 5 2 "
  120. "0B99B7006F496D135B01AB17EDB469B4BE9E"
  121. "1973884DEA757BC4E3015A8C3AB3");
  122. delegation_ds_sig_data.push_back("DS 5 3 3600" + sigdata_common);
  123. delegation_nsec_data.push_back("*.wild.example.com. NS DS RRSIG NSEC");
  124. delegation_nsec_sig_data.push_back("NSEC 5 3 7200" + sigdata_common);
  125. child_a_data.push_back("192.0.2.100");
  126. child_sig_data.push_back("A 5 4 3600 20100322084536 "
  127. "20100220084536 12447 sql1.example.com. "
  128. "FAKEFAKEFAKEFAKE");
  129. }
  130. ~Sqlite3DataSourceTest() { delete query; }
  131. Sqlite3DataSrc data_source;
  132. Message message;
  133. Query* query;
  134. // we allow these to be modified in the test
  135. RRClass rrclass;
  136. RRType rrtype;
  137. RRTTL rrttl;
  138. RRsetList result_sets;
  139. uint32_t find_flags;
  140. unsigned rrset_matched;
  141. unsigned rrset_count;
  142. vector<RRType> types;
  143. vector<RRTTL> ttls;
  144. vector<const vector<string>* > answers;
  145. vector<const vector<string>* > signatures;
  146. vector<RRType> expected_types;
  147. vector<string> common_a_data;
  148. vector<string> common_sig_data;
  149. vector<string> common_aaaa_data;
  150. vector<string> common_aaaa_sig_data;
  151. vector<string> www_nsec_data;
  152. vector<string> www_nsec_sig_data;
  153. vector<string> mix_a_data;
  154. vector<string> mix_aaaa_data;
  155. vector<string> apex_soa_data;
  156. vector<string> apex_soa_sig_data;
  157. vector<string> apex_ns_data;
  158. vector<string> apex_ns_sig_data;
  159. vector<string> apex_mx_data;
  160. vector<string> apex_mx_sig_data;
  161. vector<string> apex_nsec_data;
  162. vector<string> apex_nsec_sig_data;
  163. vector<string> apex_dnskey_data;
  164. vector<string> apex_dnskey_sig_data;
  165. vector<string> wild_a_data;
  166. vector<string> dname_data;
  167. vector<string> dname_sig_data;
  168. vector<string> cname_data;
  169. vector<string> cname_sig_data;
  170. vector<string> cname_nsec_data;
  171. vector<string> cname_nsec_sig_data;
  172. vector<string> delegation_ns_data;
  173. vector<string> delegation_ns_sig_data;
  174. vector<string> delegation_ds_data;
  175. vector<string> delegation_ds_sig_data;
  176. vector<string> delegation_nsec_data;
  177. vector<string> delegation_nsec_sig_data;
  178. vector<string> child_a_data;
  179. vector<string> child_sig_data;
  180. };
  181. void
  182. checkRRset(RRsetPtr rrset, const Name& expected_name,
  183. const RRClass& expected_class, const RRType& expected_type,
  184. const RRTTL& expected_rrttl, const vector<string>& expected_data,
  185. const vector<string>* expected_sig_data)
  186. {
  187. EXPECT_EQ(expected_name, rrset->getName());
  188. EXPECT_EQ(expected_class, rrset->getClass());
  189. EXPECT_EQ(expected_type, rrset->getType());
  190. EXPECT_EQ(expected_rrttl, rrset->getTTL());
  191. RdataIteratorPtr rdata_iterator = rrset->getRdataIterator();
  192. rdata_iterator->first();
  193. vector<string>::const_iterator data_it = expected_data.begin();
  194. for (; data_it != expected_data.end(); ++data_it) {
  195. EXPECT_FALSE(rdata_iterator->isLast());
  196. if (rdata_iterator->isLast()) {
  197. // buggy case, should stop here
  198. break;
  199. }
  200. // We use text-based comparison so that we can easily identify which
  201. // data causes the error. RDATA::compare() is the most strict
  202. // comparison method, but in this case text-based comparison should
  203. // be okay because we generate the text data from Rdata objects
  204. // rather than hand-write the expected text.
  205. EXPECT_EQ(createRdata(expected_type, expected_class,
  206. *data_it)->toText(),
  207. rdata_iterator->getCurrent().toText());
  208. rdata_iterator->next();
  209. }
  210. if (expected_sig_data != NULL) {
  211. RRsetPtr sig_rrset = rrset->getRRsig();
  212. EXPECT_FALSE(NULL == sig_rrset);
  213. // Note: we assume the TTL for RRSIG is the same as that of the
  214. // RRSIG target.
  215. checkRRset(sig_rrset, expected_name, expected_class, RRType::RRSIG(),
  216. expected_rrttl, *expected_sig_data, NULL);
  217. } else {
  218. EXPECT_TRUE(NULL == rrset->getRRsig());
  219. }
  220. EXPECT_TRUE(rdata_iterator->isLast());
  221. }
  222. void
  223. checkFind(FindMode mode, const Sqlite3DataSrc& data_source, const Query& query,
  224. const Name& expected_name, const Name* zone_name,
  225. const RRClass& expected_class, const RRType& expected_type,
  226. const vector<RRTTL>& expected_ttls, const uint32_t expected_flags,
  227. const vector<RRType>& expected_types,
  228. const vector<const vector<string>* >& expected_answers,
  229. const vector<const vector<string>* >& expected_signatures)
  230. {
  231. RRsetList result_sets;
  232. uint32_t find_flags;
  233. unsigned int rrset_matched = 0;
  234. unsigned int rrset_count = 0;
  235. switch (mode) {
  236. case NORMAL:
  237. EXPECT_EQ(DataSrc::SUCCESS,
  238. data_source.findRRset(query, expected_name, expected_class,
  239. expected_type, result_sets, find_flags,
  240. zone_name));
  241. break;
  242. case EXACT:
  243. EXPECT_EQ(DataSrc::SUCCESS,
  244. data_source.findExactRRset(query, expected_name,
  245. expected_class, expected_type,
  246. result_sets, find_flags,
  247. zone_name));
  248. break;
  249. case ADDRESS:
  250. EXPECT_EQ(DataSrc::SUCCESS,
  251. data_source.findAddrs(query, expected_name, expected_class,
  252. result_sets, find_flags, zone_name));
  253. break;
  254. case REFERRAL:
  255. EXPECT_EQ(DataSrc::SUCCESS,
  256. data_source.findReferral(query, expected_name, expected_class,
  257. result_sets, find_flags, zone_name));
  258. break;
  259. }
  260. EXPECT_EQ(expected_flags, find_flags);
  261. RRsetList::iterator it = result_sets.begin();
  262. for (; it != result_sets.end(); ++it) {
  263. vector<RRType>::const_iterator found_type =
  264. find(expected_types.begin(), expected_types.end(),
  265. (*it)->getType());
  266. // there should be a match
  267. EXPECT_TRUE(found_type != expected_types.end());
  268. if (found_type != expected_types.end()) {
  269. unsigned int index = distance(expected_types.begin(), found_type);
  270. checkRRset(*it, expected_name, expected_class, (*it)->getType(),
  271. expected_ttls[index], *expected_answers[index],
  272. expected_signatures[index]);
  273. ++rrset_matched;
  274. }
  275. ++rrset_count;
  276. }
  277. EXPECT_EQ(expected_types.size(), rrset_count);
  278. EXPECT_EQ(expected_types.size(), rrset_matched);
  279. }
  280. void
  281. checkFind(FindMode mode, const Sqlite3DataSrc& data_source, const Query& query,
  282. const Name& expected_name, const Name* zone_name,
  283. const RRClass& expected_class, const RRType& expected_type,
  284. const RRTTL& expected_rrttl, const uint32_t expected_flags,
  285. const vector<string>& expected_data,
  286. const vector<string>* expected_sig_data)
  287. {
  288. vector<RRType> types;
  289. vector<RRTTL> ttls;
  290. vector<const vector<string>* > answers;
  291. vector<const vector<string>* > signatures;
  292. types.push_back(expected_type);
  293. ttls.push_back(expected_rrttl);
  294. answers.push_back(&expected_data);
  295. signatures.push_back(expected_sig_data);
  296. checkFind(mode, data_source, query, expected_name, zone_name, expected_class,
  297. expected_type, ttls, expected_flags, types, answers,
  298. signatures);
  299. }
  300. TEST_F(Sqlite3DataSourceTest, close) {
  301. EXPECT_EQ(DataSrc::SUCCESS, data_source.close());
  302. }
  303. TEST_F(Sqlite3DataSourceTest, reOpen) {
  304. // Replace the data with a totally different zone. This should succeed,
  305. // and shouldn't match any names in the previously managed domains.
  306. EXPECT_EQ(DataSrc::SUCCESS, data_source.close());
  307. EXPECT_EQ(DataSrc::SUCCESS, data_source.init(SQLITE_DBFILE_EXAMPLE2));
  308. NameMatch name_match(www_name);
  309. data_source.findClosestEnclosure(name_match);
  310. EXPECT_EQ(NULL, name_match.closestName());
  311. EXPECT_EQ(NULL, name_match.bestDataSrc());
  312. }
  313. TEST_F(Sqlite3DataSourceTest, findClosestEnclosure) {
  314. NameMatch name_match(www_name);
  315. data_source.findClosestEnclosure(name_match);
  316. EXPECT_EQ(zone_name, *name_match.closestName());
  317. EXPECT_EQ(&data_source, name_match.bestDataSrc());
  318. }
  319. TEST_F(Sqlite3DataSourceTest, findClosestEnclosureAtDelegation) {
  320. // The search name exists both in the parent and child zones, but
  321. // child has a better match.
  322. NameMatch name_match(child_name);
  323. data_source.findClosestEnclosure(name_match);
  324. EXPECT_EQ(child_name, *name_match.closestName());
  325. EXPECT_EQ(&data_source, name_match.bestDataSrc());
  326. }
  327. TEST_F(Sqlite3DataSourceTest, findClosestEnclosureNoMatch) {
  328. NameMatch name_match(nomatch_name);
  329. data_source.findClosestEnclosure(name_match);
  330. EXPECT_EQ(NULL, name_match.closestName());
  331. EXPECT_EQ(NULL, name_match.bestDataSrc());
  332. }
  333. TEST_F(Sqlite3DataSourceTest, findRRsetNormal) {
  334. // Without specifying the zone name, and then with the zone name
  335. checkFind(NORMAL, data_source, *query, www_name, NULL, rrclass, rrtype,
  336. rrttl, 0, common_a_data, &common_sig_data);
  337. checkFind(NORMAL, data_source, *query, www_name, &zone_name, rrclass,
  338. rrtype, rrttl, 0, common_a_data, &common_sig_data);
  339. // With a zone name that doesn't match
  340. EXPECT_EQ(DataSrc::SUCCESS,
  341. data_source.findRRset(*query, www_name, rrclass, rrtype,
  342. result_sets, find_flags, &nomatch_name));
  343. EXPECT_EQ(DataSrc::NO_SUCH_ZONE, find_flags);
  344. EXPECT_TRUE(result_sets.begin() == result_sets.end()); // should be empty
  345. }
  346. TEST_F(Sqlite3DataSourceTest, findRRsetNormalANY) {
  347. types.push_back(RRType::A());
  348. types.push_back(RRType::NSEC());
  349. ttls.push_back(RRTTL(3600));
  350. ttls.push_back(RRTTL(7200));
  351. answers.push_back(&common_a_data);
  352. answers.push_back(&www_nsec_data);
  353. signatures.push_back(&common_sig_data);
  354. signatures.push_back(&www_nsec_sig_data);
  355. rrtype = RRType::ANY();
  356. checkFind(NORMAL, data_source, *query, www_name, NULL, rrclass, rrtype,
  357. ttls, 0, types, answers, signatures);
  358. checkFind(NORMAL, data_source, *query, www_name, &zone_name, rrclass,
  359. rrtype, ttls, 0, types, answers, signatures);
  360. }
  361. // Case insensitive lookup
  362. TEST_F(Sqlite3DataSourceTest, findRRsetNormalCase) {
  363. checkFind(NORMAL, data_source, *query, www_upper_name, NULL, rrclass,
  364. rrtype, rrttl, 0, common_a_data, &common_sig_data);
  365. checkFind(NORMAL, data_source, *query, www_upper_name, &zone_name, rrclass,
  366. rrtype, rrttl, 0, common_a_data, &common_sig_data);
  367. EXPECT_EQ(DataSrc::SUCCESS,
  368. data_source.findRRset(*query, www_upper_name, rrclass, rrtype,
  369. result_sets, find_flags, &nomatch_name));
  370. EXPECT_EQ(DataSrc::NO_SUCH_ZONE, find_flags);
  371. EXPECT_TRUE(result_sets.begin() == result_sets.end()); // should be empty
  372. }
  373. TEST_F(Sqlite3DataSourceTest, findRRsetApexNS) {
  374. rrtype = RRType::NS();
  375. checkFind(NORMAL, data_source, *query, zone_name, NULL, rrclass, rrtype,
  376. rrttl, DataSrc::REFERRAL, apex_ns_data, &apex_ns_sig_data);
  377. checkFind(NORMAL, data_source, *query, zone_name, &zone_name, rrclass,
  378. rrtype, rrttl, DataSrc::REFERRAL, apex_ns_data,
  379. &apex_ns_sig_data);
  380. EXPECT_EQ(DataSrc::SUCCESS,
  381. data_source.findRRset(*query, zone_name, rrclass, rrtype,
  382. result_sets, find_flags, &nomatch_name));
  383. EXPECT_EQ(DataSrc::NO_SUCH_ZONE, find_flags);
  384. EXPECT_TRUE(result_sets.begin() == result_sets.end()); // should be empty
  385. }
  386. TEST_F(Sqlite3DataSourceTest, findRRsetApexANY) {
  387. types.push_back(RRType::SOA());
  388. types.push_back(RRType::NS());
  389. types.push_back(RRType::MX());
  390. types.push_back(RRType::NSEC());
  391. types.push_back(RRType::DNSKEY());
  392. ttls.push_back(rrttl); // SOA TTL
  393. ttls.push_back(rrttl); // NS TTL
  394. ttls.push_back(rrttl); // MX TTL
  395. ttls.push_back(RRTTL(7200)); // NSEC TTL
  396. ttls.push_back(rrttl); // DNSKEY TTL
  397. answers.push_back(&apex_soa_data);
  398. answers.push_back(&apex_ns_data);
  399. answers.push_back(&apex_mx_data);
  400. answers.push_back(&apex_nsec_data);
  401. answers.push_back(&apex_dnskey_data);
  402. signatures.push_back(&apex_soa_sig_data);
  403. signatures.push_back(&apex_ns_sig_data);
  404. signatures.push_back(&apex_mx_sig_data);
  405. signatures.push_back(&apex_nsec_sig_data);
  406. signatures.push_back(&apex_dnskey_sig_data);
  407. rrtype = RRType::ANY();
  408. checkFind(NORMAL, data_source, *query, zone_name, NULL, rrclass, rrtype,
  409. ttls, 0, types, answers, signatures);
  410. checkFind(NORMAL, data_source, *query, zone_name, &zone_name, rrclass,
  411. rrtype, ttls, 0, types, answers, signatures);
  412. }
  413. TEST_F(Sqlite3DataSourceTest, findRRsetMixedANY) {
  414. // ANY query for mixed order RRs
  415. const Name qname("mix.example.com");
  416. types.push_back(RRType::A());
  417. types.push_back(RRType::AAAA());
  418. ttls.push_back(rrttl);
  419. ttls.push_back(rrttl);
  420. answers.push_back(&mix_a_data);
  421. answers.push_back(&mix_aaaa_data);
  422. signatures.push_back(NULL);
  423. signatures.push_back(NULL);
  424. rrtype = RRType::ANY();
  425. checkFind(NORMAL, data_source, *query, qname, &zone_name, rrclass,
  426. rrtype, ttls, 0, types, answers, signatures);
  427. }
  428. TEST_F(Sqlite3DataSourceTest, findRRsetApexNXRRSET) {
  429. rrtype = RRType::AAAA();
  430. EXPECT_EQ(DataSrc::SUCCESS,
  431. data_source.findRRset(*query, zone_name, rrclass, rrtype,
  432. result_sets, find_flags, &zone_name));
  433. // there's an NS RRset at the apex name, so the REFERRAL flag should be
  434. // set, too.
  435. EXPECT_EQ(DataSrc::TYPE_NOT_FOUND | DataSrc::REFERRAL, find_flags);
  436. EXPECT_TRUE(result_sets.begin() == result_sets.end());
  437. // Same test, without specifying the zone name
  438. EXPECT_EQ(DataSrc::SUCCESS,
  439. data_source.findRRset(*query, zone_name, rrclass, rrtype,
  440. result_sets, find_flags, NULL));
  441. // there's an NS RRset at the apex name, so the REFERRAL flag should be
  442. // set, too.
  443. EXPECT_EQ(DataSrc::TYPE_NOT_FOUND | DataSrc::REFERRAL, find_flags);
  444. EXPECT_TRUE(result_sets.begin() == result_sets.end());
  445. }
  446. // Matching a wildcard node. There's nothing special for the data source API
  447. // point of view, but perform minimal tests anyway.
  448. TEST_F(Sqlite3DataSourceTest, findRRsetWildcard) {
  449. const Name qname("*.wild.example.com");
  450. checkFind(NORMAL, data_source, *query, qname, NULL, rrclass,
  451. rrtype, rrttl, 0, wild_a_data, &common_sig_data);
  452. checkFind(NORMAL, data_source, *query, qname, &zone_name, rrclass,
  453. rrtype, rrttl, 0, wild_a_data, &common_sig_data);
  454. }
  455. TEST_F(Sqlite3DataSourceTest, findRRsetEmptyNode) {
  456. // foo.bar.example.com exists, but bar.example.com doesn't have any data.
  457. const Name qname("bar.example.com");
  458. EXPECT_EQ(DataSrc::SUCCESS,
  459. data_source.findRRset(*query, qname, rrclass, rrtype,
  460. result_sets, find_flags, NULL));
  461. EXPECT_EQ(DataSrc::TYPE_NOT_FOUND, find_flags);
  462. EXPECT_TRUE(result_sets.begin() == result_sets.end());
  463. EXPECT_EQ(DataSrc::SUCCESS,
  464. data_source.findRRset(*query, qname, rrclass, rrtype,
  465. result_sets, find_flags, &zone_name));
  466. EXPECT_EQ(DataSrc::TYPE_NOT_FOUND, find_flags);
  467. EXPECT_TRUE(result_sets.begin() == result_sets.end());
  468. }
  469. // There's nothing special about DNAME lookup for the data source API
  470. // point of view, but perform minimal tests anyway.
  471. TEST_F(Sqlite3DataSourceTest, findRRsetDNAME) {
  472. const Name qname("dname.example.com");
  473. rrtype = RRType::DNAME();
  474. checkFind(NORMAL, data_source, *query, qname, NULL, rrclass,
  475. rrtype, rrttl, 0, dname_data, &dname_sig_data);
  476. checkFind(NORMAL, data_source, *query, qname, &zone_name, rrclass,
  477. rrtype, rrttl, 0, dname_data, &dname_sig_data);
  478. }
  479. TEST_F(Sqlite3DataSourceTest, findRRsetCNAME) {
  480. const Name qname("foo.example.com");
  481. // This qname only has the CNAME (+ sigs). CNAME query is not different
  482. // from ordinary queries.
  483. rrtype = RRType::CNAME();
  484. checkFind(NORMAL, data_source, *query, qname, NULL, rrclass,
  485. rrtype, rrttl, 0, cname_data, &cname_sig_data);
  486. checkFind(NORMAL, data_source, *query, qname, &zone_name, rrclass,
  487. rrtype, rrttl, 0, cname_data, &cname_sig_data);
  488. // queries for (ordinary) different RR types that match the CNAME.
  489. // CNAME_FOUND flag is set, and the CNAME RR is returned instead of A
  490. rrtype = RRType::A();
  491. types.push_back(RRType::CNAME());
  492. ttls.push_back(rrttl);
  493. answers.push_back(&cname_data);
  494. signatures.push_back(&cname_sig_data);
  495. checkFind(NORMAL, data_source, *query, qname, NULL, rrclass,
  496. rrtype, ttls, DataSrc::CNAME_FOUND, types, answers, signatures);
  497. checkFind(NORMAL, data_source, *query, qname, &zone_name, rrclass,
  498. rrtype, ttls, DataSrc::CNAME_FOUND, types, answers, signatures);
  499. // NSEC query that match the CNAME.
  500. // CNAME_FOUND flag is NOT set, and the NSEC RR is returned instead of
  501. // CNAME.
  502. rrtype = RRType::NSEC();
  503. checkFind(NORMAL, data_source, *query, qname, NULL, rrclass,
  504. rrtype, RRTTL(7200), 0, cname_nsec_data, &cname_nsec_sig_data);
  505. checkFind(NORMAL, data_source, *query, qname, &zone_name, rrclass,
  506. rrtype, RRTTL(7200), 0, cname_nsec_data, &cname_nsec_sig_data);
  507. }
  508. TEST_F(Sqlite3DataSourceTest, findRRsetDelegation) {
  509. const Name qname("www.subzone.example.com");
  510. // query for a name under a zone cut. From the data source API point
  511. // of view this is no different than "NXDOMAIN".
  512. EXPECT_EQ(DataSrc::SUCCESS,
  513. data_source.findRRset(*query, qname, rrclass, rrtype,
  514. result_sets, find_flags, NULL));
  515. EXPECT_EQ(DataSrc::NAME_NOT_FOUND, find_flags);
  516. EXPECT_TRUE(result_sets.begin() == result_sets.end());
  517. }
  518. TEST_F(Sqlite3DataSourceTest, findRRsetDelegationAtZoneCut) {
  519. const Name qname("subzone.example.com");
  520. // query for a name *at* a zone cut. It matches the NS RRset for the
  521. // delegation.
  522. // For non-NS ordinary queries, "no type" should be set too, and no RRset is
  523. // returned.
  524. EXPECT_EQ(DataSrc::SUCCESS,
  525. data_source.findRRset(*query, qname, rrclass, rrtype,
  526. result_sets, find_flags, NULL));
  527. EXPECT_EQ(DataSrc::TYPE_NOT_FOUND | DataSrc::REFERRAL, find_flags);
  528. EXPECT_TRUE(result_sets.begin() == result_sets.end());
  529. EXPECT_EQ(DataSrc::SUCCESS,
  530. data_source.findRRset(*query, qname, rrclass, rrtype,
  531. result_sets, find_flags, &zone_name));
  532. EXPECT_EQ(DataSrc::TYPE_NOT_FOUND | DataSrc::REFERRAL, find_flags);
  533. EXPECT_TRUE(result_sets.begin() == result_sets.end());
  534. // For NS query, RRset is returned with the REFERRAL flag. No RRSIG should
  535. // be provided.
  536. rrtype = RRType::NS();
  537. checkFind(NORMAL, data_source, *query, qname, NULL, rrclass,
  538. rrtype, rrttl, DataSrc::REFERRAL, delegation_ns_data, NULL);
  539. checkFind(NORMAL, data_source, *query, qname, &zone_name, rrclass,
  540. rrtype, rrttl, DataSrc::REFERRAL, delegation_ns_data, NULL);
  541. // For ANY query. What should we do?
  542. #if 0
  543. rrtype = RRType::ANY();
  544. EXPECT_EQ(DataSrc::SUCCESS,
  545. data_source.findRRset(*query, qname, rrclass, rrtype,
  546. result_sets, find_flags, NULL));
  547. EXPECT_EQ(DataSrc::REFERRAL, find_flags);
  548. #endif
  549. // For NSEC query. What should we do? Probably return the NSEC + RRSIG
  550. // without REFERRAL. But it currently doesn't act like so.
  551. #if 0
  552. rrtype = RRType::NSEC();
  553. checkFind(NORMAL, data_source, *query, qname, NULL, rrclass,
  554. rrtype, RRTTL(7200), 0, delegation_nsec_data,
  555. &delegation_nsec_sig_data);
  556. checkFind(NORMAL, data_source, *query, qname, &zone_name, rrclass,
  557. rrtype, RRTTL(7200), 0, delegation_nsec_data,
  558. &delegation_nsec_sig_data);
  559. #endif
  560. }
  561. TEST_F(Sqlite3DataSourceTest, findRRsetInChildZone) {
  562. const Name qname("www.sql1.example.com");
  563. const Name child_zone_name("sql1.example.com");
  564. // If we don't specify the zone, the data source should identify the
  565. // best matching zone.
  566. checkFind(NORMAL, data_source, *query, qname, NULL, rrclass,
  567. rrtype, rrttl, 0, child_a_data, &child_sig_data);
  568. // If we specify the parent zone, it's treated as NXDOMAIN because it's
  569. // under a zone cut.
  570. EXPECT_EQ(DataSrc::SUCCESS,
  571. data_source.findRRset(*query, qname, rrclass, rrtype,
  572. result_sets, find_flags, &zone_name));
  573. EXPECT_EQ(DataSrc::NAME_NOT_FOUND, find_flags);
  574. EXPECT_TRUE(result_sets.begin() == result_sets.end());
  575. // If we specify the child zone, it should be the same as the first case.
  576. checkFind(NORMAL, data_source, *query, qname, &child_zone_name, rrclass,
  577. rrtype, rrttl, 0, child_a_data, &child_sig_data);
  578. }
  579. TEST_F(Sqlite3DataSourceTest, findExactRRset) {
  580. // Normal case. No different than findRRset.
  581. checkFind(EXACT, data_source, *query, www_name, &zone_name, rrclass, rrtype,
  582. rrttl, 0, common_a_data, &common_sig_data);
  583. }
  584. TEST_F(Sqlite3DataSourceTest, findExactRRsetCNAME) {
  585. const Name qname("foo.example.com");
  586. // This qname only has the CNAME (+ sigs). In this case it should be
  587. // no different than findRRset for CNAME query.
  588. rrtype = RRType::CNAME();
  589. checkFind(NORMAL, data_source, *query, qname, &zone_name, rrclass,
  590. rrtype, rrttl, 0, cname_data, &cname_sig_data);
  591. // queries for (ordinary) different RR types that match the CNAME.
  592. // "type not found" set, but CNAME and its sig are returned (awkward,
  593. // but that's how it currently works).
  594. rrtype = RRType::A();
  595. types.push_back(RRType::CNAME());
  596. ttls.push_back(rrttl);
  597. answers.push_back(&cname_data);
  598. signatures.push_back(&cname_sig_data);
  599. checkFind(EXACT, data_source, *query, qname, &zone_name, rrclass,
  600. rrtype, ttls, DataSrc::TYPE_NOT_FOUND, types, answers,
  601. signatures);
  602. }
  603. #if 0 // this should work, but doesn't. maybe the loadzone tool is broken?
  604. TEST_F(Sqlite3DataSourceTest, findReferralRRset) {
  605. // A referral lookup searches for NS, DS, or DNAME, or their sigs.
  606. const Name qname("sql1.example.com");
  607. types.push_back(RRType::NS());
  608. types.push_back(RRType::DS());
  609. ttls.push_back(rrttl);
  610. ttls.push_back(rrttl);
  611. answers.push_back(&apex_ns_data);
  612. answers.push_back(&delegation_ds_data);
  613. signatures.push_back(NULL);
  614. signatures.push_back(&delegation_ds_sig_data);
  615. // Note: zone_name matters here because we need to perform the search
  616. // in the parent zone.
  617. checkFind(REFERRAL, data_source, *query, qname, &zone_name, rrclass,
  618. rrtype, ttls, 0, types, answers, signatures);
  619. }
  620. #endif
  621. TEST_F(Sqlite3DataSourceTest, findReferralRRsetDNAME) {
  622. // same as above. the DNAME case.
  623. const Name qname("dname.example.com");
  624. checkFind(REFERRAL, data_source, *query, qname, &zone_name, rrclass,
  625. RRType::DNAME(), rrttl, 0, dname_data, &dname_sig_data);
  626. }
  627. TEST_F(Sqlite3DataSourceTest, findReferralRRsetFail) {
  628. // qname has not "referral" records. the result should be "empty".
  629. EXPECT_EQ(DataSrc::SUCCESS,
  630. data_source.findReferral(*query, www_name, rrclass,
  631. result_sets, find_flags, &zone_name));
  632. EXPECT_EQ(DataSrc::TYPE_NOT_FOUND, find_flags);
  633. EXPECT_TRUE(result_sets.begin() == result_sets.end());
  634. }
  635. TEST_F(Sqlite3DataSourceTest, findAddressRRset) {
  636. // A referral lookup searches for A or AAAA, or their sigs.
  637. // A-only case
  638. checkFind(ADDRESS, data_source, *query, www_name, &zone_name, rrclass,
  639. rrtype, rrttl, 0, common_a_data, &common_sig_data);
  640. // AAAA-only case
  641. checkFind(ADDRESS, data_source, *query, Name("ip6.example.com"), &zone_name,
  642. rrclass, RRType::AAAA(), rrttl, 0, common_aaaa_data,
  643. &common_aaaa_sig_data);
  644. // Dual-stack
  645. types.push_back(RRType::A());
  646. ttls.push_back(rrttl);
  647. answers.push_back(&common_a_data);
  648. signatures.push_back(&common_sig_data);
  649. types.push_back(RRType::AAAA());
  650. ttls.push_back(rrttl);
  651. answers.push_back(&common_aaaa_data);
  652. signatures.push_back(&common_aaaa_sig_data);
  653. checkFind(ADDRESS, data_source, *query, Name("ip46.example.com"),
  654. &zone_name, rrclass, rrtype, ttls, 0, types, answers, signatures);
  655. // The qname doesn't have no address records.
  656. EXPECT_EQ(DataSrc::SUCCESS,
  657. data_source.findAddrs(*query, Name("text.example.com"), rrclass,
  658. result_sets, find_flags, &zone_name));
  659. EXPECT_EQ(DataSrc::TYPE_NOT_FOUND, find_flags);
  660. EXPECT_TRUE(result_sets.begin() == result_sets.end());
  661. }
  662. }