rrsetlist_unittest.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. // $Id: rrtype_unittest.cc 476 2010-01-19 00:29:28Z jinmei $
  15. #include <vector>
  16. #include <boost/foreach.hpp>
  17. #include "rdata.h"
  18. #include "rdataclass.h"
  19. #include "rrclass.h"
  20. #include "rrtype.h"
  21. #include "rrsetlist.h"
  22. #include "rrset.h"
  23. #include "rrttl.h"
  24. #include <gtest/gtest.h>
  25. #include "unittest_util.h"
  26. using isc::UnitTestUtil;
  27. using namespace std;
  28. using namespace isc::dns;
  29. using namespace isc::dns::rdata;
  30. namespace {
  31. class RRsetListTest : public ::testing::Test {
  32. protected:
  33. RRsetListTest() {}
  34. void setupList(RRsetList& list);
  35. static const in::A rdata_in_a;
  36. static const in::AAAA rdata_in_aaaa;
  37. static const generic::NS rdata_ns;
  38. static const generic::SOA rdata_soa;
  39. static const generic::CNAME rdata_cname;
  40. };
  41. const in::A RRsetListTest::rdata_in_a("192.0.2.1");
  42. const in::AAAA RRsetListTest::rdata_in_aaaa("2001:db8::1234");
  43. const generic::NS RRsetListTest::rdata_ns("ns.example.com");
  44. const generic::SOA RRsetListTest::rdata_soa(Name("ns.example.com"),
  45. Name("root.example.com"),
  46. 2010012601, 3600, 300,
  47. 3600000, 1200);
  48. const generic::CNAME RRsetListTest::rdata_cname("target.example.com");
  49. void
  50. RRsetListTest::setupList(RRsetList& list) {
  51. RRsetPtr a(new RRset(Name("example.com"), RRClass::IN(),
  52. RRType::A(), RRTTL(3600)));
  53. RRsetPtr aaaa(new RRset(Name("example.com"), RRClass::IN(),
  54. RRType::AAAA(), RRTTL(3600)));
  55. RRsetPtr ns(new RRset(Name("example.com"), RRClass::IN(),
  56. RRType::NS(), RRTTL(3600)));
  57. RRsetPtr soa(new RRset(Name("example.com"), RRClass::IN(),
  58. RRType::SOA(), RRTTL(3600)));
  59. RRsetPtr cname(new RRset(Name("example.com"), RRClass::IN(),
  60. RRType::CNAME(), RRTTL(3600)));
  61. a->addRdata(rdata_in_a);
  62. aaaa->addRdata(rdata_in_aaaa);
  63. ns->addRdata(rdata_ns);
  64. soa->addRdata(rdata_soa);
  65. cname->addRdata(rdata_cname);
  66. list.addRRset(a);
  67. list.addRRset(aaaa);
  68. list.addRRset(ns);
  69. list.addRRset(soa);
  70. list.addRRset(cname);
  71. }
  72. TEST_F(RRsetListTest, emptyOnInitialCreate) {
  73. RRsetList list;
  74. EXPECT_EQ(list.size(), 0);
  75. }
  76. TEST_F(RRsetListTest, addRRsets) {
  77. RRsetList list;
  78. setupList(list);
  79. EXPECT_EQ(list.size(), 5);
  80. }
  81. TEST_F(RRsetListTest, extraRRset) {
  82. RRsetList list;
  83. setupList(list);
  84. RRsetPtr cname(new RRset(Name("another.example.com"), RRClass::IN(),
  85. RRType::CNAME(), RRTTL(3600)));
  86. EXPECT_THROW(list.addRRset(cname), DuplicateRRset);
  87. }
  88. TEST_F(RRsetListTest, randomAccess) {
  89. RRsetList list;
  90. setupList(list);
  91. RRsetPtr p;
  92. p = list[RRType::CNAME()];
  93. EXPECT_TRUE(p->getType() == RRType::CNAME());
  94. p = list[RRType::AAAA()];
  95. EXPECT_TRUE(p->getType() == RRType::AAAA());
  96. p = list[RRType::NS()];
  97. EXPECT_TRUE(p->getType() == RRType::NS());
  98. p = list[RRType::A()];
  99. EXPECT_TRUE(p->getType() == RRType::A());
  100. p = list[RRType::SOA()];
  101. EXPECT_TRUE(p->getType() == RRType::SOA());
  102. }
  103. TEST_F(RRsetListTest, findRRset) {
  104. RRsetList list;
  105. setupList(list);
  106. EXPECT_EQ(list[RRType::A()], list.findRRset(RRType::A(), RRClass::IN()));
  107. }
  108. TEST_F(RRsetListTest, checkData) {
  109. RRsetList list;
  110. RRsetPtr a(new RRset(Name("example.com"), RRClass::IN(),
  111. RRType::A(), RRTTL(3600)));
  112. a->addRdata(rdata_in_a);
  113. list.addRRset(a);
  114. RdataIteratorPtr it = list[RRType::A()]->getRdataIterator();
  115. it->first();
  116. EXPECT_FALSE(it->isLast());
  117. EXPECT_EQ("192.0.2.1", it->getCurrent().toText());
  118. }
  119. TEST_F(RRsetListTest, iterate) {
  120. RRsetList list;
  121. setupList(list);
  122. bool has_a, has_aaaa, has_ns, has_soa, has_cname;
  123. int i = 0;
  124. BOOST_FOREACH(RRsetPtr rrset, list) {
  125. if (rrset->getType() == RRType::A()) {
  126. has_a = true;
  127. }
  128. if (rrset->getType() == RRType::AAAA()) {
  129. has_aaaa = true;
  130. }
  131. if (rrset->getType() == RRType::NS()) {
  132. has_ns = true;
  133. }
  134. if (rrset->getType() == RRType::SOA()) {
  135. has_soa = true;
  136. }
  137. if (rrset->getType() == RRType::CNAME()) {
  138. has_cname = true;
  139. }
  140. ++i;
  141. }
  142. EXPECT_TRUE(has_a);
  143. EXPECT_TRUE(has_aaaa);
  144. EXPECT_TRUE(has_ns);
  145. EXPECT_TRUE(has_soa);
  146. EXPECT_TRUE(has_cname);
  147. EXPECT_TRUE(i == 5);
  148. }
  149. }