ExceptionTest.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright 2013-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <folly/Exception.h>
  17. #include <folly/experimental/TestUtil.h>
  18. #include <folly/portability/GTest.h>
  19. #include <cstdio>
  20. #include <memory>
  21. namespace folly {
  22. namespace test {
  23. #define EXPECT_SYSTEM_ERROR(statement, err, msg) \
  24. try { \
  25. statement; \
  26. ADD_FAILURE() << "Didn't throw"; \
  27. } catch (const std::system_error& e) { \
  28. std::system_error expected(err, std::system_category(), msg); \
  29. EXPECT_STREQ(expected.what(), e.what()); \
  30. } catch (...) { \
  31. ADD_FAILURE() << "Threw a different type"; \
  32. }
  33. TEST(ExceptionTest, Simple) {
  34. // Make sure errno isn't used when we don't want it to, set it to something
  35. // else than what we set when we call Explicit functions
  36. errno = ERANGE;
  37. EXPECT_SYSTEM_ERROR(throwSystemErrorExplicit(EIO, "hello"), EIO, "hello");
  38. errno = ERANGE;
  39. EXPECT_SYSTEM_ERROR(
  40. throwSystemErrorExplicit(EIO, "hello", " world"), EIO, "hello world");
  41. errno = ERANGE;
  42. EXPECT_SYSTEM_ERROR(
  43. throwSystemError("hello", " world"), ERANGE, "hello world");
  44. EXPECT_NO_THROW(checkPosixError(0, "hello", " world"));
  45. errno = ERANGE;
  46. EXPECT_SYSTEM_ERROR(
  47. checkPosixError(EIO, "hello", " world"), EIO, "hello world");
  48. EXPECT_NO_THROW(checkKernelError(0, "hello", " world"));
  49. EXPECT_NO_THROW(checkKernelError(EIO, "hello", " world"));
  50. errno = ERANGE;
  51. EXPECT_SYSTEM_ERROR(
  52. checkKernelError(-EIO, "hello", " world"), EIO, "hello world");
  53. EXPECT_NO_THROW(checkUnixError(0, "hello", " world"));
  54. EXPECT_NO_THROW(checkUnixError(1, "hello", " world"));
  55. errno = ERANGE;
  56. EXPECT_SYSTEM_ERROR(
  57. checkUnixError(-1, "hello", " world"), ERANGE, "hello world");
  58. EXPECT_NO_THROW(checkUnixErrorExplicit(0, EIO, "hello", " world"));
  59. EXPECT_NO_THROW(checkUnixErrorExplicit(1, EIO, "hello", " world"));
  60. errno = ERANGE;
  61. EXPECT_SYSTEM_ERROR(
  62. checkUnixErrorExplicit(-1, EIO, "hello", " world"), EIO, "hello world");
  63. TemporaryDirectory tmpdir;
  64. auto exnpath = tmpdir.path() / "ExceptionTest";
  65. auto fp = fopen(exnpath.string().c_str(), "w+b");
  66. ASSERT_TRUE(fp != nullptr);
  67. SCOPE_EXIT {
  68. fclose(fp);
  69. };
  70. EXPECT_NO_THROW(checkFopenError(fp, "hello", " world"));
  71. errno = ERANGE;
  72. EXPECT_SYSTEM_ERROR(
  73. checkFopenError(nullptr, "hello", " world"), ERANGE, "hello world");
  74. EXPECT_NO_THROW(checkFopenErrorExplicit(fp, EIO, "hello", " world"));
  75. errno = ERANGE;
  76. EXPECT_SYSTEM_ERROR(
  77. checkFopenErrorExplicit(nullptr, EIO, "hello", " world"),
  78. EIO,
  79. "hello world");
  80. }
  81. TEST(ExceptionTest, makeSystemError) {
  82. errno = ENOENT;
  83. auto ex = makeSystemErrorExplicit(EDEADLK, "stuck");
  84. EXPECT_EQ(EDEADLK, ex.code().value());
  85. EXPECT_EQ(std::system_category(), ex.code().category());
  86. EXPECT_TRUE(StringPiece{ex.what()}.contains("stuck"))
  87. << "what() string missing input message: " << ex.what();
  88. ex = makeSystemErrorExplicit(EDOM, 300, " is bigger than max=", 255);
  89. EXPECT_EQ(EDOM, ex.code().value());
  90. EXPECT_EQ(std::system_category(), ex.code().category());
  91. EXPECT_TRUE(StringPiece{ex.what()}.contains("300 is bigger than max=255"))
  92. << "what() string missing input message: " << ex.what();
  93. errno = EINVAL;
  94. ex = makeSystemError("bad argument ", 1234, ": bogus");
  95. EXPECT_EQ(EINVAL, ex.code().value());
  96. EXPECT_EQ(std::system_category(), ex.code().category());
  97. EXPECT_TRUE(StringPiece{ex.what()}.contains("bad argument 1234: bogus"))
  98. << "what() string missing input message: " << ex.what();
  99. errno = 0;
  100. ex = makeSystemError("unexpected success");
  101. EXPECT_EQ(0, ex.code().value());
  102. EXPECT_EQ(std::system_category(), ex.code().category());
  103. EXPECT_TRUE(StringPiece{ex.what()}.contains("unexpected success"))
  104. << "what() string missing input message: " << ex.what();
  105. }
  106. } // namespace test
  107. } // namespace folly