rdata_afsdb_unittest.cc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // Copyright (C) 2011-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 <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. const char* const afsdb_text = "1 afsdb.example.com.";
  30. const char* const afsdb_text2 = "0 root.example.com.";
  31. const char* const too_long_label("012345678901234567890123456789"
  32. "0123456789012345678901234567890123.");
  33. namespace {
  34. class Rdata_AFSDB_Test : public RdataTest {
  35. protected:
  36. Rdata_AFSDB_Test() :
  37. rdata_afsdb(string(afsdb_text)), rdata_afsdb2(string(afsdb_text2))
  38. {}
  39. const generic::AFSDB rdata_afsdb;
  40. const generic::AFSDB rdata_afsdb2;
  41. vector<uint8_t> expected_wire;
  42. };
  43. TEST_F(Rdata_AFSDB_Test, createFromText) {
  44. EXPECT_EQ(1, rdata_afsdb.getSubtype());
  45. EXPECT_EQ(Name("afsdb.example.com."), rdata_afsdb.getServer());
  46. EXPECT_EQ(0, rdata_afsdb2.getSubtype());
  47. EXPECT_EQ(Name("root.example.com."), rdata_afsdb2.getServer());
  48. }
  49. TEST_F(Rdata_AFSDB_Test, badText) {
  50. // subtype is too large
  51. EXPECT_THROW(const generic::AFSDB rdata_afsdb("99999999 afsdb.example.com."),
  52. InvalidRdataText);
  53. // incomplete text
  54. EXPECT_THROW(const generic::AFSDB rdata_afsdb("10"), InvalidRdataText);
  55. EXPECT_THROW(const generic::AFSDB rdata_afsdb("SPOON"), InvalidRdataText);
  56. EXPECT_THROW(const generic::AFSDB rdata_afsdb("1root.example.com."), InvalidRdataText);
  57. // number of fields (must be 2) is incorrect
  58. EXPECT_THROW(const generic::AFSDB rdata_afsdb("10 afsdb. example.com."),
  59. InvalidRdataText);
  60. // No origin and relative
  61. EXPECT_THROW(const generic::AFSDB rdata_afsdb("1 afsdb.example.com"),
  62. MissingNameOrigin);
  63. // bad name
  64. EXPECT_THROW(const generic::AFSDB rdata_afsdb("1 afsdb.example.com." +
  65. string(too_long_label)), TooLongLabel);
  66. }
  67. TEST_F(Rdata_AFSDB_Test, copy) {
  68. const generic::AFSDB rdata_afsdb2(rdata_afsdb);
  69. EXPECT_EQ(0, rdata_afsdb.compare(rdata_afsdb2));
  70. }
  71. TEST_F(Rdata_AFSDB_Test, assignment) {
  72. generic::AFSDB copy((string(afsdb_text2)));
  73. copy = rdata_afsdb;
  74. EXPECT_EQ(0, copy.compare(rdata_afsdb));
  75. // Check if the copied data is valid even after the original is deleted
  76. generic::AFSDB* copy2 = new generic::AFSDB(rdata_afsdb);
  77. generic::AFSDB copy3((string(afsdb_text2)));
  78. copy3 = *copy2;
  79. delete copy2;
  80. EXPECT_EQ(0, copy3.compare(rdata_afsdb));
  81. // Self assignment
  82. copy = copy;
  83. EXPECT_EQ(0, copy.compare(rdata_afsdb));
  84. }
  85. TEST_F(Rdata_AFSDB_Test, createFromWire) {
  86. // uncompressed names
  87. EXPECT_EQ(0, rdata_afsdb.compare(
  88. *rdataFactoryFromFile(RRType::AFSDB(), RRClass::IN(),
  89. "rdata_afsdb_fromWire1.wire")));
  90. // compressed name
  91. EXPECT_EQ(0, rdata_afsdb.compare(
  92. *rdataFactoryFromFile(RRType::AFSDB(), RRClass::IN(),
  93. "rdata_afsdb_fromWire2.wire", 13)));
  94. // RDLENGTH is too short
  95. EXPECT_THROW(rdataFactoryFromFile(RRType::AFSDB(), RRClass::IN(),
  96. "rdata_afsdb_fromWire3.wire"),
  97. InvalidRdataLength);
  98. // RDLENGTH is too long
  99. EXPECT_THROW(rdataFactoryFromFile(RRType::AFSDB(), RRClass::IN(),
  100. "rdata_afsdb_fromWire4.wire"),
  101. InvalidRdataLength);
  102. // bogus server name, the error should be detected in the name
  103. // constructor
  104. EXPECT_THROW(rdataFactoryFromFile(RRType::AFSDB(), RRClass::IN(),
  105. "rdata_afsdb_fromWire5.wire"),
  106. DNSMessageFORMERR);
  107. }
  108. TEST_F(Rdata_AFSDB_Test, createFromLexer) {
  109. EXPECT_EQ(0, rdata_afsdb.compare(
  110. *test::createRdataUsingLexer(RRType::AFSDB(), RRClass::IN(),
  111. afsdb_text)));
  112. // test::createRdataUsingLexer() constructs relative to
  113. // "example.org." origin.
  114. generic::AFSDB tmp = generic::AFSDB("1 afsdb2.example.org.");
  115. EXPECT_EQ(0, tmp.compare(
  116. *test::createRdataUsingLexer(RRType::AFSDB(), RRClass::IN(),
  117. "1 afsdb2")));
  118. // Exceptions cause NULL to be returned.
  119. EXPECT_FALSE(test::createRdataUsingLexer(RRType::AFSDB(), RRClass::IN(),
  120. "1root.example.com."));
  121. // 65536 is larger than maximum possible subtype
  122. EXPECT_FALSE(test::createRdataUsingLexer(RRType::AFSDB(), RRClass::IN(),
  123. "65536 afsdb.example.com."));
  124. // Extra text at end of line
  125. EXPECT_FALSE(test::createRdataUsingLexer(RRType::AFSDB(), RRClass::IN(),
  126. "1 afsdb.example.com. extra."));
  127. }
  128. TEST_F(Rdata_AFSDB_Test, toWireBuffer) {
  129. // construct actual data
  130. rdata_afsdb.toWire(obuffer);
  131. // construct expected data
  132. UnitTestUtil::readWireData("rdata_afsdb_toWire1.wire", expected_wire);
  133. // then compare them
  134. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  135. obuffer.getData(), obuffer.getLength(),
  136. &expected_wire[0], expected_wire.size());
  137. // clear buffer for the next test
  138. obuffer.clear();
  139. // construct actual data
  140. Name("example.com.").toWire(obuffer);
  141. rdata_afsdb2.toWire(obuffer);
  142. // construct expected data
  143. UnitTestUtil::readWireData("rdata_afsdb_toWire2.wire", expected_wire);
  144. // then compare them
  145. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  146. obuffer.getData(), obuffer.getLength(),
  147. &expected_wire[0], expected_wire.size());
  148. }
  149. TEST_F(Rdata_AFSDB_Test, toWireRenderer) {
  150. // similar to toWireBuffer, but names in RDATA could be compressed due to
  151. // preceding names. Actually they must not be compressed according to
  152. // RFC3597, and this test checks that.
  153. // construct actual data
  154. rdata_afsdb.toWire(renderer);
  155. // construct expected data
  156. UnitTestUtil::readWireData("rdata_afsdb_toWire1.wire", expected_wire);
  157. // then compare them
  158. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  159. renderer.getData(), renderer.getLength(),
  160. &expected_wire[0], expected_wire.size());
  161. // clear renderer for the next test
  162. renderer.clear();
  163. // construct actual data
  164. renderer.writeName(Name("example.com."));
  165. rdata_afsdb2.toWire(renderer);
  166. // construct expected data
  167. UnitTestUtil::readWireData("rdata_afsdb_toWire2.wire", expected_wire);
  168. // then compare them
  169. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData,
  170. renderer.getData(), renderer.getLength(),
  171. &expected_wire[0], expected_wire.size());
  172. }
  173. TEST_F(Rdata_AFSDB_Test, toText) {
  174. EXPECT_EQ(afsdb_text, rdata_afsdb.toText());
  175. EXPECT_EQ(afsdb_text2, rdata_afsdb2.toText());
  176. }
  177. TEST_F(Rdata_AFSDB_Test, compare) {
  178. // check reflexivity
  179. EXPECT_EQ(0, rdata_afsdb.compare(rdata_afsdb));
  180. // name must be compared in case-insensitive manner
  181. EXPECT_EQ(0, rdata_afsdb.compare(generic::AFSDB("1 "
  182. "AFSDB.example.com.")));
  183. const generic::AFSDB small1("10 afsdb.example.com.");
  184. const generic::AFSDB large1("65535 afsdb.example.com.");
  185. const generic::AFSDB large2("256 afsdb.example.com.");
  186. // confirm these are compared as unsigned values
  187. EXPECT_GT(0, rdata_afsdb.compare(large1));
  188. EXPECT_LT(0, large1.compare(rdata_afsdb));
  189. // confirm these are compared in network byte order
  190. EXPECT_GT(0, small1.compare(large2));
  191. EXPECT_LT(0, large2.compare(small1));
  192. // another AFSDB whose server name is larger than that of rdata_afsdb.
  193. const generic::AFSDB large3("256 zzzzz.example.com.");
  194. EXPECT_GT(0, large2.compare(large3));
  195. EXPECT_LT(0, large3.compare(large2));
  196. // comparison attempt between incompatible RR types should be rejected
  197. EXPECT_THROW(rdata_afsdb.compare(*rdata_nomatch), bad_cast);
  198. }
  199. }