1 #include "../testutil.h"
7 #include "../../e_os.h"
9 /* The size of memory buffers to display on failure */
10 #define MEM_BUFFER_SIZE (2000)
11 #define MAX_STRING_WIDTH (80)
12 #define BN_OUTPUT_SIZE (8)
14 /* Output a diff header */
15 static void test_diff_header(const char *left, const char *right)
17 test_printf_stderr("%*s# --- %s\n", subtest_level(), "", left);
18 test_printf_stderr("%*s# +++ %s\n", subtest_level(), "", right);
21 /* Formatted string output routines */
22 static void test_string_null_empty(const char *m, char c)
25 test_printf_stderr("%*s# % 4s %c NULL\n", subtest_level(), "", "", c);
27 test_printf_stderr("%*s# % 4u:%c ''\n", subtest_level(), "", 0u, c);
30 static void test_fail_string_common(const char *prefix, const char *file,
31 int line, const char *type,
32 const char *left, const char *right,
33 const char *op, const char *m1, size_t l1,
34 const char *m2, size_t l2)
36 const int indent = subtest_level();
37 const size_t width = (MAX_STRING_WIDTH - indent - 12) / 16 * 16;
38 char b1[MAX_STRING_WIDTH + 1], b2[MAX_STRING_WIDTH + 1];
39 char bdiff[MAX_STRING_WIDTH + 1];
41 unsigned int cnt = 0, diff;
43 test_fail_message_prefix(prefix, file, line, type, left, right, op);
48 if (l1 == 0 && l2 == 0) {
49 if ((m1 == NULL) == (m2 == NULL)) {
50 test_string_null_empty(m1, ' ');
52 test_diff_header(left, right);
53 test_string_null_empty(m1, '-');
54 test_string_null_empty(m2, '+');
59 if (l1 != l2 || strcmp(m1, m2) != 0)
60 test_diff_header(left, right);
62 while (l1 > 0 || l2 > 0) {
65 b1[n1 = l1 > width ? width : l1] = 0;
66 for (i = 0; i < n1; i++)
67 b1[i] = isprint(m1[i]) ? m1[i] : '.';
70 b2[n2 = l2 > width ? width : l2] = 0;
71 for (i = 0; i < n2; i++)
72 b2[i] = isprint(m2[i]) ? m2[i] : '.';
76 if (n1 > 0 && n2 > 0) {
77 const size_t j = n1 < n2 ? n1 : n2;
88 if (n1 == n2 && !diff) {
89 test_printf_stderr("%*s# % 4u: '%s'\n", indent, "", cnt,
92 if (cnt == 0 && (m1 == NULL || *m1 == '\0'))
93 test_string_null_empty(m1, '-');
95 test_printf_stderr("%*s# % 4u:- '%s'\n", indent, "", cnt, b1);
96 if (cnt == 0 && (m2 == NULL || *m2 == '\0'))
97 test_string_null_empty(m2, '+');
99 test_printf_stderr("%*s# % 4u:+ '%s'\n", indent, "", cnt, b2);
101 test_printf_stderr("%*s# % 4s %s\n", indent, "", "", bdiff);
114 * Wrapper routines so that the underlying code can be shared.
115 * The first is the call from inside the test utilities when a conditional
116 * fails. The second is the user's call to dump a string.
118 void test_fail_string_message(const char *prefix, const char *file,
119 int line, const char *type,
120 const char *left, const char *right,
121 const char *op, const char *m1, size_t l1,
122 const char *m2, size_t l2)
124 test_fail_string_common(prefix, file, line, type, left, right, op,
126 test_printf_stderr("\n");
129 void test_output_string(const char *name, const char *m, size_t l)
131 test_fail_string_common("string", NULL, 0, NULL, NULL, NULL, name,
135 /* BIGNUM formatted output routines */
138 * A basic memory byte to hex digit converter with allowance for spacing
141 static void hex_convert_memory(const unsigned char *m, size_t n, char *b,
146 for (i = 0; i < n; i++) {
147 const unsigned char c = *m++;
149 *b++ = "0123456789abcdef"[c >> 4];
150 *b++ = "0123456789abcdef"[c & 15];
151 if (i % width == width - 1 && i != n - 1)
158 * Constants to define the number of bytes to display per line and the number
159 * of characters these take.
161 static const int bn_bytes = (MAX_STRING_WIDTH - 9) / (BN_OUTPUT_SIZE * 2 + 1)
163 static const int bn_chars = (MAX_STRING_WIDTH - 9) / (BN_OUTPUT_SIZE * 2 + 1)
164 * (BN_OUTPUT_SIZE * 2 + 1) - 1;
167 * Output the header line for the bignum
169 static void test_bignum_header_line(void)
171 test_printf_stderr("%*s# %*s\n", subtest_level(), "", bn_chars + 6,
175 static const char *test_bignum_zero_null(const BIGNUM *bn)
178 return BN_is_negative(bn) ? "-0" : "0";
183 * Print a bignum zero taking care to include the correct sign.
184 * This routine correctly deals with a NULL bignum pointer as input.
186 static void test_bignum_zero_print(const BIGNUM *bn, char sep)
188 const char *v = test_bignum_zero_null(bn);
189 const char *suf = bn != NULL ? ": 0" : "";
191 test_printf_stderr("%*s# %c%*s%s\n", subtest_level(), "", sep, bn_chars,
196 * Convert a section of memory from inside a bignum into a displayable
197 * string with appropriate visual aid spaces inserted.
199 static int convert_bn_memory(const unsigned char *in, size_t bytes,
200 char *out, int *lz, const BIGNUM *bn)
202 int n = bytes * 2, i;
203 char *p = out, *q = NULL;
205 if (bn != NULL && !BN_is_zero(bn)) {
206 hex_convert_memory(in, bytes, out, BN_OUTPUT_SIZE);
208 for (; *p == '0' || *p == ' '; p++)
216 * in[bytes] is defined because we're converting a non-zero
217 * number and we've not seen a non-zero yet.
219 if ((in[bytes] & 0xf0) != 0 && BN_is_negative(bn)) {
226 if (BN_is_negative(bn)) {
228 * This is valid because we always convert more digits than
239 for (i = 0; i < n; i++) {
241 if (i % (2 * BN_OUTPUT_SIZE) == 2 * BN_OUTPUT_SIZE - 1 && i != n - 1)
248 q = BN_is_negative(bn) ? "-0" : "0";
249 strcpy(p - strlen(q), q);
254 * Common code to display either one or two bignums, including the diff
255 * pointers for changes (only when there are two).
257 static void test_fail_bignum_common(const char *prefix, const char *file,
258 int line, const char *type,
259 const char *left, const char *right,
261 const BIGNUM *bn1, const BIGNUM *bn2)
263 const int indent = subtest_level();
264 const size_t bytes = bn_bytes;
265 char b1[MAX_STRING_WIDTH + 1], b2[MAX_STRING_WIDTH + 1];
266 char *p, bdiff[MAX_STRING_WIDTH + 1];
267 size_t l1, l2, n1, n2, i, len;
268 unsigned int cnt, diff, real_diff;
269 unsigned char *m1 = NULL, *m2 = NULL;
270 int lz1 = 1, lz2 = 1;
271 unsigned char buffer[MEM_BUFFER_SIZE * 2], *bufp = buffer;
273 test_fail_message_prefix(prefix, file, line, type, left, right, op);
274 l1 = bn1 == NULL ? 0 : (BN_num_bytes(bn1) + (BN_is_negative(bn1) ? 1 : 0));
275 l2 = bn2 == NULL ? 0 : (BN_num_bytes(bn2) + (BN_is_negative(bn2) ? 1 : 0));
276 if (l1 == 0 && l2 == 0) {
277 if ((bn1 == NULL) == (bn2 == NULL)) {
278 test_bignum_header_line();
279 test_bignum_zero_print(bn1, ' ');
281 test_diff_header(left, right);
282 test_bignum_header_line();
283 test_bignum_zero_print(bn1, '-');
284 test_bignum_zero_print(bn2, '+');
289 if (l1 != l2 || bn1 == NULL || bn2 == NULL || BN_cmp(bn1, bn2) != 0)
290 test_diff_header(left, right);
291 test_bignum_header_line();
293 len = ((l1 > l2 ? l1 : l2) + bytes - 1) / bytes * bytes;
295 if (len > MEM_BUFFER_SIZE && (bufp = OPENSSL_malloc(len * 2)) == NULL) {
297 len = MEM_BUFFER_SIZE;
298 test_printf_stderr("%*s# WARNING: these BIGNUMs have been truncated",
304 BN_bn2binpad(bn1, m1, len);
308 BN_bn2binpad(bn2, m2, len);
312 cnt = 8 * (len - bytes);
313 n1 = convert_bn_memory(m1, bytes, b1, &lz1, bn1);
314 n2 = convert_bn_memory(m2, bytes, b2, &lz2, bn2);
316 diff = real_diff = 0;
319 for (i=0; b1[i] != '\0'; i++)
320 if (b1[i] == b2[i] || b1[i] == ' ' || b2[i] == ' ') {
322 diff |= b1[i] != b2[i];
325 real_diff = diff = 1;
329 test_printf_stderr("%*s# %s:% 5d\n", indent, "",
330 n2 > n1 ? b2 : b1, cnt);
332 if (cnt == 0 && bn1 == NULL)
333 test_printf_stderr("%*s# -%s\n", indent, "", b1);
334 else if (cnt == 0 || n1 > 0)
335 test_printf_stderr("%*s# -%s:% 5d\n", indent, "", b1, cnt);
336 if (cnt == 0 && bn2 == NULL)
337 test_printf_stderr("%*s# +%s\n", indent, "", b2);
338 else if (cnt == 0 || n2 > 0)
339 test_printf_stderr("%*s# +%s:% 5d\n", indent, "", b2, cnt);
340 if (real_diff && (cnt == 0 || (n1 > 0 && n2 > 0))
341 && bn1 != NULL && bn2 != NULL)
342 test_printf_stderr("%*s# %s\n", indent, "", bdiff);
357 * Wrapper routines so that the underlying code can be shared.
358 * The first two are calls from inside the test utilities when a conditional
359 * fails. The third is the user's call to dump a bignum.
361 void test_fail_bignum_message(const char *prefix, const char *file,
362 int line, const char *type,
363 const char *left, const char *right,
365 const BIGNUM *bn1, const BIGNUM *bn2)
367 test_fail_bignum_common(prefix, file, line, type, left, right, op, bn1, bn2);
368 test_printf_stderr("\n");
371 void test_fail_bignum_mono_message(const char *prefix, const char *file,
372 int line, const char *type,
373 const char *left, const char *right,
374 const char *op, const BIGNUM *bn)
376 test_fail_bignum_common(prefix, file, line, type, left, right, op, bn, bn);
377 test_printf_stderr("\n");
380 void test_output_bignum(const char *name, const BIGNUM *bn)
382 if (bn == NULL || BN_is_zero(bn)) {
383 test_printf_stderr("%*s# bignum: '%s' = %s\n", subtest_level(), "",
384 name, test_bignum_zero_null(bn));
385 } else if (BN_num_bytes(bn) <= BN_OUTPUT_SIZE) {
386 unsigned char buf[BN_OUTPUT_SIZE];
387 char out[2 * sizeof(buf) + 1];
389 int n = BN_bn2bin(bn, buf);
391 hex_convert_memory(buf, n, p, BN_OUTPUT_SIZE);
392 while (*p == '0' && *++p != '\0')
394 test_printf_stderr("%*s# bignum: '%s' = %s0x%s\n", subtest_level(), "",
395 name, BN_is_negative(bn) ? "-" : "", p);
397 test_fail_bignum_common("bignum", NULL, 0, NULL, NULL, NULL, name,
402 /* Memory output routines */
405 * Handle zero length blocks of memory or NULL pointers to memory
407 static void test_memory_null_empty(const unsigned char *m, int indent, char c)
410 test_printf_stderr("%*s# % 4s %c%s\n", indent, "", "", c, "NULL");
412 test_printf_stderr("%*s# %04x %c%s\n", indent, "", 0u, c, "empty");
416 * Common code to display one or two blocks of memory.
418 static void test_fail_memory_common(const char *prefix, const char *file,
419 int line, const char *type,
420 const char *left, const char *right,
422 const unsigned char *m1, size_t l1,
423 const unsigned char *m2, size_t l2)
425 const int indent = subtest_level();
426 const size_t bytes = (MAX_STRING_WIDTH - 9) / 17 * 8;
427 char b1[MAX_STRING_WIDTH + 1], b2[MAX_STRING_WIDTH + 1];
428 char *p, bdiff[MAX_STRING_WIDTH + 1];
430 unsigned int cnt = 0, diff;
432 test_fail_message_prefix(prefix, file, line, type, left, right, op);
437 if (l1 == 0 && l2 == 0) {
438 if ((m1 == NULL) == (m2 == NULL)) {
439 test_memory_null_empty(m1, indent, ' ');
441 test_diff_header(left, right);
442 test_memory_null_empty(m1, indent, '-');
443 test_memory_null_empty(m2, indent, '+');
448 if (l1 != l2 || (m1 != m2 && memcmp(m1, m2, l1) != 0))
449 test_diff_header(left, right);
451 while (l1 > 0 || l2 > 0) {
454 n1 = l1 > bytes ? bytes : l1;
455 hex_convert_memory(m1, n1, b1, 8);
458 n2 = l2 > bytes ? bytes : l2;
459 hex_convert_memory(m2, n2, b2, 8);
465 if (n1 > 0 && n2 > 0) {
466 const size_t j = n1 < n2 ? n1 : n2;
469 if (m1[i] == m2[i]) {
477 if (i % 8 == 7 && i != j - 1)
483 if (n1 == n2 && !diff) {
484 test_printf_stderr("%*s# %04x: %s\n", indent, "", cnt, b1);
486 if (cnt == 0 && (m1 == NULL || l1 == 0))
487 test_memory_null_empty(m1, indent, '-');
489 test_printf_stderr("%*s# %04x:-%s\n", indent, "", cnt, b1);
490 if (cnt == 0 && (m2 == NULL || l2 == 0))
491 test_memory_null_empty(m2, indent, '+');
493 test_printf_stderr("%*s# %04x:+%s\n", indent, "", cnt, b2);
495 test_printf_stderr("%*s# % 4s %s\n", indent, "", "", bdiff);
508 * Wrapper routines so that the underlying code can be shared.
509 * The first is the call from inside the test utilities when a conditional
510 * fails. The second is the user's call to dump memory.
512 void test_fail_memory_message(const char *prefix, const char *file,
513 int line, const char *type,
514 const char *left, const char *right,
516 const unsigned char *m1, size_t l1,
517 const unsigned char *m2, size_t l2)
519 test_fail_memory_common(prefix, file, line, type, left, right, op,
521 test_printf_stderr("\n");
524 void test_output_memory(const char *name, const unsigned char *m, size_t l)
526 test_fail_memory_common("memory", NULL, 0, NULL, NULL, NULL, name,