Debian Codegraph @

test/rist/unit/test_adv_ts_reconstruct.c

raw symbols SCIP
1 /* librist. SPDX-License-Identifier: BSD-2-Clause
2 *
3 * Unit test for rist_adv_ts_reconstruct() - the Advanced Profile receive-side
4 * source-clock reconstruction (src/rist-adv-ts.h). Verifies: immediate
5 * dejitter off the source clock (as the Main profile does), wrap-safe delta
6 * accumulation, retransmit positioning without state change, following a
7 * source discontinuity, and the first-window rate check that corrects a broken
8 * clock (an older 65536x-fast sender) back to arrival. Pure, link-free:
9 * exercises the header inline directly. */
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 /* NTP tick helpers (1 s = 2^32). All cast to uint64_t so printf args match
29 * PRIu64 exactly on every ABI. us matches the helper's exact 2^26/15625. */
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); /* rate-verification window */
37
38 /* Drive a conformant 1 MHz sender (wire us == elapsed us) through the window
39 * so the clock is verified and kept. Returns the seed time t0; afterwards
40 * last == 1000000 and accum ~= t0 + NTP_US(1000000). */
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 /* First live packet stamps arrival and anchors the clock. */
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 /* A retransmit arriving before any live packet has no reference: arrival. */
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 /* From the very first packets - inside the verification window - output tracks
81 * the source clock, not arrival jitter, just like the Main profile. */
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); /* seed */
87
88 /* Source +40 ms; arrival jittered to +55 ms. Still calibrating, but the
89 * output already follows the source clock. */
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 /* Once verified, in-order packets keep tracking the source clock. */
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 /* +40 ms of source; arrival jittered late to +55 ms. */
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 /* +40 ms more; arrival jittered early to +30 ms. */
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 /* A broken sender (pre-fix 65536x-fast timestamp) is used optimistically at
118 * first, so early output races ahead of arrival; the first-window rate check
119 * then rejects it and the peer corrects to arrival for the whole session. */
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); /* seed */
125
126 /* 20 ms spacing keeps each broken delta (20000*65536) under 2^31 so the
127 * signed delta stays positive; ~50 packets span the 1 s window. */
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) /* still calibrating: optimistic source races ahead */
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 /* Corrected for good: later packets and retries all use arrival. */
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 /* The 32-bit 1 MHz field wraps every ~71.6 min; a small forward delta across
151 * the wrap boundary must stay small on the verified source clock. */
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 /* Place the verified clock just below the 32-bit wrap. */
158 uint64_t src = t0 + NTP_SEC(10);
159 st.last = UINT32_MAX - 500; /* 500 us before wrap */
160 st.accum = src;
161
162 /* 1000 us later the wire value wraps past 0 to 499. */
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 /* A retransmit is positioned relative to the verified clock but must not move
170 * it: the next live packet continues from where the live stream left off. */
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 /* An older packet (source +1020 ms from t0) is recovered late. */
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 /* Next live packet continues from +1100 ms, not from the retry. */
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 /* Once verified, the source clock is trusted like the Main profile: a sender
195 * clock discontinuity is followed (reconstructed through), not snapped to
196 * arrival, which would inject worse jitter than the jump itself. */
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 /* A large forward step in the source timestamp (+500 s). */
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 }