zonetable_unittest.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 <exceptions/exceptions.h>
  15. #include <dns/name.h>
  16. #include <dns/rrclass.h>
  17. #include <datasrc/zonetable.h>
  18. // We use MemoryZone to put something into the table
  19. #include <datasrc/memory_datasrc.h>
  20. #include <gtest/gtest.h>
  21. using namespace isc::dns;
  22. using namespace isc::datasrc;
  23. namespace {
  24. TEST(ZoneTest, init) {
  25. MemoryZone zone(RRClass::IN(), Name("example.com"));
  26. EXPECT_EQ(Name("example.com"), zone.getOrigin());
  27. EXPECT_EQ(RRClass::IN(), zone.getClass());
  28. MemoryZone ch_zone(RRClass::CH(), Name("example"));
  29. EXPECT_EQ(Name("example"), ch_zone.getOrigin());
  30. EXPECT_EQ(RRClass::CH(), ch_zone.getClass());
  31. }
  32. TEST(ZoneTest, find) {
  33. MemoryZone zone(RRClass::IN(), Name("example.com"));
  34. EXPECT_EQ(Zone::NXDOMAIN,
  35. zone.find(Name("www.example.com"), RRType::A()).code);
  36. }
  37. class ZoneTableTest : public ::testing::Test {
  38. protected:
  39. ZoneTableTest() : zone1(new MemoryZone(RRClass::IN(),
  40. Name("example.com"))),
  41. zone2(new MemoryZone(RRClass::IN(),
  42. Name("example.net"))),
  43. zone3(new MemoryZone(RRClass::IN(), Name("example")))
  44. {}
  45. ZoneTable zone_table;
  46. ZonePtr zone1, zone2, zone3;
  47. };
  48. TEST_F(ZoneTableTest, addZone) {
  49. EXPECT_EQ(result::SUCCESS, zone_table.addZone(zone1));
  50. EXPECT_EQ(result::EXIST, zone_table.addZone(zone1));
  51. // names are compared in a case insensitive manner.
  52. EXPECT_EQ(result::EXIST, zone_table.addZone(
  53. ZonePtr(new MemoryZone(RRClass::IN(), Name("EXAMPLE.COM")))));
  54. EXPECT_EQ(result::SUCCESS, zone_table.addZone(zone2));
  55. EXPECT_EQ(result::SUCCESS, zone_table.addZone(zone3));
  56. // Zone table is indexed only by name. Duplicate origin name with
  57. // different zone class isn't allowed.
  58. EXPECT_EQ(result::EXIST, zone_table.addZone(
  59. ZonePtr(new MemoryZone(RRClass::CH(),
  60. Name("example.com")))));
  61. /// Bogus zone (NULL)
  62. EXPECT_THROW(zone_table.addZone(ZonePtr()), isc::InvalidParameter);
  63. }
  64. TEST_F(ZoneTableTest, DISABLED_removeZone) {
  65. EXPECT_EQ(result::SUCCESS, zone_table.addZone(zone1));
  66. EXPECT_EQ(result::SUCCESS, zone_table.addZone(zone2));
  67. EXPECT_EQ(result::SUCCESS, zone_table.addZone(zone3));
  68. EXPECT_EQ(result::SUCCESS, zone_table.removeZone(Name("example.net")));
  69. EXPECT_EQ(result::NOTFOUND, zone_table.removeZone(Name("example.net")));
  70. }
  71. TEST_F(ZoneTableTest, findZone) {
  72. EXPECT_EQ(result::SUCCESS, zone_table.addZone(zone1));
  73. EXPECT_EQ(result::SUCCESS, zone_table.addZone(zone2));
  74. EXPECT_EQ(result::SUCCESS, zone_table.addZone(zone3));
  75. EXPECT_EQ(result::SUCCESS, zone_table.findZone(Name("example.com")).code);
  76. EXPECT_EQ(Name("example.com"),
  77. zone_table.findZone(Name("example.com")).zone->getOrigin());
  78. EXPECT_EQ(result::NOTFOUND,
  79. zone_table.findZone(Name("example.org")).code);
  80. EXPECT_EQ(ConstZonePtr(),
  81. zone_table.findZone(Name("example.org")).zone);
  82. // there's no exact match. the result should be the longest match,
  83. // and the code should be PARTIALMATCH.
  84. EXPECT_EQ(result::PARTIALMATCH,
  85. zone_table.findZone(Name("www.example.com")).code);
  86. EXPECT_EQ(Name("example.com"),
  87. zone_table.findZone(Name("www.example.com")).zone->getOrigin());
  88. // make sure the partial match is indeed the longest match by adding
  89. // a zone with a shorter origin and query again.
  90. ZonePtr zone_com(new MemoryZone(RRClass::IN(), Name("com")));
  91. EXPECT_EQ(result::SUCCESS, zone_table.addZone(zone_com));
  92. EXPECT_EQ(Name("example.com"),
  93. zone_table.findZone(Name("www.example.com")).zone->getOrigin());
  94. }
  95. }