zone_writer_unittest.cc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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/memory/zone_writer_local.h>
  15. #include <datasrc/memory/zone_table_segment_local.h>
  16. #include <datasrc/memory/zone_data.h>
  17. #include <cc/data.h>
  18. #include <dns/rrclass.h>
  19. #include <dns/name.h>
  20. #include <gtest/gtest.h>
  21. #include <boost/scoped_ptr.hpp>
  22. #include <boost/bind.hpp>
  23. using boost::scoped_ptr;
  24. using boost::bind;
  25. using isc::dns::RRClass;
  26. using isc::dns::Name;
  27. using namespace isc::datasrc::memory;
  28. namespace {
  29. class TestException {};
  30. class ZoneWriterLocalTest : public ::testing::Test {
  31. public:
  32. ZoneWriterLocalTest() :
  33. // FIXME: The NullElement probably isn't the best one, but we don't
  34. // know how the config will look, so it just fills the argument
  35. // (which is currently ignored)
  36. segment_(ZoneTableSegment::create(isc::data::NullElement())),
  37. writer_(new
  38. ZoneWriterLocal(dynamic_cast<ZoneTableSegmentLocal*>(segment_.
  39. get()),
  40. bind(&ZoneWriterLocalTest::loadAction, this, _1),
  41. Name("example.org"), RRClass::IN())),
  42. load_called_(false),
  43. load_throw_(false),
  44. load_null_(false),
  45. load_data_(false)
  46. {
  47. // TODO: The setTable is only a temporary interface
  48. segment_->getHeader().
  49. setTable(ZoneTable::create(segment_->getMemorySegment(),
  50. RRClass::IN()));
  51. }
  52. void TearDown() {
  53. // Release the writer
  54. writer_.reset();
  55. // Release the table we used
  56. ZoneTable::destroy(segment_->getMemorySegment(),
  57. segment_->getHeader().getTable(), RRClass::IN());
  58. // And check we freed all memory
  59. EXPECT_TRUE(segment_->getMemorySegment().allMemoryDeallocated());
  60. }
  61. protected:
  62. scoped_ptr<ZoneTableSegment> segment_;
  63. scoped_ptr<ZoneWriterLocal> writer_;
  64. bool load_called_;
  65. bool load_throw_;
  66. bool load_null_;
  67. bool load_data_;
  68. private:
  69. ZoneData* loadAction(isc::util::MemorySegment& segment) {
  70. // Make sure it is the correct segment passed. We know the
  71. // exact instance, can compare pointers to them.
  72. EXPECT_EQ(&segment_->getMemorySegment(), &segment);
  73. // We got called
  74. load_called_ = true;
  75. if (load_throw_) {
  76. throw TestException();
  77. }
  78. if (load_null_) {
  79. // Be nasty to the caller and return NULL, which is forbidden
  80. return (NULL);
  81. }
  82. ZoneData* data = ZoneData::create(segment, Name("example.org"));
  83. if (load_data_) {
  84. // Put something inside. The node itself should be enough for
  85. // the tests.
  86. ZoneNode* node(NULL);
  87. data->insertName(segment, Name("subdomain.example.org"), &node);
  88. EXPECT_NE(static_cast<ZoneNode*>(NULL), node);
  89. }
  90. return (data);
  91. }
  92. };
  93. // We call it the way we are supposed to, check every callback is called in the
  94. // right moment.
  95. TEST_F(ZoneWriterLocalTest, correctCall) {
  96. // Nothing called before we call it
  97. EXPECT_FALSE(load_called_);
  98. // Just the load gets called now
  99. EXPECT_NO_THROW(writer_->load());
  100. EXPECT_TRUE(load_called_);
  101. load_called_ = false;
  102. EXPECT_NO_THROW(writer_->install());
  103. EXPECT_FALSE(load_called_);
  104. // We don't check explicitly how this works, but call it to free memory. If
  105. // everything is freed should be checked inside the TearDown.
  106. EXPECT_NO_THROW(writer_->cleanup());
  107. }
  108. TEST_F(ZoneWriterLocalTest, loadTwice) {
  109. // Load it the first time
  110. EXPECT_NO_THROW(writer_->load());
  111. EXPECT_TRUE(load_called_);
  112. load_called_ = false;
  113. // The second time, it should not be possible
  114. EXPECT_THROW(writer_->load(), isc::InvalidOperation);
  115. EXPECT_FALSE(load_called_);
  116. // The object should not be damaged, try installing and clearing now
  117. EXPECT_NO_THROW(writer_->install());
  118. EXPECT_FALSE(load_called_);
  119. // We don't check explicitly how this works, but call it to free memory. If
  120. // everything is freed should be checked inside the TearDown.
  121. EXPECT_NO_THROW(writer_->cleanup());
  122. }
  123. // Try loading after call to install and call to cleanup. Both is
  124. // forbidden.
  125. TEST_F(ZoneWriterLocalTest, loadLater) {
  126. // Load first, so we can install
  127. EXPECT_NO_THROW(writer_->load());
  128. EXPECT_NO_THROW(writer_->install());
  129. // Reset so we see nothing is called now
  130. load_called_ = false;
  131. EXPECT_THROW(writer_->load(), isc::InvalidOperation);
  132. EXPECT_FALSE(load_called_);
  133. // Cleanup and try loading again. Still shouldn't work.
  134. EXPECT_NO_THROW(writer_->cleanup());
  135. EXPECT_THROW(writer_->load(), isc::InvalidOperation);
  136. EXPECT_FALSE(load_called_);
  137. }
  138. // Try calling install at various bad times
  139. TEST_F(ZoneWriterLocalTest, invalidInstall) {
  140. // Nothing loaded yet
  141. EXPECT_THROW(writer_->install(), isc::InvalidOperation);
  142. EXPECT_FALSE(load_called_);
  143. EXPECT_NO_THROW(writer_->load());
  144. load_called_ = false;
  145. // This install is OK
  146. EXPECT_NO_THROW(writer_->install());
  147. // But we can't call it second time now
  148. EXPECT_THROW(writer_->install(), isc::InvalidOperation);
  149. EXPECT_FALSE(load_called_);
  150. }
  151. // We check we can clean without installing first and nothing bad
  152. // happens. We also misuse the testcase to check we can't install
  153. // after cleanup.
  154. TEST_F(ZoneWriterLocalTest, cleanWithoutInstall) {
  155. EXPECT_NO_THROW(writer_->load());
  156. EXPECT_NO_THROW(writer_->cleanup());
  157. EXPECT_TRUE(load_called_);
  158. // We cleaned up, no way to install now
  159. EXPECT_THROW(writer_->install(), isc::InvalidOperation);
  160. }
  161. // Test the case when load callback throws
  162. TEST_F(ZoneWriterLocalTest, loadThrows) {
  163. load_throw_ = true;
  164. EXPECT_THROW(writer_->load(), TestException);
  165. // We can't install now
  166. EXPECT_THROW(writer_->install(), isc::InvalidOperation);
  167. EXPECT_TRUE(load_called_);
  168. // But we can cleanup
  169. EXPECT_NO_THROW(writer_->cleanup());
  170. }
  171. // Check the strong exception guarantee - if it throws, nothing happened
  172. // to the content.
  173. TEST_F(ZoneWriterLocalTest, retry) {
  174. // First attempt fails due to some exception.
  175. load_throw_ = true;
  176. EXPECT_THROW(writer_->load(), TestException);
  177. // This one shall succeed.
  178. load_called_ = load_throw_ = false;
  179. // We want some data inside.
  180. load_data_ = true;
  181. EXPECT_NO_THROW(writer_->load());
  182. // And this one will fail again. But the old data will survive.
  183. load_data_ = false;
  184. EXPECT_THROW(writer_->load(), isc::InvalidOperation);
  185. // The rest still works correctly
  186. EXPECT_NO_THROW(writer_->install());
  187. ZoneTable* const table(segment_->getHeader().getTable());
  188. const ZoneTable::FindResult found(table->findZone(Name("example.org")));
  189. ASSERT_EQ(isc::datasrc::result::SUCCESS, found.code);
  190. // For some reason it doesn't seem to work by the ZoneNode typedef, using
  191. // the full definition instead. At least on some compilers.
  192. const isc::datasrc::memory::DomainTreeNode<RdataSet>* node;
  193. EXPECT_EQ(isc::datasrc::memory::DomainTree<RdataSet>::EXACTMATCH,
  194. found.zone_data->getZoneTree().
  195. find(Name("subdomain.example.org"), &node));
  196. EXPECT_NO_THROW(writer_->cleanup());
  197. }
  198. // Check the writer defends itsefl when load action returns NULL
  199. TEST_F(ZoneWriterLocalTest, loadNull) {
  200. load_null_ = true;
  201. EXPECT_THROW(writer_->load(), isc::InvalidOperation);
  202. // We can't install that
  203. EXPECT_THROW(writer_->install(), isc::InvalidOperation);
  204. // It should be possible to clean up safely
  205. EXPECT_NO_THROW(writer_->cleanup());
  206. }
  207. // Check the object cleans up in case we forget it.
  208. TEST_F(ZoneWriterLocalTest, autoCleanUp) {
  209. // Load data and forget about it. It should get released
  210. // when the writer itself is destroyed.
  211. EXPECT_NO_THROW(writer_->load());
  212. }
  213. }