master_loader_callbacks_test.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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/master_loader_callbacks.h>
  15. #include <datasrc/zone.h>
  16. #include <dns/rrset.h>
  17. #include <dns/rrclass.h>
  18. #include <dns/rrttl.h>
  19. #include <dns/rdata.h>
  20. #include <testutils/dnsmessage_test.h>
  21. #include <exceptions/exceptions.h>
  22. #include <gtest/gtest.h>
  23. #include <boost/lexical_cast.hpp>
  24. #include <list>
  25. #include <string>
  26. using namespace isc::datasrc;
  27. namespace {
  28. // An updater for the tests. Most of the virtual methods throw
  29. // NotImplemented, as they are not used in the tests.
  30. class MockUpdater : public ZoneUpdater {
  31. public:
  32. // We do the adding in this test. We currently only check these are
  33. // the correct ones, according to a predefined set in a list.
  34. virtual void addRRset(const isc::dns::AbstractRRset& rrset) {
  35. ASSERT_FALSE(expected_rrsets_.empty());
  36. // As the rrsetCheck requires a shared pointer, we need to create
  37. // a copy.
  38. isc::dns::RRsetPtr copy(new isc::dns::BasicRRset(rrset.getName(),
  39. rrset.getClass(),
  40. rrset.getType(),
  41. rrset.getTTL()));
  42. EXPECT_FALSE(rrset.getRRsig()) << "Unexpected RRSIG on rrset, not "
  43. "copying. Following check will likely fail as a result.";
  44. for (isc::dns::RdataIteratorPtr it(rrset.getRdataIterator());
  45. !it->isLast(); it->next()) {
  46. copy->addRdata(it->getCurrent());
  47. }
  48. isc::testutils::rrsetCheck(expected_rrsets_.front(), copy);
  49. // And remove this RRset, as it has been used.
  50. expected_rrsets_.pop_front();
  51. }
  52. // The unused but required methods
  53. virtual ZoneFinder& getFinder() {
  54. isc_throw(isc::NotImplemented, "Not to be called in this test");
  55. }
  56. virtual isc::datasrc::RRsetCollectionBase& getRRsetCollection() {
  57. isc_throw(isc::NotImplemented, "Not to be called in this test");
  58. }
  59. virtual void deleteRRset(const isc::dns::AbstractRRset&) {
  60. isc_throw(isc::NotImplemented, "Not to be called in this test");
  61. }
  62. virtual void commit() {
  63. isc_throw(isc::NotImplemented, "Not to be called in this test");
  64. }
  65. // The RRsets that are expected to appear through addRRset.
  66. std::list<isc::dns::RRsetPtr> expected_rrsets_;
  67. };
  68. class MasterLoaderCallbackTest : public ::testing::Test {
  69. protected:
  70. MasterLoaderCallbackTest() :
  71. ok_(true),
  72. callbacks_(createMasterLoaderCallbacks(isc::dns::Name("example.org"),
  73. isc::dns::RRClass::IN(), &ok_))
  74. {}
  75. // Generate a new RRset, put it to the updater and return it.
  76. void generateRRset(isc::dns::AddRRCallback callback) {
  77. const isc::dns::RRsetPtr
  78. result(new isc::dns::RRset(isc::dns::Name("example.org"),
  79. isc::dns::RRClass::IN(),
  80. isc::dns::RRType::A(),
  81. isc::dns::RRTTL(3600)));
  82. const isc::dns::rdata::RdataPtr
  83. data(isc::dns::rdata::createRdata(isc::dns::RRType::A(),
  84. isc::dns::RRClass::IN(),
  85. "192.0.2.1"));
  86. result->addRdata(data);
  87. updater_.expected_rrsets_.push_back(result);
  88. callback(result->getName(), result->getClass(), result->getType(),
  89. result->getTTL(), data);
  90. }
  91. // An updater to be passed to the context
  92. MockUpdater updater_;
  93. // Is the loading OK?
  94. bool ok_;
  95. // The tested context
  96. isc::dns::MasterLoaderCallbacks callbacks_;
  97. };
  98. // Check it doesn't crash if we don't provide the OK
  99. TEST_F(MasterLoaderCallbackTest, noOkProvided) {
  100. createMasterLoaderCallbacks(isc::dns::Name("example.org"),
  101. isc::dns::RRClass::IN(), NULL).
  102. error("No source", 1, "No reason");
  103. }
  104. // Check the callbacks can be called, don't crash and the error one switches
  105. // to non-OK mode. This, however, does not stop anybody from calling more
  106. // callbacks.
  107. TEST_F(MasterLoaderCallbackTest, callbacks) {
  108. EXPECT_NO_THROW(callbacks_.warning("No source", 1, "Just for fun"));
  109. // The warning does not hurt the OK mode.
  110. EXPECT_TRUE(ok_);
  111. // Now the error
  112. EXPECT_NO_THROW(callbacks_.error("No source", 2, "Some error"));
  113. // The OK is turned off once there's at least one error
  114. EXPECT_FALSE(ok_);
  115. // Not being OK does not hurt that much, we can still call the callbacks
  116. EXPECT_NO_THROW(callbacks_.warning("No source", 3, "Just for fun"));
  117. // The OK is not reset back to true
  118. EXPECT_FALSE(ok_);
  119. EXPECT_NO_THROW(callbacks_.error("No source", 4, "Some error"));
  120. }
  121. // Try adding some RRsets.
  122. TEST_F(MasterLoaderCallbackTest, addRRset) {
  123. isc::dns::AddRRCallback
  124. callback(createMasterLoaderAddCallback(updater_));
  125. // Put some of them in.
  126. EXPECT_NO_THROW(generateRRset(callback));
  127. EXPECT_NO_THROW(generateRRset(callback));
  128. // They all get pushed there right away, so there are none in the queue
  129. EXPECT_TRUE(updater_.expected_rrsets_.empty());
  130. }
  131. }