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.
 
 
 
 
 
 

140 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 <openssl/crypto.h>
  16. #include <openssl/err.h>
  17. #include <openssl/mem.h>
  18. static int test_overflow(void) {
  19. unsigned i;
  20. for (i = 0; i < ERR_NUM_ERRORS*2; i++) {
  21. ERR_put_error(1, 2, i+1, "test", 1);
  22. }
  23. for (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 0;
  31. }
  32. }
  33. if (ERR_get_error() != 0) {
  34. fprintf(stderr, "ERR_get_error more than the expected number of values.\n");
  35. return 0;
  36. }
  37. return 1;
  38. }
  39. static int test_put_error(void) {
  40. uint32_t peeked_packed_error, packed_error;
  41. int peeked_line, line, peeked_flags, flags;
  42. const char *peeked_file, *file, *peeked_data, *data;
  43. if (ERR_get_error() != 0) {
  44. fprintf(stderr, "ERR_get_error returned value before an error was added.\n");
  45. return 0;
  46. }
  47. ERR_put_error(1, 2, 3, "test", 4);
  48. ERR_add_error_data(1, "testing");
  49. peeked_packed_error = ERR_peek_error_line_data(&peeked_file, &peeked_line,
  50. &peeked_data, &peeked_flags);
  51. 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 0;
  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 0;
  68. }
  69. return 1;
  70. }
  71. static int test_clear_error(void) {
  72. if (ERR_get_error() != 0) {
  73. fprintf(stderr, "ERR_get_error returned value before an error was added.\n");
  74. return 0;
  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 0;
  81. }
  82. return 1;
  83. }
  84. static int test_print(void) {
  85. size_t i;
  86. char buf[256];
  87. uint32_t packed_error;
  88. ERR_put_error(1, 2, 3, "test", 4);
  89. ERR_add_error_data(1, "testing");
  90. packed_error = ERR_get_error();
  91. for (i = 0; i <= sizeof(buf); i++) {
  92. ERR_error_string_n(packed_error, buf, i);
  93. }
  94. return 1;
  95. }
  96. static int test_release(void) {
  97. ERR_put_error(1, 2, 3, "test", 4);
  98. ERR_remove_thread_state(NULL);
  99. return 1;
  100. }
  101. int main(void) {
  102. CRYPTO_library_init();
  103. if (!test_overflow() ||
  104. !test_put_error() ||
  105. !test_clear_error() ||
  106. !test_print() ||
  107. !test_release()) {
  108. return 1;
  109. }
  110. printf("PASS\n");
  111. return 0;
  112. }