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.
 
 
 
 
 
 

141 lines
3.7 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 int test_overflow(void) {
  20. unsigned i;
  21. for (i = 0; i < ERR_NUM_ERRORS*2; i++) {
  22. ERR_put_error(1, 2, i+1, "test", 1);
  23. }
  24. for (i = 0; i < ERR_NUM_ERRORS - 1; i++) {
  25. uint32_t err = ERR_get_error();
  26. /* Errors are returned in order they were pushed, with the least recent ones
  27. * removed, up to |ERR_NUM_ERRORS - 1| errors. So the errors returned are
  28. * |ERR_NUM_ERRORS + 2| through |ERR_NUM_ERRORS * 2|, inclusive. */
  29. if (err == 0 || ERR_GET_REASON(err) != i + ERR_NUM_ERRORS + 2) {
  30. fprintf(stderr, "ERR_get_error failed at %u\n", i);
  31. return 0;
  32. }
  33. }
  34. if (ERR_get_error() != 0) {
  35. fprintf(stderr, "ERR_get_error more than the expected number of values.\n");
  36. return 0;
  37. }
  38. return 1;
  39. }
  40. static int test_put_error(void) {
  41. uint32_t peeked_packed_error, packed_error;
  42. int peeked_line, line, peeked_flags, flags;
  43. const char *peeked_file, *file, *peeked_data, *data;
  44. if (ERR_get_error() != 0) {
  45. fprintf(stderr, "ERR_get_error returned value before an error was added.\n");
  46. return 0;
  47. }
  48. ERR_put_error(1, 2, 3, "test", 4);
  49. ERR_add_error_data(1, "testing");
  50. peeked_packed_error = ERR_peek_error_line_data(&peeked_file, &peeked_line,
  51. &peeked_data, &peeked_flags);
  52. 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 0;
  59. }
  60. if (strcmp(file, "test") != 0 ||
  61. line != 4 ||
  62. (flags & ERR_FLAG_STRING) == 0 ||
  63. ERR_GET_LIB(packed_error) != 1 ||
  64. ERR_GET_FUNC(packed_error) != 2 ||
  65. ERR_GET_REASON(packed_error) != 3 ||
  66. strcmp(data, "testing") != 0) {
  67. fprintf(stderr, "Bad error data returned.\n");
  68. return 0;
  69. }
  70. return 1;
  71. }
  72. static int test_clear_error(void) {
  73. if (ERR_get_error() != 0) {
  74. fprintf(stderr, "ERR_get_error returned value before an error was added.\n");
  75. return 0;
  76. }
  77. ERR_put_error(1, 2, 3, "test", 4);
  78. ERR_clear_error();
  79. if (ERR_get_error() != 0) {
  80. fprintf(stderr, "Error remained after clearing.\n");
  81. return 0;
  82. }
  83. return 1;
  84. }
  85. static int test_print(void) {
  86. size_t i;
  87. char buf[256];
  88. uint32_t packed_error;
  89. ERR_put_error(1, 2, 3, "test", 4);
  90. ERR_add_error_data(1, "testing");
  91. packed_error = ERR_get_error();
  92. for (i = 0; i <= sizeof(buf); i++) {
  93. ERR_error_string_n(packed_error, buf, i);
  94. }
  95. return 1;
  96. }
  97. static int test_release(void) {
  98. ERR_put_error(1, 2, 3, "test", 4);
  99. ERR_remove_thread_state(NULL);
  100. return 1;
  101. }
  102. int main(void) {
  103. CRYPTO_library_init();
  104. if (!test_overflow() ||
  105. !test_put_error() ||
  106. !test_clear_error() ||
  107. !test_print() ||
  108. !test_release()) {
  109. return 1;
  110. }
  111. printf("PASS\n");
  112. return 0;
  113. }