| 1 |
/* |
| 2 |
* Copyright (C) 2020 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "config.h" |
| 7 |
|
| 8 |
#include "cockpitcontrolmessages.h" |
| 9 |
#include "common/cockpitfdpassing.h" |
| 10 |
#include "common/cockpitjsonprint.h" |
| 11 |
#include "cockpitmemfdread.h" |
| 12 |
#include "cockpitsocket.h" |
| 13 |
|
| 14 |
#include "testlib/cockpittest.h" |
| 15 |
|
| 16 |
#include <gio/gunixfdmessage.h> |
| 17 |
#include <gio/gunixcredentialsmessage.h> |
| 18 |
#include <glib-unix.h> |
| 19 |
|
| 20 |
#include <errno.h> |
| 21 |
#include <fcntl.h> |
| 22 |
#include <sys/socket.h> |
| 23 |
#include <sys/mman.h> |
| 24 |
#include <stdio.h> |
| 25 |
|
| 26 |
static int |
| 27 |
memfd_create_noexec (const char *name, |
| 28 |
unsigned int flags) |
| 29 |
{ |
| 30 |
/* current kernels moan about not specifying exec mode */ |
| 31 |
#ifdef MFD_NOEXEC_SEAL |
| 32 |
int fd = memfd_create (name, flags | MFD_NOEXEC_SEAL); |
| 33 |
/* fallback for older kernels */ |
| 34 |
if (fd != -1 || errno != EINVAL) |
| 35 |
return fd; |
| 36 |
#endif |
| 37 |
return memfd_create (name, flags); |
| 38 |
} |
| 39 |
|
| 40 |
/* --- testing of reading --- */ |
| 41 |
|
| 42 |
static void |
| 43 |
test_memfd_simple (void) |
| 44 |
{ |
| 45 |
FILE *stream; |
| 46 |
|
| 47 |
stream = cockpit_json_print_open_memfd ("test", 1); |
| 48 |
gint fd = cockpit_json_print_finish_memfd (&stream); |
| 49 |
|
| 50 |
g_autoptr(GError) error = NULL; |
| 51 |
g_autofree gchar *content = cockpit_memfd_read (fd, &error); |
| 52 |
g_assert_no_error (error); |
| 53 |
close (fd); |
| 54 |
|
| 55 |
g_assert_cmpstr (content, ==, "{\"version\": 1}"); |
| 56 |
} |
| 57 |
|
| 58 |
static void |
| 59 |
test_memfd_error_cases (void) |
| 60 |
{ |
| 61 |
g_autoptr(GError) error = NULL; |
| 62 |
g_autofree gchar *content = NULL; |
| 63 |
FILE *stream; |
| 64 |
gint fd; |
| 65 |
gint r; |
| 66 |
|
| 67 |
/* not a memfd */ |
| 68 |
fd = open ("/dev/null", O_RDONLY); |
| 69 |
|
| 70 |
content = cockpit_memfd_read (fd, &error); |
| 71 |
cockpit_assert_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_INVAL, "*not memfd?*"); |
| 72 |
g_assert (content == NULL); |
| 73 |
g_clear_error (&error); |
| 74 |
close (fd); |
| 75 |
|
| 76 |
|
| 77 |
/* memfd is not properly sealed */ |
| 78 |
fd = memfd_create_noexec ("xyz", MFD_CLOEXEC); |
| 79 |
|
| 80 |
content = cockpit_memfd_read (fd, &error); |
| 81 |
cockpit_assert_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_INVAL, "*incorrect seals set*"); |
| 82 |
g_assert (content == NULL); |
| 83 |
g_clear_error (&error); |
| 84 |
close (fd); |
| 85 |
|
| 86 |
/* memfd is empty */ |
| 87 |
fd = memfd_create_noexec ("xyz", MFD_ALLOW_SEALING | MFD_CLOEXEC); |
| 88 |
r = fcntl (fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE); |
| 89 |
g_assert (r == 0); |
| 90 |
|
| 91 |
content = cockpit_memfd_read (fd, &error); |
| 92 |
cockpit_assert_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_INVAL, "*empty*"); |
| 93 |
g_assert (content == NULL); |
| 94 |
g_clear_error (&error); |
| 95 |
close (fd); |
| 96 |
|
| 97 |
|
| 98 |
/* memfd is too big */ |
| 99 |
stream = cockpit_json_print_open_memfd ("xyz", 1); |
| 100 |
fprintf (stream, "%20000s", ""); |
| 101 |
fd = cockpit_json_print_finish_memfd (&stream); |
| 102 |
|
| 103 |
content = cockpit_memfd_read (fd, &error); |
| 104 |
cockpit_assert_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_INVAL, "*unreasonably large*"); |
| 105 |
g_assert (content == NULL); |
| 106 |
g_clear_error (&error); |
| 107 |
close (fd); |
| 108 |
|
| 109 |
|
| 110 |
/* memfd can't be read */ |
| 111 |
stream = cockpit_json_print_open_memfd ("xyz", 1); |
| 112 |
int tmpfd = cockpit_json_print_finish_memfd (&stream); |
| 113 |
gchar procfile[80]; |
| 114 |
snprintf (procfile, sizeof procfile, "/proc/self/fd/%d", tmpfd); |
| 115 |
fd = open (procfile, O_WRONLY); |
| 116 |
g_assert_cmpint (fd, !=, -1); |
| 117 |
r = close (tmpfd); |
| 118 |
g_assert_cmpint (r, ==, 0); |
| 119 |
|
| 120 |
content = cockpit_memfd_read (fd, &error); |
| 121 |
cockpit_assert_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_BADF, "*failed to read*"); |
| 122 |
g_assert (content == NULL); |
| 123 |
g_clear_error (&error); |
| 124 |
close (fd); |
| 125 |
|
| 126 |
|
| 127 |
/* memfd contains a nul */ |
| 128 |
stream = cockpit_json_print_open_memfd ("xyz", 1); |
| 129 |
fputc (0, stream); |
| 130 |
fd = cockpit_json_print_finish_memfd (&stream); |
| 131 |
|
| 132 |
content = cockpit_memfd_read (fd, &error); |
| 133 |
cockpit_assert_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_INVAL, "*contains nul*"); |
| 134 |
g_assert (content == NULL); |
| 135 |
g_clear_error (&error); |
| 136 |
close (fd); |
| 137 |
|
| 138 |
/* memfd contains non-ascii */ |
| 139 |
stream = cockpit_json_print_open_memfd ("xyz", 1); |
| 140 |
fputc (0xcc, stream); |
| 141 |
fd = cockpit_json_print_finish_memfd (&stream); |
| 142 |
|
| 143 |
content = cockpit_memfd_read (fd, &error); |
| 144 |
cockpit_assert_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_INVAL, "*contains non-ASCII*"); |
| 145 |
g_assert (content == NULL); |
| 146 |
g_clear_error (&error); |
| 147 |
close (fd); |
| 148 |
} |
| 149 |
|
| 150 |
static void |
| 151 |
test_memfd_json (void) |
| 152 |
{ |
| 153 |
FILE *stream; |
| 154 |
|
| 155 |
stream = cockpit_json_print_open_memfd ("test", 1); |
| 156 |
cockpit_json_print_string_property (stream, "hello", "world", -1); |
| 157 |
cockpit_json_print_integer_property (stream, "size", 200); |
| 158 |
cockpit_json_print_bool_property (stream, "truth", true); |
| 159 |
cockpit_json_print_bool_property (stream, "falsth", false); |
| 160 |
gint fd = cockpit_json_print_finish_memfd (&stream); |
| 161 |
|
| 162 |
g_autoptr(GError) error = NULL; |
| 163 |
g_autoptr(JsonObject) object = cockpit_memfd_read_json (fd, &error); |
| 164 |
g_assert_no_error (error); |
| 165 |
close (fd); |
| 166 |
|
| 167 |
g_assert_cmpint (json_object_get_int_member (object, "version"), ==, 1); |
| 168 |
g_assert_cmpstr (json_object_get_string_member (object, "hello"), ==, "world"); |
| 169 |
g_assert_cmpint (json_object_get_int_member (object, "size"), ==, 200); |
| 170 |
g_assert_cmpint (json_object_get_boolean_member (object, "truth"), ==, TRUE); |
| 171 |
g_assert_cmpint (json_object_get_boolean_member (object, "falsth"), ==, FALSE); |
| 172 |
} |
| 173 |
|
| 174 |
static void |
| 175 |
test_memfd_json_error_cases (void) |
| 176 |
{ |
| 177 |
g_autoptr(GError) error = NULL; |
| 178 |
g_autoptr(JsonObject) object = NULL; |
| 179 |
gint fd; |
| 180 |
gint r; |
| 181 |
|
| 182 |
/* invalid json */ |
| 183 |
fd = memfd_create_noexec ("xyz", MFD_CLOEXEC | MFD_ALLOW_SEALING); |
| 184 |
g_assert_cmpint (write (fd, "beh", 3), ==, 3); |
| 185 |
r = fcntl (fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE); |
| 186 |
g_assert (r == 0); |
| 187 |
object = cockpit_memfd_read_json (fd, &error); |
| 188 |
g_assert (object == NULL); |
| 189 |
cockpit_assert_error_matches (error, JSON_PARSER_ERROR, JSON_PARSER_ERROR_INVALID_BAREWORD, "*unexpected identifier*"); |
| 190 |
g_clear_error (&error); |
| 191 |
close (fd); |
| 192 |
|
| 193 |
/* valid json, but not an object */ |
| 194 |
fd = memfd_create_noexec ("xyz", MFD_CLOEXEC | MFD_ALLOW_SEALING); |
| 195 |
g_assert_cmpint (write (fd, "[]", 2), ==, 2); |
| 196 |
r = fcntl (fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE); |
| 197 |
g_assert (r == 0); |
| 198 |
object = cockpit_memfd_read_json (fd, &error); |
| 199 |
g_assert (object == NULL); |
| 200 |
cockpit_assert_error_matches (error, JSON_PARSER_ERROR, -1, "*Not a JSON object*"); |
| 201 |
close (fd); |
| 202 |
|
| 203 |
} |
| 204 |
|
| 205 |
/* --- unix socket testing --- */ |
| 206 |
|
| 207 |
static GSList *live_control_messages; |
| 208 |
|
| 209 |
static void |
| 210 |
assert_live_control_messages (gint expected) |
| 211 |
{ |
| 212 |
g_assert_cmpint (g_slist_length (live_control_messages), ==, expected); |
| 213 |
} |
| 214 |
|
| 215 |
static void |
| 216 |
remove_message_from_list (gpointer data, |
| 217 |
GObject *where_the_object_was) |
| 218 |
{ |
| 219 |
for (GSList **n = &live_control_messages; *n; n = &(*n)->next) |
| 220 |
if ((*n)->data == where_the_object_was) |
| 221 |
{ |
| 222 |
*n = g_slist_delete_link (*n, *n); |
| 223 |
return; |
| 224 |
} |
| 225 |
|
| 226 |
g_error ("Couldn't find control message %p in list", where_the_object_was); |
| 227 |
|
| 228 |
} |
| 229 |
|
| 230 |
static void |
| 231 |
receive_cmsgs (GSocket *socket, |
| 232 |
CockpitControlMessages *ccm) |
| 233 |
{ |
| 234 |
gchar buffer[1]; |
| 235 |
GInputVector vector = { buffer, sizeof buffer }; |
| 236 |
GError *error = NULL; |
| 237 |
g_socket_receive_message (socket, |
| 238 |
NULL, /* address */ |
| 239 |
&vector, 1, |
| 240 |
&ccm->messages, &ccm->n_messages, |
| 241 |
NULL, NULL, |
| 242 |
&error); |
| 243 |
|
| 244 |
/* Use this to make sure all messages are getting properly freed */ |
| 245 |
for (gint i = 0; i < ccm->n_messages; i++) |
| 246 |
{ |
| 247 |
live_control_messages = g_slist_prepend (live_control_messages, ccm->messages[i]); |
| 248 |
g_object_weak_ref (G_OBJECT (ccm->messages[i]), remove_message_from_list, NULL); |
| 249 |
} |
| 250 |
|
| 251 |
g_assert_no_error (error); |
| 252 |
} |
| 253 |
|
| 254 |
static void |
| 255 |
receive_nothing (GSocket *socket) |
| 256 |
{ |
| 257 |
g_auto(CockpitControlMessages) ccm = COCKPIT_CONTROL_MESSAGES_INIT; |
| 258 |
|
| 259 |
receive_cmsgs (socket, &ccm); |
| 260 |
|
| 261 |
g_assert (cockpit_control_messages_empty (&ccm)); |
| 262 |
} |
| 263 |
|
| 264 |
static gint * |
| 265 |
receive_fds (GSocket *socket, |
| 266 |
gint *out_nfds, |
| 267 |
GError **error) |
| 268 |
{ |
| 269 |
g_auto(CockpitControlMessages) ccm = COCKPIT_CONTROL_MESSAGES_INIT; |
| 270 |
|
| 271 |
receive_cmsgs (socket, &ccm); |
| 272 |
|
| 273 |
int n_fds; |
| 274 |
const gint *fds = cockpit_control_messages_peek_fd_list (&ccm, &n_fds, error); |
| 275 |
|
| 276 |
if (fds == NULL) |
| 277 |
return NULL; |
| 278 |
|
| 279 |
gint *result = g_new (int, n_fds + 1); |
| 280 |
for (gint i = 0; i < n_fds; i++) |
| 281 |
result[i] = dup (fds[i]); |
| 282 |
result[n_fds] = -1; |
| 283 |
*out_nfds = n_fds; |
| 284 |
return result; |
| 285 |
} |
| 286 |
|
| 287 |
static void |
| 288 |
free_fds (gint **inout_fds, |
| 289 |
gint *inout_nfds) |
| 290 |
{ |
| 291 |
gint *fds = *inout_fds; |
| 292 |
gint nfds = *inout_nfds; |
| 293 |
|
| 294 |
for (gint i = 0; i < nfds; i++) |
| 295 |
{ |
| 296 |
g_assert (fds[i] != -1); |
| 297 |
int r = close (fds[i]); |
| 298 |
g_assert (r == 0); |
| 299 |
} |
| 300 |
g_assert (fds[nfds] == -1); |
| 301 |
|
| 302 |
g_free (fds); |
| 303 |
|
| 304 |
*inout_fds = NULL; |
| 305 |
*inout_nfds = 0; |
| 306 |
} |
| 307 |
|
| 308 |
static gint |
| 309 |
receive_fd (GSocket *socket, |
| 310 |
GError **error) |
| 311 |
{ |
| 312 |
g_auto(CockpitControlMessages) ccm = COCKPIT_CONTROL_MESSAGES_INIT; |
| 313 |
|
| 314 |
receive_cmsgs (socket, &ccm); |
| 315 |
|
| 316 |
int fd = cockpit_control_messages_peek_single_fd (&ccm, error); |
| 317 |
|
| 318 |
if (fd == -1) |
| 319 |
return -1; |
| 320 |
|
| 321 |
return dup (fd); |
| 322 |
} |
| 323 |
|
| 324 |
static void |
| 325 |
send_cmsgs (GSocket *socket, |
| 326 |
GSocketControlMessage **messages, |
| 327 |
gint n_messages, |
| 328 |
gint n_bytes) |
| 329 |
{ |
| 330 |
const gchar buffer[100] = ""; |
| 331 |
g_assert_cmpint(n_bytes, <=, sizeof buffer); |
| 332 |
GOutputVector vector = { buffer, n_bytes }; |
| 333 |
GError *error = NULL; |
| 334 |
g_socket_send_message (socket, |
| 335 |
NULL, /* address */ |
| 336 |
&vector, 1, |
| 337 |
messages, n_messages, |
| 338 |
0, NULL, &error); |
| 339 |
g_assert_no_error (error); |
| 340 |
} |
| 341 |
|
| 342 |
static void |
| 343 |
send_nothing (GSocket *socket, |
| 344 |
gint n_bytes) |
| 345 |
{ |
| 346 |
send_cmsgs (socket, NULL, 0, n_bytes); |
| 347 |
} |
| 348 |
|
| 349 |
static GSocketControlMessage * |
| 350 |
make_fd_message (const gint *fds, |
| 351 |
gint n_fds) |
| 352 |
{ |
| 353 |
g_autoptr(GUnixFDList) fdl = g_unix_fd_list_new (); |
| 354 |
|
| 355 |
for (gint i = 0; i < n_fds; i++) |
| 356 |
{ |
| 357 |
GError *error = NULL; |
| 358 |
g_unix_fd_list_append (fdl, fds[i], &error); |
| 359 |
g_assert_no_error (error); |
| 360 |
} |
| 361 |
|
| 362 |
return g_unix_fd_message_new_with_fd_list (fdl); |
| 363 |
} |
| 364 |
|
| 365 |
static void |
| 366 |
send_fds (GSocket *socket, |
| 367 |
const gint *fds, |
| 368 |
gint n_fds) |
| 369 |
{ |
| 370 |
g_autoptr(GSocketControlMessage) fdm = make_fd_message (fds, n_fds); |
| 371 |
send_cmsgs (socket, &fdm, 1, 1); |
| 372 |
} |
| 373 |
|
| 374 |
static void |
| 375 |
send_fd (GSocket *socket, |
| 376 |
gint fd) |
| 377 |
{ |
| 378 |
send_fds (socket, &fd, 1); |
| 379 |
} |
| 380 |
|
| 381 |
static void |
| 382 |
assert_base_state (GSocket *one, |
| 383 |
GSocket *two) |
| 384 |
{ |
| 385 |
assert_live_control_messages (0); |
| 386 |
g_assert (g_socket_condition_check (one, G_IO_IN | G_IO_OUT) == G_IO_OUT); |
| 387 |
g_assert (g_socket_condition_check (two, G_IO_IN | G_IO_OUT) == G_IO_OUT); |
| 388 |
} |
| 389 |
|
| 390 |
static void |
| 391 |
test_unix_socket_simple (void) |
| 392 |
{ |
| 393 |
g_autoptr(GSocket) one, two; |
| 394 |
|
| 395 |
cockpit_socket_socketpair (&one, &two); |
| 396 |
assert_base_state (one, two); |
| 397 |
|
| 398 |
/* boring */ |
| 399 |
send_nothing (one, 1); |
| 400 |
receive_nothing (two); |
| 401 |
assert_base_state (one, two); |
| 402 |
|
| 403 |
send_nothing (two, 1); |
| 404 |
receive_nothing (one); |
| 405 |
assert_base_state (one, two); |
| 406 |
|
| 407 |
/* try a single fd */ |
| 408 |
send_fd (one, 1); |
| 409 |
GError *error = NULL; |
| 410 |
gint fd = receive_fd (two, &error); |
| 411 |
g_assert_no_error (error); |
| 412 |
g_assert (fd != -1); |
| 413 |
close (fd); |
| 414 |
assert_base_state (one, two); |
| 415 |
|
| 416 |
/* try multiple fds */ |
| 417 |
send_fds (one, (gint []){ 0, 1, 2}, 3); |
| 418 |
gint n_fds = 0; /* gcc is unhappy without this... */ |
| 419 |
gint *fds = receive_fds (two, &n_fds, &error); |
| 420 |
g_assert_no_error (error); |
| 421 |
g_assert (fds != NULL); |
| 422 |
g_assert_cmpint (n_fds, ==, 3); |
| 423 |
free_fds (&fds, &n_fds); |
| 424 |
assert_base_state (one, two); |
| 425 |
|
| 426 |
/* mix-and-match with cockpitfdpassing */ |
| 427 |
int two_fd = g_socket_get_fd (two); |
| 428 |
g_unix_set_fd_nonblocking (two_fd, FALSE, &error); |
| 429 |
g_assert_no_error (error); |
| 430 |
|
| 431 |
/* one -> two */ |
| 432 |
send_fd (one, 1); |
| 433 |
int r = cockpit_socket_receive_fd (two_fd, &fd); |
| 434 |
g_assert_cmpint (r, ==, 1); |
| 435 |
g_assert (fd != -1); |
| 436 |
close (fd); |
| 437 |
|
| 438 |
/* two -> one */ |
| 439 |
cockpit_socket_send_fd (two_fd, 1); |
| 440 |
fd = receive_fd (one, &error); |
| 441 |
g_assert_no_error (error); |
| 442 |
g_assert (fd != -1); |
| 443 |
close (fd); |
| 444 |
assert_base_state (one, two); |
| 445 |
} |
| 446 |
|
| 447 |
static void |
| 448 |
test_unix_socket_partial_read (void) |
| 449 |
{ |
| 450 |
g_autoptr(GSocket) one, two; |
| 451 |
|
| 452 |
cockpit_socket_socketpair (&one, &two); |
| 453 |
assert_base_state (one, two); |
| 454 |
|
| 455 |
/* test unspecified behaviour, which we rely on: the cmsg should be |
| 456 |
* read with the first byte of the message with which it was sent. |
| 457 |
* |
| 458 |
* we depend on this because we start on the cockpit-ws side by |
| 459 |
* reading a single byte, but we will send the json blob as part of |
| 460 |
* the first full packet from cockpit-tls. |
| 461 |
*/ |
| 462 |
send_nothing (one, 10); |
| 463 |
int fd1 = 1; |
| 464 |
g_autoptr(GSocketControlMessage) fdm = make_fd_message (&fd1, 1); |
| 465 |
send_cmsgs (one, &fdm, 1, 10); |
| 466 |
|
| 467 |
for (gint i = 0; i < 20; i++) |
| 468 |
{ |
| 469 |
g_autoptr(GError) error = NULL; |
| 470 |
gint fd = receive_fd (two, &error); |
| 471 |
|
| 472 |
if (fd != -1) |
| 473 |
{ |
| 474 |
/* we expect to get this at the 11th try */ |
| 475 |
g_assert_cmpint (i, ==, 10); |
| 476 |
close (fd); |
| 477 |
} |
| 478 |
else |
| 479 |
{ |
| 480 |
cockpit_assert_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_INVAL, "*0 control message*"); |
| 481 |
} |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
static void |
| 486 |
test_unix_socket_error_cases (void) |
| 487 |
{ |
| 488 |
g_autoptr(GSocket) one, two; |
| 489 |
|
| 490 |
cockpit_socket_socketpair (&one, &two); |
| 491 |
assert_base_state (one, two); |
| 492 |
|
| 493 |
/* try receiving an fd when nothing was sent */ |
| 494 |
send_nothing (one, 1); |
| 495 |
GError *error = NULL; |
| 496 |
int fd = receive_fd (two, &error); |
| 497 |
g_assert (fd == -1); |
| 498 |
cockpit_assert_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_INVAL, "*0 control message*"); |
| 499 |
g_clear_error (&error); |
| 500 |
assert_base_state (one, two); |
| 501 |
|
| 502 |
/* see what happens if we send more fds than expected */ |
| 503 |
send_fds (one, (const gint []){ 0, 1, 2}, 3); |
| 504 |
fd = receive_fd (two, &error); |
| 505 |
g_assert (fd == -1); |
| 506 |
cockpit_assert_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_INVAL, "*received 3*1 expected*"); |
| 507 |
g_clear_error (&error); |
| 508 |
assert_base_state (one, two); |
| 509 |
|
| 510 |
/* The remaining tests rely on receiving SCM_CREDENTIALS. We need to |
| 511 |
* enable SO_PASSCRED for that. |
| 512 |
*/ |
| 513 |
int truth = 1; |
| 514 |
int r = setsockopt (g_socket_get_fd (two), SOL_SOCKET, SO_PASSCRED, &truth, sizeof truth); |
| 515 |
g_assert (r == 0); |
| 516 |
|
| 517 |
/* see what happens if we send the wrong message type */ |
| 518 |
g_autoptr(GSocketControlMessage) creds = g_unix_credentials_message_new (); |
| 519 |
send_cmsgs (one, &creds, 1, 1); |
| 520 |
fd = receive_fd (two, &error); |
| 521 |
g_assert (fd == -1); |
| 522 |
cockpit_assert_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_INVAL, |
| 523 |
"*GUnixCredentialsMessage*GUnixFDMessage expected*"); |
| 524 |
g_clear_error (&error); |
| 525 |
assert_base_state (one, two); |
| 526 |
|
| 527 |
/* see what happens if we send too many messages */ |
| 528 |
g_autoptr(GUnixFDList) fdl = g_unix_fd_list_new (); |
| 529 |
g_unix_fd_list_append (fdl, 1, &error); |
| 530 |
g_assert_no_error (error); |
| 531 |
g_autoptr(GSocketControlMessage) fdm = g_unix_fd_message_new_with_fd_list (fdl); |
| 532 |
GSocketControlMessage *messages[] = { creds, fdm }; |
| 533 |
send_cmsgs (one, messages, G_N_ELEMENTS (messages), 1); |
| 534 |
fd = receive_fd (two, &error); |
| 535 |
g_assert (fd == -1); |
| 536 |
g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL); |
| 537 |
g_assert (strstr (error->message, "2 control messages (one message")); |
| 538 |
g_clear_error (&error); |
| 539 |
assert_base_state (one, two); |
| 540 |
} |
| 541 |
|
| 542 |
/* --- putting it all together (unix sockets) --- */ |
| 543 |
|
| 544 |
static void |
| 545 |
test_unix_socket_combined (void) |
| 546 |
{ |
| 547 |
g_autoptr(GSocket) one, two; |
| 548 |
|
| 549 |
cockpit_socket_socketpair (&one, &two); |
| 550 |
assert_base_state (one, two); |
| 551 |
|
| 552 |
FILE *stream = cockpit_json_print_open_memfd ("xyz", 1); |
| 553 |
cockpit_json_print_string_property (stream, "test", "it worked!", -1); |
| 554 |
gint fd = cockpit_json_print_finish_memfd (&stream); |
| 555 |
|
| 556 |
send_fd (one, fd); |
| 557 |
close (fd); |
| 558 |
|
| 559 |
g_auto(CockpitControlMessages) ccm = COCKPIT_CONTROL_MESSAGES_INIT; |
| 560 |
receive_cmsgs (two, &ccm); |
| 561 |
|
| 562 |
g_autoptr(GError) error = NULL; |
| 563 |
g_autoptr(JsonObject) json = cockpit_memfd_read_json_from_control_messages (&ccm, &error); |
| 564 |
g_assert_no_error (error); |
| 565 |
|
| 566 |
g_assert_cmpint (json_object_get_int_member (json, "version"), ==, 1); |
| 567 |
g_assert_cmpstr (json_object_get_string_member (json, "test"), ==, "it worked!"); |
| 568 |
} |
| 569 |
|
| 570 |
int |
| 571 |
main (int argc, |
| 572 |
char **argv) |
| 573 |
{ |
| 574 |
cockpit_test_init (&argc, &argv); |
| 575 |
|
| 576 |
g_test_add_func ("/json/fd/memfd/simple", test_memfd_simple); |
| 577 |
g_test_add_func ("/json/fd/memfd/error-cases", test_memfd_error_cases); |
| 578 |
g_test_add_func ("/json/fd/memfd/json", test_memfd_json); |
| 579 |
g_test_add_func ("/json/fd/memfd/json/error-cases", test_memfd_json_error_cases); |
| 580 |
g_test_add_func ("/json/fd/unix-socket/simple", test_unix_socket_simple); |
| 581 |
g_test_add_func ("/json/fd/unix-socket/partial-read", test_unix_socket_partial_read); |
| 582 |
g_test_add_func ("/json/fd/unix-socket/error-cases", test_unix_socket_error_cases); |
| 583 |
g_test_add_func ("/json/fd/unix-socket/combined", test_unix_socket_combined); |
| 584 |
|
| 585 |
return g_test_run (); |
| 586 |
} |