master_loader_unittest.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. "\"$INCLUDE\"",
  138. NULL
  139. };
  140. for (const char** include = includes; *include != NULL; ++include) {
  141. SCOPED_TRACE(*include);
  142. clear();
  143. // Prepare input source that has the include and some more data
  144. // below (to see it returns back to the original source).
  145. const string include_str = string(*include) + " " +
  146. TEST_DATA_SRCDIR + "/example.org\nwww 3600 IN AAAA 2001:db8::1\n";
  147. stringstream ss(include_str);
  148. setLoader(ss, Name("example.org."), RRClass::IN(),
  149. MasterLoader::MANY_ERRORS);
  150. loader_->load();
  151. EXPECT_TRUE(loader_->loadedSucessfully());
  152. EXPECT_TRUE(errors_.empty());
  153. EXPECT_TRUE(warnings_.empty());
  154. checkBasicRRs();
  155. checkRR("www.example.org", RRType::AAAA(), "2001:db8::1");
  156. }
  157. }
  158. // Test the source is correctly popped even after error
  159. TEST_F(MasterLoaderTest, popAfterError) {
  160. const string include_str = "$include " TEST_DATA_SRCDIR
  161. "/broken.zone\nwww 3600 IN AAAA 2001:db8::1\n";
  162. stringstream ss(include_str);
  163. // We don't test without MANY_ERRORS, we want to see what happens
  164. // after the error.
  165. setLoader(ss, Name("example.org."), RRClass::IN(),
  166. MasterLoader::MANY_ERRORS);
  167. loader_->load();
  168. EXPECT_FALSE(loader_->loadedSucessfully());
  169. EXPECT_EQ(1, errors_.size()); // For the broken RR
  170. EXPECT_EQ(1, warnings_.size()); // For missing EOLN
  171. // The included file doesn't contain anything usable, but the
  172. // line after the include should be there.
  173. checkRR("www.example.org", RRType::AAAA(), "2001:db8::1");
  174. }
  175. // Check it works the same when created based on a stream, not filename
  176. TEST_F(MasterLoaderTest, streamConstructor) {
  177. stringstream zone_stream(prepareZone("", true));
  178. setLoader(zone_stream, Name("example.org."), RRClass::IN(),
  179. MasterLoader::MANY_ERRORS);
  180. EXPECT_FALSE(loader_->loadedSucessfully());
  181. loader_->load();
  182. EXPECT_TRUE(loader_->loadedSucessfully());
  183. EXPECT_TRUE(errors_.empty());
  184. EXPECT_TRUE(warnings_.empty());
  185. checkRR("example.org", RRType::SOA(), "ns1.example.org. "
  186. "admin.example.org. 1234 3600 1800 2419200 7200");
  187. checkRR("correct.example.org", RRType::A(), "192.0.2.2");
  188. }
  189. // Try loading data incrementally.
  190. TEST_F(MasterLoaderTest, incrementalLoad) {
  191. setLoader(TEST_DATA_SRCDIR "/example.org", Name("example.org."),
  192. RRClass::IN(), MasterLoader::MANY_ERRORS);
  193. EXPECT_FALSE(loader_->loadedSucessfully());
  194. EXPECT_FALSE(loader_->loadIncremental(2));
  195. EXPECT_FALSE(loader_->loadedSucessfully());
  196. EXPECT_TRUE(errors_.empty());
  197. EXPECT_TRUE(warnings_.empty());
  198. checkRR("example.org", RRType::SOA(),
  199. "ns1.example.org. admin.example.org. "
  200. "1234 3600 1800 2419200 7200");
  201. checkRR("example.org", RRType::NS(), "ns1.example.org.");
  202. // The third one is not loaded yet
  203. EXPECT_TRUE(rrsets_.empty());
  204. // Load the rest.
  205. EXPECT_TRUE(loader_->loadIncremental(20));
  206. EXPECT_TRUE(loader_->loadedSucessfully());
  207. EXPECT_TRUE(errors_.empty());
  208. EXPECT_TRUE(warnings_.empty());
  209. checkRR("www.example.org", RRType::A(), "192.0.2.1");
  210. }
  211. // Try loading from file that doesn't exist. There should be single error
  212. // saying so.
  213. TEST_F(MasterLoaderTest, invalidFile) {
  214. setLoader("This file doesn't exist at all",
  215. Name("exmaple.org."), RRClass::IN(), MasterLoader::MANY_ERRORS);
  216. // Nothing yet. The loader is dormant until invoked.
  217. // Is it really what we want?
  218. EXPECT_TRUE(errors_.empty());
  219. loader_->load();
  220. EXPECT_TRUE(warnings_.empty());
  221. EXPECT_TRUE(rrsets_.empty());
  222. ASSERT_EQ(1, errors_.size());
  223. EXPECT_EQ(0, errors_[0].find("Error opening the input source file: ")) <<
  224. "Different error: " << errors_[0];
  225. }
  226. struct ErrorCase {
  227. const char* const line; // The broken line in master file
  228. const char* const problem; // Description of the problem for SCOPED_TRACE
  229. } const error_cases[] = {
  230. { "www... 3600 IN A 192.0.2.1", "Invalid name" },
  231. { "www FORTNIGHT IN A 192.0.2.1", "Invalid TTL" },
  232. { "www 3600 XX A 192.0.2.1", "Invalid class" },
  233. { "www 3600 IN A bad_ip", "Invalid Rdata" },
  234. { "www 3600 IN", "Unexpected EOLN" },
  235. { "www 3600 CH TXT nothing", "Class mismatch" },
  236. { "www \"3600\" IN A 192.0.2.1", "Quoted TTL" },
  237. { "www 3600 \"IN\" A 192.0.2.1", "Quoted class" },
  238. { "www 3600 IN \"A\" 192.0.2.1", "Quoted type" },
  239. { "unbalanced)paren 3600 IN A 192.0.2.1", "Token error 1" },
  240. { "www 3600 unbalanced)paren A 192.0.2.1", "Token error 2" },
  241. // Check the unknown directive. The rest looks like ordinary RR,
  242. // so we see the $ is actually special.
  243. { "$UNKNOWN 3600 IN A 192.0.2.1", "Unknown $ directive" },
  244. { "$INCLUDE", "Missing include path" },
  245. { "$INCLUDE /file/not/found", "Include file not found" },
  246. { "$INCLUDE /file/not/found and here goes bunch of garbage",
  247. "Include file not found and garbage at the end of line" },
  248. { NULL, NULL }
  249. };
  250. // Test a broken zone is handled properly. We test several problems,
  251. // both in strict and lenient mode.
  252. TEST_F(MasterLoaderTest, brokenZone) {
  253. for (const ErrorCase* ec = error_cases; ec->line != NULL; ++ec) {
  254. SCOPED_TRACE(ec->problem);
  255. const string zone(prepareZone(ec->line, true));
  256. {
  257. SCOPED_TRACE("Strict mode");
  258. clear();
  259. stringstream zone_stream(zone);
  260. setLoader(zone_stream, Name("example.org."), RRClass::IN(),
  261. MasterLoader::DEFAULT);
  262. EXPECT_FALSE(loader_->loadedSucessfully());
  263. EXPECT_THROW(loader_->load(), MasterLoaderError);
  264. EXPECT_FALSE(loader_->loadedSucessfully());
  265. EXPECT_EQ(1, errors_.size());
  266. EXPECT_TRUE(warnings_.empty());
  267. checkRR("example.org", RRType::SOA(), "ns1.example.org. "
  268. "admin.example.org. 1234 3600 1800 2419200 7200");
  269. // In the strict mode, it is aborted. The last RR is not
  270. // even attempted.
  271. EXPECT_TRUE(rrsets_.empty());
  272. }
  273. {
  274. SCOPED_TRACE("Lenient mode");
  275. clear();
  276. stringstream zone_stream(zone);
  277. setLoader(zone_stream, Name("example.org."), RRClass::IN(),
  278. MasterLoader::MANY_ERRORS);
  279. EXPECT_FALSE(loader_->loadedSucessfully());
  280. EXPECT_NO_THROW(loader_->load());
  281. EXPECT_FALSE(loader_->loadedSucessfully());
  282. EXPECT_EQ(1, errors_.size());
  283. EXPECT_TRUE(warnings_.empty());
  284. checkRR("example.org", RRType::SOA(), "ns1.example.org. "
  285. "admin.example.org. 1234 3600 1800 2419200 7200");
  286. // This one is below the error one.
  287. checkRR("correct.example.org", RRType::A(), "192.0.2.2");
  288. EXPECT_TRUE(rrsets_.empty());
  289. }
  290. {
  291. SCOPED_TRACE("Error at EOF");
  292. // This case is interesting only in the lenient mode.
  293. clear();
  294. const string zoneEOF(prepareZone(ec->line, false));
  295. stringstream zone_stream(zoneEOF);
  296. setLoader(zone_stream, Name("example.org."), RRClass::IN(),
  297. MasterLoader::MANY_ERRORS);
  298. EXPECT_FALSE(loader_->loadedSucessfully());
  299. EXPECT_NO_THROW(loader_->load());
  300. EXPECT_FALSE(loader_->loadedSucessfully());
  301. EXPECT_EQ(1, errors_.size()) << errors_[0] << "\n" << errors_[1];
  302. // The unexpected EOF warning
  303. EXPECT_EQ(1, warnings_.size());
  304. checkRR("example.org", RRType::SOA(), "ns1.example.org. "
  305. "admin.example.org. 1234 3600 1800 2419200 7200");
  306. EXPECT_TRUE(rrsets_.empty());
  307. }
  308. }
  309. }
  310. // Check that a garbage after the include generates an error, but not fatal
  311. // one (in lenient mode) and we can recover.
  312. TEST_F(MasterLoaderTest, includeWithGarbage) {
  313. // Include an origin (example.org) because we expect it to be handled
  314. // soon and we don't want it to break here.
  315. const string include_str("$INCLUDE " TEST_DATA_SRCDIR
  316. "/example.org example.org bunch of other stuff\n"
  317. "www 3600 IN AAAA 2001:db8::1\n");
  318. stringstream zone_stream(include_str);
  319. setLoader(zone_stream, Name("example.org."), RRClass::IN(),
  320. MasterLoader::MANY_ERRORS);
  321. EXPECT_NO_THROW(loader_->load());
  322. EXPECT_FALSE(loader_->loadedSucessfully());
  323. ASSERT_EQ(1, errors_.size());
  324. // It says something about extra tokens at the end
  325. EXPECT_NE(string::npos, errors_[0].find("Extra"));
  326. EXPECT_TRUE(warnings_.empty());
  327. checkBasicRRs();
  328. checkRR("www.example.org", RRType::AAAA(), "2001:db8::1");
  329. }
  330. // Test the constructor rejects empty add callback.
  331. TEST_F(MasterLoaderTest, emptyCallback) {
  332. EXPECT_THROW(MasterLoader(TEST_DATA_SRCDIR "/example.org",
  333. Name("example.org"), RRClass::IN(), callbacks_,
  334. AddRRCallback()), isc::InvalidParameter);
  335. // And the same with the second constructor
  336. stringstream ss("");
  337. EXPECT_THROW(MasterLoader(ss, Name("example.org"), RRClass::IN(),
  338. callbacks_, AddRRCallback()),
  339. isc::InvalidParameter);
  340. }
  341. // Check it throws when we try to load after loading was complete.
  342. TEST_F(MasterLoaderTest, loadTwice) {
  343. setLoader(TEST_DATA_SRCDIR "/example.org", Name("example.org."),
  344. RRClass::IN(), MasterLoader::MANY_ERRORS);
  345. loader_->load();
  346. EXPECT_THROW(loader_->load(), isc::InvalidOperation);
  347. }
  348. // Load 0 items should be rejected
  349. TEST_F(MasterLoaderTest, loadZero) {
  350. setLoader(TEST_DATA_SRCDIR "/example.org", Name("example.org."),
  351. RRClass::IN(), MasterLoader::MANY_ERRORS);
  352. EXPECT_THROW(loader_->loadIncremental(0), isc::InvalidParameter);
  353. }
  354. // Test there's a warning when the file terminates without end of
  355. // line.
  356. TEST_F(MasterLoaderTest, noEOLN) {
  357. // No \n at the end
  358. const string input("example.org. 3600 IN SOA ns1.example.org. "
  359. "admin.example.org. 1234 3600 1800 2419200 7200");
  360. stringstream ss(input);
  361. setLoader(ss, Name("example.org."), RRClass::IN(),
  362. MasterLoader::MANY_ERRORS);
  363. loader_->load();
  364. EXPECT_TRUE(loader_->loadedSucessfully());
  365. EXPECT_TRUE(errors_.empty()) << errors_[0];
  366. // There should be one warning about the EOLN
  367. EXPECT_EQ(1, warnings_.size());
  368. checkRR("example.org", RRType::SOA(), "ns1.example.org. "
  369. "admin.example.org. 1234 3600 1800 2419200 7200");
  370. }
  371. }