2016-04-20 20:14:51 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
2021-01-19 20:40:54 -06:00
|
|
|
#include <chrono>
|
2017-12-20 07:06:26 -06:00
|
|
|
#include <config.h>
|
2016-04-20 20:14:51 -05:00
|
|
|
|
2016-04-25 01:25:23 -05:00
|
|
|
#include <errno.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
2016-04-20 20:14:51 -05:00
|
|
|
#include <Poco/Dynamic/Var.h>
|
|
|
|
#include <Poco/JSON/JSON.h>
|
|
|
|
#include <Poco/JSON/Parser.h>
|
|
|
|
#include <Poco/Net/AcceptCertificateHandler.h>
|
|
|
|
#include <Poco/Net/HTTPResponse.h>
|
|
|
|
#include <Poco/Net/InvalidCertificateHandler.h>
|
|
|
|
#include <Poco/Net/NetException.h>
|
|
|
|
#include <Poco/Net/PrivateKeyPassphraseHandler.h>
|
|
|
|
#include <Poco/Net/SSLManager.h>
|
|
|
|
#include <Poco/Net/Socket.h>
|
|
|
|
#include <Poco/Path.h>
|
|
|
|
#include <Poco/StreamCopier.h>
|
|
|
|
#include <Poco/URI.h>
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
|
|
|
#include <Common.hpp>
|
|
|
|
#include <UserMessages.hpp>
|
|
|
|
#include <Util.hpp>
|
2016-11-24 08:56:06 -06:00
|
|
|
#include <Protocol.hpp>
|
2020-01-21 03:00:52 -06:00
|
|
|
#include <test.hpp>
|
2017-12-20 07:06:26 -06:00
|
|
|
#include <helpers.hpp>
|
2021-11-18 06:08:14 -06:00
|
|
|
#include <countcoolkits.hpp>
|
2016-04-28 21:47:09 -05:00
|
|
|
|
|
|
|
using namespace helpers;
|
2016-04-20 20:14:51 -05:00
|
|
|
|
2021-11-18 06:08:14 -06:00
|
|
|
/// Tests the HTTP WebSocket API of coolwsd. The server has to be started manually before running this test.
|
2016-04-20 20:14:51 -05:00
|
|
|
class HTTPCrashTest : public CPPUNIT_NS::TestFixture
|
|
|
|
{
|
|
|
|
const Poco::URI _uri;
|
|
|
|
Poco::Net::HTTPResponse _response;
|
2021-04-12 23:48:47 -05:00
|
|
|
std::shared_ptr<SocketPoll> _socketPoll;
|
2016-04-20 20:14:51 -05:00
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(HTTPCrashTest);
|
|
|
|
|
2016-04-24 11:35:29 -05:00
|
|
|
CPPUNIT_TEST(testBarren);
|
2017-04-16 23:39:56 -05:00
|
|
|
CPPUNIT_TEST(testCrashKit);
|
2017-03-20 20:33:59 -05:00
|
|
|
CPPUNIT_TEST(testRecoverAfterKitCrash);
|
2016-11-11 21:38:14 -06:00
|
|
|
CPPUNIT_TEST(testCrashForkit);
|
2016-04-20 20:14:51 -05:00
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
2016-04-24 11:35:29 -05:00
|
|
|
void testBarren();
|
2016-04-20 20:14:51 -05:00
|
|
|
void testCrashKit();
|
2017-03-20 20:33:59 -05:00
|
|
|
void testRecoverAfterKitCrash();
|
2016-11-11 21:38:14 -06:00
|
|
|
void testCrashForkit();
|
2016-04-20 20:14:51 -05:00
|
|
|
|
2016-04-24 11:31:01 -05:00
|
|
|
static
|
2017-09-19 15:16:44 -05:00
|
|
|
void killLoKitProcesses();
|
|
|
|
void killForkitProcess();
|
2016-04-24 11:31:01 -05:00
|
|
|
|
2016-04-20 20:14:51 -05:00
|
|
|
public:
|
|
|
|
HTTPCrashTest()
|
2016-05-03 02:40:25 -05:00
|
|
|
: _uri(helpers::getTestServerURI())
|
2021-04-12 23:48:47 -05:00
|
|
|
, _socketPoll(std::make_shared<SocketPoll>("HttpCrashPoll"))
|
2016-04-20 20:14:51 -05:00
|
|
|
{
|
|
|
|
#if ENABLE_SSL
|
|
|
|
Poco::Net::initializeSSL();
|
|
|
|
// Just accept the certificate anyway for testing purposes
|
|
|
|
Poco::SharedPtr<Poco::Net::InvalidCertificateHandler> invalidCertHandler = new Poco::Net::AcceptCertificateHandler(false);
|
|
|
|
Poco::Net::Context::Params sslParams;
|
|
|
|
Poco::Net::Context::Ptr sslContext = new Poco::Net::Context(Poco::Net::Context::CLIENT_USE, sslParams);
|
2016-12-22 08:04:07 -06:00
|
|
|
Poco::Net::SSLManager::instance().initializeClient(nullptr, invalidCertHandler, sslContext);
|
2016-04-20 20:14:51 -05:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
~HTTPCrashTest()
|
|
|
|
{
|
2021-03-31 15:50:21 -05:00
|
|
|
#if ENABLE_SSL
|
2016-04-20 20:14:51 -05:00
|
|
|
Poco::Net::uninitializeSSL();
|
|
|
|
#endif
|
2021-03-31 15:50:21 -05:00
|
|
|
}
|
2016-04-20 20:14:51 -05:00
|
|
|
|
|
|
|
void setUp()
|
|
|
|
{
|
2018-01-22 08:11:43 -06:00
|
|
|
resetTestStartTime();
|
2021-11-18 06:08:14 -06:00
|
|
|
testCountHowManyCoolkits();
|
2018-01-22 08:11:43 -06:00
|
|
|
resetTestStartTime();
|
2021-04-12 23:48:47 -05:00
|
|
|
_socketPoll->startThread();
|
2016-04-20 20:14:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void tearDown()
|
|
|
|
{
|
2021-04-12 23:48:47 -05:00
|
|
|
_socketPoll->joinThread();
|
2018-01-22 08:11:43 -06:00
|
|
|
resetTestStartTime();
|
2021-11-18 06:08:14 -06:00
|
|
|
testNoExtraCoolKitsLeft();
|
2018-01-22 08:11:43 -06:00
|
|
|
resetTestStartTime();
|
2016-04-20 20:14:51 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-04-24 11:35:29 -05:00
|
|
|
void HTTPCrashTest::testBarren()
|
|
|
|
{
|
2019-05-21 02:10:32 -05:00
|
|
|
#if 0 // FIXME why does this fail?
|
2016-04-24 11:35:29 -05:00
|
|
|
// Kill all kit processes and try loading a document.
|
2018-02-07 03:17:42 -06:00
|
|
|
const char* testname = "barren ";
|
2016-04-24 11:35:29 -05:00
|
|
|
try
|
|
|
|
{
|
2017-09-19 15:16:44 -05:00
|
|
|
killLoKitProcesses();
|
2021-11-18 06:08:14 -06:00
|
|
|
countCoolKitProcesses(0);
|
2016-04-24 11:35:29 -05:00
|
|
|
|
2018-01-22 08:11:43 -06:00
|
|
|
TST_LOG("Loading after kill.");
|
2016-05-12 09:59:00 -05:00
|
|
|
|
2016-04-24 11:35:29 -05:00
|
|
|
// Load a document and get its status.
|
2021-03-31 15:50:21 -05:00
|
|
|
std::shared_ptr<http::WebSocketSession> socket
|
|
|
|
= loadDocAndGetSession(_socketPoll, "hello.odt", _uri, testname);
|
2016-10-22 10:35:22 -05:00
|
|
|
|
|
|
|
sendTextFrame(socket, "status", testname);
|
|
|
|
assertResponseString(socket, "status:", testname);
|
2021-03-31 15:50:21 -05:00
|
|
|
|
2021-04-12 23:48:47 -05:00
|
|
|
socket->asyncShutdown();
|
2021-03-31 15:50:21 -05:00
|
|
|
|
|
|
|
LOK_ASSERT_MESSAGE("Expected successful disconnection of the WebSocket",
|
|
|
|
socket->waitForDisconnection(std::chrono::seconds(5)));
|
2016-04-24 11:35:29 -05:00
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT_FAIL(exc.displayText());
|
2016-04-24 11:35:29 -05:00
|
|
|
}
|
2019-05-21 02:10:32 -05:00
|
|
|
#endif
|
2016-04-24 11:35:29 -05:00
|
|
|
}
|
|
|
|
|
2016-04-20 20:14:51 -05:00
|
|
|
void HTTPCrashTest::testCrashKit()
|
|
|
|
{
|
2018-02-07 03:17:42 -06:00
|
|
|
const char* testname = "crashKit ";
|
2016-04-20 20:14:51 -05:00
|
|
|
try
|
|
|
|
{
|
2021-03-31 15:50:21 -05:00
|
|
|
std::shared_ptr<http::WebSocketSession> socket
|
|
|
|
= loadDocAndGetSession(_socketPoll, "empty.odt", _uri, testname);
|
2016-04-20 20:14:51 -05:00
|
|
|
|
2019-05-04 16:50:00 -05:00
|
|
|
TST_LOG("Allowing time for kits to spawn and connect to wsd to get cleanly killed");
|
2020-12-24 07:55:12 -06:00
|
|
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
2019-05-04 16:50:00 -05:00
|
|
|
|
2021-11-18 06:08:14 -06:00
|
|
|
TST_LOG("Killing coolkit instances.");
|
2017-02-28 21:31:36 -06:00
|
|
|
|
2017-09-19 15:16:44 -05:00
|
|
|
killLoKitProcesses();
|
2021-11-18 06:08:14 -06:00
|
|
|
countCoolKitProcesses(0, std::chrono::seconds(1));
|
2016-04-20 20:14:51 -05:00
|
|
|
|
2020-12-24 07:55:12 -06:00
|
|
|
TST_LOG("Reading the error code from the socket.");
|
2021-03-31 15:50:21 -05:00
|
|
|
//FIXME: implement in WebSocketSession.
|
|
|
|
// std::string message;
|
|
|
|
// const int statusCode = getErrorCode(socket, message, testname);
|
|
|
|
// LOK_ASSERT_EQUAL(static_cast<int>(Poco::Net::WebSocket::WS_ENDPOINT_GOING_AWAY), statusCode);
|
2016-04-20 20:14:51 -05:00
|
|
|
|
|
|
|
// respond close frame
|
2018-01-22 08:11:43 -06:00
|
|
|
TST_LOG("Shutting down socket.");
|
2021-04-12 23:48:47 -05:00
|
|
|
socket->asyncShutdown();
|
2021-03-31 15:50:21 -05:00
|
|
|
|
|
|
|
LOK_ASSERT_MESSAGE("Expected successful disconnection of the WebSocket",
|
|
|
|
socket->waitForDisconnection(std::chrono::seconds(5)));
|
2016-04-20 20:14:51 -05:00
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT_FAIL(exc.displayText());
|
2016-04-20 20:14:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-20 20:33:59 -05:00
|
|
|
void HTTPCrashTest::testRecoverAfterKitCrash()
|
|
|
|
{
|
2018-02-07 03:17:42 -06:00
|
|
|
const char* testname = "recoverAfterKitCrash ";
|
2017-03-20 20:33:59 -05:00
|
|
|
try
|
|
|
|
{
|
2021-03-31 15:50:21 -05:00
|
|
|
std::shared_ptr<http::WebSocketSession> socket1
|
|
|
|
= loadDocAndGetSession(_socketPoll, "empty.odt", _uri, testname);
|
2017-03-20 20:33:59 -05:00
|
|
|
|
2021-01-19 20:40:54 -06:00
|
|
|
TST_LOG("Allowing time for kits to spawn and connect to wsd to get cleanly killed");
|
|
|
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
|
|
|
|
2021-11-18 06:08:14 -06:00
|
|
|
TST_LOG("Killing coolkit instances.");
|
2017-03-20 20:33:59 -05:00
|
|
|
|
2017-09-19 15:16:44 -05:00
|
|
|
killLoKitProcesses();
|
2021-11-18 06:08:14 -06:00
|
|
|
countCoolKitProcesses(0, std::chrono::seconds(1));
|
2017-03-20 20:33:59 -05:00
|
|
|
|
|
|
|
// We expect the client connection to close.
|
2018-01-22 08:11:43 -06:00
|
|
|
TST_LOG("Reconnect after kill.");
|
2017-03-20 20:33:59 -05:00
|
|
|
|
2021-03-31 15:50:21 -05:00
|
|
|
std::shared_ptr<http::WebSocketSession> socket2 = loadDocAndGetSession(
|
|
|
|
_socketPoll, "empty.odt", _uri, testname, /*isView=*/true, /*isAssert=*/false);
|
2019-08-30 05:22:04 -05:00
|
|
|
if (!socket2)
|
|
|
|
{
|
|
|
|
// In case still starting up.
|
|
|
|
sleep(2);
|
2021-03-31 15:50:21 -05:00
|
|
|
socket2 = loadDocAndGetSession(_socketPoll, "empty.odt", _uri, testname);
|
2019-08-30 05:22:04 -05:00
|
|
|
}
|
2021-03-31 15:50:21 -05:00
|
|
|
|
2017-03-20 20:33:59 -05:00
|
|
|
sendTextFrame(socket2, "status", testname);
|
|
|
|
assertResponseString(socket2, "status:", testname);
|
2021-03-31 15:50:21 -05:00
|
|
|
|
2021-04-12 23:48:47 -05:00
|
|
|
socket2->asyncShutdown();
|
|
|
|
socket1->asyncShutdown();
|
2021-03-31 15:50:21 -05:00
|
|
|
|
|
|
|
LOK_ASSERT_MESSAGE("Expected successful disconnection of the WebSocket 2",
|
|
|
|
socket2->waitForDisconnection(std::chrono::seconds(5)));
|
|
|
|
LOK_ASSERT_MESSAGE("Expected successful disconnection of the WebSocket 1",
|
|
|
|
socket1->waitForDisconnection(std::chrono::seconds(5)));
|
2017-03-20 20:33:59 -05:00
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT_FAIL(exc.displayText());
|
2017-03-20 20:33:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-11 21:38:14 -06:00
|
|
|
void HTTPCrashTest::testCrashForkit()
|
|
|
|
{
|
2018-02-07 03:17:42 -06:00
|
|
|
const char* testname = "crashForkit ";
|
2016-11-11 21:38:14 -06:00
|
|
|
try
|
|
|
|
{
|
2021-03-31 15:50:21 -05:00
|
|
|
std::shared_ptr<http::WebSocketSession> socket
|
|
|
|
= loadDocAndGetSession(_socketPoll, "empty.odt", _uri, testname);
|
2016-11-11 21:38:14 -06:00
|
|
|
|
2018-01-22 08:11:43 -06:00
|
|
|
TST_LOG("Killing forkit.");
|
2017-09-19 15:16:44 -05:00
|
|
|
killForkitProcess();
|
2018-01-22 08:11:43 -06:00
|
|
|
TST_LOG("Communicating after kill.");
|
2016-11-11 21:38:14 -06:00
|
|
|
|
|
|
|
sendTextFrame(socket, "status", testname);
|
|
|
|
assertResponseString(socket, "status:", testname);
|
|
|
|
|
|
|
|
// respond close frame
|
2021-04-12 23:48:47 -05:00
|
|
|
socket->asyncShutdown();
|
2021-03-31 15:50:21 -05:00
|
|
|
LOK_ASSERT_MESSAGE("Expected successful disconnection of the WebSocket",
|
|
|
|
socket->waitForDisconnection(std::chrono::seconds(5)));
|
2016-11-11 21:38:14 -06:00
|
|
|
|
2021-11-18 06:08:14 -06:00
|
|
|
TST_LOG("Killing coolkit.");
|
2017-09-19 15:16:44 -05:00
|
|
|
killLoKitProcesses();
|
2021-11-18 06:08:14 -06:00
|
|
|
countCoolKitProcesses(0);
|
2018-01-22 08:11:43 -06:00
|
|
|
TST_LOG("Communicating after kill.");
|
2021-03-31 15:50:21 -05:00
|
|
|
socket = loadDocAndGetSession(_socketPoll, "empty.odt", _uri, testname);
|
2021-04-12 23:48:47 -05:00
|
|
|
socket->asyncShutdown();
|
2021-03-31 15:50:21 -05:00
|
|
|
LOK_ASSERT_MESSAGE("Expected successful disconnection of the WebSocket",
|
|
|
|
socket->waitForDisconnection(std::chrono::seconds(5)));
|
2016-11-11 21:38:14 -06:00
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT_FAIL(exc.displayText());
|
2016-11-11 21:38:14 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-26 11:57:08 -05:00
|
|
|
static void killPids(const std::set<pid_t> &pids, const std::string& testname)
|
2016-04-24 11:31:01 -05:00
|
|
|
{
|
2018-01-22 08:11:43 -06:00
|
|
|
TST_LOG("kill pids " << pids.size());
|
2017-09-19 15:16:44 -05:00
|
|
|
// Now kill them
|
2021-03-26 11:57:08 -05:00
|
|
|
for (pid_t pid : pids)
|
2016-04-24 11:31:01 -05:00
|
|
|
{
|
2018-01-22 08:11:43 -06:00
|
|
|
TST_LOG_BEGIN("Killing " << pid);
|
2017-09-19 15:16:44 -05:00
|
|
|
if (kill(pid, SIGKILL) == -1)
|
2018-07-11 04:58:10 -05:00
|
|
|
TST_LOG_APPEND("kill(" << pid << ", SIGKILL) failed: " << Util::symbolicErrno(errno) << ": " << std::strerror(errno));
|
2018-01-22 08:11:43 -06:00
|
|
|
TST_LOG_END;
|
2016-04-24 11:31:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-19 15:16:44 -05:00
|
|
|
void HTTPCrashTest::killLoKitProcesses()
|
|
|
|
{
|
2018-01-22 08:11:43 -06:00
|
|
|
killPids(getKitPids(), "killLoKitProcesses ");
|
2021-11-18 06:08:14 -06:00
|
|
|
InitialCoolKitCount = 1; // non-intuitive but it will arrive soon.
|
2017-09-19 15:16:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPCrashTest::killForkitProcess()
|
|
|
|
{
|
2018-01-22 08:11:43 -06:00
|
|
|
killPids(getForKitPids(), "killForkitProcess ");
|
2017-09-19 15:16:44 -05:00
|
|
|
}
|
|
|
|
|
2016-04-20 20:14:51 -05:00
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(HTTPCrashTest);
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|