1/*
2 * Copyright 2016-2020 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/numbers.h"
16#include "testutil.h"
17#include "testutil/output.h"
18
19#define nelem(x) (int)(sizeof(x) / sizeof((x)[0]))
20
21static int justprint = 0;
22
23static 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    {
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" },
47    },
48    {
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    },
60    {
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" },
71    },
72    {
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" },
83    },
84    {
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" },
95    },
96    {
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" },
107    },
108};
109
110typedef struct z_data_st {
111    size_t value;
112    const char *format;
113    const char *expected;
114} z_data;
115
116static z_data zu_data[] = {
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
131static int test_zu(int i)
132{
133    char bio_buf[80];
134    const z_data *data = &zu_data[i];
135
136    BIO_snprintf(bio_buf, sizeof(bio_buf) - 1, data->format, data->value);
137    if (!TEST_str_eq(bio_buf, data->expected))
138        return 0;
139    return 1;
140}
141
142typedef struct j_data_st {
143    uint64_t value;
144    const char *format;
145    const char *expected;
146} j_data;
147
148static j_data jf_data[] = {
149    { 0xffffffffffffffffULL, "%ju", "18446744073709551615" },
150    { 0xffffffffffffffffULL, "%jx", "ffffffffffffffff" },
151    { 0x8000000000000000ULL, "%ju", "9223372036854775808" },
152    /*
153     * These tests imply two's-complement, but it's the only binary
154     * representation we support, see test/sanitytest.c...
155     */
156    { 0x8000000000000000ULL, "%ji", "-9223372036854775808" },
157};
158
159static int test_j(int i)
160{
161    const j_data *data = &jf_data[i];
162    char bio_buf[80];
163
164    BIO_snprintf(bio_buf, sizeof(bio_buf) - 1, data->format, data->value);
165    if (!TEST_str_eq(bio_buf, data->expected))
166        return 0;
167    return 1;
168}
169
170
171/* Precision and width. */
172typedef struct pw_st {
173    int p;
174    const char *w;
175} pw;
176
177static pw pw_params[] = {
178    { 4, "" },
179    { 5, "" },
180    { 4, "12" },
181    { 5, "12" },
182    { 0, "" },
183    { -1, "" },
184    { 4, "08" }
185};
186
187static int dofptest(int test, int sub, double val, const char *width, int prec)
188{
189    static const char *fspecs[] = {
190        "e", "f", "g", "E", "G"
191    };
192    char format[80], result[80];
193    int ret = 1, i;
194
195    for (i = 0; i < nelem(fspecs); i++) {
196        const char *fspec = fspecs[i];
197
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);
204
205        if (justprint) {
206            if (i == 0)
207                printf("    /*  %d%d */ { \"%s\"", test, sub, result);
208            else
209                printf(", \"%s\"", result);
210        } else if (!TEST_str_eq(fpexpected[test][sub][i], result)) {
211            TEST_info("test %d format=|%s| exp=|%s|, ret=|%s|",
212                    test, format, fpexpected[test][sub][i], result);
213            ret = 0;
214        }
215    }
216    if (justprint)
217        printf(" },\n");
218    return ret;
219}
220
221static int test_fp(int i)
222{
223    int t = 0, r;
224    const double frac = 2.0 / 3.0;
225    const pw *pwp = &pw_params[i];
226
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;
242}
243
244static int test_big(void)
245{
246    char buf[80];
247
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
253    return 1;
254}
255
256typedef enum OPTION_choice {
257    OPT_ERR = -1,
258    OPT_EOF = 0,
259    OPT_PRINT,
260    OPT_TEST_ENUM
261} OPTION_CHOICE;
262
263const OPTIONS *test_get_options(void)
264{
265    static const OPTIONS options[] = {
266        OPT_TEST_OPTIONS_DEFAULT_USAGE,
267        { "expected", OPT_PRINT, '-', "Output values" },
268        { NULL }
269    };
270    return options;
271}
272
273int setup_tests(void)
274{
275    OPTION_CHOICE o;
276
277    while ((o = opt_next()) != OPT_EOF) {
278        switch (o) {
279        case OPT_PRINT:
280            justprint = 1;
281            break;
282        case OPT_TEST_CASES:
283            break;
284        default:
285            return 0;
286        }
287    }
288
289    ADD_TEST(test_big);
290    ADD_ALL_TESTS(test_fp, nelem(pw_params));
291    ADD_ALL_TESTS(test_zu, nelem(zu_data));
292    ADD_ALL_TESTS(test_j, nelem(jf_data));
293    return 1;
294}
295
296/*
297 * Replace testutil output routines.  We do this to eliminate possible sources
298 * of BIO error
299 */
300BIO *bio_out = NULL;
301BIO *bio_err = NULL;
302
303static int tap_level = 0;
304
305void test_open_streams(void)
306{
307}
308
309void test_adjust_streams_tap_level(int level)
310{
311    tap_level = level;
312}
313
314void test_close_streams(void)
315{
316}
317
318/*
319 * This works out as long as caller doesn't use any "fancy" formats.
320 * But we are caller's caller, and test_str_eq is the only one called,
321 * and it uses only "%s", which is not "fancy"...
322 */
323int test_vprintf_stdout(const char *fmt, va_list ap)
324{
325    return fprintf(stdout, "%*s# ", tap_level, "") + vfprintf(stdout, fmt, ap);
326}
327
328int test_vprintf_stderr(const char *fmt, va_list ap)
329{
330    return fprintf(stderr, "%*s# ", tap_level, "") + vfprintf(stderr, fmt, ap);
331}
332
333int test_flush_stdout(void)
334{
335    return fflush(stdout);
336}
337
338int test_flush_stderr(void)
339{
340    return fflush(stderr);
341}
342
343int test_vprintf_tapout(const char *fmt, va_list ap)
344{
345    return fprintf(stdout, "%*s", tap_level, "") + vfprintf(stdout, fmt, ap);
346}
347
348int test_vprintf_taperr(const char *fmt, va_list ap)
349{
350    return fprintf(stderr, "%*s", tap_level, "") + vfprintf(stderr, fmt, ap);
351}
352
353int test_flush_tapout(void)
354{
355    return fflush(stdout);
356}
357
358int test_flush_taperr(void)
359{
360    return fflush(stderr);
361}
362
363