| 1 |
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- |
| 2 |
* |
| 3 |
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald |
| 4 |
* Copyright (C) 2011 Red Hat, Inc. |
| 5 |
* Copyright (C) 2018 Endless OS Foundation, LLC |
| 6 |
* Copyright 2019 Emmanuel Fleury |
| 7 |
* Copyright 2021-2024 Collabora Ltd. |
| 8 |
* SPDX-License-Identifier: LGPL-2.1-or-later AND LicenseRef-old-glib-tests |
| 9 |
*/ |
| 10 |
|
| 11 |
#include "libglnx-config.h" |
| 12 |
#include "libglnx.h" |
| 13 |
|
| 14 |
#include <glib/gstdio.h> |
| 15 |
#include <glib-unix.h> |
| 16 |
|
| 17 |
#include <sys/wait.h> |
| 18 |
#include <unistd.h> |
| 19 |
|
| 20 |
static void |
| 21 |
async_signal_safe_message (const char *message) |
| 22 |
{ |
| 23 |
if (write (2, message, strlen (message)) < 0 || |
| 24 |
write (2, "\n", 1) < 0) |
| 25 |
{ |
| 26 |
/* ignore: not much we can do */ |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
static void test_closefrom_subprocess_einval (void); |
| 31 |
|
| 32 |
static void |
| 33 |
test_closefrom (void) |
| 34 |
{ |
| 35 |
/* Enough file descriptors to be confident that we're operating on |
| 36 |
* all of them */ |
| 37 |
const int N_FDS = 20; |
| 38 |
int *fds; |
| 39 |
int fd; |
| 40 |
int i; |
| 41 |
pid_t child; |
| 42 |
int wait_status; |
| 43 |
|
| 44 |
/* The loop that populates @fds with pipes assumes this */ |
| 45 |
g_assert (N_FDS % 2 == 0); |
| 46 |
|
| 47 |
for (fd = 0; fd <= 2; fd++) |
| 48 |
{ |
| 49 |
int flags; |
| 50 |
|
| 51 |
g_assert_no_errno ((flags = fcntl (fd, F_GETFD))); |
| 52 |
g_assert_no_errno (fcntl (fd, F_SETFD, flags & ~FD_CLOEXEC)); |
| 53 |
} |
| 54 |
|
| 55 |
fds = g_new0 (int, N_FDS); |
| 56 |
|
| 57 |
for (i = 0; i < N_FDS; i += 2) |
| 58 |
{ |
| 59 |
GError *error = NULL; |
| 60 |
int pipefd[2]; |
| 61 |
int res; |
| 62 |
|
| 63 |
/* Intentionally neither O_CLOEXEC nor FD_CLOEXEC */ |
| 64 |
res = g_unix_open_pipe (pipefd, 0, &error); |
| 65 |
g_assert (res); |
| 66 |
g_assert_no_error (error); |
| 67 |
g_clear_error (&error); |
| 68 |
fds[i] = pipefd[0]; |
| 69 |
fds[i + 1] = pipefd[1]; |
| 70 |
} |
| 71 |
|
| 72 |
child = fork (); |
| 73 |
|
| 74 |
/* Child process exits with status = 100 + the first wrong fd, |
| 75 |
* or 0 if all were correct */ |
| 76 |
if (child == 0) |
| 77 |
{ |
| 78 |
for (i = 0; i < N_FDS; i++) |
| 79 |
{ |
| 80 |
int flags = fcntl (fds[i], F_GETFD); |
| 81 |
|
| 82 |
if (flags == -1) |
| 83 |
{ |
| 84 |
async_signal_safe_message ("fd should not have been closed"); |
| 85 |
_exit (100 + fds[i]); |
| 86 |
} |
| 87 |
|
| 88 |
if (flags & FD_CLOEXEC) |
| 89 |
{ |
| 90 |
async_signal_safe_message ("fd should not have been close-on-exec yet"); |
| 91 |
_exit (100 + fds[i]); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
g_fdwalk_set_cloexec (3); |
| 96 |
|
| 97 |
for (i = 0; i < N_FDS; i++) |
| 98 |
{ |
| 99 |
int flags = fcntl (fds[i], F_GETFD); |
| 100 |
|
| 101 |
if (flags == -1) |
| 102 |
{ |
| 103 |
async_signal_safe_message ("fd should not have been closed"); |
| 104 |
_exit (100 + fds[i]); |
| 105 |
} |
| 106 |
|
| 107 |
if (!(flags & FD_CLOEXEC)) |
| 108 |
{ |
| 109 |
async_signal_safe_message ("fd should have been close-on-exec"); |
| 110 |
_exit (100 + fds[i]); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
g_closefrom (3); |
| 115 |
|
| 116 |
for (fd = 0; fd <= 2; fd++) |
| 117 |
{ |
| 118 |
int flags = fcntl (fd, F_GETFD); |
| 119 |
|
| 120 |
if (flags == -1) |
| 121 |
{ |
| 122 |
async_signal_safe_message ("fd should not have been closed"); |
| 123 |
_exit (100 + fd); |
| 124 |
} |
| 125 |
|
| 126 |
if (flags & FD_CLOEXEC) |
| 127 |
{ |
| 128 |
async_signal_safe_message ("fd should not have been close-on-exec"); |
| 129 |
_exit (100 + fd); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
for (i = 0; i < N_FDS; i++) |
| 134 |
{ |
| 135 |
if (fcntl (fds[i], F_GETFD) != -1 || errno != EBADF) |
| 136 |
{ |
| 137 |
async_signal_safe_message ("fd should have been closed"); |
| 138 |
_exit (100 + fds[i]); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
_exit (0); |
| 143 |
} |
| 144 |
|
| 145 |
g_assert_no_errno (waitpid (child, &wait_status, 0)); |
| 146 |
|
| 147 |
if (WIFEXITED (wait_status)) |
| 148 |
{ |
| 149 |
int exit_status = WEXITSTATUS (wait_status); |
| 150 |
|
| 151 |
if (exit_status != 0) |
| 152 |
g_test_fail_printf ("File descriptor %d in incorrect state", exit_status - 100); |
| 153 |
} |
| 154 |
else |
| 155 |
{ |
| 156 |
g_test_fail_printf ("Unexpected wait status %d", wait_status); |
| 157 |
} |
| 158 |
|
| 159 |
for (i = 0; i < N_FDS; i++) |
| 160 |
g_assert_no_errno (close (fds[i])); |
| 161 |
|
| 162 |
g_free (fds); |
| 163 |
|
| 164 |
if (g_test_undefined ()) |
| 165 |
{ |
| 166 |
#if GLIB_CHECK_VERSION (2, 38, 0) |
| 167 |
g_test_trap_subprocess ("/glib-unix/closefrom/subprocess/einval", |
| 168 |
0, G_TEST_SUBPROCESS_DEFAULT); |
| 169 |
#else |
| 170 |
if (g_test_trap_fork (0, 0)) |
| 171 |
{ |
| 172 |
test_closefrom_subprocess_einval (); |
| 173 |
exit (0); |
| 174 |
} |
| 175 |
|
| 176 |
#endif |
| 177 |
g_test_trap_assert_passed (); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
static void |
| 182 |
test_closefrom_subprocess_einval (void) |
| 183 |
{ |
| 184 |
int res; |
| 185 |
int errsv; |
| 186 |
|
| 187 |
g_log_set_always_fatal (G_LOG_FATAL_MASK); |
| 188 |
g_log_set_fatal_mask ("GLib", G_LOG_FATAL_MASK); |
| 189 |
|
| 190 |
errno = 0; |
| 191 |
res = g_closefrom (-1); |
| 192 |
errsv = errno; |
| 193 |
g_assert_cmpint (res, ==, -1); |
| 194 |
g_assert_cmpint (errsv, ==, EINVAL); |
| 195 |
|
| 196 |
errno = 0; |
| 197 |
res = g_fdwalk_set_cloexec (-42); |
| 198 |
errsv = errno; |
| 199 |
g_assert_cmpint (res, ==, -1); |
| 200 |
g_assert_cmpint (errsv, ==, EINVAL); |
| 201 |
} |
| 202 |
|
| 203 |
/* Testing g_memdup2() function with various positive and negative cases */ |
| 204 |
static void |
| 205 |
test_memdup2 (void) |
| 206 |
{ |
| 207 |
gchar *str_dup = NULL; |
| 208 |
const gchar *str = "The quick brown fox jumps over the lazy dog"; |
| 209 |
|
| 210 |
/* Testing negative cases */ |
| 211 |
g_assert_null (g_memdup2 (NULL, 1024)); |
| 212 |
g_assert_null (g_memdup2 (str, 0)); |
| 213 |
g_assert_null (g_memdup2 (NULL, 0)); |
| 214 |
|
| 215 |
/* Testing normal usage cases */ |
| 216 |
str_dup = g_memdup2 (str, strlen (str) + 1); |
| 217 |
g_assert_nonnull (str_dup); |
| 218 |
g_assert_cmpstr (str, ==, str_dup); |
| 219 |
|
| 220 |
g_free (str_dup); |
| 221 |
} |
| 222 |
|
| 223 |
static void |
| 224 |
test_steal_fd (void) |
| 225 |
{ |
| 226 |
GError *error = NULL; |
| 227 |
gchar *tmpfile = NULL; |
| 228 |
int fd = -42; |
| 229 |
int borrowed; |
| 230 |
int stolen; |
| 231 |
|
| 232 |
g_assert_cmpint (g_steal_fd (&fd), ==, -42); |
| 233 |
g_assert_cmpint (fd, ==, -1); |
| 234 |
g_assert_cmpint (g_steal_fd (&fd), ==, -1); |
| 235 |
g_assert_cmpint (fd, ==, -1); |
| 236 |
|
| 237 |
fd = g_file_open_tmp (NULL, &tmpfile, &error); |
| 238 |
g_assert_cmpint (fd, >=, 0); |
| 239 |
g_assert_no_error (error); |
| 240 |
borrowed = fd; |
| 241 |
stolen = g_steal_fd (&fd); |
| 242 |
g_assert_cmpint (fd, ==, -1); |
| 243 |
g_assert_cmpint (borrowed, ==, stolen); |
| 244 |
|
| 245 |
g_assert_no_errno (close (g_steal_fd (&stolen))); |
| 246 |
g_assert_cmpint (stolen, ==, -1); |
| 247 |
|
| 248 |
g_assert_no_errno (remove (tmpfile)); |
| 249 |
g_free (tmpfile); |
| 250 |
|
| 251 |
/* Backwards compatibility with older libglnx: glnx_steal_fd is the same |
| 252 |
* as g_steal_fd */ |
| 253 |
fd = -23; |
| 254 |
g_assert_cmpint (glnx_steal_fd (&fd), ==, -23); |
| 255 |
g_assert_cmpint (fd, ==, -1); |
| 256 |
} |
| 257 |
|
| 258 |
/* Test g_strv_equal() works for various inputs. */ |
| 259 |
static void |
| 260 |
test_strv_equal (void) |
| 261 |
{ |
| 262 |
const gchar *strv_empty[] = { NULL }; |
| 263 |
const gchar *strv_empty2[] = { NULL }; |
| 264 |
const gchar *strv_simple[] = { "hello", "you", NULL }; |
| 265 |
const gchar *strv_simple2[] = { "hello", "you", NULL }; |
| 266 |
const gchar *strv_simple_reordered[] = { "you", "hello", NULL }; |
| 267 |
const gchar *strv_simple_superset[] = { "hello", "you", "again", NULL }; |
| 268 |
const gchar *strv_another[] = { "not", "a", "coded", "message", NULL }; |
| 269 |
|
| 270 |
g_assert_true (g_strv_equal (strv_empty, strv_empty)); |
| 271 |
g_assert_true (g_strv_equal (strv_empty, strv_empty2)); |
| 272 |
g_assert_true (g_strv_equal (strv_empty2, strv_empty)); |
| 273 |
g_assert_false (g_strv_equal (strv_empty, strv_simple)); |
| 274 |
g_assert_false (g_strv_equal (strv_simple, strv_empty)); |
| 275 |
g_assert_true (g_strv_equal (strv_simple, strv_simple)); |
| 276 |
g_assert_true (g_strv_equal (strv_simple, strv_simple2)); |
| 277 |
g_assert_true (g_strv_equal (strv_simple2, strv_simple)); |
| 278 |
g_assert_false (g_strv_equal (strv_simple, strv_simple_reordered)); |
| 279 |
g_assert_false (g_strv_equal (strv_simple_reordered, strv_simple)); |
| 280 |
g_assert_false (g_strv_equal (strv_simple, strv_simple_superset)); |
| 281 |
g_assert_false (g_strv_equal (strv_simple_superset, strv_simple)); |
| 282 |
g_assert_false (g_strv_equal (strv_simple, strv_another)); |
| 283 |
g_assert_false (g_strv_equal (strv_another, strv_simple)); |
| 284 |
} |
| 285 |
|
| 286 |
int main (int argc, char **argv) |
| 287 |
{ |
| 288 |
g_test_init (&argc, &argv, NULL); |
| 289 |
|
| 290 |
g_test_add_func ("/glib-unix/closefrom", test_closefrom); |
| 291 |
#if GLIB_CHECK_VERSION (2, 38, 0) |
| 292 |
g_test_add_func ("/glib-unix/closefrom/subprocess/einval", |
| 293 |
test_closefrom_subprocess_einval); |
| 294 |
#endif |
| 295 |
g_test_add_func ("/mainloop/steal-fd", test_steal_fd); |
| 296 |
g_test_add_func ("/strfuncs/memdup2", test_memdup2); |
| 297 |
g_test_add_func ("/strfuncs/strv-equal", test_strv_equal); |
| 298 |
return g_test_run(); |
| 299 |
} |