cache_unittest.cc 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 <stdexcept>
  15. #include <dns/name.h>
  16. #include <dns/rdata.h>
  17. #include <dns/rdataclass.h>
  18. #include <dns/rrclass.h>
  19. #include <dns/rrtype.h>
  20. #include <dns/rrttl.h>
  21. #include <dns/rrset.h>
  22. #include <datasrc/cache.h>
  23. #include <datasrc/data_source.h>
  24. #include <gtest/gtest.h>
  25. using namespace std;
  26. using namespace isc::dns;
  27. using namespace isc::dns::rdata;
  28. using namespace isc::datasrc;
  29. namespace {
  30. class CacheTest : public ::testing::Test {
  31. protected:
  32. CacheTest() : test_name("test.example.com"),
  33. test_nsname("ns.example.com"),
  34. test_ch("example.com")
  35. {
  36. RRsetPtr a(new RRset(test_name, RRClass::IN(), RRType::A(),
  37. RRTTL(3600)));
  38. a->addRdata(in::A("192.0.2.1"));
  39. a->addRdata(in::A("192.0.2.2"));
  40. RRsetPtr b(new RRset(test_nsname, RRClass::IN(), RRType::NS(),
  41. RRTTL(86400)));
  42. RRsetPtr c(new RRset(test_ch, RRClass::CH(), RRType::TXT(),
  43. RRTTL(0)));
  44. c->addRdata(generic::TXT("Text record"));
  45. cache.setSlots(5);
  46. cache.addPositive(a, 1, 30);
  47. cache.addPositive(b, 2, 30);
  48. cache.addPositive(c, 4, 30);
  49. }
  50. Name test_name;
  51. Name test_nsname;
  52. Name test_ch;
  53. HotCache cache;
  54. };
  55. class TestRRset : public RRset {
  56. public:
  57. TestRRset(const Name& name, int& counter) :
  58. RRset(name, RRClass::IN(), RRType::A(), RRTTL(3600)),
  59. counter_(counter)
  60. {
  61. ++counter_;
  62. }
  63. ~TestRRset() {
  64. --counter_;
  65. }
  66. int& counter_;
  67. };
  68. // make sure any remaining cache entries are purged on destruction of the
  69. // cache.
  70. TEST_F(CacheTest, cleanup) {
  71. HotCache* local_cache(new HotCache);
  72. int num_rrsets = 0;
  73. local_cache->addPositive(RRsetPtr(new TestRRset(Name("example.com"),
  74. num_rrsets)), 0, 10);
  75. local_cache->addPositive(RRsetPtr(new TestRRset(Name("example.org"),
  76. num_rrsets)), 0, 10);
  77. EXPECT_EQ(2, num_rrsets);
  78. delete local_cache;
  79. EXPECT_EQ(0, num_rrsets);
  80. }
  81. TEST_F(CacheTest, slots) {
  82. EXPECT_EQ(5, cache.getSlots());
  83. EXPECT_EQ(3, cache.getCount());
  84. }
  85. TEST_F(CacheTest, insert) {
  86. RRsetPtr aaaa(new RRset(Name("foo"), RRClass::IN(), RRType::AAAA(),
  87. RRTTL(0)));
  88. aaaa->addRdata(in::AAAA("2001:db8:3:bb::5"));
  89. cache.addPositive(aaaa, 0, 4);
  90. EXPECT_EQ(4, cache.getCount());
  91. RRsetPtr r;
  92. uint32_t f;
  93. bool hit = cache.retrieve(Name("foo"), RRClass::IN(),
  94. RRType::AAAA(), r, f);
  95. EXPECT_TRUE(hit);
  96. EXPECT_EQ(aaaa, r);
  97. }
  98. TEST_F(CacheTest, retrieveOK) {
  99. bool hit;
  100. RRsetPtr r;
  101. uint32_t f;
  102. // Test repeatedly to ensure that all records remain accessible
  103. // even after being promoted to the top of the cache
  104. hit = cache.retrieve(test_name, RRClass::IN(), RRType::A(), r, f);
  105. EXPECT_TRUE(hit);
  106. EXPECT_EQ(test_name, r->getName());
  107. EXPECT_EQ(RRClass::IN(), r->getClass());
  108. EXPECT_EQ(RRType::A(), r->getType());
  109. hit = cache.retrieve(test_nsname, RRClass::IN(), RRType::NS(), r, f);
  110. EXPECT_TRUE(hit);
  111. EXPECT_EQ(test_nsname, r->getName());
  112. EXPECT_EQ(RRClass::IN(), r->getClass());
  113. EXPECT_EQ(RRType::NS(), r->getType());
  114. hit = cache.retrieve(test_ch, RRClass::CH(), RRType::TXT(), r, f);
  115. EXPECT_TRUE(hit);
  116. EXPECT_EQ(test_ch, r->getName());
  117. EXPECT_EQ(RRClass::CH(), r->getClass());
  118. EXPECT_EQ(RRType::TXT(), r->getType());
  119. hit = cache.retrieve(test_nsname, RRClass::IN(), RRType::NS(), r, f);
  120. EXPECT_TRUE(hit);
  121. EXPECT_EQ(test_nsname, r->getName());
  122. EXPECT_EQ(RRClass::IN(), r->getClass());
  123. EXPECT_EQ(RRType::NS(), r->getType());
  124. hit = cache.retrieve(test_ch, RRClass::CH(), RRType::TXT(), r, f);
  125. EXPECT_TRUE(hit);
  126. EXPECT_EQ(test_ch, r->getName());
  127. EXPECT_EQ(RRClass::CH(), r->getClass());
  128. EXPECT_EQ(RRType::TXT(), r->getType());
  129. hit = cache.retrieve(test_name, RRClass::IN(), RRType::A(), r, f);
  130. EXPECT_TRUE(hit);
  131. EXPECT_EQ(test_name, r->getName());
  132. EXPECT_EQ(RRClass::IN(), r->getClass());
  133. EXPECT_EQ(RRType::A(), r->getType());
  134. hit = cache.retrieve(test_name, RRClass::IN(), RRType::A(), r, f);
  135. EXPECT_TRUE(hit);
  136. EXPECT_EQ(test_name, r->getName());
  137. EXPECT_EQ(RRClass::IN(), r->getClass());
  138. EXPECT_EQ(RRType::A(), r->getType());
  139. };
  140. TEST_F(CacheTest, flags) {
  141. bool hit;
  142. RRsetPtr r;
  143. uint32_t f;
  144. hit = cache.retrieve(test_name, RRClass::IN(), RRType::A(), r, f);
  145. EXPECT_TRUE(hit);
  146. EXPECT_TRUE(r);
  147. EXPECT_EQ(DataSrc::REFERRAL, f);
  148. hit = cache.retrieve(test_nsname, RRClass::IN(), RRType::NS(), r, f);
  149. EXPECT_TRUE(hit);
  150. EXPECT_TRUE(r);
  151. EXPECT_EQ(DataSrc::CNAME_FOUND, f);
  152. hit = cache.retrieve(test_ch, RRClass::CH(), RRType::TXT(), r, f);
  153. EXPECT_TRUE(hit);
  154. EXPECT_TRUE(r);
  155. EXPECT_EQ(DataSrc::NAME_NOT_FOUND, f);
  156. }
  157. TEST_F(CacheTest, retrieveFail) {
  158. bool hit;
  159. RRsetPtr r;
  160. uint32_t f;
  161. hit = cache.retrieve(Name("fake"), RRClass::IN(), RRType::A(), r, f);
  162. EXPECT_FALSE(hit);
  163. hit = cache.retrieve(test_name, RRClass::CH(), RRType::A(), r, f);
  164. EXPECT_FALSE(hit);
  165. hit = cache.retrieve(test_name, RRClass::IN(), RRType::DNSKEY(), r, f);
  166. EXPECT_FALSE(hit);
  167. }
  168. TEST_F(CacheTest, expire) {
  169. // Insert "foo" with a duration of 1 seconds; sleep 2. The
  170. // record should not be returned from the cache even though it's
  171. // at the top of the cache.
  172. RRsetPtr aaaa(new RRset(Name("foo"), RRClass::IN(), RRType::AAAA(),
  173. RRTTL(0)));
  174. aaaa->addRdata(in::AAAA("2001:db8:3:bb::5"));
  175. cache.addPositive(aaaa, 0, 1);
  176. sleep(2);
  177. RRsetPtr r;
  178. uint32_t f;
  179. bool hit = cache.retrieve(Name("foo"), RRClass::IN(), RRType::AAAA(), r, f);
  180. EXPECT_FALSE(hit);
  181. };
  182. TEST_F(CacheTest, LRU) {
  183. // Retrieve a record, cache four new records; with five slots
  184. // in the LRU queue this should remove all the previous records
  185. // except the last one retreived.
  186. bool hit;
  187. RRsetPtr r;
  188. uint32_t f;
  189. hit = cache.retrieve(test_nsname, RRClass::IN(), RRType::NS(), r, f);
  190. EXPECT_TRUE(hit);
  191. EXPECT_EQ(3, cache.getCount());
  192. RRsetPtr one(new RRset(Name("one"), RRClass::IN(), RRType::TXT(),
  193. RRTTL(0)));
  194. one->addRdata(generic::TXT("one"));
  195. cache.addPositive(one, 0, 30);
  196. EXPECT_EQ(4, cache.getCount());
  197. RRsetPtr two(new RRset(Name("two"), RRClass::IN(), RRType::TXT(),
  198. RRTTL(0)));
  199. two->addRdata(generic::TXT("two"));
  200. cache.addPositive(two, 0, 30);
  201. EXPECT_EQ(5, cache.getCount());
  202. RRsetPtr three(new RRset(Name("three"), RRClass::IN(), RRType::TXT(),
  203. RRTTL(0)));
  204. three->addRdata(generic::TXT("three"));
  205. cache.addPositive(three, 0, 30);
  206. EXPECT_EQ(5, cache.getCount());
  207. RRsetPtr four(new RRset(Name("four"), RRClass::IN(), RRType::TXT(),
  208. RRTTL(0)));
  209. four->addRdata(generic::TXT("four"));
  210. cache.addPositive(four, 0, 30);
  211. EXPECT_EQ(5, cache.getCount());
  212. hit = cache.retrieve(test_name, RRClass::IN(), RRType::A(), r, f);
  213. EXPECT_FALSE(hit);
  214. hit = cache.retrieve(test_nsname, RRClass::IN(), RRType::NS(), r, f);
  215. EXPECT_TRUE(hit);
  216. hit = cache.retrieve(test_ch, RRClass::CH(), RRType::TXT(), r, f);
  217. EXPECT_FALSE(hit);
  218. }
  219. TEST_F(CacheTest, ncache) {
  220. Name missing("missing.example.com");
  221. cache.addNegative(missing, RRClass::IN(), RRType::DNSKEY(), 8, 30);
  222. RRsetPtr r;
  223. uint32_t f;
  224. bool hit = cache.retrieve(missing, RRClass::IN(), RRType::DNSKEY(), r, f);
  225. EXPECT_TRUE(hit);
  226. EXPECT_FALSE(r);
  227. EXPECT_EQ(DataSrc::TYPE_NOT_FOUND, f);
  228. }
  229. TEST_F(CacheTest, overwrite) {
  230. EXPECT_EQ(3, cache.getCount());
  231. RRsetPtr a(new RRset(test_name, RRClass::IN(), RRType::A(), RRTTL(300)));
  232. a->addRdata(in::A("192.0.2.100"));
  233. EXPECT_NO_THROW(cache.addPositive(a, 16, 30));
  234. EXPECT_EQ(3, cache.getCount());
  235. RRsetPtr r;
  236. uint32_t f;
  237. bool hit = cache.retrieve(test_name, RRClass::IN(), RRType::A(), r, f);
  238. EXPECT_TRUE(hit);
  239. EXPECT_TRUE(r);
  240. EXPECT_EQ(16, f);
  241. EXPECT_NO_THROW(cache.addNegative(test_name, RRClass::IN(), RRType::A(), 1, 30));
  242. EXPECT_EQ(3, cache.getCount());
  243. hit = cache.retrieve(test_name, RRClass::IN(), RRType::A(), r, f);
  244. EXPECT_TRUE(hit);
  245. EXPECT_FALSE(r);
  246. EXPECT_EQ(DataSrc::REFERRAL, f);
  247. }
  248. TEST_F(CacheTest, reduceSlots) {
  249. EXPECT_EQ(3, cache.getCount());
  250. cache.setSlots(2);
  251. EXPECT_EQ(2, cache.getCount());
  252. cache.setSlots(1);
  253. EXPECT_EQ(1, cache.getCount());
  254. cache.setSlots(0);
  255. EXPECT_EQ(1, cache.getCount());
  256. }
  257. TEST_F(CacheTest, setEnabled) {
  258. cache.setEnabled(false);
  259. EXPECT_FALSE(cache.getEnabled());
  260. cache.setEnabled(true);
  261. EXPECT_TRUE(cache.getEnabled());
  262. }
  263. TEST_F(CacheTest, disabled) {
  264. bool hit;
  265. RRsetPtr r;
  266. uint32_t f;
  267. cache.setEnabled(false);
  268. hit = cache.retrieve(test_name, RRClass::IN(), RRType::A(), r, f);
  269. EXPECT_FALSE(hit);
  270. cache.setEnabled(true);
  271. hit = cache.retrieve(test_name, RRClass::IN(), RRType::A(), r, f);
  272. EXPECT_TRUE(hit);
  273. EXPECT_EQ(test_name, r->getName());
  274. EXPECT_EQ(RRClass::IN(), r->getClass());
  275. EXPECT_EQ(RRType::A(), r->getType());
  276. }
  277. }