errtest.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. * Copyright 2018-2023 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 <string.h>
  10. #include <openssl/opensslconf.h>
  11. #include <openssl/err.h>
  12. #include <openssl/macros.h>
  13. #include "testutil.h"
  14. #if defined(OPENSSL_SYS_WINDOWS)
  15. # include <windows.h>
  16. #else
  17. # include <errno.h>
  18. #endif
  19. #ifndef OPENSSL_NO_DEPRECATED_3_0
  20. # define IS_HEX(ch) ((ch >= '0' && ch <='9') || (ch >= 'A' && ch <='F'))
  21. static int test_print_error_format(void)
  22. {
  23. /* Variables used to construct an error line */
  24. char *lib;
  25. const char *func = OPENSSL_FUNC;
  26. char *reason;
  27. # ifdef OPENSSL_NO_ERR
  28. char reasonbuf[255];
  29. # endif
  30. # ifndef OPENSSL_NO_FILENAMES
  31. const char *file = OPENSSL_FILE;
  32. const int line = OPENSSL_LINE;
  33. # else
  34. const char *file = "";
  35. const int line = 0;
  36. # endif
  37. /* The format for OpenSSL error lines */
  38. const char *expected_format = ":error:%08lX:%s:%s:%s:%s:%d";
  39. /*-
  40. * ^^ ^^ ^^ ^^ ^^
  41. * "library" name --------------------------++ || || || ||
  42. * function name ------------------------------++ || || ||
  43. * reason string (system error string) -----------++ || ||
  44. * file name ----------------------------------------++ ||
  45. * line number -----------------------------------------++
  46. */
  47. char expected[512];
  48. char *out = NULL, *p = NULL;
  49. int ret = 0, len;
  50. BIO *bio = NULL;
  51. const int syserr = EPERM;
  52. unsigned long errorcode;
  53. unsigned long reasoncode;
  54. /*
  55. * We set a mark here so we can clear the system error that we generate
  56. * with ERR_PUT_error(). That is, after all, just a simulation to verify
  57. * ERR_print_errors() output, not a real error.
  58. */
  59. ERR_set_mark();
  60. ERR_PUT_error(ERR_LIB_SYS, 0, syserr, file, line);
  61. errorcode = ERR_peek_error();
  62. reasoncode = ERR_GET_REASON(errorcode);
  63. if (!TEST_int_eq(reasoncode, syserr)) {
  64. ERR_pop_to_mark();
  65. goto err;
  66. }
  67. # if !defined(OPENSSL_NO_ERR)
  68. # if defined(OPENSSL_NO_AUTOERRINIT)
  69. lib = "lib(2)";
  70. # else
  71. lib = "system library";
  72. # endif
  73. reason = strerror(syserr);
  74. # else
  75. lib = "lib(2)";
  76. BIO_snprintf(reasonbuf, sizeof(reasonbuf), "reason(%lu)", reasoncode);
  77. reason = reasonbuf;
  78. # endif
  79. BIO_snprintf(expected, sizeof(expected), expected_format,
  80. errorcode, lib, func, reason, file, line);
  81. if (!TEST_ptr(bio = BIO_new(BIO_s_mem())))
  82. goto err;
  83. ERR_print_errors(bio);
  84. if (!TEST_int_gt(len = BIO_get_mem_data(bio, &out), 0))
  85. goto err;
  86. /* Skip over the variable thread id at the start of the string */
  87. for (p = out; *p != ':' && *p != 0; ++p) {
  88. if (!TEST_true(IS_HEX(*p)))
  89. goto err;
  90. }
  91. if (!TEST_true(*p != 0)
  92. || !TEST_strn_eq(expected, p, strlen(expected)))
  93. goto err;
  94. ret = 1;
  95. err:
  96. BIO_free(bio);
  97. return ret;
  98. }
  99. #endif
  100. /* Test that querying the error queue preserves the OS error. */
  101. static int preserves_system_error(void)
  102. {
  103. #if defined(OPENSSL_SYS_WINDOWS)
  104. SetLastError(ERROR_INVALID_FUNCTION);
  105. ERR_get_error();
  106. return TEST_int_eq(GetLastError(), ERROR_INVALID_FUNCTION);
  107. #else
  108. errno = EINVAL;
  109. ERR_get_error();
  110. return TEST_int_eq(errno, EINVAL);
  111. #endif
  112. }
  113. /* Test that calls to ERR_add_error_[v]data append */
  114. static int vdata_appends(void)
  115. {
  116. const char *data;
  117. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  118. ERR_add_error_data(1, "hello ");
  119. ERR_add_error_data(1, "world");
  120. ERR_peek_error_data(&data, NULL);
  121. return TEST_str_eq(data, "hello world");
  122. }
  123. static int raised_error(void)
  124. {
  125. const char *f, *data;
  126. int l;
  127. unsigned long e;
  128. /*
  129. * When OPENSSL_NO_ERR or OPENSSL_NO_FILENAMES, no file name or line
  130. * number is saved, so no point checking them.
  131. */
  132. #if !defined(OPENSSL_NO_FILENAMES) && !defined(OPENSSL_NO_ERR)
  133. const char *file;
  134. int line;
  135. file = __FILE__;
  136. line = __LINE__ + 2; /* The error is generated on the ERR_raise_data line */
  137. #endif
  138. ERR_raise_data(ERR_LIB_NONE, ERR_R_INTERNAL_ERROR,
  139. "calling exit()");
  140. if (!TEST_ulong_ne(e = ERR_get_error_all(&f, &l, NULL, &data, NULL), 0)
  141. || !TEST_int_eq(ERR_GET_REASON(e), ERR_R_INTERNAL_ERROR)
  142. #if !defined(OPENSSL_NO_FILENAMES) && !defined(OPENSSL_NO_ERR)
  143. || !TEST_int_eq(l, line)
  144. || !TEST_str_eq(f, file)
  145. #endif
  146. || !TEST_str_eq(data, "calling exit()"))
  147. return 0;
  148. return 1;
  149. }
  150. static int test_marks(void)
  151. {
  152. unsigned long mallocfail, shouldnot;
  153. /* Set an initial error */
  154. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  155. mallocfail = ERR_peek_last_error();
  156. if (!TEST_ulong_gt(mallocfail, 0))
  157. return 0;
  158. /* Setting and clearing a mark should not affect the error */
  159. if (!TEST_true(ERR_set_mark())
  160. || !TEST_true(ERR_pop_to_mark())
  161. || !TEST_ulong_eq(mallocfail, ERR_peek_last_error())
  162. || !TEST_true(ERR_set_mark())
  163. || !TEST_true(ERR_clear_last_mark())
  164. || !TEST_ulong_eq(mallocfail, ERR_peek_last_error()))
  165. return 0;
  166. /* Test popping errors */
  167. if (!TEST_true(ERR_set_mark()))
  168. return 0;
  169. ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
  170. if (!TEST_ulong_ne(mallocfail, ERR_peek_last_error())
  171. || !TEST_true(ERR_pop_to_mark())
  172. || !TEST_ulong_eq(mallocfail, ERR_peek_last_error()))
  173. return 0;
  174. /* Nested marks should also work */
  175. if (!TEST_true(ERR_set_mark())
  176. || !TEST_true(ERR_set_mark()))
  177. return 0;
  178. ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
  179. if (!TEST_ulong_ne(mallocfail, ERR_peek_last_error())
  180. || !TEST_true(ERR_pop_to_mark())
  181. || !TEST_true(ERR_pop_to_mark())
  182. || !TEST_ulong_eq(mallocfail, ERR_peek_last_error()))
  183. return 0;
  184. if (!TEST_true(ERR_set_mark()))
  185. return 0;
  186. ERR_raise(ERR_LIB_CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  187. shouldnot = ERR_peek_last_error();
  188. if (!TEST_ulong_ne(mallocfail, shouldnot)
  189. || !TEST_true(ERR_set_mark()))
  190. return 0;
  191. ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
  192. if (!TEST_ulong_ne(shouldnot, ERR_peek_last_error())
  193. || !TEST_true(ERR_pop_to_mark())
  194. || !TEST_ulong_eq(shouldnot, ERR_peek_last_error())
  195. || !TEST_true(ERR_pop_to_mark())
  196. || !TEST_ulong_eq(mallocfail, ERR_peek_last_error()))
  197. return 0;
  198. /* Setting and clearing a mark should not affect the errors on the stack */
  199. if (!TEST_true(ERR_set_mark()))
  200. return 0;
  201. ERR_raise(ERR_LIB_CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  202. if (!TEST_true(ERR_clear_last_mark())
  203. || !TEST_ulong_eq(shouldnot, ERR_peek_last_error()))
  204. return 0;
  205. /*
  206. * Popping where no mark has been set should pop everything - but return
  207. * a failure result
  208. */
  209. if (!TEST_false(ERR_pop_to_mark())
  210. || !TEST_ulong_eq(0, ERR_peek_last_error()))
  211. return 0;
  212. /* Clearing where there is no mark should fail */
  213. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  214. if (!TEST_false(ERR_clear_last_mark())
  215. /* "get" the last error to remove it */
  216. || !TEST_ulong_eq(mallocfail, ERR_get_error())
  217. || !TEST_ulong_eq(0, ERR_peek_last_error()))
  218. return 0;
  219. /*
  220. * Setting a mark where there are no errors in the stack should fail.
  221. * NOTE: This is somewhat surprising behaviour but is historically how this
  222. * function behaves. In practice we typically set marks without first
  223. * checking whether there is anything on the stack - but we also don't
  224. * tend to check the success of this function. It turns out to work anyway
  225. * because although setting a mark with no errors fails, a subsequent call
  226. * to ERR_pop_to_mark() or ERR_clear_last_mark() will do the right thing
  227. * anyway (even though they will report a failure result).
  228. */
  229. if (!TEST_false(ERR_set_mark()))
  230. return 0;
  231. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  232. if (!TEST_true(ERR_set_mark()))
  233. return 0;
  234. ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
  235. ERR_raise(ERR_LIB_CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  236. /* Should be able to "pop" past 2 errors */
  237. if (!TEST_true(ERR_pop_to_mark())
  238. || !TEST_ulong_eq(mallocfail, ERR_peek_last_error()))
  239. return 0;
  240. if (!TEST_true(ERR_set_mark()))
  241. return 0;
  242. ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
  243. ERR_raise(ERR_LIB_CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  244. /* Should be able to "clear" past 2 errors */
  245. if (!TEST_true(ERR_clear_last_mark())
  246. || !TEST_ulong_eq(shouldnot, ERR_peek_last_error()))
  247. return 0;
  248. /* Clear remaining errors from last test */
  249. ERR_clear_error();
  250. return 1;
  251. }
  252. static int test_clear_error(void)
  253. {
  254. int flags = -1;
  255. const char *data = NULL;
  256. int res = 0;
  257. /* Raise an error with data and clear it */
  258. ERR_raise_data(0, 0, "hello %s", "world");
  259. ERR_peek_error_data(&data, &flags);
  260. if (!TEST_str_eq(data, "hello world")
  261. || !TEST_int_eq(flags, ERR_TXT_STRING | ERR_TXT_MALLOCED))
  262. goto err;
  263. ERR_clear_error();
  264. /* Raise a new error without data */
  265. ERR_raise(0, 0);
  266. ERR_peek_error_data(&data, &flags);
  267. if (!TEST_str_eq(data, "")
  268. || !TEST_int_eq(flags, ERR_TXT_MALLOCED))
  269. goto err;
  270. ERR_clear_error();
  271. /* Raise a new error with data */
  272. ERR_raise_data(0, 0, "goodbye %s world", "cruel");
  273. ERR_peek_error_data(&data, &flags);
  274. if (!TEST_str_eq(data, "goodbye cruel world")
  275. || !TEST_int_eq(flags, ERR_TXT_STRING | ERR_TXT_MALLOCED))
  276. goto err;
  277. ERR_clear_error();
  278. /*
  279. * Raise a new error without data to check that the malloced storage
  280. * is freed properly
  281. */
  282. ERR_raise(0, 0);
  283. ERR_peek_error_data(&data, &flags);
  284. if (!TEST_str_eq(data, "")
  285. || !TEST_int_eq(flags, ERR_TXT_MALLOCED))
  286. goto err;
  287. ERR_clear_error();
  288. res = 1;
  289. err:
  290. ERR_clear_error();
  291. return res;
  292. }
  293. /*
  294. * Test saving and restoring error state.
  295. * Test 0: Save using OSSL_ERR_STATE_save()
  296. * Test 1: Save using OSSL_ERR_STATE_save_to_mark()
  297. */
  298. static int test_save_restore(int idx)
  299. {
  300. ERR_STATE *es;
  301. int res = 0, i, flags = -1;
  302. unsigned long mallocfail, interr;
  303. static const char testdata[] = "test data";
  304. const char *data = NULL;
  305. if (!TEST_ptr(es = OSSL_ERR_STATE_new()))
  306. goto err;
  307. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  308. mallocfail = ERR_peek_last_error();
  309. if (!TEST_ulong_gt(mallocfail, 0))
  310. goto err;
  311. if (idx == 1 && !TEST_int_eq(ERR_set_mark(), 1))
  312. goto err;
  313. ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR, testdata);
  314. interr = ERR_peek_last_error();
  315. if (!TEST_ulong_ne(mallocfail, ERR_peek_last_error()))
  316. goto err;
  317. if (idx == 0) {
  318. OSSL_ERR_STATE_save(es);
  319. if (!TEST_ulong_eq(ERR_peek_last_error(), 0))
  320. goto err;
  321. } else {
  322. OSSL_ERR_STATE_save_to_mark(es);
  323. if (!TEST_ulong_ne(ERR_peek_last_error(), 0))
  324. goto err;
  325. }
  326. for (i = 0; i < 2; i++) {
  327. OSSL_ERR_STATE_restore(es);
  328. if (!TEST_ulong_eq(ERR_peek_last_error(), interr))
  329. goto err;
  330. ERR_peek_last_error_data(&data, &flags);
  331. if (!TEST_str_eq(data, testdata)
  332. || !TEST_int_eq(flags, ERR_TXT_STRING | ERR_TXT_MALLOCED))
  333. goto err;
  334. /* restore again to duplicate the entries */
  335. OSSL_ERR_STATE_restore(es);
  336. /* verify them all */
  337. if (idx == 0 || i == 0) {
  338. if (!TEST_ulong_eq(ERR_get_error_all(NULL, NULL, NULL,
  339. &data, &flags), mallocfail)
  340. || !TEST_int_ne(flags, ERR_TXT_STRING | ERR_TXT_MALLOCED))
  341. goto err;
  342. }
  343. if (!TEST_ulong_eq(ERR_get_error_all(NULL, NULL, NULL,
  344. &data, &flags), interr)
  345. || !TEST_str_eq(data, testdata)
  346. || !TEST_int_eq(flags, ERR_TXT_STRING | ERR_TXT_MALLOCED))
  347. goto err;
  348. if (idx == 0) {
  349. if (!TEST_ulong_eq(ERR_get_error_all(NULL, NULL, NULL,
  350. &data, &flags), mallocfail)
  351. || !TEST_int_ne(flags, ERR_TXT_STRING | ERR_TXT_MALLOCED))
  352. goto err;
  353. }
  354. if (!TEST_ulong_eq(ERR_get_error_all(NULL, NULL, NULL,
  355. &data, &flags), interr)
  356. || !TEST_str_eq(data, testdata)
  357. || !TEST_int_eq(flags, ERR_TXT_STRING | ERR_TXT_MALLOCED))
  358. goto err;
  359. if (!TEST_ulong_eq(ERR_get_error(), 0))
  360. goto err;
  361. }
  362. res = 1;
  363. err:
  364. OSSL_ERR_STATE_free(es);
  365. return res;
  366. }
  367. int setup_tests(void)
  368. {
  369. ADD_TEST(preserves_system_error);
  370. ADD_TEST(vdata_appends);
  371. ADD_TEST(raised_error);
  372. #ifndef OPENSSL_NO_DEPRECATED_3_0
  373. ADD_TEST(test_print_error_format);
  374. #endif
  375. ADD_TEST(test_marks);
  376. ADD_ALL_TESTS(test_save_restore, 2);
  377. ADD_TEST(test_clear_error);
  378. return 1;
  379. }