You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

err_test.cc 3.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* Copyright (c) 2014, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <openssl/crypto.h>
  17. #include <openssl/err.h>
  18. #include <openssl/mem.h>
  19. static bool TestOverflow() {
  20. for (unsigned i = 0; i < ERR_NUM_ERRORS*2; i++) {
  21. ERR_put_error(1, 2, i+1, "test", 1);
  22. }
  23. for (unsigned i = 0; i < ERR_NUM_ERRORS - 1; i++) {
  24. uint32_t err = ERR_get_error();
  25. /* Errors are returned in order they were pushed, with the least recent ones
  26. * removed, up to |ERR_NUM_ERRORS - 1| errors. So the errors returned are
  27. * |ERR_NUM_ERRORS + 2| through |ERR_NUM_ERRORS * 2|, inclusive. */
  28. if (err == 0 || ERR_GET_REASON(err) != i + ERR_NUM_ERRORS + 2) {
  29. fprintf(stderr, "ERR_get_error failed at %u\n", i);
  30. return false;
  31. }
  32. }
  33. if (ERR_get_error() != 0) {
  34. fprintf(stderr, "ERR_get_error more than the expected number of values.\n");
  35. return false;
  36. }
  37. return true;
  38. }
  39. static bool TestPutError() {
  40. if (ERR_get_error() != 0) {
  41. fprintf(stderr, "ERR_get_error returned value before an error was added.\n");
  42. return false;
  43. }
  44. ERR_put_error(1, 2, 3, "test", 4);
  45. ERR_add_error_data(1, "testing");
  46. int peeked_line, line, peeked_flags, flags;
  47. const char *peeked_file, *file, *peeked_data, *data;
  48. uint32_t peeked_packed_error =
  49. ERR_peek_error_line_data(&peeked_file, &peeked_line, &peeked_data,
  50. &peeked_flags);
  51. uint32_t packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
  52. if (peeked_packed_error != packed_error ||
  53. peeked_file != file ||
  54. peeked_data != data ||
  55. peeked_flags != flags) {
  56. fprintf(stderr, "Bad peeked error data returned.\n");
  57. return false;
  58. }
  59. if (strcmp(file, "test") != 0 ||
  60. line != 4 ||
  61. (flags & ERR_FLAG_STRING) == 0 ||
  62. ERR_GET_LIB(packed_error) != 1 ||
  63. ERR_GET_FUNC(packed_error) != 2 ||
  64. ERR_GET_REASON(packed_error) != 3 ||
  65. strcmp(data, "testing") != 0) {
  66. fprintf(stderr, "Bad error data returned.\n");
  67. return false;
  68. }
  69. return true;
  70. }
  71. static bool TestClearError() {
  72. if (ERR_get_error() != 0) {
  73. fprintf(stderr, "ERR_get_error returned value before an error was added.\n");
  74. return false;
  75. }
  76. ERR_put_error(1, 2, 3, "test", 4);
  77. ERR_clear_error();
  78. if (ERR_get_error() != 0) {
  79. fprintf(stderr, "Error remained after clearing.\n");
  80. return false;
  81. }
  82. return true;
  83. }
  84. static bool TestPrint() {
  85. ERR_put_error(1, 2, 3, "test", 4);
  86. ERR_add_error_data(1, "testing");
  87. uint32_t packed_error = ERR_get_error();
  88. char buf[256];
  89. for (size_t i = 0; i <= sizeof(buf); i++) {
  90. ERR_error_string_n(packed_error, buf, i);
  91. }
  92. return true;
  93. }
  94. static bool TestRelease() {
  95. ERR_put_error(1, 2, 3, "test", 4);
  96. ERR_remove_thread_state(NULL);
  97. return true;
  98. }
  99. int main() {
  100. CRYPTO_library_init();
  101. if (!TestOverflow() ||
  102. !TestPutError() ||
  103. !TestClearError() ||
  104. !TestPrint() ||
  105. !TestRelease()) {
  106. return 1;
  107. }
  108. printf("PASS\n");
  109. return 0;
  110. }