[Git][NTPsec/ntpsec][master] Test megapack

Eric S. Raymond gitlab at mg.gitlab.com
Tue Jul 31 16:14:03 UTC 2018


Eric S. Raymond pushed to branch master at NTPsec / ntpsec


Commits:
34302c37 by Ian Bruene at 2018-07-31T16:13:46Z
Test megapack

- - - - -


5 changed files:

- + tests/libntp/ntp_endian.c
- tests/libntp/ntp_random.c
- tests/libntp/statestr.c
- tests/libntp/timespecops.c
- tests/wscript


Changes:

=====================================
tests/libntp/ntp_endian.c
=====================================
@@ -0,0 +1,36 @@
+#include "config.h"
+#include "ntp_stdlib.h"
+#include "ntp_endian.h"
+
+#include "unity.h"
+#include "unity_fixture.h"
+
+TEST_GROUP(endian);
+
+TEST_SETUP(endian) {}
+
+TEST_TEAR_DOWN(endian) {}
+
+TEST(endian, Bit16) {
+	uint8_t buffer[2] = {0x22, 0x11};
+
+	TEST_ASSERT_EQUAL(0x1122, ntp_be16dec((void *)buffer));
+}
+
+TEST(endian, Bit32) {
+	uint8_t buffer[4] = {0x44, 0x33, 0x22, 0x11};
+
+	TEST_ASSERT_EQUAL(0x11223344, ntp_be32dec((void *)buffer));
+}
+
+TEST(endian, Bit64) {
+	uint8_t buffer[8] = {0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11};
+
+	TEST_ASSERT_EQUAL(0x1122334455667788, ntp_be64dec((void *)buffer));
+}
+
+TEST_GROUP_RUNNER(endian) {
+	RUN_TEST_CASE(endian, Bit16);
+	RUN_TEST_CASE(endian, Bit32);
+	RUN_TEST_CASE(endian, Bit64);
+}


=====================================
tests/libntp/ntp_random.c
=====================================
@@ -32,6 +32,28 @@ TEST(random, random32) {
 	TEST_ASSERT_EQUAL_INT32(0, zeros);
 }
 
+TEST(random, random64) {
+	int i;
+	uint64_t ones = 0;
+	uint64_t zeros = ~0;
+
+	/* This is just a crude sanity check.
+	 * It could fail when working correctly,
+	 * but the chances are pretty small.
+	 * It won't be reproducable.  ;)
+	 * You can test this code by making the loop count smaller.
+	 */
+	for (i=0; i<99; i++) {
+		uint64_t sample = ntp_random64();
+		ones |= sample;
+		zeros &= sample; 
+	}
+
+	TEST_ASSERT_EQUAL_INT64(~0, ones);
+	TEST_ASSERT_EQUAL_INT64(0, zeros);
+}
+
 TEST_GROUP_RUNNER(random) {
 	RUN_TEST_CASE(random, random32);
+	RUN_TEST_CASE(random, random64);
 }


=====================================
tests/libntp/statestr.c
=====================================
@@ -4,6 +4,8 @@
 #include "unity.h"
 #include "unity_fixture.h"
 
+#include <sys/timex.h>
+
 TEST_GROUP(statestr);
 
 TEST_SETUP(statestr) {}
@@ -15,6 +17,31 @@ TEST_TEAR_DOWN(statestr) {}
 #include "ntp_control.h"
 
 
+// res_match_flags()
+TEST(statestr, ResMatchFlags) {
+	TEST_ASSERT_EQUAL_STRING("ntpport", res_match_flags(RESM_NTPONLY));
+}
+
+// res_access_flags()
+TEST(statestr, ResAccessFlags) {
+	TEST_ASSERT_EQUAL_STRING("notrust", res_access_flags(RES_DONTTRUST));
+}
+
+// k_st_flags()
+TEST(statestr, KSTFlags) {
+	TEST_ASSERT_EQUAL_STRING("ppsfreq", k_st_flags(STA_PPSFREQ));
+}
+
+// statustoa
+TEST(statestr, StatusToA) {
+	TEST_ASSERT_EQUAL_STRING("leap_add_sec, sync_22, 7 events, no_sys_peer",
+							 statustoa(TYPE_SYS, 0x12345678));
+	TEST_ASSERT_EQUAL_STRING("authenb, reach, sel_sys.peer, 7 events, access_denied",
+							 statustoa(TYPE_PEER, 0x12345678));
+	TEST_ASSERT_EQUAL_STRING("7 events, clk_8",
+							 statustoa(TYPE_CLOCK, 0x12345678));
+}
+
 // eventstr()
 TEST(statestr, PeerRestart) {
 	TEST_ASSERT_EQUAL_STRING("restart", eventstr(PEVNT_RESTART));
@@ -34,6 +61,10 @@ TEST(statestr, ClockCodeUnknown) {
 }
 
 TEST_GROUP_RUNNER(statestr) {
+	RUN_TEST_CASE(statestr, ResMatchFlags);
+	RUN_TEST_CASE(statestr, ResAccessFlags);
+	RUN_TEST_CASE(statestr, KSTFlags);
+	RUN_TEST_CASE(statestr, StatusToA);
 	RUN_TEST_CASE(statestr, PeerRestart);
 	RUN_TEST_CASE(statestr, SysUnspecified);
 	RUN_TEST_CASE(statestr, ClockCodeExists);


=====================================
tests/libntp/timespecops.c
=====================================
@@ -180,7 +180,7 @@ TEST(timespecops, SignNoFrac) {
 	for (i = -4; i <= 4; ++i) {
 		struct timespec a = timespec_init(i, 0);
 		int E = (i > 0) - (i < 0);
-		int r = test_tspec(a);
+		int r = test_tspec_denorm(a);
 
 		TEST_ASSERT_EQUAL(E, r);
 	}
@@ -196,7 +196,7 @@ TEST(timespecops, SignWithFrac) {
 	for (i = -4; i <= 4; ++i) {
 		struct timespec a = timespec_init(i, 10);
 		int E = (i >= 0) - (i < 0);
-		int r = test_tspec(a);
+		int r = test_tspec_denorm(a);
 
 		TEST_ASSERT_EQUAL(E, r);
 	}
@@ -630,6 +630,124 @@ TEST(timespecops, test_LFProundtrip) {
 	return;
 }
 
+//----------------------------------------------------------------------
+// conversion from l_fp, unsigned
+//----------------------------------------------------------------------
+
+TEST(timespecops, test_FromLFPuBittest) {
+	struct timespec limit = timespec_init(0, 2);
+
+	// Not *exactly* a bittest, because 2**32 tests would take a
+	// really long time even on very fast machines! So we do test
+	// every 1000 fractional units.
+	uint32_t tsf;
+	for (tsf = 0; tsf < ~((uint32_t)(1000)); tsf += 1000) {
+		struct timespec E = timespec_init(1, (long)my_tsf_to_tick(tsf));
+		l_fp a = lfpinit(1, tsf);
+		struct timespec r;
+
+		r = lfp_uintv_to_tspec(a);
+		// The conversion might be off by one nanosecond when
+		// comparing to calculated value.
+		TEST_ASSERT_TRUE(AssertTimespecClose(E, r, limit));
+	}
+
+	return;
+}
+
+
+TEST(timespecops, test_FromLFPuRelPos) {
+	struct timespec limit = timespec_init(0, 2);
+	int i;
+
+	for (i = 0; i < (int)COUNTOF(fdata); ++i) {
+		l_fp a = lfpinit(1, fdata[i].frac);
+		struct timespec E = timespec_init(1, fdata[i].nsec);
+		struct timespec r;
+
+		r = lfp_uintv_to_tspec(a);
+		TEST_ASSERT_TRUE(AssertTimespecClose(E, r, limit));
+	}
+
+	return;
+}
+
+
+TEST(timespecops, test_FromLFPuRelNeg) {
+	struct timespec limit = timespec_init(0, 2);
+	int i;
+
+	for (i = 0; i < (int)COUNTOF(fdata); ++i) {
+		l_fp a = lfpinit(~0, fdata[i].frac);
+		struct timespec E = timespec_init(-1, fdata[i].nsec);
+		struct timespec r;
+
+		r = lfp_uintv_to_tspec(a);
+		TEST_ASSERT_TRUE(AssertTimespecClose(E, r, limit));
+	}
+
+	return;
+}
+
+
+// nsec -> frac -> nsec roundtrip, using a prime start and increment
+TEST(timespecops, test_LFPuRoundtrip) {
+	int32_t t;
+	uint32_t i;
+
+	for (t = -1; t < 2; ++t)
+		for (i = 4999; i < 1000000000; i += 10007) {
+			struct timespec E = timespec_init(t, (long)i);
+			l_fp a;
+			struct timespec r;
+
+			a = tspec_intv_to_lfp(E);
+			r = lfp_uintv_to_tspec(a);
+			TEST_ASSERT_EQUAL_timespec(E, r);
+		}
+
+	return;
+}
+
+//----------------------------------------------------------------------
+// conversion from double
+//----------------------------------------------------------------------
+
+TEST(timespecops, DToTspec) {
+	struct timespec res;
+
+	res = d_to_tspec(42.25);
+	TEST_ASSERT_EQUAL(42, res.tv_sec);
+	TEST_ASSERT_EQUAL(250000000, res.tv_nsec);
+}
+
+//----------------------------------------------------------------------
+// conversion from lfp stamp
+//----------------------------------------------------------------------
+
+TEST(timespecops, LfpStampToTspec) {
+	struct timespec res;
+
+	res = lfp_stamp_to_tspec(86400, 100000);
+	TEST_ASSERT_EQUAL(2085978496, res.tv_sec);
+	TEST_ASSERT_EQUAL(20117, res.tv_nsec);
+}
+
+//----------------------------------------------------------------------
+// conversion from tval
+//----------------------------------------------------------------------
+
+TEST(timespecops, TvalToTspec) {
+	struct timespec res;
+	struct timeval in;
+
+	in.tv_sec = 42;
+	in.tv_usec = 23;
+	res = tval_to_tspec(in);
+	TEST_ASSERT_EQUAL(42, res.tv_sec);
+	TEST_ASSERT_EQUAL(23000, res.tv_nsec);
+}
+
 //----------------------------------------------------------------------
 // string formatting
 //----------------------------------------------------------------------
@@ -662,6 +780,13 @@ TEST_GROUP_RUNNER(timespecops) {
 	RUN_TEST_CASE(timespecops, test_FromLFPrelPos);
 	RUN_TEST_CASE(timespecops, test_FromLFPrelNeg);
 	RUN_TEST_CASE(timespecops, test_LFProundtrip);
+	RUN_TEST_CASE(timespecops, test_FromLFPuBittest);
+	RUN_TEST_CASE(timespecops, test_FromLFPuRelPos);
+	RUN_TEST_CASE(timespecops, test_FromLFPuRelNeg);
+	RUN_TEST_CASE(timespecops, test_LFPuRoundtrip);
+	RUN_TEST_CASE(timespecops, DToTspec);
+	RUN_TEST_CASE(timespecops, LfpStampToTspec);
+	RUN_TEST_CASE(timespecops, TvalToTspec);
 }
 
 


=====================================
tests/wscript
=====================================
@@ -35,6 +35,7 @@ def build(ctx):
     libntp_source = [
         "libntp/authkeys.c",
         "libntp/ntp_calendar.c",
+		"libntp/ntp_endian.c",
         "libntp/ntp_random.c",
         "libntp/clocktime.c",
         "libntp/decodenetnum.c",



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/34302c37cd6bd00a4e47c1a3ab930dd5db7a8acf

-- 
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/34302c37cd6bd00a4e47c1a3ab930dd5db7a8acf
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20180731/db6dfacf/attachment-0001.html>


More information about the vc mailing list