wireplumber/tests/wp/conf.c

434 lines
14 KiB
C
Raw Permalink Normal View History

2022-12-21 11:59:58 -05:00
/* WirePlumber
*
* Copyright © 2022 Collabora Ltd.
* @author Julian Bouzas <julian.bouzas@collabora.com>
*
* SPDX-License-Identifier: MIT
*/
conf: refactor configuration loading Changes: - Configuration files are no longer located by libpipewire, which allows us to control the paths that are being looked up. This is a requirement for installations where pipewire and wireplumber are built using different prefixes, in which case the configuration files of wireplumber end up being installed in a place that libpipewire doesn't look into... - The location of conf files is now again $prefix/share/wireplumber, /etc/wireplumber and $XDG_CONFIG_HOME/wireplumber, instead of using the pipewire directories. Also, since the previous commits, we now also support $XDG_CONFIG_DIRS/wireplumber (typically /etc/xdg/wireplumber) and $XDG_DATA_DIRS/wireplumber for system-wide configuration. - Since libpipewire doesn't expose the parser, we now also do the parsing of sections ourselves. This has the advantage that we can optimize it a bit for our use case. - The WpConf API has changed to not be a singleton and it is a property of WpCore instead. The configuration is now expected to be opened before the core is created, which allows the caller to identify configuration errors in advance. By not being a singleton, we can also reuse the WpConf API to open other SPA-JSON files. - WpConf also now has a lazy loading mechanism. The configuration files are mmap'ed and the various sections are located in advance, but not parsed until they are actually requested. Also, the sections are not copied in memory, unlike what happens in libpipewire. They are only copied when merging is needed. - WpCore now disables loading of a configuration file in pw_context, if a WpConf is provided. This is to have complete control here. The 'context.spa-libs' and 'context.modules' sections are still loaded, but we load them in WpConf and pass them down to pw_context for parsing. If a WpConf is not provided, pw_context is left to load the default configuration file (client.conf normally).
2024-02-28 12:11:38 +02:00
#include "../common/test-log.h"
2022-12-21 11:59:58 -05:00
typedef struct {
WpConf *conf;
} TestConfFixture;
static void
test_conf_setup (TestConfFixture *self, gconstpointer user_data)
{
conf: refactor configuration loading Changes: - Configuration files are no longer located by libpipewire, which allows us to control the paths that are being looked up. This is a requirement for installations where pipewire and wireplumber are built using different prefixes, in which case the configuration files of wireplumber end up being installed in a place that libpipewire doesn't look into... - The location of conf files is now again $prefix/share/wireplumber, /etc/wireplumber and $XDG_CONFIG_HOME/wireplumber, instead of using the pipewire directories. Also, since the previous commits, we now also support $XDG_CONFIG_DIRS/wireplumber (typically /etc/xdg/wireplumber) and $XDG_DATA_DIRS/wireplumber for system-wide configuration. - Since libpipewire doesn't expose the parser, we now also do the parsing of sections ourselves. This has the advantage that we can optimize it a bit for our use case. - The WpConf API has changed to not be a singleton and it is a property of WpCore instead. The configuration is now expected to be opened before the core is created, which allows the caller to identify configuration errors in advance. By not being a singleton, we can also reuse the WpConf API to open other SPA-JSON files. - WpConf also now has a lazy loading mechanism. The configuration files are mmap'ed and the various sections are located in advance, but not parsed until they are actually requested. Also, the sections are not copied in memory, unlike what happens in libpipewire. They are only copied when merging is needed. - WpCore now disables loading of a configuration file in pw_context, if a WpConf is provided. This is to have complete control here. The 'context.spa-libs' and 'context.modules' sections are still loaded, but we load them in WpConf and pass them down to pw_context for parsing. If a WpConf is not provided, pw_context is left to load the default configuration file (client.conf normally).
2024-02-28 12:11:38 +02:00
g_autoptr (GError) error = NULL;
g_autofree gchar *file =
2022-12-21 11:59:58 -05:00
g_strdup_printf ("%s/conf/wireplumber.conf", g_getenv ("G_TEST_SRCDIR"));
conf: refactor configuration loading Changes: - Configuration files are no longer located by libpipewire, which allows us to control the paths that are being looked up. This is a requirement for installations where pipewire and wireplumber are built using different prefixes, in which case the configuration files of wireplumber end up being installed in a place that libpipewire doesn't look into... - The location of conf files is now again $prefix/share/wireplumber, /etc/wireplumber and $XDG_CONFIG_HOME/wireplumber, instead of using the pipewire directories. Also, since the previous commits, we now also support $XDG_CONFIG_DIRS/wireplumber (typically /etc/xdg/wireplumber) and $XDG_DATA_DIRS/wireplumber for system-wide configuration. - Since libpipewire doesn't expose the parser, we now also do the parsing of sections ourselves. This has the advantage that we can optimize it a bit for our use case. - The WpConf API has changed to not be a singleton and it is a property of WpCore instead. The configuration is now expected to be opened before the core is created, which allows the caller to identify configuration errors in advance. By not being a singleton, we can also reuse the WpConf API to open other SPA-JSON files. - WpConf also now has a lazy loading mechanism. The configuration files are mmap'ed and the various sections are located in advance, but not parsed until they are actually requested. Also, the sections are not copied in memory, unlike what happens in libpipewire. They are only copied when merging is needed. - WpCore now disables loading of a configuration file in pw_context, if a WpConf is provided. This is to have complete control here. The 'context.spa-libs' and 'context.modules' sections are still loaded, but we load them in WpConf and pass them down to pw_context for parsing. If a WpConf is not provided, pw_context is left to load the default configuration file (client.conf normally).
2024-02-28 12:11:38 +02:00
self->conf = wp_conf_new_open (file, NULL, &error);
g_assert_no_error (error);
g_assert_nonnull (self->conf);
2022-12-21 11:59:58 -05:00
}
static void
test_conf_teardown (TestConfFixture *self, gconstpointer user_data)
{
g_clear_object (&self->conf);
}
static void
test_conf_basic (TestConfFixture *f, gconstpointer data)
{
g_assert_nonnull (f->conf);
/* Boolean Array */
{
g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
"wireplumber.section.array.boolean");
2022-12-21 11:59:58 -05:00
g_assert_nonnull (s);
g_assert_true (wp_spa_json_is_array (s));
gboolean v1 = FALSE, v2 = TRUE;
g_assert_true (wp_spa_json_parse_array (s, "b", &v1, "b", &v2, NULL));
g_assert_true (v1);
g_assert_false (v2);
}
/* Int Array */
{
g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
"wireplumber.section.array.int");
2022-12-21 11:59:58 -05:00
g_assert_nonnull (s);
g_assert_true (wp_spa_json_is_array (s));
gint v1 = 0, v2 = 0, v3 = 0;
g_assert_true (wp_spa_json_parse_array (s, "i", &v1, "i", &v2, "i", &v3,
NULL));
g_assert_cmpint (v1, ==, 1);
g_assert_cmpint (v2, ==, 2);
g_assert_cmpint (v3, ==, 3);
}
/* Float Array */
{
g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
"wireplumber.section.array.float");
2022-12-21 11:59:58 -05:00
g_assert_nonnull (s);
g_assert_true (wp_spa_json_is_array (s));
float v1 = 0.0, v2 = 0.0, v3 = 0.0;
g_assert_true (wp_spa_json_parse_array (s, "f", &v1, "f", &v2, "f", &v3,
NULL));
g_assert_cmpfloat_with_epsilon (v1, 1.11, 0.001);
g_assert_cmpfloat_with_epsilon (v2, 2.22, 0.001);
g_assert_cmpfloat_with_epsilon (v3, 3.33, 0.001);
}
/* String Array */
{
g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
"wireplumber.section.array.string");
2022-12-21 11:59:58 -05:00
g_assert_nonnull (s);
g_assert_true (wp_spa_json_is_array (s));
g_autofree gchar *v1 = NULL, *v2 = NULL;
g_assert_true (wp_spa_json_parse_array (s, "s", &v1, "s", &v2, NULL));
g_assert_nonnull (v1);
g_assert_nonnull (v2);
g_assert_cmpstr (v1, ==, "foo");
g_assert_cmpstr (v2, ==, "bar");
}
/* Array Array */
{
g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
"wireplumber.section.array.array");
2022-12-21 11:59:58 -05:00
g_assert_nonnull (s);
g_assert_true (wp_spa_json_is_array (s));
g_autoptr (WpSpaJson) v1 = NULL;
g_autoptr (WpSpaJson) v2 = NULL;
g_assert_true (wp_spa_json_parse_array (s, "J", &v1, "J", &v2, NULL));
g_assert_nonnull (v1);
g_assert_nonnull (v2);
g_assert_true (wp_spa_json_is_array (v1));
g_assert_true (wp_spa_json_is_array (v2));
gboolean v3 = FALSE, v4 = TRUE;
g_assert_true (wp_spa_json_parse_array (v1, "b", &v3, NULL));
g_assert_true (v3);
g_assert_true (wp_spa_json_parse_array (v2, "b", &v4, NULL));
g_assert_false (v4);
}
/* Object Array */
{
g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
"wireplumber.section.array.object");
2022-12-21 11:59:58 -05:00
g_assert_nonnull (s);
g_assert_true (wp_spa_json_is_array (s));
g_autoptr (WpSpaJson) v1 = NULL;
g_autoptr (WpSpaJson) v2 = NULL;
g_assert_true (wp_spa_json_parse_array (s, "J", &v1, "J", &v2, NULL));
g_assert_nonnull (v1);
g_assert_nonnull (v2);
g_assert_true (wp_spa_json_is_object (v1));
g_assert_true (wp_spa_json_is_object (v2));
g_autofree gchar *v3 = NULL;
gint v4 = 0;
g_assert_true (wp_spa_json_object_get (v1, "key1", "s", &v3, NULL));
g_assert_cmpstr (v3, ==, "foo");
g_assert_true (wp_spa_json_object_get (v2, "key2", "i", &v4, NULL));
g_assert_cmpint (v4, ==, 4);
}
/* Object */
{
g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
"wireplumber.section.object");
2022-12-21 11:59:58 -05:00
g_assert_nonnull (s);
g_assert_true (wp_spa_json_is_object (s));
gboolean v1 = FALSE;
gint v2 = 0;
float v3 = 0.0;
g_autofree gchar *v4 = NULL;
g_autoptr (WpSpaJson) v5 = NULL;
g_autoptr (WpSpaJson) v6 = NULL;
g_assert_true (wp_spa_json_object_get (s,
"key.boolean", "b", &v1,
"key.int", "i", &v2,
"key.float", "f", &v3,
"key.string", "s", &v4,
"key.array", "J", &v5,
"key.object", "J", &v6,
NULL));
g_assert_true (v1);
g_assert_cmpint (v2, ==, -1);
g_assert_cmpfloat_with_epsilon (v3, 3.14, 0.001);
g_assert_cmpstr (v4, ==, "wireplumber");
g_assert_true (wp_spa_json_is_array (v5));
g_autofree gchar *v7 = NULL, *v8 = NULL;
g_assert_true (wp_spa_json_parse_array (v5, "s", &v7, "s", &v8, NULL));
g_assert_cmpstr (v7, ==, "an");
g_assert_cmpstr (v8, ==, "array");
g_assert_true (wp_spa_json_is_object (v6));
gboolean v9 = TRUE;
g_assert_true (wp_spa_json_object_get (v6,
"key.nested.boolean", "b", &v9,
NULL));
g_assert_false (v9);
}
}
static void
test_conf_merge (TestConfFixture *f, gconstpointer data)
{
g_assert_nonnull (f->conf);
/* Boolean Array */
{
g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
"wireplumber.section-merged.array.boolean");
2022-12-21 11:59:58 -05:00
g_assert_nonnull (s);
g_assert_true (wp_spa_json_is_array (s));
gboolean v1 = TRUE, v2 = FALSE;
g_assert_true (wp_spa_json_parse_array (s, "b", &v1, "b", &v2, NULL));
g_assert_false (v1);
g_assert_true (v2);
}
/* Int Array */
{
g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
"wireplumber.section-merged.array.int");
2022-12-21 11:59:58 -05:00
g_assert_nonnull (s);
g_assert_true (wp_spa_json_is_array (s));
gint v1 = 0, v2 = 0;
g_assert_true (wp_spa_json_parse_array (s, "i", &v1, "i", &v2, NULL));
g_assert_cmpint (v1, ==, 4);
g_assert_cmpint (v2, ==, 5);
}
/* Float Array */
{
g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
"wireplumber.section-merged.array.float");
2022-12-21 11:59:58 -05:00
g_assert_nonnull (s);
g_assert_true (wp_spa_json_is_array (s));
float v1 = 0.0, v2 = 0.0;
g_assert_true (wp_spa_json_parse_array (s, "f", &v1, "f", &v2, NULL));
g_assert_cmpfloat_with_epsilon (v1, 4.44, 0.001);
g_assert_cmpfloat_with_epsilon (v2, 5.55, 0.001);
}
/* String Array */
{
g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
"wireplumber.section-merged.array.string");
2022-12-21 11:59:58 -05:00
g_assert_nonnull (s);
g_assert_true (wp_spa_json_is_array (s));
g_autofree gchar *v1 = NULL, *v2 = NULL;
g_assert_true (wp_spa_json_parse_array (s, "s", &v1, "s", &v2, NULL));
g_assert_nonnull (v1);
g_assert_nonnull (v2);
g_assert_cmpstr (v1, ==, "first");
g_assert_cmpstr (v2, ==, "second");
}
/* Array Array */
{
g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
"wireplumber.section-merged.array.array");
2022-12-21 11:59:58 -05:00
g_assert_nonnull (s);
g_assert_true (wp_spa_json_is_array (s));
g_autoptr (WpSpaJson) v1 = NULL;
g_autoptr (WpSpaJson) v2 = NULL;
g_assert_true (wp_spa_json_parse_array (s, "J", &v1, "J", &v2, NULL));
g_assert_nonnull (v1);
g_assert_nonnull (v2);
g_assert_true (wp_spa_json_is_array (v1));
g_assert_true (wp_spa_json_is_array (v2));
gboolean v3 = FALSE, v4 = TRUE;
g_assert_true (wp_spa_json_parse_array (v1, "b", &v3, NULL));
g_assert_true (v3);
g_assert_true (wp_spa_json_parse_array (v2, "b", &v4, NULL));
g_assert_false (v4);
}
/* Object Array */
{
g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
"wireplumber.section-merged.array.object");
2022-12-21 11:59:58 -05:00
g_assert_nonnull (s);
g_assert_true (wp_spa_json_is_array (s));
g_autoptr (WpSpaJson) v1 = NULL;
g_autoptr (WpSpaJson) v2 = NULL;
g_assert_true (wp_spa_json_parse_array (s, "J", &v1, "J", &v2, NULL));
g_assert_nonnull (v1);
g_assert_nonnull (v2);
g_assert_true (wp_spa_json_is_object (v1));
g_assert_true (wp_spa_json_is_object (v2));
g_autofree gchar *v3 = NULL;
gint v4 = 0;
g_assert_true (wp_spa_json_object_get (v1, "key1", "s", &v3, NULL));
g_assert_cmpstr (v3, ==, "foo");
g_assert_true (wp_spa_json_object_get (v2, "key2", "i", &v4, NULL));
g_assert_cmpint (v4, ==, 4);
}
/* Object */
{
g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
"wireplumber.section-merged.object");
2022-12-21 11:59:58 -05:00
g_assert_nonnull (s);
g_assert_true (wp_spa_json_is_object (s));
gboolean v1 = FALSE;
gint v2 = 0;
float v3 = 0.0;
g_autofree gchar *v4 = NULL;
g_autoptr (WpSpaJson) v5 = NULL;
g_autoptr (WpSpaJson) v6 = NULL;
g_assert_true (wp_spa_json_object_get (s,
"key.boolean", "b", &v1,
"key.int", "i", &v2,
"key.float", "f", &v3,
"key.string", "s", &v4,
"key.array", "J", &v5,
"key.object", "J", &v6,
NULL));
g_assert_false (v1);
g_assert_cmpint (v2, ==, 6);
g_assert_cmpfloat_with_epsilon (v3, 6.66, 0.001);
g_assert_cmpstr (v4, ==, "merged");
g_assert_true (wp_spa_json_is_array (v5));
g_autofree gchar *v7 = NULL, *v8 = NULL;
g_assert_true (wp_spa_json_parse_array (v5, "s", &v7, "s", &v8, NULL));
g_assert_cmpstr (v7, ==, "an");
g_assert_cmpstr (v8, ==, "array");
g_assert_true (wp_spa_json_is_object (v6));
gboolean v9 = TRUE;
g_assert_true (wp_spa_json_object_get (v6,
"key.nested.boolean", "b", &v9,
NULL));
g_assert_false (v9);
}
}
static void
test_conf_merge_nested (TestConfFixture *f, gconstpointer data)
{
g_assert_nonnull (f->conf);
g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
"wireplumber.section-nested-merged");
2022-12-21 11:59:58 -05:00
g_assert_nonnull (s);
g_assert_true (wp_spa_json_is_object (s));
/* Make sure both keys exist in the nested object */
{
g_autoptr (WpSpaJson) v1 = NULL;
g_assert_true (wp_spa_json_object_get (s, "nested-object", "J", &v1, NULL));
g_assert_nonnull (v1);
g_assert_true (wp_spa_json_is_object (v1));
gboolean v2 = FALSE;
g_assert_true (wp_spa_json_object_get (v1, "key1", "b", &v2, NULL));
gint v3 = 0;
g_assert_true (wp_spa_json_object_get (v1, "key2", "i", &v3, NULL));
g_assert_cmpint (v3, ==, 3);
}
/* Make sure array has all its elements */
{
g_autoptr (WpSpaJson) v1 = NULL;
g_assert_true (wp_spa_json_object_get (s, "nested-array", "J", &v1, NULL));
g_assert_nonnull (v1);
g_assert_true (wp_spa_json_is_array (v1));
gint v2 = 0, v3 = 0, v4 = 0, v5 = 0;
g_assert_true (wp_spa_json_parse_array (v1,
"i", &v2, "i", &v3, "i", &v4, "i", &v5, NULL));
g_assert_cmpint (v2, ==, 1);
g_assert_cmpint (v3, ==, 2);
g_assert_cmpint (v4, ==, 3);
g_assert_cmpint (v5, ==, 4);
}
}
static void
test_conf_override (TestConfFixture *f, gconstpointer data)
{
g_assert_nonnull (f->conf);
g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
"wireplumber.section-override");
g_assert_nonnull (s);
g_assert_true (wp_spa_json_is_object (s));
/* Make sure key1 does not exist because it was overridden */
gboolean v1 = FALSE;
g_assert_false (wp_spa_json_object_get (s, "key1", "b", &v1, NULL));
/* Make sure key2 exists */
gint v2 = 0;
g_assert_true (wp_spa_json_object_get (s, "key2", "i", &v2, NULL));
g_assert_cmpint (v2, ==, 5);
}
static void
test_conf_override_nested (TestConfFixture *f, gconstpointer data)
{
g_assert_nonnull (f->conf);
g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf,
"wireplumber.section-nested-override");
g_assert_nonnull (s);
g_assert_true (wp_spa_json_is_object (s));
g_autoptr (WpSpaJson) v1 = NULL;
g_assert_true (wp_spa_json_object_get (s, "nested-object", "J", &v1, NULL));
g_assert_nonnull (v1);
g_assert_true (wp_spa_json_is_object (v1));
/* Make sure key1 does not exist because it was overridden */
gboolean v2 = FALSE;
g_assert_false (wp_spa_json_object_get (v1, "key1", "b", &v2, NULL));
/* Make sure key2 exists */
gint v3 = 0;
g_assert_true (wp_spa_json_object_get (v1, "key2", "i", &v3, NULL));
g_assert_cmpint (v3, ==, 3);
}
static void
test_conf_as_section (TestConfFixture *f, gconstpointer data)
{
g_autoptr (GError) error = NULL;
g_autofree gchar *file =
g_strdup_printf ("%s/conf/section.conf", g_getenv ("G_TEST_SRCDIR"));
g_autoptr (WpProperties) props =
wp_properties_new ("as-section", "test", NULL);
f->conf = wp_conf_new_open (file, g_steal_pointer (&props), &error);
g_assert_no_error (error);
g_assert_nonnull (f->conf);
g_autoptr (WpSpaJson) s = wp_conf_get_section (f->conf, "test");
g_assert_nonnull (s);
g_assert_true (wp_spa_json_is_object (s));
g_autofree gchar *v = NULL;
g_assert_true (wp_spa_json_object_get (s,
"some", "s", &v,
NULL));
g_assert_cmpstr (v, ==, "json");
g_clear_object (&f->conf);
}
2022-12-21 11:59:58 -05:00
gint
main (gint argc, gchar *argv[])
{
g_test_init (&argc, &argv, NULL);
wp_init (WP_INIT_ALL);
g_test_add ("/wp/conf/basic", TestConfFixture, NULL,
test_conf_setup, test_conf_basic, test_conf_teardown);
g_test_add ("/wp/conf/merge", TestConfFixture, NULL,
test_conf_setup, test_conf_merge, test_conf_teardown);
g_test_add ("/wp/conf/merge_nested", TestConfFixture, NULL,
test_conf_setup, test_conf_merge_nested, test_conf_teardown);
g_test_add ("/wp/conf/override", TestConfFixture, NULL,
test_conf_setup, test_conf_override, test_conf_teardown);
g_test_add ("/wp/conf/override_nested", TestConfFixture, NULL,
test_conf_setup, test_conf_override_nested, test_conf_teardown);
g_test_add ("/wp/conf/as_section", TestConfFixture, NULL,
NULL, test_conf_as_section, NULL);
2022-12-21 11:59:58 -05:00
return g_test_run ();
}