format_output.c 17 KB

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