]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
Makefile.am:
authorDamir Tomic <viperus@ntp.org>
Sat, 20 Jun 2015 12:14:57 +0000 (14:14 +0200)
committerDamir Tomic <viperus@ntp.org>
Sat, 20 Jun 2015 12:14:57 +0000 (14:14 +0200)
  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

sntp/tests/Makefile.am
sntp/tests/data/debug-output-lfp-bin [new file with mode: 0644]
sntp/tests/data/debug-output-lfp-dec [new file with mode: 0644]
sntp/tests/data/debug-output-pkt [new file with mode: 0644]
sntp/tests/g_utilities.cpp [moved from sntp/tests/utilities.cpp with 100% similarity]
sntp/tests/run-utilities.c [new file with mode: 0644]
sntp/tests/utilities.c [new file with mode: 0644]
sntp/unity/auto/generate_test_runner.rb
tests/bug-2803/bug-2803.c
tests/bug-2803/bug-2803.h [deleted file]
tests/bug-2803/run-bug-2803.c

index 591fd101e80d11e382b6fd7152023a9aac2a8deb..c0654ac368146c6fcd4138647f73ef912b94a387 100644 (file)
@@ -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 (file)
index 0000000..99d559c
--- /dev/null
@@ -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 (file)
index 0000000..6e81c85
--- /dev/null
@@ -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 (file)
index 0000000..ff61b75
--- /dev/null
@@ -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/run-utilities.c b/sntp/tests/run-utilities.c
new file mode 100644 (file)
index 0000000..715bd11
--- /dev/null
@@ -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 <setjmp.h>
+#include <stdio.h>
+
+//=======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 (file)
index 0000000..65be8df
--- /dev/null
@@ -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);
+}
+
index ada8aa5ae7fdf99d00534fa4315b036c4bec17a1..c115fadccfee9a7f34bffe1dba5867edc1de2aca 100644 (file)
@@ -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
index d17cca342f988cbb56ed59d0d21d4196581fd47e..e494ca26323760690fc6682b1750d47c078ecfd7 100644 (file)
@@ -7,12 +7,15 @@
 #include <timevalops.h>
 
 #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 (file)
index 70e501e..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-#include <config.h>
-
-#include <stdio.h>
-#include <sys/time.h>
-
-#include <ntp_fp.h>
-#include <timevalops.h>
-
-#include "unity.h"
-
-int simpleTest(void);
-void setUp(void);
-void tearDown(void);
-void test_main(void);
index 4022167d8b5e04d72e0c54705f6aa94b90e8de92..8382c5aa898a2b636497d0cb70f8525d4398437d 100644 (file)
@@ -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());
 }