]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
Added tests for reading of key file.
authorLinux Karlsson <karlsson@ntp.org>
Wed, 28 Jul 2010 11:38:35 +0000 (13:38 +0200)
committerLinux Karlsson <karlsson@ntp.org>
Wed, 28 Jul 2010 11:38:35 +0000 (13:38 +0200)
bk: 4c5016bb85NI1gLim3HW3SmXsFaKJg

tests/sntp/Makefile.am
tests/sntp/data/key-test-ascii [new file with mode: 0644]
tests/sntp/data/key-test-comments [new file with mode: 0644]
tests/sntp/data/key-test-empty [new file with mode: 0644]
tests/sntp/data/key-test-hex [new file with mode: 0644]
tests/sntp/fileHandlingTest.h [new file with mode: 0644]
tests/sntp/keyFile.cpp [new file with mode: 0644]
tests/sntp/kodFile.cpp

index e10a91e800fa37e9966bd430487f32ef256609e9..7e1c1a38d315f6a945ad4c10db1033744bbdeca1 100644 (file)
@@ -2,7 +2,8 @@ check_PROGRAMS = tests
 
 sntp_src = $(top_builddir)/sntp
 
-sntp_SOURCES_USED =    $(sntp_src)/kod_management.o    \
+sntp_SOURCES_USED =    $(sntp_src)/crypto.o            \
+                       $(sntp_src)/kod_management.o    \
                        $(sntp_src)/log.o               \
                        $(sntp_src)/sntp-opts.o
 
@@ -10,6 +11,7 @@ base_SOURCES =                ../main.cpp     \
                        sntptest.cpp
 
 tests_SOURCES = $(base_SOURCES)                \
+               keyFile.cpp             \
                kodDatabase.cpp         \
                kodFile.cpp
 
@@ -17,6 +19,7 @@ LDADD =       @GTEST_LDFLAGS@                 \
                @GTEST_LIBS@                    \
                $(LIBOPTS_LDADD)                \
                @top_builddir@/libntp/libntp.a  \
+               @LCRYPTO@                       \
                $(sntp_SOURCES_USED)
 
 AM_CXXFLAGS = @GTEST_CXXFLAGS@
diff --git a/tests/sntp/data/key-test-ascii b/tests/sntp/data/key-test-ascii
new file mode 100644 (file)
index 0000000..9240c81
--- /dev/null
@@ -0,0 +1,2 @@
+50 MD5 asciikeyOne
+40 MD5 asciikeyTwo
diff --git a/tests/sntp/data/key-test-comments b/tests/sntp/data/key-test-comments
new file mode 100644 (file)
index 0000000..437049e
--- /dev/null
@@ -0,0 +1,3 @@
+# This is an initial comment, this file should contain 2 keys.
+34 MD5 xyz #This is a comment after an ASCII key.
+10 MD5 010101010101010101010101010101 #Comment after hex key.
diff --git a/tests/sntp/data/key-test-empty b/tests/sntp/data/key-test-empty
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/sntp/data/key-test-hex b/tests/sntp/data/key-test-hex
new file mode 100644 (file)
index 0000000..fb704c5
--- /dev/null
@@ -0,0 +1,3 @@
+10 MD5 0123456789abcdef0123456789
+20 MD5 111111111111111111111111111111
+30 MD5 01010101010101010101010101
diff --git a/tests/sntp/fileHandlingTest.h b/tests/sntp/fileHandlingTest.h
new file mode 100644 (file)
index 0000000..4668cb6
--- /dev/null
@@ -0,0 +1,24 @@
+#include "sntptest.h"
+
+class fileHandlingTest : public sntptest {
+protected:
+       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) {
+                       path = m_params[argument];
+               }
+
+               if (path[path.size()-1] != DIR_SEP && !path.empty()) {
+                       path.append(1, DIR_SEP);
+               }
+               path.append(filename);
+
+               return path;
+       }
+};
diff --git a/tests/sntp/keyFile.cpp b/tests/sntp/keyFile.cpp
new file mode 100644 (file)
index 0000000..e82af1e
--- /dev/null
@@ -0,0 +1,112 @@
+#include "fileHandlingTest.h"
+
+extern "C" {
+#include "crypto.h"
+};
+
+class keyFileTest : public fileHandlingTest {
+protected:
+       ::testing::AssertionResult CompareKeys(key& expected, key& actual) {
+               if (expected.key_id != actual.key_id)
+                       return ::testing::AssertionFailure()
+                               << "Expected key_id: " << expected.key_id
+                               << " but was: " << actual.key_id;
+               if (expected.key_len != actual.key_len)
+                       return ::testing::AssertionFailure()
+                               << "Expected key_len: " << expected.key_len
+                               << " but was: " << actual.key_len;
+               if (strcmp(expected.type, actual.type) != 0)
+                       return ::testing::AssertionFailure()
+                               << "Expected key_type: " << expected.type
+                               << " but was: " << actual.type;
+               if (memcmp(expected.key_seq, actual.key_seq, expected.key_len) != 0)
+                       return ::testing::AssertionFailure()
+                               << "Key mismatch!";
+               return ::testing::AssertionSuccess();
+       }
+
+       ::testing::AssertionResult CompareKeys(int key_id,
+                                                                                 int key_len,
+                                                                                 const char* type,
+                                                                                 const char* key_seq,
+                                                                                 key& actual) {
+               key temp;
+               temp.key_id = key_id;
+               temp.key_len = key_len;
+               strncpy(temp.type, type, sizeof(temp.type));
+               temp.type[sizeof(temp.type)-1] = '\0';
+               memcpy(temp.key_seq, key_seq, key_len);
+               return CompareKeys(temp, actual);
+       }
+};
+
+TEST_F(keyFileTest, ReadEmptyKeyFile) {
+       key* keys = NULL;
+
+       ASSERT_EQ(0, auth_init(CreatePath("key-test-empty", INPUT_DIR).c_str(), &keys));
+
+       EXPECT_TRUE(keys == NULL);
+}
+
+TEST_F(keyFileTest, ReadASCIIKeys) {
+       key* keys = NULL;
+
+       ASSERT_EQ(2, auth_init(CreatePath("key-test-ascii", INPUT_DIR).c_str(), &keys));
+
+       ASSERT_TRUE(keys != NULL);
+
+       key* result = NULL;
+       get_key(40, &result);
+       ASSERT_TRUE(result != NULL);
+       EXPECT_TRUE(CompareKeys(40, 11, "MD5", "asciikeyTwo", *result));
+
+       result = NULL;
+       get_key(50, &result);
+       ASSERT_TRUE(result != NULL);
+       EXPECT_TRUE(CompareKeys(50, 11, "MD5", "asciikeyOne", *result));
+}
+
+TEST_F(keyFileTest, ReadHexKeys) {
+       key* keys = NULL;
+
+       ASSERT_EQ(3, auth_init(CreatePath("key-test-hex", INPUT_DIR).c_str(), &keys));
+
+       ASSERT_TRUE(keys != NULL);
+
+       key* result = NULL;
+       get_key(10, &result);
+       ASSERT_TRUE(result != NULL);
+       EXPECT_TRUE(CompareKeys(10, 13, "MD5",
+                "\x01\x23\x45\x67\x89\xab\xcd\xef\x01\x23\x45\x67\x89", *result));
+
+       result = NULL;
+       get_key(20, &result);
+       ASSERT_TRUE(result != NULL);
+       char data1[15]; memset(data1, 0x11, 15);
+       EXPECT_TRUE(CompareKeys(20, 15, "MD5", data1, *result));
+
+       result = NULL;
+       get_key(30, &result);
+       ASSERT_TRUE(result != NULL);
+       char data2[13]; memset(data2, 0x01, 13);
+       EXPECT_TRUE(CompareKeys(30, 13, "MD5", data2, *result));
+}
+
+TEST_F(keyFileTest, ReadKeyFileWithComments) {
+       key* keys = NULL;
+
+       ASSERT_EQ(2, auth_init(CreatePath("key-test-comments", INPUT_DIR).c_str(), &keys));
+       
+       ASSERT_TRUE(keys != NULL);
+
+       key* result = NULL;
+       get_key(10, &result);
+       ASSERT_TRUE(result != NULL);
+       char data[15]; memset(data, 0x01, 15);
+       EXPECT_TRUE(CompareKeys(10, 15, "MD5", data, *result));
+
+       result = NULL;
+       get_key(34, &result);
+       ASSERT_TRUE(result != NULL);
+       EXPECT_TRUE(CompareKeys(34, 3, "MD5", "xyz", *result));
+}
index 4cd9748377d7fa134253e54e598eaa04cc925a02..c59f3152c9d25f969800d0b5c861c2c19145368d 100644 (file)
@@ -1,4 +1,4 @@
-#include "sntptest.h"
+#include "fileHandlingTest.h"
 
 extern "C" {
 #include "kod_management.h"
@@ -19,28 +19,8 @@ extern int kod_db_cnt;
 extern kod_entry** kod_db;
 extern char* kod_db_file;
 
-class kodFileTest : public sntptest {
+class kodFileTest : public fileHandlingTest {
 protected:
-       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) {
-                       path = m_params[argument];
-               }
-
-               if (path[path.size()-1] != DIR_SEP && !path.empty()) {
-                       path.append(1, DIR_SEP);
-               }
-               path.append(filename);
-
-               return path;
-       }
-
        virtual void SetUp() {
                kod_db_cnt = 0;
                kod_db = NULL;