errtest.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * Copyright 2018-2021 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. int setup_tests(void)
  253. {
  254. ADD_TEST(preserves_system_error);
  255. ADD_TEST(vdata_appends);
  256. ADD_TEST(raised_error);
  257. #ifndef OPENSSL_NO_DEPRECATED_3_0
  258. ADD_TEST(test_print_error_format);
  259. #endif
  260. ADD_TEST(test_marks);
  261. return 1;
  262. }