rdataset_unittest.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 <exceptions/exceptions.h>
  15. #include <util/buffer.h>
  16. #include <util/memory_segment_local.h>
  17. #include <dns/rrset.h>
  18. #include <dns/rrclass.h>
  19. #include <dns/rrtype.h>
  20. #include <dns/rrttl.h>
  21. #include <datasrc/memory/rdata_encoder.h>
  22. #include <datasrc/memory/rdataset.h>
  23. #include <testutils/dnsmessage_test.h>
  24. #include <gtest/gtest.h>
  25. using namespace isc::dns;
  26. using namespace isc::datasrc::memory;
  27. using namespace isc::testutils;
  28. namespace {
  29. class RdataSetTest : public ::testing::Test {
  30. protected:
  31. RdataSetTest() :
  32. // 1076895760 = 0x40302010. Use this so we fill in all 8-bit "field"
  33. // of the 32-bit TTL
  34. a_rrset_(textToRRset("www.example.com. 1076895760 IN A 192.0.2.1")),
  35. rrsig_rrset_(textToRRset("www.example.com. 1076895760 IN RRSIG "
  36. "A 5 2 3600 20120814220826 20120715220826 "
  37. "1234 example.com. FAKE"))
  38. {}
  39. void TearDown() {
  40. EXPECT_TRUE(mem_sgmt_.allMemoryDeallocated());
  41. }
  42. ConstRRsetPtr a_rrset_, rrsig_rrset_;
  43. isc::util::MemorySegmentLocal mem_sgmt_;
  44. RdataEncoder encoder_;
  45. };
  46. // Convert the given 32-bit integer (network byte order) to the corresponding
  47. // RRTTL object.
  48. RRTTL
  49. restoreTTL(const void* ttl_data) {
  50. isc::util::InputBuffer b(ttl_data, sizeof(uint32_t));
  51. return (RRTTL(b));
  52. }
  53. TEST_F(RdataSetTest, create) {
  54. // A simple case of creating an RdataSet. Confirming the resulting
  55. // fields have the expected values, and then destroying it (TearDown()
  56. // would detect any memory leak)
  57. RdataSet* rdataset = RdataSet::create(mem_sgmt_, encoder_, a_rrset_,
  58. ConstRRsetPtr());
  59. EXPECT_FALSE(rdataset->next); // by default the next pointer should be NULL
  60. EXPECT_EQ(RRType::A(), rdataset->type);
  61. EXPECT_EQ(RRTTL(1076895760), restoreTTL(rdataset->getTTLData()));
  62. EXPECT_EQ(1, rdataset->getRdataCount());
  63. EXPECT_EQ(0, rdataset->getSigRdataCount());
  64. RdataSet::destroy(mem_sgmt_, RRClass::IN(), rdataset);
  65. }
  66. TEST_F(RdataSetTest, createWithRRSIG) {
  67. // Normal case.
  68. RdataSet* rdataset = RdataSet::create(mem_sgmt_, encoder_, a_rrset_,
  69. rrsig_rrset_);
  70. EXPECT_EQ(RRTTL(1076895760), restoreTTL(rdataset->getTTLData()));
  71. EXPECT_EQ(1, rdataset->getSigRdataCount());
  72. RdataSet::destroy(mem_sgmt_, RRClass::IN(), rdataset);
  73. // Unusual case: TTL doesn't match. This implementation accepts that,
  74. // using the TTL of the covered RRset.
  75. ConstRRsetPtr rrsig_badttl(textToRRset(
  76. "www.example.com. 3600 IN RRSIG "
  77. "A 5 2 3600 20120814220826 "
  78. "20120715220826 1234 example.com. FAKE"));
  79. rdataset = RdataSet::create(mem_sgmt_, encoder_, a_rrset_, rrsig_badttl);
  80. EXPECT_EQ(RRTTL(1076895760), restoreTTL(rdataset->getTTLData()));
  81. RdataSet::destroy(mem_sgmt_, RRClass::IN(), rdataset);
  82. }
  83. TEST_F(RdataSetTest, createWithRRSIGOnly) {
  84. // A rare, but allowed, case: RdataSet without the main RRset but with
  85. // RRSIG.
  86. RdataSet* rdataset = RdataSet::create(mem_sgmt_, encoder_, ConstRRsetPtr(),
  87. rrsig_rrset_);
  88. EXPECT_EQ(RRType::A(), rdataset->type); // type covered is used as type
  89. EXPECT_EQ(RRTTL(1076895760), restoreTTL(rdataset->getTTLData()));
  90. EXPECT_EQ(0, rdataset->getRdataCount());
  91. EXPECT_EQ(1, rdataset->getSigRdataCount());
  92. RdataSet::destroy(mem_sgmt_, RRClass::IN(), rdataset);
  93. }
  94. TEST_F(RdataSetTest, badCeate) {
  95. // Neither the RRset nor RRSIG RRset is given
  96. EXPECT_THROW(RdataSet::create(mem_sgmt_, encoder_, ConstRRsetPtr(),
  97. ConstRRsetPtr()), isc::BadValue);
  98. // Empty RRset (An RRset without RDATA)
  99. ConstRRsetPtr empty_rrset(new RRset(Name("example.com"), RRClass::IN(),
  100. RRType::A(), RRTTL(3600)));
  101. EXPECT_THROW(RdataSet::create(mem_sgmt_, encoder_, empty_rrset,
  102. ConstRRsetPtr()), isc::BadValue);
  103. ConstRRsetPtr empty_rrsig(new RRset(Name("example.com"), RRClass::IN(),
  104. RRType::RRSIG(), RRTTL(3600)));
  105. EXPECT_THROW(RdataSet::create(mem_sgmt_, encoder_, ConstRRsetPtr(),
  106. empty_rrsig), isc::BadValue);
  107. // The RRset type and RRSIG's type covered don't match
  108. ConstRRsetPtr bad_rrsig(textToRRset(
  109. "www.example.com. 1076895760 IN RRSIG "
  110. "NS 5 2 3600 20120814220826 20120715220826 "
  111. "1234 example.com. FAKE"));
  112. EXPECT_THROW(RdataSet::create(mem_sgmt_, encoder_, a_rrset_, bad_rrsig),
  113. isc::BadValue);
  114. // Pass non RRSIG for the sig parameter
  115. EXPECT_THROW(RdataSet::create(mem_sgmt_, encoder_, a_rrset_, a_rrset_),
  116. isc::BadValue);
  117. // RR class doesn't match between RRset and RRSIG
  118. ConstRRsetPtr badclass_rrsig(textToRRset(
  119. "www.example.com. 1076895760 CH RRSIG "
  120. "A 5 2 3600 20120814220826 "
  121. "20120715220826 1234 example.com. FAKE",
  122. RRClass::CH()));
  123. EXPECT_THROW(RdataSet::create(mem_sgmt_, encoder_, a_rrset_,
  124. badclass_rrsig),
  125. isc::BadValue);
  126. }
  127. }