format_output.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /*
  2. * Copyright 2017-2018 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 = (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. const char *r;
  188. if (bn != NULL && !BN_is_zero(bn)) {
  189. hex_convert_memory(in, bytes, out, BN_OUTPUT_SIZE);
  190. if (*lz) {
  191. for (; *p == '0' || *p == ' '; p++)
  192. if (*p == '0') {
  193. q = p;
  194. *p = ' ';
  195. n--;
  196. }
  197. if (*p == '\0') {
  198. /*
  199. * in[bytes] is defined because we're converting a non-zero
  200. * number and we've not seen a non-zero yet.
  201. */
  202. if ((in[bytes] & 0xf0) != 0 && BN_is_negative(bn)) {
  203. *lz = 0;
  204. *q = '-';
  205. n++;
  206. }
  207. } else {
  208. *lz = 0;
  209. if (BN_is_negative(bn)) {
  210. /*
  211. * This is valid because we always convert more digits than
  212. * the number holds.
  213. */
  214. *q = '-';
  215. n++;
  216. }
  217. }
  218. }
  219. return n;
  220. }
  221. for (i = 0; i < n; i++) {
  222. *p++ = ' ';
  223. if (i % (2 * BN_OUTPUT_SIZE) == 2 * BN_OUTPUT_SIZE - 1 && i != n - 1)
  224. *p++ = ' ';
  225. }
  226. *p = '\0';
  227. if (bn == NULL)
  228. r = "NULL";
  229. else
  230. r = BN_is_negative(bn) ? "-0" : "0";
  231. strcpy(p - strlen(r), r);
  232. return 0;
  233. }
  234. /*
  235. * Common code to display either one or two bignums, including the diff
  236. * pointers for changes (only when there are two).
  237. */
  238. static void test_fail_bignum_common(const char *prefix, const char *file,
  239. int line, const char *type,
  240. const char *left, const char *right,
  241. const char *op,
  242. const BIGNUM *bn1, const BIGNUM *bn2)
  243. {
  244. const size_t bytes = bn_bytes;
  245. char b1[MAX_STRING_WIDTH + 1], b2[MAX_STRING_WIDTH + 1];
  246. char *p, bdiff[MAX_STRING_WIDTH + 1];
  247. size_t l1, l2, n1, n2, i, len;
  248. unsigned int cnt, diff, real_diff;
  249. unsigned char *m1 = NULL, *m2 = NULL;
  250. int lz1 = 1, lz2 = 1;
  251. unsigned char buffer[MEM_BUFFER_SIZE * 2], *bufp = buffer;
  252. test_fail_message_prefix(prefix, file, line, type, left, right, op);
  253. l1 = bn1 == NULL ? 0 : (BN_num_bytes(bn1) + (BN_is_negative(bn1) ? 1 : 0));
  254. l2 = bn2 == NULL ? 0 : (BN_num_bytes(bn2) + (BN_is_negative(bn2) ? 1 : 0));
  255. if (l1 == 0 && l2 == 0) {
  256. if ((bn1 == NULL) == (bn2 == NULL)) {
  257. test_bignum_header_line();
  258. test_bignum_zero_print(bn1, ' ');
  259. } else {
  260. test_diff_header(left, right);
  261. test_bignum_header_line();
  262. test_bignum_zero_print(bn1, '-');
  263. test_bignum_zero_print(bn2, '+');
  264. }
  265. goto fin;
  266. }
  267. if (l1 != l2 || bn1 == NULL || bn2 == NULL || BN_cmp(bn1, bn2) != 0)
  268. test_diff_header(left, right);
  269. test_bignum_header_line();
  270. len = ((l1 > l2 ? l1 : l2) + bytes - 1) / bytes * bytes;
  271. if (len > MEM_BUFFER_SIZE && (bufp = OPENSSL_malloc(len * 2)) == NULL) {
  272. bufp = buffer;
  273. len = MEM_BUFFER_SIZE;
  274. test_printf_stderr("WARNING: these BIGNUMs have been truncated\n");
  275. }
  276. if (bn1 != NULL) {
  277. m1 = bufp;
  278. BN_bn2binpad(bn1, m1, len);
  279. }
  280. if (bn2 != NULL) {
  281. m2 = bufp + len;
  282. BN_bn2binpad(bn2, m2, len);
  283. }
  284. while (len > 0) {
  285. cnt = 8 * (len - bytes);
  286. n1 = convert_bn_memory(m1, bytes, b1, &lz1, bn1);
  287. n2 = convert_bn_memory(m2, bytes, b2, &lz2, bn2);
  288. diff = real_diff = 0;
  289. i = 0;
  290. p = bdiff;
  291. for (i=0; b1[i] != '\0'; i++)
  292. if (b1[i] == b2[i] || b1[i] == ' ' || b2[i] == ' ') {
  293. *p++ = ' ';
  294. diff |= b1[i] != b2[i];
  295. } else {
  296. *p++ = '^';
  297. real_diff = diff = 1;
  298. }
  299. *p++ = '\0';
  300. if (!diff) {
  301. test_printf_stderr(" %s:% 5d\n", n2 > n1 ? b2 : b1, cnt);
  302. } else {
  303. if (cnt == 0 && bn1 == NULL)
  304. test_printf_stderr("-%s\n", b1);
  305. else if (cnt == 0 || n1 > 0)
  306. test_printf_stderr("-%s:% 5d\n", b1, cnt);
  307. if (cnt == 0 && bn2 == NULL)
  308. test_printf_stderr("+%s\n", b2);
  309. else if (cnt == 0 || n2 > 0)
  310. test_printf_stderr("+%s:% 5d\n", b2, cnt);
  311. if (real_diff && (cnt == 0 || (n1 > 0 && n2 > 0))
  312. && bn1 != NULL && bn2 != NULL)
  313. test_printf_stderr(" %s\n", bdiff);
  314. }
  315. if (m1 != NULL)
  316. m1 += bytes;
  317. if (m2 != NULL)
  318. m2 += bytes;
  319. len -= bytes;
  320. }
  321. fin:
  322. test_flush_stderr();
  323. if (bufp != buffer)
  324. OPENSSL_free(bufp);
  325. }
  326. /*
  327. * Wrapper routines so that the underlying code can be shared.
  328. * The first two are calls from inside the test utilities when a conditional
  329. * fails. The third is the user's call to dump a bignum.
  330. */
  331. void test_fail_bignum_message(const char *prefix, const char *file,
  332. int line, const char *type,
  333. const char *left, const char *right,
  334. const char *op,
  335. const BIGNUM *bn1, const BIGNUM *bn2)
  336. {
  337. test_fail_bignum_common(prefix, file, line, type, left, right, op, bn1, bn2);
  338. test_printf_stderr("\n");
  339. }
  340. void test_fail_bignum_mono_message(const char *prefix, const char *file,
  341. int line, const char *type,
  342. const char *left, const char *right,
  343. const char *op, const BIGNUM *bn)
  344. {
  345. test_fail_bignum_common(prefix, file, line, type, left, right, op, bn, bn);
  346. test_printf_stderr("\n");
  347. }
  348. void test_output_bignum(const char *name, const BIGNUM *bn)
  349. {
  350. if (bn == NULL || BN_is_zero(bn)) {
  351. test_printf_stderr("bignum: '%s' = %s\n", name,
  352. test_bignum_zero_null(bn));
  353. } else if (BN_num_bytes(bn) <= BN_OUTPUT_SIZE) {
  354. unsigned char buf[BN_OUTPUT_SIZE];
  355. char out[2 * sizeof(buf) + 1];
  356. char *p = out;
  357. int n = BN_bn2bin(bn, buf);
  358. hex_convert_memory(buf, n, p, BN_OUTPUT_SIZE);
  359. while (*p == '0' && *++p != '\0')
  360. ;
  361. test_printf_stderr("bignum: '%s' = %s0x%s\n", name,
  362. BN_is_negative(bn) ? "-" : "", p);
  363. } else {
  364. test_fail_bignum_common("bignum", NULL, 0, NULL, NULL, NULL, name,
  365. bn, bn);
  366. }
  367. }
  368. /* Memory output routines */
  369. /*
  370. * Handle zero length blocks of memory or NULL pointers to memory
  371. */
  372. static void test_memory_null_empty(const unsigned char *m, char c)
  373. {
  374. if (m == NULL)
  375. test_printf_stderr("%4s %c%s\n", "", c, "NULL");
  376. else
  377. test_printf_stderr("%04x %c%s\n", 0u, c, "empty");
  378. }
  379. /*
  380. * Common code to display one or two blocks of memory.
  381. */
  382. static void test_fail_memory_common(const char *prefix, const char *file,
  383. int line, const char *type,
  384. const char *left, const char *right,
  385. const char *op,
  386. const unsigned char *m1, size_t l1,
  387. const unsigned char *m2, size_t l2)
  388. {
  389. const size_t bytes = (MAX_STRING_WIDTH - 9) / 17 * 8;
  390. char b1[MAX_STRING_WIDTH + 1], b2[MAX_STRING_WIDTH + 1];
  391. char *p, bdiff[MAX_STRING_WIDTH + 1];
  392. size_t n1, n2, i;
  393. unsigned int cnt = 0, diff;
  394. test_fail_message_prefix(prefix, file, line, type, left, right, op);
  395. if (m1 == NULL)
  396. l1 = 0;
  397. if (m2 == NULL)
  398. l2 = 0;
  399. if (l1 == 0 && l2 == 0) {
  400. if ((m1 == NULL) == (m2 == NULL)) {
  401. test_memory_null_empty(m1, ' ');
  402. } else {
  403. test_diff_header(left, right);
  404. test_memory_null_empty(m1, '-');
  405. test_memory_null_empty(m2, '+');
  406. }
  407. goto fin;
  408. }
  409. if (l1 != l2 || (m1 != m2 && memcmp(m1, m2, l1) != 0))
  410. test_diff_header(left, right);
  411. while (l1 > 0 || l2 > 0) {
  412. n1 = n2 = 0;
  413. if (l1 > 0) {
  414. n1 = l1 > bytes ? bytes : l1;
  415. hex_convert_memory(m1, n1, b1, 8);
  416. }
  417. if (l2 > 0) {
  418. n2 = l2 > bytes ? bytes : l2;
  419. hex_convert_memory(m2, n2, b2, 8);
  420. }
  421. diff = 0;
  422. i = 0;
  423. p = bdiff;
  424. if (n1 > 0 && n2 > 0) {
  425. const size_t j = n1 < n2 ? n1 : n2;
  426. for (; i < j; i++) {
  427. if (m1[i] == m2[i]) {
  428. *p++ = ' ';
  429. *p++ = ' ';
  430. } else {
  431. *p++ = '^';
  432. *p++ = '^';
  433. diff = 1;
  434. }
  435. if (i % 8 == 7 && i != j - 1)
  436. *p++ = ' ';
  437. }
  438. *p++ = '\0';
  439. }
  440. if (n1 == n2 && !diff) {
  441. test_printf_stderr("%04x: %s\n", cnt, b1);
  442. } else {
  443. if (cnt == 0 && (m1 == NULL || l1 == 0))
  444. test_memory_null_empty(m1, '-');
  445. else if (n1 > 0)
  446. test_printf_stderr("%04x:-%s\n", cnt, b1);
  447. if (cnt == 0 && (m2 == NULL || l2 == 0))
  448. test_memory_null_empty(m2, '+');
  449. else if (n2 > 0)
  450. test_printf_stderr("%04x:+%s\n", cnt, b2);
  451. if (diff && i > 0)
  452. test_printf_stderr("%4s %s\n", "", bdiff);
  453. }
  454. m1 += n1;
  455. m2 += n2;
  456. l1 -= n1;
  457. l2 -= n2;
  458. cnt += bytes;
  459. }
  460. fin:
  461. test_flush_stderr();
  462. }
  463. /*
  464. * Wrapper routines so that the underlying code can be shared.
  465. * The first is the call from inside the test utilities when a conditional
  466. * fails. The second is the user's call to dump memory.
  467. */
  468. void test_fail_memory_message(const char *prefix, const char *file,
  469. int line, const char *type,
  470. const char *left, const char *right,
  471. const char *op,
  472. const unsigned char *m1, size_t l1,
  473. const unsigned char *m2, size_t l2)
  474. {
  475. test_fail_memory_common(prefix, file, line, type, left, right, op,
  476. m1, l1, m2, l2);
  477. test_printf_stderr("\n");
  478. }
  479. void test_output_memory(const char *name, const unsigned char *m, size_t l)
  480. {
  481. test_fail_memory_common("memory", NULL, 0, NULL, NULL, NULL, name,
  482. m, l, m, l);
  483. }