unit_http_mthd_parse.c (16711B)
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) 2026 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 src/tests/unit/unit_http_mthd_parse.c 41 * @brief The tests for the HTTP method string parsing function 42 * @author Karlson2k (Evgeny Grin) 43 * 44 * Compare lookup results with length-checked byte comparisons against the 45 * standard method names. Each input is tested at several offsets, always 46 * with the terminating zero at the last allocated byte. AddressSanitizer can 47 * therefore detect reads beyond the terminator; at offset zero it can also 48 * detect reads before the input. 49 * 50 * The same source is built for every version of the function: the build 51 * defines mhd_H_M_FUNC_FOR_64, mhd_H_M_FUNC_FOR_32_FAST or 52 * mhd_H_M_FUNC_FOR_32_COMPACT (and optionally mhd_H_M_TEST_UNKNOWN_ORDER) to 53 * test this version instead of the version selected for the platform. 54 */ 55 56 #include "mhd_sys_options.h" 57 58 #include "sys_base_types.h" 59 #include "sys_malloc.h" 60 61 #include <stdio.h> 62 #include <string.h> 63 64 #include "mhd_arr_num_elems.h" 65 #include "mhd_str_macros.h" 66 67 #include "mhd_public_api.h" 68 69 #include "mhdt_checks.h" 70 #include "mhdt_has_param.h" 71 72 #include "http_method_parse.h" 73 74 /** 75 * The maximum length of the tested strings 76 */ 77 #define TEST_MAX_LEN 40 78 79 /** 80 * An input string and its expected result code 81 */ 82 struct MethodTestCase 83 { 84 /** 85 * The input string, which may contain binary zeros 86 */ 87 const char *str; 88 /** 89 * The input length, excluding the terminating zero 90 */ 91 size_t len; 92 /** 93 * The expected method code 94 */ 95 enum mhd_HTTP_Method code; 96 }; 97 98 /** 99 * Initialise #MethodTestCase for the standard method @p name 100 */ 101 #define TEST_MTHD(name) \ 102 { MHD_HTTP_METHOD_STR_ ## name, \ 103 mhd_SSTR_LEN (MHD_HTTP_METHOD_STR_ ## name), \ 104 mhd_HTTP_METHOD_ ## name } 105 106 /** 107 * Standard method names and their expected result codes 108 */ 109 static const struct MethodTestCase std_mthds[] = { 110 TEST_MTHD (GET), 111 TEST_MTHD (HEAD), 112 TEST_MTHD (POST), 113 TEST_MTHD (PUT), 114 TEST_MTHD (DELETE), 115 TEST_MTHD (CONNECT), 116 TEST_MTHD (OPTIONS), 117 TEST_MTHD (TRACE), 118 TEST_MTHD (ASTERISK) 119 }; 120 121 /** 122 * Initialise a rejection case from the string literal @p str 123 */ 124 #define TEST_OTHER(str) { (str), mhd_SSTR_LEN (str), mhd_HTTP_METHOD_OTHER } 125 126 /** 127 * Non-standard method names and binary strings that must not match. 128 * The final cases contain result-code bytes to check for false matches 129 * against the packed code slots of the 64-bit table. 130 */ 131 static const struct MethodTestCase other_cases[] = { 132 TEST_OTHER (""), 133 TEST_OTHER ("get"), 134 TEST_OTHER ("GETS"), 135 TEST_OTHER ("GE"), 136 TEST_OTHER ("**"), 137 TEST_OTHER ("OPTIONSS"), 138 TEST_OTHER ("CONNECTION"), 139 TEST_OTHER ("GET\0"), 140 TEST_OTHER ("PUT\0XPUT"), 141 TEST_OTHER ("\xF8\xF9\0\0\0\0"), 142 TEST_OTHER ("\x07\xF9\xFF\0\0\0") 143 }; 144 145 /** 146 * Input bytes covering method letters, raw and XOR-encoded result codes, 147 * zero and boundary values 148 */ 149 static const unsigned char test_bytes[] = { 150 'G', 'E', 'T', 'H', 'A', 'D', 'P', 'O', 'S', 'U', 'L', 'C', 'N', 'I', 151 'R', '*', 'g', 'e', 't', ' ', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 152 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF, 0x80, 0x7F 153 }; 154 155 /** 156 * Number of input offsets to test from the start of an allocation 157 */ 158 #define TEST_NUM_OFFSETS 16 159 160 /** 161 * Input buffers with allocation sizes increasing by one byte. 162 * Buffer @a i has @a i + 1 bytes. An input of length @a len at offset 163 * @a offset uses buffer @a len + @a offset, placing its terminating zero 164 * at the last allocated byte. 165 */ 166 static char *test_bufs[TEST_NUM_OFFSETS + TEST_MAX_LEN]; 167 168 169 /** 170 * Reference lookup using length checks and byte comparisons. 171 * @param len the length of the @p str string 172 * @param str the string 173 * @return the code of the method 174 */ 175 static MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_IN_SIZE_ (2, 1) 176 MHD_FN_PURE_ enum mhd_HTTP_Method 177 parse_method_reference (size_t len, 178 const char *const str) 179 { 180 size_t i; 181 182 for (i = 0; i < mhd_ARR_NUM_ELEMS (std_mthds); ++i) 183 { 184 if ((std_mthds[i].len == len) 185 && (0 == memcmp (std_mthds[i].str, str, len))) 186 return std_mthds[i].code; 187 } 188 return mhd_HTTP_METHOD_OTHER; 189 } 190 191 192 /** 193 * Print a failing input with non-printable bytes escaped 194 * @param len the length of the @p str string 195 * @param str the string 196 */ 197 static MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_IN_SIZE_ (2, 1) void 198 print_failed_str (size_t len, 199 const char *const str) 200 { 201 size_t i; 202 203 fprintf (stderr, "\tThe string: \""); 204 for (i = 0; i < len; ++i) 205 { 206 const unsigned char chr = (unsigned char)str[i]; 207 if ((0x20 <= chr) && (0x7F > chr) && ('"' != chr) && ('\\' != chr)) 208 fprintf (stderr, "%c", (char)chr); 209 else 210 fprintf (stderr, "\\x%02X", (unsigned int)chr); 211 } 212 fprintf (stderr, "\" (length: %u)\n", (unsigned int)len); 213 fflush (stderr); 214 } 215 216 217 /** 218 * Copy an input to @p place, append a zero and check the lookup result 219 * @param place the location for the string, at least @p len + 1 bytes 220 * @param len the length of the @p str string 221 * @param str the string 222 * @param expected the expected result 223 */ 224 static MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_IN_SIZE_ (3, 2) void 225 check_str_at (char *const place, 226 size_t len, 227 const char *const str, 228 enum mhd_HTTP_Method expected) 229 { 230 enum mhd_HTTP_Method res; 231 232 if (0 != len) 233 memcpy (place, str, len); 234 place[len] = 0; 235 res = mhd_parse_http_method (len, 236 place); 237 if (!MHDT_EXPECT_UINT_EQ_VAL ((unsigned int)res, (unsigned int)expected)) 238 print_failed_str (len, place); 239 } 240 241 242 /** 243 * Check an input at every offset against an explicit expected result 244 * @param len the length of the @p str string, not more than #TEST_MAX_LEN 245 * @param str the string, not located in the test buffers 246 * @param expected the expected result 247 */ 248 static MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_IN_SIZE_ (2, 1) void 249 check_str_expected (size_t len, 250 const char *const str, 251 enum mhd_HTTP_Method expected) 252 { 253 unsigned int offset; 254 255 if (TEST_MAX_LEN < len) 256 MHDT_ERR_EXIT_D ("The test string is too long"); 257 258 for (offset = 0; offset < TEST_NUM_OFFSETS; ++offset) 259 check_str_at (test_bufs[offset + len] + offset, len, str, expected); 260 } 261 262 263 /** 264 * Check an input at every offset against the reference lookup 265 * @param len the length of the @p str string, not more than #TEST_MAX_LEN 266 * @param str the string, not located in the test buffers 267 */ 268 static MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_IN_SIZE_ (2, 1) void 269 check_str (size_t len, 270 const char *const str) 271 { 272 check_str_expected (len, str, parse_method_reference (len, str)); 273 } 274 275 276 /** 277 * Check standard method names and rejection cases with explicit results 278 */ 279 static void 280 test_known_results (void) 281 { 282 size_t i; 283 284 for (i = 0; i < mhd_ARR_NUM_ELEMS (std_mthds); ++i) 285 check_str_expected (std_mthds[i].len, std_mthds[i].str, std_mthds[i].code); 286 287 for (i = 0; i < mhd_ARR_NUM_ELEMS (other_cases); ++i) 288 check_str_expected (other_cases[i].len, other_cases[i].str, 289 other_cases[i].code); 290 291 MHDT_TEST_RESULT (); 292 } 293 294 295 /** 296 * Check every byte combination at lengths 1 and 2 297 */ 298 static void 299 test_short_strings (void) 300 { 301 unsigned int chr0; 302 303 for (chr0 = 0; chr0 <= 0xFF; ++chr0) 304 { 305 char str[2]; 306 unsigned int chr1; 307 308 str[0] = (char)(unsigned char)chr0; 309 check_str (1, str); 310 for (chr1 = 0; chr1 <= 0xFF; ++chr1) 311 { 312 str[1] = (char)(unsigned char)chr1; 313 check_str (2, str); 314 } 315 } 316 317 MHDT_TEST_RESULT (); 318 } 319 320 321 /** 322 * Check every single-byte change to each standard method name 323 */ 324 static void 325 test_one_byte_changed (void) 326 { 327 size_t i; 328 329 for (i = 0; i < mhd_ARR_NUM_ELEMS (std_mthds); ++i) 330 { 331 char str[TEST_MAX_LEN]; 332 size_t pos; 333 334 for (pos = 0; pos < std_mthds[i].len; ++pos) 335 { 336 unsigned int chr; 337 338 for (chr = 0; chr <= 0xFF; ++chr) 339 { 340 memcpy (str, std_mthds[i].str, std_mthds[i].len); 341 str[pos] = (char)(unsigned char)chr; 342 check_str (std_mthds[i].len, str); 343 } 344 } 345 } 346 347 MHDT_TEST_RESULT (); 348 } 349 350 351 /** 352 * Check shortened and extended standard method names 353 */ 354 static void 355 test_lengths (void) 356 { 357 size_t i; 358 359 for (i = 0; i < mhd_ARR_NUM_ELEMS (std_mthds); ++i) 360 { 361 char str[TEST_MAX_LEN]; 362 size_t len; 363 size_t j; 364 365 for (len = 0; len < std_mthds[i].len; ++len) 366 check_str (len, std_mthds[i].str); 367 368 for (j = 0; j < mhd_ARR_NUM_ELEMS (test_bytes); ++j) 369 { 370 memcpy (str, std_mthds[i].str, std_mthds[i].len); 371 for (len = std_mthds[i].len; len < TEST_MAX_LEN; ++len) 372 { 373 str[len] = (char)test_bytes[j]; 374 check_str (len + 1, str); 375 } 376 } 377 } 378 379 MHDT_TEST_RESULT (); 380 } 381 382 383 /** 384 * Check inputs with a standard method name's first four and final three bytes 385 * across the tested lengths 386 */ 387 static void 388 test_first_and_last_bytes (void) 389 { 390 size_t i; 391 392 for (i = 0; i < mhd_ARR_NUM_ELEMS (std_mthds); ++i) 393 { 394 const size_t method_len = std_mthds[i].len; 395 char first_bytes[4]; 396 size_t len; 397 398 if (3 > method_len) 399 continue; 400 memset (first_bytes, 0, sizeof(first_bytes)); 401 memcpy (first_bytes, std_mthds[i].str, (4 > method_len) ? method_len : 4); 402 403 for (len = 3; len <= TEST_MAX_LEN; ++len) 404 { 405 char str[TEST_MAX_LEN]; 406 size_t j; 407 408 for (j = 0; j < 2; ++j) 409 { 410 memset (str, (0 == j) ? 'X' : 0, len); 411 memcpy (str, first_bytes, (4 > len) ? len : 4); 412 memcpy (str + len - 3, std_mthds[i].str + method_len - 3, 3); 413 check_str (len, str); 414 } 415 } 416 } 417 418 MHDT_TEST_RESULT (); 419 } 420 421 422 /** 423 * Check inputs filled with one repeated byte across the tested lengths 424 */ 425 static void 426 test_same_bytes (void) 427 { 428 unsigned int chr; 429 430 for (chr = 0; chr <= 0xFF; ++chr) 431 { 432 char str[TEST_MAX_LEN]; 433 size_t len; 434 435 memset (str, (int)chr, sizeof(str)); 436 for (len = 0; len <= TEST_MAX_LEN; ++len) 437 check_str (len, str); 438 } 439 440 MHDT_TEST_RESULT (); 441 } 442 443 444 /** 445 * Advance the 32-bit state of a linear congruential generator. 446 * @param state the generator state to update 447 * @return the number 0..0x7FFF 448 */ 449 static MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_INOUT_ (1) unsigned int 450 rnd_get (uint_least32_t *const state) 451 { 452 *state = (uint_least32_t)((*state * 1103515245u + 12345u) & 0xFFFFFFFFu); 453 return (unsigned int)((*state >> 16) & 0x7FFFu); 454 } 455 456 457 /** 458 * Get a pseudo-random byte from #test_bytes. 459 * @param state the generator state to update 460 * @return the selected byte 461 */ 462 static MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_INOUT_ (1) char 463 rnd_test_byte (uint_least32_t *const state) 464 { 465 const size_t index = rnd_get (state) % mhd_ARR_NUM_ELEMS (test_bytes); 466 467 return (char)test_bytes[index]; 468 } 469 470 471 /** 472 * Check pseudo-random inputs: modified standard method names, combinations of 473 * method and table bytes, and arbitrary bytes across the tested lengths. 474 * Each run starts from the same generator state. 475 */ 476 static void 477 test_pseudo_random (void) 478 { 479 uint_least32_t rnd_state = 0x12345678u; 480 uint_least32_t iteration; 481 482 for (iteration = 0; iteration < 150000u; ++iteration) 483 { 484 char str[TEST_MAX_LEN]; 485 size_t len; 486 size_t j; 487 488 if (0 == iteration % 3) 489 { 490 const struct MethodTestCase *const method = 491 std_mthds + (rnd_get (&rnd_state) % mhd_ARR_NUM_ELEMS (std_mthds)); 492 unsigned int num_changes; 493 494 len = method->len; 495 memcpy (str, method->str, len); 496 if (0 == rnd_get (&rnd_state) % 4) 497 len = rnd_get (&rnd_state) % 9; 498 for (j = method->len; j < len; ++j) 499 str[j] = rnd_test_byte (&rnd_state); 500 for (num_changes = rnd_get (&rnd_state) % 3; 501 0 != num_changes; --num_changes) 502 { 503 if (0 != len) 504 { 505 const size_t pos = rnd_get (&rnd_state) % len; 506 507 str[pos] = rnd_test_byte (&rnd_state); 508 } 509 } 510 } 511 else if (1 == iteration % 3) 512 { 513 len = rnd_get (&rnd_state) % 9; 514 for (j = 0; j < len; ++j) 515 str[j] = rnd_test_byte (&rnd_state); 516 } 517 else 518 { 519 len = rnd_get (&rnd_state) % (TEST_MAX_LEN + 1); 520 for (j = 0; j < len; ++j) 521 str[j] = (char)(unsigned char)(rnd_get (&rnd_state) & 0xFFu); 522 } 523 check_str (len, str); 524 } 525 526 MHDT_TEST_RESULT (); 527 } 528 529 530 /** 531 * Report the checks skipped because of the failures reported above 532 * @param checks the description of the skipped checks 533 */ 534 static MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_CSTR_ (1) void 535 print_skipped (const char *checks) 536 { 537 fprintf (stderr, 538 "Skipped %s because of the failures above.\n\n", 539 checks); 540 fflush (stderr); 541 } 542 543 544 /** 545 * Allocate the buffers for the strings 546 */ 547 static void 548 test_bufs_init (void) 549 { 550 size_t i; 551 552 for (i = 0; i < mhd_ARR_NUM_ELEMS (test_bufs); ++i) 553 { 554 test_bufs[i] = (char *)malloc (i + 1); 555 if (NULL == test_bufs[i]) 556 MHDT_ERR_EXIT_D ("Failed to allocate the memory"); 557 } 558 } 559 560 561 /** 562 * Free the buffers allocated by #test_bufs_init() 563 */ 564 static void 565 test_bufs_deinit (void) 566 { 567 size_t i; 568 569 for (i = 0; i < mhd_ARR_NUM_ELEMS (test_bufs); ++i) 570 free (test_bufs[i]); 571 } 572 573 574 /** 575 * Run the HTTP method parsing tests. 576 * @param argc number of command-line arguments 577 * @param argv command-line arguments 578 * @return zero if all checks passed, non-zero otherwise 579 */ 580 int 581 main (int argc, 582 char *const *argv) 583 { 584 const int silent = (mhdt_has_param (argc, argv, "-s") 585 || mhdt_has_param (argc, argv, "--silent")); 586 587 /* Reporting every successful check would produce excessive output */ 588 MHDT_set_verbosity (silent ? MHDT_VERB_LVL_SILENT : MHDT_VERB_LVL_BASIC); 589 590 if (!silent) 591 { 592 printf ("Testing the %s%s.\n", 593 #if defined(mhd_H_M_FUNC_FOR_64) 594 "version with 64-bit keys", 595 #elif defined(mhd_H_M_FUNC_FOR_32_FAST) 596 "fast version with 32-bit values", 597 #elif defined(mhd_H_M_FUNC_FOR_32_COMPACT) 598 "compact version with 32-bit values", 599 #else 600 "version automatically selected for the platform", 601 #endif 602 #ifdef mhd_H_M_TEST_UNKNOWN_ORDER 603 " for the unknown byte order" 604 #else 605 "" 606 #endif /* mhd_H_M_TEST_UNKNOWN_ORDER */ 607 ); 608 fflush (stdout); 609 } 610 611 test_bufs_init (); 612 613 /* The stages increase in volume. A defect usually affects many inputs, and 614 a larger stage would report it thousands of times, so a stage runs only 615 if the previous stages passed. Within the systematic stage all the tests 616 run, even after a failure, to show which kinds of inputs are affected. */ 617 test_known_results (); 618 if (!MHDT_are_all_tests_succeed ()) 619 print_skipped ("the systematic and the pseudo-random checks"); 620 else 621 { 622 test_short_strings (); 623 test_one_byte_changed (); 624 test_lengths (); 625 test_first_and_last_bytes (); 626 test_same_bytes (); 627 628 if (!MHDT_are_all_tests_succeed ()) 629 print_skipped ("the pseudo-random checks"); 630 else 631 test_pseudo_random (); 632 } 633 634 test_bufs_deinit (); 635 636 return MHDT_FINAL_RESULT (argv[0]); 637 }