zone_table_config_unittest.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Copyright (C) 2013 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 <datasrc/zone_table_config.h>
  15. #include <datasrc/tests/mock_client.h>
  16. #include <cc/data.h>
  17. #include <dns/name.h>
  18. #include <gtest/gtest.h>
  19. using namespace isc::datasrc;
  20. using namespace isc::data;
  21. using namespace isc::dns;
  22. using isc::datasrc::unittest::MockDataSourceClient;
  23. using isc::datasrc::internal::ZoneTableConfig;
  24. namespace {
  25. const char* zones[] = {
  26. "example.org.",
  27. "example.com.",
  28. NULL
  29. };
  30. class ZoneTableConfigTest : public ::testing::Test {
  31. protected:
  32. ZoneTableConfigTest() :
  33. mock_client_(zones),
  34. master_config_(Element::fromJSON(
  35. "{\"params\": "
  36. " {\".\": \"" TEST_DATA_DIR "/root.zone\"}"
  37. "}")),
  38. mock_config_(Element::fromJSON("{\"cache-zones\": [\".\"]}"))
  39. {}
  40. MockDataSourceClient mock_client_;
  41. const ConstElementPtr master_config_; // valid config for MasterFiles
  42. const ConstElementPtr mock_config_; // valid config for MasterFiles
  43. };
  44. TEST_F(ZoneTableConfigTest, constructMasterFiles) {
  45. // A simple case: configuring a MasterFiles table with a single zone
  46. const ZoneTableConfig ztconf("MasterFiles", 0, *master_config_);
  47. // getZoneConfig() returns a map containing exactly one entry
  48. // corresponding to the root zone information in the configuration.
  49. EXPECT_EQ(1, ztconf.getZoneConfig().size());
  50. EXPECT_EQ(Name::ROOT_NAME(), ztconf.getZoneConfig().begin()->first);
  51. EXPECT_EQ(TEST_DATA_DIR "/root.zone",
  52. ztconf.getZoneConfig().begin()->second);
  53. // With multiple zones. There shouldn't be anything special, so we
  54. // only check the size of getZoneConfig. Note that the constructor
  55. // doesn't check if the file exists, so they can be anything.
  56. const ConstElementPtr config_elem_multi(
  57. Element::fromJSON("{\"params\": "
  58. "{\"example.com\": \"file1\","
  59. " \"example.org\": \"file2\","
  60. " \"example.info\": \"file3\"}"
  61. "}"));
  62. EXPECT_EQ(3, ZoneTableConfig("MasterFiles", 0, *config_elem_multi).
  63. getZoneConfig().size());
  64. // A bit unusual, but acceptable case: empty parameters, so no zones.
  65. EXPECT_TRUE(ZoneTableConfig("MasterFiles", 0,
  66. *Element::fromJSON("{\"params\": {}}")).
  67. getZoneConfig().empty());
  68. }
  69. TEST_F(ZoneTableConfigTest, badConstructMasterFiles) {
  70. // no "params"
  71. EXPECT_THROW(ZoneTableConfig("MasterFiles", 0, *Element::fromJSON("{}")),
  72. isc::data::TypeError);
  73. // "params" is not a map
  74. EXPECT_THROW(ZoneTableConfig("MasterFiles", 0,
  75. *Element::fromJSON("{\"params\": []}")),
  76. isc::data::TypeError);
  77. // bogus zone name
  78. const ConstElementPtr bad_config(Element::fromJSON(
  79. "{\"params\": "
  80. "{\"bad..name\": \"file1\"}}"));
  81. EXPECT_THROW(ZoneTableConfig("MasterFiles", 0, *bad_config),
  82. isc::dns::EmptyLabel);
  83. // file name is not a string
  84. const ConstElementPtr bad_config2(Element::fromJSON(
  85. "{\"params\": {\".\": 1}}"));
  86. EXPECT_THROW(ZoneTableConfig("MasterFiles", 0, *bad_config2),
  87. isc::data::TypeError);
  88. // Specify data source client (must be null for MasterFiles)
  89. EXPECT_THROW(ZoneTableConfig("MasterFiles", &mock_client_,
  90. *Element::fromJSON("{\"params\": {}}")),
  91. isc::InvalidParameter);
  92. }
  93. TEST_F(ZoneTableConfigTest, constructWithMock) {
  94. // Performing equivalent set of tests as constructMasterFiles
  95. // Configure with a single zone.
  96. const ZoneTableConfig ztconf("mock", &mock_client_, *mock_config_);
  97. EXPECT_EQ(1, ztconf.getZoneConfig().size());
  98. EXPECT_EQ(Name::ROOT_NAME(), ztconf.getZoneConfig().begin()->first);
  99. EXPECT_EQ("", ztconf.getZoneConfig().begin()->second);
  100. // Configure with multiple zones.
  101. const ConstElementPtr config_elem_multi(
  102. Element::fromJSON("{\"cache-zones\": "
  103. "[\"example.com\", \"example.org\",\"example.info\"]"
  104. "}"));
  105. EXPECT_EQ(3, ZoneTableConfig("mock", &mock_client_, *config_elem_multi).
  106. getZoneConfig().size());
  107. // Empty
  108. EXPECT_TRUE(ZoneTableConfig("mock", &mock_client_,
  109. *Element::fromJSON("{\"cache-zones\": []}")).
  110. getZoneConfig().empty());
  111. }
  112. TEST_F(ZoneTableConfigTest, badConstructWithMock) {
  113. // no "cache-zones" (may become valid in future, but for now "notimp")
  114. EXPECT_THROW(ZoneTableConfig("mock", &mock_client_,
  115. *Element::fromJSON("{}")),
  116. isc::NotImplemented);
  117. // "cache-zones" is not a list
  118. EXPECT_THROW(ZoneTableConfig("mock", &mock_client_,
  119. *Element::fromJSON("{\"cache-zones\": {}}")),
  120. isc::data::TypeError);
  121. // "cache-zone" entry is not a string
  122. EXPECT_THROW(ZoneTableConfig("mock", &mock_client_,
  123. *Element::fromJSON("{\"cache-zones\": [1]}")),
  124. isc::data::TypeError);
  125. // bogus zone name
  126. const ConstElementPtr bad_config(Element::fromJSON(
  127. "{\"cache-zones\": [\"bad..\"]}"));
  128. EXPECT_THROW(ZoneTableConfig("mock", &mock_client_, *bad_config),
  129. isc::dns::EmptyLabel);
  130. // duplicate zone name
  131. const ConstElementPtr dup_config(Element::fromJSON(
  132. "{\"cache-zones\": "
  133. " [\"example\", \"example\"]}"));
  134. EXPECT_THROW(ZoneTableConfig("mock", &mock_client_, *dup_config),
  135. isc::InvalidParameter);
  136. // datasrc is null
  137. EXPECT_THROW(ZoneTableConfig("mock", 0, *mock_config_),
  138. isc::InvalidParameter);
  139. }
  140. }