rdata_nsec3_unittest.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Copyright (C) 2010 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 <string>
  15. #include <exceptions/exceptions.h>
  16. #include <util/buffer.h>
  17. #include <util/encode/hex.h>
  18. #include <dns/exceptions.h>
  19. #include <dns/messagerenderer.h>
  20. #include <dns/rdata.h>
  21. #include <dns/rdataclass.h>
  22. #include <dns/rrclass.h>
  23. #include <dns/rrtype.h>
  24. #include <gtest/gtest.h>
  25. #include <dns/tests/unittest_util.h>
  26. #include <dns/tests/rdata_unittest.h>
  27. using isc::UnitTestUtil;
  28. using namespace std;
  29. using namespace isc;
  30. using namespace isc::dns;
  31. using namespace isc::util;
  32. using namespace isc::util::encode;
  33. using namespace isc::dns::rdata;
  34. namespace {
  35. // Note: some tests can be shared with NSEC3PARAM. They are unified as
  36. // typed tests defined in nsec3param_like_unittest.
  37. class Rdata_NSEC3_Test : public RdataTest {
  38. // there's nothing to specialize
  39. public:
  40. Rdata_NSEC3_Test() :
  41. nsec3_txt("1 1 1 D399EAAB H9RSFB7FPF2L8HG35CMPC765TDK23RP6 "
  42. "NS SOA RRSIG DNSKEY NSEC3PARAM"),
  43. nsec3_nosalt_txt("1 1 1 - H9RSFB7FPF2L8HG35CMPC765TDK23RP6 A" )
  44. {}
  45. const string nsec3_txt;
  46. const string nsec3_nosalt_txt;
  47. };
  48. TEST_F(Rdata_NSEC3_Test, fromText) {
  49. // A normal case: the test constructor should successfully parse the
  50. // text and construct nsec3_txt. It will be tested against the wire format
  51. // representation in the createFromWire test.
  52. // hash that has the possible max length (see badText about the magic
  53. // numbers)
  54. EXPECT_EQ(255, generic::NSEC3("1 1 1 D399EAAB " +
  55. string((255 * 8) / 5, '0') +
  56. " NS").getNext().size());
  57. // type bitmap is empty. it's possible and allowed for NSEC3.
  58. EXPECT_NO_THROW(generic::NSEC3(
  59. "1 1 1 D399EAAB H9RSFB7FPF2L8HG35CMPC765TDK23RP6"));
  60. }
  61. TEST_F(Rdata_NSEC3_Test, badText) {
  62. EXPECT_THROW(generic::NSEC3("1 1 1 ADDAFEEE "
  63. "0123456789ABCDEFGHIJKLMNOPQRSTUV "
  64. "BIFF POW SPOON"),
  65. InvalidRdataText);
  66. EXPECT_THROW(generic::NSEC3("1 1 1 ADDAFEEE "
  67. "WXYZWXYZWXYZ=WXYZWXYZ==WXYZWXYZW A NS SOA"),
  68. BadValue); // bad base32hex
  69. EXPECT_THROW(generic::NSEC3("1 1 1000000 ADDAFEEE "
  70. "0123456789ABCDEFGHIJKLMNOPQRSTUV A NS SOA"),
  71. InvalidRdataText);
  72. // Next hash shouldn't be padded
  73. EXPECT_THROW(generic::NSEC3("1 1 1 ADDAFEEE CPNMU=== A NS SOA"),
  74. InvalidRdataText);
  75. // Hash is too long. Max = 255 bytes, base32-hex converts each 5 bytes
  76. // of the original to 8 characters, so 260 * 8 / 5 is the smallest length
  77. // of the encoded string that exceeds the max and doesn't require padding.
  78. EXPECT_THROW(generic::NSEC3("1 1 1 D399EAAB " + string((260 * 8) / 5, '0') +
  79. " NS"),
  80. InvalidRdataText);
  81. }
  82. TEST_F(Rdata_NSEC3_Test, createFromWire) {
  83. // A valid NSEC3 RR with empty type bitmap.
  84. EXPECT_NO_THROW(rdataFactoryFromFile(RRType::NSEC3(), RRClass::IN(),
  85. "rdata_nsec3_fromWire15.wire"));
  86. // Invalid bitmap cases are tested in Rdata_NSECBITMAP_Test.
  87. // hash length is too large
  88. EXPECT_THROW(rdataFactoryFromFile(RRType::NSEC3(), RRClass::IN(),
  89. "rdata_nsec3_fromWire12.wire"),
  90. DNSMessageFORMERR);
  91. // empty hash. invalid.
  92. EXPECT_THROW(rdataFactoryFromFile(RRType::NSEC3(), RRClass::IN(),
  93. "rdata_nsec3_fromWire14.wire"),
  94. DNSMessageFORMERR);
  95. // RDLEN is too short to hold the hash length field
  96. EXPECT_THROW(rdataFactoryFromFile(RRType::NSEC3(), RRClass::IN(),
  97. "rdata_nsec3_fromWire17.wire"),
  98. DNSMessageFORMERR);
  99. // Short buffer cases. The data is valid NSEC3 RDATA, but the buffer
  100. // is trimmed at the end. All cases should result in an exception from
  101. // the buffer class.
  102. vector<uint8_t> data;
  103. UnitTestUtil::readWireData("rdata_nsec3_fromWire1", data);
  104. const uint16_t rdlen = (data.at(0) << 8) + data.at(1);
  105. for (int i = 0; i < rdlen; ++i) {
  106. // intentionally construct a short buffer
  107. InputBuffer b(&data[0] + 2, i);
  108. EXPECT_THROW(createRdata(RRType::NSEC3(), RRClass::IN(), b, 39),
  109. InvalidBufferPosition);
  110. }
  111. }
  112. TEST_F(Rdata_NSEC3_Test, assign) {
  113. generic::NSEC3 rdata_nsec3(nsec3_txt);
  114. generic::NSEC3 other_nsec3 = rdata_nsec3;
  115. EXPECT_EQ(0, rdata_nsec3.compare(other_nsec3));
  116. }
  117. TEST_F(Rdata_NSEC3_Test, compare) {
  118. // trivial case: self equivalence
  119. EXPECT_EQ(0, generic::NSEC3(nsec3_txt).compare(generic::NSEC3(nsec3_txt)));
  120. // comparison attempt between incompatible RR types should be rejected
  121. EXPECT_THROW(generic::NSEC3(nsec3_txt).compare(*rdata_nomatch),
  122. bad_cast);
  123. // test RDATAs, sorted in the ascendent order. We only check comparison
  124. // on NSEC3-specific fields. Bitmap comparison is tested in the bitmap
  125. // tests. Common cases for NSEC3 and NSECPARAM3 are in their shared tests.
  126. vector<generic::NSEC3> compare_set;
  127. compare_set.push_back(generic::NSEC3("1 1 1 FF99EA0000 D1K6GQ38"));
  128. compare_set.push_back(generic::NSEC3("1 1 1 FF99EA0000 D1K6GQ0000000000"));
  129. compare_set.push_back(generic::NSEC3("1 1 1 FF99EA0000 D1K6GQ00UUUUUUUU"));
  130. vector<generic::NSEC3>::const_iterator it;
  131. const vector<generic::NSEC3>::const_iterator it_end = compare_set.end();
  132. for (it = compare_set.begin(); it != it_end - 1; ++it) {
  133. SCOPED_TRACE("compare " + it->toText() + " to " + (it + 1)->toText());
  134. EXPECT_GT(0, (*it).compare(*(it + 1)));
  135. EXPECT_LT(0, (*(it + 1)).compare(*it));
  136. }
  137. }
  138. }