From: Damir Tomic Date: Sat, 20 Jun 2015 12:14:57 +0000 (+0200) Subject: Makefile.am: X-Git-Tag: NTP_4_3_43~2^2~8 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8ce2bf5dd64ebfc85c50c74868a63634200c385a;p=thirdparty%2Fntp.git Makefile.am: added a few more test files to cleanfiles g_utilities.cpp: Rename: sntp/tests/utilities.cpp -> sntp/tests/g_utilities.cpp bug-2803.h~e9cdd024b7db50f6: Delete: tests/bug-2803/bug-2803.h Many files: new file Makefile.am: added utilities test, fixed a minor bug with CLEANFILES var generate_test_runner.rb: for now, I'm giving up on removing functions that are written twice (because of declaraion and defintion). That regex is difficult because ruby script splits with newline, ; and { and }. I just added declaration for resetTest, to avoid warnings bk: 55855941ev534BKyFM3_QLLvSEHN0g --- diff --git a/sntp/tests/Makefile.am b/sntp/tests/Makefile.am index 591fd101e..c0654ac36 100644 --- a/sntp/tests/Makefile.am +++ b/sntp/tests/Makefile.am @@ -1,6 +1,6 @@ NULL = BUILT_SOURCES = -CLEANFILES = kod-output-blank kod-output-single kod-output-multiple +CLEANFILES = kod-output-blank kod-output-single kod-output-multiple debug-output-lfp-dec debug-output-lfp-bin debug-output-pkt DISTCLEANFILES = kod-output-blank kod-output-single kod-output-multiple EXTRA_PROGRAMS = @@ -15,6 +15,7 @@ check_PROGRAMS = \ test-kodDatabase \ test-kodFile \ test-networking \ + test-utilities \ $(NULL) if GTEST_AVAILABLE @@ -40,7 +41,7 @@ tests_SOURCES = \ g_networking.cpp \ packetHandling.cpp \ packetProcessing.cpp \ - utilities.cpp \ + g_utilities.cpp \ $(NULL) noinst_HEADERS = \ @@ -68,7 +69,7 @@ EXTRA_DIST = \ data/kod-test-empty \ $(NULL) -CLEANFILES = \ +CLEANFILES += \ data/kod-output-multiple \ data/kod-output-single \ data/debug-output-pkt \ @@ -92,6 +93,7 @@ LDADD = \ unity_tests_LDADD = \ $(LDADD) \ $(top_builddir)/unity/libunity.a \ + $(LIBM) \ $(NULL) AM_CFLAGS = $(CFLAGS_NTP) @@ -112,6 +114,7 @@ BUILT_SOURCES += \ $(srcdir)/run-kodDatabase.c \ $(srcdir)/run-kodFile.c \ $(srcdir)/run-networking.c \ + $(srcdir)/run-utilities.c \ $(NULL) @@ -123,16 +126,6 @@ test_keyFile_LDADD = \ $(unity_tests_LDADD) \ $(NULL) -test_keyFile_SOURCES = \ - keyFile.c \ - run-keyFile.c \ - $(top_builddir)/version.c \ - $(NULL) - -$(srcdir)/run-keyFile.c: $(srcdir)/keyFile.c $(std_unity_list) - $(run_unity) keyFile.c run-keyFile.c - - test_kodDatabase_CFLAGS = \ -I$(top_srcdir)/unity \ $(NULL) @@ -141,16 +134,6 @@ test_kodDatabase_LDADD = \ $(unity_tests_LDADD) \ $(NULL) -test_kodDatabase_SOURCES = \ - kodDatabase.c \ - run-kodDatabase.c \ - $(top_builddir)/version.c \ - $(NULL) - -$(srcdir)/run-kodDatabase.c: $(srcdir)/kodDatabase.c $(std_unity_list) - $(run_unity) kodDatabase.c run-kodDatabase.c - - test_kodFile_CFLAGS = \ -I$(top_srcdir)/unity \ $(NULL) @@ -159,34 +142,69 @@ test_kodFile_LDADD = \ $(unity_tests_LDADD) \ $(NULL) -test_kodFile_SOURCES = \ - kodFile.c \ - run-kodFile.c \ - $(top_builddir)/version.c \ +test_networking_CFLAGS = \ + -I$(top_srcdir)/unity \ $(NULL) -# ../version.c - -$(srcdir)/run-kodFile.c: $(srcdir)/kodFile.c $(std_unity_list) - $(run_unity) kodFile.c run-kodFile.c +test_networking_LDADD = \ + $(unity_tests_LDADD) \ + $(NULL) -test_networking_CFLAGS = \ +test_utilities_CFLAGS = \ -I$(top_srcdir)/unity \ $(NULL) -test_networking_LDADD = \ +test_utilities_LDADD = \ + $(LDADD) \ $(unity_tests_LDADD) \ $(NULL) + test_networking_SOURCES = \ networking.c \ run-networking.c \ $(top_builddir)/version.c \ $(NULL) +test_kodDatabase_SOURCES = \ + kodDatabase.c \ + run-kodDatabase.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 + +test_utilities_SOURCES = \ + utilities.c \ + run-utilities.c \ + $(NULL) + + +$(srcdir)/run-kodFile.c: $(srcdir)/kodFile.c $(std_unity_list) + $(run_unity) kodFile.c run-kodFile.c + +$(srcdir)/run-keyFile.c: $(srcdir)/keyFile.c $(std_unity_list) + $(run_unity) keyFile.c run-keyFile.c + +$(srcdir)/run-kodDatabase.c: $(srcdir)/kodDatabase.c $(std_unity_list) + $(run_unity) kodDatabase.c run-kodDatabase.c + $(srcdir)/run-networking.c: $(srcdir)/networking.c $(std_unity_list) $(run_unity) networking.c run-networking.c +$(srcdir)/run-utilities.c: $(srcdir)/utilities.c $(std_unity_list) + $(run_unity) utilities.c run-utilities.c #$(srcdir)/../version.c: $(srcdir)/../version.c # gcc -o version.o ../version.c diff --git a/sntp/tests/data/debug-output-lfp-bin b/sntp/tests/data/debug-output-lfp-bin new file mode 100644 index 000000000..99d559cc2 --- /dev/null +++ b/sntp/tests/data/debug-output-lfp-bin @@ -0,0 +1,3 @@ +-------------------------------------------------------------------------------- +0: 00000000 1: 00000000 2: 00000000 3: 00111111 4: 00000000 5: 00000000 6: 00000000 7: 01111111 +-------------------------------------------------------------------------------- diff --git a/sntp/tests/data/debug-output-lfp-dec b/sntp/tests/data/debug-output-lfp-dec new file mode 100644 index 000000000..6e81c8543 --- /dev/null +++ b/sntp/tests/data/debug-output-lfp-dec @@ -0,0 +1,3 @@ +-------------------------------------------------------------------------------- +0: 0 1: 0 2: 24 3: 166 4: 0 5: 4 6: 181 7: 22 +-------------------------------------------------------------------------------- diff --git a/sntp/tests/data/debug-output-pkt b/sntp/tests/data/debug-output-pkt new file mode 100644 index 000000000..ff61b7526 --- /dev/null +++ b/sntp/tests/data/debug-output-pkt @@ -0,0 +1,8 @@ +-------------------------------------------------------------------------------- +0: 24 1: 0 2: 0 3: 0 4: 0 5: 0 6: 0 7: 0 +8: 0 9: 0 10: 0 11: 0 12: 0 13: 0 14: 0 15: 0 +16: 0 17: 0 18: 0 19: 0 20: 0 21: 0 22: 0 23: 0 +24: 0 25: 0 26: 0 27: 0 28: 0 29: 0 30: 0 31: 0 +32: 0 33: 0 34: 0 35: 0 36: 0 37: 0 38: 0 39: 0 +40: 0 41: 0 42: 0 43: 8 44: 7f 45: ff 46: ff 47: ff +-------------------------------------------------------------------------------- diff --git a/sntp/tests/utilities.cpp b/sntp/tests/g_utilities.cpp similarity index 100% rename from sntp/tests/utilities.cpp rename to sntp/tests/g_utilities.cpp diff --git a/sntp/tests/run-utilities.c b/sntp/tests/run-utilities.c new file mode 100644 index 000000000..715bd110c --- /dev/null +++ b/sntp/tests/run-utilities.c @@ -0,0 +1,64 @@ +/* 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); +void resetTest(void); +extern void test_IPv4Address(); +extern void test_IPv6Address(); +extern void test_SetLiVnMode1(); +extern void test_SetLiVnMode2(); +extern void test_PktOutput(); +extern void test_LfpOutputBinaryFormat(); +extern void test_LfpOutputDecimalFormat(); + + +//=======Test Reset Option===== +void resetTest() +{ + tearDown(); + setUp(); +} + +char *progname; + + +//=======MAIN===== +int main(int argc, char *argv[]) +{ + progname = argv[0]; + Unity.TestFile = "utilities.c"; + UnityBegin("utilities.c"); + RUN_TEST(test_IPv4Address, 68); + RUN_TEST(test_IPv6Address, 78); + RUN_TEST(test_SetLiVnMode1, 98); + RUN_TEST(test_SetLiVnMode2, 111); + RUN_TEST(test_PktOutput, 126); + RUN_TEST(test_LfpOutputBinaryFormat, 146); + RUN_TEST(test_LfpOutputDecimalFormat, 162); + + return (UnityEnd()); +} diff --git a/sntp/tests/utilities.c b/sntp/tests/utilities.c new file mode 100644 index 000000000..65be8df9d --- /dev/null +++ b/sntp/tests/utilities.c @@ -0,0 +1,177 @@ +#include "config.h" + +#include "sntptest.h" +#include "fileHandlingTest.h" + +#include "main.h" +#include "utilities.h" +#include "math.h" + +#include "unity.h" + +const char * Version = "stub unit test Version string"; + + + +sockaddr_u CreateSockaddr4(const char* address) { + sockaddr_u s; + s.sa4.sin_family = AF_INET; + s.sa4.sin_addr.s_addr = inet_addr(address); + SET_PORT(&s, 123); + + return s; +} + +struct addrinfo CreateAddrinfo( sockaddr_u* sock) { + struct addrinfo a; + a.ai_family = sock->sa.sa_family; + a.ai_addrlen = SIZEOF_SOCKADDR(a.ai_family); + a.ai_addr = &sock->sa; + return a; +} + + +bool outputFileOpened; +FILE* outputFile; + +//debugUtilitiesTest() : outputFileOpened(false) {} + +void InitDebugTest(const char * filename) { + // Clear the contents of the current file. + // Open the output file + outputFile = fopen(filename, "w+"); + TEST_ASSERT_TRUE(outputFile != NULL); + outputFileOpened = true; +} + +// Closes outputFile, and compare contents. +void FinishDebugTest(const char * expected, + const char * actual) { + if (outputFileOpened) + fclose(outputFile); + + FILE * e = fopen(expected,"rb"); + FILE * a = fopen(actual,"rb"); + TEST_ASSERT_TRUE(e != NULL); + TEST_ASSERT_TRUE(a != NULL); + + CompareFileContent(e, a); +} + + +/* + * These tests are essentially a copy of the tests for socktoa() + * in libntp. If sntp switches to using that functions, these + * tests can be removed. + */ + +void test_IPv4Address() { + const char* ADDR = "192.0.2.10"; + + sockaddr_u input = CreateSockaddr4(ADDR); + struct addrinfo inputA = CreateAddrinfo(&input); + + TEST_ASSERT_EQUAL_STRING(ADDR, ss_to_str(&input)); + TEST_ASSERT_EQUAL_STRING(ADDR, addrinfo_to_str(&inputA)); +} + +void test_IPv6Address() { + const struct in6_addr address = { + 0x20, 0x01, 0x0d, 0xb8, + 0x85, 0xa3, 0x08, 0xd3, + 0x13, 0x19, 0x8a, 0x2e, + 0x03, 0x70, 0x73, 0x34 + }; + const char * expected = "2001:db8:85a3:8d3:1319:8a2e:370:7334"; + sockaddr_u input; + struct addrinfo inputA; + + memset(&input, 0, sizeof(input)); + input.sa6.sin6_family = AF_INET6; + input.sa6.sin6_addr = address; + TEST_ASSERT_EQUAL_STRING(expected, ss_to_str(&input)); + + inputA = CreateAddrinfo(&input); + TEST_ASSERT_EQUAL_STRING(expected, addrinfo_to_str(&inputA)); +} + +void test_SetLiVnMode1() { + struct pkt expected; + expected.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING, + NTP_VERSION, + MODE_SERVER); + + struct pkt actual; + set_li_vn_mode(&actual, LEAP_NOWARNING, NTP_VERSION, + MODE_SERVER); + + TEST_ASSERT_EQUAL(expected.li_vn_mode, actual.li_vn_mode); +} + +void test_SetLiVnMode2() { + struct pkt expected; + expected.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOTINSYNC, + NTP_OLDVERSION, + MODE_BROADCAST); + + struct pkt actual; + set_li_vn_mode(&actual, LEAP_NOTINSYNC, NTP_OLDVERSION, + MODE_BROADCAST); + + TEST_ASSERT_EQUAL(expected.li_vn_mode, actual.li_vn_mode); +} + +/* Debug utilities tests */ + +void test_PktOutput() { + char * filename = "debug-output-pkt";//CreatePath("debug-output-pkt", OUTPUT_DIR); + InitDebugTest(filename); + + struct pkt testpkt; + memset(&testpkt, 0, sizeof(struct pkt)); + testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING, + NTP_VERSION, + MODE_SERVER); + + l_fp test; + test.l_ui = 8; + test.l_uf = 2147483647; // Lots of ones. + HTONL_FP(&test, &testpkt.xmt); + + pkt_output(&testpkt, LEN_PKT_NOMAC, outputFile); + + FinishDebugTest(CreatePath("debug-input-pkt", INPUT_DIR), filename); +} + +void test_LfpOutputBinaryFormat() { + char * filename = "debug-output-lfp-bin";//CreatePath("debug-output-lfp-bin", OUTPUT_DIR); + InitDebugTest(filename); + + l_fp test; + test.l_ui = 63; // 00000000 00000000 00000000 00111111 + test.l_uf = 127; // 00000000 00000000 00000000 01111111 + + l_fp network; + HTONL_FP(&test, &network); + + l_fp_output_bin(&network, outputFile); + + FinishDebugTest(CreatePath("debug-input-lfp-bin", INPUT_DIR), filename); +} + +void test_LfpOutputDecimalFormat() { + char * filename = "debug-output-lfp-dec"; //CreatePath("debug-output-lfp-dec", OUTPUT_DIR); + InitDebugTest(filename); + + l_fp test; + test.l_ui = 6310; // 0x000018A6 + test.l_uf = 308502; // 0x00004B516 + + l_fp network; + HTONL_FP(&test, &network); + + l_fp_output_dec(&network, outputFile); + + FinishDebugTest(CreatePath("debug-input-lfp-dec", INPUT_DIR), filename); +} + diff --git a/sntp/unity/auto/generate_test_runner.rb b/sntp/unity/auto/generate_test_runner.rb index ada8aa5ae..c115fadcc 100644 --- a/sntp/unity/auto/generate_test_runner.rb +++ b/sntp/unity/auto/generate_test_runner.rb @@ -144,6 +144,7 @@ class UnityTestRunnerGenerator output.puts("\n//=======External Functions This Runner Calls=====") output.puts("extern void setUp(void);") output.puts("extern void tearDown(void);") + output.puts("void resetTest(void);") tests.each do |test| output.puts("extern void #{test[:name]}(#{test[:call]});") end diff --git a/tests/bug-2803/bug-2803.c b/tests/bug-2803/bug-2803.c index d17cca342..e494ca263 100644 --- a/tests/bug-2803/bug-2803.c +++ b/tests/bug-2803/bug-2803.c @@ -7,12 +7,15 @@ #include #include "unity.h" -#include "bug-2803.h" +//#include "bug-2803.h" /* microseconds per second */ #define MICROSECONDS 1000000 - +int simpleTest(void); +void setUp(void); +void tearDown(void); +//void test_main(void); static int verbose = 1; // if not 0, also print results if test passed static int exit_on_err = 0; // if not 0, exit if test failed diff --git a/tests/bug-2803/bug-2803.h b/tests/bug-2803/bug-2803.h deleted file mode 100644 index 70e501efe..000000000 --- a/tests/bug-2803/bug-2803.h +++ /dev/null @@ -1,14 +0,0 @@ -#include - -#include -#include - -#include -#include - -#include "unity.h" - -int simpleTest(void); -void setUp(void); -void tearDown(void); -void test_main(void); diff --git a/tests/bug-2803/run-bug-2803.c b/tests/bug-2803/run-bug-2803.c index 4022167d8..8382c5aa8 100644 --- a/tests/bug-2803/run-bug-2803.c +++ b/tests/bug-2803/run-bug-2803.c @@ -26,8 +26,9 @@ //=======External Functions This Runner Calls===== extern void setUp(void); extern void tearDown(void); -extern void test_main(void ); void resetTest(void); +extern void test_main(void ); + //=======Test Reset Option===== void resetTest() @@ -45,7 +46,7 @@ int main(int argc, char *argv[]) progname = argv[0]; Unity.TestFile = "bug-2803.c"; UnityBegin("bug-2803.c"); - RUN_TEST(test_main, 117); + RUN_TEST(test_main, 18); return (UnityEnd()); }