AsyncSocketExceptionTest.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright 2016-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 <array>
  17. #include <folly/io/async/AsyncSocketException.h>
  18. #include <folly/io/async/SSLContext.h>
  19. #include <folly/io/async/ssl/SSLErrors.h>
  20. #include <folly/ssl/Init.h>
  21. #include <folly/portability/GTest.h>
  22. #include <folly/portability/OpenSSL.h>
  23. using namespace testing;
  24. namespace folly {
  25. TEST(AsyncSocketException, SimpleTest) {
  26. AsyncSocketException ex1(
  27. AsyncSocketException::AsyncSocketExceptionType::NOT_OPEN,
  28. "test exception 1");
  29. EXPECT_EQ(
  30. AsyncSocketException::AsyncSocketExceptionType::NOT_OPEN, ex1.getType());
  31. EXPECT_EQ(0, ex1.getErrno());
  32. EXPECT_EQ(
  33. "AsyncSocketException: test exception 1, type = Socket not open",
  34. std::string(ex1.what()));
  35. AsyncSocketException ex2(
  36. AsyncSocketException::AsyncSocketExceptionType::BAD_ARGS,
  37. "test exception 2",
  38. 111 /*ECONNREFUSED*/);
  39. EXPECT_EQ(
  40. AsyncSocketException::AsyncSocketExceptionType::BAD_ARGS, ex2.getType());
  41. EXPECT_EQ(111, ex2.getErrno());
  42. EXPECT_EQ(
  43. "AsyncSocketException: test exception 2, type = Invalid arguments, "
  44. "errno = 111 (Connection refused)",
  45. std::string(ex2.what()));
  46. }
  47. TEST(AsyncSocketException, SSLExceptionType) {
  48. {
  49. // Initiailzes OpenSSL everything. Else some of the calls will block
  50. folly::ssl::init();
  51. SSLException eof(SSL_ERROR_ZERO_RETURN, 0, 0, 0);
  52. EXPECT_EQ(eof.getType(), AsyncSocketException::END_OF_FILE);
  53. SSLException netEof(SSL_ERROR_SYSCALL, 0, 0, 0);
  54. EXPECT_EQ(netEof.getType(), AsyncSocketException::END_OF_FILE);
  55. SSLException netOther(SSL_ERROR_SYSCALL, 0, 1, 0);
  56. EXPECT_EQ(netOther.getType(), AsyncSocketException::NETWORK_ERROR);
  57. std::array<int, 6> sslErrs{{SSL_ERROR_SSL,
  58. SSL_ERROR_WANT_READ,
  59. SSL_ERROR_WANT_WRITE,
  60. SSL_ERROR_WANT_X509_LOOKUP,
  61. SSL_ERROR_WANT_CONNECT,
  62. SSL_ERROR_WANT_ACCEPT}};
  63. for (auto& e : sslErrs) {
  64. SSLException sslEx(e, 0, 0, 0);
  65. EXPECT_EQ(sslEx.getType(), AsyncSocketException::SSL_ERROR);
  66. }
  67. }
  68. {
  69. SSLException eof(SSLError::EOF_ERROR);
  70. EXPECT_EQ(eof.getType(), AsyncSocketException::END_OF_FILE);
  71. SSLException net(SSLError::NETWORK_ERROR);
  72. EXPECT_EQ(net.getType(), AsyncSocketException::NETWORK_ERROR);
  73. std::array<SSLError, 4> errs{{SSLError::CLIENT_RENEGOTIATION,
  74. SSLError::INVALID_RENEGOTIATION,
  75. SSLError::EARLY_WRITE,
  76. SSLError::SSL_ERROR}};
  77. for (auto& e : errs) {
  78. SSLException sslEx(e);
  79. EXPECT_EQ(sslEx.getType(), AsyncSocketException::SSL_ERROR);
  80. }
  81. }
  82. }
  83. } // namespace folly