1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2009 Gustavo Noronha Silva <gns@gnome.org>.
6 #include "test-utils.h"
10 SoupMessageBody *chunk_data;
13 server_callback (SoupServer *server, SoupMessage *msg,
14 const char *path, GHashTable *query,
15 SoupClientContext *context, gpointer data)
20 gsize length = 0, offset;
21 gboolean empty_response = FALSE;
23 if (msg->method != SOUP_METHOD_GET) {
24 soup_message_set_status (msg, SOUP_STATUS_NOT_IMPLEMENTED);
28 soup_message_set_status (msg, SOUP_STATUS_OK);
31 query_key = g_hash_table_lookup (query, "chunked");
32 if (query_key && g_str_equal (query_key, "yes")) {
33 soup_message_headers_set_encoding (msg->response_headers,
34 SOUP_ENCODING_CHUNKED);
37 query_key = g_hash_table_lookup (query, "empty_response");
38 if (query_key && g_str_equal (query_key, "yes"))
39 empty_response = TRUE;
42 if (!strcmp (path, "/mbox")) {
44 contents = g_strdup ("");
47 g_file_get_contents (SRCDIR "/resources/mbox",
53 g_error ("%s", error->message);
58 soup_message_headers_append (msg->response_headers,
59 "Content-Type", "text/plain");
62 if (g_str_has_prefix (path, "/text_or_binary/")) {
63 char *base_name = g_path_get_basename (path);
64 char *file_name = g_strdup_printf (SRCDIR "/resources/%s", base_name);
66 g_file_get_contents (file_name,
74 g_error ("%s", error->message);
79 soup_message_headers_append (msg->response_headers,
80 "Content-Type", "text/plain");
83 if (g_str_has_prefix (path, "/unknown/")) {
84 char *base_name = g_path_get_basename (path);
85 char *file_name = g_strdup_printf (SRCDIR "/resources/%s", base_name);
87 g_file_get_contents (file_name,
95 g_error ("%s", error->message);
100 soup_message_headers_append (msg->response_headers,
101 "Content-Type", "UNKNOWN/unknown");
104 if (g_str_has_prefix (path, "/type/")) {
105 char **components = g_strsplit (path, "/", 4);
108 char *base_name = g_path_get_basename (path);
109 char *file_name = g_strdup_printf (SRCDIR "/resources/%s", base_name);
111 g_file_get_contents (file_name,
119 g_error ("%s", error->message);
120 g_error_free (error);
124 /* Hack to allow passing type in the URI */
125 ptr = g_strrstr (components[2], "_");
128 soup_message_headers_append (msg->response_headers,
129 "Content-Type", components[2]);
130 g_strfreev (components);
133 if (g_str_has_prefix (path, "/multiple_headers/")) {
134 char *base_name = g_path_get_basename (path);
135 char *file_name = g_strdup_printf (SRCDIR "/resources/%s", base_name);
137 g_file_get_contents (file_name,
145 g_error ("%s", error->message);
146 g_error_free (error);
150 soup_message_headers_append (msg->response_headers,
151 "Content-Type", "text/xml");
152 soup_message_headers_append (msg->response_headers,
153 "Content-Type", "text/plain");
156 for (offset = 0; offset < length; offset += 500) {
157 soup_message_body_append (msg->response_body,
160 MIN(500, length - offset));
162 soup_message_body_complete (msg->response_body);
168 unpause_msg (gpointer data)
170 SoupMessage *msg = (SoupMessage*)data;
171 debug_printf (2, " unpause\n");
172 soup_session_unpause_message (session, msg);
178 content_sniffed (SoupMessage *msg, char *content_type, GHashTable *params, gpointer data)
180 gboolean should_pause = GPOINTER_TO_INT (data);
182 debug_printf (2, " content-sniffed -> %s\n", content_type);
184 if (g_object_get_data (G_OBJECT (msg), "got-chunk")) {
185 debug_printf (1, " got-chunk got emitted before content-sniffed\n");
189 g_object_set_data (G_OBJECT (msg), "content-sniffed", GINT_TO_POINTER (TRUE));
192 debug_printf (2, " pause\n");
193 soup_session_pause_message (session, msg);
194 g_idle_add (unpause_msg, msg);
199 got_headers (SoupMessage *msg, gpointer data)
201 gboolean should_pause = GPOINTER_TO_INT (data);
203 debug_printf (2, " got-headers\n");
205 if (g_object_get_data (G_OBJECT (msg), "content-sniffed")) {
206 debug_printf (1, " content-sniffed got emitted before got-headers\n");
210 g_object_set_data (G_OBJECT (msg), "got-headers", GINT_TO_POINTER (TRUE));
213 debug_printf (2, " pause\n");
214 soup_session_pause_message (session, msg);
215 g_idle_add (unpause_msg, msg);
220 got_chunk (SoupMessage *msg, SoupBuffer *chunk, gpointer data)
222 gboolean should_accumulate = GPOINTER_TO_INT (data);
224 debug_printf (2, " got-chunk\n");
226 g_object_set_data (G_OBJECT (msg), "got-chunk", GINT_TO_POINTER (TRUE));
228 if (!should_accumulate) {
230 chunk_data = soup_message_body_new ();
231 soup_message_body_append_buffer (chunk_data, chunk);
236 finished (SoupSession *session, SoupMessage *msg, gpointer data)
238 GMainLoop *loop = (GMainLoop*)data;
239 g_main_loop_quit (loop);
243 do_signals_test (gboolean should_content_sniff,
244 gboolean should_pause,
245 gboolean should_accumulate,
246 gboolean chunked_encoding,
247 gboolean empty_response)
249 SoupURI *uri = soup_uri_new_with_base (base_uri, "/mbox");
250 SoupMessage *msg = soup_message_new_from_uri ("GET", uri);
251 GMainLoop *loop = g_main_loop_new (NULL, TRUE);
254 GError *error = NULL;
255 SoupBuffer *body = NULL;
257 debug_printf (1, "do_signals_test(%ssniff, %spause, %saccumulate, %schunked, %sempty)\n",
258 should_content_sniff ? "" : "!",
259 should_pause ? "" : "!",
260 should_accumulate ? "" : "!",
261 chunked_encoding ? "" : "!",
262 empty_response ? "" : "!");
264 if (chunked_encoding)
265 soup_uri_set_query (uri, "chunked=yes");
267 if (empty_response) {
269 char *tmp = uri->query;
270 uri->query = g_strdup_printf ("%s&empty_response=yes", tmp);
273 soup_uri_set_query (uri, "empty_response=yes");
276 soup_message_set_uri (msg, uri);
278 soup_message_body_set_accumulate (msg->response_body, should_accumulate);
280 g_object_connect (msg,
281 "signal::got-headers", got_headers, GINT_TO_POINTER (should_pause),
282 "signal::got-chunk", got_chunk, GINT_TO_POINTER (should_accumulate),
283 "signal::content_sniffed", content_sniffed, GINT_TO_POINTER (should_pause),
287 soup_session_queue_message (session, msg, finished, loop);
289 g_main_loop_run (loop);
291 if (!should_content_sniff &&
292 g_object_get_data (G_OBJECT (msg), "content-sniffed")) {
293 debug_printf (1, " content-sniffed got emitted without a sniffer\n");
295 } else if (should_content_sniff &&
296 !g_object_get_data (G_OBJECT (msg), "content-sniffed")) {
297 debug_printf (1, " content-sniffed did not get emitted\n");
301 if (empty_response) {
302 contents = g_strdup ("");
305 g_file_get_contents (SRCDIR "/resources/mbox",
311 g_error ("%s", error->message);
312 g_error_free (error);
316 if (!should_accumulate && chunk_data)
317 body = soup_message_body_flatten (chunk_data);
318 else if (msg->response_body)
319 body = soup_message_body_flatten (msg->response_body);
321 if (body && body->length != length) {
322 debug_printf (1, " lengths do not match\n");
326 if (body && memcmp (body->data, contents, length)) {
327 debug_printf (1, " downloaded data does not match\n");
333 soup_buffer_free (body);
335 soup_message_body_free (chunk_data);
340 g_object_unref (msg);
341 g_main_loop_unref (loop);
345 sniffing_content_sniffed (SoupMessage *msg, const char *content_type,
346 GHashTable *params, gpointer data)
348 char **sniffed_type = (char **)data;
349 GString *full_header;
353 if (params == NULL) {
354 *sniffed_type = g_strdup (content_type);
358 full_header = g_string_new (content_type);
359 g_string_append (full_header, "; ");
361 keys = g_hash_table_get_keys (params);
362 for (iter = keys; iter != NULL; iter = iter->next) {
363 const gchar *value = (const gchar*) g_hash_table_lookup (params, iter->data);
365 soup_header_g_string_append_param (full_header,
366 (const gchar*) iter->data,
370 *sniffed_type = full_header->str;
372 g_string_free (full_header, FALSE);
377 test_sniffing (const char *path, const char *expected_type)
379 SoupURI *uri = soup_uri_new_with_base (base_uri, path);
380 SoupMessage *msg = soup_message_new_from_uri ("GET", uri);
381 GMainLoop *loop = g_main_loop_new (NULL, TRUE);
382 char *sniffed_type = NULL;
384 debug_printf (1, "test_sniffing(\"%s\", \"%s\")\n", path, expected_type);
386 g_signal_connect (msg, "content-sniffed",
387 G_CALLBACK (sniffing_content_sniffed), &sniffed_type);
391 soup_session_queue_message (session, msg, finished, loop);
393 g_main_loop_run (loop);
396 debug_printf (1, " message was not sniffed!\n");
398 } else if (strcmp (sniffed_type, expected_type) != 0) {
399 debug_printf (1, " sniffing failed! expected %s, got %s\n",
400 expected_type, sniffed_type);
403 g_free (sniffed_type);
406 g_object_unref (msg);
407 g_main_loop_unref (loop);
411 test_disabled (const char *path)
413 SoupURI *uri = soup_uri_new_with_base (base_uri, path);
414 SoupMessage *msg = soup_message_new_from_uri ("GET", uri);
415 GMainLoop *loop = g_main_loop_new (NULL, TRUE);
416 char *sniffed_type = NULL;
418 soup_message_disable_feature (msg, SOUP_TYPE_CONTENT_SNIFFER);
420 debug_printf (1, "test_disabled(\"%s\")\n", path);
422 g_signal_connect (msg, "content-sniffed",
423 G_CALLBACK (sniffing_content_sniffed), &sniffed_type);
427 soup_session_queue_message (session, msg, finished, loop);
429 g_main_loop_run (loop);
432 debug_printf (1, " message was sniffed!\n");
434 g_free (sniffed_type);
438 g_object_unref (msg);
439 g_main_loop_unref (loop);
443 main (int argc, char **argv)
447 test_init (argc, argv, NULL);
449 server = soup_test_server_new (TRUE);
450 soup_server_add_handler (server, NULL, server_callback, NULL, NULL);
451 base_uri = soup_uri_new ("http://127.0.0.1/");
452 soup_uri_set_port (base_uri, soup_server_get_port (server));
454 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, NULL);
456 /* No sniffer, no content_sniffed should be emitted */
457 do_signals_test (FALSE, FALSE, FALSE, FALSE, FALSE);
458 do_signals_test (FALSE, FALSE, FALSE, TRUE, FALSE);
459 do_signals_test (FALSE, FALSE, TRUE, FALSE, FALSE);
460 do_signals_test (FALSE, FALSE, TRUE, TRUE, FALSE);
462 do_signals_test (FALSE, TRUE, TRUE, FALSE, FALSE);
463 do_signals_test (FALSE, TRUE, TRUE, TRUE, FALSE);
464 do_signals_test (FALSE, TRUE, FALSE, FALSE, FALSE);
465 do_signals_test (FALSE, TRUE, FALSE, TRUE, FALSE);
467 /* Tests that the signals are correctly emitted for empty
469 * http://bugzilla.gnome.org/show_bug.cgi?id=587907 */
471 do_signals_test (FALSE, TRUE, TRUE, FALSE, TRUE);
472 do_signals_test (FALSE, TRUE, TRUE, TRUE, TRUE);
474 soup_session_add_feature_by_type (session, SOUP_TYPE_CONTENT_SNIFFER);
476 /* Now, with a sniffer, content_sniffed must be emitted after
477 * got-headers, and before got-chunk.
479 do_signals_test (TRUE, FALSE, FALSE, FALSE, FALSE);
480 do_signals_test (TRUE, FALSE, FALSE, TRUE, FALSE);
481 do_signals_test (TRUE, FALSE, TRUE, FALSE, FALSE);
482 do_signals_test (TRUE, FALSE, TRUE, TRUE, FALSE);
484 do_signals_test (TRUE, TRUE, TRUE, FALSE, FALSE);
485 do_signals_test (TRUE, TRUE, TRUE, TRUE, FALSE);
486 do_signals_test (TRUE, TRUE, FALSE, FALSE, FALSE);
487 do_signals_test (TRUE, TRUE, FALSE, TRUE, FALSE);
489 /* Empty response tests */
490 do_signals_test (TRUE, TRUE, TRUE, FALSE, TRUE);
491 do_signals_test (TRUE, TRUE, TRUE, TRUE, TRUE);
493 /* Test the text_or_binary sniffing path */
495 /* GIF is a 'safe' type */
496 test_sniffing ("/text_or_binary/home.gif", "image/gif");
498 /* With our current code, no sniffing is done using GIO, so
499 * the mbox will be identified as text/plain; should we change
502 test_sniffing ("/text_or_binary/mbox", "text/plain");
504 /* HTML is considered unsafe for this algorithm, since it is
505 * scriptable, so going from text/plain to text/html is
506 * considered 'privilege escalation'
508 test_sniffing ("/text_or_binary/test.html", "text/plain");
510 /* text/plain with binary content and unknown pattern should be
511 * application/octet-stream */
512 test_sniffing ("/text_or_binary/text_binary.txt", "application/octet-stream");
514 /* text/plain with binary content and scriptable pattern should be
515 * application/octet-stream to avoid 'privilege escalation' */
516 test_sniffing ("/text_or_binary/html_binary.html", "application/octet-stream");
518 /* text/plain with binary content and non scriptable known pattern should
519 * be the given type */
520 test_sniffing ("/text_or_binary/ps_binary.ps", "application/postscript");
522 /* Test the unknown sniffing path */
524 test_sniffing ("/unknown/test.html", "text/html");
525 test_sniffing ("/unknown/home.gif", "image/gif");
526 test_sniffing ("/unknown/mbox", "text/plain");
527 test_sniffing ("/unknown/text_binary.txt", "application/octet-stream");
529 /* Test the XML sniffing path */
531 test_sniffing ("/type/text_xml/home.gif", "text/xml");
532 test_sniffing ("/type/anice_type+xml/home.gif", "anice/type+xml");
533 test_sniffing ("/type/application_xml/home.gif", "application/xml");
535 /* Test the image sniffing path */
537 test_sniffing ("/type/image_png/home.gif", "image/gif");
539 /* Test the feed or html path */
541 test_sniffing ("/type/text_html/test.html", "text/html");
542 test_sniffing ("/type/text_html/rss20.xml", "application/rss+xml");
543 test_sniffing ("/type/text_html/atom.xml", "application/atom+xml");
545 /* The spec tells us to only use the last Content-Type header */
547 test_sniffing ("/multiple_headers/home.gif", "image/gif");
549 /* Test that we keep the parameters when sniffing */
550 test_sniffing ("/type/text_html; charset=UTF-8/test.html", "text/html; charset=UTF-8");
552 /* Test that disabling the sniffer works correctly */
554 test_disabled ("/text_or_binary/home.gif");
556 soup_uri_free (base_uri);
558 soup_test_session_abort_unref (session);
559 soup_test_server_quit_unref (server);