rdata_sshfp_unittest.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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_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. // spacing in the fingerprint field
  80. checkFromText_None("2 1 123456789abcdef67890 123456789abcdef67890");
  81. // multi-line fingerprint field
  82. checkFromText_None("2 1 ( 123456789abcdef67890\n 123456789abcdef67890 )");
  83. // string constructor throws if there's extra text,
  84. // but lexer constructor doesn't
  85. checkFromText_BadString(sshfp_txt + "\n" + sshfp_txt);
  86. }
  87. TEST_F(Rdata_SSHFP_Test, algorithmTypes) {
  88. // Some of these may not be RFC conformant, but we relax the check
  89. // in our code to work with algorithm and fingerprint types that may
  90. // show up in the future.
  91. EXPECT_NO_THROW(const generic::SSHFP rdata_sshfp("1 1 12ab"));
  92. EXPECT_NO_THROW(const generic::SSHFP rdata_sshfp("2 1 12ab"));
  93. EXPECT_NO_THROW(const generic::SSHFP rdata_sshfp("3 1 12ab"));
  94. EXPECT_NO_THROW(const generic::SSHFP rdata_sshfp("128 1 12ab"));
  95. EXPECT_NO_THROW(const generic::SSHFP rdata_sshfp("255 1 12ab"));
  96. EXPECT_NO_THROW(const generic::SSHFP rdata_sshfp("1 1 12ab"));
  97. EXPECT_NO_THROW(const generic::SSHFP rdata_sshfp("1 2 12ab"));
  98. EXPECT_NO_THROW(const generic::SSHFP rdata_sshfp("1 3 12ab"));
  99. EXPECT_NO_THROW(const generic::SSHFP rdata_sshfp("1 128 12ab"));
  100. EXPECT_NO_THROW(const generic::SSHFP rdata_sshfp("1 255 12ab"));
  101. // 0 is reserved, but we allow that too
  102. EXPECT_NO_THROW(const generic::SSHFP rdata_sshfp("0 1 12ab"));
  103. EXPECT_NO_THROW(const generic::SSHFP rdata_sshfp("1 0 12ab"));
  104. // > 255 would be broken
  105. EXPECT_THROW(const generic::SSHFP rdata_sshfp("256 1 12ab"),
  106. InvalidRdataText);
  107. EXPECT_THROW(const generic::SSHFP rdata_sshfp("2 256 12ab"),
  108. InvalidRdataText);
  109. }
  110. TEST_F(Rdata_SSHFP_Test, badText) {
  111. checkFromText_LexerError("1");
  112. checkFromText_LexerError("ONE 2 123456789abcdef67890123456789abcdef67890");
  113. checkFromText_LexerError("1 TWO 123456789abcdef67890123456789abcdef67890");
  114. checkFromText_InvalidText("1 2 BUCKLEMYSHOE");
  115. checkFromText_InvalidText(sshfp_txt + " extra text");
  116. // yes, these are redundant to the last test cases in algorithmTypes
  117. checkFromText_InvalidText(
  118. "2345 1 123456789abcdef67890123456789abcdef67890");
  119. checkFromText_InvalidText(
  120. "2 1234 123456789abcdef67890123456789abcdef67890");
  121. // negative values are trapped in the lexer rather than the constructor
  122. checkFromText_LexerError("-2 1 123456789abcdef67890123456789abcdef67890");
  123. checkFromText_LexerError("2 -1 123456789abcdef67890123456789abcdef67890");
  124. }
  125. TEST_F(Rdata_SSHFP_Test, copyAndAssign) {
  126. // Copy construct
  127. generic::SSHFP rdata_sshfp2(rdata_sshfp);
  128. EXPECT_EQ(0, rdata_sshfp.compare(rdata_sshfp2));
  129. // Assignment, mainly to confirm it doesn't cause disruption.
  130. rdata_sshfp2 = rdata_sshfp;
  131. EXPECT_EQ(0, rdata_sshfp.compare(rdata_sshfp2));
  132. }
  133. TEST_F(Rdata_SSHFP_Test, createFromWire) {
  134. // Basic test
  135. EXPECT_EQ(0, rdata_sshfp.compare(
  136. *rdataFactoryFromFile(RRType("SSHFP"), RRClass("IN"),
  137. "rdata_sshfp_fromWire")));
  138. // Combination of lowercase and uppercase
  139. EXPECT_EQ(0, rdata_sshfp.compare(
  140. *rdataFactoryFromFile(RRType("SSHFP"), RRClass("IN"),
  141. "rdata_sshfp_fromWire2")));
  142. // algorithm=1, fingerprint=1
  143. EXPECT_NO_THROW(rdataFactoryFromFile(RRType("SSHFP"), RRClass("IN"),
  144. "rdata_sshfp_fromWire3.wire"));
  145. // algorithm=255, fingerprint=1
  146. EXPECT_NO_THROW(rdataFactoryFromFile(RRType("SSHFP"), RRClass("IN"),
  147. "rdata_sshfp_fromWire4.wire"));
  148. // algorithm=0, fingerprint=1
  149. EXPECT_NO_THROW(rdataFactoryFromFile(RRType("SSHFP"), RRClass("IN"),
  150. "rdata_sshfp_fromWire5.wire"));
  151. // algorithm=5, fingerprint=0
  152. EXPECT_NO_THROW(rdataFactoryFromFile(RRType("SSHFP"), RRClass("IN"),
  153. "rdata_sshfp_fromWire6.wire"));
  154. // algorithm=255, fingerprint=255
  155. EXPECT_NO_THROW(rdataFactoryFromFile(RRType("SSHFP"), RRClass("IN"),
  156. "rdata_sshfp_fromWire7.wire"));
  157. // short fingerprint data
  158. EXPECT_NO_THROW(rdataFactoryFromFile(RRType("SSHFP"), RRClass("IN"),
  159. "rdata_sshfp_fromWire8.wire"));
  160. // fingerprint is shorter than rdata len
  161. EXPECT_THROW(rdataFactoryFromFile(RRType("SSHFP"), RRClass("IN"),
  162. "rdata_sshfp_fromWire9"),
  163. InvalidBufferPosition);
  164. // fingerprint is missing
  165. EXPECT_THROW(rdataFactoryFromFile(RRType("SSHFP"), RRClass("IN"),
  166. "rdata_sshfp_fromWire10"),
  167. InvalidBufferPosition);
  168. // all rdata is missing
  169. EXPECT_THROW(rdataFactoryFromFile(RRType("SSHFP"), RRClass("IN"),
  170. "rdata_sshfp_fromWire11"),
  171. InvalidBufferPosition);
  172. }
  173. TEST_F(Rdata_SSHFP_Test, createFromParams) {
  174. const generic::SSHFP rdata_sshfp2(
  175. 2, 1, "123456789abcdef67890123456789abcdef67890");
  176. EXPECT_EQ(0, rdata_sshfp2.compare(rdata_sshfp));
  177. }
  178. TEST_F(Rdata_SSHFP_Test, toText) {
  179. EXPECT_TRUE(boost::iequals(sshfp_txt, rdata_sshfp.toText()));
  180. const string sshfp_txt2("2 1");
  181. const generic::SSHFP rdata_sshfp2(sshfp_txt2);
  182. EXPECT_TRUE(boost::iequals(sshfp_txt2, rdata_sshfp2.toText()));
  183. const generic::SSHFP rdata_sshfp3("2 1 ");
  184. EXPECT_TRUE(boost::iequals(sshfp_txt2, rdata_sshfp3.toText()));
  185. }
  186. TEST_F(Rdata_SSHFP_Test, toWire) {
  187. this->obuffer.clear();
  188. rdata_sshfp.toWire(this->obuffer);
  189. EXPECT_EQ(sizeof (rdata_sshfp_wiredata),
  190. this->obuffer.getLength());
  191. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  192. this->obuffer.getData(),
  193. this->obuffer.getLength(),
  194. rdata_sshfp_wiredata, sizeof(rdata_sshfp_wiredata));
  195. }
  196. TEST_F(Rdata_SSHFP_Test, compare) {
  197. const generic::SSHFP rdata_sshfp2("2 1");
  198. EXPECT_EQ(-1, rdata_sshfp2.compare(rdata_sshfp));
  199. EXPECT_EQ(1, rdata_sshfp.compare(rdata_sshfp2));
  200. }
  201. TEST_F(Rdata_SSHFP_Test, getAlgorithmNumber) {
  202. EXPECT_EQ(2, rdata_sshfp.getAlgorithmNumber());
  203. }
  204. TEST_F(Rdata_SSHFP_Test, getFingerprintType) {
  205. EXPECT_EQ(1, rdata_sshfp.getFingerprintType());
  206. }
  207. TEST_F(Rdata_SSHFP_Test, getFingerprint) {
  208. const std::vector<uint8_t>& fingerprint =
  209. rdata_sshfp.getFingerprint();
  210. EXPECT_EQ(rdata_sshfp.getFingerprintLength(),
  211. fingerprint.size());
  212. for (int i = 0; i < fingerprint.size(); ++i) {
  213. EXPECT_EQ(rdata_sshfp_wiredata[i + 2],
  214. fingerprint.at(i));
  215. }
  216. }
  217. TEST_F(Rdata_SSHFP_Test, getFingerprintLength) {
  218. EXPECT_EQ(20, rdata_sshfp.getFingerprintLength());
  219. }
  220. TEST_F(Rdata_SSHFP_Test, emptyFingerprintFromWire) {
  221. const uint8_t rdf_wiredata[] = {
  222. // algorithm
  223. 0x04,
  224. // fingerprint type
  225. 0x09
  226. };
  227. const generic::SSHFP rdf =
  228. dynamic_cast<const generic::SSHFP&>
  229. (*rdataFactoryFromFile(RRType("SSHFP"), RRClass("IN"),
  230. "rdata_sshfp_fromWire12"));
  231. EXPECT_EQ(4, rdf.getAlgorithmNumber());
  232. EXPECT_EQ(9, rdf.getFingerprintType());
  233. EXPECT_EQ(0, rdf.getFingerprintLength());
  234. this->obuffer.clear();
  235. rdf.toWire(this->obuffer);
  236. EXPECT_EQ(2, this->obuffer.getLength());
  237. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  238. this->obuffer.getData(),
  239. this->obuffer.getLength(),
  240. rdf_wiredata, sizeof(rdf_wiredata));
  241. }
  242. TEST_F(Rdata_SSHFP_Test, emptyFingerprintFromString) {
  243. const generic::SSHFP rdata_sshfp2("5 6");
  244. const uint8_t rdata_sshfp2_wiredata[] = {
  245. // algorithm
  246. 0x05,
  247. // fingerprint type
  248. 0x06
  249. };
  250. EXPECT_EQ(5, rdata_sshfp2.getAlgorithmNumber());
  251. EXPECT_EQ(6, rdata_sshfp2.getFingerprintType());
  252. EXPECT_EQ(0, rdata_sshfp2.getFingerprintLength());
  253. this->obuffer.clear();
  254. rdata_sshfp2.toWire(this->obuffer);
  255. EXPECT_EQ(2, this->obuffer.getLength());
  256. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  257. this->obuffer.getData(),
  258. this->obuffer.getLength(),
  259. rdata_sshfp2_wiredata, sizeof(rdata_sshfp2_wiredata));
  260. }
  261. }