rdata_nsec3_unittest.cc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. // empty salt is also okay.
  61. EXPECT_NO_THROW(const generic::NSEC3 rdata_nosalt_nsec3(nsec3_nosalt_txt));
  62. }
  63. TEST_F(Rdata_NSEC3_Test, badText) {
  64. EXPECT_THROW(generic::NSEC3("1 1 1 ADDAFEEE "
  65. "0123456789ABCDEFGHIJKLMNOPQRSTUV "
  66. "BIFF POW SPOON"),
  67. InvalidRdataText);
  68. EXPECT_THROW(generic::NSEC3("1 1 1 ADDAFEEE "
  69. "WXYZWXYZWXYZ=WXYZWXYZ==WXYZWXYZW A NS SOA"),
  70. BadValue); // bad base32hex
  71. EXPECT_THROW(generic::NSEC3("1 1 1000000 ADDAFEEE "
  72. "0123456789ABCDEFGHIJKLMNOPQRSTUV A NS SOA"),
  73. InvalidRdataText);
  74. // Next hash shouldn't be padded
  75. EXPECT_THROW(generic::NSEC3("1 1 1 ADDAFEEE CPNMU=== A NS SOA"),
  76. InvalidRdataText);
  77. // String instead of number
  78. EXPECT_THROW(generic::NSEC3("foo 1 1 ADDAFEEECPNMU=== A NS SOA"),
  79. InvalidRdataText);
  80. EXPECT_THROW(generic::NSEC3("1 foo 1 ADDAFEEECPNMU=== A NS SOA"),
  81. InvalidRdataText);
  82. EXPECT_THROW(generic::NSEC3("1 1 foo ADDAFEEECPNMU=== A NS SOA"),
  83. InvalidRdataText);
  84. // Hash is too long. Max = 255 bytes, base32-hex converts each 5 bytes
  85. // of the original to 8 characters, so 260 * 8 / 5 is the smallest length
  86. // of the encoded string that exceeds the max and doesn't require padding.
  87. EXPECT_THROW(generic::NSEC3("1 1 1 D399EAAB " + string((260 * 8) / 5, '0') +
  88. " NS"),
  89. InvalidRdataText);
  90. }
  91. TEST_F(Rdata_NSEC3_Test, createFromWire) {
  92. // A valid NSEC3 RR with empty type bitmap.
  93. EXPECT_NO_THROW(rdataFactoryFromFile(RRType::NSEC3(), RRClass::IN(),
  94. "rdata_nsec3_fromWire15.wire"));
  95. // Invalid bitmap cases are tested in Rdata_NSECBITMAP_Test.
  96. // hash length is too large
  97. EXPECT_THROW(rdataFactoryFromFile(RRType::NSEC3(), RRClass::IN(),
  98. "rdata_nsec3_fromWire12.wire"),
  99. DNSMessageFORMERR);
  100. // empty hash. invalid.
  101. EXPECT_THROW(rdataFactoryFromFile(RRType::NSEC3(), RRClass::IN(),
  102. "rdata_nsec3_fromWire14.wire"),
  103. DNSMessageFORMERR);
  104. // RDLEN is too short to hold the hash length field
  105. EXPECT_THROW(rdataFactoryFromFile(RRType::NSEC3(), RRClass::IN(),
  106. "rdata_nsec3_fromWire17.wire"),
  107. DNSMessageFORMERR);
  108. // Short buffer cases. The data is valid NSEC3 RDATA, but the buffer
  109. // is trimmed at the end. All cases should result in an exception from
  110. // the buffer class.
  111. vector<uint8_t> data;
  112. UnitTestUtil::readWireData("rdata_nsec3_fromWire1", data);
  113. const uint16_t rdlen = (data.at(0) << 8) + data.at(1);
  114. for (int i = 0; i < rdlen; ++i) {
  115. // intentionally construct a short buffer
  116. InputBuffer b(&data[0] + 2, i);
  117. EXPECT_THROW(createRdata(RRType::NSEC3(), RRClass::IN(), b, 39),
  118. InvalidBufferPosition);
  119. }
  120. }
  121. TEST_F(Rdata_NSEC3_Test, createFromLexer) {
  122. const generic::NSEC3 rdata_nsec3(nsec3_txt);
  123. EXPECT_EQ(0, rdata_nsec3.compare(
  124. *test::createRdataUsingLexer(RRType::NSEC3(), RRClass::IN(),
  125. nsec3_txt)));
  126. // empty salt is also okay.
  127. const generic::NSEC3 rdata_nosalt_nsec3(nsec3_nosalt_txt);
  128. EXPECT_EQ(0, rdata_nosalt_nsec3.compare(
  129. *test::createRdataUsingLexer(RRType::NSEC3(), RRClass::IN(),
  130. nsec3_nosalt_txt)));
  131. // Exceptions cause NULL to be returned.
  132. // hash algorithm out of range
  133. EXPECT_FALSE(test::createRdataUsingLexer(RRType::NSEC3(), RRClass::IN(),
  134. "256 1 1 D399EAAB H9RSFB7FPF2L8"
  135. "HG35CMPC765TDK23RP6"));
  136. // flags out of range
  137. EXPECT_FALSE(test::createRdataUsingLexer(RRType::NSEC3(), RRClass::IN(),
  138. "1 256 1 D399EAAB H9RSFB7FPF2L8"
  139. "HG35CMPC765TDK23RP6"));
  140. // iterations out of range
  141. EXPECT_FALSE(test::createRdataUsingLexer(RRType::NSEC3(), RRClass::IN(),
  142. "1 1 65536 D399EAAB H9RSFB7FPF2L8"
  143. "HG35CMPC765TDK23RP6"));
  144. // space is not allowed in salt
  145. EXPECT_FALSE(test::createRdataUsingLexer(RRType::NSEC3(), RRClass::IN(),
  146. "1 1 1 D399 EAAB H9RSFB7FPF2L8"
  147. "HG35CMPC765TDK23RP6"));
  148. // Next hash is padded.
  149. EXPECT_FALSE(test::createRdataUsingLexer(RRType::NSEC3(), RRClass::IN(),
  150. "1 1 1 ADDAFEEE CPNMU=== "
  151. "A NS SOA"));
  152. }
  153. TEST_F(Rdata_NSEC3_Test, assign) {
  154. generic::NSEC3 rdata_nsec3(nsec3_txt);
  155. generic::NSEC3 other_nsec3 = rdata_nsec3;
  156. EXPECT_EQ(0, rdata_nsec3.compare(other_nsec3));
  157. }
  158. TEST_F(Rdata_NSEC3_Test, compare) {
  159. // trivial case: self equivalence
  160. EXPECT_EQ(0, generic::NSEC3(nsec3_txt).compare(generic::NSEC3(nsec3_txt)));
  161. // comparison attempt between incompatible RR types should be rejected
  162. EXPECT_THROW(generic::NSEC3(nsec3_txt).compare(*rdata_nomatch),
  163. bad_cast);
  164. // test RDATAs, sorted in the ascendent order. We only check comparison
  165. // on NSEC3-specific fields. Bitmap comparison is tested in the bitmap
  166. // tests. Common cases for NSEC3 and NSECPARAM3 are in their shared tests.
  167. vector<generic::NSEC3> compare_set;
  168. compare_set.push_back(generic::NSEC3("1 1 1 FF99EA0000 D1K6GQ38"));
  169. compare_set.push_back(generic::NSEC3("1 1 1 FF99EA0000 D1K6GQ0000000000"));
  170. compare_set.push_back(generic::NSEC3("1 1 1 FF99EA0000 D1K6GQ00UUUUUUUU"));
  171. vector<generic::NSEC3>::const_iterator it;
  172. const vector<generic::NSEC3>::const_iterator it_end = compare_set.end();
  173. for (it = compare_set.begin(); it != it_end - 1; ++it) {
  174. SCOPED_TRACE("compare " + it->toText() + " to " + (it + 1)->toText());
  175. EXPECT_GT(0, (*it).compare(*(it + 1)));
  176. EXPECT_LT(0, (*(it + 1)).compare(*it));
  177. }
  178. }
  179. }