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.
 
 
 
 
 
 

122 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 <gtest/gtest.h>
  17. #include <openssl/crypto.h>
  18. #include <openssl/err.h>
  19. #include <openssl/mem.h>
  20. TEST(ErrTest, Overflow) {
  21. for (unsigned i = 0; i < ERR_NUM_ERRORS*2; i++) {
  22. ERR_put_error(1, 0 /* unused */, i+1, "test", 1);
  23. }
  24. for (unsigned i = 0; i < ERR_NUM_ERRORS - 1; i++) {
  25. SCOPED_TRACE(i);
  26. uint32_t err = ERR_get_error();
  27. /* Errors are returned in order they were pushed, with the least recent ones
  28. * removed, up to |ERR_NUM_ERRORS - 1| errors. So the errors returned are
  29. * |ERR_NUM_ERRORS + 2| through |ERR_NUM_ERRORS * 2|, inclusive. */
  30. EXPECT_NE(0u, err);
  31. EXPECT_EQ(static_cast<int>(i + ERR_NUM_ERRORS + 2), ERR_GET_REASON(err));
  32. }
  33. EXPECT_EQ(0u, ERR_get_error());
  34. }
  35. TEST(ErrTest, PutError) {
  36. ASSERT_EQ(0u, ERR_get_error())
  37. << "ERR_get_error returned value before an error was added.";
  38. ERR_put_error(1, 0 /* unused */, 2, "test", 4);
  39. ERR_add_error_data(1, "testing");
  40. int peeked_line, line, peeked_flags, flags;
  41. const char *peeked_file, *file, *peeked_data, *data;
  42. uint32_t peeked_packed_error =
  43. ERR_peek_error_line_data(&peeked_file, &peeked_line, &peeked_data,
  44. &peeked_flags);
  45. uint32_t packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
  46. EXPECT_EQ(peeked_packed_error, packed_error);
  47. EXPECT_EQ(peeked_file, file);
  48. EXPECT_EQ(peeked_data, data);
  49. EXPECT_EQ(peeked_flags, flags);
  50. EXPECT_STREQ("test", file);
  51. EXPECT_EQ(4, line);
  52. EXPECT_TRUE(flags & ERR_FLAG_STRING);
  53. EXPECT_EQ(1, ERR_GET_LIB(packed_error));
  54. EXPECT_EQ(2, ERR_GET_REASON(packed_error));
  55. EXPECT_STREQ("testing", data);
  56. }
  57. TEST(ErrTest, ClearError) {
  58. ASSERT_EQ(0u, ERR_get_error())
  59. << "ERR_get_error returned value before an error was added.";
  60. ERR_put_error(1, 0 /* unused */, 2, "test", 4);
  61. ERR_clear_error();
  62. // The error queue should be cleared.
  63. EXPECT_EQ(0u, ERR_get_error());
  64. }
  65. TEST(ErrTest, Print) {
  66. ERR_put_error(1, 0 /* unused */, 2, "test", 4);
  67. ERR_add_error_data(1, "testing");
  68. uint32_t packed_error = ERR_get_error();
  69. char buf[256];
  70. for (size_t i = 0; i <= sizeof(buf); i++) {
  71. ERR_error_string_n(packed_error, buf, i);
  72. }
  73. }
  74. TEST(ErrTest, Release) {
  75. ERR_put_error(1, 0 /* unused */, 2, "test", 4);
  76. ERR_remove_thread_state(NULL);
  77. // The error queue should be cleared.
  78. EXPECT_EQ(0u, ERR_get_error());
  79. }
  80. static bool HasSuffix(const char *str, const char *suffix) {
  81. size_t suffix_len = strlen(suffix);
  82. size_t str_len = strlen(str);
  83. if (str_len < suffix_len) {
  84. return false;
  85. }
  86. return strcmp(str + str_len - suffix_len, suffix) == 0;
  87. }
  88. TEST(ErrTest, PutMacro) {
  89. int expected_line = __LINE__ + 1;
  90. OPENSSL_PUT_ERROR(USER, ERR_R_INTERNAL_ERROR);
  91. int line;
  92. const char *file;
  93. uint32_t error = ERR_get_error_line(&file, &line);
  94. EXPECT_PRED2(HasSuffix, file, "err_test.cc");
  95. EXPECT_EQ(expected_line, line);
  96. EXPECT_EQ(ERR_LIB_USER, ERR_GET_LIB(error));
  97. EXPECT_EQ(ERR_R_INTERNAL_ERROR, ERR_GET_REASON(error));
  98. }