zonetable_unittest.cc 4.0 KB

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