From b5c97b1016e5fac75c3309c97d34f7afed35b2cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Tue, 17 May 2022 13:05:57 +0200 Subject: [PATCH] gio/tests: add basic fd-list unit test to unix-fd MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To cover win32 support. Signed-off-by: Marc-André Lureau --- gio/tests/unix-fd.c | 60 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/gio/tests/unix-fd.c b/gio/tests/unix-fd.c index c12d83461..ecf1aea37 100644 --- a/gio/tests/unix-fd.c +++ b/gio/tests/unix-fd.c @@ -7,7 +7,11 @@ #include #endif #include +#include #include +#ifdef G_OS_WIN32 +#include +#endif /* ensures that no FDs are left open at the end */ static void @@ -42,6 +46,61 @@ create_fd_list (gint *fd_list) g_close (fd_list[i], NULL); } +static void +test_fd_list (void) +{ + GError *err = NULL; + GUnixFDList *list; + const gint *peek; + gint *stolen; + gint fd_list[40]; + gint sv[3]; + gint s; + + create_fd_list (fd_list); + sv[2] = -1; +#ifdef G_OS_WIN32 + s = _pipe (sv, 4096, _O_NOINHERIT | _O_BINARY); + g_assert_cmpint (s, ==, 0); +#else + g_unix_open_pipe (sv, FD_CLOEXEC, &err); + g_assert_no_error (err); +#endif + list = g_unix_fd_list_new_from_array (sv, -1); + peek = g_unix_fd_list_peek_fds (list, &s); + g_assert_cmpint (s, ==, 2); + g_assert_cmpint (peek[0], ==, sv[0]); + g_assert_cmpint (peek[1], ==, sv[1]); + + s = g_unix_fd_list_get (list, 0, &err); + g_assert_no_error (err); + g_close (s, &err); + g_assert_no_error (err); + s = g_unix_fd_list_get (list, 1, &err); + g_assert_no_error (err); + g_close (s, &err); + g_assert_no_error (err); + + s = g_unix_fd_list_append (list, sv[0], &err); + g_assert_no_error (err); + g_assert_cmpint (s, >=, 0); + stolen = g_unix_fd_list_steal_fds (list, &s); + g_assert_cmpint (s, ==, 3); + g_assert_cmpint (stolen[0], ==, sv[0]); + g_assert_cmpint (stolen[1], ==, sv[1]); + g_assert_cmpint (stolen[2], >=, 0); + g_close (stolen[0], &err); + g_assert_no_error (err); + g_close (stolen[1], &err); + g_assert_no_error (err); + g_close (stolen[2], &err); + g_assert_no_error (err); + g_free (stolen); + + g_object_unref (list); + check_fd_list (fd_list); +} + static void test_scm (void) { @@ -238,6 +297,7 @@ main (int argc, char **argv) { g_test_init (&argc, &argv, NULL); + g_test_add_func ("/unix-fd/fd-list", test_fd_list); g_test_add_func ("/unix-fd/scm", test_scm); return g_test_run();