| 1 |
/* WirePlumber |
| 2 |
* |
| 3 |
* Copyright © 2021 Collabora Ltd. |
| 4 |
* @author Julian Bouzas <[email protected]> |
| 5 |
* |
| 6 |
* SPDX-License-Identifier: MIT |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <fcntl.h> |
| 10 |
#include <sys/inotify.h> |
| 11 |
|
| 12 |
#include "../common/base-test-fixture.h" |
| 13 |
|
| 14 |
typedef struct { |
| 15 |
WpBaseTestFixture base; |
| 16 |
WpPlugin *plugin; |
| 17 |
gchar *path; |
| 18 |
gchar *file; |
| 19 |
gchar *evtype; |
| 20 |
} TestFixture; |
| 21 |
|
| 22 |
static void |
| 23 |
on_plugin_loaded (WpCore * core, GAsyncResult * res, TestFixture *f) |
| 24 |
{ |
| 25 |
gboolean loaded; |
| 26 |
GError *error = NULL; |
| 27 |
|
| 28 |
loaded = wp_core_load_component_finish (core, res, &error); |
| 29 |
g_assert_no_error (error); |
| 30 |
g_assert_true (loaded); |
| 31 |
|
| 32 |
g_main_loop_quit (f->base.loop); |
| 33 |
} |
| 34 |
|
| 35 |
static void |
| 36 |
test_file_monitor_setup (TestFixture * f, gconstpointer user_data) |
| 37 |
{ |
| 38 |
wp_base_test_fixture_setup (&f->base, WP_BASE_TEST_FLAG_DONT_CONNECT); |
| 39 |
|
| 40 |
wp_core_load_component (f->base.core, |
| 41 |
"libwireplumber-module-file-monitor-api", "module", NULL, NULL, NULL, |
| 42 |
(GAsyncReadyCallback) on_plugin_loaded, f); |
| 43 |
g_main_loop_run (f->base.loop); |
| 44 |
|
| 45 |
f->plugin = wp_plugin_find (f->base.core, "file-monitor-api"); |
| 46 |
g_assert_nonnull (f->plugin); |
| 47 |
|
| 48 |
f->path = g_strdup (g_getenv ("FILE_MONITOR_DIR")); |
| 49 |
g_mkdir_with_parents (f->path, 0700); |
| 50 |
} |
| 51 |
|
| 52 |
static void |
| 53 |
test_file_monitor_teardown (TestFixture * f, gconstpointer user_data) |
| 54 |
{ |
| 55 |
g_clear_pointer (&f->evtype, g_free); |
| 56 |
g_clear_pointer (&f->file, g_free); |
| 57 |
g_clear_pointer (&f->path, g_free); |
| 58 |
g_clear_object (&f->plugin); |
| 59 |
wp_base_test_fixture_teardown (&f->base); |
| 60 |
} |
| 61 |
|
| 62 |
static void |
| 63 |
on_changed (WpPlugin *plugin, const gchar *file, const gchar *old, |
| 64 |
const char *evtype, TestFixture * f) |
| 65 |
{ |
| 66 |
g_assert_nonnull (file); |
| 67 |
g_assert_nonnull (evtype); |
| 68 |
f->file = g_strdup (file); |
| 69 |
f->evtype = g_strdup (evtype); |
| 70 |
g_main_loop_quit (f->base.loop); |
| 71 |
} |
| 72 |
|
| 73 |
static void |
| 74 |
test_file_monitor_basic (TestFixture * f, gconstpointer user_data) |
| 75 |
{ |
| 76 |
gboolean res = FALSE; |
| 77 |
|
| 78 |
/* delete the 'foo' file if it exists in path */ |
| 79 |
g_autofree gchar *filename = g_build_filename (f->path, "foo", NULL); |
| 80 |
(void) remove (filename); |
| 81 |
|
| 82 |
/* handle changed signal */ |
| 83 |
f->file = NULL; |
| 84 |
f->evtype = NULL; |
| 85 |
g_signal_connect (f->plugin, "changed", G_CALLBACK (on_changed), f); |
| 86 |
|
| 87 |
/* add watch */ |
| 88 |
g_signal_emit_by_name (f->plugin, "add-watch", f->path, "m", &res); |
| 89 |
g_assert_true (res); |
| 90 |
|
| 91 |
/* create the foo file in path */ |
| 92 |
int fd = open (filename, O_CREAT | O_EXCL, 0700); |
| 93 |
g_assert_cmpint (fd, >=, 0); |
| 94 |
|
| 95 |
/* run */ |
| 96 |
g_main_loop_run (f->base.loop); |
| 97 |
g_assert_cmpstr (f->file, ==, filename); |
| 98 |
g_assert_cmpstr (f->evtype, ==, "created"); |
| 99 |
|
| 100 |
/* removed watch */ |
| 101 |
g_signal_emit_by_name (f->plugin, "remove-watch", f->path); |
| 102 |
|
| 103 |
/* remove 'foo' */ |
| 104 |
close (fd); |
| 105 |
(void) remove (filename); |
| 106 |
} |
| 107 |
|
| 108 |
gint |
| 109 |
main (gint argc, gchar *argv[]) |
| 110 |
{ |
| 111 |
g_test_init (&argc, &argv, NULL); |
| 112 |
wp_init (WP_INIT_ALL); |
| 113 |
|
| 114 |
g_test_add ("/modules/file-monitor/basic", |
| 115 |
TestFixture, NULL, |
| 116 |
test_file_monitor_setup, |
| 117 |
test_file_monitor_basic, |
| 118 |
test_file_monitor_teardown); |
| 119 |
|
| 120 |
return g_test_run (); |
| 121 |
} |