bioprinttest.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Copyright 2016 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. #define TESTUTIL_NO_size_t_COMPARISON
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <openssl/bio.h>
  13. #include "internal/numbers.h"
  14. #include "testutil.h"
  15. #include "testutil/output.h"
  16. #define nelem(x) (int)(sizeof(x) / sizeof((x)[0]))
  17. static int justprint = 0;
  18. static char *fpexpected[][5] = {
  19. /* 0 */ { "0.0000e+00", "0.0000", "0", "0.0000E+00", "0" },
  20. /* 1 */ { "6.7000e-01", "0.6700", "0.67", "6.7000E-01", "0.67" },
  21. /* 2 */ { "6.6667e-01", "0.6667", "0.6667", "6.6667E-01", "0.6667" },
  22. /* 3 */ { "6.6667e-04", "0.0007", "0.0006667", "6.6667E-04", "0.0006667" },
  23. /* 4 */ { "6.6667e-05", "0.0001", "6.667e-05", "6.6667E-05", "6.667E-05" },
  24. /* 5 */ { "6.6667e+00", "6.6667", "6.667", "6.6667E+00", "6.667" },
  25. /* 6 */ { "6.6667e+01", "66.6667", "66.67", "6.6667E+01", "66.67" },
  26. /* 7 */ { "6.6667e+02", "666.6667", "666.7", "6.6667E+02", "666.7" },
  27. /* 8 */ { "6.6667e+03", "6666.6667", "6667", "6.6667E+03", "6667" },
  28. /* 9 */ { "6.6667e+04", "66666.6667", "6.667e+04", "6.6667E+04", "6.667E+04" },
  29. /* 10 */ { "0.00000e+00", "0.00000", "0", "0.00000E+00", "0" },
  30. /* 11 */ { "6.70000e-01", "0.67000", "0.67", "6.70000E-01", "0.67" },
  31. /* 12 */ { "6.66667e-01", "0.66667", "0.66667", "6.66667E-01", "0.66667" },
  32. /* 13 */ { "6.66667e-04", "0.00067", "0.00066667", "6.66667E-04", "0.00066667" },
  33. /* 14 */ { "6.66667e-05", "0.00007", "6.6667e-05", "6.66667E-05", "6.6667E-05" },
  34. /* 15 */ { "6.66667e+00", "6.66667", "6.6667", "6.66667E+00", "6.6667" },
  35. /* 16 */ { "6.66667e+01", "66.66667", "66.667", "6.66667E+01", "66.667" },
  36. /* 17 */ { "6.66667e+02", "666.66667", "666.67", "6.66667E+02", "666.67" },
  37. /* 18 */ { "6.66667e+03", "6666.66667", "6666.7", "6.66667E+03", "6666.7" },
  38. /* 19 */ { "6.66667e+04", "66666.66667", "66667", "6.66667E+04", "66667" },
  39. /* 20 */ { " 0.0000e+00", " 0.0000", " 0", " 0.0000E+00", " 0" },
  40. /* 21 */ { " 6.7000e-01", " 0.6700", " 0.67", " 6.7000E-01", " 0.67" },
  41. /* 22 */ { " 6.6667e-01", " 0.6667", " 0.6667", " 6.6667E-01", " 0.6667" },
  42. /* 23 */ { " 6.6667e-04", " 0.0007", " 0.0006667", " 6.6667E-04", " 0.0006667" },
  43. /* 24 */ { " 6.6667e-05", " 0.0001", " 6.667e-05", " 6.6667E-05", " 6.667E-05" },
  44. /* 25 */ { " 6.6667e+00", " 6.6667", " 6.667", " 6.6667E+00", " 6.667" },
  45. /* 26 */ { " 6.6667e+01", " 66.6667", " 66.67", " 6.6667E+01", " 66.67" },
  46. /* 27 */ { " 6.6667e+02", " 666.6667", " 666.7", " 6.6667E+02", " 666.7" },
  47. /* 28 */ { " 6.6667e+03", " 6666.6667", " 6667", " 6.6667E+03", " 6667" },
  48. /* 29 */ { " 6.6667e+04", " 66666.6667", " 6.667e+04", " 6.6667E+04", " 6.667E+04" },
  49. /* 30 */ { " 0.00000e+00", " 0.00000", " 0", " 0.00000E+00", " 0" },
  50. /* 31 */ { " 6.70000e-01", " 0.67000", " 0.67", " 6.70000E-01", " 0.67" },
  51. /* 32 */ { " 6.66667e-01", " 0.66667", " 0.66667", " 6.66667E-01", " 0.66667" },
  52. /* 33 */ { " 6.66667e-04", " 0.00067", " 0.00066667", " 6.66667E-04", " 0.00066667" },
  53. /* 34 */ { " 6.66667e-05", " 0.00007", " 6.6667e-05", " 6.66667E-05", " 6.6667E-05" },
  54. /* 35 */ { " 6.66667e+00", " 6.66667", " 6.6667", " 6.66667E+00", " 6.6667" },
  55. /* 36 */ { " 6.66667e+01", " 66.66667", " 66.667", " 6.66667E+01", " 66.667" },
  56. /* 37 */ { " 6.66667e+02", " 666.66667", " 666.67", " 6.66667E+02", " 666.67" },
  57. /* 38 */ { " 6.66667e+03", " 6666.66667", " 6666.7", " 6.66667E+03", " 6666.7" },
  58. /* 39 */ { " 6.66667e+04", " 66666.66667", " 66667", " 6.66667E+04", " 66667" },
  59. /* 40 */ { "0e+00", "0", "0", "0E+00", "0" },
  60. /* 41 */ { "7e-01", "1", "0.7", "7E-01", "0.7" },
  61. /* 42 */ { "7e-01", "1", "0.7", "7E-01", "0.7" },
  62. /* 43 */ { "7e-04", "0", "0.0007", "7E-04", "0.0007" },
  63. /* 44 */ { "7e-05", "0", "7e-05", "7E-05", "7E-05" },
  64. /* 45 */ { "7e+00", "7", "7", "7E+00", "7" },
  65. /* 46 */ { "7e+01", "67", "7e+01", "7E+01", "7E+01" },
  66. /* 47 */ { "7e+02", "667", "7e+02", "7E+02", "7E+02" },
  67. /* 48 */ { "7e+03", "6667", "7e+03", "7E+03", "7E+03" },
  68. /* 49 */ { "7e+04", "66667", "7e+04", "7E+04", "7E+04" },
  69. /* 50 */ { "0.000000e+00", "0.000000", "0", "0.000000E+00", "0" },
  70. /* 51 */ { "6.700000e-01", "0.670000", "0.67", "6.700000E-01", "0.67" },
  71. /* 52 */ { "6.666667e-01", "0.666667", "0.666667", "6.666667E-01", "0.666667" },
  72. /* 53 */ { "6.666667e-04", "0.000667", "0.000666667", "6.666667E-04", "0.000666667" },
  73. /* 54 */ { "6.666667e-05", "0.000067", "6.66667e-05", "6.666667E-05", "6.66667E-05" },
  74. /* 55 */ { "6.666667e+00", "6.666667", "6.66667", "6.666667E+00", "6.66667" },
  75. /* 56 */ { "6.666667e+01", "66.666667", "66.6667", "6.666667E+01", "66.6667" },
  76. /* 57 */ { "6.666667e+02", "666.666667", "666.667", "6.666667E+02", "666.667" },
  77. /* 58 */ { "6.666667e+03", "6666.666667", "6666.67", "6.666667E+03", "6666.67" },
  78. /* 59 */ { "6.666667e+04", "66666.666667", "66666.7", "6.666667E+04", "66666.7" },
  79. /* 60 */ { "0.0000e+00", "000.0000", "00000000", "0.0000E+00", "00000000" },
  80. /* 61 */ { "6.7000e-01", "000.6700", "00000.67", "6.7000E-01", "00000.67" },
  81. /* 62 */ { "6.6667e-01", "000.6667", "000.6667", "6.6667E-01", "000.6667" },
  82. /* 63 */ { "6.6667e-04", "000.0007", "0.0006667", "6.6667E-04", "0.0006667" },
  83. /* 64 */ { "6.6667e-05", "000.0001", "6.667e-05", "6.6667E-05", "6.667E-05" },
  84. /* 65 */ { "6.6667e+00", "006.6667", "0006.667", "6.6667E+00", "0006.667" },
  85. /* 66 */ { "6.6667e+01", "066.6667", "00066.67", "6.6667E+01", "00066.67" },
  86. /* 67 */ { "6.6667e+02", "666.6667", "000666.7", "6.6667E+02", "000666.7" },
  87. /* 68 */ { "6.6667e+03", "6666.6667", "00006667", "6.6667E+03", "00006667" },
  88. /* 69 */ { "6.6667e+04", "66666.6667", "6.667e+04", "6.6667E+04", "6.667E+04" },
  89. };
  90. typedef struct z_data_st {
  91. size_t value;
  92. const char *format;
  93. const char *expected;
  94. } z_data;
  95. static z_data zu_data[] = {
  96. { SIZE_MAX, "%zu", (sizeof(size_t) == 4 ? "4294967295"
  97. : sizeof(size_t) == 8 ? "18446744073709551615"
  98. : "") },
  99. /*
  100. * in 2-complement, the unsigned number divided by two plus one becomes the
  101. * smallest possible negative signed number of the corresponding type
  102. */
  103. { SIZE_MAX / 2 + 1, "%zi", (sizeof(size_t) == 4 ? "-2147483648"
  104. : sizeof(size_t) == 8 ? "-9223372036854775808"
  105. : "") },
  106. { 0, "%zu", "0" },
  107. { 0, "%zi", "0" },
  108. };
  109. static int test_zu(int i)
  110. {
  111. char bio_buf[80];
  112. const z_data *data = &zu_data[i];
  113. BIO_snprintf(bio_buf, sizeof(bio_buf) - 1, data->format, data->value);
  114. if (!TEST_str_eq(bio_buf, data->expected))
  115. return 0;
  116. return 1;
  117. }
  118. typedef struct j_data_st {
  119. uint64_t value;
  120. const char *format;
  121. const char *expected;
  122. } j_data;
  123. static j_data jf_data[] = {
  124. { 0xffffffffffffffffU, "%ju", "18446744073709551615" },
  125. { 0xffffffffffffffffU, "%jx", "ffffffffffffffff" },
  126. { 0x8000000000000000U, "%ju", "9223372036854775808" },
  127. /*
  128. * These tests imply two's-complement, but it's the only binary
  129. * representation we support, see test/sanitytest.c...
  130. */
  131. { 0x8000000000000000U, "%ji", "-9223372036854775808" },
  132. };
  133. static int test_j(int i)
  134. {
  135. const j_data *data = &jf_data[i];
  136. char bio_buf[80];
  137. BIO_snprintf(bio_buf, sizeof(bio_buf) - 1, data->format, data->value);
  138. if (!TEST_str_eq(bio_buf, data->expected))
  139. return 0;
  140. return 1;
  141. }
  142. /* Precision and width. */
  143. typedef struct pw_st {
  144. int p;
  145. const char *w;
  146. } pw;
  147. static pw pw_params[] = {
  148. { 4, "" },
  149. { 5, "" },
  150. { 4, "12" },
  151. { 5, "12" },
  152. { 0, "" },
  153. { -1, "" },
  154. { 4, "08" }
  155. };
  156. static int dofptest(int test, double val, const char *width, int prec)
  157. {
  158. static const char *fspecs[] = {
  159. "e", "f", "g", "E", "G"
  160. };
  161. char format[80], result[80];
  162. int ret = 1, i;
  163. for (i = 0; i < nelem(fspecs); i++) {
  164. const char *fspec = fspecs[i];
  165. if (prec >= 0)
  166. BIO_snprintf(format, sizeof(format), "%%%s.%d%s", width, prec,
  167. fspec);
  168. else
  169. BIO_snprintf(format, sizeof(format), "%%%s%s", width, fspec);
  170. BIO_snprintf(result, sizeof(result), format, val);
  171. if (justprint) {
  172. if (i == 0)
  173. printf(" /* %d */ { \"%s\"", test, result);
  174. else
  175. printf(", \"%s\"", result);
  176. } else if (!TEST_str_eq(fpexpected[test][i], result)) {
  177. TEST_info("test %d format=|%s| exp=|%s|, ret=|%s|",
  178. test, format, fpexpected[test][i], result);
  179. ret = 0;
  180. }
  181. }
  182. if (justprint)
  183. printf(" },\n");
  184. return ret;
  185. }
  186. static int test_fp(int i)
  187. {
  188. static int t = 0;
  189. const double frac = 2.0 / 3.0;
  190. const pw *pwp = &pw_params[i];
  191. if (!TEST_true(dofptest(t++, 0.0, pwp->w, pwp->p))
  192. || !TEST_true(dofptest(t++, 0.67, pwp->w, pwp->p))
  193. || !TEST_true(dofptest(t++, frac, pwp->w, pwp->p))
  194. || !TEST_true(dofptest(t++, frac / 1000, pwp->w, pwp->p))
  195. || !TEST_true(dofptest(t++, frac / 10000, pwp->w, pwp->p))
  196. || !TEST_true(dofptest(t++, 6.0 + frac, pwp->w, pwp->p))
  197. || !TEST_true(dofptest(t++, 66.0 + frac, pwp->w, pwp->p))
  198. || !TEST_true(dofptest(t++, 666.0 + frac, pwp->w, pwp->p))
  199. || !TEST_true(dofptest(t++, 6666.0 + frac, pwp->w, pwp->p))
  200. || !TEST_true(dofptest(t++, 66666.0 + frac, pwp->w, pwp->p)))
  201. return 0;
  202. return 1;
  203. }
  204. static int test_big(void)
  205. {
  206. char buf[80];
  207. /* Test excessively big number. Should fail */
  208. if (!TEST_int_eq(BIO_snprintf(buf, sizeof(buf),
  209. "%f\n", 2 * (double)ULONG_MAX), -1))
  210. return 0;
  211. return 1;
  212. }
  213. int test_main(int argc, char **argv)
  214. {
  215. if (argc == 2 && strcmp(argv[1], "-expected") == 0)
  216. justprint = 1;
  217. ADD_TEST(test_big);
  218. ADD_ALL_TESTS(test_fp, nelem(pw_params));
  219. ADD_ALL_TESTS(test_zu, nelem(zu_data));
  220. ADD_ALL_TESTS(test_j, nelem(jf_data));
  221. return run_tests(argv[0]);
  222. }
  223. /*
  224. * Replace testutil output routines. We do this to eliminate possible sources
  225. * of BIO error
  226. */
  227. void test_open_streams(void)
  228. {
  229. }
  230. void test_close_streams(void)
  231. {
  232. }
  233. /*
  234. * This works out as long as caller doesn't use any "fancy" formats.
  235. * But we are caller's caller, and test_str_eq is the only one called,
  236. * and it uses only "%s", which is not "fancy"...
  237. */
  238. int test_vprintf_stdout(const char *fmt, va_list ap)
  239. {
  240. return vfprintf(stdout, fmt, ap);
  241. }
  242. int test_vprintf_stderr(const char *fmt, va_list ap)
  243. {
  244. return vfprintf(stderr, fmt, ap);
  245. }
  246. int test_flush_stdout(void)
  247. {
  248. return fflush(stdout);
  249. }
  250. int test_flush_stderr(void)
  251. {
  252. return fflush(stderr);
  253. }