master_loader_unittest.cc 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409
  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/rrttl.h>
  20. #include <dns/name.h>
  21. #include <dns/rdata.h>
  22. #include <gtest/gtest.h>
  23. #include <boost/bind.hpp>
  24. #include <boost/lexical_cast.hpp>
  25. #include <boost/scoped_ptr.hpp>
  26. #include <string>
  27. #include <vector>
  28. #include <list>
  29. #include <sstream>
  30. using namespace isc::dns;
  31. using std::vector;
  32. using std::string;
  33. using std::list;
  34. using std::stringstream;
  35. using std::endl;
  36. using boost::lexical_cast;
  37. namespace {
  38. class MasterLoaderTest : public ::testing::Test {
  39. public:
  40. MasterLoaderTest() :
  41. callbacks_(boost::bind(&MasterLoaderTest::callback, this,
  42. &errors_, _1, _2, _3),
  43. boost::bind(&MasterLoaderTest::callback, this,
  44. &warnings_, _1, _2, _3))
  45. {}
  46. void TearDown() {
  47. // Check there are no more RRs we didn't expect
  48. EXPECT_TRUE(rrsets_.empty());
  49. }
  50. /// Concatenate file, line, and reason, and add it to either errors
  51. /// or warnings
  52. void callback(vector<string>* target, const std::string& file, size_t line,
  53. const std::string& reason)
  54. {
  55. std::stringstream ss;
  56. ss << reason << " [" << file << ":" << line << "]";
  57. target->push_back(ss.str());
  58. }
  59. void addRRset(const Name& name, const RRClass& rrclass,
  60. const RRType& rrtype, const RRTTL& rrttl,
  61. const rdata::RdataPtr& data) {
  62. const RRsetPtr rrset(new BasicRRset(name, rrclass, rrtype, rrttl));
  63. rrset->addRdata(data);
  64. rrsets_.push_back(rrset);
  65. }
  66. void setLoader(const char* file, const Name& origin,
  67. const RRClass& rrclass, const MasterLoader::Options options)
  68. {
  69. loader_.reset(new MasterLoader(file, origin, rrclass, callbacks_,
  70. boost::bind(&MasterLoaderTest::addRRset,
  71. this, _1, _2, _3, _4, _5),
  72. options));
  73. }
  74. void setLoader(std::istream& stream, const Name& origin,
  75. const RRClass& rrclass, const MasterLoader::Options options)
  76. {
  77. loader_.reset(new MasterLoader(stream, origin, rrclass, callbacks_,
  78. boost::bind(&MasterLoaderTest::addRRset,
  79. this, _1, _2, _3, _4, _5),
  80. options));
  81. }
  82. static string prepareZone(const string& line, bool include_last) {
  83. string result;
  84. result += "example.org. 3600 IN SOA ns1.example.org. "
  85. "admin.example.org. 1234 3600 1800 2419200 7200\n";
  86. result += line;
  87. if (include_last) {
  88. result += "\n";
  89. result += "correct 3600 IN A 192.0.2.2\n";
  90. }
  91. return (result);
  92. }
  93. void clear() {
  94. warnings_.clear();
  95. errors_.clear();
  96. rrsets_.clear();
  97. }
  98. // Check the next RR in the ones produced by the loader
  99. // Other than passed arguments are checked to be the default for the tests
  100. void checkRR(const string& name, const RRType& type, const string& data,
  101. const RRTTL& rrttl = RRTTL(3600)) {
  102. ASSERT_FALSE(rrsets_.empty());
  103. RRsetPtr current = rrsets_.front();
  104. rrsets_.pop_front();
  105. EXPECT_EQ(Name(name), current->getName());
  106. EXPECT_EQ(type, current->getType());
  107. EXPECT_EQ(RRClass::IN(), current->getClass());
  108. EXPECT_EQ(rrttl, current->getTTL());
  109. ASSERT_EQ(1, current->getRdataCount());
  110. EXPECT_EQ(0, isc::dns::rdata::createRdata(type, RRClass::IN(), data)->
  111. compare(current->getRdataIterator()->getCurrent()));
  112. }
  113. void checkBasicRRs() {
  114. checkRR("example.org", RRType::SOA(),
  115. "ns1.example.org. admin.example.org. "
  116. "1234 3600 1800 2419200 7200");
  117. checkRR("example.org", RRType::NS(), "ns1.example.org.");
  118. checkRR("www.example.org", RRType::A(), "192.0.2.1");
  119. checkRR("www.example.org", RRType::AAAA(), "2001:db8::1");
  120. }
  121. void checkARR(const string& name) {
  122. checkRR(name, RRType::A(), "192.0.2.1");
  123. }
  124. MasterLoaderCallbacks callbacks_;
  125. boost::scoped_ptr<MasterLoader> loader_;
  126. vector<string> errors_;
  127. vector<string> warnings_;
  128. list<RRsetPtr> rrsets_;
  129. };
  130. // Test simple loading. The zone file contains no tricky things, and nothing is
  131. // omitted. No RRset contains more than one RR Also no errors or warnings.
  132. TEST_F(MasterLoaderTest, basicLoad) {
  133. setLoader(TEST_DATA_SRCDIR "/example.org", Name("example.org."),
  134. RRClass::IN(), MasterLoader::MANY_ERRORS);
  135. EXPECT_FALSE(loader_->loadedSucessfully());
  136. // The following three should be set to 0 initially in case the loader
  137. // is constructed from a file name.
  138. EXPECT_EQ(0, loader_->getSize());
  139. EXPECT_EQ(0, loader_->getPosition());
  140. loader_->load();
  141. EXPECT_TRUE(loader_->loadedSucessfully());
  142. EXPECT_TRUE(errors_.empty());
  143. EXPECT_TRUE(warnings_.empty());
  144. // Hardcode expected values taken from the test data file, assuming it
  145. // won't change too often.
  146. EXPECT_EQ(550, loader_->getSize());
  147. EXPECT_EQ(550, loader_->getPosition());
  148. checkBasicRRs();
  149. }
  150. // Test the $INCLUDE directive
  151. TEST_F(MasterLoaderTest, include) {
  152. // Test various cases of include
  153. const char* includes[] = {
  154. "$include",
  155. "$INCLUDE",
  156. "$Include",
  157. "$InCluDe",
  158. "\"$INCLUDE\"",
  159. NULL
  160. };
  161. for (const char** include = includes; *include != NULL; ++include) {
  162. SCOPED_TRACE(*include);
  163. clear();
  164. // Prepare input source that has the include and some more data
  165. // below (to see it returns back to the original source).
  166. const string include_str = string(*include) + " " +
  167. TEST_DATA_SRCDIR + "/example.org\nwww 3600 IN AAAA 2001:db8::1\n";
  168. stringstream ss(include_str);
  169. setLoader(ss, Name("example.org."), RRClass::IN(),
  170. MasterLoader::MANY_ERRORS);
  171. loader_->load();
  172. EXPECT_TRUE(loader_->loadedSucessfully());
  173. EXPECT_TRUE(errors_.empty());
  174. EXPECT_TRUE(warnings_.empty());
  175. checkBasicRRs();
  176. checkRR("www.example.org", RRType::AAAA(), "2001:db8::1");
  177. }
  178. }
  179. TEST_F(MasterLoaderTest, includeAndIncremental) {
  180. // Check getSize() and getPosition() are adjusted before and after
  181. // $INCLUDE.
  182. const string first_rr = "before.example.org. 0 A 192.0.2.1\n";
  183. const string include_str = "$INCLUDE " TEST_DATA_SRCDIR "/example.org";
  184. const string zone_data = first_rr + include_str + "\n" +
  185. "www 3600 IN AAAA 2001:db8::1\n";
  186. stringstream ss(zone_data);
  187. setLoader(ss, Name("example.org."), RRClass::IN(), MasterLoader::DEFAULT);
  188. // On construction, getSize() returns the size of the data (exclude the
  189. // the file to be included); position is set to 0.
  190. EXPECT_EQ(zone_data.size(), loader_->getSize());
  191. EXPECT_EQ(0, loader_->getPosition());
  192. // Read the first RR. getSize() doesn't change; position should be
  193. // at the end of the first line.
  194. loader_->loadIncremental(1);
  195. EXPECT_EQ(zone_data.size(), loader_->getSize());
  196. EXPECT_EQ(first_rr.size(), loader_->getPosition());
  197. // Read next 4. It includes $INCLUDE processing. Magic number of 550
  198. // is the size of the test zone file (see above); 507 is the position in
  199. // the file at the end of 4th RR (due to extra comments it's smaller than
  200. // the file size).
  201. loader_->loadIncremental(4);
  202. EXPECT_EQ(zone_data.size() + 550, loader_->getSize());
  203. EXPECT_EQ(first_rr.size() + include_str.size() + 507,
  204. loader_->getPosition());
  205. // Read the last one. At this point getSize and getPosition return
  206. // the same value, indicating progress of 100%.
  207. loader_->loadIncremental(1);
  208. EXPECT_EQ(zone_data.size() + 550, loader_->getSize());
  209. EXPECT_EQ(zone_data.size() + 550, loader_->getPosition());
  210. // we were not interested in checking RRs in this test. clear them to
  211. // not confuse TearDown().
  212. rrsets_.clear();
  213. }
  214. // A commonly used helper to check callback message.
  215. void
  216. checkCallbackMessage(const string& actual_msg, const string& expected_msg,
  217. size_t expected_line) {
  218. // The actual message should begin with the expected message.
  219. EXPECT_EQ(0, actual_msg.find(expected_msg)) << "actual message: " <<
  220. actual_msg << " expected: " <<
  221. expected_msg;
  222. // and it should end with "...:<line_num>]"
  223. const string line_desc = ":" + lexical_cast<string>(expected_line) + "]";
  224. EXPECT_EQ(actual_msg.size() - line_desc.size(),
  225. actual_msg.find(line_desc)) << "Expected on line " <<
  226. expected_line;
  227. }
  228. TEST_F(MasterLoaderTest, origin) {
  229. // Various forms of the directive
  230. const char* origins[] = {
  231. "$origin",
  232. "$ORIGIN",
  233. "$Origin",
  234. "$OrigiN",
  235. "\"$ORIGIN\"",
  236. NULL
  237. };
  238. for (const char** origin = origins; *origin != NULL; ++origin) {
  239. SCOPED_TRACE(*origin);
  240. clear();
  241. const string directive = *origin;
  242. const string input =
  243. "@ 1H IN A 192.0.2.1\n" +
  244. directive + " sub.example.org.\n"
  245. "\"www\" 1H IN A 192.0.2.1\n" +
  246. // Relative name in the origin
  247. directive + " relative\n"
  248. "@ 1H IN A 192.0.2.1\n"
  249. // Origin is _not_ used here (absolute name)
  250. "noorigin.example.org. 60M IN A 192.0.2.1\n";
  251. stringstream ss(input);
  252. setLoader(ss, Name("example.org."), RRClass::IN(),
  253. MasterLoader::MANY_ERRORS);
  254. loader_->load();
  255. EXPECT_TRUE(loader_->loadedSucessfully());
  256. EXPECT_TRUE(errors_.empty());
  257. // There's a relative origin in it, we warn about that.
  258. EXPECT_EQ(1, warnings_.size());
  259. checkCallbackMessage(warnings_.at(0),
  260. "The new origin is relative, did you really mean "
  261. "relative.sub.example.org.?", 4);
  262. checkARR("example.org");
  263. checkARR("www.sub.example.org");
  264. checkARR("relative.sub.example.org");
  265. checkARR("noorigin.example.org");
  266. }
  267. }
  268. TEST_F(MasterLoaderTest, generate) {
  269. // Various forms of the directive
  270. const char* generates[] = {
  271. "$generate",
  272. "$GENERATE",
  273. "$Generate",
  274. "$GeneratE",
  275. "\"$GENERATE\"",
  276. NULL
  277. };
  278. for (const char** generate = generates; *generate != NULL; ++generate) {
  279. SCOPED_TRACE(*generate);
  280. clear();
  281. const string directive = *generate;
  282. const string input =
  283. "$ORIGIN example.org.\n"
  284. "before.example.org. 3600 IN A 192.0.2.0\n" +
  285. directive + " 3-5 host$ A 192.0.2.$\n" +
  286. "after.example.org. 3600 IN A 192.0.2.255\n";
  287. stringstream ss(input);
  288. setLoader(ss, Name("example.org."), RRClass::IN(),
  289. MasterLoader::MANY_ERRORS);
  290. loader_->load();
  291. EXPECT_TRUE(loader_->loadedSucessfully());
  292. EXPECT_TRUE(errors_.empty());
  293. checkRR("before.example.org", RRType::A(), "192.0.2.0");
  294. checkRR("host3.example.org", RRType::A(), "192.0.2.3");
  295. checkRR("host4.example.org", RRType::A(), "192.0.2.4");
  296. checkRR("host5.example.org", RRType::A(), "192.0.2.5");
  297. checkRR("after.example.org", RRType::A(), "192.0.2.255");
  298. }
  299. }
  300. TEST_F(MasterLoaderTest, generateRelativeLHS) {
  301. const string input =
  302. "$ORIGIN example.org.\n"
  303. "$GENERATE 1-2 @ 3600 NS ns$.example.org.\n";
  304. stringstream ss(input);
  305. setLoader(ss, Name("example.org."), RRClass::IN(),
  306. MasterLoader::MANY_ERRORS);
  307. loader_->load();
  308. EXPECT_TRUE(loader_->loadedSucessfully());
  309. EXPECT_TRUE(errors_.empty());
  310. checkRR("example.org", RRType::NS(), "ns1.example.org.");
  311. checkRR("example.org", RRType::NS(), "ns2.example.org.");
  312. }
  313. TEST_F(MasterLoaderTest, generateInFront) {
  314. // $ is in the front
  315. const string input =
  316. "$ORIGIN example.org.\n"
  317. "$GENERATE 9-10 $host 3600 TXT \"$ pomegranate\"\n";
  318. stringstream ss(input);
  319. setLoader(ss, Name("example.org."), RRClass::IN(),
  320. MasterLoader::MANY_ERRORS);
  321. loader_->load();
  322. EXPECT_TRUE(loader_->loadedSucessfully());
  323. EXPECT_TRUE(errors_.empty());
  324. checkRR("9host.example.org", RRType::TXT(), "9 pomegranate");
  325. checkRR("10host.example.org", RRType::TXT(), "10 pomegranate");
  326. }
  327. TEST_F(MasterLoaderTest, generateInMiddle) {
  328. // $ is in the middle
  329. const string input =
  330. "$ORIGIN example.org.\n"
  331. "$GENERATE 9-10 num$-host 3600 TXT \"This is $ pomegranate\"\n";
  332. stringstream ss(input);
  333. setLoader(ss, Name("example.org."), RRClass::IN(),
  334. MasterLoader::MANY_ERRORS);
  335. loader_->load();
  336. EXPECT_TRUE(loader_->loadedSucessfully());
  337. EXPECT_TRUE(errors_.empty());
  338. checkRR("num9-host.example.org", RRType::TXT(), "This is 9 pomegranate");
  339. checkRR("num10-host.example.org", RRType::TXT(), "This is 10 pomegranate");
  340. }
  341. TEST_F(MasterLoaderTest, generateStripsQuotes) {
  342. const string input =
  343. "$ORIGIN example.org.\n"
  344. "$GENERATE 1-2 @ 3600 MX \"$ mx$.example.org.\"\n";
  345. stringstream ss(input);
  346. setLoader(ss, Name("example.org."), RRClass::IN(),
  347. MasterLoader::MANY_ERRORS);
  348. loader_->load();
  349. EXPECT_TRUE(loader_->loadedSucessfully());
  350. EXPECT_TRUE(errors_.empty());
  351. checkRR("example.org", RRType::MX(), "1 mx1.example.org.");
  352. checkRR("example.org", RRType::MX(), "2 mx2.example.org.");
  353. }
  354. TEST_F(MasterLoaderTest, generateWithDoublePlaceholder) {
  355. const string input =
  356. "$ORIGIN example.org.\n"
  357. "$GENERATE 9-10 host$ 3600 TXT \"This is $$ pomegranate\"\n";
  358. stringstream ss(input);
  359. setLoader(ss, Name("example.org."), RRClass::IN(),
  360. MasterLoader::MANY_ERRORS);
  361. loader_->load();
  362. EXPECT_TRUE(loader_->loadedSucessfully());
  363. EXPECT_TRUE(errors_.empty());
  364. checkRR("host9.example.org", RRType::TXT(), "This is $ pomegranate");
  365. checkRR("host10.example.org", RRType::TXT(), "This is $ pomegranate");
  366. }
  367. TEST_F(MasterLoaderTest, generateWithEscape) {
  368. const string input =
  369. "$ORIGIN example.org.\n"
  370. "$GENERATE 9-10 host$ 3600 TXT \"This is \\$\\pomegranate\"\n";
  371. stringstream ss(input);
  372. setLoader(ss, Name("example.org."), RRClass::IN(),
  373. MasterLoader::MANY_ERRORS);
  374. loader_->load();
  375. EXPECT_TRUE(loader_->loadedSucessfully());
  376. EXPECT_TRUE(errors_.empty());
  377. checkRR("host9.example.org", RRType::TXT(), "This is \\$\\pomegranate");
  378. checkRR("host10.example.org", RRType::TXT(), "This is \\$\\pomegranate");
  379. }
  380. TEST_F(MasterLoaderTest, generateWithParams) {
  381. const string input =
  382. "$ORIGIN example.org.\n"
  383. "$TTL 3600\n"
  384. "$GENERATE 2-3 host$ A 192.0.2.$\n"
  385. "$GENERATE 5-6 host$ 3600 A 192.0.2.$\n"
  386. "$GENERATE 8-9 host$ IN A 192.0.2.$\n"
  387. "$GENERATE 11-12 host$ IN 3600 A 192.0.2.$\n"
  388. "$GENERATE 14-15 host$ 3600 IN A 192.0.2.$\n";
  389. stringstream ss(input);
  390. setLoader(ss, Name("example.org."), RRClass::IN(),
  391. MasterLoader::MANY_ERRORS);
  392. loader_->load();
  393. EXPECT_TRUE(loader_->loadedSucessfully());
  394. EXPECT_TRUE(errors_.empty());
  395. checkRR("host2.example.org", RRType::A(), "192.0.2.2");
  396. checkRR("host3.example.org", RRType::A(), "192.0.2.3");
  397. checkRR("host5.example.org", RRType::A(), "192.0.2.5");
  398. checkRR("host6.example.org", RRType::A(), "192.0.2.6");
  399. checkRR("host8.example.org", RRType::A(), "192.0.2.8");
  400. checkRR("host9.example.org", RRType::A(), "192.0.2.9");
  401. checkRR("host11.example.org", RRType::A(), "192.0.2.11");
  402. checkRR("host12.example.org", RRType::A(), "192.0.2.12");
  403. checkRR("host14.example.org", RRType::A(), "192.0.2.14");
  404. checkRR("host15.example.org", RRType::A(), "192.0.2.15");
  405. }
  406. TEST_F(MasterLoaderTest, generateWithStep) {
  407. const string input =
  408. "$ORIGIN example.org.\n"
  409. "$GENERATE 2-9/2 host$ 3600 A 192.0.2.$\n"
  410. "$GENERATE 12-21/3 host$ 3600 A 192.0.2.$\n"
  411. "$GENERATE 30-31/1 host$ 3600 A 192.0.2.$\n";
  412. stringstream ss(input);
  413. setLoader(ss, Name("example.org."), RRClass::IN(),
  414. MasterLoader::MANY_ERRORS);
  415. loader_->load();
  416. EXPECT_TRUE(loader_->loadedSucessfully());
  417. EXPECT_TRUE(errors_.empty());
  418. checkRR("host2.example.org", RRType::A(), "192.0.2.2");
  419. checkRR("host4.example.org", RRType::A(), "192.0.2.4");
  420. checkRR("host6.example.org", RRType::A(), "192.0.2.6");
  421. checkRR("host8.example.org", RRType::A(), "192.0.2.8");
  422. checkRR("host12.example.org", RRType::A(), "192.0.2.12");
  423. checkRR("host15.example.org", RRType::A(), "192.0.2.15");
  424. checkRR("host18.example.org", RRType::A(), "192.0.2.18");
  425. checkRR("host21.example.org", RRType::A(), "192.0.2.21");
  426. checkRR("host30.example.org", RRType::A(), "192.0.2.30");
  427. checkRR("host31.example.org", RRType::A(), "192.0.2.31");
  428. }
  429. TEST_F(MasterLoaderTest, generateWithModifiers) {
  430. const string input =
  431. "$ORIGIN example.org.\n"
  432. "$TTL 3600\n"
  433. // Use a positive delta of 1 in the LHS and a negative delta of
  434. // -1 in the RHS
  435. "$GENERATE 2-9/2 host${1} A 192.0.2.${-1}\n"
  436. "$GENERATE 10-12 host${0,4} A 192.0.2.$\n"
  437. "$GENERATE 14-15 host${0,4,d} A 192.0.2.$\n"
  438. // Names are case-insensitive, so we use TXT's RDATA to check
  439. // case with hex representation.
  440. "$GENERATE 30-31 host$ TXT \"Value ${0,4,x}\"\n"
  441. "$GENERATE 42-43 host$ TXT \"Value ${0,4,X}\"\n"
  442. // Octal does not use any alphabets
  443. "$GENERATE 45-46 host${0,4,o} A 192.0.2.$\n"
  444. // Here, the LHS has a trailing dot (which would result in an
  445. // out-of-zone name), but that should be handled as a relative
  446. // name.
  447. "$GENERATE 90-92 ${0,8,n} A 192.0.2.$\n"
  448. // Here, the LHS has no trailing dot, and results in the same
  449. // number of labels as width=8 above.
  450. "$GENERATE 94-96 ${0,7,n} A 192.0.2.$\n"
  451. // Names are case-insensitive, so we use TXT's RDATA to check
  452. // case with nibble representation.
  453. "$GENERATE 106-107 host$ TXT \"Value ${0,9,n}\"\n"
  454. "$GENERATE 109-110 host$ TXT \"Value ${0,9,N}\"\n"
  455. // Junk type will not parse and 'd' is assumed.
  456. "$GENERATE 200-201 host${0,4,j} A 192.0.2.$\n";
  457. stringstream ss(input);
  458. setLoader(ss, Name("example.org."), RRClass::IN(),
  459. MasterLoader::MANY_ERRORS);
  460. loader_->load();
  461. EXPECT_TRUE(loader_->loadedSucessfully());
  462. EXPECT_TRUE(errors_.empty());
  463. checkRR("host3.example.org", RRType::A(), "192.0.2.1");
  464. checkRR("host5.example.org", RRType::A(), "192.0.2.3");
  465. checkRR("host7.example.org", RRType::A(), "192.0.2.5");
  466. checkRR("host9.example.org", RRType::A(), "192.0.2.7");
  467. checkRR("host0010.example.org", RRType::A(), "192.0.2.10");
  468. checkRR("host0011.example.org", RRType::A(), "192.0.2.11");
  469. checkRR("host0012.example.org", RRType::A(), "192.0.2.12");
  470. checkRR("host0014.example.org", RRType::A(), "192.0.2.14");
  471. checkRR("host0015.example.org", RRType::A(), "192.0.2.15");
  472. checkRR("host30.example.org", RRType::TXT(), "Value 001e");
  473. checkRR("host31.example.org", RRType::TXT(), "Value 001f");
  474. checkRR("host42.example.org", RRType::TXT(), "Value 002A");
  475. checkRR("host43.example.org", RRType::TXT(), "Value 002B");
  476. checkRR("host0055.example.org", RRType::A(), "192.0.2.45");
  477. checkRR("host0056.example.org", RRType::A(), "192.0.2.46");
  478. checkRR("a.5.0.0.example.org", RRType::A(), "192.0.2.90");
  479. checkRR("b.5.0.0.example.org", RRType::A(), "192.0.2.91");
  480. checkRR("c.5.0.0.example.org", RRType::A(), "192.0.2.92");
  481. checkRR("e.5.0.0.example.org", RRType::A(), "192.0.2.94");
  482. checkRR("f.5.0.0.example.org", RRType::A(), "192.0.2.95");
  483. checkRR("0.6.0.0.example.org", RRType::A(), "192.0.2.96");
  484. checkRR("host106.example.org", RRType::TXT(), "Value a.6.0.0.0");
  485. checkRR("host107.example.org", RRType::TXT(), "Value b.6.0.0.0");
  486. checkRR("host109.example.org", RRType::TXT(), "Value D.6.0.0.0");
  487. checkRR("host110.example.org", RRType::TXT(), "Value E.6.0.0.0");
  488. checkRR("host0200.example.org", RRType::A(), "192.0.2.200");
  489. checkRR("host0201.example.org", RRType::A(), "192.0.2.201");
  490. }
  491. TEST_F(MasterLoaderTest, generateWithNoModifiers) {
  492. const string input =
  493. "$ORIGIN example.org.\n"
  494. "$TTL 3600\n"
  495. "$GENERATE 10-12 host${} A 192.0.2.$\n";
  496. stringstream ss(input);
  497. setLoader(ss, Name("example.org."), RRClass::IN(),
  498. MasterLoader::MANY_ERRORS);
  499. loader_->load();
  500. EXPECT_FALSE(loader_->loadedSucessfully());
  501. ASSERT_EQ(2, errors_.size()); // For the broken GENERATE
  502. EXPECT_TRUE(warnings_.empty());
  503. checkCallbackMessage(errors_.at(0),
  504. "Invalid $GENERATE format modifiers", 3);
  505. checkCallbackMessage(errors_.at(1),
  506. "$GENERATE error", 3);
  507. }
  508. TEST_F(MasterLoaderTest, generateWithBadModifiers) {
  509. const string input =
  510. "$ORIGIN example.org.\n"
  511. "$TTL 3600\n"
  512. "$GENERATE 10-12 host${GARBAGE} A 192.0.2.$\n";
  513. stringstream ss(input);
  514. setLoader(ss, Name("example.org."), RRClass::IN(),
  515. MasterLoader::MANY_ERRORS);
  516. loader_->load();
  517. EXPECT_FALSE(loader_->loadedSucessfully());
  518. ASSERT_EQ(2, errors_.size()); // For the broken GENERATE
  519. EXPECT_TRUE(warnings_.empty());
  520. checkCallbackMessage(errors_.at(0),
  521. "Invalid $GENERATE format modifiers", 3);
  522. checkCallbackMessage(errors_.at(1),
  523. "$GENERATE error", 3);
  524. }
  525. TEST_F(MasterLoaderTest, generateMissingRange) {
  526. const string input =
  527. "$ORIGIN example.org.\n"
  528. "$GENERATE\n";
  529. stringstream ss(input);
  530. setLoader(ss, Name("example.org."), RRClass::IN(),
  531. MasterLoader::MANY_ERRORS);
  532. loader_->load();
  533. EXPECT_FALSE(loader_->loadedSucessfully());
  534. EXPECT_EQ(1, errors_.size()); // For the broken GENERATE
  535. EXPECT_TRUE(warnings_.empty());
  536. checkCallbackMessage(errors_.at(0),
  537. "unexpected end of input", 2);
  538. }
  539. TEST_F(MasterLoaderTest, generateMissingLHS) {
  540. const string input =
  541. "$ORIGIN example.org.\n"
  542. "$GENERATE 2-4\n";
  543. stringstream ss(input);
  544. setLoader(ss, Name("example.org."), RRClass::IN(),
  545. MasterLoader::MANY_ERRORS);
  546. loader_->load();
  547. EXPECT_FALSE(loader_->loadedSucessfully());
  548. EXPECT_EQ(1, errors_.size()); // For the broken GENERATE
  549. EXPECT_TRUE(warnings_.empty());
  550. checkCallbackMessage(errors_.at(0),
  551. "unexpected end of input", 2);
  552. }
  553. TEST_F(MasterLoaderTest, generateMissingType) {
  554. const string input =
  555. "$ORIGIN example.org.\n"
  556. "$GENERATE 2-4 host$\n";
  557. stringstream ss(input);
  558. setLoader(ss, Name("example.org."), RRClass::IN(),
  559. MasterLoader::MANY_ERRORS);
  560. loader_->load();
  561. EXPECT_FALSE(loader_->loadedSucessfully());
  562. EXPECT_EQ(1, errors_.size()); // For the broken GENERATE
  563. EXPECT_TRUE(warnings_.empty());
  564. checkCallbackMessage(errors_.at(0),
  565. "unexpected end of input", 2);
  566. }
  567. TEST_F(MasterLoaderTest, generateMissingRHS) {
  568. const string input =
  569. "$ORIGIN example.org.\n"
  570. "$GENERATE 2-4 host$ A\n";
  571. stringstream ss(input);
  572. setLoader(ss, Name("example.org."), RRClass::IN(),
  573. MasterLoader::MANY_ERRORS);
  574. loader_->load();
  575. EXPECT_FALSE(loader_->loadedSucessfully());
  576. EXPECT_EQ(1, errors_.size()); // For the broken GENERATE
  577. EXPECT_TRUE(warnings_.empty());
  578. checkCallbackMessage(errors_.at(0),
  579. "unexpected end of input", 2);
  580. }
  581. TEST_F(MasterLoaderTest, generateWithBadRangeSyntax) {
  582. const string input =
  583. "$ORIGIN example.org.\n"
  584. "$GENERATE ABCD host$ 3600 A 192.0.2.$\n";
  585. stringstream ss(input);
  586. setLoader(ss, Name("example.org."), RRClass::IN(),
  587. MasterLoader::MANY_ERRORS);
  588. loader_->load();
  589. EXPECT_FALSE(loader_->loadedSucessfully());
  590. EXPECT_EQ(1, errors_.size()); // For the broken GENERATE
  591. EXPECT_TRUE(warnings_.empty());
  592. checkCallbackMessage(errors_.at(0),
  593. "$GENERATE: invalid range: ABCD", 2);
  594. }
  595. TEST_F(MasterLoaderTest, generateWithInvalidRange) {
  596. // start > stop
  597. const string input =
  598. "$ORIGIN example.org.\n"
  599. "$GENERATE 2-1 host$ 3600 A 192.0.2.$\n";
  600. stringstream ss(input);
  601. setLoader(ss, Name("example.org."), RRClass::IN(),
  602. MasterLoader::MANY_ERRORS);
  603. loader_->load();
  604. EXPECT_FALSE(loader_->loadedSucessfully());
  605. EXPECT_EQ(1, errors_.size()); // For the broken GENERATE
  606. EXPECT_TRUE(warnings_.empty());
  607. checkCallbackMessage(errors_.at(0),
  608. "$GENERATE: invalid range: 2-1", 2);
  609. }
  610. TEST_F(MasterLoaderTest, generateWithInvalidClass) {
  611. const string input =
  612. "$ORIGIN example.org.\n"
  613. "$GENERATE 1-2 host$ 3600 CH A 192.0.2.$\n";
  614. stringstream ss(input);
  615. setLoader(ss, Name("example.org."), RRClass::IN(),
  616. MasterLoader::MANY_ERRORS);
  617. loader_->load();
  618. EXPECT_FALSE(loader_->loadedSucessfully());
  619. EXPECT_EQ(1, errors_.size()); // For the broken GENERATE
  620. EXPECT_TRUE(warnings_.empty());
  621. checkCallbackMessage(errors_.at(0),
  622. "Class mismatch: CH vs. IN", 2);
  623. }
  624. TEST_F(MasterLoaderTest, generateWithNoAvailableTTL) {
  625. const string input =
  626. "$ORIGIN example.org.\n"
  627. "$GENERATE 1-2 host$ A 192.0.2.$\n";
  628. stringstream ss(input);
  629. setLoader(ss, Name("example.org."), RRClass::IN(),
  630. MasterLoader::MANY_ERRORS);
  631. loader_->load();
  632. EXPECT_FALSE(loader_->loadedSucessfully());
  633. EXPECT_EQ(1, errors_.size()); // For the broken GENERATE
  634. EXPECT_TRUE(warnings_.empty());
  635. checkCallbackMessage(errors_.at(0),
  636. "no TTL specified; load rejected", 2);
  637. }
  638. // Test the source is correctly popped even after error
  639. TEST_F(MasterLoaderTest, popAfterError) {
  640. const string include_str = "$include " TEST_DATA_SRCDIR
  641. "/broken.zone\nwww 3600 IN AAAA 2001:db8::1\n";
  642. stringstream ss(include_str);
  643. // We perform the test with MANY_ERRORS, we want to see what happens
  644. // after the error.
  645. setLoader(ss, Name("example.org."), RRClass::IN(),
  646. MasterLoader::MANY_ERRORS);
  647. loader_->load();
  648. EXPECT_FALSE(loader_->loadedSucessfully());
  649. EXPECT_EQ(1, errors_.size()); // For the broken RR
  650. EXPECT_EQ(1, warnings_.size()); // For missing EOLN
  651. // The included file doesn't contain anything usable, but the
  652. // line after the include should be there.
  653. checkRR("www.example.org", RRType::AAAA(), "2001:db8::1");
  654. }
  655. // Check it works the same when created based on a stream, not filename
  656. TEST_F(MasterLoaderTest, streamConstructor) {
  657. const string zone_data(prepareZone("", true));
  658. stringstream zone_stream(zone_data);
  659. setLoader(zone_stream, Name("example.org."), RRClass::IN(),
  660. MasterLoader::MANY_ERRORS);
  661. EXPECT_FALSE(loader_->loadedSucessfully());
  662. // Unlike the basicLoad test, if we construct the loader from a stream
  663. // getSize() returns the data size in the stream immediately after the
  664. // construction.
  665. EXPECT_EQ(zone_data.size(), loader_->getSize());
  666. EXPECT_EQ(0, loader_->getPosition());
  667. loader_->load();
  668. EXPECT_TRUE(loader_->loadedSucessfully());
  669. EXPECT_TRUE(errors_.empty());
  670. EXPECT_TRUE(warnings_.empty());
  671. checkRR("example.org", RRType::SOA(), "ns1.example.org. "
  672. "admin.example.org. 1234 3600 1800 2419200 7200");
  673. checkRR("correct.example.org", RRType::A(), "192.0.2.2");
  674. // On completion of the load, both getSize() and getPosition() return the
  675. // size of the data.
  676. EXPECT_EQ(zone_data.size(), loader_->getSize());
  677. EXPECT_EQ(zone_data.size(), loader_->getPosition());
  678. }
  679. // Try loading data incrementally.
  680. TEST_F(MasterLoaderTest, incrementalLoad) {
  681. setLoader(TEST_DATA_SRCDIR "/example.org", Name("example.org."),
  682. RRClass::IN(), MasterLoader::MANY_ERRORS);
  683. EXPECT_FALSE(loader_->loadedSucessfully());
  684. EXPECT_FALSE(loader_->loadIncremental(2));
  685. EXPECT_FALSE(loader_->loadedSucessfully());
  686. EXPECT_TRUE(errors_.empty());
  687. EXPECT_TRUE(warnings_.empty());
  688. checkRR("example.org", RRType::SOA(),
  689. "ns1.example.org. admin.example.org. "
  690. "1234 3600 1800 2419200 7200");
  691. checkRR("example.org", RRType::NS(), "ns1.example.org.");
  692. // The third one is not loaded yet
  693. EXPECT_TRUE(rrsets_.empty());
  694. // Load the rest.
  695. EXPECT_TRUE(loader_->loadIncremental(20));
  696. EXPECT_TRUE(loader_->loadedSucessfully());
  697. EXPECT_TRUE(errors_.empty());
  698. EXPECT_TRUE(warnings_.empty());
  699. checkRR("www.example.org", RRType::A(), "192.0.2.1");
  700. checkRR("www.example.org", RRType::AAAA(), "2001:db8::1");
  701. }
  702. // Try loading from file that doesn't exist. There should be single error
  703. // saying so.
  704. TEST_F(MasterLoaderTest, invalidFile) {
  705. setLoader("This file doesn't exist at all",
  706. Name("exmaple.org."), RRClass::IN(), MasterLoader::MANY_ERRORS);
  707. // Nothing yet. The loader is dormant until invoked.
  708. // Is it really what we want?
  709. EXPECT_TRUE(errors_.empty());
  710. loader_->load();
  711. EXPECT_TRUE(warnings_.empty());
  712. EXPECT_TRUE(rrsets_.empty());
  713. ASSERT_EQ(1, errors_.size());
  714. EXPECT_EQ(0, errors_[0].find("Error opening the input source file: ")) <<
  715. "Different error: " << errors_[0];
  716. }
  717. struct ErrorCase {
  718. const char* const line; // The broken line in master file
  719. const char* const reason; // If non NULL, the reason string
  720. const char* const problem; // Description of the problem for SCOPED_TRACE
  721. } const error_cases[] = {
  722. { "www... 3600 IN A 192.0.2.1", NULL, "Invalid name" },
  723. { "www FORTNIGHT IN A 192.0.2.1", NULL, "Invalid TTL" },
  724. { "www 3600 XX A 192.0.2.1", NULL, "Invalid class" },
  725. { "www 3600 IN A bad_ip", NULL, "Invalid Rdata" },
  726. // Parameter ordering errors
  727. { "www IN A 3600 192.168.2.7",
  728. "createRdata from text failed: Bad IN/A RDATA text: '3600'",
  729. "Incorrect order of class, TTL and type" },
  730. { "www A IN 3600 192.168.2.8",
  731. "createRdata from text failed: Bad IN/A RDATA text: 'IN'",
  732. "Incorrect order of class, TTL and type" },
  733. { "www 3600 A IN 192.168.2.7",
  734. "createRdata from text failed: Bad IN/A RDATA text: 'IN'",
  735. "Incorrect order of class, TTL and type" },
  736. { "www A 3600 IN 192.168.2.8",
  737. "createRdata from text failed: Bad IN/A RDATA text: '3600'",
  738. "Incorrect order of class, TTL and type" },
  739. // Missing type and Rdata
  740. { "www", "unexpected end of input", "Missing type and Rdata" },
  741. { "www 3600", "unexpected end of input", "Missing type and Rdata" },
  742. { "www IN", "unexpected end of input", "Missing type and Rdata" },
  743. { "www 3600 IN", "unexpected end of input", "Missing type and Rdata" },
  744. { "www IN 3600", "unexpected end of input", "Missing type and Rdata" },
  745. // Missing Rdata
  746. { "www A",
  747. "createRdata from text failed: unexpected end of input",
  748. "Missing Rdata" },
  749. { "www 3600 A",
  750. "createRdata from text failed: unexpected end of input",
  751. "Missing Rdata" },
  752. { "www IN A",
  753. "createRdata from text failed: unexpected end of input",
  754. "Missing Rdata" },
  755. { "www 3600 IN A",
  756. "createRdata from text failed: unexpected end of input",
  757. "Missing Rdata" },
  758. { "www IN 3600 A",
  759. "createRdata from text failed: unexpected end of input",
  760. "Missing Rdata" },
  761. { "www 3600 IN", NULL, "Unexpected EOLN" },
  762. { "www 3600 CH TXT nothing", "Class mismatch: CH vs. IN",
  763. "Class mismatch" },
  764. { "www \"3600\" IN A 192.0.2.1", NULL, "Quoted TTL" },
  765. { "www 3600 \"IN\" A 192.0.2.1", NULL, "Quoted class" },
  766. { "www 3600 IN \"A\" 192.0.2.1", NULL, "Quoted type" },
  767. { "unbalanced)paren 3600 IN A 192.0.2.1", NULL, "Token error 1" },
  768. { "www 3600 unbalanced)paren A 192.0.2.1", NULL,
  769. "Token error 2" },
  770. // Check the unknown directive. The rest looks like ordinary RR,
  771. // so we see the $ is actually special.
  772. { "$UNKNOWN 3600 IN A 192.0.2.1", NULL, "Unknown $ directive" },
  773. { "$INCLUD " TEST_DATA_SRCDIR "/example.org", "Unknown directive 'INCLUD'",
  774. "Include too short" },
  775. { "$INCLUDES " TEST_DATA_SRCDIR "/example.org",
  776. "Unknown directive 'INCLUDES'", "Include too long" },
  777. { "$INCLUDE", "unexpected end of input", "Missing include path" },
  778. // The following two error messages are system dependant, omitting
  779. { "$INCLUDE /file/not/found", NULL, "Include file not found" },
  780. { "$INCLUDE /file/not/found example.org. and here goes bunch of garbage",
  781. NULL, "Include file not found and garbage at the end of line" },
  782. { "$ORIGIN", "unexpected end of input", "Missing origin name" },
  783. { "$ORIGIN invalid...name", "duplicate period in invalid...name",
  784. "Invalid name for origin" },
  785. { "$ORIGIN )brokentoken", "unbalanced parentheses",
  786. "Broken token in origin" },
  787. { "$ORIGIN example.org. garbage", "Extra tokens at the end of line",
  788. "Garbage after origin" },
  789. { "$ORIGI name.", "Unknown directive 'ORIGI'", "$ORIGIN too short" },
  790. { "$ORIGINAL name.", "Unknown directive 'ORIGINAL'", "$ORIGIN too long" },
  791. { "$TTL 100 extra-garbage", "Extra tokens at the end of line",
  792. "$TTL with extra token" },
  793. { "$TTL", "unexpected end of input", "missing TTL" },
  794. { "$TTL No-ttl", "Unknown unit used: N in: No-ttl", "bad TTL" },
  795. { "$TTL \"100\"", "invalid TTL: \"100\"", "bad TTL, quoted" },
  796. { "$TT 100", "Unknown directive 'TT'", "bad directive, too short" },
  797. { "$TTLLIKE 100", "Unknown directive 'TTLLIKE'", "bad directive, extra" },
  798. { NULL, NULL, NULL }
  799. };
  800. // Test a broken zone is handled properly. We test several problems,
  801. // both in strict and lenient mode.
  802. TEST_F(MasterLoaderTest, brokenZone) {
  803. for (const ErrorCase* ec = error_cases; ec->line != NULL; ++ec) {
  804. SCOPED_TRACE(ec->problem);
  805. const string zone(prepareZone(ec->line, true));
  806. {
  807. SCOPED_TRACE("Strict mode");
  808. clear();
  809. stringstream zone_stream(zone);
  810. setLoader(zone_stream, Name("example.org."), RRClass::IN(),
  811. MasterLoader::DEFAULT);
  812. EXPECT_FALSE(loader_->loadedSucessfully());
  813. EXPECT_THROW(loader_->load(), MasterLoaderError);
  814. EXPECT_FALSE(loader_->loadedSucessfully());
  815. EXPECT_EQ(1, errors_.size());
  816. if (ec->reason != NULL) {
  817. checkCallbackMessage(errors_.at(0), ec->reason, 2);
  818. }
  819. EXPECT_TRUE(warnings_.empty());
  820. checkRR("example.org", RRType::SOA(), "ns1.example.org. "
  821. "admin.example.org. 1234 3600 1800 2419200 7200");
  822. // In the strict mode, it is aborted. The last RR is not
  823. // even attempted.
  824. EXPECT_TRUE(rrsets_.empty());
  825. }
  826. {
  827. SCOPED_TRACE("Lenient mode");
  828. clear();
  829. stringstream zone_stream(zone);
  830. setLoader(zone_stream, Name("example.org."), RRClass::IN(),
  831. MasterLoader::MANY_ERRORS);
  832. EXPECT_FALSE(loader_->loadedSucessfully());
  833. EXPECT_NO_THROW(loader_->load());
  834. EXPECT_FALSE(loader_->loadedSucessfully());
  835. EXPECT_EQ(1, errors_.size());
  836. EXPECT_TRUE(warnings_.empty());
  837. checkRR("example.org", RRType::SOA(), "ns1.example.org. "
  838. "admin.example.org. 1234 3600 1800 2419200 7200");
  839. // This one is below the error one.
  840. checkRR("correct.example.org", RRType::A(), "192.0.2.2");
  841. EXPECT_TRUE(rrsets_.empty());
  842. }
  843. {
  844. SCOPED_TRACE("Error at EOF");
  845. // This case is interesting only in the lenient mode.
  846. clear();
  847. const string zoneEOF(prepareZone(ec->line, false));
  848. stringstream zone_stream(zoneEOF);
  849. setLoader(zone_stream, Name("example.org."), RRClass::IN(),
  850. MasterLoader::MANY_ERRORS);
  851. EXPECT_FALSE(loader_->loadedSucessfully());
  852. EXPECT_NO_THROW(loader_->load());
  853. EXPECT_FALSE(loader_->loadedSucessfully());
  854. EXPECT_EQ(1, errors_.size()) << errors_[0] << "\n" << errors_[1];
  855. // The unexpected EOF warning
  856. EXPECT_EQ(1, warnings_.size());
  857. checkRR("example.org", RRType::SOA(), "ns1.example.org. "
  858. "admin.example.org. 1234 3600 1800 2419200 7200");
  859. EXPECT_TRUE(rrsets_.empty());
  860. }
  861. }
  862. }
  863. // Check that a garbage after the include generates an error, but not fatal
  864. // one (in lenient mode) and we can recover.
  865. TEST_F(MasterLoaderTest, includeWithGarbage) {
  866. // Include an origin (example.org) because we expect it to be handled
  867. // soon and we don't want it to break here.
  868. const string include_str("$INCLUDE " TEST_DATA_SRCDIR
  869. "/example.org example.org. bunch of other stuff\n"
  870. "www 3600 IN AAAA 2001:db8::1\n");
  871. stringstream zone_stream(include_str);
  872. setLoader(zone_stream, Name("example.org."), RRClass::IN(),
  873. MasterLoader::MANY_ERRORS);
  874. EXPECT_NO_THROW(loader_->load());
  875. EXPECT_FALSE(loader_->loadedSucessfully());
  876. ASSERT_EQ(1, errors_.size());
  877. checkCallbackMessage(errors_.at(0), "Extra tokens at the end of line", 1);
  878. // It says something about extra tokens at the end
  879. EXPECT_NE(string::npos, errors_[0].find("Extra"));
  880. EXPECT_TRUE(warnings_.empty());
  881. checkBasicRRs();
  882. checkRR("www.example.org", RRType::AAAA(), "2001:db8::1");
  883. }
  884. // Check we error about garbage at the end of $ORIGIN line (but the line
  885. // works).
  886. TEST_F(MasterLoaderTest, originWithGarbage) {
  887. const string origin_str = "$ORIGIN www.example.org. More garbage here\n"
  888. "@ 1H IN A 192.0.2.1\n";
  889. stringstream ss(origin_str);
  890. setLoader(ss, Name("example.org."), RRClass::IN(),
  891. MasterLoader::MANY_ERRORS);
  892. EXPECT_NO_THROW(loader_->load());
  893. EXPECT_FALSE(loader_->loadedSucessfully());
  894. ASSERT_EQ(1, errors_.size());
  895. checkCallbackMessage(errors_.at(0), "Extra tokens at the end of line", 1);
  896. EXPECT_TRUE(warnings_.empty());
  897. checkARR("www.example.org");
  898. }
  899. // Test we can pass both file to include and the origin to switch
  900. TEST_F(MasterLoaderTest, includeAndOrigin) {
  901. // First, switch origin to something else, so we can check it is
  902. // switched back.
  903. const string include_string = "$ORIGIN www.example.org.\n"
  904. "@ 1H IN A 192.0.2.1\n"
  905. // Then include the file with data and switch origin back
  906. "$INCLUDE " TEST_DATA_SRCDIR "/example.org example.org.\n"
  907. // Another RR to see we fall back to the previous origin.
  908. "www 1H IN A 192.0.2.1\n";
  909. stringstream ss(include_string);
  910. setLoader(ss, Name("example.org"), RRClass::IN(),
  911. MasterLoader::MANY_ERRORS);
  912. // Successfully load the data
  913. loader_->load();
  914. EXPECT_TRUE(loader_->loadedSucessfully());
  915. EXPECT_TRUE(errors_.empty());
  916. EXPECT_TRUE(warnings_.empty());
  917. // And check it's the correct data
  918. checkARR("www.example.org");
  919. checkBasicRRs();
  920. checkARR("www.www.example.org");
  921. }
  922. // Like above, but the origin after include is bogus. The whole line should
  923. // be rejected.
  924. TEST_F(MasterLoaderTest, includeAndBadOrigin) {
  925. const string include_string =
  926. "$INCLUDE " TEST_DATA_SRCDIR "/example.org example..org.\n"
  927. // Another RR to see the switch survives after we exit include
  928. "www 1H IN A 192.0.2.1\n";
  929. stringstream ss(include_string);
  930. setLoader(ss, Name("example.org"), RRClass::IN(),
  931. MasterLoader::MANY_ERRORS);
  932. loader_->load();
  933. EXPECT_FALSE(loader_->loadedSucessfully());
  934. EXPECT_EQ(1, errors_.size());
  935. checkCallbackMessage(errors_.at(0), "duplicate period in example..org.",
  936. 1);
  937. EXPECT_TRUE(warnings_.empty());
  938. // And check it's the correct data
  939. checkARR("www.example.org");
  940. }
  941. // Check the origin doesn't get outside of the included file.
  942. TEST_F(MasterLoaderTest, includeOriginRestore) {
  943. const string include_string =
  944. "$INCLUDE " TEST_DATA_SRCDIR "/origincheck.txt\n"
  945. "@ 1H IN A 192.0.2.1\n";
  946. stringstream ss(include_string);
  947. setLoader(ss, Name("example.org"), RRClass::IN(),
  948. MasterLoader::MANY_ERRORS);
  949. // Successfully load the data
  950. loader_->load();
  951. EXPECT_TRUE(loader_->loadedSucessfully());
  952. EXPECT_TRUE(errors_.empty());
  953. EXPECT_TRUE(warnings_.empty());
  954. // And check it's the correct data
  955. checkARR("www.example.org");
  956. checkARR("example.org");
  957. }
  958. // Check we restore the last name for initial whitespace when returning from
  959. // include. But we do produce a warning if there's one just ofter the include.
  960. TEST_F(MasterLoaderTest, includeAndInitialWS) {
  961. const string include_string = "xyz 1H IN A 192.0.2.1\n"
  962. "$INCLUDE " TEST_DATA_SRCDIR "/example.org\n"
  963. " 1H IN A 192.0.2.1\n";
  964. stringstream ss(include_string);
  965. setLoader(ss, Name("example.org"), RRClass::IN(),
  966. MasterLoader::MANY_ERRORS);
  967. // Successfully load the data
  968. loader_->load();
  969. EXPECT_TRUE(loader_->loadedSucessfully());
  970. EXPECT_TRUE(errors_.empty());
  971. EXPECT_EQ(1, warnings_.size());
  972. checkCallbackMessage(warnings_.at(0),
  973. "Owner name omitted around $INCLUDE, the result might "
  974. "not be as expected", 3);
  975. checkARR("xyz.example.org");
  976. checkBasicRRs();
  977. checkARR("xyz.example.org");
  978. }
  979. // Test for "$TTL"
  980. TEST_F(MasterLoaderTest, ttlDirective) {
  981. stringstream zone_stream;
  982. // Set the default TTL with $TTL followed by an RR omitting the TTL
  983. zone_stream << "$TTL 1800\nexample.org. IN A 192.0.2.1\n";
  984. // $TTL can be quoted. Also testing the case of $TTL being changed.
  985. zone_stream << "\"$TTL\" 100\na.example.org. IN A 192.0.2.2\n";
  986. // Extended TTL form is accepted.
  987. zone_stream << "$TTL 1H\nb.example.org. IN A 192.0.2.3\n";
  988. // Matching is case insensitive.
  989. zone_stream << "$tTl 360\nc.example.org. IN A 192.0.2.4\n";
  990. // Maximum allowable TTL
  991. zone_stream << "$TTL 2147483647\nd.example.org. IN A 192.0.2.5\n";
  992. setLoader(zone_stream, Name("example.org."), RRClass::IN(),
  993. MasterLoader::DEFAULT);
  994. loader_->load();
  995. EXPECT_TRUE(loader_->loadedSucessfully());
  996. checkRR("example.org", RRType::A(), "192.0.2.1", RRTTL(1800));
  997. checkRR("a.example.org", RRType::A(), "192.0.2.2", RRTTL(100));
  998. checkRR("b.example.org", RRType::A(), "192.0.2.3", RRTTL(3600));
  999. checkRR("c.example.org", RRType::A(), "192.0.2.4", RRTTL(360));
  1000. checkRR("d.example.org", RRType::A(), "192.0.2.5", RRTTL(2147483647));
  1001. }
  1002. TEST_F(MasterLoaderTest, ttlFromSOA) {
  1003. // No $TTL, and the SOA doesn't have an explicit TTL field. Its minimum
  1004. // TTL field will be used as the RR's TTL, and it'll be used as the
  1005. // default TTL for others.
  1006. stringstream zone_stream("example.org. IN SOA . . 0 0 0 0 1800\n"
  1007. "a.example.org. IN A 192.0.2.1\n");
  1008. setLoader(zone_stream, Name("example.org."), RRClass::IN(),
  1009. MasterLoader::DEFAULT);
  1010. loader_->load();
  1011. EXPECT_TRUE(loader_->loadedSucessfully());
  1012. checkRR("example.org", RRType::SOA(), ". . 0 0 0 0 1800", RRTTL(1800));
  1013. checkRR("a.example.org", RRType::A(), "192.0.2.1", RRTTL(1800));
  1014. // The use of SOA minimum TTL should have caused a warning.
  1015. EXPECT_EQ(1, warnings_.size());
  1016. checkCallbackMessage(warnings_.at(0),
  1017. "no TTL specified; using SOA MINTTL instead", 1);
  1018. }
  1019. TEST_F(MasterLoaderTest, ttlFromPrevious) {
  1020. // No available default TTL. 2nd and 3rd RR will use the TTL of the
  1021. // 1st RR. This will result in a warning, but only for the first time.
  1022. stringstream zone_stream("a.example.org. 1800 IN A 192.0.2.1\n"
  1023. "b.example.org. IN A 192.0.2.2\n"
  1024. "c.example.org. IN A 192.0.2.3\n");
  1025. setLoader(zone_stream, Name("example.org."), RRClass::IN(),
  1026. MasterLoader::DEFAULT);
  1027. loader_->load();
  1028. EXPECT_TRUE(loader_->loadedSucessfully());
  1029. checkRR("a.example.org", RRType::A(), "192.0.2.1", RRTTL(1800));
  1030. checkRR("b.example.org", RRType::A(), "192.0.2.2", RRTTL(1800));
  1031. checkRR("c.example.org", RRType::A(), "192.0.2.3", RRTTL(1800));
  1032. EXPECT_EQ(1, warnings_.size());
  1033. checkCallbackMessage(warnings_.at(0), "using RFC1035 TTL semantics", 2);
  1034. }
  1035. TEST_F(MasterLoaderTest, RRParamsOrdering) {
  1036. // We test the order and existence of TTL, class and type. See
  1037. // MasterLoader::MasterLoaderImpl::parseRRParams() for ordering.
  1038. stringstream zone_stream;
  1039. // <TTL> <class> <type> <RDATA>
  1040. zone_stream << "a.example.org. 1800 IN A 192.0.2.1\n";
  1041. // <type> <RDATA>
  1042. zone_stream << "b.example.org. A 192.0.2.2\n";
  1043. // <class> <TTL> <type> <RDATA>
  1044. zone_stream << "c.example.org. IN 3600 A 192.0.2.3\n";
  1045. // <TTL> <type> <RDATA>
  1046. zone_stream << "d.example.org. 7200 A 192.0.2.4\n";
  1047. // <class> <type> <RDATA>
  1048. zone_stream << "e.example.org. IN A 192.0.2.5\n";
  1049. setLoader(zone_stream, Name("example.org."), RRClass::IN(),
  1050. MasterLoader::DEFAULT);
  1051. loader_->load();
  1052. EXPECT_TRUE(loader_->loadedSucessfully());
  1053. checkRR("a.example.org", RRType::A(), "192.0.2.1", RRTTL(1800));
  1054. checkRR("b.example.org", RRType::A(), "192.0.2.2", RRTTL(1800));
  1055. checkRR("c.example.org", RRType::A(), "192.0.2.3", RRTTL(3600));
  1056. checkRR("d.example.org", RRType::A(), "192.0.2.4", RRTTL(7200));
  1057. checkRR("e.example.org", RRType::A(), "192.0.2.5", RRTTL(7200));
  1058. EXPECT_EQ(1, warnings_.size());
  1059. checkCallbackMessage(warnings_.at(0), "using RFC1035 TTL semantics", 2);
  1060. }
  1061. TEST_F(MasterLoaderTest, ttlFromPreviousSOA) {
  1062. // Mixture of the previous two cases: SOA has explicit TTL, followed by
  1063. // an RR without an explicit TTL. In this case the minimum TTL won't be
  1064. // recognized as the "default TTL".
  1065. stringstream zone_stream("example.org. 100 IN SOA . . 0 0 0 0 1800\n"
  1066. "a.example.org. IN A 192.0.2.1\n");
  1067. setLoader(zone_stream, Name("example.org."), RRClass::IN(),
  1068. MasterLoader::DEFAULT);
  1069. loader_->load();
  1070. EXPECT_TRUE(loader_->loadedSucessfully());
  1071. checkRR("example.org", RRType::SOA(), ". . 0 0 0 0 1800", RRTTL(100));
  1072. checkRR("a.example.org", RRType::A(), "192.0.2.1", RRTTL(100));
  1073. EXPECT_EQ(1, warnings_.size());
  1074. checkCallbackMessage(warnings_.at(0), "using RFC1035 TTL semantics", 2);
  1075. }
  1076. TEST_F(MasterLoaderTest, ttlUnknown) {
  1077. // No available TTL is known for the first RR.
  1078. stringstream zone_stream("a.example.org. IN A 192.0.2.1\n");
  1079. setLoader(zone_stream, Name("example.org."), RRClass::IN(),
  1080. MasterLoader::DEFAULT);
  1081. EXPECT_THROW(loader_->load(), MasterLoaderError);
  1082. }
  1083. TEST_F(MasterLoaderTest, ttlUnknownAndContinue) {
  1084. stringstream zone_stream("a.example.org. IN A 192.0.2.1\n"
  1085. "b.example.org. 1800 IN A 192.0.2.2\n");
  1086. setLoader(zone_stream, Name("example.org."), RRClass::IN(),
  1087. MasterLoader::MANY_ERRORS);
  1088. loader_->load();
  1089. EXPECT_FALSE(loader_->loadedSucessfully());
  1090. checkRR("b.example.org", RRType::A(), "192.0.2.2", RRTTL(1800));
  1091. EXPECT_TRUE(warnings_.empty());
  1092. EXPECT_EQ(1, errors_.size());
  1093. checkCallbackMessage(errors_.at(0), "no TTL specified; load rejected", 1);
  1094. }
  1095. TEST_F(MasterLoaderTest, ttlUnknownAndEOF) {
  1096. // Similar to the previous case, but the input will be abruptly terminated
  1097. // after the offending RR. This will cause an additional warning.
  1098. stringstream zone_stream("a.example.org. IN A 192.0.2.1");
  1099. setLoader(zone_stream, Name("example.org."), RRClass::IN(),
  1100. MasterLoader::MANY_ERRORS);
  1101. loader_->load();
  1102. EXPECT_FALSE(loader_->loadedSucessfully());
  1103. EXPECT_TRUE(rrsets_.empty());
  1104. EXPECT_EQ(1, errors_.size());
  1105. checkCallbackMessage(errors_.at(0), "no TTL specified; load rejected", 1);
  1106. // RDATA implementation can complain about it, too. To be independent of
  1107. // its details, we focus on the very last warning.
  1108. EXPECT_FALSE(warnings_.empty());
  1109. checkCallbackMessage(*warnings_.rbegin(), "File does not end with newline",
  1110. 1);
  1111. }
  1112. TEST_F(MasterLoaderTest, ttlOverflow) {
  1113. stringstream zone_stream;
  1114. zone_stream << "example.org. IN SOA . . 0 0 0 0 2147483648\n";
  1115. zone_stream << "$TTL 3600\n"; // reset to an in-range value
  1116. zone_stream << "$TTL 2147483649\n" << "a.example.org. IN A 192.0.2.1\n";
  1117. zone_stream << "$TTL 3600\n"; // reset to an in-range value
  1118. zone_stream << "b.example.org. 2147483650 IN A 192.0.2.2\n";
  1119. setLoader(zone_stream, Name("example.org."), RRClass::IN(),
  1120. MasterLoader::DEFAULT);
  1121. loader_->load();
  1122. EXPECT_TRUE(loader_->loadedSucessfully());
  1123. EXPECT_EQ(3, rrsets_.size());
  1124. checkRR("example.org", RRType::SOA(), ". . 0 0 0 0 2147483648", RRTTL(0));
  1125. checkRR("a.example.org", RRType::A(), "192.0.2.1", RRTTL(0));
  1126. checkRR("b.example.org", RRType::A(), "192.0.2.2", RRTTL(0));
  1127. EXPECT_EQ(4, warnings_.size());
  1128. checkCallbackMessage(warnings_.at(1),
  1129. "TTL 2147483648 > MAXTTL, setting to 0 per RFC2181",
  1130. 1);
  1131. checkCallbackMessage(warnings_.at(2),
  1132. "TTL 2147483649 > MAXTTL, setting to 0 per RFC2181",
  1133. 3);
  1134. checkCallbackMessage(warnings_.at(3),
  1135. "TTL 2147483650 > MAXTTL, setting to 0 per RFC2181",
  1136. 6);
  1137. }
  1138. // Test the constructor rejects empty add callback.
  1139. TEST_F(MasterLoaderTest, emptyCallback) {
  1140. EXPECT_THROW(MasterLoader(TEST_DATA_SRCDIR "/example.org",
  1141. Name("example.org"), RRClass::IN(), callbacks_,
  1142. AddRRCallback()), isc::InvalidParameter);
  1143. // And the same with the second constructor
  1144. stringstream ss("");
  1145. EXPECT_THROW(MasterLoader(ss, Name("example.org"), RRClass::IN(),
  1146. callbacks_, AddRRCallback()),
  1147. isc::InvalidParameter);
  1148. }
  1149. // Check it throws when we try to load after loading was complete.
  1150. TEST_F(MasterLoaderTest, loadTwice) {
  1151. setLoader(TEST_DATA_SRCDIR "/example.org", Name("example.org."),
  1152. RRClass::IN(), MasterLoader::MANY_ERRORS);
  1153. loader_->load();
  1154. EXPECT_THROW(loader_->load(), isc::InvalidOperation);
  1155. // Don't check them, they are not interesting, so suppress the error
  1156. // at TearDown
  1157. rrsets_.clear();
  1158. }
  1159. // Load 0 items should be rejected
  1160. TEST_F(MasterLoaderTest, loadZero) {
  1161. setLoader(TEST_DATA_SRCDIR "/example.org", Name("example.org."),
  1162. RRClass::IN(), MasterLoader::MANY_ERRORS);
  1163. EXPECT_THROW(loader_->loadIncremental(0), isc::InvalidParameter);
  1164. }
  1165. // Test there's a warning when the file terminates without end of
  1166. // line.
  1167. TEST_F(MasterLoaderTest, noEOLN) {
  1168. // No \n at the end
  1169. const string input("example.org. 3600 IN SOA ns1.example.org. "
  1170. "admin.example.org. 1234 3600 1800 2419200 7200");
  1171. stringstream ss(input);
  1172. setLoader(ss, Name("example.org."), RRClass::IN(),
  1173. MasterLoader::MANY_ERRORS);
  1174. loader_->load();
  1175. EXPECT_TRUE(loader_->loadedSucessfully());
  1176. EXPECT_TRUE(errors_.empty());
  1177. // There should be one warning about the EOLN
  1178. EXPECT_EQ(1, warnings_.size());
  1179. checkRR("example.org", RRType::SOA(), "ns1.example.org. "
  1180. "admin.example.org. 1234 3600 1800 2419200 7200");
  1181. }
  1182. // Test it rejects when we don't have the previous name to use in place of
  1183. // initial whitespace
  1184. TEST_F(MasterLoaderTest, noPreviousName) {
  1185. const string input(" 1H IN A 192.0.2.1\n");
  1186. stringstream ss(input);
  1187. setLoader(ss, Name("example.org."), RRClass::IN(),
  1188. MasterLoader::MANY_ERRORS);
  1189. loader_->load();
  1190. EXPECT_FALSE(loader_->loadedSucessfully());
  1191. EXPECT_EQ(1, errors_.size());
  1192. checkCallbackMessage(errors_.at(0), "No previous name to use in place of "
  1193. "initial whitespace", 1);
  1194. EXPECT_TRUE(warnings_.empty());
  1195. }
  1196. // Check we warn if the first RR in an included file has omitted name
  1197. TEST_F(MasterLoaderTest, previousInInclude) {
  1198. const string input("www 1H IN A 192.0.2.1\n"
  1199. "$INCLUDE " TEST_DATA_SRCDIR "/omitcheck.txt\n");
  1200. stringstream ss(input);
  1201. setLoader(ss, Name("example.org"), RRClass::IN(),
  1202. MasterLoader::MANY_ERRORS);
  1203. loader_->load();
  1204. EXPECT_TRUE(loader_->loadedSucessfully());
  1205. EXPECT_TRUE(errors_.empty());
  1206. // There should be one warning about the EOLN
  1207. EXPECT_EQ(1, warnings_.size());
  1208. checkCallbackMessage(warnings_.at(0), "Owner name omitted around "
  1209. "$INCLUDE, the result might not be as expected", 1);
  1210. checkARR("www.example.org");
  1211. checkARR("www.example.org");
  1212. }
  1213. }