rrset_entry_unittest.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 <cache/cache_entry_key.h>
  19. #include <cache/rrset_entry.h>
  20. #include <dns/name.h>
  21. #include <dns/rrclass.h>
  22. #include <dns/rrtype.h>
  23. #include <dns/rrttl.h>
  24. #include <dns/rrset.h>
  25. using namespace isc::cache;
  26. using namespace isc::dns;
  27. using namespace std;
  28. namespace {
  29. class GenCacheKeyTest: public testing::Test {
  30. };
  31. TEST_F(GenCacheKeyTest, genCacheEntryKey1) {
  32. string name = "example.com.";
  33. uint16_t type = 12;
  34. string name_type = "example.com.12";
  35. EXPECT_EQ(name_type, genCacheEntryName(name, type));
  36. }
  37. TEST_F(GenCacheKeyTest, genCacheEntryKey2) {
  38. Name name("example.com");
  39. RRType type(1234);
  40. string keystr = "example.com.1234";
  41. EXPECT_EQ(keystr, genCacheEntryName(name, type));
  42. }
  43. class DerivedRRsetEntry: public RRsetEntry {
  44. public:
  45. DerivedRRsetEntry(const isc::dns::RRset& rrset, const RRsetTrustLevel& level) : RRsetEntry(rrset, level) {};
  46. void updateTTLForTest() {
  47. }
  48. };
  49. #define TEST_TTL 100
  50. class RRsetEntryTest : public ::testing::Test {
  51. protected:
  52. RRsetEntryTest():
  53. name("test.example.com"),
  54. rrset(name, RRClass::IN(), RRType::A(), RRTTL(TEST_TTL)),
  55. trust_level(RRSET_TRUST_ADDITIONAL_AA),
  56. rrset_entry(rrset, trust_level)
  57. {
  58. }
  59. Name name;
  60. RRset rrset;
  61. RRsetTrustLevel trust_level;
  62. RRsetEntry rrset_entry;
  63. };
  64. TEST_F(RRsetEntryTest, constructor) {
  65. EXPECT_EQ(trust_level, rrset_entry.getTrustLevel());
  66. EXPECT_EQ(rrset.getName(), rrset_entry.getRRset()->getName());
  67. EXPECT_EQ(rrset.getClass(), rrset_entry.getRRset()->getClass());
  68. EXPECT_EQ(rrset.getType(), rrset_entry.getRRset()->getType());
  69. EXPECT_EQ(rrset.getRdataCount(), rrset_entry.getRRset()->getRdataCount());
  70. }
  71. TEST_F(RRsetEntryTest, updateTTL) {
  72. uint32_t ttl = rrset_entry.getTTL();
  73. sleep(1);
  74. // The TTL should be decreased
  75. EXPECT_TRUE(rrset_entry.getTTL() < ttl);
  76. }
  77. TEST_F(RRsetEntryTest, TTLExpire) {
  78. RRset exp_rrset(name, RRClass::IN(), RRType::A(), RRTTL(1));
  79. RRsetEntry rrset_entry(exp_rrset, RRSET_TRUST_ANSWER_AA);
  80. sleep(1);
  81. uint32_t ttl = rrset_entry.getTTL();
  82. EXPECT_LT(ttl, 1);
  83. sleep(1);
  84. ttl = rrset_entry.getTTL();
  85. EXPECT_LT(ttl, 1);
  86. }
  87. TEST_F(RRsetEntryTest, getExpireTime){
  88. uint32_t exp_time = time(NULL) + TEST_TTL;
  89. EXPECT_EQ(exp_time, rrset_entry.getExpireTime());
  90. }
  91. } // namespace