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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
#include "test-utils.h"
#include "soup-message-private.h"
#include "soup-connection.h"
#include "soup-uri-utils-private.h"
static gboolean slow_https;
static void
message_finished (SoupMessage *msg, gpointer user_data)
{
gboolean *finished = user_data;
*finished = TRUE;
}
static void
message_starting_cb (SoupMessage *msg,
GSocket **ret)
{
SoupConnection *conn = soup_message_get_connection (msg);
*ret = soup_connection_get_socket (conn);
}
static void
request_queued_cb (SoupSession *session,
SoupMessage *msg,
GSocket **ret)
{
g_signal_connect (msg, "starting",
G_CALLBACK (message_starting_cb),
ret);
}
static void
do_message_to_session (SoupSession *session, GUri *uri,
const char *comment, guint expected_status)
{
SoupMessage *msg;
GBytes *body;
gboolean finished = FALSE;
GError *error = NULL;
if (comment)
debug_printf (1, " msg %s\n", comment);
msg = soup_message_new_from_uri ("GET", uri);
g_signal_connect (msg, "finished",
G_CALLBACK (message_finished), &finished);
body = soup_test_session_async_send (session, msg, NULL, &error);
if (expected_status != SOUP_STATUS_NONE) {
g_assert_no_error (error);
soup_test_assert_message_status (msg, expected_status);
g_assert_true (soup_message_is_keepalive (msg));
} else {
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
}
g_clear_error (&error);
g_assert_true (finished);
g_signal_handlers_disconnect_by_func (msg,
G_CALLBACK (message_finished),
&finished);
g_clear_pointer (&body, g_bytes_unref);
g_object_unref (msg);
}
static void
do_msg_tests_for_session (SoupSession *timeout_session,
SoupSession *idle_session,
SoupSession *plain_session,
GUri *fast_uri,
GUri *slow_uri)
{
GSocket *ret, *idle_first = NULL, *idle_second;
GSocket *plain_first = NULL, *plain_second;
if (idle_session) {
g_signal_connect (idle_session, "request-queued",
G_CALLBACK (request_queued_cb), &ret);
do_message_to_session (idle_session, fast_uri, "fast to idle", SOUP_STATUS_OK);
idle_first = g_object_ref (ret);
}
if (plain_session) {
g_signal_connect (plain_session, "request-queued",
G_CALLBACK (request_queued_cb), &ret);
do_message_to_session (plain_session, fast_uri, "fast to plain", SOUP_STATUS_OK);
plain_first = g_object_ref (ret);
}
do_message_to_session (timeout_session, fast_uri, "fast to timeout", SOUP_STATUS_OK);
do_message_to_session (timeout_session, slow_uri, "slow to timeout", SOUP_STATUS_NONE);
if (idle_session) {
do_message_to_session (idle_session, fast_uri, "fast to idle", SOUP_STATUS_OK);
idle_second = ret;
g_signal_handlers_disconnect_by_func (idle_session,
(gpointer)request_queued_cb,
&ret);
soup_test_assert (idle_first != idle_second,
"idle_session did not close first connection");
g_object_unref (idle_first);
}
if (plain_session) {
do_message_to_session (plain_session, fast_uri, "fast to plain", SOUP_STATUS_OK);
plain_second = ret;
g_signal_handlers_disconnect_by_func (plain_session,
(gpointer)request_queued_cb,
&ret);
soup_test_assert (plain_first == plain_second,
"plain_session closed connection");
g_object_unref (plain_first);
}
}
static void
do_async_timeout_tests (gconstpointer data)
{
SoupSession *timeout_session, *idle_session, *plain_session;
GUri *fast_uri = (GUri *)data;
GUri *slow_uri = g_uri_parse_relative (fast_uri, "/slow", SOUP_HTTP_URI_FLAGS, NULL);
gboolean extra_slow;
if (soup_uri_is_https (fast_uri)) {
SOUP_TEST_SKIP_IF_NO_TLS;
extra_slow = slow_https;
} else
extra_slow = FALSE;
timeout_session = soup_test_session_new ("timeout", extra_slow ? 3 : 1, NULL);
idle_session = soup_test_session_new ("idle-timeout", extra_slow ? 2 : 1, NULL);
/* The "plain" session also has an idle timeout, but it's longer
* than the test takes, so for our purposes it should behave like
* it has no timeout.
*/
plain_session = soup_test_session_new ("idle-timeout", 20, NULL);
do_msg_tests_for_session (timeout_session, idle_session, plain_session,
fast_uri, slow_uri);
soup_test_session_abort_unref (timeout_session);
soup_test_session_abort_unref (idle_session);
soup_test_session_abort_unref (plain_session);
g_uri_unref (slow_uri);
}
static void
do_sync_timeout_tests (gconstpointer data)
{
SoupSession *timeout_session, *plain_session;
GUri *fast_uri = (GUri *)data;
GUri *slow_uri = g_uri_parse_relative (fast_uri, "/slow", SOUP_HTTP_URI_FLAGS, NULL);
gboolean extra_slow;
if (soup_uri_is_https (fast_uri)) {
SOUP_TEST_SKIP_IF_NO_TLS;
extra_slow = slow_https;
} else
extra_slow = FALSE;
timeout_session = soup_test_session_new ("timeout", extra_slow ? 3 : 1, NULL);
/* SoupSession:timeout doesn't work with sync sessions */
plain_session = soup_test_session_new (NULL);
do_msg_tests_for_session (timeout_session, NULL, plain_session, fast_uri, slow_uri);
soup_test_session_abort_unref (timeout_session);
soup_test_session_abort_unref (plain_session);
g_uri_unref (slow_uri);
}
static gboolean
timeout_finish_message (gpointer msg)
{
soup_server_message_unpause (msg);
g_object_unref (msg);
return FALSE;
}
static void
server_handler (SoupServer *server,
SoupServerMessage *msg,
const char *path,
GHashTable *query,
gpointer user_data)
{
soup_server_message_set_status (msg, SOUP_STATUS_OK, NULL);
soup_server_message_set_response (msg, "text/plain",
SOUP_MEMORY_STATIC,
"ok\r\n", 4);
if (!strcmp (path, "/slow")) {
GSource *timeout;
soup_server_message_pause (msg);
timeout = soup_add_timeout (g_main_context_get_thread_default (),
4000, timeout_finish_message, g_object_ref (msg));
g_source_unref (timeout);
}
}
int
main (int argc, char **argv)
{
SoupServer *server, *https_server = NULL;
GUri *uri, *https_uri = NULL;
int ret;
test_init (argc, argv, NULL);
server = soup_test_server_new (SOUP_TEST_SERVER_IN_THREAD);
soup_server_add_handler (server, NULL, server_handler, NULL, NULL);
uri = soup_test_server_get_uri (server, "http", NULL);
if (tls_available) {
SoupSession *test_session;
gint64 start, end;
https_server = soup_test_server_new (SOUP_TEST_SERVER_IN_THREAD);
soup_server_add_handler (https_server, NULL, server_handler, NULL, NULL);
https_uri = soup_test_server_get_uri (server, "https", "127.0.0.1");
/* The 1-second timeouts are too fast for some machines... */
test_session = soup_test_session_new (NULL);
start = g_get_monotonic_time ();
do_message_to_session (test_session, uri, NULL, SOUP_STATUS_OK);
end = g_get_monotonic_time ();
soup_test_session_abort_unref (test_session);
debug_printf (2, " (https request took %0.3fs)\n", (end - start) / 1000000.0);
if (end - start > 750000) {
debug_printf (1, " (using extra-slow mode)\n\n");
slow_https = TRUE;
} else {
debug_printf (2, "\n");
slow_https = FALSE;
}
} else
https_uri = g_uri_parse ("https://fail.", SOUP_HTTP_URI_FLAGS, NULL);
g_test_add_data_func ("/timeout/http/async", uri, do_async_timeout_tests);
g_test_add_data_func ("/timeout/http/sync", uri, do_sync_timeout_tests);
g_test_add_data_func ("/timeout/https/async", https_uri, do_async_timeout_tests);
g_test_add_data_func ("/timeout/https/sync", https_uri, do_sync_timeout_tests);
ret = g_test_run ();
g_uri_unref (uri);
g_uri_unref (https_uri);
soup_test_server_quit_unref (server);
if (https_server)
soup_test_server_quit_unref (https_server);
test_cleanup ();
return ret;
}
|