Branch data Line data Source code
1 : : /* SPDX-License-Identifier: LGPL-2.1+ */
2 : :
3 : : #if HAVE_GLIB
4 : : #include <glib.h>
5 : : #endif
6 : :
7 : : #include "sd-bus.h"
8 : :
9 : : #include "alloc-util.h"
10 : : #include "bus-dump.h"
11 : : #include "bus-gvariant.h"
12 : : #include "bus-internal.h"
13 : : #include "bus-message.h"
14 : : #include "bus-util.h"
15 : : #include "macro.h"
16 : : #include "tests.h"
17 : : #include "util.h"
18 : :
19 : 4 : static void test_bus_gvariant_is_fixed_size(void) {
20 [ + - ]: 4 : log_info("/* %s */", __func__);
21 : :
22 [ - + ]: 4 : assert_se(bus_gvariant_is_fixed_size("") > 0);
23 [ - + ]: 4 : assert_se(bus_gvariant_is_fixed_size("()") == -EINVAL);
24 [ - + ]: 4 : assert_se(bus_gvariant_is_fixed_size("y") > 0);
25 [ - + ]: 4 : assert_se(bus_gvariant_is_fixed_size("u") > 0);
26 [ - + ]: 4 : assert_se(bus_gvariant_is_fixed_size("b") > 0);
27 [ - + ]: 4 : assert_se(bus_gvariant_is_fixed_size("n") > 0);
28 [ - + ]: 4 : assert_se(bus_gvariant_is_fixed_size("q") > 0);
29 [ - + ]: 4 : assert_se(bus_gvariant_is_fixed_size("i") > 0);
30 [ - + ]: 4 : assert_se(bus_gvariant_is_fixed_size("t") > 0);
31 [ - + ]: 4 : assert_se(bus_gvariant_is_fixed_size("d") > 0);
32 [ - + ]: 4 : assert_se(bus_gvariant_is_fixed_size("s") == 0);
33 [ - + ]: 4 : assert_se(bus_gvariant_is_fixed_size("o") == 0);
34 [ - + ]: 4 : assert_se(bus_gvariant_is_fixed_size("g") == 0);
35 [ - + ]: 4 : assert_se(bus_gvariant_is_fixed_size("h") > 0);
36 [ - + ]: 4 : assert_se(bus_gvariant_is_fixed_size("ay") == 0);
37 [ - + ]: 4 : assert_se(bus_gvariant_is_fixed_size("v") == 0);
38 [ - + ]: 4 : assert_se(bus_gvariant_is_fixed_size("(u)") > 0);
39 [ - + ]: 4 : assert_se(bus_gvariant_is_fixed_size("(uuuuy)") > 0);
40 [ - + ]: 4 : assert_se(bus_gvariant_is_fixed_size("(uusuuy)") == 0);
41 [ - + ]: 4 : assert_se(bus_gvariant_is_fixed_size("a{ss}") == 0);
42 [ - + ]: 4 : assert_se(bus_gvariant_is_fixed_size("((u)yyy(b(iiii)))") > 0);
43 [ - + ]: 4 : assert_se(bus_gvariant_is_fixed_size("((u)yyy(b(iiivi)))") == 0);
44 : 4 : }
45 : :
46 : 4 : static void test_bus_gvariant_get_size(void) {
47 [ + - ]: 4 : log_info("/* %s */", __func__);
48 : :
49 [ - + ]: 4 : assert_se(bus_gvariant_get_size("") == 0);
50 [ - + ]: 4 : assert_se(bus_gvariant_get_size("()") == -EINVAL);
51 [ - + ]: 4 : assert_se(bus_gvariant_get_size("y") == 1);
52 [ - + ]: 4 : assert_se(bus_gvariant_get_size("u") == 4);
53 [ - + ]: 4 : assert_se(bus_gvariant_get_size("b") == 1);
54 [ - + ]: 4 : assert_se(bus_gvariant_get_size("n") == 2);
55 [ - + ]: 4 : assert_se(bus_gvariant_get_size("q") == 2);
56 [ - + ]: 4 : assert_se(bus_gvariant_get_size("i") == 4);
57 [ - + ]: 4 : assert_se(bus_gvariant_get_size("t") == 8);
58 [ - + ]: 4 : assert_se(bus_gvariant_get_size("d") == 8);
59 [ - + ]: 4 : assert_se(bus_gvariant_get_size("s") < 0);
60 [ - + ]: 4 : assert_se(bus_gvariant_get_size("o") < 0);
61 [ - + ]: 4 : assert_se(bus_gvariant_get_size("g") < 0);
62 [ - + ]: 4 : assert_se(bus_gvariant_get_size("h") == 4);
63 [ - + ]: 4 : assert_se(bus_gvariant_get_size("ay") < 0);
64 [ - + ]: 4 : assert_se(bus_gvariant_get_size("v") < 0);
65 [ - + ]: 4 : assert_se(bus_gvariant_get_size("(u)") == 4);
66 [ - + ]: 4 : assert_se(bus_gvariant_get_size("(uuuuy)") == 20);
67 [ - + ]: 4 : assert_se(bus_gvariant_get_size("(uusuuy)") < 0);
68 [ - + ]: 4 : assert_se(bus_gvariant_get_size("a{ss}") < 0);
69 [ - + ]: 4 : assert_se(bus_gvariant_get_size("((u)yyy(b(iiii)))") == 28);
70 [ - + ]: 4 : assert_se(bus_gvariant_get_size("((u)yyy(b(iiivi)))") < 0);
71 [ - + ]: 4 : assert_se(bus_gvariant_get_size("((b)(t))") == 16);
72 [ - + ]: 4 : assert_se(bus_gvariant_get_size("((b)(b)(t))") == 16);
73 [ - + ]: 4 : assert_se(bus_gvariant_get_size("(bt)") == 16);
74 [ - + ]: 4 : assert_se(bus_gvariant_get_size("((t)(b))") == 16);
75 [ - + ]: 4 : assert_se(bus_gvariant_get_size("(tb)") == 16);
76 [ - + ]: 4 : assert_se(bus_gvariant_get_size("((b)(b))") == 2);
77 [ - + ]: 4 : assert_se(bus_gvariant_get_size("((t)(t))") == 16);
78 : 4 : }
79 : :
80 : 4 : static void test_bus_gvariant_get_alignment(void) {
81 [ + - ]: 4 : log_info("/* %s */", __func__);
82 : :
83 [ - + ]: 4 : assert_se(bus_gvariant_get_alignment("") == 1);
84 [ - + ]: 4 : assert_se(bus_gvariant_get_alignment("()") == -EINVAL);
85 [ - + ]: 4 : assert_se(bus_gvariant_get_alignment("y") == 1);
86 [ - + ]: 4 : assert_se(bus_gvariant_get_alignment("b") == 1);
87 [ - + ]: 4 : assert_se(bus_gvariant_get_alignment("u") == 4);
88 [ - + ]: 4 : assert_se(bus_gvariant_get_alignment("s") == 1);
89 [ - + ]: 4 : assert_se(bus_gvariant_get_alignment("o") == 1);
90 [ - + ]: 4 : assert_se(bus_gvariant_get_alignment("g") == 1);
91 [ - + ]: 4 : assert_se(bus_gvariant_get_alignment("v") == 8);
92 [ - + ]: 4 : assert_se(bus_gvariant_get_alignment("h") == 4);
93 [ - + ]: 4 : assert_se(bus_gvariant_get_alignment("i") == 4);
94 [ - + ]: 4 : assert_se(bus_gvariant_get_alignment("t") == 8);
95 [ - + ]: 4 : assert_se(bus_gvariant_get_alignment("x") == 8);
96 [ - + ]: 4 : assert_se(bus_gvariant_get_alignment("q") == 2);
97 [ - + ]: 4 : assert_se(bus_gvariant_get_alignment("n") == 2);
98 [ - + ]: 4 : assert_se(bus_gvariant_get_alignment("d") == 8);
99 [ - + ]: 4 : assert_se(bus_gvariant_get_alignment("ay") == 1);
100 [ - + ]: 4 : assert_se(bus_gvariant_get_alignment("as") == 1);
101 [ - + ]: 4 : assert_se(bus_gvariant_get_alignment("au") == 4);
102 [ - + ]: 4 : assert_se(bus_gvariant_get_alignment("an") == 2);
103 [ - + ]: 4 : assert_se(bus_gvariant_get_alignment("ans") == 2);
104 [ - + ]: 4 : assert_se(bus_gvariant_get_alignment("ant") == 8);
105 [ - + ]: 4 : assert_se(bus_gvariant_get_alignment("(ss)") == 1);
106 [ - + ]: 4 : assert_se(bus_gvariant_get_alignment("(ssu)") == 4);
107 [ - + ]: 4 : assert_se(bus_gvariant_get_alignment("a(ssu)") == 4);
108 [ - + ]: 4 : assert_se(bus_gvariant_get_alignment("(u)") == 4);
109 [ - + ]: 4 : assert_se(bus_gvariant_get_alignment("(uuuuy)") == 4);
110 [ - + ]: 4 : assert_se(bus_gvariant_get_alignment("(uusuuy)") == 4);
111 [ - + ]: 4 : assert_se(bus_gvariant_get_alignment("a{ss}") == 1);
112 [ - + ]: 4 : assert_se(bus_gvariant_get_alignment("((u)yyy(b(iiii)))") == 4);
113 [ - + ]: 4 : assert_se(bus_gvariant_get_alignment("((u)yyy(b(iiivi)))") == 8);
114 [ - + ]: 4 : assert_se(bus_gvariant_get_alignment("((b)(t))") == 8);
115 [ - + ]: 4 : assert_se(bus_gvariant_get_alignment("((b)(b)(t))") == 8);
116 [ - + ]: 4 : assert_se(bus_gvariant_get_alignment("(bt)") == 8);
117 [ - + ]: 4 : assert_se(bus_gvariant_get_alignment("((t)(b))") == 8);
118 [ - + ]: 4 : assert_se(bus_gvariant_get_alignment("(tb)") == 8);
119 [ - + ]: 4 : assert_se(bus_gvariant_get_alignment("((b)(b))") == 1);
120 [ - + ]: 4 : assert_se(bus_gvariant_get_alignment("((t)(t))") == 8);
121 : 4 : }
122 : :
123 : 4 : static int test_marshal(void) {
124 : 4 : _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *n = NULL;
125 : 4 : _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
126 : 4 : _cleanup_free_ void *blob = NULL;
127 : : size_t sz;
128 : : int r;
129 : :
130 : 4 : r = sd_bus_open_user(&bus);
131 [ - + ]: 4 : if (r < 0)
132 : 0 : r = sd_bus_open_system(&bus);
133 [ - + ]: 4 : if (r < 0)
134 : 0 : return log_tests_skipped_errno(r, "Failed to connect to bus");
135 : :
136 : 4 : bus->message_version = 2; /* dirty hack to enable gvariant */
137 : :
138 : 4 : r = sd_bus_message_new_method_call(bus, &m, "a.service.name",
139 : : "/an/object/path/which/is/really/really/long/so/that/we/hit/the/eight/bit/boundary/by/quite/some/margin/to/test/this/stuff/that/it/really/works",
140 : : "an.interface.name", "AMethodName");
141 [ - + ]: 4 : assert_se(r >= 0);
142 : :
143 : : assert_cc(sizeof(struct bus_header) == 16);
144 : :
145 [ - + ]: 4 : assert_se(sd_bus_message_append(m,
146 : : "a(usv)", 3,
147 : : 4711, "first-string-parameter", "(st)", "X", (uint64_t) 1111,
148 : : 4712, "second-string-parameter", "(a(si))", 2, "Y", 5, "Z", 6,
149 : : 4713, "third-string-parameter", "(uu)", 1, 2) >= 0);
150 : :
151 [ - + ]: 4 : assert_se(sd_bus_message_seal(m, 4711, 0) >= 0);
152 : :
153 : : #if HAVE_GLIB
154 : : {
155 : : GVariant *v;
156 : : char *t;
157 : :
158 : : #if !defined(GLIB_VERSION_2_36)
159 : : g_type_init();
160 : : #endif
161 : :
162 : 4 : v = g_variant_new_from_data(G_VARIANT_TYPE("(yyyyuta{tv})"), m->header, sizeof(struct bus_header) + m->fields_size, false, NULL, NULL);
163 [ - + ]: 4 : assert_se(g_variant_is_normal_form(v));
164 : 4 : t = g_variant_print(v, TRUE);
165 : 4 : printf("%s\n", t);
166 : 4 : g_free(t);
167 : 4 : g_variant_unref(v);
168 : :
169 : 4 : v = g_variant_new_from_data(G_VARIANT_TYPE("(a(usv))"), m->body.data, m->user_body_size, false, NULL, NULL);
170 [ - + ]: 4 : assert_se(g_variant_is_normal_form(v));
171 : 4 : t = g_variant_print(v, TRUE);
172 : 4 : printf("%s\n", t);
173 : 4 : g_free(t);
174 : 4 : g_variant_unref(v);
175 : : }
176 : : #endif
177 : :
178 [ - + ]: 4 : assert_se(bus_message_dump(m, NULL, BUS_MESSAGE_DUMP_WITH_HEADER) >= 0);
179 : :
180 [ - + ]: 4 : assert_se(bus_message_get_blob(m, &blob, &sz) >= 0);
181 : :
182 : : #if HAVE_GLIB
183 : : {
184 : : GVariant *v;
185 : : char *t;
186 : :
187 : 4 : v = g_variant_new_from_data(G_VARIANT_TYPE("(yyyyuta{tv}v)"), blob, sz, false, NULL, NULL);
188 [ - + ]: 4 : assert_se(g_variant_is_normal_form(v));
189 : 4 : t = g_variant_print(v, TRUE);
190 : 4 : printf("%s\n", t);
191 : 4 : g_free(t);
192 : 4 : g_variant_unref(v);
193 : : }
194 : : #endif
195 : :
196 [ - + ]: 4 : assert_se(bus_message_from_malloc(bus, blob, sz, NULL, 0, NULL, &n) >= 0);
197 : 4 : blob = NULL;
198 : :
199 [ - + ]: 4 : assert_se(bus_message_dump(n, NULL, BUS_MESSAGE_DUMP_WITH_HEADER) >= 0);
200 : :
201 : 4 : m = sd_bus_message_unref(m);
202 : :
203 [ - + ]: 4 : assert_se(sd_bus_message_new_method_call(bus, &m, "a.x", "/a/x", "a.x", "Ax") >= 0);
204 : :
205 [ - + ]: 4 : assert_se(sd_bus_message_append(m, "as", 0) >= 0);
206 : :
207 [ - + ]: 4 : assert_se(sd_bus_message_seal(m, 4712, 0) >= 0);
208 [ - + ]: 4 : assert_se(bus_message_dump(m, NULL, BUS_MESSAGE_DUMP_WITH_HEADER) >= 0);
209 : :
210 : 4 : return EXIT_SUCCESS;
211 : : }
212 : :
213 : 4 : int main(int argc, char *argv[]) {
214 : 4 : test_setup_logging(LOG_DEBUG);
215 : :
216 : 4 : test_bus_gvariant_is_fixed_size();
217 : 4 : test_bus_gvariant_get_size();
218 : 4 : test_bus_gvariant_get_alignment();
219 : :
220 : 4 : return test_marshal();
221 : : }
|