]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
Many files:
authorDamir Tomic <viperus@ntp.org>
Wed, 17 Jun 2015 09:30:50 +0000 (11:30 +0200)
committerDamir Tomic <viperus@ntp.org>
Wed, 17 Jun 2015 09:30:50 +0000 (11:30 +0200)
  new file
Makefile.am:
  removed keyFile test from check_Programs for now

bk: 55813e4aewxiVDaCOTV3rCZ-UdZV7g

sntp/tests/Makefile.am
sntp/tests/c_fileHandlingTest.h [new file with mode: 0644]
sntp/tests/keyFile.c [new file with mode: 0644]
sntp/tests/kodDatabase.c [new file with mode: 0644]
sntp/tests/run-test-keyFile.c [new file with mode: 0644]
sntp/tests/run-test-kodDatabase.c [new file with mode: 0644]

index a1ecab98c98ddbaa2626dc05bf94229f29fd9704..c9a7efe1ecdbb095b9b636a6d2301507b96ccd1f 100644 (file)
@@ -6,8 +6,9 @@ run_unity =     cd $(srcdir) && ruby ../../sntp/unity/auto/generate_test_runner.rb
 
 check_PROGRAMS =               \
        test-networking         \
-       test-kodDatabase        \
+       test-kodDatabase
        $(NULL)
+#      test-keyFile
 
 if GTEST_AVAILABLE
 check_PROGRAMS += tests
@@ -116,10 +117,18 @@ test_kodDatabase_LDADD =                  \
        $(unity_tests_LDADD)                    \
        $(NULL)
 
+test_keyFile_CFLAGS =                  \
+       -I$(top_srcdir)/unity           \
+       $(NULL)
+
+test_keyFile_LDADD =                   \
+       $(unity_tests_LDADD)            \
+       $(NULL)
 
 BUILT_SOURCES +=                                       \
        $(srcdir)/networking.c run-test-networking.c    \
        $(srcdir)/kodDatabase.c run-test-kodDatabase.c  \
+       $(srcdir)/run-test-keyFile.c                    \
        $(NULL)
 
 test_networking_SOURCES =              \
@@ -133,6 +142,11 @@ test_kodDatabase_SOURCES =         \
        ../version.c                    \
        $(NULL)
 
+test_keyFile_SOURCES =                 \
+       keyFile.c                       \
+       run-test-keyFile.c              \
+       $(NULL)
+
 $(srcdir)/run-test-networking.c: $(srcdir)/networking.c $(std_unity_list)
        $(run_unity) networking.c run-test-networking.c
 
@@ -142,6 +156,9 @@ $(srcdir)/run-test-kodDatabase.c: $(srcdir)/kodDatabase.c $(std_unity_list)
 #$(srcdir)/../version.c: $(srcdir)/../version.c
 #      gcc -o version.o ../version.c
 
+$(srcdir)/run-test-keyFile.c: $(srcdir)/keyFile.c $(std_unity_list)
+       $(run_unity) keyFile.c run-test-keyFile.c
+
 TESTS = 
 
 if !NTP_CROSSCOMPILE
diff --git a/sntp/tests/c_fileHandlingTest.h b/sntp/tests/c_fileHandlingTest.h
new file mode 100644 (file)
index 0000000..79c6192
--- /dev/null
@@ -0,0 +1,63 @@
+#ifndef FILE_HANDLING_TEST_H
+#define FILE_HANDLING_TEST_H
+
+#include "c_sntptest.h"
+
+//#include <fstream>
+#include <string.h>
+
+//using std::ifstream;
+//using std::string;
+//using std::ios;
+
+
+enum DirectoryType {
+       INPUT_DIR = 0,
+       OUTPUT_DIR = 1
+};
+
+char * CreatePath(const char* filename, DirectoryType argument) {
+       char * 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;
+}
+
+int GetFileSize(ifstream& file) {
+       int initial = file.tellg();
+
+       file.seekg(0, 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++;
+       }
+}
+
+void ClearFile(const std::string& filename) {
+       std::ofstream clear(filename.c_str(), ios::trunc);
+       ASSERT_TRUE(clear.good());
+       clear.close();
+}
+
+
+#endif // FILE_HANDLING_TEST_H
diff --git a/sntp/tests/keyFile.c b/sntp/tests/keyFile.c
new file mode 100644 (file)
index 0000000..041b724
--- /dev/null
@@ -0,0 +1,136 @@
+#include "config.h"
+#include "c_fileHandlingTest.h"
+
+#include "ntp_types.h"
+#include "crypto.h"
+
+typedef int bool;
+
+bool AssertionResult CompareKeys(key& expected, key& actual) {
+       if (expected.key_id != actual.key_id){
+               printf("Expected key_id: %d", expected.key_id);
+               printf(" but was: %d", actual.key_id);
+               return TRUE;
+       }
+       if (expected.key_len != actual.key_len){
+               printf("Expected key_len: %d", expected.key_len);
+               printf(" but was: %d", actual.key_len);
+               return TRUE;
+       }
+       if (strcmp(expected.type, actual.type) != 0){
+               printf("Expected key_type: %d", expected.type);
+               printf(" but was: ", actual.type);
+               return TRUE;
+
+       }
+       if (memcmp(expected.key_seq, actual.key_seq, expected.key_len) != 0){
+               printf("Key mismatch!");
+               return TRUE;            
+       }
+       return TRUE;
+}
+
+bool 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;
+       strlcpy(temp.type, type, sizeof(temp.type));
+       memcpy(temp.key_seq, key_seq, key_len);
+
+       return CompareKeys(temp, actual);
+}
+
+
+void(ReadEmptyKeyFile) {
+       key* keys = NULL;
+
+       TEST_ASSERT_EQUAL(0, auth_init(CreatePath("key-test-empty", INPUT_DIR).c_str(), &keys));
+
+       TEST_ASSERT_TRUE(keys == NULL);
+}
+
+void(ReadASCIIKeys) {
+       key* keys = NULL;
+
+       TEST_ASSERT_EQUAL(2, auth_init(CreatePath("key-test-ascii", INPUT_DIR).c_str(), &keys));
+
+       TEST_ASSERT_TRUE(keys != NULL);
+
+       key* result = NULL;
+       get_key(40, &result);
+       TEST_ASSERT_TRUE(result != NULL);
+       TEST_ASSERT_TRUE(CompareKeys(40, 11, "MD5", "asciikeyTwo", *result));
+
+       result = NULL;
+       get_key(50, &result);
+       TEST_ASSERT_TRUE(result != NULL);
+       TEST_ASSERT_TRUE(CompareKeys(50, 11, "MD5", "asciikeyOne", *result));
+}
+
+void(ReadHexKeys) {
+       key* keys = NULL;
+
+       TEST_ASSERT_EQUAL(3, auth_init(CreatePath("key-test-hex", INPUT_DIR).c_str(), &keys));
+
+       TEST_ASSERT_TRUE(keys != NULL);
+
+       key* result = NULL;
+       get_key(10, &result);
+       TEST_ASSERT_TRUE(result != NULL);
+       TEST_ASSERT_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);
+       TEST_ASSERT_TRUE(result != NULL);
+       char data1[15]; memset(data1, 0x11, 15);
+       TEST_ASSERT_TRUE(CompareKeys(20, 15, "MD5", data1, *result));
+
+       result = NULL;
+       get_key(30, &result);
+       TEST_ASSERT_TRUE(result != NULL);
+       char data2[13]; memset(data2, 0x01, 13);
+       TEST_ASSERT_TRUE(CompareKeys(30, 13, "MD5", data2, *result));
+}
+
+void(ReadKeyFileWithComments) {
+       key* keys = NULL;
+
+       TEST_ASSERT_EQUAL(2, auth_init(CreatePath("key-test-comments", INPUT_DIR).c_str(), &keys));
+       
+       TEST_ASSERT_TRUE(keys != NULL);
+
+       key* result = NULL;
+       get_key(10, &result);
+       TEST_ASSERT_TRUE(result != NULL);
+       char data[15]; memset(data, 0x01, 15);
+       TEST_ASSERT_TRUE(CompareKeys(10, 15, "MD5", data, *result));
+
+       result = NULL;
+       get_key(34, &result);
+       TEST_ASSERT_TRUE(result != NULL);
+       TEST_ASSERT_TRUE(CompareKeys(34, 3, "MD5", "xyz", *result));
+}
+
+void(ReadKeyFileWithInvalidHex) {
+       key* keys = NULL;
+
+       TEST_ASSERT_EQUAL(1, auth_init(CreatePath("key-test-invalid-hex", INPUT_DIR).c_str(), &keys));
+
+       TEST_ASSERT_TRUE(keys != NULL);
+
+       key* result = NULL;
+       get_key(10, &result);
+       TEST_ASSERT_TRUE(result != NULL);
+       char data[15]; memset(data, 0x01, 15);
+       TEST_ASSERT_TRUE(CompareKeys(10, 15, "MD5", data, *result));
+
+       result = NULL;
+       get_key(30, &result); // Should not exist, and result should remain NULL.
+       TEST_ASSERT_TRUE(result == NULL);
+}
diff --git a/sntp/tests/kodDatabase.c b/sntp/tests/kodDatabase.c
new file mode 100644 (file)
index 0000000..25e7b12
--- /dev/null
@@ -0,0 +1,126 @@
+#include "config.h"
+
+#include "ntp_types.h"
+#include "c_sntptest.h"
+#include "ntp_stdlib.h"
+#include "sntp-opts.h"
+
+#include "kod_management.h"
+
+#include "unity.h"
+
+void setUp(void)
+{
+       kod_init_kod_db("/dev/null", TRUE);
+}
+
+void tearDown(void)
+{
+}
+
+
+void test_SingleEntryHandling() {
+       char HOST[] = "192.0.2.5";
+       char REASON[] = "DENY";
+
+       add_entry(HOST, REASON);
+
+       struct kod_entry* result;
+
+       TEST_ASSERT_EQUAL(1, search_entry(HOST, &result));
+       TEST_ASSERT_EQUAL_STRING(HOST, result->hostname);
+       TEST_ASSERT_EQUAL_STRING(REASON, result->type);
+}
+
+void test_MultipleEntryHandling() {
+       char HOST1[] = "192.0.2.3";
+       char REASON1[] = "DENY";
+
+       char HOST2[] = "192.0.5.5";
+       char REASON2[] = "RATE";
+
+       char HOST3[] = "192.0.10.1";
+       char REASON3[] = "DENY";
+
+       add_entry(HOST1, REASON1);
+       add_entry(HOST2, REASON2);
+       add_entry(HOST3, REASON3);
+
+       struct kod_entry* result;
+
+       TEST_ASSERT_EQUAL(1, search_entry(HOST1, &result));
+       TEST_ASSERT_EQUAL_STRING(HOST1, result->hostname);
+       TEST_ASSERT_EQUAL_STRING(REASON1, result->type);
+
+       TEST_ASSERT_EQUAL(1, search_entry(HOST2, &result));
+       TEST_ASSERT_EQUAL_STRING(HOST2, result->hostname);
+       TEST_ASSERT_EQUAL_STRING(REASON2, result->type);
+
+       TEST_ASSERT_EQUAL(1, search_entry(HOST3, &result));
+       TEST_ASSERT_EQUAL_STRING(HOST3, result->hostname);
+       TEST_ASSERT_EQUAL_STRING(REASON3, result->type);
+
+       free(result);
+}
+
+void test_NoMatchInSearch() {
+       char HOST_ADD[] = "192.0.2.6";
+       char HOST_NOTADD[] = "192.0.6.1";
+       char REASON[] = "DENY";
+
+       add_entry(HOST_ADD, REASON);
+
+       struct kod_entry* result;
+
+       TEST_ASSERT_EQUAL(0, search_entry(HOST_NOTADD, &result));
+       TEST_ASSERT_TRUE(result == NULL);
+}
+
+void test_AddDuplicate() {
+       char HOST[] = "192.0.2.3";
+       char REASON1[] = "RATE";
+       char REASON2[] = "DENY";
+
+       add_entry(HOST, REASON1);
+       struct kod_entry* result1;
+       TEST_ASSERT_EQUAL(1, search_entry(HOST, &result1));
+
+       /* 
+        * Sleeps for two seconds since we want to ensure that
+        * the timestamp is updated to a new value.
+        */
+       sleep(2);
+
+       add_entry(HOST, REASON2);
+       struct kod_entry* result2;
+       TEST_ASSERT_EQUAL(1, search_entry(HOST, &result2));
+
+       TEST_ASSERT_FALSE(result1->timestamp == result2->timestamp);
+
+       free(result1);
+       free(result2);
+}
+
+void test_DeleteEntry() {
+       char HOST1[] = "192.0.2.1";
+       char HOST2[] = "192.0.2.2";
+       char HOST3[] = "192.0.2.3";
+       char REASON[] = "DENY";
+
+       add_entry(HOST1, REASON);
+       add_entry(HOST2, REASON);
+       add_entry(HOST3, REASON);
+
+       struct kod_entry* result;
+       
+       TEST_ASSERT_EQUAL(1, search_entry(HOST2, &result));
+       free(result);
+
+       delete_entry(HOST2, REASON);
+
+       TEST_ASSERT_EQUAL(0, search_entry(HOST2, &result));
+
+       // Ensure that the other entry is still there.
+       TEST_ASSERT_EQUAL(1, search_entry(HOST1, &result));
+       free(result);
+}
diff --git a/sntp/tests/run-test-keyFile.c b/sntp/tests/run-test-keyFile.c
new file mode 100644 (file)
index 0000000..a87c229
--- /dev/null
@@ -0,0 +1,49 @@
+/* AUTOGENERATED FILE. DO NOT EDIT. */
+
+//=======Test Runner Used To Run Each Test Below=====
+#define RUN_TEST(TestFunc, TestLineNum) \
+{ \
+  Unity.CurrentTestName = #TestFunc; \
+  Unity.CurrentTestLineNumber = TestLineNum; \
+  Unity.NumberOfTests++; \
+  if (TEST_PROTECT()) \
+  { \
+      setUp(); \
+      TestFunc(); \
+  } \
+  if (TEST_PROTECT() && !TEST_IS_IGNORED) \
+  { \
+    tearDown(); \
+  } \
+  UnityConcludeTest(); \
+}
+
+//=======Automagically Detected Files To Include=====
+#include "unity.h"
+#include <setjmp.h>
+#include <stdio.h>
+
+//=======External Functions This Runner Calls=====
+extern void setUp(void);
+extern void tearDown(void);
+
+
+//=======Test Reset Option=====
+void resetTest()
+{
+  tearDown();
+  setUp();
+}
+
+char *progname;
+
+
+//=======MAIN=====
+int main(int argc, char *argv[])
+{
+  progname = argv[0];
+  Unity.TestFile = "keyFile.c";
+  UnityBegin("keyFile.c");
+
+  return (UnityEnd());
+}
diff --git a/sntp/tests/run-test-kodDatabase.c b/sntp/tests/run-test-kodDatabase.c
new file mode 100644 (file)
index 0000000..7fc9678
--- /dev/null
@@ -0,0 +1,59 @@
+/* AUTOGENERATED FILE. DO NOT EDIT. */
+
+//=======Test Runner Used To Run Each Test Below=====
+#define RUN_TEST(TestFunc, TestLineNum) \
+{ \
+  Unity.CurrentTestName = #TestFunc; \
+  Unity.CurrentTestLineNumber = TestLineNum; \
+  Unity.NumberOfTests++; \
+  if (TEST_PROTECT()) \
+  { \
+      setUp(); \
+      TestFunc(); \
+  } \
+  if (TEST_PROTECT() && !TEST_IS_IGNORED) \
+  { \
+    tearDown(); \
+  } \
+  UnityConcludeTest(); \
+}
+
+//=======Automagically Detected Files To Include=====
+#include "unity.h"
+#include <setjmp.h>
+#include <stdio.h>
+
+//=======External Functions This Runner Calls=====
+extern void setUp(void);
+extern void tearDown(void);
+extern void test_SingleEntryHandling();
+extern void test_MultipleEntryHandling();
+extern void test_NoMatchInSearch();
+extern void test_AddDuplicate();
+extern void test_DeleteEntry();
+
+
+//=======Test Reset Option=====
+void resetTest()
+{
+  tearDown();
+  setUp();
+}
+
+char *progname;
+
+
+//=======MAIN=====
+int main(int argc, char *argv[])
+{
+  progname = argv[0];
+  Unity.TestFile = "kodDatabase.c";
+  UnityBegin("kodDatabase.c");
+  RUN_TEST(test_SingleEntryHandling, 22);
+  RUN_TEST(test_MultipleEntryHandling, 35);
+  RUN_TEST(test_NoMatchInSearch, 66);
+  RUN_TEST(test_AddDuplicate, 79);
+  RUN_TEST(test_DeleteEntry, 104);
+
+  return (UnityEnd());
+}