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 full_header = g_string_new (content_type);
355 keys = g_hash_table_get_keys (params);
356 for (iter = keys; iter != NULL; iter = iter->next) {
357 const gchar *value = (const gchar*) g_hash_table_lookup (params, iter->data);
359 g_string_append (full_header, "; ");
360 soup_header_g_string_append_param (full_header,
361 (const gchar*) iter->data,
365 *sniffed_type = full_header->str;
367 g_string_free (full_header, FALSE);
372 test_sniffing (const char *path, const char *expected_type)
374 SoupURI *uri = soup_uri_new_with_base (base_uri, path);
375 SoupMessage *msg = soup_message_new_from_uri ("GET", uri);
376 GMainLoop *loop = g_main_loop_new (NULL, TRUE);
377 char *sniffed_type = NULL;
379 debug_printf (1, "test_sniffing(\"%s\", \"%s\")\n", path, expected_type);
381 g_signal_connect (msg, "content-sniffed",
382 G_CALLBACK (sniffing_content_sniffed), &sniffed_type);
386 soup_session_queue_message (session, msg, finished, loop);
388 g_main_loop_run (loop);
391 debug_printf (1, " message was not sniffed!\n");
393 } else if (strcmp (sniffed_type, expected_type) != 0) {
394 debug_printf (1, " sniffing failed! expected %s, got %s\n",
395 expected_type, sniffed_type);
398 g_free (sniffed_type);
401 g_object_unref (msg);
402 g_main_loop_unref (loop);
406 test_disabled (const char *path)
408 SoupURI *uri = soup_uri_new_with_base (base_uri, path);
409 SoupMessage *msg = soup_message_new_from_uri ("GET", uri);
410 GMainLoop *loop = g_main_loop_new (NULL, TRUE);
411 char *sniffed_type = NULL;
413 soup_message_disable_feature (msg, SOUP_TYPE_CONTENT_SNIFFER);
415 debug_printf (1, "test_disabled(\"%s\")\n", path);
417 g_signal_connect (msg, "content-sniffed",
418 G_CALLBACK (sniffing_content_sniffed), &sniffed_type);
422 soup_session_queue_message (session, msg, finished, loop);
424 g_main_loop_run (loop);
427 debug_printf (1, " message was sniffed!\n");
429 g_free (sniffed_type);
433 g_object_unref (msg);
434 g_main_loop_unref (loop);
438 main (int argc, char **argv)
442 test_init (argc, argv, NULL);
444 server = soup_test_server_new (TRUE);
445 soup_server_add_handler (server, NULL, server_callback, NULL, NULL);
446 base_uri = soup_uri_new ("http://127.0.0.1/");
447 soup_uri_set_port (base_uri, soup_server_get_port (server));
449 session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, NULL);
451 /* No sniffer, no content_sniffed should be emitted */
452 do_signals_test (FALSE, FALSE, FALSE, FALSE, FALSE);
453 do_signals_test (FALSE, FALSE, FALSE, TRUE, FALSE);
454 do_signals_test (FALSE, FALSE, TRUE, FALSE, FALSE);
455 do_signals_test (FALSE, FALSE, TRUE, TRUE, FALSE);
457 do_signals_test (FALSE, TRUE, TRUE, FALSE, FALSE);
458 do_signals_test (FALSE, TRUE, TRUE, TRUE, FALSE);
459 do_signals_test (FALSE, TRUE, FALSE, FALSE, FALSE);
460 do_signals_test (FALSE, TRUE, FALSE, TRUE, FALSE);
462 /* Tests that the signals are correctly emitted for empty
464 * http://bugzilla.gnome.org/show_bug.cgi?id=587907 */
466 do_signals_test (FALSE, TRUE, TRUE, FALSE, TRUE);
467 do_signals_test (FALSE, TRUE, TRUE, TRUE, TRUE);
469 soup_session_add_feature_by_type (session, SOUP_TYPE_CONTENT_SNIFFER);
471 /* Now, with a sniffer, content_sniffed must be emitted after
472 * got-headers, and before got-chunk.
474 do_signals_test (TRUE, FALSE, FALSE, FALSE, FALSE);
475 do_signals_test (TRUE, FALSE, FALSE, TRUE, FALSE);
476 do_signals_test (TRUE, FALSE, TRUE, FALSE, FALSE);
477 do_signals_test (TRUE, FALSE, TRUE, TRUE, FALSE);
479 do_signals_test (TRUE, TRUE, TRUE, FALSE, FALSE);
480 do_signals_test (TRUE, TRUE, TRUE, TRUE, FALSE);
481 do_signals_test (TRUE, TRUE, FALSE, FALSE, FALSE);
482 do_signals_test (TRUE, TRUE, FALSE, TRUE, FALSE);
484 /* Empty response tests */
485 do_signals_test (TRUE, TRUE, TRUE, FALSE, TRUE);
486 do_signals_test (TRUE, TRUE, TRUE, TRUE, TRUE);
488 /* Test the text_or_binary sniffing path */
490 /* GIF is a 'safe' type */
491 test_sniffing ("/text_or_binary/home.gif", "image/gif");
493 /* With our current code, no sniffing is done using GIO, so
494 * the mbox will be identified as text/plain; should we change
497 test_sniffing ("/text_or_binary/mbox", "text/plain");
499 /* HTML is considered unsafe for this algorithm, since it is
500 * scriptable, so going from text/plain to text/html is
501 * considered 'privilege escalation'
503 test_sniffing ("/text_or_binary/test.html", "text/plain");
505 /* text/plain with binary content and unknown pattern should be
506 * application/octet-stream */
507 test_sniffing ("/text_or_binary/text_binary.txt", "application/octet-stream");
509 /* text/plain with binary content and scriptable pattern should be
510 * application/octet-stream to avoid 'privilege escalation' */
511 test_sniffing ("/text_or_binary/html_binary.html", "application/octet-stream");
513 /* text/plain with binary content and non scriptable known pattern should
514 * be the given type */
515 test_sniffing ("/text_or_binary/ps_binary.ps", "application/postscript");
517 /* Test the unknown sniffing path */
519 test_sniffing ("/unknown/test.html", "text/html");
520 test_sniffing ("/unknown/home.gif", "image/gif");
521 test_sniffing ("/unknown/mbox", "text/plain");
522 test_sniffing ("/unknown/text_binary.txt", "application/octet-stream");
524 /* Test the XML sniffing path */
526 test_sniffing ("/type/text_xml/home.gif", "text/xml");
527 test_sniffing ("/type/anice_type+xml/home.gif", "anice/type+xml");
528 test_sniffing ("/type/application_xml/home.gif", "application/xml");
530 /* Test the image sniffing path */
532 test_sniffing ("/type/image_png/home.gif", "image/gif");
534 /* Test the feed or html path */
536 test_sniffing ("/type/text_html/test.html", "text/html");
537 test_sniffing ("/type/text_html/rss20.xml", "application/rss+xml");
538 test_sniffing ("/type/text_html/atom.xml", "application/atom+xml");
540 /* The spec tells us to only use the last Content-Type header */
542 test_sniffing ("/multiple_headers/home.gif", "image/gif");
544 /* Test that we keep the parameters when sniffing */
545 test_sniffing ("/type/text_html; charset=UTF-8/test.html", "text/html; charset=UTF-8");
547 /* Test that disabling the sniffer works correctly */
549 test_disabled ("/text_or_binary/home.gif");
551 soup_uri_free (base_uri);
553 soup_test_session_abort_unref (session);
554 soup_test_server_quit_unref (server);