rbnode_rrset_unittest.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright (C) 2012 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 <stdexcept>
  15. #include <exceptions/exceptions.h>
  16. #include <dns/rdataclass.h>
  17. #include <datasrc/rbnode_rrset.h>
  18. #include <gtest/gtest.h>
  19. #include <dns/tests/unittest_util.h>
  20. using isc::UnitTestUtil;
  21. using namespace std;
  22. using namespace isc;
  23. using namespace isc::datasrc;
  24. using namespace isc::dns;
  25. using namespace isc::dns::rdata;
  26. using namespace isc::util;
  27. // These tests are very similar to those for RRset - indeed, the files was
  28. // created from those tests. However, the significant difference in behaviour
  29. // between RRset and RBNodeRRset - that the "set" methods in the latter mostly
  30. // result in exceptions being thrown - preclude use of full type
  31. // parameterisation of the tests.
  32. namespace {
  33. class RBNodeRRsetTest : public ::testing::Test {
  34. protected:
  35. RBNodeRRsetTest() :
  36. buffer(0), renderer(buffer),
  37. test_name("test.example.com"),
  38. test_domain("example.com"),
  39. test_nsname("ns.example.com"),
  40. rrset_a(ConstRRsetPtr(new RRset(
  41. test_name, RRClass::IN(), RRType::A(), RRTTL(3600)))),
  42. rrset_a_empty(ConstRRsetPtr(new RRset(
  43. test_name, RRClass::IN(), RRType::A(), RRTTL(3600)))),
  44. rrset_ns(ConstRRsetPtr(new RRset(
  45. test_domain, RRClass::IN(), RRType::NS(), RRTTL(86400)))),
  46. rrset_ch_txt(ConstRRsetPtr(new RRset(
  47. test_domain, RRClass::CH(), RRType::TXT(), RRTTL(0))))
  48. {
  49. // Add a couple of Rdata elements to an RRset. The easiest way to
  50. // do this is to override the "const" restrictions. As this is a test,
  51. // we don't feel too bad about doing so.
  52. AbstractRRset* rrset =
  53. const_cast<AbstractRRset*>(rrset_a.getUnderlyingRRset().get());
  54. rrset->addRdata(in::A("192.0.2.1"));
  55. rrset->addRdata(in::A("192.0.2.2"));
  56. }
  57. OutputBuffer buffer;
  58. MessageRenderer renderer;
  59. Name test_name;
  60. Name test_domain;
  61. Name test_nsname;
  62. RBNodeRRset rrset_a;
  63. RBNodeRRset rrset_a_empty;
  64. RBNodeRRset rrset_ns;
  65. RBNodeRRset rrset_ch_txt;
  66. std::vector<unsigned char> wiredata;
  67. // max number of Rdata objects added to a test RRset object.
  68. // this is an arbitrary chosen limit, but should be sufficiently large
  69. // in practice and reasonable even as an extreme test case.
  70. static const int MAX_RDATA_COUNT = 100;
  71. };
  72. TEST_F(RBNodeRRsetTest, getRdataCount) {
  73. EXPECT_EQ(0, rrset_a_empty.getRdataCount());
  74. EXPECT_EQ(2, rrset_a.getRdataCount());
  75. }
  76. TEST_F(RBNodeRRsetTest, getName) {
  77. EXPECT_EQ(test_name, rrset_a.getName());
  78. EXPECT_EQ(test_domain, rrset_ns.getName());
  79. }
  80. TEST_F(RBNodeRRsetTest, getClass) {
  81. EXPECT_EQ(RRClass("IN"), rrset_a.getClass());
  82. EXPECT_EQ(RRClass("CH"), rrset_ch_txt.getClass());
  83. }
  84. TEST_F(RBNodeRRsetTest, getType) {
  85. EXPECT_EQ(RRType("A"), rrset_a.getType());
  86. EXPECT_EQ(RRType("NS"), rrset_ns.getType());
  87. EXPECT_EQ(RRType("TXT"), rrset_ch_txt.getType());
  88. }
  89. TEST_F(RBNodeRRsetTest, getTTL) {
  90. EXPECT_EQ(RRTTL(3600), rrset_a.getTTL());
  91. EXPECT_EQ(RRTTL(86400), rrset_ns.getTTL());
  92. EXPECT_EQ(RRTTL(0), rrset_ch_txt.getTTL());
  93. }
  94. TEST_F(RBNodeRRsetTest, setName) {
  95. EXPECT_THROW(rrset_a.setName(test_nsname), NotImplemented);
  96. }
  97. TEST_F(RBNodeRRsetTest, setTTL) {
  98. EXPECT_THROW(rrset_a.setTTL(RRTTL(86400)), NotImplemented);
  99. }
  100. TEST_F(RBNodeRRsetTest, toText) {
  101. EXPECT_EQ("test.example.com. 3600 IN A 192.0.2.1\n"
  102. "test.example.com. 3600 IN A 192.0.2.2\n",
  103. rrset_a.toText());
  104. // toText() cannot be performed for an empty RRset.
  105. EXPECT_THROW(rrset_a_empty.toText(), EmptyRRset);
  106. }
  107. TEST_F(RBNodeRRsetTest, toWireRenderer) {
  108. rrset_ns.addRdata(generic::NS(test_nsname));
  109. rrset_a.toWire(renderer);
  110. rrset_ns.toWire(renderer);
  111. UnitTestUtil::readWireData("rrset_toWire2", wiredata);
  112. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData, buffer.getData(),
  113. buffer.getLength(), &wiredata[0], wiredata.size());
  114. // toWire() cannot be performed for an empty RRset.
  115. renderer.clear();
  116. EXPECT_THROW(rrset_a_empty.toWire(renderer), EmptyRRset);
  117. }
  118. TEST_F(RBNodeRRsetTest, toWireBuffer) {
  119. rrset_a.toWire(buffer);
  120. UnitTestUtil::readWireData("rrset_toWire1", wiredata);
  121. EXPECT_PRED_FORMAT4(UnitTestUtil::matchWireData, buffer.getData(),
  122. buffer.getLength(), &wiredata[0], wiredata.size());
  123. // toWire() cannot be performed for an empty RRset.
  124. buffer.clear();
  125. EXPECT_THROW(rrset_a_empty.toWire(buffer), EmptyRRset);
  126. }
  127. TEST_F(RBNodeRRsetTest, addRdata) {
  128. EXPECT_THROW(rrset_a.addRdata(in::A("192.0.2.3")), NotImplemented);
  129. // Check the same goes for tryin g to add the wrong type of data
  130. EXPECT_THROW(rrset_a.addRdata(generic::NS(test_nsname)), NotImplemented);
  131. }
  132. TEST_F(RBNodeRRsetTest, addRdataPtr) {
  133. EXPECT_THROW(rrset_a_empty.addRdata(createRdata(rrset_a_empty.getType(),
  134. rrset_a_empty.getClass(),
  135. "192.0.2.1")),
  136. NotImplemented);
  137. }
  138. TEST_F(RBNodeRRsetTest, getRDataIterator) {
  139. RdataIteratorPtr it = rrset_a.getRdataIterator();
  140. for (int i = 0; i < 2; ++i) {
  141. ASSERT_FALSE(it->isLast());
  142. ASSERT_EQ(0, it->getCurrent().compare(in::A("192.0.2.1")));
  143. it->next();
  144. ASSERT_FALSE(it->isLast());
  145. ASSERT_EQ(0, it->getCurrent().compare(in::A("192.0.2.2")));
  146. it->next();
  147. ASSERT_TRUE(it->isLast());
  148. // Should be able repeat the iteration by calling first().
  149. it->first();
  150. }
  151. }
  152. // test operator<<. We simply confirm it appends the result of toText().
  153. TEST_F(RBNodeRRsetTest, LeftShiftOperator) {
  154. ostringstream oss;
  155. oss << rrset_a;
  156. EXPECT_EQ("test.example.com. 3600 IN A 192.0.2.1\n"
  157. "test.example.com. 3600 IN A 192.0.2.2\n", oss.str());
  158. }
  159. } // Anonymous namespace