errtest.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * Copyright 2018-2020 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. # ifndef OPENSSL_NO_ERR
  68. lib = "system library";
  69. reason = strerror(syserr);
  70. # else
  71. lib = "lib(2)";
  72. BIO_snprintf(reasonbuf, sizeof(reasonbuf), "reason(%lu)", reasoncode);
  73. reason = reasonbuf;
  74. # endif
  75. BIO_snprintf(expected, sizeof(expected), expected_format,
  76. errorcode, lib, func, reason, file, line);
  77. if (!TEST_ptr(bio = BIO_new(BIO_s_mem())))
  78. goto err;
  79. ERR_print_errors(bio);
  80. if (!TEST_int_gt(len = BIO_get_mem_data(bio, &out), 0))
  81. goto err;
  82. /* Skip over the variable thread id at the start of the string */
  83. for (p = out; *p != ':' && *p != 0; ++p) {
  84. if (!TEST_true(IS_HEX(*p)))
  85. goto err;
  86. }
  87. if (!TEST_true(*p != 0)
  88. || !TEST_strn_eq(expected, p, strlen(expected)))
  89. goto err;
  90. ret = 1;
  91. err:
  92. BIO_free(bio);
  93. return ret;
  94. }
  95. #endif
  96. /* Test that querying the error queue preserves the OS error. */
  97. static int preserves_system_error(void)
  98. {
  99. #if defined(OPENSSL_SYS_WINDOWS)
  100. SetLastError(ERROR_INVALID_FUNCTION);
  101. ERR_get_error();
  102. return TEST_int_eq(GetLastError(), ERROR_INVALID_FUNCTION);
  103. #else
  104. errno = EINVAL;
  105. ERR_get_error();
  106. return TEST_int_eq(errno, EINVAL);
  107. #endif
  108. }
  109. /* Test that calls to ERR_add_error_[v]data append */
  110. static int vdata_appends(void)
  111. {
  112. const char *data;
  113. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  114. ERR_add_error_data(1, "hello ");
  115. ERR_add_error_data(1, "world");
  116. ERR_peek_error_data(&data, NULL);
  117. return TEST_str_eq(data, "hello world");
  118. }
  119. static int raised_error(void)
  120. {
  121. const char *f, *data;
  122. int l;
  123. unsigned long e;
  124. /*
  125. * When OPENSSL_NO_ERR or OPENSSL_NO_FILENAMES, no file name or line
  126. * number is saved, so no point checking them.
  127. */
  128. #if !defined(OPENSSL_NO_FILENAMES) && !defined(OPENSSL_NO_ERR)
  129. const char *file;
  130. int line;
  131. file = __FILE__;
  132. line = __LINE__ + 2; /* The error is generated on the ERR_raise_data line */
  133. #endif
  134. ERR_raise_data(ERR_LIB_NONE, ERR_R_INTERNAL_ERROR,
  135. "calling exit()");
  136. if (!TEST_ulong_ne(e = ERR_get_error_all(&f, &l, NULL, &data, NULL), 0)
  137. || !TEST_int_eq(ERR_GET_REASON(e), ERR_R_INTERNAL_ERROR)
  138. #if !defined(OPENSSL_NO_FILENAMES) && !defined(OPENSSL_NO_ERR)
  139. || !TEST_int_eq(l, line)
  140. || !TEST_str_eq(f, file)
  141. #endif
  142. || !TEST_str_eq(data, "calling exit()"))
  143. return 0;
  144. return 1;
  145. }
  146. static int test_marks(void)
  147. {
  148. unsigned long mallocfail, shouldnot;
  149. /* Set an initial error */
  150. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  151. mallocfail = ERR_peek_last_error();
  152. if (!TEST_ulong_gt(mallocfail, 0))
  153. return 0;
  154. /* Setting and clearing a mark should not affect the error */
  155. if (!TEST_true(ERR_set_mark())
  156. || !TEST_true(ERR_pop_to_mark())
  157. || !TEST_ulong_eq(mallocfail, ERR_peek_last_error())
  158. || !TEST_true(ERR_set_mark())
  159. || !TEST_true(ERR_clear_last_mark())
  160. || !TEST_ulong_eq(mallocfail, ERR_peek_last_error()))
  161. return 0;
  162. /* Test popping errors */
  163. if (!TEST_true(ERR_set_mark()))
  164. return 0;
  165. ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
  166. if (!TEST_ulong_ne(mallocfail, ERR_peek_last_error())
  167. || !TEST_true(ERR_pop_to_mark())
  168. || !TEST_ulong_eq(mallocfail, ERR_peek_last_error()))
  169. return 0;
  170. /* Nested marks should also work */
  171. if (!TEST_true(ERR_set_mark())
  172. || !TEST_true(ERR_set_mark()))
  173. return 0;
  174. ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
  175. if (!TEST_ulong_ne(mallocfail, ERR_peek_last_error())
  176. || !TEST_true(ERR_pop_to_mark())
  177. || !TEST_true(ERR_pop_to_mark())
  178. || !TEST_ulong_eq(mallocfail, ERR_peek_last_error()))
  179. return 0;
  180. if (!TEST_true(ERR_set_mark()))
  181. return 0;
  182. ERR_raise(ERR_LIB_CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  183. shouldnot = ERR_peek_last_error();
  184. if (!TEST_ulong_ne(mallocfail, shouldnot)
  185. || !TEST_true(ERR_set_mark()))
  186. return 0;
  187. ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
  188. if (!TEST_ulong_ne(shouldnot, ERR_peek_last_error())
  189. || !TEST_true(ERR_pop_to_mark())
  190. || !TEST_ulong_eq(shouldnot, ERR_peek_last_error())
  191. || !TEST_true(ERR_pop_to_mark())
  192. || !TEST_ulong_eq(mallocfail, ERR_peek_last_error()))
  193. return 0;
  194. /* Setting and clearing a mark should not affect the errors on the stack */
  195. if (!TEST_true(ERR_set_mark()))
  196. return 0;
  197. ERR_raise(ERR_LIB_CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  198. if (!TEST_true(ERR_clear_last_mark())
  199. || !TEST_ulong_eq(shouldnot, ERR_peek_last_error()))
  200. return 0;
  201. /*
  202. * Popping where no mark has been set should pop everything - but return
  203. * a failure result
  204. */
  205. if (!TEST_false(ERR_pop_to_mark())
  206. || !TEST_ulong_eq(0, ERR_peek_last_error()))
  207. return 0;
  208. /* Clearing where there is no mark should fail */
  209. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  210. if (!TEST_false(ERR_clear_last_mark())
  211. /* "get" the last error to remove it */
  212. || !TEST_ulong_eq(mallocfail, ERR_get_error())
  213. || !TEST_ulong_eq(0, ERR_peek_last_error()))
  214. return 0;
  215. /*
  216. * Setting a mark where there are no errors in the stack should fail.
  217. * NOTE: This is somewhat surprising behaviour but is historically how this
  218. * function behaves. In practice we typically set marks without first
  219. * checking whether there is anything on the stack - but we also don't
  220. * tend to check the success of this function. It turns out to work anyway
  221. * because although setting a mark with no errors fails, a subsequent call
  222. * to ERR_pop_to_mark() or ERR_clear_last_mark() will do the right thing
  223. * anyway (even though they will report a failure result).
  224. */
  225. if (!TEST_false(ERR_set_mark()))
  226. return 0;
  227. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  228. if (!TEST_true(ERR_set_mark()))
  229. return 0;
  230. ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
  231. ERR_raise(ERR_LIB_CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  232. /* Should be able to "pop" past 2 errors */
  233. if (!TEST_true(ERR_pop_to_mark())
  234. || !TEST_ulong_eq(mallocfail, ERR_peek_last_error()))
  235. return 0;
  236. if (!TEST_true(ERR_set_mark()))
  237. return 0;
  238. ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
  239. ERR_raise(ERR_LIB_CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  240. /* Should be able to "clear" past 2 errors */
  241. if (!TEST_true(ERR_clear_last_mark())
  242. || !TEST_ulong_eq(shouldnot, ERR_peek_last_error()))
  243. return 0;
  244. /* Clear remaining errors from last test */
  245. ERR_clear_error();
  246. return 1;
  247. }
  248. int setup_tests(void)
  249. {
  250. ADD_TEST(preserves_system_error);
  251. ADD_TEST(vdata_appends);
  252. ADD_TEST(raised_error);
  253. #ifndef OPENSSL_NO_DEPRECATED_3_0
  254. ADD_TEST(test_print_error_format);
  255. #endif
  256. ADD_TEST(test_marks);
  257. return 1;
  258. }