rrset_collection_unittest.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 <dns/rrset_collection.h>
  15. #include <dns/rrttl.h>
  16. #include <dns/rdataclass.h>
  17. #include <gtest/gtest.h>
  18. using namespace isc::dns;
  19. using namespace isc::dns::rdata;
  20. using namespace std;
  21. namespace {
  22. class RRsetCollectionTest : public ::testing::Test {
  23. public:
  24. RRsetCollectionTest() :
  25. rrclass("IN"),
  26. origin("example.org"),
  27. collection(TEST_DATA_SRCDIR "/example.org", origin, rrclass)
  28. {}
  29. const RRClass rrclass;
  30. const Name origin;
  31. RRsetCollection collection;
  32. };
  33. TEST_F(RRsetCollectionTest, findBase) {
  34. // Test the find() that returns isc::dns::AbstractRRset*
  35. const AbstractRRset* rrset = collection.find(Name("www.example.org"),
  36. RRType::A(), rrclass);
  37. EXPECT_NE(static_cast<AbstractRRset*>(NULL), rrset);
  38. EXPECT_EQ(RRType::A(), rrset->getType());
  39. EXPECT_EQ(RRTTL(3600), rrset->getTTL());
  40. EXPECT_EQ(RRClass("IN"), rrset->getClass());
  41. EXPECT_EQ(Name("www.example.org"), rrset->getName());
  42. // foo.example.org doesn't exist
  43. rrset = collection.find(Name("foo.example.org"), RRType::A(), rrclass);
  44. EXPECT_EQ(static_cast<AbstractRRset*>(NULL), rrset);
  45. // www.example.org exists, but not with MX
  46. rrset = collection.find(Name("www.example.org"), RRType::MX(), rrclass);
  47. EXPECT_EQ(static_cast<AbstractRRset*>(NULL), rrset);
  48. // www.example.org exists, with AAAA
  49. rrset = collection.find(Name("www.example.org"), RRType::AAAA(), rrclass);
  50. EXPECT_NE(static_cast<AbstractRRset*>(NULL), rrset);
  51. // www.example.org with AAAA does not exist in RRClass::CH()
  52. rrset = collection.find(Name("www.example.org"), RRType::AAAA(),
  53. RRClass::CH());
  54. EXPECT_EQ(static_cast<AbstractRRset*>(NULL), rrset);
  55. }
  56. template <typename T, typename TP>
  57. void doFind(T& collection, const RRClass& rrclass) {
  58. // Test the find() that returns ConstRRsetPtr
  59. TP rrset = collection.find(Name("www.example.org"), rrclass, RRType::A());
  60. EXPECT_TRUE(rrset);
  61. EXPECT_EQ(RRType::A(), rrset->getType());
  62. EXPECT_EQ(RRTTL(3600), rrset->getTTL());
  63. EXPECT_EQ(RRClass("IN"), rrset->getClass());
  64. EXPECT_EQ(Name("www.example.org"), rrset->getName());
  65. // foo.example.org doesn't exist
  66. rrset = collection.find(Name("foo.example.org"), rrclass, RRType::A());
  67. EXPECT_FALSE(rrset);
  68. // www.example.org exists, but not with MX
  69. rrset = collection.find(Name("www.example.org"), rrclass, RRType::MX());
  70. EXPECT_FALSE(rrset);
  71. // www.example.org exists, with AAAA
  72. rrset = collection.find(Name("www.example.org"), rrclass, RRType::AAAA());
  73. EXPECT_TRUE(rrset);
  74. // www.example.org with AAAA does not exist in RRClass::CH()
  75. rrset = collection.find(Name("www.example.org"), RRClass::CH(),
  76. RRType::AAAA());
  77. EXPECT_FALSE(rrset);
  78. }
  79. TEST_F(RRsetCollectionTest, findConst) {
  80. // Test the find() that returns ConstRRsetPtr
  81. const RRsetCollection& ccln = collection;
  82. doFind<const RRsetCollection, ConstRRsetPtr>(ccln, rrclass);
  83. }
  84. TEST_F(RRsetCollectionTest, find) {
  85. // Test the find() that returns RRsetPtr
  86. doFind<RRsetCollection, RRsetPtr>(collection, rrclass);
  87. }
  88. void
  89. doAddAndRemove(RRsetCollection& collection, const RRClass& rrclass) {
  90. // foo.example.org/A doesn't exist
  91. RRsetPtr rrset_found = collection.find(Name("foo.example.org"), rrclass,
  92. RRType::A());
  93. EXPECT_FALSE(rrset_found);
  94. // Add foo.example.org/A
  95. RRsetPtr rrset(new BasicRRset(Name("foo.example.org"), rrclass, RRType::A(),
  96. RRTTL(7200)));
  97. rrset->addRdata(in::A("192.0.2.1"));
  98. collection.addRRset(rrset);
  99. // foo.example.org/A should now exist
  100. rrset_found = collection.find(Name("foo.example.org"), rrclass,
  101. RRType::A());
  102. EXPECT_TRUE(rrset_found);
  103. EXPECT_EQ(RRType::A(), rrset_found->getType());
  104. EXPECT_EQ(RRTTL(7200), rrset_found->getTTL());
  105. EXPECT_EQ(RRClass("IN"), rrset_found->getClass());
  106. EXPECT_EQ(Name("foo.example.org"), rrset_found->getName());
  107. // Remove foo.example.org/A
  108. collection.removeRRset(Name("foo.example.org"), rrclass, RRType::A());
  109. // foo.example.org/A should not exist now
  110. rrset_found = collection.find(Name("foo.example.org"), rrclass,
  111. RRType::A());
  112. EXPECT_FALSE(rrset_found);
  113. }
  114. TEST_F(RRsetCollectionTest, addAndRemove) {
  115. doAddAndRemove(collection, rrclass);
  116. }
  117. TEST_F(RRsetCollectionTest, iteratorTest) {
  118. // The collection must not be empty.
  119. EXPECT_NE(collection.end(), collection.begin());
  120. // Here, we just count the records and do some basic tests on them.
  121. size_t count = 0;
  122. for (RRsetCollection::iterator it = collection.begin();
  123. it != collection.end(); ++it) {
  124. ++count;
  125. const AbstractRRset& rrset = *it;
  126. EXPECT_EQ(rrclass, rrset.getClass());
  127. EXPECT_EQ(RRTTL(3600), rrset.getTTL());
  128. }
  129. // example.org master file has SOA, NS, A, AAAA
  130. EXPECT_EQ(4, count);
  131. }
  132. } // namespace