rdata_srv_unittest.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. // there's nothing to specialize
  32. };
  33. string srv_txt("1 5 1500 a.example.com.");
  34. string srv_txt2("1 5 1400 example.com.");
  35. string too_long_label("012345678901234567890123456789"
  36. "0123456789012345678901234567890123");
  37. // 1 5 1500 a.example.com.
  38. const uint8_t wiredata_srv[] = {
  39. 0x00, 0x01, 0x00, 0x05, 0x05, 0xdc, 0x01, 0x61, 0x07, 0x65, 0x78,
  40. 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x03, 0x63, 0x6f, 0x6d, 0x00};
  41. // 1 5 1400 example.com.
  42. const uint8_t wiredata_srv2[] = {
  43. 0x00, 0x01, 0x00, 0x05, 0x05, 0x78, 0x07, 0x65, 0x78, 0x61, 0x6d,
  44. 0x70, 0x6c, 0x65, 0x03, 0x63, 0x6f, 0x6d, 0x00};
  45. const in::SRV rdata_srv(srv_txt);
  46. const in::SRV rdata_srv2(srv_txt2);
  47. TEST_F(Rdata_SRV_Test, createFromText) {
  48. EXPECT_EQ(1, rdata_srv.getPriority());
  49. EXPECT_EQ(5, rdata_srv.getWeight());
  50. EXPECT_EQ(1500, rdata_srv.getPort());
  51. EXPECT_EQ(Name("a.example.com."), rdata_srv.getTarget());
  52. }
  53. TEST_F(Rdata_SRV_Test, badText) {
  54. // priority is too large (2814...6 is 2^48)
  55. EXPECT_THROW(in::SRV("281474976710656 5 1500 a.example.com."),
  56. InvalidRdataText);
  57. // weight is too large
  58. EXPECT_THROW(in::SRV("1 281474976710656 1500 a.example.com."),
  59. InvalidRdataText);
  60. // port is too large
  61. EXPECT_THROW(in::SRV("1 5 281474976710656 a.example.com."),
  62. InvalidRdataText);
  63. // incomplete text
  64. EXPECT_THROW(in::SRV("1 5 a.example.com."),
  65. InvalidRdataText);
  66. EXPECT_THROW(in::SRV("1 5 1500a.example.com."),
  67. InvalidRdataText);
  68. // bad name
  69. EXPECT_THROW(in::SRV("1 5 1500 a.example.com." + too_long_label),
  70. TooLongLabel);
  71. }
  72. TEST_F(Rdata_SRV_Test, assignment) {
  73. in::SRV copy((string(srv_txt2)));
  74. copy = rdata_srv;
  75. EXPECT_EQ(0, copy.compare(rdata_srv));
  76. // Check if the copied data is valid even after the original is deleted
  77. in::SRV* copy2 = new in::SRV(rdata_srv);
  78. in::SRV copy3((string(srv_txt2)));
  79. copy3 = *copy2;
  80. delete copy2;
  81. EXPECT_EQ(0, copy3.compare(rdata_srv));
  82. // Self assignment
  83. copy = copy;
  84. EXPECT_EQ(0, copy.compare(rdata_srv));
  85. }
  86. TEST_F(Rdata_SRV_Test, createFromWire) {
  87. EXPECT_EQ(0, rdata_srv.compare(
  88. *rdataFactoryFromFile(RRType("SRV"), RRClass("IN"),
  89. "rdata_srv_fromWire")));
  90. // RDLENGTH is too short
  91. EXPECT_THROW(rdataFactoryFromFile(RRType("SRV"), RRClass("IN"),
  92. "rdata_srv_fromWire", 23),
  93. InvalidRdataLength);
  94. // RDLENGTH is too long
  95. EXPECT_THROW(rdataFactoryFromFile(RRType("SRV"), RRClass("IN"),
  96. "rdata_srv_fromWire", 46),
  97. InvalidRdataLength);
  98. // incomplete name. the error should be detected in the name constructor
  99. EXPECT_THROW(rdataFactoryFromFile(RRType("SRV"), RRClass("IN"),
  100. "rdata_cname_fromWire", 69),
  101. DNSMessageFORMERR);
  102. // parse compressed target name
  103. EXPECT_EQ(0, rdata_srv.compare(
  104. *rdataFactoryFromFile(RRType("SRV"), RRClass("IN"),
  105. "rdata_srv_fromWire", 89)));
  106. }
  107. TEST_F(Rdata_SRV_Test, createFromLexer) {
  108. EXPECT_EQ(0, rdata_srv.compare(
  109. *test::createRdataUsingLexer(RRType::SRV(), RRClass::IN(),
  110. "1 5 1500 a.example.com.")));
  111. // Exceptions cause NULL to be returned.
  112. EXPECT_FALSE(test::createRdataUsingLexer(RRType::SRV(), RRClass::IN(),
  113. "1 5 281474976710656 "
  114. "a.example.com."));
  115. }
  116. TEST_F(Rdata_SRV_Test, toWireBuffer) {
  117. rdata_srv.toWire(obuffer);
  118. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  119. obuffer.getData(), obuffer.getLength(),
  120. wiredata_srv, sizeof(wiredata_srv));
  121. obuffer.clear();
  122. rdata_srv2.toWire(obuffer);
  123. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  124. obuffer.getData(), obuffer.getLength(),
  125. wiredata_srv2, sizeof(wiredata_srv2));
  126. }
  127. TEST_F(Rdata_SRV_Test, toWireRenderer) {
  128. rdata_srv.toWire(renderer);
  129. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  130. renderer.getData(), renderer.getLength(),
  131. wiredata_srv, sizeof(wiredata_srv));
  132. renderer.clear();
  133. rdata_srv2.toWire(renderer);
  134. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  135. renderer.getData(), renderer.getLength(),
  136. wiredata_srv2, sizeof(wiredata_srv2));
  137. }
  138. TEST_F(Rdata_SRV_Test, toText) {
  139. EXPECT_EQ(srv_txt, rdata_srv.toText());
  140. EXPECT_EQ(srv_txt2, rdata_srv2.toText());
  141. }
  142. TEST_F(Rdata_SRV_Test, compare) {
  143. // test RDATAs, sorted in the ascendent order.
  144. vector<in::SRV> compare_set;
  145. compare_set.push_back(in::SRV("1 5 1500 a.example.com."));
  146. compare_set.push_back(in::SRV("2 5 1500 a.example.com."));
  147. compare_set.push_back(in::SRV("2 6 1500 a.example.com."));
  148. compare_set.push_back(in::SRV("2 6 1600 a.example.com."));
  149. compare_set.push_back(in::SRV("2 6 1600 example.com."));
  150. EXPECT_EQ(0, compare_set[0].compare(
  151. in::SRV("1 5 1500 a.example.com.")));
  152. vector<in::SRV>::const_iterator it;
  153. vector<in::SRV>::const_iterator it_end = compare_set.end();
  154. for (it = compare_set.begin(); it != it_end - 1; ++it) {
  155. EXPECT_GT(0, (*it).compare(*(it + 1)));
  156. EXPECT_LT(0, (*(it + 1)).compare(*it));
  157. }
  158. // comparison attempt between incompatible RR types should be rejected
  159. EXPECT_THROW(rdata_srv.compare(*RdataTest::rdata_nomatch), bad_cast);
  160. }
  161. }