1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include "bus-internal.h"
4 #include "bus-signature.h"
5 #include "log.h"
6 #include "string-util.h"
7 
main(int argc,char * argv[])8 int main(int argc, char *argv[]) {
9         char prefix[256];
10         int r;
11 
12         assert_se(signature_is_single("y", false));
13         assert_se(signature_is_single("u", false));
14         assert_se(signature_is_single("v", false));
15         assert_se(signature_is_single("as", false));
16         assert_se(signature_is_single("(ss)", false));
17         assert_se(!signature_is_single("()", false));
18         assert_se(!signature_is_single("(()()()()())", false));
19         assert_se(!signature_is_single("(((())))", false));
20         assert_se(signature_is_single("((((s))))", false));
21         assert_se(signature_is_single("{ss}", true));
22         assert_se(signature_is_single("a{ss}", false));
23         assert_se(!signature_is_single("uu", false));
24         assert_se(!signature_is_single("", false));
25         assert_se(!signature_is_single("(", false));
26         assert_se(!signature_is_single(")", false));
27         assert_se(!signature_is_single("())", false));
28         assert_se(!signature_is_single("((())", false));
29         assert_se(!signature_is_single("{)", false));
30         assert_se(!signature_is_single("{}", true));
31         assert_se(!signature_is_single("{sss}", true));
32         assert_se(!signature_is_single("{s}", true));
33         assert_se(!signature_is_single("{ss}", false));
34         assert_se(!signature_is_single("{ass}", true));
35         assert_se(!signature_is_single("a}", true));
36 
37         assert_se(signature_is_pair("yy"));
38         assert_se(signature_is_pair("ss"));
39         assert_se(signature_is_pair("sas"));
40         assert_se(signature_is_pair("sv"));
41         assert_se(signature_is_pair("sa(vs)"));
42         assert_se(!signature_is_pair(""));
43         assert_se(!signature_is_pair("va"));
44         assert_se(!signature_is_pair("sss"));
45         assert_se(!signature_is_pair("{s}ss"));
46 
47         assert_se(signature_is_valid("ssa{ss}sssub", true));
48         assert_se(signature_is_valid("ssa{ss}sssub", false));
49         assert_se(signature_is_valid("{ss}", true));
50         assert_se(!signature_is_valid("{ss}", false));
51         assert_se(signature_is_valid("", true));
52         assert_se(signature_is_valid("", false));
53 
54         assert_se(signature_is_valid("sssusa(uuubbba(uu)uuuu)a{u(uuuvas)}", false));
55 
56         assert_se(!signature_is_valid("a", false));
57         assert_se(signature_is_valid("as", false));
58         assert_se(signature_is_valid("aas", false));
59         assert_se(signature_is_valid("aaas", false));
60         assert_se(signature_is_valid("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaad", false));
61         assert_se(signature_is_valid("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaas", false));
62         assert_se(!signature_is_valid("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaau", false));
63 
64         assert_se(signature_is_valid("((((((((((((((((((((((((((((((((s))))))))))))))))))))))))))))))))", false));
65         assert_se(!signature_is_valid("((((((((((((((((((((((((((((((((()))))))))))))))))))))))))))))))))", false));
66 
67         assert_se(namespace_complex_pattern("", ""));
68         assert_se(namespace_complex_pattern("foobar", "foobar"));
69         assert_se(namespace_complex_pattern("foobar.waldo", "foobar.waldo"));
70         assert_se(namespace_complex_pattern("foobar.", "foobar.waldo"));
71         assert_se(namespace_complex_pattern("foobar.waldo", "foobar."));
72         assert_se(!namespace_complex_pattern("foobar.waldo", "foobar"));
73         assert_se(!namespace_complex_pattern("foobar", "foobar.waldo"));
74         assert_se(!namespace_complex_pattern("", "foo"));
75         assert_se(!namespace_complex_pattern("foo", ""));
76         assert_se(!namespace_complex_pattern("foo.", ""));
77 
78         assert_se(path_complex_pattern("", ""));
79         assert_se(!path_complex_pattern("", "/"));
80         assert_se(!path_complex_pattern("/", ""));
81         assert_se(path_complex_pattern("/", "/"));
82         assert_se(path_complex_pattern("/foobar/", "/"));
83         assert_se(!path_complex_pattern("/foobar/", "/foobar"));
84         assert_se(path_complex_pattern("/foobar", "/foobar"));
85         assert_se(!path_complex_pattern("/foobar", "/foobar/"));
86         assert_se(!path_complex_pattern("/foobar", "/foobar/waldo"));
87         assert_se(path_complex_pattern("/foobar/", "/foobar/waldo"));
88         assert_se(path_complex_pattern("/foobar/waldo", "/foobar/"));
89 
90         assert_se(path_simple_pattern("/foo/", "/foo/bar/waldo"));
91 
92         assert_se(namespace_simple_pattern("", ""));
93         assert_se(namespace_simple_pattern("", ".foobar"));
94         assert_se(namespace_simple_pattern("foobar", "foobar"));
95         assert_se(namespace_simple_pattern("foobar.waldo", "foobar.waldo"));
96         assert_se(namespace_simple_pattern("foobar", "foobar.waldo"));
97         assert_se(!namespace_simple_pattern("foobar.waldo", "foobar"));
98         assert_se(!namespace_simple_pattern("", "foo"));
99         assert_se(!namespace_simple_pattern("foo", ""));
100         assert_se(namespace_simple_pattern("foo.", "foo.bar.waldo"));
101 
102         assert_se(streq(object_path_startswith("/foo/bar", "/foo"), "bar"));
103         assert_se(streq(object_path_startswith("/foo", "/foo"), ""));
104         assert_se(streq(object_path_startswith("/foo", "/"), "foo"));
105         assert_se(streq(object_path_startswith("/", "/"), ""));
106         assert_se(!object_path_startswith("/foo", "/bar"));
107         assert_se(!object_path_startswith("/", "/bar"));
108         assert_se(!object_path_startswith("/foo", ""));
109 
110         assert_se(object_path_is_valid("/foo/bar"));
111         assert_se(object_path_is_valid("/foo"));
112         assert_se(object_path_is_valid("/"));
113         assert_se(object_path_is_valid("/foo5"));
114         assert_se(object_path_is_valid("/foo_5"));
115         assert_se(!object_path_is_valid(""));
116         assert_se(!object_path_is_valid("/foo/"));
117         assert_se(!object_path_is_valid("//"));
118         assert_se(!object_path_is_valid("//foo"));
119         assert_se(!object_path_is_valid("/foo//bar"));
120         assert_se(!object_path_is_valid("/foo/aaaäöä"));
121 
122         OBJECT_PATH_FOREACH_PREFIX(prefix, "/") {
123                 log_info("<%s>", prefix);
124                 assert_not_reached();
125         }
126 
127         r = 0;
128         OBJECT_PATH_FOREACH_PREFIX(prefix, "/xxx") {
129                 log_info("<%s>", prefix);
130                 assert_se(streq(prefix, "/"));
131                 assert_se(r == 0);
132                 r++;
133         }
134         assert_se(r == 1);
135 
136         r = 0;
137         OBJECT_PATH_FOREACH_PREFIX(prefix, "/xxx/yyy/zzz") {
138                 log_info("<%s>", prefix);
139                 assert_se(r != 0 || streq(prefix, "/xxx/yyy"));
140                 assert_se(r != 1 || streq(prefix, "/xxx"));
141                 assert_se(r != 2 || streq(prefix, "/"));
142                 r++;
143         }
144         assert_se(r == 3);
145 
146         return 0;
147 }
148