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