rdata_nsec3param_unittest.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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/messagerenderer.h>
  18. #include <dns/rdata.h>
  19. #include <dns/rdataclass.h>
  20. #include <dns/rrclass.h>
  21. #include <dns/rrtype.h>
  22. #include <gtest/gtest.h>
  23. #include <dns/tests/unittest_util.h>
  24. #include <dns/tests/rdata_unittest.h>
  25. using isc::UnitTestUtil;
  26. using namespace std;
  27. using namespace isc;
  28. using namespace isc::dns;
  29. using namespace isc::util;
  30. using namespace isc::dns::rdata;
  31. namespace {
  32. class Rdata_NSEC3PARAM_Test : public RdataTest {
  33. protected:
  34. Rdata_NSEC3PARAM_Test() :
  35. nsec3param_txt("1 1 1 D399EAAB"),
  36. nsec3param_nosalt_txt("1 1 1 -"),
  37. rdata_nsec3param(nsec3param_txt)
  38. {}
  39. void checkFromText_None(const string& rdata_str) {
  40. checkFromText<generic::NSEC3PARAM, isc::Exception, isc::Exception>(
  41. rdata_str, rdata_nsec3param, false, false);
  42. }
  43. void checkFromText_InvalidText(const string& rdata_str) {
  44. checkFromText<generic::NSEC3PARAM, InvalidRdataText, InvalidRdataText>(
  45. rdata_str, rdata_nsec3param, true, true);
  46. }
  47. void checkFromText_BadValue(const string& rdata_str) {
  48. checkFromText<generic::NSEC3PARAM, BadValue, BadValue>(
  49. rdata_str, rdata_nsec3param, true, true);
  50. }
  51. void checkFromText_LexerError(const string& rdata_str) {
  52. checkFromText
  53. <generic::NSEC3PARAM, InvalidRdataText, MasterLexer::LexerError>(
  54. rdata_str, rdata_nsec3param, true, true);
  55. }
  56. const string nsec3param_txt;
  57. const string nsec3param_nosalt_txt;
  58. const generic::NSEC3PARAM rdata_nsec3param;
  59. };
  60. TEST_F(Rdata_NSEC3PARAM_Test, fromText) {
  61. // Empty salt is okay.
  62. EXPECT_EQ(0, generic::NSEC3PARAM(nsec3param_nosalt_txt).getSalt().size());
  63. // Salt is missing.
  64. checkFromText_LexerError("1 1 1");
  65. // Salt has whitespace within. This only fails in the string
  66. // constructor, as the lexer constructor stops reading at the end of
  67. // its RDATA.
  68. const generic::NSEC3PARAM rdata_nsec3param2("1 1 1 D399");
  69. checkFromText<generic::NSEC3PARAM, InvalidRdataText, isc::Exception>(
  70. "1 1 1 D399 EAAB", rdata_nsec3param2, true, false);
  71. // Hash algorithm out of range.
  72. checkFromText_InvalidText("256 1 1 D399EAAB");
  73. // Flags out of range.
  74. checkFromText_InvalidText("1 256 1 D399EAAB");
  75. // Iterations out of range.
  76. checkFromText_InvalidText("1 1 65536 D399EAAB");
  77. // Bad hex sequence
  78. checkFromText_BadValue("1 1 256 D399EAABZOO");
  79. // String instead of number
  80. checkFromText_LexerError("foo 1 256 D399EAAB");
  81. checkFromText_LexerError("1 foo 256 D399EAAB");
  82. checkFromText_LexerError("1 1 foo D399EAAB");
  83. }
  84. TEST_F(Rdata_NSEC3PARAM_Test, toText) {
  85. EXPECT_EQ(nsec3param_txt, rdata_nsec3param.toText());
  86. // Garbage space at the end should be ok. RFC5155 only forbids
  87. // whitespace within the salt field, but any whitespace afterwards
  88. // should be fine.
  89. EXPECT_NO_THROW(generic::NSEC3PARAM("1 1 1 D399EAAB "));
  90. // Hash algorithm in range.
  91. EXPECT_NO_THROW(generic::NSEC3PARAM("255 1 1 D399EAAB"));
  92. // Flags in range.
  93. EXPECT_NO_THROW(generic::NSEC3PARAM("1 255 1 D399EAAB"));
  94. // Iterations in range.
  95. EXPECT_NO_THROW(generic::NSEC3PARAM("1 1 65535 D399EAAB"));
  96. }
  97. TEST_F(Rdata_NSEC3PARAM_Test, createFromWire) {
  98. EXPECT_EQ(0, rdata_nsec3param.compare(
  99. *rdataFactoryFromFile(RRType::NSEC3PARAM(), RRClass::IN(),
  100. "rdata_nsec3param_fromWire1")));
  101. // Short buffer cases. The data is valid NSEC3PARAM RDATA, but the buffer
  102. // is trimmed at the end. All cases should result in an exception from
  103. // the buffer class.
  104. vector<uint8_t> data;
  105. UnitTestUtil::readWireData("rdata_nsec3param_fromWire1", data);
  106. const uint16_t rdlen = (data.at(0) << 8) + data.at(1);
  107. for (int i = 0; i < rdlen; ++i) {
  108. // intentionally construct a short buffer
  109. InputBuffer b(&data[0] + 2, i);
  110. EXPECT_THROW(createRdata(RRType::NSEC3PARAM(), RRClass::IN(), b, 9),
  111. InvalidBufferPosition);
  112. }
  113. }
  114. TEST_F(Rdata_NSEC3PARAM_Test, createFromLexer) {
  115. EXPECT_EQ(0, rdata_nsec3param.compare(
  116. *test::createRdataUsingLexer(RRType::NSEC3PARAM(), RRClass::IN(),
  117. nsec3param_txt)));
  118. // empty salt is also okay.
  119. const generic::NSEC3PARAM rdata_nosalt_nsec3param(nsec3param_nosalt_txt);
  120. EXPECT_EQ(0, rdata_nosalt_nsec3param.compare(
  121. *test::createRdataUsingLexer(RRType::NSEC3PARAM(), RRClass::IN(),
  122. nsec3param_nosalt_txt)));
  123. }
  124. TEST_F(Rdata_NSEC3PARAM_Test, toWireRenderer) {
  125. renderer.skip(2);
  126. rdata_nsec3param.toWire(renderer);
  127. vector<unsigned char> data;
  128. UnitTestUtil::readWireData("rdata_nsec3param_fromWire1", data);
  129. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  130. static_cast<const uint8_t *>(renderer.getData()) + 2,
  131. renderer.getLength() - 2, &data[2], data.size() - 2);
  132. }
  133. TEST_F(Rdata_NSEC3PARAM_Test, toWireBuffer) {
  134. rdata_nsec3param.toWire(obuffer);
  135. vector<unsigned char> data;
  136. UnitTestUtil::readWireData("rdata_nsec3param_fromWire1", data);
  137. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  138. obuffer.getData(), obuffer.getLength(),
  139. &data[2], data.size() - 2);
  140. }
  141. TEST_F(Rdata_NSEC3PARAM_Test, getHashAlg) {
  142. EXPECT_EQ(1, rdata_nsec3param.getHashalg());
  143. }
  144. TEST_F(Rdata_NSEC3PARAM_Test, getFlags) {
  145. EXPECT_EQ(1, rdata_nsec3param.getFlags());
  146. }
  147. TEST_F(Rdata_NSEC3PARAM_Test, assign) {
  148. generic::NSEC3PARAM other_nsec3param = rdata_nsec3param;
  149. EXPECT_EQ(0, rdata_nsec3param.compare(other_nsec3param));
  150. }
  151. TEST_F(Rdata_NSEC3PARAM_Test, compare) {
  152. // trivial case: self equivalence
  153. EXPECT_EQ(0, generic::NSEC3PARAM(nsec3param_txt).
  154. compare(generic::NSEC3PARAM(nsec3param_txt)));
  155. EXPECT_EQ(0, generic::NSEC3PARAM("1 1 1 -").
  156. compare(generic::NSEC3PARAM("1 1 1 -")));
  157. // comparison attempt between incompatible RR types should be rejected
  158. EXPECT_THROW(generic::NSEC3PARAM(nsec3param_txt).compare(*rdata_nomatch),
  159. bad_cast);
  160. }
  161. }