errtest.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. const char *func = OPENSSL_FUNC;
  25. # ifndef OPENSSL_NO_FILENAMES
  26. const char *file = OPENSSL_FILE;
  27. const int line = OPENSSL_LINE;
  28. # else
  29. const char *file = "";
  30. const int line = 0;
  31. # endif
  32. /* The format for OpenSSL error lines */
  33. const char *expected_format = ":error::system library:%s:%s:%s:%d";
  34. /*-
  35. * ^^ ^^ ^^ ^^
  36. * function name -------------------------------------++ || || ||
  37. * reason string (system error string) ------------------++ || ||
  38. * file name -----------------------------------------------++ ||
  39. * line number ------------------------------------------------++
  40. */
  41. char expected[512];
  42. char *out = NULL, *p = NULL;
  43. int ret = 0, len;
  44. BIO *bio = NULL;
  45. const int syserr = EPERM;
  46. int reasoncode;
  47. /*
  48. * We set a mark here so we can clear the system error that we generate
  49. * with ERR_PUT_error(). That is, after all, just a simulation to verify
  50. * ERR_print_errors() output, not a real error.
  51. */
  52. ERR_set_mark();
  53. ERR_PUT_error(ERR_LIB_SYS, 0, syserr, file, line);
  54. reasoncode = ERR_GET_REASON(ERR_peek_error());
  55. if (!TEST_int_eq(reasoncode, syserr)) {
  56. ERR_pop_to_mark();
  57. goto err;
  58. }
  59. BIO_snprintf(expected, sizeof(expected), expected_format,
  60. func, strerror(syserr), file, line);
  61. if (!TEST_ptr(bio = BIO_new(BIO_s_mem())))
  62. goto err;
  63. ERR_print_errors(bio);
  64. if (!TEST_int_gt(len = BIO_get_mem_data(bio, &out), 0))
  65. goto err;
  66. /* Skip over the variable thread id at the start of the string */
  67. for (p = out; *p != ':' && *p != 0; ++p) {
  68. if (!TEST_true(IS_HEX(*p)))
  69. goto err;
  70. }
  71. if (!TEST_true(*p != 0)
  72. || !TEST_strn_eq(expected, p, strlen(expected)))
  73. goto err;
  74. ret = 1;
  75. err:
  76. BIO_free(bio);
  77. return ret;
  78. }
  79. #endif
  80. /* Test that querying the error queue preserves the OS error. */
  81. static int preserves_system_error(void)
  82. {
  83. #if defined(OPENSSL_SYS_WINDOWS)
  84. SetLastError(ERROR_INVALID_FUNCTION);
  85. ERR_get_error();
  86. return TEST_int_eq(GetLastError(), ERROR_INVALID_FUNCTION);
  87. #else
  88. errno = EINVAL;
  89. ERR_get_error();
  90. return TEST_int_eq(errno, EINVAL);
  91. #endif
  92. }
  93. /* Test that calls to ERR_add_error_[v]data append */
  94. static int vdata_appends(void)
  95. {
  96. const char *data;
  97. CRYPTOerr(0, ERR_R_MALLOC_FAILURE);
  98. ERR_add_error_data(1, "hello ");
  99. ERR_add_error_data(1, "world");
  100. ERR_peek_error_data(&data, NULL);
  101. return TEST_str_eq(data, "hello world");
  102. }
  103. static int raised_error(void)
  104. {
  105. const char *f, *data;
  106. int l;
  107. unsigned long e;
  108. /*
  109. * When OPENSSL_NO_ERR or OPENSSL_NO_FILENAMES, no file name or line
  110. * number is saved, so no point checking them.
  111. */
  112. #if !defined(OPENSSL_NO_FILENAMES) && !defined(OPENSSL_NO_ERR)
  113. const char *file;
  114. int line;
  115. file = __FILE__;
  116. line = __LINE__ + 2; /* The error is generated on the ERR_raise_data line */
  117. #endif
  118. ERR_raise_data(ERR_LIB_NONE, ERR_R_INTERNAL_ERROR,
  119. "calling exit()");
  120. if (!TEST_ulong_ne(e = ERR_get_error_all(&f, &l, NULL, &data, NULL), 0)
  121. || !TEST_int_eq(ERR_GET_REASON(e), ERR_R_INTERNAL_ERROR)
  122. #if !defined(OPENSSL_NO_FILENAMES) && !defined(OPENSSL_NO_ERR)
  123. || !TEST_int_eq(l, line)
  124. || !TEST_str_eq(f, file)
  125. #endif
  126. || !TEST_str_eq(data, "calling exit()"))
  127. return 0;
  128. return 1;
  129. }
  130. int setup_tests(void)
  131. {
  132. ADD_TEST(preserves_system_error);
  133. ADD_TEST(vdata_appends);
  134. ADD_TEST(raised_error);
  135. #ifndef OPENSSL_NO_DEPRECATED_3_0
  136. ADD_TEST(test_print_error_format);
  137. #endif
  138. return 1;
  139. }