xref: /src/crypto/openssl/test/bioprinttest.c (revision f25b8c9fb4f58cf61adb47d7570abe7caa6d385d)
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" : sizeof(size_t) == 8 ? "18446744073709551615"
124                                                                                  : "") },
125     /*
126      * in 2-complement, the unsigned number divided by two plus one becomes the
127      * smallest possible negative signed number of the corresponding type
128      */
129     { SIZE_MAX / 2 + 1, "%zi", (sizeof(size_t) == 4 ? "-2147483648" : sizeof(size_t) == 8 ? "-9223372036854775808"
130                                                                                           : "") },
131     { 0, "%zu", "0" },
132     { 0, "%zi", "0" },
133 };
134 
test_zu(int i)135 static int test_zu(int i)
136 {
137     char bio_buf[80];
138     const z_data *data = &zu_data[i];
139 
140     BIO_snprintf(bio_buf, sizeof(bio_buf) - 1, data->format, data->value);
141     if (!TEST_str_eq(bio_buf, data->expected))
142         return 0;
143     return 1;
144 }
145 
146 typedef struct j_data_st {
147     uint64_t value;
148     const char *format;
149     const char *expected;
150 } j_data;
151 
152 static j_data jf_data[] = {
153     { 0xffffffffffffffffULL, "%ju", "18446744073709551615" },
154     { 0xffffffffffffffffULL, "%jx", "ffffffffffffffff" },
155     { 0x8000000000000000ULL, "%ju", "9223372036854775808" },
156     /*
157      * These tests imply two's complement, but it's the only binary
158      * representation we support, see test/sanitytest.c...
159      */
160     { 0x8000000000000000ULL, "%ji", "-9223372036854775808" },
161 };
162 
test_j(int i)163 static int test_j(int i)
164 {
165     const j_data *data = &jf_data[i];
166     char bio_buf[80];
167 
168     BIO_snprintf(bio_buf, sizeof(bio_buf) - 1, data->format, data->value);
169     if (!TEST_str_eq(bio_buf, data->expected))
170         return 0;
171     return 1;
172 }
173 
174 /* Precision and width. */
175 typedef struct pw_st {
176     int p;
177     const char *w;
178 } pw;
179 
180 static pw pw_params[] = {
181     { 4, "" },
182     { 5, "" },
183     { 4, "12" },
184     { 5, "12" },
185     { 0, "" },
186     { -1, "" },
187     { 4, "08" }
188 };
189 
dofptest(int test,int sub,double val,const char * width,int prec)190 static int dofptest(int test, int sub, double val, const char *width, int prec)
191 {
192     static const char *fspecs[] = {
193         "e", "f", "g", "E", "G"
194     };
195     char format[80], result[80];
196     int ret = 1, i;
197 
198     for (i = 0; i < (int)OSSL_NELEM(fspecs); i++) {
199         const char *fspec = fspecs[i];
200 
201         if (prec >= 0)
202             BIO_snprintf(format, sizeof(format), "%%%s.%d%s", width, prec,
203                 fspec);
204         else
205             BIO_snprintf(format, sizeof(format), "%%%s%s", width, fspec);
206         BIO_snprintf(result, sizeof(result), format, val);
207 
208         if (justprint) {
209             if (i == 0)
210                 printf("    /*  %d.%02d */ { \"%s\"", test, sub, result);
211             else
212                 printf(", \"%s\"", result);
213         } else if (!TEST_str_eq(fpexpected[test][sub][i], result)) {
214             TEST_info("test %d format=|%s| exp=|%s|, ret=|%s|",
215                 test, format, fpexpected[test][sub][i], result);
216             ret = 0;
217         }
218     }
219     if (justprint)
220         printf(" },\n");
221     return ret;
222 }
223 
test_fp(int i)224 static int test_fp(int i)
225 {
226     int t = 0, r;
227     const double frac = 2.0 / 3.0;
228     const pw *pwp = &pw_params[i];
229 
230     if (justprint)
231         printf("    {\n");
232     r = TEST_true(dofptest(i, t++, 0.0, pwp->w, pwp->p))
233         && TEST_true(dofptest(i, t++, 0.67, pwp->w, pwp->p))
234         && TEST_true(dofptest(i, t++, frac, pwp->w, pwp->p))
235         && TEST_true(dofptest(i, t++, frac / 1000, pwp->w, pwp->p))
236         && TEST_true(dofptest(i, t++, frac / 10000, pwp->w, pwp->p))
237         && TEST_true(dofptest(i, t++, 6.0 + frac, pwp->w, pwp->p))
238         && TEST_true(dofptest(i, t++, 66.0 + frac, pwp->w, pwp->p))
239         && TEST_true(dofptest(i, t++, 666.0 + frac, pwp->w, pwp->p))
240         && TEST_true(dofptest(i, t++, 6666.0 + frac, pwp->w, pwp->p))
241         && TEST_true(dofptest(i, t++, 66666.0 + frac, pwp->w, pwp->p))
242         && TEST_true(dofptest(i, t++, -66666.0 - frac, pwp->w, pwp->p));
243     if (justprint)
244         printf("    },\n");
245     return r;
246 }
247 
test_big(void)248 static int test_big(void)
249 {
250     char buf[80];
251 
252     /* Test excessively big number. Should fail */
253     if (!TEST_int_eq(BIO_snprintf(buf, sizeof(buf),
254                          "%f\n", 2 * (double)ULONG_MAX),
255             -1))
256         return 0;
257 
258     return 1;
259 }
260 
261 typedef enum OPTION_choice {
262     OPT_ERR = -1,
263     OPT_EOF = 0,
264     OPT_PRINT,
265     OPT_TEST_ENUM
266 } OPTION_CHOICE;
267 
test_get_options(void)268 const OPTIONS *test_get_options(void)
269 {
270     static const OPTIONS options[] = {
271         OPT_TEST_OPTIONS_DEFAULT_USAGE,
272         { "expected", OPT_PRINT, '-', "Output values" },
273         { NULL }
274     };
275     return options;
276 }
277 
setup_tests(void)278 int setup_tests(void)
279 {
280     OPTION_CHOICE o;
281 
282     while ((o = opt_next()) != OPT_EOF) {
283         switch (o) {
284         case OPT_PRINT:
285             justprint = 1;
286             break;
287         case OPT_TEST_CASES:
288             break;
289         default:
290             return 0;
291         }
292     }
293 
294     ADD_TEST(test_big);
295     ADD_ALL_TESTS(test_fp, OSSL_NELEM(pw_params));
296     ADD_ALL_TESTS(test_zu, OSSL_NELEM(zu_data));
297     ADD_ALL_TESTS(test_j, OSSL_NELEM(jf_data));
298     return 1;
299 }
300 
301 /*
302  * Replace testutil output routines.  We do this to eliminate possible sources
303  * of BIO error
304  */
305 BIO *bio_out = NULL;
306 BIO *bio_err = NULL;
307 
308 static int tap_level = 0;
309 
test_open_streams(void)310 void test_open_streams(void)
311 {
312 }
313 
test_adjust_streams_tap_level(int level)314 void test_adjust_streams_tap_level(int level)
315 {
316     tap_level = level;
317 }
318 
test_close_streams(void)319 void test_close_streams(void)
320 {
321 }
322 
323 /*
324  * This works out as long as caller doesn't use any "fancy" formats.
325  * But we are caller's caller, and test_str_eq is the only one called,
326  * and it uses only "%s", which is not "fancy"...
327  */
test_vprintf_stdout(const char * fmt,va_list ap)328 int test_vprintf_stdout(const char *fmt, va_list ap)
329 {
330     return fprintf(stdout, "%*s# ", tap_level, "") + vfprintf(stdout, fmt, ap);
331 }
332 
test_vprintf_stderr(const char * fmt,va_list ap)333 int test_vprintf_stderr(const char *fmt, va_list ap)
334 {
335     return fprintf(stderr, "%*s# ", tap_level, "") + vfprintf(stderr, fmt, ap);
336 }
337 
test_flush_stdout(void)338 int test_flush_stdout(void)
339 {
340     return fflush(stdout);
341 }
342 
test_flush_stderr(void)343 int test_flush_stderr(void)
344 {
345     return fflush(stderr);
346 }
347 
test_vprintf_tapout(const char * fmt,va_list ap)348 int test_vprintf_tapout(const char *fmt, va_list ap)
349 {
350     return fprintf(stdout, "%*s", tap_level, "") + vfprintf(stdout, fmt, ap);
351 }
352 
test_vprintf_taperr(const char * fmt,va_list ap)353 int test_vprintf_taperr(const char *fmt, va_list ap)
354 {
355     return fprintf(stderr, "%*s", tap_level, "") + vfprintf(stderr, fmt, ap);
356 }
357 
test_flush_tapout(void)358 int test_flush_tapout(void)
359 {
360     return fflush(stdout);
361 }
362 
test_flush_taperr(void)363 int test_flush_taperr(void)
364 {
365     return fflush(stderr);
366 }
367