master_loader_unittest.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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/master_loader_callbacks.h>
  15. #include <dns/master_loader.h>
  16. #include <dns/rrtype.h>
  17. #include <dns/rrset.h>
  18. #include <dns/rrclass.h>
  19. #include <dns/name.h>
  20. #include <gtest/gtest.h>
  21. #include <boost/bind.hpp>
  22. #include <boost/scoped_ptr.hpp>
  23. #include <string>
  24. #include <vector>
  25. #include <list>
  26. #include <fstream>
  27. using namespace isc::dns;
  28. using std::vector;
  29. using std::string;
  30. using std::list;
  31. using std::ofstream;
  32. using std::endl;
  33. class MasterLoaderTest : public ::testing::Test {
  34. public:
  35. MasterLoaderTest() :
  36. callbacks_(boost::bind(&MasterLoaderTest::callback, this,
  37. true, _1, _2, _3),
  38. boost::bind(&MasterLoaderTest::callback, this,
  39. false, _1, _2, _3))
  40. {}
  41. /// Concatenate file, line, and reason, and add it to either errors
  42. /// or warnings
  43. void callback(bool error, const std::string& file, size_t line,
  44. const std::string reason)
  45. {
  46. std::stringstream ss;
  47. ss << reason << " [" << file << ":" << line << "]";
  48. if (error) {
  49. errors_.push_back(ss.str());
  50. } else {
  51. warnings_.push_back(ss.str());
  52. }
  53. }
  54. void addRRset(const Name& name, const RRClass& rrclass,
  55. const RRType& rrtype, const RRTTL& rrttl,
  56. const rdata::RdataPtr& data) {
  57. const RRsetPtr rrset(new BasicRRset(name, rrclass, rrtype, rrttl));
  58. rrset->addRdata(data);
  59. rrsets_.push_back(rrset);
  60. }
  61. void setLoader(const char* file, const Name& origin, const RRClass rrclass,
  62. const MasterLoader::Options options)
  63. {
  64. loader_.reset(new MasterLoader(file, origin, rrclass, callbacks_,
  65. boost::bind(&MasterLoaderTest::addRRset,
  66. this, _1, _2, _3, _4, _5),
  67. options));
  68. }
  69. void prepareBrokenZone(const string& filename, const string& line) {
  70. ofstream out(filename.c_str(),
  71. std::ios_base::out | std::ios_base::trunc);
  72. ASSERT_FALSE(out.fail());
  73. out << "example.org. 3600 IN SOA ns1.example.org.filename "
  74. "admin.example.org.filename 1234 3600 1800 2419200 7200" << endl;
  75. out << line << endl;
  76. out << "correct 3600 IN A 192.0.2.2" << endl;
  77. out.close();
  78. }
  79. void clear() {
  80. warnings_.clear();
  81. errors_.clear();
  82. rrsets_.clear();
  83. }
  84. // Check the next RR in the ones produced by the loader
  85. // Other than passed arguments are checked to be the default for the tests
  86. void checkRR(const string& name, const RRType& type, const string& data) {
  87. ASSERT_FALSE(rrsets_.empty());
  88. RRsetPtr current = rrsets_.front();
  89. rrsets_.pop_front();
  90. EXPECT_EQ(Name(name), current->getName());
  91. ASSERT_EQ(type, current->getType());
  92. ASSERT_EQ(1, current->getRdataCount());
  93. EXPECT_EQ(data, current->getRdataIterator()->getCurrent().toText());
  94. }
  95. MasterLoaderCallbacks callbacks_;
  96. boost::scoped_ptr<MasterLoader> loader_;
  97. vector<string> errors_;
  98. vector<string> warnings_;
  99. list<RRsetPtr> rrsets_;
  100. };
  101. // Test simple loading. The zone file contains no tricky things, and nothing is
  102. // omitted. No RRset contains more than one RR Also no errors or warnings.
  103. TEST_F(MasterLoaderTest, basicLoad) {
  104. setLoader(TEST_DATA_SRCDIR "/example.org", Name("example.org."),
  105. RRClass::IN(), MasterLoader::MANY_ERRORS);
  106. loader_->load();
  107. EXPECT_TRUE(errors_.empty());
  108. EXPECT_TRUE(warnings_.empty());
  109. checkRR("example.org", RRType::SOA(), "ns1.example.org. admin.example.org. "
  110. "1234 3600 1800 2419200 7200");
  111. checkRR("example.org", RRType::NS(), "ns1.example.org.");
  112. checkRR("www.example.org", RRType::A(), "192.0.2.1");
  113. }
  114. // Try loading data incrementally.
  115. TEST_F(MasterLoaderTest, incrementalLoad) {
  116. setLoader(TEST_DATA_SRCDIR "/example.org", Name("example.org."),
  117. RRClass::IN(), MasterLoader::MANY_ERRORS);
  118. EXPECT_FALSE(loader_->loadIncremental(2));
  119. EXPECT_TRUE(errors_.empty());
  120. EXPECT_TRUE(warnings_.empty());
  121. checkRR("example.org", RRType::SOA(), "ns1.example.org. admin.example.org. "
  122. "1234 3600 1800 2419200 7200");
  123. checkRR("example.org", RRType::NS(), "ns1.example.org.");
  124. // The third one is not loaded yet
  125. EXPECT_TRUE(rrsets_.empty());
  126. // Load the rest.
  127. EXPECT_TRUE(loader_->loadIncremental(20));
  128. EXPECT_TRUE(errors_.empty());
  129. EXPECT_TRUE(warnings_.empty());
  130. checkRR("www.example.org", RRType::A(), "192.0.2.1");
  131. }
  132. // Try loading from file that doesn't exist. There should be single error
  133. // saying so.
  134. TEST_F(MasterLoaderTest, invalidFile) {
  135. setLoader("This file doesn't exist at all",
  136. Name("exmaple.org."), RRClass::IN(), MasterLoader::MANY_ERRORS);
  137. // Nothing yet. The loader is dormant until invoked.
  138. // Is it really what we want?
  139. EXPECT_TRUE(errors_.empty());
  140. loader_->load();
  141. EXPECT_TRUE(warnings_.empty());
  142. EXPECT_TRUE(rrsets_.empty());
  143. ASSERT_EQ(1, errors_.size());
  144. EXPECT_EQ(0, errors_[0].find("Error opening the input source file: ")) <<
  145. "Different error: " << errors_[0];
  146. }
  147. struct ErrorCase {
  148. const char* line;
  149. const char* problem;
  150. } error_cases[] = {
  151. { "www... 3600 IN A 192.0.2.1", "Invalid name" },
  152. { "www FORTNIGHT IN A 192.0.2.1", "Invalid TTL" },
  153. { "www 3600 XX A 192.0.2.1", "Invalid class" },
  154. { "www 3600 IN A bad_ip", "Invalid Rdata" },
  155. { "www 3600 IN", "Unexpected EOLN" },
  156. { "www 3600 CH TXT nothing", "Class mismatch" },
  157. { NULL, NULL }
  158. };
  159. // Test a broken zone is handled properly. We test several problems,
  160. // both in strict and lenient mode.
  161. TEST_F(MasterLoaderTest, brokenZone) {
  162. const string filename(TEST_DATA_BUILDDIR "/broken.zone");
  163. for (const ErrorCase* ec = error_cases; ec->line != NULL; ++ec) {
  164. SCOPED_TRACE(ec->problem);
  165. prepareBrokenZone(filename, ec->line);
  166. {
  167. SCOPED_TRACE("Strict mode");
  168. clear();
  169. setLoader(filename.c_str(), Name("example.org."), RRClass::IN(),
  170. MasterLoader::DEFAULT);
  171. loader_->load();
  172. EXPECT_EQ(1, errors_.size());
  173. EXPECT_TRUE(warnings_.empty());
  174. checkRR("example.org", RRType::SOA(), "ns1.example.org. "
  175. "admin.example.org. 1234 3600 1800 2419200 7200");
  176. // In the strict mode, it is aborted. The last RR is not
  177. // even attempted.
  178. EXPECT_TRUE(rrsets_.empty());
  179. }
  180. {
  181. SCOPED_TRACE("Lenient mode");
  182. clear();
  183. setLoader(filename.c_str(), Name("example.org."), RRClass::IN(),
  184. MasterLoader::MANY_ERRORS);
  185. loader_->load();
  186. EXPECT_EQ(1, errors_.size());
  187. EXPECT_TRUE(warnings_.empty());
  188. checkRR("example.org", RRType::SOA(), "ns1.example.org. "
  189. "admin.example.org. 1234 3600 1800 2419200 7200");
  190. // This one is below the error one.
  191. checkRR("correct.example.org", RRType::A(), "192.0.2.2");
  192. EXPECT_TRUE(rrsets_.empty());
  193. }
  194. }
  195. }