Debian Codegraph @

test/rist/unit/test_url_profile_parse.c

raw symbols SCIP
1 /* librist. SPDX-License-Identifier: BSD-2-Clause
2 *
3 * Intentionally avoids cmocka so it runs anywhere the public library
4 * does. */
5
6 #include "librist/librist.h"
7 #include "librist/peer.h"
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 static int expect_profile(const char *url,
14 enum rist_profile want_profile,
15 int want_set)
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,
21 "FAIL: rist_parse_address2(%s) ret=%d cfg=%p\n",
22 url, ret, (void *)cfg);
23 if (cfg)
24 rist_peer_config_free2(&cfg);
25 return 1;
26 }
27 if ((int)cfg->profile != (int)want_profile ||
28 cfg->profile_set != want_set) {
29 fprintf(stderr,
30 "FAIL: %s -> profile=%d profile_set=%d "
31 "(want profile=%d profile_set=%d)\n",
32 url, (int)cfg->profile, cfg->profile_set,
33 (int)want_profile, want_set);
34 rist_peer_config_free2(&cfg);
35 return 1;
36 }
37 rist_peer_config_free2(&cfg);
38 return 0;
39 }
40
41 static int expect_parse_error(const char *url)
42 {
43 struct rist_peer_config *cfg = NULL;
44 int ret = rist_parse_address2(url, &cfg);
45 if (ret == 0) {
46 fprintf(stderr,
47 "FAIL: %s expected parse error, got success\n", url);
48 if (cfg)
49 rist_peer_config_free2(&cfg);
50 return 1;
51 }
52 if (cfg)
53 rist_peer_config_free2(&cfg);
54 return 0;
55 }
56
57 int main(void)
58 {
59 int failures = 0;
60
61 failures += expect_profile(
62 "rist://@127.0.0.1:1234",
63 RIST_DEFAULT_PROFILE, 0);
64
65 failures += expect_profile(
66 "rist://@127.0.0.1:1234?profile=0",
67 RIST_PROFILE_SIMPLE, 1);
68 failures += expect_profile(
69 "rist://@127.0.0.1:1234?profile=1",
70 RIST_PROFILE_MAIN, 1);
71 failures += expect_profile(
72 "rist://@127.0.0.1:1234?profile=2",
73 RIST_PROFILE_ADVANCED, 1);
74
75 failures += expect_profile(
76 "rist://@127.0.0.1:1234?profile=2&buffer=100",
77 RIST_PROFILE_ADVANCED, 1);
78 failures += expect_profile(
79 "rist://@127.0.0.1:1234?buffer=100&profile=2",
80 RIST_PROFILE_ADVANCED, 1);
81
82 failures += expect_parse_error(
83 "rist://@127.0.0.1:1234?profile=3");
84 failures += expect_parse_error(
85 "rist://@127.0.0.1:1234?profile=garbage");
86 failures += expect_parse_error(
87 "rist://@127.0.0.1:1234?profile=simple");
88 failures += expect_parse_error(
89 "rist://@127.0.0.1:1234?profile=main");
90 failures += expect_parse_error(
91 "rist://@127.0.0.1:1234?profile=advanced");
92 failures += expect_parse_error(
93 "rist://@127.0.0.1:1234?profile=2foo");
94 failures += expect_parse_error(
95 "rist://@127.0.0.1:1234?profile=-1");
96
97 /* Empty value: upstream parser yields val==NULL, treated as absent. */
98 failures += expect_profile(
99 "rist://@127.0.0.1:1234?profile=",
100 RIST_DEFAULT_PROFILE, 0);
101
102 if (failures == 0) {
103 printf("OK\n");
104 return 0;
105 }
106 fprintf(stderr, "Total failures: %d\n", failures);
107 return 1;
108 }