[Git][NTPsec/ntpsec][master] 15 commits: Add packetProcessing.c (not converted)

Amar Takhar gitlab at mg.gitlab.com
Fri Dec 4 18:41:47 UTC 2015


Amar Takhar pushed to branch master at NTPsec / ntpsec


Commits:
01600c3a by Amar Takhar at 2015-12-04T13:40:40Z
Add packetProcessing.c (not converted)

  This requires some changes to ntpdig/main.c to allow for injecting options
  outside of main().

- - - - -
016739ee by Amar Takhar at 2015-12-04T13:40:40Z
Remove unused networking.(c|cpp)

- - - - -
fed253a2 by Amar Takhar at 2015-12-04T13:40:40Z
Cleanup headers.

  * Remove libntptest.h
  * Move sockaddrtest.h to common/

- - - - -
78eff93d by Amar Takhar at 2015-12-04T13:40:40Z
Remove libntptest.h reference in sockaddrtest.h

- - - - -
4a3c1ff8 by Amar Takhar at 2015-12-04T13:40:40Z
Convert ntpdig/utilities.c.

  * Also fix typo in CompareFileContent()

- - - - -
09aff7f3 by Amar Takhar at 2015-12-04T13:40:40Z
Convert libntp/clocktime.c.

- - - - -
6e719472 by Amar Takhar at 2015-12-04T13:40:40Z
Convert libntp/clocktime.c.

- - - - -
24046bbb by Amar Takhar at 2015-12-04T13:40:40Z
Move IsEqualS to it's own C file.

- - - - -
6fe6aea4 by Amar Takhar at 2015-12-04T13:40:40Z
Convert libntp/netof.c.

- - - - -
02bbab3b by Amar Takhar at 2015-12-04T13:40:40Z
Convert libntp/refnumtoa.c.

- - - - -
965fde1f by Amar Takhar at 2015-12-04T13:40:40Z
Enable decodenetnum.c now that sockaddrtest.h has been fixed.

- - - - -
6165a292 by Amar Takhar at 2015-12-04T13:40:40Z
Convert libntp/caltontp.c.

- - - - -
06a07c56 by Amar Takhar at 2015-12-04T13:40:40Z
Convert libntp/caljulian.c.

- - - - -
769b0572 by Amar Takhar at 2015-12-04T13:40:40Z
Move some functions from caljulian.c and calendar.c to caltime.c.

- - - - -
c0ef5549 by Amar Takhar at 2015-12-04T13:40:40Z
Convert libntp/caljulian.c.

- - - - -


44 changed files:

- tests/common/caltime.c
- tests/common/caltime.h
- tests/common/file_handling.c
- tests/libntp/sockaddrtest.h → tests/common/sockaddrtest.c
- + tests/common/sockaddrtest.h
- tests/common/tests_main.c
- tests/libntp/a_md5encrypt.c
- tests/libntp/authkeys.c
- tests/libntp/calendar.c
- tests/libntp/caljulian.c
- tests/libntp/caltontp.c
- tests/libntp/calyearstart.c
- tests/libntp/clocktime.c
- tests/libntp/decodenetnum.c
- tests/libntp/hextolfp.c
- tests/libntp/humandate.c
- tests/libntp/lfpfunc.c
- tests/libntp/lfptest.h
- tests/libntp/lfptostr.c
- − tests/libntp/libntptest.h
- tests/libntp/modetoa.c
- tests/libntp/msyslog.c
- tests/libntp/netof.c
- tests/libntp/numtoa.c
- tests/libntp/numtohost.c
- tests/libntp/prettydate.c
- tests/libntp/recvbuff.c
- tests/libntp/refnumtoa.c
- tests/libntp/sfptostr.c
- tests/libntp/socktoa.c
- tests/libntp/ssl_init.c
- tests/libntp/statestr.c
- tests/libntp/strtolfp.c
- tests/libntp/timespecops.c
- tests/libntp/timevalops.c
- tests/libntp/tstotv.c
- tests/libntp/tvtots.c
- tests/libntp/vi64ops.c
- tests/libntp/ymd2yd.c
- − tests/ntpdig/networking.c
- − tests/ntpdig/networking.cpp
- + tests/ntpdig/packetProcessing.c
- + tests/ntpdig/utilities.c
- tests/wscript


Changes:

=====================================
tests/common/caltime.c
=====================================
--- a/tests/common/caltime.c
+++ b/tests/common/caltime.c
@@ -23,3 +23,18 @@ void settime(int y, int m, int d, int H, int M, int S) {
 	nowtime = days * SECSPERDAY + secs;
 }
 
+
+const char *CalendarToString(const struct calendar *cal) {
+	char *str = malloc(255);
+	snprintf(str, 255, "%hu-%u-%u (%u) %u:%u:%u\n", 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 str;
+}
+
+
+const char *CalendarToStringISO(const struct isodate *iso) {
+	char *str = malloc(255);
+	snprintf(str, 255, "%hu-%u-%u %u:%u:%u\n", iso->year, (u_int)iso->week, (u_int)iso->weekday, (u_int)iso->hour, (u_int)iso->minute, (u_int)iso->second);
+	return str;
+}
+


=====================================
tests/common/caltime.h
=====================================
--- a/tests/common/caltime.h
+++ b/tests/common/caltime.h
@@ -1,5 +1,8 @@
 #include <time.h>
 
+#include "ntp_calendar.h"
+
 time_t timefunc(time_t*);
 void   settime(int y, int m, int d, int H, int M, int S);
-
+const char *CalendarToString(const struct calendar *cal);
+const char *CalendarToStringISO(const struct isodate *iso);


=====================================
tests/common/file_handling.c
=====================================
--- a/tests/common/file_handling.c
+++ b/tests/common/file_handling.c
@@ -52,7 +52,7 @@ void CompareFileContent(FILE* expected, FILE* actual) {
 		TEST_ASSERT_FALSE(ferror(expected));
 		TEST_ASSERT_FALSE(ferror(actual));
 		if (fgets(e_line, sizeof(e_line), expected) != NULL &&
-		    fgets(e_line, sizeof(e_line), expected) != NULL) {
+		    fgets(a_line, sizeof(a_line), actual) != NULL) {
 			TEST_ASSERT_EQUAL_STRING(e_line, a_line);
 		}
 	}


=====================================
tests/libntp/sockaddrtest.h → tests/common/sockaddrtest.c
=====================================
--- a/tests/libntp/sockaddrtest.h
+++ b/tests/common/sockaddrtest.c
@@ -1,7 +1,7 @@
 #ifndef GUARD_TESTS_SOCKADDRTEST_H
 #define GUARD_TESTS_SOCKADDRTEST_H
 
-#include "libntptest.h"
+#include "config.h"
 
 #include "ntp.h"
 


=====================================
tests/common/sockaddrtest.h
=====================================
--- /dev/null
+++ b/tests/common/sockaddrtest.h
@@ -0,0 +1,10 @@
+#ifndef GUARD_TESTS_SOCKADDRTEST_H
+#define GUARD_TESTS_SOCKADDRTEST_H
+
+#include "ntp.h"
+
+bool IsEqualS(const sockaddr_u *expected, const sockaddr_u *actual);
+sockaddr_u CreateSockaddr4(const char* address, unsigned int port);
+
+
+#endif // GUARD_TESTS_SOCKADDRTEST_H


=====================================
tests/common/tests_main.c
=====================================
--- a/tests/common/tests_main.c
+++ b/tests/common/tests_main.c
@@ -34,31 +34,35 @@ static void RunAllTests(void)
 	RUN_TEST_GROUP(kodDatabase);
 	RUN_TEST_GROUP(kodFile);
 	RUN_TEST_GROUP(packetHandling);
+//	RUN_TEST_GROUP(packetProcessing);
+	RUN_TEST_GROUP(utilities);
 #endif
 
 #ifdef TEST_LIBNTP
 	RUN_TEST_GROUP(a_md5encrypt);
 	RUN_TEST_GROUP(authkeys);
-//	RUN_TEST_GROUP(calendar);
-//	RUN_TEST_GROUP(caljulian);
-//	RUN_TEST_GROUP(caltontp);
+	RUN_TEST_GROUP(calendar);
+	RUN_TEST_GROUP(caljulian);
+	RUN_TEST_GROUP(caltontp);
 	RUN_TEST_GROUP(calyearstart);
-//	RUN_TEST_GROUP(clocktime);
-//	RUN_TEST_GROUP(decodenetnum);
+	RUN_TEST_GROUP(clocktime);
+	RUN_TEST_GROUP(decodenetnum);
 	RUN_TEST_GROUP(hextolfp);
 //	RUN_TEST_GROUP(humandate);
 //	RUN_TEST_GROUP(lfp);
 //	RUN_TEST_GROUP(lfptostr);
 	RUN_TEST_GROUP(modetoa);
 //	RUN_TEST_GROUP(msyslog);
-//	RUN_TEST_GROUP(netof);
+	RUN_TEST_GROUP(netof);
 	RUN_TEST_GROUP(numtoa);
 	RUN_TEST_GROUP(numtohost);
 	RUN_TEST_GROUP(prettydate);
 	RUN_TEST_GROUP(recvbuff);
-//	RUN_TEST_GROUP(refnumtoa);
+#ifdef REFCLOCK
+	RUN_TEST_GROUP(refnumtoa);
+#endif
 	RUN_TEST_GROUP(sfptostr);
-//	RUN_TEST_GROUP(socktoa);
+	RUN_TEST_GROUP(socktoa);
 	RUN_TEST_GROUP(ssl_init);
 	RUN_TEST_GROUP(statestr);
 	RUN_TEST_GROUP(strtolfp);


=====================================
tests/libntp/a_md5encrypt.c
=====================================
--- a/tests/libntp/a_md5encrypt.c
+++ b/tests/libntp/a_md5encrypt.c
@@ -1,3 +1,6 @@
+#include "config.h"
+#include "ntp_stdlib.h"
+
 #include "unity.h"
 #include "unity_fixture.h"
 
@@ -7,7 +10,6 @@ TEST_SETUP(a_md5encrypt) {}
 
 TEST_TEAR_DOWN(a_md5encrypt) {}
 
-#include "libntptest.h"
 
 #ifdef HAVE_OPENSSL
 # include "openssl/err.h"


=====================================
tests/libntp/authkeys.c
=====================================
--- a/tests/libntp/authkeys.c
+++ b/tests/libntp/authkeys.c
@@ -1,7 +1,9 @@
+#include "config.h"
+#include "ntp_stdlib.h"
+
 #include "unity.h"
 #include "unity_fixture.h"
 
-#include "libntptest.h"
 
 #ifdef HAVE_OPENSSL
 # include "openssl/err.h"


=====================================
tests/libntp/calendar.c
=====================================
--- a/tests/libntp/calendar.c
+++ b/tests/libntp/calendar.c
@@ -1,7 +1,10 @@
-extern "C" {
+#include "config.h"
+#include "ntp_stdlib.h"
+
 #include "unity.h"
 #include "unity_fixture.h"
-}
+
+#include "caltime.h"
 
 TEST_GROUP(calendar);
 
@@ -9,37 +12,12 @@ TEST_SETUP(calendar) {}
 
 TEST_TEAR_DOWN(calendar) {}
 
-#include "libntptest.h"
 
-extern "C" {
 #include "ntp_calendar.h"
-}
-
-#include <string>
-#include <sstream>
 
-class calendarTest : public libntptest {
-protected:
-	static int leapdays(int year);
+#define TEST_ASSERT_GREATER_THAN(a, b) TEST_ASSERT_TRUE(a > b)
 
-	std::string CalendarToString(const calendar &cal);
-	std::string CalendarToString(const isodate &iso);
-	bool IsEqual(const calendar &expected, const calendar &actual);
-	bool IsEqual(const isodate &expected, const isodate &actual);
-
-	std::string DateToString(const calendar &cal);
-	std::string DateToString(const isodate &iso);
-	bool IsEqualDate(const calendar &expected, const calendar &actual);
-	bool IsEqualDate(const isodate &expected, const isodate &actual);
-};
-
-
-// ---------------------------------------------------------------------
-// test support stuff
-// ---------------------------------------------------------------------
-int
-calendarTest::leapdays(int year)
-{
+int leapdays(int year) {
 	if (year % 400 == 0)
 		return 1;
 	if (year % 100 == 0)
@@ -49,95 +27,73 @@ calendarTest::leapdays(int year)
 	return 0;
 }
 
-std::string 
-calendarTest::CalendarToString(const calendar &cal) {
-	std::ostringstream ss;
-	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.str();
-}
 
-std::string
-calendarTest:: CalendarToString(const isodate &iso) {
-	std::ostringstream ss;
-	ss << iso.year << "-" << (u_int)iso.week << "-" << (u_int)iso.weekday
-	   << (u_int)iso.hour << ":" << (u_int)iso.minute << ":" << (u_int)iso.second;
-	return ss.str();
-}
-
-bool
-calendarTest:: IsEqual(const calendar &expected, const calendar &actual) {
-	if (expected.year == actual.year &&
-	    (!expected.yearday || expected.yearday == actual.yearday) &&
-	    expected.month == actual.month &&
-	    expected.monthday == actual.monthday &&
-	    expected.hour == actual.hour &&
-	    expected.minute == actual.minute &&
-	    expected.second == actual.second) {
+bool IsEqualCA(const struct calendar *expected, const struct calendar *actual) {
+	if (expected->year == actual->year &&
+	    (!expected->yearday || 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 {
-		return false
-		    << "expected: " << CalendarToString(expected) << " but was "
-		    << CalendarToString(actual);
+		printf("Expected: %s but was %s\n", CalendarToString(expected), CalendarToString(actual));
+		return false;
 	}
 }
 
-bool
-calendarTest:: IsEqual(const isodate &expected, const isodate &actual) {
-	if (expected.year == actual.year &&
-	    expected.week == actual.week &&
-	    expected.weekday == actual.weekday &&
-	    expected.hour == actual.hour &&
-	    expected.minute == actual.minute &&
-	    expected.second == actual.second) {
+
+bool IsEqualISO(const struct isodate *expected, const struct isodate *actual) {
+	if (expected->year == actual->year &&
+	    expected->week == actual->week &&
+	    expected->weekday == actual->weekday &&
+	    expected->hour == actual->hour &&
+	    expected->minute == actual->minute &&
+	    expected->second == actual->second) {
 		return true;
 	} else {
-		return false
-		    << "expected: " << CalendarToString(expected) << " but was "
-		    << CalendarToString(actual);
+		printf("Expected: %s but was %s\n", CalendarToStringISO(expected), CalendarToStringISO(actual));
+		return false;
 	}
 }
 
-std::string
-calendarTest:: DateToString(const calendar &cal) {
-	std::ostringstream ss;
-	ss << cal.year << "-" << (u_int)cal.month << "-" << (u_int)cal.monthday
-	   << " (" << cal.yearday << ")";
-	return ss.str();
+
+const char *DateToString(const struct calendar *cal) {
+	char *str = malloc(255);
+	snprintf(str, 255, "%hu-%u-%u(%u)\n", cal->year, (u_int)cal->month, (u_int)cal->monthday, cal->yearday);
+	return str;
 }
 
-std::string
-calendarTest:: DateToString(const isodate &iso) {
-	std::ostringstream ss;
-	ss << iso.year << "-" << (u_int)iso.week << "-" << (u_int)iso.weekday;
-	return ss.str();
+
+const char *DateToStringISO(const struct isodate *iso) {
+	char *str = malloc(255);
+	snprintf(str, 255, "%hu-%u-%u\n", iso->year, (u_int)iso->week, (u_int)iso->weekday);
+	return str;
+
 }
 
-bool
-calendarTest:: IsEqualDate(const calendar &expected, const calendar &actual) {
-	if (expected.year == actual.year &&
-	    (!expected.yearday || expected.yearday == actual.yearday) &&
-	    expected.month == actual.month &&
-	    expected.monthday == actual.monthday) {
-		return true;
+bool IsEqualDate(const struct calendar *expected, const struct calendar *actual) {
+	if (expected->year == actual->year &&
+	    (!expected->yearday || expected->yearday == actual->yearday) &&
+	    expected->month == actual->month &&
+	    expected->monthday == actual->monthday) {
+			return true;
 	} else {
-		return false
-		    << "expected: " << DateToString(expected) << " but was "
-		    << DateToString(actual);
+		printf("Expected: %s but was %s\n", DateToString(expected), DateToString(actual));
+		return false;
 	}
 }
 
-bool
-calendarTest:: IsEqualDate(const isodate &expected, const isodate &actual) {
-	if (expected.year == actual.year &&
-	    expected.week == actual.week &&
-	    expected.weekday == actual.weekday) {
+
+bool IsEqualDateISO(const struct isodate *expected, const struct isodate *actual) {
+	if (expected->year == actual->year &&
+	    expected->week == actual->week &&
+	    expected->weekday == actual->weekday) {
 		return true;
 	} else {
-		return false
-		    << "expected: " << DateToString(expected) << " but was "
-		    << DateToString(actual);
+		printf("Expected: %s but was %s\n", DateToStringISO(expected), DateToStringISO(actual));
+		return false;
 	}
 }
 
@@ -164,12 +120,12 @@ static const u_short real_month_days[2][14] = {
 // intermediate results would definitely overflow and the hi DWORD of
 // the 'vint64' is definitely needed.
 TEST(calendar, DaySplitMerge) {
-	for (int32 day = -1000000; day <= 1000000; day += 100) {
-		for (int32 sec = -100000; sec <= 186400; sec += 10000) {
+	for (int32_t day = -1000000; day <= 1000000; day += 100) {
+		for (int32_t sec = -100000; sec <= 186400; sec += 10000) {
 			vint64	     merge = ntpcal_dayjoin(day, sec);
 			ntpcal_split split = ntpcal_daysplit(&merge);
-			int32	     eday  = day;
-			int32	     esec  = sec;
+			int32_t	     eday  = day;
+			int32_t	     esec  = sec;
 
 			while (esec >= 86400) {
 				eday += 1;
@@ -187,12 +143,12 @@ TEST(calendar, DaySplitMerge) {
 }
 
 TEST(calendar, SplitYearDays1) {
-	for (int32 eyd = -1; eyd <= 365; eyd++) {
+	for (int32_t eyd = -1; eyd <= 365; eyd++) {
 		ntpcal_split split = ntpcal_split_yeardays(eyd, 0);
 		if (split.lo >= 0 && split.hi >= 0) {
 			TEST_ASSERT_GREATER_THAN(12, split.hi);
 			TEST_ASSERT_GREATER_THAN(real_month_days[0][split.hi+1], split.lo);
-			int32 tyd = real_month_table[0][split.hi] + split.lo;
+			int32_t tyd = real_month_table[0][split.hi] + split.lo;
 			TEST_ASSERT_EQUAL(eyd, tyd);
 		} else
 			TEST_ASSERT_TRUE(eyd < 0 || eyd > 364);
@@ -200,12 +156,12 @@ TEST(calendar, SplitYearDays1) {
 }
 
 TEST(calendar, SplitYearDays2) {
-	for (int32 eyd = -1; eyd <= 366; eyd++) {
+	for (int32_t eyd = -1; eyd <= 366; eyd++) {
 		ntpcal_split split = ntpcal_split_yeardays(eyd, 1);
 		if (split.lo >= 0 && split.hi >= 0) {
 			TEST_ASSERT_GREATER_THAN(12, split.hi);
 			TEST_ASSERT_GREATER_THAN(real_month_days[1][split.hi+1], split.lo);
-			int32 tyd = real_month_table[1][split.hi] + split.lo;
+			int32_t tyd = real_month_table[1][split.hi] + split.lo;
 			TEST_ASSERT_EQUAL(eyd, tyd);
 		} else
 			TEST_ASSERT_TRUE(eyd < 0 || eyd > 365);
@@ -213,17 +169,17 @@ TEST(calendar, SplitYearDays2) {
 }
 
 TEST(calendar, RataDie1) {
-	int32	 testDate = 1; // 0001-01-01 (proleptic date)
-	calendar expected = { 1, 1, 1, 1 };
-	calendar actual;
+	int32_t	 testDate = 1; // 0001-01-01 (proleptic date)
+	struct calendar expected = { 1, 1, 1, 1, 0, 0, 0, 0};
+	struct calendar actual;
 
 	ntpcal_rd_to_date(&actual, testDate);
-	TEST_ASSERT_TRUE(IsEqualDate(expected, actual));
+	TEST_ASSERT_TRUE(IsEqualDate(&expected, &actual));
 }
 
 // check last day of february for first 10000 years
 TEST(calendar, LeapYears1) {
-	calendar dateIn, dateOut;
+	struct calendar dateIn, dateOut;
 
 	for (dateIn.year = 1; dateIn.year < 10000; ++dateIn.year) {
 		dateIn.month	= 2;
@@ -232,13 +188,13 @@ TEST(calendar, LeapYears1) {
 
 		ntpcal_rd_to_date(&dateOut, ntpcal_date_to_rd(&dateIn));
 
-		TEST_ASSERT_TRUE(IsEqualDate(dateIn, dateOut));
+		TEST_ASSERT_TRUE(IsEqualDate(&dateIn, &dateOut));
 	}
 }
 
 // check first day of march for first 10000 years
 TEST(calendar, LeapYears2) {
-	calendar dateIn, dateOut;
+	struct calendar dateIn, dateOut;
 
 	for (dateIn.year = 1; dateIn.year < 10000; ++dateIn.year) {
 		dateIn.month	= 3;
@@ -246,7 +202,7 @@ TEST(calendar, LeapYears2) {
 		dateIn.yearday	= 60 + leapdays(dateIn.year);
 
 		ntpcal_rd_to_date(&dateOut, ntpcal_date_to_rd(&dateIn));
-		TEST_ASSERT_TRUE(IsEqualDate(dateIn, dateOut));
+		TEST_ASSERT_TRUE(IsEqualDate(&dateIn, &dateOut));
 	}
 }
 
@@ -256,8 +212,8 @@ TEST(calendar, LeapYears2) {
 // and the result of the inverse calculation must match the input no
 // invalid output can occur.)
 TEST(calendar, RoundTripDate) {
-	calendar truDate, expDate = { 1600, 0, 12, 31 };;
-	int32	 truRdn, expRdn	= ntpcal_date_to_rd(&expDate);
+	struct calendar truDate, expDate = { 1600, 0, 12, 31, 0, 0, 0, 0};
+	int32_t	 truRdn, expRdn	= ntpcal_date_to_rd(&expDate);
 	int	 leaps;
 
 	while (expDate.year < 2400) {
@@ -277,7 +233,7 @@ TEST(calendar, RoundTripDate) {
 				TEST_ASSERT_EQUAL(expRdn, truRdn);
 
 				ntpcal_rd_to_date(&truDate, truRdn);
-				TEST_ASSERT_TRUE(IsEqualDate(expDate, truDate));
+				TEST_ASSERT_TRUE(IsEqualDate(&expDate, &truDate));
 			}
 		}
 	}
@@ -286,8 +242,8 @@ TEST(calendar, RoundTripDate) {
 // Roundtrip testing on calyearstart
 TEST(calendar, RoundTripYearStart) {
 	static const time_t pivot = 0;
-	u_int32 ntp, expys, truys;
-	calendar date;
+	u_int32_t ntp, expys, truys;
+	struct calendar date;
 
 	for (ntp = 0; ntp < 0xFFFFFFFFu - 30000000u; ntp += 30000000u) {
 		truys = calyearstart(ntp, &pivot);
@@ -302,8 +258,8 @@ TEST(calendar, RoundTripYearStart) {
 // Roundtrip testing on calymonthstart
 TEST(calendar, RoundTripMonthStart) {
 	static const time_t pivot = 0;
-	u_int32 ntp, expms, trums;
-	calendar date;
+	u_int32_t ntp, expms, trums;
+	struct calendar date;
 
 	for (ntp = 0; ntp < 0xFFFFFFFFu - 2000000u; ntp += 2000000u) {
 		trums = calmonthstart(ntp, &pivot);
@@ -318,8 +274,8 @@ TEST(calendar, RoundTripMonthStart) {
 // Roundtrip testing on calweekstart
 TEST(calendar, RoundTripWeekStart) {
 	static const time_t pivot = 0;
-	u_int32 ntp, expws, truws;
-	isodate date;
+	u_int32_t ntp, expws, truws;
+	struct isodate date;
 
 	for (ntp = 0; ntp < 0xFFFFFFFFu - 600000u; ntp += 600000u) {
 		truws = calweekstart(ntp, &pivot);
@@ -334,8 +290,8 @@ TEST(calendar, RoundTripWeekStart) {
 // Roundtrip testing on caldaystart
 TEST(calendar, RoundTripDayStart) {
 	static const time_t pivot = 0;
-	u_int32 ntp, expds, truds;
-	calendar date;
+	u_int32_t ntp, expds, truds;
+	struct calendar date;
 
 	for (ntp = 0; ntp < 0xFFFFFFFFu - 80000u; ntp += 80000u) {
 		truds = caldaystart(ntp, &pivot);


=====================================
tests/libntp/caljulian.c
=====================================
--- a/tests/libntp/caljulian.c
+++ b/tests/libntp/caljulian.c
@@ -1,108 +1,84 @@
-extern "C" {
+#include "config.h"
+#include "ntp_stdlib.h"
+
 #include "unity.h"
 #include "unity_fixture.h"
-}
-
-TEST_GROUP(caljulian);
 
-TEST_SETUP(caljulian) {}
+#include "ntp_calendar.h"
 
-TEST_TEAR_DOWN(caljulian) {}
+#include <string.h>
+#include "caltime.h"
 
-#include "libntptest.h"
+TEST_GROUP(caljulian);
 
-extern "C" {
-#include "ntp_calendar.h"
+TEST_SETUP(caljulian) {
+	ntpcal_set_timefunc(timefunc);
+	settime(1970, 1, 1, 0, 0, 0);
 }
 
-#include "cal.h"
-#include <string>
-#include <sstream>
-
-class caljulianTest : public libntptest {
-protected:
-	virtual void SetUp();
-	virtual void TearDown();
-
-	std::string CalendarToString(const calendar &cal) {
-		std::ostringstream ss;
-		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.str();
-	}
-
-	bool IsEqual(const calendar &expected, const calendar &actual) {
-		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 {
-			return false
-				<< "expected: " << CalendarToString(expected) << " but was "
-				<< CalendarToString(actual);
-		}
-	}
-};
-
-void caljulianTest::SetUp()
-{
-    ntpcal_set_timefunc(timefunc);
-    settime(1970, 1, 1, 0, 0, 0);
+TEST_TEAR_DOWN(caljulian) {
+	ntpcal_set_timefunc(NULL);
 }
 
-void caljulianTest::TearDown()
-{
-    ntpcal_set_timefunc(NULL);
+bool IsEqualC(const struct calendar *expected, const struct calendar *actual) {
+	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;
+	}
 }
 
 
 TEST(caljulian, RegularTime) {
 	u_long testDate = 3485080800UL; // 2010-06-09 14:00:00
-	calendar expected = {2010,160,6,9,14,0,0};
+	struct calendar expected = {2010, 160, 6, 9, 14, 0, 0, 0};
 
-	calendar actual;
+	struct calendar actual;
 
 	caljulian(testDate, &actual);
 
-	TEST_ASSERT_TRUE(IsEqual(expected, actual));
+	TEST_ASSERT_TRUE(IsEqualC(&expected, &actual));
 }
 
 TEST(caljulian, LeapYear) {
 	u_long input = 3549902400UL; // 2012-06-28 20:00:00Z
-	calendar expected = {2012, 179, 6, 28, 20, 0, 0};
+	struct calendar expected = {2012, 179, 6, 28, 20, 0, 0, 0};
 
-	calendar actual;
+	struct calendar actual;
 
 	caljulian(input, &actual);
 
-	TEST_ASSERT_TRUE(IsEqual(expected, actual));
+	TEST_ASSERT_TRUE(IsEqualC(&expected, &actual));
 }
 
 TEST(caljulian, uLongBoundary) {
 	u_long time = 4294967295UL; // 2036-02-07 6:28:15
-	calendar expected = {2036,0,2,7,6,28,15};
+	struct calendar expected = {2036, 0, 2, 7, 6, 28, 15, 0};
 
-	calendar actual;
+	struct calendar actual;
 
 	caljulian(time, &actual);
 
-	TEST_ASSERT_TRUE(IsEqual(expected, actual));
+	TEST_ASSERT_TRUE(IsEqualC(&expected, &actual));
 }
 
 TEST(caljulian, uLongWrapped) {
 	u_long time = 0;
-	calendar expected = {2036,0,2,7,6,28,16};
+	struct calendar expected = {2036,0,2,7,6,28,16, 0};
 
-	calendar actual;
+	struct calendar actual;
 
 	caljulian(time, &actual);
 
-	TEST_ASSERT_TRUE(IsEqual(expected, actual));
+	TEST_ASSERT_TRUE(IsEqualC(&expected, &actual));
 }
 
 TEST_GROUP_RUNNER(caljulian) {


=====================================
tests/libntp/caltontp.c
=====================================
--- a/tests/libntp/caltontp.c
+++ b/tests/libntp/caltontp.c
@@ -1,7 +1,10 @@
-extern "C" {
+#include "config.h"
+#include "ntp_stdlib.h"
+
 #include "unity.h"
 #include "unity_fixture.h"
-}
+
+#include "ntp_calendar.h"
 
 TEST_GROUP(caltontp);
 
@@ -9,18 +12,12 @@ TEST_SETUP(caltontp) {}
 
 TEST_TEAR_DOWN(caltontp) {}
 
-#include "libntptest.h"
 
-extern "C" {
-#include "ntp_calendar.h"
-}
 
-class caltontpTest : public libntptest {
-};
 
 TEST(caltontp, DateGivenMonthDay) {
 	// 2010-06-24 12:50:00
-	calendar input = {2010, 0, 6, 24, 12, 50, 0};
+	struct calendar input = {2010, 0, 6, 24, 12, 50, 0, 0};
 
 	u_long expected = 3486372600UL; // This is the timestamp above.
 
@@ -30,7 +27,7 @@ TEST(caltontp, DateGivenMonthDay) {
 TEST(caltontp, DateGivenYearDay) {
 	// 2010-06-24 12:50:00
 	// This is the 175th day of 2010.
-	calendar input = {2010, 175, 0, 0, 12, 50, 0};
+	struct calendar input = {2010, 175, 0, 0, 12, 50, 0, 0};
 
 	u_long expected = 3486372600UL; // This is the timestamp above.
 
@@ -40,8 +37,8 @@ TEST(caltontp, DateGivenYearDay) {
 TEST(caltontp, DateLeapYear) {
 	// 2012-06-24 12:00:00
 	// This is the 176th day of 2012 (since 2012 is a leap year).
-	calendar inputYd = {2012, 176, 0, 0, 12, 00, 00};
-	calendar inputMd = {2012, 0, 6, 24, 12, 00, 00};
+	struct calendar inputYd = {2012, 176, 0, 0, 12, 00, 00, 0};
+	struct calendar inputMd = {2012, 0, 6, 24, 12, 00, 00, 0};
 
 	u_long expected = 3549528000UL;
 
@@ -52,7 +49,7 @@ TEST(caltontp, DateLeapYear) {
 TEST(caltontp, WraparoundDateIn2036) {
 	// 2036-02-07 06:28:16
 	// This is (one) wrapping boundary where we go from ULONG_MAX to 0.
-	calendar input = {2036, 0, 2, 7, 6, 28, 16};
+	struct calendar input = {2036, 0, 2, 7, 6, 28, 16, 0};
 
 	u_long expected = 0UL;
 


=====================================
tests/libntp/calyearstart.c
=====================================
--- a/tests/libntp/calyearstart.c
+++ b/tests/libntp/calyearstart.c
@@ -1,4 +1,7 @@
 #include "config.h"
+#include "ntp_stdlib.h"
+
+#include "config.h"
 
 #include "unity.h"
 #include "unity_fixture.h"


=====================================
tests/libntp/clocktime.c
=====================================
--- a/tests/libntp/clocktime.c
+++ b/tests/libntp/clocktime.c
@@ -1,16 +1,13 @@
-extern "C" {
+#include "config.h"
+#include "ntp_stdlib.h"
+
 #include "unity.h"
 #include "unity_fixture.h"
-}
-
-TEST_GROUP(clocktime);
-
-TEST_SETUP(clocktime) {}
 
-TEST_TEAR_DOWN(clocktime) {}
+#include "ntp_calendar.h"
+#include "caltime.h"
 
-#include "libntptest.h"
-#include "cal.h"
+TEST_GROUP(clocktime);
 
 // ---------------------------------------------------------------------
 // test fixture
@@ -19,20 +16,13 @@ TEST_TEAR_DOWN(clocktime) {}
 // function for getting the current system time, so the tests are not
 // dependent on the actual system time.
 
-class clocktimeTest : public libntptest {
-	virtual void SetUp();
-	virtual void TearDown();
-};
-
-void clocktimeTest::SetUp()
-{
-    ntpcal_set_timefunc(timefunc);
-    settime(2000, 1, 1, 0, 0, 0);
+TEST_SETUP(clocktime) {
+	ntpcal_set_timefunc(timefunc);
+	settime(2000, 1, 1, 0, 0, 0);
 }
 
-void clocktimeTest::TearDown()
-{
-    ntpcal_set_timefunc(NULL);
+TEST_TEAR_DOWN(clocktime) {
+	ntpcal_set_timefunc(NULL);
 }
 
 // ---------------------------------------------------------------------
@@ -40,13 +30,13 @@ void clocktimeTest::TearDown()
 
 TEST(clocktime, CurrentYear) {
 	// Timestamp: 2010-06-24 12:50:00Z
-	const u_int32 timestamp = 3486372600UL;
-	const u_int32 expected	= timestamp; // exactly the same.
+	const u_int32_t timestamp = 3486372600UL;
+	const u_int32_t expected	= timestamp; // exactly the same.
 
 	const int yday=175, hour=12, minute=50, second=0, tzoff=0;
 
 	u_long yearstart=0;
-	u_int32 actual;
+	u_int32_t actual;
 
 	TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
 						  &yearstart, &actual));
@@ -54,7 +44,7 @@ TEST(clocktime, CurrentYear) {
 }
 
 TEST(clocktime, CurrentYearFuzz) {
-	/* 
+	/*
 	 * Timestamp (rec_ui) is: 2010-06-24 12:50:00
 	 * Time sent into function is 12:00:00.
 	 *
@@ -62,13 +52,13 @@ TEST(clocktime, CurrentYearFuzz) {
 	 * 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 u_int32_t timestamp = 3486372600UL; // 2010-06-24 12:50:00Z
+	const u_int32_t 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;
+	u_int32_t actual;
 
 	TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
 						  &yearstart, &actual));
@@ -82,13 +72,13 @@ TEST(clocktime, TimeZoneOffset) {
 	 *
 	 * Time sent into function is 04:00:00 +0800
 	 */
-	const u_int32 timestamp = 3486369600UL;
-	const u_int32 expected	= timestamp;
+	const u_int32_t timestamp = 3486369600UL;
+	const u_int32_t expected	= timestamp;
 
 	const int yday=175, hour=4, minute=0, second=0, tzoff=8;
 
 	u_long yearstart=0;
-	u_int32 actual;
+	u_int32_t actual;
 
 	TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
 						  &yearstart, &actual));
@@ -101,13 +91,13 @@ TEST(clocktime, WrongYearStart) {
 	 * 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 u_int32_t timestamp = 3471418800UL;
+	const u_int32_t expected	= timestamp;
 
 	const int yday=2, hour=11, minute=0, second=0, tzoff=0;
 
 	u_long yearstart = 302024100UL; // Yearstart of 2009.
-	u_int32 actual;
+	u_int32_t actual;
 
 	TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
 						  &yearstart, &actual));
@@ -120,13 +110,13 @@ TEST(clocktime, PreviousYear) {
 	 * 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 u_int32_t timestamp = 3471296400UL;
+	const u_int32_t expected	= 3471289200UL;
 
 	const int yday=365, hour=23, minute=0, second=0, tzoff=0;
 
 	u_long yearstart = 0;
-	u_int32 actual;
+	u_int32_t actual;
 
 	TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
 						  &yearstart, &actual));
@@ -139,12 +129,12 @@ TEST(clocktime, NextYear) {
 	 * 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 u_int32_t timestamp = 3471289200UL;
+	const u_int32_t expected	= 3471296400UL;
 
 	const int yday=1, hour=1, minute=0, second=0, tzoff=0;
 	u_long yearstart = 0;
-	u_int32 actual;
+	u_int32_t actual;
 
 	TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
 						  &yearstart, &actual));
@@ -153,11 +143,11 @@ TEST(clocktime, NextYear) {
 
 TEST(clocktime, NoReasonableConversion) {
 	/* Timestamp is: 2010-01-02 11:00:00Z */
-	const u_int32 timestamp = 3471418800UL;
+	const u_int32_t timestamp = 3471418800UL;
 
 	const int yday=100, hour=12, minute=0, second=0, tzoff=0;
 	u_long yearstart = 0;
-	u_int32 actual;
+	u_int32_t actual;
 
 	TEST_ASSERT_FALSE(clocktime(yday, hour, minute, second, tzoff, timestamp,
 						   &yearstart, &actual));
@@ -165,7 +155,7 @@ TEST(clocktime, NoReasonableConversion) {
 
 TEST(clocktime, AlwaysInLimit) {
 	/* Timestamp is: 2010-01-02 11:00:00Z */
-	const u_int32 timestamp = 3471418800UL;
+	const u_int32_t timestamp = 3471418800UL;
 	const u_short prime_incs[] = { 127, 151, 163, 179 };
 	int	cyc;
 	int	yday;
@@ -173,10 +163,9 @@ TEST(clocktime, AlwaysInLimit) {
 	u_short	ydayinc;
 	int	hour;
 	int	minute;
-	int	second;
 	u_long	yearstart;
-	u_int32	actual;
-	u_int32	diff;
+	u_int32_t	actual;
+	u_int32_t	diff;
 
 	yearstart = 0;
 	for (cyc = 0; cyc < 5; cyc++) {
@@ -189,9 +178,10 @@ TEST(clocktime, AlwaysInLimit) {
 					clocktime(yday, hour, minute, 30, 0,
 						  timestamp, &yearstart, &actual);
 					diff = actual - timestamp;
-					if (diff >= 0x80000000UL)
+					if (diff >= 0x80000000UL) {
 						diff = ~diff + 1;
-					TEST_ASSERT_LESS_THAN_OR_EQUAL(diff, (183u * SECSPERDAY));
+					}
+					TEST_ASSERT_TRUE(diff <= (183u * SECSPERDAY));
 				}
 			}
 		}


=====================================
tests/libntp/decodenetnum.c
=====================================
--- a/tests/libntp/decodenetnum.c
+++ b/tests/libntp/decodenetnum.c
@@ -1,3 +1,6 @@
+#include "config.h"
+#include "ntp_stdlib.h"
+
 #include "unity.h"
 #include "unity_fixture.h"
 


=====================================
tests/libntp/hextolfp.c
=====================================
--- a/tests/libntp/hextolfp.c
+++ b/tests/libntp/hextolfp.c
@@ -1,3 +1,6 @@
+#include "config.h"
+#include "ntp_stdlib.h"
+
 #include "unity.h"
 #include "unity_fixture.h"
 


=====================================
tests/libntp/humandate.c
=====================================
--- a/tests/libntp/humandate.c
+++ b/tests/libntp/humandate.c
@@ -1,3 +1,6 @@
+#include "config.h"
+#include "ntp_stdlib.h"
+
 extern "C" {
 #include "unity.h"
 #include "unity_fixture.h"
@@ -9,7 +12,6 @@ TEST_SETUP(humandate) {}
 
 TEST_TEAR_DOWN(humandate) {}
 
-#include "libntptest.h"
 
 #include <sstream>
 #include <string>


=====================================
tests/libntp/lfpfunc.c
=====================================
--- a/tests/libntp/lfpfunc.c
+++ b/tests/libntp/lfpfunc.c
@@ -1,3 +1,6 @@
+#include "config.h"
+#include "ntp_stdlib.h"
+
 extern "C" {
 #include "unity.h"
 #include "unity_fixture.h"
@@ -9,7 +12,6 @@ TEST_SETUP(lfpfunc) {}
 
 TEST_TEAR_DOWN(lfpfunc) {}
 
-#include "libntptest.h"
 #include "timestructs.h"
 
 extern "C" {


=====================================
tests/libntp/lfptest.h
=====================================
--- a/tests/libntp/lfptest.h
+++ b/tests/libntp/lfptest.h
@@ -1,7 +1,6 @@
 #ifndef GUARD_NTP_TESTS_LFPTEST_H
 #define GUARD_NTP_TESTS_LFPTEST_H
 
-#include "libntptest.h"
 #include "ntp_fp.h"
 
 static bool IsEqual(const l_fp *expected, const l_fp *actual) {


=====================================
tests/libntp/lfptostr.c
=====================================
--- a/tests/libntp/lfptostr.c
+++ b/tests/libntp/lfptostr.c
@@ -1,3 +1,6 @@
+#include "config.h"
+#include "ntp_stdlib.h"
+
 extern "C" {
 #include "unity.h"
 #include "unity_fixture.h"
@@ -15,7 +18,6 @@ TEST_TEAR_DOWN(lfptostr) {}
  * a macro.
  */
 
-#include "libntptest.h"
 
 extern "C" {
 #include "ntp_fp.h"


=====================================
tests/libntp/libntptest.h deleted
=====================================
--- a/tests/libntp/libntptest.h
+++ /dev/null
@@ -1,4 +0,0 @@
-#include "tests_main.h"
-
-#include "ntp_stdlib.h"
-


=====================================
tests/libntp/modetoa.c
=====================================
--- a/tests/libntp/modetoa.c
+++ b/tests/libntp/modetoa.c
@@ -1,3 +1,6 @@
+#include "config.h"
+#include "ntp_stdlib.h"
+
 #include "unity.h"
 #include "unity_fixture.h"
 
@@ -7,7 +10,6 @@ TEST_SETUP(modetoa) {}
 
 TEST_TEAR_DOWN(modetoa) {}
 
-#include "libntptest.h"
 
 
 TEST(modetoa, KnownMode) {


=====================================
tests/libntp/msyslog.c
=====================================
--- a/tests/libntp/msyslog.c
+++ b/tests/libntp/msyslog.c
@@ -1,3 +1,6 @@
+#include "config.h"
+#include "ntp_stdlib.h"
+
 extern "C" {
 #include "unity.h"
 #include "unity_fixture.h"
@@ -9,7 +12,6 @@ TEST_SETUP(msyslog) {}
 
 TEST_TEAR_DOWN(msyslog) {}
 
-#include "libntptest.h"
 
 extern "C" {
 #include <stdio.h>


=====================================
tests/libntp/netof.c
=====================================
--- a/tests/libntp/netof.c
+++ b/tests/libntp/netof.c
@@ -1,7 +1,8 @@
-extern "C" {
+#include "config.h"
+#include "ntp_stdlib.h"
+
 #include "unity.h"
 #include "unity_fixture.h"
-}
 
 TEST_GROUP(netof);
 
@@ -11,8 +12,6 @@ TEST_TEAR_DOWN(netof) {}
 
 #include "sockaddrtest.h"
 
-class netofTest : public sockaddrtest  {
-};
 
 TEST(netof, ClassBAddress) {
 	sockaddr_u input = CreateSockaddr4("172.16.2.1", NTP_PORT);
@@ -21,7 +20,7 @@ TEST(netof, ClassBAddress) {
 	sockaddr_u* actual = netof(&input);
 
 	TEST_ASSERT_TRUE(actual != NULL);
-	TEST_ASSERT_TRUE(IsEqual(expected, *actual));
+	TEST_ASSERT_TRUE(IsEqualS(&expected, actual));
 }
 
 TEST(netof, ClassCAddress) {
@@ -31,7 +30,7 @@ TEST(netof, ClassCAddress) {
 	sockaddr_u* actual = netof(&input);
 
 	TEST_ASSERT_TRUE(actual != NULL);
-	TEST_ASSERT_TRUE(IsEqual(expected, *actual));
+	TEST_ASSERT_TRUE(IsEqualS(&expected, actual));
 }
 
 TEST(netof, ClassAAddress) {
@@ -44,24 +43,24 @@ TEST(netof, ClassAAddress) {
 	sockaddr_u* actual = netof(&input);
 
 	TEST_ASSERT_TRUE(actual != NULL);
-	TEST_ASSERT_TRUE(IsEqual(expected, *actual));
+	TEST_ASSERT_TRUE(IsEqualS(&expected, actual));
 }
 
 TEST(netof, IPv6Address) {
 	/* IPv6 addresses are assumed to have 64-bit host- and 64-bit network parts. */
-	const struct in6_addr input_address = {
+	const struct in6_addr input_address = {{{
 		0x20, 0x01, 0x0d, 0xb8,
-        0x85, 0xa3, 0x08, 0xd3, 
+        0x85, 0xa3, 0x08, 0xd3,
         0x13, 0x19, 0x8a, 0x2e,
         0x03, 0x70, 0x73, 0x34
-	}; // 2001:0db8:85a3:08d3:1319:8a2e:0370:7334
+	}}}; // 2001:0db8:85a3:08d3:1319:8a2e:0370:7334
 
-	const struct in6_addr expected_address = {
+	const struct in6_addr expected_address = {{{
 		0x20, 0x01, 0x0d, 0xb8,
-        0x85, 0xa3, 0x08, 0xd3, 
+        0x85, 0xa3, 0x08, 0xd3,
         0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00
-	}; // 2001:0db8:85a3:08d3:0000:0000:0000:0000
+	}}}; // 2001:0db8:85a3:08d3:0000:0000:0000:0000
 
 	sockaddr_u input;
 	input.sa6.sin6_family = AF_INET6;
@@ -76,7 +75,7 @@ TEST(netof, IPv6Address) {
 	sockaddr_u* actual = netof(&input);
 
 	TEST_ASSERT_TRUE(actual != NULL);
-	TEST_ASSERT_TRUE(IsEqual(expected, *actual));
+	TEST_ASSERT_TRUE(IsEqualS(&expected, actual));
 }
 
 TEST_GROUP_RUNNER(netof) {


=====================================
tests/libntp/numtoa.c
=====================================
--- a/tests/libntp/numtoa.c
+++ b/tests/libntp/numtoa.c
@@ -1,3 +1,6 @@
+#include "config.h"
+#include "ntp_stdlib.h"
+
 #include "unity.h"
 #include "unity_fixture.h"
 
@@ -7,7 +10,6 @@ TEST_SETUP(numtoa) {}
 
 TEST_TEAR_DOWN(numtoa) {}
 
-#include "libntptest.h"
 
 TEST(numtoa, Address) {
 	u_int32_t input = htonl(3221225472UL+512UL+1UL); // 192.0.2.1


=====================================
tests/libntp/numtohost.c
=====================================
--- a/tests/libntp/numtohost.c
+++ b/tests/libntp/numtohost.c
@@ -1,3 +1,6 @@
+#include "config.h"
+#include "ntp_stdlib.h"
+
 #include "unity.h"
 #include "unity_fixture.h"
 
@@ -8,7 +11,6 @@ TEST_SETUP(numtohost) {}
 
 TEST_TEAR_DOWN(numtohost) {}
 
-#include "libntptest.h"
 
 
 TEST(numtohost, LoopbackNetNonResolve) {


=====================================
tests/libntp/prettydate.c
=====================================
--- a/tests/libntp/prettydate.c
+++ b/tests/libntp/prettydate.c
@@ -1,3 +1,6 @@
+#include "config.h"
+#include "ntp_stdlib.h"
+
 #include "unity.h"
 #include "unity_fixture.h"
 
@@ -7,7 +10,6 @@ TEST_SETUP(prettydate) {}
 
 TEST_TEAR_DOWN(prettydate) {}
 
-#include "libntptest.h"
 
 #include "ntp_fp.h"
 


=====================================
tests/libntp/recvbuff.c
=====================================
--- a/tests/libntp/recvbuff.c
+++ b/tests/libntp/recvbuff.c
@@ -1,6 +1,8 @@
+#include "config.h"
+#include "ntp_stdlib.h"
+
 #include "unity.h"
 #include "unity_fixture.h"
-#include "libntptest.h"
 #include "recvbuff.h"
 
 


=====================================
tests/libntp/refnumtoa.c
=====================================
--- a/tests/libntp/refnumtoa.c
+++ b/tests/libntp/refnumtoa.c
@@ -1,7 +1,11 @@
-extern "C" {
+#include "config.h"
+#include "ntp_stdlib.h"
+
 #include "unity.h"
 #include "unity_fixture.h"
-}
+
+#include "ntp_net.h"
+#include "ntp_refclock.h"
 
 TEST_GROUP(refnumtoa);
 
@@ -9,25 +13,16 @@ TEST_SETUP(refnumtoa) {}
 
 TEST_TEAR_DOWN(refnumtoa) {}
 
-#include "libntptest.h"
 
-#include "ntp_net.h"
-#include "ntp_refclock.h"
+/* Might need to be updated if a new refclock gets this id. */
+static const int UNUSED_REFCLOCK_ID = 250;
 
-#include <sstream>
 
-class refnumtoaTest : public libntptest {
-protected:
-	/* Might need to be updated if a new refclock gets this id. */
-	static const int UNUSED_REFCLOCK_ID = 250;
-};
-
-#ifdef REFCLOCK		/* clockname() is useless otherwise */
 TEST(refnumtoa, LocalClock) {
 	/* We test with a refclock address of type LOCALCLOCK.
 	 * with id 8
 	 */
-	u_int32 addr = REFCLOCK_ADDR;
+	u_int32_t addr = REFCLOCK_ADDR;
 	addr |= REFCLK_LOCALCLOCK << 8;
 	addr |= 0x8;
 
@@ -35,18 +30,15 @@ TEST(refnumtoa, LocalClock) {
 	address.sa4.sin_family = AF_INET;
 	address.sa4.sin_addr.s_addr = htonl(addr);
 
-	std::ostringstream expected;
-	expected << clockname(REFCLK_LOCALCLOCK)
-			 << "(8)";
+	char expected[255];
+	snprintf(expected, sizeof(expected), "%s(8)", clockname(REFCLK_LOCALCLOCK));
 
-	TEST_ASSERT_EQUAL_STRING(expected.str().c_str(), refnumtoa(&address));
+	TEST_ASSERT_EQUAL_STRING(expected, refnumtoa(&address));
 }
-#endif	/* REFCLOCK */
 
-#ifdef REFCLOCK		/* refnumtoa() is useless otherwise */
 TEST(refnumtoa, UnknownId) {
 	/* We test with a currently unused refclock ID */
-	u_int32 addr = REFCLOCK_ADDR;
+	u_int32_t addr = REFCLOCK_ADDR;
 	addr |= UNUSED_REFCLOCK_ID << 8;
 	addr |= 0x4;
 
@@ -54,13 +46,12 @@ TEST(refnumtoa, UnknownId) {
 	address.sa4.sin_family = AF_INET;
 	address.sa4.sin_addr.s_addr = htonl(addr);
 
-	std::ostringstream expected;
-	expected << "REFCLK(" << UNUSED_REFCLOCK_ID
-			 << ",4)";
+	char expected[255];
+	snprintf(expected, sizeof(expected), "REFCLK(%d,4)", UNUSED_REFCLOCK_ID);
 
-	TEST_ASSERT_EQUAL_STRING(expected.str().c_str(), refnumtoa(&address));
+	TEST_ASSERT_EQUAL_STRING(expected, refnumtoa(&address));
 }
-#endif	/* REFCLOCK */
+
 
 TEST_GROUP_RUNNER(refnumtoa) {
 	RUN_TEST_CASE(refnumtoa, LocalClock);


=====================================
tests/libntp/sfptostr.c
=====================================
--- a/tests/libntp/sfptostr.c
+++ b/tests/libntp/sfptostr.c
@@ -1,3 +1,6 @@
+#include "config.h"
+#include "ntp_stdlib.h"
+
 #include "unity.h"
 #include "unity_fixture.h"
 
@@ -12,7 +15,6 @@ TEST_TEAR_DOWN(sfptostr) {}
  * since all these functions are very similar.
  */
 
-#include "libntptest.h"
 
 #include "ntp_fp.h"
 


=====================================
tests/libntp/socktoa.c
=====================================
--- a/tests/libntp/socktoa.c
+++ b/tests/libntp/socktoa.c
@@ -1,7 +1,8 @@
-extern "C" {
+#include "config.h"
+#include "ntp_stdlib.h"
+
 #include "unity.h"
 #include "unity_fixture.h"
-}
 
 TEST_GROUP(socktoa);
 
@@ -11,8 +12,6 @@ TEST_TEAR_DOWN(socktoa) {}
 
 #include "sockaddrtest.h"
 
-class socktoaTest : public sockaddrtest {
-};
 
 TEST(socktoa, IPv4AddressWithPort) {
 	sockaddr_u input = CreateSockaddr4("192.0.2.10", 123);
@@ -22,12 +21,12 @@ TEST(socktoa, IPv4AddressWithPort) {
 }
 
 TEST(socktoa, IPv6AddressWithPort) {
-	const struct in6_addr address = {
+	const struct in6_addr address = {{{
 		0x20, 0x01, 0x0d, 0xb8,
-		0x85, 0xa3, 0x08, 0xd3, 
+		0x85, 0xa3, 0x08, 0xd3,
 		0x13, 0x19, 0x8a, 0x2e,
 		0x03, 0x70, 0x73, 0x34
-	};
+	}}};
 
 	const char* expected =
 		"2001:db8:85a3:8d3:1319:8a2e:370:7334";
@@ -46,16 +45,16 @@ TEST(socktoa, IPv6AddressWithPort) {
 
 #ifdef ISC_PLATFORM_HAVESCOPEID
 TEST(socktoa, ScopedIPv6AddressWithPort) {
-	const struct in6_addr address = {
+	const struct in6_addr address = {{{
 		0xfe, 0x80, 0x00, 0x00,
 		0x00, 0x00, 0x00, 0x00,
-		0x02, 0x12, 0x3f, 0xff, 
+		0x02, 0x12, 0x3f, 0xff,
 		0xfe, 0x29, 0xff, 0xfa
-	};
+	}}};
 
 	const char* expected =
 		"fe80::212:3fff:fe29:fffa%5";
-	const char* expected_port = 
+	const char* expected_port =
 		"[fe80::212:3fff:fe29:fffa%5]:123";
 
 	sockaddr_u input;
@@ -74,7 +73,7 @@ TEST(socktoa, HashEqual) {
 	sockaddr_u input1 = CreateSockaddr4("192.00.2.2", 123);
 	sockaddr_u input2 = CreateSockaddr4("192.0.2.2", 123);
 
-	TEST_ASSERT_TRUE(IsEqual(input1, input2));
+	TEST_ASSERT_TRUE(IsEqualS(&input1, &input2));
 	TEST_ASSERT_EQUAL(sock_hash(&input1), sock_hash(&input2));
 }
 
@@ -83,17 +82,17 @@ TEST(socktoa, HashNotEqual) {
 	sockaddr_u input1 = CreateSockaddr4("192.0.2.1", 123);
 	sockaddr_u input2 = CreateSockaddr4("192.0.2.2", 123);
 
-	TEST_ASSERT_FALSE(IsEqual(input1, input2));
+	TEST_ASSERT_FALSE(IsEqualS(&input1, &input2));
 	TEST_ASSERT_NOT_EQUAL(sock_hash(&input1), sock_hash(&input2));
 }
 
 TEST(socktoa, IgnoreIPv6Fields) {
-	const struct in6_addr address = {
+	const struct in6_addr address = {{{
 		0x20, 0x01, 0x0d, 0xb8,
-        0x85, 0xa3, 0x08, 0xd3, 
+        0x85, 0xa3, 0x08, 0xd3,
         0x13, 0x19, 0x8a, 0x2e,
         0x03, 0x70, 0x73, 0x34
-	};
+	}}};
 
 	sockaddr_u input1, input2;
 


=====================================
tests/libntp/ssl_init.c
=====================================
--- a/tests/libntp/ssl_init.c
+++ b/tests/libntp/ssl_init.c
@@ -1,4 +1,7 @@
 #include "config.h"
+#include "ntp_stdlib.h"
+
+#include "config.h"
 
 #include "unity.h"
 #include "unity_fixture.h"
@@ -9,7 +12,6 @@ TEST_SETUP(ssl_init) {}
 
 TEST_TEAR_DOWN(ssl_init) {}
 
-#include "libntptest.h"
 
 #ifdef HAVE_OPENSSL
 # include "openssl/err.h"


=====================================
tests/libntp/statestr.c
=====================================
--- a/tests/libntp/statestr.c
+++ b/tests/libntp/statestr.c
@@ -1,3 +1,6 @@
+#include "config.h"
+#include "ntp_stdlib.h"
+
 #include "unity.h"
 #include "unity_fixture.h"
 
@@ -7,7 +10,6 @@ TEST_SETUP(statestr) {}
 
 TEST_TEAR_DOWN(statestr) {}
 
-#include "libntptest.h"
 
 #include "ntp.h" // Needed for MAX_MAC_LEN used in ntp_control.h
 #include "ntp_control.h"


=====================================
tests/libntp/strtolfp.c
=====================================
--- a/tests/libntp/strtolfp.c
+++ b/tests/libntp/strtolfp.c
@@ -1,3 +1,6 @@
+#include "config.h"
+#include "ntp_stdlib.h"
+
 #include "unity.h"
 #include "unity_fixture.h"
 


=====================================
tests/libntp/timespecops.c
=====================================
--- a/tests/libntp/timespecops.c
+++ b/tests/libntp/timespecops.c
@@ -1,3 +1,6 @@
+#include "config.h"
+#include "ntp_stdlib.h"
+
 extern "C" {
 #include "unity.h"
 #include "unity_fixture.h"
@@ -9,7 +12,6 @@ TEST_SETUP(timespecops) {}
 
 TEST_TEAR_DOWN(timespecops) {}
 
-#include "libntptest.h"
 #include "timestructs.h"
 
 extern "C" {


=====================================
tests/libntp/timevalops.c
=====================================
--- a/tests/libntp/timevalops.c
+++ b/tests/libntp/timevalops.c
@@ -1,3 +1,6 @@
+#include "config.h"
+#include "ntp_stdlib.h"
+
 extern "C" {
 #include "unity.h"
 #include "unity_fixture.h"
@@ -9,7 +12,6 @@ TEST_SETUP(timevalops) {}
 
 TEST_TEAR_DOWN(timevalops) {}
 
-#include "libntptest.h"
 #include "timestructs.h"
 
 extern "C" {


=====================================
tests/libntp/tstotv.c
=====================================
--- a/tests/libntp/tstotv.c
+++ b/tests/libntp/tstotv.c
@@ -1,4 +1,7 @@
 #include "config.h"
+#include "ntp_stdlib.h"
+
+#include "config.h"
 
 #include "unity.h"
 #include "unity_fixture.h"


=====================================
tests/libntp/tvtots.c
=====================================
--- a/tests/libntp/tvtots.c
+++ b/tests/libntp/tvtots.c
@@ -1,4 +1,7 @@
 #include "config.h"
+#include "ntp_stdlib.h"
+
+#include "config.h"
 
 #include "unity.h"
 #include "unity_fixture.h"


=====================================
tests/libntp/vi64ops.c
=====================================
--- a/tests/libntp/vi64ops.c
+++ b/tests/libntp/vi64ops.c
@@ -1,3 +1,6 @@
+#include "config.h"
+#include "ntp_stdlib.h"
+
 #include "unity.h"
 #include "unity_fixture.h"
 
@@ -7,7 +10,6 @@ TEST_SETUP(vi64ops) {}
 
 TEST_TEAR_DOWN(vi64ops) {}
 
-#include "libntptest.h"
 
 #include "vint64ops.h"
 


=====================================
tests/libntp/ymd2yd.c
=====================================
--- a/tests/libntp/ymd2yd.c
+++ b/tests/libntp/ymd2yd.c
@@ -1,3 +1,6 @@
+#include "config.h"
+#include "ntp_stdlib.h"
+
 #include "unity.h"
 #include "unity_fixture.h"
 
@@ -7,7 +10,6 @@ TEST_SETUP(ymd2yd) {}
 
 TEST_TEAR_DOWN(ymd2yd) {}
 
-#include "libntptest.h"
 
 
 TEST(ymd2yd, NonLeapYearFebruary) {


=====================================
tests/ntpdig/networking.c deleted
=====================================
--- a/tests/ntpdig/networking.c
+++ /dev/null
@@ -1,3 +0,0 @@
-#include "ntpdigtest.h"
-
-#include "networking.h"


=====================================
tests/ntpdig/networking.cpp deleted
=====================================
--- a/tests/ntpdig/networking.cpp
+++ /dev/null
@@ -1,5 +0,0 @@
-#include "ntpdigtest.h"
-
-extern "C" {
-#include "networking.h"
-};


=====================================
tests/ntpdig/packetProcessing.c
=====================================
--- /dev/null
+++ b/tests/ntpdig/packetProcessing.c
@@ -0,0 +1,359 @@
+extern "C" {
+#include "unity.h"
+#include "unity_fixture.h"
+}
+
+TEST_GROUP(packetProcessing);
+
+TEST_SETUP(packetProcessing) {}
+
+TEST_TEAR_DOWN(packetProcessing) {}
+
+#include "ntpdigtest.h"
+
+extern "C" {
+#include "networking.h"
+#include "ntp_stdlib.h"
+};
+
+#include <sstream>
+#include <string>
+
+// Hacks into the key database.
+extern key* key_ptr;
+extern int key_cnt;
+
+class packetProcessingTest : public ntpdigtest {
+protected:
+	pkt testpkt;
+	pkt testspkt;
+	sockaddr_u testsock;
+	bool restoreKeyDb;
+
+	void PrepareAuthenticationTest(int key_id,
+								   int key_len,
+								   const char* type,
+								   const void* key_seq) {
+		std::stringstream ss;
+		ss << key_id;
+
+		ActivateOption("-a", ss.str().c_str());
+
+		key_cnt = 1;
+		key_ptr = new key;
+		key_ptr->next = NULL;
+		key_ptr->key_id = key_id;
+		key_ptr->key_len = key_len;
+		memcpy(key_ptr->type, "MD5", 3);
+
+		TEST_ASSERT_TRUE(key_len < sizeof(key_ptr->key_seq));
+
+		memcpy(key_ptr->key_seq, key_seq, key_ptr->key_len);
+		restoreKeyDb = true;
+	}
+
+	void PrepareAuthenticationTest(int key_id,
+								   int key_len,
+								   const void* key_seq) {
+		PrepareAuthenticationTest(key_id, key_len, "MD5", key_seq);
+	}
+
+	virtual void SetUp() {
+		restoreKeyDb = false;
+
+		/* Initialize the test packet and socket,
+		 * so they contain at least some valid data. */
+		testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING, NTP_VERSION,
+											MODE_SERVER);
+		testpkt.stratum = STRATUM_REFCLOCK;
+		memcpy(&testpkt.refid, "GPS\0", 4);
+
+		/* Set the origin timestamp of the received packet to the
+		 * same value as the transmit timestamp of the sent packet. */
+		l_fp tmp;
+		tmp.l_ui = 1000UL;
+		tmp.l_uf = 0UL;
+
+		HTONL_FP(&tmp, &testpkt.org);
+		HTONL_FP(&tmp, &testspkt.xmt);
+	}
+
+	virtual void TearDown() {
+		if (restoreKeyDb) {
+			key_cnt = 0;
+			delete key_ptr;
+			key_ptr = NULL;
+		}
+	}
+};
+
+TEST(packetProcessing, TooShortLength) {
+	TEST_ASSERT_EQUAL(PACKET_UNUSEABLE,
+			  process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC - 1,
+						  MODE_SERVER, &testspkt, "UnitTest", true));
+	TEST_ASSERT_EQUAL(PACKET_UNUSEABLE,
+			  process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC - 1,
+						  MODE_BROADCAST, &testspkt, "UnitTest", true));
+}
+
+TEST(packetProcessing, LengthNotMultipleOfFour) {
+	TEST_ASSERT_EQUAL(PACKET_UNUSEABLE,
+			  process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC + 6,
+						  MODE_SERVER, &testspkt, "UnitTest", true));
+	TEST_ASSERT_EQUAL(PACKET_UNUSEABLE,
+			  process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC + 3,
+						  MODE_BROADCAST, &testspkt, "UnitTest", true));
+}
+
+TEST(packetProcessing, TooShortExtensionFieldLength) {
+	/* The lower 16-bits are the length of the extension field.
+	 * This lengths must be multiples of 4 bytes, which gives
+	 * a minimum of 4 byte extension field length. */
+	testpkt.exten[7] = htonl(3); // 3 bytes is too short.
+
+	/* We send in a pkt_len of header size + 4 byte extension
+	 * header + 24 byte MAC, this prevents the length error to
+	 * be caught at an earlier stage */
+	int pkt_len = LEN_PKT_NOMAC + 4 + 24;
+
+	TEST_ASSERT_EQUAL(PACKET_UNUSEABLE,
+			  process_pkt(&testpkt, &testsock, pkt_len,
+						  MODE_SERVER, &testspkt, "UnitTest", true));
+}
+
+TEST(packetProcessing, UnauthenticatedPacketReject) {
+	// Activate authentication option
+	ActivateOption("-a", "123");
+	TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
+
+	int pkt_len = LEN_PKT_NOMAC;
+
+	// We demand authentication, but no MAC header is present.
+	TEST_ASSERT_EQUAL(SERVER_AUTH_FAIL,
+			  process_pkt(&testpkt, &testsock, pkt_len,
+						  MODE_SERVER, &testspkt, "UnitTest", true));
+}
+
+TEST(packetProcessing, CryptoNAKPacketReject) {
+	// Activate authentication option
+	ActivateOption("-a", "123");
+	TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
+
+	int pkt_len = LEN_PKT_NOMAC + 4; // + 4 byte MAC = Crypto-NAK
+
+	TEST_ASSERT_EQUAL(SERVER_AUTH_FAIL,
+			  process_pkt(&testpkt, &testsock, pkt_len,
+						  MODE_SERVER, &testspkt, "UnitTest", true));
+}
+
+TEST(packetProcessing, AuthenticatedPacketInvalid) {
+	// Activate authentication option
+	PrepareAuthenticationTest(50, 9, "123456789");
+	TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
+
+	// Prepare the packet.
+	int pkt_len = LEN_PKT_NOMAC;
+
+	testpkt.exten[0] = htonl(50);
+	int mac_len = make_mac((char*)&testpkt, pkt_len,
+						   MAX_MD5_LEN, key_ptr,
+						   (char*)&testpkt.exten[1]);
+
+	pkt_len += 4 + mac_len;
+
+	// Now, alter the MAC so it becomes invalid.
+	testpkt.exten[1] += 1;
+
+	TEST_ASSERT_EQUAL(SERVER_AUTH_FAIL,
+			  process_pkt(&testpkt, &testsock, pkt_len,
+						  MODE_SERVER, &testspkt, "UnitTest", true));
+}
+
+TEST(packetProcessing, AuthenticatedPacketUnknownKey) {
+	// Activate authentication option
+	PrepareAuthenticationTest(30, 9, "123456789");
+	TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
+
+	// Prepare the packet. Observe that the Key-ID expected is 30,
+	// but the packet has a key id of 50.
+	int pkt_len = LEN_PKT_NOMAC;
+
+	testpkt.exten[0] = htonl(50);
+	int mac_len = make_mac((char*)&testpkt, pkt_len,
+						   MAX_MD5_LEN, key_ptr,
+						   (char*)&testpkt.exten[1]);
+	pkt_len += 4 + mac_len;
+
+	TEST_ASSERT_EQUAL(SERVER_AUTH_FAIL,
+			  process_pkt(&testpkt, &testsock, pkt_len,
+						  MODE_SERVER, &testspkt, "UnitTest", true));
+}
+
+TEST(packetProcessing, ServerVersionTooOld) {
+	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
+
+	testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
+										NTP_OLDVERSION - 1,
+										MODE_CLIENT);
+	TEST_ASSERT_LESS_THAN(PKT_VERSION(testpkt.li_vn_mode), NTP_OLDVERSION);
+
+	int pkt_len = LEN_PKT_NOMAC;
+
+	TEST_ASSERT_EQUAL(SERVER_UNUSEABLE,
+			  process_pkt(&testpkt, &testsock, pkt_len,
+						  MODE_SERVER, &testspkt, "UnitTest", true));
+}
+
+TEST(packetProcessing, ServerVersionTooNew) {
+	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
+
+	testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
+										NTP_VERSION + 1,
+										MODE_CLIENT);
+	TEST_ASSERT_GREATER_THAN(PKT_VERSION(testpkt.li_vn_mode), NTP_VERSION);
+
+	int pkt_len = LEN_PKT_NOMAC;
+
+	TEST_ASSERT_EQUAL(SERVER_UNUSEABLE,
+			  process_pkt(&testpkt, &testsock, pkt_len,
+						  MODE_SERVER, &testspkt, "UnitTest", true));
+}
+
+TEST(packetProcessing, NonWantedMode) {
+	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
+
+	testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
+										NTP_VERSION,
+										MODE_CLIENT);
+
+	// The packet has a mode of MODE_CLIENT, but process_pkt expects MODE_SERVER
+
+	TEST_ASSERT_EQUAL(SERVER_UNUSEABLE,
+			  process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC,
+						  MODE_SERVER, &testspkt, "UnitTest", true));
+}
+
+/* Tests bug 1597 */
+TEST(packetProcessing, KoDRate) {
+	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
+
+	testpkt.stratum = STRATUM_PKT_UNSPEC;
+	memcpy(&testpkt.refid, "RATE", 4);
+
+	TEST_ASSERT_EQUAL(KOD_RATE,
+			  process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC,
+						  MODE_SERVER, &testspkt, "UnitTest", true));
+}
+
+TEST(packetProcessing, KoDDeny) {
+	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
+
+	testpkt.stratum = STRATUM_PKT_UNSPEC;
+	memcpy(&testpkt.refid, "DENY", 4);
+
+	TEST_ASSERT_EQUAL(KOD_DEMOBILIZE,
+			  process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC,
+						  MODE_SERVER, &testspkt, "UnitTest", true));
+}
+
+TEST(packetProcessing, RejectUnsyncedServer) {
+	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
+
+	testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOTINSYNC,
+										NTP_VERSION,
+										MODE_SERVER);
+
+	TEST_ASSERT_EQUAL(SERVER_UNUSEABLE,
+			  process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC,
+						  MODE_SERVER, &testspkt, "UnitTest", true));
+}
+
+TEST(packetProcessing, RejectWrongResponseServerMode) {
+	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
+
+	l_fp tmp;
+	tmp.l_ui = 1000UL;
+	tmp.l_uf = 0UL;
+	HTONL_FP(&tmp, &testpkt.org);
+
+	tmp.l_ui = 2000UL;
+	tmp.l_uf = 0UL;
+	HTONL_FP(&tmp, &testspkt.xmt);
+
+	TEST_ASSERT_EQUAL(PACKET_UNUSEABLE,
+			  process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC,
+						  MODE_SERVER, &testspkt, "UnitTest", true));
+}
+
+TEST(packetProcessing, AcceptNoSentPacketBroadcastMode) {
+	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
+
+	testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
+					    NTP_VERSION,
+					    MODE_BROADCAST);
+
+	TEST_ASSERT_EQUAL(LEN_PKT_NOMAC,
+		  process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC,
+			      MODE_BROADCAST, NULL, "UnitTest", true));
+}
+
+TEST(packetProcessing, CorrectUnauthenticatedPacket) {
+	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
+
+	TEST_ASSERT_EQUAL(LEN_PKT_NOMAC,
+			  process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC,
+						  MODE_SERVER, &testspkt, "UnitTest", true));
+}
+
+TEST(packetProcessing, CorrectAuthenticatedPacketMD5) {
+	PrepareAuthenticationTest(10, 15, "123456789abcdef");
+	TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
+
+	int pkt_len = LEN_PKT_NOMAC;
+
+	// Prepare the packet.
+	testpkt.exten[0] = htonl(10);
+	int mac_len = make_mac((char*)&testpkt, pkt_len,
+						   MAX_MD5_LEN, key_ptr,
+						   (char*)&testpkt.exten[1]);
+
+	pkt_len += 4 + mac_len;
+
+	TEST_ASSERT_EQUAL(pkt_len,
+			  process_pkt(&testpkt, &testsock, pkt_len,
+						  MODE_SERVER, &testspkt, "UnitTest", true));
+
+}
+
+TEST(packetProcessing, CorrectAuthenticatedPacketSHA1) {
+	PrepareAuthenticationTest(20, 15, "SHA1", "abcdefghijklmno");
+	TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
+
+	int pkt_len = LEN_PKT_NOMAC;
+
+	// Prepare the packet.
+	testpkt.exten[0] = htonl(20);
+	int mac_len = make_mac((char*)&testpkt, pkt_len,
+						   MAX_MAC_LEN, key_ptr,
+						   (char*)&testpkt.exten[1]);
+
+	pkt_len += 4 + mac_len;
+
+	TEST_ASSERT_EQUAL(pkt_len,
+			  process_pkt(&testpkt, &testsock, pkt_len,
+						  MODE_SERVER, &testspkt, "UnitTest", true));
+}
+
+TEST_GROUP_RUNNER(packetProcessing) {
+	RUN_TEST_CASE(packetProcessing, GenerateUnauthenticatedPacket);
+	RUN_TEST_CASE(packetProcessing, GenerateAuthenticatedPacket);
+	RUN_TEST_CASE(packetProcessing, OffsetCalculationPositiveOffset);
+	RUN_TEST_CASE(packetProcessing, OffsetCalculationNegativeOffset);
+	RUN_TEST_CASE(packetProcessing, HandleUnusableServer);
+	RUN_TEST_CASE(packetProcessing, HandleUnusablePacket);
+	RUN_TEST_CASE(packetProcessing, HandleServerAuthenticationFailure);
+	RUN_TEST_CASE(packetProcessing, HandleKodDemobilize);
+	RUN_TEST_CASE(packetProcessing, HandleKodRate);
+	RUN_TEST_CASE(packetProcessing, HandleCorrectPacket);
+}
+


=====================================
tests/ntpdig/utilities.c
=====================================
--- /dev/null
+++ b/tests/ntpdig/utilities.c
@@ -0,0 +1,190 @@
+#include "unity.h"
+#include "unity_fixture.h"
+
+TEST_GROUP(utilities);
+
+TEST_SETUP(utilities) {}
+
+TEST_TEAR_DOWN(utilities) {}
+
+#include "ntpdigtest.h"
+#include "file_handling.h"
+
+#include "main.h"
+#include "utilities.h"
+
+#include "sockaddrtest.h"
+
+const char * Version = "stub unit test Version string";
+
+
+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) {
+	FILE* fp_a;
+	FILE* fp_e;
+
+	if (outputFileOpened) {
+		fclose(outputFile);
+	}
+
+	fp_a = fopen(actual, "rb");
+	fp_e = fopen(expected, "rb");
+
+	CompareFileContent(fp_e, fp_a);
+
+	fclose(fp_a);
+	fclose(fp_e);
+
+	free((void*) expected);
+	free((void*) actual);
+}
+
+
+/*
+ * These tests are essentially a copy of the tests for socktoa()
+ * in libntp. If ntpdig switches to using that functions, these
+ * tests can be removed.
+ */
+
+TEST(utilities, IPv4Address) {
+	const char* ADDR = "192.0.2.10";
+
+	sockaddr_u input = CreateSockaddr4(ADDR, 123);
+	struct addrinfo inputA = CreateAddrinfo(&input);
+
+	TEST_ASSERT_EQUAL_STRING(ADDR, ss_to_str(&input));
+	TEST_ASSERT_EQUAL_STRING(ADDR, addrinfo_to_str(&inputA));
+}
+
+TEST(utilities, 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));
+}
+
+TEST(utilities, 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);
+}
+
+TEST(utilities, 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 */
+
+TEST(utilities, DebugPktOutput) {
+	const char *filename = 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);
+}
+
+TEST(utilities, DebugLfpOutputBinaryFormat) {
+	const char *filename = 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);
+}
+
+TEST(utilities, DebugLfpOutputDecimalFormat) {
+	const char *filename = 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);
+}
+
+TEST_GROUP_RUNNER(utilities) {
+	RUN_TEST_CASE(utilities, IPv4Address);
+	RUN_TEST_CASE(utilities, IPv6Address);
+	RUN_TEST_CASE(utilities, SetLiVnMode1);
+	RUN_TEST_CASE(utilities, SetLiVnMode2);
+	RUN_TEST_CASE(utilities, DebugPktOutput);
+	RUN_TEST_CASE(utilities, DebugLfpOutputBinaryFormat);
+	RUN_TEST_CASE(utilities, DebugLfpOutputDecimalFormat);
+}


=====================================
tests/wscript
=====================================
--- a/tests/wscript
+++ b/tests/wscript
@@ -18,6 +18,8 @@ def build(ctx):
 	# Test main.
 	common_source = [
 		"common/tests_main.c",
+		"common/caltime.c",
+		"common/sockaddrtest.c"
 	]
 
 
@@ -27,8 +29,9 @@ def build(ctx):
 		"ntpdig/keyFile.c",
 		"ntpdig/kodDatabase.c",
 		"ntpdig/kodFile.c",
-#		"ntpdig/networking.c",
 		"ntpdig/packetHandling.c",
+#		"ntpdig/packetProcessing.c",
+		"ntpdig/utilities.c",
 	] + common_source
 
 
@@ -53,26 +56,25 @@ def build(ctx):
 	libntp_source = [
 		"libntp/a_md5encrypt.c",
 		"libntp/authkeys.c",
-#		"libntp/calendar.c",
-#		"libntp/caljulian.c",
-#		"libntp/caltontp.c",
+		"libntp/calendar.c",
+		"libntp/caljulian.c",
+		"libntp/caltontp.c",
 		"libntp/calyearstart.c",
-#		"libntp/clocktime.c",
-#		"libntp/decodenetnum.c",
+		"libntp/clocktime.c",
+		"libntp/decodenetnum.c",
 		"libntp/hextolfp.c",
 #		"libntp/humandate.c",
 #		"libntp/lfpfunc.c",
 #		"libntp/lfptostr.c",
 		"libntp/modetoa.c",
 #		"libntp/msyslog.c",
-#		"libntp/netof.c",
+		"libntp/netof.c",
 		"libntp/numtoa.c",
 		"libntp/numtohost.c",
 		"libntp/prettydate.c",
 		"libntp/recvbuff.c",
-#		"libntp/refnumtoa.c",
 		"libntp/sfptostr.c",
-#		"libntp/socktoa.c",
+		"libntp/socktoa.c",
 		"libntp/ssl_init.c",
 		"libntp/statestr.c",
 		"libntp/strtolfp.c",
@@ -82,7 +84,10 @@ def build(ctx):
 		"libntp/tvtots.c",
 		"libntp/vi64ops.c",
 		"libntp/ymd2yd.c"
-	] + common_source + ["common/caltime.c"]
+	] + common_source
+
+	if ctx.env.REFCLOCK_ENABLE:
+		libntp_source += ["libntp/refnumtoa.c"]
 
 	ctx.ntp_test(
 		features    = "c cprogram bld_include src_include libisc_include test",



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/e108e3c68b024202dded95a96d0063b12b9f181e...c0ef5549f227759a99ebae150e7f0e97a263301c
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ntpsec.org/pipermail/vc/attachments/20151204/3fc21ef9/attachment.html>


More information about the vc mailing list