libmicrohttpd

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

test_mhd_version.c (6249B)


      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 
     41 #ifdef PACKAGE_VERSION
     42 static const char str_macro_pkg_ver[] = PACKAGE_VERSION;
     43 #else  /* ! PACKAGE_VERSION */
     44 static const char str_macro_pkg_ver[] = "error!";
     45 #error No PACKAGE_VERSION defined
     46 #endif /* ! PACKAGE_VERSION */
     47 
     48 #ifdef VERSION
     49 static const char str_macro_ver[] = VERSION;
     50 #else  /* ! VERSION */
     51 static const char str_macro_ver[] = "error!";
     52 #error No PACKAGE_VERSION defined
     53 #endif /* ! VERSION */
     54 
     55 #ifdef MHD_VERSION
     56 static const uint32_t bin_macro = (uint32_t) (MHD_VERSION);
     57 #else  /* ! MHD_VERSION */
     58 static const uint32_t bin_macro = 0;
     59 #error MHD_VERSION is not defined
     60 #endif /* ! MHD_VERSION */
     61 
     62 /* 0 = success, 1 = failure */
     63 static int
     64 test_macro1_vs_macro2_str (void)
     65 {
     66   printf ("Checking PACKAGE_VERSION macro vs VERSION macro.\n");
     67   if (0 != strcmp (str_macro_pkg_ver, str_macro_ver))
     68   {
     69     fprintf (stderr, "'%s' vs '%s' - FAILED.\n",
     70              str_macro_pkg_ver, str_macro_ver);
     71     return 1;
     72   }
     73   printf ("'%s' vs '%s' - success.\n",
     74           str_macro_pkg_ver, str_macro_ver);
     75   return 0;
     76 }
     77 
     78 
     79 /* 0 = success, 1 = failure */
     80 static int
     81 test_macro2_vs_func_str (void)
     82 {
     83   const char *str_func = MHD_get_version ();
     84   printf ("Checking VERSION macro vs MHD_get_version() function.\n");
     85   if (NULL == str_func)
     86   {
     87     fprintf (stderr, "MHD_get_version() returned NULL.\n");
     88     return 1;
     89   }
     90   if (0 != strcmp (str_macro_ver, str_func))
     91   {
     92     fprintf (stderr, "'%s' vs '%s' - FAILED.\n",
     93              str_macro_ver, str_func);
     94     return 1;
     95   }
     96   printf ("'%s' vs '%s' - success.\n",
     97           str_macro_ver, str_func);
     98   return 0;
     99 }
    100 
    101 
    102 /* 0 = success, 1 = failure */
    103 static int
    104 test_func_str_vs_macro_bin (void)
    105 {
    106   char bin_print[64];
    107   int res;
    108   const char *str_func = MHD_get_version ();
    109 
    110   printf ("Checking MHD_get_version() function vs MHD_VERSION macro.\n");
    111 #ifdef HAVE_SNPRINTF
    112   res = snprintf (bin_print, sizeof(bin_print), "%X.%X.%X",
    113                   (unsigned int) ((bin_macro >> 24) & 0xFF),
    114                   (unsigned int) ((bin_macro >> 16) & 0xFF),
    115                   (unsigned int) ((bin_macro >> 8) & 0xFF));
    116 #else  /* ! HAVE_SNPRINTF */
    117   res = sprintf (bin_print, "%X.%X.%X",
    118                  (unsigned int) ((bin_macro >> 24) & 0xFF),
    119                  (unsigned int) ((bin_macro >> 16) & 0xFF),
    120                  (unsigned int) ((bin_macro >> 8) & 0xFF));
    121 #endif /* ! HAVE_SNPRINTF */
    122   if ((9 < res) || (0 >= res))
    123   {
    124     fprintf (stderr, "snprintf() error.\n");
    125     exit (99);
    126   }
    127 
    128   if (0 != strcmp (str_func, bin_print))
    129   {
    130     fprintf (stderr, "'%s' vs '0x%08" PRIX32 "' ('%s') - FAILED.\n",
    131              str_func,
    132              bin_macro,
    133              bin_print);
    134     return 1;
    135   }
    136   fprintf (stderr, "'%s' vs '0x%08" PRIX32 "' ('%s') - success.\n",
    137            str_func,
    138            bin_macro,
    139            bin_print);
    140   return 0;
    141 }
    142 
    143 
    144 /* 0 = success, 1 = failure */
    145 static int
    146 test_macro_vs_func_bin (void)
    147 {
    148   const uint32_t bin_func = MHD_get_version_bin ();
    149 
    150   printf ("Checking MHD_VERSION macro vs MHD_get_version_bin() function.\n");
    151   if (bin_macro != bin_func)
    152   {
    153     fprintf (stderr, "'0x%08" PRIX32 "' vs '0x%08" PRIX32 "' - FAILED.\n",
    154              bin_macro, bin_func);
    155     return 1;
    156   }
    157   printf ("'0x%08" PRIX32 "' vs '0x%08" PRIX32 "' - success.\n",
    158           bin_macro, bin_func);
    159   return 0;
    160 }
    161 
    162 
    163 /* 0 = success, 1 = failure */
    164 static int
    165 test_func_bin_format (void)
    166 {
    167   const uint32_t bin_func = MHD_get_version_bin ();
    168   unsigned int test_byte;
    169   int ret = 0;
    170   printf ("Checking format of MHD_get_version_bin() function return value.\n");
    171   test_byte = (unsigned int) ((bin_func >> 24) & 0xFF);
    172   if ((0xA <= (test_byte & 0xF))
    173       || (0xA <= (test_byte >> 4)))
    174   {
    175     fprintf (stderr,
    176              "Invalid value in the first (most significant) byte: %02X\n",
    177              test_byte);
    178     ret = 1;
    179   }
    180   test_byte = (unsigned int) ((bin_func >> 16) & 0xFF);
    181   if ((0xA <= (test_byte & 0xF))
    182       || (0xA <= (test_byte >> 4)))
    183   {
    184     fprintf (stderr,
    185              "Invalid value in the second byte: %02X\n",
    186              test_byte);
    187     ret = 1;
    188   }
    189   test_byte = (unsigned int) ((bin_func >> 8) & 0xFF);
    190   if ((0xA <= (test_byte & 0xF))
    191       || (0xA <= (test_byte >> 4)))
    192   {
    193     fprintf (stderr,
    194              "Invalid value in the third byte: %02X\n",
    195              test_byte);
    196     ret = 1;
    197   }
    198   if (0 != ret)
    199   {
    200     fprintf (stderr,
    201              "The value (0x%08" PRIX32 ") returned by MHD_get_version_bin() "
    202              "function is invalid as it cannot be used as packed BCD form "
    203              "(its hexadecimal representation has at least one digit in "
    204              "A-F range).\n",
    205              bin_func);
    206     return 1;
    207   }
    208   printf ("'0x%08" PRIX32 "' - success.\n", bin_func);
    209   return 0;
    210 }
    211 
    212 
    213 int
    214 main (void)
    215 {
    216   int res;
    217   res = test_macro1_vs_macro2_str ();
    218   res += test_macro2_vs_func_str ();
    219   res += test_func_str_vs_macro_bin ();
    220   res += test_macro_vs_func_bin ();
    221   res += test_func_bin_format ();
    222 
    223   if (0 != res)
    224   {
    225     fprintf (stderr, "Test failed. Number of errors: %d\n", res);
    226     return 1;
    227   }
    228   printf ("Test succeed.\n");
    229   return 0;
    230 }