Skip to content

Commit d8c9dca

Browse files
author
Razvan Becheriu
committed
[#3602] fixed crash in http/https UTs
1 parent efec9a6 commit d8c9dca

8 files changed

+44
-45
lines changed

src/bin/d2/tests/d2_http_command_unittest.cc

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -534,9 +534,8 @@ class HttpCtrlChannelD2Test : public BaseCtrlChannelD2Test {
534534
response = "";
535535
IOServicePtr io_service = getIOService();
536536
ASSERT_TRUE(io_service);
537-
boost::scoped_ptr<TestHttpClient> client;
538-
client.reset(new TestHttpClient(io_service, SERVER_ADDRESS,
539-
SERVER_PORT));
537+
TestHttpClientPtr client(new TestHttpClient(io_service, SERVER_ADDRESS,
538+
SERVER_PORT));
540539
ASSERT_TRUE(client);
541540

542541
// Send the command. This will trigger server's handler which
@@ -620,11 +619,11 @@ class HttpsCtrlChannelD2Test : public BaseCtrlChannelD2Test {
620619
response = "";
621620
IOServicePtr io_service = getIOService();
622621
ASSERT_TRUE(io_service);
623-
boost::scoped_ptr<TestHttpsClient> client;
622+
624623
TlsContextPtr client_tls_context;
625624
configClient(client_tls_context);
626-
client.reset(new TestHttpsClient(io_service, client_tls_context,
627-
SERVER_ADDRESS, SERVER_PORT));
625+
TestHttpsClientPtr client(new TestHttpsClient(io_service, client_tls_context,
626+
SERVER_ADDRESS, SERVER_PORT));
628627
ASSERT_TRUE(client);
629628

630629
// Send the command. This will trigger server's handler which

src/bin/dhcp4/tests/ctrl_dhcp4_srv_unittest.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ class CtrlChannelDhcpv4SrvTest : public ::testing::Test {
132132
LeaseMgrFactory::destroy();
133133
StatsMgr::instance().removeAll();
134134

135-
UnixCommandMgr::instance().closeCommandSocket();
136135
CommandMgr::instance().deregisterAll();
137136
UnixCommandMgr::instance().setConnectionTimeout(TIMEOUT_DHCP_SERVER_RECEIVE_COMMAND);
138137

@@ -532,7 +531,7 @@ TEST_F(CtrlChannelDhcpv4SrvTest, controlChannelShutdown) {
532531
std::string response;
533532

534533
sendUnixCommand("{ \"command\": \"shutdown\" }", response);
535-
EXPECT_EQ("{ \"result\": 0, \"text\": \"Shutting down.\" }",response);
534+
EXPECT_EQ("{ \"result\": 0, \"text\": \"Shutting down.\" }", response);
536535
}
537536

538537
// Tests that the server properly responds to statistics commands. Note this

src/bin/dhcp4/tests/http_control_socket_unittest.cc

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,6 @@ class BaseCtrlChannelDhcpv4Test : public ::testing::Test {
107107
LeaseMgrFactory::destroy();
108108
StatsMgr::instance().removeAll();
109109

110-
if (HttpCommandMgr::instance().getHttpListener()) {
111-
HttpCommandMgr::instance().close();
112-
}
113110
CommandMgr::instance().deregisterAll();
114111
HttpCommandMgr::instance().setConnectionTimeout(TIMEOUT_DHCP_SERVER_RECEIVE_COMMAND);
115112

@@ -623,9 +620,8 @@ class HttpCtrlChannelDhcpv4Test : public BaseCtrlChannelDhcpv4Test {
623620
response = "";
624621
IOServicePtr io_service = getIOService();
625622
ASSERT_TRUE(io_service);
626-
boost::scoped_ptr<TestHttpClient> client;
627-
client.reset(new TestHttpClient(io_service, SERVER_ADDRESS,
628-
SERVER_PORT));
623+
TestHttpClientPtr client(new TestHttpClient(io_service, SERVER_ADDRESS,
624+
SERVER_PORT));
629625
ASSERT_TRUE(client);
630626

631627
// Send the command. This will trigger server's handler which receives
@@ -739,11 +735,11 @@ class HttpsCtrlChannelDhcpv4Test : public BaseCtrlChannelDhcpv4Test {
739735
response = "";
740736
IOServicePtr io_service = getIOService();
741737
ASSERT_TRUE(io_service);
742-
boost::scoped_ptr<TestHttpsClient> client;
738+
743739
TlsContextPtr client_tls_context;
744740
configClient(client_tls_context);
745-
client.reset(new TestHttpsClient(io_service, client_tls_context,
746-
SERVER_ADDRESS, SERVER_PORT));
741+
TestHttpsClientPtr client(new TestHttpsClient(io_service, client_tls_context,
742+
SERVER_ADDRESS, SERVER_PORT));
747743
ASSERT_TRUE(client);
748744

749745
// Send the command. This will trigger server's handler which receives

src/bin/dhcp6/tests/ctrl_dhcp6_srv_unittest.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ class CtrlChannelDhcpv6SrvTest : public CtrlDhcpv6SrvTest {
168168
LeaseMgrFactory::destroy();
169169
StatsMgr::instance().removeAll();
170170

171-
UnixCommandMgr::instance().closeCommandSocket();
172171
CommandMgr::instance().deregisterAll();
173172
UnixCommandMgr::instance().setConnectionTimeout(TIMEOUT_DHCP_SERVER_RECEIVE_COMMAND);
174173

@@ -558,7 +557,7 @@ TEST_F(CtrlChannelDhcpv6SrvTest, controlChannelShutdown) {
558557
std::string response;
559558

560559
sendUnixCommand("{ \"command\": \"shutdown\" }", response);
561-
EXPECT_EQ("{ \"result\": 0, \"text\": \"Shutting down.\" }",response);
560+
EXPECT_EQ("{ \"result\": 0, \"text\": \"Shutting down.\" }", response);
562561
}
563562

564563
// Tests that the server properly responds to statistics commands. Note this

src/bin/dhcp6/tests/http_control_socket_unittest.cc

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,6 @@ class BaseCtrlChannelDhcpv6Test : public HttpCtrlDhcpv6Test {
145145
LeaseMgrFactory::destroy();
146146
StatsMgr::instance().removeAll();
147147

148-
if (HttpCommandMgr::instance().getHttpListener()) {
149-
HttpCommandMgr::instance().close();
150-
}
151148
CommandMgr::instance().deregisterAll();
152149
HttpCommandMgr::instance().setConnectionTimeout(TIMEOUT_DHCP_SERVER_RECEIVE_COMMAND);
153150

@@ -650,9 +647,8 @@ class HttpCtrlChannelDhcpv6Test : public BaseCtrlChannelDhcpv6Test {
650647
response = "";
651648
IOServicePtr io_service = getIOService();
652649
ASSERT_TRUE(io_service);
653-
boost::scoped_ptr<TestHttpClient> client;
654-
client.reset(new TestHttpClient(io_service, SERVER_ADDRESS,
655-
SERVER_PORT));
650+
TestHttpClientPtr client(new TestHttpClient(io_service, SERVER_ADDRESS,
651+
SERVER_PORT));
656652
ASSERT_TRUE(client);
657653

658654
// Send the command. This will trigger server's handler which receives
@@ -766,11 +762,11 @@ class HttpsCtrlChannelDhcpv6Test : public BaseCtrlChannelDhcpv6Test {
766762
response = "";
767763
IOServicePtr io_service = getIOService();
768764
ASSERT_TRUE(io_service);
769-
boost::scoped_ptr<TestHttpsClient> client;
765+
770766
TlsContextPtr client_tls_context;
771767
configClient(client_tls_context);
772-
client.reset(new TestHttpsClient(io_service, client_tls_context,
773-
SERVER_ADDRESS, SERVER_PORT));
768+
TestHttpsClientPtr client(new TestHttpsClient(io_service, client_tls_context,
769+
SERVER_ADDRESS, SERVER_PORT));
774770
ASSERT_TRUE(client);
775771

776772
// Send the command. This will trigger server's handler which receives

src/lib/http/listener_impl.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ HttpListenerImpl::accept() {
116116
// depends on the use case.
117117
HttpResponseCreatorPtr response_creator = creator_factory_->create();
118118
HttpAcceptorCallback acceptor_callback =
119-
std::bind(&HttpListenerImpl::acceptHandler, this, ph::_1);
119+
std::bind(&HttpListenerImpl::acceptHandler, shared_from_this(), ph::_1);
120120
HttpConnectionPtr conn = createConnection(response_creator,
121121
acceptor_callback);
122122
// Transmit the use external sockets flag.

src/lib/http/listener_impl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313
#include <http/connection.h>
1414
#include <http/connection_pool.h>
1515
#include <http/response_creator_factory.h>
16+
#include <boost/enable_shared_from_this.hpp>
1617
#include <boost/scoped_ptr.hpp>
1718

1819
namespace isc {
1920
namespace http {
2021

2122
/// @brief Implementation of the @ref HttpListener.
22-
class HttpListenerImpl {
23+
class HttpListenerImpl : public boost::enable_shared_from_this<HttpListenerImpl> {
2324
public:
2425

2526
/// @brief Constructor.

src/lib/http/testutils/test_http_client.h

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <boost/asio/read.hpp>
1616
#include <boost/asio/buffer.hpp>
1717
#include <boost/asio/ip/tcp.hpp>
18+
#include <boost/enable_shared_from_this.hpp>
1819
#include <gtest/gtest.h>
1920

2021
using namespace boost::asio::ip;
@@ -85,7 +86,8 @@ class BaseTestHttpClient : public boost::noncopyable {
8586
};
8687

8788
/// @brief Entity which can connect to the HTTP server endpoint.
88-
class TestHttpClient : public BaseTestHttpClient {
89+
class TestHttpClient : public BaseTestHttpClient,
90+
public boost::enable_shared_from_this<TestHttpClient> {
8991
public:
9092

9193
/// @brief Constructor.
@@ -116,8 +118,9 @@ class TestHttpClient : public BaseTestHttpClient {
116118
/// @param request HTTP request in the textual format.
117119
virtual void startRequest(const std::string& request) {
118120
tcp::endpoint endpoint(address::from_string(server_address_), server_port_);
121+
auto ref = shared_from_this();
119122
socket_.async_connect(endpoint,
120-
[this, request](const boost::system::error_code& ec) {
123+
[this, ref, request](const boost::system::error_code& ec) {
121124
receive_done_ = false;
122125
if (ec) {
123126
// One would expect that async_connect wouldn't return
@@ -150,9 +153,10 @@ class TestHttpClient : public BaseTestHttpClient {
150153
/// @param request part of the HTTP request to be sent.
151154
virtual void sendPartialRequest(std::string request) {
152155
size_t chuck_size = std::min(TEST_HTTP_CHUCK_SIZE, request.size());
156+
auto ref = shared_from_this();
153157
socket_.async_send(boost::asio::buffer(request.data(), chuck_size),
154-
[this, request](const boost::system::error_code& ec,
155-
std::size_t bytes_transferred) mutable {
158+
[this, ref, request](const boost::system::error_code& ec,
159+
std::size_t bytes_transferred) mutable {
156160
if (ec) {
157161
if (ec.value() == boost::asio::error::operation_aborted) {
158162
return;
@@ -189,9 +193,10 @@ class TestHttpClient : public BaseTestHttpClient {
189193

190194
/// @brief Receive response from the server.
191195
virtual void receivePartialResponse() {
192-
socket_.async_read_some(boost::asio::buffer(buf_.data(), buf_.size()),
193-
[this](const boost::system::error_code& ec,
194-
std::size_t bytes_transferred) {
196+
auto ref = shared_from_this();
197+
socket_.async_read_some(boost::asio::buffer(ref->buf_.data(), ref->buf_.size()),
198+
[this, ref](const boost::system::error_code& ec,
199+
std::size_t bytes_transferred) {
195200
if (ec) {
196201
// IO service stopped so simply return.
197202
if (ec.value() == boost::asio::error::operation_aborted) {
@@ -354,7 +359,8 @@ class TestHttpClient : public BaseTestHttpClient {
354359
typedef boost::shared_ptr<TestHttpClient> TestHttpClientPtr;
355360

356361
/// @brief Entity which can connect to the HTTPS server endpoint.
357-
class TestHttpsClient : public BaseTestHttpClient {
362+
class TestHttpsClient : public BaseTestHttpClient,
363+
public boost::enable_shared_from_this<TestHttpsClient> {
358364
public:
359365

360366
/// @brief Constructor.
@@ -388,8 +394,9 @@ class TestHttpsClient : public BaseTestHttpClient {
388394
virtual void startRequest(const std::string& request) {
389395
tcp::endpoint endpoint(address::from_string(server_address_),
390396
server_port_);
397+
auto ref = shared_from_this();
391398
stream_.lowest_layer().async_connect(endpoint,
392-
[this, request](const boost::system::error_code& ec) {
399+
[this, ref, request](const boost::system::error_code& ec) {
393400
receive_done_ = false;
394401
if (ec) {
395402
// One would expect that async_connect wouldn't return
@@ -431,10 +438,11 @@ class TestHttpsClient : public BaseTestHttpClient {
431438
/// @param request part of the HTTP request to be sent.
432439
virtual void sendPartialRequest(std::string request) {
433440
size_t chuck_size = std::min(TEST_HTTP_CHUCK_SIZE, request.size());
441+
auto ref = shared_from_this();
434442
boost::asio::async_write(stream_,
435-
boost::asio::buffer(request.data(), chuck_size),
436-
[this, request](const boost::system::error_code& ec,
437-
std::size_t bytes_transferred) mutable {
443+
boost::asio::buffer(request.data(), chuck_size),
444+
[this, ref, request](const boost::system::error_code& ec,
445+
std::size_t bytes_transferred) mutable {
438446
if (ec) {
439447
if (ec.value() == boost::asio::error::operation_aborted) {
440448
return;
@@ -471,9 +479,10 @@ class TestHttpsClient : public BaseTestHttpClient {
471479

472480
/// @brief Receive response from the server.
473481
virtual void receivePartialResponse() {
474-
stream_.async_read_some(boost::asio::buffer(buf_.data(), buf_.size()),
475-
[this](const boost::system::error_code& ec,
476-
std::size_t bytes_transferred) {
482+
auto ref = shared_from_this();
483+
stream_.async_read_some(boost::asio::buffer(ref->buf_.data(), ref->buf_.size()),
484+
[this, ref](const boost::system::error_code& ec,
485+
std::size_t bytes_transferred) {
477486
if (ec) {
478487
// IO service stopped so simply return.
479488
if (ec.value() == boost::asio::error::operation_aborted) {

0 commit comments

Comments
 (0)