strerror_test.cc 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2020 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "absl/base/internal/strerror.h"
  15. #include <atomic>
  16. #include <cerrno>
  17. #include <cstdio>
  18. #include <cstring>
  19. #include <string>
  20. #include <thread> // NOLINT(build/c++11)
  21. #include <vector>
  22. #include "gmock/gmock.h"
  23. #include "gtest/gtest.h"
  24. #include "absl/strings/match.h"
  25. namespace {
  26. using ::testing::AnyOf;
  27. using ::testing::Eq;
  28. TEST(StrErrorTest, ValidErrorCode) {
  29. errno = ERANGE;
  30. EXPECT_THAT(absl::base_internal::StrError(EDOM), Eq(strerror(EDOM)));
  31. EXPECT_THAT(errno, Eq(ERANGE));
  32. }
  33. TEST(StrErrorTest, InvalidErrorCode) {
  34. errno = ERANGE;
  35. EXPECT_THAT(absl::base_internal::StrError(-1),
  36. AnyOf(Eq("No error information"), Eq("Unknown error -1")));
  37. EXPECT_THAT(errno, Eq(ERANGE));
  38. }
  39. TEST(StrErrorTest, MultipleThreads) {
  40. // In this test, we will start up 2 threads and have each one call
  41. // StrError 1000 times, each time with a different errnum. We
  42. // expect that StrError(errnum) will return a string equal to the
  43. // one returned by strerror(errnum), if the code is known. Since
  44. // strerror is known to be thread-hostile, collect all the expected
  45. // strings up front.
  46. const int kNumCodes = 1000;
  47. std::vector<std::string> expected_strings(kNumCodes);
  48. for (int i = 0; i < kNumCodes; ++i) {
  49. expected_strings[i] = strerror(i);
  50. }
  51. std::atomic_int counter(0);
  52. auto thread_fun = [&]() {
  53. for (int i = 0; i < kNumCodes; ++i) {
  54. ++counter;
  55. errno = ERANGE;
  56. const std::string value = absl::base_internal::StrError(i);
  57. // EXPECT_* could change errno. Stash it first.
  58. int check_err = errno;
  59. EXPECT_THAT(check_err, Eq(ERANGE));
  60. // Only the GNU implementation is guaranteed to provide the
  61. // string "Unknown error nnn". POSIX doesn't say anything.
  62. if (!absl::StartsWith(value, "Unknown error ")) {
  63. EXPECT_THAT(value, Eq(expected_strings[i]));
  64. }
  65. }
  66. };
  67. const int kNumThreads = 100;
  68. std::vector<std::thread> threads;
  69. for (int i = 0; i < kNumThreads; ++i) {
  70. threads.push_back(std::thread(thread_fun));
  71. }
  72. for (auto& thread : threads) {
  73. thread.join();
  74. }
  75. EXPECT_THAT(counter, Eq(kNumThreads * kNumCodes));
  76. }
  77. } // namespace