[Git][NTPsec/ntpsec][working] 6 commits: Test megapack
Ian Bruene
gitlab at mg.gitlab.com
Tue Jul 31 18:39:30 UTC 2018
Ian Bruene pushed to branch working at NTPsec / ntpsec
Commits:
34302c37 by Ian Bruene at 2018-07-31T16:13:46Z
Test megapack
- - - - -
6044ef91 by James Browning at 2018-07-31T16:28:36Z
ntpfrob array fix
- - - - -
80913de7 by James Browning at 2018-07-31T16:28:36Z
fix ntpfrob -c sorting
- - - - -
25bd9aab by James Browning at 2018-07-31T16:28:36Z
add variance as jitter
- - - - -
9641b483 by James Browning at 2018-07-31T17:40:02Z
log rotation bugfix
- - - - -
7ba5fc8b by Ian Bruene at 2018-07-31T18:38:51Z
Fixed incorrect #undef.
- - - - -
8 changed files:
- etc/logrotate-config.ntpd
- libntp/dolfptoa.c
- ntpfrob/jitter.c
- + tests/libntp/ntp_endian.c
- tests/libntp/ntp_random.c
- tests/libntp/statestr.c
- tests/libntp/timespecops.c
- tests/wscript
Changes:
=====================================
etc/logrotate-config.ntpd
=====================================
@@ -3,11 +3,11 @@
# That will rotate ntpd.log monthly and then
# kick ntpd to switch to the new log file.
-/var/log/ntp/ntpd.log {
+/var/log/ntpd.log {
monthly
postrotate
/usr/bin/killall -HUP ntpd
endscript
- rotate 9999
+ rotate 6
}
=====================================
libntp/dolfptoa.c
=====================================
@@ -104,7 +104,7 @@ dolfptoa(
M_LSHIFT(digit, fpv);
M_ADD(digit, fpv, tmph, tmpl);
#undef M_ADD
-#undef M_SHIFT
+#undef M_LSHIFT
*cpend++ = (uint8_t)digit;
}
=====================================
ntpfrob/jitter.c
=====================================
@@ -62,9 +62,12 @@ get_clocktime(
setlfpfrac(*now, (uint32_t)dtemp);
}
-static int doublecmp(const void *a, const void *b)
+// modified from https://stackoverflow.com/questions/11931547/qsort-does-not-work-for-double-array
+static int doublecmp(const void * a, const void * b)
{
- return (int)(*((const double *)a) - *((const double *)b));
+ if (*(const double*)a > *(const double*)b) return -1;
+ else if (*(const double*)a < *(const double*)b) return 1;
+ else return 0;
}
void jitter(const iomode mode)
@@ -72,6 +75,7 @@ void jitter(const iomode mode)
l_fp tr;
int i;
double gtod[NBUF];
+ doubletime_t jitter;
/*
* Force pages into memory
@@ -91,7 +95,7 @@ void jitter(const iomode mode)
* Write out gtod array for later processing with Matlab
*/
average = 0;
- for (i = 0; i < NBUF - 2; i++) {
+ for (i = 0; i < (NBUF - 1); i++) {
gtod[i] = gtod[i + 1] - gtod[i];
if (mode == raw)
printf("%13.9f\n", gtod[i]);
@@ -101,33 +105,45 @@ void jitter(const iomode mode)
if (mode == raw)
exit(0);
+ average = average / (NBUF - 1);
+
+ // calculate 'variance' and call it jitter
+ // and scale everything up a million time for clarity
+ jitter = 0;
+ for (i = 0; i < (NBUF - 1); i ++) {
+ gtod[i] = (gtod[i] - average) * 1000000;
+ jitter += gtod[i] * gtod[i];
+ }
+ jitter = jitter / (NBUF - 1);
+
/*
* Sort the gtod array and display deciles
*/
- qsort(gtod, NBUF, sizeof(gtod[0]), doublecmp);
- average = average / (NBUF - 2);
+ qsort(gtod, (NBUF - 1), sizeof(gtod[0]), doublecmp);
+
if (mode == json) {
- fprintf(stdout, "{\"Average\": %.9Lf, \"First rank\": [", average);
+ fprintf(stdout, "{\"Mean\": %.9Lf, \"High\": [", average);
for (i = 0; i < NSAMPLES; i++) {
fprintf(stdout, "%.9f", gtod[i]);
if (i < NSAMPLES - 1)
fputs(", ", stdout);
}
- fputs("], \"Last rank\": [", stdout);
+ fputs("], \"Low\": [", stdout);
for (i = NBUF - NSAMPLES - 2; i < NBUF - 2; i++) {
fprintf(stdout, "%.9f", gtod[i]);
if (i < NBUF - 3)
fputs(", ", stdout);
}
- fputs("]}\n", stdout);
+ fprintf(stdout, "], \"Jitter\": %.9Lf}\n", jitter);
}
else if (mode != raw)
{
- fprintf(stdout, "Average %13.9Lf\n", average);
- fprintf(stdout, "First rank\n");
+ fprintf(stdout, "Mean %13.9Lf\n", average);
+ fprintf(stdout, "Jitter %13.9Lf\n", jitter);
+ fprintf(stdout, "High\n");
for (i = 0; i < NSAMPLES; i++)
fprintf(stdout, "%2d %13.9f\n", i, gtod[i]);
- fprintf(stdout, "Last rank\n");
+ fprintf(stdout, "Low\n");
for (i = NBUF - NSAMPLES - 2; i < NBUF - 2; i++)
fprintf(stdout, "%2d %13.9f\n", i, gtod[i]);
}
=====================================
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/compare/fe05592ee4e6e1bbc962b16bfeb72debf5b085fa...7ba5fc8bbf842fbdab5ded23120d044ebd6958a9
--
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/fe05592ee4e6e1bbc962b16bfeb72debf5b085fa...7ba5fc8bbf842fbdab5ded23120d044ebd6958a9
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/22972d8d/attachment-0001.html>
More information about the vc
mailing list