summaryrefslogtreecommitdiff
path: root/tests/test-libglnx-backports.c
diff options
context:
space:
mode:
authorSimon McVittie <smcv@collabora.com>2022-11-08 11:05:12 +0000
committerSimon McVittie <smcv@collabora.com>2022-11-08 11:17:41 +0000
commitea18312ed03e0077740e327966a8e0e5810d7f5b (patch)
tree25f3c4e0e0dd4244b87ddcff3715e465287f5a79 /tests/test-libglnx-backports.c
parent4e44fd9c174e4196a86fb6d954722feaff612c88 (diff)
downloadlibglnx-ea18312ed03e0077740e327966a8e0e5810d7f5b.tar.gz
backports: Add g_steal_fd, from GLib >= 2.70
This is essentially the same as glnx_steal_fd, so make glnx_steal_fd an alias for it. The unit test is taken from GLib, slightly modified to avoid g_close() and also test the old name glnx_steal_fd(). Signed-off-by: Simon McVittie <smcv@collabora.com>
Diffstat (limited to 'tests/test-libglnx-backports.c')
-rw-r--r--tests/test-libglnx-backports.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/tests/test-libglnx-backports.c b/tests/test-libglnx-backports.c
index c475cd4..0fa374e 100644
--- a/tests/test-libglnx-backports.c
+++ b/tests/test-libglnx-backports.c
@@ -2,12 +2,15 @@
*
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
* Copyright 2019 Emmanuel Fleury
+ * Copyright 2021 Collabora Ltd.
* SPDX-License-Identifier: LGPL-2.1-or-later AND LicenseRef-old-glib-tests
*/
#include "libglnx-config.h"
#include "libglnx.h"
+#include <glib/gstdio.h>
+
/* Testing g_memdup2() function with various positive and negative cases */
static void
test_memdup2 (void)
@@ -28,9 +31,45 @@ test_memdup2 (void)
g_free (str_dup);
}
+static void
+test_steal_fd (void)
+{
+ GError *error = NULL;
+ gchar *tmpfile = NULL;
+ int fd = -42;
+ int borrowed;
+ int stolen;
+
+ g_assert_cmpint (g_steal_fd (&fd), ==, -42);
+ g_assert_cmpint (fd, ==, -1);
+ g_assert_cmpint (g_steal_fd (&fd), ==, -1);
+ g_assert_cmpint (fd, ==, -1);
+
+ fd = g_file_open_tmp (NULL, &tmpfile, &error);
+ g_assert_cmpint (fd, >=, 0);
+ g_assert_no_error (error);
+ borrowed = fd;
+ stolen = g_steal_fd (&fd);
+ g_assert_cmpint (fd, ==, -1);
+ g_assert_cmpint (borrowed, ==, stolen);
+
+ g_assert_no_errno (close (g_steal_fd (&stolen)));
+ g_assert_cmpint (stolen, ==, -1);
+
+ g_assert_no_errno (remove (tmpfile));
+ g_free (tmpfile);
+
+ /* Backwards compatibility with older libglnx: glnx_steal_fd is the same
+ * as g_steal_fd */
+ fd = -23;
+ g_assert_cmpint (glnx_steal_fd (&fd), ==, -23);
+ g_assert_cmpint (fd, ==, -1);
+}
+
int main (int argc, char **argv)
{
g_test_init (&argc, &argv, NULL);
+ g_test_add_func ("/mainloop/steal-fd", test_steal_fd);
g_test_add_func ("/strfuncs/memdup2", test_memdup2);
return g_test_run();
}