]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#3602] fixed crash in http/https UTs
authorRazvan Becheriu <razvan@isc.org>
Mon, 23 Sep 2024 18:55:04 +0000 (21:55 +0300)
committerRazvan Becheriu <razvan@isc.org>
Mon, 28 Oct 2024 07:37:04 +0000 (09:37 +0200)
src/bin/d2/tests/d2_http_command_unittest.cc
src/bin/dhcp4/tests/ctrl_dhcp4_srv_unittest.cc
src/bin/dhcp4/tests/http_control_socket_unittest.cc
src/bin/dhcp6/tests/ctrl_dhcp6_srv_unittest.cc
src/bin/dhcp6/tests/http_control_socket_unittest.cc
src/lib/http/listener_impl.cc
src/lib/http/listener_impl.h
src/lib/http/testutils/test_http_client.h

index e47a3155d4fbf6c32f4258f8af123bf9dbc917b2..ee17de88d02d481e1dc7d87c7a778d351f366aa4 100644 (file)
@@ -534,9 +534,8 @@ public:
         response = "";
         IOServicePtr io_service = getIOService();
         ASSERT_TRUE(io_service);
-        boost::scoped_ptr<TestHttpClient> client;
-        client.reset(new TestHttpClient(io_service, SERVER_ADDRESS,
-                                        SERVER_PORT));
+        TestHttpClientPtr client(new TestHttpClient(io_service, SERVER_ADDRESS,
+                                                    SERVER_PORT));
         ASSERT_TRUE(client);
 
         // Send the command. This will trigger server's handler which
@@ -620,11 +619,11 @@ public:
         response = "";
         IOServicePtr io_service = getIOService();
         ASSERT_TRUE(io_service);
-        boost::scoped_ptr<TestHttpsClient> client;
+
         TlsContextPtr client_tls_context;
         configClient(client_tls_context);
-        client.reset(new TestHttpsClient(io_service, client_tls_context,
-                                         SERVER_ADDRESS, SERVER_PORT));
+        TestHttpsClientPtr client(new TestHttpsClient(io_service, client_tls_context,
+                                                      SERVER_ADDRESS, SERVER_PORT));
         ASSERT_TRUE(client);
 
         // Send the command. This will trigger server's handler which
index efd0fc4bdea912e6623918f44e090578421425d2..7e86c31bd487bb05c68cfc4a25d69f22523fab9b 100644 (file)
@@ -132,7 +132,6 @@ public:
         LeaseMgrFactory::destroy();
         StatsMgr::instance().removeAll();
 
-        UnixCommandMgr::instance().closeCommandSocket();
         CommandMgr::instance().deregisterAll();
         UnixCommandMgr::instance().setConnectionTimeout(TIMEOUT_DHCP_SERVER_RECEIVE_COMMAND);
 
@@ -532,7 +531,7 @@ TEST_F(CtrlChannelDhcpv4SrvTest, controlChannelShutdown) {
     std::string response;
 
     sendUnixCommand("{ \"command\": \"shutdown\" }", response);
-    EXPECT_EQ("{ \"result\": 0, \"text\": \"Shutting down.\" }",response);
+    EXPECT_EQ("{ \"result\": 0, \"text\": \"Shutting down.\" }", response);
 }
 
 // Tests that the server properly responds to statistics commands.  Note this
index 78e32e4ce95537b0c9ef4162e7980b04993f8326..d53df7f130ac514a25671aeb7e3e843963fd0003 100644 (file)
@@ -107,9 +107,6 @@ public:
         LeaseMgrFactory::destroy();
         StatsMgr::instance().removeAll();
 
-        if (HttpCommandMgr::instance().getHttpListener()) {
-            HttpCommandMgr::instance().close();
-        }
         CommandMgr::instance().deregisterAll();
         HttpCommandMgr::instance().setConnectionTimeout(TIMEOUT_DHCP_SERVER_RECEIVE_COMMAND);
 
@@ -623,9 +620,8 @@ public:
         response = "";
         IOServicePtr io_service = getIOService();
         ASSERT_TRUE(io_service);
-        boost::scoped_ptr<TestHttpClient> client;
-        client.reset(new TestHttpClient(io_service, SERVER_ADDRESS,
-                                        SERVER_PORT));
+        TestHttpClientPtr client(new TestHttpClient(io_service, SERVER_ADDRESS,
+                                                    SERVER_PORT));
         ASSERT_TRUE(client);
 
         // Send the command. This will trigger server's handler which receives
@@ -739,11 +735,11 @@ public:
         response = "";
         IOServicePtr io_service = getIOService();
         ASSERT_TRUE(io_service);
-        boost::scoped_ptr<TestHttpsClient> client;
+
         TlsContextPtr client_tls_context;
         configClient(client_tls_context);
-        client.reset(new TestHttpsClient(io_service, client_tls_context,
-                                         SERVER_ADDRESS, SERVER_PORT));
+        TestHttpsClientPtr client(new TestHttpsClient(io_service, client_tls_context,
+                                                      SERVER_ADDRESS, SERVER_PORT));
         ASSERT_TRUE(client);
 
         // Send the command. This will trigger server's handler which receives
index cfe918eaa501be80cc0b94cecf1c4c704c4d8860..16b1949a5c43d109fb12266fbdf96042bd1be54e 100644 (file)
@@ -168,7 +168,6 @@ public:
         LeaseMgrFactory::destroy();
         StatsMgr::instance().removeAll();
 
-        UnixCommandMgr::instance().closeCommandSocket();
         CommandMgr::instance().deregisterAll();
         UnixCommandMgr::instance().setConnectionTimeout(TIMEOUT_DHCP_SERVER_RECEIVE_COMMAND);
 
@@ -558,7 +557,7 @@ TEST_F(CtrlChannelDhcpv6SrvTest, controlChannelShutdown) {
     std::string response;
 
     sendUnixCommand("{ \"command\": \"shutdown\" }", response);
-    EXPECT_EQ("{ \"result\": 0, \"text\": \"Shutting down.\" }",response);
+    EXPECT_EQ("{ \"result\": 0, \"text\": \"Shutting down.\" }", response);
 }
 
 // Tests that the server properly responds to statistics commands.  Note this
index a2cc4b772415eb6926a5b2fe8dbab59e1909470a..4487d645ce034b71db9cae0c17f4d48b62013551 100644 (file)
@@ -145,9 +145,6 @@ public:
         LeaseMgrFactory::destroy();
         StatsMgr::instance().removeAll();
 
-        if (HttpCommandMgr::instance().getHttpListener()) {
-            HttpCommandMgr::instance().close();
-        }
         CommandMgr::instance().deregisterAll();
         HttpCommandMgr::instance().setConnectionTimeout(TIMEOUT_DHCP_SERVER_RECEIVE_COMMAND);
 
@@ -650,9 +647,8 @@ public:
         response = "";
         IOServicePtr io_service = getIOService();
         ASSERT_TRUE(io_service);
-        boost::scoped_ptr<TestHttpClient> client;
-        client.reset(new TestHttpClient(io_service, SERVER_ADDRESS,
-                                        SERVER_PORT));
+        TestHttpClientPtr client(new TestHttpClient(io_service, SERVER_ADDRESS,
+                                                    SERVER_PORT));
         ASSERT_TRUE(client);
 
         // Send the command. This will trigger server's handler which receives
@@ -766,11 +762,11 @@ public:
         response = "";
         IOServicePtr io_service = getIOService();
         ASSERT_TRUE(io_service);
-        boost::scoped_ptr<TestHttpsClient> client;
+
         TlsContextPtr client_tls_context;
         configClient(client_tls_context);
-        client.reset(new TestHttpsClient(io_service, client_tls_context,
-                                         SERVER_ADDRESS, SERVER_PORT));
+        TestHttpsClientPtr client(new TestHttpsClient(io_service, client_tls_context,
+                                                      SERVER_ADDRESS, SERVER_PORT));
         ASSERT_TRUE(client);
 
         // Send the command. This will trigger server's handler which receives
index cd68e0901120fcb79fb535eea8ecd6d140683437..a5df194c1649cf3df916194d5e4d60c6c36db0a2 100644 (file)
@@ -116,7 +116,7 @@ HttpListenerImpl::accept() {
     // depends on the use case.
     HttpResponseCreatorPtr response_creator = creator_factory_->create();
     HttpAcceptorCallback acceptor_callback =
-        std::bind(&HttpListenerImpl::acceptHandler, this, ph::_1);
+        std::bind(&HttpListenerImpl::acceptHandler, shared_from_this(), ph::_1);
     HttpConnectionPtr conn = createConnection(response_creator,
                                               acceptor_callback);
     // Transmit the use external sockets flag.
index 246ead4e274d6002083934e0ebb76c182fc5ecdf..f65cd3fa6dbf979bdbf51c999fdb8f0fb7944990 100644 (file)
 #include <http/connection.h>
 #include <http/connection_pool.h>
 #include <http/response_creator_factory.h>
+#include <boost/enable_shared_from_this.hpp>
 #include <boost/scoped_ptr.hpp>
 
 namespace isc {
 namespace http {
 
 /// @brief Implementation of the @ref HttpListener.
-class HttpListenerImpl {
+class HttpListenerImpl : public boost::enable_shared_from_this<HttpListenerImpl> {
 public:
 
     /// @brief Constructor.
index 99b7689bf68b4e70bd10e87a64f571fe89ff5baf..c1d9deace9307b4c309695ba1181677e95ce419f 100644 (file)
@@ -15,6 +15,7 @@
 #include <boost/asio/read.hpp>
 #include <boost/asio/buffer.hpp>
 #include <boost/asio/ip/tcp.hpp>
+#include <boost/enable_shared_from_this.hpp>
 #include <gtest/gtest.h>
 
 using namespace boost::asio::ip;
@@ -85,7 +86,8 @@ public:
 };
 
 /// @brief Entity which can connect to the HTTP server endpoint.
-class TestHttpClient : public BaseTestHttpClient {
+class TestHttpClient : public BaseTestHttpClient,
+                       public boost::enable_shared_from_this<TestHttpClient> {
 public:
 
     /// @brief Constructor.
@@ -116,8 +118,9 @@ public:
     /// @param request HTTP request in the textual format.
     virtual void startRequest(const std::string& request) {
         tcp::endpoint endpoint(address::from_string(server_address_), server_port_);
+        auto ref = shared_from_this();
         socket_.async_connect(endpoint,
-        [this, request](const boost::system::error_code& ec) {
+                              [this, ref, request](const boost::system::error_code& ec) {
             receive_done_ = false;
             if (ec) {
                 // One would expect that async_connect wouldn't return
@@ -150,9 +153,10 @@ public:
     /// @param request part of the HTTP request to be sent.
     virtual void sendPartialRequest(std::string request) {
         size_t chuck_size = std::min(TEST_HTTP_CHUCK_SIZE, request.size());
+        auto ref = shared_from_this();
         socket_.async_send(boost::asio::buffer(request.data(), chuck_size),
-                           [this, request](const boost::system::error_code& ec,
-                                           std::size_t bytes_transferred) mutable {
+                           [this, ref, request](const boost::system::error_code& ec,
+                                                std::size_t bytes_transferred) mutable {
             if (ec) {
                 if (ec.value() == boost::asio::error::operation_aborted) {
                     return;
@@ -189,9 +193,10 @@ public:
 
     /// @brief Receive response from the server.
     virtual void receivePartialResponse() {
-        socket_.async_read_some(boost::asio::buffer(buf_.data(), buf_.size()),
-                                [this](const boost::system::error_code& ec,
-                                       std::size_t bytes_transferred) {
+        auto ref = shared_from_this();
+        socket_.async_read_some(boost::asio::buffer(ref->buf_.data(), ref->buf_.size()),
+                                [this, ref](const boost::system::error_code& ec,
+                                            std::size_t bytes_transferred) {
             if (ec) {
                 // IO service stopped so simply return.
                 if (ec.value() == boost::asio::error::operation_aborted) {
@@ -354,7 +359,8 @@ private:
 typedef boost::shared_ptr<TestHttpClient> TestHttpClientPtr;
 
 /// @brief Entity which can connect to the HTTPS server endpoint.
-class TestHttpsClient : public BaseTestHttpClient {
+class TestHttpsClient : public BaseTestHttpClient,
+                        public boost::enable_shared_from_this<TestHttpsClient> {
 public:
 
     /// @brief Constructor.
@@ -388,8 +394,9 @@ public:
     virtual void startRequest(const std::string& request) {
         tcp::endpoint endpoint(address::from_string(server_address_),
                                server_port_);
+        auto ref = shared_from_this();
         stream_.lowest_layer().async_connect(endpoint,
-        [this, request](const boost::system::error_code& ec) {
+                                             [this, ref, request](const boost::system::error_code& ec) {
             receive_done_ = false;
             if (ec) {
                 // One would expect that async_connect wouldn't return
@@ -431,10 +438,11 @@ public:
     /// @param request part of the HTTP request to be sent.
     virtual void sendPartialRequest(std::string request) {
         size_t chuck_size = std::min(TEST_HTTP_CHUCK_SIZE, request.size());
+        auto ref = shared_from_this();
         boost::asio::async_write(stream_,
-                boost::asio::buffer(request.data(), chuck_size),
-                [this, request](const boost::system::error_code& ec,
-                                std::size_t bytes_transferred) mutable {
+                                 boost::asio::buffer(request.data(), chuck_size),
+                                 [this, ref, request](const boost::system::error_code& ec,
+                                                      std::size_t bytes_transferred) mutable {
             if (ec) {
                 if (ec.value() == boost::asio::error::operation_aborted) {
                     return;
@@ -471,9 +479,10 @@ public:
 
     /// @brief Receive response from the server.
     virtual void receivePartialResponse() {
-        stream_.async_read_some(boost::asio::buffer(buf_.data(), buf_.size()),
-                                [this](const boost::system::error_code& ec,
-                                       std::size_t bytes_transferred) {
+        auto ref = shared_from_this();
+        stream_.async_read_some(boost::asio::buffer(ref->buf_.data(), ref->buf_.size()),
+                                [this, ref](const boost::system::error_code& ec,
+                                            std::size_t bytes_transferred) {
             if (ec) {
                 // IO service stopped so simply return.
                 if (ec.value() == boost::asio::error::operation_aborted) {