From: Damir Tomic Date: Fri, 19 Jun 2015 01:13:24 +0000 (+0200) Subject: g_kodFile.cpp: X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4d493822d0b459e65429d150d3bd68ca777cc3e5;p=thirdparty%2Fntp.git g_kodFile.cpp: Rename: sntp/tests/kodFile.cpp -> sntp/tests/g_kodFile.cpp g_keyFile.cpp: Rename: sntp/tests/keyFile.cpp -> sntp/tests/g_keyFile.cpp run-kodFile.c, kodFile.c: new file Makefile.am: replaced ../version.c with $(top_builddir)/version.c. Converted new test. c_fileHandlingTest.h: converted all functions from C++ to C keyFile.c: uncommented all functions, 90% converted bk: 55836cb4SunCNYQelT03F_4Lx9TVbg --- diff --git a/sntp/tests/Makefile.am b/sntp/tests/Makefile.am index cc8b08fac..1ea90e067 100644 --- a/sntp/tests/Makefile.am +++ b/sntp/tests/Makefile.am @@ -8,7 +8,9 @@ run_unity = cd $(srcdir) && ruby ../../sntp/unity/auto/generate_test_runner.rb EXTRA_PROGRAMS += test-keyFile check_PROGRAMS = \ + test-keyFile \ test-kodDatabase \ + test-kodFile \ test-networking \ $(NULL) @@ -29,9 +31,9 @@ base_SOURCES = \ tests_SOURCES = \ $(base_SOURCES) \ crypto.cpp \ - keyFile.cpp \ + g_keyFile.cpp \ g_kodDatabase.cpp \ - kodFile.cpp \ + g_kodFile.cpp \ g_networking.cpp \ packetHandling.cpp \ packetProcessing.cpp \ @@ -107,6 +109,7 @@ BUILT_SOURCES += \ $(srcdir)/run-networking.c \ $(srcdir)/run-kodDatabase.c \ $(srcdir)/run-keyFile.c \ + $(srcdir)/run-kodFile.c \ $(NULL) test_networking_CFLAGS = \ @@ -133,23 +136,39 @@ test_keyFile_LDADD = \ $(unity_tests_LDADD) \ $(NULL) +test_kodFile_CFLAGS = \ + -I$(top_srcdir)/unity \ + $(NULL) + +test_kodFile_LDADD = \ + $(unity_tests_LDADD) \ + $(NULL) + test_networking_SOURCES = \ networking.c \ run-networking.c \ - ../version.c \ + $(top_builddir)/version.c \ $(NULL) test_kodDatabase_SOURCES = \ kodDatabase.c \ run-kodDatabase.c \ - ../version.c \ + $(top_builddir)/version.c \ $(NULL) test_keyFile_SOURCES = \ keyFile.c \ run-keyFile.c \ + $(top_builddir)/version.c \ + $(NULL) + +test_kodFile_SOURCES = \ + kodFile.c \ + run-kodFile.c \ + $(top_builddir)/version.c \ $(NULL) +# ../version.c $(srcdir)/run-networking.c: $(srcdir)/networking.c $(std_unity_list) $(run_unity) networking.c run-networking.c @@ -163,6 +182,9 @@ $(srcdir)/run-kodDatabase.c: $(srcdir)/kodDatabase.c $(std_unity_list) $(srcdir)/run-keyFile.c: $(srcdir)/keyFile.c $(std_unity_list) $(run_unity) keyFile.c run-keyFile.c +$(srcdir)/run-kodFile.c: $(srcdir)/kodFile.c $(std_unity_list) + $(run_unity) kodFile.c run-kodFile.c + TESTS = if !NTP_CROSSCOMPILE diff --git a/sntp/tests/c_fileHandlingTest.h b/sntp/tests/c_fileHandlingTest.h index 79c619238..34d368923 100644 --- a/sntp/tests/c_fileHandlingTest.h +++ b/sntp/tests/c_fileHandlingTest.h @@ -1,24 +1,22 @@ #ifndef FILE_HANDLING_TEST_H #define FILE_HANDLING_TEST_H +#include "stdlib.h" #include "c_sntptest.h" -//#include #include -//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; +const char * CreatePath(const char* filename, enum DirectoryType argument) { + + char * path = malloc (sizeof (char) * 100); + /* if (m_params.size() >= argument + 1) { path = m_params[argument]; } @@ -26,37 +24,66 @@ char * CreatePath(const char* filename, DirectoryType argument) { if (path[path.size()-1] != DIR_SEP && !path.empty()) { path.append(1, DIR_SEP); } - path.append(filename); + */ + //strcpy(path,filename); + //path.append(filename); + + //return path; + + strcpy(path,"../../../sntp/tests/data/"); + strcat(path,filename); return path; } -int GetFileSize(ifstream& file) { - int initial = file.tellg(); +int GetFileSize(FILE *file) { - file.seekg(0, ios::end); - int length = file.tellg(); - file.seekg(initial); + fseek(file, 0L, SEEK_END); + int length = ftell(file); + fseek(file, 0L, SEEK_SET); + + //int initial = file.tellg(); + + //file.seekg(0, ios::end); + //int length = file.tellg(); + //file.seekg(initial); return length; } -void CompareFileContent(ifstream& expected, ifstream& actual) { +bool CompareFileContent(FILE* expected, FILE* 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; + + char * actualLine=NULL; + char * expectedLine = NULL; + size_t lenAct = 0; + size_t lenExp = 0; + ssize_t readAct,readExp; + + while ( ( (readAct = getline(&actualLine, &lenAct, actual)) != -1) && ( (readExp = getline(&expectedLine, &lenExp, expected)) != -1 ) ) { + + //printf("%s",actualLine); + //printf("%s",expectedLine); + + if( strcmp(actualLine,expectedLine) !=0 ){ + printf("Comparision failed on line %d",currentLine); + return FALSE; + } + + //I removed this and modified the test kodFile.c, because there shouldn't be any ASSERTs in .h files! + //TEST_ASSERT_EQUAL_STRING(actualLine,expectedLine);//EXPECT_EQ(expectedLine, actualLine) << "Comparision failed on line " << currentLine; currentLine++; } + + return TRUE; } -void ClearFile(const std::string& filename) { - std::ofstream clear(filename.c_str(), ios::trunc); - ASSERT_TRUE(clear.good()); - clear.close(); +void ClearFile(const char * filename) { + FILE * clear = fopen(filename, "w");//ios::trunc); //similar to truncate, I GUESS???! + + //I removed this because there shouldn't be any ASSERTs in .h files! + //TEST_ASSERT_TRUE(clear != NULL); + fclose(clear); } diff --git a/sntp/tests/keyFile.cpp b/sntp/tests/g_keyFile.cpp similarity index 100% rename from sntp/tests/keyFile.cpp rename to sntp/tests/g_keyFile.cpp diff --git a/sntp/tests/kodFile.cpp b/sntp/tests/g_kodFile.cpp similarity index 100% rename from sntp/tests/kodFile.cpp rename to sntp/tests/g_kodFile.cpp diff --git a/sntp/tests/keyFile.c b/sntp/tests/keyFile.c index 041b72446..b8fba41fe 100644 --- a/sntp/tests/keyFile.c +++ b/sntp/tests/keyFile.c @@ -1,41 +1,45 @@ #include "config.h" #include "c_fileHandlingTest.h" +#include "ntp_stdlib.h" #include "ntp_types.h" #include "crypto.h" -typedef int bool; +#include "unity.h" -bool AssertionResult CompareKeys(key& expected, key& actual) { +//typedef int bool; + + +bool CompareKeys(struct key expected, struct 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; + printf(" but was: %d\n", actual.key_id); + return FALSE; } if (expected.key_len != actual.key_len){ printf("Expected key_len: %d", expected.key_len); - printf(" but was: %d", actual.key_len); - return TRUE; + printf(" but was: %d\n", actual.key_len); + return FALSE; } if (strcmp(expected.type, actual.type) != 0){ - printf("Expected key_type: %d", expected.type); - printf(" but was: ", actual.type); - return TRUE; + printf("Expected key_type: %s", expected.type); + printf(" but was: %s\n", actual.type); + return FALSE; } if (memcmp(expected.key_seq, actual.key_seq, expected.key_len) != 0){ - printf("Key mismatch!"); - return TRUE; + printf("Key mismatch!\n"); + return FALSE; } return TRUE; } -bool CompareKeys(int key_id, +bool CompareKeysAlternative(int key_id, int key_len, const char* type, const char* key_seq, - key& actual) { - key temp; + struct key actual) { + struct key temp; temp.key_id = key_id; temp.key_len = key_len; @@ -46,89 +50,89 @@ bool CompareKeys(int key_id, } -void(ReadEmptyKeyFile) { - key* keys = NULL; +void test_ReadEmptyKeyFile() { + struct key* keys = NULL; - TEST_ASSERT_EQUAL(0, auth_init(CreatePath("key-test-empty", INPUT_DIR).c_str(), &keys)); + TEST_ASSERT_EQUAL(0, auth_init(CreatePath("key-test-empty", INPUT_DIR), &keys)); TEST_ASSERT_TRUE(keys == NULL); } -void(ReadASCIIKeys) { - key* keys = NULL; +void test_ReadASCIIKeys() { + struct key* keys = NULL; - TEST_ASSERT_EQUAL(2, auth_init(CreatePath("key-test-ascii", INPUT_DIR).c_str(), &keys)); + TEST_ASSERT_EQUAL(2, auth_init(CreatePath("key-test-ascii", INPUT_DIR), &keys)); TEST_ASSERT_TRUE(keys != NULL); - key* result = NULL; + struct key* result = NULL; get_key(40, &result); TEST_ASSERT_TRUE(result != NULL); - TEST_ASSERT_TRUE(CompareKeys(40, 11, "MD5", "asciikeyTwo", *result)); + TEST_ASSERT_TRUE(CompareKeysAlternative(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)); + TEST_ASSERT_TRUE(CompareKeysAlternative(50, 11, "MD5", "asciikeyOne", *result)); } -void(ReadHexKeys) { - key* keys = NULL; +void test_ReadHexKeys() { + struct key* keys = NULL; - TEST_ASSERT_EQUAL(3, auth_init(CreatePath("key-test-hex", INPUT_DIR).c_str(), &keys)); + TEST_ASSERT_EQUAL(3, auth_init(CreatePath("key-test-hex", INPUT_DIR), &keys)); TEST_ASSERT_TRUE(keys != NULL); - key* result = NULL; + struct key* result = NULL; get_key(10, &result); TEST_ASSERT_TRUE(result != NULL); - TEST_ASSERT_TRUE(CompareKeys(10, 13, "MD5", + TEST_ASSERT_TRUE(CompareKeysAlternative(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)); + TEST_ASSERT_TRUE(CompareKeysAlternative(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)); + TEST_ASSERT_TRUE(CompareKeysAlternative(30, 13, "MD5", data2, *result)); } -void(ReadKeyFileWithComments) { - key* keys = NULL; +void test_ReadKeyFileWithComments() { + struct key* keys = NULL; - TEST_ASSERT_EQUAL(2, auth_init(CreatePath("key-test-comments", INPUT_DIR).c_str(), &keys)); + TEST_ASSERT_EQUAL(2, auth_init(CreatePath("key-test-comments", INPUT_DIR), &keys)); TEST_ASSERT_TRUE(keys != NULL); - key* result = NULL; + struct 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)); + TEST_ASSERT_TRUE(CompareKeysAlternative(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)); + TEST_ASSERT_TRUE(CompareKeysAlternative(34, 3, "MD5", "xyz", *result)); } -void(ReadKeyFileWithInvalidHex) { - key* keys = NULL; +void test_ReadKeyFileWithInvalidHex() { + struct key* keys = NULL; - TEST_ASSERT_EQUAL(1, auth_init(CreatePath("key-test-invalid-hex", INPUT_DIR).c_str(), &keys)); + TEST_ASSERT_EQUAL(1, auth_init(CreatePath("key-test-invalid-hex", INPUT_DIR), &keys)); TEST_ASSERT_TRUE(keys != NULL); - key* result = NULL; + struct 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)); + TEST_ASSERT_TRUE(CompareKeysAlternative(10, 15, "MD5", data, *result)); result = NULL; get_key(30, &result); // Should not exist, and result should remain NULL. diff --git a/sntp/tests/kodFile.c b/sntp/tests/kodFile.c new file mode 100644 index 000000000..d84c1a0c4 --- /dev/null +++ b/sntp/tests/kodFile.c @@ -0,0 +1,142 @@ +#include "config.h" +#include "ntp_types.h" +#include "ntp_stdlib.h" // For estrdup() + + +#include "c_fileHandlingTest.h" + +#include "kod_management.h" + +#include "unity.h" + +/* + * We access some parts of the kod database directly, without + * going through the public interface + */ +extern int kod_db_cnt; +extern struct kod_entry** kod_db; +extern char* kod_db_file; + +void setUp() { + kod_db_cnt = 0; + kod_db = NULL; +} + +void tearDown() { +} + + +void test_ReadEmptyFile() { + kod_init_kod_db(CreatePath("kod-test-empty", INPUT_DIR), TRUE); + + TEST_ASSERT_EQUAL(0, kod_db_cnt); +} + +void test_ReadCorrectFile() { + kod_init_kod_db(CreatePath("kod-test-correct", INPUT_DIR), TRUE); + + TEST_ASSERT_EQUAL(2, kod_db_cnt); + + struct kod_entry* res; + + TEST_ASSERT_EQUAL(1, search_entry("192.0.2.5", &res)); + TEST_ASSERT_EQUAL_STRING("DENY", res->type); + TEST_ASSERT_EQUAL_STRING("192.0.2.5", res->hostname); + TEST_ASSERT_EQUAL(0x12345678, res->timestamp); + + TEST_ASSERT_EQUAL(1, search_entry("192.0.2.100", &res)); + TEST_ASSERT_EQUAL_STRING("RSTR", res->type); + TEST_ASSERT_EQUAL_STRING("192.0.2.100", res->hostname); + TEST_ASSERT_EQUAL(0xfff, res->timestamp); +} + +void test_ReadFileWithBlankLines() { + kod_init_kod_db(CreatePath("kod-test-blanks", INPUT_DIR), TRUE); + + TEST_ASSERT_EQUAL(3, kod_db_cnt); + + struct kod_entry* res; + + TEST_ASSERT_EQUAL(1, search_entry("192.0.2.5", &res)); + TEST_ASSERT_EQUAL_STRING("DENY", res->type); + TEST_ASSERT_EQUAL_STRING("192.0.2.5", res->hostname); + TEST_ASSERT_EQUAL(0x12345678, res->timestamp); + + TEST_ASSERT_EQUAL(1, search_entry("192.0.2.100", &res)); + TEST_ASSERT_EQUAL_STRING("RSTR", res->type); + TEST_ASSERT_EQUAL_STRING("192.0.2.100", res->hostname); + TEST_ASSERT_EQUAL(0xfff, res->timestamp); + + TEST_ASSERT_EQUAL(1, search_entry("example.com", &res)); + TEST_ASSERT_EQUAL_STRING("DENY", res->type); + TEST_ASSERT_EQUAL_STRING("example.com", res->hostname); + TEST_ASSERT_EQUAL(0xabcd, res->timestamp); +} + +void test_WriteEmptyFile() { + kod_db_file = estrdup(CreatePath("kod-output-blank", OUTPUT_DIR)); + + write_kod_db(); + + // Open file and ensure that the filesize is 0 bytes. + FILE * is; + is = fopen(kod_db_file, "rb");//std::ios::binary); + TEST_ASSERT_FALSE(is == NULL );//is.fail()); + + TEST_ASSERT_EQUAL(0, GetFileSize(is)); + + fclose(is); +} + +void test_WriteFileWithSingleEntry() { + kod_db_file = estrdup(CreatePath("kod-output-single", OUTPUT_DIR)); + + 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. + FILE * actual = fopen(kod_db_file, "rb"); + FILE * expected = fopen(CreatePath("kod-expected-single", INPUT_DIR),"rb"); + TEST_ASSERT_TRUE(actual !=NULL);//TEST_ASSERT_TRUE(actual.good()); + TEST_ASSERT_TRUE(expected !=NULL);//TEST_ASSERT_TRUE(expected.good()); + + TEST_ASSERT_EQUAL(GetFileSize(expected), GetFileSize(actual)); + + TEST_ASSERT_TRUE(CompareFileContent(expected, actual)); +} + +void test_WriteFileWithMultipleEntries() { + kod_db_file = estrdup(CreatePath("kod-output-multiple", OUTPUT_DIR)); + + 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. + FILE * actual = fopen(kod_db_file, "rb"); + FILE * expected = fopen(CreatePath("kod-expected-multiple", INPUT_DIR),"rb"); + TEST_ASSERT_TRUE(actual !=NULL);//TEST_ASSERT_TRUE(actual.good()); + TEST_ASSERT_TRUE(expected !=NULL);//TEST_ASSERT_TRUE(expected.good()); + + + TEST_ASSERT_EQUAL(GetFileSize(expected), GetFileSize(actual)); + + TEST_ASSERT_TRUE(CompareFileContent(expected, actual)); +} + diff --git a/sntp/tests/run-keyFile.c b/sntp/tests/run-keyFile.c index a87c22984..0a6380d33 100644 --- a/sntp/tests/run-keyFile.c +++ b/sntp/tests/run-keyFile.c @@ -26,6 +26,11 @@ //=======External Functions This Runner Calls===== extern void setUp(void); extern void tearDown(void); +extern void test_ReadEmptyKeyFile(); +extern void test_ReadASCIIKeys(); +extern void test_ReadHexKeys(); +extern void test_ReadKeyFileWithComments(); +extern void test_ReadKeyFileWithInvalidHex(); //=======Test Reset Option===== @@ -44,6 +49,11 @@ int main(int argc, char *argv[]) progname = argv[0]; Unity.TestFile = "keyFile.c"; UnityBegin("keyFile.c"); + RUN_TEST(test_ReadEmptyKeyFile, 53); + RUN_TEST(test_ReadASCIIKeys, 61); + RUN_TEST(test_ReadHexKeys, 79); + RUN_TEST(test_ReadKeyFileWithComments, 105); + RUN_TEST(test_ReadKeyFileWithInvalidHex, 124); return (UnityEnd()); } diff --git a/sntp/tests/run-kodFile.c b/sntp/tests/run-kodFile.c new file mode 100644 index 000000000..87b825d85 --- /dev/null +++ b/sntp/tests/run-kodFile.c @@ -0,0 +1,61 @@ +/* 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 +#include + +//=======External Functions This Runner Calls===== +extern void setUp(void); +extern void tearDown(void); +extern void test_ReadEmptyFile(); +extern void test_ReadCorrectFile(); +extern void test_ReadFileWithBlankLines(); +extern void test_WriteEmptyFile(); +extern void test_WriteFileWithSingleEntry(); +extern void test_WriteFileWithMultipleEntries(); + + +//=======Test Reset Option===== +void resetTest() +{ + tearDown(); + setUp(); +} + +char *progname; + + +//=======MAIN===== +int main(int argc, char *argv[]) +{ + progname = argv[0]; + Unity.TestFile = "kodFile.c"; + UnityBegin("kodFile.c"); + RUN_TEST(test_ReadEmptyFile, 29); + RUN_TEST(test_ReadCorrectFile, 35); + RUN_TEST(test_ReadFileWithBlankLines, 53); + RUN_TEST(test_WriteEmptyFile, 76); + RUN_TEST(test_WriteFileWithSingleEntry, 91); + RUN_TEST(test_WriteFileWithMultipleEntries, 114); + + return (UnityEnd()); +}