Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

215 wiersze
7.0 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. #include "./internal.h"
  21. TEST(ErrTest, Overflow) {
  22. for (unsigned i = 0; i < ERR_NUM_ERRORS*2; i++) {
  23. ERR_put_error(1, 0 /* unused */, i+1, "test", 1);
  24. }
  25. for (unsigned i = 0; i < ERR_NUM_ERRORS - 1; i++) {
  26. SCOPED_TRACE(i);
  27. uint32_t err = ERR_get_error();
  28. // Errors are returned in order they were pushed, with the least recent ones
  29. // removed, up to |ERR_NUM_ERRORS - 1| errors. So the errors returned are
  30. // |ERR_NUM_ERRORS + 2| through |ERR_NUM_ERRORS * 2|, inclusive.
  31. EXPECT_NE(0u, err);
  32. EXPECT_EQ(static_cast<int>(i + ERR_NUM_ERRORS + 2), ERR_GET_REASON(err));
  33. }
  34. EXPECT_EQ(0u, ERR_get_error());
  35. }
  36. TEST(ErrTest, PutError) {
  37. ASSERT_EQ(0u, ERR_get_error())
  38. << "ERR_get_error returned value before an error was added.";
  39. ERR_put_error(1, 0 /* unused */, 2, "test", 4);
  40. ERR_add_error_data(1, "testing");
  41. int peeked_line, line, peeked_flags, flags;
  42. const char *peeked_file, *file, *peeked_data, *data;
  43. uint32_t peeked_packed_error =
  44. ERR_peek_error_line_data(&peeked_file, &peeked_line, &peeked_data,
  45. &peeked_flags);
  46. uint32_t packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
  47. EXPECT_EQ(peeked_packed_error, packed_error);
  48. EXPECT_EQ(peeked_file, file);
  49. EXPECT_EQ(peeked_data, data);
  50. EXPECT_EQ(peeked_flags, flags);
  51. EXPECT_STREQ("test", file);
  52. EXPECT_EQ(4, line);
  53. EXPECT_TRUE(flags & ERR_FLAG_STRING);
  54. EXPECT_EQ(1, ERR_GET_LIB(packed_error));
  55. EXPECT_EQ(2, ERR_GET_REASON(packed_error));
  56. EXPECT_STREQ("testing", data);
  57. }
  58. TEST(ErrTest, ClearError) {
  59. ASSERT_EQ(0u, ERR_get_error())
  60. << "ERR_get_error returned value before an error was added.";
  61. ERR_put_error(1, 0 /* unused */, 2, "test", 4);
  62. ERR_clear_error();
  63. // The error queue should be cleared.
  64. EXPECT_EQ(0u, ERR_get_error());
  65. }
  66. TEST(ErrTest, Print) {
  67. ERR_put_error(1, 0 /* unused */, 2, "test", 4);
  68. ERR_add_error_data(1, "testing");
  69. uint32_t packed_error = ERR_get_error();
  70. char buf[256];
  71. for (size_t i = 0; i <= sizeof(buf); i++) {
  72. ERR_error_string_n(packed_error, buf, i);
  73. }
  74. }
  75. TEST(ErrTest, Release) {
  76. ERR_put_error(1, 0 /* unused */, 2, "test", 4);
  77. ERR_remove_thread_state(NULL);
  78. // The error queue should be cleared.
  79. EXPECT_EQ(0u, ERR_get_error());
  80. }
  81. static bool HasSuffix(const char *str, const char *suffix) {
  82. size_t suffix_len = strlen(suffix);
  83. size_t str_len = strlen(str);
  84. if (str_len < suffix_len) {
  85. return false;
  86. }
  87. return strcmp(str + str_len - suffix_len, suffix) == 0;
  88. }
  89. TEST(ErrTest, PutMacro) {
  90. int expected_line = __LINE__ + 1;
  91. OPENSSL_PUT_ERROR(USER, ERR_R_INTERNAL_ERROR);
  92. int line;
  93. const char *file;
  94. uint32_t error = ERR_get_error_line(&file, &line);
  95. EXPECT_PRED2(HasSuffix, file, "err_test.cc");
  96. EXPECT_EQ(expected_line, line);
  97. EXPECT_EQ(ERR_LIB_USER, ERR_GET_LIB(error));
  98. EXPECT_EQ(ERR_R_INTERNAL_ERROR, ERR_GET_REASON(error));
  99. }
  100. TEST(ErrTest, SaveAndRestore) {
  101. // Restoring no state clears the error queue, including error data.
  102. ERR_put_error(1, 0 /* unused */, 1, "test1.c", 1);
  103. ERR_put_error(2, 0 /* unused */, 2, "test2.c", 2);
  104. ERR_add_error_data(1, "data1");
  105. ERR_restore_state(nullptr);
  106. EXPECT_EQ(0u, ERR_get_error());
  107. // Add some entries to the error queue and save it.
  108. ERR_put_error(1, 0 /* unused */, 1, "test1.c", 1);
  109. ERR_add_error_data(1, "data1");
  110. ERR_put_error(2, 0 /* unused */, 2, "test2.c", 2);
  111. ERR_put_error(3, 0 /* unused */, 3, "test3.c", 3);
  112. ERR_add_error_data(1, "data3");
  113. bssl::UniquePtr<ERR_SAVE_STATE> saved(ERR_save_state());
  114. ASSERT_TRUE(saved);
  115. // The existing error queue entries still exist.
  116. int line, flags;
  117. const char *file, *data;
  118. uint32_t packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
  119. EXPECT_EQ(ERR_GET_LIB(packed_error), 1);
  120. EXPECT_EQ(ERR_GET_REASON(packed_error), 1);
  121. EXPECT_STREQ("test1.c", file);
  122. EXPECT_EQ(line, 1);
  123. EXPECT_STREQ(data, "data1");
  124. EXPECT_EQ(flags, ERR_FLAG_STRING);
  125. // The state may be restored, both over an empty and non-empty state.
  126. for (unsigned i = 0; i < 2; i++) {
  127. SCOPED_TRACE(i);
  128. ERR_restore_state(saved.get());
  129. packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
  130. EXPECT_EQ(ERR_GET_LIB(packed_error), 1);
  131. EXPECT_EQ(ERR_GET_REASON(packed_error), 1);
  132. EXPECT_STREQ("test1.c", file);
  133. EXPECT_EQ(line, 1);
  134. EXPECT_STREQ(data, "data1");
  135. EXPECT_EQ(flags, ERR_FLAG_STRING);
  136. packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
  137. EXPECT_EQ(ERR_GET_LIB(packed_error), 2);
  138. EXPECT_EQ(ERR_GET_REASON(packed_error), 2);
  139. EXPECT_STREQ("test2.c", file);
  140. EXPECT_EQ(line, 2);
  141. EXPECT_STREQ(data, ""); // No error data is reported as the empty string.
  142. EXPECT_EQ(flags, 0);
  143. packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
  144. EXPECT_EQ(ERR_GET_LIB(packed_error), 3);
  145. EXPECT_EQ(ERR_GET_REASON(packed_error), 3);
  146. EXPECT_STREQ("test3.c", file);
  147. EXPECT_EQ(line, 3);
  148. EXPECT_STREQ(data, "data3");
  149. EXPECT_EQ(flags, ERR_FLAG_STRING);
  150. // The error queue is now empty for the next iteration.
  151. EXPECT_EQ(0u, ERR_get_error());
  152. }
  153. // Test a case where the error queue wraps around. The first set of errors
  154. // will all be discarded, but result in wrapping the list around.
  155. ERR_clear_error();
  156. for (unsigned i = 0; i < ERR_NUM_ERRORS / 2; i++) {
  157. ERR_put_error(0, 0 /* unused */, 0, "invalid", 0);
  158. }
  159. for (unsigned i = 1; i < ERR_NUM_ERRORS; i++) {
  160. ERR_put_error(i, 0 /* unused */, i, "test", i);
  161. }
  162. saved.reset(ERR_save_state());
  163. // The state may be restored, both over an empty and non-empty state. Pop one
  164. // error off so the first iteration is tested to not be a no-op.
  165. ERR_get_error();
  166. for (int i = 0; i < 2; i++) {
  167. SCOPED_TRACE(i);
  168. ERR_restore_state(saved.get());
  169. for (int j = 1; j < ERR_NUM_ERRORS; j++) {
  170. SCOPED_TRACE(j);
  171. packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
  172. EXPECT_EQ(ERR_GET_LIB(packed_error), j);
  173. EXPECT_EQ(ERR_GET_REASON(packed_error), j);
  174. EXPECT_STREQ("test", file);
  175. EXPECT_EQ(line, j);
  176. }
  177. // The error queue is now empty for the next iteration.
  178. EXPECT_EQ(0u, ERR_get_error());
  179. }
  180. }