format_output.c 17 KB

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