zone_loader_unittest.cc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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 <datasrc/zone_loader.h>
  15. #include <datasrc/data_source.h>
  16. #include <datasrc/memory/zone_table_segment.h>
  17. #include <datasrc/memory/memory_client.h>
  18. #include <dns/rrclass.h>
  19. #include <dns/name.h>
  20. #include <dns/rrset.h>
  21. #include <dns/rdataclass.h>
  22. #include <util/memory_segment_local.h>
  23. #include <exceptions/exceptions.h>
  24. #include <gtest/gtest.h>
  25. #include <boost/shared_ptr.hpp>
  26. #include <boost/scoped_ptr.hpp>
  27. #include <boost/foreach.hpp>
  28. #include <string>
  29. #include <vector>
  30. using namespace isc::dns;
  31. using namespace isc::datasrc;
  32. using boost::shared_ptr;
  33. using std::string;
  34. using std::vector;
  35. namespace {
  36. class MockClient : public DataSourceClient {
  37. public:
  38. MockClient() :
  39. commit_called_(false),
  40. missing_zone_(false),
  41. rrclass_(RRClass::IN())
  42. {}
  43. class Finder : public ZoneFinder {
  44. public:
  45. Finder(const Name& origin) :
  46. origin_(origin)
  47. {}
  48. Name getOrigin() const {
  49. return (origin_);
  50. }
  51. RRClass getClass() const {
  52. return (RRClass::IN());
  53. }
  54. // The rest is not to be called, so they throw.
  55. shared_ptr<Context> find(const Name&, const RRType&,
  56. const FindOptions)
  57. {
  58. isc_throw(isc::NotImplemented, "Not implemented");
  59. }
  60. shared_ptr<Context> findAll(const Name&,
  61. vector<ConstRRsetPtr>&,
  62. const FindOptions)
  63. {
  64. isc_throw(isc::NotImplemented, "Not implemented");
  65. }
  66. FindNSEC3Result findNSEC3(const Name&, bool) {
  67. isc_throw(isc::NotImplemented, "Not implemented");
  68. }
  69. private:
  70. Name origin_;
  71. };
  72. class Iterator : public ZoneIterator {
  73. public:
  74. Iterator(const Name& origin) :
  75. origin_(origin),
  76. soa_(new RRset(origin_, RRClass::IN(), RRType::SOA(),
  77. RRTTL(3600)))
  78. {
  79. // The RData here is bogus, but it is not used to anything. There
  80. // just needs to be some.
  81. soa_->addRdata(rdata::generic::SOA(Name::ROOT_NAME(),
  82. Name::ROOT_NAME(),
  83. 0, 0, 0, 0, 0));
  84. rrsets_.push_back(soa_);
  85. // There is no NS record on purpose here.
  86. // Dummy A rrset. This is used for checking zone data after
  87. // reload.
  88. RRsetPtr rrset(new RRset(Name("tstzonedata").concatenate(origin_),
  89. RRClass::IN(), RRType::A(),
  90. RRTTL(3600)));
  91. rrset->addRdata(rdata::in::A("192.0.2.1"));
  92. rrsets_.push_back(rrset);
  93. rrsets_.push_back(ConstRRsetPtr());
  94. it_ = rrsets_.begin();
  95. }
  96. virtual isc::dns::ConstRRsetPtr getNextRRset() {
  97. ConstRRsetPtr result = *it_;
  98. ++it_;
  99. return (result);
  100. }
  101. virtual isc::dns::ConstRRsetPtr getSOA() const {
  102. return (soa_);
  103. }
  104. private:
  105. const Name origin_;
  106. const RRsetPtr soa_;
  107. std::vector<ConstRRsetPtr> rrsets_;
  108. std::vector<ConstRRsetPtr>::const_iterator it_;
  109. };
  110. virtual ZoneIteratorPtr getIterator(const isc::dns::Name& name,
  111. bool) const
  112. {
  113. if (name != Name("example.org")) {
  114. isc_throw(DataSourceError, "No such zone");
  115. }
  116. return (ZoneIteratorPtr(new Iterator(Name("example.org"))));
  117. }
  118. virtual FindResult findZone(const Name& name) const {
  119. const Name origin("example.org");
  120. const ZoneFinderPtr finder(new Finder(origin));
  121. NameComparisonResult compar(origin.compare(name));
  122. switch (compar.getRelation()) {
  123. case NameComparisonResult::EQUAL:
  124. return (FindResult(result::SUCCESS, finder));
  125. case NameComparisonResult::SUPERDOMAIN:
  126. return (FindResult(result::PARTIALMATCH, finder));
  127. default:
  128. return (FindResult(result::NOTFOUND, ZoneFinderPtr()));
  129. }
  130. };
  131. virtual std::pair<ZoneJournalReader::Result, ZoneJournalReaderPtr>
  132. getJournalReader(const Name&, uint32_t, uint32_t) const
  133. {
  134. isc_throw(isc::NotImplemented, "Method not used in tests");
  135. }
  136. virtual ZoneUpdaterPtr getUpdater(const Name& name, bool replace,
  137. bool journaling) const;
  138. // We store some information about what was happening here.
  139. // It is publicly accessible, since this is private testing class
  140. // anyway, so no need to dress it fancy into getters. Some are mutable,
  141. // since many client methods are const, but we still want to know they
  142. // were called.
  143. mutable vector<Name> provided_updaters_;
  144. vector<RRsetPtr> rrsets_;
  145. // List of rrsets as texts, for easier manipulation
  146. vector<string> rrset_texts_;
  147. bool commit_called_;
  148. // If set to true, getUpdater returns NULL
  149. bool missing_zone_;
  150. // The pretended class of the client. Usualy IN, but can be overriden.
  151. RRClass rrclass_;
  152. };
  153. // Test implementation of RRsetCollectionBase.
  154. class TestRRsetCollection : public isc::datasrc::RRsetCollectionBase {
  155. public:
  156. TestRRsetCollection(ZoneUpdater& updater,
  157. const isc::dns::RRClass& rrclass) :
  158. isc::datasrc::RRsetCollectionBase(updater, rrclass)
  159. {}
  160. virtual ~TestRRsetCollection() {}
  161. protected:
  162. virtual RRsetCollectionBase::IterPtr getBeginning() {
  163. isc_throw(isc::NotImplemented, "This method is not implemented.");
  164. }
  165. virtual RRsetCollectionBase::IterPtr getEnd() {
  166. isc_throw(isc::NotImplemented, "This method is not implemented.");
  167. }
  168. };
  169. // The updater isn't really correct according to the API. For example,
  170. // the whole client can be committed only once in its lifetime. The
  171. // updaters would influence each other if there were more. But we
  172. // don't need more updaters in the same test, so it doesn't matter
  173. // and this way, it is much simpler.
  174. class Updater : public ZoneUpdater {
  175. public:
  176. Updater(MockClient* client, const Name& name) :
  177. client_(client),
  178. finder_(client_->rrclass_, name, client_->rrsets_)
  179. {}
  180. virtual ZoneFinder& getFinder() {
  181. return (finder_);
  182. }
  183. virtual isc::datasrc::RRsetCollectionBase& getRRsetCollection() {
  184. if (!rrset_collection_) {
  185. rrset_collection_.reset(new TestRRsetCollection(*this,
  186. client_->rrclass_));
  187. }
  188. return (*rrset_collection_);
  189. }
  190. virtual void addRRset(const isc::dns::AbstractRRset& rrset) {
  191. if (client_->commit_called_) {
  192. isc_throw(DataSourceError, "Add after commit");
  193. }
  194. // We need to copy the RRset. We don't do it properly (we omit the
  195. // signature, for example), because we don't need to.
  196. RRsetPtr new_rrset(new isc::dns::BasicRRset(rrset.getName(),
  197. rrset.getClass(),
  198. rrset.getType(),
  199. rrset.getTTL()));
  200. for (isc::dns::RdataIteratorPtr i(rrset.getRdataIterator());
  201. !i->isLast(); i->next()) {
  202. new_rrset->addRdata(i->getCurrent());
  203. }
  204. client_->rrsets_.push_back(new_rrset);
  205. client_->rrset_texts_.push_back(rrset.toText());
  206. }
  207. virtual void deleteRRset(const isc::dns::AbstractRRset&) {
  208. isc_throw(isc::NotImplemented, "Method not used in tests");
  209. }
  210. virtual void commit() {
  211. client_->commit_called_ = true;
  212. }
  213. private:
  214. MockClient* client_;
  215. boost::scoped_ptr<TestRRsetCollection> rrset_collection_;
  216. class Finder : public ZoneFinder {
  217. public:
  218. Finder(const RRClass& rrclass, const Name& name,
  219. const vector<RRsetPtr>& rrsets) :
  220. class_(rrclass),
  221. name_(name),
  222. rrsets_(rrsets)
  223. {}
  224. virtual RRClass getClass() const {
  225. return (class_);
  226. }
  227. virtual Name getOrigin() const {
  228. return (name_);
  229. }
  230. virtual shared_ptr<Context> find(const Name& name, const RRType& type,
  231. const FindOptions options)
  232. {
  233. // The method is not completely correct. It ignores many special
  234. // cases and also the options except for the result. But this is
  235. // enough for the tests. We care only about exact match here.
  236. BOOST_FOREACH(const RRsetPtr& rrset, rrsets_) {
  237. if (rrset->getName() == name && rrset->getType() == type) {
  238. return (shared_ptr<Context>(
  239. new GenericContext(*this, options,
  240. ResultContext(SUCCESS, rrset))));
  241. }
  242. }
  243. return (shared_ptr<Context>(
  244. new GenericContext(*this, options,
  245. ResultContext(NXRRSET, ConstRRsetPtr()))));
  246. }
  247. virtual shared_ptr<Context> findAll(const Name&,
  248. vector<ConstRRsetPtr>&,
  249. const FindOptions)
  250. {
  251. isc_throw(isc::NotImplemented, "Method not used in tests");
  252. }
  253. virtual FindNSEC3Result findNSEC3(const Name&, bool) {
  254. isc_throw(isc::NotImplemented, "Method not used in tests");
  255. }
  256. private:
  257. const RRClass class_;
  258. const Name name_;
  259. const vector<RRsetPtr>& rrsets_;
  260. } finder_;
  261. };
  262. ZoneUpdaterPtr
  263. MockClient::getUpdater(const Name& name, bool replace, bool journaling) const {
  264. if (missing_zone_) {
  265. return (ZoneUpdaterPtr());
  266. }
  267. EXPECT_TRUE(replace);
  268. EXPECT_FALSE(journaling);
  269. provided_updaters_.push_back(name);
  270. // const_cast is bad. But the const on getUpdater seems wrong in the first
  271. // place, since updater will be modifying the data there. And the updater
  272. // wants to store data into the client so we can examine it later.
  273. return (ZoneUpdaterPtr(new Updater(const_cast<MockClient*>(this), name)));
  274. }
  275. class ZoneLoaderTest : public ::testing::Test {
  276. protected:
  277. ZoneLoaderTest() :
  278. rrclass_(RRClass::IN()),
  279. ztable_segment_(memory::ZoneTableSegment::
  280. create(isc::data::NullElement(), rrclass_)),
  281. source_client_(ztable_segment_, rrclass_)
  282. {}
  283. void prepareSource(const Name& zone, const char* filename) {
  284. source_client_.load(zone, string(TEST_DATA_DIR) + "/" + filename);
  285. }
  286. private:
  287. const RRClass rrclass_;
  288. // This is because of the in-memory client. We use it to read data
  289. // from. It is still easier than setting up sqlite3 client, since
  290. // we have this one in the linked library.
  291. // FIXME: We should be destroying it by ZoneTableSegment::destroy.
  292. // But the shared pointer won't let us, will it?
  293. shared_ptr<memory::ZoneTableSegment> ztable_segment_;
  294. protected:
  295. memory::InMemoryClient source_client_;
  296. // This one is mocked. It will help us see what is happening inside.
  297. // Also, mocking it is simpler than setting up an sqlite3 client.
  298. MockClient destination_client_;
  299. };
  300. // Use the loader to load an unsigned zone.
  301. TEST_F(ZoneLoaderTest, copyUnsigned) {
  302. prepareSource(Name::ROOT_NAME(), "root.zone");
  303. ZoneLoader loader(destination_client_, Name::ROOT_NAME(), source_client_);
  304. // It gets the updater directly in the constructor
  305. ASSERT_EQ(1, destination_client_.provided_updaters_.size());
  306. EXPECT_EQ(Name::ROOT_NAME(), destination_client_.provided_updaters_[0]);
  307. // Counter is initialized to 0, progress is "unknown" in case of copy.
  308. EXPECT_EQ(0, loader.getRRCount());
  309. EXPECT_EQ(ZoneLoader::PROGRESS_UNKNOWN, loader.getProgress());
  310. // Now load the whole zone
  311. loader.load();
  312. EXPECT_TRUE(destination_client_.commit_called_);
  313. // We don't check the whole zone. We check the first and last and the
  314. // count, which should be enough.
  315. // The count is 34 because we expect the RRs to be separated.
  316. EXPECT_EQ(34, destination_client_.rrsets_.size());
  317. // Check various counters. getRRCount should be identical of the RRs
  318. // we've seen. Progress is still "unknown" in the copy operation.
  319. EXPECT_EQ(destination_client_.rrsets_.size(), loader.getRRCount());
  320. EXPECT_EQ(ZoneLoader::PROGRESS_UNKNOWN, loader.getProgress());
  321. // Ensure known order.
  322. std::sort(destination_client_.rrset_texts_.begin(),
  323. destination_client_.rrset_texts_.end());
  324. EXPECT_EQ(". 518400 IN NS a.root-servers.net.\n",
  325. destination_client_.rrset_texts_.front());
  326. EXPECT_EQ("m.root-servers.net. 3600000 IN AAAA 2001:dc3::35\n",
  327. destination_client_.rrset_texts_.back());
  328. // It isn't possible to try again now
  329. EXPECT_THROW(loader.load(), isc::InvalidOperation);
  330. EXPECT_THROW(loader.loadIncremental(1), isc::InvalidOperation);
  331. // Even 0, which should load nothing, returns the error
  332. EXPECT_THROW(loader.loadIncremental(0), isc::InvalidOperation);
  333. }
  334. // Try loading incrementally.
  335. TEST_F(ZoneLoaderTest, copyUnsignedIncremental) {
  336. prepareSource(Name::ROOT_NAME(), "root.zone");
  337. ZoneLoader loader(destination_client_, Name::ROOT_NAME(), source_client_);
  338. // Try loading few RRs first.
  339. loader.loadIncremental(10);
  340. // We should get the 10 we asked for
  341. EXPECT_EQ(10, destination_client_.rrsets_.size());
  342. // Not committed yet, we didn't complete the loading
  343. EXPECT_FALSE(destination_client_.commit_called_);
  344. // Check we can get intermediate counters. Progress is always "unknown"
  345. // in case of copy.
  346. EXPECT_EQ(destination_client_.rrsets_.size(), loader.getRRCount());
  347. EXPECT_EQ(ZoneLoader::PROGRESS_UNKNOWN, loader.getProgress());
  348. // This is unusual, but allowed. Check it doesn't do anything
  349. loader.loadIncremental(0);
  350. EXPECT_EQ(10, destination_client_.rrsets_.size());
  351. EXPECT_FALSE(destination_client_.commit_called_);
  352. // We can finish the rest
  353. loader.loadIncremental(30);
  354. EXPECT_EQ(34, destination_client_.rrsets_.size());
  355. EXPECT_TRUE(destination_client_.commit_called_);
  356. // No more loading now
  357. EXPECT_THROW(loader.load(), isc::InvalidOperation);
  358. EXPECT_THROW(loader.loadIncremental(1), isc::InvalidOperation);
  359. EXPECT_THROW(loader.loadIncremental(0), isc::InvalidOperation);
  360. }
  361. // Check we can load RRSIGs and NSEC3 (which could break due to them being
  362. // in separate namespace)
  363. TEST_F(ZoneLoaderTest, copySigned) {
  364. prepareSource(Name("example.org"), "example.org.nsec3-signed");
  365. ZoneLoader loader(destination_client_, Name("example.org"),
  366. source_client_);
  367. loader.load();
  368. // All the RRs are there, including the ones in NSEC3 namespace
  369. EXPECT_EQ(14, destination_client_.rrsets_.size());
  370. EXPECT_TRUE(destination_client_.commit_called_);
  371. // Same trick with sorting to know where they are
  372. std::sort(destination_client_.rrset_texts_.begin(),
  373. destination_client_.rrset_texts_.end());
  374. // Due to the R at the beginning, this one should be last
  375. EXPECT_EQ("09GM5T42SMIMT7R8DF6RTG80SFMS1NLU.example.org. 1200 IN NSEC3 "
  376. "1 0 10 AABBCCDD RKOF8QMFRB5F2V9EJHFBVB2JPVSA0DJD A RRSIG\n",
  377. destination_client_.rrset_texts_[0]);
  378. EXPECT_EQ("09GM5T42SMIMT7R8DF6RTG80SFMS1NLU.example.org. 1200 IN RRSIG "
  379. "NSEC3 7 3 1200 20120301040838 20120131040838 19562 example.org."
  380. " EdwMeepLf//lV+KpCAN+213Scv1rrZyj4i2OwoCP4XxxS3CWGSuvYuKOyfZc8w"
  381. "KRcrD/4YG6nZVXE0s5O8NahjBJmDIyVt4WkfZ6QthxGg8ggLVvcD3dFksPyiKHf"
  382. "/WrTOZPSsxvN5m/i1Ey6+YWS01Gf3WDCMWDauC7Nmh3CTM=\n",
  383. destination_client_.rrset_texts_[1]);
  384. }
  385. // If the destination zone does not exist, it throws
  386. TEST_F(ZoneLoaderTest, copyMissingDestination) {
  387. destination_client_.missing_zone_ = true;
  388. prepareSource(Name::ROOT_NAME(), "root.zone");
  389. EXPECT_THROW(ZoneLoader(destination_client_, Name::ROOT_NAME(),
  390. source_client_), DataSourceError);
  391. }
  392. // If the source zone does not exist, it throws
  393. TEST_F(ZoneLoaderTest, copyMissingSource) {
  394. EXPECT_THROW(ZoneLoader(destination_client_, Name::ROOT_NAME(),
  395. source_client_), DataSourceError);
  396. }
  397. // The class of the source and destination are different
  398. TEST_F(ZoneLoaderTest, classMismatch) {
  399. destination_client_.rrclass_ = RRClass::CH();
  400. prepareSource(Name::ROOT_NAME(), "root.zone");
  401. EXPECT_THROW(ZoneLoader(destination_client_, Name::ROOT_NAME(),
  402. source_client_), isc::InvalidParameter);
  403. }
  404. // Load an unsigned zone, all at once
  405. TEST_F(ZoneLoaderTest, loadUnsigned) {
  406. ZoneLoader loader(destination_client_, Name::ROOT_NAME(),
  407. TEST_DATA_DIR "/root.zone");
  408. // Counter and progress are initialized to 0.
  409. EXPECT_EQ(0, loader.getRRCount());
  410. EXPECT_EQ(0, loader.getProgress());
  411. // It gets the updater directly in the constructor
  412. ASSERT_EQ(1, destination_client_.provided_updaters_.size());
  413. EXPECT_EQ(Name::ROOT_NAME(), destination_client_.provided_updaters_[0]);
  414. // Now load the whole zone
  415. loader.load();
  416. EXPECT_TRUE(destination_client_.commit_called_);
  417. // We don't check the whole zone. We check the first and last and the
  418. // count, which should be enough.
  419. // The count is 34 because we expect the RRs to be separated.
  420. EXPECT_EQ(34, destination_client_.rrsets_.size());
  421. // getRRCount should be identical of the RRs we've seen. progress
  422. // should reach 100% (= 1).
  423. EXPECT_EQ(destination_client_.rrsets_.size(), loader.getRRCount());
  424. EXPECT_EQ(1, loader.getProgress());
  425. // Ensure known order.
  426. std::sort(destination_client_.rrset_texts_.begin(),
  427. destination_client_.rrset_texts_.end());
  428. EXPECT_EQ(". 518400 IN NS a.root-servers.net.\n",
  429. destination_client_.rrset_texts_.front());
  430. EXPECT_EQ("m.root-servers.net. 3600000 IN AAAA 2001:dc3::35\n",
  431. destination_client_.rrset_texts_.back());
  432. // It isn't possible to try again now
  433. EXPECT_THROW(loader.load(), isc::InvalidOperation);
  434. EXPECT_THROW(loader.loadIncremental(1), isc::InvalidOperation);
  435. // Even 0, which should load nothing, returns the error
  436. EXPECT_THROW(loader.loadIncremental(0), isc::InvalidOperation);
  437. }
  438. // Try loading from master file incrementally.
  439. TEST_F(ZoneLoaderTest, loadUnsignedIncremental) {
  440. ZoneLoader loader(destination_client_, Name::ROOT_NAME(),
  441. TEST_DATA_DIR "/root.zone");
  442. // Counters are initialized to 0.
  443. EXPECT_EQ(0, loader.getRRCount());
  444. EXPECT_EQ(0, loader.getProgress());
  445. // Try loading few RRs first.
  446. loader.loadIncremental(10);
  447. // We should get the 10 we asked for
  448. EXPECT_EQ(10, destination_client_.rrsets_.size());
  449. // Not committed yet, we didn't complete the loading
  450. EXPECT_FALSE(destination_client_.commit_called_);
  451. EXPECT_EQ(10, destination_client_.rrsets_.size());
  452. EXPECT_FALSE(destination_client_.commit_called_);
  453. // Check we can get intermediate counters. Expected progress is calculated
  454. // based on the size of the zone file and the offset to the end of 10th RR
  455. // (subject to future changes to the file, but we assume it's a rare
  456. // event.). The expected value should be the exact expression that
  457. // getProgress() should do internally, so EXPECT_EQ() should work here,
  458. // but floating-point comparison can be always tricky we use
  459. // EXPECT_DOUBLE_EQ just in case.
  460. EXPECT_EQ(destination_client_.rrsets_.size(), loader.getRRCount());
  461. // file size = 1541, offset = 428 (27.77%).
  462. EXPECT_DOUBLE_EQ(static_cast<double>(428) / 1541, loader.getProgress());
  463. // We can finish the rest
  464. loader.loadIncremental(30);
  465. EXPECT_EQ(34, destination_client_.rrsets_.size());
  466. EXPECT_TRUE(destination_client_.commit_called_);
  467. // Counters are updated accordingly. Progress should reach 100%.
  468. EXPECT_EQ(destination_client_.rrsets_.size(), loader.getRRCount());
  469. EXPECT_EQ(1, loader.getProgress());
  470. // No more loading now
  471. EXPECT_THROW(loader.load(), isc::InvalidOperation);
  472. EXPECT_THROW(loader.loadIncremental(1), isc::InvalidOperation);
  473. EXPECT_THROW(loader.loadIncremental(0), isc::InvalidOperation);
  474. }
  475. // If the destination zone does not exist, it throws
  476. TEST_F(ZoneLoaderTest, loadMissingDestination) {
  477. destination_client_.missing_zone_ = true;
  478. EXPECT_THROW(ZoneLoader(destination_client_, Name::ROOT_NAME(),
  479. TEST_DATA_DIR "/root.zone"), DataSourceError);
  480. }
  481. // Check we can load RRSIGs and NSEC3 (which could break due to them being
  482. // in separate namespace)
  483. TEST_F(ZoneLoaderTest, loadSigned) {
  484. ZoneLoader loader(destination_client_, Name("example.org"),
  485. TEST_DATA_DIR "/example.org.nsec3-signed");
  486. loader.load();
  487. // All the RRs are there, including the ones in NSEC3 namespace
  488. EXPECT_EQ(14, destination_client_.rrsets_.size());
  489. EXPECT_TRUE(destination_client_.commit_called_);
  490. // Same trick with sorting to know where they are
  491. std::sort(destination_client_.rrset_texts_.begin(),
  492. destination_client_.rrset_texts_.end());
  493. // Due to the R at the beginning, this one should be last
  494. EXPECT_EQ("09GM5T42SMIMT7R8DF6RTG80SFMS1NLU.example.org. 1200 IN NSEC3 "
  495. "1 0 10 AABBCCDD RKOF8QMFRB5F2V9EJHFBVB2JPVSA0DJD A RRSIG\n",
  496. destination_client_.rrset_texts_[0]);
  497. EXPECT_EQ("09GM5T42SMIMT7R8DF6RTG80SFMS1NLU.example.org. 1200 IN RRSIG "
  498. "NSEC3 7 3 1200 20120301040838 20120131040838 19562 example.org."
  499. " EdwMeepLf//lV+KpCAN+213Scv1rrZyj4i2OwoCP4XxxS3CWGSuvYuKOyfZc8w"
  500. "KRcrD/4YG6nZVXE0s5O8NahjBJmDIyVt4WkfZ6QthxGg8ggLVvcD3dFksPyiKHf"
  501. "/WrTOZPSsxvN5m/i1Ey6+YWS01Gf3WDCMWDauC7Nmh3CTM=\n",
  502. destination_client_.rrset_texts_[1]);
  503. }
  504. // Test it throws when there's no such file
  505. TEST_F(ZoneLoaderTest, loadNoSuchFile) {
  506. ZoneLoader loader(destination_client_, Name::ROOT_NAME(),
  507. "This file does not exist");
  508. EXPECT_THROW(loader.load(), MasterFileError);
  509. EXPECT_FALSE(destination_client_.commit_called_);
  510. }
  511. // And it also throws when there's a syntax error in the master file
  512. TEST_F(ZoneLoaderTest, loadSyntaxError) {
  513. ZoneLoader loader(destination_client_, Name::ROOT_NAME(),
  514. // This is not a master file for sure
  515. // (misusing a file that happens to be there
  516. // already).
  517. TEST_DATA_DIR "/example.org.sqlite3");
  518. EXPECT_THROW(loader.load(), MasterFileError);
  519. EXPECT_FALSE(destination_client_.commit_called_);
  520. }
  521. // Test there's validation of the data in the zone loader.
  522. TEST_F(ZoneLoaderTest, loadCheck) {
  523. ZoneLoader loader(destination_client_, Name("example.org"),
  524. TEST_DATA_DIR "/novalidate.zone");
  525. EXPECT_THROW(loader.loadIncremental(10), ZoneContentError);
  526. // The messages go to the log. We don't have an easy way to examine them.
  527. EXPECT_FALSE(destination_client_.commit_called_);
  528. }
  529. // Check a warning doesn't disrupt the loading of the zone
  530. TEST_F(ZoneLoaderTest, loadCheckWarn) {
  531. ZoneLoader loader(destination_client_, Name("example.org"),
  532. TEST_DATA_DIR "/checkwarn.zone");
  533. EXPECT_TRUE(loader.loadIncremental(10));
  534. // The messages go to the log. We don't have an easy way to examine them.
  535. // But the zone was committed and contains all 3 RRs
  536. EXPECT_TRUE(destination_client_.commit_called_);
  537. EXPECT_EQ(3, destination_client_.rrsets_.size());
  538. }
  539. TEST_F(ZoneLoaderTest, copyCheckWarn) {
  540. prepareSource(Name("example.org"), "checkwarn.zone");
  541. ZoneLoader loader(destination_client_, Name("example.org"),
  542. source_client_);
  543. EXPECT_TRUE(loader.loadIncremental(10));
  544. // The messages go to the log. We don't have an easy way to examine them.
  545. // But the zone was committed and contains all 3 RRs
  546. EXPECT_TRUE(destination_client_.commit_called_);
  547. EXPECT_EQ(3, destination_client_.rrsets_.size());
  548. }
  549. // Test there's validation of the data in the zone loader when copying
  550. // from another data source.
  551. TEST_F(ZoneLoaderTest, copyCheck) {
  552. // In this test, my_source_client provides a zone that does not
  553. // validate (no NS).
  554. MockClient my_source_client;
  555. ZoneLoader loader(destination_client_, Name("example.org"),
  556. my_source_client);
  557. EXPECT_THROW(loader.loadIncremental(10), ZoneContentError);
  558. // The messages go to the log. We don't have an easy way to examine them.
  559. EXPECT_FALSE(destination_client_.commit_called_);
  560. }
  561. }