blob: 83fb993d456f6898328b21edcfcd754722019350 [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
zeshuai0076b1cb302020-05-27 12:08:01 +080020#include <boost/test/unit_test.hpp>
Martin Haimberger0ad6ee92015-11-13 03:18:50 -080021#include <boost/test/unit_test_suite.hpp>
Martin Haimberger0ad6ee92015-11-13 03:18:50 -080022#include <boost/chrono/duration.hpp>
23#include <boost/date_time/posix_time/posix_time_duration.hpp>
24#include <boost/thread/thread.hpp>
25#include <boost/filesystem.hpp>
26#include <boost/format.hpp>
cyy316723a2019-01-05 16:35:14 +080027#include <memory>
Martin Haimberger0ad6ee92015-11-13 03:18:50 -080028#include <thrift/transport/TSSLSocket.h>
29#include <thrift/transport/TSSLServerSocket.h>
Martin Haimberger0ad6ee92015-11-13 03:18:50 -080030#ifdef __linux__
31#include <signal.h>
32#endif
33
34using apache::thrift::transport::TSSLServerSocket;
35using apache::thrift::transport::TSSLSocket;
36using apache::thrift::transport::TTransport;
37using apache::thrift::transport::TTransportException;
38using apache::thrift::transport::TSSLSocketFactory;
39
cyy316723a2019-01-05 16:35:14 +080040using std::static_pointer_cast;
41using std::shared_ptr;
James E. King, III82ae9572017-08-05 12:23:54 -040042
John Siroisb867b272016-02-12 17:44:01 -070043BOOST_AUTO_TEST_SUITE(TSSLSocketInterruptTest)
44
Martin Haimberger0ad6ee92015-11-13 03:18:50 -080045boost::filesystem::path keyDir;
46boost::filesystem::path certFile(const std::string& filename)
47{
48 return keyDir / filename;
49}
50boost::mutex gMutex;
51
52struct GlobalFixtureSSL
53{
54 GlobalFixtureSSL()
55 {
56 using namespace boost::unit_test::framework;
57 for (int i = 0; i < master_test_suite().argc; ++i)
58 {
59 BOOST_TEST_MESSAGE(boost::format("argv[%1%] = \"%2%\"") % i % master_test_suite().argv[i]);
60 }
61
62#ifdef __linux__
63 // OpenSSL calls send() without MSG_NOSIGPIPE so writing to a socket that has
tpcwangaf81cf02016-10-05 09:48:23 -070064 // disconnected can cause a SIGPIPE signal...
65 signal(SIGPIPE, SIG_IGN);
Martin Haimberger0ad6ee92015-11-13 03:18:50 -080066#endif
67
68 TSSLSocketFactory::setManualOpenSSLInitialization(true);
69 apache::thrift::transport::initializeOpenSSL();
70
71 keyDir = boost::filesystem::current_path().parent_path().parent_path().parent_path() / "test" / "keys";
72 if (!boost::filesystem::exists(certFile("server.crt")))
73 {
74 keyDir = boost::filesystem::path(master_test_suite().argv[master_test_suite().argc - 1]);
75 if (!boost::filesystem::exists(certFile("server.crt")))
76 {
77 throw std::invalid_argument("The last argument to this test must be the directory containing the test certificate(s).");
78 }
79 }
80 }
81
82 virtual ~GlobalFixtureSSL()
83 {
84 apache::thrift::transport::cleanupOpenSSL();
85#ifdef __linux__
86 signal(SIGPIPE, SIG_DFL);
87#endif
88 }
89};
90
91#if (BOOST_VERSION >= 105900)
92BOOST_GLOBAL_FIXTURE(GlobalFixtureSSL);
93#else
94BOOST_GLOBAL_FIXTURE(GlobalFixtureSSL)
95#endif
96
James E. King, III82ae9572017-08-05 12:23:54 -040097void readerWorker(shared_ptr<TTransport> tt, uint32_t expectedResult) {
Martin Haimberger0ad6ee92015-11-13 03:18:50 -080098 uint8_t buf[4];
99 try {
100 tt->read(buf, 1);
101 BOOST_CHECK_EQUAL(expectedResult, tt->read(buf, 4));
102 } catch (const TTransportException& tx) {
tpcwangaf81cf02016-10-05 09:48:23 -0700103 BOOST_CHECK_EQUAL(TTransportException::TIMED_OUT, tx.getType());
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800104 }
105}
106
James E. King, III82ae9572017-08-05 12:23:54 -0400107void readerWorkerMustThrow(shared_ptr<TTransport> tt) {
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800108 try {
109 uint8_t buf[400];
110 tt->read(buf, 1);
111 tt->read(buf, 400);
112 BOOST_ERROR("should not have gotten here");
113 } catch (const TTransportException& tx) {
114 BOOST_CHECK_EQUAL(TTransportException::INTERRUPTED, tx.getType());
115 }
116}
117
James E. King, III82ae9572017-08-05 12:23:54 -0400118shared_ptr<TSSLSocketFactory> createServerSocketFactory() {
119 shared_ptr<TSSLSocketFactory> pServerSocketFactory;
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800120
121 pServerSocketFactory.reset(new TSSLSocketFactory());
122 pServerSocketFactory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
Antonio Di Monaco796667b2016-01-04 23:05:19 +0100123 pServerSocketFactory->loadCertificate(certFile("server.crt").string().c_str());
124 pServerSocketFactory->loadPrivateKey(certFile("server.key").string().c_str());
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800125 pServerSocketFactory->server(true);
126 return pServerSocketFactory;
127}
128
James E. King, III82ae9572017-08-05 12:23:54 -0400129shared_ptr<TSSLSocketFactory> createClientSocketFactory() {
130 shared_ptr<TSSLSocketFactory> pClientSocketFactory;
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800131
132 pClientSocketFactory.reset(new TSSLSocketFactory());
133 pClientSocketFactory->authenticate(true);
Antonio Di Monaco796667b2016-01-04 23:05:19 +0100134 pClientSocketFactory->loadCertificate(certFile("client.crt").string().c_str());
135 pClientSocketFactory->loadPrivateKey(certFile("client.key").string().c_str());
136 pClientSocketFactory->loadTrustedCertificates(certFile("CA.pem").string().c_str());
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800137 return pClientSocketFactory;
138}
139
140BOOST_AUTO_TEST_CASE(test_ssl_interruptable_child_read_while_handshaking) {
James E. King, III82ae9572017-08-05 12:23:54 -0400141 shared_ptr<TSSLSocketFactory> pServerSocketFactory = createServerSocketFactory();
John Siroisb867b272016-02-12 17:44:01 -0700142 TSSLServerSocket sock1("localhost", 0, pServerSocketFactory);
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800143 sock1.listen();
John Siroisb867b272016-02-12 17:44:01 -0700144 int port = sock1.getPort();
James E. King, III82ae9572017-08-05 12:23:54 -0400145 shared_ptr<TSSLSocketFactory> pClientSocketFactory = createClientSocketFactory();
146 shared_ptr<TSSLSocket> clientSock = pClientSocketFactory->createSocket("localhost", port);
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800147 clientSock->open();
James E. King, III82ae9572017-08-05 12:23:54 -0400148 shared_ptr<TTransport> accepted = sock1.accept();
cyy316723a2019-01-05 16:35:14 +0800149 boost::thread readThread(std::bind(readerWorkerMustThrow, accepted));
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800150 boost::this_thread::sleep(boost::posix_time::milliseconds(50));
151 // readThread is practically guaranteed to be blocking now
152 sock1.interruptChildren();
153 BOOST_CHECK_MESSAGE(readThread.try_join_for(boost::chrono::milliseconds(20)),
154 "server socket interruptChildren did not interrupt child read");
155 clientSock->close();
156 accepted->close();
157 sock1.close();
158}
159
160BOOST_AUTO_TEST_CASE(test_ssl_interruptable_child_read) {
James E. King, III82ae9572017-08-05 12:23:54 -0400161 shared_ptr<TSSLSocketFactory> pServerSocketFactory = createServerSocketFactory();
John Siroisb867b272016-02-12 17:44:01 -0700162 TSSLServerSocket sock1("localhost", 0, pServerSocketFactory);
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800163 sock1.listen();
John Siroisb867b272016-02-12 17:44:01 -0700164 int port = sock1.getPort();
James E. King, III82ae9572017-08-05 12:23:54 -0400165 shared_ptr<TSSLSocketFactory> pClientSocketFactory = createClientSocketFactory();
166 shared_ptr<TSSLSocket> clientSock = pClientSocketFactory->createSocket("localhost", port);
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800167 clientSock->open();
James E. King, III82ae9572017-08-05 12:23:54 -0400168 shared_ptr<TTransport> accepted = sock1.accept();
cyy316723a2019-01-05 16:35:14 +0800169 boost::thread readThread(std::bind(readerWorkerMustThrow, accepted));
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800170 clientSock->write((const uint8_t*)"0", 1);
171 boost::this_thread::sleep(boost::posix_time::milliseconds(50));
172 // readThread is practically guaranteed to be blocking now
173 sock1.interruptChildren();
174 BOOST_CHECK_MESSAGE(readThread.try_join_for(boost::chrono::milliseconds(20)),
175 "server socket interruptChildren did not interrupt child read");
176 accepted->close();
177 clientSock->close();
178 sock1.close();
179}
180
181BOOST_AUTO_TEST_CASE(test_ssl_non_interruptable_child_read) {
James E. King, III82ae9572017-08-05 12:23:54 -0400182 shared_ptr<TSSLSocketFactory> pServerSocketFactory = createServerSocketFactory();
John Siroisb867b272016-02-12 17:44:01 -0700183 TSSLServerSocket sock1("localhost", 0, pServerSocketFactory);
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800184 sock1.setInterruptableChildren(false); // returns to pre-THRIFT-2441 behavior
185 sock1.listen();
John Siroisb867b272016-02-12 17:44:01 -0700186 int port = sock1.getPort();
James E. King, III82ae9572017-08-05 12:23:54 -0400187 shared_ptr<TSSLSocketFactory> pClientSocketFactory = createClientSocketFactory();
188 shared_ptr<TSSLSocket> clientSock = pClientSocketFactory->createSocket("localhost", port);
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800189 clientSock->open();
James E. King, III82ae9572017-08-05 12:23:54 -0400190 shared_ptr<TTransport> accepted = sock1.accept();
191 static_pointer_cast<TSSLSocket>(accepted)->setRecvTimeout(1000);
cyy316723a2019-01-05 16:35:14 +0800192 boost::thread readThread(std::bind(readerWorker, accepted, 0));
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800193 clientSock->write((const uint8_t*)"0", 1);
194 boost::this_thread::sleep(boost::posix_time::milliseconds(50));
195 // readThread is practically guaranteed to be blocking here
196 sock1.interruptChildren();
197 BOOST_CHECK_MESSAGE(!readThread.try_join_for(boost::chrono::milliseconds(200)),
198 "server socket interruptChildren interrupted child read");
199
tpcwangaf81cf02016-10-05 09:48:23 -0700200 // wait for receive timeout to kick in
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800201 readThread.join();
202 accepted->close();
tpcwangaf81cf02016-10-05 09:48:23 -0700203 clientSock->close();
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800204 sock1.close();
205}
206
207BOOST_AUTO_TEST_CASE(test_ssl_cannot_change_after_listen) {
James E. King, III82ae9572017-08-05 12:23:54 -0400208 shared_ptr<TSSLSocketFactory> pServerSocketFactory = createServerSocketFactory();
John Siroisb867b272016-02-12 17:44:01 -0700209 TSSLServerSocket sock1("localhost", 0, pServerSocketFactory);
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800210 sock1.listen();
211 BOOST_CHECK_THROW(sock1.setInterruptableChildren(false), std::logic_error);
212 sock1.close();
213}
214
James E. King, III82ae9572017-08-05 12:23:54 -0400215void peekerWorker(shared_ptr<TTransport> tt, bool expectedResult) {
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800216 uint8_t buf[400];
tpcwangaf81cf02016-10-05 09:48:23 -0700217 try {
218 tt->read(buf, 1);
James E. King, III07f59972017-03-10 06:18:33 -0500219 BOOST_CHECK_EQUAL(expectedResult, tt->peek());
tpcwangaf81cf02016-10-05 09:48:23 -0700220 } catch (const TTransportException& tx) {
221 BOOST_CHECK_EQUAL(TTransportException::TIMED_OUT, tx.getType());
222 }
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800223}
224
James E. King, III82ae9572017-08-05 12:23:54 -0400225void peekerWorkerInterrupt(shared_ptr<TTransport> tt) {
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800226 uint8_t buf[400];
227 try {
228 tt->read(buf, 1);
229 tt->peek();
230 } catch (const TTransportException& tx) {
231 BOOST_CHECK_EQUAL(TTransportException::INTERRUPTED, tx.getType());
232 }
233}
234
235BOOST_AUTO_TEST_CASE(test_ssl_interruptable_child_peek) {
James E. King, III82ae9572017-08-05 12:23:54 -0400236 shared_ptr<TSSLSocketFactory> pServerSocketFactory = createServerSocketFactory();
John Siroisb867b272016-02-12 17:44:01 -0700237 TSSLServerSocket sock1("localhost", 0, pServerSocketFactory);
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800238 sock1.listen();
John Siroisb867b272016-02-12 17:44:01 -0700239 int port = sock1.getPort();
James E. King, III82ae9572017-08-05 12:23:54 -0400240 shared_ptr<TSSLSocketFactory> pClientSocketFactory = createClientSocketFactory();
241 shared_ptr<TSSLSocket> clientSock = pClientSocketFactory->createSocket("localhost", port);
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800242 clientSock->open();
James E. King, III82ae9572017-08-05 12:23:54 -0400243 shared_ptr<TTransport> accepted = sock1.accept();
cyy316723a2019-01-05 16:35:14 +0800244 boost::thread peekThread(std::bind(peekerWorkerInterrupt, accepted));
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800245 clientSock->write((const uint8_t*)"0", 1);
246 boost::this_thread::sleep(boost::posix_time::milliseconds(50));
247 // peekThread is practically guaranteed to be blocking now
248 sock1.interruptChildren();
249 BOOST_CHECK_MESSAGE(peekThread.try_join_for(boost::chrono::milliseconds(200)),
250 "server socket interruptChildren did not interrupt child peek");
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800251 accepted->close();
tpcwangaf81cf02016-10-05 09:48:23 -0700252 clientSock->close();
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800253 sock1.close();
254}
255
256BOOST_AUTO_TEST_CASE(test_ssl_non_interruptable_child_peek) {
James E. King, III82ae9572017-08-05 12:23:54 -0400257 shared_ptr<TSSLSocketFactory> pServerSocketFactory = createServerSocketFactory();
John Siroisb867b272016-02-12 17:44:01 -0700258 TSSLServerSocket sock1("localhost", 0, pServerSocketFactory);
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800259 sock1.setInterruptableChildren(false); // returns to pre-THRIFT-2441 behavior
260 sock1.listen();
John Siroisb867b272016-02-12 17:44:01 -0700261 int port = sock1.getPort();
James E. King, III82ae9572017-08-05 12:23:54 -0400262 shared_ptr<TSSLSocketFactory> pClientSocketFactory = createClientSocketFactory();
263 shared_ptr<TSSLSocket> clientSock = pClientSocketFactory->createSocket("localhost", port);
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800264 clientSock->open();
James E. King, III82ae9572017-08-05 12:23:54 -0400265 shared_ptr<TTransport> accepted = sock1.accept();
266 static_pointer_cast<TSSLSocket>(accepted)->setRecvTimeout(1000);
cyy316723a2019-01-05 16:35:14 +0800267 boost::thread peekThread(std::bind(peekerWorker, accepted, false));
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800268 clientSock->write((const uint8_t*)"0", 1);
269 boost::this_thread::sleep(boost::posix_time::milliseconds(50));
270 // peekThread is practically guaranteed to be blocking now
271 sock1.interruptChildren();
272 BOOST_CHECK_MESSAGE(!peekThread.try_join_for(boost::chrono::milliseconds(200)),
273 "server socket interruptChildren interrupted child peek");
274
tpcwangaf81cf02016-10-05 09:48:23 -0700275 // wait for the receive timeout to kick in
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800276 peekThread.join();
277 accepted->close();
tpcwangaf81cf02016-10-05 09:48:23 -0700278 clientSock->close();
Martin Haimberger0ad6ee92015-11-13 03:18:50 -0800279 sock1.close();
280}
281
282BOOST_AUTO_TEST_SUITE_END()