ctrl_dhcp6_srv_unittest.cc 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  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. using namespace isc::test;
  37. namespace {
  38. class NakedControlledDhcpv6Srv: public ControlledDhcpv6Srv {
  39. // "Naked" DHCPv6 server, exposes internal fields
  40. public:
  41. NakedControlledDhcpv6Srv():ControlledDhcpv6Srv(DHCP6_SERVER_PORT + 10000) {
  42. CfgMgr::instance().setFamily(AF_INET6);
  43. }
  44. /// Expose internal methods for the sake of testing
  45. using Dhcpv6Srv::receivePacket;
  46. };
  47. class CtrlDhcpv6SrvTest : public BaseServerTest {
  48. public:
  49. CtrlDhcpv6SrvTest()
  50. : BaseServerTest() {
  51. reset();
  52. }
  53. virtual ~CtrlDhcpv6SrvTest() {
  54. LeaseMgrFactory::destroy();
  55. StatsMgr::instance().removeAll();
  56. reset();
  57. };
  58. /// @brief Reset hooks data
  59. ///
  60. /// Resets the data for the hooks-related portion of the test by ensuring
  61. /// that no libraries are loaded and that any marker files are deleted.
  62. virtual void reset() {
  63. // Unload any previously-loaded libraries.
  64. HooksManager::unloadLibraries();
  65. // Get rid of any marker files.
  66. static_cast<void>(remove(LOAD_MARKER_FILE));
  67. static_cast<void>(remove(UNLOAD_MARKER_FILE));
  68. IfaceMgr::instance().deleteAllExternalSockets();
  69. CfgMgr::instance().clear();
  70. }
  71. };
  72. class CtrlChannelDhcpv6SrvTest : public CtrlDhcpv6SrvTest {
  73. public:
  74. /// @brief Path to the UNIX socket being used to communicate with the server
  75. std::string socket_path_;
  76. /// @brief Pointer to the tested server object
  77. boost::shared_ptr<NakedControlledDhcpv6Srv> server_;
  78. /// @brief Default constructor
  79. ///
  80. /// Sets socket path to its default value.
  81. CtrlChannelDhcpv6SrvTest() {
  82. const char* env = getenv("KEA_SOCKET_TEST_DIR");
  83. if (env) {
  84. socket_path_ = string(env) + "/kea6.sock";
  85. } else {
  86. socket_path_ = string(TEST_DATA_BUILDDIR) + "/kea6.sock";
  87. }
  88. reset();
  89. }
  90. /// @brief Destructor
  91. ~CtrlChannelDhcpv6SrvTest() {
  92. server_.reset();
  93. reset();
  94. };
  95. /// @brief Returns pointer to the server's IO service.
  96. ///
  97. /// @return Pointer to the server's IO service or null pointer if the server
  98. /// hasn't been created.
  99. IOServicePtr getIOService() {
  100. return (server_ ? server_->getIOService() : IOServicePtr());
  101. }
  102. void createUnixChannelServer() {
  103. static_cast<void>(::remove(socket_path_.c_str()));
  104. // Just a simple config. The important part here is the socket
  105. // location information.
  106. std::string header =
  107. "{"
  108. " \"interfaces-config\": {"
  109. " \"interfaces\": [ \"*\" ]"
  110. " },"
  111. " \"expired-leases-processing\": {"
  112. " \"reclaim-timer-wait-time\": 60,"
  113. " \"hold-reclaimed-time\": 500,"
  114. " \"flush-reclaimed-timer-wait-time\": 60"
  115. " },"
  116. " \"rebind-timer\": 2000, "
  117. " \"renew-timer\": 1000, "
  118. " \"subnet6\": [ ],"
  119. " \"valid-lifetime\": 4000,"
  120. " \"control-socket\": {"
  121. " \"socket-type\": \"unix\","
  122. " \"socket-name\": \"";
  123. std::string footer =
  124. "\" },"
  125. " \"lease-database\": {"
  126. " \"type\": \"memfile\", \"persist\": false }"
  127. "}";
  128. // Fill in the socket-name value with socket_path_ to
  129. // make the actual configuration text.
  130. std::string config_txt = header + socket_path_ + footer;
  131. ASSERT_NO_THROW(server_.reset(new NakedControlledDhcpv6Srv()));
  132. ConstElementPtr config;
  133. ASSERT_NO_THROW(config = parseDHCP6(config_txt));
  134. ConstElementPtr answer = server_->processConfig(config);
  135. // Commit the configuration so any subsequent reconfigurations
  136. // will only close the command channel if its configuration has
  137. // changed.
  138. CfgMgr::instance().commit();
  139. ASSERT_TRUE(answer);
  140. int status = 0;
  141. ConstElementPtr txt = isc::config::parseAnswer(status, answer);
  142. // This should succeed. If not, print the error message.
  143. ASSERT_EQ(0, status) << txt->str();
  144. // Now check that the socket was indeed open.
  145. ASSERT_GT(isc::config::CommandMgr::instance().getControlSocketFD(), -1);
  146. }
  147. /// @brief Reset
  148. void reset() {
  149. CtrlDhcpv6SrvTest::reset();
  150. static_cast<void>(::remove(socket_path_.c_str()));
  151. }
  152. /// @brief Conducts a command/response exchange via UnixCommandSocket
  153. ///
  154. /// This method connects to the given server over the given socket path.
  155. /// If successful, it then sends the given command and retrieves the
  156. /// server's response. Note that it calls the server's receivePacket()
  157. /// method where needed to cause the server to process IO events on
  158. /// control channel the control channel sockets.
  159. ///
  160. /// @param command the command text to execute in JSON form
  161. /// @param response variable into which the received response should be
  162. /// placed.
  163. void sendUnixCommand(const std::string& command, std::string& response) {
  164. response = "";
  165. boost::scoped_ptr<UnixControlClient> client;
  166. client.reset(new UnixControlClient());
  167. ASSERT_TRUE(client);
  168. // Connect to the server. This is expected to trigger server's acceptor
  169. // handler when IOService::poll() is run.
  170. ASSERT_TRUE(client->connectToServer(socket_path_));
  171. ASSERT_NO_THROW(getIOService()->poll());
  172. // Send the command. This will trigger server's handler which receives
  173. // data over the unix domain socket. The server will start sending
  174. // response to the client.
  175. ASSERT_TRUE(client->sendCommand(command));
  176. ASSERT_NO_THROW(getIOService()->poll());
  177. // Read the response generated by the server. Note that getResponse
  178. // only fails if there an IO error or no response data was present.
  179. // It is not based on the response content.
  180. ASSERT_TRUE(client->getResponse(response));
  181. // Now disconnect and process the close event
  182. client->disconnectFromServer();
  183. ASSERT_NO_THROW(getIOService()->poll());
  184. }
  185. /// @brief Checks response for list-commands
  186. ///
  187. /// This method checks if the list-commands response is generally sane
  188. /// and whether specified command is mentioned in the response.
  189. ///
  190. /// @param rsp response sent back by the server
  191. /// @param command command expected to be on the list.
  192. void checkListCommands(const ConstElementPtr& rsp, const std::string& command) {
  193. ConstElementPtr params;
  194. int status_code = -1;
  195. EXPECT_NO_THROW(params = parseAnswer(status_code, rsp));
  196. EXPECT_EQ(CONTROL_RESULT_SUCCESS, status_code);
  197. ASSERT_TRUE(params);
  198. ASSERT_EQ(Element::list, params->getType());
  199. int cnt = 0;
  200. for (size_t i = 0; i < params->size(); ++i) {
  201. string tmp = params->get(i)->stringValue();
  202. if (tmp == command) {
  203. // Command found, but that's not enough. Need to continue working
  204. // through the list to see if there are no duplicates.
  205. cnt++;
  206. }
  207. }
  208. // Exactly one command on the list is expected.
  209. EXPECT_EQ(1, cnt) << "Command " << command << " not found";
  210. }
  211. /// @brief Check if the answer for write-config command is correct
  212. ///
  213. /// @param response_txt response in text form (as read from the control socket)
  214. /// @param exp_status expected status (0 success, 1 failure)
  215. /// @param exp_txt for success cases this defines the expected filename,
  216. /// for failure cases this defines the expected error message
  217. void checkConfigWrite(const std::string& response_txt, int exp_status,
  218. const std::string& exp_txt = "") {
  219. ConstElementPtr rsp;
  220. EXPECT_NO_THROW(rsp = Element::fromJSON(response_txt));
  221. ASSERT_TRUE(rsp);
  222. int status;
  223. ConstElementPtr params = parseAnswer(status, rsp);
  224. EXPECT_EQ(exp_status, status);
  225. if (exp_status == CONTROL_RESULT_SUCCESS) {
  226. // Let's check couple things...
  227. // The parameters must include filename
  228. ASSERT_TRUE(params);
  229. ASSERT_TRUE(params->get("filename"));
  230. ASSERT_EQ(Element::string, params->get("filename")->getType());
  231. EXPECT_EQ(exp_txt, params->get("filename")->stringValue());
  232. // The parameters must include size. And the size
  233. // must indicate some content.
  234. ASSERT_TRUE(params->get("size"));
  235. ASSERT_EQ(Element::integer, params->get("size")->getType());
  236. int64_t size = params->get("size")->intValue();
  237. EXPECT_LE(1, size);
  238. // Now check if the file is really there and suitable for
  239. // opening.
  240. ifstream f(exp_txt, ios::binary | ios::ate);
  241. ASSERT_TRUE(f.good());
  242. // Now check that it is the correct size as reported.
  243. EXPECT_EQ(size, static_cast<int64_t>(f.tellg()));
  244. // Finally, check that it's really a JSON.
  245. ElementPtr from_file = Element::fromJSONFile(exp_txt);
  246. ASSERT_TRUE(from_file);
  247. } else if (exp_status == CONTROL_RESULT_ERROR) {
  248. // Let's check if the reason for failure was given.
  249. ConstElementPtr text = rsp->get("text");
  250. ASSERT_TRUE(text);
  251. ASSERT_EQ(Element::string, text->getType());
  252. EXPECT_EQ(exp_txt, text->stringValue());
  253. } else {
  254. ADD_FAILURE() << "Invalid expected status: " << exp_status;
  255. }
  256. }
  257. };
  258. TEST_F(CtrlDhcpv6SrvTest, commands) {
  259. boost::scoped_ptr<ControlledDhcpv6Srv> srv;
  260. ASSERT_NO_THROW(
  261. srv.reset(new ControlledDhcpv6Srv(DHCP6_SERVER_PORT + 10000))
  262. );
  263. // Use empty parameters list
  264. ElementPtr params(new isc::data::MapElement());
  265. int rcode = -1;
  266. // Case 1: send bogus command
  267. ConstElementPtr result = ControlledDhcpv6Srv::processCommand("blah", params);
  268. ConstElementPtr comment = isc::config::parseAnswer(rcode, result);
  269. EXPECT_EQ(1, rcode); // expect failure (no such command as blah)
  270. // Case 2: send shutdown command without any parameters
  271. result = ControlledDhcpv6Srv::processCommand("shutdown", params);
  272. comment = isc::config::parseAnswer(rcode, result);
  273. EXPECT_EQ(0, rcode); // expect success
  274. const pid_t pid(getpid());
  275. ConstElementPtr x(new isc::data::IntElement(pid));
  276. params->set("pid", x);
  277. // Case 3: send shutdown command with 1 parameter: pid
  278. result = ControlledDhcpv6Srv::processCommand("shutdown", params);
  279. comment = isc::config::parseAnswer(rcode, result);
  280. EXPECT_EQ(0, rcode); // Expect success
  281. }
  282. // Check that the "libreload" command will reload libraries
  283. TEST_F(CtrlChannelDhcpv6SrvTest, libreload) {
  284. createUnixChannelServer();
  285. // Ensure no marker files to start with.
  286. ASSERT_FALSE(checkMarkerFileExists(LOAD_MARKER_FILE));
  287. ASSERT_FALSE(checkMarkerFileExists(UNLOAD_MARKER_FILE));
  288. // Load two libraries
  289. HookLibsCollection libraries;
  290. libraries.push_back(make_pair(CALLOUT_LIBRARY_1, ConstElementPtr()));
  291. libraries.push_back(make_pair(CALLOUT_LIBRARY_2, ConstElementPtr()));
  292. HooksManager::loadLibraries(libraries);
  293. // Check they are loaded.
  294. HookLibsCollection loaded_libraries =
  295. HooksManager::getLibraryInfo();
  296. ASSERT_TRUE(libraries == loaded_libraries);
  297. // ... which also included checking that the marker file created by the
  298. // load functions exists and holds the correct value (of "12" - the
  299. // first library appends "1" to the file, the second appends "2"). Also
  300. // check that the unload marker file does not yet exist.
  301. EXPECT_TRUE(checkMarkerFile(LOAD_MARKER_FILE, "12"));
  302. EXPECT_FALSE(checkMarkerFileExists(UNLOAD_MARKER_FILE));
  303. // Now execute the "libreload" command. This should cause the libraries
  304. // to unload and to reload.
  305. std::string response;
  306. sendUnixCommand("{ \"command\": \"libreload\" }", response);
  307. EXPECT_EQ("{ \"result\": 0, "
  308. "\"text\": \"Hooks libraries successfully reloaded.\" }"
  309. , response);
  310. // Check that the libraries have unloaded and reloaded. The libraries are
  311. // unloaded in the reverse order to which they are loaded. When they load,
  312. // they should append information to the loading marker file.
  313. EXPECT_TRUE(checkMarkerFile(UNLOAD_MARKER_FILE, "21"));
  314. EXPECT_TRUE(checkMarkerFile(LOAD_MARKER_FILE, "1212"));
  315. }
  316. // Check that the "config-set" command will replace current configuration
  317. TEST_F(CtrlChannelDhcpv6SrvTest, configSet) {
  318. createUnixChannelServer();
  319. // Define strings to permutate the config arguments
  320. // (Note the line feeds makes errors easy to find)
  321. string set_config_txt = "{ \"command\": \"config-set\" \n";
  322. string args_txt = " \"arguments\": { \n";
  323. string dhcp6_cfg_txt =
  324. " \"Dhcp6\": { \n"
  325. " \"interfaces-config\": { \n"
  326. " \"interfaces\": [\"*\"] \n"
  327. " }, \n"
  328. " \"preferred-lifetime\": 3000, \n"
  329. " \"valid-lifetime\": 4000, \n"
  330. " \"renew-timer\": 1000, \n"
  331. " \"rebind-timer\": 2000, \n"
  332. " \"lease-database\": { \n"
  333. " \"type\": \"memfile\", \n"
  334. " \"persist\":false, \n"
  335. " \"lfc-interval\": 0 \n"
  336. " }, \n"
  337. " \"expired-leases-processing\": { \n"
  338. " \"reclaim-timer-wait-time\": 0, \n"
  339. " \"hold-reclaimed-time\": 0, \n"
  340. " \"flush-reclaimed-timer-wait-time\": 0 \n"
  341. " },"
  342. " \"subnet6\": [ \n";
  343. string subnet1 =
  344. " {\"subnet\": \"3002::/64\", \n"
  345. " \"pools\": [{ \"pool\": \"3002::100-3002::200\" }]}\n";
  346. string subnet2 =
  347. " {\"subnet\": \"3003::/64\", \n"
  348. " \"pools\": [{ \"pool\": \"3003::100-3003::200\" }]}\n";
  349. string bad_subnet =
  350. " {\"BOGUS\": \"3005::/64\", \n"
  351. " \"pools\": [{ \"pool\": \"3005::100-3005::200\" }]}\n";
  352. string subnet_footer =
  353. " ] \n";
  354. string control_socket_header =
  355. " ,\"control-socket\": { \n"
  356. " \"socket-type\": \"unix\", \n"
  357. " \"socket-name\": \"";
  358. string control_socket_footer =
  359. "\" \n} \n";
  360. string logger_txt =
  361. " \"Logging\": { \n"
  362. " \"loggers\": [ { \n"
  363. " \"name\": \"kea\", \n"
  364. " \"severity\": \"FATAL\", \n"
  365. " \"output_options\": [{ \n"
  366. " \"output\": \"/dev/null\" \n"
  367. " }] \n"
  368. " }] \n"
  369. " } \n";
  370. std::ostringstream os;
  371. // Create a valid config with all the parts should parse
  372. os << set_config_txt << ","
  373. << args_txt
  374. << dhcp6_cfg_txt
  375. << subnet1
  376. << subnet_footer
  377. << control_socket_header
  378. << socket_path_
  379. << control_socket_footer
  380. << "}\n" // close dhcp6
  381. << ","
  382. << logger_txt
  383. << "}}";
  384. // Send the config-set command
  385. std::string response;
  386. sendUnixCommand(os.str(), response);
  387. // Verify the configuration was successful.
  388. EXPECT_EQ("{ \"result\": 0, \"text\": \"Configuration successful.\" }",
  389. response);
  390. // Check that the config was indeed applied.
  391. const Subnet6Collection* subnets =
  392. CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  393. EXPECT_EQ(1, subnets->size());
  394. // Create a config with malformed subnet that should fail to parse.
  395. os.str("");
  396. os << set_config_txt << ","
  397. << args_txt
  398. << dhcp6_cfg_txt
  399. << bad_subnet
  400. << subnet_footer
  401. << control_socket_header
  402. << socket_path_
  403. << control_socket_footer
  404. << "}\n" // close dhcp6
  405. "}}";
  406. // Send the config-set command
  407. sendUnixCommand(os.str(), response);
  408. // Should fail with a syntax error
  409. EXPECT_EQ("{ \"result\": 1, "
  410. "\"text\": \"subnet configuration failed: mandatory 'subnet' parameter is missing for a subnet being configured (<string>:21:17)\" }",
  411. response);
  412. // Check that the config was not lost
  413. subnets = CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  414. EXPECT_EQ(1, subnets->size());
  415. // Create a valid config with two subnets and no command channel.
  416. // It should succeed but client will not receive a the response
  417. os.str("");
  418. os << set_config_txt << ","
  419. << args_txt
  420. << dhcp6_cfg_txt
  421. << subnet1
  422. << ",\n"
  423. << subnet2
  424. << subnet_footer
  425. << "}\n" // close dhcp6
  426. << "}}";
  427. // Verify the control channel socket exists.
  428. ASSERT_TRUE(fileExists(socket_path_));
  429. // Send the config-set command.
  430. sendUnixCommand(os.str(), response);
  431. // Verify the control channel socket no longer exists.
  432. EXPECT_FALSE(fileExists(socket_path_));
  433. // With no command channel, should still receive the response.
  434. EXPECT_EQ("{ \"result\": 0, \"text\": \"Configuration successful.\" }",
  435. response);
  436. // Check that the config was not lost
  437. subnets = CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  438. EXPECT_EQ(2, subnets->size());
  439. // Clean up after the test.
  440. CfgMgr::instance().clear();
  441. }
  442. // Verify that the "config-test" command will do what we expect.
  443. TEST_F(CtrlChannelDhcpv6SrvTest, configTest) {
  444. createUnixChannelServer();
  445. // Define strings to permutate the config arguments
  446. // (Note the line feeds makes errors easy to find)
  447. string set_config_txt = "{ \"command\": \"config-set\" \n";
  448. string config_test_txt = "{ \"command\": \"config-test\" \n";
  449. string args_txt = " \"arguments\": { \n";
  450. string dhcp6_cfg_txt =
  451. " \"Dhcp6\": { \n"
  452. " \"interfaces-config\": { \n"
  453. " \"interfaces\": [\"*\"] \n"
  454. " }, \n"
  455. " \"preferred-lifetime\": 3000, \n"
  456. " \"valid-lifetime\": 4000, \n"
  457. " \"renew-timer\": 1000, \n"
  458. " \"rebind-timer\": 2000, \n"
  459. " \"lease-database\": { \n"
  460. " \"type\": \"memfile\", \n"
  461. " \"persist\":false, \n"
  462. " \"lfc-interval\": 0 \n"
  463. " }, \n"
  464. " \"expired-leases-processing\": { \n"
  465. " \"reclaim-timer-wait-time\": 0, \n"
  466. " \"hold-reclaimed-time\": 0, \n"
  467. " \"flush-reclaimed-timer-wait-time\": 0 \n"
  468. " },"
  469. " \"subnet6\": [ \n";
  470. string subnet1 =
  471. " {\"subnet\": \"3002::/64\", \n"
  472. " \"pools\": [{ \"pool\": \"3002::100-3002::200\" }]}\n";
  473. string subnet2 =
  474. " {\"subnet\": \"3003::/64\", \n"
  475. " \"pools\": [{ \"pool\": \"3003::100-3003::200\" }]}\n";
  476. string bad_subnet =
  477. " {\"BOGUS\": \"3005::/64\", \n"
  478. " \"pools\": [{ \"pool\": \"3005::100-3005::200\" }]}\n";
  479. string subnet_footer =
  480. " ] \n";
  481. string control_socket_header =
  482. " ,\"control-socket\": { \n"
  483. " \"socket-type\": \"unix\", \n"
  484. " \"socket-name\": \"";
  485. string control_socket_footer =
  486. "\" \n} \n";
  487. string logger_txt =
  488. " \"Logging\": { \n"
  489. " \"loggers\": [ { \n"
  490. " \"name\": \"kea\", \n"
  491. " \"severity\": \"FATAL\", \n"
  492. " \"output_options\": [{ \n"
  493. " \"output\": \"/dev/null\" \n"
  494. " }] \n"
  495. " }] \n"
  496. " } \n";
  497. std::ostringstream os;
  498. // Create a valid config with all the parts should parse
  499. os << set_config_txt << ","
  500. << args_txt
  501. << dhcp6_cfg_txt
  502. << subnet1
  503. << subnet_footer
  504. << control_socket_header
  505. << socket_path_
  506. << control_socket_footer
  507. << "}\n" // close dhcp6
  508. << ","
  509. << logger_txt
  510. << "}}";
  511. // Send the config-set command
  512. std::string response;
  513. sendUnixCommand(os.str(), response);
  514. // Verify the configuration was successful.
  515. EXPECT_EQ("{ \"result\": 0, \"text\": \"Configuration successful.\" }",
  516. response);
  517. // Check that the config was indeed applied.
  518. const Subnet6Collection* subnets =
  519. CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  520. EXPECT_EQ(1, subnets->size());
  521. // Create a config with malformed subnet that should fail to parse.
  522. os.str("");
  523. os << config_test_txt << ","
  524. << args_txt
  525. << dhcp6_cfg_txt
  526. << bad_subnet
  527. << subnet_footer
  528. << control_socket_header
  529. << socket_path_
  530. << control_socket_footer
  531. << "}\n" // close dhcp6
  532. "}}";
  533. // Send the config-test command
  534. sendUnixCommand(os.str(), response);
  535. // Should fail with a syntax error
  536. EXPECT_EQ("{ \"result\": 1, "
  537. "\"text\": \"subnet configuration failed: mandatory 'subnet' parameter "
  538. "is missing for a subnet being configured (<string>:21:17)\" }",
  539. response);
  540. // Check that the config was not lost
  541. subnets = CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  542. EXPECT_EQ(1, subnets->size());
  543. // Create a valid config with two subnets and no command channel.
  544. os.str("");
  545. os << config_test_txt << ","
  546. << args_txt
  547. << dhcp6_cfg_txt
  548. << subnet1
  549. << ",\n"
  550. << subnet2
  551. << subnet_footer
  552. << "}\n" // close dhcp6
  553. << "}}";
  554. // Verify the control channel socket exists.
  555. ASSERT_TRUE(fileExists(socket_path_));
  556. // Send the config-test command.
  557. sendUnixCommand(os.str(), response);
  558. // Verify the control channel socket still exists.
  559. EXPECT_TRUE(fileExists(socket_path_));
  560. // Verify the configuration was successful.
  561. EXPECT_EQ("{ \"result\": 0, \"text\": \"Configuration seems sane. "
  562. "Control-socket, hook-libraries, and D2 configuration were "
  563. "sanity checked, but not applied.\" }",
  564. response);
  565. // Check that the config was not applied.
  566. subnets = CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  567. EXPECT_EQ(1, subnets->size());
  568. // Clean up after the test.
  569. CfgMgr::instance().clear();
  570. }
  571. typedef std::map<std::string, isc::data::ConstElementPtr> ElementMap;
  572. // This test checks which commands are registered by the DHCPv6 server.
  573. TEST_F(CtrlDhcpv6SrvTest, commandsRegistration) {
  574. ConstElementPtr list_cmds = createCommand("list-commands");
  575. ConstElementPtr answer;
  576. // By default the list should be empty (except the standard list-commands
  577. // supported by the CommandMgr itself)
  578. EXPECT_NO_THROW(answer = CommandMgr::instance().processCommand(list_cmds));
  579. ASSERT_TRUE(answer);
  580. ASSERT_TRUE(answer->get("arguments"));
  581. EXPECT_EQ("[ \"list-commands\" ]", answer->get("arguments")->str());
  582. // Created server should register several additional commands.
  583. boost::scoped_ptr<ControlledDhcpv6Srv> srv;
  584. ASSERT_NO_THROW(
  585. srv.reset(new ControlledDhcpv6Srv(0));
  586. );
  587. EXPECT_NO_THROW(answer = CommandMgr::instance().processCommand(list_cmds));
  588. ASSERT_TRUE(answer);
  589. ASSERT_TRUE(answer->get("arguments"));
  590. std::string command_list = answer->get("arguments")->str();
  591. EXPECT_TRUE(command_list.find("\"list-commands\"") != string::npos);
  592. EXPECT_TRUE(command_list.find("\"build-report\"") != string::npos);
  593. EXPECT_TRUE(command_list.find("\"config-get\"") != string::npos);
  594. EXPECT_TRUE(command_list.find("\"config-write\"") != string::npos);
  595. EXPECT_TRUE(command_list.find("\"leases-reclaim\"") != string::npos);
  596. EXPECT_TRUE(command_list.find("\"libreload\"") != string::npos);
  597. EXPECT_TRUE(command_list.find("\"config-set\"") != string::npos);
  598. EXPECT_TRUE(command_list.find("\"shutdown\"") != string::npos);
  599. EXPECT_TRUE(command_list.find("\"statistic-get\"") != string::npos);
  600. EXPECT_TRUE(command_list.find("\"statistic-get-all\"") != string::npos);
  601. EXPECT_TRUE(command_list.find("\"statistic-remove\"") != string::npos);
  602. EXPECT_TRUE(command_list.find("\"statistic-remove-all\"") != string::npos);
  603. EXPECT_TRUE(command_list.find("\"statistic-reset\"") != string::npos);
  604. EXPECT_TRUE(command_list.find("\"statistic-reset-all\"") != string::npos);
  605. EXPECT_TRUE(command_list.find("\"version-get\"") != string::npos);
  606. // Ok, and now delete the server. It should deregister its commands.
  607. srv.reset();
  608. // The list should be (almost) empty again.
  609. EXPECT_NO_THROW(answer = CommandMgr::instance().processCommand(list_cmds));
  610. ASSERT_TRUE(answer);
  611. ASSERT_TRUE(answer->get("arguments"));
  612. EXPECT_EQ("[ \"list-commands\" ]", answer->get("arguments")->str());
  613. }
  614. // Tests that the server properly responds to invalid commands sent
  615. // via ControlChannel
  616. TEST_F(CtrlChannelDhcpv6SrvTest, controlChannelNegative) {
  617. createUnixChannelServer();
  618. std::string response;
  619. sendUnixCommand("{ \"command\": \"bogus\" }", response);
  620. EXPECT_EQ("{ \"result\": 2,"
  621. " \"text\": \"'bogus' command not supported.\" }", response);
  622. sendUnixCommand("utter nonsense", response);
  623. EXPECT_EQ("{ \"result\": 1, "
  624. "\"text\": \"error: unexpected character u in <string>:1:2\" }",
  625. response);
  626. }
  627. // Tests that the server properly responds to shtudown command sent
  628. // via ControlChannel
  629. TEST_F(CtrlChannelDhcpv6SrvTest, controlChannelShutdown) {
  630. createUnixChannelServer();
  631. std::string response;
  632. sendUnixCommand("{ \"command\": \"shutdown\" }", response);
  633. EXPECT_EQ("{ \"result\": 0, \"text\": \"Shutting down.\" }",response);
  634. }
  635. // This test verifies that the DHCP server handles version-get commands
  636. TEST_F(CtrlChannelDhcpv6SrvTest, getversion) {
  637. createUnixChannelServer();
  638. std::string response;
  639. // Send the version-get command
  640. sendUnixCommand("{ \"command\": \"version-get\" }", response);
  641. EXPECT_TRUE(response.find("\"result\": 0") != string::npos);
  642. EXPECT_TRUE(response.find("log4cplus") != string::npos);
  643. EXPECT_FALSE(response.find("GTEST_VERSION") != string::npos);
  644. // Send the build-report command
  645. sendUnixCommand("{ \"command\": \"build-report\" }", response);
  646. EXPECT_TRUE(response.find("\"result\": 0") != string::npos);
  647. EXPECT_TRUE(response.find("GTEST_VERSION") != string::npos);
  648. }
  649. // This test verifies that the DHCP server immediately reclaims expired
  650. // leases on leases-reclaim command
  651. TEST_F(CtrlChannelDhcpv6SrvTest, controlLeasesReclaim) {
  652. createUnixChannelServer();
  653. // Create expired leases. Leases are expired by 40 seconds ago
  654. // (valid lifetime = 60, cltt = now - 100).
  655. DuidPtr duid0(new DUID(DUID::fromText("00:01:02:03:04:05:06").getDuid()));
  656. Lease6Ptr lease0(new Lease6(Lease::TYPE_NA, IOAddress("3000::1"),
  657. duid0, 1, 50, 60, 10, 20, SubnetID(1)));
  658. lease0->cltt_ = time(NULL) - 100;
  659. DuidPtr duid1(new DUID(DUID::fromText("01:02:03:04:05:06:07").getDuid()));
  660. Lease6Ptr lease1(new Lease6(Lease::TYPE_NA, IOAddress("3000::2"),
  661. duid1, 1, 50, 60, 10, 20, SubnetID(1)));
  662. lease1->cltt_ = time(NULL) - 100;
  663. // Add leases to the database.
  664. LeaseMgr& lease_mgr = LeaseMgrFactory::instance();
  665. ASSERT_NO_THROW(lease_mgr.addLease(lease0));
  666. ASSERT_NO_THROW(lease_mgr.addLease(lease1));
  667. // Make sure they have been added.
  668. ASSERT_TRUE(lease_mgr.getLease6(Lease::TYPE_NA, IOAddress("3000::1")));
  669. ASSERT_TRUE(lease_mgr.getLease6(Lease::TYPE_NA, IOAddress("3000::2")));
  670. // No arguments
  671. std::string response;
  672. sendUnixCommand("{ \"command\": \"leases-reclaim\" }", response);
  673. EXPECT_EQ("{ \"result\": 1, \"text\": "
  674. "\"Missing mandatory 'remove' parameter.\" }", response);
  675. // Bad argument name
  676. sendUnixCommand("{ \"command\": \"leases-reclaim\", "
  677. "\"arguments\": { \"reclaim\": true } }", response);
  678. EXPECT_EQ("{ \"result\": 1, \"text\": "
  679. "\"Missing mandatory 'remove' parameter.\" }", response);
  680. // Bad remove argument type
  681. sendUnixCommand("{ \"command\": \"leases-reclaim\", "
  682. "\"arguments\": { \"remove\": \"bogus\" } }", response);
  683. EXPECT_EQ("{ \"result\": 1, \"text\": "
  684. "\"'remove' parameter expected to be a boolean.\" }", response);
  685. // Send the command
  686. sendUnixCommand("{ \"command\": \"leases-reclaim\", "
  687. "\"arguments\": { \"remove\": false } }", response);
  688. EXPECT_EQ("{ \"result\": 0, \"text\": "
  689. "\"Reclamation of expired leases is complete.\" }", response);
  690. // Leases should be reclaimed, but not removed
  691. ASSERT_NO_THROW(
  692. lease0 = lease_mgr.getLease6(Lease::TYPE_NA, IOAddress("3000::1"))
  693. );
  694. ASSERT_NO_THROW(
  695. lease1 = lease_mgr.getLease6(Lease::TYPE_NA, IOAddress("3000::2"))
  696. );
  697. ASSERT_TRUE(lease0);
  698. ASSERT_TRUE(lease1);
  699. EXPECT_TRUE(lease0->stateExpiredReclaimed());
  700. EXPECT_TRUE(lease1->stateExpiredReclaimed());
  701. }
  702. // This test verifies that the DHCP server immediately reclaims expired
  703. // leases on leases-reclaim command with remove = true
  704. TEST_F(CtrlChannelDhcpv6SrvTest, controlLeasesReclaimRemove) {
  705. createUnixChannelServer();
  706. // Create expired leases. Leases are expired by 40 seconds ago
  707. // (valid lifetime = 60, cltt = now - 100).
  708. DuidPtr duid0(new DUID(DUID::fromText("00:01:02:03:04:05:06").getDuid()));
  709. Lease6Ptr lease0(new Lease6(Lease::TYPE_NA, IOAddress("3000::1"),
  710. duid0, 1, 50, 60, 10, 20, SubnetID(1)));
  711. lease0->cltt_ = time(NULL) - 100;
  712. DuidPtr duid1(new DUID(DUID::fromText("01:02:03:04:05:06:07").getDuid()));
  713. Lease6Ptr lease1(new Lease6(Lease::TYPE_NA, IOAddress("3000::2"),
  714. duid1, 1, 50, 60, 10, 20, SubnetID(1)));
  715. lease1->cltt_ = time(NULL) - 100;
  716. // Add leases to the database.
  717. LeaseMgr& lease_mgr = LeaseMgrFactory::instance();
  718. ASSERT_NO_THROW(lease_mgr.addLease(lease0));
  719. ASSERT_NO_THROW(lease_mgr.addLease(lease1));
  720. // Make sure they have been added.
  721. ASSERT_TRUE(lease_mgr.getLease6(Lease::TYPE_NA, IOAddress("3000::1")));
  722. ASSERT_TRUE(lease_mgr.getLease6(Lease::TYPE_NA, IOAddress("3000::2")));
  723. // Send the command
  724. std::string response;
  725. sendUnixCommand("{ \"command\": \"leases-reclaim\", "
  726. "\"arguments\": { \"remove\": true } }", response);
  727. EXPECT_EQ("{ \"result\": 0, \"text\": "
  728. "\"Reclamation of expired leases is complete.\" }", response);
  729. // Leases should have been removed.
  730. ASSERT_NO_THROW(
  731. lease0 = lease_mgr.getLease6(Lease::TYPE_NA, IOAddress("3000::1"))
  732. );
  733. ASSERT_NO_THROW(
  734. lease1 = lease_mgr.getLease6(Lease::TYPE_NA, IOAddress("3000::2"))
  735. );
  736. ASSERT_FALSE(lease0);
  737. ASSERT_FALSE(lease1);
  738. }
  739. // Tests that the server properly responds to statistics commands. Note this
  740. // is really only intended to verify that the appropriate Statistics handler
  741. // is called based on the command. It is not intended to be an exhaustive
  742. // test of Dhcpv6 statistics.
  743. TEST_F(CtrlChannelDhcpv6SrvTest, controlChannelStats) {
  744. createUnixChannelServer();
  745. std::string response;
  746. // Check statistic-get
  747. sendUnixCommand("{ \"command\" : \"statistic-get\", "
  748. " \"arguments\": {"
  749. " \"name\":\"bogus\" }}", response);
  750. EXPECT_EQ("{ \"arguments\": { }, \"result\": 0 }", response);
  751. // Check statistic-get-all
  752. sendUnixCommand("{ \"command\" : \"statistic-get-all\", "
  753. " \"arguments\": {}}", response);
  754. EXPECT_EQ("{ \"arguments\": { }, \"result\": 0 }", response);
  755. // Check statistic-reset
  756. sendUnixCommand("{ \"command\" : \"statistic-reset\", "
  757. " \"arguments\": {"
  758. " \"name\":\"bogus\" }}", response);
  759. EXPECT_EQ("{ \"result\": 1, \"text\": \"No 'bogus' statistic found\" }",
  760. response);
  761. // Check statistic-reset-all
  762. sendUnixCommand("{ \"command\" : \"statistic-reset-all\", "
  763. " \"arguments\": {}}", response);
  764. EXPECT_EQ("{ \"result\": 0, \"text\": "
  765. "\"All statistics reset to neutral values.\" }", response);
  766. // Check statistic-remove
  767. sendUnixCommand("{ \"command\" : \"statistic-remove\", "
  768. " \"arguments\": {"
  769. " \"name\":\"bogus\" }}", response);
  770. EXPECT_EQ("{ \"result\": 1, \"text\": \"No 'bogus' statistic found\" }",
  771. response);
  772. // Check statistic-remove-all
  773. sendUnixCommand("{ \"command\" : \"statistic-remove-all\", "
  774. " \"arguments\": {}}", response);
  775. EXPECT_EQ("{ \"result\": 0, \"text\": \"All statistics removed.\" }",
  776. response);
  777. }
  778. // Tests that the server properly responds to shtudown command sent
  779. // via ControlChannel
  780. TEST_F(CtrlChannelDhcpv6SrvTest, commandsList) {
  781. createUnixChannelServer();
  782. std::string response;
  783. sendUnixCommand("{ \"command\": \"list-commands\" }", response);
  784. ConstElementPtr rsp;
  785. EXPECT_NO_THROW(rsp = Element::fromJSON(response));
  786. // We expect the server to report at least the following commands:
  787. checkListCommands(rsp, "build-report");
  788. checkListCommands(rsp, "config-get");
  789. checkListCommands(rsp, "config-set");
  790. checkListCommands(rsp, "config-test");
  791. checkListCommands(rsp, "config-write");
  792. checkListCommands(rsp, "list-commands");
  793. checkListCommands(rsp, "leases-reclaim");
  794. checkListCommands(rsp, "libreload");
  795. checkListCommands(rsp, "version-get");
  796. checkListCommands(rsp, "shutdown");
  797. checkListCommands(rsp, "statistic-get");
  798. checkListCommands(rsp, "statistic-get-all");
  799. checkListCommands(rsp, "statistic-remove");
  800. checkListCommands(rsp, "statistic-remove-all");
  801. checkListCommands(rsp, "statistic-reset");
  802. checkListCommands(rsp, "statistic-reset-all");
  803. }
  804. // Tests if the server returns its configuration using config-get.
  805. // Note there are separate tests that verify if toElement() called by the
  806. // get-config handler are actually converting the configuration correctly.
  807. TEST_F(CtrlChannelDhcpv6SrvTest, configGet) {
  808. createUnixChannelServer();
  809. std::string response;
  810. sendUnixCommand("{ \"command\": \"config-get\" }", response);
  811. ConstElementPtr rsp;
  812. // The response should be a valid JSON.
  813. EXPECT_NO_THROW(rsp = Element::fromJSON(response));
  814. ASSERT_TRUE(rsp);
  815. int status;
  816. ConstElementPtr cfg = parseAnswer(status, rsp);
  817. EXPECT_EQ(CONTROL_RESULT_SUCCESS, status);
  818. // Ok, now roughly check if the response seems legit.
  819. ASSERT_TRUE(cfg);
  820. ASSERT_EQ(Element::map, cfg->getType());
  821. EXPECT_TRUE(cfg->get("Dhcp6"));
  822. }
  823. // Tests if config-write can be called without any parameters.
  824. TEST_F(CtrlChannelDhcpv6SrvTest, configWriteNoFilename) {
  825. createUnixChannelServer();
  826. std::string response;
  827. // This is normally set by the command line -c parameter.
  828. server_->setConfigFile("test1.json");
  829. // If the filename is not explicitly specified, the name used
  830. // in -c command line switch is used.
  831. sendUnixCommand("{ \"command\": \"config-write\" }", response);
  832. checkConfigWrite(response, CONTROL_RESULT_SUCCESS, "test1.json");
  833. ::remove("test1.json");
  834. }
  835. // Tests if config-write can be called with a valid filename as parameter.
  836. TEST_F(CtrlChannelDhcpv6SrvTest, configWriteFilename) {
  837. createUnixChannelServer();
  838. std::string response;
  839. sendUnixCommand("{ \"command\": \"config-write\", "
  840. "\"arguments\": { \"filename\": \"test2.json\" } }", response);
  841. checkConfigWrite(response, CONTROL_RESULT_SUCCESS, "test2.json");
  842. ::remove("test2.json");
  843. }
  844. // Tests if config-reload attempts to reload a file and reports that the
  845. // file is missing.
  846. TEST_F(CtrlChannelDhcpv6SrvTest, configReloadMissingFile) {
  847. createUnixChannelServer();
  848. std::string response;
  849. // This is normally set to whatever value is passed to -c when the server is
  850. // started, but we're not starting it that way, so need to set it by hand.
  851. server_->setConfigFile("test6.json");
  852. // Tell the server to reload its configuration. It should attempt to load
  853. // test6.json (and fail, because the file is not there).
  854. sendUnixCommand("{ \"command\": \"config-reload\" }", response);
  855. // Verify the reload was rejected.
  856. EXPECT_EQ("{ \"result\": 1, \"text\": \"Config reload failed:"
  857. "configuration error using file 'test6.json': Unable to open file "
  858. "test6.json\" }",
  859. response);
  860. }
  861. // Tests if config-reload attempts to reload a file and reports that the
  862. // file is not a valid JSON.
  863. TEST_F(CtrlChannelDhcpv6SrvTest, configReloadBrokenFile) {
  864. createUnixChannelServer();
  865. std::string response;
  866. // This is normally set to whatever value is passed to -c when the server is
  867. // started, but we're not starting it that way, so need to set it by hand.
  868. server_->setConfigFile("test7.json");
  869. // Although Kea is smart, its AI routines are not smart enough to handle
  870. // this one... at least not yet.
  871. ofstream f("test7.json", ios::trunc);
  872. f << "gimme some addr, bro!";
  873. f.close();
  874. // Now tell Kea to reload its config.
  875. sendUnixCommand("{ \"command\": \"config-reload\" }", response);
  876. // Verify the reload will fail.
  877. EXPECT_EQ("{ \"result\": 1, \"text\": \"Config reload failed:"
  878. "configuration error using file 'test7.json': "
  879. "test7.json:1.1: Invalid character: g\" }",
  880. response);
  881. ::remove("test7.json");
  882. }
  883. // Tests if config-reload attempts to reload a file and reports that the
  884. // file is loaded correctly.
  885. TEST_F(CtrlChannelDhcpv6SrvTest, configReloadValid) {
  886. createUnixChannelServer();
  887. std::string response;
  888. // This is normally set to whatever value is passed to -c when the server is
  889. // started, but we're not starting it that way, so need to set it by hand.
  890. server_->setConfigFile("test8.json");
  891. // Ok, enough fooling around. Let's create a valid config.
  892. const std::string cfg_txt =
  893. "{ \"Dhcp6\": {"
  894. " \"interfaces-config\": {"
  895. " \"interfaces\": [ \"*\" ]"
  896. " },"
  897. " \"subnet6\": ["
  898. " { \"subnet\": \"2001:db8:1::/64\" },"
  899. " { \"subnet\": \"2001:db8:2::/64\" }"
  900. " ],"
  901. " \"lease-database\": {"
  902. " \"type\": \"memfile\", \"persist\": false }"
  903. "} }";
  904. ofstream f("test8.json", ios::trunc);
  905. f << cfg_txt;
  906. f.close();
  907. // This command should reload test8.json config.
  908. sendUnixCommand("{ \"command\": \"config-reload\" }", response);
  909. // Verify the configuration was successful.
  910. EXPECT_EQ("{ \"result\": 0, \"text\": \"Configuration successful.\" }",
  911. response);
  912. // Check that the config was indeed applied.
  913. const Subnet6Collection* subnets =
  914. CfgMgr::instance().getCurrentCfg()->getCfgSubnets6()->getAll();
  915. EXPECT_EQ(2, subnets->size());
  916. ::remove("test8.json");
  917. }
  918. /// Verify that concurrent connections over the control channel can be
  919. /// established.
  920. /// @todo Future Kea 1.3 tickets will modify the behavior of the CommandMgr
  921. /// such that the server will be able to send response in multiple chunks.
  922. /// This test will need to be extended. For now, the receive and write
  923. /// operations are atomic and there is no conflict between concurrent
  924. /// connections.
  925. TEST_F(CtrlChannelDhcpv6SrvTest, concurrentConnections) {
  926. createUnixChannelServer();
  927. boost::scoped_ptr<UnixControlClient> client1(new UnixControlClient());
  928. ASSERT_TRUE(client1);
  929. boost::scoped_ptr<UnixControlClient> client2(new UnixControlClient());
  930. ASSERT_TRUE(client2);
  931. // Client 1 connects.
  932. ASSERT_TRUE(client1->connectToServer(socket_path_));
  933. ASSERT_NO_THROW(getIOService()->poll());
  934. // Client 2 connects.
  935. ASSERT_TRUE(client2->connectToServer(socket_path_));
  936. ASSERT_NO_THROW(getIOService()->poll());
  937. // Send the command while another client is connected.
  938. ASSERT_TRUE(client2->sendCommand("{ \"command\": \"list-commands\" }"));
  939. ASSERT_NO_THROW(getIOService()->poll());
  940. std::string response;
  941. // The server should respond ok.
  942. ASSERT_TRUE(client2->getResponse(response));
  943. EXPECT_TRUE(response.find("\"result\": 0") != std::string::npos);
  944. // Disconnect the servers.
  945. client1->disconnectFromServer();
  946. client2->disconnectFromServer();
  947. ASSERT_NO_THROW(getIOService()->poll());
  948. }
  949. } // End of anonymous namespace