rdata_srv_unittest.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for generic
  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 <util/buffer.h>
  15. #include <dns/exceptions.h>
  16. #include <dns/messagerenderer.h>
  17. #include <dns/rdata.h>
  18. #include <dns/rdataclass.h>
  19. #include <dns/rrclass.h>
  20. #include <dns/rrtype.h>
  21. #include <gtest/gtest.h>
  22. #include <dns/tests/unittest_util.h>
  23. #include <dns/tests/rdata_unittest.h>
  24. using isc::UnitTestUtil;
  25. using namespace std;
  26. using namespace isc::dns;
  27. using namespace isc::util;
  28. using namespace isc::dns::rdata;
  29. namespace {
  30. class Rdata_SRV_Test : public RdataTest {
  31. public:
  32. Rdata_SRV_Test() :
  33. srv_txt("1 5 1500 a.example.com."),
  34. srv_txt2("1 5 1400 example.com."),
  35. too_long_label("012345678901234567890123456789"
  36. "0123456789012345678901234567890123."),
  37. rdata_srv(srv_txt),
  38. rdata_srv2(srv_txt2)
  39. {}
  40. const string srv_txt;
  41. const string srv_txt2;
  42. const string too_long_label;
  43. const in::SRV rdata_srv;
  44. const in::SRV rdata_srv2;
  45. };
  46. // 1 5 1500 a.example.com.
  47. const uint8_t wiredata_srv[] = {
  48. 0x00, 0x01, 0x00, 0x05, 0x05, 0xdc, 0x01, 0x61, 0x07, 0x65, 0x78,
  49. 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x03, 0x63, 0x6f, 0x6d, 0x00};
  50. // 1 5 1400 example.com.
  51. const uint8_t wiredata_srv2[] = {
  52. 0x00, 0x01, 0x00, 0x05, 0x05, 0x78, 0x07, 0x65, 0x78, 0x61, 0x6d,
  53. 0x70, 0x6c, 0x65, 0x03, 0x63, 0x6f, 0x6d, 0x00};
  54. TEST_F(Rdata_SRV_Test, createFromText) {
  55. EXPECT_EQ(1, rdata_srv.getPriority());
  56. EXPECT_EQ(5, rdata_srv.getWeight());
  57. EXPECT_EQ(1500, rdata_srv.getPort());
  58. EXPECT_EQ(Name("a.example.com."), rdata_srv.getTarget());
  59. }
  60. TEST_F(Rdata_SRV_Test, badText) {
  61. // priority is too large (2814...6 is 2^48)
  62. EXPECT_THROW(in::SRV("281474976710656 5 1500 a.example.com."),
  63. InvalidRdataText);
  64. // weight is too large
  65. EXPECT_THROW(in::SRV("1 281474976710656 1500 a.example.com."),
  66. InvalidRdataText);
  67. // port is too large
  68. EXPECT_THROW(in::SRV("1 5 281474976710656 a.example.com."),
  69. InvalidRdataText);
  70. // incomplete text
  71. EXPECT_THROW(in::SRV("1 5 a.example.com."),
  72. InvalidRdataText);
  73. EXPECT_THROW(in::SRV("1 5 1500a.example.com."),
  74. InvalidRdataText);
  75. // bad name
  76. EXPECT_THROW(in::SRV("1 5 1500 a.example.com." + too_long_label),
  77. TooLongLabel);
  78. // Extra text at end of line
  79. EXPECT_THROW(in::SRV("1 5 1500 a.example.com. extra."), InvalidRdataText);
  80. }
  81. TEST_F(Rdata_SRV_Test, assignment) {
  82. in::SRV copy((string(srv_txt2)));
  83. copy = rdata_srv;
  84. EXPECT_EQ(0, copy.compare(rdata_srv));
  85. // Check if the copied data is valid even after the original is deleted
  86. in::SRV* copy2 = new in::SRV(rdata_srv);
  87. in::SRV copy3((string(srv_txt2)));
  88. copy3 = *copy2;
  89. delete copy2;
  90. EXPECT_EQ(0, copy3.compare(rdata_srv));
  91. // Self assignment
  92. copy = copy;
  93. EXPECT_EQ(0, copy.compare(rdata_srv));
  94. }
  95. TEST_F(Rdata_SRV_Test, createFromWire) {
  96. EXPECT_EQ(0, rdata_srv.compare(
  97. *rdataFactoryFromFile(RRType("SRV"), RRClass("IN"),
  98. "rdata_srv_fromWire")));
  99. // RDLENGTH is too short
  100. EXPECT_THROW(rdataFactoryFromFile(RRType("SRV"), RRClass("IN"),
  101. "rdata_srv_fromWire", 23),
  102. InvalidRdataLength);
  103. // RDLENGTH is too long
  104. EXPECT_THROW(rdataFactoryFromFile(RRType("SRV"), RRClass("IN"),
  105. "rdata_srv_fromWire", 46),
  106. InvalidRdataLength);
  107. // incomplete name. the error should be detected in the name constructor
  108. EXPECT_THROW(rdataFactoryFromFile(RRType("SRV"), RRClass("IN"),
  109. "rdata_cname_fromWire", 69),
  110. DNSMessageFORMERR);
  111. // parse compressed target name
  112. EXPECT_EQ(0, rdata_srv.compare(
  113. *rdataFactoryFromFile(RRType("SRV"), RRClass("IN"),
  114. "rdata_srv_fromWire", 89)));
  115. }
  116. TEST_F(Rdata_SRV_Test, createFromLexer) {
  117. EXPECT_EQ(0, rdata_srv.compare(
  118. *test::createRdataUsingLexer(RRType::SRV(), RRClass::IN(),
  119. "1 5 1500 a.example.com.")));
  120. // test::createRdataUsingLexer() constructs relative to
  121. // "example.org." origin.
  122. EXPECT_EQ(0, in::SRV("1 5 1500 server16.example.org.").compare(
  123. *test::createRdataUsingLexer(RRType::SRV(), RRClass::IN(),
  124. "1 5 1500 server16")));
  125. // Exceptions cause NULL to be returned.
  126. // Bad priority
  127. EXPECT_FALSE(test::createRdataUsingLexer(RRType::SRV(), RRClass::IN(),
  128. "65536 5 1500 "
  129. "a.example.com."));
  130. // Bad weight
  131. EXPECT_FALSE(test::createRdataUsingLexer(RRType::SRV(), RRClass::IN(),
  132. "1 65536 1500 "
  133. "a.example.com."));
  134. // Bad port
  135. EXPECT_FALSE(test::createRdataUsingLexer(RRType::SRV(), RRClass::IN(),
  136. "1 5 281474976710656 "
  137. "a.example.com."));
  138. // Extra text at end of line
  139. EXPECT_FALSE(test::createRdataUsingLexer(RRType::SRV(), RRClass::IN(),
  140. "1 5 1500 a.example.com. extra."));
  141. }
  142. TEST_F(Rdata_SRV_Test, toWireBuffer) {
  143. rdata_srv.toWire(obuffer);
  144. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  145. obuffer.getData(), obuffer.getLength(),
  146. wiredata_srv, sizeof(wiredata_srv));
  147. obuffer.clear();
  148. rdata_srv2.toWire(obuffer);
  149. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  150. obuffer.getData(), obuffer.getLength(),
  151. wiredata_srv2, sizeof(wiredata_srv2));
  152. }
  153. TEST_F(Rdata_SRV_Test, toWireRenderer) {
  154. rdata_srv.toWire(renderer);
  155. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  156. renderer.getData(), renderer.getLength(),
  157. wiredata_srv, sizeof(wiredata_srv));
  158. renderer.clear();
  159. rdata_srv2.toWire(renderer);
  160. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  161. renderer.getData(), renderer.getLength(),
  162. wiredata_srv2, sizeof(wiredata_srv2));
  163. }
  164. TEST_F(Rdata_SRV_Test, toText) {
  165. EXPECT_EQ(srv_txt, rdata_srv.toText());
  166. EXPECT_EQ(srv_txt2, rdata_srv2.toText());
  167. }
  168. TEST_F(Rdata_SRV_Test, compare) {
  169. // test RDATAs, sorted in the ascendent order.
  170. vector<in::SRV> compare_set;
  171. compare_set.push_back(in::SRV("1 5 1500 a.example.com."));
  172. compare_set.push_back(in::SRV("2 5 1500 a.example.com."));
  173. compare_set.push_back(in::SRV("2 6 1500 a.example.com."));
  174. compare_set.push_back(in::SRV("2 6 1600 a.example.com."));
  175. compare_set.push_back(in::SRV("2 6 1600 example.com."));
  176. EXPECT_EQ(0, compare_set[0].compare(
  177. in::SRV("1 5 1500 a.example.com.")));
  178. vector<in::SRV>::const_iterator it;
  179. vector<in::SRV>::const_iterator it_end = compare_set.end();
  180. for (it = compare_set.begin(); it != it_end - 1; ++it) {
  181. EXPECT_GT(0, (*it).compare(*(it + 1)));
  182. EXPECT_LT(0, (*(it + 1)).compare(*it));
  183. }
  184. // comparison attempt between incompatible RR types should be rejected
  185. EXPECT_THROW(rdata_srv.compare(*RdataTest::rdata_nomatch), bad_cast);
  186. }
  187. }