bioprinttest.c 12 KB

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