blob: cf75c160e5bbe27818dc200040b23b653bc3c3f0 [file] [log] [blame]
Martin Haimberger0ad6ee92015-11-13 03:18:50 -08001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#include <boost/test/auto_unit_test.hpp>
21#include <boost/test/unit_test_suite.hpp>
22#include <boost/bind.hpp>
23#include <boost/chrono/duration.hpp>
24#include <boost/date_time/posix_time/posix_time_duration.hpp>
25#include <boost/thread/thread.hpp>
26#include <boost/filesystem.hpp>
27#include <boost/format.hpp>
28#include <boost/shared_ptr.hpp>
29#include <thrift/transport/TSSLSocket.h>
30#include <thrift/transport/TSSLServerSocket.h>
Martin Haimberger0ad6ee92015-11-13 03:18:50 -080031#ifdef __linux__
32#include <signal.h>
33#endif
34
35using apache::thrift::transport::TSSLServerSocket;
36using apache::thrift::transport::TSSLSocket;
37using apache::thrift::transport::TTransport;
38using apache::thrift::transport::TTransportException;
39using apache::thrift::transport::TSSLSocketFactory;
40
John Siroisb867b272016-02-12 17:44:01 -070041BOOST_AUTO_TEST_SUITE(TSSLSocketInterruptTest)
42
Martin Haimberger0ad6ee92015-11-13 03:18:50 -080043boost::filesystem::path keyDir;
44boost::filesystem::path certFile(const std::string& filename)
45{
46 return keyDir / filename;
47}
48boost::mutex gMutex;
49
50struct GlobalFixtureSSL
51{
52 GlobalFixtureSSL()
53 {
54 using namespace boost::unit_test::framework;
55 for (int i = 0; i < master_test_suite().argc; ++i)
56 {
57 BOOST_TEST_MESSAGE(boost::format("argv[%1%] = \"%2%\"") % i % master_test_suite().argv[i]);
58 }
59
60#ifdef __linux__
61 // OpenSSL calls send() without MSG_NOSIGPIPE so writing to a socket that has
62 // disconnected can cause a SIGPIPE signal...
63 signal(SIGPIPE, SIG_IGN);
64#endif
65
66 TSSLSocketFactory::setManualOpenSSLInitialization(true);
67 apache::thrift::transport::initializeOpenSSL();
68
69 keyDir = boost::filesystem::current_path().parent_path().parent_path().parent_path() / "test" / "keys";
70 if (!boost::filesystem::exists(certFile("server.crt")))
71 {
72 keyDir = boost::filesystem::path(master_test_suite().argv[master_test_suite().argc - 1]);
73 if (!boost::filesystem::exists(certFile("server.crt")))
74 {
75 throw std::invalid_argument("The last argument to this test must be the directory containing the test certificate(s).");
76 }
77 }
78 }
79
80 virtual ~GlobalFixtureSSL()
81 {
82 apache::thrift::transport::cleanupOpenSSL();
83#ifdef __linux__
84 signal(SIGPIPE, SIG_DFL);
85#endif
86 }
87};
88
89#if (BOOST_VERSION >= 105900)
90BOOST_GLOBAL_FIXTURE(GlobalFixtureSSL);
91#else
92BOOST_GLOBAL_FIXTURE(GlobalFixtureSSL)
93#endif
94
Martin Haimberger0ad6ee92015-11-13 03:18:50 -080095void readerWorker(boost::shared_ptr<TTransport> tt, uint32_t expectedResult) {
96 uint8_t buf[4];
97 try {
98 tt->read(buf, 1);
99 BOOST_CHECK_EQUAL(expectedResult, tt->read(buf, 4));
100 } catch (const TTransportException& tx) {
101 BOOST_CHECK_EQUAL(TTransportException::INTERNAL_ERROR, tx.getType());
102 }
103}
104
105void readerWorkerMustThrow(boost::shared_ptr<TTransport> tt) {
106 try {
107 uint8_t buf[400];
108 tt->read(buf, 1);
109 tt->read(buf, 400);
110 BOOST_ERROR("should not have gotten here");
111 } catch (const TTransportException& tx) {
112 BOOST_CHECK_EQUAL(TTransportException::INTERRUPTED, tx.getType());
113 }
114}
115
116boost::shared_ptr<TSSLSocketFactory> createServerSocketFactory() {
117 boost::shared_ptr<TSSLSocketFactory> pServerSocketFactory;
118
119 pServerSocketFactory.reset(new TSSLSocketFactory());
120 pServerSocketFactory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
Antonio Di Monaco796667b2016-01-04 23:05:19 +0100121 pServerSocketFactory->loadCertificate(certFile("server.crt").string().c_str());
122 pServerSocketFactory->loadPrivateKey(certFile("server.key").string().c_str());
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800123 pServerSocketFactory->server(true);
124 return pServerSocketFactory;
125}
126
127boost::shared_ptr<TSSLSocketFactory> createClientSocketFactory() {
128 boost::shared_ptr<TSSLSocketFactory> pClientSocketFactory;
129
130 pClientSocketFactory.reset(new TSSLSocketFactory());
131 pClientSocketFactory->authenticate(true);
Antonio Di Monaco796667b2016-01-04 23:05:19 +0100132 pClientSocketFactory->loadCertificate(certFile("client.crt").string().c_str());
133 pClientSocketFactory->loadPrivateKey(certFile("client.key").string().c_str());
134 pClientSocketFactory->loadTrustedCertificates(certFile("CA.pem").string().c_str());
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800135 return pClientSocketFactory;
136}
137
138BOOST_AUTO_TEST_CASE(test_ssl_interruptable_child_read_while_handshaking) {
139 boost::shared_ptr<TSSLSocketFactory> pServerSocketFactory = createServerSocketFactory();
John Siroisb867b272016-02-12 17:44:01 -0700140 TSSLServerSocket sock1("localhost", 0, pServerSocketFactory);
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800141 sock1.listen();
John Siroisb867b272016-02-12 17:44:01 -0700142 int port = sock1.getPort();
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800143 boost::shared_ptr<TSSLSocketFactory> pClientSocketFactory = createClientSocketFactory();
John Siroisb867b272016-02-12 17:44:01 -0700144 boost::shared_ptr<TSSLSocket> clientSock = pClientSocketFactory->createSocket("localhost", port);
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800145 clientSock->open();
146 boost::shared_ptr<TTransport> accepted = sock1.accept();
147 boost::thread readThread(boost::bind(readerWorkerMustThrow, accepted));
148 boost::this_thread::sleep(boost::posix_time::milliseconds(50));
149 // readThread is practically guaranteed to be blocking now
150 sock1.interruptChildren();
151 BOOST_CHECK_MESSAGE(readThread.try_join_for(boost::chrono::milliseconds(20)),
152 "server socket interruptChildren did not interrupt child read");
153 clientSock->close();
154 accepted->close();
155 sock1.close();
156}
157
158BOOST_AUTO_TEST_CASE(test_ssl_interruptable_child_read) {
159 boost::shared_ptr<TSSLSocketFactory> pServerSocketFactory = createServerSocketFactory();
John Siroisb867b272016-02-12 17:44:01 -0700160 TSSLServerSocket sock1("localhost", 0, pServerSocketFactory);
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800161 sock1.listen();
John Siroisb867b272016-02-12 17:44:01 -0700162 int port = sock1.getPort();
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800163 boost::shared_ptr<TSSLSocketFactory> pClientSocketFactory = createClientSocketFactory();
John Siroisb867b272016-02-12 17:44:01 -0700164 boost::shared_ptr<TSSLSocket> clientSock = pClientSocketFactory->createSocket("localhost", port);
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800165 clientSock->open();
166 boost::shared_ptr<TTransport> accepted = sock1.accept();
167 boost::thread readThread(boost::bind(readerWorkerMustThrow, accepted));
168 clientSock->write((const uint8_t*)"0", 1);
169 boost::this_thread::sleep(boost::posix_time::milliseconds(50));
170 // readThread is practically guaranteed to be blocking now
171 sock1.interruptChildren();
172 BOOST_CHECK_MESSAGE(readThread.try_join_for(boost::chrono::milliseconds(20)),
173 "server socket interruptChildren did not interrupt child read");
174 accepted->close();
175 clientSock->close();
176 sock1.close();
177}
178
179BOOST_AUTO_TEST_CASE(test_ssl_non_interruptable_child_read) {
180 std::cout << "An error message from SSL_Shutdown on the console is expected:" << std::endl;
181 boost::shared_ptr<TSSLSocketFactory> pServerSocketFactory = createServerSocketFactory();
John Siroisb867b272016-02-12 17:44:01 -0700182 TSSLServerSocket sock1("localhost", 0, pServerSocketFactory);
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800183 sock1.setInterruptableChildren(false); // returns to pre-THRIFT-2441 behavior
184 sock1.listen();
John Siroisb867b272016-02-12 17:44:01 -0700185 int port = sock1.getPort();
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800186 boost::shared_ptr<TSSLSocketFactory> pClientSocketFactory = createClientSocketFactory();
John Siroisb867b272016-02-12 17:44:01 -0700187 boost::shared_ptr<TSSLSocket> clientSock = pClientSocketFactory->createSocket("localhost", port);
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800188 clientSock->open();
189 boost::shared_ptr<TTransport> accepted = sock1.accept();
190 boost::thread readThread(boost::bind(readerWorker, accepted, 0));
191 clientSock->write((const uint8_t*)"0", 1);
192 boost::this_thread::sleep(boost::posix_time::milliseconds(50));
193 // readThread is practically guaranteed to be blocking here
194 sock1.interruptChildren();
195 BOOST_CHECK_MESSAGE(!readThread.try_join_for(boost::chrono::milliseconds(200)),
196 "server socket interruptChildren interrupted child read");
197
198 // only way to proceed is to have the client disconnect
199 clientSock->close();
200 readThread.join();
201 accepted->close();
202 sock1.close();
203}
204
205BOOST_AUTO_TEST_CASE(test_ssl_cannot_change_after_listen) {
206 boost::shared_ptr<TSSLSocketFactory> pServerSocketFactory = createServerSocketFactory();
John Siroisb867b272016-02-12 17:44:01 -0700207 TSSLServerSocket sock1("localhost", 0, pServerSocketFactory);
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800208 sock1.listen();
209 BOOST_CHECK_THROW(sock1.setInterruptableChildren(false), std::logic_error);
210 sock1.close();
211}
212
213void peekerWorker(boost::shared_ptr<TTransport> tt, bool expectedResult) {
214 uint8_t buf[400];
215
216 tt->read(buf, 1);
217 BOOST_CHECK_EQUAL(expectedResult, tt->peek());
218}
219
220void peekerWorkerInterrupt(boost::shared_ptr<TTransport> tt) {
221 uint8_t buf[400];
222 try {
223 tt->read(buf, 1);
224 tt->peek();
225 } catch (const TTransportException& tx) {
226 BOOST_CHECK_EQUAL(TTransportException::INTERRUPTED, tx.getType());
227 }
228}
229
230BOOST_AUTO_TEST_CASE(test_ssl_interruptable_child_peek) {
231 std::cout << "An error message from SSL_Shutdown on the console is expected:" << std::endl;
232 boost::shared_ptr<TSSLSocketFactory> pServerSocketFactory = createServerSocketFactory();
John Siroisb867b272016-02-12 17:44:01 -0700233 TSSLServerSocket sock1("localhost", 0, pServerSocketFactory);
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800234 sock1.listen();
John Siroisb867b272016-02-12 17:44:01 -0700235 int port = sock1.getPort();
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800236 boost::shared_ptr<TSSLSocketFactory> pClientSocketFactory = createClientSocketFactory();
John Siroisb867b272016-02-12 17:44:01 -0700237 boost::shared_ptr<TSSLSocket> clientSock = pClientSocketFactory->createSocket("localhost", port);
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800238 clientSock->open();
239 boost::shared_ptr<TTransport> accepted = sock1.accept();
240 // peek() will return false if child is interrupted
241 boost::thread peekThread(boost::bind(peekerWorkerInterrupt, accepted));
242 clientSock->write((const uint8_t*)"0", 1);
243 boost::this_thread::sleep(boost::posix_time::milliseconds(50));
244 // peekThread is practically guaranteed to be blocking now
245 sock1.interruptChildren();
246 BOOST_CHECK_MESSAGE(peekThread.try_join_for(boost::chrono::milliseconds(200)),
247 "server socket interruptChildren did not interrupt child peek");
248#ifdef __linux__
249 signal(SIGPIPE, SIG_IGN);
250#endif
251 clientSock->close();
252 accepted->close();
253 sock1.close();
254}
255
256BOOST_AUTO_TEST_CASE(test_ssl_non_interruptable_child_peek) {
257 std::cout << "An error message from SSL_Shutdown on the console is expected:" << std::endl;
258 boost::shared_ptr<TSSLSocketFactory> pServerSocketFactory = createServerSocketFactory();
John Siroisb867b272016-02-12 17:44:01 -0700259 TSSLServerSocket sock1("localhost", 0, pServerSocketFactory);
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800260 sock1.setInterruptableChildren(false); // returns to pre-THRIFT-2441 behavior
261 sock1.listen();
John Siroisb867b272016-02-12 17:44:01 -0700262 int port = sock1.getPort();
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800263 boost::shared_ptr<TSSLSocketFactory> pClientSocketFactory = createClientSocketFactory();
John Siroisb867b272016-02-12 17:44:01 -0700264 boost::shared_ptr<TSSLSocket> clientSock = pClientSocketFactory->createSocket("localhost", port);
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800265 clientSock->open();
266 boost::shared_ptr<TTransport> accepted = sock1.accept();
267 // peek() will return false when remote side is closed
268 boost::thread peekThread(boost::bind(peekerWorker, accepted, false));
269 //boost::thread peekThread(boost::bind(peekerWorkerRead, clientSock, false));
270 clientSock->write((const uint8_t*)"0", 1);
271 boost::this_thread::sleep(boost::posix_time::milliseconds(50));
272 // peekThread is practically guaranteed to be blocking now
273 sock1.interruptChildren();
274 BOOST_CHECK_MESSAGE(!peekThread.try_join_for(boost::chrono::milliseconds(200)),
275 "server socket interruptChildren interrupted child peek");
276
277 // only way to proceed is to have the client disconnect
278#ifdef __linux__
279 signal(SIGPIPE, SIG_IGN);
280#endif
281 clientSock->close();
282 peekThread.join();
283 accepted->close();
284 sock1.close();
285}
286
287BOOST_AUTO_TEST_SUITE_END()