From: Lokesh Walase Date: Mon, 3 Aug 2015 07:00:29 +0000 (+0530) Subject: ntp_util.c: X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f7ea7c38330b6d5244c06016ca8c2b1e0cbb3ba5;p=thirdparty%2Fntp.git ntp_util.c: new file bk: 55bf118dX6Ds4PSSwqTyMO0dDLcDjw --- diff --git a/tests/libntp/decodenetnum.c b/tests/libntp/decodenetnum.c index 77491b8b3..39f0320bf 100644 --- a/tests/libntp/decodenetnum.c +++ b/tests/libntp/decodenetnum.c @@ -6,8 +6,10 @@ extern void test_IPv4AddressOnly(void); extern void test_IPv4AddressWithPort(void); +#ifdef ISC_PLATFORM_HAVEIPV6 extern void test_IPv6AddressOnly(void); extern void test_IPv6AddressWithPort(void); +#endif /* ISC_PLATFORM_HAVEIPV6 */ extern void test_IllegalAddress(void); extern void test_IllegalCharInPort(void); @@ -40,6 +42,7 @@ test_IPv4AddressWithPort(void) { TEST_ASSERT_TRUE(IsEqual(expected, actual)); } +#ifdef ISC_PLATFORM_HAVEIPV6 void test_IPv6AddressOnly(void) { const struct in6_addr address = { @@ -60,7 +63,9 @@ test_IPv6AddressOnly(void) { TEST_ASSERT_TRUE(decodenetnum(str, &actual)); TEST_ASSERT_TRUE(IsEqual(expected, actual)); } +#endif /* ISC_PLATFORM_HAVEIPV6 */ +#ifdef ISC_PLATFORM_HAVEIPV6 void test_IPv6AddressWithPort(void) { const struct in6_addr address = { @@ -82,6 +87,7 @@ test_IPv6AddressWithPort(void) { TEST_ASSERT_TRUE(IsEqual(expected, actual)); } +#endif /* ISC_PLATFORM_HAVEIPV6 */ void test_IllegalAddress(void) { const char *str = "192.0.2.270:2000"; diff --git a/tests/libntp/socktoa.c b/tests/libntp/socktoa.c index 9ce3a1e99..6d65fea94 100644 --- a/tests/libntp/socktoa.c +++ b/tests/libntp/socktoa.c @@ -6,12 +6,15 @@ #include "unity.h" #include "sockaddrtest.h" + void test_IPv4AddressWithPort(void); +#ifdef ISC_PLATFORM_HAVEIPV6 void test_IPv6AddressWithPort(void); +void test_IgnoreIPv6Fields(void); +#endif /* ISC_PLATFORM_HAVEIPV6 */ void test_ScopedIPv6AddressWithPort(void); void test_HashEqual(void); void test_HashNotEqual(void); -void test_IgnoreIPv6Fields(void); void test_IPv4AddressWithPort(void) { @@ -21,6 +24,7 @@ test_IPv4AddressWithPort(void) { TEST_ASSERT_EQUAL_STRING("192.0.2.10:123", sockporttoa(&input)); } +#ifdef ISC_PLATFORM_HAVEIPV6 void test_IPv6AddressWithPort(void) { const struct in6_addr address = { @@ -44,6 +48,7 @@ test_IPv6AddressWithPort(void) { TEST_ASSERT_EQUAL_STRING(expected, socktoa(&input)); TEST_ASSERT_EQUAL_STRING(expected_port, sockporttoa(&input)); } +#endif /* ISC_PLATFORM_HAVEIPV6 */ void test_ScopedIPv6AddressWithPort(void) { @@ -93,6 +98,7 @@ test_HashNotEqual(void) { TEST_ASSERT_FALSE(sock_hash(&input1) == sock_hash(&input2)); } +#ifdef ISC_PLATFORM_HAVEIPV6 void test_IgnoreIPv6Fields(void) { const struct in6_addr address = { @@ -116,3 +122,4 @@ test_IgnoreIPv6Fields(void) { TEST_ASSERT_EQUAL(sock_hash(&input1), sock_hash(&input2)); } +#endif /* ISC_PLATFORM_HAVEIPV6 */ diff --git a/tests/ntpd/Makefile.am b/tests/ntpd/Makefile.am index c9430efcd..c984f9a77 100644 --- a/tests/ntpd/Makefile.am +++ b/tests/ntpd/Makefile.am @@ -12,6 +12,7 @@ check_PROGRAMS = \ test-leapsec \ test-ntp_prio_q \ test-ntp_restrict \ + test-ntp_util \ test-rc_cmdlength \ test-ntp_scanner \ $(NULL) @@ -59,6 +60,7 @@ tests_SOURCES = $(top_srcdir)/sntp/tests_main.cpp \ $(top_srcdir)/ntpd/ntp_leapsec.c \ $(top_srcdir)/ntpd/ntp_prio_q.c \ $(top_srcdir)/ntpd/ntp_restrict.c \ + $(top_srcdir)/ntpd/ntp_util.c \ $(top_srcdir)/ntpd/rc_cmdlength.c \ $(top_srcdir)/ntpd/ntp_signd.c \ g_leapsec.cpp \ @@ -68,12 +70,14 @@ BUILT_SOURCES += \ $(srcdir)/run-leapsec.c \ $(srcdir)/run-ntp_prio_q.c \ $(srcdir)/run-ntp_restrict.c \ + $(srcdir)/run-ntp_util.c \ $(srcdir)/run-rc_cmdlength.c \ $(srcdir)/run-ntp_signd.c \ $(NULL) noinst_HEADERS = g_ntpdtest.h \ $(srcdir)/../libntp/test-libntp.h \ + sockaddrtest.h \ $(NULL) ### @@ -135,6 +139,25 @@ $(srcdir)/run-ntp_restrict.c: $(srcdir)/ntp_restrict.c $(std_unity_list) $(run_unity) ntp_restrict.c run-ntp_restrict.c +### +test_ntp_util_CFLAGS = \ + -I$(top_srcdir)/sntp/unity \ + $(NULL) + +test_ntp_util_LDADD = \ + $(unity_tests_LDADD) \ + $(NULL) + +test_ntp_util_SOURCES = \ + ntp_util.c \ + run-ntp_util.c \ + $(srcdir)/../libntp/test-libntp.c \ + $(NULL) + +$(srcdir)/run-ntp_util.c: $(srcdir)/ntp_util.c $(std_unity_list) + $(run_unity) ntp_util.c run-ntp_util.c + + ### test_rc_cmdlength_CFLAGS = \ -I$(top_srcdir)/sntp/unity \ diff --git a/tests/ntpd/ntp_util.c b/tests/ntpd/ntp_util.c new file mode 100644 index 000000000..8b90a72d3 --- /dev/null +++ b/tests/ntpd/ntp_util.c @@ -0,0 +1,74 @@ +#include "config.h" + +#include "ntp.h" +#include "ntp_calendar.h" +#include "ntp_stdlib.h" + +#include "unity.h" +#include "test-libntp.h" + +#include +#include + +void test_mprintf_clock_stats(void){ + char *one = "this"; + char *two = "is"; + char *three = "becoming a string"; + + int ret_value1 = mprintf_clock_stats(NULL, "%s", one) ; + int ret_value2 = mprintf_clock_stats(NULL, "%s %s %s", one, two, three) ; + + TEST_ASSERT_EQUAL( strlen(one), ret_value1); + TEST_ASSERT_EQUAL(25, ret_value2); + +} + + +void test_fstostr(void){ + u_int32 ntp; + char *buf ; + + ntp = 0; + buf = fstostr(ntp); + TEST_ASSERT_EQUAL_STRING("203602070628", buf); + //The output format of function fstostr() is : YYYY-MM-DD-HH-MI + //[ Year-Month-MonthDay-Hour-Minute ] + + ntp += 60; //Increment by 60 secs ie 1 minute + buf = fstostr(ntp); + TEST_ASSERT_EQUAL_STRING("203602070629", buf); + + ntp += 60; //Increment by 60 secs ie 1 minute + buf = fstostr(ntp); + TEST_ASSERT_EQUAL_STRING("203602070630", buf); + + ntp += 3600; //Increment by 3600 secs ie 1 hour + buf = fstostr(ntp); + TEST_ASSERT_EQUAL_STRING("203602070730", buf); + + ntp += 3600; //Increment by 3600 secs ie 1 hour + buf = fstostr(ntp); + TEST_ASSERT_EQUAL_STRING("203602070830", buf); + + ntp += (24*60*60); //Increment by 1 day + buf = fstostr(ntp); + TEST_ASSERT_EQUAL_STRING("203602080830", buf); + + ntp += (24*60*60); //Increment by 1 day + buf = fstostr(ntp); + TEST_ASSERT_EQUAL_STRING("203602090830", buf); + + ntp += (10*24*60*60); //Increment by 10 days + buf = fstostr(ntp); + TEST_ASSERT_EQUAL_STRING("203602190830", buf); + + ntp += (12*24*60*60); //Increment by 12 days + buf = fstostr(ntp); + TEST_ASSERT_EQUAL_STRING("203603020830", buf); + + ntp += (31*24*60*60); //Increment by 1 month + buf = fstostr(ntp); + TEST_ASSERT_EQUAL_STRING("203604020830", buf); + +} +