VirtualBox

source: vbox/trunk/src/libs/openssl-3.4.1/test/bioprinttest.c

Last change on this file was 109052, checked in by vboxsync, 4 weeks ago

openssl-3.4.1: Applied our changes, regenerated files, added missing files and functions. This time with a three way merge. ​bugref:10890

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette