master_loader_unittest.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 <dns/rdata.h>
  21. #include <gtest/gtest.h>
  22. #include <boost/bind.hpp>
  23. #include <boost/scoped_ptr.hpp>
  24. #include <string>
  25. #include <vector>
  26. #include <list>
  27. #include <sstream>
  28. using namespace isc::dns;
  29. using std::vector;
  30. using std::string;
  31. using std::list;
  32. using std::stringstream;
  33. using std::endl;
  34. namespace {
  35. class MasterLoaderTest : public ::testing::Test {
  36. public:
  37. MasterLoaderTest() :
  38. callbacks_(boost::bind(&MasterLoaderTest::callback, this,
  39. &errors_, _1, _2, _3),
  40. boost::bind(&MasterLoaderTest::callback, this,
  41. &warnings_, _1, _2, _3))
  42. {}
  43. /// Concatenate file, line, and reason, and add it to either errors
  44. /// or warnings
  45. void callback(vector<string>* target, const std::string& file, size_t line,
  46. const std::string& reason)
  47. {
  48. std::stringstream ss;
  49. ss << reason << " [" << file << ":" << line << "]";
  50. target->push_back(ss.str());
  51. }
  52. void addRRset(const Name& name, const RRClass& rrclass,
  53. const RRType& rrtype, const RRTTL& rrttl,
  54. const rdata::RdataPtr& data) {
  55. const RRsetPtr rrset(new BasicRRset(name, rrclass, rrtype, rrttl));
  56. rrset->addRdata(data);
  57. rrsets_.push_back(rrset);
  58. }
  59. void setLoader(const char* file, const Name& origin,
  60. const RRClass& rrclass, const MasterLoader::Options options)
  61. {
  62. loader_.reset(new MasterLoader(file, origin, rrclass, callbacks_,
  63. boost::bind(&MasterLoaderTest::addRRset,
  64. this, _1, _2, _3, _4, _5),
  65. options));
  66. }
  67. void setLoader(std::istream& stream, const Name& origin,
  68. const RRClass& rrclass, const MasterLoader::Options options)
  69. {
  70. loader_.reset(new MasterLoader(stream, origin, rrclass, callbacks_,
  71. boost::bind(&MasterLoaderTest::addRRset,
  72. this, _1, _2, _3, _4, _5),
  73. options));
  74. }
  75. static string prepareZone(const string& line, bool include_last) {
  76. string result;
  77. result += "example.org. 3600 IN SOA ns1.example.org. "
  78. "admin.example.org. 1234 3600 1800 2419200 7200\n";
  79. result += line;
  80. if (include_last) {
  81. result += "\n";
  82. result += "correct 3600 IN A 192.0.2.2\n";
  83. }
  84. return (result);
  85. }
  86. void clear() {
  87. warnings_.clear();
  88. errors_.clear();
  89. rrsets_.clear();
  90. }
  91. // Check the next RR in the ones produced by the loader
  92. // Other than passed arguments are checked to be the default for the tests
  93. void checkRR(const string& name, const RRType& type, const string& data) {
  94. ASSERT_FALSE(rrsets_.empty());
  95. RRsetPtr current = rrsets_.front();
  96. rrsets_.pop_front();
  97. EXPECT_EQ(Name(name), current->getName());
  98. EXPECT_EQ(type, current->getType());
  99. EXPECT_EQ(RRClass::IN(), current->getClass());
  100. ASSERT_EQ(1, current->getRdataCount());
  101. EXPECT_EQ(0, isc::dns::rdata::createRdata(type, RRClass::IN(), data)->
  102. compare(current->getRdataIterator()->getCurrent()));
  103. }
  104. void checkBasicRRs() {
  105. checkRR("example.org", RRType::SOA(),
  106. "ns1.example.org. admin.example.org. "
  107. "1234 3600 1800 2419200 7200");
  108. checkRR("example.org", RRType::NS(), "ns1.example.org.");
  109. checkRR("www.example.org", RRType::A(), "192.0.2.1");
  110. }
  111. MasterLoaderCallbacks callbacks_;
  112. boost::scoped_ptr<MasterLoader> loader_;
  113. vector<string> errors_;
  114. vector<string> warnings_;
  115. list<RRsetPtr> rrsets_;
  116. };
  117. // Test simple loading. The zone file contains no tricky things, and nothing is
  118. // omitted. No RRset contains more than one RR Also no errors or warnings.
  119. TEST_F(MasterLoaderTest, basicLoad) {
  120. setLoader(TEST_DATA_SRCDIR "/example.org", Name("example.org."),
  121. RRClass::IN(), MasterLoader::MANY_ERRORS);
  122. EXPECT_FALSE(loader_->loadedSucessfully());
  123. loader_->load();
  124. EXPECT_TRUE(loader_->loadedSucessfully());
  125. EXPECT_TRUE(errors_.empty());
  126. EXPECT_TRUE(warnings_.empty());
  127. checkBasicRRs();
  128. }
  129. // Test the $INCLUDE directive
  130. TEST_F(MasterLoaderTest, include) {
  131. // Test various cases of include
  132. const char* includes[] = {
  133. "include",
  134. "INCLUDE",
  135. "Include",
  136. "InCluDe",
  137. NULL
  138. };
  139. for (const char** include = includes; *include != NULL; ++include) {
  140. SCOPED_TRACE(*include);
  141. clear();
  142. // Prepare input source that has the include and some more data
  143. // below (to see it returns back to the original source).
  144. const string include_str = "$" + string(*include) + " " +
  145. TEST_DATA_SRCDIR + "/example.org\nwww 3600 IN AAAA 2001:db8::1\n";
  146. stringstream ss(include_str);
  147. setLoader(ss, Name("example.org."), RRClass::IN(),
  148. MasterLoader::MANY_ERRORS);
  149. loader_->load();
  150. EXPECT_TRUE(loader_->loadedSucessfully());
  151. EXPECT_TRUE(errors_.empty());
  152. EXPECT_TRUE(warnings_.empty());
  153. checkBasicRRs();
  154. checkRR("www.example.org", RRType::AAAA(), "2001:db8::1");
  155. }
  156. }
  157. // Test the source is correctly popped even after error
  158. TEST_F(MasterLoaderTest, popAfterError) {
  159. const string include_str = "$include " TEST_DATA_SRCDIR
  160. "/broken.zone\nwww 3600 IN AAAA 2001:db8::1\n";
  161. stringstream ss(include_str);
  162. // We don't test without MANY_ERRORS, we want to see what happens
  163. // after the error.
  164. setLoader(ss, Name("example.org."), RRClass::IN(),
  165. MasterLoader::MANY_ERRORS);
  166. loader_->load();
  167. EXPECT_FALSE(loader_->loadedSucessfully());
  168. EXPECT_EQ(1, errors_.size()); // For the broken RR
  169. EXPECT_EQ(1, warnings_.size()); // For missing EOLN
  170. // The included file doesn't contain anything usable, but the
  171. // line after the include should be there.
  172. checkRR("www.example.org", RRType::AAAA(), "2001:db8::1");
  173. }
  174. // Check it works the same when created based on a stream, not filename
  175. TEST_F(MasterLoaderTest, streamConstructor) {
  176. stringstream zone_stream(prepareZone("", true));
  177. setLoader(zone_stream, Name("example.org."), RRClass::IN(),
  178. MasterLoader::MANY_ERRORS);
  179. EXPECT_FALSE(loader_->loadedSucessfully());
  180. loader_->load();
  181. EXPECT_TRUE(loader_->loadedSucessfully());
  182. EXPECT_TRUE(errors_.empty());
  183. EXPECT_TRUE(warnings_.empty());
  184. checkRR("example.org", RRType::SOA(), "ns1.example.org. "
  185. "admin.example.org. 1234 3600 1800 2419200 7200");
  186. checkRR("correct.example.org", RRType::A(), "192.0.2.2");
  187. }
  188. // Try loading data incrementally.
  189. TEST_F(MasterLoaderTest, incrementalLoad) {
  190. setLoader(TEST_DATA_SRCDIR "/example.org", Name("example.org."),
  191. RRClass::IN(), MasterLoader::MANY_ERRORS);
  192. EXPECT_FALSE(loader_->loadedSucessfully());
  193. EXPECT_FALSE(loader_->loadIncremental(2));
  194. EXPECT_FALSE(loader_->loadedSucessfully());
  195. EXPECT_TRUE(errors_.empty());
  196. EXPECT_TRUE(warnings_.empty());
  197. checkRR("example.org", RRType::SOA(),
  198. "ns1.example.org. admin.example.org. "
  199. "1234 3600 1800 2419200 7200");
  200. checkRR("example.org", RRType::NS(), "ns1.example.org.");
  201. // The third one is not loaded yet
  202. EXPECT_TRUE(rrsets_.empty());
  203. // Load the rest.
  204. EXPECT_TRUE(loader_->loadIncremental(20));
  205. EXPECT_TRUE(loader_->loadedSucessfully());
  206. EXPECT_TRUE(errors_.empty());
  207. EXPECT_TRUE(warnings_.empty());
  208. checkRR("www.example.org", RRType::A(), "192.0.2.1");
  209. }
  210. // Try loading from file that doesn't exist. There should be single error
  211. // saying so.
  212. TEST_F(MasterLoaderTest, invalidFile) {
  213. setLoader("This file doesn't exist at all",
  214. Name("exmaple.org."), RRClass::IN(), MasterLoader::MANY_ERRORS);
  215. // Nothing yet. The loader is dormant until invoked.
  216. // Is it really what we want?
  217. EXPECT_TRUE(errors_.empty());
  218. loader_->load();
  219. EXPECT_TRUE(warnings_.empty());
  220. EXPECT_TRUE(rrsets_.empty());
  221. ASSERT_EQ(1, errors_.size());
  222. EXPECT_EQ(0, errors_[0].find("Error opening the input source file: ")) <<
  223. "Different error: " << errors_[0];
  224. }
  225. struct ErrorCase {
  226. const char* const line; // The broken line in master file
  227. const char* const problem; // Description of the problem for SCOPED_TRACE
  228. } const error_cases[] = {
  229. { "www... 3600 IN A 192.0.2.1", "Invalid name" },
  230. { "www FORTNIGHT IN A 192.0.2.1", "Invalid TTL" },
  231. { "www 3600 XX A 192.0.2.1", "Invalid class" },
  232. { "www 3600 IN A bad_ip", "Invalid Rdata" },
  233. { "www 3600 IN", "Unexpected EOLN" },
  234. { "www 3600 CH TXT nothing", "Class mismatch" },
  235. { "www \"3600\" IN A 192.0.2.1", "Quoted TTL" },
  236. { "www 3600 \"IN\" A 192.0.2.1", "Quoted class" },
  237. { "www 3600 IN \"A\" 192.0.2.1", "Quoted type" },
  238. { "unbalanced)paren 3600 IN A 192.0.2.1", "Token error 1" },
  239. { "www 3600 unbalanced)paren A 192.0.2.1", "Token error 2" },
  240. // Check the unknown directive. The rest looks like ordinary RR,
  241. // so we see the $ is actually special.
  242. { "$UNKNOWN 3600 IN A 192.0.2.1", "Unknown $ directive" },
  243. { NULL, NULL }
  244. };
  245. // Test a broken zone is handled properly. We test several problems,
  246. // both in strict and lenient mode.
  247. TEST_F(MasterLoaderTest, brokenZone) {
  248. for (const ErrorCase* ec = error_cases; ec->line != NULL; ++ec) {
  249. SCOPED_TRACE(ec->problem);
  250. const string zone(prepareZone(ec->line, true));
  251. {
  252. SCOPED_TRACE("Strict mode");
  253. clear();
  254. stringstream zone_stream(zone);
  255. setLoader(zone_stream, Name("example.org."), RRClass::IN(),
  256. MasterLoader::DEFAULT);
  257. EXPECT_FALSE(loader_->loadedSucessfully());
  258. EXPECT_THROW(loader_->load(), MasterLoaderError);
  259. EXPECT_FALSE(loader_->loadedSucessfully());
  260. EXPECT_EQ(1, errors_.size());
  261. EXPECT_TRUE(warnings_.empty());
  262. checkRR("example.org", RRType::SOA(), "ns1.example.org. "
  263. "admin.example.org. 1234 3600 1800 2419200 7200");
  264. // In the strict mode, it is aborted. The last RR is not
  265. // even attempted.
  266. EXPECT_TRUE(rrsets_.empty());
  267. }
  268. {
  269. SCOPED_TRACE("Lenient mode");
  270. clear();
  271. stringstream zone_stream(zone);
  272. setLoader(zone_stream, Name("example.org."), RRClass::IN(),
  273. MasterLoader::MANY_ERRORS);
  274. EXPECT_FALSE(loader_->loadedSucessfully());
  275. EXPECT_NO_THROW(loader_->load());
  276. EXPECT_FALSE(loader_->loadedSucessfully());
  277. EXPECT_EQ(1, errors_.size());
  278. EXPECT_TRUE(warnings_.empty());
  279. checkRR("example.org", RRType::SOA(), "ns1.example.org. "
  280. "admin.example.org. 1234 3600 1800 2419200 7200");
  281. // This one is below the error one.
  282. checkRR("correct.example.org", RRType::A(), "192.0.2.2");
  283. EXPECT_TRUE(rrsets_.empty());
  284. }
  285. {
  286. SCOPED_TRACE("Error at EOF");
  287. // This case is interesting only in the lenient mode.
  288. const string zoneEOF(prepareZone(ec->line, false));
  289. clear();
  290. stringstream zone_stream(zoneEOF);
  291. setLoader(zone_stream, Name("example.org."), RRClass::IN(),
  292. MasterLoader::MANY_ERRORS);
  293. EXPECT_FALSE(loader_->loadedSucessfully());
  294. EXPECT_NO_THROW(loader_->load());
  295. EXPECT_FALSE(loader_->loadedSucessfully());
  296. EXPECT_EQ(1, errors_.size());
  297. // The unexpected EOF warning
  298. EXPECT_EQ(1, warnings_.size());
  299. checkRR("example.org", RRType::SOA(), "ns1.example.org. "
  300. "admin.example.org. 1234 3600 1800 2419200 7200");
  301. EXPECT_TRUE(rrsets_.empty());
  302. }
  303. }
  304. }
  305. // Test the constructor rejects empty add callback.
  306. TEST_F(MasterLoaderTest, emptyCallback) {
  307. EXPECT_THROW(MasterLoader(TEST_DATA_SRCDIR "/example.org",
  308. Name("example.org"), RRClass::IN(), callbacks_,
  309. AddRRCallback()), isc::InvalidParameter);
  310. // And the same with the second constructor
  311. stringstream ss("");
  312. EXPECT_THROW(MasterLoader(ss, Name("example.org"), RRClass::IN(),
  313. callbacks_, AddRRCallback()),
  314. isc::InvalidParameter);
  315. }
  316. // Check it throws when we try to load after loading was complete.
  317. TEST_F(MasterLoaderTest, loadTwice) {
  318. setLoader(TEST_DATA_SRCDIR "/example.org", Name("example.org."),
  319. RRClass::IN(), MasterLoader::MANY_ERRORS);
  320. loader_->load();
  321. EXPECT_THROW(loader_->load(), isc::InvalidOperation);
  322. }
  323. // Load 0 items should be rejected
  324. TEST_F(MasterLoaderTest, loadZero) {
  325. setLoader(TEST_DATA_SRCDIR "/example.org", Name("example.org."),
  326. RRClass::IN(), MasterLoader::MANY_ERRORS);
  327. EXPECT_THROW(loader_->loadIncremental(0), isc::InvalidParameter);
  328. }
  329. }