format_output.c 17 KB

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