commit a667b4da84d777755adbcecbdc2e88cd75395c79
parent cf0f7283e49a82fa3a5f0b50f8b8116fdd15c795
Author: Evgeny Grin (Karlson2k) <k2k@drgrin.dev>
Date: Wed, 18 Nov 2026 15:26:05 +0100
Added unit tests for parsing HTTP basic methods
Diffstat:
2 files changed, 678 insertions(+), 1 deletion(-)
diff --git a/src/tests/unit/Makefile.am b/src/tests/unit/Makefile.am
@@ -41,7 +41,14 @@ check_PROGRAMS = \
unit_str_token_remove \
unit_str_tokens_remove \
unit_str_pct \
- unit_str
+ unit_str \
+ unit_http_mthd_parse \
+ unit_http_mthd_parse_c64 \
+ unit_http_mthd_parse_c32f \
+ unit_http_mthd_parse_c32c \
+ unit_http_mthd_parse_c64_unk \
+ unit_http_mthd_parse_c32f_unk \
+ unit_http_mthd_parse_c32c_unk
if MHD_SUPPORT_HTTP2
check_PROGRAMS += \
@@ -93,6 +100,39 @@ unit_str_token_remove_SOURCES = \
unit_str_tokens_remove_SOURCES = \
unit_str_tokens_remove.c
+unit_http_mthd_parse_SOURCES = \
+ unit_http_mthd_parse.c \
+ $(srcdir)/../mhdt_checks.h \
+ $(srcdir)/../mhdt_has_param.h \
+ $(srcdir)/../../mhd2/http_method_parse.c \
+ $(srcdir)/../../mhd2/http_method_parse.h
+unit_http_mthd_parse_CPPFLAGS = $(AM_CPPFLAGS)
+
+unit_http_mthd_parse_c64_SOURCES = $(unit_http_mthd_parse_SOURCES)
+unit_http_mthd_parse_c64_CPPFLAGS = $(AM_CPPFLAGS) -Dmhd_H_M_FUNC_FOR_64=1
+
+unit_http_mthd_parse_c32f_SOURCES = $(unit_http_mthd_parse_SOURCES)
+unit_http_mthd_parse_c32f_CPPFLAGS = \
+ $(AM_CPPFLAGS) -Dmhd_H_M_FUNC_FOR_32_FAST=1
+
+unit_http_mthd_parse_c32c_SOURCES = $(unit_http_mthd_parse_SOURCES)
+unit_http_mthd_parse_c32c_CPPFLAGS = \
+ $(AM_CPPFLAGS) -Dmhd_H_M_FUNC_FOR_32_COMPACT=1
+
+unit_http_mthd_parse_c64_unk_SOURCES = $(unit_http_mthd_parse_SOURCES)
+unit_http_mthd_parse_c64_unk_CPPFLAGS = \
+ $(AM_CPPFLAGS) -Dmhd_H_M_FUNC_FOR_64=1 -Dmhd_H_M_TEST_UNKNOWN_ORDER=1
+
+unit_http_mthd_parse_c32f_unk_SOURCES = $(unit_http_mthd_parse_SOURCES)
+unit_http_mthd_parse_c32f_unk_CPPFLAGS = \
+ $(AM_CPPFLAGS) -Dmhd_H_M_FUNC_FOR_32_FAST=1 \
+ -Dmhd_H_M_TEST_UNKNOWN_ORDER=1
+
+unit_http_mthd_parse_c32c_unk_SOURCES = $(unit_http_mthd_parse_SOURCES)
+unit_http_mthd_parse_c32c_unk_CPPFLAGS = \
+ $(AM_CPPFLAGS) -Dmhd_H_M_FUNC_FOR_32_COMPACT=1 \
+ -Dmhd_H_M_TEST_UNKNOWN_ORDER=1
+
unit_h2_huffman_encode_SOURCES = \
unit_h2_huffman_codec.c \
$(srcdir)/../mhdt_has_in_name.h \
diff --git a/src/tests/unit/unit_http_mthd_parse.c b/src/tests/unit/unit_http_mthd_parse.c
@@ -0,0 +1,637 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later OR (GPL-2.0-or-later WITH eCos-exception-2.0) */
+/*
+ This file is part of GNU libmicrohttpd.
+ Copyright (C) 2026 Evgeny Grin (Karlson2k)
+
+ GNU libmicrohttpd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ GNU libmicrohttpd is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ Alternatively, you can redistribute GNU libmicrohttpd and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2 of
+ the License, or (at your option) any later version, together
+ with the eCos exception, as follows:
+
+ As a special exception, if other files instantiate templates or
+ use macros or inline functions from this file, or you compile this
+ file and link it with other works to produce a work based on this
+ file, this file does not by itself cause the resulting work to be
+ covered by the GNU General Public License. However the source code
+ for this file must still be made available in accordance with
+ section (3) of the GNU General Public License v2.
+
+ This exception does not invalidate any other reasons why a work
+ based on this file might be covered by the GNU General Public
+ License.
+
+ You should have received copies of the GNU Lesser General Public
+ License and the GNU General Public License along with this library;
+ if not, see <https://www.gnu.org/licenses/>.
+*/
+
+/**
+ * @file src/tests/unit/unit_http_mthd_parse.c
+ * @brief The tests for the HTTP method string parsing function
+ * @author Karlson2k (Evgeny Grin)
+ *
+ * Compare lookup results with length-checked byte comparisons against the
+ * standard method names. Each input is tested at several offsets, always
+ * with the terminating zero at the last allocated byte. AddressSanitizer can
+ * therefore detect reads beyond the terminator; at offset zero it can also
+ * detect reads before the input.
+ *
+ * The same source is built for every version of the function: the build
+ * defines mhd_H_M_FUNC_FOR_64, mhd_H_M_FUNC_FOR_32_FAST or
+ * mhd_H_M_FUNC_FOR_32_COMPACT (and optionally mhd_H_M_TEST_UNKNOWN_ORDER) to
+ * test this version instead of the version selected for the platform.
+ */
+
+#include "mhd_sys_options.h"
+
+#include "sys_base_types.h"
+#include "sys_malloc.h"
+
+#include <stdio.h>
+#include <string.h>
+
+#include "mhd_arr_num_elems.h"
+#include "mhd_str_macros.h"
+
+#include "mhd_public_api.h"
+
+#include "mhdt_checks.h"
+#include "mhdt_has_param.h"
+
+#include "http_method_parse.h"
+
+/**
+ * The maximum length of the tested strings
+ */
+#define TEST_MAX_LEN 40
+
+/**
+ * An input string and its expected result code
+ */
+struct MethodTestCase
+{
+ /**
+ * The input string, which may contain binary zeros
+ */
+ const char *str;
+ /**
+ * The input length, excluding the terminating zero
+ */
+ size_t len;
+ /**
+ * The expected method code
+ */
+ enum mhd_HTTP_Method code;
+};
+
+/**
+ * Initialise #MethodTestCase for the standard method @p name
+ */
+#define TEST_MTHD(name) \
+ { MHD_HTTP_METHOD_STR_ ## name, \
+ mhd_SSTR_LEN (MHD_HTTP_METHOD_STR_ ## name), \
+ mhd_HTTP_METHOD_ ## name }
+
+/**
+ * Standard method names and their expected result codes
+ */
+static const struct MethodTestCase std_mthds[] = {
+ TEST_MTHD (GET),
+ TEST_MTHD (HEAD),
+ TEST_MTHD (POST),
+ TEST_MTHD (PUT),
+ TEST_MTHD (DELETE),
+ TEST_MTHD (CONNECT),
+ TEST_MTHD (OPTIONS),
+ TEST_MTHD (TRACE),
+ TEST_MTHD (ASTERISK)
+};
+
+/**
+ * Initialise a rejection case from the string literal @p str
+ */
+#define TEST_OTHER(str) { (str), mhd_SSTR_LEN (str), mhd_HTTP_METHOD_OTHER }
+
+/**
+ * Non-standard method names and binary strings that must not match.
+ * The final cases contain result-code bytes to check for false matches
+ * against the packed code slots of the 64-bit table.
+ */
+static const struct MethodTestCase other_cases[] = {
+ TEST_OTHER (""),
+ TEST_OTHER ("get"),
+ TEST_OTHER ("GETS"),
+ TEST_OTHER ("GE"),
+ TEST_OTHER ("**"),
+ TEST_OTHER ("OPTIONSS"),
+ TEST_OTHER ("CONNECTION"),
+ TEST_OTHER ("GET\0"),
+ TEST_OTHER ("PUT\0XPUT"),
+ TEST_OTHER ("\xF8\xF9\0\0\0\0"),
+ TEST_OTHER ("\x07\xF9\xFF\0\0\0")
+};
+
+/**
+ * Input bytes covering method letters, raw and XOR-encoded result codes,
+ * zero and boundary values
+ */
+static const unsigned char test_bytes[] = {
+ 'G', 'E', 'T', 'H', 'A', 'D', 'P', 'O', 'S', 'U', 'L', 'C', 'N', 'I',
+ 'R', '*', 'g', 'e', 't', ' ', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
+ 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF, 0x80, 0x7F
+};
+
+/**
+ * Number of input offsets to test from the start of an allocation
+ */
+#define TEST_NUM_OFFSETS 16
+
+/**
+ * Input buffers with allocation sizes increasing by one byte.
+ * Buffer @a i has @a i + 1 bytes. An input of length @a len at offset
+ * @a offset uses buffer @a len + @a offset, placing its terminating zero
+ * at the last allocated byte.
+ */
+static char *test_bufs[TEST_NUM_OFFSETS + TEST_MAX_LEN];
+
+
+/**
+ * Reference lookup using length checks and byte comparisons.
+ * @param len the length of the @p str string
+ * @param str the string
+ * @return the code of the method
+ */
+static MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_IN_SIZE_ (2, 1)
+MHD_FN_PURE_ enum mhd_HTTP_Method
+parse_method_reference (size_t len,
+ const char *const str)
+{
+ size_t i;
+
+ for (i = 0; i < mhd_ARR_NUM_ELEMS (std_mthds); ++i)
+ {
+ if ((std_mthds[i].len == len)
+ && (0 == memcmp (std_mthds[i].str, str, len)))
+ return std_mthds[i].code;
+ }
+ return mhd_HTTP_METHOD_OTHER;
+}
+
+
+/**
+ * Print a failing input with non-printable bytes escaped
+ * @param len the length of the @p str string
+ * @param str the string
+ */
+static MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_IN_SIZE_ (2, 1) void
+print_failed_str (size_t len,
+ const char *const str)
+{
+ size_t i;
+
+ fprintf (stderr, "\tThe string: \"");
+ for (i = 0; i < len; ++i)
+ {
+ const unsigned char chr = (unsigned char)str[i];
+ if ((0x20 <= chr) && (0x7F > chr) && ('"' != chr) && ('\\' != chr))
+ fprintf (stderr, "%c", (char)chr);
+ else
+ fprintf (stderr, "\\x%02X", (unsigned int)chr);
+ }
+ fprintf (stderr, "\" (length: %u)\n", (unsigned int)len);
+ fflush (stderr);
+}
+
+
+/**
+ * Copy an input to @p place, append a zero and check the lookup result
+ * @param place the location for the string, at least @p len + 1 bytes
+ * @param len the length of the @p str string
+ * @param str the string
+ * @param expected the expected result
+ */
+static MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_IN_SIZE_ (3, 2) void
+check_str_at (char *const place,
+ size_t len,
+ const char *const str,
+ enum mhd_HTTP_Method expected)
+{
+ enum mhd_HTTP_Method res;
+
+ if (0 != len)
+ memcpy (place, str, len);
+ place[len] = 0;
+ res = mhd_parse_http_method (len,
+ place);
+ if (!MHDT_EXPECT_UINT_EQ_VAL ((unsigned int)res, (unsigned int)expected))
+ print_failed_str (len, place);
+}
+
+
+/**
+ * Check an input at every offset against an explicit expected result
+ * @param len the length of the @p str string, not more than #TEST_MAX_LEN
+ * @param str the string, not located in the test buffers
+ * @param expected the expected result
+ */
+static MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_IN_SIZE_ (2, 1) void
+check_str_expected (size_t len,
+ const char *const str,
+ enum mhd_HTTP_Method expected)
+{
+ unsigned int offset;
+
+ if (TEST_MAX_LEN < len)
+ MHDT_ERR_EXIT_D ("The test string is too long");
+
+ for (offset = 0; offset < TEST_NUM_OFFSETS; ++offset)
+ check_str_at (test_bufs[offset + len] + offset, len, str, expected);
+}
+
+
+/**
+ * Check an input at every offset against the reference lookup
+ * @param len the length of the @p str string, not more than #TEST_MAX_LEN
+ * @param str the string, not located in the test buffers
+ */
+static MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_IN_SIZE_ (2, 1) void
+check_str (size_t len,
+ const char *const str)
+{
+ check_str_expected (len, str, parse_method_reference (len, str));
+}
+
+
+/**
+ * Check standard method names and rejection cases with explicit results
+ */
+static void
+test_known_results (void)
+{
+ size_t i;
+
+ for (i = 0; i < mhd_ARR_NUM_ELEMS (std_mthds); ++i)
+ check_str_expected (std_mthds[i].len, std_mthds[i].str, std_mthds[i].code);
+
+ for (i = 0; i < mhd_ARR_NUM_ELEMS (other_cases); ++i)
+ check_str_expected (other_cases[i].len, other_cases[i].str,
+ other_cases[i].code);
+
+ MHDT_TEST_RESULT ();
+}
+
+
+/**
+ * Check every byte combination at lengths 1 and 2
+ */
+static void
+test_short_strings (void)
+{
+ unsigned int chr0;
+
+ for (chr0 = 0; chr0 <= 0xFF; ++chr0)
+ {
+ char str[2];
+ unsigned int chr1;
+
+ str[0] = (char)(unsigned char)chr0;
+ check_str (1, str);
+ for (chr1 = 0; chr1 <= 0xFF; ++chr1)
+ {
+ str[1] = (char)(unsigned char)chr1;
+ check_str (2, str);
+ }
+ }
+
+ MHDT_TEST_RESULT ();
+}
+
+
+/**
+ * Check every single-byte change to each standard method name
+ */
+static void
+test_one_byte_changed (void)
+{
+ size_t i;
+
+ for (i = 0; i < mhd_ARR_NUM_ELEMS (std_mthds); ++i)
+ {
+ char str[TEST_MAX_LEN];
+ size_t pos;
+
+ for (pos = 0; pos < std_mthds[i].len; ++pos)
+ {
+ unsigned int chr;
+
+ for (chr = 0; chr <= 0xFF; ++chr)
+ {
+ memcpy (str, std_mthds[i].str, std_mthds[i].len);
+ str[pos] = (char)(unsigned char)chr;
+ check_str (std_mthds[i].len, str);
+ }
+ }
+ }
+
+ MHDT_TEST_RESULT ();
+}
+
+
+/**
+ * Check shortened and extended standard method names
+ */
+static void
+test_lengths (void)
+{
+ size_t i;
+
+ for (i = 0; i < mhd_ARR_NUM_ELEMS (std_mthds); ++i)
+ {
+ char str[TEST_MAX_LEN];
+ size_t len;
+ size_t j;
+
+ for (len = 0; len < std_mthds[i].len; ++len)
+ check_str (len, std_mthds[i].str);
+
+ for (j = 0; j < mhd_ARR_NUM_ELEMS (test_bytes); ++j)
+ {
+ memcpy (str, std_mthds[i].str, std_mthds[i].len);
+ for (len = std_mthds[i].len; len < TEST_MAX_LEN; ++len)
+ {
+ str[len] = (char)test_bytes[j];
+ check_str (len + 1, str);
+ }
+ }
+ }
+
+ MHDT_TEST_RESULT ();
+}
+
+
+/**
+ * Check inputs with a standard method name's first four and final three bytes
+ * across the tested lengths
+ */
+static void
+test_first_and_last_bytes (void)
+{
+ size_t i;
+
+ for (i = 0; i < mhd_ARR_NUM_ELEMS (std_mthds); ++i)
+ {
+ const size_t method_len = std_mthds[i].len;
+ char first_bytes[4];
+ size_t len;
+
+ if (3 > method_len)
+ continue;
+ memset (first_bytes, 0, sizeof(first_bytes));
+ memcpy (first_bytes, std_mthds[i].str, (4 > method_len) ? method_len : 4);
+
+ for (len = 3; len <= TEST_MAX_LEN; ++len)
+ {
+ char str[TEST_MAX_LEN];
+ size_t j;
+
+ for (j = 0; j < 2; ++j)
+ {
+ memset (str, (0 == j) ? 'X' : 0, len);
+ memcpy (str, first_bytes, (4 > len) ? len : 4);
+ memcpy (str + len - 3, std_mthds[i].str + method_len - 3, 3);
+ check_str (len, str);
+ }
+ }
+ }
+
+ MHDT_TEST_RESULT ();
+}
+
+
+/**
+ * Check inputs filled with one repeated byte across the tested lengths
+ */
+static void
+test_same_bytes (void)
+{
+ unsigned int chr;
+
+ for (chr = 0; chr <= 0xFF; ++chr)
+ {
+ char str[TEST_MAX_LEN];
+ size_t len;
+
+ memset (str, (int)chr, sizeof(str));
+ for (len = 0; len <= TEST_MAX_LEN; ++len)
+ check_str (len, str);
+ }
+
+ MHDT_TEST_RESULT ();
+}
+
+
+/**
+ * Advance the 32-bit state of a linear congruential generator.
+ * @param state the generator state to update
+ * @return the number 0..0x7FFF
+ */
+static MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_INOUT_ (1) unsigned int
+rnd_get (uint_least32_t *const state)
+{
+ *state = (uint_least32_t)((*state * 1103515245u + 12345u) & 0xFFFFFFFFu);
+ return (unsigned int)((*state >> 16) & 0x7FFFu);
+}
+
+
+/**
+ * Get a pseudo-random byte from #test_bytes.
+ * @param state the generator state to update
+ * @return the selected byte
+ */
+static MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_INOUT_ (1) char
+rnd_test_byte (uint_least32_t *const state)
+{
+ const size_t index = rnd_get (state) % mhd_ARR_NUM_ELEMS (test_bytes);
+
+ return (char)test_bytes[index];
+}
+
+
+/**
+ * Check pseudo-random inputs: modified standard method names, combinations of
+ * method and table bytes, and arbitrary bytes across the tested lengths.
+ * Each run starts from the same generator state.
+ */
+static void
+test_pseudo_random (void)
+{
+ uint_least32_t rnd_state = 0x12345678u;
+ uint_least32_t iteration;
+
+ for (iteration = 0; iteration < 150000u; ++iteration)
+ {
+ char str[TEST_MAX_LEN];
+ size_t len;
+ size_t j;
+
+ if (0 == iteration % 3)
+ {
+ const struct MethodTestCase *const method =
+ std_mthds + (rnd_get (&rnd_state) % mhd_ARR_NUM_ELEMS (std_mthds));
+ unsigned int num_changes;
+
+ len = method->len;
+ memcpy (str, method->str, len);
+ if (0 == rnd_get (&rnd_state) % 4)
+ len = rnd_get (&rnd_state) % 9;
+ for (j = method->len; j < len; ++j)
+ str[j] = rnd_test_byte (&rnd_state);
+ for (num_changes = rnd_get (&rnd_state) % 3;
+ 0 != num_changes; --num_changes)
+ {
+ if (0 != len)
+ {
+ const size_t pos = rnd_get (&rnd_state) % len;
+
+ str[pos] = rnd_test_byte (&rnd_state);
+ }
+ }
+ }
+ else if (1 == iteration % 3)
+ {
+ len = rnd_get (&rnd_state) % 9;
+ for (j = 0; j < len; ++j)
+ str[j] = rnd_test_byte (&rnd_state);
+ }
+ else
+ {
+ len = rnd_get (&rnd_state) % (TEST_MAX_LEN + 1);
+ for (j = 0; j < len; ++j)
+ str[j] = (char)(unsigned char)(rnd_get (&rnd_state) & 0xFFu);
+ }
+ check_str (len, str);
+ }
+
+ MHDT_TEST_RESULT ();
+}
+
+
+/**
+ * Report the checks skipped because of the failures reported above
+ * @param checks the description of the skipped checks
+ */
+static MHD_FN_PAR_NONNULL_ALL_ MHD_FN_PAR_CSTR_ (1) void
+print_skipped (const char *checks)
+{
+ fprintf (stderr,
+ "Skipped %s because of the failures above.\n\n",
+ checks);
+ fflush (stderr);
+}
+
+
+/**
+ * Allocate the buffers for the strings
+ */
+static void
+test_bufs_init (void)
+{
+ size_t i;
+
+ for (i = 0; i < mhd_ARR_NUM_ELEMS (test_bufs); ++i)
+ {
+ test_bufs[i] = (char *)malloc (i + 1);
+ if (NULL == test_bufs[i])
+ MHDT_ERR_EXIT_D ("Failed to allocate the memory");
+ }
+}
+
+
+/**
+ * Free the buffers allocated by #test_bufs_init()
+ */
+static void
+test_bufs_deinit (void)
+{
+ size_t i;
+
+ for (i = 0; i < mhd_ARR_NUM_ELEMS (test_bufs); ++i)
+ free (test_bufs[i]);
+}
+
+
+/**
+ * Run the HTTP method parsing tests.
+ * @param argc number of command-line arguments
+ * @param argv command-line arguments
+ * @return zero if all checks passed, non-zero otherwise
+ */
+int
+main (int argc,
+ char *const *argv)
+{
+ const int silent = (mhdt_has_param (argc, argv, "-s")
+ || mhdt_has_param (argc, argv, "--silent"));
+
+ /* Reporting every successful check would produce excessive output */
+ MHDT_set_verbosity (silent ? MHDT_VERB_LVL_SILENT : MHDT_VERB_LVL_BASIC);
+
+ if (!silent)
+ {
+ printf ("Testing the %s%s.\n",
+#if defined(mhd_H_M_FUNC_FOR_64)
+ "version with 64-bit keys",
+#elif defined(mhd_H_M_FUNC_FOR_32_FAST)
+ "fast version with 32-bit values",
+#elif defined(mhd_H_M_FUNC_FOR_32_COMPACT)
+ "compact version with 32-bit values",
+#else
+ "version automatically selected for the platform",
+#endif
+#ifdef mhd_H_M_TEST_UNKNOWN_ORDER
+ " for the unknown byte order"
+#else
+ ""
+#endif /* mhd_H_M_TEST_UNKNOWN_ORDER */
+ );
+ fflush (stdout);
+ }
+
+ test_bufs_init ();
+
+ /* The stages increase in volume. A defect usually affects many inputs, and
+ a larger stage would report it thousands of times, so a stage runs only
+ if the previous stages passed. Within the systematic stage all the tests
+ run, even after a failure, to show which kinds of inputs are affected. */
+ test_known_results ();
+ if (!MHDT_are_all_tests_succeed ())
+ print_skipped ("the systematic and the pseudo-random checks");
+ else
+ {
+ test_short_strings ();
+ test_one_byte_changed ();
+ test_lengths ();
+ test_first_and_last_bytes ();
+ test_same_bytes ();
+
+ if (!MHDT_are_all_tests_succeed ())
+ print_skipped ("the pseudo-random checks");
+ else
+ test_pseudo_random ();
+ }
+
+ test_bufs_deinit ();
+
+ return MHDT_FINAL_RESULT (argv[0]);
+}