rdata_sshfp_unittest.cc 10 KB

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