| 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 "cockpittransport.h" |
| 9 |
#include "cockpitpipe.h" |
| 10 |
#include "cockpitpipetransport.h" |
| 11 |
|
| 12 |
#include "testlib/cockpittest.h" |
| 13 |
#include "testlib/mock-transport.h" |
| 14 |
|
| 15 |
#include "websocket.h" |
| 16 |
|
| 17 |
#include <glib.h> |
| 18 |
|
| 19 |
#include <string.h> |
| 20 |
|
| 21 |
#include <sys/types.h> |
| 22 |
#include <sys/socket.h> |
| 23 |
#include <sys/uio.h> |
| 24 |
|
| 25 |
#define WAIT_UNTIL(cond) \ |
| 26 |
G_STMT_START \ |
| 27 |
while (!(cond)) g_main_context_iteration (NULL, TRUE); \ |
| 28 |
G_STMT_END |
| 29 |
|
| 30 |
typedef struct { |
| 31 |
CockpitTransport *transport; |
| 32 |
CockpitPipe *pipe; |
| 33 |
} TestCase; |
| 34 |
|
| 35 |
static void |
| 36 |
setup_with_child (TestCase *tc, |
| 37 |
gconstpointer data) |
| 38 |
{ |
| 39 |
gchar *argv[] = { (gchar *)data, NULL }; |
| 40 |
GError *error = NULL; |
| 41 |
GPid pid; |
| 42 |
int in; |
| 43 |
int out; |
| 44 |
|
| 45 |
g_spawn_async_with_pipes (NULL, argv, NULL, G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD, |
| 46 |
NULL, NULL, &pid, &in, &out, NULL, &error); |
| 47 |
g_assert_no_error (error); |
| 48 |
|
| 49 |
tc->pipe = g_object_new (COCKPIT_TYPE_PIPE, |
| 50 |
"name", argv[0], |
| 51 |
"in-fd", out, |
| 52 |
"out-fd", in, |
| 53 |
"pid", pid, |
| 54 |
NULL); |
| 55 |
tc->transport = cockpit_pipe_transport_new (tc->pipe); |
| 56 |
} |
| 57 |
|
| 58 |
static void |
| 59 |
setup_no_child (TestCase *tc, |
| 60 |
gconstpointer data) |
| 61 |
{ |
| 62 |
int sv[2]; |
| 63 |
|
| 64 |
if (socketpair (PF_LOCAL, SOCK_STREAM, 0, sv) < 0) |
| 65 |
g_assert_not_reached (); |
| 66 |
|
| 67 |
tc->pipe = cockpit_pipe_new ("mock", sv[0], sv[1]); |
| 68 |
tc->transport = cockpit_pipe_transport_new (tc->pipe); |
| 69 |
} |
| 70 |
|
| 71 |
static void |
| 72 |
teardown_transport (TestCase *tc, |
| 73 |
gconstpointer data) |
| 74 |
{ |
| 75 |
cockpit_assert_expected (); |
| 76 |
|
| 77 |
g_object_add_weak_pointer (G_OBJECT (tc->transport), |
| 78 |
(gpointer *)&tc->transport); |
| 79 |
g_object_unref (tc->transport); |
| 80 |
|
| 81 |
/* If this asserts, outstanding references to transport */ |
| 82 |
g_assert (tc->transport == NULL); |
| 83 |
|
| 84 |
g_object_add_weak_pointer (G_OBJECT (tc->pipe), |
| 85 |
(gpointer *)&tc->pipe); |
| 86 |
g_object_unref (tc->pipe); |
| 87 |
|
| 88 |
/* If this asserts, outstanding references to transport */ |
| 89 |
g_assert (tc->pipe == NULL); |
| 90 |
} |
| 91 |
|
| 92 |
static gboolean |
| 93 |
on_recv_get_payload (CockpitTransport *transport, |
| 94 |
const gchar *channel, |
| 95 |
GBytes *message, |
| 96 |
gpointer user_data) |
| 97 |
{ |
| 98 |
GBytes **received = user_data; |
| 99 |
if (channel == NULL) |
| 100 |
return FALSE; |
| 101 |
g_assert_cmpstr (channel, ==, "546"); |
| 102 |
g_assert (*received == NULL); |
| 103 |
*received = g_bytes_ref (message); |
| 104 |
return TRUE; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
static gboolean |
| 109 |
on_recv_multiple (CockpitTransport *transport, |
| 110 |
const gchar *channel, |
| 111 |
GBytes *message, |
| 112 |
gpointer user_data) |
| 113 |
{ |
| 114 |
gint *state = user_data; |
| 115 |
GBytes *check = NULL; |
| 116 |
|
| 117 |
if (channel == NULL) |
| 118 |
return FALSE; |
| 119 |
g_assert_cmpstr (channel, ==, "9"); |
| 120 |
|
| 121 |
if (*state == 0) |
| 122 |
check = g_bytes_new_static ("one", 3); |
| 123 |
else if (*state == 1) |
| 124 |
check = g_bytes_new_static ("two", 3); |
| 125 |
else |
| 126 |
g_assert_not_reached (); |
| 127 |
|
| 128 |
(*state)++; |
| 129 |
g_assert (g_bytes_equal (message, check)); |
| 130 |
g_bytes_unref (check); |
| 131 |
return TRUE; |
| 132 |
} |
| 133 |
|
| 134 |
static void |
| 135 |
on_closed_set_flag (CockpitTransport *transport, |
| 136 |
const gchar *problem, |
| 137 |
gpointer user_data) |
| 138 |
{ |
| 139 |
gboolean *flag = user_data; |
| 140 |
g_assert (problem == NULL); |
| 141 |
g_assert (*flag == FALSE); |
| 142 |
*flag = TRUE; |
| 143 |
} |
| 144 |
|
| 145 |
static void |
| 146 |
test_properties (TestCase *tc, |
| 147 |
gconstpointer data) |
| 148 |
{ |
| 149 |
CockpitPipe *pipe; |
| 150 |
|
| 151 |
g_assert (cockpit_pipe_transport_get_pipe (COCKPIT_PIPE_TRANSPORT (tc->transport)) == tc->pipe); |
| 152 |
|
| 153 |
g_object_get (tc->transport, "pipe", &pipe, NULL); |
| 154 |
g_assert (pipe == tc->pipe); |
| 155 |
g_object_unref (pipe); |
| 156 |
} |
| 157 |
|
| 158 |
static void |
| 159 |
test_echo_and_close (TestCase *tc, |
| 160 |
gconstpointer data) |
| 161 |
{ |
| 162 |
|
| 163 |
GBytes *received = NULL; |
| 164 |
GBytes *sent; |
| 165 |
gboolean closed = FALSE; |
| 166 |
|
| 167 |
sent = g_bytes_new_static ("the message", 11); |
| 168 |
g_signal_connect (tc->transport, "recv", G_CALLBACK (on_recv_get_payload), &received); |
| 169 |
cockpit_transport_send (tc->transport, "546", sent); |
| 170 |
|
| 171 |
WAIT_UNTIL (received != NULL); |
| 172 |
|
| 173 |
g_assert (g_bytes_equal (received, sent)); |
| 174 |
g_bytes_unref (sent); |
| 175 |
g_bytes_unref (received); |
| 176 |
received = NULL; |
| 177 |
|
| 178 |
g_signal_connect (tc->transport, "closed", G_CALLBACK (on_closed_set_flag), &closed); |
| 179 |
cockpit_transport_close (tc->transport, NULL); |
| 180 |
|
| 181 |
WAIT_UNTIL (closed == TRUE); |
| 182 |
} |
| 183 |
|
| 184 |
static void |
| 185 |
test_ping_pong (void) |
| 186 |
{ |
| 187 |
JsonObject *reply = NULL; |
| 188 |
MockTransport *mock; |
| 189 |
CockpitTransport *transport; |
| 190 |
GBytes *sent; |
| 191 |
|
| 192 |
mock = mock_transport_new (); |
| 193 |
transport = COCKPIT_TRANSPORT (mock); |
| 194 |
|
| 195 |
/* |
| 196 |
* A "ping" without a channel is not forwarded, but responded to directly |
| 197 |
* by the peer transport. Here we test that CockpitTransport does |
| 198 |
* that response properly. |
| 199 |
*/ |
| 200 |
sent = cockpit_transport_build_control ("command", "ping", "value", "blah", "other", "marmalade", NULL); |
| 201 |
cockpit_transport_emit_recv (transport, NULL, sent); |
| 202 |
g_bytes_unref (sent); |
| 203 |
|
| 204 |
reply = mock_transport_pop_control (mock); |
| 205 |
|
| 206 |
cockpit_assert_json_eq (reply, "{ \"command\": \"pong\", \"value\": \"blah\", \"other\": \"marmalade\" }"); |
| 207 |
g_object_unref (mock); |
| 208 |
} |
| 209 |
|
| 210 |
static void |
| 211 |
test_ping_channel (void) |
| 212 |
{ |
| 213 |
JsonObject *reply = NULL; |
| 214 |
MockTransport *mock; |
| 215 |
CockpitTransport *transport; |
| 216 |
GBytes *sent; |
| 217 |
|
| 218 |
mock = mock_transport_new (); |
| 219 |
transport = COCKPIT_TRANSPORT (mock); |
| 220 |
|
| 221 |
/* This is a "ping" in a channel. But that channel is not open, so its ignored */ |
| 222 |
sent = cockpit_transport_build_control ("command", "ping", "channel", "444", NULL); |
| 223 |
cockpit_transport_emit_recv (transport, NULL, sent); |
| 224 |
g_bytes_unref (sent); |
| 225 |
|
| 226 |
/* This is a single hop, non-forwarded ping, responded to by CockpitTransport */ |
| 227 |
sent = cockpit_transport_build_control ("command", "ping", "value", "blah", "other", "marmalade", NULL); |
| 228 |
cockpit_transport_emit_recv (transport, NULL, sent); |
| 229 |
g_bytes_unref (sent); |
| 230 |
|
| 231 |
reply = mock_transport_pop_control (mock); |
| 232 |
|
| 233 |
/* Note that we don't get a pong for the first one, since it's for a channel, and no such channel exists */ |
| 234 |
cockpit_assert_json_eq (reply, "{ \"command\": \"pong\", \"value\": \"blah\", \"other\": \"marmalade\" }"); |
| 235 |
g_object_unref (mock); |
| 236 |
} |
| 237 |
|
| 238 |
static void |
| 239 |
test_echo_queue (TestCase *tc, |
| 240 |
gconstpointer data) |
| 241 |
{ |
| 242 |
GBytes *sent; |
| 243 |
gint state = 0; |
| 244 |
gboolean closed = FALSE; |
| 245 |
|
| 246 |
g_signal_connect (tc->transport, "recv", G_CALLBACK (on_recv_multiple), &state); |
| 247 |
g_signal_connect (tc->transport, "closed", G_CALLBACK (on_closed_set_flag), &closed); |
| 248 |
|
| 249 |
sent = g_bytes_new_static ("one", 3); |
| 250 |
cockpit_transport_send (tc->transport, "9", sent); |
| 251 |
g_bytes_unref (sent); |
| 252 |
sent = g_bytes_new_static ("two", 3); |
| 253 |
cockpit_transport_send (tc->transport, "9", sent); |
| 254 |
g_bytes_unref (sent); |
| 255 |
|
| 256 |
/* Only closes after above are sent */ |
| 257 |
cockpit_transport_close (tc->transport, NULL); |
| 258 |
|
| 259 |
WAIT_UNTIL (state == 2 && closed == TRUE); |
| 260 |
} |
| 261 |
|
| 262 |
static void |
| 263 |
test_echo_large (TestCase *tc, |
| 264 |
gconstpointer data) |
| 265 |
{ |
| 266 |
GBytes *received = NULL; |
| 267 |
GBytes *sent; |
| 268 |
|
| 269 |
g_signal_connect (tc->transport, "recv", G_CALLBACK (on_recv_get_payload), &received); |
| 270 |
|
| 271 |
/* Medium length */ |
| 272 |
sent = g_bytes_new_take (g_strnfill (1020, '!'), 1020); |
| 273 |
cockpit_transport_send (tc->transport, "546", sent); |
| 274 |
WAIT_UNTIL (received != NULL); |
| 275 |
g_assert (g_bytes_equal (received, sent)); |
| 276 |
g_bytes_unref (sent); |
| 277 |
g_bytes_unref (received); |
| 278 |
received = NULL; |
| 279 |
|
| 280 |
/* Extra large */ |
| 281 |
sent = g_bytes_new_take (g_strnfill (10 * 1000 * 1000, '?'), 10 * 1000 * 1000); |
| 282 |
cockpit_transport_send (tc->transport, "546", sent); |
| 283 |
WAIT_UNTIL (received != NULL); |
| 284 |
g_assert (g_bytes_equal (received, sent)); |
| 285 |
g_bytes_unref (sent); |
| 286 |
g_bytes_unref (received); |
| 287 |
received = NULL; |
| 288 |
|
| 289 |
/* Double check that didn't csrew things up */ |
| 290 |
sent = g_bytes_new_static ("yello", 5); |
| 291 |
cockpit_transport_send (tc->transport, "546", sent); |
| 292 |
WAIT_UNTIL (received != NULL); |
| 293 |
g_assert (g_bytes_equal (received, sent)); |
| 294 |
g_bytes_unref (sent); |
| 295 |
g_bytes_unref (received); |
| 296 |
received = NULL; |
| 297 |
|
| 298 |
} |
| 299 |
|
| 300 |
static void |
| 301 |
on_closed_get_problem (CockpitTransport *transport, |
| 302 |
const gchar *problem, |
| 303 |
gpointer user_data) |
| 304 |
{ |
| 305 |
const gchar **ret = user_data; |
| 306 |
g_assert (problem != NULL); |
| 307 |
g_assert (*ret == NULL); |
| 308 |
*ret = g_strdup (problem); |
| 309 |
} |
| 310 |
|
| 311 |
static void |
| 312 |
test_close_problem (TestCase *tc, |
| 313 |
gconstpointer data) |
| 314 |
{ |
| 315 |
gchar *problem = NULL; |
| 316 |
|
| 317 |
g_signal_connect (tc->transport, "closed", G_CALLBACK (on_closed_get_problem), &problem); |
| 318 |
cockpit_transport_close (tc->transport, "right now"); |
| 319 |
|
| 320 |
WAIT_UNTIL (problem != NULL); |
| 321 |
|
| 322 |
g_assert_cmpstr (problem, ==, "right now"); |
| 323 |
g_free (problem); |
| 324 |
} |
| 325 |
|
| 326 |
static void |
| 327 |
test_terminate_problem (TestCase *tc, |
| 328 |
gconstpointer data) |
| 329 |
{ |
| 330 |
gchar *problem = NULL; |
| 331 |
GPid pid; |
| 332 |
|
| 333 |
g_signal_connect (tc->transport, "closed", G_CALLBACK (on_closed_get_problem), &problem); |
| 334 |
|
| 335 |
g_assert (cockpit_pipe_get_pid (tc->pipe, &pid)); |
| 336 |
g_assert (pid != 0); |
| 337 |
kill (pid, SIGTERM); |
| 338 |
|
| 339 |
WAIT_UNTIL (problem != NULL); |
| 340 |
|
| 341 |
g_assert_cmpstr (problem, ==, "terminated"); |
| 342 |
g_free (problem); |
| 343 |
} |
| 344 |
|
| 345 |
static void |
| 346 |
test_exception_problem (TestCase *tc, |
| 347 |
gconstpointer data) |
| 348 |
{ |
| 349 |
gchar *problem = NULL; |
| 350 |
|
| 351 |
g_signal_connect (tc->transport, "closed", G_CALLBACK (on_closed_get_problem), &problem); |
| 352 |
|
| 353 |
WAIT_UNTIL (problem != NULL); |
| 354 |
g_assert_cmpstr (problem, ==, "internal-error"); |
| 355 |
g_free (problem); |
| 356 |
} |
| 357 |
|
| 358 |
static void |
| 359 |
test_nocockpit_problem (TestCase *tc, |
| 360 |
gconstpointer data) |
| 361 |
{ |
| 362 |
gchar *problem = NULL; |
| 363 |
|
| 364 |
g_signal_connect (tc->transport, "closed", G_CALLBACK (on_closed_get_problem), &problem); |
| 365 |
|
| 366 |
WAIT_UNTIL (problem != NULL); |
| 367 |
g_assert_cmpstr (problem, ==, "no-cockpit"); |
| 368 |
g_free (problem); |
| 369 |
} |
| 370 |
|
| 371 |
static void |
| 372 |
test_read_error (void) |
| 373 |
{ |
| 374 |
CockpitTransport *transport; |
| 375 |
gchar *problem = NULL; |
| 376 |
gint fds[2]; |
| 377 |
|
| 378 |
/* Assuming FD 1000 is not taken */ |
| 379 |
g_assert (write (1000, "1", 1) < 0); |
| 380 |
|
| 381 |
g_assert_cmpint (pipe (fds), ==, 0); |
| 382 |
|
| 383 |
|
| 384 |
g_test_expect_message ("cockpit-protocol", G_LOG_LEVEL_WARNING, "*Bad file descriptor"); |
| 385 |
g_test_expect_message ("cockpit-protocol", G_LOG_LEVEL_WARNING, "*Bad file descriptor"); |
| 386 |
|
| 387 |
/* Pass in a bad read descriptor */ |
| 388 |
transport = cockpit_pipe_transport_new_fds ("test", 1000, fds[0]); |
| 389 |
|
| 390 |
g_signal_connect (transport, "closed", G_CALLBACK (on_closed_get_problem), &problem); |
| 391 |
|
| 392 |
WAIT_UNTIL (problem != NULL); |
| 393 |
g_assert_cmpstr (problem, ==, "internal-error"); |
| 394 |
g_free (problem); |
| 395 |
|
| 396 |
cockpit_assert_expected (); |
| 397 |
|
| 398 |
g_object_unref (transport); |
| 399 |
close (fds[1]); |
| 400 |
} |
| 401 |
|
| 402 |
static void |
| 403 |
test_write_error (void) |
| 404 |
{ |
| 405 |
CockpitTransport *transport; |
| 406 |
gchar *problem = NULL; |
| 407 |
GBytes *sent; |
| 408 |
int fds[2]; |
| 409 |
|
| 410 |
/* Just used so we have a valid fd */ |
| 411 |
if (pipe(fds) < 0) |
| 412 |
g_assert_not_reached (); |
| 413 |
|
| 414 |
/* Assuming FD 1000 is not taken */ |
| 415 |
g_assert (write (1000, "1", 1) < 0); |
| 416 |
|
| 417 |
g_test_expect_message ("cockpit-protocol", G_LOG_LEVEL_WARNING, "*Bad file descriptor"); |
| 418 |
g_test_expect_message ("cockpit-protocol", G_LOG_LEVEL_WARNING, "*Bad file descriptor"); |
| 419 |
|
| 420 |
/* Pass in a bad write descriptor */ |
| 421 |
transport = cockpit_pipe_transport_new_fds ("test", fds[0], 1000); |
| 422 |
|
| 423 |
sent = g_bytes_new ("test", 4); |
| 424 |
cockpit_transport_send (transport, "3333", sent); |
| 425 |
g_bytes_unref (sent); |
| 426 |
|
| 427 |
g_signal_connect (transport, "closed", G_CALLBACK (on_closed_get_problem), &problem); |
| 428 |
|
| 429 |
WAIT_UNTIL (problem != NULL); |
| 430 |
g_assert_cmpstr (problem, ==, "internal-error"); |
| 431 |
g_free (problem); |
| 432 |
|
| 433 |
close (fds[0]); |
| 434 |
close (fds[1]); |
| 435 |
|
| 436 |
g_object_unref (transport); |
| 437 |
|
| 438 |
cockpit_assert_expected (); |
| 439 |
} |
| 440 |
|
| 441 |
static void |
| 442 |
test_read_combined (void) |
| 443 |
{ |
| 444 |
CockpitTransport *transport; |
| 445 |
struct iovec iov[4]; |
| 446 |
gint state = 0; |
| 447 |
gint fds[2]; |
| 448 |
gint out; |
| 449 |
|
| 450 |
if (pipe(fds) < 0) |
| 451 |
g_assert_not_reached (); |
| 452 |
|
| 453 |
out = dup (2); |
| 454 |
g_assert (out >= 0); |
| 455 |
|
| 456 |
/* Pass in a read end of the pipe */ |
| 457 |
transport = cockpit_pipe_transport_new_fds ("test", fds[0], out); |
| 458 |
g_signal_connect (transport, "recv", G_CALLBACK (on_recv_multiple), &state); |
| 459 |
|
| 460 |
/* Write two messages to the pipe at once */ |
| 461 |
iov[0].iov_base = "5\n"; |
| 462 |
iov[0].iov_len = 2; |
| 463 |
iov[1].iov_base = "9\none"; |
| 464 |
iov[1].iov_len = 5; |
| 465 |
iov[2].iov_base = "5\n"; |
| 466 |
iov[2].iov_len = 2; |
| 467 |
iov[3].iov_base = "9\ntwo"; |
| 468 |
iov[3].iov_len = 5; |
| 469 |
g_assert_cmpint (writev (fds[1], iov, 4), ==, 14); |
| 470 |
|
| 471 |
WAIT_UNTIL (state == 2); |
| 472 |
|
| 473 |
close (fds[1]); |
| 474 |
g_object_unref (transport); |
| 475 |
} |
| 476 |
|
| 477 |
static void |
| 478 |
test_read_truncated (void) |
| 479 |
{ |
| 480 |
CockpitTransport *transport; |
| 481 |
gchar *problem = NULL; |
| 482 |
gint fds[2]; |
| 483 |
gint out; |
| 484 |
|
| 485 |
if (pipe(fds) < 0) |
| 486 |
g_assert_not_reached (); |
| 487 |
|
| 488 |
out = dup (2); |
| 489 |
g_assert (out >= 0); |
| 490 |
|
| 491 |
/* Pass in a read end of the pipe */ |
| 492 |
transport = cockpit_pipe_transport_new_fds ("test", fds[0], out); |
| 493 |
g_signal_connect (transport, "closed", G_CALLBACK (on_closed_get_problem), &problem); |
| 494 |
|
| 495 |
/* Not a full 4 byte length (ie: truncated) */ |
| 496 |
g_assert_cmpint (write (fds[1], "5", 1), ==, 1); |
| 497 |
g_assert_cmpint (close (fds[1]), ==, 0); |
| 498 |
|
| 499 |
WAIT_UNTIL (problem != NULL); |
| 500 |
|
| 501 |
g_assert_cmpstr (problem, ==, "disconnected"); |
| 502 |
g_free (problem); |
| 503 |
|
| 504 |
g_object_unref (transport); |
| 505 |
|
| 506 |
cockpit_assert_expected (); |
| 507 |
} |
| 508 |
|
| 509 |
static void |
| 510 |
test_incorrect_protocol (void) |
| 511 |
{ |
| 512 |
CockpitTransport *transport; |
| 513 |
gchar *problem = NULL; |
| 514 |
gint fds[2]; |
| 515 |
gint out; |
| 516 |
|
| 517 |
if (pipe(fds) < 0) |
| 518 |
g_assert_not_reached (); |
| 519 |
|
| 520 |
out = dup (2); |
| 521 |
g_assert (out >= 0); |
| 522 |
|
| 523 |
g_test_expect_message ("cockpit-protocol", G_LOG_LEVEL_WARNING, "*received invalid length prefix"); |
| 524 |
|
| 525 |
/* Pass in a read end of the pipe */ |
| 526 |
transport = cockpit_pipe_transport_new_fds ("test", fds[0], out); |
| 527 |
g_signal_connect (transport, "closed", G_CALLBACK (on_closed_get_problem), &problem); |
| 528 |
|
| 529 |
/* Not a full 4 byte length (ie: truncated) */ |
| 530 |
g_assert_cmpint (write (fds[1], "X", 1), ==, 1); |
| 531 |
g_assert_cmpint (close (fds[1]), ==, 0); |
| 532 |
|
| 533 |
WAIT_UNTIL (problem != NULL); |
| 534 |
|
| 535 |
g_assert_cmpstr (problem, ==, "protocol-error"); |
| 536 |
g_free (problem); |
| 537 |
|
| 538 |
g_object_unref (transport); |
| 539 |
|
| 540 |
cockpit_assert_expected (); |
| 541 |
} |
| 542 |
|
| 543 |
static void |
| 544 |
test_parse_frame (void) |
| 545 |
{ |
| 546 |
GBytes *message; |
| 547 |
GBytes *payload; |
| 548 |
gchar *channel; |
| 549 |
|
| 550 |
message = g_bytes_new_static ("134\ntest", 8); |
| 551 |
|
| 552 |
payload = cockpit_transport_parse_frame (message, &channel); |
| 553 |
g_assert (payload != NULL); |
| 554 |
g_assert_cmpstr (g_bytes_get_data (payload, NULL), ==, "test"); |
| 555 |
g_assert_cmpstr (channel, ==, "134"); |
| 556 |
|
| 557 |
g_bytes_unref (payload); |
| 558 |
g_bytes_unref (message); |
| 559 |
g_free (channel); |
| 560 |
} |
| 561 |
|
| 562 |
static void |
| 563 |
test_parse_frame_bad (void) |
| 564 |
{ |
| 565 |
gchar *channel = NULL; |
| 566 |
GBytes *message; |
| 567 |
GBytes *payload; |
| 568 |
|
| 569 |
g_test_expect_message ("cockpit-protocol", G_LOG_LEVEL_MESSAGE, "*invalid channel prefix"); |
| 570 |
|
| 571 |
message = g_bytes_new_static ("b\x00y\ntest", 8); |
| 572 |
payload = cockpit_transport_parse_frame (message, &channel); |
| 573 |
g_assert (payload == NULL); |
| 574 |
g_bytes_unref (message); |
| 575 |
g_free (channel); |
| 576 |
|
| 577 |
cockpit_assert_expected (); |
| 578 |
|
| 579 |
g_test_expect_message ("cockpit-protocol", G_LOG_LEVEL_MESSAGE, "*invalid message without channel prefix"); |
| 580 |
|
| 581 |
channel = NULL; |
| 582 |
message = g_bytes_new_static ("test", 4); |
| 583 |
payload = cockpit_transport_parse_frame (message, &channel); |
| 584 |
g_assert (payload == NULL); |
| 585 |
g_bytes_unref (message); |
| 586 |
g_free (channel); |
| 587 |
|
| 588 |
cockpit_assert_expected (); |
| 589 |
} |
| 590 |
|
| 591 |
static void |
| 592 |
test_parse_command (void) |
| 593 |
{ |
| 594 |
const gchar *input = "{ \"command\": \"test\", \"channel\": \"66\", \"opt\": \"one\" }"; |
| 595 |
GBytes *message; |
| 596 |
const gchar *channel; |
| 597 |
const gchar *command; |
| 598 |
JsonObject *options; |
| 599 |
gboolean ret; |
| 600 |
|
| 601 |
message = g_bytes_new_static (input, strlen (input)); |
| 602 |
|
| 603 |
ret = cockpit_transport_parse_command (message, &command, &channel, &options); |
| 604 |
g_bytes_unref (message); |
| 605 |
|
| 606 |
g_assert (ret == TRUE); |
| 607 |
g_assert_cmpstr (command, ==, "test"); |
| 608 |
g_assert_cmpstr (channel, ==, "66"); |
| 609 |
g_assert_cmpstr (json_object_get_string_member (options, "opt"), ==, "one"); |
| 610 |
|
| 611 |
json_object_unref (options); |
| 612 |
} |
| 613 |
|
| 614 |
static void |
| 615 |
test_parse_command_no_channel (void) |
| 616 |
{ |
| 617 |
const gchar *input = "{ \"command\": \"test\", \"opt\": \"one\" }"; |
| 618 |
GBytes *message; |
| 619 |
const gchar *channel; |
| 620 |
const gchar *command; |
| 621 |
JsonObject *options; |
| 622 |
gboolean ret; |
| 623 |
|
| 624 |
message = g_bytes_new_static (input, strlen (input)); |
| 625 |
|
| 626 |
ret = cockpit_transport_parse_command (message, &command, &channel, &options); |
| 627 |
g_bytes_unref (message); |
| 628 |
|
| 629 |
g_assert (ret == TRUE); |
| 630 |
g_assert_cmpstr (command, ==, "test"); |
| 631 |
g_assert_cmpstr (channel, ==, NULL); |
| 632 |
g_assert_cmpstr (json_object_get_string_member (options, "opt"), ==, "one"); |
| 633 |
|
| 634 |
json_object_unref (options); |
| 635 |
} |
| 636 |
|
| 637 |
static void |
| 638 |
test_parse_command_nulls (void) |
| 639 |
{ |
| 640 |
const gchar *input = "{ \"command\": \"test\", \"opt\": \"one\" }"; |
| 641 |
GBytes *message; |
| 642 |
JsonObject *options; |
| 643 |
gboolean ret; |
| 644 |
|
| 645 |
message = g_bytes_new_static (input, strlen (input)); |
| 646 |
|
| 647 |
ret = cockpit_transport_parse_command (message, NULL, NULL, &options); |
| 648 |
g_bytes_unref (message); |
| 649 |
|
| 650 |
g_assert (ret == TRUE); |
| 651 |
g_assert_cmpstr (json_object_get_string_member (options, "opt"), ==, "one"); |
| 652 |
|
| 653 |
json_object_unref (options); |
| 654 |
} |
| 655 |
|
| 656 |
struct { |
| 657 |
const char *name; |
| 658 |
const char *json; |
| 659 |
} bad_command_payloads[] = { |
| 660 |
{ "no-command", "{ \"no-command\": \"test\" }", }, |
| 661 |
{ "empty-command", "{ \"command\": \"\" }", }, |
| 662 |
{ "invalid-json", "{ xxxxxxxxxxxxxxxxxxxxx", }, |
| 663 |
{ "not-an-object", "55", }, |
| 664 |
{ "number-channel", "{ \"command\": \"test\", \"channel\": 0 }", }, |
| 665 |
{ "empty-channel", "{ \"command\": \"test\", \"channel\": \"\" }", }, |
| 666 |
{ "newline-channel", "{ \"command\": \"test\", \"channel\": \"blah\nline\" }", }, |
| 667 |
}; |
| 668 |
|
| 669 |
static void |
| 670 |
test_parse_command_bad (gconstpointer input) |
| 671 |
{ |
| 672 |
GBytes *message; |
| 673 |
const gchar *channel; |
| 674 |
const gchar *command; |
| 675 |
JsonObject *options; |
| 676 |
gboolean ret; |
| 677 |
|
| 678 |
g_test_expect_message ("cockpit-protocol", G_LOG_LEVEL_WARNING, "*"); |
| 679 |
|
| 680 |
message = g_bytes_new_static (input, strlen (input)); |
| 681 |
|
| 682 |
ret = cockpit_transport_parse_command (message, &command, &channel, &options); |
| 683 |
g_bytes_unref (message); |
| 684 |
|
| 685 |
g_assert (ret == FALSE); |
| 686 |
|
| 687 |
cockpit_assert_expected (); |
| 688 |
} |
| 689 |
|
| 690 |
int |
| 691 |
main (int argc, |
| 692 |
char *argv[]) |
| 693 |
{ |
| 694 |
gint i; |
| 695 |
|
| 696 |
cockpit_test_init (&argc, &argv); |
| 697 |
|
| 698 |
g_test_add_func ("/transport/parse-frame/ok", test_parse_frame); |
| 699 |
g_test_add_func ("/transport/parse-frame/bad", test_parse_frame_bad); |
| 700 |
|
| 701 |
g_test_add_func ("/transport/parse-command/normal", test_parse_command); |
| 702 |
g_test_add_func ("/transport/parse-command/no-channel", test_parse_command_no_channel); |
| 703 |
g_test_add_func ("/transport/parse-command/nulls", test_parse_command_nulls); |
| 704 |
|
| 705 |
for (i = 0; i < G_N_ELEMENTS (bad_command_payloads); i++) |
| 706 |
{ |
| 707 |
gchar *name = g_strdup_printf ("/transport/parse-command/%s", bad_command_payloads[i].name); |
| 708 |
g_test_add_data_func (name, bad_command_payloads[i].json, test_parse_command_bad); |
| 709 |
g_free (name); |
| 710 |
} |
| 711 |
|
| 712 |
g_test_add ("/transport/properties", TestCase, NULL, |
| 713 |
setup_no_child, test_properties, teardown_transport); |
| 714 |
|
| 715 |
g_test_add ("/transport/echo-message/child", TestCase, |
| 716 |
BUILDDIR "/mock-echo", setup_with_child, |
| 717 |
test_echo_and_close, teardown_transport); |
| 718 |
g_test_add ("/transport/echo-message/no-child", TestCase, |
| 719 |
NULL, setup_no_child, |
| 720 |
test_echo_and_close, teardown_transport); |
| 721 |
g_test_add ("/transport/echo-queue/child", TestCase, |
| 722 |
BUILDDIR "/mock-echo", setup_with_child, |
| 723 |
test_echo_queue, teardown_transport); |
| 724 |
g_test_add ("/transport/echo-queue/no-child", TestCase, |
| 725 |
NULL, setup_no_child, |
| 726 |
test_echo_queue, teardown_transport); |
| 727 |
g_test_add ("/transport/echo-large/child", TestCase, |
| 728 |
"cat", setup_with_child, |
| 729 |
test_echo_large, teardown_transport); |
| 730 |
g_test_add ("/transport/echo-large/no-child", TestCase, |
| 731 |
NULL, setup_no_child, |
| 732 |
test_echo_large, teardown_transport); |
| 733 |
|
| 734 |
g_test_add ("/transport/close-problem/child", TestCase, |
| 735 |
BUILDDIR "/mock-echo", setup_with_child, |
| 736 |
test_close_problem, teardown_transport); |
| 737 |
g_test_add ("/transport/close-problem/no-child", TestCase, |
| 738 |
NULL, setup_no_child, |
| 739 |
test_close_problem, teardown_transport); |
| 740 |
|
| 741 |
g_test_add ("/transport/terminate-problem", TestCase, |
| 742 |
BUILDDIR "/mock-echo", setup_with_child, |
| 743 |
test_terminate_problem, teardown_transport); |
| 744 |
|
| 745 |
g_test_add ("/transport/exception-problem", TestCase, |
| 746 |
SRCDIR "/src/ws/mock-pipes/someprogram", setup_with_child, |
| 747 |
test_exception_problem, teardown_transport); |
| 748 |
|
| 749 |
g_test_add ("/transport/nocockpit-problem", TestCase, |
| 750 |
SRCDIR "/src/ws/mock-pipes/cockpit-session", setup_with_child, |
| 751 |
test_nocockpit_problem, teardown_transport); |
| 752 |
|
| 753 |
g_test_add_func ("/transport/ping/pong", test_ping_pong); |
| 754 |
g_test_add_func ("/transport/ping/channel", test_ping_channel); |
| 755 |
|
| 756 |
g_test_add_func ("/transport/read-error", test_read_error); |
| 757 |
g_test_add_func ("/transport/write-error", test_write_error); |
| 758 |
g_test_add_func ("/transport/read-combined", test_read_combined); |
| 759 |
g_test_add_func ("/transport/read-truncated", test_read_truncated); |
| 760 |
g_test_add_func ("/transport/read-incorrect", test_incorrect_protocol); |
| 761 |
|
| 762 |
return g_test_run (); |
| 763 |
} |