test_get_response_cleanup.c (11443B)
1 /* 2 This file is part of libmicrohttpd 3 Copyright (C) 2007, 2009 Christian Grothoff 4 Copyright (C) 2014-2022 Evgeny Grin (Karlson2k) 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 /** 23 * @file daemontest_get_response_cleanup.c 24 * @brief Testcase for libmicrohttpd response cleanup 25 * @author Christian Grothoff 26 * @author Karlson2k (Evgeny Grin) 27 */ 28 29 #include "MHD_config.h" 30 #include "platform.h" 31 #include <microhttpd.h> 32 #include <stdlib.h> 33 #include <unistd.h> 34 #include <string.h> 35 #include <time.h> 36 #include <sys/types.h> 37 #include <sys/wait.h> 38 #include <fcntl.h> 39 #include <errno.h> 40 #ifndef _WIN32 41 #include <signal.h> 42 #endif /* _WIN32 */ 43 44 #ifndef WINDOWS 45 #include <sys/socket.h> 46 #include <unistd.h> 47 #endif 48 49 #ifdef _WIN32 50 #ifndef WIN32_LEAN_AND_MEAN 51 #define WIN32_LEAN_AND_MEAN 1 52 #endif /* !WIN32_LEAN_AND_MEAN */ 53 #include <windows.h> 54 #endif 55 56 #include "mhd_has_in_name.h" 57 58 /* Turn any MHD_PANIC() or failing mhd_assert() reached from this 59 test into a marked, classifiable test error (TESTING.md, P5). */ 60 #include "mhd_panic_tripwire.h" 61 62 #if defined(MHD_CPU_COUNT) && (MHD_CPU_COUNT + 0) < 2 63 #undef MHD_CPU_COUNT 64 #endif 65 #if ! defined(MHD_CPU_COUNT) 66 #define MHD_CPU_COUNT 2 67 #endif 68 69 static int oneone; 70 71 static int ok; 72 73 74 static pid_t 75 fork_curl (const char *url) 76 { 77 pid_t ret; 78 79 ret = fork (); 80 if (ret != 0) 81 return ret; 82 execlp ("curl", "curl", "-s", "-N", "--proxy", "", "-o", "/dev/null", 83 "-GET", url, NULL); 84 fprintf (stderr, 85 "Failed to exec curl: %s\n", 86 strerror (errno)); 87 _exit (-1); 88 } 89 90 91 static void 92 kill_curl (pid_t pid) 93 { 94 int status; 95 96 /* fprintf (stderr, "Killing curl\n"); */ 97 kill (pid, SIGTERM); 98 waitpid (pid, &status, 0); 99 } 100 101 102 static ssize_t 103 push_callback (void *cls, uint64_t pos, char *buf, size_t max) 104 { 105 (void) cls; (void) pos; /* Unused. Silent compiler warning. */ 106 107 if (max == 0) 108 return 0; 109 buf[0] = 'd'; 110 return 1; 111 } 112 113 114 static void 115 push_free_callback (void *cls) 116 { 117 int *ok_p = cls; 118 119 /* fprintf (stderr, "Cleanup callback called!\n"); */ 120 *ok_p = 0; 121 } 122 123 124 static enum MHD_Result 125 ahc_echo (void *cls, 126 struct MHD_Connection *connection, 127 const char *url, 128 const char *method, 129 const char *version, 130 const char *upload_data, size_t *upload_data_size, 131 void **req_cls) 132 { 133 static int ptr; 134 struct MHD_Response *response; 135 enum MHD_Result ret; 136 (void) cls; 137 (void) url; (void) version; /* Unused. Silent compiler warning. */ 138 (void) upload_data; (void) upload_data_size; /* Unused. Silent compiler warning. */ 139 140 if (0 != strcmp (MHD_HTTP_METHOD_GET, method)) 141 return MHD_NO; /* unexpected method */ 142 if (&ptr != *req_cls) 143 { 144 *req_cls = &ptr; 145 return MHD_YES; 146 } 147 *req_cls = NULL; 148 response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN, 149 32 * 1024, 150 &push_callback, 151 &ok, 152 &push_free_callback); 153 ret = MHD_queue_response (connection, MHD_HTTP_OK, response); 154 MHD_destroy_response (response); 155 if (ret == MHD_NO) 156 abort (); 157 return ret; 158 } 159 160 161 static unsigned int 162 testInternalGet (void) 163 { 164 struct MHD_Daemon *d; 165 pid_t curl; 166 uint16_t port; 167 char url[127]; 168 169 if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) 170 port = 0; 171 else 172 { 173 port = 1180; 174 if (oneone) 175 port += 10; 176 } 177 178 ok = 1; 179 d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, 180 port, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END); 181 if (d == NULL) 182 return 1; 183 if (0 == port) 184 { 185 const union MHD_DaemonInfo *dinfo; 186 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT); 187 if ((NULL == dinfo) || (0 == dinfo->port) ) 188 { 189 MHD_stop_daemon (d); return 32; 190 } 191 port = dinfo->port; 192 } 193 snprintf (url, 194 sizeof (url), 195 "http://127.0.0.1:%u/", 196 (unsigned int) port); 197 curl = fork_curl (url); 198 (void) sleep (1); 199 kill_curl (curl); 200 (void) sleep (1); 201 /* fprintf (stderr, "Stopping daemon!\n"); */ 202 MHD_stop_daemon (d); 203 if (ok != 0) 204 return 2; 205 return 0; 206 } 207 208 209 static unsigned int 210 testMultithreadedGet (void) 211 { 212 struct MHD_Daemon *d; 213 pid_t curl; 214 uint16_t port; 215 char url[127]; 216 217 if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) 218 port = 0; 219 else 220 { 221 port = 1181; 222 if (oneone) 223 port += 10; 224 } 225 226 ok = 1; 227 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION 228 | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, 229 port, NULL, NULL, &ahc_echo, NULL, 230 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 2, 231 MHD_OPTION_END); 232 if (d == NULL) 233 return 16; 234 if (0 == port) 235 { 236 const union MHD_DaemonInfo *dinfo; 237 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT); 238 if ((NULL == dinfo) || (0 == dinfo->port) ) 239 { 240 MHD_stop_daemon (d); return 32; 241 } 242 port = dinfo->port; 243 } 244 snprintf (url, 245 sizeof (url), 246 "http://127.0.0.1:%u/", 247 (unsigned int) port); 248 /* fprintf (stderr, "Forking cURL!\n"); */ 249 curl = fork_curl (url); 250 (void) sleep (1); 251 kill_curl (curl); 252 (void) sleep (1); 253 curl = fork_curl (url); 254 (void) sleep (1); 255 if (ok != 0) 256 { 257 kill_curl (curl); 258 MHD_stop_daemon (d); 259 return 64; 260 } 261 kill_curl (curl); 262 (void) sleep (1); 263 /* fprintf (stderr, "Stopping daemon!\n"); */ 264 MHD_stop_daemon (d); 265 if (ok != 0) 266 return 32; 267 268 return 0; 269 } 270 271 272 static unsigned int 273 testMultithreadedPoolGet (void) 274 { 275 struct MHD_Daemon *d; 276 pid_t curl; 277 uint16_t port; 278 char url[127]; 279 280 if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) 281 port = 0; 282 else 283 { 284 port = 1182; 285 if (oneone) 286 port += 10; 287 } 288 289 ok = 1; 290 d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, 291 port, NULL, NULL, &ahc_echo, NULL, 292 MHD_OPTION_THREAD_POOL_SIZE, MHD_CPU_COUNT, 293 MHD_OPTION_END); 294 if (d == NULL) 295 return 64; 296 if (0 == port) 297 { 298 const union MHD_DaemonInfo *dinfo; 299 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT); 300 if ((NULL == dinfo) || (0 == dinfo->port) ) 301 { 302 MHD_stop_daemon (d); return 32; 303 } 304 port = dinfo->port; 305 } 306 snprintf (url, 307 sizeof (url), 308 "http://127.0.0.1:%u/", 309 (unsigned int) port); 310 curl = fork_curl (url); 311 (void) sleep (1); 312 kill_curl (curl); 313 (void) sleep (1); 314 /* fprintf (stderr, "Stopping daemon!\n"); */ 315 MHD_stop_daemon (d); 316 if (ok != 0) 317 return 128; 318 return 0; 319 } 320 321 322 static unsigned int 323 testExternalGet (void) 324 { 325 struct MHD_Daemon *d; 326 fd_set rs; 327 fd_set ws; 328 fd_set es; 329 MHD_socket max; 330 time_t start; 331 struct timeval tv; 332 pid_t curl; 333 uint16_t port; 334 char url[127]; 335 336 if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) 337 port = 0; 338 else 339 { 340 port = 1183; 341 if (oneone) 342 port += 10; 343 } 344 345 ok = 1; 346 d = MHD_start_daemon (MHD_USE_ERROR_LOG, 347 port, NULL, NULL, &ahc_echo, NULL, 348 MHD_OPTION_APP_FD_SETSIZE, (int) FD_SETSIZE, 349 MHD_OPTION_END); 350 if (d == NULL) 351 return 256; 352 if (0 == port) 353 { 354 const union MHD_DaemonInfo *dinfo; 355 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT); 356 if ((NULL == dinfo) || (0 == dinfo->port) ) 357 { 358 MHD_stop_daemon (d); return 32; 359 } 360 port = dinfo->port; 361 } 362 snprintf (url, 363 sizeof (url), 364 "http://127.0.0.1:%u/", 365 (unsigned int) port); 366 curl = fork_curl (url); 367 368 start = time (NULL); 369 while ((time (NULL) - start < 2)) 370 { 371 max = 0; 372 FD_ZERO (&rs); 373 FD_ZERO (&ws); 374 FD_ZERO (&es); 375 if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max)) 376 { 377 MHD_stop_daemon (d); 378 return 4096; 379 } 380 tv.tv_sec = 0; 381 tv.tv_usec = 1000; 382 if (-1 == select (max + 1, &rs, &ws, &es, &tv)) 383 { 384 #ifdef MHD_POSIX_SOCKETS 385 if (EINTR != errno) 386 { 387 fprintf (stderr, "Unexpected select() error: %d. Line: %d\n", 388 (int) errno, __LINE__); 389 fflush (stderr); 390 exit (99); 391 } 392 #else 393 if ((WSAEINVAL != WSAGetLastError ()) || 394 (0 != rs.fd_count) || (0 != ws.fd_count) || (0 != es.fd_count) ) 395 { 396 fprintf (stderr, "Unexpected select() error: %d. Line: %d\n", 397 (int) WSAGetLastError (), __LINE__); 398 fflush (stderr); 399 exit (99); 400 } 401 Sleep (1); 402 #endif 403 } 404 MHD_run (d); 405 } 406 kill_curl (curl); 407 start = time (NULL); 408 while ((time (NULL) - start < 2)) 409 { 410 max = 0; 411 FD_ZERO (&rs); 412 FD_ZERO (&ws); 413 FD_ZERO (&es); 414 if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max)) 415 { 416 MHD_stop_daemon (d); 417 return 4096; 418 } 419 tv.tv_sec = 0; 420 tv.tv_usec = 1000; 421 if (-1 == select (max + 1, &rs, &ws, &es, &tv)) 422 { 423 #ifdef MHD_POSIX_SOCKETS 424 if (EINTR != errno) 425 { 426 fprintf (stderr, "Unexpected select() error: %d. Line: %d\n", 427 (int) errno, __LINE__); 428 fflush (stderr); 429 exit (99); 430 } 431 #else 432 if ((WSAEINVAL != WSAGetLastError ()) || 433 (0 != rs.fd_count) || (0 != ws.fd_count) || (0 != es.fd_count) ) 434 { 435 fprintf (stderr, "Unexpected select() error: %d. Line: %d\n", 436 (int) WSAGetLastError (), __LINE__); 437 fflush (stderr); 438 exit (99); 439 } 440 Sleep (1); 441 #endif 442 } 443 MHD_run (d); 444 } 445 /* fprintf (stderr, "Stopping daemon!\n"); */ 446 MHD_stop_daemon (d); 447 if (ok != 0) 448 return 1024; 449 return 0; 450 } 451 452 453 int 454 main (int argc, char *const *argv) 455 { 456 unsigned int errorCount = 0; 457 (void) argc; /* Unused. Silent compiler warning. */ 458 459 #ifndef _WIN32 460 /* Solaris has no way to disable SIGPIPE on socket disconnect. */ 461 if (MHD_NO == MHD_is_feature_supported (MHD_FEATURE_AUTOSUPPRESS_SIGPIPE)) 462 { 463 struct sigaction act; 464 465 act.sa_handler = SIG_IGN; 466 sigaction (SIGPIPE, &act, NULL); 467 } 468 #endif /* _WIN32 */ 469 470 if ((NULL == argv) || (0 == argv[0])) 471 return 99; 472 oneone = has_in_name (argv[0], "11"); 473 if (MHD_YES == MHD_is_feature_supported (MHD_FEATURE_THREADS)) 474 { 475 errorCount += testInternalGet (); 476 errorCount += testMultithreadedGet (); 477 errorCount += testMultithreadedPoolGet (); 478 } 479 errorCount += testExternalGet (); 480 if (errorCount != 0) 481 fprintf (stderr, "Error (code: %u)\n", errorCount); 482 return (0 == errorCount) ? 0 : 1; /* 0 == pass */ 483 }