]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#1245] Added unit tests
authorFrancis Dupont <fdupont@isc.org>
Sun, 14 Jun 2020 10:27:25 +0000 (12:27 +0200)
committerFrancis Dupont <fdupont@isc.org>
Wed, 17 Jun 2020 09:27:52 +0000 (11:27 +0200)
src/bin/dhcp4/tests/config_parser_unittest.cc
src/bin/dhcp6/tests/config_parser_unittest.cc
src/lib/testutils/log_utils.cc
src/lib/testutils/log_utils.h

index 36e4bf8f5a2bb3110ad3061c3527149e08b13573..8153f34b09a7815d498c0876b8d2eca392c0c10a 100644 (file)
@@ -30,6 +30,7 @@
 #include <process/config_ctl_info.h>
 #include <hooks/hooks_manager.h>
 #include <stats/stats_mgr.h>
+#include <testutils/log_utils.h>
 #include <util/boost_time_utils.h>
 #include <util/doubles.h>
 
@@ -262,7 +263,7 @@ const char* PARSER_CONFIGS[] = {
     "} \n"
 };
 
-class Dhcp4ParserTest : public ::testing::Test {
+class Dhcp4ParserTest : public LogContentTest {
 protected:
     // Check that no hooks libraries are loaded.  This is a pre-condition for
     // a number of tests, so is checked in one place.  As this uses an
@@ -2396,6 +2397,13 @@ TEST_F(Dhcp4ParserTest, optionDefDuplicate) {
     checkResult(status, 1);
     EXPECT_TRUE(errorContainsPosition(status, "<string>"));
 
+    // Specific check for incorrect report using default config pair
+    // as option-def is parsed first.
+    string expected = "failed to create or run parser for configuration ";
+    expected += "element option-def: option definition with code '100' ";
+    expected += "already exists in option space 'isc'";
+    EXPECT_EQ(1, countFile(expected));
+
     // The new configuration should have inserted option 100, but
     // once configuration failed (on the duplicate option definition)
     // the original configuration in libdhcp++ should be reverted.
index 30e3c027efb8f804d875b318eb6347dcdd9c2e12..52edb700d8ddc4a7bbe1efa3c29135c55fe9530e 100644 (file)
@@ -29,6 +29,7 @@
 #include <hooks/hooks_manager.h>
 #include <process/config_ctl_info.h>
 #include <stats/stats_mgr.h>
+#include <testutils/log_utils.h>
 #include <util/boost_time_utils.h>
 
 #include "test_data_files_config.h"
@@ -350,7 +351,7 @@ const char* PARSER_CONFIGS[] = {
     "}"
 };
 
-class Dhcp6ParserTest : public ::testing::Test {
+class Dhcp6ParserTest : public LogContentTest {
 protected:
     // Check that no hooks libraries are loaded.  This is a pre-condition for
     // a number of tests, so is checked in one place.  As this uses an
@@ -2698,6 +2699,13 @@ TEST_F(Dhcp6ParserTest, optionDefDuplicate) {
     checkResult(status, 1);
     EXPECT_TRUE(errorContainsPosition(status, "<string>"));
 
+    // Specific check for incorrect report using default config pair
+    // as option-def is parsed first.
+    string expected = "failed to create or run parser for configuration ";
+    expected += "element option-def: option definition with code '100' ";
+    expected += "already exists in option space 'isc'";
+    EXPECT_EQ(1, countFile(expected));
+
     // The new configuration should have inserted option 100, but
     // once configuration failed (on the duplicate option definition)
     // the original configuration in libdhcp++ should be reverted.
index 33fffd1ea042bbded1ed2092beb8d439ad7ad697..63db6ee6a8cd22f5386370bc6070eb7b0527a1b3 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2016 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2016-2020 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -74,6 +74,35 @@ bool LogContentTest::checkFile() {
     return (true);
 }
 
+size_t LogContentTest::countFile(const string& exp_string) {
+    ifstream file(LOG_FILE);
+    EXPECT_TRUE(file.is_open());
+    string line;
+    size_t cnt = 0;
+
+    using namespace std;
+
+    if (verbose_) {
+        cout << "Looking for:" << exp_string << endl;
+    }
+    while (getline(file, line)) {
+        if (verbose_) {
+            cout << "Read line  :" << line << endl;
+        }
+        if (line.find(exp_string) != string::npos) {
+            ++cnt;
+        }
+    }
+
+    file.close();
+
+    if (verbose_) {
+        cout << "Final count: " << cnt << endl;
+    }
+
+    return (cnt);
+}
+
 void LogContentTest::remFile() {
     static_cast<void>(remove(LOG_FILE));
 }
@@ -86,6 +115,6 @@ void LogContentTest::addString(const string& new_string) {
 // the debug statements
 const char *LogContentTest::LOG_FILE = "test.log";
 
-}; // end of isc::dhcp::test namespace
-}; // end of isc::dhcp namespace
-}; // end of isc namespace
+} // end of isc::dhcp::test namespace
+} // end of isc::dhcp namespace
+} // end of isc namespace
index c7f835b389319dc980ce2316b497bd70df801eca..d222ae3ae00a78dca36c8d3244f685ba5df2f09f 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2016 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2016-2020 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -60,6 +60,13 @@ public:
     /// @return true if all of the strings match
     bool checkFile();
 
+    /// @brief check that the requested string is in the
+    /// test log file.
+    ///
+    /// @param exp_string the string to be searched
+    /// @return count of matching lines
+    size_t countFile(const string& exp_string);
+
     /// @brief remove the test log file
     void remFile();
 
@@ -90,10 +97,7 @@ public:
     bool verbose_;
 };
 
-
-
-}; // end of isc::dhcp::test namespace
-}; // end of isc::dhcp namespace
-}; // end of isc namespace
-
+} // end of isc::dhcp::test namespace
+} // end of isc::dhcp namespace
+} // end of isc namespace
 #endif // TEST_LOG_UTILS_H