Debian Codegraph @

src/ws/test-webresponse.c

raw
1 /*
2 * Copyright (C) 2014 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 #include "config.h"
7
8 #include "cockpitwebinject.h"
9 #include "cockpitwebresponse.h"
10 #include "cockpitwebserver.h"
11
12 #include "testlib/cockpittest.h"
13
14 #include "websocket.h"
15
16 #include <glib/gstdio.h>
17
18 #include <stdlib.h>
19 #include <string.h>
20
21 /* headers that are present in every request */
22 #define STATIC_HEADERS "X-DNS-Prefetch-Control: off\r\nReferrer-Policy: no-referrer\r\nX-Content-Type-Options: nosniff\r\nCross-Origin-Resource-Policy: same-origin\r\nX-Frame-Options: sameorigin\r\n\r\n"
23 static gchar *srcdir;
24
25 typedef struct {
26 CockpitWebResponse *response;
27 GOutputStream *output;
28 gchar *scratch;
29 gboolean response_done;
30 gulong sig_done;
31 } TestCase;
32
33 typedef struct {
34 const gchar *path;
35 const gchar *header;
36 const gchar *value;
37 const gchar *method;
38 const gchar *expected_content_type;
39 CockpitCacheType cache;
40 gboolean for_tls_proxy;
41 } TestFixture;
42
43 static void
44 on_response_done (CockpitWebResponse *response,
45 gboolean reusable,
46 gpointer user_data)
47 {
48 gboolean *response_done = user_data;
49 g_assert (response_done != NULL);
50 g_assert (*response_done == FALSE);
51 *response_done = TRUE;
52 }
53
54 static void
55 setup (TestCase *tc,
56 gconstpointer data)
57 {
58 const TestFixture *fixture = data;
59 const gchar *path = NULL;
60 GHashTable *headers = NULL;
61 GInputStream *input;
62 GIOStream *io;
63
64 if (fixture)
65 path = fixture->path;
66
67 input = g_memory_input_stream_new ();
68 tc->output = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
69 io = g_simple_io_stream_new (input, tc->output);
70 g_object_unref (input);
71
72 if (fixture && fixture->header)
73 {
74 headers = cockpit_web_server_new_table ();
75 g_hash_table_insert (headers, g_strdup (fixture->header), g_strdup (fixture->value));
76 }
77
78 tc->response = cockpit_web_response_new (io, path, path, headers,
79 (fixture && fixture->method) ? fixture->method : "GET",
80 (fixture && fixture->for_tls_proxy) ? "https" : "http");
81
82 if (headers)
83 g_hash_table_unref (headers);
84 g_object_unref (io);
85
86 tc->sig_done = g_signal_connect (tc->response, "done",
87 G_CALLBACK (on_response_done),
88 &tc->response_done);
89 }
90
91 static void
92 teardown (TestCase *tc,
93 gconstpointer data)
94 {
95 while (g_main_context_iteration (NULL, FALSE));
96 g_assert (tc->response_done);
97
98 g_signal_handler_disconnect (tc->response, tc->sig_done);
99 g_clear_object (&tc->output);
100 g_clear_object (&tc->response);
101 g_free (tc->scratch);
102 }
103
104 static const gchar *
105 output_as_string (TestCase *tc)
106 {
107 while (!tc->response_done)
108 g_main_context_iteration (NULL, TRUE);
109
110 g_free (tc->scratch);
111 tc->scratch = g_strndup (g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (tc->output)),
112 g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (tc->output)));
113 return tc->scratch;
114 }
115
116 static void
117 test_return_content (TestCase *tc,
118 gconstpointer data)
119 {
120 const gchar *resp;
121 GBytes *content;
122
123 content = g_bytes_new_static ("the content", 11);
124 cockpit_web_response_content (tc->response, NULL, content, NULL);
125 g_bytes_unref (content);
126
127 resp = output_as_string (tc);
128 g_assert_cmpstr (resp, ==, "HTTP/1.1 200 OK\r\nContent-Length: 11\r\n" STATIC_HEADERS "the content");
129 }
130
131 static void
132 test_return_content_headers (TestCase *tc,
133 gconstpointer data)
134 {
135 const gchar *resp;
136 GHashTable *headers;
137 GBytes *content;
138
139 headers = cockpit_web_server_new_table ();
140 g_hash_table_insert (headers, g_strdup ("My-header"), g_strdup ("my-value"));
141
142 content = g_bytes_new_static ("the content", 11);
143 cockpit_web_response_content (tc->response, headers, content, NULL);
144 g_bytes_unref (content);
145 g_hash_table_destroy (headers);
146
147 resp = output_as_string (tc);
148 g_assert_cmpstr (resp, ==, "HTTP/1.1 200 OK\r\nMy-header: my-value\r\nContent-Length: 11\r\n" STATIC_HEADERS "the content");
149 }
150
151
152 static void
153 test_return_error (TestCase *tc,
154 gconstpointer data)
155 {
156 const gchar *resp;
157
158 cockpit_web_response_error (tc->response, 500, NULL, "Reason here: %s", "booyah");
159
160 resp = output_as_string (tc);
161 g_assert_cmpstr (resp, ==,
162 "HTTP/1.1 500 Reason here: booyah\r\n"
163 "Content-Type: text/html; charset=utf8\r\n"
164 "Transfer-Encoding: chunked\r\n"
165 STATIC_HEADERS
166 "5d\r\n<html><head><title>Reason here: booyah</title></head><body>Reason here: booyah</body></html>\n\r\n"
167 "0\r\n\r\n");
168 }
169
170 static void
171 test_return_error_headers (TestCase *tc,
172 gconstpointer data)
173 {
174 const gchar *resp;
175 GHashTable *headers;
176
177 headers = cockpit_web_server_new_table ();
178 g_hash_table_insert (headers, g_strdup ("Header1"), g_strdup ("value1"));
179
180 cockpit_web_response_error (tc->response, 500, headers, "Reason here: %s", "booyah");
181
182 g_hash_table_destroy (headers);
183
184 resp = output_as_string (tc);
185 cockpit_assert_strmatch(resp,"HTTP/1.1 500 Reason here: booyah\r*\n"
186 "Header1: value1\r*\n"
187 "\r\n");
188 }
189
190 static void
191 test_return_gerror_headers (TestCase *tc,
192 gconstpointer data)
193 {
194 const gchar *resp;
195 GHashTable *headers;
196 GError *error;
197
198 headers = cockpit_web_server_new_table ();
199 g_hash_table_insert (headers, g_strdup ("Header1"), g_strdup ("value1"));
200
201 error = g_error_new (G_IO_ERROR, G_IO_ERROR_FAILED, "Reason here: %s", "booyah");
202 cockpit_web_response_gerror (tc->response, headers, NULL, error);
203
204 g_error_free (error);
205 g_hash_table_destroy (headers);
206
207 resp = output_as_string (tc);
208 cockpit_assert_strmatch(resp,"HTTP/1.1 500 Reason here: booyah\r*\n"
209 "Header1: value1\r*\n"
210 "\r\n");
211 }
212
213 static void
214 test_file_not_found (TestCase *tc,
215 gconstpointer user_data)
216 {
217 const gchar *roots[] = { srcdir, NULL };
218 cockpit_web_response_file (tc->response, "/non-existent", roots);
219 cockpit_assert_strmatch (output_as_string (tc), "HTTP/1.1 404 Not Found*");
220 }
221
222 static void
223 test_file_directory_denied (TestCase *tc,
224 gconstpointer user_data)
225 {
226 const gchar *roots[] = { srcdir, NULL };
227 cockpit_web_response_file (tc->response, "/src", roots);
228 cockpit_assert_strmatch (output_as_string (tc), "HTTP/1.1 403 Directory Listing Denied*");
229 }
230
231 static void
232 test_file_access_denied (TestCase *tc,
233 gconstpointer user_data)
234 {
235 const gchar *roots[] = { "/tmp", NULL };
236 gchar templ[] = "/tmp/test-temp.XXXXXX";
237
238 if (!g_mkdtemp_full (templ, 0000))
239 g_assert_not_reached ();
240
241 cockpit_web_response_file (tc->response, templ + 4, roots);
242 cockpit_assert_strmatch (output_as_string (tc), "HTTP/1.1 403*");
243
244 g_unlink (templ);
245 }
246
247 static void
248 test_file_breakout_denied (TestCase *tc,
249 gconstpointer user_data)
250 {
251 gchar *root = realpath ( SRCDIR "/src", NULL);
252 const gchar *roots[] = { root, NULL };
253 const gchar *breakout = "/../Makefile.am";
254 gchar *check = g_build_filename (roots[0], breakout, NULL);
255 g_assert (root);
256 g_assert (g_file_test (check, G_FILE_TEST_EXISTS));
257 g_free (check);
258 cockpit_web_response_file (tc->response, breakout, roots);
259 cockpit_assert_strmatch (output_as_string (tc), "HTTP/1.1 404*");
260 free (root);
261 }
262
263 static void
264 test_file_encoding_denied (TestCase *tc,
265 gconstpointer user_data)
266 {
267 gchar *root = realpath ( SRCDIR "/src", NULL);
268 const gchar *roots[] = { root, NULL };
269 const gchar *breakout = "/common/Makefile-common.am%00";
270 gchar *check = g_build_filename (roots[0], "common", "Makefile-common.am", NULL);
271 g_assert (root);
272 g_assert (g_file_test (check, G_FILE_TEST_EXISTS));
273 g_free (check);
274 cockpit_web_response_file (tc->response, breakout, roots);
275 cockpit_assert_strmatch (output_as_string (tc), "HTTP/1.1 404*");
276 free (root);
277 }
278
279 static void
280 test_file_slash_denied (TestCase *tc,
281 gconstpointer user_data)
282 {
283 gchar *root = realpath ( SRCDIR "/src", NULL);
284 const gchar *roots[] = { root, NULL };
285 const gchar *breakout = "/common%2fMakefile-common.am";
286 gchar *check = g_build_filename (roots[0], "common", "Makefile-common.am", NULL);
287 g_assert (root);
288 g_assert (g_file_test (check, G_FILE_TEST_EXISTS));
289 g_free (check);
290 cockpit_web_response_file (tc->response, breakout, roots);
291 cockpit_assert_strmatch (output_as_string (tc), "HTTP/1.1 404*");
292 free (root);
293 }
294
295 static void
296 test_file_breakout_non_existant (TestCase *tc,
297 gconstpointer user_data)
298 {
299 gchar *root = realpath ( SRCDIR "/src", NULL);
300 const gchar *roots[] = { root, NULL };
301 const gchar *breakout = "/../non-existent";
302 gchar *check = g_build_filename (roots[0], breakout, NULL);
303 g_assert (root);
304 g_assert (!g_file_test (check, G_FILE_TEST_EXISTS));
305 g_free (check);
306 cockpit_web_response_file (tc->response, breakout, roots);
307 cockpit_assert_strmatch (output_as_string (tc), "HTTP/1.1 404*");
308 free (root);
309 }
310
311 static const TestFixture content_type_fixture_html = {
312 .path = "/pkg/shell/index.html",
313 .expected_content_type = "text/html",
314 };
315
316 static const TestFixture content_type_fixture_png = {
317 .path = "/pkg/shell/images/server-small.png",
318 .expected_content_type = "image/png",
319 };
320
321 static const TestFixture content_type_fixture_wasm = {
322 .path = "/src/ws/mock-content/test.wasm",
323 .expected_content_type = "application/wasm",
324 };
325
326 static const TestFixture fixture_head = {
327 .method = "HEAD",
328 };
329
330 static const TestFixture fixture_unsupported_method = {
331 .method = "PATCH",
332 };
333
334 static void
335 test_content_type (TestCase *tc,
336 gconstpointer user_data)
337 {
338 const TestFixture *fixture = user_data;
339 const gchar *roots[] = { srcdir, NULL };
340 GHashTable *headers;
341 const gchar *resp;
342 gsize length;
343 guint status;
344 gssize off;
345
346 cockpit_web_response_file (tc->response, NULL, roots);
347
348 resp = output_as_string (tc);
349 length = strlen (resp);
350
351 off = web_socket_util_parse_status_line (resp, length, NULL, &status, NULL);
352 g_assert_cmpuint (off, >, 0);
353 g_assert_cmpint (status, ==, 200);
354
355 off = web_socket_util_parse_headers (resp + off, length - off, &headers);
356 g_assert_cmpuint (off, >, 0);
357
358 g_assert_cmpstr (g_hash_table_lookup (headers, "Content-Type"), ==, fixture->expected_content_type);
359
360 g_hash_table_unref (headers);
361 }
362
363 static const TestFixture template_fixture = {
364 .path = "/test.css"
365 };
366
367 static void
368 test_template (TestCase *tc,
369 gconstpointer user_data)
370 {
371 const gchar *roots[] = { SRCDIR "/src/ws/mock-content/", NULL };
372 const gchar *resp;
373 GHashTable *data = g_hash_table_new (g_str_hash, g_str_equal);
374
375 g_hash_table_insert (data, "NAME", "test");
376 g_hash_table_insert (data, "VARIANT", "VALUE");
377 cockpit_web_response_template (tc->response, NULL, roots, data);
378
379 resp = output_as_string (tc);
380 g_assert_cmpstr (resp, ==, "HTTP/1.1 200 OK\r\nContent-Type: text/css\r\nTransfer-Encoding: chunked\r\n" STATIC_HEADERS "17\r\n#brand {\n content: \"\r\n4\r\ntest\r\n4\r\n <b>\r\n5\r\nVALUE\r\n9\r\n</b>\";\n}\n\r\n0\r\n\r\n");
381 g_hash_table_unref (data);
382 }
383
384 static const TestFixture cache_none_fixture = {
385 .path = "/pkg/shell/index.html",
386 .cache = COCKPIT_WEB_RESPONSE_NO_CACHE
387 };
388
389 static const TestFixture cache_fixture = {
390 .path = "/pkg/shell/index.html",
391 .cache = COCKPIT_WEB_RESPONSE_CACHE
392 };
393
394 static const TestFixture cache_unset_fixture = {
395 .path = "/pkg/shell/index.html",
396 .cache = COCKPIT_WEB_RESPONSE_CACHE_UNSET
397 };
398
399 static void
400 test_cache (TestCase *tc,
401 gconstpointer user_data)
402 {
403 const TestFixture *fixture = user_data;
404 const gchar *roots[] = { srcdir, NULL };
405 GHashTable *headers;
406 const gchar *resp;
407 gsize length;
408 guint status;
409 gssize off;
410
411 cockpit_web_response_set_cache_type (tc->response, fixture->cache);
412 cockpit_web_response_file (tc->response, NULL, roots);
413
414 resp = output_as_string (tc);
415 length = strlen (resp);
416
417 off = web_socket_util_parse_status_line (resp, length, NULL, &status, NULL);
418 g_assert_cmpuint (off, >, 0);
419 g_assert_cmpint (status, ==, 200);
420
421 off = web_socket_util_parse_headers (resp + off, length - off, &headers);
422 g_assert_cmpuint (off, >, 0);
423
424 if (fixture->cache == COCKPIT_WEB_RESPONSE_CACHE)
425 g_assert_cmpstr (g_hash_table_lookup (headers, "Vary"), ==, "Cookie");
426 else
427 g_assert_null (g_hash_table_lookup (headers, "Vary"));
428
429 if (fixture->cache == COCKPIT_WEB_RESPONSE_NO_CACHE)
430 g_assert_cmpstr (g_hash_table_lookup (headers, "Cache-Control"), ==, "no-cache, no-store");
431 else if (fixture->cache == COCKPIT_WEB_RESPONSE_CACHE)
432 g_assert_cmpstr (g_hash_table_lookup (headers, "Cache-Control"), ==, "max-age=86400, private");
433 else
434 g_assert_null (g_hash_table_lookup (headers, "Cache-Control"));
435 g_hash_table_unref (headers);
436 }
437
438 static void
439 test_content_encoding (TestCase *tc,
440 gconstpointer data)
441 {
442 const gchar *resp;
443 GBytes *content;
444
445 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_READY);
446
447 cockpit_web_response_headers (tc->response, 200, "OK", 50,
448 "Content-Encoding", "blah",
449 NULL);
450
451 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_QUEUING);
452
453 while (g_main_context_iteration (NULL, FALSE));
454
455 content = g_bytes_new_static ("Cockpit is perfect for new sysadmins, ", 38);
456 cockpit_web_response_queue (tc->response, content);
457 g_bytes_unref (content);
458
459 cockpit_web_response_complete (tc->response);
460
461 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_COMPLETE);
462
463 resp = output_as_string (tc);
464 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_SENT);
465
466 g_assert_cmpstr (resp, ==, "HTTP/1.1 200 OK\r\nContent-Encoding: blah\r\n"
467 "Transfer-Encoding: chunked\r\n" STATIC_HEADERS
468 "26\r\nCockpit is perfect for new sysadmins, \r\n0\r\n\r\n");
469 }
470
471 static void
472 test_stream (TestCase *tc,
473 gconstpointer data)
474 {
475 const gchar *resp;
476 GBytes *content;
477
478 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_READY);
479
480 cockpit_web_response_headers (tc->response, 200, "OK", 11, NULL);
481
482 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_QUEUING);
483
484 while (g_main_context_iteration (NULL, FALSE));
485
486 content = g_bytes_new_static ("the content", 11);
487 cockpit_web_response_queue (tc->response, content);
488 g_bytes_unref (content);
489
490 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_QUEUING);
491
492 cockpit_web_response_complete (tc->response);
493
494 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_COMPLETE);
495
496 resp = output_as_string (tc);
497 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_SENT);
498
499 g_assert_cmpstr (resp, ==, "HTTP/1.1 200 OK\r\nContent-Length: 11\r\n" STATIC_HEADERS "the content");
500 }
501
502 static void
503 on_pressure_set_throttle (CockpitWebResponse *response,
504 gboolean throttle,
505 gpointer user_data)
506 {
507 gint *data = user_data;
508 g_assert (user_data != NULL);
509 *data = throttle ? 1 : 0;
510 }
511
512 static void
513 test_pressure (TestCase *tc,
514 gconstpointer data)
515 {
516 GBytes *sent;
517 gint throttle = -1;
518 gint i;
519
520 g_signal_connect (tc->response, "pressure", G_CALLBACK (on_pressure_set_throttle), &throttle);
521 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_READY);
522
523 cockpit_web_response_headers (tc->response, 200, "OK", -1, NULL);
524
525 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_QUEUING);
526
527 while (g_main_context_iteration (NULL, FALSE));
528
529 /* Sent this a thousand times */
530 sent = g_bytes_new_take (g_strnfill (10 * 1000, '?'), 10 * 1000);
531 for (i = 0; i < 1000; i++)
532 cockpit_web_response_queue (tc->response, sent);
533 g_bytes_unref (sent);
534
535 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_QUEUING);
536
537 /*
538 * This should have put way too much in the queue, and thus
539 * emitted the back-pressure signal. This signal would normally
540 * be used by others to slow down their queueing, but in this
541 * case we just check that it was fired.
542 */
543 g_assert_cmpint (throttle, ==, 1);
544 throttle = -1;
545
546 cockpit_web_response_complete (tc->response);
547 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_COMPLETE);
548
549 /*
550 * Now the queue is getting drained. At some point, it will be
551 * signaled that back pressure has been turned off
552 */
553 while (throttle == -1)
554 g_main_context_iteration (NULL, TRUE);
555
556 g_assert_cmpint (throttle, ==, 0);
557
558 while (!tc->response_done)
559 g_main_context_iteration (NULL, TRUE);
560 g_assert_cmpint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (tc->output)), >, 10 * 1000 * 1000);
561 }
562
563 static void
564 test_head (TestCase *tc,
565 gconstpointer data)
566 {
567 const gchar *resp;
568 GBytes *content;
569
570 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_READY);
571
572 cockpit_web_response_headers (tc->response, 200, "OK", 19, NULL);
573
574 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_QUEUING);
575
576 while (g_main_context_iteration (NULL, FALSE));
577
578 content = g_bytes_new_static ("I shall not be seen", 19);
579 cockpit_web_response_queue (tc->response, content);
580 g_bytes_unref (content);
581
582 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_QUEUING);
583
584 cockpit_web_response_complete (tc->response);
585
586 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_COMPLETE);
587
588 resp = output_as_string (tc);
589 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_SENT);
590
591 g_assert_cmpstr (resp, ==, "HTTP/1.1 200 OK\r\nContent-Length: 19\r\n" STATIC_HEADERS);
592 }
593
594 static void
595 test_unsupported_method (TestCase *tc,
596 gconstpointer data)
597 {
598 cockpit_web_response_error (tc->response, 405, NULL, "Unsupported method");
599
600 const gchar *resp = output_as_string (tc);
601 g_assert (g_str_has_prefix (resp, "HTTP/1.1 405 Unsupported method\r\n"));
602 /* not a HEAD request, thus has body */
603 g_assert (strstr (resp, "<body>"));
604 }
605
606 static void
607 test_chunked_transfer_encoding (TestCase *tc,
608 gconstpointer data)
609 {
610 const gchar *resp;
611 GBytes *content;
612
613 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_READY);
614
615 cockpit_web_response_headers (tc->response, 200, "OK", -1, NULL);
616
617 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_QUEUING);
618
619 while (g_main_context_iteration (NULL, FALSE));
620
621 content = g_bytes_new_static ("Cockpit is perfect for new sysadmins, ", 38);
622 cockpit_web_response_queue (tc->response, content);
623 g_bytes_unref (content);
624
625 content = g_bytes_new_static ("allowing them to easily perform simple tasks such as storage administration, ", 77);
626 cockpit_web_response_queue (tc->response, content);
627 g_bytes_unref (content);
628
629 content = g_bytes_new_static ("inspecting journals and starting and stopping services.", 55);
630 cockpit_web_response_queue (tc->response, content);
631 g_bytes_unref (content);
632
633 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_QUEUING);
634
635 cockpit_web_response_complete (tc->response);
636
637 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_COMPLETE);
638
639 resp = output_as_string (tc);
640 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_SENT);
641
642 g_assert_cmpstr (resp, ==, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n" STATIC_HEADERS
643 "26\r\nCockpit is perfect for new sysadmins, \r\n"
644 "4d\r\nallowing them to easily perform simple tasks such as storage administration, \r\n"
645 "37\r\ninspecting journals and starting and stopping services.\r\n0\r\n\r\n");
646 }
647
648 static void
649 test_chunked_zero_length (TestCase *tc,
650 gconstpointer data)
651 {
652 const gchar *resp;
653 GBytes *content;
654
655 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_READY);
656
657 cockpit_web_response_headers (tc->response, 200, "OK", -1, NULL);
658
659 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_QUEUING);
660
661 while (g_main_context_iteration (NULL, FALSE));
662
663 content = g_bytes_new_static ("Cockpit is perfect for new sysadmins, ", 38);
664 cockpit_web_response_queue (tc->response, content);
665 g_bytes_unref (content);
666
667 content = g_bytes_new_static ("", 0);
668 cockpit_web_response_queue (tc->response, content);
669 g_bytes_unref (content);
670
671 content = g_bytes_new_static ("inspecting journals and starting and stopping services.", 55);
672 cockpit_web_response_queue (tc->response, content);
673 g_bytes_unref (content);
674
675 content = g_bytes_new_static ("", 0);
676 cockpit_web_response_queue (tc->response, content);
677 g_bytes_unref (content);
678
679 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_QUEUING);
680 cockpit_web_response_complete (tc->response);
681
682 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_COMPLETE);
683
684 resp = output_as_string (tc);
685 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_SENT);
686
687 g_assert_cmpstr (resp, ==, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n" STATIC_HEADERS
688 "26\r\nCockpit is perfect for new sysadmins, \r\n"
689 "37\r\ninspecting journals and starting and stopping services.\r\n0\r\n\r\n");
690 }
691
692 static void
693 test_chunked_head (TestCase *tc,
694 gconstpointer data)
695 {
696 const gchar *resp;
697
698 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_READY);
699
700 cockpit_web_response_headers (tc->response, 200, "OK", -1, NULL);
701
702 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_QUEUING);
703
704 while (g_main_context_iteration (NULL, FALSE));
705
706 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_QUEUING);
707 cockpit_web_response_complete (tc->response);
708
709 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_COMPLETE);
710
711 resp = output_as_string (tc);
712 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_SENT);
713
714 g_assert_cmpstr (resp, ==, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n" STATIC_HEADERS);
715 }
716
717
718 static GBytes *
719 bytes_static (const gchar *data)
720 {
721 return g_bytes_new_static (data, strlen (data));
722 }
723
724 static void
725 test_web_filter_simple (TestCase *tc,
726 gconstpointer data)
727 {
728 CockpitWebFilter *filter;
729 const gchar *resp;
730 GBytes *content;
731 GBytes *inject;
732
733 inject = bytes_static ("<meta inject>");
734 filter = cockpit_web_inject_new ("<head>", inject, 1);
735 cockpit_web_response_add_filter (tc->response, filter);
736 g_object_unref (filter);
737 g_bytes_unref (inject);
738
739 content = bytes_static ("<html><head><title>The Title</title></head></html>");
740 cockpit_web_response_content (tc->response, NULL, content, NULL);
741 g_bytes_unref (content);
742
743 while (cockpit_web_response_get_state (tc->response) != COCKPIT_WEB_RESPONSE_COMPLETE)
744 g_main_context_iteration (NULL, TRUE);
745
746 resp = output_as_string (tc);
747 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_SENT);
748
749 g_assert_cmpstr (resp, ==, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n" STATIC_HEADERS
750 "c\r\n<html><head>\r\n"
751 "d\r\n<meta inject>\r\n"
752 "26\r\n<title>The Title</title></head></html>\r\n"
753 "0\r\n\r\n");
754 }
755
756 static void
757 test_web_filter_multiple (TestCase *tc,
758 gconstpointer data)
759 {
760 CockpitWebFilter *filter;
761 const gchar *resp;
762 GBytes *content;
763 GBytes *inject;
764
765 inject = bytes_static ("<meta inject>");
766 filter = cockpit_web_inject_new ("<head>", inject, 1);
767 cockpit_web_response_add_filter (tc->response, filter);
768 g_object_unref (filter);
769 g_bytes_unref (inject);
770
771 inject = bytes_static ("<body>Body</body>");
772 filter = cockpit_web_inject_new ("</head>", inject, 1);
773 cockpit_web_response_add_filter (tc->response, filter);
774 g_object_unref (filter);
775 g_bytes_unref (inject);
776
777 inject = bytes_static ("Prefix ");
778 filter = cockpit_web_inject_new ("<title>", inject, 1);
779 cockpit_web_response_add_filter (tc->response, filter);
780 g_object_unref (filter);
781 g_bytes_unref (inject);
782
783 inject = bytes_static (" ");
784 filter = cockpit_web_inject_new (">", inject, 3);
785 cockpit_web_response_add_filter (tc->response, filter);
786 g_object_unref (filter);
787 g_bytes_unref (inject);
788
789 content = bytes_static ("<html><head><title>The Title</title></head></html>");
790 cockpit_web_response_content (tc->response, NULL, content, NULL);
791 g_bytes_unref (content);
792
793 while (cockpit_web_response_get_state (tc->response) != COCKPIT_WEB_RESPONSE_COMPLETE)
794 g_main_context_iteration (NULL, TRUE);
795
796 resp = output_as_string (tc);
797 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_SENT);
798
799 g_assert_cmpstr (resp, ==, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n" STATIC_HEADERS
800 "6\r\n<html>\r\n"
801 "1\r\n \r\n"
802 "6\r\n<head>\r\n"
803 "1\r\n \r\n"
804 "d\r\n<meta inject>\r\n"
805 "1\r\n \r\n"
806 "7\r\n<title>\r\n"
807 "7\r\nPrefix \r\n"
808 "18\r\nThe Title</title></head>\r\n"
809 "11\r\n<body>Body</body>\r\n"
810 "7\r\n</html>\r\n"
811 "0\r\n\r\n");
812 }
813
814 static void
815 test_web_filter_split (TestCase *tc,
816 gconstpointer data)
817 {
818 CockpitWebFilter *filter;
819 const gchar *string;
820 const gchar *resp;
821 GBytes *inject;
822 GBytes *block;
823 gsize i, x, len;
824
825 inject = bytes_static ("<meta inject>");
826 filter = cockpit_web_inject_new ("<head>", inject, 1);
827 cockpit_web_response_add_filter (tc->response, filter);
828 g_object_unref (filter);
829 g_bytes_unref (inject);
830
831 inject = bytes_static ("<body>Body</body>");
832 filter = cockpit_web_inject_new ("</head>", inject, 1);
833 cockpit_web_response_add_filter (tc->response, filter);
834 g_object_unref (filter);
835 g_bytes_unref (inject);
836
837 inject = bytes_static ("Prefix ");
838 filter = cockpit_web_inject_new ("<title>", inject, 1);
839 cockpit_web_response_add_filter (tc->response, filter);
840 g_object_unref (filter);
841 g_bytes_unref (inject);
842
843 cockpit_web_response_headers (tc->response, 200, "OK", -1, NULL);
844
845 string = "<html><head><title>The Title</title></head></html>";
846 len = strlen (string);
847
848 for (i = 0, x = 1; i < len; i += x, x = 1 + (i % 4))
849 {
850 block = g_bytes_new_static (string + i, MIN (x, strlen (string + i)));
851 g_assert (cockpit_web_response_queue (tc->response, block) == TRUE);
852 g_bytes_unref (block);
853 }
854
855 cockpit_web_response_complete (tc->response);
856
857 while (cockpit_web_response_get_state (tc->response) != COCKPIT_WEB_RESPONSE_COMPLETE)
858 g_main_context_iteration (NULL, TRUE);
859
860 resp = output_as_string (tc);
861 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_SENT);
862
863 g_assert_cmpstr (resp, ==, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n" STATIC_HEADERS
864 "1\r\n<\r\n"
865 "2\r\nht\r\n"
866 "4\r\nml><\r\n"
867 "4\r\nhead\r\n"
868 "1\r\n>\r\n"
869 "d\r\n<meta inject>\r\n"
870 "3\r\n<ti\r\n"
871 "4\r\ntle>\r\n"
872 "7\r\nPrefix \r\n"
873 "4\r\nThe \r\n"
874 "4\r\nTitl\r\n"
875 "4\r\ne</t\r\n"
876 "4\r\nitle\r\n"
877 "4\r\n></h\r\n"
878 "4\r\nead>\r\n"
879 "11\r\n<body>Body</body>\r\n"
880 "4\r\n</ht\r\n"
881 "3\r\nml>\r\n"
882 "0\r\n\r\n");
883 }
884
885 static void
886 test_web_filter_shift (TestCase *tc,
887 gconstpointer data)
888 {
889 CockpitWebFilter *filter;
890 const gchar *resp;
891 GBytes *block;
892 GBytes *inject;
893
894 inject = bytes_static ("injected");
895 filter = cockpit_web_inject_new ("foofn", inject, 1);
896 cockpit_web_response_add_filter (tc->response, filter);
897 g_object_unref (filter);
898 g_bytes_unref (inject);
899
900 cockpit_web_response_headers_full (tc->response, 200, "OK", -1, NULL);
901
902 /* Total content is foofoofn and split after the first 4 characters */
903 block = bytes_static ("foof");
904 g_assert (cockpit_web_response_queue (tc->response, block) == TRUE);
905 g_bytes_unref (block);
906 block = bytes_static ("oofn");
907 g_assert (cockpit_web_response_queue (tc->response, block) == TRUE);
908 g_bytes_unref (block);
909 cockpit_web_response_complete (tc->response);
910
911 while (cockpit_web_response_get_state (tc->response) != COCKPIT_WEB_RESPONSE_COMPLETE)
912 g_main_context_iteration (NULL, TRUE);
913
914 resp = output_as_string (tc);
915 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_SENT);
916
917 g_assert_cmpstr (resp, ==, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n" STATIC_HEADERS
918 "4\r\nfoof\r\n"
919 "4\r\noofn\r\n"
920 "8\r\ninjected\r\n"
921 "0\r\n\r\n");
922 }
923
924 static void
925 test_web_filter_shift_three (TestCase *tc,
926 gconstpointer data)
927 {
928 CockpitWebFilter *filter;
929 const gchar *resp;
930 GBytes *block;
931 GBytes *inject;
932
933 inject = bytes_static ("injected");
934 filter = cockpit_web_inject_new ("foofn", inject, 1);
935 cockpit_web_response_add_filter (tc->response, filter);
936 g_object_unref (filter);
937 g_bytes_unref (inject);
938
939 cockpit_web_response_headers_full (tc->response, 200, "OK", -1, NULL);
940
941 /* Total content is foofoofn and split across multiple packets after the first 4 characters */
942 block = bytes_static ("foof");
943 g_assert (cockpit_web_response_queue (tc->response, block) == TRUE);
944 g_bytes_unref (block);
945 block = bytes_static ("o");
946 g_assert (cockpit_web_response_queue (tc->response, block) == TRUE);
947 g_bytes_unref (block);
948 block = bytes_static ("of");
949 g_assert (cockpit_web_response_queue (tc->response, block) == TRUE);
950 g_bytes_unref (block);
951 block = bytes_static ("n");
952 g_assert (cockpit_web_response_queue (tc->response, block) == TRUE);
953 g_bytes_unref (block);
954 cockpit_web_response_complete (tc->response);
955
956 while (cockpit_web_response_get_state (tc->response) != COCKPIT_WEB_RESPONSE_COMPLETE)
957 g_main_context_iteration (NULL, TRUE);
958
959 resp = output_as_string (tc);
960 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_SENT);
961
962 g_assert_cmpstr (resp, ==, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n" STATIC_HEADERS
963 "4\r\nfoof\r\n"
964 "1\r\no\r\n"
965 "2\r\nof\r\n"
966 "1\r\nn\r\n"
967 "8\r\ninjected\r\n"
968 "0\r\n\r\n");
969 }
970
971 static void
972 test_web_filter_passthrough (TestCase *tc,
973 gconstpointer data)
974 {
975 CockpitWebFilter *filter;
976 const gchar *resp;
977 GBytes *content;
978 GBytes *inject;
979
980 inject = bytes_static ("<meta inject>");
981 filter = cockpit_web_inject_new ("<unknown>", inject, 1);
982 cockpit_web_response_add_filter (tc->response, filter);
983 g_object_unref (filter);
984 g_bytes_unref (inject);
985
986 content = bytes_static ("<html><head><title>The Title</title></head></html>");
987 cockpit_web_response_content (tc->response, NULL, content, NULL);
988 g_bytes_unref (content);
989
990 while (cockpit_web_response_get_state (tc->response) != COCKPIT_WEB_RESPONSE_COMPLETE)
991 g_main_context_iteration (NULL, TRUE);
992
993 resp = output_as_string (tc);
994 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_SENT);
995
996 g_assert_cmpstr (resp, ==, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n" STATIC_HEADERS
997 "32\r\n<html><head><title>The Title</title></head></html>\r\n"
998 "0\r\n\r\n");
999 }
1000
1001 static void
1002 on_response_done_not_resuable (CockpitWebResponse *response,
1003 gboolean reusable,
1004 gpointer user_data)
1005 {
1006 g_assert (reusable == FALSE);
1007 }
1008
1009 static void
1010 test_abort (TestCase *tc,
1011 gconstpointer data)
1012 {
1013 const gchar *resp;
1014 GBytes *content;
1015
1016 cockpit_web_response_headers (tc->response, 200, "OK", 11, NULL);
1017 g_signal_connect (tc->response, "done", G_CALLBACK (on_response_done_not_resuable), NULL);
1018
1019 while (g_main_context_iteration (NULL, FALSE));
1020
1021 content = g_bytes_new_static ("the content", 11);
1022 cockpit_web_response_queue (tc->response, content);
1023 g_bytes_unref (content);
1024
1025 cockpit_web_response_abort (tc->response);
1026 g_assert_cmpint (cockpit_web_response_get_state (tc->response), ==, COCKPIT_WEB_RESPONSE_SENT);
1027
1028 resp = output_as_string (tc);
1029
1030 g_assert_cmpstr (resp, ==, "HTTP/1.1 200 OK\r\nContent-Length: 11\r\n" STATIC_HEADERS);
1031 }
1032
1033 static const TestFixture fixture_connection_close = {
1034 .header = "Connection",
1035 .value = "close",
1036 };
1037
1038 static void
1039 test_connection_close (TestCase *tc,
1040 gconstpointer data)
1041 {
1042 const gchar *resp;
1043 GBytes *content;
1044
1045 g_assert (data == &fixture_connection_close);
1046
1047 g_signal_connect (tc->response, "done", G_CALLBACK (on_response_done_not_resuable), NULL);
1048
1049 content = g_bytes_new_static ("the content", 11);
1050 cockpit_web_response_content (tc->response, NULL, content, NULL);
1051 g_bytes_unref (content);
1052
1053 resp = output_as_string (tc);
1054 g_assert_cmpstr (resp, ==, "HTTP/1.1 200 OK\r\nContent-Length: 11\r\nConnection: close\r\n"
1055 STATIC_HEADERS "the content");
1056 }
1057
1058 static const TestFixture fixture_origin_default = {
1059 .header = "Host",
1060 .value = "somemachine",
1061 .for_tls_proxy = FALSE,
1062 };
1063
1064 static const TestFixture fixture_origin_tls_proxy = {
1065 .header = "Host",
1066 .value = "somemachine",
1067 .for_tls_proxy = TRUE,
1068 };
1069
1070 static void
1071 test_origin (TestCase *tc,
1072 gconstpointer data)
1073 {
1074 const TestFixture *fixture = data;
1075 GBytes *content;
1076
1077 if (fixture->for_tls_proxy)
1078 g_assert_cmpstr (cockpit_web_response_get_origin (tc->response), ==, "https://somemachine");
1079 else
1080 g_assert_cmpstr (cockpit_web_response_get_origin (tc->response), ==, "http://somemachine");
1081
1082 content = g_bytes_new_static ("the content", 11);
1083 cockpit_web_response_content (tc->response, NULL, content, NULL);
1084 g_bytes_unref (content);
1085 output_as_string (tc);
1086 }
1087
1088 typedef struct {
1089 GHashTable *headers;
1090 GIOStream *io;
1091 } TestPlain;
1092
1093 static void
1094 setup_plain (TestPlain *tc,
1095 gconstpointer unused)
1096 {
1097 GInputStream *input;
1098 GOutputStream *output;
1099
1100 input = g_memory_input_stream_new ();
1101 output = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
1102
1103 tc->io = g_simple_io_stream_new (input, output);
1104
1105 g_object_unref (input);
1106 g_object_unref (output);
1107
1108 tc->headers = cockpit_web_server_new_table ();
1109 }
1110
1111 static void
1112 teardown_plain (TestPlain *tc,
1113 gconstpointer unused)
1114 {
1115 g_object_unref (tc->io);
1116 g_hash_table_unref (tc->headers);
1117 }
1118
1119 static void
1120 test_pop_path (TestPlain *tc,
1121 gconstpointer unused)
1122 {
1123 CockpitWebResponse *response;
1124 gchar *part;
1125 const gchar *start = "/cockpit/@localhost/another/test.html";
1126
1127 response = cockpit_web_response_new (tc->io, start, start, tc->headers, "GET", NULL);
1128 g_assert_cmpstr (cockpit_web_response_get_path (response), ==, start);
1129 g_assert_cmpstr (cockpit_web_response_get_url_root (response), ==, NULL);
1130
1131 part = cockpit_web_response_pop_path (response);
1132 g_assert_cmpstr (part, ==, "cockpit");
1133 g_assert_cmpstr (cockpit_web_response_get_path (response), ==, "/@localhost/another/test.html");
1134 g_free (part);
1135
1136 part = cockpit_web_response_pop_path (response);
1137 g_assert_cmpstr (part, ==, "@localhost");
1138 g_assert_cmpstr (cockpit_web_response_get_path (response), ==, "/another/test.html");
1139 g_free (part);
1140
1141 part = cockpit_web_response_pop_path (response);
1142 g_assert_cmpstr (part, ==, "another");
1143 g_assert_cmpstr (cockpit_web_response_get_path (response), ==, "/test.html");
1144 g_free (part);
1145
1146 part = cockpit_web_response_pop_path (response);
1147 g_assert_cmpstr (part, ==, "test.html");
1148 g_assert_cmpstr (cockpit_web_response_get_path (response), ==, NULL);
1149 g_free (part);
1150
1151 part = cockpit_web_response_pop_path (response);
1152 g_assert (part == NULL);
1153 g_assert (cockpit_web_response_get_path (response) == NULL);
1154 g_free (part);
1155
1156 cockpit_web_response_abort (response);
1157 g_object_unref (response);
1158 }
1159
1160 static void
1161 test_pop_path_root (TestPlain *tc,
1162 gconstpointer unused)
1163 {
1164 CockpitWebResponse *response;
1165 gchar *part;
1166
1167 response = cockpit_web_response_new (tc->io, "/", "/", tc->headers, "GET", NULL);
1168 g_assert_cmpstr (cockpit_web_response_get_path (response), ==, "/");
1169
1170 part = cockpit_web_response_pop_path (response);
1171 g_assert_cmpstr (part, ==, NULL);
1172 g_assert_cmpstr (cockpit_web_response_get_path (response), ==, NULL);
1173 g_free (part);
1174
1175 cockpit_web_response_abort (response);
1176 g_object_unref (response);
1177 }
1178
1179 static void
1180 test_skip_path (TestPlain *tc,
1181 gconstpointer unused)
1182 {
1183 CockpitWebResponse *response;
1184 const gchar *start = "/cockpit/@localhost/another/test.html";
1185
1186 response = cockpit_web_response_new (tc->io, start, start, tc->headers, "GET", NULL);
1187 g_assert_cmpstr (cockpit_web_response_get_path (response), ==, "/cockpit/@localhost/another/test.html");
1188
1189 g_assert (cockpit_web_response_skip_path (response) == TRUE);
1190 g_assert_cmpstr (cockpit_web_response_get_path (response), ==, "/@localhost/another/test.html");
1191
1192 g_assert (cockpit_web_response_skip_path (response) == TRUE);
1193 g_assert_cmpstr (cockpit_web_response_get_path (response), ==, "/another/test.html");
1194
1195 g_assert (cockpit_web_response_skip_path (response) == TRUE);
1196 g_assert_cmpstr (cockpit_web_response_get_path (response), ==, "/test.html");
1197
1198 g_assert (cockpit_web_response_skip_path (response) == TRUE);
1199 g_assert_cmpstr (cockpit_web_response_get_path (response), ==, NULL);
1200
1201 g_assert (cockpit_web_response_skip_path (response) == FALSE);
1202 g_assert (cockpit_web_response_get_path (response) == NULL);
1203
1204 cockpit_web_response_abort (response);
1205 g_object_unref (response);
1206 }
1207
1208 static void
1209 test_skip_path_root (TestPlain *tc,
1210 gconstpointer unused)
1211 {
1212 CockpitWebResponse *response;
1213
1214 response = cockpit_web_response_new (tc->io, "/", "/", tc->headers, "GET", NULL);
1215 g_assert_cmpstr (cockpit_web_response_get_path (response), ==, "/");
1216
1217 g_assert (cockpit_web_response_skip_path (response) == FALSE);
1218 g_assert_cmpstr (cockpit_web_response_get_path (response), ==, NULL);
1219
1220 cockpit_web_response_abort (response);
1221 g_object_unref (response);
1222 }
1223
1224 static void
1225 test_removed_prefix (TestPlain *tc,
1226 gconstpointer unused)
1227 {
1228 CockpitWebResponse *response;
1229
1230 response = cockpit_web_response_new (tc->io, "/", "/", tc->headers, "GET", NULL);
1231 g_assert_cmpstr (cockpit_web_response_get_path (response), ==, "/");
1232 g_assert_cmpstr (cockpit_web_response_get_url_root (response), ==, NULL);
1233 cockpit_web_response_abort (response);
1234 g_clear_object (&response);
1235
1236 response = cockpit_web_response_new (tc->io, "/path/", "/path/", tc->headers, "GET", NULL);
1237 g_assert_cmpstr (cockpit_web_response_get_path (response), ==, "/path/");
1238 g_assert_cmpstr (cockpit_web_response_get_url_root (response), ==, NULL);
1239 cockpit_web_response_abort (response);
1240 g_clear_object (&response);
1241
1242 response = cockpit_web_response_new (tc->io, "/path/path2/", "/path2/", tc->headers, "GET", NULL);
1243 g_assert_cmpstr (cockpit_web_response_get_path (response), ==, "/path2/");
1244 g_assert_cmpstr (cockpit_web_response_get_url_root (response), ==, "/path");
1245 cockpit_web_response_abort (response);
1246 g_clear_object (&response);
1247
1248 response = cockpit_web_response_new (tc->io, "/mis/", "/match/", tc->headers, "GET", NULL);
1249 g_assert_cmpstr (cockpit_web_response_get_path (response), ==, "/match/");
1250 g_assert_cmpstr (cockpit_web_response_get_url_root (response), ==, NULL);
1251 cockpit_web_response_abort (response);
1252 g_clear_object (&response);
1253
1254 response = cockpit_web_response_new (tc->io, NULL, NULL, tc->headers, "GET", NULL);
1255 g_assert_cmpstr (cockpit_web_response_get_path (response), ==, NULL);
1256 g_assert_cmpstr (cockpit_web_response_get_url_root (response), ==, NULL);
1257 cockpit_web_response_abort (response);
1258 g_clear_object (&response);
1259 }
1260
1261 static void
1262 test_negotiation_first (void)
1263 {
1264 gboolean is_language_specific, is_compressed;
1265 GError *error = NULL;
1266 GBytes *bytes;
1267
1268 bytes = cockpit_web_response_negotiation (SRCDIR "/src/ws/mock-content/test-file.txt",
1269 NULL, NULL, &is_language_specific, &is_compressed, &error);
1270
1271 cockpit_assert_bytes_eq (bytes, "A small test file\n", -1);
1272 g_assert_no_error (error);
1273 g_bytes_unref (bytes);
1274
1275 g_assert (!is_language_specific);
1276 g_assert (!is_compressed);
1277 }
1278
1279 static void
1280 test_negotiation_last (void)
1281 {
1282 gboolean is_language_specific, is_compressed;
1283 GError *error = NULL;
1284 gchar *checksum;
1285 GBytes *bytes;
1286
1287 bytes = cockpit_web_response_negotiation (SRCDIR "/src/ws/mock-content/large.js",
1288 NULL, NULL, &is_language_specific, &is_compressed, &error);
1289
1290 g_assert_no_error (error);
1291 g_assert (!is_language_specific);
1292 g_assert (is_compressed);
1293
1294 checksum = g_compute_checksum_for_bytes (G_CHECKSUM_MD5, bytes);
1295 g_assert_cmpstr (checksum, ==, "e5284b625b7665fc04e082827de3436c");
1296 g_free (checksum);
1297
1298 g_bytes_unref (bytes);
1299 }
1300
1301 static void
1302 test_negotiation_prune (void)
1303 {
1304 GError *error = NULL;
1305 GBytes *bytes;
1306
1307 bytes = cockpit_web_response_negotiation (SRCDIR "/src/ws/mock-content/test-file.extra.extension.txt",
1308 NULL, NULL, NULL, NULL, &error);
1309
1310 cockpit_assert_bytes_eq (bytes, "A small test file\n", -1);
1311 g_assert_no_error (error);
1312 g_bytes_unref (bytes);
1313 }
1314
1315 static void
1316 test_negotiation_with_listing (void)
1317 {
1318 GHashTable *existing;
1319 GError *error = NULL;
1320 GBytes *bytes;
1321
1322 /* Lie and say that only the .gz file exists */
1323 existing = g_hash_table_new (g_str_hash, g_str_equal);
1324 g_hash_table_add (existing, SRCDIR "/src/ws/mock-content/test-file.txt.gz");
1325
1326 bytes = cockpit_web_response_negotiation (SRCDIR "/src/ws/mock-content/test-file.txt",
1327 existing, NULL, NULL, NULL, &error);
1328
1329 cockpit_assert_bytes_eq (bytes, "\x1F\x8B\x08\x08N1\x03U\x00\x03test-file.txt\x00"
1330 "sT(\xCEM\xCC\xC9Q(I-.QH\xCB\xCCI\xE5\x02\x00>PjG\x12\x00\x00\x00", 52);
1331 g_assert_no_error (error);
1332 g_bytes_unref (bytes);
1333
1334 g_hash_table_unref (existing);
1335 }
1336
1337 static void
1338 test_negotiation_locale (void)
1339 {
1340 gboolean is_language_specific, is_compressed;
1341 GError *error = NULL;
1342 GBytes *bytes;
1343
1344 bytes = cockpit_web_response_negotiation (SRCDIR "/src/ws/mock-content/test-file.txt",
1345 NULL, "zh-cn", &is_language_specific, &is_compressed, &error);
1346
1347 cockpit_assert_bytes_eq (bytes, "A translated test file\n", -1);
1348 g_assert_no_error (error);
1349 g_bytes_unref (bytes);
1350
1351 g_assert (is_language_specific);
1352 g_assert (!is_compressed);
1353 }
1354
1355 static void
1356 test_negotiation_notfound (void)
1357 {
1358 GError *error = NULL;
1359 GBytes *bytes;
1360
1361 bytes = cockpit_web_response_negotiation (SRCDIR "/src/ws/mock-content/non-existent",
1362 NULL, NULL, NULL, NULL, &error);
1363
1364 g_assert_no_error (error);
1365 g_assert (bytes == NULL);
1366 }
1367
1368 static void
1369 test_negotiation_failure (void)
1370 {
1371 GError *error = NULL;
1372 GBytes *bytes;
1373
1374 bytes = cockpit_web_response_negotiation (SRCDIR "/src/ws/mock-content/directory",
1375 NULL, NULL, NULL, NULL, &error);
1376
1377 g_assert (error != NULL);
1378 g_error_free (error);
1379
1380 g_assert (bytes == NULL);
1381 }
1382
1383 int
1384 main (int argc,
1385 char *argv[])
1386 {
1387 extern const gchar *cockpit_webresponse_fail_html_text;
1388 cockpit_webresponse_fail_html_text =
1389 "<html><head><title>@@message@@</title></head><body>@@message@@</body></html>\n";
1390
1391 gint ret;
1392
1393 srcdir = realpath (SRCDIR, NULL);
1394 g_assert (srcdir != NULL);
1395
1396 cockpit_test_init (&argc, &argv);
1397
1398 g_test_add ("/web-response/return-content", TestCase, NULL,
1399 setup, test_return_content, teardown);
1400 g_test_add ("/web-response/return-content-headers", TestCase, NULL,
1401 setup, test_return_content_headers, teardown);
1402 g_test_add ("/web-response/return-error", TestCase, NULL,
1403 setup, test_return_error, teardown);
1404 g_test_add ("/web-response/return-error-headers", TestCase, NULL,
1405 setup, test_return_error_headers, teardown);
1406 g_test_add ("/web-response/return-gerror-headers", TestCase, NULL,
1407 setup, test_return_gerror_headers, teardown);
1408 g_test_add ("/web-response/file/not-found", TestCase, NULL,
1409 setup, test_file_not_found, teardown);
1410 g_test_add ("/web-response/file/directory-denied", TestCase, NULL,
1411 setup, test_file_directory_denied, teardown);
1412 g_test_add ("/web-response/file/access-denied", TestCase, NULL,
1413 setup, test_file_access_denied, teardown);
1414 g_test_add ("/web-response/file/breakout-denied", TestCase, NULL,
1415 setup, test_file_breakout_denied, teardown);
1416 g_test_add ("/web-response/file/invalid-encoding-denied", TestCase, NULL,
1417 setup, test_file_encoding_denied, teardown);
1418 g_test_add ("/web-response/file/file-slash-denied", TestCase, NULL,
1419 setup, test_file_slash_denied, teardown);
1420 g_test_add ("/web-response/file/breakout-non-existant", TestCase, NULL,
1421 setup, test_file_breakout_non_existant, teardown);
1422 g_test_add ("/web-reponse/file/template", TestCase, &template_fixture,
1423 setup, test_template, teardown);
1424 g_test_add ("/web-response/content-type/html", TestCase, &content_type_fixture_html,
1425 setup, test_content_type, teardown);
1426 g_test_add ("/web-response/content-type/png", TestCase, &content_type_fixture_png,
1427 setup, test_content_type, teardown);
1428 g_test_add ("/web-response/content-type/wasm", TestCase, &content_type_fixture_wasm,
1429 setup, test_content_type, teardown);
1430 g_test_add ("/web-response/content-encoding", TestCase, NULL,
1431 setup, test_content_encoding, teardown);
1432 g_test_add ("/web-response/stream", TestCase, NULL,
1433 setup, test_stream, teardown);
1434 g_test_add ("/web-response/pressure", TestCase, NULL,
1435 setup, test_pressure, teardown);
1436 g_test_add ("/web-response/head", TestCase, &fixture_head,
1437 setup, test_head, teardown);
1438 g_test_add ("/web-response/unsupported-method", TestCase, &fixture_unsupported_method,
1439 setup, test_unsupported_method, teardown);
1440 g_test_add ("/web-response/chunked-transfer-encoding", TestCase, NULL,
1441 setup, test_chunked_transfer_encoding, teardown);
1442 g_test_add ("/web-response/chunked-zero-length", TestCase, NULL,
1443 setup, test_chunked_zero_length, teardown);
1444 g_test_add ("/web-response/chunked-head", TestCase, &fixture_head,
1445 setup, test_chunked_head, teardown);
1446 g_test_add ("/web-response/abort", TestCase, NULL,
1447 setup, test_abort, teardown);
1448 g_test_add ("/web-response/connection-close", TestCase, &fixture_connection_close,
1449 setup, test_connection_close, teardown);
1450 g_test_add ("/web-response/origin-default", TestCase, &fixture_origin_default,
1451 setup, test_origin, teardown);
1452 g_test_add ("/web-response/origin-tls-proxy", TestCase, &fixture_origin_tls_proxy,
1453 setup, test_origin, teardown);
1454
1455 g_test_add ("/web-response/cache", TestCase, &cache_fixture,
1456 setup, test_cache, teardown);
1457 g_test_add ("/web-response/cache-none", TestCase, &cache_none_fixture,
1458 setup, test_cache, teardown);
1459 g_test_add ("/web-response/cache-unset", TestCase, &cache_unset_fixture,
1460 setup, test_cache, teardown);
1461
1462 g_test_add ("/web-response/filter/simple", TestCase, NULL,
1463 setup, test_web_filter_simple, teardown);
1464 g_test_add ("/web-response/filter/multiple", TestCase, NULL,
1465 setup, test_web_filter_multiple, teardown);
1466 g_test_add ("/web-response/filter/passthrough", TestCase, NULL,
1467 setup, test_web_filter_passthrough, teardown);
1468 g_test_add ("/web-response/filter/split", TestCase, NULL,
1469 setup, test_web_filter_split, teardown);
1470 g_test_add ("/web-response/filter/shift", TestCase, NULL,
1471 setup, test_web_filter_shift, teardown);
1472 g_test_add ("/web-response/filter/shift_three", TestCase, NULL,
1473 setup, test_web_filter_shift_three, teardown);
1474
1475 g_test_add ("/web-response/path/pop", TestPlain, NULL,
1476 setup_plain, test_pop_path, teardown_plain);
1477 g_test_add ("/web-response/path/pop-root", TestPlain, NULL,
1478 setup_plain, test_pop_path_root, teardown_plain);
1479 g_test_add ("/web-response/path/skip", TestPlain, NULL,
1480 setup_plain, test_skip_path, teardown_plain);
1481 g_test_add ("/web-response/path/skip-root", TestPlain, NULL,
1482 setup_plain, test_skip_path_root, teardown_plain);
1483 g_test_add ("/web-response/path/removed-prefix", TestPlain, NULL,
1484 setup_plain, test_removed_prefix, teardown_plain);
1485
1486 g_test_add_func ("/web-response/negotiation/first", test_negotiation_first);
1487 g_test_add_func ("/web-response/negotiation/last", test_negotiation_last);
1488 g_test_add_func ("/web-response/negotiation/locale", test_negotiation_locale);
1489 g_test_add_func ("/web-response/negotiation/prune", test_negotiation_prune);
1490 g_test_add_func ("/web-response/negotiation/with-listing", test_negotiation_with_listing);
1491 g_test_add_func ("/web-response/negotiation/notfound", test_negotiation_notfound);
1492 g_test_add_func ("/web-response/negotiation/failure", test_negotiation_failure);
1493
1494 ret = g_test_run ();
1495
1496 free (srcdir);
1497
1498 return ret;
1499 }