rrcollator_unittest.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. a_rdata1_ = createRdata(RRType::A(), rrclass_, "192.0.2.1");
  40. a_rdata2_ = createRdata(RRType::A(), rrclass_, "192.0.2.2");
  41. txt_rdata_ = createRdata(RRType::TXT(), rrclass_, "test");
  42. sig_rdata1_ = createRdata(RRType::RRSIG(), rrclass_,
  43. "A 5 3 3600 20000101000000 20000201000000 "
  44. "12345 example.com. FAKE\n");
  45. sig_rdata2_ = createRdata(RRType::RRSIG(), rrclass_,
  46. "NS 5 3 3600 20000101000000 20000201000000 "
  47. "12345 example.com. FAKE\n");
  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. SCOPED_TRACE(expected_name.toText(true) + "/" +
  53. expected_class.toText() + "/" + expected_type.toText());
  54. // This test always clears rrsets_ to confirm RRsets are added
  55. // one-by-one
  56. ASSERT_EQ(1, rrsets_.size());
  57. ConstRRsetPtr actual = rrsets_[0];
  58. EXPECT_EQ(expected_name, actual->getName());
  59. EXPECT_EQ(expected_class, actual->getClass());
  60. EXPECT_EQ(expected_type, actual->getType());
  61. EXPECT_EQ(expected_ttl, actual->getTTL());
  62. ASSERT_EQ(expected_rdatas.size(), actual->getRdataCount());
  63. vector<ConstRdataPtr>::const_iterator it = expected_rdatas.begin();
  64. for (RdataIteratorPtr rit = actual->getRdataIterator();
  65. !rit->isLast();
  66. rit->next()) {
  67. EXPECT_EQ(0, rit->getCurrent().compare(**it));
  68. ++it;
  69. }
  70. rrsets_.clear();
  71. }
  72. const Name origin_;
  73. const RRClass rrclass_;
  74. const RRTTL rrttl_;
  75. vector<ConstRRsetPtr> rrsets_;
  76. RdataPtr a_rdata1_, a_rdata2_, txt_rdata_, sig_rdata1_, sig_rdata2_;
  77. RRCollator collator_;
  78. AddRRCallback rr_callback_;
  79. vector<ConstRdataPtr> rdatas_; // placeholder for expected data
  80. };
  81. TEST_F(RRCollatorTest, basicCases) {
  82. // Add two RRs belonging to the same RRset. These will be buffered.
  83. rr_callback_(origin_, rrclass_, RRType::A(), rrttl_, a_rdata1_);
  84. EXPECT_TRUE(rrsets_.empty()); // not yet given as an RRset
  85. rr_callback_(origin_, rrclass_, RRType::A(), rrttl_, a_rdata2_);
  86. EXPECT_TRUE(rrsets_.empty()); // still not given
  87. // Add another type of RR. This completes the construction of the A RRset,
  88. // which will be given via the callback.
  89. rr_callback_(origin_, rrclass_, RRType::TXT(), rrttl_, txt_rdata_);
  90. rdatas_.push_back(a_rdata1_);
  91. rdatas_.push_back(a_rdata2_);
  92. checkRRset(origin_, rrclass_, RRType::A(), rrttl_, rdatas_);
  93. // Add the same type of RR but of different name. This should make another
  94. // callback for the previous TXT RR.
  95. rr_callback_(Name("txt.example.com"), rrclass_, RRType::TXT(), rrttl_,
  96. txt_rdata_);
  97. rdatas_.clear();
  98. rdatas_.push_back(txt_rdata_);
  99. checkRRset(origin_, rrclass_, RRType::TXT(), rrttl_, rdatas_);
  100. // Add the same type and name of RR but of different class (rare case
  101. // in practice)
  102. rr_callback_(Name("txt.example.com"), RRClass::CH(), RRType::TXT(), rrttl_,
  103. txt_rdata_);
  104. rdatas_.clear();
  105. rdatas_.push_back(txt_rdata_);
  106. checkRRset(Name("txt.example.com"), rrclass_, RRType::TXT(), rrttl_,
  107. rdatas_);
  108. // Tell the collator we are done, then we'll see the last RR as an RRset.
  109. collator_.finish();
  110. checkRRset(Name("txt.example.com"), RRClass::CH(), RRType::TXT(), rrttl_,
  111. rdatas_);
  112. // Redundant finish() will be no-op.
  113. collator_.finish();
  114. EXPECT_TRUE(rrsets_.empty());
  115. }
  116. TEST_F(RRCollatorTest, minTTLFirst) {
  117. // RRs of the same RRset but has different TTLs. The first RR has
  118. // the smaller TTL, which should be used for the TTL of the RRset.
  119. rr_callback_(origin_, rrclass_, RRType::A(), RRTTL(10), a_rdata1_);
  120. rr_callback_(origin_, rrclass_, RRType::A(), RRTTL(20), a_rdata2_);
  121. rdatas_.push_back(a_rdata1_);
  122. rdatas_.push_back(a_rdata2_);
  123. collator_.finish();
  124. checkRRset(origin_, rrclass_, RRType::A(), RRTTL(10), rdatas_);
  125. }
  126. TEST_F(RRCollatorTest, maxTTLFirst) {
  127. // RRs of the same RRset but has different TTLs. The second RR has
  128. // the smaller TTL, which should be used for the TTL of the RRset.
  129. rr_callback_(origin_, rrclass_, RRType::A(), RRTTL(20), a_rdata1_);
  130. rr_callback_(origin_, rrclass_, RRType::A(), RRTTL(10), a_rdata2_);
  131. rdatas_.push_back(a_rdata1_);
  132. rdatas_.push_back(a_rdata2_);
  133. collator_.finish();
  134. checkRRset(origin_, rrclass_, RRType::A(), RRTTL(10), rdatas_);
  135. }
  136. TEST_F(RRCollatorTest, addRRSIGs) {
  137. // RRSIG is special; they are also distinguished by their covered types.
  138. rr_callback_(origin_, rrclass_, RRType::RRSIG(), rrttl_, sig_rdata1_);
  139. rr_callback_(origin_, rrclass_, RRType::RRSIG(), rrttl_, sig_rdata2_);
  140. rdatas_.push_back(sig_rdata1_);
  141. checkRRset(origin_, rrclass_, RRType::RRSIG(), rrttl_, rdatas_);
  142. }
  143. TEST_F(RRCollatorTest, emptyFinish) {
  144. collator_.finish();
  145. EXPECT_TRUE(rrsets_.empty());
  146. }
  147. }