No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

err_test.cc 4.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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, i+1, "function", "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, "function", "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. const char *function = ERR_peek_function();
  52. uint32_t packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
  53. if (peeked_packed_error != packed_error ||
  54. peeked_file != file ||
  55. peeked_data != data ||
  56. peeked_flags != flags) {
  57. fprintf(stderr, "Bad peeked error data returned.\n");
  58. return false;
  59. }
  60. if (strcmp(function, "function") != 0 ||
  61. strcmp(file, "test") != 0 ||
  62. line != 4 ||
  63. (flags & ERR_FLAG_STRING) == 0 ||
  64. ERR_GET_LIB(packed_error) != 1 ||
  65. ERR_GET_REASON(packed_error) != 2 ||
  66. strcmp(data, "testing") != 0) {
  67. fprintf(stderr, "Bad error data returned.\n");
  68. return false;
  69. }
  70. return true;
  71. }
  72. static bool TestClearError() {
  73. if (ERR_get_error() != 0) {
  74. fprintf(stderr, "ERR_get_error returned value before an error was added.\n");
  75. return false;
  76. }
  77. ERR_put_error(1, 2, "function", "test", 4);
  78. ERR_clear_error();
  79. if (ERR_get_error() != 0) {
  80. fprintf(stderr, "Error remained after clearing.\n");
  81. return false;
  82. }
  83. return true;
  84. }
  85. static bool TestPrint() {
  86. ERR_put_error(1, 2, "function", "test", 4);
  87. ERR_add_error_data(1, "testing");
  88. uint32_t packed_error = ERR_get_error();
  89. char buf[256];
  90. for (size_t i = 0; i <= sizeof(buf); i++) {
  91. ERR_error_string_n(packed_error, buf, i);
  92. }
  93. return true;
  94. }
  95. static bool TestRelease() {
  96. ERR_put_error(1, 2, "function", "test", 4);
  97. ERR_remove_thread_state(NULL);
  98. return true;
  99. }
  100. static bool HasSuffix(const char *str, const char *suffix) {
  101. size_t suffix_len = strlen(suffix);
  102. size_t str_len = strlen(str);
  103. if (str_len < suffix_len) {
  104. return false;
  105. }
  106. return strcmp(str + str_len - suffix_len, suffix) == 0;
  107. }
  108. static bool TestPutMacro() {
  109. int expected_line = __LINE__ + 1;
  110. OPENSSL_PUT_ERROR(USER, ERR_R_INTERNAL_ERROR);
  111. int line;
  112. const char *file;
  113. const char *function = ERR_peek_function();
  114. uint32_t error = ERR_get_error_line(&file, &line);
  115. if (strcmp(function, "TestPutMacro") != 0 ||
  116. !HasSuffix(file, "err_test.cc") ||
  117. line != expected_line ||
  118. ERR_GET_LIB(error) != ERR_LIB_USER ||
  119. ERR_GET_REASON(error) != ERR_R_INTERNAL_ERROR) {
  120. fprintf(stderr, "Bad error data returned.\n");
  121. return false;
  122. }
  123. return true;
  124. }
  125. int main() {
  126. CRYPTO_library_init();
  127. if (!TestOverflow() ||
  128. !TestPutError() ||
  129. !TestClearError() ||
  130. !TestPrint() ||
  131. !TestRelease() ||
  132. !TestPutMacro()) {
  133. return 1;
  134. }
  135. printf("PASS\n");
  136. return 0;
  137. }