From: Damir Tomic Date: Sat, 13 Jun 2015 23:40:35 +0000 (+0200) Subject: Many files: X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=accb7296d26be9d2b8f38a671f62fe27944613ee;p=thirdparty%2Fntp.git Many files: new file calyearstart.c: removed the functions from here, put them in test-libntp.c Makefile.am: adding support for new tests g_calyearstart.cpp: Rename: tests/libntp/calyearstart.cpp -> tests/libntp/g_calyearstart.cpp g_clocktime.cpp: Rename: tests/libntp/clocktime.cpp -> tests/libntp/g_clocktime.cpp test-libntp.h: added headers for new functions test-libntp.c: moved the functions which 3 tests use into this file run-test-calyearstart.c: minor update bk: 557cbf73UcilkAjKKA_xbjnxWJzxAQ --- diff --git a/tests/libntp/Makefile.am b/tests/libntp/Makefile.am index 9bb33fd56..11a824a35 100644 --- a/tests/libntp/Makefile.am +++ b/tests/libntp/Makefile.am @@ -7,7 +7,7 @@ run_unity = cd $(srcdir) && ruby ../../sntp/unity/auto/generate_test_runner.rb #removed test-libntp check_PROGRAMS = test-modetoa test-uglydate test-ymd2yd test-statestr test-numtoa test-numtohost \ test-hextoint test-atoint test-atouint test-authkeys test-a_md5encrypt test-lfpfunc test-vi64ops \ -test-refnumtoa +test-refnumtoa test-calyearstart test-clocktime test-caljulian if GTEST_AVAILABLE check_PROGRAMS += tests @@ -52,10 +52,10 @@ tests_SOURCES = $(top_srcdir)/sntp/tests_main.cpp \ g_authkeys.cpp \ buftvtots.cpp \ calendar.cpp \ - caljulian.cpp \ + g_caljulian.cpp \ caltontp.cpp \ - calyearstart.cpp \ - clocktime.cpp \ + g_calyearstart.cpp \ + g_clocktime.cpp \ decodenetnum.cpp \ g_hextoint.cpp \ hextolfp.cpp \ @@ -205,28 +205,46 @@ test_lfpfunc_LDADD = \ $(unity_tests_LDADD) \ $(NULL) -test_lfpfunc_CFLAGS = \ +test_vi64ops_CFLAGS = \ -I$(top_srcdir)/sntp/unity \ - -DUNITY_INCLUDE_DOUBLE \ $(NULL) -test_lfpfunc_LDADD = \ +test_vi64ops_LDADD = \ $(unity_tests_LDADD) \ $(NULL) -test_vi64ops_CFLAGS = \ +test_refnumtoa_CFLAGS = \ -I$(top_srcdir)/sntp/unity \ $(NULL) -test_vi64ops_LDADD = \ - $(unity_tests_LDADD) \ +test_refnumtoa_LDADD = \ + $(LDADD) \ + $(top_builddir)/sntp/unity/libunity.a \ $(NULL) -test_refnumtoa_CFLAGS = \ +test_calyearstart_CFLAGS = \ -I$(top_srcdir)/sntp/unity \ $(NULL) -test_refnumtoa_LDADD = \ +test_calyearstart_LDADD = \ + $(LDADD) \ + $(top_builddir)/sntp/unity/libunity.a \ + $(NULL) + +test_clocktime_CFLAGS = \ + -I$(top_srcdir)/sntp/unity \ + $(NULL) + +test_clocktime_LDADD = \ + $(LDADD) \ + $(top_builddir)/sntp/unity/libunity.a \ + $(NULL) + +test_caljulian_CFLAGS = \ + -I$(top_srcdir)/sntp/unity \ + $(NULL) + +test_caljulian_LDADD = \ $(LDADD) \ $(top_builddir)/sntp/unity/libunity.a \ $(NULL) @@ -311,6 +329,25 @@ test_refnumtoa_SOURCES = \ run-test-refnumtoa.c \ $(NULL) +test_calyearstart_SOURCES = \ + calyearstart.c \ + run-test-calyearstart.c \ + test-libntp.c \ + $(NULL) + +test_clocktime_SOURCES = \ + clocktime.c \ + run-test-clocktime.c \ + test-libntp.c \ + $(NULL) + +test_caljulian_SOURCES = \ + caljulian.c \ + run-test-caljulian.c \ + test-libntp.c \ + $(NULL) + + $(srcdir)/run-test-modetoa.c: $(srcdir)/modetoa.c $(std_unity_list) $(run_unity) modetoa.c run-test-modetoa.c @@ -353,6 +390,14 @@ $(srcdir)/run-test-vi64ops.c: $(srcdir)/vi64ops.c $(std_unity_list) $(srcdir)/run-test-refnumtoa.c: $(srcdir)/refnumtoa.c $(std_unity_list) $(run_unity) refnumtoa.c run-test-refnumtoa.c +$(srcdir)/run-test-calyearstart.c: $(srcdir)/calyearstart.c $(std_unity_list) + $(run_unity) calyearstart.c run-test-calyearstart.c + +$(srcdir)/run-test-clocktime.c: $(srcdir)/clocktime.c $(std_unity_list) + $(run_unity) clocktime.c run-test-clocktime.c + +$(srcdir)/run-test-caljulian.c: $(srcdir)/caljulian.c $(std_unity_list) + $(run_unity) caljulian.c run-test-caljulian.c TESTS = diff --git a/tests/libntp/caljulian.c b/tests/libntp/caljulian.c new file mode 100644 index 000000000..52e65fcca --- /dev/null +++ b/tests/libntp/caljulian.c @@ -0,0 +1,101 @@ +//#include "libntptest.h" + +#include "config.h" + + +//#include "ntp_stdlib.h" //test fail without this include +#include "ntp_calendar.h" +#include "unity.h" +//#include "ntp_types.h" + +#include "test-libntp.h" + + +#include +//#include + +//added struct to calendar! + +char * CalendarToString(const struct calendar cal) { //&cal + char ss[100]; + + //ss << cal.year << "-" << (u_int)cal.month << "-" << (u_int)cal.monthday + // << " (" << cal.yearday << ") " << (u_int)cal.hour << ":" + // << (u_int)cal.minute << ":" << (u_int)cal.second; + return ss; +} + +//tehnically boolean +int IsEqual(const struct calendar expected, const struct calendar actual) { // &ex &act + if (expected.year == actual.year && + (expected.yearday == actual.yearday || + (expected.month == actual.month && + expected.monthday == actual.monthday)) && + expected.hour == actual.hour && + expected.minute == actual.minute && + expected.second == actual.second) { + return TRUE; + } else { + printf("expected: %s but was %s", CalendarToString(expected) ,CalendarToString(actual)); + return FALSE; + + } +} + + +void setUp() +{ + + ntpcal_set_timefunc(timefunc); + settime(1970, 1, 1, 0, 0, 0); +} + +void tearDown() +{ + ntpcal_set_timefunc(NULL); +} + + +void test_RegularTime() { + u_long testDate = 3485080800UL; // 2010-06-09 14:00:00 + struct calendar expected = {2010,160,6,9,14,0,0}; + + struct calendar actual; + + caljulian(testDate, &actual); + + TEST_ASSERT_TRUE(IsEqual(expected, actual)); +} + +void test_LeapYear() { + u_long input = 3549902400UL; // 2012-06-28 20:00:00Z + struct calendar expected = {2012, 179, 6, 28, 20, 0, 0}; + + struct calendar actual; + + caljulian(input, &actual); + + TEST_ASSERT_TRUE(IsEqual(expected, actual)); +} + +void test_uLongBoundary() { + u_long time = 4294967295UL; // 2036-02-07 6:28:15 + struct calendar expected = {2036,0,2,7,6,28,15}; + + struct calendar actual; + + caljulian(time, &actual); + + TEST_ASSERT_TRUE(IsEqual(expected, actual)); +} + +void test_uLongWrapped() { + u_long time = 0; + struct calendar expected = {2036,0,2,7,6,28,16}; + + struct calendar actual; + + caljulian(time, &actual); + + TEST_ASSERT_TRUE(IsEqual(expected, actual)); +} diff --git a/tests/libntp/calyearstart.c b/tests/libntp/calyearstart.c new file mode 100644 index 000000000..b293c9339 --- /dev/null +++ b/tests/libntp/calyearstart.c @@ -0,0 +1,44 @@ +#include "config.h" + +#include "ntp_stdlib.h" //test fail without this include, for some reason +#include "ntp_calendar.h" +#include "unity.h" + +#include "test-libntp.h" + + +void setUp() +{ + ntpcal_set_timefunc(timefunc); + settime(1970, 1, 1, 0, 0, 0); +} + +void tearDown() +{ + ntpcal_set_timefunc(NULL); +} + + +void test_NoWrapInDateRange() { + const u_int32 input = 3486372600UL; // 2010-06-24 12:50:00. + const u_int32 expected = 3471292800UL; // 2010-01-01 00:00:00 + + TEST_ASSERT_EQUAL(expected, calyearstart(input, &nowtime)); + TEST_ASSERT_EQUAL(expected, calyearstart(input, NULL)); +} + +void test_NoWrapInDateRangeLeapYear() { + const u_int32 input = 3549528000UL; // 2012-06-24 12:00:00 + const u_int32 expected = 3534364800UL; // 2012-01-01 00:00:00 + + TEST_ASSERT_EQUAL(expected, calyearstart(input, &nowtime)); + TEST_ASSERT_EQUAL(expected, calyearstart(input, NULL)); +} + +void test_WrapInDateRange() { + const u_int32 input = 19904UL; // 2036-02-07 12:00:00 + const u_int32 expected = 4291747200UL; // 2036-01-01 00:00:00 + + TEST_ASSERT_EQUAL(expected, calyearstart(input, &nowtime)); + TEST_ASSERT_EQUAL(expected, calyearstart(input, NULL)); +} diff --git a/tests/libntp/clocktime.c b/tests/libntp/clocktime.c new file mode 100644 index 000000000..e6812a17b --- /dev/null +++ b/tests/libntp/clocktime.c @@ -0,0 +1,199 @@ +#include "config.h" + +#include "ntp_calendar.h" +#include "unity.h" + +#include "test-libntp.h" + +// --------------------------------------------------------------------- +// test fixture +// +// The clocktimeTest uses the NTP calendar feature to use a mockup +// function for getting the current system time, so the tests are not +// dependent on the actual system time. + + +void setUp() +{ + ntpcal_set_timefunc(timefunc); + settime(2000, 1, 1, 0, 0, 0); +} + +void tearDown() +{ + ntpcal_set_timefunc(NULL); +} + +// --------------------------------------------------------------------- +// test cases + +void test_CurrentYear() { + // Timestamp: 2010-06-24 12:50:00Z + const u_int32 timestamp = 3486372600UL; + const u_int32 expected = timestamp; // exactly the same. + + const int yday=175, hour=12, minute=50, second=0, tzoff=0; + + u_long yearstart=0; + u_int32 actual; + + TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp, + &yearstart, &actual)); + TEST_ASSERT_EQUAL(expected, actual); +} + +void test_CurrentYearFuzz() { + /* + * Timestamp (rec_ui) is: 2010-06-24 12:50:00 + * Time sent into function is 12:00:00. + * + * Since the fuzz is rather small, we should get a NTP + * timestamp for the 12:00:00 time. + */ + + const u_int32 timestamp = 3486372600UL; // 2010-06-24 12:50:00Z + const u_int32 expected = 3486369600UL; // 2010-06-24 12:00:00Z + + const int yday=175, hour=12, minute=0, second=0, tzoff=0; + + u_long yearstart=0; + u_int32 actual; + + TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp, + &yearstart, &actual)); + TEST_ASSERT_EQUAL(expected, actual); +} + +void test_TimeZoneOffset() { + /* + * Timestamp (rec_ui) is: 2010-06-24 12:00:00 +0800 + * (which is 2010-06-24 04:00:00Z) + * + * Time sent into function is 04:00:00 +0800 + */ + const u_int32 timestamp = 3486369600UL; + const u_int32 expected = timestamp; + + const int yday=175, hour=4, minute=0, second=0, tzoff=8; + + u_long yearstart=0; + u_int32 actual; + + TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp, + &yearstart, &actual)); + TEST_ASSERT_EQUAL(expected, actual); +} + +void test_WrongYearStart() { + /* + * Timestamp (rec_ui) is: 2010-01-02 11:00:00Z + * Time sent into function is 11:00:00. + * Yearstart sent into function is the yearstart of 2009! + */ + const u_int32 timestamp = 3471418800UL; + const u_int32 expected = timestamp; + + const int yday=2, hour=11, minute=0, second=0, tzoff=0; + + u_long yearstart = 302024100UL; // Yearstart of 2009. + u_int32 actual; + + TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp, + &yearstart, &actual)); + TEST_ASSERT_EQUAL(expected, actual); +} + +void test_PreviousYear() { + /* + * Timestamp is: 2010-01-01 01:00:00Z + * Time sent into function is 23:00:00 + * (which is meant to be 2009-12-31 23:00:00Z) + */ + const u_int32 timestamp = 3471296400UL; + const u_int32 expected = 3471289200UL; + + const int yday=365, hour=23, minute=0, second=0, tzoff=0; + + u_long yearstart = 0; + u_int32 actual; + + TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp, + &yearstart, &actual)); + TEST_ASSERT_EQUAL(expected, actual); +} + +void test_NextYear() { + /* + * Timestamp is: 2009-12-31 23:00:00Z + * Time sent into function is 01:00:00 + * (which is meant to be 2010-01-01 01:00:00Z) + */ + const u_int32 timestamp = 3471289200UL; + const u_int32 expected = 3471296400UL; + + const int yday=1, hour=1, minute=0, second=0, tzoff=0; + u_long yearstart = 0; + u_int32 actual; + + TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp, + &yearstart, &actual)); + TEST_ASSERT_EQUAL(expected, actual); +} + +void test_NoReasonableConversion() { + /* Timestamp is: 2010-01-02 11:00:00Z */ + const u_int32 timestamp = 3471418800UL; + + const int yday=100, hour=12, minute=0, second=0, tzoff=0; + u_long yearstart = 0; + u_int32 actual; + + TEST_ASSERT_FALSE(clocktime(yday, hour, minute, second, tzoff, timestamp, + &yearstart, &actual)); +} + +// *** FUNCTION isLE, to simulate gtest's ASSERT_LE using Unity's TEST_ASSERT_TRUE +//tehnically boolean +int isLE(u_int32 diff,u_int32 actual){ + if(diff <= actual){ + return TRUE; + } + else return FALSE; +} + + +void test_AlwaysInLimit() { + /* Timestamp is: 2010-01-02 11:00:00Z */ + const u_int32 timestamp = 3471418800UL; + const u_short prime_incs[] = { 127, 151, 163, 179 }; + int cyc; + int yday; + u_char whichprime; + u_short ydayinc; + int hour; + int minute; + int second; + u_long yearstart; + u_int32 actual; + u_int32 diff; + + yearstart = 0; + for (cyc = 0; cyc < 5; cyc++) { + settime(1900 + cyc * 65, 1, 1, 0, 0, 0); + for (yday = -26000; yday < 26000; yday += ydayinc) { + whichprime = abs(yday) % COUNTOF(prime_incs); + ydayinc = prime_incs[whichprime]; + for (hour = -204; hour < 204; hour += 2) { + for (minute = -60; minute < 60; minute++) { + clocktime(yday, hour, minute, 30, 0, + timestamp, &yearstart, &actual); + diff = actual - timestamp; + if (diff >= 0x80000000UL) + diff = ~diff + 1; + TEST_ASSERT_TRUE(isLE(diff, (183u * SECSPERDAY))); // adding new function to return TRUE if first number is less or equal the second + //TEST_ASSERT_LE(diff, (183u * SECSPERDAY)); + } + } + } + } +} diff --git a/tests/libntp/calyearstart.cpp b/tests/libntp/g_calyearstart.cpp similarity index 100% rename from tests/libntp/calyearstart.cpp rename to tests/libntp/g_calyearstart.cpp diff --git a/tests/libntp/clocktime.cpp b/tests/libntp/g_clocktime.cpp similarity index 100% rename from tests/libntp/clocktime.cpp rename to tests/libntp/g_clocktime.cpp diff --git a/tests/libntp/run-test-caljulian.c b/tests/libntp/run-test-caljulian.c new file mode 100644 index 000000000..853953361 --- /dev/null +++ b/tests/libntp/run-test-caljulian.c @@ -0,0 +1,57 @@ +/* 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_RegularTime(); +extern void test_LeapYear(); +extern void test_uLongBoundary(); +extern void test_uLongWrapped(); + + +//=======Test Reset Option===== +void resetTest() +{ + tearDown(); + setUp(); +} + +char *progname; + + +//=======MAIN===== +int main(int argc, char *argv[]) +{ + progname = argv[0]; + Unity.TestFile = "caljulian.c"; + UnityBegin("caljulian.c"); + RUN_TEST(test_RegularTime, 59); + RUN_TEST(test_LeapYear, 70); + RUN_TEST(test_uLongBoundary, 81); + RUN_TEST(test_uLongWrapped, 92); + + return (UnityEnd()); +} diff --git a/tests/libntp/run-test-calyearstart.c b/tests/libntp/run-test-calyearstart.c new file mode 100644 index 000000000..d54a0b562 --- /dev/null +++ b/tests/libntp/run-test-calyearstart.c @@ -0,0 +1,55 @@ +/* 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_NoWrapInDateRange(); +extern void test_NoWrapInDateRangeLeapYear(); +extern void test_WrapInDateRange(); + + +//=======Test Reset Option===== +void resetTest() +{ + tearDown(); + setUp(); +} + +char *progname; + + +//=======MAIN===== +int main(int argc, char *argv[]) +{ + progname = argv[0]; + Unity.TestFile = "calyearstart.c"; + UnityBegin("calyearstart.c"); + RUN_TEST(test_NoWrapInDateRange, 22); + RUN_TEST(test_NoWrapInDateRangeLeapYear, 30); + RUN_TEST(test_WrapInDateRange, 38); + + return (UnityEnd()); +} diff --git a/tests/libntp/run-test-clocktime.c b/tests/libntp/run-test-clocktime.c new file mode 100644 index 000000000..a6e1413d5 --- /dev/null +++ b/tests/libntp/run-test-clocktime.c @@ -0,0 +1,65 @@ +/* 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_CurrentYear(); +extern void test_CurrentYearFuzz(); +extern void test_TimeZoneOffset(); +extern void test_WrongYearStart(); +extern void test_PreviousYear(); +extern void test_NextYear(); +extern void test_NoReasonableConversion(); +extern void test_AlwaysInLimit(); + + +//=======Test Reset Option===== +void resetTest() +{ + tearDown(); + setUp(); +} + +char *progname; + + +//=======MAIN===== +int main(int argc, char *argv[]) +{ + progname = argv[0]; + Unity.TestFile = "clocktime.c"; + UnityBegin("clocktime.c"); + RUN_TEST(test_CurrentYear, 30); + RUN_TEST(test_CurrentYearFuzz, 45); + RUN_TEST(test_TimeZoneOffset, 67); + RUN_TEST(test_WrongYearStart, 87); + RUN_TEST(test_PreviousYear, 106); + RUN_TEST(test_NextYear, 125); + RUN_TEST(test_NoReasonableConversion, 143); + RUN_TEST(test_AlwaysInLimit, 165); + + return (UnityEnd()); +} diff --git a/tests/libntp/test-libntp.c b/tests/libntp/test-libntp.c index f42e0d106..3e50e8e8f 100644 --- a/tests/libntp/test-libntp.c +++ b/tests/libntp/test-libntp.c @@ -9,31 +9,23 @@ // current_time is needed by authkeys. Used only in to calculate lifetime. //u_long current_time = 4; -/* -void -setUp(void) -{ - printf("setUp from big test"); -} -void -tearDown(void) -{ -} +time_t nowtime = 0; -void -test_modetoa(void) +time_t timefunc(time_t *ptr) { -printf("modetoa:test_KnownMode()\n"); - test_KnownMode(); -printf("modetoa:test_UnknownMode()\n"); - test_UnknownMode(); + if (ptr) + *ptr = nowtime; + return nowtime; } -void -test_uglydate(void) +void settime(int y, int m, int d, int H, int M, int S) { -printf("uglydate:test_ConstantDateTime()\n"); - test_ConstantDateTime(); + + time_t days = ntpcal_edate_to_eradays(y-1, m-1, d-1) + 1 - DAY_UNIX_STARTS; + time_t secs = ntpcal_etime_to_seconds(H, M, S); + + nowtime = days * SECSPERDAY + secs; } -*/ + + diff --git a/tests/libntp/test-libntp.h b/tests/libntp/test-libntp.h index 9d2bca61f..eb113cd52 100644 --- a/tests/libntp/test-libntp.h +++ b/tests/libntp/test-libntp.h @@ -1,6 +1,3 @@ -// From modetoa.c: -extern void test_KnownMode(void); -extern void test_UnknownMode(void); - -// From uglydate.c: -extern void test_ConstantDateTime(void); +time_t timefunc(time_t *ptr); +void settime(int y, int m, int d, int H, int M, int S); +time_t nowtime;