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