| 1 |
/* |
| 2 |
* Copyright (C) 2013 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "config.h" |
| 7 |
|
| 8 |
#include "cockpitchannel.h" |
| 9 |
#include "cockpitjson.h" |
| 10 |
#include "cockpitpipe.h" |
| 11 |
#include "cockpitpipetransport.h" |
| 12 |
|
| 13 |
#include "testlib/cockpittest.h" |
| 14 |
#include "testlib/mock-pressure.h" |
| 15 |
#include "testlib/mock-transport.h" |
| 16 |
|
| 17 |
#include <json-glib/json-glib.h> |
| 18 |
|
| 19 |
#include <gio/gio.h> |
| 20 |
|
| 21 |
#include <sys/types.h> |
| 22 |
#include <sys/socket.h> |
| 23 |
#include <string.h> |
| 24 |
|
| 25 |
/* ---------------------------------------------------------------------------- |
| 26 |
* Mock |
| 27 |
*/ |
| 28 |
|
| 29 |
static GType mock_echo_channel_get_type (void) G_GNUC_CONST; |
| 30 |
|
| 31 |
typedef struct { |
| 32 |
CockpitChannel parent; |
| 33 |
gboolean close_called; |
| 34 |
} MockEchoChannel; |
| 35 |
|
| 36 |
typedef CockpitChannelClass MockEchoChannelClass; |
| 37 |
|
| 38 |
G_DEFINE_TYPE (MockEchoChannel, mock_echo_channel, COCKPIT_TYPE_CHANNEL); |
| 39 |
|
| 40 |
static void |
| 41 |
mock_echo_channel_recv (CockpitChannel *channel, |
| 42 |
GBytes *message) |
| 43 |
{ |
| 44 |
cockpit_channel_send (channel, message, FALSE); |
| 45 |
} |
| 46 |
|
| 47 |
static gboolean |
| 48 |
mock_echo_channel_control (CockpitChannel *channel, |
| 49 |
const gchar *command, |
| 50 |
JsonObject *options) |
| 51 |
{ |
| 52 |
cockpit_channel_control (channel, command, options); |
| 53 |
return TRUE; |
| 54 |
} |
| 55 |
|
| 56 |
static void |
| 57 |
mock_echo_channel_close (CockpitChannel *channel, |
| 58 |
const gchar *problem) |
| 59 |
{ |
| 60 |
MockEchoChannel *self = (MockEchoChannel *)channel; |
| 61 |
self->close_called = TRUE; |
| 62 |
COCKPIT_CHANNEL_CLASS (mock_echo_channel_parent_class)->close (channel, problem); |
| 63 |
} |
| 64 |
|
| 65 |
static void |
| 66 |
mock_echo_channel_init (MockEchoChannel *self) |
| 67 |
{ |
| 68 |
|
| 69 |
} |
| 70 |
|
| 71 |
static void |
| 72 |
mock_echo_channel_class_init (MockEchoChannelClass *klass) |
| 73 |
{ |
| 74 |
CockpitChannelClass *channel_class = COCKPIT_CHANNEL_CLASS (klass); |
| 75 |
channel_class->recv = mock_echo_channel_recv; |
| 76 |
channel_class->control = mock_echo_channel_control; |
| 77 |
channel_class->close = mock_echo_channel_close; |
| 78 |
} |
| 79 |
|
| 80 |
static CockpitChannel * |
| 81 |
mock_echo_channel_open (CockpitTransport *transport, |
| 82 |
const gchar *channel_id) |
| 83 |
{ |
| 84 |
CockpitChannel *channel; |
| 85 |
JsonObject *options; |
| 86 |
|
| 87 |
g_assert (channel_id != NULL); |
| 88 |
|
| 89 |
options = json_object_new (); |
| 90 |
channel = g_object_new (mock_echo_channel_get_type (), |
| 91 |
"transport", transport, |
| 92 |
"id", channel_id, |
| 93 |
"options", options, |
| 94 |
NULL); |
| 95 |
|
| 96 |
json_object_unref (options); |
| 97 |
return channel; |
| 98 |
} |
| 99 |
|
| 100 |
static GType mock_null_channel_get_type (void) G_GNUC_CONST; |
| 101 |
|
| 102 |
typedef CockpitChannel MockNullChannel; |
| 103 |
typedef CockpitChannelClass MockNullChannelClass; |
| 104 |
|
| 105 |
G_DEFINE_TYPE (MockNullChannel, mock_null_channel, COCKPIT_TYPE_CHANNEL); |
| 106 |
|
| 107 |
static void |
| 108 |
mock_null_channel_init (MockNullChannel *self) |
| 109 |
{ |
| 110 |
|
| 111 |
} |
| 112 |
|
| 113 |
static void |
| 114 |
mock_null_channel_class_init (MockNullChannelClass *klass) |
| 115 |
{ |
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
/* ---------------------------------------------------------------------------- |
| 120 |
* Testing |
| 121 |
*/ |
| 122 |
|
| 123 |
typedef struct { |
| 124 |
MockTransport *transport; |
| 125 |
CockpitChannel *channel; |
| 126 |
} TestCase; |
| 127 |
|
| 128 |
static void |
| 129 |
setup (TestCase *tc, |
| 130 |
gconstpointer unused) |
| 131 |
{ |
| 132 |
tc->transport = g_object_new (mock_transport_get_type (), NULL); |
| 133 |
tc->channel = mock_echo_channel_open (COCKPIT_TRANSPORT (tc->transport), "554"); |
| 134 |
while (g_main_context_iteration (NULL, FALSE)); |
| 135 |
} |
| 136 |
|
| 137 |
static void |
| 138 |
teardown (TestCase *tc, |
| 139 |
gconstpointer unused) |
| 140 |
{ |
| 141 |
g_object_add_weak_pointer (G_OBJECT (tc->channel), (gpointer *)&tc->channel); |
| 142 |
g_object_add_weak_pointer (G_OBJECT (tc->transport), (gpointer *)&tc->transport); |
| 143 |
g_object_unref (tc->channel); |
| 144 |
g_object_unref (tc->transport); |
| 145 |
g_assert (tc->channel == NULL); |
| 146 |
g_assert (tc->transport == NULL); |
| 147 |
} |
| 148 |
|
| 149 |
static void |
| 150 |
test_recv_and_send (TestCase *tc, |
| 151 |
gconstpointer unused) |
| 152 |
{ |
| 153 |
GBytes *sent; |
| 154 |
GBytes *payload; |
| 155 |
|
| 156 |
/* Ready to go */ |
| 157 |
cockpit_channel_ready (tc->channel, NULL); |
| 158 |
|
| 159 |
payload = g_bytes_new ("Yeehaw!", 7); |
| 160 |
cockpit_transport_emit_recv (COCKPIT_TRANSPORT (tc->transport), "554", payload); |
| 161 |
|
| 162 |
sent = mock_transport_pop_channel (tc->transport, "554"); |
| 163 |
g_assert (sent != NULL); |
| 164 |
|
| 165 |
g_assert (g_bytes_equal (payload, sent)); |
| 166 |
g_bytes_unref (payload); |
| 167 |
} |
| 168 |
|
| 169 |
static void |
| 170 |
test_recv_and_queue (TestCase *tc, |
| 171 |
gconstpointer unused) |
| 172 |
{ |
| 173 |
GBytes *payload; |
| 174 |
GBytes *control; |
| 175 |
const gchar *data; |
| 176 |
GBytes *sent; |
| 177 |
JsonObject *object; |
| 178 |
|
| 179 |
payload = g_bytes_new ("Yeehaw!", 7); |
| 180 |
cockpit_transport_emit_recv (COCKPIT_TRANSPORT (tc->transport), "554", payload); |
| 181 |
|
| 182 |
data = "{ \"command\": \"blah\", \"channel\": \"554\" }"; |
| 183 |
control = g_bytes_new_static (data, strlen (data)); |
| 184 |
cockpit_transport_emit_recv (COCKPIT_TRANSPORT (tc->transport), NULL, control); |
| 185 |
|
| 186 |
/* Shouldn't have received it yet */ |
| 187 |
g_assert_cmpuint (mock_transport_count_sent (tc->transport), ==, 0); |
| 188 |
|
| 189 |
/* Ready to go */ |
| 190 |
cockpit_channel_ready (tc->channel, NULL); |
| 191 |
|
| 192 |
/* The control message */ |
| 193 |
object = mock_transport_pop_control (tc->transport); |
| 194 |
g_assert (object != NULL); |
| 195 |
cockpit_assert_json_eq (object, data); |
| 196 |
g_bytes_unref (control); |
| 197 |
|
| 198 |
sent = mock_transport_pop_channel (tc->transport, "554"); |
| 199 |
g_assert (sent != NULL); |
| 200 |
g_assert (g_bytes_equal (payload, sent)); |
| 201 |
g_bytes_unref (payload); |
| 202 |
} |
| 203 |
|
| 204 |
static void |
| 205 |
test_ready_message (TestCase *tc, |
| 206 |
gconstpointer unused) |
| 207 |
{ |
| 208 |
JsonObject *message; |
| 209 |
JsonObject *sent; |
| 210 |
|
| 211 |
message = json_object_new (); |
| 212 |
json_object_set_string_member (message, "mop", "bucket"); |
| 213 |
|
| 214 |
/* Ready to go */ |
| 215 |
cockpit_channel_ready (tc->channel, message); |
| 216 |
json_object_unref (message); |
| 217 |
|
| 218 |
sent = mock_transport_pop_control (tc->transport); |
| 219 |
cockpit_assert_json_eq (sent, |
| 220 |
"{ \"command\": \"ready\", \"channel\": \"554\", \"mop\": \"bucket\" }"); |
| 221 |
} |
| 222 |
|
| 223 |
static void |
| 224 |
test_close_immediately (TestCase *tc, |
| 225 |
gconstpointer unused) |
| 226 |
{ |
| 227 |
GBytes *payload; |
| 228 |
JsonObject *sent; |
| 229 |
|
| 230 |
payload = g_bytes_new ("Yeehaw!", 7); |
| 231 |
cockpit_transport_emit_recv (COCKPIT_TRANSPORT (tc->transport), "554", payload); |
| 232 |
g_bytes_unref (payload); |
| 233 |
|
| 234 |
/* Shouldn't have received it yet */ |
| 235 |
g_assert_cmpuint (mock_transport_count_sent (tc->transport), ==, 0); |
| 236 |
|
| 237 |
/* Now close without getting anything */ |
| 238 |
cockpit_channel_close (tc->channel, "bad-boy"); |
| 239 |
|
| 240 |
g_assert (mock_transport_pop_channel (tc->transport, "554") == NULL); |
| 241 |
g_assert_cmpuint (mock_transport_count_sent (tc->transport), ==, 1); |
| 242 |
|
| 243 |
sent = mock_transport_pop_control (tc->transport); |
| 244 |
g_assert (sent != NULL); |
| 245 |
|
| 246 |
cockpit_assert_json_eq (sent, |
| 247 |
"{ \"command\": \"close\", \"channel\": \"554\", \"problem\": \"bad-boy\"}"); |
| 248 |
} |
| 249 |
|
| 250 |
static void |
| 251 |
test_close_option (TestCase *tc, |
| 252 |
gconstpointer unused) |
| 253 |
{ |
| 254 |
JsonObject *sent; |
| 255 |
JsonObject *options; |
| 256 |
|
| 257 |
options = cockpit_channel_close_options (tc->channel); |
| 258 |
json_object_set_string_member (options, "option", "four"); |
| 259 |
cockpit_channel_close (tc->channel, "bad-boy"); |
| 260 |
|
| 261 |
g_assert_cmpuint (mock_transport_count_sent (tc->transport), ==, 1); |
| 262 |
|
| 263 |
sent = mock_transport_pop_control (tc->transport); |
| 264 |
g_assert (sent != NULL); |
| 265 |
|
| 266 |
cockpit_assert_json_eq (sent, |
| 267 |
"{ \"command\": \"close\", \"channel\": \"554\", \"problem\": \"bad-boy\", \"option\": \"four\" }"); |
| 268 |
} |
| 269 |
|
| 270 |
static void |
| 271 |
test_close_json_option (TestCase *tc, |
| 272 |
gconstpointer unused) |
| 273 |
{ |
| 274 |
JsonObject *sent; |
| 275 |
JsonObject *obj; |
| 276 |
JsonObject *options; |
| 277 |
|
| 278 |
obj = json_object_new (); |
| 279 |
json_object_set_string_member (obj, "test", "value"); |
| 280 |
options = cockpit_channel_close_options (tc->channel); |
| 281 |
json_object_set_object_member (options, "option", obj); |
| 282 |
|
| 283 |
cockpit_channel_close (tc->channel, "bad-boy"); |
| 284 |
|
| 285 |
g_assert_cmpuint (mock_transport_count_sent (tc->transport), ==, 1); |
| 286 |
|
| 287 |
sent = mock_transport_pop_control (tc->transport); |
| 288 |
g_assert (sent != NULL); |
| 289 |
|
| 290 |
cockpit_assert_json_eq (sent, |
| 291 |
"{ \"command\": \"close\", \"channel\": \"554\", \"problem\": \"bad-boy\", \"option\": { \"test\": \"value\" } }"); |
| 292 |
} |
| 293 |
|
| 294 |
static void |
| 295 |
on_closed_get_problem (CockpitChannel *channel, |
| 296 |
const gchar *problem, |
| 297 |
gpointer user_data) |
| 298 |
{ |
| 299 |
gchar **retval = user_data; |
| 300 |
g_assert (*retval == NULL); |
| 301 |
*retval = g_strdup (problem); |
| 302 |
} |
| 303 |
|
| 304 |
static void |
| 305 |
test_close_transport (TestCase *tc, |
| 306 |
gconstpointer unused) |
| 307 |
{ |
| 308 |
MockEchoChannel *chan; |
| 309 |
JsonObject *control; |
| 310 |
GBytes *sent; |
| 311 |
gchar *problem = NULL; |
| 312 |
|
| 313 |
chan = (MockEchoChannel *)tc->channel; |
| 314 |
cockpit_channel_ready (tc->channel, NULL); |
| 315 |
|
| 316 |
sent = g_bytes_new ("Yeehaw!", 7); |
| 317 |
cockpit_transport_emit_recv (COCKPIT_TRANSPORT (tc->transport), "554", sent); |
| 318 |
g_bytes_unref (sent); |
| 319 |
|
| 320 |
g_assert (chan->close_called == FALSE); |
| 321 |
|
| 322 |
g_signal_connect (tc->channel, "closed", G_CALLBACK (on_closed_get_problem), &problem); |
| 323 |
cockpit_transport_close (COCKPIT_TRANSPORT (tc->transport), "boooo"); |
| 324 |
|
| 325 |
g_assert (chan->close_called == TRUE); |
| 326 |
|
| 327 |
g_assert_cmpstr (problem, ==, "boooo"); |
| 328 |
control = mock_transport_pop_control (tc->transport); |
| 329 |
g_assert_cmpstr (json_object_get_string_member (control, "command"), ==, "ready"); |
| 330 |
g_assert (mock_transport_pop_control (tc->transport) == NULL); |
| 331 |
|
| 332 |
g_free (problem); |
| 333 |
} |
| 334 |
|
| 335 |
static void |
| 336 |
test_get_option (void) |
| 337 |
{ |
| 338 |
JsonObject *options; |
| 339 |
CockpitTransport *transport; |
| 340 |
CockpitChannel *channel; |
| 341 |
|
| 342 |
options = json_object_new (); |
| 343 |
json_object_set_string_member (options, "scruffy", "janitor"); |
| 344 |
json_object_set_int_member (options, "age", 5); |
| 345 |
|
| 346 |
transport = g_object_new (mock_transport_get_type (), NULL); |
| 347 |
channel = g_object_new (mock_echo_channel_get_type (), |
| 348 |
"transport", transport, |
| 349 |
"id", "55", |
| 350 |
"options", options, |
| 351 |
NULL); |
| 352 |
g_object_unref (transport); |
| 353 |
json_object_unref (options); |
| 354 |
|
| 355 |
options = cockpit_channel_get_options (channel); |
| 356 |
g_assert_cmpstr (json_object_get_string_member (options, "scruffy"), ==, "janitor"); |
| 357 |
g_assert_cmpint (json_object_get_int_member (options, "age"), ==, 5); |
| 358 |
g_assert (json_object_get_member (options, "marmalade") == NULL); |
| 359 |
|
| 360 |
g_object_unref (channel); |
| 361 |
} |
| 362 |
|
| 363 |
static void |
| 364 |
test_properties (void) |
| 365 |
{ |
| 366 |
JsonObject *options; |
| 367 |
CockpitTransport *transport; |
| 368 |
CockpitTransport *check; |
| 369 |
CockpitChannel *channel; |
| 370 |
gchar *channel_id; |
| 371 |
|
| 372 |
options = json_object_new (); |
| 373 |
transport = g_object_new (mock_transport_get_type (), NULL); |
| 374 |
channel = g_object_new (mock_echo_channel_get_type (), |
| 375 |
"transport", transport, |
| 376 |
"id", "55", |
| 377 |
"options", options, |
| 378 |
NULL); |
| 379 |
g_object_unref (transport); |
| 380 |
json_object_unref (options); |
| 381 |
|
| 382 |
g_object_get (channel, "transport", &check, "id", &channel_id, NULL); |
| 383 |
g_assert (check == transport); |
| 384 |
g_assert_cmpstr (cockpit_channel_get_id (channel), ==, "55"); |
| 385 |
g_assert_cmpstr (channel_id, ==, "55"); |
| 386 |
g_free (channel_id); |
| 387 |
|
| 388 |
g_object_unref (channel); |
| 389 |
} |
| 390 |
|
| 391 |
static void |
| 392 |
test_close_not_capable (void) |
| 393 |
{ |
| 394 |
JsonObject *options; |
| 395 |
JsonObject *sent; |
| 396 |
JsonArray *capabilities; |
| 397 |
MockTransport *transport; |
| 398 |
CockpitChannel *channel; |
| 399 |
CockpitChannel *channel2; |
| 400 |
const gchar *cap[] = { "supported", NULL }; |
| 401 |
|
| 402 |
g_test_expect_message ("cockpit-protocol", G_LOG_LEVEL_MESSAGE, |
| 403 |
"55: unsupported capability required: unsupported1"); |
| 404 |
g_test_expect_message ("cockpit-protocol", G_LOG_LEVEL_MESSAGE, |
| 405 |
"55: unsupported capability required: unsupported2"); |
| 406 |
g_test_expect_message ("cockpit-protocol", G_LOG_LEVEL_MESSAGE, |
| 407 |
"55: unsupported capability required: unsupported1"); |
| 408 |
g_test_expect_message ("cockpit-protocol", G_LOG_LEVEL_MESSAGE, |
| 409 |
"55: unsupported capability required: unsupported2"); |
| 410 |
|
| 411 |
options = json_object_new (); |
| 412 |
capabilities = json_array_new (); |
| 413 |
json_array_add_string_element (capabilities, "unsupported1"); |
| 414 |
json_array_add_string_element (capabilities, "unsupported2"); |
| 415 |
json_object_set_array_member (options, "capabilities", capabilities); |
| 416 |
transport = g_object_new (mock_transport_get_type (), NULL); |
| 417 |
|
| 418 |
channel = g_object_new (mock_echo_channel_get_type (), |
| 419 |
"transport", transport, |
| 420 |
"id", "55", |
| 421 |
"options", options, |
| 422 |
NULL); |
| 423 |
|
| 424 |
while (g_main_context_iteration (NULL, FALSE)); |
| 425 |
|
| 426 |
sent = mock_transport_pop_control (transport); |
| 427 |
g_assert (sent != NULL); |
| 428 |
|
| 429 |
cockpit_assert_json_eq (sent, |
| 430 |
"{ \"command\": \"close\", \"channel\": \"55\", \"problem\": \"not-supported\", \"capabilities\":[]}"); |
| 431 |
g_object_unref (channel); |
| 432 |
|
| 433 |
channel2 = g_object_new (mock_echo_channel_get_type (), |
| 434 |
"transport", transport, |
| 435 |
"id", "55", |
| 436 |
"options", options, |
| 437 |
"capabilities", cap, |
| 438 |
NULL); |
| 439 |
json_object_unref (options); |
| 440 |
|
| 441 |
while (g_main_context_iteration (NULL, FALSE)); |
| 442 |
|
| 443 |
sent = mock_transport_pop_control (transport); |
| 444 |
g_assert (sent != NULL); |
| 445 |
|
| 446 |
cockpit_assert_json_eq (sent, |
| 447 |
"{ \"command\": \"close\", \"channel\": \"55\", \"problem\": \"not-supported\", \"capabilities\":[\"supported\"]}"); |
| 448 |
|
| 449 |
g_object_unref (channel2); |
| 450 |
g_object_unref (transport); |
| 451 |
} |
| 452 |
|
| 453 |
static void |
| 454 |
test_capable (void) |
| 455 |
{ |
| 456 |
JsonObject *options; |
| 457 |
JsonObject *sent; |
| 458 |
JsonArray *capabilities; |
| 459 |
MockTransport *transport; |
| 460 |
CockpitChannel *channel; |
| 461 |
const gchar *cap[] = { "supported", NULL }; |
| 462 |
|
| 463 |
options = json_object_new (); |
| 464 |
capabilities = json_array_new (); |
| 465 |
json_array_add_string_element (capabilities, "supported"); |
| 466 |
json_object_set_array_member (options, "capabilities", capabilities); |
| 467 |
transport = g_object_new (mock_transport_get_type (), NULL); |
| 468 |
|
| 469 |
channel = g_object_new (mock_echo_channel_get_type (), |
| 470 |
"transport", transport, |
| 471 |
"id", "55", |
| 472 |
"options", options, |
| 473 |
"capabilities", cap, |
| 474 |
NULL); |
| 475 |
json_object_unref (options); |
| 476 |
|
| 477 |
while (g_main_context_iteration (NULL, FALSE)); |
| 478 |
|
| 479 |
sent = mock_transport_pop_control (transport); |
| 480 |
g_assert (sent == NULL); |
| 481 |
|
| 482 |
g_object_unref (channel); |
| 483 |
g_object_unref (transport); |
| 484 |
} |
| 485 |
|
| 486 |
static void |
| 487 |
test_null_close_control (void) |
| 488 |
{ |
| 489 |
MockTransport *transport; |
| 490 |
CockpitChannel *channel; |
| 491 |
|
| 492 |
transport = g_object_new (mock_transport_get_type (), NULL); |
| 493 |
|
| 494 |
channel = g_object_new (mock_echo_channel_get_type (), |
| 495 |
"transport", transport, |
| 496 |
"id", "55", |
| 497 |
NULL); |
| 498 |
|
| 499 |
/* Make sure the NULL here works */ |
| 500 |
cockpit_channel_control (channel, "close", NULL); |
| 501 |
|
| 502 |
g_object_unref (channel); |
| 503 |
g_object_unref (transport); |
| 504 |
} |
| 505 |
|
| 506 |
static void |
| 507 |
test_ping_channel (void) |
| 508 |
{ |
| 509 |
JsonObject *reply = NULL; |
| 510 |
JsonObject *options; |
| 511 |
MockTransport *mock; |
| 512 |
CockpitTransport *transport; |
| 513 |
CockpitChannel *channel; |
| 514 |
GBytes *sent; |
| 515 |
|
| 516 |
mock = mock_transport_new (); |
| 517 |
transport = COCKPIT_TRANSPORT (mock); |
| 518 |
|
| 519 |
options = json_object_new (); |
| 520 |
channel = g_object_new (mock_echo_channel_get_type (), |
| 521 |
"transport", transport, |
| 522 |
"id", "55", |
| 523 |
"options", options, |
| 524 |
NULL); |
| 525 |
cockpit_channel_ready (channel, NULL); |
| 526 |
json_object_unref (options); |
| 527 |
|
| 528 |
sent = cockpit_transport_build_control ("command", "ping", "channel", "55", "other", "marmalade", NULL); |
| 529 |
cockpit_transport_emit_recv (transport, NULL, sent); |
| 530 |
g_bytes_unref (sent); |
| 531 |
|
| 532 |
reply = mock_transport_pop_control (mock); |
| 533 |
g_assert (reply != NULL); |
| 534 |
cockpit_assert_json_eq (reply, "{ \"command\": \"ready\", \"channel\": \"55\" }"); |
| 535 |
|
| 536 |
reply = mock_transport_pop_control (mock); |
| 537 |
g_assert (reply != NULL); |
| 538 |
cockpit_assert_json_eq (reply, "{ \"command\": \"pong\", \"channel\": \"55\", \"other\": \"marmalade\" }"); |
| 539 |
|
| 540 |
g_object_unref (channel); |
| 541 |
g_object_unref (mock); |
| 542 |
} |
| 543 |
|
| 544 |
static void |
| 545 |
test_ping_no_channel (void) |
| 546 |
{ |
| 547 |
JsonObject *reply = NULL; |
| 548 |
JsonObject *options; |
| 549 |
MockTransport *mock; |
| 550 |
CockpitTransport *transport; |
| 551 |
CockpitChannel *channel; |
| 552 |
GBytes *sent; |
| 553 |
|
| 554 |
cockpit_expect_message ("received unknown control command: ping"); |
| 555 |
|
| 556 |
mock = mock_transport_new (); |
| 557 |
transport = COCKPIT_TRANSPORT (mock); |
| 558 |
|
| 559 |
options = json_object_new (); |
| 560 |
channel = g_object_new (mock_echo_channel_get_type (), |
| 561 |
"transport", transport, |
| 562 |
"id", "55", |
| 563 |
"options", options, |
| 564 |
NULL); |
| 565 |
json_object_unref (options); |
| 566 |
|
| 567 |
/* |
| 568 |
* Sending a "ping" on an unknown channel. There should be nothing that |
| 569 |
* responds to this and returns a "pong" message. |
| 570 |
*/ |
| 571 |
sent = cockpit_transport_build_control ("command", "ping", "channel", "unknown", "other", "marmalade", NULL); |
| 572 |
cockpit_transport_emit_recv (transport, NULL, sent); |
| 573 |
g_bytes_unref (sent); |
| 574 |
|
| 575 |
cockpit_channel_ready (channel, NULL); |
| 576 |
|
| 577 |
/* Should just get a ready message back */ |
| 578 |
reply = mock_transport_pop_control (mock); |
| 579 |
g_assert (reply != NULL); |
| 580 |
cockpit_assert_json_eq (reply, "{ \"command\": \"ready\", \"channel\": \"55\" }"); |
| 581 |
|
| 582 |
reply = mock_transport_pop_control (mock); |
| 583 |
g_assert (reply == NULL); |
| 584 |
|
| 585 |
g_object_unref (channel); |
| 586 |
g_object_unref (mock); |
| 587 |
} |
| 588 |
|
| 589 |
typedef struct { |
| 590 |
CockpitTransport *transport_a; |
| 591 |
CockpitChannel *channel_a; |
| 592 |
CockpitTransport *transport_b; |
| 593 |
CockpitChannel *channel_b; |
| 594 |
} TestPairCase; |
| 595 |
|
| 596 |
static void |
| 597 |
setup_pair (TestPairCase *tc, |
| 598 |
gconstpointer data) |
| 599 |
{ |
| 600 |
CockpitPipe *pipe; |
| 601 |
JsonObject *options; |
| 602 |
int sv[2]; |
| 603 |
|
| 604 |
if (socketpair (PF_LOCAL, SOCK_STREAM, 0, sv) < 0) |
| 605 |
g_assert_not_reached (); |
| 606 |
|
| 607 |
pipe = cockpit_pipe_new ("a", sv[0], sv[0]); |
| 608 |
tc->transport_a = cockpit_pipe_transport_new (pipe); |
| 609 |
g_object_unref (pipe); |
| 610 |
|
| 611 |
options = json_object_new (); |
| 612 |
json_object_set_string_member (options, "command", "open"); |
| 613 |
json_object_set_string_member (options, "channel", "999"); |
| 614 |
json_object_set_boolean_member (options, "flow-control", TRUE); |
| 615 |
tc->channel_a = g_object_new (mock_null_channel_get_type (), |
| 616 |
"id", "999", |
| 617 |
"options", options, |
| 618 |
"transport", tc->transport_a, |
| 619 |
NULL); |
| 620 |
cockpit_channel_prepare (tc->channel_a); |
| 621 |
json_object_unref (options); |
| 622 |
|
| 623 |
pipe = cockpit_pipe_new ("b", sv[1], sv[1]); |
| 624 |
tc->transport_b = cockpit_pipe_transport_new (pipe); |
| 625 |
g_object_unref (pipe); |
| 626 |
|
| 627 |
options = json_object_new (); |
| 628 |
json_object_set_string_member (options, "channel", "999"); |
| 629 |
json_object_set_boolean_member (options, "flow-control", TRUE); |
| 630 |
tc->channel_b = g_object_new (mock_null_channel_get_type (), |
| 631 |
"id", "999", |
| 632 |
"options", options, |
| 633 |
"transport", tc->transport_b, |
| 634 |
NULL); |
| 635 |
cockpit_channel_prepare (tc->channel_b); |
| 636 |
json_object_unref (options); |
| 637 |
} |
| 638 |
|
| 639 |
static void |
| 640 |
teardown_pair (TestPairCase *tc, |
| 641 |
gconstpointer data) |
| 642 |
{ |
| 643 |
g_object_add_weak_pointer (G_OBJECT (tc->channel_a), (gpointer *)&tc->channel_a); |
| 644 |
g_object_add_weak_pointer (G_OBJECT (tc->transport_a), (gpointer *)&tc->transport_a); |
| 645 |
g_object_add_weak_pointer (G_OBJECT (tc->channel_b), (gpointer *)&tc->channel_b); |
| 646 |
g_object_add_weak_pointer (G_OBJECT (tc->transport_b), (gpointer *)&tc->transport_b); |
| 647 |
g_object_unref (tc->channel_a); |
| 648 |
g_object_unref (tc->channel_b); |
| 649 |
g_object_unref (tc->transport_a); |
| 650 |
g_object_unref (tc->transport_b); |
| 651 |
g_assert (tc->channel_a == NULL); |
| 652 |
g_assert (tc->transport_a == NULL); |
| 653 |
g_assert (tc->channel_b == NULL); |
| 654 |
g_assert (tc->transport_b == NULL); |
| 655 |
} |
| 656 |
|
| 657 |
static gboolean |
| 658 |
on_timeout_set_flag (gpointer user_data) |
| 659 |
{ |
| 660 |
gboolean *data = user_data; |
| 661 |
g_assert (user_data); |
| 662 |
g_assert (*data == FALSE); |
| 663 |
*data = TRUE; |
| 664 |
return FALSE; |
| 665 |
} |
| 666 |
|
| 667 |
static void |
| 668 |
on_pressure_set_throttle (CockpitChannel *channel, |
| 669 |
gboolean throttle, |
| 670 |
gpointer user_data) |
| 671 |
{ |
| 672 |
gint *data = user_data; |
| 673 |
g_assert (user_data != NULL); |
| 674 |
*data = throttle ? 1 : 0; |
| 675 |
} |
| 676 |
|
| 677 |
static void |
| 678 |
test_pressure_window (TestPairCase *tc, |
| 679 |
gconstpointer data) |
| 680 |
{ |
| 681 |
gint throttle = -1; |
| 682 |
GBytes *sent; |
| 683 |
gint i; |
| 684 |
|
| 685 |
/* Ready to go */ |
| 686 |
cockpit_channel_ready (tc->channel_a, NULL); |
| 687 |
cockpit_channel_ready (tc->channel_b, NULL); |
| 688 |
g_signal_connect (tc->channel_a, "pressure", G_CALLBACK (on_pressure_set_throttle), &throttle); |
| 689 |
|
| 690 |
/* Sent this a thousand times */ |
| 691 |
sent = g_bytes_new_take (g_strnfill (1000 * 1000, '?'), 1000 * 1000); |
| 692 |
for (i = 0; i < 10; i++) |
| 693 |
cockpit_channel_send (tc->channel_a, sent, TRUE); |
| 694 |
g_bytes_unref (sent); |
| 695 |
|
| 696 |
/* |
| 697 |
* This should have put way too much in the queue, and thus |
| 698 |
* emitted the back-pressure signal. This signal would normally |
| 699 |
* be used by others to slow down their queueing, but in this |
| 700 |
* case we just check that it was fired. |
| 701 |
*/ |
| 702 |
g_assert_cmpint (throttle, ==, 1); |
| 703 |
|
| 704 |
/* |
| 705 |
* Now the queue is getting drained. At some point, it will be |
| 706 |
* signaled that back pressure has been turned off |
| 707 |
*/ |
| 708 |
while (throttle != 0) |
| 709 |
g_main_context_iteration (NULL, TRUE); |
| 710 |
} |
| 711 |
|
| 712 |
static void |
| 713 |
test_pressure_throttle (TestPairCase *tc, |
| 714 |
gconstpointer data) |
| 715 |
{ |
| 716 |
CockpitFlow *pressure = mock_pressure_new (); |
| 717 |
gboolean timeout = FALSE; |
| 718 |
gboolean throttle = 0; |
| 719 |
GBytes *sent; |
| 720 |
|
| 721 |
cockpit_channel_ready (tc->channel_a, NULL); |
| 722 |
cockpit_channel_ready (tc->channel_b, NULL); |
| 723 |
|
| 724 |
g_signal_connect (tc->channel_a, "pressure", G_CALLBACK (on_pressure_set_throttle), &throttle); |
| 725 |
|
| 726 |
/* Send this a thousand times over the echo pipe */ |
| 727 |
sent = g_bytes_new_take (g_strnfill (400 * 1000, '?'), 400 * 1000); |
| 728 |
|
| 729 |
/* Turn on pressure on the remote side */ |
| 730 |
cockpit_flow_throttle (COCKPIT_FLOW (tc->channel_b), pressure); |
| 731 |
cockpit_flow_emit_pressure (pressure, TRUE); |
| 732 |
|
| 733 |
/* In spite of us running the main loop, no we should have pressure */ |
| 734 |
g_timeout_add_seconds (2, on_timeout_set_flag, &timeout); |
| 735 |
while (timeout == FALSE) |
| 736 |
{ |
| 737 |
if (throttle != 1) |
| 738 |
cockpit_channel_send (tc->channel_a, sent, TRUE); |
| 739 |
g_main_context_iteration (NULL, throttle == 1); |
| 740 |
} |
| 741 |
|
| 742 |
g_assert_cmpint (throttle, ==, 1); |
| 743 |
|
| 744 |
/* Now lets turn off the pressure on the remote side */ |
| 745 |
cockpit_flow_emit_pressure (pressure, FALSE); |
| 746 |
|
| 747 |
/* And we should see the pressure here go down too */ |
| 748 |
while (throttle == 1) |
| 749 |
g_main_context_iteration (NULL, TRUE); |
| 750 |
|
| 751 |
g_assert_cmpint (throttle, ==, 0); |
| 752 |
|
| 753 |
cockpit_flow_throttle (COCKPIT_FLOW (tc->channel_b), NULL); |
| 754 |
g_object_unref (pressure); |
| 755 |
g_bytes_unref (sent); |
| 756 |
} |
| 757 |
|
| 758 |
|
| 759 |
int |
| 760 |
main (int argc, |
| 761 |
char *argv[]) |
| 762 |
{ |
| 763 |
cockpit_test_init (&argc, &argv); |
| 764 |
|
| 765 |
g_test_add_func ("/channel/get-option", test_get_option); |
| 766 |
g_test_add_func ("/channel/properties", test_properties); |
| 767 |
g_test_add_func ("/channel/test-null-close-control", test_null_close_control); |
| 768 |
g_test_add_func ("/channel/test_close_not_capable", |
| 769 |
test_close_not_capable); |
| 770 |
g_test_add_func ("/channel/test_capable", |
| 771 |
test_capable); |
| 772 |
g_test_add ("/channel/recv-send", TestCase, NULL, |
| 773 |
setup, test_recv_and_send, teardown); |
| 774 |
g_test_add ("/channel/recv-queue", TestCase, NULL, |
| 775 |
setup, test_recv_and_queue, teardown); |
| 776 |
g_test_add ("/channel/ready-message", TestCase, NULL, |
| 777 |
setup, test_ready_message, teardown); |
| 778 |
g_test_add ("/channel/close-immediately", TestCase, NULL, |
| 779 |
setup, test_close_immediately, teardown); |
| 780 |
g_test_add ("/channel/close-option", TestCase, NULL, |
| 781 |
setup, test_close_option, teardown); |
| 782 |
g_test_add ("/channel/close-json-option", TestCase, NULL, |
| 783 |
setup, test_close_json_option, teardown); |
| 784 |
g_test_add ("/channel/close-transport", TestCase, NULL, |
| 785 |
setup, test_close_transport, teardown); |
| 786 |
|
| 787 |
g_test_add ("/channel/pressure/window", TestPairCase, NULL, |
| 788 |
setup_pair, test_pressure_window, teardown_pair); |
| 789 |
g_test_add ("/channel/pressure/throttle", TestPairCase, NULL, |
| 790 |
setup_pair, test_pressure_throttle, teardown_pair); |
| 791 |
|
| 792 |
g_test_add_func ("/channel/ping/normal", test_ping_channel); |
| 793 |
g_test_add_func ("/channel/ping/no-channel", test_ping_no_channel); |
| 794 |
|
| 795 |
return g_test_run (); |
| 796 |
} |