From 2e28c9aea18d1740cc8bdf8d618166934bddfdb3 Mon Sep 17 00:00:00 2001 From: George Kiagiadakis <george.kiagiadakis@collabora.com> Date: Sun, 25 Aug 2019 12:45:24 +0300 Subject: [PATCH] tests: add unit test for WpProxy This also adds a "test server" helper, which is used to start a local pipewire server in the same process, for testing functionality that requires a server. --- tests/meson.build | 6 ++ tests/proxy.c | 148 ++++++++++++++++++++++++++++++++++++++++++++ tests/test-server.h | 47 ++++++++++++++ 3 files changed, 201 insertions(+) create mode 100644 tests/proxy.c create mode 100644 tests/test-server.h diff --git a/tests/meson.build b/tests/meson.build index 9b384426..655092e1 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -9,3 +9,9 @@ test( executable('test-properties', 'properties.c', dependencies: common_deps), env: common_env, ) + +test( + 'test-proxy', + executable('test-proxy', 'proxy.c', dependencies: common_deps), + env: common_env, +) diff --git a/tests/proxy.c b/tests/proxy.c new file mode 100644 index 00000000..0d39c576 --- /dev/null +++ b/tests/proxy.c @@ -0,0 +1,148 @@ +/* WirePlumber + * + * Copyright © 2019 Collabora Ltd. + * @author George Kiagiadakis <george.kiagiadakis@collabora.com> + * + * SPDX-License-Identifier: MIT + */ + +#include <wp/wp.h> +#include <pipewire/pipewire.h> + +#include "test-server.h" + +typedef struct { + /* the local pipewire server */ + WpTestServer server; + + /* the main loop */ + GMainLoop *loop; + + /* the client wireplumber objects */ + WpCore *core; + WpRemote *remote; + +} TestProxyFixture; + +static void +test_proxy_setup (TestProxyFixture *self, gconstpointer user_data) +{ + wp_test_server_setup (&self->server); + g_setenv ("PIPEWIRE_REMOTE", self->server.name, TRUE); + self->loop = g_main_loop_new (NULL, FALSE); + self->core = wp_core_new (); + self->remote = wp_remote_pipewire_new (self->core, NULL); +} + +static void +test_proxy_teardown (TestProxyFixture *self, gconstpointer user_data) +{ + g_clear_object (&self->remote); + g_clear_object (&self->core); + g_clear_pointer (&self->loop, g_main_loop_unref); + g_unsetenv ("PIPEWIRE_REMOTE"); + wp_test_server_teardown (&self->server); +} + +static void +test_proxy_state_changed (WpRemote *remote, WpRemoteState state, + TestProxyFixture *fixture) +{ + g_autofree gchar * msg = NULL; + + switch (state) { + case WP_REMOTE_STATE_ERROR: + g_object_get (remote, "error-message", &msg, NULL); + g_message ("remote error: %s", msg); + g_test_fail (); + g_main_loop_quit (fixture->loop); + break; + default: + break; + } +} + +static void +test_proxy_basic_augmented (WpProxy *proxy, GAsyncResult *res, + TestProxyFixture *fixture) +{ + g_autoptr (GError) error = NULL; + g_assert_true (wp_proxy_augment_finish (proxy, res, &error)); + + g_assert_true (wp_proxy_get_features (proxy) & WP_PROXY_FEATURE_PW_PROXY); + g_assert_nonnull (wp_proxy_get_pw_proxy (proxy)); + + g_main_loop_quit (fixture->loop); +} + +static void +test_proxy_basic_global_added (WpRemote *remote, WpProxy *proxy, + TestProxyFixture *fixture) +{ + g_assert_nonnull (proxy); + { + g_autoptr (WpRemote) remote = wp_proxy_get_remote (proxy); + g_assert_nonnull (remote); + } + g_assert_cmpuint (wp_proxy_get_global_id (proxy), !=, 0); + g_assert_true (wp_proxy_is_global (proxy)); + g_assert_cmpuint (wp_proxy_get_interface_quark (proxy), ==, + g_quark_from_string ("client")); + g_assert_cmpuint (wp_proxy_get_interface_type (proxy), ==, + PW_TYPE_INTERFACE_Client); + g_assert_cmpstr (wp_proxy_get_interface_name (proxy), ==, + "PipeWire:Interface:Client"); + g_assert_cmphex (wp_proxy_get_global_permissions (proxy), ==, PW_PERM_RWX); + + g_assert_cmphex (wp_proxy_get_features (proxy), ==, 0); + g_assert_null (wp_proxy_get_pw_proxy (proxy)); + g_assert_null (wp_proxy_get_native_info (proxy)); + + { + g_autoptr (WpProperties) props = wp_proxy_get_global_properties (proxy); + g_assert_nonnull (props); + g_assert_cmpstr (wp_properties_get (props, PW_KEY_PROTOCOL), ==, + "protocol-native"); + } + + wp_proxy_augment (proxy, WP_PROXY_FEATURE_PW_PROXY, NULL, + (GAsyncReadyCallback) test_proxy_basic_augmented, fixture); +} + +static gboolean +timeout_callback (TestProxyFixture *fixture) +{ + g_message ("test timed out"); + g_test_fail (); + g_main_loop_quit (fixture->loop); + + return G_SOURCE_REMOVE; +} + +static void +test_proxy_basic (TestProxyFixture *fixture, gconstpointer data) +{ + g_signal_connect (fixture->remote, "state-changed", + (GCallback) test_proxy_state_changed, fixture); + + /* our test server should advertise exactly one + * client: our WpRemote; use this to test WpProxy */ + g_signal_connect (fixture->remote, "global-added::client", + (GCallback) test_proxy_basic_global_added, fixture); + + g_assert_true (wp_remote_connect (fixture->remote)); + g_timeout_add_seconds (3, (GSourceFunc) timeout_callback, fixture); + g_main_loop_run (fixture->loop); +} + +gint +main (gint argc, gchar *argv[]) +{ + g_test_init (&argc, &argv, NULL); + pw_init (NULL, NULL); + + g_test_add ("/wp/proxy/basic", TestProxyFixture, NULL, + test_proxy_setup, test_proxy_basic, test_proxy_teardown); + + return g_test_run (); +} diff --git a/tests/test-server.h b/tests/test-server.h new file mode 100644 index 00000000..23eafb10 --- /dev/null +++ b/tests/test-server.h @@ -0,0 +1,47 @@ +/* WirePlumber + * + * Copyright © 2019 Collabora Ltd. + * @author George Kiagiadakis <george.kiagiadakis@collabora.com> + * + * SPDX-License-Identifier: MIT + */ + +#include <pipewire/pipewire.h> + +typedef struct { + gchar *name; + struct pw_core *core; + struct pw_loop *loop; + struct pw_thread_loop *thread_loop; +} WpTestServer; + +static inline void +wp_test_server_setup (WpTestServer *self) +{ + struct pw_properties *properties; + + self->name = g_strdup_printf ("wp-test-server-%d-%d", getpid(), + g_random_int ()); + properties = pw_properties_new( + PW_KEY_CORE_DAEMON, "1", + PW_KEY_CORE_NAME, self->name, + NULL); + + self->loop = pw_loop_new (NULL); + self->thread_loop = pw_thread_loop_new (self->loop, "wp-test-server"); + self->core = pw_core_new (self->loop, properties, 0); + + pw_module_load (self->core, "libpipewire-module-protocol-native", NULL, NULL); + pw_module_load (self->core, "libpipewire-module-access", NULL, NULL); + + pw_thread_loop_start (self->thread_loop); +} + +static inline void +wp_test_server_teardown (WpTestServer *self) +{ + pw_thread_loop_stop (self->thread_loop); + pw_core_destroy (self->core); + pw_thread_loop_destroy (self->thread_loop); + pw_loop_destroy (self->loop); +} -- GitLab