1 /*
2 * Copyright 2016-2025 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #define TESTUTIL_NO_size_t_COMPARISON
11
12 #include <stdio.h>
13 #include <string.h>
14 #include <openssl/bio.h>
15 #include "internal/nelem.h"
16 #include "internal/numbers.h"
17 #include "testutil.h"
18 #include "testutil/output.h"
19
20 static int justprint = 0;
21
22 static char *fpexpected[][11][5] = {
23 {
24 /* 0.00 */ { "0.0000e+00", "0.0000", "0", "0.0000E+00", "0" },
25 /* 0.01 */ { "6.7000e-01", "0.6700", "0.67", "6.7000E-01", "0.67" },
26 /* 0.02 */ { "6.6667e-01", "0.6667", "0.6667", "6.6667E-01", "0.6667" },
27 /* 0.03 */ { "6.6667e-04", "0.0007", "0.0006667", "6.6667E-04", "0.0006667" },
28 /* 0.04 */ { "6.6667e-05", "0.0001", "6.667e-05", "6.6667E-05", "6.667E-05" },
29 /* 0.05 */ { "6.6667e+00", "6.6667", "6.667", "6.6667E+00", "6.667" },
30 /* 0.06 */ { "6.6667e+01", "66.6667", "66.67", "6.6667E+01", "66.67" },
31 /* 0.07 */ { "6.6667e+02", "666.6667", "666.7", "6.6667E+02", "666.7" },
32 /* 0.08 */ { "6.6667e+03", "6666.6667", "6667", "6.6667E+03", "6667" },
33 /* 0.09 */ { "6.6667e+04", "66666.6667", "6.667e+04", "6.6667E+04", "6.667E+04" },
34 /* 0.10 */ { "-6.6667e+04", "-66666.6667", "-6.667e+04", "-6.6667E+04", "-6.667E+04" },
35 },
36 {
37 /* 1.00 */ { "0.00000e+00", "0.00000", "0", "0.00000E+00", "0" },
38 /* 1.01 */ { "6.70000e-01", "0.67000", "0.67", "6.70000E-01", "0.67" },
39 /* 1.02 */ { "6.66667e-01", "0.66667", "0.66667", "6.66667E-01", "0.66667" },
40 /* 1.03 */ { "6.66667e-04", "0.00067", "0.00066667", "6.66667E-04", "0.00066667" },
41 /* 1.04 */ { "6.66667e-05", "0.00007", "6.6667e-05", "6.66667E-05", "6.6667E-05" },
42 /* 1.05 */ { "6.66667e+00", "6.66667", "6.6667", "6.66667E+00", "6.6667" },
43 /* 1.06 */ { "6.66667e+01", "66.66667", "66.667", "6.66667E+01", "66.667" },
44 /* 1.07 */ { "6.66667e+02", "666.66667", "666.67", "6.66667E+02", "666.67" },
45 /* 1.08 */ { "6.66667e+03", "6666.66667", "6666.7", "6.66667E+03", "6666.7" },
46 /* 1.09 */ { "6.66667e+04", "66666.66667", "66667", "6.66667E+04", "66667" },
47 /* 1.10 */ { "-6.66667e+04", "-66666.66667", "-66667", "-6.66667E+04", "-66667" },
48 },
49 {
50 /* 2.00 */ { " 0.0000e+00", " 0.0000", " 0", " 0.0000E+00", " 0" },
51 /* 2.01 */ { " 6.7000e-01", " 0.6700", " 0.67", " 6.7000E-01", " 0.67" },
52 /* 2.02 */ { " 6.6667e-01", " 0.6667", " 0.6667", " 6.6667E-01", " 0.6667" },
53 /* 2.03 */ { " 6.6667e-04", " 0.0007", " 0.0006667", " 6.6667E-04", " 0.0006667" },
54 /* 2.04 */ { " 6.6667e-05", " 0.0001", " 6.667e-05", " 6.6667E-05", " 6.667E-05" },
55 /* 2.05 */ { " 6.6667e+00", " 6.6667", " 6.667", " 6.6667E+00", " 6.667" },
56 /* 2.06 */ { " 6.6667e+01", " 66.6667", " 66.67", " 6.6667E+01", " 66.67" },
57 /* 2.07 */ { " 6.6667e+02", " 666.6667", " 666.7", " 6.6667E+02", " 666.7" },
58 /* 2.08 */ { " 6.6667e+03", " 6666.6667", " 6667", " 6.6667E+03", " 6667" },
59 /* 2.09 */ { " 6.6667e+04", " 66666.6667", " 6.667e+04", " 6.6667E+04", " 6.667E+04" },
60 /* 2.10 */ { " -6.6667e+04", " -66666.6667", " -6.667e+04", " -6.6667E+04", " -6.667E+04" },
61 },
62 {
63 /* 3.00 */ { " 0.00000e+00", " 0.00000", " 0", " 0.00000E+00", " 0" },
64 /* 3.01 */ { " 6.70000e-01", " 0.67000", " 0.67", " 6.70000E-01", " 0.67" },
65 /* 3.02 */ { " 6.66667e-01", " 0.66667", " 0.66667", " 6.66667E-01", " 0.66667" },
66 /* 3.03 */ { " 6.66667e-04", " 0.00067", " 0.00066667", " 6.66667E-04", " 0.00066667" },
67 /* 3.04 */ { " 6.66667e-05", " 0.00007", " 6.6667e-05", " 6.66667E-05", " 6.6667E-05" },
68 /* 3.05 */ { " 6.66667e+00", " 6.66667", " 6.6667", " 6.66667E+00", " 6.6667" },
69 /* 3.06 */ { " 6.66667e+01", " 66.66667", " 66.667", " 6.66667E+01", " 66.667" },
70 /* 3.07 */ { " 6.66667e+02", " 666.66667", " 666.67", " 6.66667E+02", " 666.67" },
71 /* 3.08 */ { " 6.66667e+03", " 6666.66667", " 6666.7", " 6.66667E+03", " 6666.7" },
72 /* 3.09 */ { " 6.66667e+04", " 66666.66667", " 66667", " 6.66667E+04", " 66667" },
73 /* 3.10 */ { "-6.66667e+04", "-66666.66667", " -66667", "-6.66667E+04", " -66667" },
74 },
75 {
76 /* 4.00 */ { "0e+00", "0", "0", "0E+00", "0" },
77 /* 4.01 */ { "7e-01", "1", "0.7", "7E-01", "0.7" },
78 /* 4.02 */ { "7e-01", "1", "0.7", "7E-01", "0.7" },
79 /* 4.03 */ { "7e-04", "0", "0.0007", "7E-04", "0.0007" },
80 /* 4.04 */ { "7e-05", "0", "7e-05", "7E-05", "7E-05" },
81 /* 4.05 */ { "7e+00", "7", "7", "7E+00", "7" },
82 /* 4.06 */ { "7e+01", "67", "7e+01", "7E+01", "7E+01" },
83 /* 4.07 */ { "7e+02", "667", "7e+02", "7E+02", "7E+02" },
84 /* 4.08 */ { "7e+03", "6667", "7e+03", "7E+03", "7E+03" },
85 /* 4.09 */ { "7e+04", "66667", "7e+04", "7E+04", "7E+04" },
86 /* 4.10 */ { "-7e+04", "-66667", "-7e+04", "-7E+04", "-7E+04" },
87 },
88 {
89 /* 5.00 */ { "0.000000e+00", "0.000000", "0", "0.000000E+00", "0" },
90 /* 5.01 */ { "6.700000e-01", "0.670000", "0.67", "6.700000E-01", "0.67" },
91 /* 5.02 */ { "6.666667e-01", "0.666667", "0.666667", "6.666667E-01", "0.666667" },
92 /* 5.03 */ { "6.666667e-04", "0.000667", "0.000666667", "6.666667E-04", "0.000666667" },
93 /* 5.04 */ { "6.666667e-05", "0.000067", "6.66667e-05", "6.666667E-05", "6.66667E-05" },
94 /* 5.05 */ { "6.666667e+00", "6.666667", "6.66667", "6.666667E+00", "6.66667" },
95 /* 5.06 */ { "6.666667e+01", "66.666667", "66.6667", "6.666667E+01", "66.6667" },
96 /* 5.07 */ { "6.666667e+02", "666.666667", "666.667", "6.666667E+02", "666.667" },
97 /* 5.08 */ { "6.666667e+03", "6666.666667", "6666.67", "6.666667E+03", "6666.67" },
98 /* 5.09 */ { "6.666667e+04", "66666.666667", "66666.7", "6.666667E+04", "66666.7" },
99 /* 5.10 */ { "-6.666667e+04", "-66666.666667", "-66666.7", "-6.666667E+04", "-66666.7" },
100 },
101 {
102 /* 6.00 */ { "0.0000e+00", "000.0000", "00000000", "0.0000E+00", "00000000" },
103 /* 6.01 */ { "6.7000e-01", "000.6700", "00000.67", "6.7000E-01", "00000.67" },
104 /* 6.02 */ { "6.6667e-01", "000.6667", "000.6667", "6.6667E-01", "000.6667" },
105 /* 6.03 */ { "6.6667e-04", "000.0007", "0.0006667", "6.6667E-04", "0.0006667" },
106 /* 6.04 */ { "6.6667e-05", "000.0001", "6.667e-05", "6.6667E-05", "6.667E-05" },
107 /* 6.05 */ { "6.6667e+00", "006.6667", "0006.667", "6.6667E+00", "0006.667" },
108 /* 6.06 */ { "6.6667e+01", "066.6667", "00066.67", "6.6667E+01", "00066.67" },
109 /* 6.07 */ { "6.6667e+02", "666.6667", "000666.7", "6.6667E+02", "000666.7" },
110 /* 6.08 */ { "6.6667e+03", "6666.6667", "00006667", "6.6667E+03", "00006667" },
111 /* 6.09 */ { "6.6667e+04", "66666.6667", "6.667e+04", "6.6667E+04", "6.667E+04" },
112 /* 6.10 */ { "-6.6667e+04", "-66666.6667", "-6.667e+04", "-6.6667E+04", "-6.667E+04" },
113 },
114 };
115
116 typedef struct z_data_st {
117 size_t value;
118 const char *format;
119 const char *expected;
120 } z_data;
121
122 static z_data zu_data[] = {
123 { SIZE_MAX, "%zu", (sizeof(size_t) == 4 ? "4294967295"
124 : sizeof(size_t) == 8 ? "18446744073709551615"
125 : "") },
126 /*
127 * in 2-complement, the unsigned number divided by two plus one becomes the
128 * smallest possible negative signed number of the corresponding type
129 */
130 { SIZE_MAX / 2 + 1, "%zi", (sizeof(size_t) == 4 ? "-2147483648"
131 : sizeof(size_t) == 8 ? "-9223372036854775808"
132 : "") },
133 { 0, "%zu", "0" },
134 { 0, "%zi", "0" },
135 };
136
test_zu(int i)137 static int test_zu(int i)
138 {
139 char bio_buf[80];
140 const z_data *data = &zu_data[i];
141
142 BIO_snprintf(bio_buf, sizeof(bio_buf) - 1, data->format, data->value);
143 if (!TEST_str_eq(bio_buf, data->expected))
144 return 0;
145 return 1;
146 }
147
148 typedef struct j_data_st {
149 uint64_t value;
150 const char *format;
151 const char *expected;
152 } j_data;
153
154 static j_data jf_data[] = {
155 { 0xffffffffffffffffULL, "%ju", "18446744073709551615" },
156 { 0xffffffffffffffffULL, "%jx", "ffffffffffffffff" },
157 { 0x8000000000000000ULL, "%ju", "9223372036854775808" },
158 /*
159 * These tests imply two's complement, but it's the only binary
160 * representation we support, see test/sanitytest.c...
161 */
162 { 0x8000000000000000ULL, "%ji", "-9223372036854775808" },
163 };
164
test_j(int i)165 static int test_j(int i)
166 {
167 const j_data *data = &jf_data[i];
168 char bio_buf[80];
169
170 BIO_snprintf(bio_buf, sizeof(bio_buf) - 1, data->format, data->value);
171 if (!TEST_str_eq(bio_buf, data->expected))
172 return 0;
173 return 1;
174 }
175
176
177 /* Precision and width. */
178 typedef struct pw_st {
179 int p;
180 const char *w;
181 } pw;
182
183 static pw pw_params[] = {
184 { 4, "" },
185 { 5, "" },
186 { 4, "12" },
187 { 5, "12" },
188 { 0, "" },
189 { -1, "" },
190 { 4, "08" }
191 };
192
dofptest(int test,int sub,double val,const char * width,int prec)193 static int dofptest(int test, int sub, double val, const char *width, int prec)
194 {
195 static const char *fspecs[] = {
196 "e", "f", "g", "E", "G"
197 };
198 char format[80], result[80];
199 int ret = 1, i;
200
201 for (i = 0; i < (int)OSSL_NELEM(fspecs); i++) {
202 const char *fspec = fspecs[i];
203
204 if (prec >= 0)
205 BIO_snprintf(format, sizeof(format), "%%%s.%d%s", width, prec,
206 fspec);
207 else
208 BIO_snprintf(format, sizeof(format), "%%%s%s", width, fspec);
209 BIO_snprintf(result, sizeof(result), format, val);
210
211 if (justprint) {
212 if (i == 0)
213 printf(" /* %d.%02d */ { \"%s\"", test, sub, result);
214 else
215 printf(", \"%s\"", result);
216 } else if (!TEST_str_eq(fpexpected[test][sub][i], result)) {
217 TEST_info("test %d format=|%s| exp=|%s|, ret=|%s|",
218 test, format, fpexpected[test][sub][i], result);
219 ret = 0;
220 }
221 }
222 if (justprint)
223 printf(" },\n");
224 return ret;
225 }
226
test_fp(int i)227 static int test_fp(int i)
228 {
229 int t = 0, r;
230 const double frac = 2.0 / 3.0;
231 const pw *pwp = &pw_params[i];
232
233 if (justprint)
234 printf(" {\n");
235 r = TEST_true(dofptest(i, t++, 0.0, pwp->w, pwp->p))
236 && TEST_true(dofptest(i, t++, 0.67, pwp->w, pwp->p))
237 && TEST_true(dofptest(i, t++, frac, pwp->w, pwp->p))
238 && TEST_true(dofptest(i, t++, frac / 1000, pwp->w, pwp->p))
239 && TEST_true(dofptest(i, t++, frac / 10000, pwp->w, pwp->p))
240 && TEST_true(dofptest(i, t++, 6.0 + frac, pwp->w, pwp->p))
241 && TEST_true(dofptest(i, t++, 66.0 + frac, pwp->w, pwp->p))
242 && TEST_true(dofptest(i, t++, 666.0 + frac, pwp->w, pwp->p))
243 && TEST_true(dofptest(i, t++, 6666.0 + frac, pwp->w, pwp->p))
244 && TEST_true(dofptest(i, t++, 66666.0 + frac, pwp->w, pwp->p))
245 && TEST_true(dofptest(i, t++, -66666.0 - frac, pwp->w, pwp->p));
246 if (justprint)
247 printf(" },\n");
248 return r;
249 }
250
test_big(void)251 static int test_big(void)
252 {
253 char buf[80];
254
255 /* Test excessively big number. Should fail */
256 if (!TEST_int_eq(BIO_snprintf(buf, sizeof(buf),
257 "%f\n", 2 * (double)ULONG_MAX), -1))
258 return 0;
259
260 return 1;
261 }
262
263 typedef enum OPTION_choice {
264 OPT_ERR = -1,
265 OPT_EOF = 0,
266 OPT_PRINT,
267 OPT_TEST_ENUM
268 } OPTION_CHOICE;
269
test_get_options(void)270 const OPTIONS *test_get_options(void)
271 {
272 static const OPTIONS options[] = {
273 OPT_TEST_OPTIONS_DEFAULT_USAGE,
274 { "expected", OPT_PRINT, '-', "Output values" },
275 { NULL }
276 };
277 return options;
278 }
279
setup_tests(void)280 int setup_tests(void)
281 {
282 OPTION_CHOICE o;
283
284 while ((o = opt_next()) != OPT_EOF) {
285 switch (o) {
286 case OPT_PRINT:
287 justprint = 1;
288 break;
289 case OPT_TEST_CASES:
290 break;
291 default:
292 return 0;
293 }
294 }
295
296 ADD_TEST(test_big);
297 ADD_ALL_TESTS(test_fp, OSSL_NELEM(pw_params));
298 ADD_ALL_TESTS(test_zu, OSSL_NELEM(zu_data));
299 ADD_ALL_TESTS(test_j, OSSL_NELEM(jf_data));
300 return 1;
301 }
302
303 /*
304 * Replace testutil output routines. We do this to eliminate possible sources
305 * of BIO error
306 */
307 BIO *bio_out = NULL;
308 BIO *bio_err = NULL;
309
310 static int tap_level = 0;
311
test_open_streams(void)312 void test_open_streams(void)
313 {
314 }
315
test_adjust_streams_tap_level(int level)316 void test_adjust_streams_tap_level(int level)
317 {
318 tap_level = level;
319 }
320
test_close_streams(void)321 void test_close_streams(void)
322 {
323 }
324
325 /*
326 * This works out as long as caller doesn't use any "fancy" formats.
327 * But we are caller's caller, and test_str_eq is the only one called,
328 * and it uses only "%s", which is not "fancy"...
329 */
test_vprintf_stdout(const char * fmt,va_list ap)330 int test_vprintf_stdout(const char *fmt, va_list ap)
331 {
332 return fprintf(stdout, "%*s# ", tap_level, "") + vfprintf(stdout, fmt, ap);
333 }
334
test_vprintf_stderr(const char * fmt,va_list ap)335 int test_vprintf_stderr(const char *fmt, va_list ap)
336 {
337 return fprintf(stderr, "%*s# ", tap_level, "") + vfprintf(stderr, fmt, ap);
338 }
339
test_flush_stdout(void)340 int test_flush_stdout(void)
341 {
342 return fflush(stdout);
343 }
344
test_flush_stderr(void)345 int test_flush_stderr(void)
346 {
347 return fflush(stderr);
348 }
349
test_vprintf_tapout(const char * fmt,va_list ap)350 int test_vprintf_tapout(const char *fmt, va_list ap)
351 {
352 return fprintf(stdout, "%*s", tap_level, "") + vfprintf(stdout, fmt, ap);
353 }
354
test_vprintf_taperr(const char * fmt,va_list ap)355 int test_vprintf_taperr(const char *fmt, va_list ap)
356 {
357 return fprintf(stderr, "%*s", tap_level, "") + vfprintf(stderr, fmt, ap);
358 }
359
test_flush_tapout(void)360 int test_flush_tapout(void)
361 {
362 return fflush(stdout);
363 }
364
test_flush_taperr(void)365 int test_flush_taperr(void)
366 {
367 return fflush(stderr);
368 }
369
370