master_loader_unittest.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. { "$INCLUDE", "Missing include path" },
  244. { "$INCLUDE /file/not/found", "Include file not found" },
  245. { "$INCLUDE /file/not/found and here goes bunch of garbage",
  246. "Include file not found and garbage at the end of line" },
  247. { NULL, NULL }
  248. };
  249. // Test a broken zone is handled properly. We test several problems,
  250. // both in strict and lenient mode.
  251. TEST_F(MasterLoaderTest, brokenZone) {
  252. for (const ErrorCase* ec = error_cases; ec->line != NULL; ++ec) {
  253. SCOPED_TRACE(ec->problem);
  254. const string zone(prepareZone(ec->line, true));
  255. {
  256. SCOPED_TRACE("Strict mode");
  257. clear();
  258. stringstream zone_stream(zone);
  259. setLoader(zone_stream, Name("example.org."), RRClass::IN(),
  260. MasterLoader::DEFAULT);
  261. EXPECT_FALSE(loader_->loadedSucessfully());
  262. EXPECT_THROW(loader_->load(), MasterLoaderError);
  263. EXPECT_FALSE(loader_->loadedSucessfully());
  264. EXPECT_EQ(1, errors_.size());
  265. EXPECT_TRUE(warnings_.empty());
  266. checkRR("example.org", RRType::SOA(), "ns1.example.org. "
  267. "admin.example.org. 1234 3600 1800 2419200 7200");
  268. // In the strict mode, it is aborted. The last RR is not
  269. // even attempted.
  270. EXPECT_TRUE(rrsets_.empty());
  271. }
  272. {
  273. SCOPED_TRACE("Lenient mode");
  274. clear();
  275. stringstream zone_stream(zone);
  276. setLoader(zone_stream, Name("example.org."), RRClass::IN(),
  277. MasterLoader::MANY_ERRORS);
  278. EXPECT_FALSE(loader_->loadedSucessfully());
  279. EXPECT_NO_THROW(loader_->load());
  280. EXPECT_FALSE(loader_->loadedSucessfully());
  281. EXPECT_EQ(1, errors_.size());
  282. EXPECT_TRUE(warnings_.empty());
  283. checkRR("example.org", RRType::SOA(), "ns1.example.org. "
  284. "admin.example.org. 1234 3600 1800 2419200 7200");
  285. // This one is below the error one.
  286. checkRR("correct.example.org", RRType::A(), "192.0.2.2");
  287. EXPECT_TRUE(rrsets_.empty());
  288. }
  289. {
  290. SCOPED_TRACE("Error at EOF");
  291. // This case is interesting only in the lenient mode.
  292. clear();
  293. const string zoneEOF(prepareZone(ec->line, false));
  294. stringstream zone_stream(zoneEOF);
  295. setLoader(zone_stream, Name("example.org."), RRClass::IN(),
  296. MasterLoader::MANY_ERRORS);
  297. EXPECT_FALSE(loader_->loadedSucessfully());
  298. EXPECT_NO_THROW(loader_->load());
  299. EXPECT_FALSE(loader_->loadedSucessfully());
  300. EXPECT_EQ(1, errors_.size()) << errors_[0] << "\n" << errors_[1];
  301. // The unexpected EOF warning
  302. EXPECT_EQ(1, warnings_.size());
  303. checkRR("example.org", RRType::SOA(), "ns1.example.org. "
  304. "admin.example.org. 1234 3600 1800 2419200 7200");
  305. EXPECT_TRUE(rrsets_.empty());
  306. }
  307. }
  308. }
  309. // Check that a garbage after the include generates an error, but not fatal
  310. // one (in lenient mode) and we can recover.
  311. TEST_F(MasterLoaderTest, includeWithGarbage) {
  312. // Include an origin (example.org) because we expect it to be handled
  313. // soon and we don't want it to break here.
  314. const string include_str("$INCLUDE " TEST_DATA_SRCDIR
  315. "/example.org example.org bunch of other stuff\n"
  316. "www 3600 IN AAAA 2001:db8::1\n");
  317. stringstream zone_stream(include_str);
  318. setLoader(zone_stream, Name("example.org."), RRClass::IN(),
  319. MasterLoader::MANY_ERRORS);
  320. EXPECT_NO_THROW(loader_->load());
  321. EXPECT_FALSE(loader_->loadedSucessfully());
  322. ASSERT_EQ(1, errors_.size());
  323. // It says something about extra tokens at the end
  324. EXPECT_NE(string::npos, errors_[0].find("Extra"));
  325. EXPECT_TRUE(warnings_.empty());
  326. checkBasicRRs();
  327. checkRR("www.example.org", RRType::AAAA(), "2001:db8::1");
  328. }
  329. // Test the constructor rejects empty add callback.
  330. TEST_F(MasterLoaderTest, emptyCallback) {
  331. EXPECT_THROW(MasterLoader(TEST_DATA_SRCDIR "/example.org",
  332. Name("example.org"), RRClass::IN(), callbacks_,
  333. AddRRCallback()), isc::InvalidParameter);
  334. // And the same with the second constructor
  335. stringstream ss("");
  336. EXPECT_THROW(MasterLoader(ss, Name("example.org"), RRClass::IN(),
  337. callbacks_, AddRRCallback()),
  338. isc::InvalidParameter);
  339. }
  340. // Check it throws when we try to load after loading was complete.
  341. TEST_F(MasterLoaderTest, loadTwice) {
  342. setLoader(TEST_DATA_SRCDIR "/example.org", Name("example.org."),
  343. RRClass::IN(), MasterLoader::MANY_ERRORS);
  344. loader_->load();
  345. EXPECT_THROW(loader_->load(), isc::InvalidOperation);
  346. }
  347. // Load 0 items should be rejected
  348. TEST_F(MasterLoaderTest, loadZero) {
  349. setLoader(TEST_DATA_SRCDIR "/example.org", Name("example.org."),
  350. RRClass::IN(), MasterLoader::MANY_ERRORS);
  351. EXPECT_THROW(loader_->loadIncremental(0), isc::InvalidParameter);
  352. }
  353. // Test there's a warning when the file terminates without end of
  354. // line.
  355. TEST_F(MasterLoaderTest, noEOLN) {
  356. // No \n at the end
  357. const string input("example.org. 3600 IN SOA ns1.example.org. "
  358. "admin.example.org. 1234 3600 1800 2419200 7200");
  359. stringstream ss(input);
  360. setLoader(ss, Name("example.org."), RRClass::IN(),
  361. MasterLoader::MANY_ERRORS);
  362. loader_->load();
  363. EXPECT_TRUE(loader_->loadedSucessfully());
  364. EXPECT_TRUE(errors_.empty()) << errors_[0];
  365. // There should be one warning about the EOLN
  366. EXPECT_EQ(1, warnings_.size());
  367. checkRR("example.org", RRType::SOA(), "ns1.example.org. "
  368. "admin.example.org. 1234 3600 1800 2419200 7200");
  369. }
  370. }