Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

163 Zeilen
4.4 KiB

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