zone_finder_context_unittest.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <dns/masterload.h>
  15. #include <dns/name.h>
  16. #include <dns/rrclass.h>
  17. #include <datasrc/zone.h>
  18. #include <datasrc/memory_datasrc.h>
  19. #include <datasrc/database.h>
  20. #include <datasrc/sqlite3_accessor.h>
  21. #include <testutils/dnsmessage_test.h>
  22. #include <gtest/gtest.h>
  23. #include <boost/bind.hpp>
  24. #include <boost/foreach.hpp>
  25. #include <boost/shared_ptr.hpp>
  26. #include <cstdlib>
  27. #include <vector>
  28. using namespace std;
  29. using boost::shared_ptr;
  30. using namespace isc::dns;
  31. using namespace isc::datasrc;
  32. using namespace isc::testutils;
  33. namespace {
  34. // Commonly used test zone file.
  35. const char* const TEST_ZONE_FILE = TEST_DATA_DIR "/contexttest.zone";
  36. // Convenient shortcut
  37. typedef shared_ptr<DataSourceClient> DataSourceClientPtr;
  38. // This is the type used as the test parameter. Note that this is
  39. // intentionally a plain old type (i.e. a function pointer), not a class;
  40. // otherwise it could cause initialization fiasco at the instantiation time.
  41. typedef DataSourceClientPtr (*ClientCreator)(RRClass, const Name&);
  42. // Creator for the in-memory client to be tested
  43. DataSourceClientPtr
  44. createInMemoryClient(RRClass zclass, const Name& zname) {
  45. shared_ptr<InMemoryClient> client(new InMemoryClient);
  46. shared_ptr<InMemoryZoneFinder> finder(
  47. new InMemoryZoneFinder(zclass, zname));
  48. finder->load(TEST_ZONE_FILE);
  49. client->addZone(finder);
  50. return (client);
  51. }
  52. // Creator for the SQLite3 client to be tested. addRRset() is a helper
  53. // subroutine.
  54. void
  55. addRRset(ZoneUpdaterPtr updater, ConstRRsetPtr rrset) {
  56. updater->addRRset(*rrset);
  57. }
  58. DataSourceClientPtr
  59. createSQLite3Client(RRClass zclass, const Name& zname) {
  60. // We always begin with an empty template SQLite3 DB file and install
  61. // the zone data from the zone file to ensure both cases have the
  62. // same test data.
  63. const char* const install_cmd = INSTALL_PROG " " TEST_DATA_DIR
  64. "/rwtest.sqlite3 " TEST_DATA_BUILDDIR "/contexttest.sqlite3.copied";
  65. if (system(install_cmd) != 0) {
  66. isc_throw(isc::Unexpected,
  67. "Error setting up; command failed: " << install_cmd);
  68. }
  69. shared_ptr<SQLite3Accessor> accessor(
  70. new SQLite3Accessor(TEST_DATA_BUILDDIR "/contexttest.sqlite3.copied",
  71. zclass.toText()));
  72. shared_ptr<DatabaseClient> client(new DatabaseClient(zclass, accessor));
  73. ZoneUpdaterPtr updater = client->getUpdater(zname, true);
  74. masterLoad(TEST_ZONE_FILE, zname, zclass, boost::bind(addRRset, updater,
  75. _1));
  76. // Insert an out-of-zone name to test if it's incorrectly returned.
  77. // Note that neither updater nor SQLite3 accessor checks this condition,
  78. // so this should succeed.
  79. stringstream ss("ns.example.com. 3600 IN A 192.0.2.7");
  80. masterLoad(ss, Name::ROOT_NAME(), zclass,
  81. boost::bind(addRRset, updater, _1));
  82. updater->commit();
  83. return (client);
  84. }
  85. // The test class. Its parameterized so we can share the test scnearios
  86. // for any concrete data source implementaitons.
  87. class ZoneFinderContextTest :
  88. public ::testing::TestWithParam<ClientCreator>
  89. {
  90. protected:
  91. ZoneFinderContextTest() : qclass_(RRClass::IN()), qzone_("example.org") {
  92. client_ = (*GetParam())(qclass_, qzone_);
  93. REQUESTED_A.push_back(RRType::A());
  94. REQUESTED_AAAA.push_back(RRType::AAAA());
  95. REQUESTED_BOTH.push_back(RRType::A());
  96. REQUESTED_BOTH.push_back(RRType::AAAA());
  97. }
  98. void SetUp() {
  99. finder_ = client_->findZone(qzone_).zone_finder;
  100. ASSERT_TRUE(finder_);
  101. }
  102. const RRClass qclass_;
  103. const Name qzone_;
  104. DataSourceClientPtr client_;
  105. ZoneFinderPtr finder_;
  106. vector<RRType> requested_types_;
  107. vector<RRType> REQUESTED_A;
  108. vector<RRType> REQUESTED_AAAA;
  109. vector<RRType> REQUESTED_BOTH;
  110. vector<ConstRRsetPtr> result_sets_;
  111. };
  112. // We test the in-memory and SQLite3 data source implementations.
  113. INSTANTIATE_TEST_CASE_P(, ZoneFinderContextTest,
  114. ::testing::Values(createInMemoryClient,
  115. createSQLite3Client));
  116. TEST_P(ZoneFinderContextTest, getAdditionalAuthNS) {
  117. ZoneFinderContextPtr ctx = finder_->find(qzone_, RRType::NS());
  118. EXPECT_EQ(ZoneFinder::SUCCESS, ctx->code);
  119. // Getting both A and AAAA NS addresses
  120. ctx->getAdditional(REQUESTED_BOTH, result_sets_);
  121. rrsetsCheck("ns1.example.org. 3600 IN A 192.0.2.1\n"
  122. "ns1.example.org. 3600 IN AAAA 2001:db8::1\n"
  123. "ns2.example.org. 3600 IN A 192.0.2.2\n",
  124. result_sets_.begin(), result_sets_.end());
  125. // Getting only A
  126. result_sets_.clear();
  127. ctx->getAdditional(REQUESTED_A, result_sets_);
  128. rrsetsCheck("ns1.example.org. 3600 IN A 192.0.2.1\n"
  129. "ns2.example.org. 3600 IN A 192.0.2.2\n",
  130. result_sets_.begin(), result_sets_.end());
  131. // Getting only AAAA
  132. result_sets_.clear();
  133. ctx->getAdditional(REQUESTED_AAAA, result_sets_);
  134. rrsetsCheck("ns1.example.org. 3600 IN AAAA 2001:db8::1\n",
  135. result_sets_.begin(), result_sets_.end());
  136. // Normally expected type set contain only A and/or AAAA, but others aren't
  137. // excluded.
  138. result_sets_.clear();
  139. requested_types_.push_back(RRType::TXT());
  140. ctx->getAdditional(requested_types_, result_sets_);
  141. rrsetsCheck("ns2.example.org. 3600 IN TXT \"text data\"",
  142. result_sets_.begin(), result_sets_.end());
  143. // Even empty set is okay. The result should also be empty.
  144. result_sets_.clear();
  145. ctx->getAdditional(vector<RRType>(), result_sets_);
  146. EXPECT_TRUE(result_sets_.empty());
  147. }
  148. TEST_P(ZoneFinderContextTest, getAdditionalDelegation) {
  149. // Basically similar to the AuthNS case, but NS names are glues.
  150. // It contains an out-of-zone NS name. Its address (even if it's somehow
  151. // inserted to the zone data) shouldn't be returned.
  152. const Name qname("www.a.example.org");
  153. ZoneFinderContextPtr ctx = finder_->find(qname, RRType::AAAA());
  154. EXPECT_EQ(ZoneFinder::DELEGATION, ctx->code);
  155. ctx->getAdditional(REQUESTED_BOTH, result_sets_);
  156. rrsetsCheck("ns1.a.example.org. 3600 IN A 192.0.2.5\n"
  157. "ns2.a.example.org. 3600 IN A 192.0.2.6\n"
  158. "ns2.a.example.org. 3600 IN AAAA 2001:db8::6\n",
  159. result_sets_.begin(), result_sets_.end());
  160. result_sets_.clear();
  161. ctx->getAdditional(REQUESTED_A, result_sets_);
  162. rrsetsCheck("ns1.a.example.org. 3600 IN A 192.0.2.5\n"
  163. "ns2.a.example.org. 3600 IN A 192.0.2.6\n",
  164. result_sets_.begin(), result_sets_.end());
  165. result_sets_.clear();
  166. ctx->getAdditional(REQUESTED_AAAA, result_sets_);
  167. rrsetsCheck("ns2.a.example.org. 3600 IN AAAA 2001:db8::6\n",
  168. result_sets_.begin(), result_sets_.end());
  169. }
  170. TEST_P(ZoneFinderContextTest, getAdditionalMX) {
  171. // Similar to the previous cases, but for MX addresses. The test zone
  172. // contains MX name under a zone cut. Its address shouldn't be returned.
  173. ZoneFinderContextPtr ctx = finder_->find(qzone_, RRType::MX());
  174. EXPECT_EQ(ZoneFinder::SUCCESS, ctx->code);
  175. // Getting both A and AAAA NS addresses
  176. ctx->getAdditional(REQUESTED_BOTH, result_sets_);
  177. rrsetsCheck("mx1.example.org. 3600 IN A 192.0.2.10\n"
  178. "mx2.example.org. 3600 IN AAAA 2001:db8::10\n",
  179. result_sets_.begin(), result_sets_.end());
  180. // Getting only A
  181. result_sets_.clear();
  182. ctx->getAdditional(REQUESTED_A, result_sets_);
  183. rrsetsCheck("mx1.example.org. 3600 IN A 192.0.2.10\n",
  184. result_sets_.begin(), result_sets_.end());
  185. // Getting only AAAA
  186. result_sets_.clear();
  187. ctx->getAdditional(REQUESTED_AAAA, result_sets_);
  188. rrsetsCheck("mx2.example.org. 3600 IN AAAA 2001:db8::10\n",
  189. result_sets_.begin(), result_sets_.end());
  190. }
  191. TEST_P(ZoneFinderContextTest, getAdditionalWithSIG) {
  192. // Similar to the AuthNS test, but the original find() requested DNSSEC
  193. // RRSIGs. Then additional records will also have RRSIGs.
  194. ZoneFinderContextPtr ctx = finder_->find(qzone_, RRType::NS(),
  195. ZoneFinder::FIND_DNSSEC);
  196. EXPECT_EQ(ZoneFinder::SUCCESS, ctx->code);
  197. ctx->getAdditional(REQUESTED_BOTH, result_sets_);
  198. rrsetsCheck("ns1.example.org. 3600 IN A 192.0.2.1\n"
  199. "ns1.example.org. 3600 IN AAAA 2001:db8::1\n"
  200. "ns2.example.org. 3600 IN A 192.0.2.2\n",
  201. result_sets_.begin(), result_sets_.end());
  202. vector<ConstRRsetPtr> sigresult_sets;
  203. BOOST_FOREACH(ConstRRsetPtr rrset, result_sets_) {
  204. ConstRRsetPtr sig_rrset = rrset->getRRsig();
  205. if (sig_rrset) {
  206. sigresult_sets.push_back(sig_rrset);
  207. }
  208. }
  209. rrsetsCheck("ns1.example.org. 3600 IN RRSIG A 7 3 3600 20150420235959 "
  210. "20051021000000 40430 example.org. FAKEFAKE\n"
  211. "ns1.example.org. 3600 IN RRSIG AAAA 7 3 3600 20150420235959 "
  212. "20051021000000 40430 example.org. FAKEFAKEFAKE\n",
  213. sigresult_sets.begin(), sigresult_sets.end());
  214. }
  215. TEST_P(ZoneFinderContextTest, getAdditionalNoOP) {
  216. // getAdditional() is only meaningful after SUCCESS or DELEGATION.
  217. ZoneFinderContextPtr ctx = finder_->find(Name("nxdomain.example.org"),
  218. RRType::NS());
  219. EXPECT_EQ(ZoneFinder::NXDOMAIN, ctx->code);
  220. ctx->getAdditional(REQUESTED_BOTH, result_sets_);
  221. EXPECT_TRUE(result_sets_.empty());
  222. ctx = finder_->find(qzone_, RRType::TXT());
  223. EXPECT_EQ(ZoneFinder::NXRRSET, ctx->code);
  224. ctx->getAdditional(REQUESTED_BOTH, result_sets_);
  225. EXPECT_TRUE(result_sets_.empty());
  226. ctx = finder_->find(Name("alias.example.org."), RRType::A());
  227. EXPECT_EQ(ZoneFinder::CNAME, ctx->code);
  228. ctx->getAdditional(REQUESTED_BOTH, result_sets_);
  229. EXPECT_TRUE(result_sets_.empty());
  230. ctx = finder_->find(Name("www.dname.example.org."), RRType::A());
  231. EXPECT_EQ(ZoneFinder::DNAME, ctx->code);
  232. ctx->getAdditional(REQUESTED_BOTH, result_sets_);
  233. EXPECT_TRUE(result_sets_.empty());
  234. }
  235. }