errtest.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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_get_error_line_data(NULL, NULL, &data, NULL);
  38. return TEST_str_eq(data, "hello world");
  39. }
  40. /* Test that setting a platform error sets the right values. */
  41. static int platform_error(void)
  42. {
  43. const char *file, *f, *data;
  44. int line;
  45. int l;
  46. unsigned long e;
  47. file = __FILE__;
  48. line = __LINE__ + 1; /* The error is generated on the next line */
  49. ERR_raise_data(ERR_LIB_SYS, ERR_R_INTERNAL_ERROR,
  50. "calling exit()");
  51. if (!TEST_ulong_ne(e = ERR_get_error_line_data(&f, &l, &data, NULL), 0)
  52. || !TEST_int_eq(ERR_GET_REASON(e), ERR_R_INTERNAL_ERROR)
  53. || !TEST_int_eq(l, line)
  54. || !TEST_str_eq(f, file)
  55. || !TEST_str_eq(data, "calling exit()"))
  56. return 0;
  57. return 1;
  58. }
  59. int setup_tests(void)
  60. {
  61. ADD_TEST(preserves_system_error);
  62. ADD_TEST(vdata_appends);
  63. ADD_TEST(platform_error);
  64. return 1;
  65. }