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