From: Tomek Mrugalski Date: Thu, 8 May 2014 15:52:54 +0000 (+0200) Subject: [3400] Daemon::readFile() method removed. X-Git-Tag: trac3434_base~32^2~17 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=56e20ea0a56796dff8f116e13cccee5b9c3bda9e;p=thirdparty%2Fkea.git [3400] Daemon::readFile() method removed. --- diff --git a/src/bin/dhcp6/jsonfile_backend.cc b/src/bin/dhcp6/jsonfile_backend.cc index 9689431791..a0b85757b0 100644 --- a/src/bin/dhcp6/jsonfile_backend.cc +++ b/src/bin/dhcp6/jsonfile_backend.cc @@ -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); diff --git a/src/bin/dhcp6/tests/jsonfile_backend_unittest.cc b/src/bin/dhcp6/tests/jsonfile_backend_unittest.cc index 3dfbf25fbd..bb09bb2f55 100644 --- a/src/bin/dhcp6/tests/jsonfile_backend_unittest.cc +++ b/src/bin/dhcp6/tests/jsonfile_backend_unittest.cc @@ -52,6 +52,7 @@ public: } ~JSONFileBackendTest() { + static_cast(unlink(TEST_FILE)); }; void writeFile(const std::string& file_name, const std::string& content) { diff --git a/src/lib/dhcpsrv/daemon.cc b/src/lib/dhcpsrv/daemon.cc index df74ea4019..bf03c7bc74 100644 --- a/src/lib/dhcpsrv/daemon.cc +++ b/src/lib/dhcpsrv/daemon.cc @@ -21,7 +21,6 @@ #include #include #include -#include #include /// @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. - 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 diff --git a/src/lib/dhcpsrv/daemon.h b/src/lib/dhcpsrv/daemon.h index dc00fb159a..fe17938ca5 100644 --- a/src/lib/dhcpsrv/daemon.h +++ b/src/lib/dhcpsrv/daemon.h @@ -87,21 +87,6 @@ public: /// /// 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 diff --git a/src/lib/dhcpsrv/tests/daemon_unittest.cc b/src/lib/dhcpsrv/tests/daemon_unittest.cc index d106f217d7..315148b5d4 100644 --- a/src/lib/dhcpsrv/tests/daemon_unittest.cc +++ b/src/lib/dhcpsrv/tests/daemon_unittest.cc @@ -16,118 +16,17 @@ #include #include #include -#include 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(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. };