| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include <config.h> |
| 22 |
|
| 23 |
#include <errno.h> |
| 24 |
#include <fcntl.h> |
| 25 |
|
| 26 |
#ifdef HAVE_UNISTD_H |
| 27 |
# include <unistd.h> |
| 28 |
#endif |
| 29 |
|
| 30 |
#include <glib/gstdio.h> |
| 31 |
|
| 32 |
#include <libdex.h> |
| 33 |
|
| 34 |
#include "dex-aio-backend-private.h" |
| 35 |
#include "dex-posix-aio-backend-private.h" |
| 36 |
|
| 37 |
#ifdef HAVE_LIBURING |
| 38 |
# include "dex-uring-aio-backend-private.h" |
| 39 |
#endif |
| 40 |
|
| 41 |
static DexFuture * |
| 42 |
quit_cb (DexFuture *future, |
| 43 |
gpointer user_data) |
| 44 |
{ |
| 45 |
g_main_loop_quit (user_data); |
| 46 |
return NULL; |
| 47 |
} |
| 48 |
|
| 49 |
static DexFuture * |
| 50 |
await_future (DexFuture *future) |
| 51 |
{ |
| 52 |
GMainLoop *main_loop = g_main_loop_new (NULL, FALSE); |
| 53 |
|
| 54 |
future = dex_future_finally (future, quit_cb, main_loop, NULL); |
| 55 |
g_main_loop_run (main_loop); |
| 56 |
|
| 57 |
g_main_loop_unref (main_loop); |
| 58 |
|
| 59 |
return future; |
| 60 |
} |
| 61 |
|
| 62 |
static void |
| 63 |
run_aio_close_success (DexAioContext *aio_context) |
| 64 |
{ |
| 65 |
char *path = NULL; |
| 66 |
GError * error = NULL; |
| 67 |
DexFuture *future; |
| 68 |
gboolean ret; |
| 69 |
int fd; |
| 70 |
|
| 71 |
fd = g_file_open_tmp ("libdex-aio-close-XXXXXX", &path, &error); |
| 72 |
g_assert_no_error (error); |
| 73 |
g_assert_cmpint (fd, >=, 0); |
| 74 |
|
| 75 |
future = await_future (dex_aio_close (aio_context, fd)); |
| 76 |
ret = dex_await_boolean (future, &error); |
| 77 |
|
| 78 |
g_assert_no_error (error); |
| 79 |
g_assert_true (ret); |
| 80 |
g_clear_error (&error); |
| 81 |
|
| 82 |
errno = 0; |
| 83 |
g_assert_cmpint (fcntl (fd, F_GETFD), ==, -1); |
| 84 |
g_assert_cmpint (errno, ==, EBADF); |
| 85 |
|
| 86 |
g_assert_cmpint (g_unlink (path), ==, 0); |
| 87 |
g_clear_pointer (&path, g_free); |
| 88 |
} |
| 89 |
|
| 90 |
static void |
| 91 |
run_aio_close_bad_fd (DexAioContext *aio_context) |
| 92 |
{ |
| 93 |
GError * error = NULL; |
| 94 |
DexFuture *future; |
| 95 |
gboolean ret; |
| 96 |
|
| 97 |
future = await_future (dex_aio_close (aio_context, G_MAXINT)); |
| 98 |
ret = dex_await_boolean (future, &error); |
| 99 |
|
| 100 |
g_assert_false (ret); |
| 101 |
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED); |
| 102 |
g_clear_error (&error); |
| 103 |
} |
| 104 |
|
| 105 |
static void |
| 106 |
run_aio_open_success (DexAioContext *aio_context) |
| 107 |
{ |
| 108 |
static const char contents[] = "libdex aio open"; |
| 109 |
char *path = NULL; |
| 110 |
GError * error = NULL; |
| 111 |
DexFuture *future; |
| 112 |
char buffer[sizeof contents] = {0}; |
| 113 |
int source_fd; |
| 114 |
int fd; |
| 115 |
|
| 116 |
source_fd = g_file_open_tmp ("libdex-aio-open-XXXXXX", &path, &error); |
| 117 |
g_assert_no_error (error); |
| 118 |
g_assert_cmpint (source_fd, >=, 0); |
| 119 |
g_assert_cmpint (write (source_fd, contents, strlen (contents)), ==, strlen (contents)); |
| 120 |
g_assert_cmpint (close (source_fd), ==, 0); |
| 121 |
|
| 122 |
future = await_future (dex_aio_open (aio_context, path, O_RDONLY | O_CLOEXEC, 0)); |
| 123 |
fd = dex_await_fd (future, &error); |
| 124 |
|
| 125 |
g_assert_no_error (error); |
| 126 |
g_assert_cmpint (fd, >=, 0); |
| 127 |
g_assert_cmpint (read (fd, buffer, sizeof buffer), ==, strlen (contents)); |
| 128 |
g_assert_cmpmem (buffer, strlen (contents), contents, strlen (contents)); |
| 129 |
g_assert_cmpint (close (fd), ==, 0); |
| 130 |
g_clear_error (&error); |
| 131 |
|
| 132 |
g_assert_cmpint (g_unlink (path), ==, 0); |
| 133 |
g_clear_pointer (&path, g_free); |
| 134 |
} |
| 135 |
|
| 136 |
static void |
| 137 |
run_aio_open_missing (DexAioContext *aio_context) |
| 138 |
{ |
| 139 |
char *path = NULL; |
| 140 |
GError * error = NULL; |
| 141 |
DexFuture *future; |
| 142 |
int fd; |
| 143 |
|
| 144 |
fd = g_file_open_tmp ("libdex-aio-open-missing-XXXXXX", &path, &error); |
| 145 |
g_assert_no_error (error); |
| 146 |
g_assert_cmpint (fd, >=, 0); |
| 147 |
g_assert_cmpint (close (fd), ==, 0); |
| 148 |
g_assert_cmpint (g_unlink (path), ==, 0); |
| 149 |
|
| 150 |
future = await_future (dex_aio_open (aio_context, path, O_RDONLY | O_CLOEXEC, 0)); |
| 151 |
fd = dex_await_fd (future, &error); |
| 152 |
|
| 153 |
g_assert_cmpint (fd, ==, -1); |
| 154 |
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND); |
| 155 |
g_clear_error (&error); |
| 156 |
|
| 157 |
g_clear_pointer (&path, g_free); |
| 158 |
} |
| 159 |
|
| 160 |
static void |
| 161 |
test_aio_close_success (void) |
| 162 |
{ |
| 163 |
run_aio_close_success (NULL); |
| 164 |
} |
| 165 |
|
| 166 |
static void |
| 167 |
test_aio_close_bad_fd (void) |
| 168 |
{ |
| 169 |
run_aio_close_bad_fd (NULL); |
| 170 |
} |
| 171 |
|
| 172 |
static void |
| 173 |
test_aio_open_success (void) |
| 174 |
{ |
| 175 |
run_aio_open_success (NULL); |
| 176 |
} |
| 177 |
|
| 178 |
static void |
| 179 |
test_aio_open_missing (void) |
| 180 |
{ |
| 181 |
run_aio_open_missing (NULL); |
| 182 |
} |
| 183 |
|
| 184 |
static void |
| 185 |
test_aio_open_posix (void) |
| 186 |
{ |
| 187 |
DexAioBackend *backend; |
| 188 |
DexAioContext *aio_context; |
| 189 |
|
| 190 |
backend = dex_posix_aio_backend_new (); |
| 191 |
aio_context = dex_aio_backend_create_context (backend); |
| 192 |
g_source_attach ((GSource *)aio_context, NULL); |
| 193 |
|
| 194 |
run_aio_close_success (aio_context); |
| 195 |
run_aio_close_bad_fd (aio_context); |
| 196 |
run_aio_open_success (aio_context); |
| 197 |
run_aio_open_missing (aio_context); |
| 198 |
|
| 199 |
g_source_destroy ((GSource *)aio_context); |
| 200 |
g_source_unref ((GSource *)aio_context); |
| 201 |
dex_unref (backend); |
| 202 |
} |
| 203 |
|
| 204 |
#ifdef HAVE_LIBURING |
| 205 |
static void |
| 206 |
test_aio_open_uring (void) |
| 207 |
{ |
| 208 |
DexAioBackend *backend; |
| 209 |
DexAioContext *aio_context; |
| 210 |
|
| 211 |
if (!(backend = dex_uring_aio_backend_new ())) |
| 212 |
{ |
| 213 |
g_test_skip ("io_uring backend is not available"); |
| 214 |
return; |
| 215 |
} |
| 216 |
|
| 217 |
aio_context = dex_aio_backend_create_context (backend); |
| 218 |
g_source_attach ((GSource *)aio_context, NULL); |
| 219 |
|
| 220 |
run_aio_close_success (aio_context); |
| 221 |
run_aio_close_bad_fd (aio_context); |
| 222 |
run_aio_open_success (aio_context); |
| 223 |
run_aio_open_missing (aio_context); |
| 224 |
|
| 225 |
g_source_destroy ((GSource *)aio_context); |
| 226 |
g_source_unref ((GSource *)aio_context); |
| 227 |
dex_unref (backend); |
| 228 |
} |
| 229 |
#endif |
| 230 |
|
| 231 |
int |
| 232 |
main (int argc, |
| 233 |
char *argv[]) |
| 234 |
{ |
| 235 |
dex_init (); |
| 236 |
g_test_init (&argc, &argv, NULL); |
| 237 |
g_test_add_func ("/Dex/TestSuite/Aio/close-success", test_aio_close_success); |
| 238 |
g_test_add_func ("/Dex/TestSuite/Aio/close-bad-fd", test_aio_close_bad_fd); |
| 239 |
g_test_add_func ("/Dex/TestSuite/Aio/open-success", test_aio_open_success); |
| 240 |
g_test_add_func ("/Dex/TestSuite/Aio/open-missing", test_aio_open_missing); |
| 241 |
g_test_add_func ("/Dex/TestSuite/Aio/open-posix", test_aio_open_posix); |
| 242 |
#ifdef HAVE_LIBURING |
| 243 |
g_test_add_func ("/Dex/TestSuite/Aio/open-uring", test_aio_open_uring); |
| 244 |
#endif |
| 245 |
return g_test_run (); |
| 246 |
} |