Debian Codegraph @

tests/wp/conf.c

raw
1 /* WirePlumber
2 *
3 * Copyright © 2022 Collabora Ltd.
4 * @author Julian Bouzas <[email protected]>
5 *
6 * SPDX-License-Identifier: MIT
7 */
8
9 #include "../common/test-log.h"
10
11 typedef struct {
12 WpConf *conf;
13 } TestConfFixture;
14
15 static void
16 test_conf_setup (TestConfFixture *self, gconstpointer user_data)
17 {
18 g_autoptr (GError) error = NULL;
19 g_autofree gchar *file =
20 g_strdup_printf ("%s/conf/wireplumber.conf", g_getenv ("G_TEST_SRCDIR"));
21 self->conf = wp_conf_new_open (file, NULL, &error);
22 g_assert_no_error (error);
23 g_assert_nonnull (self->conf);
24 }
25
26 static void
27 test_conf_teardown (TestConfFixture *self, gconstpointer user_data)
28 {
29 g_clear_object (&self->conf);
30 }
31
32 static void
33 test_conf_basic (TestConfFixture *f, gconstpointer data)
34 {
35 g_assert_nonnull (f->conf);
36
37 /* Boolean Array */
38 {
39 g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
40 "wireplumber.section.array.boolean");
41 g_assert_nonnull (s);
42 g_assert_true (wp_spa_json_is_array (s));
43 gboolean v1 = FALSE, v2 = TRUE;
44 g_assert_true (wp_spa_json_parse_array (s, "b", &v1, "b", &v2, NULL));
45 g_assert_true (v1);
46 g_assert_false (v2);
47 }
48
49 /* Int Array */
50 {
51 g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
52 "wireplumber.section.array.int");
53 g_assert_nonnull (s);
54 g_assert_true (wp_spa_json_is_array (s));
55 gint v1 = 0, v2 = 0, v3 = 0;
56 g_assert_true (wp_spa_json_parse_array (s, "i", &v1, "i", &v2, "i", &v3,
57 NULL));
58 g_assert_cmpint (v1, ==, 1);
59 g_assert_cmpint (v2, ==, 2);
60 g_assert_cmpint (v3, ==, 3);
61 }
62
63 /* Float Array */
64 {
65 g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
66 "wireplumber.section.array.float");
67 g_assert_nonnull (s);
68 g_assert_true (wp_spa_json_is_array (s));
69 float v1 = 0.0, v2 = 0.0, v3 = 0.0;
70 g_assert_true (wp_spa_json_parse_array (s, "f", &v1, "f", &v2, "f", &v3,
71 NULL));
72 g_assert_cmpfloat_with_epsilon (v1, 1.11, 0.001);
73 g_assert_cmpfloat_with_epsilon (v2, 2.22, 0.001);
74 g_assert_cmpfloat_with_epsilon (v3, 3.33, 0.001);
75 }
76
77 /* String Array */
78 {
79 g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
80 "wireplumber.section.array.string");
81 g_assert_nonnull (s);
82 g_assert_true (wp_spa_json_is_array (s));
83 g_autofree gchar *v1 = NULL, *v2 = NULL;
84 g_assert_true (wp_spa_json_parse_array (s, "s", &v1, "s", &v2, NULL));
85 g_assert_nonnull (v1);
86 g_assert_nonnull (v2);
87 g_assert_cmpstr (v1, ==, "foo");
88 g_assert_cmpstr (v2, ==, "bar");
89 }
90
91 /* Array Array */
92 {
93 g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
94 "wireplumber.section.array.array");
95 g_assert_nonnull (s);
96 g_assert_true (wp_spa_json_is_array (s));
97 g_autoptr (WpSpaJson) v1 = NULL;
98 g_autoptr (WpSpaJson) v2 = NULL;
99 g_assert_true (wp_spa_json_parse_array (s, "J", &v1, "J", &v2, NULL));
100 g_assert_nonnull (v1);
101 g_assert_nonnull (v2);
102 g_assert_true (wp_spa_json_is_array (v1));
103 g_assert_true (wp_spa_json_is_array (v2));
104 gboolean v3 = FALSE, v4 = TRUE;
105 g_assert_true (wp_spa_json_parse_array (v1, "b", &v3, NULL));
106 g_assert_true (v3);
107 g_assert_true (wp_spa_json_parse_array (v2, "b", &v4, NULL));
108 g_assert_false (v4);
109 }
110
111 /* Object Array */
112 {
113 g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
114 "wireplumber.section.array.object");
115 g_assert_nonnull (s);
116 g_assert_true (wp_spa_json_is_array (s));
117 g_autoptr (WpSpaJson) v1 = NULL;
118 g_autoptr (WpSpaJson) v2 = NULL;
119 g_assert_true (wp_spa_json_parse_array (s, "J", &v1, "J", &v2, NULL));
120 g_assert_nonnull (v1);
121 g_assert_nonnull (v2);
122 g_assert_true (wp_spa_json_is_object (v1));
123 g_assert_true (wp_spa_json_is_object (v2));
124 g_autofree gchar *v3 = NULL;
125 gint v4 = 0;
126 g_assert_true (wp_spa_json_object_get (v1, "key1", "s", &v3, NULL));
127 g_assert_cmpstr (v3, ==, "foo");
128 g_assert_true (wp_spa_json_object_get (v2, "key2", "i", &v4, NULL));
129 g_assert_cmpint (v4, ==, 4);
130 }
131
132 /* Object */
133 {
134 g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
135 "wireplumber.section.object");
136 g_assert_nonnull (s);
137 g_assert_true (wp_spa_json_is_object (s));
138 gboolean v1 = FALSE;
139 gint v2 = 0;
140 float v3 = 0.0;
141 g_autofree gchar *v4 = NULL;
142 g_autoptr (WpSpaJson) v5 = NULL;
143 g_autoptr (WpSpaJson) v6 = NULL;
144 g_assert_true (wp_spa_json_object_get (s,
145 "key.boolean", "b", &v1,
146 "key.int", "i", &v2,
147 "key.float", "f", &v3,
148 "key.string", "s", &v4,
149 "key.array", "J", &v5,
150 "key.object", "J", &v6,
151 NULL));
152 g_assert_true (v1);
153 g_assert_cmpint (v2, ==, -1);
154 g_assert_cmpfloat_with_epsilon (v3, 3.14, 0.001);
155 g_assert_cmpstr (v4, ==, "wireplumber");
156 g_assert_true (wp_spa_json_is_array (v5));
157 g_autofree gchar *v7 = NULL, *v8 = NULL;
158 g_assert_true (wp_spa_json_parse_array (v5, "s", &v7, "s", &v8, NULL));
159 g_assert_cmpstr (v7, ==, "an");
160 g_assert_cmpstr (v8, ==, "array");
161 g_assert_true (wp_spa_json_is_object (v6));
162 gboolean v9 = TRUE;
163 g_assert_true (wp_spa_json_object_get (v6,
164 "key.nested.boolean", "b", &v9,
165 NULL));
166 g_assert_false (v9);
167 }
168 }
169
170 static void
171 test_conf_merge (TestConfFixture *f, gconstpointer data)
172 {
173 g_assert_nonnull (f->conf);
174
175 /* Boolean Array */
176 {
177 g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
178 "wireplumber.section-merged.array.boolean");
179 g_assert_nonnull (s);
180 g_assert_true (wp_spa_json_is_array (s));
181 gboolean v1 = TRUE, v2 = FALSE;
182 g_assert_true (wp_spa_json_parse_array (s, "b", &v1, "b", &v2, NULL));
183 g_assert_false (v1);
184 g_assert_true (v2);
185 }
186
187 /* Int Array */
188 {
189 g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
190 "wireplumber.section-merged.array.int");
191 g_assert_nonnull (s);
192 g_assert_true (wp_spa_json_is_array (s));
193 gint v1 = 0, v2 = 0;
194 g_assert_true (wp_spa_json_parse_array (s, "i", &v1, "i", &v2, NULL));
195 g_assert_cmpint (v1, ==, 4);
196 g_assert_cmpint (v2, ==, 5);
197 }
198
199 /* Float Array */
200 {
201 g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
202 "wireplumber.section-merged.array.float");
203 g_assert_nonnull (s);
204 g_assert_true (wp_spa_json_is_array (s));
205 float v1 = 0.0, v2 = 0.0;
206 g_assert_true (wp_spa_json_parse_array (s, "f", &v1, "f", &v2, NULL));
207 g_assert_cmpfloat_with_epsilon (v1, 4.44, 0.001);
208 g_assert_cmpfloat_with_epsilon (v2, 5.55, 0.001);
209 }
210
211 /* String Array */
212 {
213 g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
214 "wireplumber.section-merged.array.string");
215 g_assert_nonnull (s);
216 g_assert_true (wp_spa_json_is_array (s));
217 g_autofree gchar *v1 = NULL, *v2 = NULL;
218 g_assert_true (wp_spa_json_parse_array (s, "s", &v1, "s", &v2, NULL));
219 g_assert_nonnull (v1);
220 g_assert_nonnull (v2);
221 g_assert_cmpstr (v1, ==, "first");
222 g_assert_cmpstr (v2, ==, "second");
223 }
224
225 /* Array Array */
226 {
227 g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
228 "wireplumber.section-merged.array.array");
229 g_assert_nonnull (s);
230 g_assert_true (wp_spa_json_is_array (s));
231 g_autoptr (WpSpaJson) v1 = NULL;
232 g_autoptr (WpSpaJson) v2 = NULL;
233 g_assert_true (wp_spa_json_parse_array (s, "J", &v1, "J", &v2, NULL));
234 g_assert_nonnull (v1);
235 g_assert_nonnull (v2);
236 g_assert_true (wp_spa_json_is_array (v1));
237 g_assert_true (wp_spa_json_is_array (v2));
238 gboolean v3 = FALSE, v4 = TRUE;
239 g_assert_true (wp_spa_json_parse_array (v1, "b", &v3, NULL));
240 g_assert_true (v3);
241 g_assert_true (wp_spa_json_parse_array (v2, "b", &v4, NULL));
242 g_assert_false (v4);
243 }
244
245 /* Object Array */
246 {
247 g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
248 "wireplumber.section-merged.array.object");
249 g_assert_nonnull (s);
250 g_assert_true (wp_spa_json_is_array (s));
251 g_autoptr (WpSpaJson) v1 = NULL;
252 g_autoptr (WpSpaJson) v2 = NULL;
253 g_assert_true (wp_spa_json_parse_array (s, "J", &v1, "J", &v2, NULL));
254 g_assert_nonnull (v1);
255 g_assert_nonnull (v2);
256 g_assert_true (wp_spa_json_is_object (v1));
257 g_assert_true (wp_spa_json_is_object (v2));
258 g_autofree gchar *v3 = NULL;
259 gint v4 = 0;
260 g_assert_true (wp_spa_json_object_get (v1, "key1", "s", &v3, NULL));
261 g_assert_cmpstr (v3, ==, "foo");
262 g_assert_true (wp_spa_json_object_get (v2, "key2", "i", &v4, NULL));
263 g_assert_cmpint (v4, ==, 4);
264 }
265
266 /* Object */
267 {
268 g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
269 "wireplumber.section-merged.object");
270 g_assert_nonnull (s);
271 g_assert_true (wp_spa_json_is_object (s));
272 gboolean v1 = FALSE;
273 gint v2 = 0;
274 float v3 = 0.0;
275 g_autofree gchar *v4 = NULL;
276 g_autoptr (WpSpaJson) v5 = NULL;
277 g_autoptr (WpSpaJson) v6 = NULL;
278 g_assert_true (wp_spa_json_object_get (s,
279 "key.boolean", "b", &v1,
280 "key.int", "i", &v2,
281 "key.float", "f", &v3,
282 "key.string", "s", &v4,
283 "key.array", "J", &v5,
284 "key.object", "J", &v6,
285 NULL));
286 g_assert_false (v1);
287 g_assert_cmpint (v2, ==, 6);
288 g_assert_cmpfloat_with_epsilon (v3, 6.66, 0.001);
289 g_assert_cmpstr (v4, ==, "merged");
290 g_assert_true (wp_spa_json_is_array (v5));
291 g_autofree gchar *v7 = NULL, *v8 = NULL;
292 g_assert_true (wp_spa_json_parse_array (v5, "s", &v7, "s", &v8, NULL));
293 g_assert_cmpstr (v7, ==, "an");
294 g_assert_cmpstr (v8, ==, "array");
295 g_assert_true (wp_spa_json_is_object (v6));
296 gboolean v9 = TRUE;
297 g_assert_true (wp_spa_json_object_get (v6,
298 "key.nested.boolean", "b", &v9,
299 NULL));
300 g_assert_false (v9);
301 }
302 }
303
304 static void
305 test_conf_merge_nested (TestConfFixture *f, gconstpointer data)
306 {
307 g_assert_nonnull (f->conf);
308
309 g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
310 "wireplumber.section-nested-merged");
311 g_assert_nonnull (s);
312 g_assert_true (wp_spa_json_is_object (s));
313
314 /* Make sure both keys exist in the nested object */
315 {
316 g_autoptr (WpSpaJson) v1 = NULL;
317 g_assert_true (wp_spa_json_object_get (s, "nested-object", "J", &v1, NULL));
318 g_assert_nonnull (v1);
319 g_assert_true (wp_spa_json_is_object (v1));
320 gboolean v2 = FALSE;
321 g_assert_true (wp_spa_json_object_get (v1, "key1", "b", &v2, NULL));
322 gint v3 = 0;
323 g_assert_true (wp_spa_json_object_get (v1, "key2", "i", &v3, NULL));
324 g_assert_cmpint (v3, ==, 3);
325 }
326
327 /* Make sure array has all its elements */
328 {
329 g_autoptr (WpSpaJson) v1 = NULL;
330 g_assert_true (wp_spa_json_object_get (s, "nested-array", "J", &v1, NULL));
331 g_assert_nonnull (v1);
332 g_assert_true (wp_spa_json_is_array (v1));
333 gint v2 = 0, v3 = 0, v4 = 0, v5 = 0;
334 g_assert_true (wp_spa_json_parse_array (v1,
335 "i", &v2, "i", &v3, "i", &v4, "i", &v5, NULL));
336 g_assert_cmpint (v2, ==, 1);
337 g_assert_cmpint (v3, ==, 2);
338 g_assert_cmpint (v4, ==, 3);
339 g_assert_cmpint (v5, ==, 4);
340 }
341 }
342
343 static void
344 test_conf_override (TestConfFixture *f, gconstpointer data)
345 {
346 g_assert_nonnull (f->conf);
347
348 g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
349 "wireplumber.section-override");
350 g_assert_nonnull (s);
351 g_assert_true (wp_spa_json_is_object (s));
352
353 /* Make sure key1 does not exist because it was overridden */
354 gboolean v1 = FALSE;
355 g_assert_false (wp_spa_json_object_get (s, "key1", "b", &v1, NULL));
356
357 /* Make sure key2 exists */
358 gint v2 = 0;
359 g_assert_true (wp_spa_json_object_get (s, "key2", "i", &v2, NULL));
360 g_assert_cmpint (v2, ==, 5);
361 }
362
363 static void
364 test_conf_override_nested (TestConfFixture *f, gconstpointer data)
365 {
366 g_assert_nonnull (f->conf);
367
368 g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
369 "wireplumber.section-nested-override");
370 g_assert_nonnull (s);
371 g_assert_true (wp_spa_json_is_object (s));
372
373 g_autoptr (WpSpaJson) v1 = NULL;
374 g_assert_true (wp_spa_json_object_get (s, "nested-object", "J", &v1, NULL));
375 g_assert_nonnull (v1);
376 g_assert_true (wp_spa_json_is_object (v1));
377
378 /* Make sure key1 does not exist because it was overridden */
379 gboolean v2 = FALSE;
380 g_assert_false (wp_spa_json_object_get (v1, "key1", "b", &v2, NULL));
381
382 /* Make sure key2 exists */
383 gint v3 = 0;
384 g_assert_true (wp_spa_json_object_get (v1, "key2", "i", &v3, NULL));
385 g_assert_cmpint (v3, ==, 3);
386 }
387
388 static void
389 test_conf_as_section (TestConfFixture *f, gconstpointer data)
390 {
391 g_autoptr (GError) error = NULL;
392 g_autofree gchar *file =
393 g_strdup_printf ("%s/conf/section.conf", g_getenv ("G_TEST_SRCDIR"));
394 g_autoptr (WpProperties) props =
395 wp_properties_new ("as-section", "test", NULL);
396 f->conf = wp_conf_new_open (file, g_steal_pointer (&props), &error);
397 g_assert_no_error (error);
398 g_assert_nonnull (f->conf);
399
400 g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf, "test");
401 g_assert_nonnull (s);
402 g_assert_true (wp_spa_json_is_object (s));
403
404 g_autofree gchar *v = NULL;
405 g_assert_true (wp_spa_json_object_get (s,
406 "some", "s", &v,
407 NULL));
408 g_assert_cmpstr (v, ==, "json");
409
410 g_clear_object (&f->conf);
411 }
412
413 gint
414 main (gint argc, gchar *argv[])
415 {
416 g_test_init (&argc, &argv, NULL);
417 wp_init (WP_INIT_ALL);
418
419 g_test_add ("/wp/conf/basic", TestConfFixture, NULL,
420 test_conf_setup, test_conf_basic, test_conf_teardown);
421 g_test_add ("/wp/conf/merge", TestConfFixture, NULL,
422 test_conf_setup, test_conf_merge, test_conf_teardown);
423 g_test_add ("/wp/conf/merge_nested", TestConfFixture, NULL,
424 test_conf_setup, test_conf_merge_nested, test_conf_teardown);
425 g_test_add ("/wp/conf/override", TestConfFixture, NULL,
426 test_conf_setup, test_conf_override, test_conf_teardown);
427 g_test_add ("/wp/conf/override_nested", TestConfFixture, NULL,
428 test_conf_setup, test_conf_override_nested, test_conf_teardown);
429 g_test_add ("/wp/conf/as_section", TestConfFixture, NULL,
430 NULL, test_conf_as_section, NULL);
431
432 return g_test_run ();
433 }