Debian Codegraph @

test/rist/unit/test_recovery_maxbitrate_zero.c

raw symbols SCIP
1 /* librist. SPDX-License-Identifier: BSD-2-Clause
2 *
3 * A recovery_maxbitrate of 0 through the C API must be read as "unset" and
4 * replaced with the default, not as "no ceiling" (#229). Driven through the
5 * public API, asserting on the log. */
6
7 #include "librist/librist.h"
8 #include "librist/logging.h"
9 #include "librist/peer.h"
10 #include "librist/receiver.h"
11 #include "librist/sender.h"
12
13 #include <stdbool.h>
14 #include <stdio.h>
15 #include <string.h>
16
17 /* Room for every line a peer emits at INFO; overflow is dropped rather than
18 * truncating mid-assertion. */
19 static char captured[16384];
20 static size_t captured_len;
21
22 static int capture(void *arg, enum rist_log_level level, const char *msg)
23 {
24 (void)arg;
25 (void)level;
26 if (!msg)
27 return 0;
28 size_t len = strlen(msg);
29 if (len < sizeof(captured) - captured_len) {
30 memcpy(captured + captured_len, msg, len);
31 captured_len += len;
32 captured[captured_len] = '\0';
33 }
34 return 0;
35 }
36
37 static void capture_reset(void)
38 {
39 captured[0] = '\0';
40 captured_len = 0;
41 }
42
43 static int failures;
44
45 static void expect(const char *name, const char *needle, bool want)
46 {
47 bool got = strstr(captured, needle) != NULL;
48 if (got != want) {
49 fprintf(stderr, "FAIL: %s: %s \"%s\"\n", name,
50 want ? "expected but did not find" : "unexpectedly found",
51 needle);
52 failures++;
53 }
54 }
55
56 /* Creates one peer from url on a fresh context of the given role, leaving the
57 * log of that peer's creation in captured. maxbitrate is written into the
58 * parsed config the way an application using the C API would. */
59 static int make_peer(struct rist_logging_settings *logs, bool receiver,
60 const char *url, uint32_t maxbitrate)
61 {
62 struct rist_ctx *ctx = NULL;
63 int ret = receiver ? rist_receiver_create(&ctx, RIST_PROFILE_MAIN, logs)
64 : rist_sender_create(&ctx, RIST_PROFILE_MAIN, 0, logs);
65 if (ret != 0) {
66 fprintf(stderr, "rist_%s_create failed\n", receiver ? "receiver" : "sender");
67 return -1;
68 }
69
70 struct rist_peer_config *cfg = NULL;
71 struct rist_peer *peer = NULL;
72 int fails = 0;
73
74 if (rist_parse_address2(url, &cfg) != 0 || cfg == NULL) {
75 fprintf(stderr, "rist_parse_address2(%s) failed\n", url);
76 fails = -1;
77 goto cleanup;
78 }
79 cfg->recovery_maxbitrate = maxbitrate;
80
81 capture_reset();
82 if (rist_peer_create(ctx, &peer, cfg) != 0) {
83 fprintf(stderr, "rist_peer_create(%s, maxbitrate=%u) failed\n", url, maxbitrate);
84 fails = -1;
85 }
86
87 cleanup:
88 if (cfg)
89 rist_peer_config_free2(&cfg);
90 rist_destroy(ctx);
91 return fails;
92 }
93
94 int main(void)
95 {
96 struct rist_logging_settings *logs = NULL;
97 /* INFO, and no stream, so the receiver's config line is captured and the
98 * test stays quiet on success. */
99 if (rist_logging_set(&logs, RIST_LOG_INFO, capture, NULL, NULL, NULL) != 0) {
100 fprintf(stderr, "rist_logging_set failed\n");
101 return 1;
102 }
103
104 /* A zero is corrected, and says so rather than silently. */
105 if (make_peer(logs, false, "rist://127.0.0.1:30200", 0) < 0)
106 failures++;
107 else {
108 expect("sender_zero", "recovery-maxbitrate of 0", true);
109 expect("sender_zero", "100000 kbps default", true);
110 }
111
112 /* A real ceiling is left alone. */
113 if (make_peer(logs, false, "rist://127.0.0.1:30202", 8000) < 0)
114 failures++;
115 else
116 expect("sender_nonzero", "recovery-maxbitrate of 0", false);
117
118 /* The correction lands before the derived settings are computed, not just
119 * in the warning: the receiver's own config line reports the default. */
120 if (make_peer(logs, true, "rist://@127.0.0.1:30204", 0) < 0)
121 failures++;
122 else {
123 expect("receiver_zero", "recovery-maxbitrate of 0", true);
124 expect("receiver_zero", "maxrate=100000/", true);
125 }
126
127 rist_logging_settings_free2(&logs);
128
129 if (failures == 0) {
130 printf("OK\n");
131 return 0;
132 }
133 fprintf(stderr, "Total failures: %d\n", failures);
134 return 1;
135 }