tsigrecord_unittest.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright (C) 2011 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 <vector>
  15. #include <sstream>
  16. #include <gtest/gtest.h>
  17. #include <util/buffer.h>
  18. #include <dns/exceptions.h>
  19. #include <dns/messagerenderer.h>
  20. #include <dns/name.h>
  21. #include <dns/rdata.h>
  22. #include <dns/rdataclass.h>
  23. #include <dns/tsig.h>
  24. #include <dns/tsigkey.h>
  25. #include <dns/tsigrecord.h>
  26. #include <dns/tests/unittest_util.h>
  27. using namespace std;
  28. using namespace isc::util;
  29. using namespace isc::dns;
  30. using namespace isc::dns::rdata;
  31. using isc::UnitTestUtil;
  32. namespace {
  33. class TSIGRecordTest : public ::testing::Test {
  34. protected:
  35. TSIGRecordTest() :
  36. test_name("www.example.com"), test_mac(16, 0xda),
  37. test_rdata(any::TSIG(TSIGKey::HMACMD5_NAME(), 0x4da8877a,
  38. TSIGContext::DEFAULT_FUDGE,
  39. test_mac.size(), &test_mac[0],
  40. 0x2d65, 0, 0, NULL)),
  41. test_record(test_name, test_rdata),
  42. buffer(0), renderer(buffer)
  43. {}
  44. const Name test_name;
  45. vector<unsigned char> test_mac;
  46. const any::TSIG test_rdata;
  47. const TSIGRecord test_record;
  48. OutputBuffer buffer;
  49. MessageRenderer renderer;
  50. vector<unsigned char> data;
  51. };
  52. TEST_F(TSIGRecordTest, getName) {
  53. EXPECT_EQ(test_name, test_record.getName());
  54. }
  55. TEST_F(TSIGRecordTest, getLength) {
  56. // 85 = 17 + 26 + 16 + 26
  57. // len(www.example.com) = 17
  58. // len(hmac-md5.sig-alg.reg.int) = 26
  59. // len(MAC) = 16
  60. // the rest are fixed length fields (26 in total)
  61. EXPECT_EQ(85, test_record.getLength());
  62. }
  63. TEST_F(TSIGRecordTest, fromParams) {
  64. // Construct the same TSIG RR as test_record from parameters.
  65. // See the getLength test for the magic number of 85 (although it
  66. // actually doesn't matter)
  67. const TSIGRecord record(test_name, TSIGRecord::getClass(),
  68. TSIGRecord::getTTL(), test_rdata, 85);
  69. // Perform straight sanity checks
  70. EXPECT_EQ(test_name, record.getName());
  71. EXPECT_EQ(85, record.getLength());
  72. EXPECT_EQ(0, test_rdata.compare(record.getRdata()));
  73. // The constructor doesn't check the length...
  74. EXPECT_NO_THROW(TSIGRecord(test_name, TSIGRecord::getClass(),
  75. TSIGRecord::getTTL(), test_rdata, 82));
  76. // ...even for impossibly small values...
  77. EXPECT_NO_THROW(TSIGRecord(test_name, TSIGRecord::getClass(),
  78. TSIGRecord::getTTL(), test_rdata, 1));
  79. // ...or too large values.
  80. EXPECT_NO_THROW(TSIGRecord(test_name, TSIGRecord::getClass(),
  81. TSIGRecord::getTTL(), test_rdata, 65536));
  82. // RDATA must indeed be TSIG
  83. EXPECT_THROW(TSIGRecord(test_name, TSIGRecord::getClass(),
  84. TSIGRecord::getTTL(), in::A("192.0.2.1"), 85),
  85. DNSMessageFORMERR);
  86. // Unexpected class
  87. EXPECT_THROW(TSIGRecord(test_name, RRClass::IN(), TSIGRecord::getTTL(),
  88. test_rdata, 85),
  89. DNSMessageFORMERR);
  90. // Unexpected TTL (simply ignored)
  91. EXPECT_NO_THROW(TSIGRecord(test_name, TSIGRecord::getClass(),
  92. RRTTL(3600), test_rdata, 85));
  93. >>>>>>> trac813
  94. }
  95. TEST_F(TSIGRecordTest, recordToWire) {
  96. UnitTestUtil::readWireData("tsigrecord_toWire1.wire", data);
  97. EXPECT_EQ(1, test_record.toWire(renderer));
  98. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  99. renderer.getData(), renderer.getLength(),
  100. &data[0], data.size());
  101. // Same test for a dumb buffer
  102. buffer.clear();
  103. EXPECT_EQ(1, test_record.toWire(buffer));
  104. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  105. buffer.getData(), buffer.getLength(),
  106. &data[0], data.size());
  107. }
  108. TEST_F(TSIGRecordTest, recordToOLongToWire) {
  109. // By setting the limit to "record length - 1", it will fail, and the
  110. // renderer will be marked as "truncated".
  111. renderer.setLengthLimit(test_record.getLength() - 1);
  112. EXPECT_FALSE(renderer.isTruncated()); // not marked before render attempt
  113. EXPECT_EQ(0, test_record.toWire(renderer));
  114. EXPECT_TRUE(renderer.isTruncated());
  115. }
  116. TEST_F(TSIGRecordTest, recordToWireAfterNames) {
  117. // A similar test but the TSIG RR follows some domain names that could
  118. // cause name compression inside TSIG. Our implementation shouldn't
  119. // compress either owner (key) name or the algorithm name. This test
  120. // confirms that.
  121. UnitTestUtil::readWireData("tsigrecord_toWire2.wire", data);
  122. renderer.writeName(TSIGKey::HMACMD5_NAME());
  123. renderer.writeName(Name("foo.example.com"));
  124. EXPECT_EQ(1, test_record.toWire(renderer));
  125. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  126. renderer.getData(), renderer.getLength(),
  127. &data[0], data.size());
  128. }
  129. TEST_F(TSIGRecordTest, toText) {
  130. EXPECT_EQ("www.example.com. 0 ANY TSIG hmac-md5.sig-alg.reg.int. "
  131. "1302890362 300 16 2tra2tra2tra2tra2tra2g== 11621 NOERROR 0\n",
  132. test_record.toText());
  133. }
  134. // test operator<<. We simply confirm it appends the result of toText().
  135. TEST_F(TSIGRecordTest, LeftShiftOperator) {
  136. ostringstream oss;
  137. oss << test_record;
  138. EXPECT_EQ(test_record.toText(), oss.str());
  139. }
  140. } // end namespace