zonetable_unittest.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 InMemoryZone 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. InMemoryZoneFinder zone(RRClass::IN(), Name("example.com"));
  26. EXPECT_EQ(Name("example.com"), zone.getOrigin());
  27. EXPECT_EQ(RRClass::IN(), zone.getClass());
  28. InMemoryZoneFinder 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. InMemoryZoneFinder zone(RRClass::IN(), Name("example.com"));
  34. EXPECT_EQ(ZoneFinder::NXDOMAIN,
  35. zone.find(Name("www.example.com"), RRType::A()).code);
  36. }
  37. class ZoneTableTest : public ::testing::Test {
  38. protected:
  39. ZoneTableTest() : zone1(new InMemoryZoneFinder(RRClass::IN(),
  40. Name("example.com"))),
  41. zone2(new InMemoryZoneFinder(RRClass::IN(),
  42. Name("example.net"))),
  43. zone3(new InMemoryZoneFinder(RRClass::IN(),
  44. Name("example")))
  45. {}
  46. ZoneTable zone_table;
  47. ZoneFinderPtr zone1, zone2, zone3;
  48. };
  49. TEST_F(ZoneTableTest, addZone) {
  50. EXPECT_EQ(result::SUCCESS, zone_table.addZone(zone1));
  51. EXPECT_EQ(result::EXIST, zone_table.addZone(zone1));
  52. // names are compared in a case insensitive manner.
  53. EXPECT_EQ(result::EXIST, zone_table.addZone(
  54. ZoneFinderPtr(new InMemoryZoneFinder(RRClass::IN(),
  55. Name("EXAMPLE.COM")))));
  56. EXPECT_EQ(result::SUCCESS, zone_table.addZone(zone2));
  57. EXPECT_EQ(result::SUCCESS, zone_table.addZone(zone3));
  58. // Zone table is indexed only by name. Duplicate origin name with
  59. // different zone class isn't allowed.
  60. EXPECT_EQ(result::EXIST, zone_table.addZone(
  61. ZoneFinderPtr(new InMemoryZoneFinder(RRClass::CH(),
  62. Name("example.com")))));
  63. /// Bogus zone (NULL)
  64. EXPECT_THROW(zone_table.addZone(ZoneFinderPtr()), isc::InvalidParameter);
  65. }
  66. TEST_F(ZoneTableTest, DISABLED_removeZone) {
  67. EXPECT_EQ(result::SUCCESS, zone_table.addZone(zone1));
  68. EXPECT_EQ(result::SUCCESS, zone_table.addZone(zone2));
  69. EXPECT_EQ(result::SUCCESS, zone_table.addZone(zone3));
  70. EXPECT_EQ(result::SUCCESS, zone_table.removeZone(Name("example.net")));
  71. EXPECT_EQ(result::NOTFOUND, zone_table.removeZone(Name("example.net")));
  72. }
  73. TEST_F(ZoneTableTest, findZone) {
  74. EXPECT_EQ(result::SUCCESS, zone_table.addZone(zone1));
  75. EXPECT_EQ(result::SUCCESS, zone_table.addZone(zone2));
  76. EXPECT_EQ(result::SUCCESS, zone_table.addZone(zone3));
  77. EXPECT_EQ(result::SUCCESS, zone_table.findZone(Name("example.com")).code);
  78. EXPECT_EQ(Name("example.com"),
  79. zone_table.findZone(Name("example.com")).zone->getOrigin());
  80. EXPECT_EQ(result::NOTFOUND,
  81. zone_table.findZone(Name("example.org")).code);
  82. EXPECT_EQ(ConstZoneFinderPtr(),
  83. zone_table.findZone(Name("example.org")).zone);
  84. // there's no exact match. the result should be the longest match,
  85. // and the code should be PARTIALMATCH.
  86. EXPECT_EQ(result::PARTIALMATCH,
  87. zone_table.findZone(Name("www.example.com")).code);
  88. EXPECT_EQ(Name("example.com"),
  89. zone_table.findZone(Name("www.example.com")).zone->getOrigin());
  90. // make sure the partial match is indeed the longest match by adding
  91. // a zone with a shorter origin and query again.
  92. ZoneFinderPtr zone_com(new InMemoryZoneFinder(RRClass::IN(), Name("com")));
  93. EXPECT_EQ(result::SUCCESS, zone_table.addZone(zone_com));
  94. EXPECT_EQ(Name("example.com"),
  95. zone_table.findZone(Name("www.example.com")).zone->getOrigin());
  96. }
  97. }