rdata_dnskey_unittest.cc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. #include <util/unittests/wiredata.h>
  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. using isc::UnitTestUtil;
  32. using isc::util::unittests::matchWireData;
  33. namespace {
  34. class Rdata_DNSKEY_Test : public RdataTest {
  35. protected:
  36. Rdata_DNSKEY_Test() :
  37. dnskey_txt("257 3 5 BEAAAAOhHQDBrhQbtphgq2wQUpEQ5t4DtUHxoMV"
  38. "Fu2hWLDMvoOMRXjGrhhCeFvAZih7yJHf8ZGfW6hd38hXG/x"
  39. "ylYCO6Krpbdojwx8YMXLA5/kA+u50WIL8ZR1R6KTbsYVMf/"
  40. "Qx5RiNbPClw+vT+U8eXEJmO20jIS1ULgqy347cBB1zMnnz/"
  41. "4LJpA0da9CbKj3A254T515sNIMcwsB8/2+2E63/zZrQzBkj"
  42. "0BrN/9Bexjpiks3jRhZatEsXn3dTy47R09Uix5WcJt+xzqZ"
  43. "7+ysyLKOOedS39Z7SDmsn2eA0FKtQpwA6LXeG2w+jxmw3oA"
  44. "8lVUgEf/rzeC/bByBNsO70aEFTd"),
  45. dnskey_txt2("257 3 5 YmluZDEwLmlzYy5vcmc="),
  46. rdata_dnskey(dnskey_txt),
  47. rdata_dnskey2(dnskey_txt2)
  48. {}
  49. void checkFromText_None(const string& rdata_str) {
  50. checkFromText<generic::DNSKEY, isc::Exception, isc::Exception>(
  51. rdata_str, rdata_dnskey2, false, false);
  52. }
  53. void checkFromText_InvalidText(const string& rdata_str) {
  54. checkFromText<generic::DNSKEY, InvalidRdataText, InvalidRdataText>(
  55. rdata_str, rdata_dnskey2, true, true);
  56. }
  57. void checkFromText_InvalidLength(const string& rdata_str) {
  58. checkFromText<generic::DNSKEY, InvalidRdataLength, InvalidRdataLength>(
  59. rdata_str, rdata_dnskey2, true, true);
  60. }
  61. void checkFromText_BadValue(const string& rdata_str) {
  62. checkFromText<generic::DNSKEY, BadValue, BadValue>(
  63. rdata_str, rdata_dnskey2, true, true);
  64. }
  65. void checkFromText_LexerError(const string& rdata_str) {
  66. checkFromText
  67. <generic::DNSKEY, InvalidRdataText, MasterLexer::LexerError>(
  68. rdata_str, rdata_dnskey2, true, true);
  69. }
  70. void checkFromText_BadString(const string& rdata_str) {
  71. checkFromText
  72. <generic::DNSKEY, InvalidRdataText, isc::Exception>(
  73. rdata_str, rdata_dnskey2, true, false);
  74. }
  75. const string dnskey_txt;
  76. const string dnskey_txt2;
  77. const generic::DNSKEY rdata_dnskey;
  78. const generic::DNSKEY rdata_dnskey2;
  79. };
  80. TEST_F(Rdata_DNSKEY_Test, fromText) {
  81. EXPECT_EQ(dnskey_txt, rdata_dnskey.toText());
  82. // Space in key data is OK
  83. checkFromText_None("257 3 5 YmluZDEw LmlzYy5vcmc=");
  84. // Delimited number in key data is OK
  85. checkFromText_None("257 3 5 YmluZDEwLmlzYy 5 vcmc=");
  86. // Missing keydata is OK
  87. EXPECT_NO_THROW(const generic::DNSKEY rdata_dnskey3("257 3 5"));
  88. // Key data too short for RSA/MD5 algorithm is OK when
  89. // constructing. But getTag() on this object would throw (see
  90. // .getTag tests).
  91. EXPECT_NO_THROW(const generic::DNSKEY rdata_dnskey4("1 1 1 YQ=="));
  92. // Flags field out of range
  93. checkFromText_InvalidText("65536 3 5 YmluZDEwLmlzYy5vcmc=");
  94. // Protocol field out of range
  95. checkFromText_InvalidText("257 256 5 YmluZDEwLmlzYy5vcmc=");
  96. // Algorithm field out of range
  97. checkFromText_InvalidText("257 3 256 YmluZDEwLmlzYy5vcmc=");
  98. // Missing algorithm field
  99. checkFromText_LexerError("257 3 YmluZDEwLmlzYy5vcmc=");
  100. // Invalid key data field (not Base64)
  101. checkFromText_BadValue("257 3 5 BAAAAAAAAAAAD");
  102. // String instead of number
  103. checkFromText_LexerError("foo 3 5 YmluZDEwLmlzYy5vcmc=");
  104. checkFromText_LexerError("257 foo 5 YmluZDEwLmlzYy5vcmc=");
  105. checkFromText_LexerError("257 3 foo YmluZDEwLmlzYy5vcmc=");
  106. // Trailing garbage. This should cause only the string constructor
  107. // to fail, but the lexer constructor must be able to continue
  108. // parsing from it.
  109. checkFromText_BadString("257 3 5 YmluZDEwLmlzYy5vcmc= ; comment\n"
  110. "257 3 4 YmluZDEwLmlzYy5vcmc=");
  111. // Unmatched parenthesis should cause a lexer error
  112. checkFromText_LexerError("257 3 5 )YmluZDEwLmlzYy5vcmc=");
  113. }
  114. TEST_F(Rdata_DNSKEY_Test, assign) {
  115. generic::DNSKEY rdata_dnskey2("257 3 5 YQ==");
  116. rdata_dnskey2 = rdata_dnskey;
  117. EXPECT_EQ(0, rdata_dnskey.compare(rdata_dnskey2));
  118. }
  119. TEST_F(Rdata_DNSKEY_Test, createFromLexer) {
  120. EXPECT_EQ(0, rdata_dnskey.compare(
  121. *test::createRdataUsingLexer(RRType::DNSKEY(), RRClass::IN(),
  122. dnskey_txt)));
  123. }
  124. TEST_F(Rdata_DNSKEY_Test, toWireRenderer) {
  125. renderer.skip(2);
  126. rdata_dnskey.toWire(renderer);
  127. vector<unsigned char> data;
  128. UnitTestUtil::readWireData("rdata_dnskey_fromWire.wire", data);
  129. matchWireData(&data[2], data.size() - 2,
  130. static_cast<const uint8_t *>(renderer.getData()) + 2,
  131. renderer.getLength() - 2);
  132. }
  133. TEST_F(Rdata_DNSKEY_Test, toWireBuffer) {
  134. rdata_dnskey.toWire(obuffer);
  135. vector<unsigned char> data;
  136. UnitTestUtil::readWireData("rdata_dnskey_fromWire.wire", data);
  137. matchWireData(&data[2], data.size() - 2,
  138. obuffer.getData(), obuffer.getLength());
  139. }
  140. TEST_F(Rdata_DNSKEY_Test, createFromWire) {
  141. EXPECT_EQ(0, rdata_dnskey.compare(
  142. *rdataFactoryFromFile(RRType("DNSKEY"), RRClass("IN"),
  143. "rdata_dnskey_fromWire.wire")));
  144. // Missing keydata is OK
  145. const generic::DNSKEY rdata_dnskey_missing_keydata("257 3 5");
  146. EXPECT_EQ(0, rdata_dnskey_missing_keydata.compare(
  147. *rdataFactoryFromFile(RRType("DNSKEY"), RRClass("IN"),
  148. "rdata_dnskey_empty_keydata_fromWire.wire")));
  149. }
  150. TEST_F(Rdata_DNSKEY_Test, getTag) {
  151. EXPECT_EQ(12892, rdata_dnskey.getTag());
  152. // Short keydata with algorithm RSA/MD5 must throw.
  153. const generic::DNSKEY rdata_dnskey_short_keydata1("1 1 1 YQ==");
  154. EXPECT_THROW(rdata_dnskey_short_keydata1.getTag(), isc::OutOfRange);
  155. // Short keydata with algorithm not RSA/MD5 must not throw.
  156. const generic::DNSKEY rdata_dnskey_short_keydata2("257 3 5 YQ==");
  157. EXPECT_NO_THROW(rdata_dnskey_short_keydata2.getTag());
  158. }
  159. TEST_F(Rdata_DNSKEY_Test, getAlgorithm) {
  160. EXPECT_EQ(5, rdata_dnskey.getAlgorithm());
  161. }
  162. TEST_F(Rdata_DNSKEY_Test, getFlags) {
  163. EXPECT_EQ(257, rdata_dnskey.getFlags());
  164. }
  165. }