master_loader_callbacks_test.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 <exceptions/exceptions.h>
  20. #include <gtest/gtest.h>
  21. #include <boost/lexical_cast.hpp>
  22. #include <list>
  23. #include <string>
  24. using namespace isc::datasrc;
  25. namespace {
  26. // An updater for the tests. Most of the virtual methods throw
  27. // NotImplemented, as they are not used in the tests.
  28. class MockUpdater : public ZoneUpdater {
  29. public:
  30. // We do the adding in this test. We currently only check these are
  31. // the correct ones, according to a predefined set in a list.
  32. virtual void addRRset(const isc::dns::AbstractRRset& rrset) {
  33. ASSERT_FALSE(expected_rrsets_.empty());
  34. // In our tests, pointer equality is enough.
  35. EXPECT_EQ(expected_rrsets_.front().get(), &rrset);
  36. // And remove this RRset, as it has been used.
  37. expected_rrsets_.pop_front();
  38. }
  39. // The unused but required methods
  40. virtual ZoneFinder& getFinder() {
  41. isc_throw(isc::NotImplemented, "Not to be called in this test");
  42. }
  43. virtual void deleteRRset(const isc::dns::AbstractRRset&) {
  44. isc_throw(isc::NotImplemented, "Not to be called in this test");
  45. }
  46. virtual void commit() {
  47. isc_throw(isc::NotImplemented, "Not to be called in this test");
  48. }
  49. // The RRsets that are expected to appear through addRRset.
  50. std::list<isc::dns::RRsetPtr> expected_rrsets_;
  51. };
  52. class MasterLoaderCallbackTest : public ::testing::Test {
  53. protected:
  54. MasterLoaderCallbackTest() :
  55. ok_(true),
  56. callbacks_(createMasterLoaderCallbacks(isc::dns::Name("example.org"),
  57. isc::dns::RRClass::IN(), &ok_))
  58. {}
  59. // Generate a new RRset, put it to the updater and return it.
  60. isc::dns::RRsetPtr generateRRset() {
  61. const isc::dns::RRsetPtr
  62. result(new isc::dns::RRset(isc::dns::Name("example.org"),
  63. isc::dns::RRClass::IN(),
  64. isc::dns::RRType::A(),
  65. isc::dns::RRTTL(3600)));
  66. updater_.expected_rrsets_.push_back(result);
  67. return (result);
  68. }
  69. // An updater to be passed to the context
  70. MockUpdater updater_;
  71. // Is the loading OK?
  72. bool ok_;
  73. // The tested context
  74. isc::dns::MasterLoaderCallbacks callbacks_;
  75. };
  76. // Check it doesn't crash if we don't provide the OK
  77. TEST_F(MasterLoaderCallbackTest, noOkProvided) {
  78. createMasterLoaderCallbacks(isc::dns::Name("example.org"),
  79. isc::dns::RRClass::IN(), NULL).
  80. error("No source", 1, "No reason");
  81. }
  82. // Check the callbacks can be called, don't crash and the error one switches
  83. // to non-OK mode. This, however, does not stop anybody from calling more
  84. // callbacks.
  85. TEST_F(MasterLoaderCallbackTest, callbacks) {
  86. EXPECT_NO_THROW(callbacks_.warning("No source", 1, "Just for fun"));
  87. // The warning does not hurt the OK mode.
  88. EXPECT_TRUE(ok_);
  89. // Now the error
  90. EXPECT_NO_THROW(callbacks_.error("No source", 2, "Some error"));
  91. // The OK is turned off once there's at least one error
  92. EXPECT_FALSE(ok_);
  93. // Not being OK does not hurt that much, we can still call the callbacks
  94. EXPECT_NO_THROW(callbacks_.warning("No source", 3, "Just for fun"));
  95. // The OK is not reset back to true
  96. EXPECT_FALSE(ok_);
  97. EXPECT_NO_THROW(callbacks_.error("No source", 4, "Some error"));
  98. }
  99. // Try adding some RRsets.
  100. TEST_F(MasterLoaderCallbackTest, addRRset) {
  101. isc::dns::AddRRsetCallback
  102. callback(createMasterLoaderAddCallback(updater_));
  103. // Put some of them in.
  104. EXPECT_NO_THROW(callback(generateRRset()));
  105. EXPECT_NO_THROW(callback(generateRRset()));
  106. // They all get pushed there right away, so there are none in the queue
  107. EXPECT_TRUE(updater_.expected_rrsets_.empty());
  108. }
  109. }