TESTUTIL: Adjust the rest of testutil
[openssl.git] / test / testutil / format_output.c
1 /*
2  * Copyright 2017-2018 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 #include "../testutil.h"
11 #include "output.h"
12 #include "tu_local.h"
13
14 #include <string.h>
15 #include <ctype.h>
16 #include "internal/nelem.h"
17
18 /* The size of memory buffers to display on failure */
19 #define MEM_BUFFER_SIZE     (2000)
20 #define MAX_STRING_WIDTH    (80)
21 #define BN_OUTPUT_SIZE      (8)
22
23 /* Output a diff header */
24 static void test_diff_header(const char *left, const char *right)
25 {
26     test_printf_stderr("--- %s\n", left);
27     test_printf_stderr("+++ %s\n", right);
28 }
29
30 /* Formatted string output routines */
31 static void test_string_null_empty(const char *m, char c)
32 {
33     if (m == NULL)
34         test_printf_stderr("%4s %c NULL\n", "", c);
35     else
36         test_printf_stderr("%4u:%c ''\n", 0u, c);
37 }
38
39 static void test_fail_string_common(const char *prefix, const char *file,
40                                     int line, const char *type,
41                                     const char *left, const char *right,
42                                     const char *op, const char *m1, size_t l1,
43                                     const char *m2, size_t l2)
44 {
45     const size_t width =
46         (MAX_STRING_WIDTH - BIO_get_indent(bio_err) - 12) / 16 * 16;
47     char b1[MAX_STRING_WIDTH + 1], b2[MAX_STRING_WIDTH + 1];
48     char bdiff[MAX_STRING_WIDTH + 1];
49     size_t n1, n2, i;
50     unsigned int cnt = 0, diff;
51
52     test_fail_message_prefix(prefix, file, line, type, left, right, op);
53     if (m1 == NULL)
54         l1 = 0;
55     if (m2 == NULL)
56         l2 = 0;
57     if (l1 == 0 && l2 == 0) {
58         if ((m1 == NULL) == (m2 == NULL)) {
59             test_string_null_empty(m1, ' ');
60         } else {
61             test_diff_header(left, right);
62             test_string_null_empty(m1, '-');
63             test_string_null_empty(m2, '+');
64         }
65         goto fin;
66     }
67
68     if (l1 != l2 || strcmp(m1, m2) != 0)
69         test_diff_header(left, right);
70
71     while (l1 > 0 || l2 > 0) {
72         n1 = n2 = 0;
73         if (l1 > 0) {
74             b1[n1 = l1 > width ? width : l1] = 0;
75             for (i = 0; i < n1; i++)
76                 b1[i] = isprint((unsigned char)m1[i]) ? m1[i] : '.';
77         }
78         if (l2 > 0) {
79             b2[n2 = l2 > width ? width : l2] = 0;
80             for (i = 0; i < n2; i++)
81                 b2[i] = isprint((unsigned char)m2[i]) ? m2[i] : '.';
82         }
83         diff = 0;
84         i = 0;
85         if (n1 > 0 && n2 > 0) {
86             const size_t j = n1 < n2 ? n1 : n2;
87
88             for (; i < j; i++)
89                 if (m1[i] == m2[i]) {
90                     bdiff[i] = ' ';
91                 } else {
92                     bdiff[i] = '^';
93                     diff = 1;
94                 }
95             bdiff[i] = '\0';
96         }
97         if (n1 == n2 && !diff) {
98             test_printf_stderr("%4u:  '%s'\n", cnt, n2 > n1 ? b2 : b1);
99         } else {
100             if (cnt == 0 && (m1 == NULL || *m1 == '\0'))
101                 test_string_null_empty(m1, '-');
102             else if (n1 > 0)
103                 test_printf_stderr("%4u:- '%s'\n", cnt, b1);
104             if (cnt == 0 && (m2 == NULL || *m2 == '\0'))
105                test_string_null_empty(m2, '+');
106             else if (n2 > 0)
107                 test_printf_stderr("%4u:+ '%s'\n", cnt, b2);
108             if (diff && i > 0)
109                 test_printf_stderr("%4s    %s\n", "", bdiff);
110         }
111         m1 += n1;
112         m2 += n2;
113         l1 -= n1;
114         l2 -= n2;
115         cnt += width;
116     }
117 fin:
118     test_flush_stderr();
119 }
120
121 /*
122  * Wrapper routines so that the underlying code can be shared.
123  * The first is the call from inside the test utilities when a conditional
124  * fails.  The second is the user's call to dump a string.
125  */
126 void test_fail_string_message(const char *prefix, const char *file,
127                               int line, const char *type,
128                               const char *left, const char *right,
129                               const char *op, const char *m1, size_t l1,
130                               const char *m2, size_t l2)
131 {
132     test_fail_string_common(prefix, file, line, type, left, right, op,
133                             m1, l1, m2, l2);
134     test_printf_stderr("\n");
135 }
136
137 void test_output_string(const char *name, const char *m, size_t l)
138 {
139     test_fail_string_common("string", NULL, 0, NULL, NULL, NULL, name,
140                             m, l, m, l);
141 }
142
143 /* BIGNUM formatted output routines */
144
145 /*
146  * A basic memory byte to hex digit converter with allowance for spacing
147  * every so often.
148  */
149 static void hex_convert_memory(const unsigned char *m, size_t n, char *b,
150                                size_t width)
151 {
152     size_t i;
153
154     for (i = 0; i < n; i++) {
155         const unsigned char c = *m++;
156
157         *b++ = "0123456789abcdef"[c >> 4];
158         *b++ = "0123456789abcdef"[c & 15];
159         if (i % width == width - 1 && i != n - 1)
160             *b++ = ' ';
161     }
162     *b = '\0';
163 }
164
165 /*
166  * Constants to define the number of bytes to display per line and the number
167  * of characters these take.
168  */
169 static const int bn_bytes = (MAX_STRING_WIDTH - 9) / (BN_OUTPUT_SIZE * 2 + 1)
170                             * BN_OUTPUT_SIZE;
171 static const int bn_chars = (MAX_STRING_WIDTH - 9) / (BN_OUTPUT_SIZE * 2 + 1)
172                             * (BN_OUTPUT_SIZE * 2 + 1) - 1;
173
174 /*
175  * Output the header line for the bignum
176  */
177 static void test_bignum_header_line(void)
178 {
179     test_printf_stderr(" %*s\n", bn_chars + 6, "bit position");
180 }
181
182 static const char *test_bignum_zero_null(const BIGNUM *bn)
183 {
184     if (bn != NULL)
185         return BN_is_negative(bn) ? "-0" : "0";
186     return "NULL";
187 }
188
189 /*
190  * Print a bignum zero taking care to include the correct sign.
191  * This routine correctly deals with a NULL bignum pointer as input.
192  */
193 static void test_bignum_zero_print(const BIGNUM *bn, char sep)
194 {
195     const char *v = test_bignum_zero_null(bn);
196     const char *suf = bn != NULL ? ":    0" : "";
197
198     test_printf_stderr("%c%*s%s\n", sep, bn_chars, v, suf);
199 }
200
201 /*
202  * Convert a section of memory from inside a bignum into a displayable
203  * string with appropriate visual aid spaces inserted.
204  */
205 static int convert_bn_memory(const unsigned char *in, size_t bytes,
206                              char *out, int *lz, const BIGNUM *bn)
207 {
208     int n = bytes * 2, i;
209     char *p = out, *q = NULL;
210     const char *r;
211
212     if (bn != NULL && !BN_is_zero(bn)) {
213         hex_convert_memory(in, bytes, out, BN_OUTPUT_SIZE);
214         if (*lz) {
215             for (; *p == '0' || *p == ' '; p++)
216                 if (*p == '0') {
217                     q = p;
218                     *p = ' ';
219                     n--;
220                 }
221             if (*p == '\0') {
222                 /*
223                  * in[bytes] is defined because we're converting a non-zero
224                  * number and we've not seen a non-zero yet.
225                  */
226                 if ((in[bytes] & 0xf0) != 0 && BN_is_negative(bn)) {
227                     *lz = 0;
228                     *q = '-';
229                     n++;
230                 }
231             } else {
232                 *lz = 0;
233                 if (BN_is_negative(bn)) {
234                     /*
235                      * This is valid because we always convert more digits than
236                      * the number holds.
237                      */
238                     *q = '-';
239                     n++;
240                 }
241             }
242         }
243        return n;
244     }
245
246     for (i = 0; i < n; i++) {
247         *p++ = ' ';
248         if (i % (2 * BN_OUTPUT_SIZE) == 2 * BN_OUTPUT_SIZE - 1 && i != n - 1)
249             *p++ = ' ';
250     }
251     *p = '\0';
252     if (bn == NULL)
253         r = "NULL";
254     else
255         r = BN_is_negative(bn) ? "-0" : "0";
256     strcpy(p - strlen(r), r);
257     return 0;
258 }
259
260 /*
261  * Common code to display either one or two bignums, including the diff
262  * pointers for changes (only when there are two).
263  */
264 static void test_fail_bignum_common(const char *prefix, const char *file,
265                                     int line, const char *type,
266                                     const char *left, const char *right,
267                                     const char *op,
268                                     const BIGNUM *bn1, const BIGNUM *bn2)
269 {
270     const size_t bytes = bn_bytes;
271     char b1[MAX_STRING_WIDTH + 1], b2[MAX_STRING_WIDTH + 1];
272     char *p, bdiff[MAX_STRING_WIDTH + 1];
273     size_t l1, l2, n1, n2, i, len;
274     unsigned int cnt, diff, real_diff;
275     unsigned char *m1 = NULL, *m2 = NULL;
276     int lz1 = 1, lz2 = 1;
277     unsigned char buffer[MEM_BUFFER_SIZE * 2], *bufp = buffer;
278
279     test_fail_message_prefix(prefix, file, line, type, left, right, op);
280     l1 = bn1 == NULL ? 0 : (BN_num_bytes(bn1) + (BN_is_negative(bn1) ? 1 : 0));
281     l2 = bn2 == NULL ? 0 : (BN_num_bytes(bn2) + (BN_is_negative(bn2) ? 1 : 0));
282     if (l1 == 0 && l2 == 0) {
283         if ((bn1 == NULL) == (bn2 == NULL)) {
284             test_bignum_header_line();
285             test_bignum_zero_print(bn1, ' ');
286         } else {
287             test_diff_header(left, right);
288             test_bignum_header_line();
289             test_bignum_zero_print(bn1, '-');
290             test_bignum_zero_print(bn2, '+');
291         }
292         goto fin;
293     }
294
295     if (l1 != l2 || bn1 == NULL || bn2 == NULL || BN_cmp(bn1, bn2) != 0)
296         test_diff_header(left, right);
297     test_bignum_header_line();
298
299     len = ((l1 > l2 ? l1 : l2) + bytes - 1) / bytes * bytes;
300
301     if (len > MEM_BUFFER_SIZE && (bufp = OPENSSL_malloc(len * 2)) == NULL) {
302         bufp = buffer;
303         len = MEM_BUFFER_SIZE;
304         test_printf_stderr("WARNING: these BIGNUMs have been truncated\n");
305     }
306
307     if (bn1 != NULL) {
308         m1 = bufp;
309         BN_bn2binpad(bn1, m1, len);
310     }
311     if (bn2 != NULL) {
312         m2 = bufp + len;
313         BN_bn2binpad(bn2, m2, len);
314     }
315
316     while (len > 0) {
317         cnt = 8 * (len - bytes);
318         n1 = convert_bn_memory(m1, bytes, b1, &lz1, bn1);
319         n2 = convert_bn_memory(m2, bytes, b2, &lz2, bn2);
320
321         diff = real_diff = 0;
322         i = 0;
323         p = bdiff;
324         for (i=0; b1[i] != '\0'; i++)
325             if (b1[i] == b2[i] || b1[i] == ' ' || b2[i] == ' ') {
326                 *p++ = ' ';
327                 diff |= b1[i] != b2[i];
328             } else {
329                 *p++ = '^';
330                 real_diff = diff = 1;
331             }
332         *p++ = '\0';
333         if (!diff) {
334             test_printf_stderr(" %s:% 5d\n", n2 > n1 ? b2 : b1, cnt);
335         } else {
336             if (cnt == 0 && bn1 == NULL)
337                 test_printf_stderr("-%s\n", b1);
338             else if (cnt == 0 || n1 > 0)
339                 test_printf_stderr("-%s:% 5d\n", b1, cnt);
340             if (cnt == 0 && bn2 == NULL)
341                 test_printf_stderr("+%s\n", b2);
342             else if (cnt == 0 || n2 > 0)
343                 test_printf_stderr("+%s:% 5d\n", b2, cnt);
344             if (real_diff && (cnt == 0 || (n1 > 0 && n2 > 0))
345                     && bn1 != NULL && bn2 != NULL)
346                 test_printf_stderr(" %s\n", bdiff);
347         }
348         if (m1 != NULL)
349             m1 += bytes;
350         if (m2 != NULL)
351             m2 += bytes;
352         len -= bytes;
353     }
354 fin:
355     test_flush_stderr();
356     if (bufp != buffer)
357         OPENSSL_free(bufp);
358 }
359
360 /*
361  * Wrapper routines so that the underlying code can be shared.
362  * The first two are calls from inside the test utilities when a conditional
363  * fails.  The third is the user's call to dump a bignum.
364  */
365 void test_fail_bignum_message(const char *prefix, const char *file,
366                               int line, const char *type,
367                               const char *left, const char *right,
368                               const char *op,
369                               const BIGNUM *bn1, const BIGNUM *bn2)
370 {
371     test_fail_bignum_common(prefix, file, line, type, left, right, op, bn1, bn2);
372     test_printf_stderr("\n");
373 }
374
375 void test_fail_bignum_mono_message(const char *prefix, const char *file,
376                                    int line, const char *type,
377                                    const char *left, const char *right,
378                                    const char *op, const BIGNUM *bn)
379 {
380     test_fail_bignum_common(prefix, file, line, type, left, right, op, bn, bn);
381     test_printf_stderr("\n");
382 }
383
384 void test_output_bignum(const char *name, const BIGNUM *bn)
385 {
386     if (bn == NULL || BN_is_zero(bn)) {
387         test_printf_stderr("bignum: '%s' = %s\n", name,
388                            test_bignum_zero_null(bn));
389     } else if (BN_num_bytes(bn) <= BN_OUTPUT_SIZE) {
390         unsigned char buf[BN_OUTPUT_SIZE];
391         char out[2 * sizeof(buf) + 1];
392         char *p = out;
393         int n = BN_bn2bin(bn, buf);
394
395         hex_convert_memory(buf, n, p, BN_OUTPUT_SIZE);
396         while (*p == '0' && *++p != '\0')
397             ;
398         test_printf_stderr("bignum: '%s' = %s0x%s\n", name,
399                            BN_is_negative(bn) ? "-" : "", p);
400     } else {
401         test_fail_bignum_common("bignum", NULL, 0, NULL, NULL, NULL, name,
402                                 bn, bn);
403     }
404 }
405
406 /* Memory output routines */
407
408 /*
409  * Handle zero length blocks of memory or NULL pointers to memory
410  */
411 static void test_memory_null_empty(const unsigned char *m, char c)
412 {
413     if (m == NULL)
414         test_printf_stderr("%4s %c%s\n", "", c, "NULL");
415     else
416         test_printf_stderr("%04x %c%s\n", 0u, c, "empty");
417 }
418
419 /*
420  * Common code to display one or two blocks of memory.
421  */
422 static void test_fail_memory_common(const char *prefix, const char *file,
423                                     int line, const char *type,
424                                     const char *left, const char *right,
425                                     const char *op,
426                                     const unsigned char *m1, size_t l1,
427                                     const unsigned char *m2, size_t l2)
428 {
429     const size_t bytes = (MAX_STRING_WIDTH - 9) / 17 * 8;
430     char b1[MAX_STRING_WIDTH + 1], b2[MAX_STRING_WIDTH + 1];
431     char *p, bdiff[MAX_STRING_WIDTH + 1];
432     size_t n1, n2, i;
433     unsigned int cnt = 0, diff;
434
435     test_fail_message_prefix(prefix, file, line, type, left, right, op);
436     if (m1 == NULL)
437         l1 = 0;
438     if (m2 == NULL)
439         l2 = 0;
440     if (l1 == 0 && l2 == 0) {
441         if ((m1 == NULL) == (m2 == NULL)) {
442             test_memory_null_empty(m1, ' ');
443         } else {
444             test_diff_header(left, right);
445             test_memory_null_empty(m1, '-');
446             test_memory_null_empty(m2, '+');
447         }
448         goto fin;
449     }
450
451     if (l1 != l2 || (m1 != m2 && memcmp(m1, m2, l1) != 0))
452         test_diff_header(left, right);
453
454     while (l1 > 0 || l2 > 0) {
455         n1 = n2 = 0;
456         if (l1 > 0) {
457             n1 = l1 > bytes ? bytes : l1;
458             hex_convert_memory(m1, n1, b1, 8);
459         }
460         if (l2 > 0) {
461             n2 = l2 > bytes ? bytes : l2;
462             hex_convert_memory(m2, n2, b2, 8);
463         }
464
465         diff = 0;
466         i = 0;
467         p = bdiff;
468         if (n1 > 0 && n2 > 0) {
469             const size_t j = n1 < n2 ? n1 : n2;
470
471             for (; i < j; i++) {
472                 if (m1[i] == m2[i]) {
473                     *p++ = ' ';
474                     *p++ = ' ';
475                 } else {
476                     *p++ = '^';
477                     *p++ = '^';
478                     diff = 1;
479                 }
480                 if (i % 8 == 7 && i != j - 1)
481                     *p++ = ' ';
482             }
483             *p++ = '\0';
484         }
485
486         if (n1 == n2 && !diff) {
487             test_printf_stderr("%04x: %s\n", cnt, b1);
488         } else {
489             if (cnt == 0 && (m1 == NULL || l1 == 0))
490                 test_memory_null_empty(m1, '-');
491             else if (n1 > 0)
492                 test_printf_stderr("%04x:-%s\n", cnt, b1);
493             if (cnt == 0 && (m2 == NULL || l2 == 0))
494                 test_memory_null_empty(m2, '+');
495             else if (n2 > 0)
496                 test_printf_stderr("%04x:+%s\n", cnt, b2);
497             if (diff && i > 0)
498                 test_printf_stderr("%4s  %s\n", "", bdiff);
499         }
500         m1 += n1;
501         m2 += n2;
502         l1 -= n1;
503         l2 -= n2;
504         cnt += bytes;
505     }
506 fin:
507     test_flush_stderr();
508 }
509
510 /*
511  * Wrapper routines so that the underlying code can be shared.
512  * The first is the call from inside the test utilities when a conditional
513  * fails.  The second is the user's call to dump memory.
514  */
515 void test_fail_memory_message(const char *prefix, const char *file,
516                               int line, const char *type,
517                               const char *left, const char *right,
518                               const char *op,
519                               const unsigned char *m1, size_t l1,
520                               const unsigned char *m2, size_t l2)
521 {
522     test_fail_memory_common(prefix, file, line, type, left, right, op,
523                             m1, l1, m2, l2);
524     test_printf_stderr("\n");
525 }
526
527 void test_output_memory(const char *name, const unsigned char *m, size_t l)
528 {
529     test_fail_memory_common("memory", NULL, 0, NULL, NULL, NULL, name,
530                             m, l, m, l);
531 }