Commit 56e20ea0 authored by Tomek Mrugalski's avatar Tomek Mrugalski 🛰
Browse files

[3400] Daemon::readFile() method removed.

parent 36519e4d
......@@ -56,11 +56,8 @@ ControlledDhcpv6Srv::init(const std::string& file_name) {
try {
// Read contents of the file
string config = readFile(file_name, true);
// Try to parse it as JSON
json = Element::fromJSON(config);
// Read contents of the file and parse it as JSON
json = Element::fromJSONFile(file_name, true);
// Use parsed JSON structures to configure the server
result = processCommand("config-reload", json);
......@@ -52,6 +52,7 @@ public:
~JSONFileBackendTest() {
void writeFile(const std::string& file_name, const std::string& content) {
......@@ -21,7 +21,6 @@
#include <log/output_option.h>
#include <dhcpsrv/daemon.h>
#include <exceptions/exceptions.h>
#include <fstream>
#include <errno.h>
/// @brief provides default implementation for basic daemon operations
......@@ -49,47 +48,6 @@ void Daemon::shutdown() {
Daemon::~Daemon() {
std::string Daemon::readFile(const std::string& file_name, bool ignore_comments) {
std::ifstream infile(file_name.c_str(), std::ios::in | std::ios::binary);
if (infile.is_open())
std::string contents;
std::string line;
if (ignore_comments) {
while (std::getline(infile, line)) {
// If this is a comments line, replace it with empty line
// (so the line numbers will still match
if (!line.empty() && line[0] == '#') {
line = "";
// getline() removes end line charaters. Unfortunately, we need
// it for getting the line numbers right (in case we report an
// error.
return (contents);
} else {
// This is the fastest method to read a file according to:
infile.seekg(0, std::ios::end);
infile.seekg(0, std::ios::beg);[0], contents.size());
isc_throw(InvalidOperation, "Failed to read file " << file_name << ",error:"
<< errno);
void Daemon::loggerInit(const char* log_name, bool verbose, bool ) {
// This method configures logger. For now it is very simple.
// We'll make it more robust once we add support for JSON-based logging
......@@ -87,21 +87,6 @@ public:
/// This method initializes logger. I
static void loggerInit(const char* log_name, bool verbose, bool stand_alone);
/// @brief reads file and return its content as a string
/// This is an utility method that is expected to be used by several daemons.
/// It reads contents of a text file and return it as a string.
/// For now, comments are defined as lines starting with a hash.
/// @param file_name name of the file to read
/// @param ignore_comments whether ignore comment lines
/// @return content of the file
std::string readFile(const std::string& file_name,
bool ingore_comments = false);
}; // end of isc::dhcp namespace
......@@ -16,118 +16,17 @@
#include <exceptions/exceptions.h>
#include <dhcpsrv/daemon.h>
#include <gtest/gtest.h>
#include <fstream>
using namespace isc;
using namespace isc::dhcp;
namespace {
/// @brief Test-friendly version of the daemon
/// This class exposes internal Daemon class methods.
class NakedDaemon: public Daemon {
using Daemon::readFile;
/// @brief Test class for testing Deamon class
class DaemonTest : public ::testing::Test {
/// @brief writes specified text to a file
/// That is an auxilliary funtion used in fileRead() tests.
/// @param content text to be written to disk
void writeFile(const std::string& content) {
// Write sample content to disk
std::ofstream write_me(TEMP_FILE);
write_me << content;
/// destructor
~DaemonTest() {
/// Name of the temporary file
static const char* TEMP_FILE;
/// The daemon implementation being tested
NakedDaemon daemon_;
/// Temporary file name used in some tests
const char* DaemonTest::TEMP_FILE="temp-file.json";
// Test checks whether a text file can be read from disk.
TEST_F(DaemonTest, readFile) {
const char* content = "Horse doesn't eat cucumber salad";
// Check that the read content is correct
EXPECT_EQ(std::string(content), daemon_.readFile(TEMP_FILE));
// Very simple test. Checks whether Daemon can be instantiated.
TEST(DaemonTest, noop) {
// Test checks whether a text file can be read from disk.
TEST_F(DaemonTest, readFileMultiline) {
const char* no_endline = "A text\nwithout\nendline in the last line";
const char* with_endline = "A text\nwith\endline in the last line\n";
// Write sample content to disk
// Check that the read content is correct
EXPECT_EQ(std::string(no_endline), daemon_.readFile(TEMP_FILE));
// Write sample content to disk
// Check that the read content is correct
EXPECT_EQ(std::string(with_endline), daemon_.readFile(TEMP_FILE));
// Test checks whether comments in file are ignored as expected.
TEST_F(DaemonTest, readFileComments) {
NakedDaemon x;
const char* commented_content = "# This is a comment\n"
"this is a normal line\n"
"Second line\n"
"# another comment";
// The same as above with comments removed and comment lines
// substituted with empty lines.
const char* filtered_content ="\nthis is a normal line\nSecond line\n\n";
// Write sample content to disk
// Check that the read content is correct (without comment elimination)
EXPECT_EQ(std::string(commented_content), x.readFile(TEMP_FILE, false));
// Check that the read content is correct (with comment elimination)
EXPECT_EQ(std::string(filtered_content), x.readFile(TEMP_FILE, true));
// This test checks that missing file will generate an exception.
TEST_F(DaemonTest, readFileError) {
// Check that the read content is correct
EXPECT_THROW(daemon_.readFile("no-such-file.txt"), isc::InvalidOperation);
// More tests will appear here as we develop Daemon class.
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment