2017-01-03 08:23:44 -05:00
|
|
|
// Copyright (C) 2012-2017 Internet Systems Consortium, Inc. ("ISC")
|
2012-06-08 15:27:45 +02:00
|
|
|
//
|
2015-12-15 21:37:34 +01:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2012-06-08 15:27:45 +02:00
|
|
|
|
|
|
|
#include <config.h>
|
2012-11-16 14:15:45 +00:00
|
|
|
|
2015-06-10 10:48:24 +02:00
|
|
|
#include <cc/command_interpreter.h>
|
2015-06-03 16:32:59 +02:00
|
|
|
#include <config/command_mgr.h>
|
2012-11-16 14:15:45 +00:00
|
|
|
#include <dhcp/dhcp4.h>
|
|
|
|
#include <dhcp4/ctrl_dhcp4_srv.h>
|
2016-12-17 01:05:26 +01:00
|
|
|
#include <dhcp4/tests/dhcp4_test_utils.h>
|
2015-07-14 20:03:20 +02:00
|
|
|
#include <dhcpsrv/cfgmgr.h>
|
2015-10-16 23:50:39 +02:00
|
|
|
#include <dhcpsrv/lease.h>
|
|
|
|
#include <dhcpsrv/lease_mgr_factory.h>
|
2013-07-25 13:20:02 +01:00
|
|
|
#include <hooks/hooks_manager.h>
|
2016-12-09 16:08:24 -05:00
|
|
|
#include <log/logger_support.h>
|
2015-10-17 11:19:55 +02:00
|
|
|
#include <stats/stats_mgr.h>
|
2016-12-22 13:38:00 -05:00
|
|
|
#include <testutils/io_utils.h>
|
2015-07-14 20:03:20 +02:00
|
|
|
#include <testutils/unix_control_client.h>
|
2013-07-25 13:20:02 +01:00
|
|
|
|
|
|
|
#include "marker_file.h"
|
|
|
|
#include "test_libraries.h"
|
2012-11-16 14:15:45 +00:00
|
|
|
|
2013-04-11 13:30:32 +01:00
|
|
|
#include <boost/scoped_ptr.hpp>
|
2012-11-16 14:15:45 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2012-06-08 15:27:45 +02:00
|
|
|
#include <fstream>
|
2012-11-16 14:15:45 +00:00
|
|
|
#include <iostream>
|
2012-06-08 15:27:45 +02:00
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
2013-07-25 13:20:02 +01:00
|
|
|
#include <unistd.h>
|
2012-06-08 15:27:45 +02:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace isc;
|
|
|
|
using namespace isc::asiolink;
|
2012-06-08 20:39:01 +02:00
|
|
|
using namespace isc::config;
|
2013-07-25 13:20:02 +01:00
|
|
|
using namespace isc::data;
|
|
|
|
using namespace isc::dhcp;
|
|
|
|
using namespace isc::dhcp::test;
|
|
|
|
using namespace isc::hooks;
|
2015-10-17 11:19:55 +02:00
|
|
|
using namespace isc::stats;
|
2017-02-07 13:29:04 +01:00
|
|
|
using namespace isc::test;
|
2012-06-08 15:27:45 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2012-06-08 15:28:18 +02:00
|
|
|
class NakedControlledDhcpv4Srv: public ControlledDhcpv4Srv {
|
2013-04-11 13:30:32 +01:00
|
|
|
// "Naked" DHCPv4 server, exposes internal fields
|
2012-06-08 15:27:45 +02:00
|
|
|
public:
|
2015-07-14 20:30:38 +02:00
|
|
|
NakedControlledDhcpv4Srv():ControlledDhcpv4Srv(0) { }
|
2015-07-14 20:03:20 +02:00
|
|
|
|
|
|
|
/// Expose internal methods for the sake of testing
|
|
|
|
using Dhcpv4Srv::receivePacket;
|
2012-06-08 15:27:45 +02:00
|
|
|
};
|
|
|
|
|
2015-07-14 20:03:20 +02:00
|
|
|
/// @brief Fixture class intended for testin control channel in the DHCPv4Srv
|
|
|
|
class CtrlChannelDhcpv4SrvTest : public ::testing::Test {
|
2012-06-08 15:27:45 +02:00
|
|
|
public:
|
2015-07-14 20:03:20 +02:00
|
|
|
|
|
|
|
/// @brief Path to the UNIX socket being used to communicate with the server
|
|
|
|
std::string socket_path_;
|
|
|
|
|
|
|
|
/// @brief Pointer to the tested server object
|
|
|
|
boost::shared_ptr<NakedControlledDhcpv4Srv> server_;
|
|
|
|
|
|
|
|
/// @brief Default constructor
|
|
|
|
///
|
|
|
|
/// Sets socket path to its default value.
|
|
|
|
CtrlChannelDhcpv4SrvTest() {
|
|
|
|
const char* env = getenv("KEA_SOCKET_TEST_DIR");
|
|
|
|
if (env) {
|
|
|
|
socket_path_ = string(env) + "/kea4.sock";
|
|
|
|
} else {
|
|
|
|
socket_path_ = string(TEST_DATA_BUILDDIR) + "/kea4.sock";
|
|
|
|
}
|
2013-07-25 13:20:02 +01:00
|
|
|
reset();
|
2012-06-08 15:27:45 +02:00
|
|
|
}
|
|
|
|
|
2015-07-14 20:03:20 +02:00
|
|
|
/// @brief Destructor
|
|
|
|
~CtrlChannelDhcpv4SrvTest() {
|
2015-10-17 11:19:55 +02:00
|
|
|
LeaseMgrFactory::destroy();
|
|
|
|
StatsMgr::instance().removeAll();
|
2015-07-14 20:03:20 +02:00
|
|
|
server_.reset();
|
2013-07-25 13:20:02 +01:00
|
|
|
reset();
|
2012-06-08 15:27:45 +02:00
|
|
|
};
|
2013-07-25 13:20:02 +01:00
|
|
|
|
2015-07-14 20:03:20 +02:00
|
|
|
void createUnixChannelServer() {
|
|
|
|
::remove(socket_path_.c_str());
|
|
|
|
|
|
|
|
// Just a simple config. The important part here is the socket
|
|
|
|
// location information.
|
|
|
|
std::string header =
|
|
|
|
"{"
|
|
|
|
" \"interfaces-config\": {"
|
|
|
|
" \"interfaces\": [ \"*\" ]"
|
|
|
|
" },"
|
2015-10-16 23:50:39 +02:00
|
|
|
" \"expired-leases-processing\": {"
|
|
|
|
" \"reclaim-timer-wait-time\": 60,"
|
|
|
|
" \"hold-reclaimed-time\": 500,"
|
|
|
|
" \"flush-reclaimed-timer-wait-time\": 60"
|
|
|
|
" },"
|
2015-07-14 20:03:20 +02:00
|
|
|
" \"rebind-timer\": 2000, "
|
|
|
|
" \"renew-timer\": 1000, "
|
|
|
|
" \"subnet4\": [ ],"
|
|
|
|
" \"valid-lifetime\": 4000,"
|
|
|
|
" \"control-socket\": {"
|
|
|
|
" \"socket-type\": \"unix\","
|
|
|
|
" \"socket-name\": \"";
|
|
|
|
|
|
|
|
std::string footer =
|
|
|
|
"\" },"
|
|
|
|
" \"lease-database\": {"
|
|
|
|
" \"type\": \"memfile\", \"persist\": false }"
|
|
|
|
"}";
|
|
|
|
|
|
|
|
// Fill in the socket-name value with socket_path_ to
|
|
|
|
// make the actual configuration text.
|
|
|
|
std::string config_txt = header + socket_path_ + footer;
|
|
|
|
|
|
|
|
ASSERT_NO_THROW(server_.reset(new NakedControlledDhcpv4Srv()));
|
|
|
|
|
2016-12-17 01:05:26 +01:00
|
|
|
ConstElementPtr config;
|
|
|
|
ASSERT_NO_THROW(config = parseDHCP4(config_txt));
|
2015-07-14 20:03:20 +02:00
|
|
|
ConstElementPtr answer = server_->processConfig(config);
|
2016-12-16 11:02:04 -05:00
|
|
|
|
|
|
|
// Commit the configuration so any subsequent reconfigurations
|
|
|
|
// will only close the command channel if its configuration has
|
|
|
|
// changed.
|
|
|
|
CfgMgr::instance().commit();
|
|
|
|
|
|
|
|
|
2015-07-14 20:03:20 +02:00
|
|
|
ASSERT_TRUE(answer);
|
|
|
|
|
|
|
|
int status = 0;
|
|
|
|
ConstElementPtr txt = isc::config::parseAnswer(status, answer);
|
|
|
|
// This should succeed. If not, print the error message.
|
|
|
|
ASSERT_EQ(0, status) << txt->str();
|
|
|
|
|
|
|
|
// Now check that the socket was indeed open.
|
|
|
|
ASSERT_GT(isc::config::CommandMgr::instance().getControlSocketFD(), -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-25 13:20:02 +01:00
|
|
|
/// @brief Reset hooks data
|
|
|
|
///
|
|
|
|
/// Resets the data for the hooks-related portion of the test by ensuring
|
|
|
|
/// that no libraries are loaded and that any marker files are deleted.
|
|
|
|
void reset() {
|
|
|
|
// Unload any previously-loaded libraries.
|
|
|
|
HooksManager::unloadLibraries();
|
|
|
|
|
|
|
|
// Get rid of any marker files.
|
2015-05-12 22:51:43 +02:00
|
|
|
static_cast<void>(remove(LOAD_MARKER_FILE));
|
|
|
|
static_cast<void>(remove(UNLOAD_MARKER_FILE));
|
2015-07-14 20:03:20 +02:00
|
|
|
|
|
|
|
IfaceMgr::instance().deleteAllExternalSockets();
|
|
|
|
CfgMgr::instance().clear();
|
|
|
|
|
|
|
|
// Remove unix socket file
|
|
|
|
::remove(socket_path_.c_str());
|
2013-07-25 13:20:02 +01:00
|
|
|
}
|
2015-06-08 19:46:26 +02:00
|
|
|
|
2015-07-14 20:03:20 +02:00
|
|
|
/// @brief Conducts a command/response exchange via UnixCommandSocket
|
2015-06-08 19:46:26 +02:00
|
|
|
///
|
2015-07-14 20:03:20 +02:00
|
|
|
/// This method connects to the given server over the given socket path.
|
|
|
|
/// If successful, it then sends the given command and retrieves the
|
|
|
|
/// server's response. Note that it calls the server's receivePacket()
|
|
|
|
/// method where needed to cause the server to process IO events on
|
|
|
|
/// control channel the control channel sockets.
|
2015-06-08 19:46:26 +02:00
|
|
|
///
|
2015-07-14 20:03:20 +02:00
|
|
|
/// @param command the command text to execute in JSON form
|
|
|
|
/// @param response variable into which the received response should be
|
|
|
|
/// placed.
|
|
|
|
void sendUnixCommand(const std::string& command, std::string& response) {
|
|
|
|
response = "";
|
|
|
|
boost::scoped_ptr<UnixControlClient> client;
|
|
|
|
client.reset(new UnixControlClient());
|
|
|
|
ASSERT_TRUE(client);
|
|
|
|
|
|
|
|
// Connect and then call server's receivePacket() so it can
|
|
|
|
// detect the control socket connect and call the accept handler
|
|
|
|
ASSERT_TRUE(client->connectToServer(socket_path_));
|
|
|
|
ASSERT_NO_THROW(server_->receivePacket(0));
|
|
|
|
|
|
|
|
// Send the command and then call server's receivePacket() so it can
|
|
|
|
// detect the inbound data and call the read handler
|
|
|
|
ASSERT_TRUE(client->sendCommand(command));
|
|
|
|
ASSERT_NO_THROW(server_->receivePacket(0));
|
|
|
|
|
|
|
|
// Read the response generated by the server. Note that getResponse
|
|
|
|
// only fails if there an IO error or no response data was present.
|
|
|
|
// It is not based on the response content.
|
|
|
|
ASSERT_TRUE(client->getResponse(response));
|
|
|
|
|
|
|
|
// Now disconnect and process the close event
|
|
|
|
client->disconnectFromServer();
|
|
|
|
ASSERT_NO_THROW(server_->receivePacket(0));
|
2015-06-08 19:46:26 +02:00
|
|
|
}
|
2017-03-14 21:06:56 +01:00
|
|
|
|
|
|
|
/// @brief Checks response for list-commands
|
|
|
|
///
|
|
|
|
/// This method checks if the list-commands response is generally sane
|
|
|
|
/// and whether specified command is mentioned in the response.
|
|
|
|
///
|
|
|
|
/// @param rsp response sent back by the server
|
|
|
|
/// @param command command expected to be on the list.
|
|
|
|
void checkListCommands(const ConstElementPtr& rsp, const std::string& command) {
|
|
|
|
ConstElementPtr params;
|
|
|
|
int status_code;
|
|
|
|
EXPECT_NO_THROW(params = parseAnswer(status_code, rsp));
|
|
|
|
EXPECT_EQ(CONTROL_RESULT_SUCCESS, status_code);
|
|
|
|
ASSERT_TRUE(params);
|
|
|
|
ASSERT_EQ(Element::list, params->getType());
|
|
|
|
|
|
|
|
int cnt = 0;
|
|
|
|
for (int i=0; i < params->size(); ++i) {
|
|
|
|
string tmp = params->get(i)->stringValue();
|
|
|
|
if (tmp == command) {
|
|
|
|
// Command found, but that's not enough. Need to continue working
|
|
|
|
// through the list to see if there are no duplicates.
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exactly one command on the list is expected.
|
|
|
|
EXPECT_EQ(1, cnt) << "Command " << command << " not found";
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief Check if the answer for write-config command is correct
|
|
|
|
///
|
|
|
|
/// @param response_txt response in text form (as read from the control socket)
|
|
|
|
/// @param exp_status expected status (0 success, 1 failure)
|
|
|
|
/// @param exp_txt for success cases this defines the expected filename,
|
|
|
|
/// for failure cases this defines the expected error message
|
|
|
|
void checkWriteConfig(const std::string& response_txt, int exp_status,
|
|
|
|
const std::string& exp_txt = "") {
|
|
|
|
|
|
|
|
cout << "#### response=" << response_txt << endl;
|
|
|
|
|
|
|
|
ConstElementPtr rsp;
|
|
|
|
EXPECT_NO_THROW(rsp = Element::fromJSON(response_txt));
|
|
|
|
ASSERT_TRUE(rsp);
|
|
|
|
|
|
|
|
int status;
|
|
|
|
ConstElementPtr params = parseAnswer(status, rsp);
|
|
|
|
EXPECT_EQ(exp_status, status);
|
|
|
|
|
|
|
|
if (exp_status == CONTROL_RESULT_SUCCESS) {
|
|
|
|
// Let's check couple things...
|
|
|
|
|
|
|
|
// The parameters must include filename
|
|
|
|
ASSERT_TRUE(params);
|
|
|
|
ASSERT_TRUE(params->get("filename"));
|
|
|
|
EXPECT_EQ(Element::string, params->get("filename")->getType());
|
|
|
|
EXPECT_EQ(exp_txt, params->get("filename")->stringValue());
|
|
|
|
|
|
|
|
// The parameters must include size. And the size
|
|
|
|
// must indicate some content.
|
|
|
|
ASSERT_TRUE(params->get("size"));
|
|
|
|
EXPECT_EQ(Element::integer, params->get("size")->getType());
|
|
|
|
int64_t size = params->get("size")->intValue();
|
|
|
|
EXPECT_LE(1, size);
|
|
|
|
|
|
|
|
// Now check if the file is really there and suitable for
|
|
|
|
// opening.
|
|
|
|
ifstream f(exp_txt, ios::binary | ios::ate);
|
|
|
|
ASSERT_TRUE(f.good());
|
|
|
|
|
|
|
|
// Now check that it is the correct size as reported.
|
|
|
|
EXPECT_EQ(size, static_cast<int64_t>(f.tellg()));
|
|
|
|
|
|
|
|
// Finally, check that it's really a JSON.
|
|
|
|
ElementPtr from_file = Element::fromJSONFile(exp_txt);
|
|
|
|
ASSERT_TRUE(from_file);
|
|
|
|
} else if (exp_status == CONTROL_RESULT_ERROR) {
|
|
|
|
|
|
|
|
// Let's check if the reason for failure was given.
|
|
|
|
ConstElementPtr text = rsp->get("text");
|
|
|
|
ASSERT_TRUE(text);
|
|
|
|
ASSERT_EQ(Element::string, text->getType());
|
|
|
|
EXPECT_EQ(exp_txt, text->stringValue());
|
|
|
|
} else {
|
|
|
|
ADD_FAILURE() << "Invalid expected status: " << exp_status;
|
|
|
|
}
|
|
|
|
}
|
2012-06-08 15:27:45 +02:00
|
|
|
};
|
|
|
|
|
2015-07-14 20:03:20 +02:00
|
|
|
TEST_F(CtrlChannelDhcpv4SrvTest, commands) {
|
2012-06-08 15:27:45 +02:00
|
|
|
|
2013-04-11 13:30:32 +01:00
|
|
|
ASSERT_NO_THROW(
|
2015-07-14 20:30:38 +02:00
|
|
|
server_.reset(new NakedControlledDhcpv4Srv());
|
2013-04-11 13:30:32 +01:00
|
|
|
);
|
2012-06-08 15:27:45 +02:00
|
|
|
|
2013-04-11 13:30:32 +01:00
|
|
|
// Use empty parameters list
|
2012-06-08 20:39:01 +02:00
|
|
|
ElementPtr params(new isc::data::MapElement());
|
|
|
|
int rcode = -1;
|
|
|
|
|
2013-04-11 13:30:32 +01:00
|
|
|
// Case 1: send bogus command
|
2014-05-21 16:16:18 +02:00
|
|
|
ConstElementPtr result = ControlledDhcpv4Srv::processCommand("blah", params);
|
2012-06-08 20:39:01 +02:00
|
|
|
ConstElementPtr comment = parseAnswer(rcode, result);
|
|
|
|
EXPECT_EQ(1, rcode); // expect failure (no such command as blah)
|
|
|
|
|
2013-04-11 13:30:32 +01:00
|
|
|
// Case 2: send shutdown command without any parameters
|
2014-05-21 16:16:18 +02:00
|
|
|
result = ControlledDhcpv4Srv::processCommand("shutdown", params);
|
2012-06-08 20:39:01 +02:00
|
|
|
comment = parseAnswer(rcode, result);
|
|
|
|
EXPECT_EQ(0, rcode); // expect success
|
|
|
|
|
|
|
|
const pid_t pid(getpid());
|
|
|
|
ConstElementPtr x(new isc::data::IntElement(pid));
|
|
|
|
params->set("pid", x);
|
|
|
|
|
2013-04-11 13:30:32 +01:00
|
|
|
// Case 3: send shutdown command with 1 parameter: pid
|
2014-05-21 16:16:18 +02:00
|
|
|
result = ControlledDhcpv4Srv::processCommand("shutdown", params);
|
2012-06-08 20:39:01 +02:00
|
|
|
comment = parseAnswer(rcode, result);
|
|
|
|
EXPECT_EQ(0, rcode); // expect success
|
2012-06-08 15:27:45 +02:00
|
|
|
}
|
|
|
|
|
2013-07-25 13:20:02 +01:00
|
|
|
// Check that the "libreload" command will reload libraries
|
|
|
|
|
2015-07-14 20:03:20 +02:00
|
|
|
TEST_F(CtrlChannelDhcpv4SrvTest, libreload) {
|
2016-11-22 14:09:22 -05:00
|
|
|
createUnixChannelServer();
|
2014-05-21 16:16:18 +02:00
|
|
|
|
2013-07-25 13:20:02 +01:00
|
|
|
// Ensure no marker files to start with.
|
|
|
|
ASSERT_FALSE(checkMarkerFileExists(LOAD_MARKER_FILE));
|
|
|
|
ASSERT_FALSE(checkMarkerFileExists(UNLOAD_MARKER_FILE));
|
|
|
|
|
|
|
|
// Load two libraries
|
2016-03-09 18:59:06 +01:00
|
|
|
HookLibsCollection libraries;
|
|
|
|
libraries.push_back(make_pair(CALLOUT_LIBRARY_1, ConstElementPtr()));
|
|
|
|
libraries.push_back(make_pair(CALLOUT_LIBRARY_2, ConstElementPtr()));
|
2013-07-25 13:20:02 +01:00
|
|
|
HooksManager::loadLibraries(libraries);
|
|
|
|
|
|
|
|
// Check they are loaded.
|
|
|
|
std::vector<std::string> loaded_libraries =
|
|
|
|
HooksManager::getLibraryNames();
|
2016-03-09 18:59:06 +01:00
|
|
|
ASSERT_TRUE(extractNames(libraries) == loaded_libraries);
|
2013-07-25 13:20:02 +01:00
|
|
|
|
|
|
|
// ... which also included checking that the marker file created by the
|
2013-07-25 16:11:34 +01:00
|
|
|
// load functions exists and holds the correct value (of "12" - the
|
|
|
|
// first library appends "1" to the file, the second appends "2"). Also
|
|
|
|
// check that the unload marker file does not yet exist.
|
2013-07-25 13:20:02 +01:00
|
|
|
EXPECT_TRUE(checkMarkerFile(LOAD_MARKER_FILE, "12"));
|
|
|
|
EXPECT_FALSE(checkMarkerFileExists(UNLOAD_MARKER_FILE));
|
|
|
|
|
|
|
|
// Now execute the "libreload" command. This should cause the libraries
|
|
|
|
// to unload and to reload.
|
2016-11-22 14:09:22 -05:00
|
|
|
std::string response;
|
|
|
|
sendUnixCommand("{ \"command\": \"libreload\" }", response);
|
|
|
|
EXPECT_EQ("{ \"result\": 0, "
|
|
|
|
"\"text\": \"Hooks libraries successfully reloaded.\" }"
|
|
|
|
, response);
|
2013-07-25 13:20:02 +01:00
|
|
|
|
|
|
|
// Check that the libraries have unloaded and reloaded. The libraries are
|
|
|
|
// unloaded in the reverse order to which they are loaded. When they load,
|
|
|
|
// they should append information to the loading marker file.
|
|
|
|
EXPECT_TRUE(checkMarkerFile(UNLOAD_MARKER_FILE, "21"));
|
|
|
|
EXPECT_TRUE(checkMarkerFile(LOAD_MARKER_FILE, "1212"));
|
|
|
|
}
|
|
|
|
|
2015-06-03 16:32:59 +02:00
|
|
|
// This test checks which commands are registered by the DHCPv4 server.
|
2015-07-14 20:03:20 +02:00
|
|
|
TEST_F(CtrlChannelDhcpv4SrvTest, commandsRegistration) {
|
2015-06-03 16:32:59 +02:00
|
|
|
|
|
|
|
ConstElementPtr list_cmds = createCommand("list-commands");
|
|
|
|
ConstElementPtr answer;
|
|
|
|
|
|
|
|
// By default the list should be empty (except the standard list-commands
|
|
|
|
// supported by the CommandMgr itself)
|
|
|
|
EXPECT_NO_THROW(answer = CommandMgr::instance().processCommand(list_cmds));
|
|
|
|
ASSERT_TRUE(answer);
|
|
|
|
ASSERT_TRUE(answer->get("arguments"));
|
|
|
|
EXPECT_EQ("[ \"list-commands\" ]", answer->get("arguments")->str());
|
|
|
|
|
|
|
|
// Created server should register several additional commands.
|
|
|
|
ASSERT_NO_THROW(
|
2015-07-14 20:30:38 +02:00
|
|
|
server_.reset(new NakedControlledDhcpv4Srv());
|
2015-06-03 16:32:59 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
EXPECT_NO_THROW(answer = CommandMgr::instance().processCommand(list_cmds));
|
|
|
|
ASSERT_TRUE(answer);
|
|
|
|
ASSERT_TRUE(answer->get("arguments"));
|
2015-07-14 20:03:20 +02:00
|
|
|
std::string command_list = answer->get("arguments")->str();
|
|
|
|
|
|
|
|
EXPECT_TRUE(command_list.find("\"list-commands\"") != string::npos);
|
|
|
|
EXPECT_TRUE(command_list.find("\"statistic-get\"") != string::npos);
|
|
|
|
EXPECT_TRUE(command_list.find("\"statistic-get-all\"") != string::npos);
|
|
|
|
EXPECT_TRUE(command_list.find("\"statistic-remove\"") != string::npos);
|
|
|
|
EXPECT_TRUE(command_list.find("\"statistic-remove-all\"") != string::npos);
|
|
|
|
EXPECT_TRUE(command_list.find("\"statistic-reset\"") != string::npos);
|
|
|
|
EXPECT_TRUE(command_list.find("\"statistic-reset-all\"") != string::npos);
|
2015-06-03 16:32:59 +02:00
|
|
|
|
|
|
|
// Ok, and now delete the server. It should deregister its commands.
|
2015-07-14 20:30:38 +02:00
|
|
|
server_.reset();
|
2015-06-03 16:32:59 +02:00
|
|
|
|
|
|
|
// The list should be (almost) empty again.
|
|
|
|
EXPECT_NO_THROW(answer = CommandMgr::instance().processCommand(list_cmds));
|
|
|
|
ASSERT_TRUE(answer);
|
|
|
|
ASSERT_TRUE(answer->get("arguments"));
|
|
|
|
EXPECT_EQ("[ \"list-commands\" ]", answer->get("arguments")->str());
|
|
|
|
}
|
|
|
|
|
2015-07-14 20:03:20 +02:00
|
|
|
// Tests that the server properly responds to invalid commands sent
|
|
|
|
// via ControlChannel
|
|
|
|
TEST_F(CtrlChannelDhcpv4SrvTest, controlChannelNegative) {
|
|
|
|
createUnixChannelServer();
|
|
|
|
std::string response;
|
|
|
|
|
|
|
|
sendUnixCommand("{ \"command\": \"bogus\" }", response);
|
|
|
|
EXPECT_EQ("{ \"result\": 1,"
|
|
|
|
" \"text\": \"'bogus' command not supported.\" }", response);
|
|
|
|
|
|
|
|
sendUnixCommand("utter nonsense", response);
|
|
|
|
EXPECT_EQ("{ \"result\": 1, "
|
|
|
|
"\"text\": \"error: unexpected character u in <string>:1:2\" }",
|
|
|
|
response);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that the server properly responds to shtudown command sent
|
|
|
|
// via ControlChannel
|
|
|
|
TEST_F(CtrlChannelDhcpv4SrvTest, controlChannelShutdown) {
|
|
|
|
createUnixChannelServer();
|
|
|
|
std::string response;
|
|
|
|
|
|
|
|
sendUnixCommand("{ \"command\": \"shutdown\" }", response);
|
|
|
|
EXPECT_EQ("{ \"result\": 0, \"text\": \"Shutting down.\" }",response);
|
|
|
|
}
|
|
|
|
|
2015-10-23 04:23:45 +02:00
|
|
|
// This test verifies that the DHCP server immediately reclaims expired
|
2015-10-17 11:22:24 +02:00
|
|
|
// leases on leases-reclaim command
|
|
|
|
TEST_F(CtrlChannelDhcpv4SrvTest, controlLeasesReclaim) {
|
|
|
|
createUnixChannelServer();
|
|
|
|
|
2015-10-23 04:23:45 +02:00
|
|
|
// Create expired leases. Leases are expired by 40 seconds ago
|
2015-10-17 11:22:24 +02:00
|
|
|
// (valid lifetime = 60, cltt = now - 100).
|
2015-10-23 04:23:45 +02:00
|
|
|
HWAddrPtr hwaddr0(new HWAddr(HWAddr::fromText("00:01:02:03:04:05")));
|
|
|
|
Lease4Ptr lease0(new Lease4(IOAddress("10.0.0.1"), hwaddr0,
|
|
|
|
ClientIdPtr(), 60, 10, 20,
|
|
|
|
time(NULL) - 100, SubnetID(1)));
|
|
|
|
HWAddrPtr hwaddr1(new HWAddr(HWAddr::fromText("01:02:03:04:05:06")));
|
|
|
|
Lease4Ptr lease1(new Lease4(IOAddress("10.0.0.2"), hwaddr1,
|
|
|
|
ClientIdPtr(), 60, 10, 20,
|
|
|
|
time(NULL) - 100, SubnetID(1)));
|
|
|
|
|
|
|
|
// Add leases to the database.
|
2015-11-02 12:30:58 +09:00
|
|
|
LeaseMgr& lease_mgr = LeaseMgrFactory::instance();
|
2015-10-23 04:23:45 +02:00
|
|
|
ASSERT_NO_THROW(lease_mgr.addLease(lease0));
|
|
|
|
ASSERT_NO_THROW(lease_mgr.addLease(lease1));
|
2015-10-17 11:22:24 +02:00
|
|
|
|
2015-10-23 04:23:45 +02:00
|
|
|
// Make sure they have been added.
|
2015-10-17 11:22:24 +02:00
|
|
|
ASSERT_TRUE(lease_mgr.getLease4(IOAddress("10.0.0.1")));
|
2015-10-23 04:23:45 +02:00
|
|
|
ASSERT_TRUE(lease_mgr.getLease4(IOAddress("10.0.0.2")));
|
2015-10-17 11:22:24 +02:00
|
|
|
|
2015-10-23 04:23:45 +02:00
|
|
|
// No arguments
|
2015-10-17 11:22:24 +02:00
|
|
|
std::string response;
|
|
|
|
sendUnixCommand("{ \"command\": \"leases-reclaim\" }", response);
|
2015-10-23 04:23:45 +02:00
|
|
|
EXPECT_EQ("{ \"result\": 1, \"text\": "
|
|
|
|
"\"Missing mandatory 'remove' parameter.\" }", response);
|
|
|
|
|
|
|
|
// Bad argument name
|
|
|
|
sendUnixCommand("{ \"command\": \"leases-reclaim\", "
|
|
|
|
"\"arguments\": { \"reclaim\": true } }", response);
|
|
|
|
EXPECT_EQ("{ \"result\": 1, \"text\": "
|
|
|
|
"\"Missing mandatory 'remove' parameter.\" }", response);
|
|
|
|
|
|
|
|
// Bad remove argument type
|
|
|
|
sendUnixCommand("{ \"command\": \"leases-reclaim\", "
|
|
|
|
"\"arguments\": { \"remove\": \"bogus\" } }", response);
|
|
|
|
EXPECT_EQ("{ \"result\": 1, \"text\": "
|
|
|
|
"\"'remove' parameter expected to be a boolean.\" }", response);
|
|
|
|
|
|
|
|
// Send the command
|
|
|
|
sendUnixCommand("{ \"command\": \"leases-reclaim\", "
|
|
|
|
"\"arguments\": { \"remove\": false } }", response);
|
|
|
|
EXPECT_EQ("{ \"result\": 0, \"text\": "
|
|
|
|
"\"Reclamation of expired leases is complete.\" }", response);
|
|
|
|
|
|
|
|
// Leases should be reclaimed, but not removed
|
|
|
|
ASSERT_NO_THROW(lease0 = lease_mgr.getLease4(IOAddress("10.0.0.1")));
|
|
|
|
ASSERT_NO_THROW(lease1 = lease_mgr.getLease4(IOAddress("10.0.0.2")));
|
|
|
|
ASSERT_TRUE(lease0);
|
|
|
|
ASSERT_TRUE(lease1);
|
|
|
|
EXPECT_TRUE(lease0->stateExpiredReclaimed());
|
|
|
|
EXPECT_TRUE(lease1->stateExpiredReclaimed());
|
|
|
|
}
|
|
|
|
|
2016-12-14 16:57:44 +02:00
|
|
|
// This test verifies that the DHCP server immediately removed expired
|
2015-10-23 04:23:45 +02:00
|
|
|
// leases on leases-reclaim command with remove = true
|
|
|
|
TEST_F(CtrlChannelDhcpv4SrvTest, controlLeasesReclaimRemove) {
|
|
|
|
createUnixChannelServer();
|
|
|
|
|
|
|
|
// Create expired leases. Leases are expired by 40 seconds ago
|
|
|
|
// (valid lifetime = 60, cltt = now - 100).
|
|
|
|
HWAddrPtr hwaddr0(new HWAddr(HWAddr::fromText("00:01:02:03:04:05")));
|
|
|
|
Lease4Ptr lease0(new Lease4(IOAddress("10.0.0.1"), hwaddr0,
|
|
|
|
ClientIdPtr(), 60, 10, 20,
|
|
|
|
time(NULL) - 100, SubnetID(1)));
|
|
|
|
HWAddrPtr hwaddr1(new HWAddr(HWAddr::fromText("01:02:03:04:05:06")));
|
|
|
|
Lease4Ptr lease1(new Lease4(IOAddress("10.0.0.2"), hwaddr1,
|
|
|
|
ClientIdPtr(), 60, 10, 20,
|
|
|
|
time(NULL) - 100, SubnetID(1)));
|
|
|
|
|
|
|
|
// Add leases to the database.
|
2015-11-02 12:30:58 +09:00
|
|
|
LeaseMgr& lease_mgr = LeaseMgrFactory::instance();
|
2015-10-23 04:23:45 +02:00
|
|
|
ASSERT_NO_THROW(lease_mgr.addLease(lease0));
|
|
|
|
ASSERT_NO_THROW(lease_mgr.addLease(lease1));
|
2015-10-17 11:22:24 +02:00
|
|
|
|
2015-10-23 04:23:45 +02:00
|
|
|
// Make sure they have been added.
|
|
|
|
ASSERT_TRUE(lease_mgr.getLease4(IOAddress("10.0.0.1")));
|
|
|
|
ASSERT_TRUE(lease_mgr.getLease4(IOAddress("10.0.0.2")));
|
|
|
|
|
|
|
|
// Send the command
|
|
|
|
std::string response;
|
|
|
|
sendUnixCommand("{ \"command\": \"leases-reclaim\", "
|
|
|
|
"\"arguments\": { \"remove\": true } }", response);
|
|
|
|
EXPECT_EQ("{ \"result\": 0, \"text\": "
|
|
|
|
"\"Reclamation of expired leases is complete.\" }", response);
|
|
|
|
|
|
|
|
// Leases should have been removed.
|
|
|
|
ASSERT_NO_THROW(lease0 = lease_mgr.getLease4(IOAddress("10.0.0.1")));
|
|
|
|
ASSERT_NO_THROW(lease1 = lease_mgr.getLease4(IOAddress("10.0.0.2")));
|
|
|
|
EXPECT_FALSE(lease0);
|
|
|
|
EXPECT_FALSE(lease1);
|
2015-10-17 11:22:24 +02:00
|
|
|
}
|
|
|
|
|
2015-07-14 20:03:20 +02:00
|
|
|
// Tests that the server properly responds to statistics commands. Note this
|
|
|
|
// is really only intended to verify that the appropriate Statistics handler
|
|
|
|
// is called based on the command. It is not intended to be an exhaustive
|
2015-07-14 20:30:38 +02:00
|
|
|
// test of Dhcpv4 statistics.
|
2015-07-14 20:03:20 +02:00
|
|
|
TEST_F(CtrlChannelDhcpv4SrvTest, controlChannelStats) {
|
|
|
|
createUnixChannelServer();
|
|
|
|
std::string response;
|
|
|
|
|
|
|
|
// Check statistic-get
|
|
|
|
sendUnixCommand("{ \"command\" : \"statistic-get\", "
|
|
|
|
" \"arguments\": {"
|
|
|
|
" \"name\":\"bogus\" }}", response);
|
|
|
|
EXPECT_EQ("{ \"arguments\": { }, \"result\": 0 }", response);
|
|
|
|
|
|
|
|
// Check statistic-get-all
|
|
|
|
sendUnixCommand("{ \"command\" : \"statistic-get-all\", "
|
|
|
|
" \"arguments\": {}}", response);
|
|
|
|
EXPECT_EQ("{ \"arguments\": { }, \"result\": 0 }", response);
|
|
|
|
|
|
|
|
// Check statistic-reset
|
|
|
|
sendUnixCommand("{ \"command\" : \"statistic-reset\", "
|
|
|
|
" \"arguments\": {"
|
|
|
|
" \"name\":\"bogus\" }}", response);
|
|
|
|
EXPECT_EQ("{ \"result\": 1, \"text\": \"No 'bogus' statistic found\" }",
|
|
|
|
response);
|
|
|
|
|
|
|
|
// Check statistic-reset-all
|
|
|
|
sendUnixCommand("{ \"command\" : \"statistic-reset-all\", "
|
|
|
|
" \"arguments\": {}}", response);
|
|
|
|
EXPECT_EQ("{ \"result\": 0, \"text\": "
|
|
|
|
"\"All statistics reset to neutral values.\" }", response);
|
|
|
|
|
|
|
|
// Check statistic-remove
|
|
|
|
sendUnixCommand("{ \"command\" : \"statistic-remove\", "
|
|
|
|
" \"arguments\": {"
|
|
|
|
" \"name\":\"bogus\" }}", response);
|
|
|
|
EXPECT_EQ("{ \"result\": 1, \"text\": \"No 'bogus' statistic found\" }",
|
|
|
|
response);
|
|
|
|
|
|
|
|
// Check statistic-remove-all
|
|
|
|
sendUnixCommand("{ \"command\" : \"statistic-remove-all\", "
|
|
|
|
" \"arguments\": {}}", response);
|
|
|
|
EXPECT_EQ("{ \"result\": 0, \"text\": \"All statistics removed.\" }",
|
|
|
|
response);
|
|
|
|
}
|
|
|
|
|
2016-12-16 11:02:04 -05:00
|
|
|
// Check that the "set-config" command will replace current configuration
|
|
|
|
TEST_F(CtrlChannelDhcpv4SrvTest, set_config) {
|
|
|
|
createUnixChannelServer();
|
|
|
|
|
|
|
|
// Define strings to permutate the config arguments
|
|
|
|
// (Note the line feeds makes errors easy to find)
|
|
|
|
string set_config_txt = "{ \"command\": \"set-config\" \n";
|
|
|
|
string args_txt = " \"arguments\": { \n";
|
|
|
|
string dhcp4_cfg_txt =
|
|
|
|
" \"Dhcp4\": { \n"
|
|
|
|
" \"interfaces-config\": { \n"
|
|
|
|
" \"interfaces\": [\"*\"] \n"
|
|
|
|
" }, \n"
|
|
|
|
" \"valid-lifetime\": 4000, \n"
|
|
|
|
" \"renew-timer\": 1000, \n"
|
|
|
|
" \"rebind-timer\": 2000, \n"
|
2017-01-03 08:23:44 -05:00
|
|
|
" \"lease-database\": { \n"
|
|
|
|
" \"type\": \"memfile\", \n"
|
|
|
|
" \"persist\":false, \n"
|
|
|
|
" \"lfc-interval\": 0 \n"
|
|
|
|
" }, \n"
|
2016-12-16 11:02:04 -05:00
|
|
|
" \"expired-leases-processing\": { \n"
|
|
|
|
" \"reclaim-timer-wait-time\": 0, \n"
|
|
|
|
" \"hold-reclaimed-time\": 0, \n"
|
|
|
|
" \"flush-reclaimed-timer-wait-time\": 0 \n"
|
|
|
|
" },"
|
|
|
|
" \"subnet4\": [ \n";
|
|
|
|
string subnet1 =
|
|
|
|
" {\"subnet\": \"192.2.0.0/24\", \n"
|
|
|
|
" \"pools\": [{ \"pool\": \"192.2.0.1-192.2.0.50\" }]}\n";
|
|
|
|
string subnet2 =
|
|
|
|
" {\"subnet\": \"192.2.1.0/24\", \n"
|
|
|
|
" \"pools\": [{ \"pool\": \"192.2.1.1-192.2.1.50\" }]}\n";
|
|
|
|
string bad_subnet =
|
|
|
|
" {\"BOGUS\": \"192.2.2.0/24\", \n"
|
|
|
|
" \"pools\": [{ \"pool\": \"192.2.2.1-192.2.2.50\" }]}\n";
|
|
|
|
string subnet_footer =
|
|
|
|
" ] \n";
|
|
|
|
string control_socket_header =
|
|
|
|
" ,\"control-socket\": { \n"
|
|
|
|
" \"socket-type\": \"unix\", \n"
|
|
|
|
" \"socket-name\": \"";
|
|
|
|
string control_socket_footer =
|
|
|
|
"\" \n} \n";
|
|
|
|
string logger_txt =
|
|
|
|
" \"Logging\": { \n"
|
|
|
|
" \"loggers\": [ { \n"
|
|
|
|
" \"name\": \"kea\", \n"
|
|
|
|
" \"severity\": \"FATAL\", \n"
|
|
|
|
" \"output_options\": [{ \n"
|
|
|
|
" \"output\": \"/dev/null\" \n"
|
|
|
|
" }] \n"
|
|
|
|
" }] \n"
|
|
|
|
" } \n";
|
|
|
|
|
|
|
|
std::ostringstream os;
|
|
|
|
|
|
|
|
// Create a valid config with all the parts should parse
|
|
|
|
os << set_config_txt << ","
|
|
|
|
<< args_txt
|
|
|
|
<< dhcp4_cfg_txt
|
|
|
|
<< subnet1
|
|
|
|
<< subnet_footer
|
|
|
|
<< control_socket_header
|
|
|
|
<< socket_path_
|
|
|
|
<< control_socket_footer
|
|
|
|
<< "}\n" // close dhcp4
|
|
|
|
<< ","
|
|
|
|
<< logger_txt
|
|
|
|
<< "}}";
|
|
|
|
|
|
|
|
// Send the set-config command
|
|
|
|
std::string response;
|
|
|
|
sendUnixCommand(os.str(), response);
|
|
|
|
|
|
|
|
// Verify the configuration was successful.
|
|
|
|
EXPECT_EQ("{ \"result\": 0, \"text\": \"Configuration successful.\" }",
|
|
|
|
response);
|
|
|
|
|
|
|
|
// Check that the config was indeed applied.
|
|
|
|
const Subnet4Collection* subnets =
|
|
|
|
CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->getAll();
|
|
|
|
EXPECT_EQ(1, subnets->size());
|
|
|
|
|
|
|
|
// Create a config with malformed subnet that should fail to parse.
|
|
|
|
os.str("");
|
|
|
|
os << set_config_txt << ","
|
|
|
|
<< args_txt
|
|
|
|
<< dhcp4_cfg_txt
|
|
|
|
<< bad_subnet
|
|
|
|
<< subnet_footer
|
|
|
|
<< control_socket_header
|
|
|
|
<< socket_path_
|
|
|
|
<< control_socket_footer
|
|
|
|
<< "}\n" // close dhcp4
|
|
|
|
"}}";
|
|
|
|
|
|
|
|
// Send the set-config command
|
|
|
|
sendUnixCommand(os.str(), response);
|
|
|
|
|
|
|
|
// Should fail with a syntax error
|
|
|
|
EXPECT_EQ("{ \"result\": 1, "
|
2017-01-28 12:01:23 +01:00
|
|
|
"\"text\": \"subnet configuration failed: mandatory 'subnet' parameter is missing for a subnet being configured (<string>:20:17)\" }",
|
2016-12-16 11:02:04 -05:00
|
|
|
response);
|
|
|
|
|
|
|
|
// Check that the config was not lost
|
|
|
|
subnets = CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->getAll();
|
|
|
|
EXPECT_EQ(1, subnets->size());
|
|
|
|
|
|
|
|
// Create a valid config with two subnets and no command channel.
|
2016-12-22 13:38:00 -05:00
|
|
|
// It should succeed, client should still receive the response
|
2016-12-16 11:02:04 -05:00
|
|
|
os.str("");
|
|
|
|
os << set_config_txt << ","
|
|
|
|
<< args_txt
|
|
|
|
<< dhcp4_cfg_txt
|
|
|
|
<< subnet1
|
|
|
|
<< ",\n"
|
|
|
|
<< subnet2
|
|
|
|
<< subnet_footer
|
|
|
|
<< "}\n" // close dhcp4
|
|
|
|
<< "}}";
|
|
|
|
|
2016-12-22 13:38:00 -05:00
|
|
|
/* Verify the control channel socket exists */
|
|
|
|
ASSERT_TRUE(fileExists(socket_path_));
|
|
|
|
|
2016-12-16 11:02:04 -05:00
|
|
|
// Send the set-config command
|
|
|
|
sendUnixCommand(os.str(), response);
|
|
|
|
|
2016-12-22 13:38:00 -05:00
|
|
|
/* Verify the control channel socket no longer exists */
|
|
|
|
EXPECT_FALSE(fileExists(socket_path_));
|
|
|
|
|
|
|
|
// With no command channel, should still receive the response.
|
|
|
|
EXPECT_EQ("{ \"result\": 0, \"text\": \"Configuration successful.\" }",
|
|
|
|
response);
|
2016-12-16 11:02:04 -05:00
|
|
|
|
|
|
|
// Check that the config was not lost
|
|
|
|
subnets = CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->getAll();
|
|
|
|
EXPECT_EQ(2, subnets->size());
|
|
|
|
|
|
|
|
// Clean up after the test.
|
|
|
|
CfgMgr::instance().clear();
|
|
|
|
}
|
|
|
|
|
2017-03-14 21:06:56 +01:00
|
|
|
// Tests that the server properly responds to shtudown command sent
|
|
|
|
// via ControlChannel
|
|
|
|
TEST_F(CtrlChannelDhcpv4SrvTest, listCommands) {
|
|
|
|
createUnixChannelServer();
|
|
|
|
std::string response;
|
|
|
|
|
|
|
|
sendUnixCommand("{ \"command\": \"list-commands\" }", response);
|
|
|
|
|
|
|
|
ConstElementPtr rsp;
|
|
|
|
EXPECT_NO_THROW(rsp = Element::fromJSON(response));
|
|
|
|
|
|
|
|
// We expect the server to report at least the following commands:
|
|
|
|
checkListCommands(rsp, "get-config");
|
|
|
|
checkListCommands(rsp, "list-commands");
|
|
|
|
checkListCommands(rsp, "leases-reclaim");
|
|
|
|
checkListCommands(rsp, "libreload");
|
|
|
|
checkListCommands(rsp, "set-config");
|
|
|
|
checkListCommands(rsp, "shutdown");
|
|
|
|
checkListCommands(rsp, "statistic-get");
|
|
|
|
checkListCommands(rsp, "statistic-get-all");
|
|
|
|
checkListCommands(rsp, "statistic-remove");
|
|
|
|
checkListCommands(rsp, "statistic-remove-all");
|
|
|
|
checkListCommands(rsp, "statistic-reset");
|
|
|
|
checkListCommands(rsp, "statistic-reset-all");
|
|
|
|
checkListCommands(rsp, "write-config");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests if the server returns its configuration using get-config.
|
|
|
|
// Note there are separate tests that verify if toElement() called by the
|
|
|
|
// get-config handler are actually converting the configuration correctly.
|
|
|
|
TEST_F(CtrlChannelDhcpv4SrvTest, getConfig) {
|
|
|
|
createUnixChannelServer();
|
|
|
|
std::string response;
|
|
|
|
|
|
|
|
sendUnixCommand("{ \"command\": \"get-config\" }", response);
|
|
|
|
ConstElementPtr rsp;
|
|
|
|
|
|
|
|
// The response should be a valid JSON.
|
|
|
|
EXPECT_NO_THROW(rsp = Element::fromJSON(response));
|
|
|
|
ASSERT_TRUE(rsp);
|
|
|
|
|
|
|
|
int status;
|
|
|
|
ConstElementPtr cfg = parseAnswer(status, rsp);
|
|
|
|
EXPECT_EQ(CONTROL_RESULT_SUCCESS, status);
|
|
|
|
|
|
|
|
// Ok, now roughly check if the response seems legit.
|
|
|
|
ASSERT_TRUE(cfg);
|
|
|
|
EXPECT_EQ(Element::map, cfg->getType());
|
|
|
|
EXPECT_TRUE(cfg->get("Dhcp4"));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(CtrlChannelDhcpv4SrvTest, writeConfigNoFilename) {
|
|
|
|
createUnixChannelServer();
|
|
|
|
std::string response;
|
|
|
|
|
|
|
|
// This is normally set by the command line -c parameter.
|
|
|
|
server_->setConfigFile("test1.json");
|
|
|
|
|
|
|
|
// If the filename is not explicitly specified, the name used
|
|
|
|
// in -c command line switch is used.
|
|
|
|
sendUnixCommand("{ \"command\": \"write-config\" }", response);
|
|
|
|
|
|
|
|
checkWriteConfig(response, CONTROL_RESULT_SUCCESS, "test1.json");
|
|
|
|
::remove("test1.json");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CtrlChannelDhcpv4SrvTest, writeConfigFilename) {
|
|
|
|
createUnixChannelServer();
|
|
|
|
std::string response;
|
|
|
|
|
|
|
|
sendUnixCommand("{ \"command\": \"write-config\", "
|
|
|
|
"\"arguments\": { \"filename\": \"test2.json\" } }", response);
|
|
|
|
checkWriteConfig(response, CONTROL_RESULT_SUCCESS, "test2.json");
|
|
|
|
::remove("test2.json");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CtrlChannelDhcpv4SrvTest, writeConfigInvalidJailEscape) {
|
|
|
|
createUnixChannelServer();
|
|
|
|
std::string response;
|
|
|
|
|
|
|
|
sendUnixCommand("{ \"command\": \"write-config\", \"arguments\": "
|
|
|
|
"{ \"filename\": \"../test3.json\" } }", response);
|
|
|
|
checkWriteConfig(response, CONTROL_RESULT_ERROR,
|
|
|
|
"Using '..' in filename is not allowed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CtrlChannelDhcpv4SrvTest, writeConfigInvalidAbsPath) {
|
|
|
|
createUnixChannelServer();
|
|
|
|
std::string response;
|
|
|
|
|
|
|
|
sendUnixCommand("{ \"command\": \"write-config\", \"arguments\": "
|
|
|
|
"{ \"filename\": \"/tmp/test4.json\" } }", response);
|
|
|
|
checkWriteConfig(response, CONTROL_RESULT_ERROR,
|
|
|
|
"Absolute path in filename is not allowed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CtrlChannelDhcpv4SrvTest, writeConfigInvalidEscape) {
|
|
|
|
createUnixChannelServer();
|
|
|
|
std::string response;
|
|
|
|
|
|
|
|
// This will be converted to foo(single backslash)test5.json
|
|
|
|
sendUnixCommand("{ \"command\": \"write-config\", \"arguments\": "
|
|
|
|
"{ \"filename\": \"foo\\\\test5.json\" } }", response);
|
|
|
|
checkWriteConfig(response, CONTROL_RESULT_ERROR,
|
|
|
|
"Using \\ in filename is not allowed.");
|
|
|
|
}
|
|
|
|
|
2013-04-11 13:30:32 +01:00
|
|
|
} // End of anonymous namespace
|