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