| 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 "cockpitwebservice.h" |
| 9 |
#include "cockpitchannelresponse.h" |
| 10 |
#include "cockpitchannelsocket.h" |
| 11 |
#include "cockpitws.h" |
| 12 |
|
| 13 |
#include "cockpitpipe.h" |
| 14 |
#include "common/cockpitconf.h" |
| 15 |
#include "cockpitpipetransport.h" |
| 16 |
#include "testlib/cockpittest.h" |
| 17 |
#include "cockpitwebserver.h" |
| 18 |
#include "cockpitwebinject.h" |
| 19 |
|
| 20 |
#include "ws/mock-service.h" |
| 21 |
|
| 22 |
#include <gio/gio.h> |
| 23 |
#include <glib-unix.h> |
| 24 |
#include <glib/gstdio.h> |
| 25 |
#include <string.h> |
| 26 |
|
| 27 |
/* Override from cockpitconf.c */ |
| 28 |
extern const gchar *cockpit_config_file; |
| 29 |
|
| 30 |
static GMainLoop *loop = NULL; |
| 31 |
static gboolean signalled = FALSE; |
| 32 |
static int exit_code = 0; |
| 33 |
static gint server_port = 0; |
| 34 |
static gchar **bridge_argv; |
| 35 |
static const gchar *bus_address; |
| 36 |
static const gchar *direct_address; |
| 37 |
static gchar **server_roots; |
| 38 |
|
| 39 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 40 |
|
| 41 |
static GObject *exported = NULL; |
| 42 |
static GObject *exported_b = NULL; |
| 43 |
static GObject *direct = NULL; |
| 44 |
static GObject *direct_b = NULL; |
| 45 |
|
| 46 |
static GDBusMessage * |
| 47 |
on_filter_func (GDBusConnection *connection, |
| 48 |
GDBusMessage *message, |
| 49 |
gboolean incoming, |
| 50 |
gpointer user_data) |
| 51 |
{ |
| 52 |
GError *error = NULL; |
| 53 |
GDBusMessage *reply = NULL; |
| 54 |
|
| 55 |
if (incoming) |
| 56 |
{ |
| 57 |
if (g_dbus_message_get_message_type (message) == G_DBUS_MESSAGE_TYPE_METHOD_CALL && |
| 58 |
g_str_equal (g_dbus_message_get_path (message), "/bork") && |
| 59 |
g_str_equal (g_dbus_message_get_interface (message), "borkety.Bork") && |
| 60 |
g_str_equal (g_dbus_message_get_member (message), "Echo")) |
| 61 |
{ |
| 62 |
reply = g_dbus_message_new_method_reply (message); |
| 63 |
g_dbus_message_set_body (reply, g_dbus_message_get_body (message)); |
| 64 |
} |
| 65 |
|
| 66 |
if (g_dbus_message_get_message_type (message) == G_DBUS_MESSAGE_TYPE_SIGNAL && |
| 67 |
g_str_equal (g_dbus_message_get_path (message), "/bork") && |
| 68 |
g_str_equal (g_dbus_message_get_interface (message), "borkety.Bork")) |
| 69 |
{ |
| 70 |
reply = g_dbus_message_new_signal ("/bork", "borkety.Bork", |
| 71 |
g_dbus_message_get_member (message)); |
| 72 |
g_dbus_message_set_body (reply, g_dbus_message_get_body (message)); |
| 73 |
} |
| 74 |
|
| 75 |
if (reply) |
| 76 |
{ |
| 77 |
g_dbus_connection_send_message (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &error); |
| 78 |
if (error != NULL) |
| 79 |
{ |
| 80 |
g_warning ("Couldn't send DBus message: %s", error->message); |
| 81 |
g_error_free (error); |
| 82 |
} |
| 83 |
g_object_unref (reply); |
| 84 |
g_object_unref (message); |
| 85 |
return NULL; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
return message; |
| 90 |
} |
| 91 |
|
| 92 |
static void |
| 93 |
on_bus_acquired (GDBusConnection *connection, |
| 94 |
const gchar *name, |
| 95 |
gpointer user_data) |
| 96 |
{ |
| 97 |
exported = mock_service_create_and_export (connection, "/otree"); |
| 98 |
exported_b = mock_service_create_and_export (connection, "/different"); |
| 99 |
g_dbus_connection_add_filter (connection, on_filter_func, NULL, NULL); |
| 100 |
} |
| 101 |
|
| 102 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 103 |
|
| 104 |
static gboolean |
| 105 |
mock_http_info (CockpitWebRequest *request, |
| 106 |
CockpitWebResponse *response) |
| 107 |
{ |
| 108 |
g_autoptr(JsonObject) info = json_object_new (); |
| 109 |
json_object_set_boolean_member (info, "skip_slow_tests", g_getenv ("COCKPIT_SKIP_SLOW_TESTS") != NULL); |
| 110 |
|
| 111 |
g_autoptr(GBytes) bytes = cockpit_json_write_bytes (info); |
| 112 |
cockpit_web_response_content (response, NULL, bytes, NULL); |
| 113 |
return TRUE; |
| 114 |
} |
| 115 |
|
| 116 |
static gboolean |
| 117 |
mock_http_qs (CockpitWebRequest *request, |
| 118 |
CockpitWebResponse *response) |
| 119 |
{ |
| 120 |
const gchar *qs; |
| 121 |
GBytes *bytes; |
| 122 |
|
| 123 |
qs = cockpit_web_request_get_query (request); |
| 124 |
if (!qs) |
| 125 |
{ |
| 126 |
cockpit_web_response_error (response, 400, NULL, "No query string"); |
| 127 |
} |
| 128 |
else |
| 129 |
{ |
| 130 |
bytes = g_bytes_new (qs, strlen (qs)); |
| 131 |
cockpit_web_response_content (response, NULL, bytes, NULL); |
| 132 |
g_bytes_unref (bytes); |
| 133 |
} |
| 134 |
|
| 135 |
return TRUE; |
| 136 |
} |
| 137 |
|
| 138 |
static gboolean |
| 139 |
send_numbers (gpointer data) |
| 140 |
{ |
| 141 |
CockpitWebResponse *response = data; |
| 142 |
gint *at = g_object_get_data (data, "at"); |
| 143 |
gchar *string; |
| 144 |
GBytes *bytes; |
| 145 |
|
| 146 |
string = g_strdup_printf ("%d ", *at); |
| 147 |
(*at) += 1; |
| 148 |
|
| 149 |
bytes = g_bytes_new_take (string, strlen (string)); |
| 150 |
cockpit_web_response_queue (response, bytes); |
| 151 |
g_bytes_unref (bytes); |
| 152 |
|
| 153 |
if (*at == 10) |
| 154 |
{ |
| 155 |
cockpit_web_response_complete (response); |
| 156 |
return FALSE; |
| 157 |
} |
| 158 |
|
| 159 |
return TRUE; |
| 160 |
} |
| 161 |
|
| 162 |
static const char* SPLIT_UTF8_FRAMES[] = { |
| 163 |
"initial", |
| 164 |
/* split an é in the middle */ |
| 165 |
"first half \xc3", |
| 166 |
"\xa9 second half", |
| 167 |
"final", |
| 168 |
NULL, |
| 169 |
}; |
| 170 |
|
| 171 |
static gboolean |
| 172 |
send_split_utf8 (gpointer data) |
| 173 |
{ |
| 174 |
CockpitWebResponse *response = data; |
| 175 |
gint *at = g_object_get_data (data, "at"); |
| 176 |
const char *frame = SPLIT_UTF8_FRAMES[*at]; |
| 177 |
|
| 178 |
if (frame == NULL) |
| 179 |
{ |
| 180 |
cockpit_web_response_complete (response); |
| 181 |
return FALSE; |
| 182 |
} |
| 183 |
|
| 184 |
(*at) += 1; |
| 185 |
|
| 186 |
g_autoptr(GBytes) bytes = g_bytes_new_static (frame, strlen (frame)); |
| 187 |
cockpit_web_response_queue (response, bytes); |
| 188 |
return TRUE; |
| 189 |
} |
| 190 |
|
| 191 |
static gboolean |
| 192 |
send_truncated_utf8 (gpointer data) |
| 193 |
{ |
| 194 |
CockpitWebResponse *response = data; |
| 195 |
gint *at = g_object_get_data (data, "at"); |
| 196 |
const char *frame = SPLIT_UTF8_FRAMES[*at]; |
| 197 |
|
| 198 |
/* only send the first two frames */ |
| 199 |
if (*at == 2) |
| 200 |
{ |
| 201 |
cockpit_web_response_complete (response); |
| 202 |
return FALSE; |
| 203 |
} |
| 204 |
|
| 205 |
(*at) += 1; |
| 206 |
|
| 207 |
g_autoptr(GBytes) bytes = g_bytes_new_static (frame, strlen (frame)); |
| 208 |
cockpit_web_response_queue (response, bytes); |
| 209 |
return TRUE; |
| 210 |
} |
| 211 |
|
| 212 |
static gboolean |
| 213 |
send_binary_data (gpointer data) |
| 214 |
{ |
| 215 |
CockpitWebResponse *response = data; |
| 216 |
g_autoptr(GBytes) bytes = g_bytes_new_static ("\xFF\x01\xFF\x02", 4); |
| 217 |
cockpit_web_response_queue (response, bytes); |
| 218 |
cockpit_web_response_complete (response); |
| 219 |
return FALSE; |
| 220 |
} |
| 221 |
|
| 222 |
static gboolean |
| 223 |
mock_http_stream (CockpitWebResponse *response, GSourceFunc func) |
| 224 |
{ |
| 225 |
cockpit_web_response_headers (response, 200, "OK", -1, NULL); |
| 226 |
g_object_set_data_full (G_OBJECT (response), "at", g_new0 (gint, 1), g_free); |
| 227 |
g_timeout_add_full (G_PRIORITY_DEFAULT, 100, func, |
| 228 |
g_object_ref (response), g_object_unref); |
| 229 |
|
| 230 |
return TRUE; |
| 231 |
} |
| 232 |
|
| 233 |
static gboolean |
| 234 |
mock_http_headers (CockpitWebResponse *response, |
| 235 |
GHashTable *in_headers) |
| 236 |
{ |
| 237 |
GHashTableIter iter; |
| 238 |
GHashTable *headers; |
| 239 |
gpointer name, value; |
| 240 |
|
| 241 |
headers = cockpit_web_server_new_table(); |
| 242 |
g_hash_table_iter_init (&iter, in_headers); |
| 243 |
while (g_hash_table_iter_next (&iter, &name, &value)) |
| 244 |
{ |
| 245 |
if (g_str_has_prefix (name, "Header")) |
| 246 |
g_hash_table_insert (headers, g_strdup (name), g_strdup (value)); |
| 247 |
} |
| 248 |
g_hash_table_replace (headers, g_strdup ("Header3"), g_strdup ("three")); |
| 249 |
g_hash_table_replace (headers, g_strdup ("Header4"), g_strdup ("marmalade")); |
| 250 |
|
| 251 |
cockpit_web_response_headers_full (response, 201, "Yoo Hoo", -1, headers); |
| 252 |
cockpit_web_response_complete (response); |
| 253 |
|
| 254 |
g_hash_table_unref (headers); |
| 255 |
|
| 256 |
return TRUE; |
| 257 |
} |
| 258 |
|
| 259 |
static gboolean |
| 260 |
mock_http_host (CockpitWebResponse *response, |
| 261 |
GHashTable *in_headers) |
| 262 |
{ |
| 263 |
GHashTable *headers; |
| 264 |
|
| 265 |
headers = cockpit_web_server_new_table(); |
| 266 |
g_hash_table_insert (headers, g_strdup ("Host"), g_strdup (g_hash_table_lookup (in_headers, "Host"))); |
| 267 |
cockpit_web_response_headers_full (response, 201, "Yoo Hoo", -1, headers); |
| 268 |
cockpit_web_response_complete (response); |
| 269 |
|
| 270 |
g_hash_table_unref (headers); |
| 271 |
|
| 272 |
return TRUE; |
| 273 |
} |
| 274 |
|
| 275 |
static gboolean |
| 276 |
mock_http_connection (CockpitWebResponse *response) |
| 277 |
{ |
| 278 |
GIOStream *io; |
| 279 |
GBytes *bytes; |
| 280 |
gchar *output; |
| 281 |
|
| 282 |
/* Lets caller have an indication of which IO stream is being used */ |
| 283 |
|
| 284 |
io = cockpit_web_response_get_stream (response); |
| 285 |
output = g_strdup_printf ("%p", io); |
| 286 |
bytes = g_bytes_new_take (output, strlen (output)); |
| 287 |
|
| 288 |
cockpit_web_response_content (response, NULL, bytes, NULL); |
| 289 |
g_bytes_unref (bytes); |
| 290 |
|
| 291 |
return TRUE; |
| 292 |
} |
| 293 |
|
| 294 |
static gboolean |
| 295 |
mock_http_headonly (CockpitWebRequest *request, |
| 296 |
CockpitWebResponse *response) |
| 297 |
{ |
| 298 |
if (!g_str_equal (cockpit_web_request_get_method (request), "HEAD")) |
| 299 |
{ |
| 300 |
cockpit_web_response_error (response, 400, NULL, "Only HEAD allowed on this path"); |
| 301 |
} |
| 302 |
else |
| 303 |
{ |
| 304 |
const char *input_data = cockpit_web_request_lookup_header (request, "InputData"); |
| 305 |
if (!input_data) |
| 306 |
{ |
| 307 |
cockpit_web_response_error (response, 400, NULL, "Requires InputData header"); |
| 308 |
return TRUE; |
| 309 |
} |
| 310 |
|
| 311 |
g_autoptr(GHashTable) headers = cockpit_web_server_new_table(); |
| 312 |
g_hash_table_insert (headers, g_strdup ("InputDataLength"), g_strdup_printf ("%zu", strlen (input_data))); |
| 313 |
cockpit_web_response_headers_full (response, 200, "OK", -1, headers); |
| 314 |
cockpit_web_response_complete (response); |
| 315 |
} |
| 316 |
|
| 317 |
return TRUE; |
| 318 |
} |
| 319 |
|
| 320 |
static gboolean |
| 321 |
mock_http_expect_warnings (CockpitWebResponse *response, |
| 322 |
GLogLevelFlags warnings) |
| 323 |
{ |
| 324 |
g_log_set_always_fatal (warnings | G_LOG_LEVEL_ERROR); |
| 325 |
|
| 326 |
cockpit_web_response_headers_full (response, 200, "OK", 0, NULL); |
| 327 |
cockpit_web_response_complete (response); |
| 328 |
|
| 329 |
return TRUE; |
| 330 |
} |
| 331 |
|
| 332 |
static gboolean |
| 333 |
on_handle_mock (CockpitWebServer *server, |
| 334 |
CockpitWebRequest *request, |
| 335 |
const gchar *path, |
| 336 |
GHashTable *headers, |
| 337 |
CockpitWebResponse *response, |
| 338 |
gpointer data) |
| 339 |
{ |
| 340 |
g_assert (g_str_has_prefix (path, "/mock/")); |
| 341 |
path += 5; |
| 342 |
|
| 343 |
if (g_str_equal (path, "/info")) |
| 344 |
return mock_http_info (request, response); |
| 345 |
if (g_str_equal (path, "/qs")) |
| 346 |
return mock_http_qs (request, response); |
| 347 |
if (g_str_equal (path, "/stream")) |
| 348 |
return mock_http_stream (response, send_numbers); |
| 349 |
if (g_str_equal (path, "/split-utf8")) |
| 350 |
return mock_http_stream (response, send_split_utf8); |
| 351 |
if (g_str_equal (path, "/truncated-utf8")) |
| 352 |
return mock_http_stream (response, send_truncated_utf8); |
| 353 |
if (g_str_equal (path, "/binary-data")) |
| 354 |
return mock_http_stream (response, send_binary_data); |
| 355 |
if (g_str_equal (path, "/headers")) |
| 356 |
return mock_http_headers (response, headers); |
| 357 |
if (g_str_equal (path, "/host")) |
| 358 |
return mock_http_host (response, headers); |
| 359 |
if (g_str_equal (path, "/connection")) |
| 360 |
return mock_http_connection (response); |
| 361 |
if (g_str_equal (path, "/headonly")) |
| 362 |
return mock_http_headonly (request, response); |
| 363 |
if (g_str_equal (path, "/expect-warnings")) |
| 364 |
return mock_http_expect_warnings (response, 0); |
| 365 |
if (g_str_equal (path, "/dont-expect-warnings")) |
| 366 |
return mock_http_expect_warnings (response, G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL); |
| 367 |
else |
| 368 |
return FALSE; |
| 369 |
} |
| 370 |
|
| 371 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 372 |
|
| 373 |
/* Called by @server when handling HTTP requests to /socket - runs in a separate |
| 374 |
* thread dedicated to the request so it may do blocking I/O |
| 375 |
*/ |
| 376 |
|
| 377 |
static CockpitWebService *service; |
| 378 |
static CockpitPipe *bridge; |
| 379 |
|
| 380 |
static gboolean |
| 381 |
on_transport_control (CockpitTransport *transport, |
| 382 |
const char *command, |
| 383 |
const gchar *channel, |
| 384 |
JsonObject *options, |
| 385 |
GBytes *payload, |
| 386 |
gpointer data) |
| 387 |
{ |
| 388 |
gboolean *flag = data; |
| 389 |
g_assert (flag != NULL); |
| 390 |
|
| 391 |
if (g_str_equal (command, "init")) |
| 392 |
*flag = TRUE; |
| 393 |
|
| 394 |
return FALSE; |
| 395 |
} |
| 396 |
|
| 397 |
static gboolean |
| 398 |
on_handle_stream_socket (CockpitWebServer *server, |
| 399 |
CockpitWebRequest *request, |
| 400 |
gpointer user_data) |
| 401 |
{ |
| 402 |
const gchar *path = cockpit_web_request_get_path (request); |
| 403 |
|
| 404 |
CockpitTransport *transport; |
| 405 |
const gchar *query = NULL; |
| 406 |
CockpitCreds *creds; |
| 407 |
int session_stdin = -1; |
| 408 |
int session_stdout = -1; |
| 409 |
GError *error = NULL; |
| 410 |
gboolean ready = FALSE; |
| 411 |
gulong handler; |
| 412 |
GPid pid = 0; |
| 413 |
|
| 414 |
gchar *value; |
| 415 |
gchar **argv; |
| 416 |
|
| 417 |
if (!g_str_has_prefix (path, "/cockpit/socket")) |
| 418 |
return FALSE; |
| 419 |
|
| 420 |
if (path[15] == '?') |
| 421 |
{ |
| 422 |
query = path + 16; |
| 423 |
} |
| 424 |
else if (path[15] != '\0') |
| 425 |
{ |
| 426 |
return FALSE; |
| 427 |
} |
| 428 |
|
| 429 |
if (service) |
| 430 |
{ |
| 431 |
g_object_ref (service); |
| 432 |
} |
| 433 |
else |
| 434 |
{ |
| 435 |
g_clear_object (&bridge); |
| 436 |
|
| 437 |
value = g_strdup_printf ("%d", server_port); |
| 438 |
|
| 439 |
argv = g_strdupv (bridge_argv); |
| 440 |
if (query) |
| 441 |
argv[g_strv_length (argv) - 1] = g_strdup (query); |
| 442 |
|
| 443 |
g_spawn_async_with_pipes (NULL, argv, NULL, |
| 444 |
G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD, |
| 445 |
NULL, NULL, &pid, &session_stdin, &session_stdout, NULL, &error); |
| 446 |
|
| 447 |
g_free (argv); |
| 448 |
g_free (value); |
| 449 |
|
| 450 |
if (error) |
| 451 |
{ |
| 452 |
g_critical ("couldn't run bridge %s: %s", bridge_argv[0], error->message); |
| 453 |
return FALSE; |
| 454 |
} |
| 455 |
|
| 456 |
bridge = g_object_new (COCKPIT_TYPE_PIPE, |
| 457 |
"name", "test-server-bridge", |
| 458 |
"in-fd", session_stdout, |
| 459 |
"out-fd", session_stdin, |
| 460 |
"pid", pid, |
| 461 |
NULL); |
| 462 |
|
| 463 |
creds = cockpit_creds_new ("test", |
| 464 |
COCKPIT_CRED_CSRF_TOKEN, "myspecialtoken", |
| 465 |
NULL); |
| 466 |
|
| 467 |
transport = cockpit_pipe_transport_new (bridge); |
| 468 |
service = cockpit_web_service_new (creds, transport); |
| 469 |
|
| 470 |
/* Manually created services won't be init'd yet, wait for that before sending data */ |
| 471 |
handler = g_signal_connect (transport, "control", G_CALLBACK (on_transport_control), &ready); |
| 472 |
|
| 473 |
while (!ready) |
| 474 |
g_main_context_iteration (NULL, TRUE); |
| 475 |
|
| 476 |
cockpit_creds_unref (creds); |
| 477 |
g_object_unref (transport); |
| 478 |
|
| 479 |
/* Clear the pointer automatically when service is done */ |
| 480 |
g_object_add_weak_pointer (G_OBJECT (service), (gpointer *)&service); |
| 481 |
|
| 482 |
g_signal_handler_disconnect (transport, handler); |
| 483 |
} |
| 484 |
|
| 485 |
cockpit_web_service_socket (service, request); |
| 486 |
|
| 487 |
/* Keeps ref on itself until it closes */ |
| 488 |
g_object_unref (service); |
| 489 |
|
| 490 |
return TRUE; |
| 491 |
} |
| 492 |
|
| 493 |
static void |
| 494 |
on_echo_socket_message (WebSocketConnection *self, |
| 495 |
WebSocketDataType type, |
| 496 |
GBytes *message, |
| 497 |
gpointer user_data) |
| 498 |
{ |
| 499 |
GByteArray *array = g_bytes_unref_to_array (g_bytes_ref (message)); |
| 500 |
GBytes *payload; |
| 501 |
guint i; |
| 502 |
|
| 503 |
/* Capitalize and relay back */ |
| 504 |
for (i = 0; i < array->len; i++) |
| 505 |
array->data[i] = g_ascii_toupper (array->data[i]); |
| 506 |
|
| 507 |
payload = g_byte_array_free_to_bytes (array); |
| 508 |
web_socket_connection_send (self, type, NULL, payload); |
| 509 |
g_bytes_unref (payload); |
| 510 |
} |
| 511 |
|
| 512 |
static void |
| 513 |
on_echo_socket_close (WebSocketConnection *ws, |
| 514 |
gpointer user_data) |
| 515 |
{ |
| 516 |
g_object_unref (ws); |
| 517 |
} |
| 518 |
|
| 519 |
static gboolean |
| 520 |
on_handle_stream_external (CockpitWebServer *server, |
| 521 |
CockpitWebRequest *request, |
| 522 |
gpointer user_data) |
| 523 |
{ |
| 524 |
const gchar *path = cockpit_web_request_get_path (request); |
| 525 |
GIOStream *io_stream = cockpit_web_request_get_io_stream (request); |
| 526 |
GByteArray *input = cockpit_web_request_get_buffer (request); |
| 527 |
GHashTable *headers = cockpit_web_request_get_headers (request); |
| 528 |
|
| 529 |
gboolean handled = FALSE; |
| 530 |
const gchar *upgrade; |
| 531 |
CockpitCreds *creds; |
| 532 |
const gchar *expected; |
| 533 |
const gchar *segment; |
| 534 |
JsonObject *open = NULL; |
| 535 |
GBytes *bytes; |
| 536 |
guchar *decoded; |
| 537 |
gsize length; |
| 538 |
|
| 539 |
if (g_str_has_prefix (path, "/cockpit/echosocket")) |
| 540 |
{ |
| 541 |
const gchar *protocols[] = { "cockpit1", NULL }; |
| 542 |
const gchar *origins[2] = { NULL, NULL }; |
| 543 |
WebSocketConnection *ws = NULL; |
| 544 |
|
| 545 |
origins[0] = g_strdup_printf ("http://localhost:%u", server_port); |
| 546 |
|
| 547 |
ws = web_socket_server_new_for_stream ((const gchar **)origins, |
| 548 |
protocols, io_stream, headers, input); |
| 549 |
|
| 550 |
g_signal_connect (ws, "message", G_CALLBACK (on_echo_socket_message), NULL); |
| 551 |
g_signal_connect (ws, "close", G_CALLBACK (on_echo_socket_close), NULL); |
| 552 |
return TRUE; |
| 553 |
} |
| 554 |
|
| 555 |
if (!g_str_has_prefix (path, "/cockpit/channel/")) |
| 556 |
return FALSE; |
| 557 |
|
| 558 |
/* Remove /cockpit/channel/ part */ |
| 559 |
segment = path + 17; |
| 560 |
|
| 561 |
if (service) |
| 562 |
{ |
| 563 |
creds = cockpit_web_service_get_creds (service); |
| 564 |
g_return_val_if_fail (creds != NULL, FALSE); |
| 565 |
|
| 566 |
expected = cockpit_creds_get_csrf_token (creds); |
| 567 |
g_return_val_if_fail (expected != NULL, FALSE); |
| 568 |
|
| 569 |
/* No such path is valid */ |
| 570 |
if (g_str_equal (segment, expected)) |
| 571 |
{ |
| 572 |
decoded = g_base64_decode (cockpit_web_request_get_query (request), &length); |
| 573 |
if (decoded) |
| 574 |
{ |
| 575 |
bytes = g_bytes_new_take (decoded, length); |
| 576 |
if (!cockpit_transport_parse_command (bytes, NULL, NULL, &open)) |
| 577 |
{ |
| 578 |
open = NULL; |
| 579 |
g_message ("invalid external channel query"); |
| 580 |
} |
| 581 |
g_bytes_unref (bytes); |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
if (open) |
| 586 |
{ |
| 587 |
upgrade = g_hash_table_lookup (headers, "Upgrade"); |
| 588 |
if (upgrade && g_ascii_strcasecmp (upgrade, "websocket") == 0) |
| 589 |
{ |
| 590 |
cockpit_channel_socket_open (service, open, request); |
| 591 |
handled = TRUE; |
| 592 |
} |
| 593 |
else |
| 594 |
{ |
| 595 |
cockpit_channel_response_open (service, request, open); |
| 596 |
handled = TRUE; |
| 597 |
} |
| 598 |
|
| 599 |
json_object_unref (open); |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
return handled; |
| 604 |
} |
| 605 |
|
| 606 |
static void |
| 607 |
inject_address (CockpitWebResponse *response, |
| 608 |
const gchar *name, |
| 609 |
const gchar *value) |
| 610 |
{ |
| 611 |
GBytes *inject = NULL; |
| 612 |
CockpitWebFilter *filter = NULL; |
| 613 |
gchar *line = NULL; |
| 614 |
|
| 615 |
if (value) |
| 616 |
{ |
| 617 |
line = g_strconcat ("\n<script>\nvar ", name, " = '", value, "';\n</script>", NULL); |
| 618 |
|
| 619 |
inject = g_bytes_new (line, strlen (line)); |
| 620 |
filter = cockpit_web_inject_new ("<head>", inject, 1); |
| 621 |
g_bytes_unref (inject); |
| 622 |
|
| 623 |
cockpit_web_response_add_filter (response, filter); |
| 624 |
g_object_unref (filter); |
| 625 |
} |
| 626 |
|
| 627 |
g_free (line); |
| 628 |
} |
| 629 |
|
| 630 |
static void |
| 631 |
handle_raw_data (CockpitWebResponse *response, |
| 632 |
const gchar *data) |
| 633 |
{ |
| 634 |
GBytes *block; |
| 635 |
|
| 636 |
/* For testing code that uses "manifests" return empty manifests for now */ |
| 637 |
block = g_bytes_new_static (data, strlen (data)); |
| 638 |
cockpit_web_response_content (response, NULL, block, NULL); |
| 639 |
g_bytes_unref (block); |
| 640 |
} |
| 641 |
|
| 642 |
static void |
| 643 |
handle_manifests_js (CockpitWebResponse *response) |
| 644 |
{ |
| 645 |
/* For testing code that uses "manifests" return empty manifests for now */ |
| 646 |
handle_raw_data (response, "define({ });"); |
| 647 |
} |
| 648 |
|
| 649 |
static void |
| 650 |
handle_manifests_json (CockpitWebResponse *response) |
| 651 |
{ |
| 652 |
/* For testing code that uses "/pkg/manifests.json" return empty manifests for now */ |
| 653 |
handle_raw_data (response, "{ }"); |
| 654 |
} |
| 655 |
|
| 656 |
static void |
| 657 |
handle_package_file (CockpitWebServer *server, |
| 658 |
CockpitWebResponse *response, |
| 659 |
gchar **parts) |
| 660 |
{ |
| 661 |
gchar *rebuilt; |
| 662 |
|
| 663 |
/* TODO: This needs a better implementation later, when the tests aren't all broken */ |
| 664 |
if (g_strcmp0 (parts[2], "system") == 0) |
| 665 |
{ |
| 666 |
g_free (parts[2]); |
| 667 |
parts[2] = g_strdup ("systemd"); |
| 668 |
} |
| 669 |
if (g_strcmp0 (parts[2], "base1") == 0) |
| 670 |
{ |
| 671 |
g_free (parts[1]); |
| 672 |
parts[1] = g_strdup ("src"); |
| 673 |
} |
| 674 |
else if (g_strcmp0 (parts[2], "lib") == 0) |
| 675 |
{ |
| 676 |
g_free (parts[1]); |
| 677 |
parts[1] = g_strdup("lib"); |
| 678 |
parts++; |
| 679 |
} |
| 680 |
|
| 681 |
rebuilt = g_strjoinv ("/", parts); |
| 682 |
cockpit_web_response_file (response, rebuilt, (const gchar **)server_roots); |
| 683 |
g_free (rebuilt); |
| 684 |
} |
| 685 |
|
| 686 |
static gboolean |
| 687 |
on_handle_resource (CockpitWebServer *server, |
| 688 |
CockpitWebRequest *request, |
| 689 |
const gchar *path, |
| 690 |
GHashTable *headers, |
| 691 |
CockpitWebResponse *response, |
| 692 |
gpointer user_data) |
| 693 |
{ |
| 694 |
gchar **parts; |
| 695 |
|
| 696 |
g_assert (g_str_has_prefix (path, "/pkg")); |
| 697 |
|
| 698 |
cockpit_web_response_set_cache_type (response, COCKPIT_WEB_RESPONSE_NO_CACHE); |
| 699 |
|
| 700 |
parts = g_strsplit (path, "/", -1); |
| 701 |
if (g_strcmp0 (parts[2], "manifests.js") == 0 && !parts[3]) |
| 702 |
handle_manifests_js (response); |
| 703 |
else if (g_strcmp0 (parts[2], "manifests.json") == 0 && !parts[3]) |
| 704 |
handle_manifests_json (response); |
| 705 |
else |
| 706 |
handle_package_file (server, response, parts); |
| 707 |
|
| 708 |
g_strfreev (parts); |
| 709 |
return TRUE; |
| 710 |
} |
| 711 |
|
| 712 |
static gboolean |
| 713 |
on_handle_source (CockpitWebServer *server, |
| 714 |
CockpitWebRequest *request, |
| 715 |
const gchar *path, |
| 716 |
GHashTable *headers, |
| 717 |
CockpitWebResponse *response, |
| 718 |
gpointer user_data) |
| 719 |
{ |
| 720 |
cockpit_web_response_set_cache_type (response, COCKPIT_WEB_RESPONSE_NO_CACHE); |
| 721 |
if (g_str_has_suffix (path, ".html")) |
| 722 |
{ |
| 723 |
inject_address (response, "bus_address", bus_address); |
| 724 |
inject_address (response, "direct_address", direct_address); |
| 725 |
} |
| 726 |
cockpit_web_response_file_or_gz (response, path, (const gchar **)server_roots); |
| 727 |
return TRUE; |
| 728 |
} |
| 729 |
|
| 730 |
static gboolean |
| 731 |
on_handle_favicon (CockpitWebServer *server, |
| 732 |
CockpitWebRequest *request, |
| 733 |
const gchar *path, |
| 734 |
GHashTable *headers, |
| 735 |
CockpitWebResponse *response, |
| 736 |
gpointer user_data) |
| 737 |
{ |
| 738 |
const char* roots[] = { SRCDIR "/src/branding/default", NULL }; |
| 739 |
cockpit_web_response_file (response, NULL, roots); |
| 740 |
return TRUE; |
| 741 |
} |
| 742 |
|
| 743 |
static void |
| 744 |
server_ready (void) |
| 745 |
{ |
| 746 |
const gchar *roots[] = { ".", SRCDIR, BUILDDIR, NULL }; |
| 747 |
GError *error = NULL; |
| 748 |
CockpitWebServer *server; |
| 749 |
gchar *url; |
| 750 |
const gchar *address_fd_str = g_getenv("TEST_SERVER_ADDRESS_FD"); |
| 751 |
|
| 752 |
if (!isatty (1)) |
| 753 |
server_port = 0; /* select one automatically */ |
| 754 |
else |
| 755 |
server_port = 8765; |
| 756 |
|
| 757 |
server_roots = cockpit_web_response_resolve_roots (roots); |
| 758 |
server = cockpit_web_server_new (COCKPIT_WEB_SERVER_NONE); |
| 759 |
server_port = cockpit_web_server_add_inet_listener (server, NULL, server_port, &error); |
| 760 |
g_assert_no_error (error); |
| 761 |
g_assert (server_port != 0); |
| 762 |
|
| 763 |
|
| 764 |
g_signal_connect (server, "handle-stream", |
| 765 |
G_CALLBACK (on_handle_stream_socket), NULL); |
| 766 |
g_signal_connect (server, "handle-stream", |
| 767 |
G_CALLBACK (on_handle_stream_external), NULL); |
| 768 |
g_signal_connect (server, "handle-resource::/pkg/", |
| 769 |
G_CALLBACK (on_handle_resource), NULL); |
| 770 |
g_signal_connect (server, "handle-resource::/dist/", |
| 771 |
G_CALLBACK (on_handle_source), NULL); |
| 772 |
g_signal_connect (server, "handle-resource::/qunit/", |
| 773 |
G_CALLBACK (on_handle_source), NULL); |
| 774 |
g_signal_connect (server, "handle-resource::/favicon.ico", |
| 775 |
G_CALLBACK (on_handle_favicon), NULL); |
| 776 |
g_signal_connect (server, "handle-resource::/mock/", |
| 777 |
G_CALLBACK (on_handle_mock), NULL); |
| 778 |
|
| 779 |
url = g_strdup_printf("http://localhost:%d/", server_port); |
| 780 |
|
| 781 |
cockpit_web_server_start (server); |
| 782 |
|
| 783 |
if (address_fd_str) |
| 784 |
{ |
| 785 |
int fd = atoi (address_fd_str); |
| 786 |
int r = write (fd, url, strlen (url)); |
| 787 |
close (fd); |
| 788 |
g_assert (r == strlen (url)); |
| 789 |
} |
| 790 |
else if (!isatty (1)) |
| 791 |
{ |
| 792 |
g_print ("%s\n", url); |
| 793 |
} |
| 794 |
else |
| 795 |
{ |
| 796 |
g_print ("**********************************************************************\n" |
| 797 |
"Please connect a supported web browser to\n" |
| 798 |
"\n" |
| 799 |
" %s/qunit/base1/test-dbus.html\n" |
| 800 |
"\n" |
| 801 |
"and check that the test suite passes. Press Ctrl+C to exit.\n" |
| 802 |
"**********************************************************************\n" |
| 803 |
"\n", url); |
| 804 |
} |
| 805 |
|
| 806 |
g_free (url); |
| 807 |
} |
| 808 |
|
| 809 |
static gboolean name_acquired = FALSE; |
| 810 |
static gboolean second_acquired = FALSE; |
| 811 |
|
| 812 |
static void |
| 813 |
on_name_acquired (GDBusConnection *connection, |
| 814 |
const gchar *name, |
| 815 |
gpointer user_data) |
| 816 |
{ |
| 817 |
name_acquired = TRUE; |
| 818 |
if (name_acquired && second_acquired) |
| 819 |
server_ready (); |
| 820 |
} |
| 821 |
|
| 822 |
static void |
| 823 |
on_second_acquired (GDBusConnection *connection, |
| 824 |
const gchar *name, |
| 825 |
gpointer user_data) |
| 826 |
{ |
| 827 |
second_acquired = TRUE; |
| 828 |
if (name_acquired && second_acquired) |
| 829 |
server_ready (); |
| 830 |
} |
| 831 |
|
| 832 |
static void |
| 833 |
on_name_lost (GDBusConnection *connection, |
| 834 |
const gchar *name, |
| 835 |
gpointer user_data) |
| 836 |
{ |
| 837 |
|
| 838 |
} |
| 839 |
|
| 840 |
static gboolean |
| 841 |
on_new_direct_connection (GDBusServer *server, |
| 842 |
GDBusConnection *connection, |
| 843 |
gpointer unused) |
| 844 |
{ |
| 845 |
direct = mock_service_create_and_export (connection, "/otree"); |
| 846 |
direct_b = mock_service_create_and_export (connection, "/different"); |
| 847 |
g_dbus_connection_add_filter (connection, on_filter_func, NULL, NULL); |
| 848 |
return TRUE; |
| 849 |
} |
| 850 |
|
| 851 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 852 |
|
| 853 |
static void |
| 854 |
setup_path (const char *argv0) |
| 855 |
{ |
| 856 |
const gchar *old = g_getenv ("PATH"); |
| 857 |
gchar *dir = g_path_get_dirname (argv0); |
| 858 |
gchar *path; |
| 859 |
|
| 860 |
path = g_strdup_printf ("%s%s%s", dir, |
| 861 |
old ? ":" : "", |
| 862 |
old ? old : NULL); |
| 863 |
|
| 864 |
cockpit_test_setenv ("PATH", path); |
| 865 |
|
| 866 |
g_free (path); |
| 867 |
g_free (dir); |
| 868 |
} |
| 869 |
|
| 870 |
static gint |
| 871 |
exit_code_from_pipe_status (CockpitPipe *pipe) |
| 872 |
{ |
| 873 |
gint status; |
| 874 |
|
| 875 |
status = cockpit_pipe_exit_status (pipe); |
| 876 |
if (WIFEXITED (status)) |
| 877 |
return WEXITSTATUS (status); |
| 878 |
else if (status != 0) |
| 879 |
return 1; |
| 880 |
else |
| 881 |
return 0; |
| 882 |
} |
| 883 |
|
| 884 |
static void |
| 885 |
on_bridge_done (CockpitPipe *pipe, |
| 886 |
const gchar *problem, |
| 887 |
gpointer user_data) |
| 888 |
{ |
| 889 |
exit_code = exit_code_from_pipe_status (pipe); |
| 890 |
g_main_loop_quit (loop); |
| 891 |
} |
| 892 |
|
| 893 |
static gboolean |
| 894 |
on_signal_done (gpointer data) |
| 895 |
{ |
| 896 |
gboolean first = !signalled; |
| 897 |
signalled = TRUE; |
| 898 |
|
| 899 |
if (first) |
| 900 |
{ |
| 901 |
if (service) |
| 902 |
cockpit_web_service_disconnect (service); |
| 903 |
if (bridge) |
| 904 |
{ |
| 905 |
if (!cockpit_pipe_is_closed (bridge)) |
| 906 |
{ |
| 907 |
g_signal_connect (bridge, "close", G_CALLBACK (on_bridge_done), NULL); |
| 908 |
return TRUE; |
| 909 |
} |
| 910 |
|
| 911 |
exit_code = exit_code_from_pipe_status (bridge); |
| 912 |
} |
| 913 |
} |
| 914 |
|
| 915 |
g_main_loop_quit (loop); |
| 916 |
return TRUE; |
| 917 |
} |
| 918 |
|
| 919 |
int |
| 920 |
main (int argc, |
| 921 |
char *argv[]) |
| 922 |
{ |
| 923 |
GError *error = NULL; |
| 924 |
guint sig_term; |
| 925 |
guint sig_int; |
| 926 |
int i; |
| 927 |
gchar *rm_rf_argv[] = {"rm", "-rfv", NULL, NULL}; |
| 928 |
|
| 929 |
GOptionEntry entries[] = { |
| 930 |
{ NULL } |
| 931 |
}; |
| 932 |
|
| 933 |
g_log_writer_default_set_use_stderr (TRUE); |
| 934 |
|
| 935 |
signal (SIGPIPE, SIG_IGN); |
| 936 |
/* avoid gvfs (http://bugzilla.gnome.org/show_bug.cgi?id=526454) */ |
| 937 |
cockpit_test_setenv ("GIO_USE_VFS", "local"); |
| 938 |
|
| 939 |
/* playground config directory */ |
| 940 |
g_autofree gchar *config_dir = g_dir_make_tmp ("cockpit.config.XXXXXX", NULL); |
| 941 |
g_assert (config_dir); |
| 942 |
g_autofree gchar *machines_dir = g_build_filename (config_dir, "cockpit", "machines.d", NULL); |
| 943 |
g_assert (g_mkdir_with_parents (machines_dir, 0755) == 0); |
| 944 |
|
| 945 |
cockpit_test_setenv ("PYTHONPATH", SRCDIR "/src"); |
| 946 |
cockpit_test_setenv ("XDG_DATA_HOME", SRCDIR "/test/data/mock-resource/home"); |
| 947 |
cockpit_test_setenv ("XDG_DATA_DIRS", SRCDIR "/test/data/mock-resource/system"); |
| 948 |
cockpit_test_setenv ("XDG_CONFIG_DIRS", config_dir); |
| 949 |
|
| 950 |
setup_path (argv[0]); |
| 951 |
|
| 952 |
g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_ERROR); |
| 953 |
|
| 954 |
sig_term = g_unix_signal_add (SIGTERM, on_signal_done, NULL); |
| 955 |
sig_int = g_unix_signal_add (SIGINT, on_signal_done, NULL); |
| 956 |
|
| 957 |
// System cockpit configuration file should not be loaded |
| 958 |
cockpit_config_file = NULL; |
| 959 |
|
| 960 |
g_autoptr(GOptionContext) context = g_option_context_new ("- test dbus json server"); |
| 961 |
g_option_context_add_main_entries (context, entries, NULL); |
| 962 |
g_option_context_set_ignore_unknown_options (context, TRUE); |
| 963 |
if (!g_option_context_parse (context, &argc, &argv, &error)) |
| 964 |
{ |
| 965 |
g_printerr ("test-server: %s\n", error->message); |
| 966 |
exit (2); |
| 967 |
} |
| 968 |
|
| 969 |
/* This isolates us from affecting other processes during tests */ |
| 970 |
g_autoptr(GTestDBus) bus = g_test_dbus_new (G_TEST_DBUS_NONE); |
| 971 |
g_test_dbus_up (bus); |
| 972 |
bus_address = g_test_dbus_get_bus_address (bus); |
| 973 |
|
| 974 |
g_autofree gchar *guid = g_dbus_generate_guid (); |
| 975 |
g_autoptr(GDBusServer) direct_dbus_server = g_dbus_server_new_sync ( |
| 976 |
"unix:tmpdir=/tmp", |
| 977 |
G_DBUS_SERVER_FLAGS_NONE, |
| 978 |
guid, |
| 979 |
NULL, |
| 980 |
NULL, |
| 981 |
&error); |
| 982 |
if (direct_dbus_server == NULL) |
| 983 |
{ |
| 984 |
g_printerr ("test-server: %s\n", error->message); |
| 985 |
exit (3); |
| 986 |
} |
| 987 |
|
| 988 |
/* Skip the program name */ |
| 989 |
argc--; |
| 990 |
argv++; |
| 991 |
|
| 992 |
/* Null terminate the bridge command line */ |
| 993 |
bridge_argv = g_new0 (char *, argc + 4); |
| 994 |
for (i = 0; i < argc; i++) |
| 995 |
bridge_argv[i] = argv[i]; |
| 996 |
|
| 997 |
/* Default case */ |
| 998 |
if (i == 0) { |
| 999 |
bridge_argv[i++] = "python3"; |
| 1000 |
bridge_argv[i++] = "-m"; |
| 1001 |
bridge_argv[i++] = "cockpit.bridge"; |
| 1002 |
} |
| 1003 |
|
| 1004 |
loop = g_main_loop_new (NULL, FALSE); |
| 1005 |
|
| 1006 |
g_bus_own_name (G_BUS_TYPE_SESSION, |
| 1007 |
"com.redhat.Cockpit.DBusTests.Test", |
| 1008 |
G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT | G_BUS_NAME_OWNER_FLAGS_REPLACE, |
| 1009 |
on_bus_acquired, |
| 1010 |
on_name_acquired, |
| 1011 |
on_name_lost, |
| 1012 |
loop, |
| 1013 |
NULL); |
| 1014 |
|
| 1015 |
g_bus_own_name (G_BUS_TYPE_SESSION, |
| 1016 |
"com.redhat.Cockpit.DBusTests.Second", |
| 1017 |
G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT | G_BUS_NAME_OWNER_FLAGS_REPLACE, |
| 1018 |
NULL, |
| 1019 |
on_second_acquired, |
| 1020 |
on_name_lost, |
| 1021 |
loop, |
| 1022 |
NULL); |
| 1023 |
|
| 1024 |
g_signal_connect_object (direct_dbus_server, |
| 1025 |
"new-connection", |
| 1026 |
G_CALLBACK (on_new_direct_connection), |
| 1027 |
NULL, 0); |
| 1028 |
g_dbus_server_start (direct_dbus_server); |
| 1029 |
direct_address = g_dbus_server_get_client_address (direct_dbus_server); |
| 1030 |
|
| 1031 |
g_main_loop_run (loop); |
| 1032 |
|
| 1033 |
g_source_remove (sig_term); |
| 1034 |
g_source_remove (sig_int); |
| 1035 |
|
| 1036 |
g_clear_object (&bridge); |
| 1037 |
g_clear_object (&exported); |
| 1038 |
g_clear_object (&exported_b); |
| 1039 |
g_clear_object (&direct); |
| 1040 |
g_clear_object (&direct_b); |
| 1041 |
g_main_loop_unref (loop); |
| 1042 |
|
| 1043 |
g_strfreev (server_roots); |
| 1044 |
g_test_dbus_down (bus); |
| 1045 |
g_free (bridge_argv); |
| 1046 |
|
| 1047 |
/* clean up temporary config dir */ |
| 1048 |
rm_rf_argv[2] = config_dir; |
| 1049 |
g_spawn_sync (NULL, rm_rf_argv, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL); |
| 1050 |
|
| 1051 |
return exit_code; |
| 1052 |
} |