]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
Added tests for writing of KoD database to file.
authorLinux Karlsson <karlsson@ntp.org>
Tue, 27 Jul 2010 18:38:53 +0000 (20:38 +0200)
committerLinux Karlsson <karlsson@ntp.org>
Tue, 27 Jul 2010 18:38:53 +0000 (20:38 +0200)
bk: 4c4f27bdMP-C1ButpLNEQKemenLIbA

tests/sntp/data/kod-expected-multiple [new file with mode: 0644]
tests/sntp/data/kod-expected-single [new file with mode: 0644]
tests/sntp/kodFile.cpp

diff --git a/tests/sntp/data/kod-expected-multiple b/tests/sntp/data/kod-expected-multiple
new file mode 100644 (file)
index 0000000..c6ba292
--- /dev/null
@@ -0,0 +1,3 @@
+000000000000abcd DENY 192.0.2.1
+000000000000abcd RSTR 192.0.2.5
+000000000000abcd RATE example.com
diff --git a/tests/sntp/data/kod-expected-single b/tests/sntp/data/kod-expected-single
new file mode 100644 (file)
index 0000000..8ad3c7c
--- /dev/null
@@ -0,0 +1 @@
+0000000000000001 DENY host1
index b3791158ca6ed7111dee5ed431e144c38b606ab0..b669d34583b98bdeeb99988896f5403977ef793c 100644 (file)
@@ -6,6 +6,11 @@ extern "C" {
 #include "ntp_stdlib.h" // For estrdup()
 };
 
+#include <fstream>
+using std::ifstream;
+using std::ios;
+using std::string;
+
 /*
  * We access some parts of the kod database directly, without
  * going through the public interface
@@ -16,7 +21,12 @@ extern char* kod_db_file;
 
 class kodFileTest : public sntptest {
 protected:
-       std::string CreatePath(const char* filename, int argument) {
+       enum DirectoryType {
+               INPUT_DIR = 0,
+               OUTPUT_DIR = 1
+       };
+
+       std::string CreatePath(const char* filename, DirectoryType argument) {
                std::string path;
 
                if (m_params.size() >= argument + 1) {
@@ -46,16 +56,38 @@ protected:
 
                kod_db_file = estrdup("/dev/null");
        }
+
+       int GetFileSize(ifstream& file) {
+               int initial = file.tellg();
+
+               file.seekg(0, std::ios::end);
+               int length = file.tellg();
+               file.seekg(initial);
+
+               return length;
+       }
+
+       void CompareFileContent(ifstream& expected, ifstream& actual) {
+               int currentLine = 1;
+               while (actual.good() && expected.good()) {
+                       string actualLine, expectedLine;
+                       getline(actual, actualLine);
+                       getline(expected, expectedLine);
+                       
+                       EXPECT_EQ(expectedLine, actualLine) << "Comparision failed on line " << currentLine;
+                       currentLine++;
+               }
+       }
 };
 
 TEST_F(kodFileTest, ReadEmptyFile) {
-       kod_init_kod_db(CreatePath("kod-test-empty", 0).c_str());
+       kod_init_kod_db(CreatePath("kod-test-empty", INPUT_DIR).c_str());
 
        EXPECT_EQ(0, kod_db_cnt);
 }
 
 TEST_F(kodFileTest, ReadCorrectFile) {
-       kod_init_kod_db(CreatePath("kod-test-correct", 0).c_str());
+       kod_init_kod_db(CreatePath("kod-test-correct", INPUT_DIR).c_str());
        
        EXPECT_EQ(2, kod_db_cnt);
 
@@ -73,7 +105,7 @@ TEST_F(kodFileTest, ReadCorrectFile) {
 }
 
 TEST_F(kodFileTest, ReadFileWithBlankLines) {
-       kod_init_kod_db(CreatePath("kod-test-blanks", 0).c_str());
+       kod_init_kod_db(CreatePath("kod-test-blanks", INPUT_DIR).c_str());
 
        EXPECT_EQ(3, kod_db_cnt);
 
@@ -94,3 +126,68 @@ TEST_F(kodFileTest, ReadFileWithBlankLines) {
        EXPECT_STREQ("example.com", res->hostname);
        EXPECT_EQ(0xabcd, res->timestamp);
 }
+
+TEST_F(kodFileTest, WriteEmptyFile) {
+       kod_db_file = estrdup(CreatePath("kod-output-blank", OUTPUT_DIR).c_str());
+
+       write_kod_db();
+
+       // Open file and ensure that the filesize is 0 bytes.
+       std::ifstream is(kod_db_file, std::ios::binary);
+       ASSERT_FALSE(is.fail());
+       
+       EXPECT_EQ(0, GetFileSize(is));
+
+       is.close();
+}
+
+TEST_F(kodFileTest, WriteFileWithSingleEntry) {
+       kod_db_file = estrdup(CreatePath("kod-output-single", OUTPUT_DIR).c_str());
+
+       add_entry("host1", "DENY");
+
+       /* Here we must manipulate the timestamps, so they match the one in
+        * the expected file.
+        */
+       kod_db[0]->timestamp = 1;
+
+       write_kod_db();
+
+       // Open file and compare sizes.
+       ifstream actual(kod_db_file, ios::binary);
+       ifstream expected(CreatePath("kod-expected-single", INPUT_DIR).c_str());
+       ASSERT_TRUE(actual.good());
+       ASSERT_TRUE(expected.good());
+
+       ASSERT_EQ(GetFileSize(expected), GetFileSize(actual));
+
+       CompareFileContent(expected, actual);
+}
+
+TEST_F(kodFileTest, WriteFileWithMultipleEntries) {
+       kod_db_file = estrdup(CreatePath("kod-output-multiple", OUTPUT_DIR).c_str());
+
+       add_entry("example.com", "RATE");
+       add_entry("192.0.2.1", "DENY");
+       add_entry("192.0.2.5", "RSTR");
+
+       /*
+        * Manipulate timestamps. This is a bit of a hack, ideally these
+        * tests should not care about the internal representation.
+        */
+       kod_db[0]->timestamp = 0xabcd;
+       kod_db[1]->timestamp = 0xabcd;
+       kod_db[2]->timestamp = 0xabcd;
+
+       write_kod_db();
+
+       // Open file and compare sizes and content.
+       ifstream actual(kod_db_file, ios::binary);
+       ifstream expected(CreatePath("kod-expected-multiple", INPUT_DIR).c_str());
+       ASSERT_TRUE(actual.good());
+       ASSERT_TRUE(expected.good());
+       
+       ASSERT_EQ(GetFileSize(expected), GetFileSize(actual));
+
+       CompareFileContent(expected, actual);
+}