format_output.c 17 KB

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