| 1 |
/* |
| 2 |
* Copyright (C) 2014 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "config.h" |
| 7 |
|
| 8 |
#include "cockpitpipe.h" |
| 9 |
|
| 10 |
#include "testlib/cockpittest.h" |
| 11 |
#include "testlib/mock-pressure.h" |
| 12 |
|
| 13 |
#include <glib.h> |
| 14 |
#include <glib/gstdio.h> |
| 15 |
#include <gio/gunixsocketaddress.h> |
| 16 |
|
| 17 |
#include <sys/uio.h> |
| 18 |
#include <sys/wait.h> |
| 19 |
#include <string.h> |
| 20 |
|
| 21 |
/* ---------------------------------------------------------------------------- |
| 22 |
* Mock |
| 23 |
*/ |
| 24 |
|
| 25 |
static GType mock_echo_pipe_get_type (void) G_GNUC_CONST; |
| 26 |
|
| 27 |
typedef struct { |
| 28 |
CockpitPipe parent; |
| 29 |
GByteArray *received; |
| 30 |
gboolean closed; |
| 31 |
gchar *problem; |
| 32 |
} MockEchoPipe; |
| 33 |
|
| 34 |
typedef CockpitPipeClass MockEchoPipeClass; |
| 35 |
|
| 36 |
G_DEFINE_TYPE (MockEchoPipe, mock_echo_pipe, COCKPIT_TYPE_PIPE); |
| 37 |
|
| 38 |
static void |
| 39 |
mock_echo_pipe_read (CockpitPipe *pipe, |
| 40 |
GByteArray *buffer, |
| 41 |
gboolean end_of_data) |
| 42 |
{ |
| 43 |
MockEchoPipe *self = (MockEchoPipe *)pipe; |
| 44 |
g_byte_array_append (self->received, buffer->data, buffer->len); |
| 45 |
g_byte_array_set_size (buffer, 0); |
| 46 |
} |
| 47 |
|
| 48 |
static void |
| 49 |
mock_echo_pipe_close (CockpitPipe *pipe, |
| 50 |
const gchar *problem) |
| 51 |
{ |
| 52 |
MockEchoPipe *self = (MockEchoPipe *)pipe; |
| 53 |
g_assert (!self->closed); |
| 54 |
self->closed = TRUE; |
| 55 |
self->problem = g_strdup (problem); |
| 56 |
} |
| 57 |
|
| 58 |
static void |
| 59 |
mock_echo_pipe_init (MockEchoPipe *self) |
| 60 |
{ |
| 61 |
self->received = g_byte_array_new (); |
| 62 |
} |
| 63 |
|
| 64 |
static void |
| 65 |
mock_echo_pipe_finalize (GObject *object) |
| 66 |
{ |
| 67 |
MockEchoPipe *self = (MockEchoPipe *)object; |
| 68 |
|
| 69 |
g_byte_array_free (self->received, TRUE); |
| 70 |
g_free (self->problem); |
| 71 |
|
| 72 |
G_OBJECT_CLASS (mock_echo_pipe_parent_class)->finalize (object); |
| 73 |
} |
| 74 |
static void |
| 75 |
mock_echo_pipe_class_init (MockEchoPipeClass *klass) |
| 76 |
{ |
| 77 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 78 |
CockpitPipeClass *pipe_class = COCKPIT_PIPE_CLASS (klass); |
| 79 |
|
| 80 |
object_class->finalize = mock_echo_pipe_finalize; |
| 81 |
|
| 82 |
pipe_class->read = mock_echo_pipe_read; |
| 83 |
pipe_class->close = mock_echo_pipe_close; |
| 84 |
} |
| 85 |
|
| 86 |
/* ---------------------------------------------------------------------------- |
| 87 |
* Testing |
| 88 |
*/ |
| 89 |
|
| 90 |
typedef struct { |
| 91 |
CockpitPipe *pipe; |
| 92 |
guint timeout; |
| 93 |
} TestCase; |
| 94 |
|
| 95 |
typedef struct { |
| 96 |
const gchar *pipe_type_name; |
| 97 |
const gchar *command; |
| 98 |
gboolean no_timeout; |
| 99 |
} TestFixture; |
| 100 |
|
| 101 |
static gboolean |
| 102 |
on_timeout_abort (gpointer unused) |
| 103 |
{ |
| 104 |
g_error ("timed out"); |
| 105 |
return FALSE; |
| 106 |
} |
| 107 |
|
| 108 |
static void |
| 109 |
setup_timeout (TestCase *tc, |
| 110 |
gconstpointer data) |
| 111 |
{ |
| 112 |
const TestFixture *fixture = data; |
| 113 |
if (!fixture || !fixture->no_timeout) |
| 114 |
tc->timeout = g_timeout_add_seconds (10, on_timeout_abort, tc); |
| 115 |
} |
| 116 |
|
| 117 |
static void |
| 118 |
setup_simple (TestCase *tc, |
| 119 |
gconstpointer data) |
| 120 |
{ |
| 121 |
const TestFixture *fixture = data; |
| 122 |
const gchar *pipe_type; |
| 123 |
GError *error = NULL; |
| 124 |
gchar **argv; |
| 125 |
int fds[2]; |
| 126 |
GPid pid = 0; |
| 127 |
|
| 128 |
setup_timeout (tc, data); |
| 129 |
|
| 130 |
pipe_type = "MockEchoPipe"; |
| 131 |
if (fixture && fixture->pipe_type_name) |
| 132 |
pipe_type = fixture->pipe_type_name; |
| 133 |
|
| 134 |
if (fixture && fixture->command) |
| 135 |
{ |
| 136 |
g_shell_parse_argv (fixture->command, NULL, &argv, &error); |
| 137 |
g_assert_no_error (error); |
| 138 |
|
| 139 |
g_spawn_async_with_pipes (NULL, argv, NULL, |
| 140 |
G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH, NULL, NULL, |
| 141 |
&pid, fds + 1, fds + 0, NULL, |
| 142 |
&error); |
| 143 |
g_assert_no_error (error); |
| 144 |
g_strfreev (argv); |
| 145 |
} |
| 146 |
else |
| 147 |
{ |
| 148 |
if (pipe (fds) < 0) |
| 149 |
g_assert_not_reached (); |
| 150 |
} |
| 151 |
|
| 152 |
tc->pipe = g_object_new (g_type_from_name (pipe_type), |
| 153 |
"name", "test", |
| 154 |
"in-fd", fds[0], |
| 155 |
"out-fd", fds[1], |
| 156 |
"pid", pid, |
| 157 |
NULL); |
| 158 |
} |
| 159 |
|
| 160 |
static void |
| 161 |
teardown (TestCase *tc, |
| 162 |
gconstpointer data) |
| 163 |
{ |
| 164 |
if (tc->pipe) |
| 165 |
{ |
| 166 |
g_object_add_weak_pointer (G_OBJECT (tc->pipe), |
| 167 |
(gpointer *)&tc->pipe); |
| 168 |
g_object_unref (tc->pipe); |
| 169 |
|
| 170 |
/* If this asserts, outstanding references to transport */ |
| 171 |
g_assert (tc->pipe == NULL); |
| 172 |
} |
| 173 |
|
| 174 |
if (tc->timeout) |
| 175 |
g_source_remove (tc->timeout); |
| 176 |
} |
| 177 |
|
| 178 |
static gboolean |
| 179 |
on_timeout_set_flag (gpointer user_data) |
| 180 |
{ |
| 181 |
gboolean *data = user_data; |
| 182 |
g_assert (user_data); |
| 183 |
g_assert (*data == FALSE); |
| 184 |
*data = TRUE; |
| 185 |
return FALSE; |
| 186 |
} |
| 187 |
|
| 188 |
static void |
| 189 |
test_echo_and_close (TestCase *tc, |
| 190 |
gconstpointer data) |
| 191 |
{ |
| 192 |
MockEchoPipe *echo_pipe = (MockEchoPipe *)tc->pipe; |
| 193 |
GBytes *sent, *bytes; |
| 194 |
|
| 195 |
sent = g_bytes_new_static ("the message", 11); |
| 196 |
cockpit_pipe_write (tc->pipe, sent); |
| 197 |
|
| 198 |
while (echo_pipe->received->len < 11) |
| 199 |
g_main_context_iteration (NULL, TRUE); |
| 200 |
|
| 201 |
g_byte_array_ref (echo_pipe->received); |
| 202 |
bytes = g_byte_array_free_to_bytes (echo_pipe->received); |
| 203 |
g_assert (g_bytes_equal (bytes, sent)); |
| 204 |
g_bytes_unref (sent); |
| 205 |
g_bytes_unref (bytes); |
| 206 |
|
| 207 |
cockpit_pipe_close (tc->pipe, NULL); |
| 208 |
|
| 209 |
while (!echo_pipe->closed) |
| 210 |
g_main_context_iteration (NULL, TRUE); |
| 211 |
} |
| 212 |
|
| 213 |
static void |
| 214 |
test_echo_queue (TestCase *tc, |
| 215 |
gconstpointer data) |
| 216 |
{ |
| 217 |
MockEchoPipe *echo_pipe = (MockEchoPipe *)tc->pipe; |
| 218 |
GBytes *sent; |
| 219 |
|
| 220 |
sent = g_bytes_new_static ("one", 3); |
| 221 |
cockpit_pipe_write (tc->pipe, sent); |
| 222 |
g_bytes_unref (sent); |
| 223 |
sent = g_bytes_new_static ("two", 3); |
| 224 |
cockpit_pipe_write (tc->pipe, sent); |
| 225 |
g_bytes_unref (sent); |
| 226 |
|
| 227 |
/* Only closes after above are sent */ |
| 228 |
cockpit_pipe_close (tc->pipe, NULL); |
| 229 |
|
| 230 |
while (!echo_pipe->closed) |
| 231 |
g_main_context_iteration (NULL, TRUE); |
| 232 |
|
| 233 |
g_assert_cmpint (echo_pipe->received->len, ==, 6); |
| 234 |
g_assert (memcmp (echo_pipe->received->data, "onetwo", 6) == 0); |
| 235 |
} |
| 236 |
|
| 237 |
static const TestFixture fixture_no_timeout = { |
| 238 |
.no_timeout = TRUE |
| 239 |
}; |
| 240 |
|
| 241 |
static void |
| 242 |
test_echo_large (TestCase *tc, |
| 243 |
gconstpointer data) |
| 244 |
{ |
| 245 |
MockEchoPipe *echo_pipe = (MockEchoPipe *)tc->pipe; |
| 246 |
GBytes *sent; |
| 247 |
|
| 248 |
/* Medium length */ |
| 249 |
sent = g_bytes_new_take (g_strnfill (1020, '!'), 1020); |
| 250 |
cockpit_pipe_write (tc->pipe, sent); |
| 251 |
while (echo_pipe->received->len < g_bytes_get_size (sent)) |
| 252 |
g_main_context_iteration (NULL, TRUE); |
| 253 |
g_assert_cmpint (echo_pipe->received->len, ==, g_bytes_get_size (sent)); |
| 254 |
g_assert (memcmp (echo_pipe->received->data, g_bytes_get_data (sent, NULL), g_bytes_get_size (sent)) == 0); |
| 255 |
g_bytes_unref (sent); |
| 256 |
|
| 257 |
g_byte_array_set_size (echo_pipe->received, 0); |
| 258 |
|
| 259 |
/* Extra large */ |
| 260 |
sent = g_bytes_new_take (g_strnfill (10 * 1000 * 1000, '?'), 10 * 1000 * 1000); |
| 261 |
cockpit_pipe_write (tc->pipe, sent); |
| 262 |
while (echo_pipe->received->len < g_bytes_get_size (sent)) |
| 263 |
g_main_context_iteration (NULL, TRUE); |
| 264 |
g_assert_cmpint (echo_pipe->received->len, ==, g_bytes_get_size (sent)); |
| 265 |
g_assert (memcmp (echo_pipe->received->data, g_bytes_get_data (sent, NULL), g_bytes_get_size (sent)) == 0); |
| 266 |
g_bytes_unref (sent); |
| 267 |
|
| 268 |
g_byte_array_set_size (echo_pipe->received, 0); |
| 269 |
|
| 270 |
/* Double check that didn't csrew things up */ |
| 271 |
sent = g_bytes_new_static ("yello", 5); |
| 272 |
cockpit_pipe_write (tc->pipe, sent); |
| 273 |
while (echo_pipe->received->len < g_bytes_get_size (sent)) |
| 274 |
g_main_context_iteration (NULL, TRUE); |
| 275 |
g_assert_cmpint (echo_pipe->received->len, ==, g_bytes_get_size (sent)); |
| 276 |
g_assert (memcmp (echo_pipe->received->data, g_bytes_get_data (sent, NULL), g_bytes_get_size (sent)) == 0); |
| 277 |
g_bytes_unref (sent); |
| 278 |
} |
| 279 |
|
| 280 |
static void |
| 281 |
test_close_problem (TestCase *tc, |
| 282 |
gconstpointer data) |
| 283 |
{ |
| 284 |
MockEchoPipe *echo_pipe = (MockEchoPipe *)tc->pipe; |
| 285 |
|
| 286 |
cockpit_pipe_close (tc->pipe, "right now"); |
| 287 |
|
| 288 |
while (!echo_pipe->closed) |
| 289 |
g_main_context_iteration (NULL, TRUE); |
| 290 |
|
| 291 |
g_assert_cmpstr (echo_pipe->problem, ==, "right now"); |
| 292 |
} |
| 293 |
|
| 294 |
static const TestFixture fixture_pid = { |
| 295 |
.command = "cat" |
| 296 |
}; |
| 297 |
|
| 298 |
static void |
| 299 |
test_pid (TestCase *tc, |
| 300 |
gconstpointer data) |
| 301 |
{ |
| 302 |
MockEchoPipe *echo_pipe = (MockEchoPipe *)tc->pipe; |
| 303 |
GPid pid; |
| 304 |
GPid check; |
| 305 |
|
| 306 |
g_assert (cockpit_pipe_get_pid (tc->pipe, &pid)); |
| 307 |
g_assert (pid != 0); |
| 308 |
|
| 309 |
/* Test it's real */ |
| 310 |
g_assert_cmpint (kill (pid, SIGTERM), ==, 0); |
| 311 |
|
| 312 |
/* Should still be available after closing */ |
| 313 |
while (!echo_pipe->closed) |
| 314 |
g_main_context_iteration (NULL, TRUE); |
| 315 |
g_assert (cockpit_pipe_get_pid (tc->pipe, &check)); |
| 316 |
g_assert_cmpuint (pid, ==, check); |
| 317 |
} |
| 318 |
|
| 319 |
static void |
| 320 |
on_pressure_set_throttle (CockpitPipe *pipe, |
| 321 |
gboolean throttle, |
| 322 |
gpointer user_data) |
| 323 |
{ |
| 324 |
gint *data = user_data; |
| 325 |
g_assert (user_data != NULL); |
| 326 |
*data = throttle ? 1 : 0; |
| 327 |
} |
| 328 |
|
| 329 |
static void |
| 330 |
test_pressure_queue (TestCase *tc, |
| 331 |
gconstpointer data) |
| 332 |
{ |
| 333 |
MockEchoPipe *echo_pipe = (MockEchoPipe *)tc->pipe; |
| 334 |
gint throttle = -1; |
| 335 |
GBytes *sent; |
| 336 |
gint i; |
| 337 |
|
| 338 |
g_signal_connect (tc->pipe, "pressure", G_CALLBACK (on_pressure_set_throttle), &throttle); |
| 339 |
sent = g_bytes_new_take (g_strnfill (10 * 1000, '?'), 10 * 1000); |
| 340 |
|
| 341 |
/* Sent this a thousand times */ |
| 342 |
for (i = 0; i < 1000; i++) |
| 343 |
cockpit_pipe_write (tc->pipe, sent); |
| 344 |
|
| 345 |
g_bytes_unref (sent); |
| 346 |
|
| 347 |
/* |
| 348 |
* This should have put way too much in the queue, and thus |
| 349 |
* emitted the back-pressure signal. This signal would normally |
| 350 |
* be used by others to slow down their queueing, but in this |
| 351 |
* case we just check that it was fired. |
| 352 |
*/ |
| 353 |
g_assert_cmpint (throttle, ==, 1); |
| 354 |
throttle = -1; |
| 355 |
|
| 356 |
/* |
| 357 |
* Now the queue is getting drained. At some point, it will be |
| 358 |
* signaled that back pressure has been turned off |
| 359 |
*/ |
| 360 |
while (throttle == -1) |
| 361 |
g_main_context_iteration (NULL, TRUE); |
| 362 |
|
| 363 |
g_assert_cmpint (throttle, ==, 0); |
| 364 |
g_assert_cmpint (echo_pipe->received->len, >, 10 * 1000); |
| 365 |
} |
| 366 |
|
| 367 |
static void |
| 368 |
test_pressure_throttle (TestCase *tc, |
| 369 |
gconstpointer data) |
| 370 |
{ |
| 371 |
CockpitFlow *pressure = mock_pressure_new (); |
| 372 |
MockEchoPipe *echo_pipe = (MockEchoPipe *)tc->pipe; |
| 373 |
gboolean timeout = FALSE; |
| 374 |
gsize received; |
| 375 |
GBytes *sent; |
| 376 |
gint i; |
| 377 |
|
| 378 |
cockpit_flow_throttle (COCKPIT_FLOW (tc->pipe), pressure); |
| 379 |
sent = g_bytes_new_take (g_strnfill (1024, '?'), 1024); |
| 380 |
|
| 381 |
/* Send 2MB over the echo pipe */ |
| 382 |
for (i = 0; i < 2048; i++) |
| 383 |
cockpit_pipe_write (tc->pipe, sent); |
| 384 |
|
| 385 |
g_bytes_unref (sent); |
| 386 |
|
| 387 |
/* |
| 388 |
* So we should start receiving the echoed data. But we apply |
| 389 |
* the throttle pressure after receiving some data, and the rest |
| 390 |
* just waits. |
| 391 |
*/ |
| 392 |
while (echo_pipe->received->len == 0) |
| 393 |
g_main_context_iteration (NULL, TRUE); |
| 394 |
cockpit_flow_emit_pressure (pressure, TRUE); |
| 395 |
|
| 396 |
received = echo_pipe->received->len; |
| 397 |
g_assert_cmpint (received, <, 2048 * 1024); |
| 398 |
|
| 399 |
/* Now remaining data input should wait, no further data received*/ |
| 400 |
g_timeout_add_seconds (2, on_timeout_set_flag, &timeout); |
| 401 |
while (timeout == FALSE) |
| 402 |
g_main_context_iteration (NULL, TRUE); |
| 403 |
g_assert_cmpint (received, ==, echo_pipe->received->len); |
| 404 |
|
| 405 |
/* Remove the pressure, and we should get more data */ |
| 406 |
cockpit_flow_emit_pressure (pressure, FALSE); |
| 407 |
while (received < echo_pipe->received->len) |
| 408 |
g_main_context_iteration (NULL, TRUE); |
| 409 |
|
| 410 |
/* Clearing the throttle should work too. This pressure signal has no effect */ |
| 411 |
cockpit_flow_throttle (COCKPIT_FLOW (tc->pipe), NULL); |
| 412 |
cockpit_flow_emit_pressure (pressure, TRUE); |
| 413 |
|
| 414 |
/* Now wait for the remaining data */ |
| 415 |
while (echo_pipe->received->len < 2048 * 1024) |
| 416 |
g_main_context_iteration (NULL, TRUE); |
| 417 |
|
| 418 |
g_object_unref (pressure); |
| 419 |
} |
| 420 |
|
| 421 |
static const TestFixture fixture_buffer = { |
| 422 |
.pipe_type_name = "CockpitPipe" |
| 423 |
}; |
| 424 |
|
| 425 |
static void |
| 426 |
test_buffer (TestCase *tc, |
| 427 |
gconstpointer data) |
| 428 |
{ |
| 429 |
GByteArray *buffer; |
| 430 |
GBytes *sent; |
| 431 |
|
| 432 |
buffer = cockpit_pipe_get_buffer (tc->pipe); |
| 433 |
g_assert (buffer != NULL); |
| 434 |
g_assert_cmpuint (buffer->len, ==, 0); |
| 435 |
|
| 436 |
/* Including null terminator */ |
| 437 |
sent = g_bytes_new_static ("blahdeedoo", 11); |
| 438 |
cockpit_pipe_write (tc->pipe, sent); |
| 439 |
g_bytes_unref (sent); |
| 440 |
|
| 441 |
while (buffer->len == 0) |
| 442 |
g_main_context_iteration (NULL, TRUE); |
| 443 |
|
| 444 |
g_assert_cmpint (buffer->len, ==, 11); |
| 445 |
g_assert_cmpstr ((gchar *)buffer->data, ==, "blahdeedoo"); |
| 446 |
} |
| 447 |
|
| 448 |
static void |
| 449 |
test_skip_zero (TestCase *tc, |
| 450 |
gconstpointer data) |
| 451 |
{ |
| 452 |
MockEchoPipe *echo_pipe = (MockEchoPipe *)tc->pipe; |
| 453 |
GBytes *sent; |
| 454 |
GBytes *zero; |
| 455 |
|
| 456 |
/* Including null terminator */ |
| 457 |
sent = g_bytes_new_static ("blah", 4); |
| 458 |
zero = g_bytes_new_static ("", 0); |
| 459 |
cockpit_pipe_write (tc->pipe, sent); |
| 460 |
cockpit_pipe_write (tc->pipe, zero); |
| 461 |
cockpit_pipe_write (tc->pipe, sent); |
| 462 |
g_bytes_unref (zero); |
| 463 |
g_bytes_unref (sent); |
| 464 |
|
| 465 |
while (echo_pipe->received->len < 8) |
| 466 |
g_main_context_iteration (NULL, TRUE); |
| 467 |
|
| 468 |
g_assert_cmpint (echo_pipe->received->len, ==, 8); |
| 469 |
g_byte_array_append (echo_pipe->received, (guint8 *)"", 1); |
| 470 |
g_assert_cmpstr ((gchar *)echo_pipe->received->data, ==, "blahblah"); |
| 471 |
} |
| 472 |
|
| 473 |
static const TestFixture fixture_exit_success = { |
| 474 |
.command = "true" |
| 475 |
}; |
| 476 |
|
| 477 |
static void |
| 478 |
test_exit_success (TestCase *tc, |
| 479 |
gconstpointer data) |
| 480 |
{ |
| 481 |
MockEchoPipe *echo_pipe = (MockEchoPipe *)tc->pipe; |
| 482 |
gint status; |
| 483 |
|
| 484 |
while (!echo_pipe->closed) |
| 485 |
g_main_context_iteration (NULL, TRUE); |
| 486 |
|
| 487 |
status = cockpit_pipe_exit_status (tc->pipe); |
| 488 |
g_assert (WIFEXITED (status)); |
| 489 |
g_assert_cmpint (WEXITSTATUS (status), ==, 0); |
| 490 |
|
| 491 |
} |
| 492 |
|
| 493 |
static const TestFixture fixture_exit_fail = { |
| 494 |
.command = "sh -c 'exit 5'" |
| 495 |
}; |
| 496 |
|
| 497 |
static void |
| 498 |
test_exit_fail (TestCase *tc, |
| 499 |
gconstpointer data) |
| 500 |
{ |
| 501 |
MockEchoPipe *echo_pipe = (MockEchoPipe *)tc->pipe; |
| 502 |
gint status; |
| 503 |
|
| 504 |
while (!echo_pipe->closed) |
| 505 |
g_main_context_iteration (NULL, TRUE); |
| 506 |
|
| 507 |
status = cockpit_pipe_exit_status (tc->pipe); |
| 508 |
g_assert (WIFEXITED (status)); |
| 509 |
g_assert_cmpint (WEXITSTATUS (status), ==, 5); |
| 510 |
} |
| 511 |
|
| 512 |
static const TestFixture fixture_exit_signal = { |
| 513 |
.command = "cat" |
| 514 |
}; |
| 515 |
|
| 516 |
static void |
| 517 |
test_exit_signal (TestCase *tc, |
| 518 |
gconstpointer data) |
| 519 |
{ |
| 520 |
MockEchoPipe *echo_pipe = (MockEchoPipe *)tc->pipe; |
| 521 |
gint status; |
| 522 |
GPid pid = 0; |
| 523 |
|
| 524 |
g_object_get (tc->pipe, "pid", &pid, NULL); |
| 525 |
g_assert (pid != 0); |
| 526 |
|
| 527 |
kill (pid, SIGINT); |
| 528 |
|
| 529 |
while (!echo_pipe->closed) |
| 530 |
g_main_context_iteration (NULL, TRUE); |
| 531 |
|
| 532 |
status = cockpit_pipe_exit_status (tc->pipe); |
| 533 |
g_assert (!WIFEXITED (status)); |
| 534 |
g_assert (WIFSIGNALED (status)); |
| 535 |
g_assert_cmpint (WTERMSIG (status), ==, SIGINT); |
| 536 |
} |
| 537 |
|
| 538 |
static void |
| 539 |
test_read_error (void) |
| 540 |
{ |
| 541 |
MockEchoPipe *echo_pipe; |
| 542 |
int out; |
| 543 |
|
| 544 |
/* Assuming FD 1000 is not taken */ |
| 545 |
g_assert (write (1000, "1", 1) < 0); |
| 546 |
|
| 547 |
out = dup (2); |
| 548 |
g_assert (out >= 0); |
| 549 |
|
| 550 |
g_test_expect_message ("cockpit-protocol", G_LOG_LEVEL_WARNING, "*Bad file descriptor"); |
| 551 |
g_test_expect_message ("cockpit-protocol", G_LOG_LEVEL_WARNING, "*Bad file descriptor"); |
| 552 |
|
| 553 |
/* Pass in a bad read descriptor */ |
| 554 |
echo_pipe = g_object_new (mock_echo_pipe_get_type (), |
| 555 |
"name", "test", |
| 556 |
"in-fd", 1000, |
| 557 |
"out-fd", out, |
| 558 |
NULL); |
| 559 |
|
| 560 |
while (!echo_pipe->closed) |
| 561 |
g_main_context_iteration (NULL, TRUE); |
| 562 |
|
| 563 |
cockpit_assert_expected (); |
| 564 |
|
| 565 |
g_assert_cmpstr (echo_pipe->problem, ==, "internal-error"); |
| 566 |
|
| 567 |
g_object_unref (echo_pipe); |
| 568 |
} |
| 569 |
|
| 570 |
static void |
| 571 |
test_write_error (void) |
| 572 |
{ |
| 573 |
MockEchoPipe *echo_pipe; |
| 574 |
GBytes *sent; |
| 575 |
int fds[2]; |
| 576 |
|
| 577 |
/* Just used so we have a valid fd */ |
| 578 |
if (pipe(fds) < 0) |
| 579 |
g_assert_not_reached (); |
| 580 |
|
| 581 |
g_test_expect_message ("cockpit-protocol", G_LOG_LEVEL_WARNING, "*Bad file descriptor"); |
| 582 |
g_test_expect_message ("cockpit-protocol", G_LOG_LEVEL_WARNING, "*Bad file descriptor"); |
| 583 |
|
| 584 |
/* Pass in a bad write descriptor */ |
| 585 |
echo_pipe = g_object_new (mock_echo_pipe_get_type (), |
| 586 |
"name", "test", |
| 587 |
"in-fd", fds[0], |
| 588 |
"out-fd", 1000, |
| 589 |
NULL); |
| 590 |
|
| 591 |
sent = g_bytes_new ("test", 4); |
| 592 |
cockpit_pipe_write (COCKPIT_PIPE (echo_pipe), sent); |
| 593 |
g_bytes_unref (sent); |
| 594 |
|
| 595 |
while (!echo_pipe->closed) |
| 596 |
g_main_context_iteration (NULL, TRUE); |
| 597 |
|
| 598 |
cockpit_assert_expected (); |
| 599 |
|
| 600 |
g_assert_cmpstr (echo_pipe->problem, ==, "internal-error"); |
| 601 |
|
| 602 |
close (fds[1]); |
| 603 |
|
| 604 |
g_object_unref (echo_pipe); |
| 605 |
} |
| 606 |
|
| 607 |
static void |
| 608 |
test_read_combined (void) |
| 609 |
{ |
| 610 |
MockEchoPipe *echo_pipe; |
| 611 |
struct iovec iov[4]; |
| 612 |
gint fds[2]; |
| 613 |
int out; |
| 614 |
|
| 615 |
if (pipe(fds) < 0) |
| 616 |
g_assert_not_reached (); |
| 617 |
|
| 618 |
out = dup (2); |
| 619 |
g_assert (out >= 0); |
| 620 |
|
| 621 |
/* Pass in a read end of the pipe */ |
| 622 |
echo_pipe = g_object_new (mock_echo_pipe_get_type (), |
| 623 |
"name", "test", |
| 624 |
"in-fd", fds[0], |
| 625 |
"out-fd", out, |
| 626 |
NULL); |
| 627 |
|
| 628 |
/* Write two messages to the pipe at once */ |
| 629 |
iov[0].iov_base = "one"; |
| 630 |
iov[0].iov_len = 3; |
| 631 |
iov[1].iov_base = "two"; |
| 632 |
iov[1].iov_len = 3; |
| 633 |
iov[2].iov_base = "three"; |
| 634 |
iov[2].iov_len = 5; |
| 635 |
iov[3].iov_base = "\0"; |
| 636 |
iov[3].iov_len = 1; |
| 637 |
g_assert_cmpint (writev (fds[1], iov, 4), ==, 12); |
| 638 |
|
| 639 |
while (echo_pipe->received->len < 12) |
| 640 |
g_main_context_iteration (NULL, TRUE); |
| 641 |
|
| 642 |
g_assert_cmpint (echo_pipe->received->len, ==, 12); |
| 643 |
g_assert_cmpstr ((gchar *)echo_pipe->received->data, ==, "onetwothree"); |
| 644 |
|
| 645 |
close (fds[1]); |
| 646 |
g_object_unref (echo_pipe); |
| 647 |
} |
| 648 |
|
| 649 |
static void |
| 650 |
test_consume_entire (void) |
| 651 |
{ |
| 652 |
GByteArray *buffer; |
| 653 |
GBytes *bytes; |
| 654 |
|
| 655 |
buffer = g_byte_array_new (); |
| 656 |
g_byte_array_append (buffer, (guint8 *)"Marmaalaaaade!", 15); |
| 657 |
|
| 658 |
bytes = cockpit_pipe_consume (buffer, 0, 15, 0); |
| 659 |
g_assert_cmpuint (buffer->len, ==, 0); |
| 660 |
g_byte_array_free (buffer, TRUE); |
| 661 |
|
| 662 |
g_assert_cmpuint (g_bytes_get_size (bytes), ==, 15); |
| 663 |
g_assert_cmpstr (g_bytes_get_data (bytes, NULL), ==, "Marmaalaaaade!"); |
| 664 |
g_bytes_unref (bytes); |
| 665 |
} |
| 666 |
|
| 667 |
static void |
| 668 |
test_consume_partial (void) |
| 669 |
{ |
| 670 |
GByteArray *buffer; |
| 671 |
GBytes *bytes; |
| 672 |
|
| 673 |
buffer = g_byte_array_new (); |
| 674 |
g_byte_array_append (buffer, (guint8 *)"Marmaalaaaade!", 15); |
| 675 |
|
| 676 |
bytes = cockpit_pipe_consume (buffer, 0, 7, 0); |
| 677 |
g_assert_cmpuint (buffer->len, ==, 8); |
| 678 |
g_assert_cmpstr ((gchar *)buffer->data, ==, "aaaade!"); |
| 679 |
g_byte_array_free (buffer, TRUE); |
| 680 |
|
| 681 |
g_assert_cmpuint (g_bytes_get_size (bytes), ==, 7); |
| 682 |
g_assert (memcmp (g_bytes_get_data (bytes, NULL), "Marmaal", 7) == 0); |
| 683 |
g_bytes_unref (bytes); |
| 684 |
} |
| 685 |
|
| 686 |
static void |
| 687 |
test_consume_skip (void) |
| 688 |
{ |
| 689 |
GByteArray *buffer; |
| 690 |
GBytes *bytes; |
| 691 |
|
| 692 |
buffer = g_byte_array_new (); |
| 693 |
g_byte_array_append (buffer, (guint8 *)"Marmaalaaaade!", 15); |
| 694 |
|
| 695 |
bytes = cockpit_pipe_consume (buffer, 7, 8, 0); |
| 696 |
g_assert_cmpuint (buffer->len, ==, 0); |
| 697 |
g_byte_array_free (buffer, TRUE); |
| 698 |
|
| 699 |
g_assert_cmpuint (g_bytes_get_size (bytes), ==, 8); |
| 700 |
g_assert_cmpstr (g_bytes_get_data (bytes, NULL), ==, "aaaade!"); |
| 701 |
g_bytes_unref (bytes); |
| 702 |
} |
| 703 |
|
| 704 |
static void |
| 705 |
test_buffer_skip (void) |
| 706 |
{ |
| 707 |
GByteArray *buffer; |
| 708 |
|
| 709 |
buffer = g_byte_array_new (); |
| 710 |
g_byte_array_append (buffer, (guint8 *)"Marmaalaaaade!", 15); |
| 711 |
|
| 712 |
cockpit_pipe_skip (buffer, 7); |
| 713 |
g_assert_cmpuint (buffer->len, ==, 8); |
| 714 |
|
| 715 |
g_assert_cmpstr ((char *)buffer->data, ==, "aaaade!"); |
| 716 |
g_byte_array_free (buffer, TRUE); |
| 717 |
} |
| 718 |
|
| 719 |
static void |
| 720 |
test_properties (void) |
| 721 |
{ |
| 722 |
CockpitPipe *tpipe; |
| 723 |
gchar *name; |
| 724 |
gint in; |
| 725 |
gint out; |
| 726 |
int fds[2]; |
| 727 |
|
| 728 |
if (pipe(fds) < 0) |
| 729 |
g_assert_not_reached (); |
| 730 |
|
| 731 |
tpipe = g_object_new (mock_echo_pipe_get_type (), |
| 732 |
"name", "testo", |
| 733 |
"in-fd", fds[0], |
| 734 |
"out-fd", fds[1], |
| 735 |
NULL); |
| 736 |
|
| 737 |
g_object_get (tpipe, "name", &name, "in-fd", &in, "out-fd", &out, NULL); |
| 738 |
g_assert_cmpstr (name, ==, "testo"); |
| 739 |
g_free (name); |
| 740 |
g_assert_cmpint (in, ==, fds[0]); |
| 741 |
g_assert_cmpint (out, ==, fds[1]); |
| 742 |
|
| 743 |
g_object_unref (tpipe); |
| 744 |
} |
| 745 |
|
| 746 |
static void |
| 747 |
on_close_get_flag (CockpitPipe *pipe, |
| 748 |
const gchar *problem, |
| 749 |
gpointer user_data) |
| 750 |
{ |
| 751 |
gboolean *retval = user_data; |
| 752 |
g_assert (*retval == FALSE); |
| 753 |
*retval = TRUE; |
| 754 |
} |
| 755 |
|
| 756 |
static void |
| 757 |
on_close_get_problem (CockpitPipe *pipe, |
| 758 |
const gchar *problem, |
| 759 |
gpointer user_data) |
| 760 |
{ |
| 761 |
gchar **retval = user_data; |
| 762 |
g_assert (retval != NULL && *retval == NULL); |
| 763 |
*retval = g_strdup (problem ? problem : ""); |
| 764 |
} |
| 765 |
|
| 766 |
static void |
| 767 |
test_spawn_and_read (void) |
| 768 |
{ |
| 769 |
gboolean closed = FALSE; |
| 770 |
GByteArray *buffer; |
| 771 |
CockpitPipe *pipe; |
| 772 |
|
| 773 |
const gchar *argv[] = { "/bin/sh", "-c", "set", NULL }; |
| 774 |
const gchar *env[] = { "ENVIRON=Marmalaaade", NULL, }; |
| 775 |
|
| 776 |
pipe = cockpit_pipe_spawn (argv, env, NULL, COCKPIT_PIPE_FLAGS_NONE); |
| 777 |
g_assert (pipe != NULL); |
| 778 |
g_signal_connect (pipe, "close", G_CALLBACK (on_close_get_flag), &closed); |
| 779 |
|
| 780 |
while (closed == FALSE) |
| 781 |
g_main_context_iteration (NULL, TRUE); |
| 782 |
|
| 783 |
buffer = cockpit_pipe_get_buffer (pipe); |
| 784 |
g_byte_array_append (buffer, (const guint8 *)"\0", 1); |
| 785 |
|
| 786 |
cockpit_assert_strmatch ((gchar *)buffer->data, "*ENVIRON*Marmalaaade*"); |
| 787 |
|
| 788 |
buffer = cockpit_pipe_get_stderr (pipe); |
| 789 |
g_assert (buffer == NULL); |
| 790 |
|
| 791 |
g_object_unref (pipe); |
| 792 |
} |
| 793 |
|
| 794 |
static void |
| 795 |
test_spawn_and_write (void) |
| 796 |
{ |
| 797 |
CockpitPipe *pipe; |
| 798 |
GByteArray *buffer; |
| 799 |
GBytes *sent; |
| 800 |
|
| 801 |
const gchar *argv[] = { "/bin/cat", NULL }; |
| 802 |
|
| 803 |
pipe = cockpit_pipe_spawn (argv, NULL, NULL, COCKPIT_PIPE_FLAGS_NONE); |
| 804 |
g_assert (pipe != NULL); |
| 805 |
|
| 806 |
/* Sending on the pipe before actually connected */ |
| 807 |
sent = g_bytes_new_static ("jola", 5); |
| 808 |
cockpit_pipe_write (pipe, sent); |
| 809 |
g_bytes_unref (sent); |
| 810 |
|
| 811 |
buffer = cockpit_pipe_get_buffer (pipe); |
| 812 |
while (buffer->len == 0) |
| 813 |
g_main_context_iteration (NULL, TRUE); |
| 814 |
|
| 815 |
g_assert_cmpuint (buffer->len, ==, 5); |
| 816 |
g_assert_cmpstr ((gchar *)buffer->data, ==, "jola"); |
| 817 |
|
| 818 |
g_object_unref (pipe); |
| 819 |
} |
| 820 |
|
| 821 |
static void |
| 822 |
test_spawn_and_fail (void) |
| 823 |
{ |
| 824 |
gchar *problem = NULL; |
| 825 |
CockpitPipe *pipe; |
| 826 |
|
| 827 |
const gchar *argv[] = { "/non-existent", NULL }; |
| 828 |
|
| 829 |
pipe = cockpit_pipe_spawn (argv, NULL, NULL, COCKPIT_PIPE_FLAGS_NONE); |
| 830 |
g_assert (pipe != NULL); |
| 831 |
g_signal_connect (pipe, "close", G_CALLBACK (on_close_get_problem), &problem); |
| 832 |
|
| 833 |
while (problem == NULL) |
| 834 |
g_main_context_iteration (NULL, TRUE); |
| 835 |
|
| 836 |
g_assert_cmpstr (problem, ==, "not-found"); |
| 837 |
g_free (problem); |
| 838 |
g_object_unref (pipe); |
| 839 |
} |
| 840 |
|
| 841 |
static void |
| 842 |
test_spawn_close_terminate (TestCase *tc, |
| 843 |
gconstpointer unused) |
| 844 |
{ |
| 845 |
CockpitPipe *pipe; |
| 846 |
gboolean closed = FALSE; |
| 847 |
gint status; |
| 848 |
|
| 849 |
const gchar *argv[] = { "/bin/sleep", "500", NULL }; |
| 850 |
|
| 851 |
pipe = cockpit_pipe_spawn (argv, NULL, NULL, COCKPIT_PIPE_FLAGS_NONE); |
| 852 |
g_assert (pipe != NULL); |
| 853 |
|
| 854 |
g_signal_connect (pipe, "close", G_CALLBACK (on_close_get_flag), &closed); |
| 855 |
cockpit_pipe_close (pipe, "terminate"); |
| 856 |
|
| 857 |
while (!closed) |
| 858 |
g_main_context_iteration (NULL, TRUE); |
| 859 |
|
| 860 |
status = cockpit_pipe_exit_status (pipe); |
| 861 |
g_assert (WIFSIGNALED (status)); |
| 862 |
g_assert_cmpint (WTERMSIG (status), ==, SIGTERM); |
| 863 |
|
| 864 |
g_object_unref (pipe); |
| 865 |
} |
| 866 |
|
| 867 |
static void |
| 868 |
test_spawn_close_clean (TestCase *tc, |
| 869 |
gconstpointer unused) |
| 870 |
{ |
| 871 |
CockpitPipe *pipe; |
| 872 |
gboolean closed = FALSE; |
| 873 |
gint status; |
| 874 |
|
| 875 |
const gchar *argv[] = { "/bin/cat", NULL }; |
| 876 |
|
| 877 |
pipe = cockpit_pipe_spawn (argv, NULL, NULL, COCKPIT_PIPE_FLAGS_NONE); |
| 878 |
g_assert (pipe != NULL); |
| 879 |
|
| 880 |
g_signal_connect (pipe, "close", G_CALLBACK (on_close_get_flag), &closed); |
| 881 |
cockpit_pipe_close (pipe, NULL); |
| 882 |
|
| 883 |
while (!closed) |
| 884 |
g_main_context_iteration (NULL, TRUE); |
| 885 |
|
| 886 |
status = cockpit_pipe_exit_status (pipe); |
| 887 |
g_assert (!WIFSIGNALED (status)); |
| 888 |
g_assert_cmpint (WEXITSTATUS (status), ==, 0); |
| 889 |
|
| 890 |
g_object_unref (pipe); |
| 891 |
} |
| 892 |
|
| 893 |
static void |
| 894 |
test_spawn_and_buffer_stderr (void) |
| 895 |
{ |
| 896 |
gboolean closed = FALSE; |
| 897 |
GByteArray *buffer; |
| 898 |
CockpitPipe *pipe; |
| 899 |
|
| 900 |
const gchar *argv[] = { "/bin/sh", "-c", "echo error >&2; echo output; echo error2 >&2", NULL }; |
| 901 |
|
| 902 |
pipe = cockpit_pipe_spawn (argv, NULL, NULL, COCKPIT_PIPE_STDERR_TO_MEMORY); |
| 903 |
g_assert (pipe != NULL); |
| 904 |
g_signal_connect (pipe, "close", G_CALLBACK (on_close_get_flag), &closed); |
| 905 |
|
| 906 |
while (closed == FALSE) |
| 907 |
g_main_context_iteration (NULL, TRUE); |
| 908 |
|
| 909 |
buffer = cockpit_pipe_get_buffer (pipe); |
| 910 |
g_assert (buffer != NULL); |
| 911 |
|
| 912 |
g_byte_array_append (buffer, (const guint8 *)"\0", 1); |
| 913 |
g_assert_cmpstr ((gchar *)buffer->data, ==, "output\n"); |
| 914 |
|
| 915 |
buffer = cockpit_pipe_get_stderr (pipe); |
| 916 |
g_assert (buffer != NULL); |
| 917 |
|
| 918 |
g_byte_array_append (buffer, (const guint8 *)"\0", 1); |
| 919 |
g_assert_cmpstr ((gchar *)buffer->data, ==, "error\nerror2\n"); |
| 920 |
|
| 921 |
g_object_unref (pipe); |
| 922 |
} |
| 923 |
|
| 924 |
typedef struct { |
| 925 |
GSocket *listen_sock; |
| 926 |
GSource *listen_source; |
| 927 |
GSocket *conn_sock; |
| 928 |
GSource *conn_source; |
| 929 |
GSocketAddress *address; |
| 930 |
} TestConnect; |
| 931 |
|
| 932 |
static gboolean |
| 933 |
on_socket_input (GSocket *socket, |
| 934 |
GIOCondition cond, |
| 935 |
gpointer user_data) |
| 936 |
{ |
| 937 |
gchar buffer[1024]; |
| 938 |
GError *error = NULL; |
| 939 |
gssize ret, wret; |
| 940 |
|
| 941 |
ret = g_socket_receive (socket, buffer, sizeof (buffer), NULL, &error); |
| 942 |
g_assert_no_error (error); |
| 943 |
|
| 944 |
if (ret == 0) |
| 945 |
{ |
| 946 |
g_socket_shutdown (socket, FALSE, TRUE, &error); |
| 947 |
g_assert_no_error (error); |
| 948 |
return FALSE; |
| 949 |
} |
| 950 |
|
| 951 |
g_assert (ret > 0); |
| 952 |
wret = g_socket_send (socket, buffer, ret, NULL, &error); |
| 953 |
g_assert_no_error (error); |
| 954 |
g_assert (wret == ret); |
| 955 |
return TRUE; |
| 956 |
} |
| 957 |
|
| 958 |
static gboolean |
| 959 |
on_socket_connection (GSocket *socket, |
| 960 |
GIOCondition cond, |
| 961 |
gpointer user_data) |
| 962 |
{ |
| 963 |
TestConnect *tc = user_data; |
| 964 |
GError *error = NULL; |
| 965 |
|
| 966 |
g_assert (tc->conn_source == NULL); |
| 967 |
tc->conn_sock = g_socket_accept (tc->listen_sock, NULL, &error); |
| 968 |
g_assert_no_error (error); |
| 969 |
|
| 970 |
tc->conn_source = g_socket_create_source (tc->conn_sock, G_IO_IN, NULL); |
| 971 |
g_source_set_callback (tc->conn_source, (GSourceFunc)on_socket_input, tc, NULL); |
| 972 |
g_source_attach (tc->conn_source, NULL); |
| 973 |
|
| 974 |
/* Only one connection */ |
| 975 |
return FALSE; |
| 976 |
} |
| 977 |
|
| 978 |
static void |
| 979 |
setup_connect (TestConnect *tc, |
| 980 |
gconstpointer data) |
| 981 |
{ |
| 982 |
GError *error = NULL; |
| 983 |
GInetAddress *inet; |
| 984 |
GSocketAddress *address; |
| 985 |
|
| 986 |
inet = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4); |
| 987 |
address = g_inet_socket_address_new (inet, 0); |
| 988 |
g_object_unref (inet); |
| 989 |
|
| 990 |
tc->listen_sock = g_socket_new (G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_STREAM, |
| 991 |
G_SOCKET_PROTOCOL_DEFAULT, &error); |
| 992 |
g_assert_no_error (error); |
| 993 |
|
| 994 |
g_socket_bind (tc->listen_sock, address, TRUE, &error); |
| 995 |
g_object_unref (address); |
| 996 |
g_assert_no_error (error); |
| 997 |
|
| 998 |
tc->address = g_socket_get_local_address (tc->listen_sock, &error); |
| 999 |
g_assert_no_error (error); |
| 1000 |
|
| 1001 |
g_socket_listen (tc->listen_sock, &error); |
| 1002 |
g_assert_no_error (error); |
| 1003 |
|
| 1004 |
tc->listen_source = g_socket_create_source (tc->listen_sock, G_IO_IN, NULL); |
| 1005 |
g_source_set_callback (tc->listen_source, (GSourceFunc)on_socket_connection, tc, NULL); |
| 1006 |
g_source_attach (tc->listen_source, NULL); |
| 1007 |
} |
| 1008 |
|
| 1009 |
static void |
| 1010 |
teardown_connect (TestConnect *tc, |
| 1011 |
gconstpointer data) |
| 1012 |
{ |
| 1013 |
g_object_unref (tc->address); |
| 1014 |
if (tc->conn_source) |
| 1015 |
{ |
| 1016 |
g_source_destroy (tc->conn_source); |
| 1017 |
g_source_unref (tc->conn_source); |
| 1018 |
} |
| 1019 |
if (tc->listen_source) |
| 1020 |
{ |
| 1021 |
g_source_destroy (tc->listen_source); |
| 1022 |
g_source_unref (tc->listen_source); |
| 1023 |
} |
| 1024 |
g_clear_object (&tc->listen_sock); |
| 1025 |
g_clear_object (&tc->conn_sock); |
| 1026 |
} |
| 1027 |
|
| 1028 |
static void |
| 1029 |
test_connect_and_read (TestConnect *tc, |
| 1030 |
gconstpointer user_data) |
| 1031 |
{ |
| 1032 |
CockpitPipe *pipe; |
| 1033 |
GError *error = NULL; |
| 1034 |
GByteArray *buffer; |
| 1035 |
|
| 1036 |
pipe = cockpit_pipe_connect ("broooo", tc->address); |
| 1037 |
g_assert (pipe != NULL); |
| 1038 |
|
| 1039 |
while (tc->conn_sock == NULL) |
| 1040 |
g_main_context_iteration (NULL, TRUE); |
| 1041 |
|
| 1042 |
/* Send the null terminator */ |
| 1043 |
g_assert_cmpint (g_socket_send (tc->conn_sock, "eier", 5, NULL, &error), ==, 5); |
| 1044 |
g_assert_no_error (error); |
| 1045 |
|
| 1046 |
buffer = cockpit_pipe_get_buffer (pipe); |
| 1047 |
while (buffer->len == 0) |
| 1048 |
g_main_context_iteration (NULL, TRUE); |
| 1049 |
|
| 1050 |
g_assert_cmpuint (buffer->len, ==, 5); |
| 1051 |
g_assert_cmpstr ((gchar *)buffer->data, ==, "eier"); |
| 1052 |
|
| 1053 |
g_object_unref (pipe); |
| 1054 |
} |
| 1055 |
|
| 1056 |
static void |
| 1057 |
test_connect_and_write (TestConnect *tc, |
| 1058 |
gconstpointer user_data) |
| 1059 |
{ |
| 1060 |
gchar buffer[8]; |
| 1061 |
CockpitPipe *pipe; |
| 1062 |
GError *error = NULL; |
| 1063 |
GBytes *sent; |
| 1064 |
|
| 1065 |
pipe = cockpit_pipe_connect ("broooo", tc->address); |
| 1066 |
g_assert (pipe != NULL); |
| 1067 |
|
| 1068 |
/* Sending on the pipe before actually connected */ |
| 1069 |
sent = g_bytes_new_static ("jola", 5); |
| 1070 |
cockpit_pipe_write (pipe, sent); |
| 1071 |
g_bytes_unref (sent); |
| 1072 |
g_assert (tc->conn_sock == NULL); |
| 1073 |
|
| 1074 |
/* Now we connect in main loop */ |
| 1075 |
while (tc->conn_sock == NULL) |
| 1076 |
g_main_context_iteration (NULL, TRUE); |
| 1077 |
|
| 1078 |
/* Read from the socket */ |
| 1079 |
g_assert_cmpint (g_socket_receive (tc->conn_sock, buffer, sizeof (buffer), NULL, &error), ==, 5); |
| 1080 |
g_assert_no_error (error); |
| 1081 |
|
| 1082 |
g_assert_cmpstr (buffer, ==, "jola"); |
| 1083 |
g_object_unref (pipe); |
| 1084 |
} |
| 1085 |
|
| 1086 |
static void |
| 1087 |
test_fail_not_found (void) |
| 1088 |
{ |
| 1089 |
CockpitPipe *pipe; |
| 1090 |
GSocketAddress *address; |
| 1091 |
gchar *problem = NULL; |
| 1092 |
|
| 1093 |
g_test_expect_message ("cockpit-protocol", G_LOG_LEVEL_MESSAGE, "*No such file or directory"); |
| 1094 |
|
| 1095 |
address = g_unix_socket_address_new ("/non-existent"); |
| 1096 |
pipe = cockpit_pipe_connect ("bad", address); |
| 1097 |
g_object_unref (address); |
| 1098 |
|
| 1099 |
/* Should not have closed at this point */ |
| 1100 |
g_assert (pipe != NULL); |
| 1101 |
g_signal_connect (pipe, "close", G_CALLBACK (on_close_get_problem), &problem); |
| 1102 |
|
| 1103 |
/* closes in main loop */ |
| 1104 |
while (problem == NULL) |
| 1105 |
g_main_context_iteration (NULL, TRUE); |
| 1106 |
|
| 1107 |
cockpit_assert_expected (); |
| 1108 |
|
| 1109 |
g_assert_cmpstr (problem, ==, "not-found"); |
| 1110 |
g_free (problem); |
| 1111 |
g_object_unref (pipe); |
| 1112 |
} |
| 1113 |
|
| 1114 |
static void |
| 1115 |
test_fail_access_denied (void) |
| 1116 |
{ |
| 1117 |
CockpitPipe *pipe; |
| 1118 |
GSocketAddress *address; |
| 1119 |
gchar *unix_path; |
| 1120 |
gchar *problem = NULL; |
| 1121 |
gint fd; |
| 1122 |
|
| 1123 |
if (geteuid () == 0) |
| 1124 |
{ |
| 1125 |
g_test_skip ("running as root"); |
| 1126 |
return; |
| 1127 |
} |
| 1128 |
|
| 1129 |
unix_path = g_strdup ("/tmp/cockpit-test-XXXXXX.sock"); |
| 1130 |
fd = g_mkstemp (unix_path); |
| 1131 |
g_assert_cmpint (fd, >=, 0); |
| 1132 |
|
| 1133 |
/* Take away all permissions from the file */ |
| 1134 |
g_assert_cmpint (fchmod (fd, 0000), ==, 0); |
| 1135 |
|
| 1136 |
g_test_expect_message ("cockpit-protocol", G_LOG_LEVEL_MESSAGE, "*Permission denied"); |
| 1137 |
|
| 1138 |
address = g_unix_socket_address_new (unix_path); |
| 1139 |
pipe = cockpit_pipe_connect ("bad", address); |
| 1140 |
g_object_unref (address); |
| 1141 |
|
| 1142 |
/* Should not have closed at this point */ |
| 1143 |
g_assert (pipe != NULL); |
| 1144 |
g_signal_connect (pipe, "close", G_CALLBACK (on_close_get_problem), &problem); |
| 1145 |
|
| 1146 |
/* closes in main loop */ |
| 1147 |
while (problem == NULL) |
| 1148 |
g_main_context_iteration (NULL, TRUE); |
| 1149 |
|
| 1150 |
cockpit_assert_expected (); |
| 1151 |
|
| 1152 |
g_assert_cmpstr (problem, ==, "access-denied"); |
| 1153 |
g_free (unix_path); |
| 1154 |
g_free (problem); |
| 1155 |
g_object_unref (pipe); |
| 1156 |
} |
| 1157 |
|
| 1158 |
static void |
| 1159 |
test_problem_later (void) |
| 1160 |
{ |
| 1161 |
gchar *problem = NULL; |
| 1162 |
gchar *check; |
| 1163 |
CockpitPipe *pipe; |
| 1164 |
|
| 1165 |
pipe = g_object_new (COCKPIT_TYPE_PIPE, |
| 1166 |
"problem", "i-have-a-problem", |
| 1167 |
NULL); |
| 1168 |
g_signal_connect (pipe, "close", G_CALLBACK (on_close_get_problem), &problem); |
| 1169 |
|
| 1170 |
g_object_get (pipe, "problem", &check, NULL); |
| 1171 |
g_assert_cmpstr (check, ==, "i-have-a-problem"); |
| 1172 |
g_free (check); |
| 1173 |
check = NULL; |
| 1174 |
|
| 1175 |
g_assert (problem == NULL); |
| 1176 |
while (problem == NULL) |
| 1177 |
g_main_context_iteration (NULL, TRUE); |
| 1178 |
|
| 1179 |
g_assert_cmpstr (problem, ==, "i-have-a-problem"); |
| 1180 |
g_object_get (pipe, "problem", &check, NULL); |
| 1181 |
g_assert_cmpstr (problem, ==, check); |
| 1182 |
|
| 1183 |
g_object_unref (pipe); |
| 1184 |
g_free (problem); |
| 1185 |
g_free (check); |
| 1186 |
} |
| 1187 |
|
| 1188 |
int |
| 1189 |
main (int argc, |
| 1190 |
char *argv[]) |
| 1191 |
{ |
| 1192 |
cockpit_test_init (&argc, &argv); |
| 1193 |
|
| 1194 |
g_test_add_func ("/pipe/buffer/consume-entire", test_consume_entire); |
| 1195 |
g_test_add_func ("/pipe/buffer/consume-partial", test_consume_partial); |
| 1196 |
g_test_add_func ("/pipe/buffer/consume-skip", test_consume_skip); |
| 1197 |
g_test_add_func ("/pipe/buffer/skip", test_buffer_skip); |
| 1198 |
|
| 1199 |
g_test_add_func ("/pipe/properties", test_properties); |
| 1200 |
|
| 1201 |
/* |
| 1202 |
* Fixture data is the GType name of the pipe class |
| 1203 |
* so register these types here. |
| 1204 |
*/ |
| 1205 |
g_type_class_ref (mock_echo_pipe_get_type ()); |
| 1206 |
g_type_class_ref (cockpit_pipe_get_type ()); |
| 1207 |
|
| 1208 |
g_test_add ("/pipe/echo-message", TestCase, NULL, |
| 1209 |
setup_simple, test_echo_and_close, teardown); |
| 1210 |
g_test_add ("/pipe/echo-queue", TestCase, NULL, |
| 1211 |
setup_simple, test_echo_queue, teardown); |
| 1212 |
g_test_add ("/pipe/echo-large", TestCase, &fixture_no_timeout, |
| 1213 |
setup_simple, test_echo_large, teardown); |
| 1214 |
g_test_add ("/pipe/close-problem", TestCase, NULL, |
| 1215 |
setup_simple, test_close_problem, teardown); |
| 1216 |
g_test_add ("/pipe/buffer", TestCase, &fixture_buffer, |
| 1217 |
setup_simple, test_buffer, teardown); |
| 1218 |
g_test_add ("/pipe/skip-zero", TestCase, NULL, |
| 1219 |
setup_simple, test_skip_zero, teardown); |
| 1220 |
g_test_add ("/pipe/pid", TestCase, &fixture_pid, |
| 1221 |
setup_simple, test_pid, teardown); |
| 1222 |
|
| 1223 |
g_test_add ("/pipe/pressure/queue", TestCase, NULL, |
| 1224 |
setup_simple, test_pressure_queue, teardown); |
| 1225 |
g_test_add ("/pipe/pressure/throttle", TestCase, NULL, |
| 1226 |
setup_simple, test_pressure_throttle, teardown); |
| 1227 |
|
| 1228 |
g_test_add ("/pipe/exit-success", TestCase, &fixture_exit_success, |
| 1229 |
setup_simple, test_exit_success, teardown); |
| 1230 |
g_test_add ("/pipe/exit-fail", TestCase, &fixture_exit_fail, |
| 1231 |
setup_simple, test_exit_fail, teardown); |
| 1232 |
g_test_add ("/pipe/exit-signal", TestCase, &fixture_exit_signal, |
| 1233 |
setup_simple, test_exit_signal, teardown); |
| 1234 |
|
| 1235 |
g_test_add_func ("/pipe/read-error", test_read_error); |
| 1236 |
g_test_add_func ("/pipe/write-error", test_write_error); |
| 1237 |
g_test_add_func ("/pipe/read-combined", test_read_combined); |
| 1238 |
|
| 1239 |
g_test_add_func ("/pipe/spawn/and-read", test_spawn_and_read); |
| 1240 |
g_test_add_func ("/pipe/spawn/and-write", test_spawn_and_write); |
| 1241 |
g_test_add_func ("/pipe/spawn/and-fail", test_spawn_and_fail); |
| 1242 |
g_test_add_func ("/pipe/spawn/buffer-stderr", test_spawn_and_buffer_stderr); |
| 1243 |
|
| 1244 |
g_test_add ("/pipe/spawn/close-clean", TestCase, NULL, |
| 1245 |
setup_timeout, test_spawn_close_clean, teardown); |
| 1246 |
g_test_add ("/pipe/spawn/close-terminate", TestCase, NULL, |
| 1247 |
setup_timeout, test_spawn_close_terminate, teardown); |
| 1248 |
|
| 1249 |
g_test_add ("/pipe/connect/and-read", TestConnect, NULL, |
| 1250 |
setup_connect, test_connect_and_read, teardown_connect); |
| 1251 |
g_test_add ("/pipe/connect/and-write", TestConnect, NULL, |
| 1252 |
setup_connect, test_connect_and_write, teardown_connect); |
| 1253 |
|
| 1254 |
g_test_add_func ("/pipe/problem-later", test_problem_later); |
| 1255 |
|
| 1256 |
g_test_add_func ("/pipe/connect/not-found", test_fail_not_found); |
| 1257 |
g_test_add_func ("/pipe/connect/access-denied", test_fail_access_denied); |
| 1258 |
|
| 1259 |
return g_test_run (); |
| 1260 |
} |