Debian Codegraph @

src/microhttpd/test_mhd_version.c

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