| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#include "rist-adv-ts.h" |
| 12 |
|
| 13 |
#include <inttypes.h> |
| 14 |
#include <stdio.h> |
| 15 |
|
| 16 |
static int failures = 0; |
| 17 |
|
| 18 |
#define CHECK(cond, ...) \ |
| 19 |
do { \ |
| 20 |
if (!(cond)) { \ |
| 21 |
fprintf(stderr, "FAIL %s:%d: " #cond, __FILE__, __LINE__); \ |
| 22 |
fprintf(stderr, " - "); fprintf(stderr, __VA_ARGS__); \ |
| 23 |
fprintf(stderr, "\n"); \ |
| 24 |
failures++; \ |
| 25 |
} \ |
| 26 |
} while (0) |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#define NTP_SEC(s) ((uint64_t)((uint64_t)(s) << 32)) |
| 31 |
#define NTP_MS(ms) ((uint64_t)(((uint64_t)(ms) << 32) / 1000)) |
| 32 |
#define NTP_US(us) ((uint64_t)(((uint64_t)(us) * 67108864ULL) / 15625ULL)) |
| 33 |
|
| 34 |
static uint64_t absdiff(uint64_t a, uint64_t b) { return a > b ? a - b : b - a; } |
| 35 |
|
| 36 |
static const uint64_t WINDOW = NTP_SEC(1); |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
static uint64_t seed_and_verify(struct rist_adv_ts_state *st, uint64_t t0) |
| 42 |
{ |
| 43 |
uint64_t out = rist_adv_ts_reconstruct(st, 0, t0, WINDOW, true); |
| 44 |
CHECK(out == t0, "first packet stamps arrival(seed): got %" PRIu64, out); |
| 45 |
for (uint32_t ms = 200; ms < 1000; ms += 200) { |
| 46 |
uint64_t now = t0 + NTP_MS(ms); |
| 47 |
out = rist_adv_ts_reconstruct(st, ms * 1000u, now, WINDOW, true); |
| 48 |
CHECK(absdiff(out, t0 + NTP_US(ms * 1000)) <= 3, |
| 49 |
"calibrating packet uses source clock at %ums", ms); |
| 50 |
CHECK(st->calibrating, "still calibrating before the window closes"); |
| 51 |
} |
| 52 |
uint64_t now = t0 + NTP_SEC(1); |
| 53 |
out = rist_adv_ts_reconstruct(st, 1000000u, now, WINDOW, true); |
| 54 |
CHECK(!st->calibrating, "verdict taken at window close"); |
| 55 |
CHECK(st->use_source, "conformant clock kept"); |
| 56 |
CHECK(absdiff(out, t0 + NTP_SEC(1)) <= 3, "source ~= +1s: got %" PRIu64, out); |
| 57 |
return t0; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
static void test_seed_is_arrival(void) |
| 62 |
{ |
| 63 |
struct rist_adv_ts_state st = {0}; |
| 64 |
uint64_t now = NTP_SEC(1000); |
| 65 |
uint64_t out = rist_adv_ts_reconstruct(&st, 500000, now, WINDOW, true); |
| 66 |
CHECK(out == now, "first packet must stamp arrival: got %" PRIu64, out); |
| 67 |
CHECK(st.seeded && st.calibrating, "clock anchored after first live packet"); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
static void test_retry_before_seed(void) |
| 72 |
{ |
| 73 |
struct rist_adv_ts_state st = {0}; |
| 74 |
uint64_t now = NTP_SEC(1000); |
| 75 |
uint64_t out = rist_adv_ts_reconstruct(&st, 12345, now, WINDOW, false); |
| 76 |
CHECK(out == now, "retry before seed -> arrival: got %" PRIu64, out); |
| 77 |
CHECK(!st.seeded, "retry must not anchor the clock"); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
static void test_source_used_immediately(void) |
| 83 |
{ |
| 84 |
struct rist_adv_ts_state st = {0}; |
| 85 |
uint64_t t0 = NTP_SEC(1000); |
| 86 |
rist_adv_ts_reconstruct(&st, 0, t0, WINDOW, true); |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
uint64_t out = rist_adv_ts_reconstruct(&st, 40000, t0 + NTP_MS(55), |
| 91 |
WINDOW, true); |
| 92 |
CHECK(absdiff(out, t0 + NTP_US(40000)) <= 3, |
| 93 |
"source used immediately: got %" PRIu64 " want ~%" PRIu64, |
| 94 |
out, t0 + NTP_US(40000)); |
| 95 |
CHECK(st.calibrating, "still within the verification window"); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
static void test_verified_tracks_source(void) |
| 100 |
{ |
| 101 |
struct rist_adv_ts_state st = {0}; |
| 102 |
uint64_t t0 = seed_and_verify(&st, NTP_SEC(1000)); |
| 103 |
|
| 104 |
|
| 105 |
uint64_t out = rist_adv_ts_reconstruct(&st, 1040000u, t0 + NTP_MS(1055), |
| 106 |
WINDOW, true); |
| 107 |
CHECK(absdiff(out, t0 + NTP_US(1040000)) <= 4, |
| 108 |
"tracks source not arrival: got %" PRIu64, out); |
| 109 |
|
| 110 |
|
| 111 |
out = rist_adv_ts_reconstruct(&st, 1080000u, t0 + NTP_MS(1085), |
| 112 |
WINDOW, true); |
| 113 |
CHECK(absdiff(out, t0 + NTP_US(1080000)) <= 5, |
| 114 |
"stays on source clock: got %" PRIu64, out); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
static void test_broken_sender_corrected(void) |
| 121 |
{ |
| 122 |
struct rist_adv_ts_state st = {0}; |
| 123 |
uint64_t t0 = NTP_SEC(1000); |
| 124 |
rist_adv_ts_reconstruct(&st, 0, t0, WINDOW, true); |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
uint32_t wire = 0; |
| 129 |
uint64_t now = t0; |
| 130 |
for (int i = 1; i <= 51; i++) { |
| 131 |
wire += 20000u * 65536u; |
| 132 |
now = t0 + NTP_MS(20 * i); |
| 133 |
uint64_t out = rist_adv_ts_reconstruct(&st, wire, now, WINDOW, true); |
| 134 |
if (i < 50) |
| 135 |
CHECK(out > now + NTP_SEC(1), |
| 136 |
"broken clock used optimistically before verdict: got %" PRIu64, |
| 137 |
out); |
| 138 |
} |
| 139 |
CHECK(!st.calibrating, "verdict taken"); |
| 140 |
CHECK(!st.use_source, "broken clock rejected"); |
| 141 |
|
| 142 |
|
| 143 |
uint64_t later = t0 + NTP_SEC(5); |
| 144 |
CHECK(rist_adv_ts_reconstruct(&st, wire + 50000u, later, WINDOW, true) == later, |
| 145 |
"corrected: live packet uses arrival"); |
| 146 |
CHECK(rist_adv_ts_reconstruct(&st, 12345, later, WINDOW, false) == later, |
| 147 |
"corrected: retry uses arrival"); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
static void test_wrap(void) |
| 153 |
{ |
| 154 |
struct rist_adv_ts_state st = {0}; |
| 155 |
uint64_t t0 = seed_and_verify(&st, NTP_SEC(1000)); |
| 156 |
|
| 157 |
|
| 158 |
uint64_t src = t0 + NTP_SEC(10); |
| 159 |
st.last = UINT32_MAX - 500; |
| 160 |
st.accum = src; |
| 161 |
|
| 162 |
|
| 163 |
uint64_t out = rist_adv_ts_reconstruct(&st, 499, src + NTP_US(1000), |
| 164 |
WINDOW, true); |
| 165 |
CHECK(absdiff(out, src + NTP_US(1000)) <= 2, |
| 166 |
"wrap yields +1000us, not a ~71min jump: got %" PRIu64, out); |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
static void test_retry_positions_without_advancing(void) |
| 172 |
{ |
| 173 |
struct rist_adv_ts_state st = {0}; |
| 174 |
uint64_t t0 = seed_and_verify(&st, NTP_SEC(1000)); |
| 175 |
rist_adv_ts_reconstruct(&st, 1100000u, t0 + NTP_MS(1100), WINDOW, true); |
| 176 |
uint32_t last_before = st.last; |
| 177 |
uint64_t accum_before = st.accum; |
| 178 |
|
| 179 |
|
| 180 |
uint64_t r = rist_adv_ts_reconstruct(&st, 1020000u, t0 + NTP_MS(1250), |
| 181 |
WINDOW, false); |
| 182 |
CHECK(absdiff(r, t0 + NTP_US(1020000)) <= 4, |
| 183 |
"retry stamped at its own source time: got %" PRIu64, r); |
| 184 |
CHECK(st.last == last_before && st.accum == accum_before, |
| 185 |
"retry must not mutate the clock"); |
| 186 |
|
| 187 |
|
| 188 |
uint64_t out = rist_adv_ts_reconstruct(&st, 1140000u, t0 + NTP_MS(1300), |
| 189 |
WINDOW, true); |
| 190 |
CHECK(absdiff(out, t0 + NTP_US(1140000)) <= 4, |
| 191 |
"live stream resumes after retry: got %" PRIu64, out); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
static void test_discontinuity_follows_source(void) |
| 198 |
{ |
| 199 |
struct rist_adv_ts_state st = {0}; |
| 200 |
uint64_t t0 = seed_and_verify(&st, NTP_SEC(1000)); |
| 201 |
|
| 202 |
|
| 203 |
uint64_t now = t0 + NTP_MS(1080); |
| 204 |
uint64_t out = rist_adv_ts_reconstruct(&st, 1000000u + 500000000u, now, WINDOW, true); |
| 205 |
CHECK(out != now, "discontinuity is followed, not snapped to arrival: got %" PRIu64, out); |
| 206 |
CHECK(out > now + NTP_SEC(100), |
| 207 |
"output jumps forward with the source clock: got %" PRIu64, out); |
| 208 |
CHECK(st.use_source, "source clock stays in use through a discontinuity"); |
| 209 |
} |
| 210 |
|
| 211 |
int main(void) |
| 212 |
{ |
| 213 |
test_seed_is_arrival(); |
| 214 |
test_retry_before_seed(); |
| 215 |
test_source_used_immediately(); |
| 216 |
test_verified_tracks_source(); |
| 217 |
test_broken_sender_corrected(); |
| 218 |
test_wrap(); |
| 219 |
test_retry_positions_without_advancing(); |
| 220 |
test_discontinuity_follows_source(); |
| 221 |
|
| 222 |
if (failures) { |
| 223 |
fprintf(stderr, "adv_ts_reconstruct: %d check(s) failed\n", failures); |
| 224 |
return 1; |
| 225 |
} |
| 226 |
printf("adv_ts_reconstruct: all checks passed\n"); |
| 227 |
return 0; |
| 228 |
} |