Debian Codegraph @

test/rist/unit/test_url_recovery_depth_parse.c

raw symbols SCIP
1 /* librist. SPDX-License-Identifier: BSD-2-Clause
2 *
3 * ?recovery-depth= URL parsing exercised through the public
4 * rist_parse_address2 API. recovery-depth is a numeric exponent
5 * (RIST_RECOVERY_DEPTH_MIN..MAX); the ring holds 65536 << depth packets.
6 * Intentionally avoids cmocka so it runs anywhere the public library does. */
7
8 #include "librist/librist.h"
9 #include "librist/peer.h"
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 static int expect_depth(const char *url, unsigned want)
16 {
17 struct rist_peer_config *cfg = NULL;
18 int ret = rist_parse_address2(url, &cfg);
19 if (ret != 0 || cfg == NULL) {
20 fprintf(stderr, "FAIL: rist_parse_address2(%s) ret=%d cfg=%p\n",
21 url, ret, (void *)cfg);
22 if (cfg)
23 rist_peer_config_free2(&cfg);
24 return 1;
25 }
26 if (cfg->recovery_depth != want) {
27 fprintf(stderr, "FAIL: %s -> recovery_depth=%u (want %u)\n",
28 url, (unsigned)cfg->recovery_depth, want);
29 rist_peer_config_free2(&cfg);
30 return 1;
31 }
32 rist_peer_config_free2(&cfg);
33 return 0;
34 }
35
36 /* An out-of-range / non-numeric token must be flagged (non-zero return) per
37 * the parse_url_options contract, and must not corrupt the default value. */
38 static int expect_parse_error(const char *url)
39 {
40 struct rist_peer_config *cfg = NULL;
41 int ret = rist_parse_address2(url, &cfg);
42 int bad = 0;
43 if (ret == 0) {
44 fprintf(stderr, "FAIL: %s expected parse error, got success\n", url);
45 bad = 1;
46 } else if (cfg != NULL && cfg->recovery_depth != RIST_RECOVERY_DEPTH_DEFAULT) {
47 fprintf(stderr, "FAIL: %s left recovery_depth=%u (want default %d)\n",
48 url, (unsigned)cfg->recovery_depth, RIST_RECOVERY_DEPTH_DEFAULT);
49 bad = 1;
50 }
51 if (cfg)
52 rist_peer_config_free2(&cfg);
53 return bad;
54 }
55
56 int main(void)
57 {
58 int failures = 0;
59
60 /* Absent parameter leaves the library default. */
61 failures += expect_depth("rist://@127.0.0.1:1234", RIST_RECOVERY_DEPTH_DEFAULT);
62
63 /* Boundary and representative interior values. */
64 failures += expect_depth("rist://@127.0.0.1:1234?recovery-depth=0", 0);
65 failures += expect_depth("rist://@127.0.0.1:1234?recovery-depth=3", 3);
66 failures += expect_depth("rist://@127.0.0.1:1234?recovery-depth=6", 6);
67 failures += expect_depth("rist://@127.0.0.1:1234?recovery-depth=16", 16);
68
69 /* Coexists with other parameters in either order. */
70 failures += expect_depth("rist://@127.0.0.1:1234?buffer=100&recovery-depth=5", 5);
71 failures += expect_depth("rist://@127.0.0.1:1234?recovery-depth=8&bandwidth=8000", 8);
72
73 /* Out-of-range and non-numeric are rejected and leave the default. */
74 failures += expect_parse_error("rist://@127.0.0.1:1234?recovery-depth=17");
75 failures += expect_parse_error("rist://@127.0.0.1:1234?recovery-depth=-1");
76 failures += expect_parse_error("rist://@127.0.0.1:1234?recovery-depth=large");
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 }