rdata_sshfp_unittest.cc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // Copyright (C) 2012 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 <algorithm>
  15. #include <string>
  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 <boost/algorithm/string.hpp>
  26. using isc::UnitTestUtil;
  27. using namespace std;
  28. using namespace isc::dns;
  29. using namespace isc::util;
  30. using namespace isc::dns::rdata;
  31. namespace {
  32. class Rdata_SSHFP_Test : public RdataTest {
  33. // there's nothing to specialize
  34. };
  35. const string sshfp_txt("2 1 123456789abcdef67890123456789abcdef67890");
  36. const generic::SSHFP rdata_sshfp(2, 1, "123456789abcdef67890123456789abcdef67890");
  37. const uint8_t rdata_sshfp_wiredata[] = {
  38. // algorithm
  39. 0x02,
  40. // fingerprint type
  41. 0x01,
  42. // fingerprint
  43. 0x12, 0x34, 0x56, 0x78,
  44. 0x9a, 0xbc, 0xde, 0xf6,
  45. 0x78, 0x90, 0x12, 0x34,
  46. 0x56, 0x78, 0x9a, 0xbc,
  47. 0xde, 0xf6, 0x78, 0x90
  48. };
  49. TEST_F(Rdata_SSHFP_Test, createFromText) {
  50. // Basic test
  51. const generic::SSHFP rdata_sshfp2(sshfp_txt);
  52. EXPECT_EQ(0, rdata_sshfp2.compare(rdata_sshfp));
  53. // With different spacing
  54. const generic::SSHFP rdata_sshfp3("2 1 123456789abcdef67890123456789abcdef67890");
  55. EXPECT_EQ(0, rdata_sshfp3.compare(rdata_sshfp));
  56. // Combination of lowercase and uppercase
  57. const generic::SSHFP rdata_sshfp4("2 1 123456789ABCDEF67890123456789abcdef67890");
  58. EXPECT_EQ(0, rdata_sshfp4.compare(rdata_sshfp));
  59. }
  60. TEST_F(Rdata_SSHFP_Test, algorithmTypes) {
  61. // Some of these may not be RFC conformant, but we relax the check
  62. // in our code to work with algorithm and fingerprint types that may
  63. // show up in the future.
  64. EXPECT_NO_THROW(const generic::SSHFP rdata_sshfp("1 1 12ab"));
  65. EXPECT_NO_THROW(const generic::SSHFP rdata_sshfp("2 1 12ab"));
  66. EXPECT_NO_THROW(const generic::SSHFP rdata_sshfp("3 1 12ab"));
  67. EXPECT_NO_THROW(const generic::SSHFP rdata_sshfp("128 1 12ab"));
  68. EXPECT_NO_THROW(const generic::SSHFP rdata_sshfp("255 1 12ab"));
  69. EXPECT_NO_THROW(const generic::SSHFP rdata_sshfp("1 1 12ab"));
  70. EXPECT_NO_THROW(const generic::SSHFP rdata_sshfp("1 2 12ab"));
  71. EXPECT_NO_THROW(const generic::SSHFP rdata_sshfp("1 3 12ab"));
  72. EXPECT_NO_THROW(const generic::SSHFP rdata_sshfp("1 128 12ab"));
  73. EXPECT_NO_THROW(const generic::SSHFP rdata_sshfp("1 255 12ab"));
  74. // 0 is reserved, but we allow that too
  75. EXPECT_NO_THROW(const generic::SSHFP rdata_sshfp("0 1 12ab"));
  76. EXPECT_NO_THROW(const generic::SSHFP rdata_sshfp("1 0 12ab"));
  77. // > 255 would be broken
  78. EXPECT_THROW(const generic::SSHFP rdata_sshfp("256 1 12ab"),
  79. InvalidRdataText);
  80. EXPECT_THROW(const generic::SSHFP rdata_sshfp("2 256 12ab"),
  81. InvalidRdataText);
  82. }
  83. TEST_F(Rdata_SSHFP_Test, badText) {
  84. EXPECT_THROW(const generic::SSHFP rdata_sshfp("1"), InvalidRdataText);
  85. EXPECT_THROW(const generic::SSHFP rdata_sshfp("BUCKLE MY SHOES"), InvalidRdataText);
  86. EXPECT_THROW(const generic::SSHFP rdata_sshfp("1 2 foo bar"), InvalidRdataText);
  87. }
  88. TEST_F(Rdata_SSHFP_Test, copy) {
  89. const generic::SSHFP rdata_sshfp2(rdata_sshfp);
  90. EXPECT_EQ(0, rdata_sshfp.compare(rdata_sshfp2));
  91. }
  92. TEST_F(Rdata_SSHFP_Test, createFromWire) {
  93. // Basic test
  94. EXPECT_EQ(0, rdata_sshfp.compare(
  95. *rdataFactoryFromFile(RRType("SSHFP"), RRClass("IN"),
  96. "rdata_sshfp_fromWire")));
  97. // Combination of lowercase and uppercase
  98. EXPECT_EQ(0, rdata_sshfp.compare(
  99. *rdataFactoryFromFile(RRType("SSHFP"), RRClass("IN"),
  100. "rdata_sshfp_fromWire2")));
  101. // algorithm=1, fingerprint=1
  102. EXPECT_NO_THROW(rdataFactoryFromFile(RRType("SSHFP"), RRClass("IN"),
  103. "rdata_sshfp_fromWire3.wire"));
  104. // algorithm=255, fingerprint=1
  105. EXPECT_NO_THROW(rdataFactoryFromFile(RRType("SSHFP"), RRClass("IN"),
  106. "rdata_sshfp_fromWire4.wire"));
  107. // algorithm=0, fingerprint=1
  108. EXPECT_NO_THROW(rdataFactoryFromFile(RRType("SSHFP"), RRClass("IN"),
  109. "rdata_sshfp_fromWire5.wire"));
  110. // algorithm=5, fingerprint=0
  111. EXPECT_NO_THROW(rdataFactoryFromFile(RRType("SSHFP"), RRClass("IN"),
  112. "rdata_sshfp_fromWire6.wire"));
  113. // algorithm=255, fingerprint=255
  114. EXPECT_NO_THROW(rdataFactoryFromFile(RRType("SSHFP"), RRClass("IN"),
  115. "rdata_sshfp_fromWire7.wire"));
  116. // short fingerprint data
  117. EXPECT_NO_THROW(rdataFactoryFromFile(RRType("SSHFP"), RRClass("IN"),
  118. "rdata_sshfp_fromWire8.wire"));
  119. // fingerprint is shorter than rdata len
  120. EXPECT_THROW(rdataFactoryFromFile(RRType("SSHFP"), RRClass("IN"),
  121. "rdata_sshfp_fromWire9"),
  122. InvalidBufferPosition);
  123. // fingerprint is missing
  124. EXPECT_THROW(rdataFactoryFromFile(RRType("SSHFP"), RRClass("IN"),
  125. "rdata_sshfp_fromWire10"),
  126. InvalidBufferPosition);
  127. // all rdata is missing
  128. EXPECT_THROW(rdataFactoryFromFile(RRType("SSHFP"), RRClass("IN"),
  129. "rdata_sshfp_fromWire11"),
  130. InvalidBufferPosition);
  131. }
  132. TEST_F(Rdata_SSHFP_Test, toText) {
  133. EXPECT_TRUE(boost::iequals(sshfp_txt, rdata_sshfp.toText()));
  134. const string sshfp_txt2("2 1");
  135. const generic::SSHFP rdata_sshfp2(sshfp_txt2);
  136. EXPECT_TRUE(boost::iequals(sshfp_txt2, rdata_sshfp2.toText()));
  137. const generic::SSHFP rdata_sshfp3("2 1 ");
  138. EXPECT_TRUE(boost::iequals(sshfp_txt2, rdata_sshfp3.toText()));
  139. }
  140. TEST_F(Rdata_SSHFP_Test, toWire) {
  141. this->obuffer.clear();
  142. rdata_sshfp.toWire(this->obuffer);
  143. EXPECT_EQ(22, this->obuffer.getLength());
  144. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  145. this->obuffer.getData(),
  146. this->obuffer.getLength(),
  147. rdata_sshfp_wiredata, sizeof(rdata_sshfp_wiredata));
  148. }
  149. TEST_F(Rdata_SSHFP_Test, compare) {
  150. const generic::SSHFP rdata_sshfp2("2 1");
  151. EXPECT_EQ(-1, rdata_sshfp2.compare(rdata_sshfp));
  152. EXPECT_EQ(1, rdata_sshfp.compare(rdata_sshfp2));
  153. }
  154. TEST_F(Rdata_SSHFP_Test, getAlgorithmNumber) {
  155. EXPECT_EQ(2, rdata_sshfp.getAlgorithmNumber());
  156. }
  157. TEST_F(Rdata_SSHFP_Test, getFingerprintType) {
  158. EXPECT_EQ(1, rdata_sshfp.getFingerprintType());
  159. }
  160. TEST_F(Rdata_SSHFP_Test, getFingerprintLen) {
  161. EXPECT_EQ(20, rdata_sshfp.getFingerprintLen());
  162. }
  163. TEST_F(Rdata_SSHFP_Test, emptyFingerprintFromWire) {
  164. const uint8_t rdf_wiredata[] = {
  165. // algorithm
  166. 0x04,
  167. // fingerprint type
  168. 0x09
  169. };
  170. const generic::SSHFP rdf =
  171. dynamic_cast<const generic::SSHFP&>
  172. (*rdataFactoryFromFile(RRType("SSHFP"), RRClass("IN"),
  173. "rdata_sshfp_fromWire12"));
  174. EXPECT_EQ(4, rdf.getAlgorithmNumber());
  175. EXPECT_EQ(9, rdf.getFingerprintType());
  176. EXPECT_EQ(0, rdf.getFingerprintLen());
  177. this->obuffer.clear();
  178. rdf.toWire(this->obuffer);
  179. EXPECT_EQ(2, this->obuffer.getLength());
  180. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  181. this->obuffer.getData(),
  182. this->obuffer.getLength(),
  183. rdf_wiredata, sizeof(rdf_wiredata));
  184. }
  185. TEST_F(Rdata_SSHFP_Test, emptyFingerprintFromString) {
  186. const generic::SSHFP rdata_sshfp2("5 6");
  187. const uint8_t rdata_sshfp2_wiredata[] = {
  188. // algorithm
  189. 0x05,
  190. // fingerprint type
  191. 0x06
  192. };
  193. EXPECT_EQ(5, rdata_sshfp2.getAlgorithmNumber());
  194. EXPECT_EQ(6, rdata_sshfp2.getFingerprintType());
  195. EXPECT_EQ(0, rdata_sshfp2.getFingerprintLen());
  196. this->obuffer.clear();
  197. rdata_sshfp2.toWire(this->obuffer);
  198. EXPECT_EQ(2, this->obuffer.getLength());
  199. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  200. this->obuffer.getData(),
  201. this->obuffer.getLength(),
  202. rdata_sshfp2_wiredata, sizeof(rdata_sshfp2_wiredata));
  203. }
  204. }