#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
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.
- contents.append(line);
- contents.append("\n");
- }
- return (contents);
- } else {
-
- // This is the fastest method to read a file according to:
- // http://insanecoding.blogspot.com/2011/11/how-to-read-in-file-in-c.html
- infile.seekg(0, std::ios::end);
- contents.resize(infile.tellg());
- infile.seekg(0, std::ios::beg);
- infile.read(&contents[0], contents.size());
- infile.close();
- return(contents);
- }
- }
- 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
///
/// This method initializes logger. I
static void loggerInit(const char* log_name, bool verbose, bool stand_alone);
-
- protected:
-
- /// @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
#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 {
-public:
- using Daemon::readFile;
-};
-
-/// @brief Test class for testing Deamon class
-class DaemonTest : public ::testing::Test {
-public:
-
- /// @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
- unlink(TEMP_FILE);
- std::ofstream write_me(TEMP_FILE);
- EXPECT_TRUE(write_me.is_open());
- write_me << content;
- write_me.close();
- }
-
- /// destructor
- ~DaemonTest() {
- static_cast<void>(unlink(TEMP_FILE));
- }
-
- /// 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";
-
- writeFile(content);
-
- // Check that the read content is correct
- EXPECT_EQ(std::string(content), daemon_.readFile(TEMP_FILE));
-
- unlink(TEMP_FILE);
+// Very simple test. Checks whether Daemon can be instantiated.
+TEST(DaemonTest, noop) {
+ EXPECT_NO_THROW(Daemon x);
}
-// 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
- writeFile(no_endline);
-
- // Check that the read content is correct
- EXPECT_EQ(std::string(no_endline), daemon_.readFile(TEMP_FILE));
-
- // Write sample content to disk
- writeFile(with_endline);
-
- // 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
- writeFile(commented_content);
-
- // 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.
};