ctrl_dhcp6_srv_unittest.cc 25 KB

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