diff options
Diffstat (limited to 'tests/range-test.c')
-rw-r--r-- | tests/range-test.c | 216 |
1 files changed, 124 insertions, 92 deletions
diff --git a/tests/range-test.c b/tests/range-test.c index 98f56450..c23ba462 100644 --- a/tests/range-test.c +++ b/tests/range-test.c @@ -7,23 +7,6 @@ int total_length; char *test_response; static void -get_full_response (void) -{ - char *contents; - gsize length; - GError *error = NULL; - - if (!g_file_get_contents (SRCDIR "/index.txt", &contents, &length, &error)) { - g_printerr ("Could not read index.txt: %s\n", - error->message); - exit (1); - } - - full_response = soup_buffer_new (SOUP_MEMORY_TAKE, contents, length); - debug_printf (1, "Total response length is %d\n\n", (int)length); -} - -static void check_part (SoupMessageHeaders *headers, const char *body, gsize body_len, gboolean check_start_end, int expected_start, int expected_end) { @@ -33,41 +16,39 @@ check_part (SoupMessageHeaders *headers, const char *body, gsize body_len, soup_message_headers_get_one (headers, "Content-Range")); if (!soup_message_headers_get_content_range (headers, &start, &end, &total_length)) { - debug_printf (1, " Could not find/parse Content-Range\n"); - errors++; + soup_test_assert (FALSE, "Could not find/parse Content-Range"); return; } if (total_length != full_response->length && total_length != -1) { - debug_printf (1, " Unexpected total length %" G_GINT64_FORMAT " in response\n", - total_length); - errors++; + soup_test_assert (FALSE, + "Unexpected total length %" G_GINT64_FORMAT " in response\n", + total_length); return; } if (check_start_end) { if ((expected_start >= 0 && start != expected_start) || (expected_start < 0 && start != full_response->length + expected_start)) { - debug_printf (1, " Unexpected range start %" G_GINT64_FORMAT " in response\n", - start); - errors++; + soup_test_assert (FALSE, + "Unexpected range start %" G_GINT64_FORMAT " in response\n", + start); return; } if ((expected_end >= 0 && end != expected_end) || (expected_end < 0 && end != full_response->length - 1)) { - debug_printf (1, " Unexpected range end %" G_GINT64_FORMAT " in response\n", - end); - errors++; + soup_test_assert (FALSE, + "Unexpected range end %" G_GINT64_FORMAT " in response\n", + end); return; } } if (end - start + 1 != body_len) { - debug_printf (1, " Range length (%d) does not match body length (%d)\n", - (int)(end - start) + 1, - (int)body_len); - errors++; + soup_test_assert (FALSE, "Range length (%d) does not match body length (%d)\n", + (int)(end - start) + 1, + (int)body_len); return; } @@ -76,7 +57,7 @@ check_part (SoupMessageHeaders *headers, const char *body, gsize body_len, static void do_single_range (SoupSession *session, SoupMessage *msg, - int start, int end) + int start, int end, gboolean succeed) { const char *content_type; @@ -85,22 +66,26 @@ do_single_range (SoupSession *session, SoupMessage *msg, soup_session_send_message (session, msg); - if (msg->status_code != SOUP_STATUS_PARTIAL_CONTENT) { - debug_printf (1, " Unexpected status %d %s\n", - msg->status_code, msg->reason_phrase); + if (!succeed) { + soup_test_assert_message_status (msg, SOUP_STATUS_REQUESTED_RANGE_NOT_SATISFIABLE); + if (msg->status_code != SOUP_STATUS_REQUESTED_RANGE_NOT_SATISFIABLE) { + const char *content_range; + + content_range = soup_message_headers_get_one (msg->response_headers, + "Content-Range"); + if (content_range) + debug_printf (1, " Content-Range: %s\n", content_range); + } + g_object_unref (msg); - errors++; return; } + soup_test_assert_message_status (msg, SOUP_STATUS_PARTIAL_CONTENT); + content_type = soup_message_headers_get_content_type ( msg->response_headers, NULL); - if (content_type && !strcmp (content_type, "multipart/byteranges")) { - debug_printf (1, " Response body should not have been multipart/byteranges\n"); - g_object_unref (msg); - errors++; - return; - } + g_assert_cmpstr (content_type, !=, "multipart/byteranges"); check_part (msg->response_headers, msg->response_body->data, msg->response_body->length, TRUE, start, end); @@ -109,13 +94,13 @@ do_single_range (SoupSession *session, SoupMessage *msg, static void request_single_range (SoupSession *session, const char *uri, - int start, int end) + int start, int end, gboolean succeed) { SoupMessage *msg; msg = soup_message_new ("GET", uri); soup_message_headers_set_range (msg->request_headers, start, end); - do_single_range (session, msg, start, end); + do_single_range (session, msg, start, end, succeed); } static void @@ -131,38 +116,21 @@ do_multi_range (SoupSession *session, SoupMessage *msg, soup_session_send_message (session, msg); - if (msg->status_code != SOUP_STATUS_PARTIAL_CONTENT) { - debug_printf (1, " Unexpected status %d %s\n", - msg->status_code, msg->reason_phrase); - g_object_unref (msg); - errors++; - return; - } + soup_test_assert_message_status (msg, SOUP_STATUS_PARTIAL_CONTENT); content_type = soup_message_headers_get_content_type (msg->response_headers, NULL); - if (!content_type || strcmp (content_type, "multipart/byteranges") != 0) { - debug_printf (1, " Response Content-Type (%s) was not multipart/byteranges\n", - content_type); - g_object_unref (msg); - errors++; - return; - } + g_assert_cmpstr (content_type, ==, "multipart/byteranges"); multipart = soup_multipart_new_from_message (msg->response_headers, msg->response_body); if (!multipart) { - debug_printf (1, " Could not parse multipart\n"); + soup_test_assert (FALSE, "Could not parse multipart"); g_object_unref (msg); - errors++; return; } length = soup_multipart_get_length (multipart); - if (length != expected_return_ranges) { - debug_printf (1, " Expected %d ranges, got %d\n", - expected_return_ranges, length); - errors++; - } + g_assert_cmpint (length, ==, expected_return_ranges); for (i = 0; i < length; i++) { SoupMessageHeaders *headers; @@ -196,7 +164,8 @@ request_double_range (SoupSession *session, const char *uri, if (expected_return_ranges == 1) { do_single_range (session, msg, MIN (first_start, second_start), - MAX (first_end, second_end)); + MAX (first_end, second_end), + TRUE); } else do_multi_range (session, msg, expected_return_ranges); } @@ -223,13 +192,36 @@ request_triple_range (SoupSession *session, const char *uri, if (expected_return_ranges == 1) { do_single_range (session, msg, MIN (first_start, MIN (second_start, third_start)), - MAX (first_end, MAX (second_end, third_end))); + MAX (first_end, MAX (second_end, third_end)), + TRUE); } else do_multi_range (session, msg, expected_return_ranges); } static void -do_range_test (SoupSession *session, const char *uri, gboolean expect_coalesce) +request_semi_invalid_range (SoupSession *session, const char *uri, + int first_good_start, int first_good_end, + int bad_start, int bad_end, + int second_good_start, int second_good_end) +{ + SoupMessage *msg; + SoupRange ranges[3]; + + msg = soup_message_new ("GET", uri); + ranges[0].start = first_good_start; + ranges[0].end = first_good_end; + ranges[1].start = bad_start; + ranges[1].end = bad_end; + ranges[2].start = second_good_start; + ranges[2].end = second_good_end; + soup_message_headers_set_ranges (msg->request_headers, ranges, 3); + + do_multi_range (session, msg, 2); +} + +static void +do_range_test (SoupSession *session, const char *uri, + gboolean expect_coalesce, gboolean expect_partial_coalesce) { int twelfths = full_response->length / 12; @@ -255,7 +247,8 @@ do_range_test (SoupSession *session, const char *uri, gboolean expect_coalesce) /* A: 0, simple request */ debug_printf (1, "Requesting %d-%d\n", 0 * twelfths, 1 * twelfths); request_single_range (session, uri, - 0 * twelfths, 1 * twelfths); + 0 * twelfths, 1 * twelfths, + TRUE); /* B: 11, end-relative request. These two are mostly redundant * in terms of data coverage, but they may still catch @@ -263,10 +256,12 @@ do_range_test (SoupSession *session, const char *uri, gboolean expect_coalesce) */ debug_printf (1, "Requesting %d-\n", 11 * twelfths); request_single_range (session, uri, - 11 * twelfths, -1); + 11 * twelfths, -1, + TRUE); debug_printf (1, "Requesting -%d\n", 1 * twelfths); request_single_range (session, uri, - -1 * twelfths, -1); + -1 * twelfths, -1, + TRUE); /* C: 2 and 5 */ debug_printf (1, "Requesting %d-%d,%d-%d\n", @@ -309,12 +304,43 @@ do_range_test (SoupSession *session, const char *uri, gboolean expect_coalesce) 9 * twelfths, 10 * twelfths + 5, 4 * twelfths, 5 * twelfths, 10 * twelfths - 5, 11 * twelfths, - expect_coalesce ? 2 : 3); + expect_partial_coalesce ? 2 : 3); - if (memcmp (full_response->data, test_response, full_response->length) != 0) { - debug_printf (1, "\nfull_response and test_response don't match\n"); - errors++; - } + soup_assert_cmpmem (full_response->data, full_response->length, + test_response, full_response->length); + + debug_printf (1, "Requesting (invalid) %d-%d\n", + (int) full_response->length + 1, + (int) full_response->length + 100); + request_single_range (session, uri, + full_response->length + 1, full_response->length + 100, + FALSE); + + debug_printf (1, "Requesting (semi-invalid) 1-10,%d-%d,20-30\n", + (int) full_response->length + 1, + (int) full_response->length + 100); + request_semi_invalid_range (session, uri, + 1, 10, + full_response->length + 1, full_response->length + 100, + 20, 30); +} + +static void +do_apache_range_test (void) +{ + SoupSession *session; + + SOUP_TEST_SKIP_IF_NO_APACHE; + + session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, NULL); + +#if HAVE_APACHE_2_2 + do_range_test (session, "http://127.0.0.1:47524/", FALSE, FALSE); +#else + do_range_test (session, "http://127.0.0.1:47524/", TRUE, FALSE); +#endif + + soup_test_session_abort_unref (session); } static void @@ -330,38 +356,44 @@ server_handler (SoupServer *server, full_response); } -int -main (int argc, char **argv) +static void +do_libsoup_range_test (void) { SoupSession *session; SoupServer *server; char *base_uri; - test_init (argc, argv, NULL); - apache_init (); - - get_full_response (); - test_response = g_malloc0 (full_response->length); - session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, NULL); - debug_printf (1, "1. Testing against apache\n"); - do_range_test (session, "http://127.0.0.1:47524/", FALSE); - - debug_printf (1, "\n2. Testing against SoupServer\n"); server = soup_test_server_new (FALSE); soup_server_add_handler (server, NULL, server_handler, NULL, NULL); base_uri = g_strdup_printf ("http://127.0.0.1:%u/", soup_server_get_port (server)); - do_range_test (session, base_uri, TRUE); + do_range_test (session, base_uri, TRUE, TRUE); g_free (base_uri); soup_test_server_quit_unref (server); soup_test_session_abort_unref (session); +} + +int +main (int argc, char **argv) +{ + int ret; + + test_init (argc, argv, NULL); + apache_init (); + + full_response = soup_test_get_index (); + test_response = g_malloc0 (full_response->length); + + g_test_add_func ("/ranges/apache", do_apache_range_test); + g_test_add_func ("/ranges/libsoup", do_libsoup_range_test); + + ret = g_test_run (); - soup_buffer_free (full_response); g_free (test_response); test_cleanup (); - return errors != 0; + return ret; } |