rdata_nsec3_unittest.cc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 <dns/exceptions.h>
  18. #include <dns/messagerenderer.h>
  19. #include <dns/rdata.h>
  20. #include <dns/rdataclass.h>
  21. #include <dns/rrclass.h>
  22. #include <dns/rrtype.h>
  23. #include <gtest/gtest.h>
  24. #include <dns/tests/unittest_util.h>
  25. #include <dns/tests/rdata_unittest.h>
  26. using isc::UnitTestUtil;
  27. using namespace std;
  28. using namespace isc;
  29. using namespace isc::dns;
  30. using namespace isc::util;
  31. using namespace isc::dns::rdata;
  32. namespace {
  33. // Note: some tests can be shared with NSEC3PARAM. They are unified as
  34. // typed tests defined in nsec3param_like_unittest.
  35. class Rdata_NSEC3_Test : public RdataTest {
  36. protected:
  37. Rdata_NSEC3_Test() :
  38. nsec3_txt("1 1 1 D399EAAB H9RSFB7FPF2L8HG35CMPC765TDK23RP6 "
  39. "A NS SOA"),
  40. nsec3_nosalt_txt("1 1 1 - H9RSFB7FPF2L8HG35CMPC765TDK23RP6 A NS SOA"),
  41. nsec3_notype_txt("1 1 1 D399EAAB H9RSFB7FPF2L8HG35CMPC765TDK23RP6"),
  42. rdata_nsec3(nsec3_txt)
  43. {}
  44. void checkFromText_None(const string& rdata_str) {
  45. checkFromText<generic::NSEC3, isc::Exception, isc::Exception>(
  46. rdata_str, rdata_nsec3, false, false);
  47. }
  48. void checkFromText_InvalidText(const string& rdata_str) {
  49. checkFromText<generic::NSEC3, InvalidRdataText, InvalidRdataText>(
  50. rdata_str, rdata_nsec3, true, true);
  51. }
  52. void checkFromText_BadValue(const string& rdata_str) {
  53. checkFromText<generic::NSEC3, BadValue, BadValue>(
  54. rdata_str, rdata_nsec3, true, true);
  55. }
  56. void checkFromText_LexerError(const string& rdata_str) {
  57. checkFromText
  58. <generic::NSEC3, InvalidRdataText, MasterLexer::LexerError>(
  59. rdata_str, rdata_nsec3, true, true);
  60. }
  61. void checkFromText_BadString(const string& rdata_str) {
  62. checkFromText
  63. <generic::NSEC3, InvalidRdataText, isc::Exception>(
  64. rdata_str, rdata_nsec3, true, false);
  65. }
  66. const string nsec3_txt;
  67. const string nsec3_nosalt_txt;
  68. const string nsec3_notype_txt;
  69. const generic::NSEC3 rdata_nsec3;
  70. };
  71. TEST_F(Rdata_NSEC3_Test, fromText) {
  72. // Hash that has the possible max length
  73. EXPECT_EQ(255, generic::NSEC3("1 1 1 D399EAAB " +
  74. string((255 * 8) / 5, '0') +
  75. " NS").getNext().size());
  76. // Hash is too long. Max = 255 bytes, base32-hex converts each 5 bytes
  77. // of the original to 8 characters, so 260 * 8 / 5 is the smallest length
  78. // of the encoded string that exceeds the max and doesn't require padding.
  79. checkFromText_InvalidText("1 1 1 D399EAAB " + string((260 * 8) / 5, '0') +
  80. " A NS SOA");
  81. // Type bitmap is empty. it's possible and allowed for NSEC3.
  82. EXPECT_NO_THROW(const generic::NSEC3 rdata_notype_nsec3(nsec3_notype_txt));
  83. // Empty salt is also okay.
  84. EXPECT_NO_THROW(const generic::NSEC3 rdata_nosalt_nsec3(nsec3_nosalt_txt));
  85. // Bad type mnemonics
  86. checkFromText_InvalidText("1 1 1 D399EAAB H9RSFB7FPF2L8HG35CMPC765TDK23RP6"
  87. " BIFF POW SPOON");
  88. // Bad base32hex
  89. checkFromText_BadValue("1 1 1 D399EAAB "
  90. "WXYZWXYZWXYZ=WXYZWXYZ==WXYZWXYZW A NS SOA");
  91. // Hash algorithm out of range
  92. checkFromText_InvalidText("256 1 1 D399EAAB "
  93. "H9RSFB7FPF2L8HG35CMPC765TDK23RP6 A NS SOA");
  94. // Flags out of range
  95. checkFromText_InvalidText("1 256 1 D399EAAB "
  96. "H9RSFB7FPF2L8HG35CMPC765TDK23RP6 A NS SOA");
  97. // Iterations out of range
  98. checkFromText_InvalidText("1 1 65536 D399EAAB "
  99. "H9RSFB7FPF2L8HG35CMPC765TDK23RP6 A NS SOA");
  100. // Space is not allowed in salt or the next hash. This actually
  101. // causes the Base32 decoder that parses the next hash that comes
  102. // afterwards, to throw.
  103. checkFromText_BadValue("1 1 1 D399 EAAB H9RSFB7FPF2L8"
  104. "HG35CMPC765TDK23RP6 A NS SOA");
  105. // Next hash must not contain padding (trailing '=' characters)
  106. checkFromText_InvalidText("1 1 1 D399EAAB "
  107. "AAECAwQFBgcICQoLDA0ODw== A NS SOA");
  108. // String instead of number
  109. checkFromText_LexerError("foo 1 1 D399EAAB "
  110. "H9RSFB7FPF2L8HG35CMPC765TDK23RP6 A NS SOA");
  111. checkFromText_LexerError("1 foo 1 D399EAAB "
  112. "H9RSFB7FPF2L8HG35CMPC765TDK23RP6 A NS SOA");
  113. checkFromText_LexerError("1 1 foo D399EAAB "
  114. "H9RSFB7FPF2L8HG35CMPC765TDK23RP6 A NS SOA");
  115. // Trailing garbage. This should cause only the string constructor
  116. // to fail, but the lexer constructor must be able to continue
  117. // parsing from it.
  118. checkFromText_BadString(
  119. "1 1 1 D399EAAB H9RSFB7FPF2L8HG35CMPC765TDK23RP6 A NS SOA ;comment\n"
  120. "1 1 1 D399EAAB H9RSFB7FPF2L8HG35CMPC765TDK23RP6 A NS SOA");
  121. // Unmatched parenthesis should cause a lexer error
  122. checkFromText_LexerError("1 1 1 D399EAAB "
  123. "H9RSFB7FPF2L8HG35CMPC765TDK23RP6 A ) NS SOA");
  124. }
  125. TEST_F(Rdata_NSEC3_Test, createFromWire) {
  126. // A valid NSEC3 RR with empty type bitmap.
  127. EXPECT_NO_THROW(rdataFactoryFromFile(RRType::NSEC3(), RRClass::IN(),
  128. "rdata_nsec3_fromWire15.wire"));
  129. // Invalid bitmap cases are tested in Rdata_NSECBITMAP_Test.
  130. // hash length is too large
  131. EXPECT_THROW(rdataFactoryFromFile(RRType::NSEC3(), RRClass::IN(),
  132. "rdata_nsec3_fromWire12.wire"),
  133. DNSMessageFORMERR);
  134. // empty hash. invalid.
  135. EXPECT_THROW(rdataFactoryFromFile(RRType::NSEC3(), RRClass::IN(),
  136. "rdata_nsec3_fromWire14.wire"),
  137. DNSMessageFORMERR);
  138. // RDLEN is too short to hold the hash length field
  139. EXPECT_THROW(rdataFactoryFromFile(RRType::NSEC3(), RRClass::IN(),
  140. "rdata_nsec3_fromWire17.wire"),
  141. DNSMessageFORMERR);
  142. // Short buffer cases. The data is valid NSEC3 RDATA, but the buffer
  143. // is trimmed at the end. All cases should result in an exception from
  144. // the buffer class.
  145. vector<uint8_t> data;
  146. UnitTestUtil::readWireData("rdata_nsec3_fromWire1", data);
  147. const uint16_t rdlen = (data.at(0) << 8) + data.at(1);
  148. for (int i = 0; i < rdlen; ++i) {
  149. // intentionally construct a short buffer
  150. InputBuffer b(&data[0] + 2, i);
  151. EXPECT_THROW(createRdata(RRType::NSEC3(), RRClass::IN(), b, 39),
  152. InvalidBufferPosition);
  153. }
  154. }
  155. TEST_F(Rdata_NSEC3_Test, createFromLexer) {
  156. EXPECT_EQ(0, rdata_nsec3.compare(
  157. *test::createRdataUsingLexer(RRType::NSEC3(), RRClass::IN(),
  158. nsec3_txt)));
  159. // empty salt is also okay.
  160. const generic::NSEC3 rdata_nosalt_nsec3(nsec3_nosalt_txt);
  161. EXPECT_EQ(0, rdata_nosalt_nsec3.compare(
  162. *test::createRdataUsingLexer(RRType::NSEC3(), RRClass::IN(),
  163. nsec3_nosalt_txt)));
  164. }
  165. TEST_F(Rdata_NSEC3_Test, assign) {
  166. generic::NSEC3 other_nsec3 = rdata_nsec3;
  167. EXPECT_EQ(0, rdata_nsec3.compare(other_nsec3));
  168. }
  169. TEST_F(Rdata_NSEC3_Test, compare) {
  170. // trivial case: self equivalence
  171. EXPECT_EQ(0, generic::NSEC3(nsec3_txt).compare(generic::NSEC3(nsec3_txt)));
  172. // comparison attempt between incompatible RR types should be rejected
  173. EXPECT_THROW(generic::NSEC3(nsec3_txt).compare(*rdata_nomatch),
  174. bad_cast);
  175. // test RDATAs, sorted in the ascendent order. We only check comparison
  176. // on NSEC3-specific fields. Bitmap comparison is tested in the bitmap
  177. // tests. Common cases for NSEC3 and NSECPARAM3 are in their shared tests.
  178. vector<generic::NSEC3> compare_set;
  179. compare_set.push_back(generic::NSEC3("1 1 1 FF99EA0000 D1K6GQ38"));
  180. compare_set.push_back(generic::NSEC3("1 1 1 FF99EA0000 D1K6GQ0000000000"));
  181. compare_set.push_back(generic::NSEC3("1 1 1 FF99EA0000 D1K6GQ00UUUUUUUU"));
  182. vector<generic::NSEC3>::const_iterator it;
  183. const vector<generic::NSEC3>::const_iterator it_end = compare_set.end();
  184. for (it = compare_set.begin(); it != it_end - 1; ++it) {
  185. SCOPED_TRACE("compare " + it->toText() + " to " + (it + 1)->toText());
  186. EXPECT_GT(0, (*it).compare(*(it + 1)));
  187. EXPECT_LT(0, (*(it + 1)).compare(*it));
  188. }
  189. }
  190. }