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