Debian Codegraph @

test/rist/unit/test_url_rtt_drop_parse.c

raw symbols SCIP
1 /* librist. SPDX-License-Identifier: BSD-2-Clause
2 *
3 * ?rtt-drop= / ?rtt-restore= / ?rtt-drop-settle= / ?rtt-drop-trickle= URL
4 * parsing exercised through the public rist_parse_address2 API. Intentionally
5 * avoids cmocka so it runs anywhere the public library does. */
6
7 #include "librist/librist.h"
8 #include "librist/peer.h"
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13
14 struct want {
15 uint32_t drop;
16 uint32_t restore;
17 uint32_t settle;
18 uint32_t trickle;
19 };
20
21 static int expect_cfg(const char *url, struct want w)
22 {
23 struct rist_peer_config *cfg = NULL;
24 int ret = rist_parse_address2(url, &cfg);
25 if (ret != 0 || cfg == NULL) {
26 fprintf(stderr, "FAIL: rist_parse_address2(%s) ret=%d cfg=%p\n",
27 url, ret, (void *)cfg);
28 if (cfg)
29 rist_peer_config_free2(&cfg);
30 return 1;
31 }
32 int bad = cfg->rtt_drop != w.drop || cfg->rtt_restore != w.restore
33 || cfg->rtt_drop_settle != w.settle || cfg->rtt_drop_trickle != w.trickle;
34 if (bad) {
35 fprintf(stderr,
36 "FAIL: %s -> drop=%u restore=%u settle=%u trickle=%u "
37 "(want %u/%u/%u/%u)\n",
38 url, cfg->rtt_drop, cfg->rtt_restore, cfg->rtt_drop_settle,
39 cfg->rtt_drop_trickle, w.drop, w.restore, w.settle, w.trickle);
40 }
41 rist_peer_config_free2(&cfg);
42 return bad ? 1 : 0;
43 }
44
45 int main(void)
46 {
47 int failures = 0;
48
49 /* Absent parameters keep the versioned defaults (feature disabled). */
50 failures += expect_cfg("rist://@127.0.0.1:1234",
51 (struct want){ RIST_DEFAULT_RTT_DROP, RIST_DEFAULT_RTT_RESTORE,
52 RIST_DEFAULT_RTT_DROP_SETTLE, RIST_DEFAULT_RTT_DROP_TRICKLE });
53
54 /* Each knob parses independently, leaving the rest at their defaults. */
55 failures += expect_cfg("rist://@127.0.0.1:1234?rtt-drop=500",
56 (struct want){ 500, RIST_DEFAULT_RTT_RESTORE,
57 RIST_DEFAULT_RTT_DROP_SETTLE, RIST_DEFAULT_RTT_DROP_TRICKLE });
58 failures += expect_cfg("rist://@127.0.0.1:1234?rtt-restore=300",
59 (struct want){ RIST_DEFAULT_RTT_DROP, 300,
60 RIST_DEFAULT_RTT_DROP_SETTLE, RIST_DEFAULT_RTT_DROP_TRICKLE });
61 failures += expect_cfg("rist://@127.0.0.1:1234?rtt-drop-settle=5000",
62 (struct want){ RIST_DEFAULT_RTT_DROP, RIST_DEFAULT_RTT_RESTORE,
63 5000, RIST_DEFAULT_RTT_DROP_TRICKLE });
64 failures += expect_cfg("rist://@127.0.0.1:1234?rtt-drop-trickle=50",
65 (struct want){ RIST_DEFAULT_RTT_DROP, RIST_DEFAULT_RTT_RESTORE,
66 RIST_DEFAULT_RTT_DROP_SETTLE, 50 });
67
68 /* All four together, order-independent, coexisting with other params. */
69 failures += expect_cfg(
70 "rist://@127.0.0.1:1234?weight=5&rtt-drop=500&rtt-restore=350&rtt-drop-settle=2000&rtt-drop-trickle=40",
71 (struct want){ 500, 350, 2000, 40 });
72
73 /* Negative values are rejected by the >=0 guards and keep the defaults. */
74 failures += expect_cfg("rist://@127.0.0.1:1234?rtt-drop=-1",
75 (struct want){ RIST_DEFAULT_RTT_DROP, RIST_DEFAULT_RTT_RESTORE,
76 RIST_DEFAULT_RTT_DROP_SETTLE, RIST_DEFAULT_RTT_DROP_TRICKLE });
77
78 if (failures == 0) {
79 printf("OK\n");
80 return 0;
81 }
82 fprintf(stderr, "Total failures: %d\n", failures);
83 return 1;
84 }