/// over from previous tests
LegalLibLoadTest() : LibLoadTest(LEGAL_LOG_LIB_SO) {
reset();
+ setLogTestPath();
}
/// @brief Destructor
/// Removes files that may be left over from previous tests
virtual ~LegalLibLoadTest() {
reset();
+ resetLogPath();
}
/// @brief Removes files that may be left over from previous tests
}
}
+ /// @brief Sets the log path for log files.
+ /// @param custom_path path to use as the log file path.
+ void setLogTestPath(const std::string explicit_path = "") {
+ LegalLogMgr::getLogPath(true, (!explicit_path.empty() ?
+ explicit_path : TEST_DATA_BUILDDIR));
+ }
+
+ /// @brief Resets the log path to LEGAL_LOG_DIR.
+ void resetLogPath() {
+ LegalLogMgr::getLogPath(true);
+ }
+
/// @brief Checks if the given file exists
///
/// @return true if the file exists, false if it does not
data::ElementPtr params = data::Element::createMap();
params->set("type", data::Element::create("no-such-type"));
// Still set path and base-name in the case the library loads
- params->set("path", data::Element::create(TEST_DATA_BUILDDIR));
params->set("base-name", data::Element::create("nobody-should-use-this"));
// Attempting to Load the library should fail.
// one runs the risk of harming a legal file opened with default values
// by a live instance of Kea.
TEST_F(LegalLibLoadTest, validLoad) {
+ // Set family and daemon's proc name.
+ isc::dhcp::CfgMgr::instance().setFamily(AF_INET);
+ isc::process::Daemon::setProcName("kea-dhcp4");
// Prepare parameters for the callout parameters library.
data::ElementPtr params = data::Element::createMap();
- params->set("path", data::Element::create(TEST_DATA_BUILDDIR));
params->set("base-name", data::Element::create("test-legal"));
params->set("request-parser-format", data::Element::create("'request'"));
params->set("response-parser-format", data::Element::create("'response'"));
// Prepare parameters for the callout parameters library.
// Even if loads will fail still avoid defaults!
data::ElementPtr params = data::Element::createMap();
- params->set("path", data::Element::create(TEST_DATA_BUILDDIR));
params->set("base-name", data::Element::create("test-legal"));
// V4 is invalid when family is AF_INET6.
// Prepare parameters for the callout parameters library.
data::ElementPtr params = data::Element::createMap();
- params->set("path", data::Element::create(TEST_DATA_BUILDDIR));
params->set("base-name", data::Element::create("test-legal"));
params->set("type", data::Element::create("logfile"));
params->set("password", data::Element::create("keatest"));
// Still set path and base-name in the case the library switches to file
- params->set("path", data::Element::create(TEST_DATA_BUILDDIR));
params->set("base-name", data::Element::create("nobody-should-use-this"));
// Load the library. Is there a way to check more than return code?
params->set("password", data::Element::create("keatest"));
// Still set path and base-name in the case the library switches to file
- params->set("path", data::Element::create(TEST_DATA_BUILDDIR));
params->set("base-name", data::Element::create("nobody-should-use-this"));
// Attempting to Load the library should fail.
params->set("password", data::Element::create("keatest"));
// Still set path and base-name in the case the library switches to file
- params->set("path", data::Element::create(TEST_DATA_BUILDDIR));
params->set("base-name", data::Element::create("nobody-should-use-this"));
// Load the library. Is there a way to check more than return code?
params->set("password", data::Element::create("keatest"));
// Still set path and base-name in the case the library switches to file
- params->set("path", data::Element::create(TEST_DATA_BUILDDIR));
params->set("base-name", data::Element::create("nobody-should-use-this"));
// Attempting to Load the library should fail.
#include <dhcpsrv/legal_log_db_log.h>
#include <legal_log_log.h>
#include <rotating_file.h>
+#include <testutils/env_var_wrapper.h>
#include <gtest/gtest.h>
using namespace isc::db;
using namespace isc::dhcp;
using namespace isc::legal_log;
+using namespace isc::test;
using namespace std;
namespace {
/// @brief Test fixture
struct LegalLogMgrTest : ::testing::Test {
+ /// @brief Construtor.
+ LegalLogMgrTest() : legal_log_dir_env_var_("KEA_LEGAL_LOG_DIR") {
+ }
+
/// @brief Destructor.
virtual ~LegalLogMgrTest() = default;
/// @brief Called before each test.
virtual void SetUp() override {
+ setLogTestPath();
// Clean up from past tests.
LegalLogMgrFactory::delAllBackends();
}
// Clean up from past tests.
LegalLogMgrFactory::delAllBackends();
reset();
+ resetLogPath();
}
/// @brief Removes files that may be left over from previous tests
}
}
+ /// @brief Sets the log path for log files.
+ /// @param custom_path path to use as the log file path.
+ void setLogTestPath(const std::string explicit_path = "") {
+ LegalLogMgr::getLogPath(true, (!explicit_path.empty() ?
+ explicit_path : TEST_DATA_BUILDDIR));
+ }
+
+ /// @brief Resets the log path to LEGAL_LOG_DIR.
+ void resetLogPath() {
+ LegalLogMgr::getLogPath(true);
+ }
+
/// @brief Initializer.
RotatingFileInit init_;
+
+ /// @brief RAII wrapper for KEA_LEGAL_LOG_DIR env variable.
+ EnvVarWrapper legal_log_dir_env_var_;
};
// Verifies output of genDurationString()
EXPECT_TRUE(LegalLogMgr::vectorHexDump(bytes).empty());
}
+// Verify path validation
+TEST_F(LegalLogMgrTest, pathValidation) {
+ isc::db::DatabaseConnection::ParameterMap map;
+ EXPECT_NO_THROW(LegalLogMgr::parseConfig(ConstElementPtr(), map));
+ EXPECT_NO_THROW(LegalLogMgrFactory::addBackend(map));
+ EXPECT_TRUE(LegalLogMgrFactory::instance());
+
+ // Default path should be OK.
+ ElementPtr params = Element::createMap();
+ params->set("base-name", Element::create("name"));
+ ASSERT_NO_THROW_LOG(LegalLogMgr::parseFile(params, map));
+
+ // Valid path should be OK.
+ params = Element::createMap();
+ params->set("path", Element::create(LegalLogMgr::getLogPath()));
+ ASSERT_NO_THROW_LOG(LegalLogMgr::parseFile(params, map));
+
+ // Invalid path should NOT be OK.
+ params = Element::createMap();
+ params->set("path", Element::create("/tmp"));
+ std::ostringstream os;
+ os << "invalid path specified: '/tmp', supported path is '"
+ << LegalLogMgr::getLogPath() << "'";
+ ASSERT_THROW_MSG(LegalLogMgr::parseFile(params, map), BadValue, os.str());
+}
+
+// Verify env variable path override.
+TEST_F(LegalLogMgrTest, pathEnvVarOverride) {
+ legal_log_dir_env_var_.setValue("/tmp");
+ resetLogPath();
+ isc::db::DatabaseConnection::ParameterMap map;
+ EXPECT_NO_THROW(LegalLogMgr::parseConfig(ConstElementPtr(), map));
+ EXPECT_NO_THROW(LegalLogMgrFactory::addBackend(map));
+ EXPECT_TRUE(LegalLogMgrFactory::instance());
+
+ // Default path should be OK.
+ ElementPtr params = Element::createMap();
+ params->set("base-name", Element::create("name"));
+ ASSERT_NO_THROW_LOG(LegalLogMgr::parseFile(params, map));
+
+ // Valid path should be OK.
+ params = Element::createMap();
+ params->set("path", Element::create("/tmp"));
+ ASSERT_NO_THROW_LOG(LegalLogMgr::parseFile(params, map));
+
+ // Invalid path should NOT be OK.
+ params = Element::createMap();
+ params->set("path", Element::create("/bogus"));
+ std::ostringstream os;
+ os << "invalid path specified: '/bogus', supported path is '"
+ << LegalLogMgr::getLogPath() << "'";
+ ASSERT_THROW_MSG(LegalLogMgr::parseFile(params, map), BadValue, os.str());
+}
+
// Verify that parsing extra parameters for rotate file works
TEST_F(LegalLogMgrTest, parseExtraRotatingFileParameters) {
isc::db::DatabaseConnection::ParameterMap map;
EXPECT_NO_THROW(LegalLogMgr::parseConfig(ConstElementPtr(), map));
- map["path"] = TEST_DATA_BUILDDIR;
EXPECT_NO_THROW(LegalLogMgrFactory::addBackend(map));
EXPECT_TRUE(LegalLogMgrFactory::instance());
RotatingFile& rotating_file = dynamic_cast<RotatingFile&>(*LegalLogMgrFactory::instance());
ElementPtr params = Element::createMap();
- params->set("path", Element::create("path"));
+ params->set("path", Element::create(LegalLogMgr::getLogPath()));
params->set("base-name", Element::create("name"));
params->set("time-unit", Element::create(0));
// Verify that parsing extra parameters works
TEST_F(LegalLogMgrTest, parseExtraParameters) {
db::DatabaseConnection::ParameterMap map;
- map["path"] = "path";
map["base-name"] = "name";
ASSERT_NO_THROW(LegalLogMgrFactory::instance().reset(new RotatingFile(map)));
ElementPtr params = Element::createMap();
TEST_F(LegalLogMgrTest, fileNoParameters) {
db::DatabaseConnection::ParameterMap map;
EXPECT_NO_THROW(LegalLogMgr::parseFile(ConstElementPtr(), map));
- map["path"] = TEST_DATA_BUILDDIR;
ASSERT_NO_THROW(LegalLogMgrFactory::instance().reset(new RotatingFile(map)));
ASSERT_NO_THROW(LegalLogMgrFactory::instance()->open());
RotatingFile& rotating_file = dynamic_cast<RotatingFile&>(*LegalLogMgrFactory::instance());
map.clear();
rotating_file.apply(map);
- EXPECT_EQ(rotating_file.getPath(), LEGAL_LOG_DIR);
+ EXPECT_EQ(rotating_file.getPath(), LegalLogMgr::getLogPath());
EXPECT_EQ(rotating_file.getBaseName(), "kea-legal");
}