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 1 : static void test_bus_gvariant_is_fixed_size(void) {
20 1 : log_info("/* %s */", __func__);
21 :
22 1 : assert_se(bus_gvariant_is_fixed_size("") > 0);
23 1 : assert_se(bus_gvariant_is_fixed_size("()") == -EINVAL);
24 1 : assert_se(bus_gvariant_is_fixed_size("y") > 0);
25 1 : assert_se(bus_gvariant_is_fixed_size("u") > 0);
26 1 : assert_se(bus_gvariant_is_fixed_size("b") > 0);
27 1 : assert_se(bus_gvariant_is_fixed_size("n") > 0);
28 1 : assert_se(bus_gvariant_is_fixed_size("q") > 0);
29 1 : assert_se(bus_gvariant_is_fixed_size("i") > 0);
30 1 : assert_se(bus_gvariant_is_fixed_size("t") > 0);
31 1 : assert_se(bus_gvariant_is_fixed_size("d") > 0);
32 1 : assert_se(bus_gvariant_is_fixed_size("s") == 0);
33 1 : assert_se(bus_gvariant_is_fixed_size("o") == 0);
34 1 : assert_se(bus_gvariant_is_fixed_size("g") == 0);
35 1 : assert_se(bus_gvariant_is_fixed_size("h") > 0);
36 1 : assert_se(bus_gvariant_is_fixed_size("ay") == 0);
37 1 : assert_se(bus_gvariant_is_fixed_size("v") == 0);
38 1 : assert_se(bus_gvariant_is_fixed_size("(u)") > 0);
39 1 : assert_se(bus_gvariant_is_fixed_size("(uuuuy)") > 0);
40 1 : assert_se(bus_gvariant_is_fixed_size("(uusuuy)") == 0);
41 1 : assert_se(bus_gvariant_is_fixed_size("a{ss}") == 0);
42 1 : assert_se(bus_gvariant_is_fixed_size("((u)yyy(b(iiii)))") > 0);
43 1 : assert_se(bus_gvariant_is_fixed_size("((u)yyy(b(iiivi)))") == 0);
44 1 : }
45 :
46 1 : static void test_bus_gvariant_get_size(void) {
47 1 : log_info("/* %s */", __func__);
48 :
49 1 : assert_se(bus_gvariant_get_size("") == 0);
50 1 : assert_se(bus_gvariant_get_size("()") == -EINVAL);
51 1 : assert_se(bus_gvariant_get_size("y") == 1);
52 1 : assert_se(bus_gvariant_get_size("u") == 4);
53 1 : assert_se(bus_gvariant_get_size("b") == 1);
54 1 : assert_se(bus_gvariant_get_size("n") == 2);
55 1 : assert_se(bus_gvariant_get_size("q") == 2);
56 1 : assert_se(bus_gvariant_get_size("i") == 4);
57 1 : assert_se(bus_gvariant_get_size("t") == 8);
58 1 : assert_se(bus_gvariant_get_size("d") == 8);
59 1 : assert_se(bus_gvariant_get_size("s") < 0);
60 1 : assert_se(bus_gvariant_get_size("o") < 0);
61 1 : assert_se(bus_gvariant_get_size("g") < 0);
62 1 : assert_se(bus_gvariant_get_size("h") == 4);
63 1 : assert_se(bus_gvariant_get_size("ay") < 0);
64 1 : assert_se(bus_gvariant_get_size("v") < 0);
65 1 : assert_se(bus_gvariant_get_size("(u)") == 4);
66 1 : assert_se(bus_gvariant_get_size("(uuuuy)") == 20);
67 1 : assert_se(bus_gvariant_get_size("(uusuuy)") < 0);
68 1 : assert_se(bus_gvariant_get_size("a{ss}") < 0);
69 1 : assert_se(bus_gvariant_get_size("((u)yyy(b(iiii)))") == 28);
70 1 : assert_se(bus_gvariant_get_size("((u)yyy(b(iiivi)))") < 0);
71 1 : assert_se(bus_gvariant_get_size("((b)(t))") == 16);
72 1 : assert_se(bus_gvariant_get_size("((b)(b)(t))") == 16);
73 1 : assert_se(bus_gvariant_get_size("(bt)") == 16);
74 1 : assert_se(bus_gvariant_get_size("((t)(b))") == 16);
75 1 : assert_se(bus_gvariant_get_size("(tb)") == 16);
76 1 : assert_se(bus_gvariant_get_size("((b)(b))") == 2);
77 1 : assert_se(bus_gvariant_get_size("((t)(t))") == 16);
78 1 : }
79 :
80 1 : static void test_bus_gvariant_get_alignment(void) {
81 1 : log_info("/* %s */", __func__);
82 :
83 1 : assert_se(bus_gvariant_get_alignment("") == 1);
84 1 : assert_se(bus_gvariant_get_alignment("()") == -EINVAL);
85 1 : assert_se(bus_gvariant_get_alignment("y") == 1);
86 1 : assert_se(bus_gvariant_get_alignment("b") == 1);
87 1 : assert_se(bus_gvariant_get_alignment("u") == 4);
88 1 : assert_se(bus_gvariant_get_alignment("s") == 1);
89 1 : assert_se(bus_gvariant_get_alignment("o") == 1);
90 1 : assert_se(bus_gvariant_get_alignment("g") == 1);
91 1 : assert_se(bus_gvariant_get_alignment("v") == 8);
92 1 : assert_se(bus_gvariant_get_alignment("h") == 4);
93 1 : assert_se(bus_gvariant_get_alignment("i") == 4);
94 1 : assert_se(bus_gvariant_get_alignment("t") == 8);
95 1 : assert_se(bus_gvariant_get_alignment("x") == 8);
96 1 : assert_se(bus_gvariant_get_alignment("q") == 2);
97 1 : assert_se(bus_gvariant_get_alignment("n") == 2);
98 1 : assert_se(bus_gvariant_get_alignment("d") == 8);
99 1 : assert_se(bus_gvariant_get_alignment("ay") == 1);
100 1 : assert_se(bus_gvariant_get_alignment("as") == 1);
101 1 : assert_se(bus_gvariant_get_alignment("au") == 4);
102 1 : assert_se(bus_gvariant_get_alignment("an") == 2);
103 1 : assert_se(bus_gvariant_get_alignment("ans") == 2);
104 1 : assert_se(bus_gvariant_get_alignment("ant") == 8);
105 1 : assert_se(bus_gvariant_get_alignment("(ss)") == 1);
106 1 : assert_se(bus_gvariant_get_alignment("(ssu)") == 4);
107 1 : assert_se(bus_gvariant_get_alignment("a(ssu)") == 4);
108 1 : assert_se(bus_gvariant_get_alignment("(u)") == 4);
109 1 : assert_se(bus_gvariant_get_alignment("(uuuuy)") == 4);
110 1 : assert_se(bus_gvariant_get_alignment("(uusuuy)") == 4);
111 1 : assert_se(bus_gvariant_get_alignment("a{ss}") == 1);
112 1 : assert_se(bus_gvariant_get_alignment("((u)yyy(b(iiii)))") == 4);
113 1 : assert_se(bus_gvariant_get_alignment("((u)yyy(b(iiivi)))") == 8);
114 1 : assert_se(bus_gvariant_get_alignment("((b)(t))") == 8);
115 1 : assert_se(bus_gvariant_get_alignment("((b)(b)(t))") == 8);
116 1 : assert_se(bus_gvariant_get_alignment("(bt)") == 8);
117 1 : assert_se(bus_gvariant_get_alignment("((t)(b))") == 8);
118 1 : assert_se(bus_gvariant_get_alignment("(tb)") == 8);
119 1 : assert_se(bus_gvariant_get_alignment("((b)(b))") == 1);
120 1 : assert_se(bus_gvariant_get_alignment("((t)(t))") == 8);
121 1 : }
122 :
123 1 : static int test_marshal(void) {
124 1 : _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *n = NULL;
125 1 : _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
126 1 : _cleanup_free_ void *blob = NULL;
127 : size_t sz;
128 : int r;
129 :
130 1 : r = sd_bus_open_user(&bus);
131 1 : if (r < 0)
132 0 : r = sd_bus_open_system(&bus);
133 1 : if (r < 0)
134 0 : return log_tests_skipped_errno(r, "Failed to connect to bus");
135 :
136 1 : bus->message_version = 2; /* dirty hack to enable gvariant */
137 :
138 1 : 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 1 : assert_se(r >= 0);
142 :
143 : assert_cc(sizeof(struct bus_header) == 16);
144 :
145 1 : 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 1 : 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 1 : v = g_variant_new_from_data(G_VARIANT_TYPE("(yyyyuta{tv})"), m->header, sizeof(struct bus_header) + m->fields_size, false, NULL, NULL);
163 1 : assert_se(g_variant_is_normal_form(v));
164 1 : t = g_variant_print(v, TRUE);
165 1 : printf("%s\n", t);
166 1 : g_free(t);
167 1 : g_variant_unref(v);
168 :
169 1 : v = g_variant_new_from_data(G_VARIANT_TYPE("(a(usv))"), m->body.data, m->user_body_size, false, NULL, NULL);
170 1 : assert_se(g_variant_is_normal_form(v));
171 1 : t = g_variant_print(v, TRUE);
172 1 : printf("%s\n", t);
173 1 : g_free(t);
174 1 : g_variant_unref(v);
175 : }
176 : #endif
177 :
178 1 : assert_se(bus_message_dump(m, NULL, BUS_MESSAGE_DUMP_WITH_HEADER) >= 0);
179 :
180 1 : assert_se(bus_message_get_blob(m, &blob, &sz) >= 0);
181 :
182 : #if HAVE_GLIB
183 : {
184 : GVariant *v;
185 : char *t;
186 :
187 1 : v = g_variant_new_from_data(G_VARIANT_TYPE("(yyyyuta{tv}v)"), blob, sz, false, NULL, NULL);
188 1 : assert_se(g_variant_is_normal_form(v));
189 1 : t = g_variant_print(v, TRUE);
190 1 : printf("%s\n", t);
191 1 : g_free(t);
192 1 : g_variant_unref(v);
193 : }
194 : #endif
195 :
196 1 : assert_se(bus_message_from_malloc(bus, blob, sz, NULL, 0, NULL, &n) >= 0);
197 1 : blob = NULL;
198 :
199 1 : assert_se(bus_message_dump(n, NULL, BUS_MESSAGE_DUMP_WITH_HEADER) >= 0);
200 :
201 1 : m = sd_bus_message_unref(m);
202 :
203 1 : assert_se(sd_bus_message_new_method_call(bus, &m, "a.x", "/a/x", "a.x", "Ax") >= 0);
204 :
205 1 : assert_se(sd_bus_message_append(m, "as", 0) >= 0);
206 :
207 1 : assert_se(sd_bus_message_seal(m, 4712, 0) >= 0);
208 1 : assert_se(bus_message_dump(m, NULL, BUS_MESSAGE_DUMP_WITH_HEADER) >= 0);
209 :
210 1 : return EXIT_SUCCESS;
211 : }
212 :
213 1 : int main(int argc, char *argv[]) {
214 1 : test_setup_logging(LOG_DEBUG);
215 :
216 1 : test_bus_gvariant_is_fixed_size();
217 1 : test_bus_gvariant_get_size();
218 1 : test_bus_gvariant_get_alignment();
219 :
220 1 : return test_marshal();
221 : }
|