libmicrohttpd

HTTP/1.x server C library (MHD 1.x, stable)
Log | Files | Refs | Submodules | README | LICENSE

test_mhd_version.c (6845B)


      1 /*
      2   This file is part of libmicrohttpd
      3   Copyright (C) 2023 Evgeny Grin (Karlson2k)
      4 
      5   This test tool is free software; you can redistribute it and/or
      6   modify it under the terms of the GNU Lesser General Public
      7   License as published by the Free Software Foundation; either
      8   version 2.1 of the License, or (at your option) any later version.
      9 
     10   This test tool is distributed in the hope that it will be useful,
     11   but WITHOUT ANY WARRANTY; without even the implied warranty of
     12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13   Lesser General Public License for more details.
     14 
     15   You should have received a copy of the GNU Lesser General Public
     16   License along with this library.
     17   If not, see <http://www.gnu.org/licenses/>.
     18 */
     19 
     20 /**
     21  * @file microhttpd/test_mhd_version.с
     22  * @brief  Tests for MHD versions identifiers
     23  * @author Karlson2k (Evgeny Grin)
     24  */
     25 
     26 #include "mhd_options.h"
     27 #include <stdio.h>
     28 #include <string.h>
     29 #include <stdint.h>
     30 #ifdef HAVE_INTTYPES_H
     31 #include <inttypes.h>
     32 #else  /* ! HAVE_INTTYPES_H */
     33 #define PRIX32 "X"
     34 #endif /* ! HAVE_INTTYPES_H */
     35 #ifdef HAVE_STDLIB_H
     36 #include <stdlib.h>
     37 #endif /* HAVE_STDLIB_H */
     38 #include "microhttpd.h"
     39 
     40 /* Turn any MHD_PANIC() or failing mhd_assert() reached from this
     41    test into a marked, classifiable test error (TESTING.md, P5). */
     42 #include "mhd_panic_tripwire.h"
     43 
     44 
     45 #ifdef PACKAGE_VERSION
     46 static const char str_macro_pkg_ver[] = PACKAGE_VERSION;
     47 #else  /* ! PACKAGE_VERSION */
     48 static const char str_macro_pkg_ver[] = "error!";
     49 #error No PACKAGE_VERSION defined
     50 #endif /* ! PACKAGE_VERSION */
     51 
     52 #ifdef VERSION
     53 static const char str_macro_ver[] = VERSION;
     54 #else  /* ! VERSION */
     55 static const char str_macro_ver[] = "error!";
     56 #error No PACKAGE_VERSION defined
     57 #endif /* ! VERSION */
     58 
     59 #ifdef MHD_VERSION
     60 static const uint32_t bin_macro = (uint32_t) (MHD_VERSION);
     61 #else  /* ! MHD_VERSION */
     62 static const uint32_t bin_macro = 0;
     63 #error MHD_VERSION is not defined
     64 #endif /* ! MHD_VERSION */
     65 
     66 /* 0 = success, 1 = failure */
     67 static int
     68 test_macro1_vs_macro2_str (void)
     69 {
     70   printf ("Checking PACKAGE_VERSION macro vs VERSION macro.\n");
     71   if (0 != strcmp (str_macro_pkg_ver, str_macro_ver))
     72   {
     73     fflush (stdout);
     74     fprintf (stderr, "'%s' vs '%s' - FAILED.\n",
     75              str_macro_pkg_ver, str_macro_ver);
     76     return 1;
     77   }
     78   printf ("'%s' vs '%s' - success.\n",
     79           str_macro_pkg_ver, str_macro_ver);
     80   return 0;
     81 }
     82 
     83 
     84 /* 0 = success, 1 = failure */
     85 static int
     86 test_macro2_vs_func_str (void)
     87 {
     88   const char *str_func = MHD_get_version ();
     89   printf ("Checking VERSION macro vs MHD_get_version() function.\n");
     90   if (NULL == str_func)
     91   {
     92     fflush (stdout);
     93     fprintf (stderr, "MHD_get_version() returned NULL.\n");
     94     fflush (stderr);
     95     return 1;
     96   }
     97   if (0 != strcmp (str_macro_ver, str_func))
     98   {
     99     fflush (stdout);
    100     fprintf (stderr, "'%s' vs '%s' - FAILED.\n",
    101              str_macro_ver, str_func);
    102     fflush (stderr);
    103     return 1;
    104   }
    105   printf ("'%s' vs '%s' - success.\n",
    106           str_macro_ver, str_func);
    107   return 0;
    108 }
    109 
    110 
    111 /* 0 = success, 1 = failure */
    112 static int
    113 test_func_str_vs_macro_bin (void)
    114 {
    115   char bin_print[64];
    116   int res;
    117   const char *str_func = MHD_get_version ();
    118 
    119   printf ("Checking MHD_get_version() function vs MHD_VERSION macro.\n");
    120 #ifdef HAVE_SNPRINTF
    121   res = snprintf (bin_print, sizeof(bin_print), "%X.%X.%X",
    122                   (unsigned int) ((bin_macro >> 24) & 0xFF),
    123                   (unsigned int) ((bin_macro >> 16) & 0xFF),
    124                   (unsigned int) ((bin_macro >> 8) & 0xFF));
    125 #else  /* ! HAVE_SNPRINTF */
    126   res = sprintf (bin_print, "%X.%X.%X",
    127                  (unsigned int) ((bin_macro >> 24) & 0xFF),
    128                  (unsigned int) ((bin_macro >> 16) & 0xFF),
    129                  (unsigned int) ((bin_macro >> 8) & 0xFF));
    130 #endif /* ! HAVE_SNPRINTF */
    131   if ((9 < res) || (0 >= res))
    132   {
    133     fflush (stdout);
    134     fprintf (stderr, "snprintf() error.\n");
    135     fflush (stderr);
    136     exit (99);
    137   }
    138 
    139   if (0 != strcmp (str_func, bin_print))
    140   {
    141     fflush (stdout);
    142     fprintf (stderr, "'%s' vs '0x%08" PRIX32 "' ('%s') - FAILED.\n",
    143              str_func,
    144              bin_macro,
    145              bin_print);
    146     fflush (stderr);
    147     return 1;
    148   }
    149   printf ("'%s' vs '0x%08" PRIX32 "' ('%s') - success.\n",
    150           str_func,
    151           bin_macro,
    152           bin_print);
    153   return 0;
    154 }
    155 
    156 
    157 /* 0 = success, 1 = failure */
    158 static int
    159 test_macro_vs_func_bin (void)
    160 {
    161   const uint32_t bin_func = MHD_get_version_bin ();
    162 
    163   printf ("Checking MHD_VERSION macro vs MHD_get_version_bin() function.\n");
    164   if (bin_macro != bin_func)
    165   {
    166     fflush (stdout);
    167     fprintf (stderr, "'0x%08" PRIX32 "' vs '0x%08" PRIX32 "' - FAILED.\n",
    168              bin_macro, bin_func);
    169     fflush (stderr);
    170     return 1;
    171   }
    172   printf ("'0x%08" PRIX32 "' vs '0x%08" PRIX32 "' - success.\n",
    173           bin_macro, bin_func);
    174   return 0;
    175 }
    176 
    177 
    178 /* 0 = success, 1 = failure */
    179 static int
    180 test_func_bin_format (void)
    181 {
    182   const uint32_t bin_func = MHD_get_version_bin ();
    183   unsigned int test_byte;
    184   int ret = 0;
    185   printf ("Checking format of MHD_get_version_bin() function return value.\n");
    186   test_byte = (unsigned int) ((bin_func >> 24) & 0xFF);
    187   if ((0xA <= (test_byte & 0xF))
    188       || (0xA <= (test_byte >> 4)))
    189   {
    190     fflush (stdout);
    191     fprintf (stderr,
    192              "Invalid value in the first (most significant) byte: %02X\n",
    193              test_byte);
    194     fflush (stderr);
    195     ret = 1;
    196   }
    197   test_byte = (unsigned int) ((bin_func >> 16) & 0xFF);
    198   if ((0xA <= (test_byte & 0xF))
    199       || (0xA <= (test_byte >> 4)))
    200   {
    201     fflush (stdout);
    202     fprintf (stderr,
    203              "Invalid value in the second byte: %02X\n",
    204              test_byte);
    205     fflush (stderr);
    206     ret = 1;
    207   }
    208   test_byte = (unsigned int) ((bin_func >> 8) & 0xFF);
    209   if ((0xA <= (test_byte & 0xF))
    210       || (0xA <= (test_byte >> 4)))
    211   {
    212     fflush (stdout);
    213     fprintf (stderr,
    214              "Invalid value in the third byte: %02X\n",
    215              test_byte);
    216     fflush (stderr);
    217     ret = 1;
    218   }
    219   if (0 != ret)
    220   {
    221     fflush (stdout);
    222     fprintf (stderr,
    223              "The value (0x%08" PRIX32 ") returned by MHD_get_version_bin() "
    224              "function is invalid as it cannot be used as packed BCD form "
    225              "(its hexadecimal representation has at least one digit in "
    226              "A-F range).\n",
    227              bin_func);
    228     fflush (stderr);
    229     return 1;
    230   }
    231   printf ("'0x%08" PRIX32 "' - success.\n", bin_func);
    232   return 0;
    233 }
    234 
    235 
    236 int
    237 main (void)
    238 {
    239   int res;
    240   res = test_macro1_vs_macro2_str ();
    241   res += test_macro2_vs_func_str ();
    242   res += test_func_str_vs_macro_bin ();
    243   res += test_macro_vs_func_bin ();
    244   res += test_func_bin_format ();
    245 
    246   if (0 != res)
    247   {
    248     fflush (stdout);
    249     fprintf (stderr, "Test failed. Number of errors: %d\n", res);
    250     fflush (stderr);
    251     return 1;
    252   }
    253   printf ("Test succeed.\n");
    254   return 0;
    255 }