2
0
mirror of https://gitlab.isc.org/isc-projects/kea synced 2025-08-23 02:17:33 +00:00
kea/src/bin/dhcp4/tests/ctrl_dhcp4_srv_unittest.cc

384 lines
14 KiB
C++
Raw Normal View History

// Copyright (C) 2012-2013 Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
#include <config.h>
#include <cc/command_interpreter.h>
#include <config/command_mgr.h>
#include <dhcp/dhcp4.h>
#include <dhcp4/ctrl_dhcp4_srv.h>
#include <dhcpsrv/cfgmgr.h>
#include <hooks/hooks_manager.h>
#include <testutils/unix_control_client.h>
#include "marker_file.h"
#include "test_libraries.h"
#include <boost/scoped_ptr.hpp>
#include <gtest/gtest.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <arpa/inet.h>
#include <unistd.h>
using namespace std;
using namespace isc;
using namespace isc::asiolink;
using namespace isc::config;
using namespace isc::data;
using namespace isc::dhcp;
using namespace isc::dhcp::test;
using namespace isc::hooks;
namespace {
class NakedControlledDhcpv4Srv: public ControlledDhcpv4Srv {
// "Naked" DHCPv4 server, exposes internal fields
public:
NakedControlledDhcpv4Srv():ControlledDhcpv4Srv(0) { }
/// Expose internal methods for the sake of testing
using Dhcpv4Srv::receivePacket;
};
/// @brief Fixture class intended for testin control channel in the DHCPv4Srv
class CtrlChannelDhcpv4SrvTest : public ::testing::Test {
public:
/// @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";
}
reset();
}
/// @brief Destructor
~CtrlChannelDhcpv4SrvTest() {
server_.reset();
reset();
};
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\": [ \"*\" ]"
" },"
" \"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()));
ConstElementPtr config = Element::fromJSON(config_txt);
ConstElementPtr answer = server_->processConfig(config);
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);
}
/// @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));
IfaceMgr::instance().deleteAllExternalSockets();
CfgMgr::instance().clear();
// Remove unix socket file
::remove(socket_path_.c_str());
}
/// @brief Conducts a command/response exchange via UnixCommandSocket
///
/// 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.
///
/// @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));
}
};
TEST_F(CtrlChannelDhcpv4SrvTest, commands) {
ASSERT_NO_THROW(
server_.reset(new NakedControlledDhcpv4Srv());
);
// Use empty parameters list
ElementPtr params(new isc::data::MapElement());
int rcode = -1;
// Case 1: send bogus command
2014-05-21 16:16:18 +02:00
ConstElementPtr result = ControlledDhcpv4Srv::processCommand("blah", params);
ConstElementPtr comment = parseAnswer(rcode, result);
EXPECT_EQ(1, rcode); // expect failure (no such command as blah)
// Case 2: send shutdown command without any parameters
2014-05-21 16:16:18 +02:00
result = ControlledDhcpv4Srv::processCommand("shutdown", params);
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);
// Case 3: send shutdown command with 1 parameter: pid
2014-05-21 16:16:18 +02:00
result = ControlledDhcpv4Srv::processCommand("shutdown", params);
comment = parseAnswer(rcode, result);
EXPECT_EQ(0, rcode); // expect success
}
// Check that the "libreload" command will reload libraries
TEST_F(CtrlChannelDhcpv4SrvTest, libreload) {
2014-05-21 16:16:18 +02:00
// Sending commands for processing now requires a server that can process
// them.
ASSERT_NO_THROW(
server_.reset(new NakedControlledDhcpv4Srv());
2014-05-21 16:16:18 +02:00
);
// Ensure no marker files to start with.
ASSERT_FALSE(checkMarkerFileExists(LOAD_MARKER_FILE));
ASSERT_FALSE(checkMarkerFileExists(UNLOAD_MARKER_FILE));
// Load two libraries
std::vector<std::string> libraries;
libraries.push_back(CALLOUT_LIBRARY_1);
libraries.push_back(CALLOUT_LIBRARY_2);
HooksManager::loadLibraries(libraries);
// Check they are loaded.
std::vector<std::string> loaded_libraries =
HooksManager::getLibraryNames();
ASSERT_TRUE(libraries == loaded_libraries);
// ... which also included checking that the marker file created by the
// 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.
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.
// Use empty parameters list
ElementPtr params(new isc::data::MapElement());
int rcode = -1;
ConstElementPtr result =
2014-05-21 16:16:18 +02:00
ControlledDhcpv4Srv::processCommand("libreload", params);
ConstElementPtr comment = parseAnswer(rcode, result);
EXPECT_EQ(0, rcode); // Expect success
// 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"));
}
// This test checks which commands are registered by the DHCPv4 server.
TEST_F(CtrlChannelDhcpv4SrvTest, commandsRegistration) {
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(
server_.reset(new NakedControlledDhcpv4Srv());
);
EXPECT_NO_THROW(answer = CommandMgr::instance().processCommand(list_cmds));
ASSERT_TRUE(answer);
ASSERT_TRUE(answer->get("arguments"));
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);
// Ok, and now delete the server. It should deregister its commands.
server_.reset();
// 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());
}
// 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);
}
// 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
// test of Dhcpv4 statistics.
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);
}
} // End of anonymous namespace