rrset_unittest.cc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // Copyright (C) 2010 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/messagerenderer.h>
  16. #include <dns/name.h>
  17. #include <dns/rdata.h>
  18. #include <dns/rdataclass.h>
  19. #include <dns/rrclass.h>
  20. #include <dns/rrtype.h>
  21. #include <dns/rrttl.h>
  22. #include <dns/rrset.h>
  23. #include <dns/tests/unittest_util.h>
  24. #include <gtest/gtest.h>
  25. #include <stdexcept>
  26. #include <sstream>
  27. using isc::UnitTestUtil;
  28. using namespace std;
  29. using namespace isc::dns;
  30. using namespace isc::util;
  31. using namespace isc::dns::rdata;
  32. namespace {
  33. class RRsetTest : public ::testing::Test {
  34. protected:
  35. RRsetTest() : buffer(0),
  36. test_name("test.example.com"),
  37. test_domain("example.com"),
  38. test_nsname("ns.example.com"),
  39. rrset_a(test_name, RRClass::IN(), RRType::A(), RRTTL(3600)),
  40. rrset_a_empty(test_name, RRClass::IN(), RRType::A(),
  41. RRTTL(3600)),
  42. rrset_ns(test_domain, RRClass::IN(), RRType::NS(),
  43. RRTTL(86400)),
  44. rrset_ch_txt(test_domain, RRClass::CH(), RRType::TXT(),
  45. RRTTL(0))
  46. {
  47. rrset_a.addRdata(in::A("192.0.2.1"));
  48. rrset_a.addRdata(in::A("192.0.2.2"));
  49. }
  50. OutputBuffer buffer;
  51. MessageRenderer renderer;
  52. Name test_name;
  53. Name test_domain;
  54. Name test_nsname;
  55. RRset rrset_a;
  56. RRset rrset_a_empty;
  57. RRset rrset_ns;
  58. RRset rrset_ch_txt;
  59. std::vector<unsigned char> wiredata;
  60. // max number of Rdata objects added to a test RRset object.
  61. // this is an arbitrary chosen limit, but should be sufficiently large
  62. // in practice and reasonable even as an extreme test case.
  63. static const int MAX_RDATA_COUNT = 100;
  64. };
  65. TEST_F(RRsetTest, getRdataCount) {
  66. for (int i = 0; i < MAX_RDATA_COUNT; ++i) {
  67. EXPECT_EQ(i, rrset_a_empty.getRdataCount());
  68. rrset_a_empty.addRdata(in::A("192.0.2.1"));
  69. }
  70. }
  71. TEST_F(RRsetTest, getName) {
  72. EXPECT_EQ(test_name, rrset_a.getName());
  73. EXPECT_EQ(test_domain, rrset_ns.getName());
  74. }
  75. TEST_F(RRsetTest, getClass) {
  76. EXPECT_EQ(RRClass("IN"), rrset_a.getClass());
  77. EXPECT_EQ(RRClass("CH"), rrset_ch_txt.getClass());
  78. }
  79. TEST_F(RRsetTest, getType) {
  80. EXPECT_EQ(RRType("A"), rrset_a.getType());
  81. EXPECT_EQ(RRType("NS"), rrset_ns.getType());
  82. EXPECT_EQ(RRType("TXT"), rrset_ch_txt.getType());
  83. }
  84. TEST_F(RRsetTest, getTTL) {
  85. EXPECT_EQ(RRTTL(3600), rrset_a.getTTL());
  86. EXPECT_EQ(RRTTL(86400), rrset_ns.getTTL());
  87. EXPECT_EQ(RRTTL(0), rrset_ch_txt.getTTL());
  88. }
  89. TEST_F(RRsetTest, setTTL) {
  90. rrset_a.setTTL(RRTTL(86400));
  91. EXPECT_EQ(RRTTL(86400), rrset_a.getTTL());
  92. rrset_a.setTTL(RRTTL(0));
  93. EXPECT_EQ(RRTTL(0), rrset_a.getTTL());
  94. }
  95. TEST_F(RRsetTest, setName) {
  96. rrset_a.setName(test_nsname);
  97. EXPECT_EQ(test_nsname, rrset_a.getName());
  98. }
  99. TEST_F(RRsetTest, isSameKind) {
  100. RRset rrset_w(test_name, RRClass::IN(), RRType::A(), RRTTL(3600));
  101. RRset rrset_x(test_name, RRClass::IN(), RRType::A(), RRTTL(3600));
  102. RRset rrset_y(test_name, RRClass::IN(), RRType::NS(), RRTTL(3600));
  103. RRset rrset_z(test_name, RRClass::CH(), RRType::A(), RRTTL(3600));
  104. RRset rrset_p(test_nsname, RRClass::IN(), RRType::A(), RRTTL(3600));
  105. EXPECT_TRUE(rrset_w.isSameKind(rrset_w));
  106. EXPECT_TRUE(rrset_w.isSameKind(rrset_x));
  107. EXPECT_FALSE(rrset_w.isSameKind(rrset_y));
  108. EXPECT_FALSE(rrset_w.isSameKind(rrset_z));
  109. EXPECT_FALSE(rrset_w.isSameKind(rrset_p));
  110. }
  111. void
  112. addRdataTestCommon(const RRset& rrset) {
  113. EXPECT_EQ(2, rrset.getRdataCount());
  114. RdataIteratorPtr it = rrset.getRdataIterator(); // cursor is set to the 1st
  115. EXPECT_FALSE(it->isLast());
  116. EXPECT_EQ(0, it->getCurrent().compare(in::A("192.0.2.1")));
  117. it->next();
  118. EXPECT_FALSE(it->isLast());
  119. EXPECT_EQ(0, it->getCurrent().compare(in::A("192.0.2.2")));
  120. it->next();
  121. EXPECT_TRUE(it->isLast());
  122. }
  123. TEST_F(RRsetTest, addRdata) {
  124. addRdataTestCommon(rrset_a);
  125. // Reference version of addRdata() doesn't allow to add a different
  126. // type of Rdata.
  127. EXPECT_THROW(rrset_a.addRdata(generic::NS(test_nsname)), std::bad_cast);
  128. }
  129. TEST_F(RRsetTest, addRdataPtr) {
  130. rrset_a_empty.addRdata(createRdata(rrset_a_empty.getType(),
  131. rrset_a_empty.getClass(),
  132. "192.0.2.1"));
  133. rrset_a_empty.addRdata(createRdata(rrset_a_empty.getType(),
  134. rrset_a_empty.getClass(),
  135. "192.0.2.2"));
  136. addRdataTestCommon(rrset_a);
  137. // Pointer version of addRdata() doesn't type check and does allow to
  138. //add a different type of Rdata as a result.
  139. rrset_a_empty.addRdata(createRdata(RRType::NS(), RRClass::IN(),
  140. "ns.example.com"));
  141. EXPECT_EQ(3, rrset_a_empty.getRdataCount());
  142. }
  143. TEST_F(RRsetTest, iterator) {
  144. // Iterator for an empty RRset.
  145. RdataIteratorPtr it = rrset_a_empty.getRdataIterator();
  146. EXPECT_TRUE(it->isLast());
  147. // Normal case (already tested, but do it again just in case)
  148. rrset_a_empty.addRdata(in::A("192.0.2.1"));
  149. rrset_a_empty.addRdata(in::A("192.0.2.2"));
  150. addRdataTestCommon(rrset_a_empty);
  151. // Rewind test: should be repeat the iteration by calling first().
  152. for (int i = 0; i < 2; ++i) {
  153. it = rrset_a_empty.getRdataIterator();
  154. it->first();
  155. EXPECT_FALSE(it->isLast());
  156. it->next();
  157. EXPECT_FALSE(it->isLast());
  158. it->next();
  159. EXPECT_TRUE(it->isLast());
  160. }
  161. }
  162. TEST_F(RRsetTest, toText) {
  163. EXPECT_EQ("test.example.com. 3600 IN A 192.0.2.1\n"
  164. "test.example.com. 3600 IN A 192.0.2.2\n",
  165. rrset_a.toText());
  166. // toText() cannot be performed for an empty RRset.
  167. EXPECT_THROW(rrset_a_empty.toText(), EmptyRRset);
  168. }
  169. TEST_F(RRsetTest, toWireBuffer) {
  170. rrset_a.toWire(buffer);
  171. UnitTestUtil::readWireData("rrset_toWire1", wiredata);
  172. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData, buffer.getData(),
  173. buffer.getLength(), &wiredata[0], wiredata.size());
  174. // toWire() cannot be performed for an empty RRset.
  175. buffer.clear();
  176. EXPECT_THROW(rrset_a_empty.toWire(buffer), EmptyRRset);
  177. }
  178. TEST_F(RRsetTest, toWireRenderer) {
  179. rrset_ns.addRdata(generic::NS(test_nsname));
  180. rrset_a.toWire(renderer);
  181. rrset_ns.toWire(renderer);
  182. UnitTestUtil::readWireData("rrset_toWire2", wiredata);
  183. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData, renderer.getData(),
  184. renderer.getLength(), &wiredata[0], wiredata.size());
  185. // toWire() cannot be performed for an empty RRset.
  186. renderer.clear();
  187. EXPECT_THROW(rrset_a_empty.toWire(renderer), EmptyRRset);
  188. }
  189. // test operator<<. We simply confirm it appends the result of toText().
  190. TEST_F(RRsetTest, LeftShiftOperator) {
  191. ostringstream oss;
  192. oss << rrset_a;
  193. EXPECT_EQ(rrset_a.toText(), oss.str());
  194. }
  195. }