test_client_server.c (12623B)
1 /* SPDX-License-Identifier: LGPL-2.1-or-later OR (GPL-2.0-or-later WITH eCos-exception-2.0) */ 2 /* 3 This file is part of GNU libmicrohttpd. 4 Copyright (C) 2016, 2024, 2025 Christian Grothoff & Evgeny Grin (Karlson2k) 5 6 GNU libmicrohttpd is free software; you can redistribute it and/or 7 modify it under the terms of the GNU Lesser General Public 8 License as published by the Free Software Foundation; either 9 version 2.1 of the License, or (at your option) any later version. 10 11 GNU libmicrohttpd is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 Lesser General Public License for more details. 15 16 Alternatively, you can redistribute GNU libmicrohttpd and/or 17 modify it under the terms of the GNU General Public License as 18 published by the Free Software Foundation; either version 2 of 19 the License, or (at your option) any later version, together 20 with the eCos exception, as follows: 21 22 As a special exception, if other files instantiate templates or 23 use macros or inline functions from this file, or you compile this 24 file and link it with other works to produce a work based on this 25 file, this file does not by itself cause the resulting work to be 26 covered by the GNU General Public License. However the source code 27 for this file must still be made available in accordance with 28 section (3) of the GNU General Public License v2. 29 30 This exception does not invalidate any other reasons why a work 31 based on this file might be covered by the GNU General Public 32 License. 33 34 You should have received copies of the GNU Lesser General Public 35 License and the GNU General Public License along with this library; 36 if not, see <https://www.gnu.org/licenses/>. 37 */ 38 39 /** 40 * @file test_client_server.c 41 * @brief test with client against server 42 * @author Christian Grothoff 43 */ 44 #include "libtest.h" 45 46 47 int 48 main (int argc, char *argv[]) 49 { 50 struct MHD_DaemonOptionAndValue thread1select[] = { 51 MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_SELECT), 52 MHD_D_OPTION_WM_WORKER_THREADS (1), 53 MHD_D_OPTION_TERMINATE () 54 }; 55 struct MHD_DaemonOptionAndValue thread2select[] = { 56 MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_SELECT), 57 MHD_D_OPTION_WM_WORKER_THREADS (2), 58 MHD_D_OPTION_TERMINATE () 59 }; 60 struct MHD_DaemonOptionAndValue thread1poll[] = { 61 MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_POLL), 62 MHD_D_OPTION_WM_WORKER_THREADS (1), 63 MHD_D_OPTION_TERMINATE () 64 }; 65 struct MHD_DaemonOptionAndValue thread2poll[] = { 66 MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_POLL), 67 MHD_D_OPTION_WM_WORKER_THREADS (2), 68 MHD_D_OPTION_TERMINATE () 69 }; 70 struct MHD_DaemonOptionAndValue thread1epoll[] = { 71 MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_EPOLL), 72 MHD_D_OPTION_WM_WORKER_THREADS (1), 73 MHD_D_OPTION_TERMINATE () 74 }; 75 struct MHD_DaemonOptionAndValue thread2epoll[] = { 76 MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_EPOLL), 77 MHD_D_OPTION_WM_WORKER_THREADS (2), 78 MHD_D_OPTION_TERMINATE () 79 }; 80 struct MHD_DaemonOptionAndValue thread1kq[] = { 81 MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_KQUEUE), 82 MHD_D_OPTION_WM_WORKER_THREADS (1), 83 MHD_D_OPTION_TERMINATE () 84 }; 85 struct MHD_DaemonOptionAndValue thread2kq[] = { 86 MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_KQUEUE), 87 MHD_D_OPTION_WM_WORKER_THREADS (2), 88 MHD_D_OPTION_TERMINATE () 89 }; 90 struct MHD_DaemonOptionAndValue thread1auto[] = { 91 MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_AUTO), 92 MHD_D_OPTION_WM_WORKER_THREADS (1), 93 MHD_D_OPTION_TERMINATE () 94 }; 95 struct MHD_DaemonOptionAndValue thread2auto[] = { 96 MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_AUTO), 97 MHD_D_OPTION_WM_WORKER_THREADS (2), 98 MHD_D_OPTION_TERMINATE () 99 }; 100 struct MHD_DaemonOptionAndValue external0auto[] = { 101 MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_AUTO), 102 MHD_D_OPTION_WM_EXTERNAL_PERIODIC (), 103 MHD_D_OPTION_TERMINATE () 104 }; 105 struct ServerType 106 { 107 const char *label; 108 MHDT_ServerSetup server_setup; 109 void *server_setup_cls; 110 MHDT_ServerRunner server_runner; 111 void *server_runner_cls; 112 } configs[] = { 113 #ifdef MHD_SUPPORT_SELECT 114 { 115 .label = "single threaded select", 116 .server_setup = &MHDT_server_setup_minimal, 117 .server_setup_cls = thread1select, 118 .server_runner = &MHDT_server_run_minimal, 119 }, 120 { 121 .label = "multi-threaded select", 122 .server_setup = &MHDT_server_setup_minimal, 123 .server_setup_cls = thread2select, 124 .server_runner = &MHDT_server_run_minimal, 125 }, 126 #endif 127 #ifdef MHD_SUPPORT_POLL 128 { 129 .label = "single threaded poll", 130 .server_setup = &MHDT_server_setup_minimal, 131 .server_setup_cls = thread1poll, 132 .server_runner = &MHDT_server_run_minimal, 133 }, 134 { 135 .label = "multi-threaded poll", 136 .server_setup = &MHDT_server_setup_minimal, 137 .server_setup_cls = thread2poll, 138 .server_runner = &MHDT_server_run_minimal, 139 }, 140 #endif 141 #if MHD_SUPPORT_EPOLL 142 { 143 .label = "single threaded epoll", 144 .server_setup = &MHDT_server_setup_minimal, 145 .server_setup_cls = thread1epoll, 146 .server_runner = &MHDT_server_run_minimal, 147 }, 148 { 149 .label = "multi-threaded epoll", 150 .server_setup = &MHDT_server_setup_minimal, 151 .server_setup_cls = thread2epoll, 152 .server_runner = &MHDT_server_run_minimal, 153 }, 154 #endif 155 #if MHD_SUPPORT_KQUEUE 156 { 157 .label = "single threaded kqueue", 158 .server_setup = &MHDT_server_setup_minimal, 159 .server_setup_cls = thread1kq, 160 .server_runner = &MHDT_server_run_minimal, 161 }, 162 { 163 .label = "multi-threaded kqueue", 164 .server_setup = &MHDT_server_setup_minimal, 165 .server_setup_cls = thread2kq, 166 .server_runner = &MHDT_server_run_minimal, 167 }, 168 #endif 169 { 170 .label = "auto-selected mode, single threaded", 171 .server_setup = &MHDT_server_setup_minimal, 172 .server_setup_cls = thread1auto, 173 .server_runner = &MHDT_server_run_minimal, 174 }, 175 { 176 .label = "auto-selected mode, multi-threaded", 177 .server_setup = &MHDT_server_setup_minimal, 178 .server_setup_cls = thread1auto, 179 .server_runner = &MHDT_server_run_minimal, 180 }, 181 #ifdef MHD_SUPPORT_EPOLL 182 { 183 .label = "external events loop mode, no internal threads", 184 .server_setup = &MHDT_server_setup_external, 185 .server_setup_cls = NULL, 186 .server_runner = &MHDT_server_run_external, 187 }, 188 #endif /* MHD_SUPPORT_EPOLL */ 189 #if 1 190 /* FIXME: remove once MHD_daemon_process_blocking 191 has been implemented */ 192 { 193 .label = "END" 194 }, 195 #endif 196 { 197 .label = "auto-selected external event loop mode, no threads", 198 .server_setup = &MHDT_server_setup_minimal, 199 .server_setup_cls = external0auto, 200 .server_runner = &MHDT_server_run_blocking, 201 }, 202 { 203 .label = "END" 204 } 205 }; 206 static char medium_text[20000]; 207 struct MHDT_Phase phases[] = { 208 { 209 .label = "simple get", 210 .server_cb = &MHDT_server_reply_text, 211 .server_cb_cls = (void *)"Hello world", 212 .client_cb = &MHDT_client_get_root, 213 .client_cb_cls = "Hello world", 214 .timeout_ms = 2500, 215 }, 216 { 217 .label = "GET with sendfile", 218 .server_cb = &MHDT_server_reply_file, 219 .server_cb_cls = (void *)"Hello world", 220 .client_cb = &MHDT_client_get_root, 221 .client_cb_cls = "Hello world", 222 .timeout_ms = 2500, 223 }, 224 { 225 /* The body does not fit a single send() buffer, so it is transferred 226 by several iterations */ 227 .label = "medium body get, buffer response", 228 .server_cb = &MHDT_server_reply_text, 229 .server_cb_cls = medium_text, 230 .client_cb = &MHDT_client_get_root, 231 .client_cb_cls = medium_text, 232 .timeout_ms = 5000, 233 }, 234 { 235 .label = "simple get, file response, unknown size", 236 .server_cb = &MHDT_server_reply_file_unknown_size, 237 .server_cb_cls = (void *)"Hello world", 238 .client_cb = &MHDT_client_get_root, 239 .client_cb_cls = "Hello world", 240 .timeout_ms = 2500, 241 }, 242 { 243 /* The end of the content is detected by the end of the file */ 244 .label = "medium body get, file response, unknown size", 245 .server_cb = &MHDT_server_reply_file_unknown_size, 246 .server_cb_cls = medium_text, 247 .client_cb = &MHDT_client_get_root, 248 .client_cb_cls = medium_text, 249 .timeout_ms = 5000, 250 }, 251 { 252 .label = "medium body get, file response", 253 .server_cb = &MHDT_server_reply_file, 254 .server_cb_cls = medium_text, 255 .client_cb = &MHDT_client_get_root, 256 .client_cb_cls = medium_text, 257 .timeout_ms = 5000, 258 }, 259 { 260 .label = "medium body get, callback response", 261 .server_cb = &MHDT_server_reply_chunked_text, 262 .server_cb_cls = medium_text, 263 .client_cb = &MHDT_client_get_root, 264 .client_cb_cls = medium_text, 265 .timeout_ms = 5000, 266 }, 267 { 268 .label = "client PUT with content-length", 269 .server_cb = &MHDT_server_reply_check_upload, 270 .server_cb_cls = (void *)"simple-upload-value", 271 .client_cb = &MHDT_client_put_data, 272 .client_cb_cls = "simple-upload-value", 273 .timeout_ms = 2500, 274 }, 275 { 276 .label = "client PUT with 2 chunks", 277 .server_cb = &MHDT_server_reply_check_upload, 278 .server_cb_cls = (void *)"chunky-upload-value", 279 .client_cb = &MHDT_client_chunk_data, 280 .client_cb_cls = "chunky-upload-value", 281 .timeout_ms = 2500, 282 }, 283 { 284 .label = "client request with custom header", 285 .server_cb = &MHDT_server_reply_check_header, 286 .server_cb_cls = (void *)"C-Header:testvalue", 287 .client_cb = &MHDT_client_set_header, 288 .client_cb_cls = "C-Header:testvalue", 289 .timeout_ms = 2500, 290 }, 291 { 292 .label = "server response with custom header", 293 .server_cb = &MHDT_server_reply_with_header, 294 .server_cb_cls = (void *)"X-Header:testvalue", 295 .client_cb = &MHDT_client_expect_header, 296 .client_cb_cls = "X-Header:testvalue", 297 .timeout_ms = 2500, 298 }, 299 { 300 .label = "URL with query parameters 1", 301 .server_cb = &MHDT_server_reply_check_query, 302 .server_cb_cls = (void *)"a=b&c", 303 .client_cb = &MHDT_client_get_with_query, 304 .client_cb_cls = "?a=b&c", 305 .timeout_ms = 5000, 306 .num_clients = 4 307 }, 308 { 309 .label = "URL with query parameters 2", 310 .server_cb = &MHDT_server_reply_check_query, 311 .server_cb_cls = (void *)"a=b&c", /* a => b, c => NULL */ 312 .client_cb = &MHDT_client_get_with_query, 313 .client_cb_cls = "?c&a=b", 314 .timeout_ms = 5000, 315 .num_clients = 1 316 }, 317 { 318 .label = "URL with query parameters 3", 319 .server_cb = &MHDT_server_reply_check_query, 320 .server_cb_cls = (void *)"a=&c", /* a => "", c => NULL */ 321 .client_cb = &MHDT_client_get_with_query, 322 .client_cb_cls = "?c&a=", 323 .timeout_ms = 5000, 324 .num_clients = 1 325 }, 326 { 327 .label = "URL with query parameters 4", 328 .server_cb = &MHDT_server_reply_check_query, 329 .server_cb_cls = (void *)"a=", /* a => "" */ 330 .client_cb = &MHDT_client_get_with_query, 331 .client_cb_cls = "?a=", 332 .timeout_ms = 5000, 333 .num_clients = 1 334 }, 335 { 336 .label = "URL with query parameters 5", 337 .server_cb = &MHDT_server_reply_check_query, 338 .server_cb_cls = (void *)"a=b", /* a => "b" */ 339 .client_cb = &MHDT_client_get_with_query, 340 .client_cb_cls = "?a=b", 341 .timeout_ms = 5000, 342 .num_clients = 1 343 }, 344 { 345 .label = "chunked response get", 346 .server_cb = &MHDT_server_reply_chunked_text, 347 .server_cb_cls = (void *)"Hello world", 348 .client_cb = &MHDT_client_get_root, 349 .client_cb_cls = "Hello world", 350 .timeout_ms = 2500, 351 }, 352 // TODO: chunked download 353 { 354 .label = NULL, 355 }, 356 }; 357 unsigned int i; 358 359 (void)argc; /* Unused. Silence compiler warning. */ 360 (void)argv; /* Unused. Silence compiler warning. */ 361 362 MHDT_fill_text_data (medium_text, 363 sizeof(medium_text)); 364 365 for (i = 0; NULL != configs[i].server_setup; i++) 366 { 367 int ret; 368 369 fprintf (stderr, 370 "Running tests with server setup '%s'\n", 371 configs[i].label); 372 ret = MHDT_test (configs[i].server_setup, 373 configs[i].server_setup_cls, 374 configs[i].server_runner, 375 configs[i].server_runner_cls, 376 phases); 377 if (0 != ret) 378 { 379 fprintf (stderr, 380 "Test failed with server of type '%s' (%u)\n", 381 configs[i].label, 382 i); 383 return ret; 384 } 385 } 386 return 0; 387 }