ctrl_dhcp6_srv_unittest.cc 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. // Copyright (C) 2012-2017 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #include <config.h>
  7. #include <asiolink/io_address.h>
  8. #include <cc/command_interpreter.h>
  9. #include <config/command_mgr.h>
  10. #include <dhcpsrv/cfgmgr.h>
  11. #include <dhcpsrv/lease.h>
  12. #include <dhcpsrv/lease_mgr_factory.h>
  13. #include <dhcp6/ctrl_dhcp6_srv.h>
  14. #include <dhcp6/tests/dhcp6_test_utils.h>
  15. #include <hooks/hooks_manager.h>
  16. #include <log/logger_support.h>
  17. #include <stats/stats_mgr.h>
  18. #include <testutils/unix_control_client.h>
  19. #include <testutils/io_utils.h>
  20. #include "marker_file.h"
  21. #include "test_libraries.h"
  22. #include <boost/scoped_ptr.hpp>
  23. #include <gtest/gtest.h>
  24. #include <sys/select.h>
  25. #include <sys/stat.h>
  26. #include <sys/ioctl.h>
  27. #include <cstdlib>
  28. using namespace std;
  29. using namespace isc::asiolink;
  30. using namespace isc::config;
  31. using namespace isc::data;
  32. using namespace isc::dhcp;
  33. using namespace isc::dhcp::test;
  34. using namespace isc::hooks;
  35. using namespace isc::stats;
  36. namespace {
  37. class NakedControlledDhcpv6Srv: public ControlledDhcpv6Srv {
  38. // "Naked" DHCPv6 server, exposes internal fields
  39. public:
  40. NakedControlledDhcpv6Srv():ControlledDhcpv6Srv(DHCP6_SERVER_PORT + 10000) {
  41. }
  42. /// Expose internal methods for the sake of testing
  43. using Dhcpv6Srv::receivePacket;
  44. };
  45. class CtrlDhcpv6SrvTest : public BaseServerTest {
  46. public:
  47. CtrlDhcpv6SrvTest()
  48. : BaseServerTest() {
  49. reset();
  50. }
  51. virtual ~CtrlDhcpv6SrvTest() {
  52. LeaseMgrFactory::destroy();
  53. StatsMgr::instance().removeAll();
  54. reset();
  55. };
  56. /// @brief Reset hooks data
  57. ///
  58. /// Resets the data for the hooks-related portion of the test by ensuring
  59. /// that no libraries are loaded and that any marker files are deleted.
  60. virtual void reset() {
  61. // Unload any previously-loaded libraries.
  62. HooksManager::unloadLibraries();
  63. // Get rid of any marker files.
  64. static_cast<void>(remove(LOAD_MARKER_FILE));
  65. static_cast<void>(remove(UNLOAD_MARKER_FILE));
  66. IfaceMgr::instance().deleteAllExternalSockets();
  67. CfgMgr::instance().clear();
  68. }
  69. };
  70. class CtrlChannelDhcpv6SrvTest : public CtrlDhcpv6SrvTest {
  71. public:
  72. /// @brief Path to the UNIX socket being used to communicate with the server
  73. std::string socket_path_;
  74. /// @brief Pointer to the tested server object
  75. boost::shared_ptr<NakedControlledDhcpv6Srv> server_;
  76. /// @brief Default constructor
  77. ///
  78. /// Sets socket path to its default value.
  79. CtrlChannelDhcpv6SrvTest() {
  80. const char* env = getenv("KEA_SOCKET_TEST_DIR");
  81. if (env) {
  82. socket_path_ = string(env) + "/kea6.sock";
  83. } else {
  84. socket_path_ = string(TEST_DATA_BUILDDIR) + "/kea6.sock";
  85. }
  86. reset();
  87. }
  88. /// @brief Destructor
  89. ~CtrlChannelDhcpv6SrvTest() {
  90. server_.reset();
  91. reset();
  92. };
  93. void createUnixChannelServer() {
  94. static_cast<void>(::remove(socket_path_.c_str()));
  95. // Just a simple config. The important part here is the socket
  96. // location information.
  97. std::string header =
  98. "{"
  99. " \"interfaces-config\": {"
  100. " \"interfaces\": [ \"*\" ]"
  101. " },"
  102. " \"expired-leases-processing\": {"
  103. " \"reclaim-timer-wait-time\": 60,"
  104. " \"hold-reclaimed-time\": 500,"
  105. " \"flush-reclaimed-timer-wait-time\": 60"
  106. " },"
  107. " \"rebind-timer\": 2000, "
  108. " \"renew-timer\": 1000, "
  109. " \"subnet6\": [ ],"
  110. " \"valid-lifetime\": 4000,"
  111. " \"control-socket\": {"
  112. " \"socket-type\": \"unix\","
  113. " \"socket-name\": \"";
  114. std::string footer =
  115. "\" },"
  116. " \"lease-database\": {"
  117. " \"type\": \"memfile\", \"persist\": false }"
  118. "}";
  119. // Fill in the socket-name value with socket_path_ to
  120. // make the actual configuration text.
  121. std::string config_txt = header + socket_path_ + footer;
  122. ASSERT_NO_THROW(server_.reset(new NakedControlledDhcpv6Srv()));
  123. ConstElementPtr config;
  124. ASSERT_NO_THROW(config = parseDHCP6(config_txt));
  125. ConstElementPtr answer = server_->processConfig(config);
  126. // Commit the configuration so any subsequent reconfigurations
  127. // will only close the command channel if its configuration has
  128. // changed.
  129. CfgMgr::instance().commit();
  130. ASSERT_TRUE(answer);
  131. int status = 0;
  132. ConstElementPtr txt = isc::config::parseAnswer(status, answer);
  133. // This should succeed. If not, print the error message.
  134. ASSERT_EQ(0, status) << txt->str();
  135. // Now check that the socket was indeed open.
  136. ASSERT_GT(isc::config::CommandMgr::instance().getControlSocketFD(), -1);
  137. }
  138. /// @brief Reset
  139. void reset() {
  140. CtrlDhcpv6SrvTest::reset();
  141. static_cast<void>(::remove(socket_path_.c_str()));
  142. }
  143. /// @brief Conducts a command/response exchange via UnixCommandSocket
  144. ///
  145. /// This method connects to the given server over the given socket path.
  146. /// If successful, it then sends the given command and retrieves the
  147. /// server's response. Note that it calls the server's receivePacket()
  148. /// method where needed to cause the server to process IO events on
  149. /// control channel the control channel sockets.
  150. ///
  151. /// @param command the command text to execute in JSON form
  152. /// @param response variable into which the received response should be
  153. /// placed.
  154. void sendUnixCommand(const std::string& command, std::string& response) {
  155. response = "";
  156. boost::scoped_ptr<UnixControlClient> client;
  157. client.reset(new UnixControlClient());
  158. ASSERT_TRUE(client);
  159. // Connect and then call server's receivePacket() so it can
  160. // detect the control socket connect and call the accept handler
  161. ASSERT_TRUE(client->connectToServer(socket_path_));
  162. ASSERT_NO_THROW(server_->receivePacket(0));
  163. // Send the command and then call server's receivePacket() so it can
  164. // detect the inbound data and call the read handler
  165. ASSERT_TRUE(client->sendCommand(command));
  166. ASSERT_NO_THROW(server_->receivePacket(0));
  167. // Read the response generated by the server. Note that getResponse
  168. // only fails if there an IO error or no response data was present.
  169. // It is not based on the response content.
  170. ASSERT_TRUE(client->getResponse(response));
  171. // Now disconnect and process the close event
  172. client->disconnectFromServer();
  173. ASSERT_NO_THROW(server_->receivePacket(0));
  174. }
  175. };
  176. TEST_F(CtrlDhcpv6SrvTest, commands) {
  177. boost::scoped_ptr<ControlledDhcpv6Srv> srv;
  178. ASSERT_NO_THROW(
  179. srv.reset(new ControlledDhcpv6Srv(DHCP6_SERVER_PORT + 10000))
  180. );
  181. // Use empty parameters list
  182. ElementPtr params(new isc::data::MapElement());
  183. int rcode = -1;
  184. // Case 1: send bogus command
  185. ConstElementPtr result = ControlledDhcpv6Srv::processCommand("blah", params);
  186. ConstElementPtr comment = isc::config::parseAnswer(rcode, result);
  187. EXPECT_EQ(1, rcode); // expect failure (no such command as blah)
  188. // Case 2: send shutdown command without any parameters
  189. result = ControlledDhcpv6Srv::processCommand("shutdown", params);
  190. comment = isc::config::parseAnswer(rcode, result);
  191. EXPECT_EQ(0, rcode); // expect success
  192. const pid_t pid(getpid());
  193. ConstElementPtr x(new isc::data::IntElement(pid));
  194. params->set("pid", x);
  195. // Case 3: send shutdown command with 1 parameter: pid
  196. result = ControlledDhcpv6Srv::processCommand("shutdown", params);
  197. comment = isc::config::parseAnswer(rcode, result);
  198. EXPECT_EQ(0, rcode); // Expect success
  199. }
  200. // Check that the "libreload" command will reload libraries
  201. TEST_F(CtrlChannelDhcpv6SrvTest, libreload) {
  202. createUnixChannelServer();
  203. // Ensure no marker files to start with.
  204. ASSERT_FALSE(checkMarkerFileExists(LOAD_MARKER_FILE));
  205. ASSERT_FALSE(checkMarkerFileExists(UNLOAD_MARKER_FILE));
  206. // Load two libraries
  207. HookLibsCollection libraries;
  208. libraries.push_back(make_pair(CALLOUT_LIBRARY_1, ConstElementPtr()));
  209. libraries.push_back(make_pair(CALLOUT_LIBRARY_2, ConstElementPtr()));
  210. HooksManager::loadLibraries(libraries);
  211. // Check they are loaded.
  212. HookLibsCollection loaded_libraries =
  213. HooksManager::getLibraryInfo();
  214. ASSERT_TRUE(libraries == loaded_libraries);
  215. // ... which also included checking that the marker file created by the
  216. // load functions exists and holds the correct value (of "12" - the
  217. // first library appends "1" to the file, the second appends "2"). Also
  218. // check that the unload marker file does not yet exist.
  219. EXPECT_TRUE(checkMarkerFile(LOAD_MARKER_FILE, "12"));
  220. EXPECT_FALSE(checkMarkerFileExists(UNLOAD_MARKER_FILE));
  221. // Now execute the "libreload" command. This should cause the libraries
  222. // to unload and to reload.
  223. std::string response;
  224. sendUnixCommand("{ \"command\": \"libreload\" }", response);
  225. EXPECT_EQ("{ \"result\": 0, "
  226. "\"text\": \"Hooks libraries successfully reloaded.\" }"
  227. , response);
  228. // Check that the libraries have unloaded and reloaded. The libraries are
  229. // unloaded in the reverse order to which they are loaded. When they load,
  230. // they should append information to the loading marker file.
  231. EXPECT_TRUE(checkMarkerFile(UNLOAD_MARKER_FILE, "21"));
  232. EXPECT_TRUE(checkMarkerFile(LOAD_MARKER_FILE, "1212"));
  233. }
  234. // Check that the "configReload" command will reload libraries
  235. TEST_F(CtrlDhcpv6SrvTest, configReload) {
  236. // Sending commands for processing now requires a server that can process
  237. // them.
  238. boost::scoped_ptr<ControlledDhcpv6Srv> srv;
  239. ASSERT_NO_THROW(
  240. srv.reset(new ControlledDhcpv6Srv(0))
  241. );
  242. // Now execute the "config-reload" command. This should cause the libraries
  243. // to unload and to reload.
  244. // Use empty parameters list
  245. // Prepare configuration file.
  246. string config_txt = "{ \"Dhcp6\": { \"interfaces-config\": {"
  247. " \"interfaces\": [ \"*\" ]"
  248. "},"
  249. "\"preferred-lifetime\": 3000,"
  250. "\"rebind-timer\": 2000, "
  251. "\"renew-timer\": 1000, "
  252. "\"subnet6\": [ { "
  253. " \"pools\": [ { \"pool\": \"2001:db8:1::/80\" } ],"
  254. " \"subnet\": \"2001:db8:1::/64\" "
  255. " },"
  256. " {"
  257. " \"pools\": [ { \"pool\": \"2001:db8:2::/80\" } ],"
  258. " \"subnet\": \"2001:db8:2::/64\", "
  259. " \"id\": 0"
  260. " },"
  261. " {"
  262. " \"pools\": [ { \"pool\": \"2001:db8:3::/80\" } ],"
  263. " \"subnet\": \"2001:db8:3::/64\" "
  264. " } ],"
  265. "\"valid-lifetime\": 4000 }}";
  266. ConstElementPtr config;
  267. ASSERT_NO_THROW(config = parseJSON(config_txt));
  268. // Make sure there are no subnets configured.
  269. CfgMgr::instance().clear();
  270. // Now send the command
  271. int rcode = -1;
  272. ConstElementPtr result =
  273. ControlledDhcpv6Srv::processCommand("config-reload", config);
  274. ConstElementPtr comment = isc::config::parseAnswer(rcode, result);
  275. EXPECT_EQ(0, rcode); // Expect success
  276. // Check that the config was indeed applied.
  277. const Subnet6Collection* subnets =
  278. CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  279. EXPECT_EQ(3, subnets->size());
  280. // Clean up after the test.
  281. CfgMgr::instance().clear();
  282. }
  283. // Check that the "set-config" command will replace current configuration
  284. TEST_F(CtrlChannelDhcpv6SrvTest, set_config) {
  285. createUnixChannelServer();
  286. // Define strings to permutate the config arguments
  287. // (Note the line feeds makes errors easy to find)
  288. string set_config_txt = "{ \"command\": \"set-config\" \n";
  289. string args_txt = " \"arguments\": { \n";
  290. string dhcp6_cfg_txt =
  291. " \"Dhcp6\": { \n"
  292. " \"interfaces-config\": { \n"
  293. " \"interfaces\": [\"*\"] \n"
  294. " }, \n"
  295. " \"preferred-lifetime\": 3000, \n"
  296. " \"valid-lifetime\": 4000, \n"
  297. " \"renew-timer\": 1000, \n"
  298. " \"rebind-timer\": 2000, \n"
  299. " \"lease-database\": { \n"
  300. " \"type\": \"memfile\", \n"
  301. " \"persist\":false, \n"
  302. " \"lfc-interval\": 0 \n"
  303. " }, \n"
  304. " \"expired-leases-processing\": { \n"
  305. " \"reclaim-timer-wait-time\": 0, \n"
  306. " \"hold-reclaimed-time\": 0, \n"
  307. " \"flush-reclaimed-timer-wait-time\": 0 \n"
  308. " },"
  309. " \"subnet6\": [ \n";
  310. string subnet1 =
  311. " {\"subnet\": \"3002::/64\", \n"
  312. " \"pools\": [{ \"pool\": \"3002::100-3002::200\" }]}\n";
  313. string subnet2 =
  314. " {\"subnet\": \"3003::/64\", \n"
  315. " \"pools\": [{ \"pool\": \"3003::100-3003::200\" }]}\n";
  316. string bad_subnet =
  317. " {\"BOGUS\": \"3005::/64\", \n"
  318. " \"pools\": [{ \"pool\": \"3005::100-3005::200\" }]}\n";
  319. string subnet_footer =
  320. " ] \n";
  321. string control_socket_header =
  322. " ,\"control-socket\": { \n"
  323. " \"socket-type\": \"unix\", \n"
  324. " \"socket-name\": \"";
  325. string control_socket_footer =
  326. "\" \n} \n";
  327. string logger_txt =
  328. " \"Logging\": { \n"
  329. " \"loggers\": [ { \n"
  330. " \"name\": \"kea\", \n"
  331. " \"severity\": \"FATAL\", \n"
  332. " \"output_options\": [{ \n"
  333. " \"output\": \"/dev/null\" \n"
  334. " }] \n"
  335. " }] \n"
  336. " } \n";
  337. std::ostringstream os;
  338. // Create a valid config with all the parts should parse
  339. os << set_config_txt << ","
  340. << args_txt
  341. << dhcp6_cfg_txt
  342. << subnet1
  343. << subnet_footer
  344. << control_socket_header
  345. << socket_path_
  346. << control_socket_footer
  347. << "}\n" // close dhcp6
  348. << ","
  349. << logger_txt
  350. << "}}";
  351. // Send the set-config command
  352. std::string response;
  353. sendUnixCommand(os.str(), response);
  354. // Verify the configuration was successful.
  355. EXPECT_EQ("{ \"result\": 0, \"text\": \"Configuration successful.\" }",
  356. response);
  357. // Check that the config was indeed applied.
  358. const Subnet6Collection* subnets =
  359. CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  360. EXPECT_EQ(1, subnets->size());
  361. // Create a config with malformed subnet that should fail to parse.
  362. os.str("");
  363. os << set_config_txt << ","
  364. << args_txt
  365. << dhcp6_cfg_txt
  366. << bad_subnet
  367. << subnet_footer
  368. << control_socket_header
  369. << socket_path_
  370. << control_socket_footer
  371. << "}\n" // close dhcp6
  372. "}}";
  373. // Send the set-config command
  374. sendUnixCommand(os.str(), response);
  375. // Should fail with a syntax error
  376. EXPECT_EQ("{ \"result\": 1, "
  377. "\"text\": \"unsupported parameter: BOGUS (<string>:21:26)\" }",
  378. response);
  379. // Check that the config was not lost
  380. subnets = CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  381. EXPECT_EQ(1, subnets->size());
  382. // Create a valid config with two subnets and no command channel.
  383. // It should succeed but client will not receive a the response
  384. os.str("");
  385. os << set_config_txt << ","
  386. << args_txt
  387. << dhcp6_cfg_txt
  388. << subnet1
  389. << ",\n"
  390. << subnet2
  391. << subnet_footer
  392. << "}\n" // close dhcp6
  393. << "}}";
  394. /* Verify the control channel socket exists */
  395. ASSERT_TRUE(fileExists(socket_path_));
  396. // Send the set-config command
  397. sendUnixCommand(os.str(), response);
  398. /* Verify the control channel socket no longer exists */
  399. EXPECT_FALSE(fileExists(socket_path_));
  400. // With no command channel, should still receive the response.
  401. EXPECT_EQ("{ \"result\": 0, \"text\": \"Configuration successful.\" }",
  402. response);
  403. // Check that the config was not lost
  404. subnets = CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  405. EXPECT_EQ(2, subnets->size());
  406. // Clean up after the test.
  407. CfgMgr::instance().clear();
  408. }
  409. typedef std::map<std::string, isc::data::ConstElementPtr> ElementMap;
  410. // This test checks which commands are registered by the DHCPv4 server.
  411. TEST_F(CtrlDhcpv6SrvTest, commandsRegistration) {
  412. ConstElementPtr list_cmds = createCommand("list-commands");
  413. ConstElementPtr answer;
  414. // By default the list should be empty (except the standard list-commands
  415. // supported by the CommandMgr itself)
  416. EXPECT_NO_THROW(answer = CommandMgr::instance().processCommand(list_cmds));
  417. ASSERT_TRUE(answer);
  418. ASSERT_TRUE(answer->get("arguments"));
  419. EXPECT_EQ("[ \"list-commands\" ]", answer->get("arguments")->str());
  420. // Created server should register several additional commands.
  421. boost::scoped_ptr<ControlledDhcpv6Srv> srv;
  422. ASSERT_NO_THROW(
  423. srv.reset(new ControlledDhcpv6Srv(0));
  424. );
  425. EXPECT_NO_THROW(answer = CommandMgr::instance().processCommand(list_cmds));
  426. ASSERT_TRUE(answer);
  427. ASSERT_TRUE(answer->get("arguments"));
  428. std::string command_list = answer->get("arguments")->str();
  429. EXPECT_TRUE(command_list.find("\"list-commands\"") != string::npos);
  430. EXPECT_TRUE(command_list.find("\"statistic-get\"") != string::npos);
  431. EXPECT_TRUE(command_list.find("\"statistic-get-all\"") != string::npos);
  432. EXPECT_TRUE(command_list.find("\"statistic-remove\"") != string::npos);
  433. EXPECT_TRUE(command_list.find("\"statistic-remove-all\"") != string::npos);
  434. EXPECT_TRUE(command_list.find("\"statistic-reset\"") != string::npos);
  435. EXPECT_TRUE(command_list.find("\"statistic-reset-all\"") != string::npos);
  436. // Ok, and now delete the server. It should deregister its commands.
  437. srv.reset();
  438. // The list should be (almost) empty again.
  439. EXPECT_NO_THROW(answer = CommandMgr::instance().processCommand(list_cmds));
  440. ASSERT_TRUE(answer);
  441. ASSERT_TRUE(answer->get("arguments"));
  442. EXPECT_EQ("[ \"list-commands\" ]", answer->get("arguments")->str());
  443. }
  444. // Tests that the server properly responds to invalid commands sent
  445. // via ControlChannel
  446. TEST_F(CtrlChannelDhcpv6SrvTest, controlChannelNegative) {
  447. createUnixChannelServer();
  448. std::string response;
  449. sendUnixCommand("{ \"command\": \"bogus\" }", response);
  450. EXPECT_EQ("{ \"result\": 1,"
  451. " \"text\": \"'bogus' command not supported.\" }", response);
  452. sendUnixCommand("utter nonsense", response);
  453. EXPECT_EQ("{ \"result\": 1, "
  454. "\"text\": \"error: unexpected character u in <string>:1:2\" }",
  455. response);
  456. }
  457. // Tests that the server properly responds to shtudown command sent
  458. // via ControlChannel
  459. TEST_F(CtrlChannelDhcpv6SrvTest, controlChannelShutdown) {
  460. createUnixChannelServer();
  461. std::string response;
  462. sendUnixCommand("{ \"command\": \"shutdown\" }", response);
  463. EXPECT_EQ("{ \"result\": 0, \"text\": \"Shutting down.\" }",response);
  464. }
  465. // This test verifies that the DHCP server immediately reclaims expired
  466. // leases on leases-reclaim command
  467. TEST_F(CtrlChannelDhcpv6SrvTest, controlLeasesReclaim) {
  468. createUnixChannelServer();
  469. // Create expired leases. Leases are expired by 40 seconds ago
  470. // (valid lifetime = 60, cltt = now - 100).
  471. DuidPtr duid0(new DUID(DUID::fromText("00:01:02:03:04:05:06").getDuid()));
  472. Lease6Ptr lease0(new Lease6(Lease::TYPE_NA, IOAddress("3000::1"),
  473. duid0, 1, 50, 60, 10, 20, SubnetID(1)));
  474. lease0->cltt_ = time(NULL) - 100;
  475. DuidPtr duid1(new DUID(DUID::fromText("01:02:03:04:05:06:07").getDuid()));
  476. Lease6Ptr lease1(new Lease6(Lease::TYPE_NA, IOAddress("3000::2"),
  477. duid1, 1, 50, 60, 10, 20, SubnetID(1)));
  478. lease1->cltt_ = time(NULL) - 100;
  479. // Add leases to the database.
  480. LeaseMgr& lease_mgr = LeaseMgrFactory::instance();
  481. ASSERT_NO_THROW(lease_mgr.addLease(lease0));
  482. ASSERT_NO_THROW(lease_mgr.addLease(lease1));
  483. // Make sure they have been added.
  484. ASSERT_TRUE(lease_mgr.getLease6(Lease::TYPE_NA, IOAddress("3000::1")));
  485. ASSERT_TRUE(lease_mgr.getLease6(Lease::TYPE_NA, IOAddress("3000::2")));
  486. // No arguments
  487. std::string response;
  488. sendUnixCommand("{ \"command\": \"leases-reclaim\" }", response);
  489. EXPECT_EQ("{ \"result\": 1, \"text\": "
  490. "\"Missing mandatory 'remove' parameter.\" }", response);
  491. // Bad argument name
  492. sendUnixCommand("{ \"command\": \"leases-reclaim\", "
  493. "\"arguments\": { \"reclaim\": true } }", response);
  494. EXPECT_EQ("{ \"result\": 1, \"text\": "
  495. "\"Missing mandatory 'remove' parameter.\" }", response);
  496. // Bad remove argument type
  497. sendUnixCommand("{ \"command\": \"leases-reclaim\", "
  498. "\"arguments\": { \"remove\": \"bogus\" } }", response);
  499. EXPECT_EQ("{ \"result\": 1, \"text\": "
  500. "\"'remove' parameter expected to be a boolean.\" }", response);
  501. // Send the command
  502. sendUnixCommand("{ \"command\": \"leases-reclaim\", "
  503. "\"arguments\": { \"remove\": false } }", response);
  504. EXPECT_EQ("{ \"result\": 0, \"text\": "
  505. "\"Reclamation of expired leases is complete.\" }", response);
  506. // Leases should be reclaimed, but not removed
  507. ASSERT_NO_THROW(
  508. lease0 = lease_mgr.getLease6(Lease::TYPE_NA, IOAddress("3000::1"))
  509. );
  510. ASSERT_NO_THROW(
  511. lease1 = lease_mgr.getLease6(Lease::TYPE_NA, IOAddress("3000::2"))
  512. );
  513. ASSERT_TRUE(lease0);
  514. ASSERT_TRUE(lease1);
  515. EXPECT_TRUE(lease0->stateExpiredReclaimed());
  516. EXPECT_TRUE(lease1->stateExpiredReclaimed());
  517. }
  518. // This test verifies that the DHCP server immediately reclaims expired
  519. // leases on leases-reclaim command with remove = true
  520. TEST_F(CtrlChannelDhcpv6SrvTest, controlLeasesReclaimRemove) {
  521. createUnixChannelServer();
  522. // Create expired leases. Leases are expired by 40 seconds ago
  523. // (valid lifetime = 60, cltt = now - 100).
  524. DuidPtr duid0(new DUID(DUID::fromText("00:01:02:03:04:05:06").getDuid()));
  525. Lease6Ptr lease0(new Lease6(Lease::TYPE_NA, IOAddress("3000::1"),
  526. duid0, 1, 50, 60, 10, 20, SubnetID(1)));
  527. lease0->cltt_ = time(NULL) - 100;
  528. DuidPtr duid1(new DUID(DUID::fromText("01:02:03:04:05:06:07").getDuid()));
  529. Lease6Ptr lease1(new Lease6(Lease::TYPE_NA, IOAddress("3000::2"),
  530. duid1, 1, 50, 60, 10, 20, SubnetID(1)));
  531. lease1->cltt_ = time(NULL) - 100;
  532. // Add leases to the database.
  533. LeaseMgr& lease_mgr = LeaseMgrFactory::instance();
  534. ASSERT_NO_THROW(lease_mgr.addLease(lease0));
  535. ASSERT_NO_THROW(lease_mgr.addLease(lease1));
  536. // Make sure they have been added.
  537. ASSERT_TRUE(lease_mgr.getLease6(Lease::TYPE_NA, IOAddress("3000::1")));
  538. ASSERT_TRUE(lease_mgr.getLease6(Lease::TYPE_NA, IOAddress("3000::2")));
  539. // Send the command
  540. std::string response;
  541. sendUnixCommand("{ \"command\": \"leases-reclaim\", "
  542. "\"arguments\": { \"remove\": true } }", response);
  543. EXPECT_EQ("{ \"result\": 0, \"text\": "
  544. "\"Reclamation of expired leases is complete.\" }", response);
  545. // Leases should have been removed.
  546. ASSERT_NO_THROW(
  547. lease0 = lease_mgr.getLease6(Lease::TYPE_NA, IOAddress("3000::1"))
  548. );
  549. ASSERT_NO_THROW(
  550. lease1 = lease_mgr.getLease6(Lease::TYPE_NA, IOAddress("3000::2"))
  551. );
  552. ASSERT_FALSE(lease0);
  553. ASSERT_FALSE(lease1);
  554. }
  555. // Tests that the server properly responds to statistics commands. Note this
  556. // is really only intended to verify that the appropriate Statistics handler
  557. // is called based on the command. It is not intended to be an exhaustive
  558. // test of Dhcpv6 statistics.
  559. TEST_F(CtrlChannelDhcpv6SrvTest, controlChannelStats) {
  560. createUnixChannelServer();
  561. std::string response;
  562. // Check statistic-get
  563. sendUnixCommand("{ \"command\" : \"statistic-get\", "
  564. " \"arguments\": {"
  565. " \"name\":\"bogus\" }}", response);
  566. EXPECT_EQ("{ \"arguments\": { }, \"result\": 0 }", response);
  567. // Check statistic-get-all
  568. sendUnixCommand("{ \"command\" : \"statistic-get-all\", "
  569. " \"arguments\": {}}", response);
  570. EXPECT_EQ("{ \"arguments\": { }, \"result\": 0 }", response);
  571. // Check statistic-reset
  572. sendUnixCommand("{ \"command\" : \"statistic-reset\", "
  573. " \"arguments\": {"
  574. " \"name\":\"bogus\" }}", response);
  575. EXPECT_EQ("{ \"result\": 1, \"text\": \"No 'bogus' statistic found\" }",
  576. response);
  577. // Check statistic-reset-all
  578. sendUnixCommand("{ \"command\" : \"statistic-reset-all\", "
  579. " \"arguments\": {}}", response);
  580. EXPECT_EQ("{ \"result\": 0, \"text\": "
  581. "\"All statistics reset to neutral values.\" }", response);
  582. // Check statistic-remove
  583. sendUnixCommand("{ \"command\" : \"statistic-remove\", "
  584. " \"arguments\": {"
  585. " \"name\":\"bogus\" }}", response);
  586. EXPECT_EQ("{ \"result\": 1, \"text\": \"No 'bogus' statistic found\" }",
  587. response);
  588. // Check statistic-remove-all
  589. sendUnixCommand("{ \"command\" : \"statistic-remove-all\", "
  590. " \"arguments\": {}}", response);
  591. EXPECT_EQ("{ \"result\": 0, \"text\": \"All statistics removed.\" }",
  592. response);
  593. }
  594. } // End of anonymous namespace