d2_client_unittest.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // Copyright (C) 2012-2014 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 <config.h>
  15. #include <dhcpsrv/d2_client.h>
  16. #include <exceptions/exceptions.h>
  17. #include <gtest/gtest.h>
  18. using namespace std;
  19. using namespace isc::asiolink;
  20. using namespace isc::dhcp;
  21. using namespace isc::util;
  22. using namespace isc;
  23. namespace {
  24. /// @brief Checks constructors and accessors of D2ClientConfig.
  25. TEST(D2ClientConfigTest, constructorsAndAccessors) {
  26. D2ClientConfigPtr d2_client_config;
  27. // Verify default constructor creates a disabled instance.
  28. ASSERT_NO_THROW(d2_client_config.reset(new D2ClientConfig()));
  29. EXPECT_FALSE(d2_client_config->getEnableUpdates());
  30. d2_client_config.reset();
  31. bool enable_updates = true;
  32. isc::asiolink::IOAddress server_ip("127.0.0.1");
  33. size_t server_port = 477;
  34. dhcp_ddns::NameChangeProtocol ncr_protocol = dhcp_ddns::NCR_UDP;
  35. dhcp_ddns::NameChangeFormat ncr_format = dhcp_ddns::FMT_JSON;
  36. bool remove_on_renew = true;
  37. bool always_include_fqdn = true;
  38. bool override_no_update = true;
  39. bool override_client_update = true;
  40. bool replace_client_name = true;
  41. std::string generated_prefix = "the_prefix";
  42. std::string qualifying_suffix = "the.suffix.";
  43. // Verify that we can construct a valid, enabled instance.
  44. ASSERT_NO_THROW(d2_client_config.reset(new
  45. D2ClientConfig(enable_updates,
  46. server_ip,
  47. server_port,
  48. ncr_protocol,
  49. ncr_format,
  50. remove_on_renew,
  51. always_include_fqdn,
  52. override_no_update,
  53. override_client_update,
  54. replace_client_name,
  55. generated_prefix,
  56. qualifying_suffix)));
  57. ASSERT_TRUE(d2_client_config);
  58. // Verify that the accessors return the expected values.
  59. EXPECT_EQ(d2_client_config->getEnableUpdates(), enable_updates);
  60. EXPECT_EQ(d2_client_config->getServerIp(), server_ip);
  61. EXPECT_EQ(d2_client_config->getServerPort(), server_port);
  62. EXPECT_EQ(d2_client_config->getNcrProtocol(), ncr_protocol);
  63. EXPECT_EQ(d2_client_config->getNcrFormat(), ncr_format);
  64. EXPECT_EQ(d2_client_config->getRemoveOnRenew(), remove_on_renew);
  65. EXPECT_EQ(d2_client_config->getAlwaysIncludeFqdn(), always_include_fqdn);
  66. EXPECT_EQ(d2_client_config->getOverrideNoUpdate(), override_no_update);
  67. EXPECT_EQ(d2_client_config->getOverrideClientUpdate(),
  68. override_client_update);
  69. EXPECT_EQ(d2_client_config->getReplaceClientName(), replace_client_name);
  70. EXPECT_EQ(d2_client_config->getGeneratedPrefix(), generated_prefix);
  71. EXPECT_EQ(d2_client_config->getQualifyingSuffix(), qualifying_suffix);
  72. // Verify that toText called by << operator doesn't bomb.
  73. ASSERT_NO_THROW(std::cout << "toText test:" << std::endl <<
  74. *d2_client_config << std::endl);
  75. // Verify that constructor does not allow use of NCR_TCP.
  76. /// @todo obviously this becomes invalid once TCP is supported.
  77. ASSERT_THROW(d2_client_config.reset(new
  78. D2ClientConfig(enable_updates,
  79. server_ip,
  80. server_port,
  81. dhcp_ddns::NCR_TCP,
  82. ncr_format,
  83. remove_on_renew,
  84. always_include_fqdn,
  85. override_no_update,
  86. override_client_update,
  87. replace_client_name,
  88. generated_prefix,
  89. qualifying_suffix)),
  90. D2ClientError);
  91. /// @todo if additional validation is added to ctor, this test needs to
  92. /// expand accordingly.
  93. }
  94. /// @brief Tests the equality and inequality operators of D2ClientConfig.
  95. TEST(D2ClientConfigTest, equalityOperator) {
  96. D2ClientConfigPtr ref_config;
  97. D2ClientConfigPtr test_config;
  98. isc::asiolink::IOAddress ref_address("127.0.0.1");
  99. isc::asiolink::IOAddress test_address("127.0.0.2");
  100. // Create an instance to use as a reference.
  101. ASSERT_NO_THROW(ref_config.reset(new D2ClientConfig(true,
  102. ref_address, 477,
  103. dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
  104. true, true, true, true, true,
  105. "pre-fix", "suf-fix")));
  106. ASSERT_TRUE(ref_config);
  107. // Check a configuration that is identical to reference configuration.
  108. ASSERT_NO_THROW(test_config.reset(new D2ClientConfig(true,
  109. ref_address, 477,
  110. dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
  111. true, true, true, true, true,
  112. "pre-fix", "suf-fix")));
  113. ASSERT_TRUE(test_config);
  114. EXPECT_TRUE(*ref_config == *test_config);
  115. EXPECT_FALSE(*ref_config != *test_config);
  116. // Check a configuration that differs only by enable flag.
  117. ASSERT_NO_THROW(test_config.reset(new D2ClientConfig(false,
  118. ref_address, 477,
  119. dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
  120. true, true, true, true, true,
  121. "pre-fix", "suf-fix")));
  122. ASSERT_TRUE(test_config);
  123. EXPECT_FALSE(*ref_config == *test_config);
  124. EXPECT_TRUE(*ref_config != *test_config);
  125. // Check a configuration that differs only by server ip.
  126. ASSERT_NO_THROW(test_config.reset(new D2ClientConfig(true,
  127. test_address, 477,
  128. dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
  129. true, true, true, true, true,
  130. "pre-fix", "suf-fix")));
  131. ASSERT_TRUE(test_config);
  132. EXPECT_FALSE(*ref_config == *test_config);
  133. EXPECT_TRUE(*ref_config != *test_config);
  134. // Check a configuration that differs only by server port.
  135. ASSERT_NO_THROW(test_config.reset(new D2ClientConfig(true,
  136. ref_address, 333,
  137. dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
  138. true, true, true, true, true,
  139. "pre-fix", "suf-fix")));
  140. ASSERT_TRUE(test_config);
  141. EXPECT_FALSE(*ref_config == *test_config);
  142. EXPECT_TRUE(*ref_config != *test_config);
  143. // Check a configuration that differs only by remove_on_renew.
  144. ASSERT_NO_THROW(test_config.reset(new D2ClientConfig(true,
  145. ref_address, 477,
  146. dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
  147. false, true, true, true, true,
  148. "pre-fix", "suf-fix")));
  149. ASSERT_TRUE(test_config);
  150. EXPECT_FALSE(*ref_config == *test_config);
  151. EXPECT_TRUE(*ref_config != *test_config);
  152. // Check a configuration that differs only by always_include_fqdn.
  153. ASSERT_NO_THROW(test_config.reset(new D2ClientConfig(true,
  154. ref_address, 477,
  155. dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
  156. true, false, true, true, true,
  157. "pre-fix", "suf-fix")));
  158. ASSERT_TRUE(test_config);
  159. EXPECT_FALSE(*ref_config == *test_config);
  160. EXPECT_TRUE(*ref_config != *test_config);
  161. // Check a configuration that differs only by override_no_update.
  162. ASSERT_NO_THROW(test_config.reset(new D2ClientConfig(true,
  163. ref_address, 477,
  164. dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
  165. true, true, false, true, true,
  166. "pre-fix", "suf-fix")));
  167. ASSERT_TRUE(test_config);
  168. EXPECT_FALSE(*ref_config == *test_config);
  169. EXPECT_TRUE(*ref_config != *test_config);
  170. // Check a configuration that differs only by override_client_update.
  171. ASSERT_NO_THROW(test_config.reset(new D2ClientConfig(true,
  172. ref_address, 477,
  173. dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
  174. true, true, true, false, true,
  175. "pre-fix", "suf-fix")));
  176. ASSERT_TRUE(test_config);
  177. EXPECT_FALSE(*ref_config == *test_config);
  178. EXPECT_TRUE(*ref_config != *test_config);
  179. // Check a configuration that differs only by replace_client_name.
  180. ASSERT_NO_THROW(test_config.reset(new D2ClientConfig(true,
  181. ref_address, 477,
  182. dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
  183. true, true, true, true, false,
  184. "pre-fix", "suf-fix")));
  185. ASSERT_TRUE(test_config);
  186. EXPECT_FALSE(*ref_config == *test_config);
  187. EXPECT_TRUE(*ref_config != *test_config);
  188. // Check a configuration that differs only by generated_prefix.
  189. ASSERT_NO_THROW(test_config.reset(new D2ClientConfig(true,
  190. ref_address, 477,
  191. dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
  192. true, true, true, true, true,
  193. "bogus", "suf-fix")));
  194. ASSERT_TRUE(test_config);
  195. EXPECT_FALSE(*ref_config == *test_config);
  196. EXPECT_TRUE(*ref_config != *test_config);
  197. // Check a configuration that differs only by qualifying_suffix.
  198. ASSERT_NO_THROW(test_config.reset(new D2ClientConfig(true,
  199. ref_address, 477,
  200. dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
  201. true, true, true, true, true,
  202. "pre-fix", "bogus")));
  203. ASSERT_TRUE(test_config);
  204. EXPECT_FALSE(*ref_config == *test_config);
  205. EXPECT_TRUE(*ref_config != *test_config);
  206. }
  207. /// @brief Checks the D2ClientMgr constructor.
  208. TEST(D2ClientMgr, constructor) {
  209. D2ClientMgrPtr d2_client_mgr;
  210. // Verify we can construct with the default constructor.
  211. ASSERT_NO_THROW(d2_client_mgr.reset(new D2ClientMgr()));
  212. // After construction, D2 configuration should be disabled.
  213. // Fetch it and verify this is the case.
  214. D2ClientConfigPtr original_config = d2_client_mgr->getD2ClientConfig();
  215. ASSERT_TRUE(original_config);
  216. EXPECT_FALSE(original_config->getEnableUpdates());
  217. // Make sure convenience method agrees.
  218. EXPECT_FALSE(d2_client_mgr->ddnsEnabled());
  219. }
  220. /// @brief Checks passing the D2ClientMgr a valid D2 client configuration.
  221. /// @todo Once NameChangeSender is integrated, this test needs to expand, and
  222. /// additional scenario tests will need to be written.
  223. TEST(D2ClientMgr, validConfig) {
  224. D2ClientMgrPtr d2_client_mgr;
  225. // Construct the manager and fetch its initial configuration.
  226. ASSERT_NO_THROW(d2_client_mgr.reset(new D2ClientMgr()));
  227. D2ClientConfigPtr original_config = d2_client_mgr->getD2ClientConfig();
  228. ASSERT_TRUE(original_config);
  229. // Verify that we cannot set the config to an empty pointer.
  230. D2ClientConfigPtr new_cfg;
  231. ASSERT_THROW(d2_client_mgr->setD2ClientConfig(new_cfg), D2ClientError);
  232. // Create a new, enabled config.
  233. ASSERT_NO_THROW(new_cfg.reset(new D2ClientConfig(true,
  234. isc::asiolink::IOAddress("127.0.0.1"), 477,
  235. dhcp_ddns::NCR_UDP, dhcp_ddns::FMT_JSON,
  236. true, true, true, true, true,
  237. "pre-fix", "suf-fix")));
  238. // Verify that we can assign a new, non-empty configuration.
  239. ASSERT_NO_THROW(d2_client_mgr->setD2ClientConfig(new_cfg));
  240. // Verify that we can fetch the newly assigned configuration.
  241. D2ClientConfigPtr updated_config = d2_client_mgr->getD2ClientConfig();
  242. ASSERT_TRUE(updated_config);
  243. EXPECT_TRUE(updated_config->getEnableUpdates());
  244. // Make sure convenience method agrees with the updated configuration.
  245. EXPECT_TRUE(d2_client_mgr->ddnsEnabled());
  246. // Make sure the configuration we fetched is the one we assigned,
  247. // and not the original configuration.
  248. EXPECT_EQ(*new_cfg, *updated_config);
  249. EXPECT_NE(*original_config, *updated_config);
  250. }
  251. } // end of anonymous namespace