]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/bioprinttest.c
make update
[thirdparty/openssl.git] / test / bioprinttest.c
CommitLineData
f8f686ec
MC
1/*
2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (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
f044cd05
RL
10#define TESTUTIL_NO_size_t_COMPARISON
11
f8f686ec
MC
12#include <stdio.h>
13#include <string.h>
f044cd05
RL
14#include <openssl/e_os2.h>
15#ifdef OPENSSL_SYS_WINDOWS
16# include <winsock.h>
17#else
18# include OPENSSL_UNISTD
19#endif
f8f686ec 20#include <openssl/bio.h>
6e6aa5a9 21#include "internal/numbers.h"
3304d578 22#include "testutil.h"
6e6aa5a9 23
3304d578 24#define nelem(x) (int)(sizeof(x) / sizeof((x)[0]))
f8f686ec
MC
25
26static int justprint = 0;
27
28static char *fpexpected[][5] = {
29 /* 0 */ { "0.0000e+00", "0.0000", "0", "0.0000E+00", "0" },
30 /* 1 */ { "6.7000e-01", "0.6700", "0.67", "6.7000E-01", "0.67" },
31 /* 2 */ { "6.6667e-01", "0.6667", "0.6667", "6.6667E-01", "0.6667" },
32 /* 3 */ { "6.6667e-04", "0.0007", "0.0006667", "6.6667E-04", "0.0006667" },
33 /* 4 */ { "6.6667e-05", "0.0001", "6.667e-05", "6.6667E-05", "6.667E-05" },
34 /* 5 */ { "6.6667e+00", "6.6667", "6.667", "6.6667E+00", "6.667" },
35 /* 6 */ { "6.6667e+01", "66.6667", "66.67", "6.6667E+01", "66.67" },
36 /* 7 */ { "6.6667e+02", "666.6667", "666.7", "6.6667E+02", "666.7" },
37 /* 8 */ { "6.6667e+03", "6666.6667", "6667", "6.6667E+03", "6667" },
38 /* 9 */ { "6.6667e+04", "66666.6667", "6.667e+04", "6.6667E+04", "6.667E+04" },
39 /* 10 */ { "0.00000e+00", "0.00000", "0", "0.00000E+00", "0" },
40 /* 11 */ { "6.70000e-01", "0.67000", "0.67", "6.70000E-01", "0.67" },
41 /* 12 */ { "6.66667e-01", "0.66667", "0.66667", "6.66667E-01", "0.66667" },
42 /* 13 */ { "6.66667e-04", "0.00067", "0.00066667", "6.66667E-04", "0.00066667" },
43 /* 14 */ { "6.66667e-05", "0.00007", "6.6667e-05", "6.66667E-05", "6.6667E-05" },
44 /* 15 */ { "6.66667e+00", "6.66667", "6.6667", "6.66667E+00", "6.6667" },
45 /* 16 */ { "6.66667e+01", "66.66667", "66.667", "6.66667E+01", "66.667" },
46 /* 17 */ { "6.66667e+02", "666.66667", "666.67", "6.66667E+02", "666.67" },
47 /* 18 */ { "6.66667e+03", "6666.66667", "6666.7", "6.66667E+03", "6666.7" },
48 /* 19 */ { "6.66667e+04", "66666.66667", "66667", "6.66667E+04", "66667" },
49 /* 20 */ { " 0.0000e+00", " 0.0000", " 0", " 0.0000E+00", " 0" },
50 /* 21 */ { " 6.7000e-01", " 0.6700", " 0.67", " 6.7000E-01", " 0.67" },
51 /* 22 */ { " 6.6667e-01", " 0.6667", " 0.6667", " 6.6667E-01", " 0.6667" },
52 /* 23 */ { " 6.6667e-04", " 0.0007", " 0.0006667", " 6.6667E-04", " 0.0006667" },
53 /* 24 */ { " 6.6667e-05", " 0.0001", " 6.667e-05", " 6.6667E-05", " 6.667E-05" },
54 /* 25 */ { " 6.6667e+00", " 6.6667", " 6.667", " 6.6667E+00", " 6.667" },
55 /* 26 */ { " 6.6667e+01", " 66.6667", " 66.67", " 6.6667E+01", " 66.67" },
56 /* 27 */ { " 6.6667e+02", " 666.6667", " 666.7", " 6.6667E+02", " 666.7" },
57 /* 28 */ { " 6.6667e+03", " 6666.6667", " 6667", " 6.6667E+03", " 6667" },
58 /* 29 */ { " 6.6667e+04", " 66666.6667", " 6.667e+04", " 6.6667E+04", " 6.667E+04" },
59 /* 30 */ { " 0.00000e+00", " 0.00000", " 0", " 0.00000E+00", " 0" },
60 /* 31 */ { " 6.70000e-01", " 0.67000", " 0.67", " 6.70000E-01", " 0.67" },
61 /* 32 */ { " 6.66667e-01", " 0.66667", " 0.66667", " 6.66667E-01", " 0.66667" },
62 /* 33 */ { " 6.66667e-04", " 0.00067", " 0.00066667", " 6.66667E-04", " 0.00066667" },
63 /* 34 */ { " 6.66667e-05", " 0.00007", " 6.6667e-05", " 6.66667E-05", " 6.6667E-05" },
64 /* 35 */ { " 6.66667e+00", " 6.66667", " 6.6667", " 6.66667E+00", " 6.6667" },
65 /* 36 */ { " 6.66667e+01", " 66.66667", " 66.667", " 6.66667E+01", " 66.667" },
66 /* 37 */ { " 6.66667e+02", " 666.66667", " 666.67", " 6.66667E+02", " 666.67" },
67 /* 38 */ { " 6.66667e+03", " 6666.66667", " 6666.7", " 6.66667E+03", " 6666.7" },
68 /* 39 */ { " 6.66667e+04", " 66666.66667", " 66667", " 6.66667E+04", " 66667" },
69 /* 40 */ { "0e+00", "0", "0", "0E+00", "0" },
70 /* 41 */ { "7e-01", "1", "0.7", "7E-01", "0.7" },
71 /* 42 */ { "7e-01", "1", "0.7", "7E-01", "0.7" },
72 /* 43 */ { "7e-04", "0", "0.0007", "7E-04", "0.0007" },
73 /* 44 */ { "7e-05", "0", "7e-05", "7E-05", "7E-05" },
74 /* 45 */ { "7e+00", "7", "7", "7E+00", "7" },
75 /* 46 */ { "7e+01", "67", "7e+01", "7E+01", "7E+01" },
76 /* 47 */ { "7e+02", "667", "7e+02", "7E+02", "7E+02" },
77 /* 48 */ { "7e+03", "6667", "7e+03", "7E+03", "7E+03" },
78 /* 49 */ { "7e+04", "66667", "7e+04", "7E+04", "7E+04" },
79 /* 50 */ { "0.000000e+00", "0.000000", "0", "0.000000E+00", "0" },
80 /* 51 */ { "6.700000e-01", "0.670000", "0.67", "6.700000E-01", "0.67" },
81 /* 52 */ { "6.666667e-01", "0.666667", "0.666667", "6.666667E-01", "0.666667" },
82 /* 53 */ { "6.666667e-04", "0.000667", "0.000666667", "6.666667E-04", "0.000666667" },
83 /* 54 */ { "6.666667e-05", "0.000067", "6.66667e-05", "6.666667E-05", "6.66667E-05" },
84 /* 55 */ { "6.666667e+00", "6.666667", "6.66667", "6.666667E+00", "6.66667" },
85 /* 56 */ { "6.666667e+01", "66.666667", "66.6667", "6.666667E+01", "66.6667" },
86 /* 57 */ { "6.666667e+02", "666.666667", "666.667", "6.666667E+02", "666.667" },
87 /* 58 */ { "6.666667e+03", "6666.666667", "6666.67", "6.666667E+03", "6666.67" },
88 /* 59 */ { "6.666667e+04", "66666.666667", "66666.7", "6.666667E+04", "66666.7" },
89 /* 60 */ { "0.0000e+00", "000.0000", "00000000", "0.0000E+00", "00000000" },
90 /* 61 */ { "6.7000e-01", "000.6700", "00000.67", "6.7000E-01", "00000.67" },
91 /* 62 */ { "6.6667e-01", "000.6667", "000.6667", "6.6667E-01", "000.6667" },
92 /* 63 */ { "6.6667e-04", "000.0007", "0.0006667", "6.6667E-04", "0.0006667" },
93 /* 64 */ { "6.6667e-05", "000.0001", "6.667e-05", "6.6667E-05", "6.667E-05" },
94 /* 65 */ { "6.6667e+00", "006.6667", "0006.667", "6.6667E+00", "0006.667" },
95 /* 66 */ { "6.6667e+01", "066.6667", "00066.67", "6.6667E+01", "00066.67" },
96 /* 67 */ { "6.6667e+02", "666.6667", "000666.7", "6.6667E+02", "000666.7" },
97 /* 68 */ { "6.6667e+03", "6666.6667", "00006667", "6.6667E+03", "00006667" },
98 /* 69 */ { "6.6667e+04", "66666.6667", "6.667e+04", "6.6667E+04", "6.667E+04" },
99};
100
3304d578 101typedef struct z_data_st {
6e6aa5a9
RL
102 size_t value;
103 const char *format;
104 const char *expected;
3304d578
RS
105} z_data;
106
107static z_data zu_data[] = {
6e6aa5a9
RL
108 { SIZE_MAX, "%zu", (sizeof(size_t) == 4 ? "4294967295"
109 : sizeof(size_t) == 8 ? "18446744073709551615"
110 : "") },
111 /*
112 * in 2-complement, the unsigned number divided by two plus one becomes the
113 * smallest possible negative signed number of the corresponding type
114 */
115 { SIZE_MAX / 2 + 1, "%zi", (sizeof(size_t) == 4 ? "-2147483648"
116 : sizeof(size_t) == 8 ? "-9223372036854775808"
117 : "") },
118 { 0, "%zu", "0" },
119 { 0, "%zi", "0" },
120};
121
3304d578 122static int test_zu(int i)
6e6aa5a9
RL
123{
124 char bio_buf[80];
3304d578 125 const z_data *data = &zu_data[i];
6e6aa5a9
RL
126
127 BIO_snprintf(bio_buf, sizeof(bio_buf) - 1, data->format, data->value);
3304d578
RS
128 if (!TEST_str_eq(bio_buf, data->expected))
129 return 0;
130 return 1;
6e6aa5a9
RL
131}
132
3304d578 133typedef struct j_data_st {
74d9519a
AP
134 uint64_t value;
135 const char *format;
136 const char *expected;
3304d578
RS
137} j_data;
138
139static j_data jf_data[] = {
74d9519a
AP
140 { 0xffffffffffffffffU, "%ju", "18446744073709551615" },
141 { 0xffffffffffffffffU, "%jx", "ffffffffffffffff" },
142 { 0x8000000000000000U, "%ju", "9223372036854775808" },
143 /*
144 * These tests imply two's-complement, but it's the only binary
145 * representation we support, see test/sanitytest.c...
146 */
147 { 0x8000000000000000U, "%ji", "-9223372036854775808" },
148};
149
3304d578 150static int test_j(int i)
74d9519a 151{
3304d578 152 const j_data *data = &jf_data[i];
74d9519a
AP
153 char bio_buf[80];
154
155 BIO_snprintf(bio_buf, sizeof(bio_buf) - 1, data->format, data->value);
3304d578
RS
156 if (!TEST_str_eq(bio_buf, data->expected))
157 return 0;
158 return 1;
74d9519a
AP
159}
160
3304d578
RS
161
162/* Precision and width. */
163typedef struct pw_st {
164 int p;
165 const char *w;
166} pw;
167
168static pw pw_params[] = {
169 { 4, "" },
170 { 5, "" },
171 { 4, "12" },
172 { 5, "12" },
173 { 0, "" },
174 { -1, "" },
175 { 4, "08" }
176};
177
178static int dofptest(int test, double val, const char *width, int prec)
f8f686ec 179{
3304d578
RS
180 static const char *fspecs[] = {
181 "e", "f", "g", "E", "G"
182 };
183 char format[80], result[80];
184 int ret = 1, i;
f8f686ec 185
3304d578
RS
186 for (i = 0; i < nelem(fspecs); i++) {
187 const char *fspec = fspecs[i];
f8f686ec 188
3304d578
RS
189 if (prec >= 0)
190 BIO_snprintf(format, sizeof(format), "%%%s.%d%s", width, prec,
191 fspec);
192 else
193 BIO_snprintf(format, sizeof(format), "%%%s%s", width, fspec);
194 BIO_snprintf(result, sizeof(result), format, val);
f8f686ec 195
3304d578
RS
196 if (justprint) {
197 if (i == 0)
198 printf(" /* %d */ { \"%s\"", test, result);
199 else
200 printf(", \"%s\"", result);
201 } else if (!TEST_str_eq(fpexpected[test][i], result)) {
202 TEST_info("test %d format=|%s| exp=|%s|, ret=|%s|",
203 test, format, fpexpected[test][i], result);
204 ret = 0;
f8f686ec 205 }
f8f686ec 206 }
3304d578
RS
207 if (justprint)
208 printf(" },\n");
209 return ret;
210}
f8f686ec 211
3304d578
RS
212static int test_fp(int i)
213{
214 static int t = 0;
215 const double frac = 2.0 / 3.0;
216 const pw *pwp = &pw_params[i];
217
218 if (!TEST_true(dofptest(t++, 0.0, pwp->w, pwp->p))
219 || !TEST_true(dofptest(t++, 0.67, pwp->w, pwp->p))
220 || !TEST_true(dofptest(t++, frac, pwp->w, pwp->p))
221 || !TEST_true(dofptest(t++, frac / 1000, pwp->w, pwp->p))
222 || !TEST_true(dofptest(t++, frac / 10000, pwp->w, pwp->p))
223 || !TEST_true(dofptest(t++, 6.0 + frac, pwp->w, pwp->p))
224 || !TEST_true(dofptest(t++, 66.0 + frac, pwp->w, pwp->p))
225 || !TEST_true(dofptest(t++, 666.0 + frac, pwp->w, pwp->p))
226 || !TEST_true(dofptest(t++, 6666.0 + frac, pwp->w, pwp->p))
227 || !TEST_true(dofptest(t++, 66666.0 + frac, pwp->w, pwp->p)))
228 return 0;
229 return 1;
230}
f8f686ec 231
3304d578
RS
232static int test_big(void)
233{
234 char buf[80];
6e6aa5a9 235
3304d578
RS
236 /* Test excessively big number. Should fail */
237 if (!TEST_int_eq(BIO_snprintf(buf, sizeof(buf),
238 "%f\n", 2 * (double)ULONG_MAX), -1))
239 return 0;
240 return 1;
241}
6e6aa5a9 242
f8f686ec 243
3304d578
RS
244int test_main(int argc, char **argv)
245{
246 if (argc == 2 && strcmp(argv[1], "-expected") == 0)
247 justprint = 1;
f8f686ec 248
3304d578
RS
249 ADD_TEST(test_big);
250 ADD_ALL_TESTS(test_fp, nelem(pw_params));
251 ADD_ALL_TESTS(test_zu, nelem(zu_data));
252 ADD_ALL_TESTS(test_j, nelem(jf_data));
f8f686ec 253
3304d578
RS
254 return run_tests(argv[0]);
255}
f044cd05
RL
256
257/*
258 * Replace testutil output routines. We do this to eliminate possible sources
259 * of BIO error
260 */
261void test_open_streams(void)
262{
263}
264
265void test_close_streams(void)
266{
267}
268
269int test_puts_stdout(const char *str)
270{
271 return write(1, str, strlen(str));
272}
273
274int test_puts_stderr(const char *str)
275{
276 return write(2, str, strlen(str));
277}
278
279static char vprint_buf[10240];
280
281int test_vprintf_stdout(const char *fmt, va_list ap)
282{
283 size_t len = vsnprintf(vprint_buf, sizeof(vprint_buf), fmt, ap);
284
285 if (len >= sizeof(vprint_buf))
286 return -1;
287 return test_puts_stdout(vprint_buf);
288}
289
290int test_vprintf_stderr(const char *fmt, va_list ap)
291{
292 size_t len = vsnprintf(vprint_buf, sizeof(vprint_buf), fmt, ap);
293
294 if (len >= sizeof(vprint_buf))
295 return -1;
296 return test_puts_stderr(vprint_buf);
297}
298
299int test_flush_stdout(void)
300{
301 return 0;
302}
303
304int test_flush_stderr(void)
305{
306 return 0;
307}