1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
#include "test-utils.h"
#include "soup-misc.h"
#include "soup-session-private.h"
#include "soup-server-private.h"
#include <glib/gprintf.h>
#ifdef G_OS_UNIX
#include <gio/gunixsocketaddress.h>
#endif
#include <locale.h>
#include <signal.h>
#ifdef HAVE_APACHE
static gboolean apache_running;
static char *server_root = NULL;
#endif
static SoupLogger *logger;
static GBytes *index_buffer;
int debug_level;
gboolean expect_warning, tls_available;
static int http_debug_level;
static gboolean
increment_debug_level (const char *option_name, const char *value,
gpointer data, GError **error)
{
debug_level++;
return TRUE;
}
static gboolean
increment_http_debug_level (const char *option_name, const char *value,
gpointer data, GError **error)
{
http_debug_level++;
return TRUE;
}
static GOptionEntry debug_entry[] = {
{ "debug", 'd', G_OPTION_FLAG_NO_ARG,
G_OPTION_ARG_CALLBACK, increment_debug_level,
"Enable (or increase) test-specific debugging", NULL },
{ "http-debug", 'H', G_OPTION_FLAG_NO_ARG,
G_OPTION_ARG_CALLBACK, increment_http_debug_level,
"Enable (or increase) HTTP-level debugging", NULL },
{ NULL }
};
static void
quit (int sig)
{
#ifdef HAVE_APACHE
if (apache_running)
apache_cleanup ();
#endif
exit (1);
}
void
test_init (int argc, char **argv, GOptionEntry *entries)
{
GOptionContext *opts;
char *name;
GError *error = NULL;
GTlsBackend *tls_backend;
setlocale (LC_ALL, "");
g_setenv ("GSETTINGS_BACKEND", "memory", TRUE);
g_setenv ("GIO_USE_PROXY_RESOLVER", "dummy", TRUE);
g_setenv ("GIO_USE_VFS", "local", TRUE);
name = strrchr (argv[0], '/');
if (!name++)
name = argv[0];
if (!strncmp (name, "lt-", 3))
name += 3;
g_set_prgname (name);
g_test_init (&argc, &argv, NULL);
g_test_set_nonfatal_assertions ();
g_test_bug_base ("https://bugzilla.gnome.org/");
opts = g_option_context_new (NULL);
g_option_context_add_main_entries (opts, debug_entry, NULL);
if (entries)
g_option_context_add_main_entries (opts, entries, NULL);
if (!g_option_context_parse (opts, &argc, &argv, &error)) {
g_printerr ("Could not parse arguments: %s\n",
error->message);
g_printerr ("%s",
g_option_context_get_help (opts, TRUE, NULL));
exit (1);
}
g_option_context_free (opts);
/* Exit cleanly on ^C in case we're valgrinding. */
signal (SIGINT, quit);
tls_backend = g_tls_backend_get_default ();
tls_available = g_tls_backend_supports_tls (tls_backend);
}
void
test_cleanup (void)
{
#ifdef HAVE_APACHE
if (apache_running)
apache_cleanup ();
#endif
if (logger)
g_object_unref (logger);
if (index_buffer)
g_bytes_unref (index_buffer);
g_main_context_unref (g_main_context_default ());
debug_printf (1, "\n");
}
void
debug_printf (int level, const char *format, ...)
{
va_list args;
static char last_char = '\n';
if (debug_level < level)
return;
/* Very hacky solution to outputing in the TAP format,
* every line starts with # */
if (last_char == '\n')
g_printf ("# ");
last_char = format[strlen (format) - 1];
va_start (args, format);
g_vprintf (format, args);
va_end (args);
fflush (stdout);
}
gboolean
have_curl(void) {
char *found;
found = g_find_program_in_path ("curl");
if (found) {
g_free (found);
return TRUE;
} else {
return FALSE;
}
}
#ifdef HAVE_APACHE
static gboolean
apache_cmd (const char *cmd)
{
GPtrArray *argv;
char *cwd, *pid_file, *error_log;
#ifdef HAVE_APACHE_2_4
char *default_runtime_dir;
#endif
int status;
gboolean ok;
GString *str;
guint i;
if (server_root == NULL) {
g_test_message ("Server root not initialized");
return FALSE;
}
cwd = g_get_current_dir ();
#ifdef HAVE_APACHE_2_4
default_runtime_dir = g_strdup_printf ("DefaultRuntimeDir %s", cwd);
#endif
pid_file = g_strdup_printf ("PidFile %s/httpd.pid", cwd);
error_log = g_strdup_printf ("ErrorLog %s/error.log", cwd);
argv = g_ptr_array_new ();
g_ptr_array_add (argv, APACHE_HTTPD);
g_ptr_array_add (argv, "-d");
g_ptr_array_add (argv, server_root);
g_ptr_array_add (argv, "-f");
g_ptr_array_add (argv, "httpd.conf");
#ifdef HAVE_APACHE_2_4
g_ptr_array_add (argv, "-c");
g_ptr_array_add (argv, default_runtime_dir);
#endif
g_ptr_array_add (argv, "-c");
g_ptr_array_add (argv, error_log);
g_ptr_array_add (argv, "-c");
g_ptr_array_add (argv, pid_file);
g_ptr_array_add (argv, "-k");
g_ptr_array_add (argv, (char *)cmd);
g_ptr_array_add (argv, NULL);
str = g_string_new ("Apache command:");
for (i = 0; i < argv->len - 1; i++) {
char *escaped = g_shell_quote (argv->pdata[i]);
g_string_append_c (str, ' ');
g_string_append (str, escaped);
g_free (escaped);
}
g_test_message ("%s", str->str);
g_string_free (str, TRUE);
ok = g_spawn_sync (cwd, (char **)argv->pdata, NULL, 0, NULL, NULL,
NULL, NULL, &status, NULL);
if (ok)
ok = (status == 0);
g_free (cwd);
g_free (pid_file);
g_free (error_log);
#ifdef HAVE_APACHE_2_4
g_free (default_runtime_dir);
#endif
g_ptr_array_free (argv, TRUE);
g_test_message (ok ? "-> success" : "-> failed");
return ok;
}
void
apache_init (void)
{
g_test_message ("[%f] enter %s", g_get_monotonic_time () / 1e6, G_STRFUNC);
/* Set this environment variable if you are already running a
* suitably-configured Apache server */
if (g_getenv ("SOUP_TESTS_ALREADY_RUNNING_APACHE"))
goto out;
server_root = soup_test_build_filename_abs (G_TEST_BUILT, "", NULL);
if (!apache_cmd ("start")) {
g_printerr ("Could not start apache\n");
exit (1);
}
apache_running = TRUE;
out:
g_test_message ("[%f] leave %s", g_get_monotonic_time () / 1e6, G_STRFUNC);
}
void
apache_cleanup (void)
{
pid_t pid;
char *contents;
g_test_message ("[%f] enter %s", g_get_monotonic_time () / 1e6, G_STRFUNC);
if (g_file_get_contents ("httpd.pid", &contents, NULL, NULL)) {
pid = strtoul (contents, NULL, 10);
g_free (contents);
} else
pid = 0;
if (!apache_cmd ("graceful-stop")) {
g_printerr ("Could not stop Apache\n");
goto out;
}
apache_running = FALSE;
if (pid) {
while (kill (pid, 0) == 0)
g_usleep (100);
}
if (g_file_get_contents ("error.log", &contents, NULL, NULL)) {
g_test_message ("error.log contents:\n%s", contents);
g_free (contents);
}
g_clear_pointer (&server_root, g_free);
out:
g_test_message ("[%f] leave %s", g_get_monotonic_time () / 1e6, G_STRFUNC);
}
#endif /* HAVE_APACHE */
SoupSession *
soup_test_session_new (const char *propname, ...)
{
va_list args;
SoupSession *session;
GTlsDatabase *tlsdb;
char *cafile;
GError *error = NULL;
va_start (args, propname);
session = (SoupSession *)g_object_new_valist (SOUP_TYPE_SESSION, propname, args);
va_end (args);
if (tls_available) {
char *abs_cafile;
cafile = g_test_build_filename (G_TEST_DIST, "test-cert.pem", NULL);
abs_cafile = g_canonicalize_filename (cafile, NULL);
g_free (cafile);
tlsdb = g_tls_file_database_new (abs_cafile, &error);
g_free (abs_cafile);
if (error) {
if (g_strcmp0 (g_getenv ("GIO_USE_TLS"), "dummy") == 0)
g_clear_error (&error);
else
g_assert_no_error (error);
}
soup_session_set_tls_database (session, tlsdb);
g_clear_object (&tlsdb);
}
if (http_debug_level && !logger) {
SoupLoggerLogLevel level = MIN ((SoupLoggerLogLevel)http_debug_level, SOUP_LOGGER_LOG_BODY);
logger = soup_logger_new (level);
}
if (logger)
soup_session_add_feature (session, SOUP_SESSION_FEATURE (logger));
return session;
}
void
soup_test_session_abort_unref (SoupSession *session)
{
soup_session_abort (session);
g_assert_cmpint (G_OBJECT (session)->ref_count, ==, 1);
g_object_unref (session);
}
typedef struct {
SoupMessage *msg;
GBytes *body;
GError *error;
gboolean done;
gboolean message_finished;
} SendAsyncData;
static void
send_and_read_async_ready_cb (SoupSession *session,
GAsyncResult *result,
SendAsyncData *data)
{
data->done = TRUE;
g_assert_true (soup_session_get_async_result_message (session, result) == data->msg);
data->body = soup_session_send_and_read_finish (session, result, &data->error);
if (g_error_matches (data->error, SOUP_SESSION_ERROR, SOUP_SESSION_ERROR_MESSAGE_ALREADY_IN_QUEUE))
data->message_finished = TRUE;
}
static void
on_message_finished (SoupMessage *msg,
SendAsyncData *data)
{
data->message_finished = TRUE;
}
GBytes *
soup_test_session_async_send (SoupSession *session,
SoupMessage *msg,
GCancellable *cancellable,
GError **error)
{
GMainContext *async_context = g_main_context_ref_thread_default ();
gulong signal_id;
SendAsyncData data = { msg, NULL, NULL, FALSE, FALSE };
signal_id = g_signal_connect (msg, "finished",
G_CALLBACK (on_message_finished), &data);
soup_session_send_and_read_async (session, msg, G_PRIORITY_DEFAULT, cancellable,
(GAsyncReadyCallback)send_and_read_async_ready_cb, &data);
while (!data.done || !data.message_finished)
g_main_context_iteration (async_context, TRUE);
g_signal_handler_disconnect (msg, signal_id);
if (data.error)
g_propagate_error (error, data.error);
g_main_context_unref (async_context);
return data.body;
}
guint
soup_test_session_send_message (SoupSession *session,
SoupMessage *msg)
{
GInputStream *stream;
stream = soup_session_send (session, msg, NULL, NULL);
if (stream)
g_object_unref (stream);
return soup_message_get_status (msg);
}
const char *
soup_test_server_get_unix_path (SoupServer *server)
{
return g_object_get_data (G_OBJECT (server), "unix-socket-path");
}
static void
server_listen (SoupServer *server)
{
GError *error = NULL;
SoupServerListenOptions options = 0;
GSocket *socket;
if (g_getenv ("SOUP_TEST_NO_IPV6"))
options = SOUP_SERVER_LISTEN_IPV4_ONLY;
socket = g_object_get_data (G_OBJECT (server), "listen-socket");
if (socket != NULL)
soup_server_listen_socket (server, socket, 0, &error);
else
soup_server_listen_local (server, 0, options, &error);
if (error) {
g_printerr ("Unable to create server: %s\n", error->message);
exit (1);
}
}
static GMutex server_start_mutex;
static GCond server_start_cond;
static gpointer
run_server_thread (gpointer user_data)
{
SoupServer *server = user_data;
SoupTestServerOptions options =
GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (server), "options"));
GMainContext *context;
GMainLoop *loop;
context = g_main_context_new ();
g_main_context_push_thread_default (context);
loop = g_main_loop_new (context, FALSE);
g_object_set_data (G_OBJECT (server), "GMainLoop", loop);
if (!(options & SOUP_TEST_SERVER_NO_DEFAULT_LISTENER))
server_listen (server);
g_mutex_lock (&server_start_mutex);
g_cond_signal (&server_start_cond);
g_mutex_unlock (&server_start_mutex);
g_main_loop_run (loop);
g_main_loop_unref (loop);
soup_server_disconnect (server);
g_main_context_pop_thread_default (context);
g_main_context_unref (context);
return NULL;
}
void
soup_test_server_run_in_thread (SoupServer *server)
{
GThread *thread;
g_mutex_lock (&server_start_mutex);
thread = g_thread_new ("server_thread", run_server_thread, server);
g_cond_wait (&server_start_cond, &server_start_mutex);
g_mutex_unlock (&server_start_mutex);
g_object_set_data (G_OBJECT (server), "thread", thread);
}
SoupServer *
soup_test_server_new (SoupTestServerOptions options)
{
SoupServer *server;
GTlsCertificate *cert = NULL;
GError *error = NULL;
if (tls_available) {
char *ssl_cert_file, *ssl_key_file;
ssl_cert_file = g_test_build_filename (G_TEST_DIST, "test-cert.pem", NULL);
ssl_key_file = g_test_build_filename (G_TEST_DIST, "test-key.pem", NULL);
cert = g_tls_certificate_new_from_files (ssl_cert_file,
ssl_key_file,
&error);
g_free (ssl_cert_file);
g_free (ssl_key_file);
if (error) {
g_printerr ("Unable to create server: %s\n", error->message);
exit (1);
}
}
server = soup_server_new ("tls-certificate", cert,
NULL);
g_clear_object (&cert);
soup_server_set_http2_enabled (server, options & SOUP_TEST_SERVER_HTTP2);
g_object_set_data (G_OBJECT (server), "options", GUINT_TO_POINTER (options));
if (options & SOUP_TEST_SERVER_UNIX_SOCKET) {
#ifdef G_OS_UNIX
char *socket_dir, *socket_path;
GSocket *listen_socket;
GSocketAddress *listen_address;
socket_dir = g_dir_make_tmp ("unix-socket-test-XXXXXX", NULL);
socket_path = g_build_filename (socket_dir, "socket", NULL);
g_object_set_data_full (G_OBJECT (server), "unix-socket-path", socket_path, g_free);
g_free (socket_dir);
listen_socket = g_socket_new (G_SOCKET_FAMILY_UNIX,
G_SOCKET_TYPE_STREAM,
G_SOCKET_PROTOCOL_DEFAULT,
&error);
if (listen_socket == NULL) {
g_printerr ("Unable to create unix socket: %s\n", error->message);
exit (1);
}
listen_address = g_unix_socket_address_new (socket_path);
if (!g_socket_bind (listen_socket, listen_address, TRUE, &error)) {
g_printerr ("Unable to bind unix socket to %s: %s\n", socket_path, error->message);
exit (1);
}
g_object_unref (listen_address);
if (!g_socket_listen (listen_socket, &error)) {
g_printerr ("Unable to listen on unix socket: %s\n", error->message);
exit (1);
}
g_object_set_data_full (G_OBJECT (server), "listen-socket", listen_socket, g_object_unref);
#else
g_printerr ("Unix socket support not available\n");
exit (1);
#endif
}
if (options & SOUP_TEST_SERVER_IN_THREAD)
soup_test_server_run_in_thread (server);
else if (!(options & SOUP_TEST_SERVER_NO_DEFAULT_LISTENER))
server_listen (server);
return server;
}
static GUri *
find_server_uri (SoupServer *server, const char *scheme, const char *host)
{
GSList *uris, *u;
GUri *uri, *ret_uri = NULL;
uris = soup_server_get_uris (server);
for (u = uris; u; u = u->next) {
uri = u->data;
if (scheme && strcmp (g_uri_get_scheme (uri), scheme) != 0)
continue;
if (host && strcmp (g_uri_get_host (uri), host) != 0)
continue;
ret_uri = g_uri_ref (uri);
break;
}
g_slist_free_full (uris, (GDestroyNotify)g_uri_unref);
return ret_uri;
}
static GUri *
add_listener (SoupServer *server, const char *scheme, const char *host)
{
SoupServerListenOptions options = 0;
GError *error = NULL;
if (!g_strcmp0 (scheme, "https"))
options |= SOUP_SERVER_LISTEN_HTTPS;
if (!g_strcmp0 (host, "127.0.0.1") || g_getenv ("SOUP_TEST_NO_IPV6"))
options |= SOUP_SERVER_LISTEN_IPV4_ONLY;
else if (!g_strcmp0 (host, "::1"))
options |= SOUP_SERVER_LISTEN_IPV6_ONLY;
soup_server_listen_local (server, 0, options, &error);
g_assert_no_error (error);
return find_server_uri (server, scheme, host);
}
typedef struct {
GMutex mutex;
GCond cond;
SoupServer *server;
const char *scheme;
const char *host;
GUri *uri;
} AddListenerData;
static gboolean
add_listener_in_thread (gpointer user_data)
{
AddListenerData *data = user_data;
GUri *uri;
uri = add_listener (data->server, data->scheme, data->host);
g_mutex_lock (&data->mutex);
data->uri = uri;
g_cond_signal (&data->cond);
g_mutex_unlock (&data->mutex);
return FALSE;
}
GUri *
soup_test_server_get_uri (SoupServer *server,
const char *scheme,
const char *host)
{
GUri *uri;
GMainLoop *loop;
uri = find_server_uri (server, scheme, host);
if (uri)
return uri;
/* Need to add a new listener */
loop = g_object_get_data (G_OBJECT (server), "GMainLoop");
if (loop) {
GMainContext *context = g_main_loop_get_context (loop);
AddListenerData data;
g_mutex_init (&data.mutex);
g_cond_init (&data.cond);
data.server = server;
data.scheme = scheme;
data.host = host;
data.uri = NULL;
soup_add_completion (context, add_listener_in_thread, &data);
g_mutex_lock (&data.mutex);
while (!data.uri)
g_cond_wait (&data.cond, &data.mutex);
g_mutex_unlock (&data.mutex);
g_mutex_clear (&data.mutex);
g_cond_clear (&data.cond);
uri = data.uri;
} else
uri = add_listener (server, scheme, host);
return uri;
}
static gboolean
done_waiting (gpointer user_data)
{
gboolean *done = user_data;
*done = TRUE;
return FALSE;
}
static void
disconnect_and_wait (SoupServer *server,
GMainContext *context)
{
GSource *source;
gboolean done = FALSE;
source = g_idle_source_new ();
g_source_set_priority (source, G_PRIORITY_LOW);
g_source_set_callback (source, done_waiting, &done, NULL);
g_source_attach (source, context);
g_source_unref (source);
soup_server_disconnect (server);
while (!done)
g_main_context_iteration (context, TRUE);
}
static gboolean
idle_quit_server (gpointer user_data)
{
SoupServer *server = user_data;
GMainLoop *loop = g_object_get_data (G_OBJECT (server), "GMainLoop");
disconnect_and_wait (server, g_main_loop_get_context (loop));
g_main_loop_quit (loop);
return FALSE;
}
void
soup_test_server_quit_unref (SoupServer *server)
{
GThread *thread;
thread = g_object_get_data (G_OBJECT (server), "thread");
if (thread) {
GMainLoop *loop;
GMainContext *context;
loop = g_object_get_data (G_OBJECT (server), "GMainLoop");
context = g_main_loop_get_context (loop);
g_main_context_ref (context);
soup_add_completion (context, idle_quit_server, server);
g_main_context_unref (context);
g_thread_join (thread);
} else
disconnect_and_wait (server, NULL);
g_assert_cmpint (G_OBJECT (server)->ref_count, ==, 1);
g_object_unref (server);
}
typedef struct {
GMainLoop *loop;
GAsyncResult *result;
} AsyncAsSyncData;
static void
async_as_sync_callback (GObject *object,
GAsyncResult *result,
gpointer user_data)
{
AsyncAsSyncData *data = user_data;
data->result = g_object_ref (result);
g_main_loop_quit (data->loop);
}
static gboolean
cancel_request_timeout (GCancellable *cancellable)
{
g_cancellable_cancel (cancellable);
return FALSE;
}
GInputStream *
soup_test_request_send (SoupSession *session,
SoupMessage *msg,
GCancellable *cancellable,
guint flags,
GError **error)
{
AsyncAsSyncData data;
GInputStream *stream;
data.loop = g_main_loop_new (g_main_context_get_thread_default (), FALSE);
if (flags & SOUP_TEST_REQUEST_CANCEL_SOON || flags & SOUP_TEST_REQUEST_CANCEL_IMMEDIATE) {
guint interval = flags & SOUP_TEST_REQUEST_CANCEL_SOON ? 100 : 0;
g_timeout_add_full (G_PRIORITY_HIGH, interval,
(GSourceFunc)cancel_request_timeout,
g_object_ref (cancellable), g_object_unref);
}
if (flags & SOUP_TEST_REQUEST_CANCEL_PREEMPTIVE)
g_cancellable_cancel (cancellable);
soup_session_send_async (session, msg, G_PRIORITY_DEFAULT, cancellable,
async_as_sync_callback, &data);
g_main_loop_run (data.loop);
g_assert_true (soup_session_get_async_result_message (session, data.result) == msg);
stream = soup_session_send_finish (session, data.result, error);
if (flags & SOUP_TEST_REQUEST_CANCEL_AFTER_SEND_FINISH) {
if (flags & SOUP_TEST_REQUEST_CANCEL_BY_SESSION)
soup_session_cancel_message (session, msg);
else
g_cancellable_cancel (cancellable);
}
g_main_loop_unref (data.loop);
g_object_unref (data.result);
return stream;
}
gboolean
soup_test_request_read_all (GInputStream *stream,
GCancellable *cancellable,
GError **error)
{
char buf[8192];
AsyncAsSyncData data;
gsize nread;
data.loop = g_main_loop_new (g_main_context_get_thread_default (), FALSE);
do {
g_input_stream_read_async (stream, buf, sizeof (buf),
G_PRIORITY_DEFAULT, cancellable,
async_as_sync_callback, &data);
g_main_loop_run (data.loop);
nread = g_input_stream_read_finish (stream, data.result, error);
g_object_unref (data.result);
} while (nread > 0);
g_main_loop_unref (data.loop);
return nread == 0;
}
gboolean
soup_test_request_close_stream (GInputStream *stream,
GCancellable *cancellable,
GError **error)
{
AsyncAsSyncData data;
gboolean ok;
data.loop = g_main_loop_new (g_main_context_get_thread_default (), FALSE);
g_input_stream_close_async (stream, G_PRIORITY_DEFAULT, cancellable,
async_as_sync_callback, &data);
g_main_loop_run (data.loop);
ok = g_input_stream_close_finish (stream, data.result, error);
g_main_loop_unref (data.loop);
g_object_unref (data.result);
return ok;
}
void
soup_test_register_resources (void)
{
static gboolean registered = FALSE;
GResource *resource;
char *path;
GError *error = NULL;
if (registered)
return;
path = g_test_build_filename (G_TEST_BUILT, "soup-tests.gresource", NULL);
resource = g_resource_load (path, &error);
if (!resource) {
g_printerr ("Could not load resource soup-tests.gresource: %s\n",
error->message);
exit (1);
}
g_free (path);
g_resources_register (resource);
g_resource_unref (resource);
registered = TRUE;
}
GBytes *
soup_test_load_resource (const char *name,
GError **error)
{
GBytes *bytes;
char *path;
soup_test_register_resources ();
path = g_build_path ("/", "/org/gnome/libsoup/tests/resources", name, NULL);
bytes = g_resources_lookup_data (path, G_RESOURCE_LOOKUP_FLAGS_NONE, error);
g_free (path);
return bytes;
}
GBytes *
soup_test_get_index (void)
{
if (!index_buffer) {
char *path, *contents;
gsize length;
GError *error = NULL;
path = g_test_build_filename (G_TEST_DIST, "index.txt", NULL);
if (!g_file_get_contents (path, &contents, &length, &error)) {
g_printerr ("Could not read index.txt: %s\n",
error->message);
exit (1);
}
g_free (path);
index_buffer = g_bytes_new_take (contents, length);
}
return index_buffer;
}
char *
soup_test_build_filename_abs (GTestFileType file_type,
const gchar *first_path,
...)
{
const gchar *pathv[16];
gsize num_path_segments;
char *path;
char *path_abs;
va_list ap;
va_start (ap, first_path);
pathv[0] = g_test_get_dir (file_type);
pathv[1] = first_path;
for (num_path_segments = 2; num_path_segments < G_N_ELEMENTS (pathv); num_path_segments++) {
pathv[num_path_segments] = va_arg (ap, const char *);
if (pathv[num_path_segments] == NULL)
break;
}
va_end (ap);
g_assert_cmpint (num_path_segments, <, G_N_ELEMENTS (pathv));
path = g_build_filenamev ((gchar **) pathv);
if (g_path_is_absolute (path))
return path;
path_abs = g_canonicalize_filename (path, NULL);
g_free (path);
return path_abs;
}
#ifndef G_HAVE_ISO_VARARGS
void
soup_test_assert (gboolean expr, const char *fmt, ...)
{
char *message;
va_list args;
if (G_UNLIKELY (!expr)) {
va_start (args, fmt);
message = g_strdup_vprintf (fmt, args);
va_end (args);
g_assertion_message (G_LOG_DOMAIN,
"???", 0, "???"
message);
g_free (message);
}
}
#endif
|