errtest.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright 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. #include <openssl/opensslconf.h>
  10. #include <openssl/err.h>
  11. #include "testutil.h"
  12. #if defined(OPENSSL_SYS_WINDOWS)
  13. # include <windows.h>
  14. #else
  15. # include <errno.h>
  16. #endif
  17. /* Test that querying the error queue preserves the OS error. */
  18. static int preserves_system_error(void)
  19. {
  20. #if defined(OPENSSL_SYS_WINDOWS)
  21. SetLastError(ERROR_INVALID_FUNCTION);
  22. ERR_get_error();
  23. return TEST_int_eq(GetLastError(), ERROR_INVALID_FUNCTION);
  24. #else
  25. errno = EINVAL;
  26. ERR_get_error();
  27. return TEST_int_eq(errno, EINVAL);
  28. #endif
  29. }
  30. /* Test that calls to ERR_add_error_[v]data append */
  31. static int vdata_appends(void)
  32. {
  33. const char *data;
  34. CRYPTOerr(0, ERR_R_MALLOC_FAILURE);
  35. ERR_add_error_data(1, "hello ");
  36. ERR_add_error_data(1, "world");
  37. ERR_peek_error_data(&data, NULL);
  38. return TEST_str_eq(data, "hello world");
  39. }
  40. static int raised_error(void)
  41. {
  42. const char *f, *data;
  43. int l;
  44. unsigned long e;
  45. /*
  46. * When OPENSSL_NO_ERR or OPENSSL_NO_FILENAMES, no file name or line
  47. * number is saved, so no point checking them.
  48. */
  49. #if !defined(OPENSSL_NO_FILENAMES) && !defined(OPENSSL_NO_ERR)
  50. const char *file;
  51. int line;
  52. file = __FILE__;
  53. line = __LINE__ + 2; /* The error is generated on the ERR_raise_data line */
  54. #endif
  55. ERR_raise_data(ERR_LIB_SYS, ERR_R_INTERNAL_ERROR,
  56. "calling exit()");
  57. if (!TEST_ulong_ne(e = ERR_get_error_all(&f, &l, NULL, &data, NULL), 0)
  58. || !TEST_int_eq(ERR_GET_REASON(e), ERR_R_INTERNAL_ERROR)
  59. #if !defined(OPENSSL_NO_FILENAMES) && !defined(OPENSSL_NO_ERR)
  60. || !TEST_int_eq(l, line)
  61. || !TEST_str_eq(f, file)
  62. #endif
  63. || !TEST_str_eq(data, "calling exit()"))
  64. return 0;
  65. return 1;
  66. }
  67. int setup_tests(void)
  68. {
  69. ADD_TEST(preserves_system_error);
  70. ADD_TEST(vdata_appends);
  71. ADD_TEST(raised_error);
  72. return 1;
  73. }