[Git][NTPsec/ntpsec][test_megapack] 8 commits: Added test for refid_str.

Ian Bruene gitlab at mg.gitlab.com
Mon Jul 30 23:38:25 UTC 2018


Ian Bruene pushed to branch test_megapack at NTPsec / ntpsec


Commits:
ed5d672c by Ian Bruene at 2018-07-30T23:26:55Z
Added test for refid_str.

- - - - -
591dc944 by Ian Bruene at 2018-07-30T23:36:12Z
Added tests for statestr.c.

- - - - -
cd1e1587 by Ian Bruene at 2018-07-30T23:36:12Z
Added tests for timespecops.c.

- - - - -
3df2cce7 by Ian Bruene at 2018-07-30T23:36:19Z
Added tests for ntp_endian.c.

- - - - -
c86d5434 by Ian Bruene at 2018-07-30T23:36:19Z
Fixed incorrect array size and missing #include.

- - - - -
c419ffad by Ian Bruene at 2018-07-30T23:36:19Z
Revert "Fixed incorrect array size and missing #include."

This reverts commit 50314ff2b12dca3ba5a47e64103ec6030169adab.

- - - - -
d1d03a7b by Ian Bruene at 2018-07-30T23:36:19Z
Fixed incorrect array size and missing #include.

- - - - -
0ed880ea by Ian Bruene at 2018-07-30T23:36:19Z
Added tests for ntp_random.c.

- - - - -


6 changed files:

- + tests/libntp/ntp_endian.c
- tests/libntp/ntp_random.c
- tests/libntp/numtoa.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/numtoa.c
=====================================
@@ -25,7 +25,19 @@ TEST(numtoa, Netmask) {
 	TEST_ASSERT_EQUAL_STRING("255.255.255.0", numtoa(input));
 }
 
+TEST(numtoa, RefidStr) {
+	const char *res;
+
+	// Test stratum > 1
+	res = refid_str(0x11223344, 8);
+	TEST_ASSERT_EQUAL_STRING("68.51.34.17", res);
+	// Test !(stratum > 1)
+	res = refid_str(0x535047, 0);
+	TEST_ASSERT_EQUAL_STRING(".GPS.", res);
+}
+
 TEST_GROUP_RUNNER(numtoa) {
 	RUN_TEST_CASE(numtoa, Address);
 	RUN_TEST_CASE(numtoa, Netmask);
+	RUN_TEST_CASE(numtoa, RefidStr);
 }


=====================================
tests/libntp/statestr.c
=====================================
@@ -15,6 +15,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("pll", k_st_flags(STA_PLL));
+}
+
+// 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 +59,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/compare/ea11c45417a0ca98405ac2f4eed7d862c0f88f79...0ed880ea7690bb026ba82af4f51f1f19962d2708

-- 
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/ea11c45417a0ca98405ac2f4eed7d862c0f88f79...0ed880ea7690bb026ba82af4f51f1f19962d2708
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/20180730/946bb2fd/attachment-0001.html>


More information about the vc mailing list