rdata_sshfp_unittest.cc 9.8 KB

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