zone_writer_unittest.cc 8.1 KB

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