Debian Codegraph @

test/rist/unit/test_url_cbr_output.c

raw symbols SCIP
1 /* librist. SPDX-License-Identifier: BSD-2-Clause
2 *
3 * ?cbr-output= parsing on output URLs and rist_receiver_set_cbr_output().
4 * Pacing happens in the flow's output loop ahead of the fan-out, so the setting
5 * belongs to the context: whichever route claims it first wins, and a later
6 * disagreement from either route is refused rather than resolved.
7 * Intentionally avoids cmocka so it runs anywhere the public library does. */
8
9 #include "librist/librist.h"
10 #include "librist/urlparam.h"
11 #include "librist/receiver.h"
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16
17 static int expect_out_parsed(const char *url, int want_set, int want_val)
18 {
19 struct rist_udp_config *cfg = NULL;
20 int fails = 0;
21 if (rist_parse_udp_address2(url, &cfg) != 0 || !cfg) {
22 fprintf(stderr, "FAIL: could not parse output url %s\n", url);
23 if (cfg) rist_udp_config_free2(&cfg);
24 return 1;
25 }
26 if (cfg->cbr_output_set != want_set || cfg->cbr_output != want_val) {
27 fprintf(stderr, "FAIL: %s -> set=%d val=%d (want %d/%d)\n",
28 url, cfg->cbr_output_set, cfg->cbr_output, want_set, want_val);
29 fails++;
30 }
31 rist_udp_config_free2(&cfg);
32 return fails;
33 }
34
35 /* The output URL is where the parameter belongs, since it governs output. */
36 static int test_output_url_parsing(void)
37 {
38 int fails = 0;
39 fails += expect_out_parsed("udp://127.0.0.1:31100", 0, 0);
40 fails += expect_out_parsed("udp://127.0.0.1:31100?cbr-output=1", 1, 1);
41 fails += expect_out_parsed("udp://127.0.0.1:31100?cbr-output=0", 1, 0);
42
43 struct rist_udp_config *cfg = NULL;
44 if (rist_parse_udp_address2("udp://127.0.0.1:31100?cbr-output=2", &cfg) == 0) {
45 fprintf(stderr, "FAIL: output url cbr-output=2 accepted\n");
46 fails++;
47 }
48 if (cfg) rist_udp_config_free2(&cfg);
49
50 if (!fails)
51 printf(" output urls: 0|1 accepted, absent stays unset, junk rejected\n");
52 return fails;
53 }
54
55 /* What ristreceiver does with each -o url: parse it, and if it stated a value,
56 * hand that to the API. Returns the API result, or -2 if the url did not ask. */
57 static int apply_out_url(struct rist_ctx *ctx, const char *url)
58 {
59 struct rist_udp_config *cfg = NULL;
60 if (rist_parse_udp_address2(url, &cfg) != 0 || !cfg) {
61 fprintf(stderr, "could not parse output url %s\n", url);
62 if (cfg) rist_udp_config_free2(&cfg);
63 return -3;
64 }
65 int ret = -2;
66 if (cfg->version >= 2 && cfg->cbr_output_set)
67 ret = rist_receiver_set_cbr_output(ctx, cfg->cbr_output != 0);
68 rist_udp_config_free2(&cfg);
69 return ret;
70 }
71
72 /* Outputs agreeing is fine; the setting is context-wide. */
73 static int test_two_agreeing_output_urls(struct rist_logging_settings *logs)
74 {
75 struct rist_ctx *ctx = NULL;
76 if (rist_receiver_create(&ctx, RIST_PROFILE_MAIN, logs) != 0) {
77 fprintf(stderr, "agreeing: rist_receiver_create failed\n");
78 return 1;
79 }
80 int fails = 0;
81 if (apply_out_url(ctx, "udp://127.0.0.1:31200?cbr-output=1") != 0 ||
82 apply_out_url(ctx, "udp://127.0.0.1:31201?cbr-output=1") != 0) {
83 fprintf(stderr, "agreeing: two outputs asking for 1 were not accepted\n");
84 fails++;
85 } else {
86 printf(" two outputs both asking for cbr-output=1 accepted\n");
87 }
88 rist_destroy(ctx);
89 return fails;
90 }
91
92 /* first then second on one context: the second must be refused. */
93 static int test_conflicting_output_urls(struct rist_logging_settings *logs,
94 int first, int second)
95 {
96 struct rist_ctx *ctx = NULL;
97 if (rist_receiver_create(&ctx, RIST_PROFILE_MAIN, logs) != 0) {
98 fprintf(stderr, "conflict: rist_receiver_create failed\n");
99 return 1;
100 }
101 char u1[96], u2[96];
102 snprintf(u1, sizeof(u1), "udp://127.0.0.1:31210?cbr-output=%d", first);
103 snprintf(u2, sizeof(u2), "udp://127.0.0.1:31211?cbr-output=%d", second);
104
105 int fails = 0;
106 if (apply_out_url(ctx, u1) != 0) {
107 fprintf(stderr, "conflict: first output (%s) rejected\n", u1);
108 fails++;
109 } else if (apply_out_url(ctx, u2) == 0) {
110 fprintf(stderr, "conflict: %s accepted after %s; expected refusal\n", u2, u1);
111 fails++;
112 } else {
113 printf(" output cbr-output=%d after cbr-output=%d refused\n", second, first);
114 }
115 rist_destroy(ctx);
116 return fails;
117 }
118
119 /* The API is the route a run script or provisioning layer uses, and it shares
120 * the latch with the output-URL route. */
121 static int test_api_and_url_share_the_latch(struct rist_logging_settings *logs)
122 {
123 struct rist_ctx *ctx = NULL;
124 if (rist_receiver_create(&ctx, RIST_PROFILE_MAIN, logs) != 0) {
125 fprintf(stderr, "api: rist_receiver_create failed\n");
126 return 1;
127 }
128 int fails = 0;
129
130 if (rist_receiver_set_cbr_output(ctx, true) != 0) {
131 fprintf(stderr, "api: first call rejected\n");
132 fails++;
133 }
134 if (rist_receiver_set_cbr_output(ctx, true) != 0) {
135 fprintf(stderr, "api: agreeing repeat rejected\n");
136 fails++;
137 }
138 if (rist_receiver_set_cbr_output(ctx, false) == 0) {
139 fprintf(stderr, "api: disagreeing call accepted; expected refusal\n");
140 fails++;
141 }
142
143 /* An output URL disagreeing with the API must lose too. */
144 if (apply_out_url(ctx, "udp://127.0.0.1:31202?cbr-output=0") == 0) {
145 fprintf(stderr, "api: output url disagreeing with the API accepted\n");
146 fails++;
147 }
148
149 if (!fails)
150 printf(" api latches, agrees with itself, and refuses a conflicting url\n");
151 rist_destroy(ctx);
152 return fails;
153 }
154
155 /* A sender context has no output loop to pace, so the call must not succeed. */
156 static int test_api_rejects_a_sender(struct rist_logging_settings *logs)
157 {
158 struct rist_ctx *ctx = NULL;
159 if (rist_sender_create(&ctx, RIST_PROFILE_MAIN, 0, logs) != 0) {
160 fprintf(stderr, "sender: rist_sender_create failed\n");
161 return 1;
162 }
163 int fails = 0;
164 if (rist_receiver_set_cbr_output(ctx, true) == 0) {
165 fprintf(stderr, "sender: accepted on a sender context\n");
166 fails++;
167 } else {
168 printf(" api refused on a sender context\n");
169 }
170 rist_destroy(ctx);
171 return fails;
172 }
173
174 int main(void)
175 {
176 struct rist_logging_settings *logs = NULL;
177 if (rist_logging_set(&logs, RIST_LOG_ERROR, NULL, NULL, NULL, stderr) != 0) {
178 fprintf(stderr, "rist_logging_set failed\n");
179 return 1;
180 }
181
182 printf("?cbr-output= parsing and override semantics\n");
183 int fails = 0;
184 fails += test_output_url_parsing();
185 fails += test_api_and_url_share_the_latch(logs);
186 fails += test_api_rejects_a_sender(logs);
187 fails += test_two_agreeing_output_urls(logs);
188 fails += test_conflicting_output_urls(logs, 1, 0);
189 /* 0 equals the default, so it must still claim the setting. */
190 fails += test_conflicting_output_urls(logs, 0, 1);
191
192 free(logs);
193 if (fails) {
194 fprintf(stderr, "%d failure(s)\n", fails);
195 return 1;
196 }
197 printf("all cbr-output invariants held\n");
198 return 0;
199 }