test_toolarge.c (57467B)
1 /* 2 This file is part of libmicrohttpd 3 Copyright (C) 2014-2022 Evgeny Grin (Karlson2k) 4 Copyright (C) 2007, 2009, 2011 Christian Grothoff 5 6 libmicrohttpd is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published 8 by the Free Software Foundation; either version 2, or (at your 9 option) any later version. 10 11 libmicrohttpd is distributed in the hope that it will be useful, but 12 WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with libmicrohttpd; see the file COPYING. If not, write to the 18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 Boston, MA 02110-1301, USA. 20 */ 21 /** 22 * @file test_toolarge.c 23 * @brief Testcase for handling of data larger then buffers. 24 * @details Testcases for handling of various situations when data cannot fit 25 * the buffers. Address sanitizers and debug asserts should increase 26 * number of problems detected by this test (detect actual overrun). 27 * Tests start with valid sizes to ensure that normal data is processed 28 * correctly and then sizes are monotonically increased to ensure that 29 * overflow is handled correctly at all stages in all codepaths. 30 * Tests with valid sizes are repeated several times to ensure that 31 * tests are failed because of overflow, but because of second run. 32 * @author Karlson2k (Evgeny Grin) 33 * @author Christian Grothoff 34 */ 35 #include "mhd_options.h" 36 #include "platform.h" 37 #include <curl/curl.h> 38 #include <microhttpd.h> 39 #include <stdlib.h> 40 #include <string.h> 41 #include <time.h> 42 #include <errno.h> 43 #include "mhd_has_in_name.h" 44 #include "mhd_has_param.h" 45 #include "mhd_sockets.h" /* only macros used */ 46 47 /* Turn any MHD_PANIC() or failing mhd_assert() reached from this 48 test into a marked, classifiable test error (TESTING.md, P5). */ 49 #include "mhd_panic_tripwire.h" 50 51 #ifdef HAVE_STRINGS_H 52 #include <strings.h> 53 #endif /* HAVE_STRINGS_H */ 54 55 #ifdef _WIN32 56 #ifndef WIN32_LEAN_AND_MEAN 57 #define WIN32_LEAN_AND_MEAN 1 58 #endif /* !WIN32_LEAN_AND_MEAN */ 59 #include <windows.h> 60 #endif 61 62 #ifndef WINDOWS 63 #include <unistd.h> 64 #include <sys/socket.h> 65 #endif 66 67 #ifdef HAVE_LIMITS_H 68 #include <limits.h> 69 #endif /* HAVE_LIMITS_H */ 70 71 #if defined(MHD_CPU_COUNT) && (MHD_CPU_COUNT + 0) < 2 72 #undef MHD_CPU_COUNT 73 #endif 74 #if ! defined(MHD_CPU_COUNT) 75 #define MHD_CPU_COUNT 2 76 #endif 77 #if MHD_CPU_COUNT > 32 78 #undef MHD_CPU_COUNT 79 /* Limit to reasonable value */ 80 #define MHD_CPU_COUNT 32 81 #endif /* MHD_CPU_COUNT > 32 */ 82 83 84 #if defined(HAVE___FUNC__) 85 #define externalErrorExit(ignore) \ 86 _externalErrorExit_func(NULL, __func__, __LINE__) 87 #define externalErrorExitDesc(errDesc) \ 88 _externalErrorExit_func(errDesc, __func__, __LINE__) 89 #define libcurlErrorExit(ignore) \ 90 _libcurlErrorExit_func(NULL, __func__, __LINE__) 91 #define libcurlErrorExitDesc(errDesc) \ 92 _libcurlErrorExit_func(errDesc, __func__, __LINE__) 93 #define mhdErrorExit(ignore) \ 94 _mhdErrorExit_func(NULL, __func__, __LINE__) 95 #define mhdErrorExitDesc(errDesc) \ 96 _mhdErrorExit_func(errDesc, __func__, __LINE__) 97 #elif defined(HAVE___FUNCTION__) 98 #define externalErrorExit(ignore) \ 99 _externalErrorExit_func(NULL, __FUNCTION__, __LINE__) 100 #define externalErrorExitDesc(errDesc) \ 101 _externalErrorExit_func(errDesc, __FUNCTION__, __LINE__) 102 #define libcurlErrorExit(ignore) \ 103 _libcurlErrorExit_func(NULL, __FUNCTION__, __LINE__) 104 #define libcurlErrorExitDesc(errDesc) \ 105 _libcurlErrorExit_func(errDesc, __FUNCTION__, __LINE__) 106 #define mhdErrorExit(ignore) \ 107 _mhdErrorExit_func(NULL, __FUNCTION__, __LINE__) 108 #define mhdErrorExitDesc(errDesc) \ 109 _mhdErrorExit_func(errDesc, __FUNCTION__, __LINE__) 110 #else 111 #define externalErrorExit(ignore) _externalErrorExit_func(NULL, NULL, __LINE__) 112 #define externalErrorExitDesc(errDesc) \ 113 _externalErrorExit_func(errDesc, NULL, __LINE__) 114 #define libcurlErrorExit(ignore) _libcurlErrorExit_func(NULL, NULL, __LINE__) 115 #define libcurlErrorExitDesc(errDesc) \ 116 _libcurlErrorExit_func(errDesc, NULL, __LINE__) 117 #define mhdErrorExit(ignore) _mhdErrorExit_func(NULL, NULL, __LINE__) 118 #define mhdErrorExitDesc(errDesc) _mhdErrorExit_func(errDesc, NULL, __LINE__) 119 #endif 120 121 122 _MHD_NORETURN static void 123 _externalErrorExit_func (const char *errDesc, const char *funcName, int lineNum) 124 { 125 if ((NULL != errDesc) && (0 != errDesc[0])) 126 fprintf (stderr, "%s", errDesc); 127 else 128 fprintf (stderr, "System or external library call failed"); 129 if ((NULL != funcName) && (0 != funcName[0])) 130 fprintf (stderr, " in %s", funcName); 131 if (0 < lineNum) 132 fprintf (stderr, " at line %d", lineNum); 133 134 fprintf (stderr, ".\nLast errno value: %d (%s)\n", (int) errno, 135 strerror (errno)); 136 #ifdef MHD_WINSOCK_SOCKETS 137 fprintf (stderr, "WSAGetLastError() value: %d\n", (int) WSAGetLastError ()); 138 #endif /* MHD_WINSOCK_SOCKETS */ 139 fflush (stderr); 140 exit (99); 141 } 142 143 144 static char libcurl_errbuf[CURL_ERROR_SIZE] = ""; 145 146 _MHD_NORETURN static void 147 _libcurlErrorExit_func (const char *errDesc, const char *funcName, int lineNum) 148 { 149 if ((NULL != errDesc) && (0 != errDesc[0])) 150 fprintf (stderr, "%s", errDesc); 151 else 152 fprintf (stderr, "CURL library call failed"); 153 if ((NULL != funcName) && (0 != funcName[0])) 154 fprintf (stderr, " in %s", funcName); 155 if (0 < lineNum) 156 fprintf (stderr, " at line %d", lineNum); 157 158 fprintf (stderr, ".\nLast errno value: %d (%s)\n", (int) errno, 159 strerror (errno)); 160 if (0 != libcurl_errbuf[0]) 161 fprintf (stderr, "Last libcurl error details: %s\n", libcurl_errbuf); 162 163 fflush (stderr); 164 exit (99); 165 } 166 167 168 _MHD_NORETURN static void 169 _mhdErrorExit_func (const char *errDesc, const char *funcName, int lineNum) 170 { 171 if ((NULL != errDesc) && (0 != errDesc[0])) 172 fprintf (stderr, "%s", errDesc); 173 else 174 fprintf (stderr, "MHD unexpected error"); 175 if ((NULL != funcName) && (0 != funcName[0])) 176 fprintf (stderr, " in %s", funcName); 177 if (0 < lineNum) 178 fprintf (stderr, " at line %d", lineNum); 179 180 fprintf (stderr, ".\nLast errno value: %d (%s)\n", (int) errno, 181 strerror (errno)); 182 183 fflush (stderr); 184 exit (8); 185 } 186 187 188 /* Could be increased to facilitate debugging */ 189 #define TIMEOUTS_VAL 5 190 191 #define EXPECTED_URI_BASE_PATH "/a" 192 193 #define URL_SCHEME_HOST "http:/" "/127.0.0.1" 194 195 #define N1_HEADER_NAME "n" 196 #define N1_HEADER_VALUE "1" 197 #define N1_HEADER N1_HEADER_NAME ": " N1_HEADER_VALUE 198 #define N1_HEADER_CRLF N1_HEADER "\r\n" 199 200 #define BUFFER_SIZE 1024 201 202 /* The size of the test element that must pass the test */ 203 #ifndef MHD_ASAN_POISON_ACTIVE 204 #define TEST_OK_SIZE (BUFFER_SIZE - 384) 205 #else /* MHD_ASAN_POISON_ACTIVE */ 206 #define TEST_OK_SIZE (BUFFER_SIZE - 384 - 80) 207 #endif /* MHD_ASAN_POISON_ACTIVE */ 208 209 /* The size of the test element where tests are started */ 210 #define TEST_START_SIZE (TEST_OK_SIZE - 16) 211 212 /* The size of the test element that must definitely fail */ 213 #define TEST_FAIL_SIZE (BUFFER_SIZE + 32) 214 215 /* Special value for request many headers test. 216 * MHD uses the same buffer to store headers strings and pointers to the strings 217 * so allocation is multiplied for small request header. */ 218 /* The size of the test element that must pass the test */ 219 #define TEST_RQ_N1_OK_SIZE 50 220 221 /* The size of the test element where tests are started */ 222 #define TEST_RQ_N1_START_SIZE (TEST_RQ_N1_OK_SIZE - 32) 223 224 /* Global parameters */ 225 static int verbose; /**< Be verbose */ 226 static int oneone; /**< If false use HTTP/1.0 for requests*/ 227 static uint16_t global_port; /**< MHD daemons listen port number */ 228 static int large_req_method; /**< Large request method */ 229 static int large_req_url; /**< Large request URL */ 230 static int large_req_header_name; /**< Large request single header name */ 231 static int large_req_header_value; /**< Large request single header value */ 232 static int large_req_headers; /**< Large request headers */ 233 static int large_rsp_header_name; /**< Large response single header name */ 234 static int large_rsp_header_value; /**< Large response single header value */ 235 static int large_rsp_headers; /**< Large response headers */ 236 static int response_timeout_val = TIMEOUTS_VAL; 237 238 /* Current test parameters */ 239 /* * Moved to local variables * */ 240 241 /* Static helper variables */ 242 /* * None for this test * */ 243 244 static void 245 test_global_init (void) 246 { 247 libcurl_errbuf[0] = 0; 248 249 if (0 != curl_global_init (CURL_GLOBAL_WIN32)) 250 externalErrorExit (); 251 } 252 253 254 static void 255 test_global_cleanup (void) 256 { 257 curl_global_cleanup (); 258 } 259 260 261 struct headers_check_result 262 { 263 unsigned int num_n1_headers; 264 size_t large_header_name_size; 265 size_t large_header_value_size; 266 int large_header_valid; 267 }; 268 269 static size_t 270 lcurl_hdr_callback (char *buffer, size_t size, size_t nitems, 271 void *userdata) 272 { 273 const size_t data_size = size * nitems; 274 struct headers_check_result *check_res = 275 (struct headers_check_result *) userdata; 276 277 if ((6 == data_size) && (0 == memcmp (N1_HEADER_CRLF, buffer, 6))) 278 check_res->num_n1_headers++; 279 else if ((5 <= data_size) && ('0' == buffer[0])) 280 { 281 const char *const col_ptr = memchr (buffer, ':', data_size); 282 if (0 != check_res->large_header_value_size) 283 mhdErrorExitDesc ("Expected only one large header, " \ 284 "but found two large headers in the reply"); 285 if (NULL == col_ptr) 286 check_res->large_header_valid = 0; 287 else if ((size_t) (col_ptr - buffer) >= data_size - 2) 288 check_res->large_header_valid = 0; 289 else if (*(col_ptr + 1) != ' ') 290 check_res->large_header_valid = 0; 291 else 292 { 293 const char *const name = buffer; 294 const size_t name_len = (size_t) (col_ptr - buffer); 295 const size_t val_pos = name_len + 2; 296 const size_t val_len = data_size - val_pos - 2; /* 2 = strlen("\r\n") */ 297 const char *const value = buffer + val_pos; 298 size_t i; 299 check_res->large_header_name_size = name_len; 300 check_res->large_header_value_size = val_len; 301 check_res->large_header_valid = 1; /* To be reset if any problem found */ 302 for (i = 1; i < name_len; i++) 303 if ('a' + (char) (i % ('z' - 'a' + 1)) != name[i]) 304 { 305 fprintf (stderr, "Wrong sequence in reply header name " \ 306 "at position %u. Expected '%c', got '%c'\n", 307 (unsigned int) i, 308 'a' + (char) (i % ('z' - 'a' + 1)), 309 name[i]); 310 check_res->large_header_valid = 0; 311 break; 312 } 313 for (i = 0; i < val_len; i++) 314 if ('Z' - (char) (i % ('Z' - 'A' + 1)) != value[i]) 315 { 316 fprintf (stderr, "Wrong sequence in reply header value " \ 317 "at position %u. Expected '%c', got '%c'\n", 318 (unsigned int) i, 319 'Z' - (char) (i % ('Z' - 'A' + 1)), 320 value[i]); 321 check_res->large_header_valid = 0; 322 break; 323 } 324 } 325 } 326 327 return data_size; 328 } 329 330 331 struct lcurl_data_cb_param 332 { 333 char *buf; 334 size_t pos; 335 size_t size; 336 }; 337 338 339 static size_t 340 copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx) 341 { 342 struct lcurl_data_cb_param *cbc = ctx; 343 344 if (cbc->pos + size * nmemb > cbc->size) 345 externalErrorExit (); /* overflow */ 346 memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb); 347 cbc->pos += size * nmemb; 348 return size * nmemb; 349 } 350 351 352 struct check_uri_cls 353 { 354 const char *volatile uri; 355 }; 356 357 static void * 358 check_uri_cb (void *cls, 359 const char *uri, 360 struct MHD_Connection *con) 361 { 362 struct check_uri_cls *param = (struct check_uri_cls *) cls; 363 (void) con; 364 365 if (0 != strcmp (param->uri, 366 uri)) 367 { 368 fprintf (stderr, 369 "Wrong URI: `%s', line: %d\n", 370 uri, __LINE__); 371 exit (22); 372 } 373 return NULL; 374 } 375 376 377 struct mhd_header_checker_param 378 { 379 unsigned int num_n1_headers; 380 size_t large_header_name_size; 381 size_t large_header_value_size; 382 int large_header_valid; 383 }; 384 385 static enum MHD_Result 386 headerCheckerInterator (void *cls, 387 enum MHD_ValueKind kind, 388 const char *key, 389 size_t key_size, 390 const char *value, 391 size_t value_size) 392 { 393 struct mhd_header_checker_param *const param = 394 (struct mhd_header_checker_param *) cls; 395 396 if (NULL == param) 397 mhdErrorExitDesc ("cls parameter is NULL"); 398 399 if (MHD_HEADER_KIND != kind) 400 return MHD_YES; /* Continue iteration */ 401 402 if (0 == key_size) 403 mhdErrorExitDesc ("Zero key length"); 404 405 if ((1 == key_size) && (1 == value_size) && 406 ('n' == key[0]) && ('1' == value[0])) 407 param->num_n1_headers++; 408 else if ('0' == key[0]) 409 { /* Found 'large' header */ 410 size_t i; 411 param->large_header_name_size = key_size; 412 param->large_header_value_size = value_size; 413 param->large_header_valid = 1; 414 for (i = 1; i < key_size; i++) 415 if ('a' + (char) (i % ('z' - 'a' + 1)) != key[i]) 416 { 417 fprintf (stderr, "Wrong sequence in request header name " \ 418 "at position %u. Expected '%c', got '%c'\n", 419 (unsigned int) i, 420 'a' + (char) (i % ('z' - 'a' + 1)), 421 key[i]); 422 param->large_header_valid = 0; 423 break; 424 } 425 for (i = 0; i < value_size; i++) 426 if ('Z' - (char) (i % ('Z' - 'A' + 1)) != value[i]) 427 { 428 fprintf (stderr, "Wrong sequence in request header value " \ 429 "at position %u. Expected '%c', got '%c'\n", 430 (unsigned int) i, 431 'Z' - (char) (i % ('Z' - 'A' + 1)), 432 value[i]); 433 param->large_header_valid = 0; 434 break; 435 } 436 } 437 return MHD_YES; 438 } 439 440 441 struct ahc_cls_type 442 { 443 const char *volatile rp_data; 444 volatile size_t rp_data_size; 445 volatile size_t rp_num_n1_hdrs; 446 volatile size_t rp_large_hdr_name_size; 447 volatile size_t rp_large_hdr_value_size; 448 struct mhd_header_checker_param header_check_param; 449 const char *volatile rq_method; 450 const char *volatile rq_url; 451 }; 452 453 454 static enum MHD_Result 455 ahcCheck (void *cls, 456 struct MHD_Connection *connection, 457 const char *url, 458 const char *method, 459 const char *version, 460 const char *upload_data, size_t *upload_data_size, 461 void **req_cls) 462 { 463 static int ptr; 464 struct MHD_Response *response; 465 enum MHD_Result ret; 466 struct ahc_cls_type *const param = (struct ahc_cls_type *) cls; 467 size_t i; 468 469 if (NULL == param) 470 mhdErrorExitDesc ("cls parameter is NULL"); 471 472 if (0 != strcmp (version, MHD_HTTP_VERSION_1_1)) 473 mhdErrorExitDesc ("Unexpected HTTP version"); 474 475 if (0 != strcmp (url, param->rq_url)) 476 mhdErrorExitDesc ("Unexpected URI"); 477 478 if (NULL != upload_data) 479 mhdErrorExitDesc ("'upload_data' is not NULL"); 480 481 if (NULL == upload_data_size) 482 mhdErrorExitDesc ("'upload_data_size' pointer is NULL"); 483 484 if (0 != *upload_data_size) 485 mhdErrorExitDesc ("'*upload_data_size' value is not zero"); 486 487 if (0 != strcmp (param->rq_method, method)) 488 mhdErrorExitDesc ("Unexpected request method"); 489 490 if (&ptr != *req_cls) 491 { 492 *req_cls = &ptr; 493 return MHD_YES; 494 } 495 *req_cls = NULL; 496 497 if (1 > MHD_get_connection_values_n (connection, MHD_HEADER_KIND, 498 &headerCheckerInterator, 499 ¶m->header_check_param)) 500 mhdErrorExitDesc ("Wrong number of headers in the request"); 501 502 response = 503 MHD_create_response_from_buffer_copy (param->rp_data_size, 504 (const void *) param->rp_data); 505 if (NULL == response) 506 mhdErrorExitDesc ("Failed to create response"); 507 508 for (i = 0; i < param->rp_num_n1_hdrs; i++) 509 if (MHD_YES != MHD_add_response_header (response, 510 N1_HEADER_NAME, 511 N1_HEADER_VALUE)) 512 mhdErrorExitDesc ("Cannot add header"); 513 514 if (0 != param->rp_large_hdr_name_size) 515 { 516 const size_t large_hdr_name_size = param->rp_large_hdr_name_size; 517 char *large_hrd_name; 518 const size_t large_hdr_value_size = param->rp_large_hdr_value_size; 519 char *large_hrd_value; 520 521 large_hrd_name = malloc (large_hdr_name_size + 1); 522 if (NULL == large_hrd_name) 523 externalErrorExit (); 524 large_hrd_value = malloc (large_hdr_value_size + 1); 525 if (NULL == large_hrd_value) 526 externalErrorExit (); 527 large_hrd_name[0] = '0'; /* Name starts with zero for unique identification */ 528 for (i = 1; i < large_hdr_name_size; i++) 529 large_hrd_name[i] = 'a' + (char) (unsigned char) (i % ('z' - 'a' + 1)); 530 large_hrd_name[large_hdr_name_size] = 0; 531 for (i = 0; i < large_hdr_value_size; i++) 532 large_hrd_value[i] = 'Z' - (char) (unsigned char) (i % ('Z' - 'A' + 1)); 533 large_hrd_value[large_hdr_value_size] = 0; 534 if (MHD_YES != MHD_add_response_header (response, 535 large_hrd_name, 536 large_hrd_value)) 537 mhdErrorExitDesc ("Cannot add large header"); 538 539 free (large_hrd_value); 540 free (large_hrd_name); 541 } 542 543 ret = MHD_queue_response (connection, 544 MHD_HTTP_OK, 545 response); 546 MHD_destroy_response (response); 547 if (MHD_YES != ret) 548 mhdErrorExitDesc ("Failed to queue response"); 549 550 return ret; 551 } 552 553 554 static CURL * 555 curlEasyInitForTest (const char *queryPath, const char *method, 556 uint16_t port, 557 struct lcurl_data_cb_param *dcbp, 558 struct headers_check_result *hdr_chk_result, 559 struct curl_slist *headers) 560 { 561 CURL *c; 562 563 c = curl_easy_init (); 564 if (NULL == c) 565 libcurlErrorExitDesc ("curl_easy_init() failed"); 566 567 if ((CURLE_OK != curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L)) || 568 (CURLE_OK != curl_easy_setopt (c, CURLOPT_PROXY, "")) || 569 (CURLE_OK != curl_easy_setopt (c, CURLOPT_URL, queryPath)) || 570 (CURLE_OK != curl_easy_setopt (c, CURLOPT_PORT, (long) port)) || 571 (CURLE_OK != curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, 572 ©Buffer)) || 573 (CURLE_OK != curl_easy_setopt (c, CURLOPT_WRITEDATA, dcbp)) || 574 (CURLE_OK != curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 575 (long) response_timeout_val)) || 576 (CURLE_OK != curl_easy_setopt (c, CURLOPT_TIMEOUT, 577 (long) response_timeout_val)) || 578 (CURLE_OK != curl_easy_setopt (c, CURLOPT_ERRORBUFFER, 579 libcurl_errbuf)) || 580 (CURLE_OK != curl_easy_setopt (c, CURLOPT_HEADERFUNCTION, 581 lcurl_hdr_callback)) || 582 (CURLE_OK != curl_easy_setopt (c, CURLOPT_HEADERDATA, 583 hdr_chk_result)) || 584 (CURLE_OK != curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L)) || 585 (CURLE_OK != curl_easy_setopt (c, CURLOPT_HTTP_VERSION, 586 (oneone) ? 587 CURL_HTTP_VERSION_1_1 : 588 CURL_HTTP_VERSION_1_0))) 589 libcurlErrorExitDesc ("curl_easy_setopt() failed"); 590 591 if (CURLE_OK != curl_easy_setopt (c, CURLOPT_CUSTOMREQUEST, method)) 592 libcurlErrorExitDesc ("curl_easy_setopt() failed"); 593 594 if (CURLE_OK != curl_easy_setopt (c, CURLOPT_HTTPHEADER, headers)) 595 libcurlErrorExitDesc ("curl_easy_setopt() failed"); 596 597 return c; 598 } 599 600 601 static CURLcode 602 performQueryExternal (struct MHD_Daemon *d, CURL *c) 603 { 604 CURLM *multi; 605 time_t start; 606 struct timeval tv; 607 CURLcode ret; 608 609 ret = CURLE_FAILED_INIT; /* will be replaced with real result */ 610 multi = NULL; 611 multi = curl_multi_init (); 612 if (multi == NULL) 613 libcurlErrorExitDesc ("curl_multi_init() failed"); 614 if (CURLM_OK != curl_multi_add_handle (multi, c)) 615 libcurlErrorExitDesc ("curl_multi_add_handle() failed"); 616 617 start = time (NULL); 618 while (time (NULL) - start <= TIMEOUTS_VAL) 619 { 620 fd_set rs; 621 fd_set ws; 622 fd_set es; 623 MHD_socket maxMhdSk; 624 int maxCurlSk; 625 int running; 626 627 maxMhdSk = MHD_INVALID_SOCKET; 628 maxCurlSk = -1; 629 FD_ZERO (&rs); 630 FD_ZERO (&ws); 631 FD_ZERO (&es); 632 if (NULL != multi) 633 { 634 curl_multi_perform (multi, &running); 635 if (0 == running) 636 { 637 struct CURLMsg *msg; 638 int msgLeft; 639 int totalMsgs = 0; 640 do 641 { 642 msg = curl_multi_info_read (multi, &msgLeft); 643 if (NULL == msg) 644 libcurlErrorExitDesc ("curl_multi_info_read() failed"); 645 totalMsgs++; 646 if (CURLMSG_DONE == msg->msg) 647 ret = msg->data.result; 648 } while (msgLeft > 0); 649 if (1 != totalMsgs) 650 { 651 fprintf (stderr, 652 "curl_multi_info_read returned wrong " 653 "number of results (%d).\n", 654 totalMsgs); 655 externalErrorExit (); 656 } 657 curl_multi_remove_handle (multi, c); 658 curl_multi_cleanup (multi); 659 multi = NULL; 660 } 661 else 662 { 663 if (CURLM_OK != curl_multi_fdset (multi, &rs, &ws, &es, &maxCurlSk)) 664 libcurlErrorExitDesc ("curl_multi_fdset() failed"); 665 } 666 } 667 if (NULL == multi) 668 { /* libcurl has finished, check whether MHD still needs to perform cleanup */ 669 if (0 != MHD_get_timeout64s (d)) 670 break; /* MHD finished as well */ 671 } 672 if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &maxMhdSk)) 673 mhdErrorExitDesc ("MHD_get_fdset() failed"); 674 tv.tv_sec = 0; 675 tv.tv_usec = 1000; 676 #ifdef MHD_POSIX_SOCKETS 677 if (maxMhdSk > maxCurlSk) 678 maxCurlSk = maxMhdSk; 679 #endif /* MHD_POSIX_SOCKETS */ 680 if (-1 == select (maxCurlSk + 1, &rs, &ws, &es, &tv)) 681 { 682 #ifdef MHD_POSIX_SOCKETS 683 if (EINTR != errno) 684 externalErrorExitDesc ("Unexpected select() error"); 685 #else 686 if ((WSAEINVAL != WSAGetLastError ()) || 687 (0 != rs.fd_count) || (0 != ws.fd_count) || (0 != es.fd_count) ) 688 externalErrorExitDesc ("Unexpected select() error"); 689 Sleep (1); 690 #endif 691 } 692 if (MHD_YES != MHD_run_from_select (d, &rs, &ws, &es)) 693 mhdErrorExitDesc ("MHD_run_from_select() failed"); 694 } 695 696 return ret; 697 } 698 699 700 struct curlQueryParams 701 { 702 /* Destination path for CURL query */ 703 const char *queryPath; 704 705 /* Custom query method, NULL for default */ 706 const char *method; 707 708 /* Destination port for CURL query */ 709 uint16_t queryPort; 710 711 /* List of additional request headers */ 712 struct curl_slist *headers; 713 714 /* CURL query result error flag */ 715 volatile unsigned int queryError; 716 717 /* Response HTTP code, zero if no response */ 718 volatile int responseCode; 719 }; 720 721 722 /* Returns zero for successful response and non-zero for failed response */ 723 static unsigned int 724 doCurlQueryInThread (struct MHD_Daemon *d, 725 struct curlQueryParams *p, 726 struct headers_check_result *hdr_res, 727 const char *expected_data, 728 size_t expected_data_size) 729 { 730 const union MHD_DaemonInfo *dinfo; 731 CURL *c; 732 struct lcurl_data_cb_param dcbp; 733 CURLcode errornum; 734 int use_external_poll; 735 long resp_code; 736 737 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_FLAGS); 738 if (NULL == dinfo) 739 mhdErrorExitDesc ("MHD_get_daemon_info() failed"); 740 use_external_poll = (0 == (dinfo->flags 741 & MHD_USE_INTERNAL_POLLING_THREAD)); 742 743 if (NULL == p->queryPath) 744 abort (); 745 746 if (0 == p->queryPort) 747 abort (); 748 749 /* Test must not fail due to test's internal buffer shortage */ 750 dcbp.size = TEST_FAIL_SIZE * 2 + 1; 751 dcbp.buf = malloc (dcbp.size); 752 if (NULL == dcbp.buf) 753 externalErrorExit (); 754 dcbp.pos = 0; 755 756 memset (hdr_res, 0, sizeof(*hdr_res)); 757 758 c = curlEasyInitForTest (p->queryPath, p->method, p->queryPort, 759 &dcbp, hdr_res, p->headers); 760 761 if (! use_external_poll) 762 errornum = curl_easy_perform (c); 763 else 764 errornum = performQueryExternal (d, c); 765 766 if (CURLE_OK != curl_easy_getinfo (c, CURLINFO_RESPONSE_CODE, &resp_code)) 767 libcurlErrorExitDesc ("curl_easy_getinfo() failed"); 768 769 p->responseCode = (int) resp_code; 770 if ((CURLE_OK == errornum) && (200 != resp_code)) 771 { 772 fprintf (stderr, 773 "Got reply with unexpected status code: %d\n", 774 p->responseCode); 775 mhdErrorExit (); 776 } 777 778 if (CURLE_OK != errornum) 779 { 780 if ((CURLE_GOT_NOTHING != errornum) && (CURLE_RECV_ERROR != errornum) 781 && (CURLE_HTTP_RETURNED_ERROR != errornum)) 782 { 783 if (CURLE_OPERATION_TIMEDOUT == errornum) 784 mhdErrorExitDesc ("Request was aborted due to timeout"); 785 fprintf (stderr, "libcurl returned unexpected error: %s\n", 786 curl_easy_strerror (errornum)); 787 mhdErrorExitDesc ("Request failed due to unexpected error"); 788 } 789 p->queryError = 1; 790 switch (resp_code) 791 { 792 case 0: /* No parsed response */ 793 case 413: /* "Content Too Large" */ 794 case 414: /* "URI Too Long" */ 795 case 431: /* "Request Header Fields Too Large" */ 796 case 501: /* "Not Implemented" */ 797 /* Expected error codes */ 798 break; 799 default: 800 fprintf (stderr, 801 "Got reply with unexpected status code: %ld\n", 802 resp_code); 803 mhdErrorExit (); 804 } 805 } 806 else 807 { 808 if (dcbp.pos != expected_data_size) 809 mhdErrorExit ("libcurl reports wrong size of MHD reply body data"); 810 else if (0 != memcmp (expected_data, dcbp.buf, 811 expected_data_size)) 812 mhdErrorExit ("libcurl reports wrong MHD reply body data"); 813 else 814 p->queryError = 0; 815 } 816 817 curl_easy_cleanup (c); 818 free (dcbp.buf); 819 820 return p->queryError; 821 } 822 823 824 /* Perform test queries, shut down MHD daemon, and free parameters */ 825 static unsigned int 826 performTestQueries (struct MHD_Daemon *d, uint16_t d_port, 827 struct ahc_cls_type *ahc_param, 828 struct check_uri_cls *uri_cb_param) 829 { 830 struct curlQueryParams qParam; 831 unsigned int ret = 0; /* Return value */ 832 struct headers_check_result rp_headers_check; 833 char *buf; 834 size_t i; 835 size_t first_failed_at = 0; 836 837 838 buf = malloc (TEST_FAIL_SIZE + 1 + strlen (URL_SCHEME_HOST)); 839 if (NULL == buf) 840 externalErrorExit (); 841 842 /* Common parameters, to be individually overridden by specific test cases */ 843 qParam.queryPort = d_port; 844 qParam.method = NULL; /* Use libcurl default: GET */ 845 qParam.queryPath = URL_SCHEME_HOST EXPECTED_URI_BASE_PATH; 846 qParam.headers = NULL; /* No additional headers */ 847 uri_cb_param->uri = EXPECTED_URI_BASE_PATH; 848 ahc_param->rq_url = EXPECTED_URI_BASE_PATH; 849 ahc_param->rq_method = "GET"; /* Default expected method */ 850 851 ahc_param->rp_data = "~"; 852 ahc_param->rp_data_size = 1; 853 ahc_param->rp_large_hdr_name_size = 0; 854 ahc_param->rp_large_hdr_value_size = 0; 855 ahc_param->rp_num_n1_hdrs = 0; 856 857 if (large_req_method) 858 { 859 for (i = 0; i < TEST_START_SIZE; i++) 860 buf[i] = 'A' + (char) (unsigned char) (i % ('Z' - 'A' + 1)); 861 for (; i <= TEST_FAIL_SIZE; i++) 862 { 863 buf[i] = 0; 864 865 qParam.method = buf; 866 ahc_param->rq_method = buf; 867 868 memset (&ahc_param->header_check_param, 0, 869 sizeof (ahc_param->header_check_param)); 870 if (0 != doCurlQueryInThread (d, &qParam, &rp_headers_check, 871 ahc_param->rp_data, 872 ahc_param->rp_data_size)) 873 { 874 if ((0 != qParam.responseCode) && (501 != qParam.responseCode)) 875 { 876 fprintf (stderr, 877 "Got reply with status code %d, " 878 "while code 501 (\"Not Implemented\") is expected.\n", 879 qParam.responseCode); 880 mhdErrorExit (); 881 } 882 if (TEST_OK_SIZE >= i) 883 { 884 fprintf (stderr, 885 "Request failed when running with the valid value size.\n"); 886 ret = 1; /* Failed too early */ 887 } 888 if (0 == first_failed_at) 889 { 890 if (verbose) 891 fprintf (stderr, "First failed size is %u.\n", (unsigned int) i); 892 first_failed_at = i; 893 } 894 } 895 else 896 { 897 if (TEST_FAIL_SIZE == i) 898 { 899 fprintf (stderr, "Request succeed with the largest size.\n"); 900 ret = 1; /* Succeed with largest value */ 901 } 902 } 903 if (0 != ahc_param->header_check_param.num_n1_headers) 904 mhdErrorExitDesc ("Detected unexpected request headers"); 905 if (0 != ahc_param->header_check_param.large_header_name_size) 906 mhdErrorExitDesc ("Detected unexpected large request header"); 907 if (0 != rp_headers_check.num_n1_headers) 908 mhdErrorExitDesc ("Detected unexpected reply headers"); 909 if (0 != rp_headers_check.large_header_name_size) 910 mhdErrorExitDesc ("Detected unexpected large reply header"); 911 912 buf[i] = 'A' + (char) (unsigned char) (i % ('Z' - 'A' + 1)); 913 } 914 } 915 else if (large_req_url) 916 { 917 const size_t base_size = strlen (URL_SCHEME_HOST); 918 char *const url = buf + base_size; 919 920 memcpy (buf, URL_SCHEME_HOST, base_size); 921 url[0] = '/'; 922 for (i = 1; i < TEST_START_SIZE; i++) 923 url[i] = 'a' + (char) (unsigned char) (i % ('z' - 'a' + 1)); 924 for (; i <= TEST_FAIL_SIZE; i++) 925 { 926 url[i] = 0; 927 928 qParam.queryPath = buf; 929 uri_cb_param->uri = url; 930 ahc_param->rq_url = url; 931 932 memset (&ahc_param->header_check_param, 0, 933 sizeof (ahc_param->header_check_param)); 934 if (0 != doCurlQueryInThread (d, &qParam, &rp_headers_check, 935 ahc_param->rp_data, 936 ahc_param->rp_data_size)) 937 { 938 if ((0 != qParam.responseCode) && (414 != qParam.responseCode)) 939 { 940 fprintf (stderr, 941 "Got reply with status code %d, " 942 "while code 414 (\"URI Too Long\") is expected.\n", 943 qParam.responseCode); 944 mhdErrorExit (); 945 } 946 if (TEST_OK_SIZE >= i) 947 { 948 fprintf (stderr, 949 "Request failed when running with the valid value size.\n"); 950 ret = 1; /* Failed too early */ 951 } 952 if (0 == first_failed_at) 953 { 954 if (verbose) 955 fprintf (stderr, "First failed size is %u.\n", (unsigned int) i); 956 first_failed_at = i; 957 } 958 } 959 else 960 { 961 if (TEST_FAIL_SIZE == i) 962 { 963 fprintf (stderr, "Request succeed with the largest size.\n"); 964 ret = 1; /* Succeed with largest value */ 965 } 966 } 967 if (0 != ahc_param->header_check_param.num_n1_headers) 968 mhdErrorExitDesc ("Detected unexpected request headers"); 969 if (0 != ahc_param->header_check_param.large_header_name_size) 970 mhdErrorExitDesc ("Detected unexpected large request header"); 971 if (0 != rp_headers_check.num_n1_headers) 972 mhdErrorExitDesc ("Detected unexpected reply headers"); 973 if (0 != rp_headers_check.large_header_name_size) 974 mhdErrorExitDesc ("Detected unexpected large reply header"); 975 976 url[i] = 'a' + (char) (unsigned char) (i % ('z' - 'a' + 1)); 977 } 978 } 979 else if (large_req_header_name) 980 { 981 buf[0] = '0'; /* Name starts with zero for unique identification */ 982 for (i = 1; i < TEST_START_SIZE; i++) 983 buf[i] = 'a' + (char) (unsigned char) (i % ('z' - 'a' + 1)); 984 for (; i <= TEST_FAIL_SIZE; i++) 985 { 986 struct curl_slist *curl_headers; 987 curl_headers = NULL; 988 989 memcpy (buf + i, ": Z", 3); /* Note: strlen(": Z") is less than strlen(URL_SCHEME_HOST) */ 990 buf[i + 3] = 0; 991 992 curl_headers = curl_slist_append (curl_headers, buf); 993 if (NULL == curl_headers) 994 externalErrorExit (); 995 996 qParam.headers = curl_headers; 997 998 memset (&ahc_param->header_check_param, 0, 999 sizeof (ahc_param->header_check_param)); 1000 if (0 != doCurlQueryInThread (d, &qParam, &rp_headers_check, 1001 ahc_param->rp_data, 1002 ahc_param->rp_data_size)) 1003 { 1004 if ((0 != qParam.responseCode) && (431 != qParam.responseCode)) 1005 { 1006 fprintf (stderr, 1007 "Got reply with status code %d, while code 431 " 1008 "(\"Request Header Fields Too Large\") is expected.\n", 1009 qParam.responseCode); 1010 mhdErrorExit (); 1011 } 1012 if (0 != ahc_param->header_check_param.large_header_name_size) 1013 { /* If large header was processed, it must be valid */ 1014 if (i != ahc_param->header_check_param.large_header_name_size) 1015 mhdErrorExitDesc ("Detected wrong large request header name size"); 1016 if (1 != ahc_param->header_check_param.large_header_value_size) 1017 mhdErrorExitDesc ("Detected wrong large request header value size"); 1018 if (0 == ahc_param->header_check_param.large_header_valid) 1019 mhdErrorExitDesc ("Detected wrong large request header"); 1020 } 1021 if (TEST_OK_SIZE >= i) 1022 { 1023 fprintf (stderr, 1024 "Request failed when running with the valid value size.\n"); 1025 ret = 1; /* Failed too early */ 1026 } 1027 if (0 == first_failed_at) 1028 { 1029 if (verbose) 1030 fprintf (stderr, "First failed size is %u.\n", (unsigned int) i); 1031 first_failed_at = i; 1032 } 1033 } 1034 else 1035 { 1036 if (i != ahc_param->header_check_param.large_header_name_size) 1037 mhdErrorExitDesc ("Detected wrong large request header name size"); 1038 if (1 != ahc_param->header_check_param.large_header_value_size) 1039 mhdErrorExitDesc ("Detected wrong large request header value size"); 1040 if (0 == ahc_param->header_check_param.large_header_valid) 1041 mhdErrorExitDesc ("Detected wrong large request header"); 1042 if (TEST_FAIL_SIZE == i) 1043 { 1044 fprintf (stderr, "Request succeed with the largest size.\n"); 1045 ret = 1; /* Succeed with largest value */ 1046 } 1047 } 1048 if (0 != ahc_param->header_check_param.num_n1_headers) 1049 mhdErrorExitDesc ("Detected unexpected request headers"); 1050 if (0 != rp_headers_check.num_n1_headers) 1051 mhdErrorExitDesc ("Detected unexpected reply headers"); 1052 if (0 != rp_headers_check.large_header_name_size) 1053 mhdErrorExitDesc ("Detected unexpected large reply header"); 1054 1055 curl_slist_free_all (curl_headers); 1056 buf[i] = 'a' + (char) (unsigned char) (i % ('z' - 'a' + 1)); 1057 } 1058 } 1059 else if (large_req_header_value) 1060 { 1061 char *const hdr_value = buf + 3; 1062 /* Name starts with zero for unique identification */ 1063 memcpy (buf, "0: ", 3); /* Note: strlen(": Z") is less than strlen(URL_SCHEME_HOST) */ 1064 for (i = 0; i < TEST_START_SIZE; i++) 1065 hdr_value[i] = 'Z' - (char) (unsigned char) (i % ('Z' - 'A' + 1)); 1066 for (; i <= TEST_FAIL_SIZE; i++) 1067 { 1068 struct curl_slist *curl_headers; 1069 curl_headers = NULL; 1070 1071 hdr_value[i] = 0; 1072 1073 curl_headers = curl_slist_append (curl_headers, buf); 1074 if (NULL == curl_headers) 1075 externalErrorExit (); 1076 1077 qParam.headers = curl_headers; 1078 1079 memset (&ahc_param->header_check_param, 0, 1080 sizeof (ahc_param->header_check_param)); 1081 if (0 != doCurlQueryInThread (d, &qParam, &rp_headers_check, 1082 ahc_param->rp_data, 1083 ahc_param->rp_data_size)) 1084 { 1085 if ((0 != qParam.responseCode) && (431 != qParam.responseCode)) 1086 { 1087 fprintf (stderr, 1088 "Got reply with status code %d, while code 431 " 1089 "(\"Request Header Fields Too Large\") is expected.\n", 1090 qParam.responseCode); 1091 mhdErrorExit (); 1092 } 1093 if (0 != ahc_param->header_check_param.large_header_name_size) 1094 { /* If large header was processed, it must be valid */ 1095 if (1 != ahc_param->header_check_param.large_header_name_size) 1096 mhdErrorExitDesc ("Detected wrong large request header name size"); 1097 if (i != ahc_param->header_check_param.large_header_value_size) 1098 mhdErrorExitDesc ("Detected wrong large request header value size"); 1099 if (0 == ahc_param->header_check_param.large_header_valid) 1100 mhdErrorExitDesc ("Detected wrong large request header"); 1101 } 1102 if (TEST_OK_SIZE >= i) 1103 { 1104 fprintf (stderr, 1105 "Request failed when running with the valid value size.\n"); 1106 ret = 1; /* Failed too early */ 1107 } 1108 if (0 == first_failed_at) 1109 { 1110 if (verbose) 1111 fprintf (stderr, "First failed size is %u.\n", (unsigned int) i); 1112 first_failed_at = i; 1113 } 1114 } 1115 else 1116 { 1117 if (1 != ahc_param->header_check_param.large_header_name_size) 1118 mhdErrorExitDesc ("Detected wrong large request header name size"); 1119 if (i != ahc_param->header_check_param.large_header_value_size) 1120 mhdErrorExitDesc ("Detected wrong large request header value size"); 1121 if (0 == ahc_param->header_check_param.large_header_valid) 1122 mhdErrorExitDesc ("Detected wrong large request header"); 1123 if (TEST_FAIL_SIZE == i) 1124 { 1125 fprintf (stderr, "Request succeed with the largest size.\n"); 1126 ret = 1; /* Succeed with largest value */ 1127 } 1128 } 1129 if (0 != ahc_param->header_check_param.num_n1_headers) 1130 mhdErrorExitDesc ("Detected unexpected request headers"); 1131 if (0 != rp_headers_check.num_n1_headers) 1132 mhdErrorExitDesc ("Detected unexpected reply headers"); 1133 if (0 != rp_headers_check.large_header_name_size) 1134 mhdErrorExitDesc ("Detected unexpected large reply header"); 1135 1136 curl_slist_free_all (curl_headers); 1137 hdr_value[i] = 'Z' - (char) (unsigned char) (i % ('Z' - 'A' + 1)); 1138 } 1139 } 1140 else if (large_req_headers) 1141 { 1142 unsigned int num_hdrs = 0; 1143 struct curl_slist *curl_headers; 1144 const size_t hdr_size = strlen (N1_HEADER_CRLF); 1145 1146 curl_headers = NULL; 1147 1148 for (i = 0; i < TEST_RQ_N1_START_SIZE; i += hdr_size) 1149 { 1150 curl_headers = curl_slist_append (curl_headers, N1_HEADER); 1151 if (NULL == curl_headers) 1152 externalErrorExit (); 1153 num_hdrs++; 1154 } 1155 for (; i <= TEST_FAIL_SIZE; i += hdr_size) 1156 { 1157 qParam.headers = curl_headers; 1158 ahc_param->header_check_param.num_n1_headers = num_hdrs; 1159 1160 memset (&ahc_param->header_check_param, 0, 1161 sizeof (ahc_param->header_check_param)); 1162 if (0 != doCurlQueryInThread (d, &qParam, &rp_headers_check, 1163 ahc_param->rp_data, 1164 ahc_param->rp_data_size)) 1165 { 1166 if ((0 != qParam.responseCode) && (431 != qParam.responseCode)) 1167 { 1168 fprintf (stderr, 1169 "Got reply with status code %d, while code 431 " 1170 "(\"Request Header Fields Too Large\") is expected.\n", 1171 qParam.responseCode); 1172 mhdErrorExit (); 1173 } 1174 if (0 != ahc_param->header_check_param.num_n1_headers) 1175 { /* If headers were processed, they must be valid */ 1176 if (num_hdrs != ahc_param->header_check_param.num_n1_headers) 1177 mhdErrorExitDesc ("Detected wrong number of request headers"); 1178 } 1179 if (TEST_RQ_N1_OK_SIZE >= i) 1180 { 1181 fprintf (stderr, 1182 "Request failed when running with the valid value size.\n"); 1183 ret = 1; /* Failed too early */ 1184 } 1185 if (0 == first_failed_at) 1186 { 1187 if (verbose) 1188 fprintf (stderr, "First failed size is %u.\n", (unsigned int) i); 1189 first_failed_at = i; 1190 } 1191 } 1192 else 1193 { 1194 if (num_hdrs != ahc_param->header_check_param.num_n1_headers) 1195 mhdErrorExitDesc ("Detected wrong number of request headers"); 1196 if (TEST_FAIL_SIZE == i) 1197 { 1198 fprintf (stderr, "Request succeed with the largest size.\n"); 1199 ret = 1; /* Succeed with largest value */ 1200 } 1201 } 1202 if (0 != ahc_param->header_check_param.large_header_name_size) 1203 mhdErrorExitDesc ("Detected unexpected large request header"); 1204 if (0 != rp_headers_check.num_n1_headers) 1205 mhdErrorExitDesc ("Detected unexpected reply headers"); 1206 if (0 != rp_headers_check.large_header_name_size) 1207 mhdErrorExitDesc ("Detected unexpected large reply header"); 1208 1209 curl_headers = curl_slist_append (curl_headers, N1_HEADER); 1210 if (NULL == curl_headers) 1211 externalErrorExit (); 1212 num_hdrs++; 1213 } 1214 curl_slist_free_all (curl_headers); 1215 } 1216 else if (large_rsp_header_name) 1217 { 1218 for (i = TEST_START_SIZE; i <= TEST_FAIL_SIZE; i++) 1219 { 1220 ahc_param->rp_large_hdr_name_size = i; 1221 ahc_param->rp_large_hdr_value_size = 1; 1222 1223 memset (&ahc_param->header_check_param, 0, 1224 sizeof (ahc_param->header_check_param)); 1225 if (0 != doCurlQueryInThread (d, &qParam, &rp_headers_check, 1226 ahc_param->rp_data, 1227 ahc_param->rp_data_size)) 1228 { 1229 (void) qParam.responseCode; /* TODO: check for the right response code */ 1230 if (0 != rp_headers_check.large_header_name_size) 1231 mhdErrorExitDesc ("Detected unexpected large reply header"); 1232 if (TEST_OK_SIZE >= i) 1233 { 1234 fprintf (stderr, 1235 "Request failed when running with the valid value size.\n"); 1236 ret = 1; /* Failed too early */ 1237 } 1238 if (0 == first_failed_at) 1239 { 1240 if (verbose) 1241 fprintf (stderr, "First failed size is %u.\n", (unsigned int) i); 1242 first_failed_at = i; 1243 } 1244 } 1245 else 1246 { 1247 if (i != rp_headers_check.large_header_name_size) 1248 mhdErrorExitDesc ("Detected wrong large reply header name size"); 1249 if (1 != rp_headers_check.large_header_value_size) 1250 mhdErrorExitDesc ("Detected wrong large reply header value size"); 1251 if (0 == rp_headers_check.large_header_valid) 1252 mhdErrorExitDesc ("Detected wrong large reply header"); 1253 if (TEST_FAIL_SIZE == i) 1254 { 1255 fprintf (stderr, "Request succeed with the largest size.\n"); 1256 ret = 1; /* Succeed with largest value */ 1257 } 1258 } 1259 if (0 != ahc_param->header_check_param.num_n1_headers) 1260 mhdErrorExitDesc ("Detected unexpected request headers"); 1261 if (0 != ahc_param->header_check_param.large_header_name_size) 1262 mhdErrorExitDesc ("Detected unexpected large request header"); 1263 if (0 != rp_headers_check.num_n1_headers) 1264 mhdErrorExitDesc ("Detected unexpected reply headers"); 1265 } 1266 } 1267 else if (large_rsp_header_value) 1268 { 1269 for (i = TEST_START_SIZE; i <= TEST_FAIL_SIZE; i++) 1270 { 1271 ahc_param->rp_large_hdr_name_size = 1; 1272 ahc_param->rp_large_hdr_value_size = i; 1273 1274 memset (&ahc_param->header_check_param, 0, 1275 sizeof (ahc_param->header_check_param)); 1276 if (0 != doCurlQueryInThread (d, &qParam, &rp_headers_check, 1277 ahc_param->rp_data, 1278 ahc_param->rp_data_size)) 1279 { 1280 (void) qParam.responseCode; /* TODO: check for the right response code */ 1281 if (0 != rp_headers_check.large_header_name_size) 1282 mhdErrorExitDesc ("Detected unexpected large reply header"); 1283 if (TEST_OK_SIZE >= i) 1284 { 1285 fprintf (stderr, 1286 "Request failed when running with the valid value size.\n"); 1287 ret = 1; /* Failed too early */ 1288 } 1289 if (0 == first_failed_at) 1290 { 1291 if (verbose) 1292 fprintf (stderr, "First failed size is %u.\n", (unsigned int) i); 1293 first_failed_at = i; 1294 } 1295 } 1296 else 1297 { 1298 if (1 != rp_headers_check.large_header_name_size) 1299 mhdErrorExitDesc ("Detected wrong large reply header name size"); 1300 if (i != rp_headers_check.large_header_value_size) 1301 mhdErrorExitDesc ("Detected wrong large reply header value size"); 1302 if (0 == rp_headers_check.large_header_valid) 1303 mhdErrorExitDesc ("Detected wrong large reply header"); 1304 if (TEST_FAIL_SIZE == i) 1305 { 1306 fprintf (stderr, "Request succeed with the largest size.\n"); 1307 ret = 1; /* Succeed with largest value */ 1308 } 1309 } 1310 if (0 != ahc_param->header_check_param.num_n1_headers) 1311 mhdErrorExitDesc ("Detected unexpected request headers"); 1312 if (0 != ahc_param->header_check_param.large_header_name_size) 1313 mhdErrorExitDesc ("Detected unexpected large request header"); 1314 if (0 != rp_headers_check.num_n1_headers) 1315 mhdErrorExitDesc ("Detected unexpected reply headers"); 1316 } 1317 } 1318 else if (large_rsp_headers) 1319 { 1320 size_t num_hrds; 1321 const size_t hdr_size = strlen (N1_HEADER_CRLF); 1322 1323 for (num_hrds = TEST_START_SIZE / hdr_size; 1324 num_hrds * hdr_size <= TEST_FAIL_SIZE; num_hrds++) 1325 { 1326 i = num_hrds * hdr_size; 1327 ahc_param->rp_num_n1_hdrs = num_hrds; 1328 1329 memset (&ahc_param->header_check_param, 0, 1330 sizeof (ahc_param->header_check_param)); 1331 if (0 != doCurlQueryInThread (d, &qParam, &rp_headers_check, 1332 ahc_param->rp_data, 1333 ahc_param->rp_data_size)) 1334 { 1335 (void) qParam.responseCode; /* TODO: check for the right response code */ 1336 if (0 != rp_headers_check.num_n1_headers) 1337 mhdErrorExitDesc ("Detected unexpected reply headers"); 1338 if (TEST_OK_SIZE >= i) 1339 { 1340 fprintf (stderr, 1341 "Request failed when running with the valid value size.\n"); 1342 ret = 1; /* Failed too early */ 1343 } 1344 if (0 == first_failed_at) 1345 { 1346 if (verbose) 1347 fprintf (stderr, "First failed size is %u.\n", (unsigned int) i); 1348 first_failed_at = i; 1349 } 1350 } 1351 else 1352 { 1353 if (num_hrds != rp_headers_check.num_n1_headers) 1354 mhdErrorExitDesc ("Detected wrong number of reply headers"); 1355 if (TEST_FAIL_SIZE == i) 1356 { 1357 fprintf (stderr, "Request succeed with the largest size.\n"); 1358 ret = 1; /* Succeed with largest value */ 1359 } 1360 } 1361 if (0 != ahc_param->header_check_param.num_n1_headers) 1362 mhdErrorExitDesc ("Detected unexpected request headers"); 1363 if (0 != ahc_param->header_check_param.large_header_name_size) 1364 mhdErrorExitDesc ("Detected unexpected large request header"); 1365 if (0 != rp_headers_check.large_header_name_size) 1366 mhdErrorExitDesc ("Detected unexpected large reply header"); 1367 } 1368 } 1369 else 1370 externalErrorExitDesc ("No valid test test was selected"); 1371 1372 MHD_stop_daemon (d); 1373 free (buf); 1374 free (uri_cb_param); 1375 free (ahc_param); 1376 1377 return ret; 1378 } 1379 1380 1381 enum testMhdThreadsType 1382 { 1383 testMhdThreadExternal = 0, 1384 testMhdThreadInternal = MHD_USE_INTERNAL_POLLING_THREAD, 1385 testMhdThreadInternalPerConnection = MHD_USE_THREAD_PER_CONNECTION 1386 | MHD_USE_INTERNAL_POLLING_THREAD, 1387 testMhdThreadInternalPool 1388 }; 1389 1390 enum testMhdPollType 1391 { 1392 testMhdPollBySelect = 0, 1393 testMhdPollByPoll = MHD_USE_POLL, 1394 testMhdPollByEpoll = MHD_USE_EPOLL, 1395 testMhdPollAuto = MHD_USE_AUTO 1396 }; 1397 1398 /* Get number of threads for thread pool depending 1399 * on used poll function and test type. */ 1400 static unsigned int 1401 testNumThreadsForPool (enum testMhdPollType pollType) 1402 { 1403 unsigned int numThreads = MHD_CPU_COUNT; 1404 (void) pollType; /* Don't care about pollType for this test */ 1405 return numThreads; /* No practical limit for non-cleanup test */ 1406 } 1407 1408 1409 static struct MHD_Daemon * 1410 startTestMhdDaemon (enum testMhdThreadsType thrType, 1411 enum testMhdPollType pollType, uint16_t *pport, 1412 struct ahc_cls_type **ahc_param, 1413 struct check_uri_cls **uri_cb_param) 1414 { 1415 struct MHD_Daemon *d; 1416 const union MHD_DaemonInfo *dinfo; 1417 1418 if ((NULL == ahc_param) || (NULL == uri_cb_param)) 1419 abort (); 1420 1421 *ahc_param = (struct ahc_cls_type *) malloc (sizeof(struct ahc_cls_type)); 1422 if (NULL == *ahc_param) 1423 externalErrorExit (); 1424 *uri_cb_param = 1425 (struct check_uri_cls *) malloc (sizeof(struct check_uri_cls)); 1426 if (NULL == *uri_cb_param) 1427 externalErrorExit (); 1428 1429 if ( (0 == *pport) && 1430 (MHD_NO == MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) ) 1431 { 1432 *pport = 4100; 1433 if (large_req_method) 1434 *pport += 1; 1435 if (large_req_url) 1436 *pport += 2; 1437 if (large_req_header_name) 1438 *pport += 3; 1439 if (large_req_header_value) 1440 *pport += 4; 1441 if (large_req_headers) 1442 *pport += 5; 1443 if (large_rsp_header_name) 1444 *pport += 6; 1445 if (large_rsp_header_value) 1446 *pport += 7; 1447 if (large_rsp_headers) 1448 *pport += 8; 1449 if (! oneone) 1450 *pport += 16; 1451 } 1452 1453 if (testMhdThreadExternal == thrType) 1454 d = MHD_start_daemon (((unsigned int) thrType) | ((unsigned int) pollType) 1455 | (verbose ? MHD_USE_ERROR_LOG : 0) 1456 | MHD_USE_NO_THREAD_SAFETY, 1457 *pport, NULL, NULL, 1458 &ahcCheck, *ahc_param, 1459 MHD_OPTION_URI_LOG_CALLBACK, &check_uri_cb, 1460 *uri_cb_param, 1461 MHD_OPTION_CONNECTION_MEMORY_LIMIT, 1462 (size_t) BUFFER_SIZE, 1463 MHD_OPTION_APP_FD_SETSIZE, (int) FD_SETSIZE, 1464 MHD_OPTION_END); 1465 else if (testMhdThreadInternalPool != thrType) 1466 d = MHD_start_daemon (((unsigned int) thrType) | ((unsigned int) pollType) 1467 | (verbose ? MHD_USE_ERROR_LOG : 0), 1468 *pport, NULL, NULL, 1469 &ahcCheck, *ahc_param, 1470 MHD_OPTION_URI_LOG_CALLBACK, &check_uri_cb, 1471 *uri_cb_param, 1472 MHD_OPTION_CONNECTION_MEMORY_LIMIT, 1473 (size_t) BUFFER_SIZE, 1474 MHD_OPTION_END); 1475 else 1476 d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD 1477 | ((unsigned int) pollType) 1478 | (verbose ? MHD_USE_ERROR_LOG : 0), 1479 *pport, NULL, NULL, 1480 &ahcCheck, *ahc_param, 1481 MHD_OPTION_THREAD_POOL_SIZE, 1482 testNumThreadsForPool (pollType), 1483 MHD_OPTION_URI_LOG_CALLBACK, &check_uri_cb, 1484 *uri_cb_param, 1485 MHD_OPTION_CONNECTION_MEMORY_LIMIT, 1486 (size_t) BUFFER_SIZE, 1487 MHD_OPTION_END); 1488 1489 if (NULL == d) 1490 { 1491 fprintf (stderr, "Failed to start MHD daemon, errno=%d.\n", errno); 1492 abort (); 1493 } 1494 1495 if (0 == *pport) 1496 { 1497 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT); 1498 if ((NULL == dinfo) || (0 == dinfo->port) ) 1499 { 1500 fprintf (stderr, "MHD_get_daemon_info() failed.\n"); 1501 abort (); 1502 } 1503 *pport = dinfo->port; 1504 if (0 == global_port) 1505 global_port = *pport; /* Reuse the same port for all tests */ 1506 } 1507 1508 return d; 1509 } 1510 1511 1512 /* Test runners */ 1513 1514 1515 static unsigned int 1516 testExternalGet (void) 1517 { 1518 struct MHD_Daemon *d; 1519 uint16_t d_port = global_port; /* Daemon's port */ 1520 struct ahc_cls_type *ahc_param; 1521 struct check_uri_cls *uri_cb_param; 1522 1523 d = startTestMhdDaemon (testMhdThreadExternal, testMhdPollBySelect, &d_port, 1524 &ahc_param, &uri_cb_param); 1525 1526 return performTestQueries (d, d_port, ahc_param, uri_cb_param); 1527 } 1528 1529 1530 static unsigned int 1531 testInternalGet (enum testMhdPollType pollType) 1532 { 1533 struct MHD_Daemon *d; 1534 uint16_t d_port = global_port; /* Daemon's port */ 1535 struct ahc_cls_type *ahc_param; 1536 struct check_uri_cls *uri_cb_param; 1537 1538 d = startTestMhdDaemon (testMhdThreadInternal, pollType, &d_port, 1539 &ahc_param, &uri_cb_param); 1540 1541 return performTestQueries (d, d_port, ahc_param, uri_cb_param); 1542 } 1543 1544 1545 static unsigned int 1546 testMultithreadedGet (enum testMhdPollType pollType) 1547 { 1548 struct MHD_Daemon *d; 1549 uint16_t d_port = global_port; /* Daemon's port */ 1550 struct ahc_cls_type *ahc_param; 1551 struct check_uri_cls *uri_cb_param; 1552 1553 d = startTestMhdDaemon (testMhdThreadInternalPerConnection, pollType, &d_port, 1554 &ahc_param, &uri_cb_param); 1555 return performTestQueries (d, d_port, ahc_param, uri_cb_param); 1556 } 1557 1558 1559 static unsigned int 1560 testMultithreadedPoolGet (enum testMhdPollType pollType) 1561 { 1562 struct MHD_Daemon *d; 1563 uint16_t d_port = global_port; /* Daemon's port */ 1564 struct ahc_cls_type *ahc_param; 1565 struct check_uri_cls *uri_cb_param; 1566 1567 d = startTestMhdDaemon (testMhdThreadInternalPool, pollType, &d_port, 1568 &ahc_param, &uri_cb_param); 1569 return performTestQueries (d, d_port, ahc_param, uri_cb_param); 1570 } 1571 1572 1573 int 1574 main (int argc, char *const *argv) 1575 { 1576 unsigned int errorCount = 0; 1577 unsigned int test_result = 0; 1578 verbose = 0; 1579 1580 if ((NULL == argv) || (0 == argv[0])) 1581 return 99; 1582 oneone = ! has_in_name (argv[0], "10"); 1583 large_req_method = has_in_name (argv[0], "_method") ? 1 : 0; 1584 large_req_url = has_in_name (argv[0], "_url") ? 1 : 0; 1585 large_req_header_name = has_in_name (argv[0], "_request_header_name") ? 1586 1 : 0; 1587 large_req_header_value = has_in_name (argv[0], "_request_header_value") ? 1588 1 : 0; 1589 large_req_headers = has_in_name (argv[0], "_request_headers") ? 1 : 0; 1590 large_rsp_header_name = has_in_name (argv[0], "_reply_header_name") ? 1591 1 : 0; 1592 large_rsp_header_value = has_in_name (argv[0], "_reply_header_value") ? 1593 1 : 0; 1594 large_rsp_headers = has_in_name (argv[0], "_reply_headers") ? 1 : 0; 1595 if (large_req_method + large_req_url + large_req_header_name 1596 + large_req_header_value + large_req_headers + large_rsp_header_name 1597 + large_rsp_header_value + large_rsp_headers != 1) 1598 return 99; 1599 verbose = ! (has_param (argc, argv, "-q") || 1600 has_param (argc, argv, "--quiet") || 1601 has_param (argc, argv, "-s") || 1602 has_param (argc, argv, "--silent")); 1603 1604 test_global_init (); 1605 1606 /* Could be set to non-zero value to enforce using specific port 1607 * in the test */ 1608 global_port = 0; 1609 test_result = testExternalGet (); 1610 if (test_result) 1611 fprintf (stderr, "FAILED: testExternalGet () - %u.\n", test_result); 1612 else if (verbose) 1613 printf ("PASSED: testExternalGet ().\n"); 1614 errorCount += test_result; 1615 if (MHD_YES == MHD_is_feature_supported (MHD_FEATURE_THREADS)) 1616 { 1617 test_result = testInternalGet (testMhdPollAuto); 1618 if (test_result) 1619 fprintf (stderr, "FAILED: testInternalGet (testMhdPollAuto) - %u.\n", 1620 test_result); 1621 else if (verbose) 1622 printf ("PASSED: testInternalGet (testMhdPollBySelect).\n"); 1623 errorCount += test_result; 1624 #ifdef _MHD_HEAVY_TESTS 1625 /* Actually tests are not heavy, but took too long to complete while 1626 * not really provide any additional results. */ 1627 test_result = testInternalGet (testMhdPollBySelect); 1628 if (test_result) 1629 fprintf (stderr, "FAILED: testInternalGet (testMhdPollBySelect) - %u.\n", 1630 test_result); 1631 else if (verbose) 1632 printf ("PASSED: testInternalGet (testMhdPollBySelect).\n"); 1633 errorCount += test_result; 1634 test_result = testMultithreadedPoolGet (testMhdPollBySelect); 1635 if (test_result) 1636 fprintf (stderr, 1637 "FAILED: testMultithreadedPoolGet (testMhdPollBySelect) - %u.\n", 1638 test_result); 1639 else if (verbose) 1640 printf ("PASSED: testMultithreadedPoolGet (testMhdPollBySelect).\n"); 1641 errorCount += test_result; 1642 test_result = testMultithreadedGet (testMhdPollBySelect); 1643 if (test_result) 1644 fprintf (stderr, 1645 "FAILED: testMultithreadedGet (testMhdPollBySelect) - %u.\n", 1646 test_result); 1647 else if (verbose) 1648 printf ("PASSED: testMultithreadedGet (testMhdPollBySelect).\n"); 1649 errorCount += test_result; 1650 if (MHD_YES == MHD_is_feature_supported (MHD_FEATURE_POLL)) 1651 { 1652 test_result = testInternalGet (testMhdPollByPoll); 1653 if (test_result) 1654 fprintf (stderr, "FAILED: testInternalGet (testMhdPollByPoll) - %u.\n", 1655 test_result); 1656 else if (verbose) 1657 printf ("PASSED: testInternalGet (testMhdPollByPoll).\n"); 1658 errorCount += test_result; 1659 } 1660 if (MHD_YES == MHD_is_feature_supported (MHD_FEATURE_EPOLL)) 1661 { 1662 test_result = testInternalGet (testMhdPollByEpoll); 1663 if (test_result) 1664 fprintf (stderr, "FAILED: testInternalGet (testMhdPollByEpoll) - %u.\n", 1665 test_result); 1666 else if (verbose) 1667 printf ("PASSED: testInternalGet (testMhdPollByEpoll).\n"); 1668 errorCount += test_result; 1669 } 1670 #else 1671 /* Mute compiler warnings */ 1672 (void) testMultithreadedGet; 1673 (void) testMultithreadedPoolGet; 1674 #endif /* _MHD_HEAVY_TESTS */ 1675 } 1676 if (0 != errorCount) 1677 fprintf (stderr, 1678 "Error (code: %u)\n", 1679 errorCount); 1680 else if (verbose) 1681 printf ("All tests passed.\n"); 1682 1683 test_global_cleanup (); 1684 1685 return (errorCount == 0) ? 0 : 1; /* 0 == pass */ 1686 }