rdata_dnskey_unittest.cc 6.7 KB

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