resolver_cache_unittest.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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$
  15. #include <config.h>
  16. #include <string>
  17. #include <gtest/gtest.h>
  18. #include <dns/rrset.h>
  19. #include "resolver_cache.h"
  20. #include "cache_test_util.h"
  21. using namespace isc::cache;
  22. using namespace isc::dns;
  23. using namespace std;
  24. namespace {
  25. class ResolverCacheTest: public testing::Test {
  26. public:
  27. ResolverCacheTest() {
  28. vector<CacheSizeInfo> vec;
  29. CacheSizeInfo class_in(RRClass::IN(), 100, 200);
  30. CacheSizeInfo class_ch(RRClass::CH(), 100, 200);
  31. vec.push_back(class_in);
  32. vec.push_back(class_ch);
  33. cache = new ResolverCache(vec);
  34. }
  35. ~ResolverCacheTest() {
  36. delete cache;
  37. }
  38. ResolverCache* cache;
  39. };
  40. TEST_F(ResolverCacheTest, testUpdateMessage) {
  41. Message msg(Message::PARSE);
  42. messageFromFile(msg, "message_fromWire3");
  43. cache->update(msg);
  44. Name qname("example.com.");
  45. RRType qtype(6);
  46. RRClass klass(1);
  47. msg.makeResponse();
  48. EXPECT_TRUE(cache->lookup(qname, qtype, klass, msg));
  49. EXPECT_TRUE(msg.getHeaderFlag(Message::HEADERFLAG_AA));
  50. // Test whether the old message can be updated
  51. Message new_msg(Message::PARSE);
  52. messageFromFile(new_msg, "message_fromWire4");
  53. cache->update(new_msg);
  54. new_msg.makeResponse();
  55. EXPECT_TRUE(cache->lookup(qname, qtype, klass, new_msg));
  56. EXPECT_FALSE(new_msg.getHeaderFlag(Message::HEADERFLAG_AA));
  57. }
  58. #if 0
  59. TEST_F(ResolverCacheTest, testUpdateRRset) {
  60. Message msg(Message::PARSE);
  61. messageFromFile(msg, "message_fromWire3");
  62. cache->update(msg);
  63. Name qname("example.com.");
  64. RRType qtype(6);
  65. RRClass klass(1);
  66. msg.makeResponse();
  67. EXPECT_TRUE(cache->lookup(qname, qtype, klass, msg));
  68. Message except_msg(Message::RENDER);
  69. EXPECT_THROW(cache->lookup(qname, qtype, klass, except_msg),
  70. MessageNoQuestionSection);
  71. // Get one rrset in the message, then use it to
  72. // update rrset cache-> Test whether the local zone
  73. // data is updated.
  74. RRsetIterator iter = msg.beginSection(Message::SECTION_AUTHORITY);
  75. RRsetPtr rrset_ptr = *iter;
  76. cache->update(rrset_ptr);
  77. Message new_msg(Message::RENDER);
  78. Question question(qname, klass, RRType::NS());
  79. new_msg.addQuestion(question);
  80. EXPECT_TRUE(cache->lookup(qname, RRType::NS(), klass, new_msg));
  81. EXPECT_EQ(0, sectionRRsetCount(new_msg, Message::SECTION_AUTHORITY));
  82. EXPECT_EQ(0, sectionRRsetCount(new_msg, Message::SECTION_ADDITIONAL));
  83. }
  84. TEST_F(ResolverCacheTest, testLookupUnsupportedClass) {
  85. Message msg(Message::PARSE);
  86. messageFromFile(msg, "message_fromWire3");
  87. cache->update(msg);
  88. Name qname("example.com.");
  89. msg.makeResponse();
  90. EXPECT_FALSE(cache->lookup(qname, RRType::SOA(), RRClass::CH(), msg));
  91. EXPECT_FALSE(cache->lookup(qname, RRType::SOA(), RRClass::CH()));
  92. }
  93. TEST_F(ResolverCacheTest, testLookupClosestRRset) {
  94. Message msg(Message::PARSE);
  95. messageFromFile(msg, "message_fromWire3");
  96. cache->update(msg);
  97. Name qname("www.test.example.com.");
  98. RRsetPtr rrset_ptr = cache->lookupClosestRRset(qname, RRType::NS(),
  99. RRClass::IN());
  100. EXPECT_TRUE(rrset_ptr);
  101. EXPECT_EQ(rrset_ptr->getName(), Name("example.com."));
  102. rrset_ptr = cache->lookupClosestRRset(Name("example.com."),
  103. RRType::NS(), RRClass::IN());
  104. EXPECT_TRUE(rrset_ptr);
  105. EXPECT_EQ(rrset_ptr->getName(), Name("example.com."));
  106. rrset_ptr = cache->lookupClosestRRset(Name("com."),
  107. RRType::NS(), RRClass::IN());
  108. EXPECT_FALSE(rrset_ptr);
  109. }
  110. TEST_F(ResolverCacheTest, testHasClass) {
  111. EXPECT_TRUE(cache->getClassCache(RRClass::IN()));
  112. EXPECT_TRUE(cache->getClassCache(RRClass::CH()));
  113. EXPECT_FALSE(cache->getClassCache(RRClass::ANY()));
  114. }
  115. #endif
  116. }