d2_udp_unittest.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. // Copyright (C) 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. /// @file d2_upd_unittest.cc Unit tests for D2ClientMgr UDP communications.
  15. /// Note these tests are not intended to verify the actual send and receive
  16. /// across UDP sockets. This level of testing is done in libdhcp-ddns.
  17. #include <asio.hpp>
  18. #include <asiolink/io_service.h>
  19. #include <config.h>
  20. #include <dhcp/iface_mgr.h>
  21. #include <dhcpsrv/d2_client_mgr.h>
  22. #include <exceptions/exceptions.h>
  23. #include <boost/function.hpp>
  24. #include <boost/bind.hpp>
  25. #include <gtest/gtest.h>
  26. #include <sys/select.h>
  27. using namespace std;
  28. using namespace isc::dhcp;
  29. using namespace isc;
  30. namespace {
  31. /// @brief Test fixture for excerising D2ClientMgr send management
  32. /// services. It inherents from D2ClientMgr to allow overriding various
  33. /// methods and accessing otherwise restricted member. In particular it
  34. /// overrides the NameChangeSender completion completion callback, allowing
  35. /// the injection of send errors.
  36. class D2ClientMgrTest : public D2ClientMgr, public ::testing::Test {
  37. public:
  38. /// @brief If true simulates a send which completed with a failed status.
  39. bool simulate_send_failure_;
  40. /// @brief If true causes an exception throw in the client error handler.
  41. bool error_handler_throw_;
  42. /// @brief Tracks the number times the completion handler is called.
  43. int callback_count_;
  44. /// @brief Tracks the number of times the client error handler was called.
  45. int error_handler_count_;
  46. /// @brief Constructor
  47. D2ClientMgrTest() : simulate_send_failure_(false),
  48. error_handler_throw_(false),
  49. callback_count_(0), error_handler_count_(0) {
  50. }
  51. /// @brief virtual Destructor
  52. virtual ~D2ClientMgrTest(){
  53. }
  54. /// @brief Updates the D2ClientMgr's configuration to DDNS disabled.
  55. void disableDdns() {
  56. D2ClientConfigPtr new_cfg;
  57. ASSERT_NO_THROW(new_cfg.reset(new D2ClientConfig()));
  58. ASSERT_NO_THROW(setD2ClientConfig(new_cfg));
  59. ASSERT_FALSE(ddnsEnabled());
  60. }
  61. /// @brief Updates the D2ClientMgr's configuration to DDNS enabled.
  62. ///
  63. /// @param server_address IP address of b10-dhcp-ddns.
  64. /// @param server_port IP port number of b10-dhcp-ddns.
  65. /// @param protocol NCR protocol to use. (Currently only UDP is
  66. /// supported).
  67. void enableDdns(const std::string& server_address,
  68. const size_t server_port,
  69. const dhcp_ddns::NameChangeProtocol protocol) {
  70. // Update the configuration with one that is enabled.
  71. D2ClientConfigPtr new_cfg;
  72. ASSERT_NO_THROW(new_cfg.reset(new D2ClientConfig(true,
  73. isc::asiolink::IOAddress(server_address),
  74. server_port,
  75. protocol, dhcp_ddns::FMT_JSON,
  76. true, true, true, true,
  77. "myhost", ".example.com.")));
  78. ASSERT_NO_THROW(setD2ClientConfig(new_cfg));
  79. ASSERT_TRUE(ddnsEnabled());
  80. }
  81. /// @brief Checks sender's select-fd against an expected state of readiness.
  82. ///
  83. /// Uses select() to determine if the sender's select_fd is marked as
  84. /// ready to read, and compares this against the expected state. The
  85. /// select function is called with a timeout of 0.0 (non blocking).
  86. ///
  87. /// @param expect_ready Expected state of readiness (True if expecting
  88. /// a ready to ready result, false if expecting otherwise).
  89. void selectCheck(bool expect_ready) {
  90. fd_set read_fds;
  91. int maxfd = 0;
  92. FD_ZERO(&read_fds);
  93. int select_fd = -1;
  94. ASSERT_NO_THROW(select_fd = getSelectFd());
  95. FD_SET(select_fd, &read_fds);
  96. maxfd = select_fd;
  97. struct timeval select_timeout;
  98. select_timeout.tv_sec = 0;
  99. select_timeout.tv_usec = 0;
  100. int result = (select(maxfd + 1, &read_fds, NULL, NULL,
  101. &select_timeout));
  102. if (result < 0) {
  103. const char *errstr = strerror(errno);
  104. FAIL() << "select failed :" << errstr;
  105. }
  106. if (expect_ready) {
  107. ASSERT_TRUE(result > 0);
  108. } else {
  109. ASSERT_TRUE(result == 0);
  110. }
  111. }
  112. /// @brief Overrides base class completion callback.
  113. ///
  114. /// This method will be invoked each time a send completes. It allows
  115. /// intervention prior to calling the production implemenation in the
  116. /// base. If simulate_send_failure_ is true, the base call impl will
  117. /// be called with an error status, otherwise it will be called with
  118. /// the result paramater given.
  119. ///
  120. /// @param result Result code of the send operation.
  121. /// @param ncr NameChangeRequest which failed to send.
  122. virtual void operator()(const dhcp_ddns::NameChangeSender::Result result,
  123. dhcp_ddns::NameChangeRequestPtr& ncr) {
  124. ++callback_count_;
  125. if (simulate_send_failure_) {
  126. simulate_send_failure_ = false;
  127. D2ClientMgr::operator()(dhcp_ddns::NameChangeSender::ERROR, ncr);
  128. } else {
  129. D2ClientMgr::operator()(result, ncr);
  130. }
  131. }
  132. /// @brief Serves as the "application level" client error handler.
  133. ///
  134. /// This method is passed into calls to startSender as the client error
  135. /// handler. It should be invoked whenever the completion callback is
  136. /// passed a result other than SUCCESS. If error_handler_throw_
  137. /// is true it will throw an exception.
  138. ///
  139. /// @param result unused - Result code of the send operation.
  140. /// @param ncr unused -NameChangeRequest which failed to send.
  141. void error_handler(const dhcp_ddns::NameChangeSender::Result /*result*/,
  142. dhcp_ddns::NameChangeRequestPtr& /*ncr*/) {
  143. if (error_handler_throw_) {
  144. error_handler_throw_ = false;
  145. isc_throw(isc::InvalidOperation, "Simulated client handler throw");
  146. }
  147. ++error_handler_count_;
  148. }
  149. /// @brief Returns D2ClientErroHandler bound to this::error_handler_.
  150. D2ClientErrorHandler getErrorHandler() {
  151. return (boost::bind(&D2ClientMgrTest::error_handler, this, _1, _2));
  152. }
  153. /// @brief Contructs a NameChangeRequest message from a fixed JSON string.
  154. dhcp_ddns::NameChangeRequestPtr buildTestNcr() {
  155. // Build an NCR from json string.
  156. const char* ncr_str =
  157. "{"
  158. " \"change_type\" : 0 , "
  159. " \"forward_change\" : true , "
  160. " \"reverse_change\" : false , "
  161. " \"fqdn\" : \"myhost.example.com.\" , "
  162. " \"ip_address\" : \"192.168.2.1\" , "
  163. " \"dhcid\" : \"010203040A7F8E3D\" , "
  164. " \"lease_expires_on\" : \"20140121132405\" , "
  165. " \"lease_length\" : 1300 "
  166. "}";
  167. return (dhcp_ddns::NameChangeRequest::fromJSON(ncr_str));
  168. }
  169. /// Expose restricted members.
  170. using D2ClientMgr::getSelectFd;
  171. };
  172. /// @brief Checks that D2ClientMgr disable and enable a UDP sender.
  173. TEST_F(D2ClientMgrTest, udpSenderEnableDisable) {
  174. // Verify DDNS is disabled by default.
  175. ASSERT_FALSE(ddnsEnabled());
  176. // Verify we are not in send mode.
  177. ASSERT_FALSE(amSending());
  178. // Enable DDNS with server at 127.0.0.1/prot 53001 via UDP.
  179. enableDdns("127.0.0.1", 530001, dhcp_ddns::NCR_UDP);
  180. ASSERT_FALSE(amSending());
  181. ASSERT_NO_THROW(startSender(getErrorHandler()));
  182. ASSERT_TRUE(amSending());
  183. // Verify that we take sender out of send mode.
  184. ASSERT_NO_THROW(stopSender());
  185. ASSERT_FALSE(amSending());
  186. }
  187. /// @brief Checks D2ClientMgr queuing methods with a UDP sender.
  188. TEST_F(D2ClientMgrTest, udpSenderQueing) {
  189. // Enable DDNS with server at 127.0.0.1/prot 53001 via UDP.
  190. enableDdns("127.0.0.1", 530001, dhcp_ddns::NCR_UDP);
  191. ASSERT_FALSE(amSending());
  192. // Queue should be empty.
  193. EXPECT_EQ(0, getQueueSize());
  194. // Trying to peek past the end of the queue should throw.
  195. EXPECT_THROW(peekAt(1), dhcp_ddns::NcrSenderError);
  196. // Trying to send a NCR when not in send mode should fail.
  197. dhcp_ddns::NameChangeRequestPtr ncr = buildTestNcr();
  198. EXPECT_THROW(sendRequest(ncr), dhcp_ddns::NcrSenderError);
  199. // Place sender in send mode.
  200. ASSERT_NO_THROW(startSender(getErrorHandler()));
  201. ASSERT_TRUE(amSending());
  202. // Send should succeed now.
  203. ASSERT_NO_THROW(sendRequest(ncr));
  204. // Queue should have 1 entry.
  205. EXPECT_EQ(1, getQueueSize());
  206. // Attempt to fetch the entry we just queued.
  207. dhcp_ddns::NameChangeRequestPtr ncr2;
  208. ASSERT_NO_THROW(ncr2 = peekAt(0));
  209. // Verify what we queued matches what we fetched.
  210. EXPECT_TRUE(*ncr == *ncr2);
  211. // Clearing the queue while in send mode should fail.
  212. ASSERT_THROW(clearQueue(), dhcp_ddns::NcrSenderError);
  213. // We should still have 1 in the queue.
  214. EXPECT_EQ(1, getQueueSize());
  215. // Get out of send mode.
  216. ASSERT_NO_THROW(stopSender());
  217. ASSERT_FALSE(amSending());
  218. // Clear queue should succeed now.
  219. ASSERT_NO_THROW(clearQueue());
  220. EXPECT_EQ(0, getQueueSize());
  221. }
  222. /// @brief Checks that D2ClientMgr can send with a UDP sender and
  223. /// a private IOService.
  224. TEST_F(D2ClientMgrTest, udpSend) {
  225. // Enable DDNS with server at 127.0.0.1/prot 53001 via UDP.
  226. enableDdns("127.0.0.1", 530001, dhcp_ddns::NCR_UDP);
  227. // Trying to fetch the select-fd when not sending should fail.
  228. ASSERT_THROW(getSelectFd(), D2ClientError);
  229. // Place sender in send mode.
  230. ASSERT_NO_THROW(startSender(getErrorHandler()));
  231. // select_fd should evaluate to NOT ready to read.
  232. selectCheck(false);
  233. // Build a test request and send it.
  234. dhcp_ddns::NameChangeRequestPtr ncr = buildTestNcr();
  235. ASSERT_NO_THROW(sendRequest(ncr));
  236. // select_fd should evaluate to ready to read.
  237. selectCheck(true);
  238. // Call service handler.
  239. runReadyIO();
  240. // select_fd should evaluate to not ready to read.
  241. selectCheck(false);
  242. }
  243. /// @brief Checks that D2ClientMgr can send with a UDP sender and
  244. /// an external IOService.
  245. TEST_F(D2ClientMgrTest, udpSendExternalIOService) {
  246. // Enable DDNS with server at 127.0.0.1/prot 53001 via UDP.
  247. enableDdns("127.0.0.1", 530001, dhcp_ddns::NCR_UDP);
  248. // Place sender in send mode using an external IO service.
  249. asiolink::IOService io_service;
  250. ASSERT_NO_THROW(startSender(getErrorHandler(), io_service));
  251. // select_fd should evaluate to NOT ready to read.
  252. selectCheck(false);
  253. // Build a test request and send it.
  254. dhcp_ddns::NameChangeRequestPtr ncr = buildTestNcr();
  255. ASSERT_NO_THROW(sendRequest(ncr));
  256. // select_fd should evaluate to ready to read.
  257. selectCheck(true);
  258. // Call service handler.
  259. runReadyIO();
  260. // select_fd should evaluate to not ready to read.
  261. selectCheck(false);
  262. }
  263. /// @brief Checks that D2ClientMgr invokes the client error handler
  264. /// when send errors occur.
  265. TEST_F(D2ClientMgrTest, udpSendErrorHandler) {
  266. // Enable DDNS with server at 127.0.0.1/prot 53001 via UDP.
  267. enableDdns("127.0.0.1", 530001, dhcp_ddns::NCR_UDP);
  268. // Trying to fetch the select-fd when not sending should fail.
  269. ASSERT_THROW(getSelectFd(), D2ClientError);
  270. // Place sender in send mode.
  271. ASSERT_NO_THROW(startSender(getErrorHandler()));
  272. // select_fd should evaluate to NOT ready to read.
  273. selectCheck(false);
  274. // Simulate a failed response in the send call back. This should
  275. // cause the error handler to get invoked.
  276. simulate_send_failure_ = true;
  277. ASSERT_EQ(0, error_handler_count_);
  278. // Send a test request.
  279. dhcp_ddns::NameChangeRequestPtr ncr = buildTestNcr();
  280. ASSERT_NO_THROW(sendRequest(ncr));
  281. // select_fd should evaluate to ready to read.
  282. selectCheck(true);
  283. // Call service handler.
  284. runReadyIO();
  285. // select_fd should evaluate to not ready to read.
  286. selectCheck(false);
  287. ASSERT_EQ(1, error_handler_count_);
  288. // Simulate a failed response in the send call back. This should
  289. // cause the error handler to get invoked.
  290. simulate_send_failure_ = true;
  291. error_handler_throw_ = true;
  292. // Send a test request.
  293. ncr = buildTestNcr();
  294. ASSERT_NO_THROW(sendRequest(ncr));
  295. // Call the io service handler.
  296. runReadyIO();
  297. // Simulation flag should be false.
  298. ASSERT_FALSE(error_handler_throw_);
  299. // Count should still be 1.
  300. ASSERT_EQ(1, error_handler_count_);
  301. }
  302. TEST_F(D2ClientMgrTest, ifaceRegister) {
  303. // Enable DDNS with server at 127.0.0.1/prot 53001 via UDP.
  304. enableDdns("127.0.0.1", 530001, dhcp_ddns::NCR_UDP);
  305. // Place sender in send mode.
  306. ASSERT_NO_THROW(startSender(getErrorHandler()));
  307. // Queue three messages.
  308. for (int i = 0; i < 3; ++i) {
  309. dhcp_ddns::NameChangeRequestPtr ncr = buildTestNcr();
  310. ASSERT_NO_THROW(sendRequest(ncr));
  311. }
  312. EXPECT_EQ(3, getQueueSize());
  313. // select_fd should evaluate to ready to read.
  314. selectCheck(true);
  315. // Calling receive should complete the first message and start the second.
  316. IfaceMgr::instance().receive4(0, 0);
  317. // Verify the callback hander was invoked, no errors counted.
  318. EXPECT_EQ(2, getQueueSize());
  319. ASSERT_EQ(1, callback_count_);
  320. ASSERT_EQ(0, error_handler_count_);
  321. // Stop the sender. This should complete the second message but leave
  322. // the third in the queue.
  323. ASSERT_NO_THROW(stopSender());
  324. EXPECT_EQ(1, getQueueSize());
  325. ASSERT_EQ(2, callback_count_);
  326. ASSERT_EQ(0, error_handler_count_);
  327. // Calling recevie again should have no affect.
  328. IfaceMgr::instance().receive4(0, 0);
  329. EXPECT_EQ(1, getQueueSize());
  330. ASSERT_EQ(2, callback_count_);
  331. ASSERT_EQ(0, error_handler_count_);
  332. }
  333. } // end of anonymous namespace