blob: bf5c7d7656527eb67bf84903f763a5149585d00f [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
tpcwangaf81cf02016-10-05 09:48:23 -070062 // disconnected can cause a SIGPIPE signal...
63 signal(SIGPIPE, SIG_IGN);
Martin Haimberger0ad6ee92015-11-13 03:18:50 -080064#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) {
tpcwangaf81cf02016-10-05 09:48:23 -0700101 BOOST_CHECK_EQUAL(TTransportException::TIMED_OUT, tx.getType());
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800102 }
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) {
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800180 boost::shared_ptr<TSSLSocketFactory> pServerSocketFactory = createServerSocketFactory();
John Siroisb867b272016-02-12 17:44:01 -0700181 TSSLServerSocket sock1("localhost", 0, pServerSocketFactory);
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800182 sock1.setInterruptableChildren(false); // returns to pre-THRIFT-2441 behavior
183 sock1.listen();
John Siroisb867b272016-02-12 17:44:01 -0700184 int port = sock1.getPort();
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800185 boost::shared_ptr<TSSLSocketFactory> pClientSocketFactory = createClientSocketFactory();
John Siroisb867b272016-02-12 17:44:01 -0700186 boost::shared_ptr<TSSLSocket> clientSock = pClientSocketFactory->createSocket("localhost", port);
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800187 clientSock->open();
188 boost::shared_ptr<TTransport> accepted = sock1.accept();
tpcwangaf81cf02016-10-05 09:48:23 -0700189 boost::static_pointer_cast<TSSLSocket>(accepted)->setRecvTimeout(1000);
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800190 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
tpcwangaf81cf02016-10-05 09:48:23 -0700198 // wait for receive timeout to kick in
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800199 readThread.join();
200 accepted->close();
tpcwangaf81cf02016-10-05 09:48:23 -0700201 clientSock->close();
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800202 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];
tpcwangaf81cf02016-10-05 09:48:23 -0700215 try {
216 tt->read(buf, 1);
James E. King, III07f59972017-03-10 06:18:33 -0500217 BOOST_CHECK_EQUAL(expectedResult, tt->peek());
tpcwangaf81cf02016-10-05 09:48:23 -0700218 } catch (const TTransportException& tx) {
219 BOOST_CHECK_EQUAL(TTransportException::TIMED_OUT, tx.getType());
220 }
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800221}
222
223void peekerWorkerInterrupt(boost::shared_ptr<TTransport> tt) {
224 uint8_t buf[400];
225 try {
226 tt->read(buf, 1);
227 tt->peek();
228 } catch (const TTransportException& tx) {
229 BOOST_CHECK_EQUAL(TTransportException::INTERRUPTED, tx.getType());
230 }
231}
232
233BOOST_AUTO_TEST_CASE(test_ssl_interruptable_child_peek) {
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800234 boost::shared_ptr<TSSLSocketFactory> pServerSocketFactory = createServerSocketFactory();
John Siroisb867b272016-02-12 17:44:01 -0700235 TSSLServerSocket sock1("localhost", 0, pServerSocketFactory);
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800236 sock1.listen();
John Siroisb867b272016-02-12 17:44:01 -0700237 int port = sock1.getPort();
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800238 boost::shared_ptr<TSSLSocketFactory> pClientSocketFactory = createClientSocketFactory();
John Siroisb867b272016-02-12 17:44:01 -0700239 boost::shared_ptr<TSSLSocket> clientSock = pClientSocketFactory->createSocket("localhost", port);
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800240 clientSock->open();
241 boost::shared_ptr<TTransport> accepted = sock1.accept();
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800242 boost::thread peekThread(boost::bind(peekerWorkerInterrupt, accepted));
243 clientSock->write((const uint8_t*)"0", 1);
244 boost::this_thread::sleep(boost::posix_time::milliseconds(50));
245 // peekThread is practically guaranteed to be blocking now
246 sock1.interruptChildren();
247 BOOST_CHECK_MESSAGE(peekThread.try_join_for(boost::chrono::milliseconds(200)),
248 "server socket interruptChildren did not interrupt child peek");
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800249 accepted->close();
tpcwangaf81cf02016-10-05 09:48:23 -0700250 clientSock->close();
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800251 sock1.close();
252}
253
254BOOST_AUTO_TEST_CASE(test_ssl_non_interruptable_child_peek) {
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800255 boost::shared_ptr<TSSLSocketFactory> pServerSocketFactory = createServerSocketFactory();
John Siroisb867b272016-02-12 17:44:01 -0700256 TSSLServerSocket sock1("localhost", 0, pServerSocketFactory);
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800257 sock1.setInterruptableChildren(false); // returns to pre-THRIFT-2441 behavior
258 sock1.listen();
John Siroisb867b272016-02-12 17:44:01 -0700259 int port = sock1.getPort();
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800260 boost::shared_ptr<TSSLSocketFactory> pClientSocketFactory = createClientSocketFactory();
John Siroisb867b272016-02-12 17:44:01 -0700261 boost::shared_ptr<TSSLSocket> clientSock = pClientSocketFactory->createSocket("localhost", port);
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800262 clientSock->open();
263 boost::shared_ptr<TTransport> accepted = sock1.accept();
tpcwangaf81cf02016-10-05 09:48:23 -0700264 boost::static_pointer_cast<TSSLSocket>(accepted)->setRecvTimeout(1000);
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800265 boost::thread peekThread(boost::bind(peekerWorker, accepted, false));
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800266 clientSock->write((const uint8_t*)"0", 1);
267 boost::this_thread::sleep(boost::posix_time::milliseconds(50));
268 // peekThread is practically guaranteed to be blocking now
269 sock1.interruptChildren();
270 BOOST_CHECK_MESSAGE(!peekThread.try_join_for(boost::chrono::milliseconds(200)),
271 "server socket interruptChildren interrupted child peek");
272
tpcwangaf81cf02016-10-05 09:48:23 -0700273 // wait for the receive timeout to kick in
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800274 peekThread.join();
275 accepted->close();
tpcwangaf81cf02016-10-05 09:48:23 -0700276 clientSock->close();
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800277 sock1.close();
278}
279
280BOOST_AUTO_TEST_SUITE_END()