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