proxygen
AsyncSocketExceptionTest.cpp
Go to the documentation of this file.
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 
18 
19 #include <array>
20 
21 #include <folly/Conv.h>
26 #include <folly/ssl/Init.h>
27 
28 using namespace testing;
29 
30 namespace folly {
31 
34  AsyncSocketException::AsyncSocketExceptionType::NOT_OPEN,
35  "test exception 1");
36 
37  EXPECT_EQ(
38  AsyncSocketException::AsyncSocketExceptionType::NOT_OPEN, ex1.getType());
39  EXPECT_EQ(0, ex1.getErrno());
40  EXPECT_EQ(
41  "AsyncSocketException: test exception 1, type = Socket not open",
42  std::string(ex1.what()));
43 
45  AsyncSocketException::AsyncSocketExceptionType::BAD_ARGS,
46  "test exception 2",
47  ECONNREFUSED);
48 
49  EXPECT_EQ(
50  AsyncSocketException::AsyncSocketExceptionType::BAD_ARGS, ex2.getType());
51  EXPECT_EQ(ECONNREFUSED, ex2.getErrno());
52  EXPECT_EQ(
53  "AsyncSocketException: test exception 2, type = Invalid arguments, "
54  "errno = " +
55  to<std::string>(ECONNREFUSED) + " (Connection refused)",
56  std::string(ex2.what()));
57 }
58 
59 TEST(AsyncSocketException, SSLExceptionType) {
60  {
61  // Initiailzes OpenSSL everything. Else some of the calls will block
63  SSLException eof(SSL_ERROR_ZERO_RETURN, 0, 0, 0);
64  EXPECT_EQ(eof.getType(), AsyncSocketException::END_OF_FILE);
65 
66  SSLException netEof(SSL_ERROR_SYSCALL, 0, 0, 0);
67  EXPECT_EQ(netEof.getType(), AsyncSocketException::END_OF_FILE);
68 
69  SSLException netOther(SSL_ERROR_SYSCALL, 0, 1, 0);
70  EXPECT_EQ(netOther.getType(), AsyncSocketException::NETWORK_ERROR);
71 
72  std::array<int, 6> sslErrs{{SSL_ERROR_SSL,
73  SSL_ERROR_WANT_READ,
74  SSL_ERROR_WANT_WRITE,
75  SSL_ERROR_WANT_X509_LOOKUP,
76  SSL_ERROR_WANT_CONNECT,
77  SSL_ERROR_WANT_ACCEPT}};
78 
79  for (auto& e : sslErrs) {
80  SSLException sslEx(e, 0, 0, 0);
81  EXPECT_EQ(sslEx.getType(), AsyncSocketException::SSL_ERROR);
82  }
83  }
84 
85  {
86  SSLException eof(SSLError::EOF_ERROR);
87  EXPECT_EQ(eof.getType(), AsyncSocketException::END_OF_FILE);
88 
89  SSLException net(SSLError::NETWORK_ERROR);
90  EXPECT_EQ(net.getType(), AsyncSocketException::NETWORK_ERROR);
91 
92  std::array<SSLError, 4> errs{{SSLError::CLIENT_RENEGOTIATION,
93  SSLError::INVALID_RENEGOTIATION,
94  SSLError::EARLY_WRITE,
95  SSLError::SSL_ERROR}};
96 
97  for (auto& e : errs) {
98  SSLException sslEx(e);
99  EXPECT_EQ(sslEx.getType(), AsyncSocketException::SSL_ERROR);
100  }
101  }
102 }
103 
104 } // namespace folly
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void init()
Definition: Init.cpp:54
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
TEST(GTestEnvVarTest, Dummy)
void SimpleTest(std::shared_ptr< folly::Executor > const &parent)
const char * string
Definition: Conv.cpp:212
AsyncSocketExceptionType getType() const noexcept