rrcollator_unittest.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 <dns/name.h>
  15. #include <dns/rrclass.h>
  16. #include <dns/rrcollator.h>
  17. #include <dns/rdata.h>
  18. #include <dns/rrset.h>
  19. #include <dns/rrttl.h>
  20. #include <gtest/gtest.h>
  21. #include <boost/bind.hpp>
  22. #include <vector>
  23. using std::vector;
  24. using namespace isc::dns;
  25. using namespace isc::dns::rdata;
  26. namespace {
  27. typedef RRCollator::AddRRsetCallback AddRRsetCallback;
  28. void
  29. addRRset(const RRsetPtr& rrset, vector<ConstRRsetPtr>* to_append) {
  30. to_append->push_back(rrset);
  31. }
  32. class RRCollatorTest : public ::testing::Test {
  33. protected:
  34. RRCollatorTest() :
  35. origin_("example.com"), rrclass_(RRClass::IN()), rrttl_(3600),
  36. collator_(boost::bind(addRRset, _1, &rrsets_)),
  37. rr_callback_(collator_.getCallback())
  38. {
  39. RRsetPtr rrset(new RRset(origin_, rrclass_, RRType::A(), rrttl_));
  40. a_rdata1_ = createRdata(RRType::A(), rrclass_, "192.0.2.1");
  41. a_rdata2_ = createRdata(RRType::A(), rrclass_, "192.0.2.2");
  42. rrset->addRdata(a_rdata1_);
  43. rrset->addRdata(a_rdata2_);
  44. a_rrset_ = rrset;
  45. rrset = RRsetPtr(new RRset(origin_, rrclass_, RRType::AAAA(), rrttl_));
  46. aaaa_rdata_ = createRdata(RRType::AAAA(), rrclass_, "2001:db8::1");
  47. aaaa_rrset_ = rrset;
  48. }
  49. void checkRRset(const Name& expected_name, const RRClass& expected_class,
  50. const RRType& expected_type, const RRTTL& expected_ttl,
  51. const vector<ConstRdataPtr>& expected_rdatas) {
  52. // This test always clears rrsets_ to confirm RRsets are added
  53. // one-by-one
  54. ASSERT_EQ(1, rrsets_.size());
  55. ConstRRsetPtr actual = rrsets_[0];
  56. EXPECT_EQ(expected_name, actual->getName());
  57. EXPECT_EQ(expected_class, actual->getClass());
  58. EXPECT_EQ(expected_type, actual->getType());
  59. EXPECT_EQ(expected_ttl, actual->getTTL());
  60. ASSERT_EQ(expected_rdatas.size(), actual->getRdataCount());
  61. vector<ConstRdataPtr>::const_iterator it = expected_rdatas.begin();
  62. for (RdataIteratorPtr rit = actual->getRdataIterator();
  63. !rit->isLast();
  64. rit->next()) {
  65. EXPECT_EQ(0, rit->getCurrent().compare(**it));
  66. ++it;
  67. }
  68. rrsets_.clear();
  69. }
  70. const Name origin_;
  71. const RRClass rrclass_;
  72. const RRTTL rrttl_;
  73. vector<ConstRRsetPtr> rrsets_;
  74. RdataPtr a_rdata1_, a_rdata2_, aaaa_rdata_;
  75. ConstRRsetPtr a_rrset_, aaaa_rrset_;
  76. RRCollator collator_;
  77. AddRRCallback rr_callback_;
  78. };
  79. TEST_F(RRCollatorTest, add) {
  80. vector<ConstRdataPtr> rdatas;
  81. // Add two RRs belonging to the same RRset. These will be buffered.
  82. rr_callback_(origin_, rrclass_, RRType::A(), rrttl_, a_rdata1_);
  83. EXPECT_TRUE(rrsets_.empty()); // not yet given as an RRset
  84. rr_callback_(origin_, rrclass_, RRType::A(), rrttl_, a_rdata2_);
  85. EXPECT_TRUE(rrsets_.empty()); // still not given
  86. // Add another type of RR. This completes the construction of the A RRset,
  87. // which will be given via the callback.
  88. rr_callback_(origin_, rrclass_, RRType::AAAA(), rrttl_, aaaa_rdata_);
  89. rdatas.push_back(a_rdata1_);
  90. rdatas.push_back(a_rdata2_);
  91. checkRRset(origin_, rrclass_, RRType::A(), rrttl_, rdatas);
  92. // Let the collator we are done, then we'll see the AAAA RR as an RRset.
  93. collator_.finish();
  94. rdatas.clear();
  95. rdatas.push_back(aaaa_rdata_);
  96. checkRRset(origin_, rrclass_, RRType::AAAA(), rrttl_, rdatas);
  97. }
  98. }