1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * Copyright (C) 2007 Red Hat, Inc.
4 * Copyright (C) 2011 Igalia, S.L.
5 */
6
7 #include "test-utils.h"
8
9 SoupServer *server;
10 SoupURI *base_uri;
11
12 static void
server_callback(SoupServer * server,SoupMessage * msg,const char * path,GHashTable * query,SoupClientContext * context,gpointer data)13 server_callback (SoupServer *server, SoupMessage *msg,
14 const char *path, GHashTable *query,
15 SoupClientContext *context, gpointer data)
16 {
17 const char *accept_encoding, *options;
18 GSList *codings;
19 SoupBuffer *response = NULL;
20
21 options = soup_message_headers_get_one (msg->request_headers,
22 "X-Test-Options");
23 if (!options)
24 options = "";
25
26 accept_encoding = soup_message_headers_get_list (msg->request_headers,
27 "Accept-Encoding");
28 if (accept_encoding && !soup_header_contains (options, "force-encode"))
29 codings = soup_header_parse_quality_list (accept_encoding, NULL);
30 else
31 codings = NULL;
32
33 if (codings) {
34 gboolean claim_deflate, claim_gzip;
35 const char *extension = NULL, *encoding = NULL;
36
37 claim_deflate = g_slist_find_custom (codings, "deflate", (GCompareFunc)g_ascii_strcasecmp) != NULL;
38 claim_gzip = g_slist_find_custom (codings, "gzip", (GCompareFunc)g_ascii_strcasecmp) != NULL;
39
40 if (claim_gzip && (!claim_deflate ||
41 (!soup_header_contains (options, "prefer-deflate-zlib") &&
42 !soup_header_contains (options, "prefer-deflate-raw")))) {
43 extension = "gz";
44 encoding = "gzip";
45 } else if (claim_deflate) {
46 if (soup_header_contains (options, "prefer-deflate-raw")) {
47 extension = "raw";
48 encoding = "deflate";
49 } else {
50 extension = "zlib";
51 encoding = "deflate";
52 }
53 }
54 if (extension && encoding) {
55 char *resource;
56
57 resource = g_strdup_printf ("%s.%s", path, extension);
58 response = soup_test_load_resource (resource, NULL);
59
60 if (response) {
61 soup_message_headers_append (msg->response_headers,
62 "Content-Encoding",
63 encoding);
64 }
65 g_free (resource);
66 }
67 }
68
69 soup_header_free_list (codings);
70
71 if (!response)
72 response = soup_test_load_resource (path, NULL);
73 if (!response) {
74 /* If path.gz exists but can't be read, we'll send back
75 * the error with "Content-Encoding: gzip" but there's
76 * no body, so, eh.
77 */
78 soup_message_set_status (msg, SOUP_STATUS_NOT_FOUND);
79 return;
80 }
81
82 if (soup_header_contains (options, "force-encode")) {
83 const gchar *encoding = "gzip";
84
85 if (soup_header_contains (options, "prefer-deflate-zlib") ||
86 soup_header_contains (options, "prefer-deflate-raw"))
87 encoding = "deflate";
88
89 soup_message_headers_replace (msg->response_headers,
90 "Content-Encoding",
91 encoding);
92 }
93
94 /* Content-Type matches the "real" format, not the sent format */
95 if (g_str_has_suffix (path, ".gz")) {
96 soup_message_headers_append (msg->response_headers,
97 "Content-Type",
98 "application/gzip");
99 } else {
100 soup_message_headers_append (msg->response_headers,
101 "Content-Type",
102 "text/plain");
103 }
104
105 soup_message_set_status (msg, SOUP_STATUS_OK);
106 soup_message_headers_set_encoding (msg->response_headers, SOUP_ENCODING_CHUNKED);
107
108 if (!soup_header_contains (options, "empty"))
109 soup_message_body_append_buffer (msg->response_body, response);
110 soup_buffer_free (response);
111
112 if (soup_header_contains (options, "trailing-junk")) {
113 soup_message_body_append (msg->response_body, SOUP_MEMORY_COPY,
114 options, strlen (options));
115 }
116 soup_message_body_complete (msg->response_body);
117 }
118
119 typedef struct {
120 SoupSession *session;
121 SoupMessage *msg;
122 SoupRequest *req;
123 SoupBuffer *response;
124 } CodingTestData;
125
126 typedef enum {
127 CODING_TEST_DEFAULT = 0,
128 CODING_TEST_NO_DECODER = (1 << 0),
129 CODING_TEST_REQUEST_API = (1 << 1),
130 CODING_TEST_EMPTY = (1 << 2)
131 } CodingTestType;
132
133 typedef enum {
134 NO_CHECK,
135 EXPECT_DECODED,
136 EXPECT_NOT_DECODED
137 } MessageContentStatus;
138
139 static void
check_response(CodingTestData * data,const char * expected_encoding,const char * expected_content_type,MessageContentStatus status,GByteArray * body)140 check_response (CodingTestData *data,
141 const char *expected_encoding,
142 const char *expected_content_type,
143 MessageContentStatus status,
144 GByteArray *body)
145 {
146 const char *coding, *type;
147
148 soup_test_assert_message_status (data->msg, SOUP_STATUS_OK);
149
150 coding = soup_message_headers_get_one (data->msg->response_headers, "Content-Encoding");
151 g_assert_cmpstr (coding, ==, expected_encoding);
152
153 if (status != NO_CHECK) {
154 if (status == EXPECT_DECODED)
155 g_assert_true (soup_message_get_flags (data->msg) & SOUP_MESSAGE_CONTENT_DECODED);
156 else
157 g_assert_false (soup_message_get_flags (data->msg) & SOUP_MESSAGE_CONTENT_DECODED);
158 }
159
160 type = soup_message_headers_get_one (data->msg->response_headers, "Content-Type");
161 g_assert_cmpstr (type, ==, expected_content_type);
162
163 if (body) {
164 soup_assert_cmpmem (body->data,
165 body->len,
166 data->response->data,
167 data->response->length);
168 } else {
169 soup_assert_cmpmem (data->msg->response_body->data,
170 data->msg->response_body->length,
171 data->response->data,
172 data->response->length);
173 }
174 }
175
176 static void
setup_coding_test(CodingTestData * data,gconstpointer test_data)177 setup_coding_test (CodingTestData *data, gconstpointer test_data)
178 {
179 CodingTestType test_type = GPOINTER_TO_INT (test_data);
180 SoupMessage *msg;
181 SoupURI *uri;
182
183 data->session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
184 SOUP_SESSION_USE_THREAD_CONTEXT, TRUE,
185 NULL);
186
187 uri = soup_uri_new_with_base (base_uri, "/mbox");
188
189 if (test_type & CODING_TEST_EMPTY)
190 data->response = soup_buffer_new (SOUP_MEMORY_STATIC, "", 0);
191 else {
192 msg = soup_message_new_from_uri ("GET", uri);
193 soup_session_send_message (data->session, msg);
194
195 data->response = soup_message_body_flatten (msg->response_body);
196 g_object_unref (msg);
197 }
198
199 if (test_type & CODING_TEST_REQUEST_API) {
200 SoupRequestHTTP *reqh;
201
202 reqh = soup_session_request_http_uri (data->session, "GET", uri, NULL);
203 data->req = SOUP_REQUEST (reqh);
204 data->msg = soup_request_http_get_message (reqh);
205 } else
206 data->msg = soup_message_new_from_uri ("GET", uri);
207 soup_uri_free (uri);
208
209 if (! (test_type & CODING_TEST_NO_DECODER))
210 soup_session_add_feature_by_type (data->session, SOUP_TYPE_CONTENT_DECODER);
211 }
212
213 static void
teardown_coding_test(CodingTestData * data,gconstpointer test_data)214 teardown_coding_test (CodingTestData *data, gconstpointer test_data)
215 {
216 soup_buffer_free (data->response);
217
218 g_clear_object (&data->req);
219 g_object_unref (data->msg);
220
221 soup_test_session_abort_unref (data->session);
222 }
223
224 static void
do_coding_test_plain(CodingTestData * data,gconstpointer test_data)225 do_coding_test_plain (CodingTestData *data, gconstpointer test_data)
226 {
227 soup_session_send_message (data->session, data->msg);
228 check_response (data, NULL, "text/plain", EXPECT_NOT_DECODED, NULL);
229 }
230
231 static void
do_coding_test_gzip(CodingTestData * data,gconstpointer test_data)232 do_coding_test_gzip (CodingTestData *data, gconstpointer test_data)
233 {
234 soup_session_send_message (data->session, data->msg);
235 check_response (data, "gzip", "text/plain", EXPECT_DECODED, NULL);
236 }
237
238 static void
do_coding_test_gzip_with_junk(CodingTestData * data,gconstpointer test_data)239 do_coding_test_gzip_with_junk (CodingTestData *data, gconstpointer test_data)
240 {
241 g_test_bug ("606352");
242 g_test_bug ("676477");
243
244 soup_message_headers_append (data->msg->request_headers,
245 "X-Test-Options", "trailing-junk");
246
247 soup_session_send_message (data->session, data->msg);
248 check_response (data, "gzip", "text/plain", EXPECT_DECODED, NULL);
249 }
250
251 static void
do_coding_test_gzip_bad_server(CodingTestData * data,gconstpointer test_data)252 do_coding_test_gzip_bad_server (CodingTestData *data, gconstpointer test_data)
253 {
254 g_test_bug ("613361");
255
256 soup_message_headers_append (data->msg->request_headers,
257 "X-Test-Options", "force-encode");
258
259 soup_session_send_message (data->session, data->msg);
260
261 /* Failed content-decoding should have left the body untouched
262 * from what the server sent... which happens to be the
263 * uncompressed data.
264 */
265 check_response (data, "gzip", "text/plain", EXPECT_NOT_DECODED, NULL);
266 }
267
268 static void
do_coding_test_deflate(CodingTestData * data,gconstpointer test_data)269 do_coding_test_deflate (CodingTestData *data, gconstpointer test_data)
270 {
271 soup_message_headers_append (data->msg->request_headers,
272 "X-Test-Options", "prefer-deflate-zlib");
273 soup_session_send_message (data->session, data->msg);
274
275 check_response (data, "deflate", "text/plain", EXPECT_DECODED, NULL);
276 }
277
278 static void
do_coding_test_deflate_with_junk(CodingTestData * data,gconstpointer test_data)279 do_coding_test_deflate_with_junk (CodingTestData *data, gconstpointer test_data)
280 {
281 g_test_bug ("606352");
282 g_test_bug ("676477");
283
284 soup_message_headers_append (data->msg->request_headers,
285 "X-Test-Options", "prefer-deflate-zlib, trailing-junk");
286 soup_session_send_message (data->session, data->msg);
287
288 check_response (data, "deflate", "text/plain", EXPECT_DECODED, NULL);
289 }
290
291 static void
do_coding_test_deflate_bad_server(CodingTestData * data,gconstpointer test_data)292 do_coding_test_deflate_bad_server (CodingTestData *data, gconstpointer test_data)
293 {
294 g_test_bug ("613361");
295
296 soup_message_headers_append (data->msg->request_headers,
297 "X-Test-Options", "force-encode, prefer-deflate-zlib");
298 soup_session_send_message (data->session, data->msg);
299
300 check_response (data, "deflate", "text/plain", EXPECT_NOT_DECODED, NULL);
301 }
302
303 static void
do_coding_test_deflate_raw(CodingTestData * data,gconstpointer test_data)304 do_coding_test_deflate_raw (CodingTestData *data, gconstpointer test_data)
305 {
306 soup_message_headers_append (data->msg->request_headers,
307 "X-Test-Options", "prefer-deflate-raw");
308 soup_session_send_message (data->session, data->msg);
309
310 check_response (data, "deflate", "text/plain", EXPECT_DECODED, NULL);
311 }
312
313 static void
do_coding_test_deflate_raw_bad_server(CodingTestData * data,gconstpointer test_data)314 do_coding_test_deflate_raw_bad_server (CodingTestData *data, gconstpointer test_data)
315 {
316 g_test_bug ("613361");
317
318 soup_message_headers_append (data->msg->request_headers,
319 "X-Test-Options", "force-encode, prefer-deflate-raw");
320 soup_session_send_message (data->session, data->msg);
321
322 check_response (data, "deflate", "text/plain", EXPECT_NOT_DECODED, NULL);
323 }
324
325 static void
read_finished(GObject * stream,GAsyncResult * result,gpointer user_data)326 read_finished (GObject *stream, GAsyncResult *result, gpointer user_data)
327 {
328 gssize *nread = user_data;
329 GError *error = NULL;
330
331 *nread = g_input_stream_read_finish (G_INPUT_STREAM (stream),
332 result, &error);
333 g_assert_no_error (error);
334 g_clear_error (&error);
335 }
336
337 static void
do_single_coding_req_test(CodingTestData * data,const char * expected_encoding,const char * expected_content_type,MessageContentStatus status)338 do_single_coding_req_test (CodingTestData *data,
339 const char *expected_encoding,
340 const char *expected_content_type,
341 MessageContentStatus status)
342 {
343 GInputStream *stream;
344 GByteArray *body;
345 guchar buf[1024];
346 gssize nread;
347 GError *error = NULL;
348
349 body = g_byte_array_new ();
350
351 stream = soup_test_request_send (data->req, NULL, 0, &error);
352 if (!stream) {
353 g_assert_no_error (error);
354 g_error_free (error);
355 return;
356 }
357
358 do {
359 nread = -2;
360 g_input_stream_read_async (stream, buf, sizeof (buf),
361 G_PRIORITY_DEFAULT,
362 NULL, read_finished, &nread);
363 while (nread == -2)
364 g_main_context_iteration (NULL, TRUE);
365
366 if (nread > 0)
367 g_byte_array_append (body, buf, nread);
368 } while (nread > 0);
369
370 soup_test_request_close_stream (data->req, stream, NULL, &error);
371 g_assert_no_error (error);
372 g_clear_error (&error);
373 g_object_unref (stream);
374
375 check_response (data, expected_encoding, expected_content_type, status, body);
376 g_byte_array_free (body, TRUE);
377 }
378
379 static void
do_coding_req_test_plain(CodingTestData * data,gconstpointer test_data)380 do_coding_req_test_plain (CodingTestData *data, gconstpointer test_data)
381 {
382 do_single_coding_req_test (data, NULL, "text/plain", EXPECT_NOT_DECODED);
383 }
384
385 static void
do_coding_req_test_gzip(CodingTestData * data,gconstpointer test_data)386 do_coding_req_test_gzip (CodingTestData *data, gconstpointer test_data)
387 {
388 do_single_coding_req_test (data, "gzip", "text/plain", EXPECT_DECODED);
389 }
390
391 static void
do_coding_req_test_gzip_with_junk(CodingTestData * data,gconstpointer test_data)392 do_coding_req_test_gzip_with_junk (CodingTestData *data, gconstpointer test_data)
393 {
394 g_test_bug ("606352");
395 g_test_bug ("676477");
396
397 soup_message_headers_append (data->msg->request_headers,
398 "X-Test-Options", "trailing-junk");
399
400 do_single_coding_req_test (data, "gzip", "text/plain", EXPECT_DECODED);
401 }
402
403 static void
do_coding_req_test_gzip_bad_server(CodingTestData * data,gconstpointer test_data)404 do_coding_req_test_gzip_bad_server (CodingTestData *data, gconstpointer test_data)
405 {
406 g_test_bug ("613361");
407
408 soup_message_headers_append (data->msg->request_headers,
409 "X-Test-Options", "force-encode");
410 do_single_coding_req_test (data, "gzip", "text/plain", EXPECT_NOT_DECODED);
411 }
412
413 static void
do_coding_req_test_deflate(CodingTestData * data,gconstpointer test_data)414 do_coding_req_test_deflate (CodingTestData *data, gconstpointer test_data)
415 {
416 soup_message_headers_append (data->msg->request_headers,
417 "X-Test-Options", "prefer-deflate-zlib");
418 do_single_coding_req_test (data, "deflate", "text/plain", EXPECT_DECODED);
419 }
420
421 static void
do_coding_req_test_deflate_with_junk(CodingTestData * data,gconstpointer test_data)422 do_coding_req_test_deflate_with_junk (CodingTestData *data, gconstpointer test_data)
423 {
424 g_test_bug ("606352");
425 g_test_bug ("676477");
426
427 soup_message_headers_append (data->msg->request_headers,
428 "X-Test-Options", "prefer-deflate-zlib, trailing-junk");
429 do_single_coding_req_test (data, "deflate", "text/plain", EXPECT_DECODED);
430 }
431
432 static void
do_coding_req_test_deflate_bad_server(CodingTestData * data,gconstpointer test_data)433 do_coding_req_test_deflate_bad_server (CodingTestData *data, gconstpointer test_data)
434 {
435 g_test_bug ("613361");
436
437 soup_message_headers_append (data->msg->request_headers,
438 "X-Test-Options", "force-encode, prefer-deflate-zlib");
439 do_single_coding_req_test (data, "deflate", "text/plain", EXPECT_NOT_DECODED);
440 }
441
442 static void
do_coding_req_test_deflate_raw(CodingTestData * data,gconstpointer test_data)443 do_coding_req_test_deflate_raw (CodingTestData *data, gconstpointer test_data)
444 {
445 soup_message_headers_append (data->msg->request_headers,
446 "X-Test-Options", "prefer-deflate-raw");
447 do_single_coding_req_test (data, "deflate", "text/plain", EXPECT_DECODED);
448 }
449
450 static void
do_coding_req_test_deflate_raw_bad_server(CodingTestData * data,gconstpointer test_data)451 do_coding_req_test_deflate_raw_bad_server (CodingTestData *data, gconstpointer test_data)
452 {
453 g_test_bug ("613361");
454
455 soup_message_headers_append (data->msg->request_headers,
456 "X-Test-Options", "force-encode, prefer-deflate-raw");
457 do_single_coding_req_test (data, "deflate", "text/plain", EXPECT_NOT_DECODED);
458 }
459
460 static void
do_coding_msg_empty_test(CodingTestData * data,gconstpointer test_data)461 do_coding_msg_empty_test (CodingTestData *data, gconstpointer test_data)
462 {
463 g_test_bug ("697527");
464
465 soup_message_headers_append (data->msg->request_headers,
466 "X-Test-Options", "empty");
467 soup_session_send_message (data->session, data->msg);
468
469 check_response (data, "gzip", "text/plain", EXPECT_NOT_DECODED, NULL);
470 }
471
472 static void
do_coding_req_empty_test(CodingTestData * data,gconstpointer test_data)473 do_coding_req_empty_test (CodingTestData *data, gconstpointer test_data)
474 {
475 g_test_bug ("697527");
476
477 soup_message_headers_append (data->msg->request_headers,
478 "X-Test-Options", "empty");
479 do_single_coding_req_test (data, "gzip", "text/plain", EXPECT_NOT_DECODED);
480 }
481
482 int
main(int argc,char ** argv)483 main (int argc, char **argv)
484 {
485 int ret;
486
487 test_init (argc, argv, NULL);
488
489 server = soup_test_server_new (SOUP_TEST_SERVER_IN_THREAD);
490 soup_server_add_handler (server, NULL, server_callback, NULL, NULL);
491 base_uri = soup_test_server_get_uri (server, "http", NULL);
492
493 g_test_add ("/coding/message/plain", CodingTestData,
494 GINT_TO_POINTER (CODING_TEST_NO_DECODER),
495 setup_coding_test, do_coding_test_plain, teardown_coding_test);
496 g_test_add ("/coding/message/gzip", CodingTestData,
497 GINT_TO_POINTER (CODING_TEST_DEFAULT),
498 setup_coding_test, do_coding_test_gzip, teardown_coding_test);
499 g_test_add ("/coding/message/gzip/with-junk", CodingTestData,
500 GINT_TO_POINTER (CODING_TEST_DEFAULT),
501 setup_coding_test, do_coding_test_gzip_with_junk, teardown_coding_test);
502 g_test_add ("/coding/message/gzip/bad-server", CodingTestData,
503 GINT_TO_POINTER (CODING_TEST_DEFAULT),
504 setup_coding_test, do_coding_test_gzip_bad_server, teardown_coding_test);
505 g_test_add ("/coding/message/deflate", CodingTestData,
506 GINT_TO_POINTER (CODING_TEST_DEFAULT),
507 setup_coding_test, do_coding_test_deflate, teardown_coding_test);
508 g_test_add ("/coding/message/deflate/with-junk", CodingTestData,
509 GINT_TO_POINTER (CODING_TEST_DEFAULT),
510 setup_coding_test, do_coding_test_deflate_with_junk, teardown_coding_test);
511 g_test_add ("/coding/message/deflate/bad-server", CodingTestData,
512 GINT_TO_POINTER (CODING_TEST_DEFAULT),
513 setup_coding_test, do_coding_test_deflate_bad_server, teardown_coding_test);
514 g_test_add ("/coding/message/deflate-raw", CodingTestData,
515 GINT_TO_POINTER (CODING_TEST_DEFAULT),
516 setup_coding_test, do_coding_test_deflate_raw, teardown_coding_test);
517 g_test_add ("/coding/message/deflate-raw/bad-server", CodingTestData,
518 GINT_TO_POINTER (CODING_TEST_DEFAULT),
519 setup_coding_test, do_coding_test_deflate_raw_bad_server, teardown_coding_test);
520
521 g_test_add ("/coding/request/plain", CodingTestData,
522 GINT_TO_POINTER (CODING_TEST_NO_DECODER | CODING_TEST_REQUEST_API),
523 setup_coding_test, do_coding_req_test_plain, teardown_coding_test);
524 g_test_add ("/coding/request/gzip", CodingTestData,
525 GINT_TO_POINTER (CODING_TEST_REQUEST_API),
526 setup_coding_test, do_coding_req_test_gzip, teardown_coding_test);
527 g_test_add ("/coding/request/gzip/with-junk", CodingTestData,
528 GINT_TO_POINTER (CODING_TEST_REQUEST_API),
529 setup_coding_test, do_coding_req_test_gzip_with_junk, teardown_coding_test);
530 g_test_add ("/coding/request/gzip/bad-server", CodingTestData,
531 GINT_TO_POINTER (CODING_TEST_REQUEST_API),
532 setup_coding_test, do_coding_req_test_gzip_bad_server, teardown_coding_test);
533 g_test_add ("/coding/request/deflate", CodingTestData,
534 GINT_TO_POINTER (CODING_TEST_REQUEST_API),
535 setup_coding_test, do_coding_req_test_deflate, teardown_coding_test);
536 g_test_add ("/coding/request/deflate/with-junk", CodingTestData,
537 GINT_TO_POINTER (CODING_TEST_REQUEST_API),
538 setup_coding_test, do_coding_req_test_deflate_with_junk, teardown_coding_test);
539 g_test_add ("/coding/request/deflate/bad-server", CodingTestData,
540 GINT_TO_POINTER (CODING_TEST_REQUEST_API),
541 setup_coding_test, do_coding_req_test_deflate_bad_server, teardown_coding_test);
542 g_test_add ("/coding/request/deflate-raw", CodingTestData,
543 GINT_TO_POINTER (CODING_TEST_REQUEST_API),
544 setup_coding_test, do_coding_req_test_deflate_raw, teardown_coding_test);
545 g_test_add ("/coding/request/deflate-raw/bad-server", CodingTestData,
546 GINT_TO_POINTER (CODING_TEST_REQUEST_API),
547 setup_coding_test, do_coding_req_test_deflate_raw_bad_server, teardown_coding_test);
548
549 g_test_add ("/coding/message/empty", CodingTestData,
550 GINT_TO_POINTER (CODING_TEST_EMPTY),
551 setup_coding_test, do_coding_msg_empty_test, teardown_coding_test);
552 g_test_add ("/coding/request/empty", CodingTestData,
553 GINT_TO_POINTER (CODING_TEST_REQUEST_API | CODING_TEST_EMPTY),
554 setup_coding_test, do_coding_req_empty_test, teardown_coding_test);
555
556 ret = g_test_run ();
557
558 soup_uri_free (base_uri);
559 soup_test_server_quit_unref (server);
560
561 test_cleanup ();
562 return ret;
563 }
564